Skip to content

Commit

Permalink
Display hyperlinks in copy mode and add copy_cursor_hyperlink format to
Browse files Browse the repository at this point in the history
get the hyperlink under the cursor.
  • Loading branch information
nicm committed Aug 27, 2024
1 parent d0c8124 commit 141cd78
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
16 changes: 14 additions & 2 deletions hyperlinks.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ struct hyperlinks {
u_int next_inner;
struct hyperlinks_by_inner_tree by_inner;
struct hyperlinks_by_uri_tree by_uri;
u_int references;
};

static int
Expand Down Expand Up @@ -206,6 +207,15 @@ hyperlinks_init(void)
hl->next_inner = 1;
RB_INIT(&hl->by_uri);
RB_INIT(&hl->by_inner);
hl->references = 1;
return (hl);
}

/* Copy hyperlink set. */
struct hyperlinks *
hyperlinks_copy(struct hyperlinks *hl)
{
hl->references++;
return (hl);
}

Expand All @@ -223,6 +233,8 @@ hyperlinks_reset(struct hyperlinks *hl)
void
hyperlinks_free(struct hyperlinks *hl)
{
hyperlinks_reset(hl);
free(hl);
if (--hl->references == 0) {
hyperlinks_reset(hl);
free(hl);
}
}
1 change: 1 addition & 0 deletions tmux.1
Original file line number Diff line number Diff line change
Expand Up @@ -5498,6 +5498,7 @@ The following variables are available, where appropriate:
.It Li "command_list_name" Ta "" Ta "Command name if listing commands"
.It Li "command_list_usage" Ta "" Ta "Command usage if listing commands"
.It Li "config_files" Ta "" Ta "List of configuration files loaded"
.It Li "copy_cursor_hyperlink" Ta "" Ta "Hyperlink under cursor in copy mode"
.It Li "copy_cursor_line" Ta "" Ta "Line the cursor is on in copy mode"
.It Li "copy_cursor_word" Ta "" Ta "Word under cursor in copy mode"
.It Li "copy_cursor_x" Ta "" Ta "Cursor X position in copy mode"
Expand Down
1 change: 1 addition & 0 deletions tmux.h
Original file line number Diff line number Diff line change
Expand Up @@ -3434,6 +3434,7 @@ u_int hyperlinks_put(struct hyperlinks *, const char *,
int hyperlinks_get(struct hyperlinks *, u_int,
const char **, const char **, const char **);
struct hyperlinks *hyperlinks_init(void);
struct hyperlinks *hyperlinks_copy(struct hyperlinks *);
void hyperlinks_reset(struct hyperlinks *);
void hyperlinks_free(struct hyperlinks *);

Expand Down
19 changes: 18 additions & 1 deletion window-copy.c
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,8 @@ window_copy_init(struct window_mode_entry *wme,
data->scroll_exit = args_has(args, 'e');
data->hide_position = args_has(args, 'H');

if (base->hyperlinks != NULL)
data->screen.hyperlinks = hyperlinks_copy(base->hyperlinks);
data->screen.cx = data->cx;
data->screen.cy = data->cy;
data->mx = data->cx;
Expand Down Expand Up @@ -764,6 +766,18 @@ window_copy_get_line(struct window_pane *wp, u_int y)
return (format_grid_line(gd, gd->hsize + y));
}

static void *
window_copy_cursor_hyperlink_cb(struct format_tree *ft)
{
struct window_pane *wp = format_get_pane(ft);
struct window_mode_entry *wme = TAILQ_FIRST(&wp->modes);
struct window_copy_mode_data *data = wme->data;
struct grid *gd = data->screen.grid;

return (format_grid_hyperlink(gd, data->cx, gd->hsize + data->cy,
&data->screen));
}

static void *
window_copy_cursor_word_cb(struct format_tree *ft)
{
Expand Down Expand Up @@ -833,6 +847,8 @@ window_copy_formats(struct window_mode_entry *wme, struct format_tree *ft)

format_add_cb(ft, "copy_cursor_word", window_copy_cursor_word_cb);
format_add_cb(ft, "copy_cursor_line", window_copy_cursor_line_cb);
format_add_cb(ft, "copy_cursor_hyperlink",
window_copy_cursor_hyperlink_cb);
}

static void
Expand Down Expand Up @@ -2486,7 +2502,8 @@ window_copy_cmd_refresh_from_pane(struct window_copy_cmd_state *cs)

screen_free(data->backing);
free(data->backing);
data->backing = window_copy_clone_screen(&wp->base, &data->screen, NULL, NULL, wme->swp != wme->wp);
data->backing = window_copy_clone_screen(&wp->base, &data->screen, NULL,
NULL, wme->swp != wme->wp);

window_copy_size_changed(wme);
return (WINDOW_COPY_CMD_REDRAW);
Expand Down

0 comments on commit 141cd78

Please sign in to comment.