Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

[PATCH] vcs_info: Escape '%' signs in payloads

Test case: a patch whose subject is '%Sfoo%sbar'.  ('S' and 's' are
expandos both in prompts and in the 'formats' style.)
 Doc/Zsh/contrib.yo                               | 17 +++++++++++++++++
 Etc/BUGS                                         |  7 -------
 Functions/VCS_Info/Backends/VCS_INFO_get_data_hg |  1 +
 Functions/VCS_Info/VCS_INFO_set-patch-format     | 14 ++++++++++++++
 README                                           |  9 +++++++++
 5 files changed, 41 insertions(+), 7 deletions(-)

diff --git a/Doc/Zsh/contrib.yo b/Doc/Zsh/contrib.yo
index a454f60..5d0696d 100644
--- a/Doc/Zsh/contrib.yo
+++ b/Doc/Zsh/contrib.yo
@@ -1344,6 +1344,12 @@ tt(branchformat), use tt(%%%%b). Sorry for this inconvenience, but it
 cannot be easily avoided. Luckily we do not clash with a lot of prompt
 expansions and this only needs to be done for those.
+When one of the tt(gen-applied-string), tt(gen-unapplied-string), and
+tt(set-patch-format) hooks is defined,
+applying tt(%)-escaping (`tt(foo=${foo//'%'/%%})') to the interpolated values
+for use in the prompt is the responsibility of those hooks (jointly);
+when neither of those hooks is defined, tt(vcs_info) handles escaping by itself.
+We regret this coupling, but it was required for backwards compatibility.
 subsect(Quilt Support)
@@ -1616,6 +1622,9 @@ top-most patch and so forth.
 When setting tt(ret) to non-zero, the string in
 tt(${hook_com[applied-string]}) will be
 available as tt(%p) in the tt(patch-format) and tt(nopatch-format) styles.
+This hook is, in concert with tt(set-patch-format), responsible for
+tt(%)-escaping that value for use in the prompt.
+(See the `Oddities' section.)
 Called in the tt(git) (with tt(stgit) or during rebase), and tt(hg) (with
@@ -1629,6 +1638,9 @@ the patch next-in-line to be applied and so forth.
 When setting tt(ret) to non-zero, the string in
 tt(${hook_com[unapplied-string]}) will be available as tt(%u) in the
 tt(patch-format) and tt(nopatch-format) styles.
+This hook is, in concert with tt(set-patch-format), responsible for
+tt(%)-escaping that value for use in the prompt.
+(See the `Oddities' section.)
 Called in the tt(hg) backend when tt(guards-string) is generated; the
@@ -1706,6 +1718,11 @@ controllable in addition to that.
 If tt(ret) is set to non-zero, the string in tt(${hook_com[patch-replace]})
 will be used unchanged instead of an expanded format from tt(patch-format) or
+This hook is, in concert with the tt(gen-applied-string) or
+tt(gen-unapplied-string) hooks if they are defined, responsible for
+tt(%)-escaping the final tt(patch-format) value for use in the prompt.
+(See the `Oddities' section.)
 Called each time before a `tt(vcs_info_msg_)var(N)tt(_)' message is set.
diff --git a/Etc/BUGS b/Etc/BUGS
index a351464..008e337 100644
--- a/Etc/BUGS
+++ b/Etc/BUGS
@@ -15,13 +15,6 @@ It is currently impossible to time builtins.
 40106: The comp* completion-related builtins (compadd, compset, etc) are
 run with $_comp_options in effect, rather than the user's options.
-40240: vcs_info: percent escapes in payloads are interpreted
-Example: hg branch names and quilt patch subjects that contain the literal
-string '%F{blue}', cause $vcs_info_msg_N_ to be rendered in blue.
-40240 has a patch, but 40241 explains why that patch is incomplete.
 users/20807: vcs_info quilt 'addon' mode: hook lookup context specifies
 the underlying VCS but not whether quilt is used.
diff --git a/Functions/VCS_Info/Backends/VCS_INFO_get_data_hg b/Functions/VCS_Info/Backends/VCS_INFO_get_data_hg
index 143eb42..d403012 100644
--- a/Functions/VCS_Info/Backends/VCS_INFO_get_data_hg
+++ b/Functions/VCS_Info/Backends/VCS_INFO_get_data_hg
@@ -202,6 +202,7 @@ if zstyle -T ":vcs_info:${vcs}:${usercontext}:${rrn}" get-mq \
     if VCS_INFO_hook 'gen-mqguards-string' "${mqguards[@]}"; then
+        # TODO: %-escape extra_zformats[g:...] value
diff --git a/Functions/VCS_Info/VCS_INFO_set-patch-format b/Functions/VCS_Info/VCS_INFO_set-patch-format
index c5da368..cdf2d30 100644
--- a/Functions/VCS_Info/VCS_INFO_set-patch-format
+++ b/Functions/VCS_Info/VCS_INFO_set-patch-format
@@ -18,12 +18,15 @@
 # - $hook_com is overwritten and the keys 'applied', 'applied-n',
 #   'unapplied', 'unapplied-n', 'all-n' are set.
+    local applied_needs_escaping='unknown'
+    local unapplied_needs_escaping='unknown'
     if VCS_INFO_hook 'gen-applied-string' "${(@P)1}"; then
         if (( ${(P)#1} )); then
+        applied_needs_escaping='yes'
@@ -32,6 +35,7 @@
     if VCS_INFO_hook 'gen-unapplied-string' "${(@P)3}"; then
+        unapplied_needs_escaping='yes'
@@ -54,12 +58,22 @@
     hook_com[all-n]=$(( ${hook_com[applied-n]} + ${hook_com[unapplied-n]} ))
     hook_com+=( ${7:+"${(@kvP)7}"} )
     if VCS_INFO_hook 'set-patch-format' "${(P)6}"; then
+        # Escape the value for use in $PS1
+        if [[ $applied_needs_escaping == 'yes' ]]; then
+          hook_com[applied]=${hook_com[applied]//'%'/%%}
+        fi
+        if [[ $unapplied_needs_escaping == 'yes' ]]; then
+          hook_com[unapplied]=${hook_com[unapplied]//'%'/%%}
+        fi
         zformat -f REPLY "${(P)6}" "p:${hook_com[applied]}" "u:${hook_com[unapplied]}" \
                                         "n:${hook_com[applied-n]}" "c:${hook_com[unapplied-n]}" \
                                         "a:${hook_com[all-n]}" \
+        unset applied_needs_escaping unapplied_needs_escaping # the hook deals with escaping
diff --git a/README b/README
index 26eb245..594b6f1 100644
--- a/README
+++ b/README
@@ -62,6 +62,15 @@ Note that functions including a non-leading / behave as before,
 e.g. if `dir/name' is found anywhere under a directory in $fpath it is
 loaded as a function named `dir/name'.
+3) vcs_info: When neither a set-patch-format nor a gen-applied-string
+(resp. gen-unapplied-string) hook is set, vcs_info now '%'-escapes the
+applied-string (resp. unapplied-string) before interpolating it into the
+patch-format string, to prevent literal `%' signs in the interpolated
+value from being interpreted as prompt escape sequences.  If you use
+${vcs_info_msg_0_} in a context other than the shell prompt, you may need
+to undo the escaping with:
+    print -v vcs_info_msg_0_ -Pr -- "${vcs_info_msg_0_}"
 Incompatibilities between 5.0.8 and 5.3

Messages sorted by: Reverse Date, Date, Thread, Author