abhinav / tmux-fastcopy

easymotion-style text copying for tmux.
GNU General Public License v2.0
66 stars 5 forks source link

fix: Unescaping certain regex options. #177

Closed yjyao closed 11 months ago

yjyao commented 11 months ago

Many regexes contain escaped characters like \s or \w. tmux-fastcopy queries the tmux environment for regex overwrites. Specifically, it uses the tmux show-options -g command. The output in turn escapes the backward slashes, turning \s into \\s. To correctly parse the regex overwrites, tmux-fastcopy needs to unescape these backward slashes too.

The tmux show-options -g command prints values in three forms:

Note that the output escapes backward slashes in all three forms:

$ tmux set @myopt '\hey' ; tmux show @myopt
@myopt \\hey

$ tmux set @myopt '\"hey' ; tmux show @myopt
@myopt '\\"hey'

$ tmux set @myopt '\ hey' ; tmux show @myopt
@myopt "\\ hey"

For many regexes, tmux would print the value unquoted. For example, the path regex built-in to tmux-fastcopy is like this:

$ tmux set-option @myopt '(\b([\w.-]+|~)?(/[\w.-]+)+\b)' ; tmux show @myopt
@myopt (\\b([\\w.-]+|~)?(/[\\w.-]+)+\\b)

Currently, tmux-fastcopy relies on strconv.Unquote in the tmuxopt.Loader.ReadValue function to unescape the backslashes. However, a caveat is that strconv.Unquote only performs if the given string is quoted. As a result, tmux-fastcopy never unescapes the regex from the last paragraph, and would fail to match it.

This commit fixes the problem by manually double-quoting unquoted strings, forcing strconv.Unquote to unescape them.

(The above results were tested with tmux 3.3a in bash 5.2.15(1)-release.)


TIP: Before this PR is merged (if ever), a workaround is to force tmux to double-quote the value for example by prefixing your regex with a "{0} which matches nothing.

abhinav commented 11 months ago

This is excellent! Thanks for the fix, and the thorough explanation. Much appreciated! I added a small integration test to verify this behavior with running copies of tmux, and that appears to be green for all versions of tmux we're testing against.

abhinav commented 11 months ago

Released in 0.14.1. Thanks!