From 8084a2c9e60b8cc0c32b55f23bfd7b839f082252 Mon Sep 17 00:00:00 2001 From: nicm Date: Fri, 21 Oct 2016 13:51:59 +0000 Subject: [PATCH] Add %%% to substitute with quotes escaped (convert " to \"). Use this for the prompts in copy mode. Fixes problems with jumping to ' reported by Theo Buehler. --- cmd.c | 18 +++++++++++++----- key-bindings.c | 24 ++++++++++++------------ tmux.1 | 6 +++++- 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/cmd.c b/cmd.c index a6f11b5f..3dca4c1f 100644 --- a/cmd.c +++ b/cmd.c @@ -653,8 +653,8 @@ char * cmd_template_replace(const char *template, const char *s, int idx) { char ch, *buf; - const char *ptr; - int replaced; + const char *ptr, *cp; + int replaced, quoted; size_t len; if (strchr(template, '%') == NULL) @@ -676,9 +676,17 @@ cmd_template_replace(const char *template, const char *s, int idx) } ptr++; - len += strlen(s); - buf = xrealloc(buf, len + 1); - strlcat(buf, s, len + 1); + quoted = (*ptr == '%'); + if (quoted) + ptr++; + + buf = xrealloc(buf, len + (strlen(s) * 2) + 1); + for (cp = s; *cp != '\0'; cp++) { + if (quoted && *cp == '"') + buf[len++] = '\\'; + buf[len++] = *cp; + } + buf[len] = '\0'; continue; } buf = xrealloc(buf, len + 2); diff --git a/key-bindings.c b/key-bindings.c index d7df60cc..d07c7b49 100644 --- a/key-bindings.c +++ b/key-bindings.c @@ -242,23 +242,23 @@ key_bindings_init(void) "bind -Tcopy-mode C-k send -X copy-end-of-line", "bind -Tcopy-mode C-n send -X cursor-down", "bind -Tcopy-mode C-p send -X cursor-up", - "bind -Tcopy-mode C-r command-prompt -p'search up' \"send -X search-backward '%%'\"", - "bind -Tcopy-mode C-s command-prompt -p'search down' \"send -X search-forward '%%'\"", + "bind -Tcopy-mode C-r command-prompt -p'search up' \"send -X search-backward \\\"%%%\\\"\"", + "bind -Tcopy-mode C-s command-prompt -p'search down' \"send -X search-forward \\\"%%%\\\"\"", "bind -Tcopy-mode C-v send -X page-down", "bind -Tcopy-mode C-w send -X copy-selection-and-cancel", "bind -Tcopy-mode Escape send -X cancel", "bind -Tcopy-mode Space send -X page-down", "bind -Tcopy-mode , send -X jump-reverse", "bind -Tcopy-mode \\; send -X jump-again", - "bind -Tcopy-mode F command-prompt -1p'jump backward' \"send -X jump-backward '%%'\"", + "bind -Tcopy-mode F command-prompt -1p'jump backward' \"send -X jump-backward \\\"%%%\\\"\"", "bind -Tcopy-mode N send -X search-reverse", "bind -Tcopy-mode R send -X rectangle-toggle", - "bind -Tcopy-mode T command-prompt -1p'jump to backward' \"send -X jump-to-backward '%%'\"", - "bind -Tcopy-mode f command-prompt -1p'jump forward' \"send -X jump-forward '%%'\"", + "bind -Tcopy-mode T command-prompt -1p'jump to backward' \"send -X jump-to-backward \\\"%%%\\\"\"", + "bind -Tcopy-mode f command-prompt -1p'jump forward' \"send -X jump-forward \\\"%%%\\\"\"", "bind -Tcopy-mode g command-prompt -p'goto line' \"send -X goto-line '%%'\"", "bind -Tcopy-mode n send -X search-again", "bind -Tcopy-mode q send -X cancel", - "bind -Tcopy-mode t command-prompt -1p'jump to forward' \"send -X jump-to-forward '%%'\"", + "bind -Tcopy-mode t command-prompt -1p'jump to forward' \"send -X jump-to-forward \\\"%%%\\\"\"", "bind -Tcopy-mode MouseDrag1Pane send -X begin-selection", "bind -Tcopy-mode MouseDragEnd1Pane send -X copy-selection-and-cancel", "bind -Tcopy-mode WheelUpPane send -N5 -X scroll-up", @@ -310,7 +310,7 @@ key_bindings_init(void) "bind -Tcopy-mode-vi Space send -X begin-selection", "bind -Tcopy-mode-vi '$' send -X end-of-line", "bind -Tcopy-mode-vi , send -X jump-reverse", - "bind -Tcopy-mode-vi / command-prompt -p'search down' \"send -X search-forward '%%'\"", + "bind -Tcopy-mode-vi / command-prompt -p'search down' \"send -X search-forward \\\"%%%\\\"\"", "bind -Tcopy-mode-vi 0 send -X start-of-line", "bind -Tcopy-mode-vi 1 command-prompt -Np'repeat' -I1 \"send -N '%%'\"", "bind -Tcopy-mode-vi 2 command-prompt -Np'repeat' -I2 \"send -N '%%'\"", @@ -323,12 +323,12 @@ key_bindings_init(void) "bind -Tcopy-mode-vi 9 command-prompt -Np'repeat' -I9 \"send -N '%%'\"", "bind -Tcopy-mode-vi : command-prompt -p'goto line' \"send -X goto-line '%%'\"", "bind -Tcopy-mode-vi \\; send -X jump-again", - "bind -Tcopy-mode-vi ? command-prompt -p'search up' \"send -X search-backward '%%'\"", + "bind -Tcopy-mode-vi ? command-prompt -p'search up' \"send -X search-backward \\\"%%%\\\"\"", "bind -Tcopy-mode-vi A send -X append-selection-and-cancel", "bind -Tcopy-mode-vi B send -X previous-space", "bind -Tcopy-mode-vi D send -X copy-end-of-line", "bind -Tcopy-mode-vi E send -X next-space-end", - "bind -Tcopy-mode-vi F command-prompt -1p'jump backward' \"send -X jump-backward '%%'\"", + "bind -Tcopy-mode-vi F command-prompt -1p'jump backward' \"send -X jump-backward \\\"%%%\\\"\"", "bind -Tcopy-mode-vi G send -X history-bottom", "bind -Tcopy-mode-vi H send -X top-line", "bind -Tcopy-mode-vi J send -X scroll-down", @@ -336,13 +336,13 @@ key_bindings_init(void) "bind -Tcopy-mode-vi L send -X bottom-line", "bind -Tcopy-mode-vi M send -X middle-line", "bind -Tcopy-mode-vi N send -X search-reverse", - "bind -Tcopy-mode-vi T command-prompt -1p'jump to backward' \"send -X jump-to-backward '%%'\"", + "bind -Tcopy-mode-vi T command-prompt -1p'jump to backward' \"send -X jump-to-backward \\\"%%%\\\"\"", "bind -Tcopy-mode-vi V send -X select-line", "bind -Tcopy-mode-vi W send -X next-space", "bind -Tcopy-mode-vi ^ send -X back-to-indentation", "bind -Tcopy-mode-vi b send -X previous-word", "bind -Tcopy-mode-vi e send -X next-word-end", - "bind -Tcopy-mode-vi f command-prompt -1p'jump forward' \"send -X jump-forward '%%'\"", + "bind -Tcopy-mode-vi f command-prompt -1p'jump forward' \"send -X jump-forward \\\"%%%\\\"\"", "bind -Tcopy-mode-vi g send -X history-top", "bind -Tcopy-mode-vi h send -X cursor-left", "bind -Tcopy-mode-vi j send -X cursor-down", @@ -351,7 +351,7 @@ key_bindings_init(void) "bind -Tcopy-mode-vi n send -X search-again", "bind -Tcopy-mode-vi o send -X other-end", "bind -Tcopy-mode-vi q send -X cancel", - "bind -Tcopy-mode-vi t command-prompt -1p'jump to forward' \"send -X jump-to-forward '%%'\"", + "bind -Tcopy-mode-vi t command-prompt -1p'jump to forward' \"send -X jump-to-forward \\\"%%%\\\"\"", "bind -Tcopy-mode-vi v send -X rectangle-toggle", "bind -Tcopy-mode-vi w send -X next-word", "bind -Tcopy-mode-vi { send -X previous-paragraph", diff --git a/tmux.1 b/tmux.1 index 419e3bbd..61612831 100644 --- a/tmux.1 +++ b/tmux.1 @@ -3147,6 +3147,7 @@ option. .Xc If this option is set, searches will wrap around the end of the pane contents. The default is on. +.Pp .It Xo Ic xterm-keys .Op Ic on | off .Xc @@ -3157,7 +3158,6 @@ will generate function key sequences; these have a number included to indicate modifiers such as Shift, Alt or Ctrl. The default is off. -.Pp .El .It Xo Ic show-options .Op Fl gqsvw @@ -3770,6 +3770,10 @@ Up to nine prompt responses may be replaced to .Ql %9 .Pc . +.Ql %%% +is like +.Ql %% +but any quotation marks are escaped. .Pp .Fl 1 makes the prompt only accept one key press, in this case the resulting input