fzf.vim
This file
Path
plugin/fzf.vim
251 lines · 6.3 KiB
Last changed by
Merge pull request #116 from junegunn/goJunegunn Choibefore adoption209a6d3
History of this fileTake it
1" Copyright (c) 2015 Junegunn Choi2"3" MIT License4"5" Permission is hereby granted, free of charge, to any person obtaining6" a copy of this software and associated documentation files (the7" "Software"), to deal in the Software without restriction, including8" without limitation the rights to use, copy, modify, merge, publish,9" distribute, sublicense, and/or sell copies of the Software, and to10" permit persons to whom the Software is furnished to do so, subject to11" the following conditions:12"13" The above copyright notice and this permission notice shall be14" included in all copies or substantial portions of the Software.15"16" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,17" EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF18" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND19" NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE20" LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION21" 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:min_tmux_width = 1025let s:min_tmux_height = 326let s:default_tmux_height = '40%'27let s:launcher = 'xterm -e bash -ic %s'28let s:fzf_go = expand('<sfile>:h:h').'/bin/fzf'29let s:fzf_rb = expand('<sfile>:h:h').'/fzf'3031let s:cpo_save = &cpo32set cpo&vim3334function! s:fzf_exec()35 if !exists('s:exec')36 call system('type fzf')37 if v:shell_error38 let s:exec = executable(s:fzf_go) ?39 \ s:fzf_go : (executable(s:fzf_rb) ? s:fzf_rb : '')40 else41 let s:exec = 'fzf'42 endif43 return s:fzf_exec()44 elseif empty(s:exec)45 unlet s:exec46 throw 'fzf executable not found'47 else48 return s:exec49 endif50endfunction5152function! s:tmux_enabled()53 if has('gui_running')54 return 055 endif5657 if exists('s:tmux')58 return s:tmux59 endif6061 let s:tmux = 062 if exists('$TMUX')63 let output = system('tmux -V')64 let s:tmux = !v:shell_error && output >= 'tmux 1.7'65 endif66 return s:tmux67endfunction6869function! s:shellesc(arg)70 return '"'.substitute(a:arg, '"', '\\"', 'g').'"'71endfunction7273function! s:escape(path)74 return substitute(a:path, ' ', '\\ ', 'g')75endfunction7677function! fzf#run(...) abort78 let dict = exists('a:1') ? a:1 : {}79 let temps = { 'result': tempname() }80 let optstr = get(dict, 'options', '')81 try82 let fzf_exec = s:fzf_exec()83 catch84 throw v:exception85 endtry8687 if has_key(dict, 'source')88 let source = dict.source89 let type = type(source)90 if type == 191 let prefix = source.'|'92 elseif type == 393 let temps.input = tempname()94 call writefile(source, temps.input)95 let prefix = 'cat '.s:shellesc(temps.input).'|'96 else97 throw 'Invalid source type'98 endif99 else100 let prefix = ''101 endif102 let command = prefix.fzf_exec.' '.optstr.' > '.temps.result103104 if s:tmux_enabled() && s:tmux_splittable(dict)105 return s:execute_tmux(dict, command, temps)106 else107 return s:execute(dict, command, temps)108 endif109endfunction110111function! s:tmux_splittable(dict)112 return113 \ min([&columns, get(a:dict, 'tmux_width', 0)]) >= s:min_tmux_width ||114 \ min([&lines, get(a:dict, 'tmux_height', get(a:dict, 'tmux', 0))]) >= s:min_tmux_height115endfunction116117function! s:pushd(dict)118 if !empty(get(a:dict, 'dir', ''))119 let a:dict.prev_dir = getcwd()120 execute 'chdir '.s:escape(a:dict.dir)121 endif122endfunction123124function! s:popd(dict)125 if has_key(a:dict, 'prev_dir')126 execute 'chdir '.s:escape(remove(a:dict, 'prev_dir'))127 endif128endfunction129130function! s:execute(dict, command, temps)131 call s:pushd(a:dict)132 silent !clear133 if has('gui_running')134 let launcher = get(a:dict, 'launcher', get(g:, 'fzf_launcher', s:launcher))135 let command = printf(launcher, "'".substitute(a:command, "'", "'\"'\"'", 'g')."'")136 else137 let command = a:command138 endif139 execute 'silent !'.command140 redraw!141 if v:shell_error142 " Do not print error message on exit status 1143 if v:shell_error > 1144 echohl ErrorMsg145 echo 'Error running ' . command146 endif147 return []148 else149 return s:callback(a:dict, a:temps, 0)150 endif151endfunction152153function! s:env_var(name)154 if exists('$'.a:name)155 return a:name . "='". substitute(expand('$'.a:name), "'", "'\\\\''", 'g') . "' "156 else157 return ''158 endif159endfunction160161function! s:execute_tmux(dict, command, temps)162 let command = s:env_var('FZF_DEFAULT_OPTS').s:env_var('FZF_DEFAULT_COMMAND').a:command163 if !empty(get(a:dict, 'dir', ''))164 let command = 'cd '.s:escape(a:dict.dir).' && '.command165 endif166167 let splitopt = '-v'168 if has_key(a:dict, 'tmux_width')169 let splitopt = '-h'170 let size = a:dict.tmux_width171 else172 let size = get(a:dict, 'tmux_height', get(a:dict, 'tmux'))173 endif174175 if type(size) == 1 && size =~ '%$'176 let sizeopt = '-p '.size[0:-2]177 else178 let sizeopt = '-l '.size179 endif180181 let s:pane = substitute(182 \ system(183 \ printf(184 \ 'tmux split-window %s %s -P -F "#{pane_id}" %s',185 \ splitopt, sizeopt, s:shellesc(command))), '\n', '', 'g')186 let s:dict = a:dict187 let s:temps = a:temps188189 augroup fzf_tmux190 autocmd!191 autocmd VimResized * nested call s:tmux_check()192 augroup END193endfunction194195function! s:tmux_check()196 let panes = split(system('tmux list-panes -a -F "#{pane_id}"'), '\n')197198 if index(panes, s:pane) < 0199 augroup fzf_tmux200 autocmd!201 augroup END202203 call s:callback(s:dict, s:temps, 1)204 redraw205 endif206endfunction207208function! s:callback(dict, temps, cd)209 if !filereadable(a:temps.result)210 let lines = []211 else212 if a:cd | call s:pushd(a:dict) | endif213214 let lines = readfile(a:temps.result)215 if has_key(a:dict, 'sink')216 for line in lines217 if type(a:dict.sink) == 2218 call a:dict.sink(line)219 else220 execute a:dict.sink.' '.s:escape(line)221 endif222 endfor223 endif224 endif225226 for tf in values(a:temps)227 silent! call delete(tf)228 endfor229230 call s:popd(a:dict)231232 return lines233endfunction234235function! s:cmd(bang, ...) abort236 let args = copy(a:000)237 let opts = {}238 if len(args) > 0 && isdirectory(expand(args[-1]))239 let opts.dir = remove(args, -1)240 endif241 if !a:bang242 let opts.tmux = get(g:, 'fzf_tmux_height', s:default_tmux_height)243 endif244 call fzf#run(extend({ 'sink': 'e', 'options': join(args) }, opts))245endfunction246247command! -nargs=* -complete=dir -bang FZF call s:cmd('<bang>' == '!', <f-args>)248249let &cpo = s:cpo_save250unlet s:cpo_save251