From e252f457624d2e88850ee5aa02191e2a5c3a6604 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Wed, 2 Sep 2015 19:14:56 +0900 Subject: [PATCH] Command history and search history (#6) --- README.md | 2 ++ doc/fzf-vim.txt | 2 ++ plugin/fzf.vim | 75 +++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 73 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index ef362ce..cfde327 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,8 @@ Commands | `Windows` | Windows | | `Locate PATTERN` | `locate` command output | | `History` | `v:oldfiles` and open buffers | +| `History:` | Command history | +| `History/` | Search history | | `Snippets` | Snippets ([UltiSnips][us]) | | `Commands` | Commands | | `Helptags` | Help tags | diff --git a/doc/fzf-vim.txt b/doc/fzf-vim.txt index 44d44a7..6b9fb43 100644 --- a/doc/fzf-vim.txt +++ b/doc/fzf-vim.txt @@ -69,6 +69,8 @@ COMMANDS *fzf-vim-commands* `Windows` | Windows `Locate PATTERN` | `locate` command output `History` | `v:oldfiles` and open buffers + `History:` | Command history + `History/` | Search history `Snippets` | Snippets ({UltiSnips}{6}) `Commands` | Commands `Helptags` | Help tags diff --git a/plugin/fzf.vim b/plugin/fzf.vim index 8607560..e210f0a 100644 --- a/plugin/fzf.vim +++ b/plugin/fzf.vim @@ -227,7 +227,7 @@ command! -bang -nargs=1 Locate call s:fzf({ \}, 0) " ------------------------------------------------------------------ -" History +" History[:/] " ------------------------------------------------------------------ function! s:all_files() return extend( @@ -236,11 +236,74 @@ function! s:all_files() \ filter(map(s:buflisted(), 'bufname(v:val)'), '!empty(v:val)')) endfunction -command! -bang History call s:fzf({ -\ 'source': reverse(s:all_files()), -\ 'sink*': function('common_sink'), -\ 'options': '--prompt "Hist> " -m' . s:expect(), -\}, 0) +function! s:history_source(type) + let max = histnr(a:type) + let fmt = '%'.len(string(max)).'d' + let list = filter(map(range(1, max), '[-v:val, histget(a:type, - v:val)]'), '!empty(v:val[1])') + return extend([':: Press CTRL-E to edit'], + \ map(list, 'v:val[0]." ".s:yellow(printf(fmt, len(list) - v:key)).": ".v:val[1]')) +endfunction + +function! s:do() + execute s:command +endfunction + +nnoremap (-fzf-vim-do) :call do() + +function! s:history_sink(type, lines) + if empty(a:lines) + return + endif + + let key = a:lines[0] + let item = histget(a:type, split(a:lines[1])[1]) + if key == 'ctrl-e' + call histadd(a:type, item) + call feedkeys(a:type."\") + else + let s:command = "normal ".a:type.item."\" + call feedkeys("\(-fzf-vim-do)") + endif +endfunction + +function! s:cmd_history_sink(lines) + call s:history_sink(':', a:lines) +endfunction + +function! s:cmd_history(bang) + call s:fzf({ + \ 'source': s:history_source(':'), + \ 'sink*': function('s:cmd_history_sink'), + \ 'options': '+m --ansi --with-nth=2.. --prompt="Hist:> " --header-lines=1 --expect=ctrl-e --tiebreak=index'}, a:bang) +endfunction + +function! s:search_history_sink(lines) + call s:history_sink('/', a:lines) +endfunction + +function! s:search_history(bang) + call s:fzf({ + \ 'source': s:history_source('/'), + \ 'sink*': function('s:search_history_sink'), + \ 'options': '+m --ansi --with-nth 2.. --prompt="Hist/> " --header-lines=1 --expect=ctrl-e --tiebreak=index'}, a:bang) +endfunction + +function! s:history(arg, bang) + let bang = a:bang || a:arg[len(a:arg)-1] == '!' + if a:arg[0] == ':' + call s:cmd_history(bang) + elseif a:arg[0] == '/' + call s:search_history(bang) + else + call s:fzf({ + \ 'source': reverse(s:all_files()), + \ 'sink*': function('s:common_sink'), + \ 'options': '--prompt "Hist> " -m' . s:expect(), + \}, bang) + endif +endfunction + +command! -bang -nargs=* History call s:history(, 0) " ------------------------------------------------------------------ " Buffers