Implement --no-sort options with some fixes
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Junegunn Choi · committed by Junegunn Choi · parent 9efb663 · Browse files · 3 files·+122 added−73 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+122 added−73 removed
- other:+122 added−73 removed
Provenance
- Commit
abd07ff- Tree
cf001ec- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
9efb663
3 files changed+122 added−73 removed
modified:plugin/fzf.vim+3 added-3 removed
21 unmodified lines22 " WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.23 24 let s:exec = expand('<sfile>:h:h').'/fzf'25 26-function! s:fzf(cmd)26+function! s:fzf(args)27 try28 let tf = tempname()29- let prefix = empty(a:cmd) ? '' : a:cmd.' | '30- execute "silent !".prefix."/usr/bin/env bash ".s:exec." > ".tf29+ let prefix = exists('g:fzf_command') ? g:fzf_command.'|' : ''30+ execute "silent !".prefix."/usr/bin/env bash ".s:exec." ".a:args." > ".tf31 if !v:shell_error32 execute 'silent e '.join(readfile(tf), '')33 endif34 finally7 unmodified linesmodified:fzf+85 added-61 removed
55 unmodified lines56 @count = 057 @cursor_x = 058 @vcursor = 059 @matches = []60-@fan = '-\|/-\|/'.split(//)60+@loaded = false61+@sort = ARGV.delete('--no-sort').nil?62 @stat = OpenStruct.new(:hit => 0, :partial_hit => 0,63 :prefix_hit => 0, :search => 0)64 65 def max_items; C.lines - 2; end10 unmodified lines76 cprint '> ', 177 cprint @query, 278 end79 79-def print_info msg = nil80+def print_info progress = true, msg = nil81+ @fan ||= '-\|/-\|/'.split(//)82 C.setpos cursor_y - 1, 083 C.clrtoeol84 prefix =85 if fan = @fan.shift2 unmodified lines88 ' '89 else90 ' '91 end90- C.addstr "#{prefix}#{@matches.length}/#{@count}"92+ C.addstr "#{prefix}#{@matches.length}/#{@count}" if progress93 C.addstr msg if msg94 end95 96 def refresh55 unmodified lines152 @new << line.chomp153 @cv.broadcast154 end155 end156+ @mtx.synchronize do157+ @loaded = true158+ @cv.broadcast159+ end160 @smtx.synchronize { @fan = [] }161 }162 163 searcher = Thread.new {164 fcache = {}165 matches = []166 new_length = 0161- pquery = nil162- vcursor = 0163- pvcursor = nil167+ pquery = ''168+ pvcursor = 0169+ ploaded = false170+ plength = 0171 zz = [0, 0]172 173 begin174 while true175 query_changed = nil176 new_items = nil177 vcursor_moved = nil178+ wait_for_completion = nil179 @mtx.synchronize do180 while true173- new_items = !@new.empty?174- query_changed = pquery != @query175- vcursor_moved = pvcursor != @vcursor181+ new_items = !@new.empty?182+ query_changed = pquery != @query183+ vcursor_moved = pvcursor != @vcursor184+ loading_finished = ploaded != @loaded185+ wait_for_completion = !@sort && !@loaded186 177- if !new_items && !query_changed && !vcursor_moved187+ if !new_items && !query_changed && !vcursor_moved && !loading_finished188 @cv.wait @mtx189 next190 end181-191 break192 end193 185- if new_items194+ if !wait_for_completion && new_items195 @lists << [@new, {}]196 @count += @new.length188-189- @new = []190- fcache = {}197+ @new = []198+ fcache = {}199 end200 201 pquery = @query202 pvcursor = @vcursor203+ ploaded = @loaded204 end#mtx205 206+ if wait_for_completion207+ @smtx.synchronize do208+ print_info false, " +#{@new.length}"209+ print_input210+ refresh211+ sleep 0.1212+ end213+ next214+ end215+216 new_search = new_items || query_changed198- if new_search217+ if new_search && !@lists.empty?218 regexp = pquery.empty? ? nil :219 Regexp.new(pquery.split(//).inject('') { |sum, e|220 e = Regexp.escape e221 sum << "#{e}[^#{e}]*?"222 }, Regexp::IGNORECASE)223 205- if fcache.has_key?(pquery)206- @stat.hit += 1207- else208- @smtx.synchronize do209- print_info ' ..'210- refresh211- end unless pquery.empty?212- end213- matches = fcache[pquery] ||= @lists.map { |pair|214- list, cache = pair215-216- if cache[pquery]217- @stat.partial_hit += 1218- cache[pquery]224+ matches =225+ if fcache.has_key?(pquery)226+ @stat.hit += 1227+ fcache[pquery]228 else220- prefix_cache = nil221- (pquery.length - 1).downto(1) do |len|222- prefix = pquery[0, len]223- if prefix_cache = cache[prefix]224- @stat.prefix_hit += 1225- break226- end227- end229+ @smtx.synchronize do230+ print_info true, ' ..'231+ refresh232+ end unless pquery.empty?233 229- cache[pquery] ||= (prefix_cache ? prefix_cache.map { |e| e.first } : list).map { |line|230- if regexp231- md = line.match regexp232- md ? [line, md.offset(0)] : nil234+ found = @lists.map { |pair|235+ list, cache = pair236+237+ if cache[pquery]238+ @stat.partial_hit += 1239+ cache[pquery]240 else241+ prefix_cache = nil242+ (pquery.length - 1).downto(1) do |len|243+ prefix = pquery[0, len]244+ if prefix_cache = cache[prefix]245+ @stat.prefix_hit += 1246+ break247+ end248+ end249+250+ cache[pquery] ||= (prefix_cache ? prefix_cache.map { |e| e.first } : list).map { |line|251+ if regexp252+ # Ignore errors: e.g. invalid byte sequence in UTF-8253+ md = line.match(regexp) rescue nil254+ md ? [line, md.offset(0)] : nil255+ else234- [line, zz]256+ [line, zz]257+ end258+ }.compact259 end236- }.compact260+ }.inject([]) { |all, e| all.concat e }261+ fcache[pquery] = @sort ? found : found.reverse262 end238- }.inject([]) { |all, e| all.concat e }263 @stat.search += 1264 265 new_length = matches.length242- if new_length <= MAX_SORT_LEN266+ if @sort && new_length <= MAX_SORT_LEN267 matches.replace matches.sort_by { |pair|268 line, offset = pair269 [offset.last - offset.first, line.length, line]270 }271 end248- end#new matches272+ end#new_search273 250- # This small delay reduces the number of partial lists.274+ # This small delay reduces the number of partial lists275 sleep 0.2 if !query_changed && !vcursor_moved276 253- prev_length = @matches.length277 if vcursor_moved || new_search255- vcursor =256- @mtx.synchronize do257- @matches = matches258- @vcursor = [0, [@vcursor, new_length - 1, max_items - 1].min].max259- end278+ @mtx.synchronize do279+ plength = [@matches.length, max_items].min280+ @matches = matches281+ pvcursor = @vcursor = [0, [@vcursor, new_length - 1, max_items - 1].min].max282+ end283 end284 285 # Output286 @smtx.synchronize do264- if new_length < prev_length265- prev_length.downto(new_length) do |idx|287+ item_length = [new_length, max_items].min288+ if item_length < plength289+ plength.downto(item_length) do |idx|290 C.setpos cursor_y - idx - 2, 0291 C.clrtoeol292 end293 end294 295 maxc = C.cols - 3296 matches[0, max_items].each_with_index do |item, idx|273- next if !new_search && !((vcursor-1)..(vcursor+1)).include?(idx)297+ next if !new_search && !((pvcursor-1)..(pvcursor+1)).include?(idx)298 299 line, offset = item300 row = cursor_y - idx - 2277- chosen = idx == vcursor301+ chosen = idx == pvcursor302 303 if line.length > maxc304 line = line[0, maxc] + '..'305 end14 unmodified lines320 end321 C.attroff C.color_pair(3) | C::A_BOLD if chosen322 end323 300- print_info324+ print_info if !@lists.empty? || ploaded325 print_input326 refresh327 end328 end21 unmodified lines350 ctrl(:a) => proc { cursor = 0 },351 ctrl(:e) => proc { cursor = input.length },352 ctrl(:j) => proc {353 @mtx.synchronize do330- @vcursor = [0, @vcursor - 1].max354+ @vcursor -= 1355 @cv.broadcast356 end357 },358 ctrl(:k) => proc {359 @mtx.synchronize do336- @vcursor = [0, [@matches.length - 1, @vcursor + 1].min].max360+ @vcursor += 1361 @cv.broadcast362 end363 },364 ctrl(:w) => proc {55 unmodified linesmodified:README.md+34 added-9 removed
50 unmodified lines5152535455565758596061555657589 unmodified lines68697071727374757677787980813 unmodified lines85868788909189909192939495969798991001011021031041051061071081091101116 unmodified lines11811912012112212312412512612712812913013113213313413511 unmodified lines50 unmodified lines call plug#end()
3. Run `:PlugInstall`
Then, you have `:FZF [optional command]` command.
```vim:FZF:FZF find ~/github -type d```
Usage-----
fzf will launch curses-based finder, read the list from STDIN, and write the9 unmodified lines```shvim `fzf````
If you do not want the matched items to be sorted, provide `--no-sort` option.
```shhistory | fzf --no-sort```
### Key binding
Use CTRL-J and CTRL-K (or CTRL-N and CTRL-P) to change the selection, pressenter key to select the item. CTRL-C will terminate the finder.3 unmodified lines- CTRL-A / CTRL-E- CTRL-B / CTRL-F- CTRL-W / CTRL-U
Useful bash bindings and settings---------------------------------Usage as Vim plugin-------------------
If you install fzf as a Vim plugin, `:FZF` command will be added.
```vim:FZF:FZF --no-sort```
You can override the command which produces input to fzf.
```vimlet g:fzf_command = 'find . -type f'```
Useful bash examples--------------------
```sh# vimf - Open selected file in Vimalias vimf='vim `fzf`'
6 unmodified linesfda() { DIR=`find ${1:-*} -type d 2> /dev/null | fzf` && cd "$DIR"}
# fh - repeat historyfh() { eval $(history | fzf --no-sort | sed 's/ *[0-9]* *//')}
# fkill - kill processfkill() { ps -ef | sed 1d | fzf | awk '{print $2}' | xargs kill -${1:-9}}
# CTRL-T - Open fuzzy finder and paste the selected item to the command linebind '"\er": redraw-current-line'bind '"\C-t": " \C-u \C-a\C-k$(fzf)\e\C-e\C-y\C-a\C-y\ey\C-h\C-e\er"'```11 unmodified lines