From 5aba26f2cb7aa9609a3c3d2bd38a3942b6a378b8 Mon Sep 17 00:00:00 2001 From: nicm Date: Fri, 17 Apr 2020 08:03:22 +0000 Subject: [PATCH] Add a copy-command option and change copy-pipe and friends to pipe to it if used without arguments, allows all copy key bindings to be changed to pipe with one option. --- key-bindings.c | 24 ++++++++++++------------ options-table.c | 6 ++++++ tmux.1 | 10 +++++++--- window-copy.c | 22 +++++++++++++--------- 4 files changed, 38 insertions(+), 24 deletions(-) diff --git a/key-bindings.c b/key-bindings.c index 09f0e0b1..09a4eafa 100644 --- a/key-bindings.c +++ b/key-bindings.c @@ -333,10 +333,10 @@ key_bindings_init(void) "bind -n MouseDown2Pane select-pane -t=\\; if -F '#{||:#{pane_in_mode},#{mouse_any_flag}}' { send -M } { paste -p }", /* Mouse button 1 double click on pane. */ - "bind -n DoubleClick1Pane select-pane -t=\\; if -F '#{||:#{pane_in_mode},#{mouse_any_flag}}' { send -M } { copy-mode -H; send -X select-word; run -d0.3; send -X copy-selection-and-cancel }", + "bind -n DoubleClick1Pane select-pane -t=\\; if -F '#{||:#{pane_in_mode},#{mouse_any_flag}}' { send -M } { copy-mode -H; send -X select-word; run -d0.3; send -X copy-pipe-and-cancel }", /* Mouse button 1 triple click on pane. */ - "bind -n TripleClick1Pane select-pane -t=\\; if -F '#{||:#{pane_in_mode},#{mouse_any_flag}}' { send -M } { copy-mode -H; send -X select-line; run -d0.3; send -X copy-selection-and-cancel }", + "bind -n TripleClick1Pane select-pane -t=\\; if -F '#{||:#{pane_in_mode},#{mouse_any_flag}}' { send -M } { copy-mode -H; send -X select-line; run -d0.3; send -X copy-pipe-and-cancel }", /* Mouse button 1 drag on border. */ "bind -n MouseDrag1Border resize-pane -M", @@ -374,7 +374,7 @@ key_bindings_init(void) "bind -Tcopy-mode C-r command-prompt -ip'(search up)' -I'#{pane_search_string}' 'send -X search-backward-incremental \"%%%\"'", "bind -Tcopy-mode C-s command-prompt -ip'(search down)' -I'#{pane_search_string}' 'send -X search-forward-incremental \"%%%\"'", "bind -Tcopy-mode C-v send -X page-down", - "bind -Tcopy-mode C-w send -X copy-selection-and-cancel", + "bind -Tcopy-mode C-w send -X copy-pipe-and-cancel", "bind -Tcopy-mode Escape send -X cancel", "bind -Tcopy-mode Space send -X page-down", "bind -Tcopy-mode , send -X jump-reverse", @@ -393,11 +393,11 @@ key_bindings_init(void) "bind -Tcopy-mode End send -X end-of-line", "bind -Tcopy-mode MouseDown1Pane select-pane", "bind -Tcopy-mode MouseDrag1Pane select-pane\\; send -X begin-selection", - "bind -Tcopy-mode MouseDragEnd1Pane send -X copy-selection-and-cancel", + "bind -Tcopy-mode MouseDragEnd1Pane send -X copy-pipe-and-cancel", "bind -Tcopy-mode WheelUpPane select-pane\\; send -N5 -X scroll-up", "bind -Tcopy-mode WheelDownPane select-pane\\; send -N5 -X scroll-down", - "bind -Tcopy-mode DoubleClick1Pane select-pane\\; send -X select-word\\; run -d0.3\\; send -X copy-selection-and-cancel", - "bind -Tcopy-mode TripleClick1Pane select-pane\\; send -X select-line\\; run -d0.3\\; send -X copy-selection-and-cancel", + "bind -Tcopy-mode DoubleClick1Pane select-pane\\; send -X select-word\\; run -d0.3\\; send -X copy-pipe-and-cancel", + "bind -Tcopy-mode TripleClick1Pane select-pane\\; send -X select-line\\; run -d0.3\\; send -X copy-pipe-and-cancel", "bind -Tcopy-mode NPage send -X page-down", "bind -Tcopy-mode PPage send -X page-up", "bind -Tcopy-mode Up send -X cursor-up", @@ -423,7 +423,7 @@ key_bindings_init(void) "bind -Tcopy-mode M-m send -X back-to-indentation", "bind -Tcopy-mode M-r send -X middle-line", "bind -Tcopy-mode M-v send -X page-up", - "bind -Tcopy-mode M-w send -X copy-selection-and-cancel", + "bind -Tcopy-mode M-w send -X copy-pipe-and-cancel", "bind -Tcopy-mode 'M-{' send -X previous-paragraph", "bind -Tcopy-mode 'M-}' send -X next-paragraph", "bind -Tcopy-mode M-Up send -X halfpage-up", @@ -440,8 +440,8 @@ key_bindings_init(void) "bind -Tcopy-mode-vi C-b send -X page-up", "bind -Tcopy-mode-vi C-f send -X page-down", "bind -Tcopy-mode-vi C-h send -X cursor-left", - "bind -Tcopy-mode-vi C-j send -X copy-selection-and-cancel", - "bind -Tcopy-mode-vi Enter send -X copy-selection-and-cancel", + "bind -Tcopy-mode-vi C-j send -X copy-pipe-and-cancel", + "bind -Tcopy-mode-vi Enter send -X copy-pipe-and-cancel", "bind -Tcopy-mode-vi C-u send -X halfpage-up", "bind -Tcopy-mode-vi C-v send -X rectangle-toggle", "bind -Tcopy-mode-vi C-y send -X scroll-up", @@ -499,11 +499,11 @@ key_bindings_init(void) "bind -Tcopy-mode-vi % send -X next-matching-bracket", "bind -Tcopy-mode-vi MouseDown1Pane select-pane", "bind -Tcopy-mode-vi MouseDrag1Pane select-pane\\; send -X begin-selection", - "bind -Tcopy-mode-vi MouseDragEnd1Pane send -X copy-selection-and-cancel", + "bind -Tcopy-mode-vi MouseDragEnd1Pane send -X copy-pipe-and-cancel", "bind -Tcopy-mode-vi WheelUpPane select-pane\\; send -N5 -X scroll-up", "bind -Tcopy-mode-vi WheelDownPane select-pane\\; send -N5 -X scroll-down", - "bind -Tcopy-mode-vi DoubleClick1Pane select-pane\\; send -X select-word\\; run -d0.3\\; send -X copy-selection-and-cancel", - "bind -Tcopy-mode-vi TripleClick1Pane select-pane\\; send -X select-line\\; run -d0.3\\; send -X copy-selection-and-cancel", + "bind -Tcopy-mode-vi DoubleClick1Pane select-pane\\; send -X select-word\\; run -d0.3\\; send -X copy-pipe-and-cancel", + "bind -Tcopy-mode-vi TripleClick1Pane select-pane\\; send -X select-line\\; run -d0.3\\; send -X copy-pipe-and-cancel", "bind -Tcopy-mode-vi BSpace send -X cursor-left", "bind -Tcopy-mode-vi NPage send -X page-down", "bind -Tcopy-mode-vi PPage send -X page-up", diff --git a/options-table.c b/options-table.c index 33ee4402..1401f05d 100644 --- a/options-table.c +++ b/options-table.c @@ -198,6 +198,12 @@ const struct options_table_entry options_table[] = { .separator = "," }, + { .name = "copy-command", + .type = OPTIONS_TABLE_STRING, + .scope = OPTIONS_TABLE_SERVER, + .default_str = "" + }, + { .name = "default-terminal", .type = OPTIONS_TABLE_STRING, .scope = OPTIONS_TABLE_SERVER, diff --git a/tmux.1 b/tmux.1 index fd022c9e..28c8c9c3 100644 --- a/tmux.1 +++ b/tmux.1 @@ -1483,9 +1483,9 @@ The following commands are supported in copy mode: .It Li "clear-selection" Ta "Escape" Ta "C-g" .It Li "copy-end-of-line []" Ta "D" Ta "C-k" .It Li "copy-line []" Ta "" Ta "" -.It Li "copy-pipe []" Ta "" Ta "" -.It Li "copy-pipe-no-clear []" Ta "" Ta "" -.It Li "copy-pipe-and-cancel []" Ta "" Ta "" +.It Li "copy-pipe [] []" Ta "" Ta "" +.It Li "copy-pipe-no-clear [] []" Ta "" Ta "" +.It Li "copy-pipe-and-cancel [] []" Ta "" Ta "" .It Li "copy-selection []" Ta "" Ta "" .It Li "copy-selection-no-clear []" Ta "" Ta "" .It Li "copy-selection-and-cancel []" Ta "Enter" Ta "M-w" @@ -3090,6 +3090,10 @@ be set to .Ql screen , .Ql tmux or a derivative of them. +.It Ic copy-command Ar shell-command +Give the command to pipe to if the +.Ic copy-pipe +copy mode command is used without arguments. .It Ic escape-time Ar time Set the time in milliseconds for which .Nm diff --git a/window-copy.c b/window-copy.c index 8364ef11..bdf2c148 100644 --- a/window-copy.c +++ b/window-copy.c @@ -1728,11 +1728,10 @@ window_copy_cmd_copy_pipe_no_clear(struct window_copy_cmd_state *cs) if (cs->args->argc == 3) prefix = format_single(NULL, cs->args->argv[2], c, s, wl, wp); - if (s != NULL && *cs->args->argv[1] != '\0') { + if (s != NULL && cs->args->argc > 1 && *cs->args->argv[1] != '\0') command = format_single(NULL, cs->args->argv[1], c, s, wl, wp); - window_copy_copy_pipe(wme, s, prefix, command); - free(command); - } + window_copy_copy_pipe(wme, s, prefix, command); + free(command); free(prefix); return (WINDOW_COPY_CMD_NOTHING); @@ -2066,11 +2065,11 @@ static const struct { window_copy_cmd_copy_end_of_line }, { "copy-line", 0, 1, 0, window_copy_cmd_copy_line }, - { "copy-pipe-no-clear", 1, 2, 0, + { "copy-pipe-no-clear", 0, 2, 0, window_copy_cmd_copy_pipe_no_clear }, - { "copy-pipe", 1, 2, 0, + { "copy-pipe", 0, 2, 0, window_copy_cmd_copy_pipe }, - { "copy-pipe-and-cancel", 1, 2, 0, + { "copy-pipe-and-cancel", 0, 2, 0, window_copy_cmd_copy_pipe_and_cancel }, { "copy-selection-no-clear", 0, 1, 0, window_copy_cmd_copy_selection_no_clear }, @@ -3471,8 +3470,13 @@ window_copy_copy_pipe(struct window_mode_entry *wme, struct session *s, if (buf == NULL) return; - job = job_run(cmd, s, NULL, NULL, NULL, NULL, NULL, JOB_NOWAIT, -1, -1); - bufferevent_write(job_get_event(job), buf, len); + if (cmd == NULL || *cmd == '\0') + cmd = options_get_string(global_options, "copy-command"); + if (cmd != NULL && *cmd != '\0') { + job = job_run(cmd, s, NULL, NULL, NULL, NULL, NULL, JOB_NOWAIT, + -1, -1); + bufferevent_write(job_get_event(job), buf, len); + } window_copy_copy_buffer(wme, prefix, buf, len); }