Skip to content
Myr

[vim] Add fzf#wrap helper function

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Close #627

authored by Junegunn Choi · committed by Junegunn Choi · parent a7c9c08 · Browse files · 3 files+188 added−50 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+188 added−50 removed

  • tests:+74 added−0 removed
  • other:+114 added−50 removed

3 files+188 added−50 removed

modified:plugin/fzf.vim+100 added-47 removed
20 unmodified lines
21" OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION22" WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.2324let s:default_layout = { 'down': '~40%' }25let s:layout_keys = ['window', 'up', 'down', 'left', 'right']26let s:fzf_go = expand('<sfile>:h:h').'/bin/fzf'27let s:install = expand('<sfile>:h:h').'/install'28let s:installed = 029let s:fzf_tmux = expand('<sfile>:h:h').'/bin/fzf-tmux'
74 unmodified lines
104 echom a:msg105 echohl None106endfunction107108function! s:has_any(dict, keys)109 for key in a:keys110 if has_key(a:dict, key)111 return 1112 endif113 endfor114 return 0115endfunction116117function! s:open(cmd, target)118 if stridx('edit', a:cmd) == 0 && fnamemodify(a:target, ':p') ==# expand('%:p')119 return120 endif121 execute a:cmd s:escape(a:target)122endfunction123124function! s:common_sink(action, lines) abort125 if len(a:lines) < 2126 return127 endif128 let key = remove(a:lines, 0)129 let cmd = get(a:action, key, 'e')130 if len(a:lines) > 1131 augroup fzf_swap132 autocmd SwapExists * let v:swapchoice='o'133 \| call s:warn('fzf: E325: swap file exists: '.expand('<afile>'))134 augroup END135 endif136 try137 let empty = empty(expand('%')) && line('$') == 1 && empty(getline(1)) && !&modified138 let autochdir = &autochdir139 set noautochdir140 for item in a:lines141 if empty142 execute 'e' s:escape(item)143 let empty = 0144 else145 call s:open(cmd, item)146 endif147 if exists('#BufEnter') && isdirectory(item)148 doautocmd BufEnter149 endif150 endfor151 finally152 let &autochdir = autochdir153 silent! autocmd! fzf_swap154 endtry155endfunction156157" name string, [opts dict, [fullscreen boolean]]158function! fzf#wrap(name, ...)159 if type(a:name) != type('')160 throw 'invalid name type: string expected'161 endif162 let opts = copy(get(a:000, 0, {}))163 let bang = get(a:000, 1, 0)164165 " Layout: g:fzf_layout (and deprecated g:fzf_height)166 if bang167 for key in s:layout_keys168 if has_key(opts, key)169 call remove(opts, key)170 endif171 endfor172 elseif !s:has_any(opts, s:layout_keys)173 if !exists('g:fzf_layout') && exists('g:fzf_height')174 let opts.down = g:fzf_height175 else176 let opts = extend(opts, get(g:, 'fzf_layout', s:default_layout))177 endif178 endif179180 " History: g:fzf_history_dir181 let opts.options = get(opts, 'options', '')182 if len(get(g:, 'fzf_history_dir', ''))183 let dir = expand(g:fzf_history_dir)184 if !isdirectory(dir)185 call mkdir(dir, 'p')186 endif187 let opts.options = join(['--history', s:escape(dir.'/'.a:name), opts.options])188 endif189190 " Action: g:fzf_action191 if !s:has_any(opts, ['sink', 'sink*'])192 let opts._action = get(g:, 'fzf_action', s:default_action)193 let opts.options .= ' --expect='.join(keys(opts._action), ',')194 function! opts.sink(lines) abort195 return s:common_sink(self._action, a:lines)196 endfunction197 let opts['sink*'] = remove(opts, 'sink')198 endif199200 return opts201endfunction202203function! fzf#run(...) abort204try205 let oshell = &shell206 set shell=sh
25 unmodified lines
232 let temps.input = tempname()233 call writefile(source, temps.input)234 let prefix = 'cat '.s:shellesc(temps.input).'|'235 else140 throw 'Invalid source type'236 throw 'invalid source type'237 endif238 else239 let prefix = ''240 endif
289 unmodified lines
530 endif531endfunction532533let s:default_action = {438 \ 'ctrl-m': 'e',534 \ 'ctrl-t': 'tab split',535 \ 'ctrl-x': 'split',536 \ 'ctrl-v': 'vsplit' }537443function! s:cmd_callback(lines) abort444 if empty(a:lines)445 return446 endif447 let key = remove(a:lines, 0)448 let cmd = get(s:action, key, 'e')449 if len(a:lines) > 1450 augroup fzf_swap451 autocmd SwapExists * let v:swapchoice='o'452 \| call s:warn('fzf: E325: swap file exists: '.expand('<afile>'))453 augroup END454 endif455 try456 let empty = empty(expand('%')) && line('$') == 1 && empty(getline(1)) && !&modified457 let autochdir = &autochdir458 set noautochdir459 for item in a:lines460 if empty461 execute 'e' s:escape(item)462 let empty = 0463 else464 execute cmd s:escape(item)465 endif466 if exists('#BufEnter') && isdirectory(item)467 doautocmd BufEnter468 endif469 endfor470 finally471 let &autochdir = autochdir472 silent! autocmd! fzf_swap473 endtry474endfunction475538function! s:cmd(bang, ...) abort477 let s:action = get(g:, 'fzf_action', s:default_action)478 let args = extend(['--expect='.join(keys(s:action), ',')], a:000)539 let args = copy(a:000)540 let opts = {}480 if len(args) > 0 && isdirectory(expand(args[-1]))541 if len(args) && isdirectory(expand(args[-1]))542 let opts.dir = substitute(remove(args, -1), '\\\(["'']\)', '\1', 'g')543 endif483 if !a:bang484 " For backward compatibility485 if !exists('g:fzf_layout') && exists('g:fzf_height')486 let opts.down = g:fzf_height487 else488 let opts = extend(opts, get(g:, 'fzf_layout', s:default_layout))489 endif490 endif491 call fzf#run(extend({'options': join(args), 'sink*': function('<sid>cmd_callback')}, opts))544 call fzf#run(fzf#wrap('FZF', extend({'options': join(args)}, opts), a:bang))545endfunction546547command! -nargs=* -complete=dir -bang FZF call s:cmd(<bang>0, <f-args>)548
3 unmodified lines
modified:test/fzf.vader+74 added
1Execute (Setup):2 let g:dir = fnamemodify(g:vader_file, ':p:h')3 unlet! g:fzf_layout g:fzf_action g:fzf_history_dir4 Log 'Test directory: ' . g:dir5 Save &acd67Execute (fzf#run with dir option):
61 unmodified lines
69 call fzf#run({'source': ['/foobar'], 'sink': 'e', 'dir': '/', 'options': '-1'})70 " Working directory changed due to &acd71 AssertEqual '/', getcwd()7273Execute (fzf#wrap):74 AssertThrows fzf#wrap({'foo': 'bar'})7576 let opts = fzf#wrap('foobar')77 Log opts78 AssertEqual '~40%', opts.down79 Assert opts.options =~ '--expect='80 Assert !has_key(opts, 'sink')81 Assert has_key(opts, 'sink*')8283 let opts = fzf#wrap('foobar', {}, 0)84 Log opts85 AssertEqual '~40%', opts.down8687 let opts = fzf#wrap('foobar', {}, 1)88 Log opts89 Assert !has_key(opts, 'down')9091 let opts = fzf#wrap('foobar', {'down': '50%'})92 Log opts93 AssertEqual '50%', opts.down9495 let opts = fzf#wrap('foobar', {'down': '50%'}, 1)96 Log opts97 Assert !has_key(opts, 'down')9899 let opts = fzf#wrap('foobar', {'sink': 'e'})100 Log opts101 AssertEqual 'e', opts.sink102 Assert !has_key(opts, 'sink*')103104 let opts = fzf#wrap('foobar', {'options': '--reverse'})105 Log opts106 Assert opts.options =~ '--expect='107 Assert opts.options =~ '--reverse'108109 let g:fzf_layout = {'window': 'enew'}110 let opts = fzf#wrap('foobar')111 Log opts112 AssertEqual 'enew', opts.window113114 let opts = fzf#wrap('foobar', {}, 1)115 Log opts116 Assert !has_key(opts, 'window')117118 let opts = fzf#wrap('foobar', {'right': '50%'})119 Log opts120 Assert !has_key(opts, 'window')121 AssertEqual '50%', opts.right122123 let opts = fzf#wrap('foobar', {'right': '50%'}, 1)124 Log opts125 Assert !has_key(opts, 'window')126 Assert !has_key(opts, 'right')127128 let g:fzf_action = {'a': 'tabe'}129 let opts = fzf#wrap('foobar')130 Log opts131 Assert opts.options =~ '--expect=a'132 Assert !has_key(opts, 'sink')133 Assert has_key(opts, 'sink*')134135 let opts = fzf#wrap('foobar', {'sink': 'e'})136 Log opts137 AssertEqual 'e', opts.sink138 Assert !has_key(opts, 'sink*')139140 let g:fzf_history_dir = '/tmp'141 let opts = fzf#wrap('foobar', {'options': '--color light'})142 Log opts143 Assert opts.options =~ '--history /tmp/foobar'144 Assert opts.options =~ '--color light'145146Execute (Cleanup):147 unlet g:dir148 Restore
modified:README.md+14 added-3 removed
318 unmodified lines
319customization.320321[fzf-config]: https://github.com/junegunn/fzf/wiki/Configuring-FZF-command-(vim)322323#### `fzf#run([options])`323#### `fzf#run`324325For more advanced uses, you can use `fzf#run()` function with the following326options.325For more advanced uses, you can use `fzf#run([options])` function with the326following options.327328| Option name | Type | Description |329| -------------------------- | ------------- | ---------------------------------------------------------------- |330| `source` | string | External command to generate input to fzf (e.g. `find .`) |
10 unmodified lines
341342Examples can be found on [the wiki343page](https://github.com/junegunn/fzf/wiki/Examples-(vim)).344345#### `fzf#wrap`346347`fzf#wrap(name string, [opts dict, [fullscreen boolean]])` is a helper348function that decorates the options dictionary so that it understands349`g:fzf_layout`, `g:fzf_action`, and `g:fzf_history_dir` like `:FZF`.350351```vim352command! -bang MyStuff353 \ call fzf#run(fzf#wrap('my-stuff', {'dir': '~/my-stuff'}, <bang>0))354```355356Tips357----358359#### Rendering issues
65 unmodified lines