[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
Provenance
- Commit
f787f7e- Tree
8513106- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
a7c9c08
3 files changed+188 added−50 removed
modified:plugin/fzf.vim+100 added-47 removed
20 unmodified lines21 " OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION22 " WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.23 24 let s:default_layout = { 'down': '~40%' }25+let s:layout_keys = ['window', 'up', 'down', 'left', 'right']26 let s:fzf_go = expand('<sfile>:h:h').'/bin/fzf'27 let s:install = expand('<sfile>:h:h').'/install'28 let s:installed = 029 let s:fzf_tmux = expand('<sfile>:h:h').'/bin/fzf-tmux'74 unmodified lines104 echom a:msg105 echohl None106 endfunction107 108+function! s:has_any(dict, keys)109+ for key in a:keys110+ if has_key(a:dict, key)111+ return 1112+ endif113+ endfor114+ return 0115+endfunction116+117+function! 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)122+endfunction123+124+function! 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+ endtry155+endfunction156+157+" name string, [opts dict, [fullscreen boolean]]158+function! 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)164+165+ " 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+ endif179+180+ " 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+ endif189+190+ " 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+ endif199+200+ return opts201+endfunction202+203 function! fzf#run(...) abort204 try205 let oshell = &shell206 set shell=sh25 unmodified lines232 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 endif289 unmodified lines530 endif531 endfunction532 533 let s:default_action = {438- \ 'ctrl-m': 'e',534 \ 'ctrl-t': 'tab split',535 \ 'ctrl-x': 'split',536 \ 'ctrl-v': 'vsplit' }537 443-function! 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- endtry474-endfunction475-538 function! 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))545 endfunction546 547 command! -nargs=* -complete=dir -bang FZF call s:cmd(<bang>0, <f-args>)548 3 unmodified linesmodified:test/fzf.vader+74 added
1 Execute (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 &acd6 7 Execute (fzf#run with dir option):61 unmodified lines69 call fzf#run({'source': ['/foobar'], 'sink': 'e', 'dir': '/', 'options': '-1'})70 " Working directory changed due to &acd71 AssertEqual '/', getcwd()72 73+Execute (fzf#wrap):74+ AssertThrows fzf#wrap({'foo': 'bar'})75+76+ 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*')82+83+ let opts = fzf#wrap('foobar', {}, 0)84+ Log opts85+ AssertEqual '~40%', opts.down86+87+ let opts = fzf#wrap('foobar', {}, 1)88+ Log opts89+ Assert !has_key(opts, 'down')90+91+ let opts = fzf#wrap('foobar', {'down': '50%'})92+ Log opts93+ AssertEqual '50%', opts.down94+95+ let opts = fzf#wrap('foobar', {'down': '50%'}, 1)96+ Log opts97+ Assert !has_key(opts, 'down')98+99+ let opts = fzf#wrap('foobar', {'sink': 'e'})100+ Log opts101+ AssertEqual 'e', opts.sink102+ Assert !has_key(opts, 'sink*')103+104+ let opts = fzf#wrap('foobar', {'options': '--reverse'})105+ Log opts106+ Assert opts.options =~ '--expect='107+ Assert opts.options =~ '--reverse'108+109+ let g:fzf_layout = {'window': 'enew'}110+ let opts = fzf#wrap('foobar')111+ Log opts112+ AssertEqual 'enew', opts.window113+114+ let opts = fzf#wrap('foobar', {}, 1)115+ Log opts116+ Assert !has_key(opts, 'window')117+118+ let opts = fzf#wrap('foobar', {'right': '50%'})119+ Log opts120+ Assert !has_key(opts, 'window')121+ AssertEqual '50%', opts.right122+123+ let opts = fzf#wrap('foobar', {'right': '50%'}, 1)124+ Log opts125+ Assert !has_key(opts, 'window')126+ Assert !has_key(opts, 'right')127+128+ 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*')134+135+ let opts = fzf#wrap('foobar', {'sink': 'e'})136+ Log opts137+ AssertEqual 'e', opts.sink138+ Assert !has_key(opts, 'sink*')139+140+ 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'145+146 Execute (Cleanup):147 unlet g:dir148 Restoremodified:README.md+14 added-3 removed
318 unmodified lines319 customization.320 321 [fzf-config]: https://github.com/junegunn/fzf/wiki/Configuring-FZF-command-(vim)322 323-#### `fzf#run([options])`323+#### `fzf#run`324 325-For more advanced uses, you can use `fzf#run()` function with the following326-options.325+For more advanced uses, you can use `fzf#run([options])` function with the326+following options.327 328 | Option name | Type | Description |329 | -------------------------- | ------------- | ---------------------------------------------------------------- |330 | `source` | string | External command to generate input to fzf (e.g. `find .`) |10 unmodified lines341 342 Examples can be found on [the wiki343 page](https://github.com/junegunn/fzf/wiki/Examples-(vim)).344 345+#### `fzf#wrap`346+347+`fzf#wrap(name string, [opts dict, [fullscreen boolean]])` is a helper348+function that decorates the options dictionary so that it understands349+`g:fzf_layout`, `g:fzf_action`, and `g:fzf_history_dir` like `:FZF`.350+351+```vim352+command! -bang MyStuff353+ \ call fzf#run(fzf#wrap('my-stuff', {'dir': '~/my-stuff'}, <bang>0))354+```355+356 Tips357 ----358 359 #### Rendering issues65 unmodified lines