feat(zsh): Handle multi-line history selection (#4595)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by LangLangBart · committed by GitHub · parent af2a81d · Browse files · 2 files·+111 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+111 added−6 removed
- tests:+78 added−0 removed
- other:+33 added−6 removed
Provenance
- Commit
cfc37ca- Tree
7411c8d- Author
LangLangBart <92653266+LangLangBart@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
af2a81d
2 files changed+111 added−6 removed
modified:shell/key-bindings.zsh+33 added-6 removed
126 unmodified linesExpand all12712812913013113213113213313413513613713813913914014114214314314414514614714814915015114814915215315415515615715815916016116216316416516616716816917017117217317417517617717817918018 unmodified lines126 unmodified linesExpand allfi
# CTRL-R - Paste the selected command from history into the command linefzf-history-widget() { local selected setopt localoptions noglobsubst noposixbuiltins pipefail no_aliases noglob nobash_rematch 2> /dev/null local selected extracted_with_perl=0 setopt localoptions noglobsubst noposixbuiltins pipefail no_aliases no_glob no_ksharrays extendedglob 2> /dev/null # Ensure the module is loaded if not already, and the required features, such # as the associative 'history' array, which maps event numbers to full history # lines, are set. Also, make sure Perl is installed for multi-line output. if zmodload -F zsh/parameter p:{commands,history} 2>/dev/null && (( ${+commands[perl]} )); then selected="$(printf '%s\t%s\000' "${(kv)history[@]}" | perl -0 -ne 'if (!$seen{(/^\s*[0-9]+\**\t(.*)/s, $1)}++) { s/\n/\n\t/g; print; }' | FZF_DEFAULT_OPTS=$(__fzf_defaults "" "-n2..,.. --scheme=history --bind=ctrl-r:toggle-sort,alt-r:toggle-raw --wrap-sign '\t↳ ' --highlight-line ${FZF_CTRL_R_OPTS-} --query=${(qqq)LBUFFER} +m --read0") \ FZF_DEFAULT_OPTS=$(__fzf_defaults "" "-n2..,.. --scheme=history --bind=ctrl-r:toggle-sort,alt-r:toggle-raw --wrap-sign '\t↳ ' --highlight-line --multi ${FZF_CTRL_R_OPTS-} --query=${(qqq)LBUFFER} --read0") \ FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd))" extracted_with_perl=1 else selected="$(fc -rl 1 | __fzf_exec_awk '{ cmd=$0; sub(/^[ \t]*[0-9]+\**[ \t]+/, "", cmd); if (!seen[cmd]++) print $0 }' | FZF_DEFAULT_OPTS=$(__fzf_defaults "" "-n2..,.. --scheme=history --bind=ctrl-r:toggle-sort,alt-r:toggle-raw --wrap-sign '\t↳ ' --highlight-line ${FZF_CTRL_R_OPTS-} --query=${(qqq)LBUFFER} +m") \ FZF_DEFAULT_OPTS=$(__fzf_defaults "" "-n2..,.. --scheme=history --bind=ctrl-r:toggle-sort,alt-r:toggle-raw --wrap-sign '\t↳ ' --highlight-line --multi ${FZF_CTRL_R_OPTS-} --query=${(qqq)LBUFFER}") \ FZF_DEFAULT_OPTS_FILE='' $(__fzfcmd))" fi local ret=$? local -a cmds # Avoid leaking auto assigned values when using backreferences '(#b)' local -a mbegin mend match if [ -n "$selected" ]; then if [[ $(__fzf_exec_awk '{print $1; exit}' <<< "$selected") =~ ^[1-9][0-9]* ]]; then zle vi-fetch-history -n $MATCH # Heuristic to check if the selected value is from history or a custom query if ((( extracted_with_perl )) && [[ $selected == <->$'\t'* ]]) || ((( ! extracted_with_perl )) && [[ $selected == [[:blank:]]#<->( |\* )* ]]); then # Split at newlines for line in ${(ps:\n:)selected}; do if (( extracted_with_perl )); then if [[ $line == (#b)(<->)(#B)$'\t'* ]]; then (( ${+history[${match[1]}]} )) && cmds+=("${history[${match[1]}]}") fi elif [[ $line == [[:blank:]]#(#b)(<->)(#B)( |\* )* ]]; then # Avoid $history array: lags behind 'fc' on foreign commands (*) # https://zsh.org/mla/users/2024/msg00692.html # Push BUFFER onto stack; fetch and save history entry from BUFFER; restore zle .push-line zle vi-fetch-history -n ${match[1]} (( ${#BUFFER} )) && cmds+=("${BUFFER}") BUFFER="" zle .get-line fi done if (( ${#cmds[@]} )); then # Join by newline after stripping trailing newlines from each command BUFFER="${(pj:\n:)${(@)cmds%%$'\n'#}}" CURSOR=${#BUFFER} fi else # selected is a custom query, not from history LBUFFER="$selected" fi fi18 unmodified linesmodified:test/test_shell_integration.rb+78 added
460 unmodified lines461 tmux.until { |lines| assert_equal "#{command} FZFFOOBAR", lines[-1] }462 tmux.send_keys 'C-c'463 end464 end465+466+ # Helper function to run test with Perl and again with Awk467+ def self.test_perl_and_awk(name, &block)468+ define_method("test_#{name}") do469+ instance_eval(&block)470+ end471+472+ define_method("test_#{name}_awk") do473+ tmux.send_keys "unset 'commands[perl]'", :Enter474+ tmux.prepare475+ # Verify perl is actually unset (0 = not found)476+ tmux.send_keys 'echo ${+commands[perl]}', :Enter477+ tmux.until { |lines| assert_equal '0', lines[-1] }478+ tmux.prepare479+ instance_eval(&block)480+ end481+ end482+483+ def prepare_ctrl_r_test484+ tmux.send_keys ':', :Enter485+ tmux.send_keys 'echo match-collision', :Enter486+ tmux.prepare487+ tmux.send_keys 'echo "line 1', :Enter, '2 line 2"', :Enter488+ tmux.prepare489+ tmux.send_keys 'echo "foo', :Enter, 'bar"', :Enter490+ tmux.prepare491+ tmux.send_keys 'echo "bar', :Enter, 'foo"', :Enter492+ tmux.prepare493+ tmux.send_keys 'echo "trailing_space "', :Enter494+ tmux.prepare495+ tmux.send_keys 'cat <<EOF | wc -c', :Enter, 'qux thud', :Enter, 'EOF', :Enter496+ tmux.prepare497+ tmux.send_keys 'C-l', 'C-r'498+ end499+500+ test_perl_and_awk 'ctrl_r_accept_or_print_query' do501+ set_var('FZF_CTRL_R_OPTS', '--bind enter:accept-or-print-query')502+ prepare_ctrl_r_test503+ tmux.until { |lines| assert_operator lines.match_count, :>, 0 }504+ tmux.send_keys '1 foobar'505+ tmux.until { |lines| assert_equal 0, lines.match_count }506+ tmux.send_keys :Enter507+ tmux.until { |lines| assert_equal '1 foobar', lines[-1] }508+ end509+510+ test_perl_and_awk 'ctrl_r_multiline_index_collision' do511+ # Leading number in multi-line history content is not confused with index512+ prepare_ctrl_r_test513+ tmux.send_keys "'line 1"514+ tmux.until { |lines| assert_equal 1, lines.match_count }515+ tmux.send_keys :Enter516+ tmux.until do |lines|517+ assert_equal ['echo "line 1', '2 line 2"'], lines[-2..]518+ end519+ end520+521+ test_perl_and_awk 'ctrl_r_multi_selection' do522+ prepare_ctrl_r_test523+ tmux.until { |lines| assert_operator lines.match_count, :>, 0 }524+ tmux.send_keys :BTab, :BTab, :BTab525+ tmux.until { |lines| assert_includes lines[-2], '(3)' }526+ tmux.send_keys :Enter527+ tmux.until do |lines|528+ assert_equal ['cat <<EOF | wc -c', 'qux thud', 'EOF', 'echo "trailing_space "', 'echo "bar', 'foo"'], lines[-6..]529+ end530+ end531+532+ test_perl_and_awk 'ctrl_r_no_multi_selection' do533+ set_var('FZF_CTRL_R_OPTS', '--no-multi')534+ prepare_ctrl_r_test535+ tmux.until { |lines| assert_operator lines.match_count, :>, 0 }536+ tmux.send_keys :BTab, :BTab, :BTab537+ tmux.until { |lines| refute_includes lines[-2], '(3)' }538+ tmux.send_keys :Enter539+ tmux.until do |lines|540+ assert_equal ['cat <<EOF | wc -c', 'qux thud', 'EOF'], lines[-3..]541+ end542+ end543 end544 545 class TestFish < TestBase546 include TestShell49 unmodified lines