[vim] Add 'exit' callback

A spec can have `exit` callback that is called with the exit status of fzf.
This can be used to clean up temporary resources or restore the original
state when fzf is closed without a selection.
This commit is contained in:
Junegunn Choi 2024-08-19 20:51:08 +09:00
parent 5cce17e80a
commit 8cb59e6fca
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627
4 changed files with 15 additions and 15 deletions

View File

@ -23,6 +23,8 @@ CHANGELOG
fzf --preview "printf '<< \e]8;;http://github.com/junegunn/fzf\e\\Link to \e[32mfz\e[0mf\e]8;;\e\\ >>'" fzf --preview "printf '<< \e]8;;http://github.com/junegunn/fzf\e\\Link to \e[32mfz\e[0mf\e]8;;\e\\ >>'"
``` ```
- [vim] A spec can have `exit` callback that is called with the exit status of fzf
- This can be used to clean up temporary resources or restore the original state when fzf is closed without a selection
- Fixed `--tmux bottom` when the status line is not at the bottom - Fixed `--tmux bottom` when the status line is not at the bottom
- Fixed extra scroll offset in multi-line mode (`--read0` or `--wrap`) - Fixed extra scroll offset in multi-line mode (`--read0` or `--wrap`)
- Added fallback `ps` command for `kill` completion on Cygwin - Added fallback `ps` command for `kill` completion on Cygwin

View File

@ -289,8 +289,9 @@ The following table summarizes the available options.
| `source` | string | External command to generate input to fzf (e.g. `find .`) | | `source` | string | External command to generate input to fzf (e.g. `find .`) |
| `source` | list | Vim list as input to fzf | | `source` | list | Vim list as input to fzf |
| `sink` | string | Vim command to handle the selected item (e.g. `e`, `tabe`) | | `sink` | string | Vim command to handle the selected item (e.g. `e`, `tabe`) |
| `sink` | funcref | Reference to function to process each selected item | | `sink` | funcref | Function to be called with each selected item |
| `sinklist` (or `sink*`) | funcref | Similar to `sink`, but takes the list of output lines at once | | `sinklist` (or `sink*`) | funcref | Similar to `sink`, but takes the list of output lines at once |
| `exit` | funcref | Function to be called with the exit status of fzf (e.g. 0, 1, 2, 130) |
| `options` | string/list | Options to fzf | | `options` | string/list | Options to fzf |
| `dir` | string | Working directory | | `dir` | string | Working directory |
| `up`/`down`/`left`/`right` | number/string | (Layout) Window position and size (e.g. `20`, `50%`) | | `up`/`down`/`left`/`right` | number/string | (Layout) Window position and size (e.g. `20`, `50%`) |

View File

@ -306,8 +306,9 @@ The following table summarizes the available options.
`source` | string | External command to generate input to fzf (e.g. `find .` ) `source` | string | External command to generate input to fzf (e.g. `find .` )
`source` | list | Vim list as input to fzf `source` | list | Vim list as input to fzf
`sink` | string | Vim command to handle the selected item (e.g. `e` , `tabe` ) `sink` | string | Vim command to handle the selected item (e.g. `e` , `tabe` )
`sink` | funcref | Reference to function to process each selected item `sink` | funcref | Function to be called with each selected item
`sinklist` (or `sink*` ) | funcref | Similar to `sink` , but takes the list of output lines at once `sinklist` (or `sink*` ) | funcref | Similar to `sink` , but takes the list of output lines at once
`exit` | funcref | Function to be called with the exit status of fzf (e.g. 0, 1, 2, 130)
`options` | string/list | Options to fzf `options` | string/list | Options to fzf
`dir` | string | Working directory `dir` | string | Working directory
`up` / `down` / `left` / `right` | number/string | (Layout) Window position and size (e.g. `20` , `50%` ) `up` / `down` / `left` / `right` | number/string | (Layout) Window position and size (e.g. `20` , `50%` )

View File

@ -665,21 +665,17 @@ else
let s:launcher = function('s:xterm_launcher') let s:launcher = function('s:xterm_launcher')
endif endif
function! s:exit_handler(code, command, ...) function! s:exit_handler(dict, code, command, ...)
if a:code == 130 if has_key(a:dict, 'exit')
return 0 call a:dict.exit(a:code)
elseif has('nvim') && a:code == 129 endif
" When deleting the terminal buffer while fzf is still running, if a:code == 2
" Nvim sends SIGHUP.
return 0
elseif a:code > 1
call s:error('Error running ' . a:command) call s:error('Error running ' . a:command)
if !empty(a:000) if !empty(a:000)
sleep sleep
endif endif
return 0
endif endif
return 1 return a:code
endfunction endfunction
function! s:execute(dict, command, use_height, temps) abort function! s:execute(dict, command, use_height, temps) abort
@ -731,7 +727,7 @@ function! s:execute(dict, command, use_height, temps) abort
let exit_status = v:shell_error let exit_status = v:shell_error
redraw! redraw!
let lines = s:collect(a:temps) let lines = s:collect(a:temps)
return s:exit_handler(exit_status, command) ? lines : [] return s:exit_handler(a:dict, exit_status, command) == 0 ? lines : []
endfunction endfunction
function! s:execute_tmux(dict, command, temps) abort function! s:execute_tmux(dict, command, temps) abort
@ -746,7 +742,7 @@ function! s:execute_tmux(dict, command, temps) abort
let exit_status = v:shell_error let exit_status = v:shell_error
redraw! redraw!
let lines = s:collect(a:temps) let lines = s:collect(a:temps)
return s:exit_handler(exit_status, command) ? lines : [] return s:exit_handler(a:dict, exit_status, command) == 0 ? lines : []
endfunction endfunction
function! s:calc_size(max, val, dict) function! s:calc_size(max, val, dict)
@ -912,7 +908,7 @@ function! s:execute_term(dict, command, temps) abort
endif endif
let lines = s:collect(self.temps) let lines = s:collect(self.temps)
if !s:exit_handler(a:code, self.command, 1) if s:exit_handler(self.dict, a:code, self.command, 1) != 0
return return
endif endif