From 347c4b26258bf3e20777c9a1a6d1258e58a3eba3 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Sat, 22 May 2021 13:13:55 +0900 Subject: Add 'unbind' action Fix #2486 --- ADVANCED.md | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ CHANGELOG.md | 16 ++++++++++++++++ man/man1/fzf.1 | 3 ++- src/options.go | 18 +++++++++++++++--- src/terminal.go | 6 ++++++ test/test_go.rb | 11 +++++++++++ 6 files changed, 101 insertions(+), 4 deletions(-) diff --git a/ADVANCED.md b/ADVANCED.md index cfd60fdd..fb502ca2 100644 --- a/ADVANCED.md +++ b/ADVANCED.md @@ -16,6 +16,7 @@ Advanced fzf examples * [Ripgrep integration](#ripgrep-integration) * [Using fzf as the secondary filter](#using-fzf-as-the-secondary-filter) * [Using fzf as interative Ripgrep launcher](#using-fzf-as-interative-ripgrep-launcher) + * [Switching to fzf-only search mode](#switching-to-fzf-only-search-mode) * [Log tailing](#log-tailing) * [Key bindings for git objects](#key-bindings-for-git-objects) * [Files listed in `git status`](#files-listed-in-git-status) @@ -354,6 +355,56 @@ IFS=: read -ra selected < <( reduce the number of intermediate Ripgrep processes while we're typing in a query. +### Switching to fzf-only search mode + +*(Requires fzf 0.27.1 or above)* + +In the previous example, we lost fuzzy matching capability as we completely +delegated search functionality to Ripgrep. But we can dynamically switch to +fzf-only search mode by *"unbinding"* `reload` action from `change` event. + +```sh +#!/usr/bin/env bash + +# Two-phase filtering with Ripgrep and fzf +# +# 1. Search for text in files using Ripgrep +# 2. Interactively restart Ripgrep with reload action +# * Press alt-enter to switch to fzf-only filtering +# 3. Open the file in Vim +RG_PREFIX="rg --column --line-number --no-heading --color=always --smart-case " +INITIAL_QUERY="${*:-}" +IFS=: read -ra selected < <( + FZF_DEFAULT_COMMAND="$RG_PREFIX $(printf %q "$INITIAL_QUERY")" \ + fzf --ansi \ + --color "hl:-1:underline,hl+:-1:underline:reverse" \ + --disabled --query "$INITIAL_QUERY" \ + --bind "change:reload:sleep 0.1; $RG_PREFIX {q} || true" \ + --bind "alt-enter:unbind(change,alt-enter)+change-prompt(2. fzf> )+enable-search+clear-query" \ + --prompt '1. ripgrep> ' \ + --delimiter : \ + --preview 'bat --color=always {1} --highlight-line {2}' \ + --preview-window 'up,60%,border-bottom,+{2}+3/3,~3' +) +[ -n "${selected[0]}" ] && vim "${selected[0]}" "+${selected[1]}" +``` + +* Phase 1. Filtering with Ripgrep +![image](https://user-images.githubusercontent.com/700826/119213880-735e8a80-bafd-11eb-8493-123e4be24fbc.png) +* Phase 2. Filtering with fzf +![image](https://user-images.githubusercontent.com/700826/119213887-7e191f80-bafd-11eb-98c9-71a1af9d7aab.png) + +- We added `--prompt` option to show that fzf is initially running in "Ripgrep + launcher mode". +- We added `alt-enter` binding that + 1. unbinds `change` event, so Ripgrep is no longer restarted on key press + 2. changes the prompt to `2. fzf>` + 3. enables search functionality of fzf + 4. clears the current query string that was used to start Ripgrep process + 5. and unbinds `alt-enter` itself as this is a one-off event +- We reverted `--color` option for customizing how the matching chunks are + displayed in the second phase + Log tailing ----------- diff --git a/CHANGELOG.md b/CHANGELOG.md index b9683721..c5f7f509 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,22 @@ CHANGELOG ========= +0.27.1 +------ +- Added `unbind` action. In the following Ripgrep launcher example, you can + use `unbind(reload)` to switch to fzf-only filtering mode. + - See https://github.com/junegunn/fzf/blob/master/ADVANCED.md#switching-to-fzf-only-search-mode +- Vim plugin + - Vim plugin will stop immediately even when the source command hasn't finished + ```vim + " fzf will read the stream file while allowing other processes to append to it + call fzf#run({'source': 'cat /dev/null > /tmp/stream; tail -f /tmp/stream'}) + ``` + - It is now possible to open popup window relative to the currrent window + ```vim + let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.6, 'relative': v:true, 'yoffset': 1.0 } } + ``` + 0.27.0 ------ - More border options for `--preview-window` diff --git a/man/man1/fzf.1 b/man/man1/fzf.1 index 8a7dcba4..3e835b99 100644 --- a/man/man1/fzf.1 +++ b/man/man1/fzf.1 @@ -21,7 +21,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. .. -.TH fzf 1 "Apr 2021" "fzf 0.27.0" "fzf - a command-line fuzzy finder" +.TH fzf 1 "May 2021" "fzf 0.27.1" "fzf - a command-line fuzzy finder" .SH NAME fzf - a command-line fuzzy finder @@ -852,6 +852,7 @@ A key or an event can be bound to one or more of the following actions. \fBtoggle-search\fR (toggle search functionality) \fBtoggle-sort\fR \fBtoggle+up\fR \fIbtab (shift-tab)\fR + \fBunbind(...)\fR (unbind bindings) \fBunix-line-discard\fR \fIctrl-u\fR \fBunix-word-rubout\fR \fIctrl-w\fR \fBup\fR \fIctrl-k ctrl-p up\fR diff --git a/src/options.go b/src/options.go index dc40fdc7..7070bdf4 100644 --- a/src/options.go +++ b/src/options.go @@ -748,7 +748,7 @@ func init() { // Backreferences are not supported. // "~!@#$%^&*;/|".each_char.map { |c| Regexp.escape(c) }.map { |c| "#{c}[^#{c}]*#{c}" }.join('|') executeRegexp = regexp.MustCompile( - `(?si)[:+](execute(?:-multi|-silent)?|reload|preview|change-prompt):.+|[:+](execute(?:-multi|-silent)?|reload|preview|change-prompt)(\([^)]*\)|\[[^\]]*\]|~[^~]*~|![^!]*!|@[^@]*@|\#[^\#]*\#|\$[^\$]*\$|%[^%]*%|\^[^\^]*\^|&[^&]*&|\*[^\*]*\*|;[^;]*;|/[^/]*/|\|[^\|]*\|)`) + `(?si)[:+](execute(?:-multi|-silent)?|reload|preview|change-prompt|unbind):.+|[:+](execute(?:-multi|-silent)?|reload|preview|change-prompt|unbind)(\([^)]*\)|\[[^\]]*\]|~[^~]*~|![^!]*!|@[^@]*@|\#[^\#]*\#|\$[^\$]*\$|%[^%]*%|\^[^\^]*\^|&[^&]*&|\*[^\*]*\*|;[^;]*;|/[^/]*/|\|[^\|]*\|)`) } func parseKeymap(keymap map[tui.Event][]action, str string) { @@ -762,6 +762,8 @@ func parseKeymap(keymap map[tui.Event][]action, str string) { prefix = symbol + "reload" } else if strings.HasPrefix(src[1:], "preview") { prefix = symbol + "preview" + } else if strings.HasPrefix(src[1:], "unbind") { + prefix = symbol + "unbind" } else if strings.HasPrefix(src[1:], "change-prompt") { prefix = symbol + "change-prompt" } else if src[len(prefix)] == '-' { @@ -957,6 +959,8 @@ func parseKeymap(keymap map[tui.Event][]action, str string) { offset = len("preview") case actChangePrompt: offset = len("change-prompt") + case actUnbind: + offset = len("unbind") case actExecuteSilent: offset = len("execute-silent") case actExecuteMulti: @@ -964,15 +968,21 @@ func parseKeymap(keymap map[tui.Event][]action, str string) { default: offset = len("execute") } + var actionArg string if spec[offset] == ':' { if specIndex == len(specs)-1 { - actions = append(actions, action{t: t, a: spec[offset+1:]}) + actionArg = spec[offset+1:] + actions = append(actions, action{t: t, a: actionArg}) } else { prevSpec = spec + "+" continue } } else { - actions = append(actions, action{t: t, a: spec[offset+1 : len(spec)-1]}) + actionArg = spec[offset+1 : len(spec)-1] + actions = append(actions, action{t: t, a: actionArg}) + } + if t == actUnbind { + parseKeyChords(actionArg, "unbind target required") } } } @@ -994,6 +1004,8 @@ func isExecuteAction(str string) actionType { switch prefix { case "reload": return actReload + case "unbind": + return actUnbind case "preview": return actPreview case "change-prompt": diff --git a/src/terminal.go b/src/terminal.go index aabeb07c..d393b610 100644 --- a/src/terminal.go +++ b/src/terminal.go @@ -284,6 +284,7 @@ const ( actEnableSearch actSelect actDeselect + actUnbind ) type placeholderFlags struct { @@ -2657,6 +2658,11 @@ func (t *Terminal) Loop() { command := t.replacePlaceholder(a.a, false, string(t.input), list) newCommand = &command } + case actUnbind: + keys := parseKeyChords(a.a, "PANIC") + for key := range keys { + delete(t.keymap, key) + } } return true } diff --git a/test/test_go.rb b/test/test_go.rb index 7ec519b1..60f14a4b 100755 --- a/test/test_go.rb +++ b/test/test_go.rb @@ -2042,6 +2042,17 @@ class TestGoFZF < TestBase tmux.send_keys 'C-K' tmux.until { |lines| assert_equal(%w[1 2 3 4 5], top5[lines]) } end + + def test_unbind + tmux.send_keys "seq 100 | #{FZF} --bind 'c:clear-query,d:unbind(c,d)'", :Enter + tmux.until { |lines| assert_equal 100, lines.item_count } + tmux.send_keys 'ab' + tmux.until { |lines| assert_equal '> ab', lines[-1] } + tmux.send_keys 'c' + tmux.until { |lines| assert_equal '>', lines[-1] } + tmux.send_keys 'dabcd' + tmux.until { |lines| assert_equal '> abcd', lines[-1] } + end end module TestShell -- cgit v1.2.3