From 7fb8de989300761849ea5063e0ef192899ec1db0 Mon Sep 17 00:00:00 2001 From: wsdjeg Date: Sun, 1 Apr 2018 20:11:11 +0800 Subject: [PATCH] Add histroy support in flygrep --- autoload/SpaceVim/plugins/flygrep.vim | 42 ++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/autoload/SpaceVim/plugins/flygrep.vim b/autoload/SpaceVim/plugins/flygrep.vim index 55b68cf5b..7e0e04e02 100644 --- a/autoload/SpaceVim/plugins/flygrep.vim +++ b/autoload/SpaceVim/plugins/flygrep.vim @@ -28,12 +28,15 @@ let [ \ ] = SpaceVim#mapping#search#default_tool() let s:grep_timer_id = 0 let s:grepid = 0 +let s:grep_history = [] +let s:complete_input_history_num = [0,0] " }}} " grep local funcs:{{{ " @vimlint(EVL103, 1, a:timer) let s:current_grep_pattern = '' function! s:grep_timer(timer) abort + call add(s:grep_history, s:grep_expr) let s:current_grep_pattern = join(split(s:grep_expr), '.*') let cmd = s:get_search_cmd(s:current_grep_pattern) call SpaceVim#logger#info('grep cmd: ' . string(cmd)) @@ -329,7 +332,42 @@ function! s:toggle_expr_mode() abort call s:MPT._handle_fly(s:MPT._prompt.begin . s:MPT._prompt.cursor .s:MPT._prompt.end) endfunction +let s:complete_input_history_base = '' +function! s:previous_match_history() abort + if s:complete_input_history_num == [0,0] + let s:complete_input_history_base = s:MPT._prompt.begin + let s:MPT._prompt.cursor = '' + let s:MPT._prompt.end = '' + else + let s:MPT._prompt.begin = s:complete_input_history_base + endif + let s:complete_input_history_num[0] += 1 + let s:MPT._prompt.begin = s:complete_input_history(s:complete_input_history_base, s:complete_input_history_num) +endfunction +function! s:next_match_history() abort + + if s:complete_input_history_num == [0,0] + let s:complete_input_history_base = s:MPT._prompt.begin + let s:MPT._prompt.cursor = '' + let s:MPT._prompt.end = '' + else + let s:MPT._prompt.begin = s:complete_input_history_base + endif + let s:complete_input_history_num[1] += 1 + let s:MPT._prompt.begin = s:complete_input_history(s:complete_input_history_base, s:complete_input_history_num) +endfunction + +function! s:complete_input_history(str,num) abort + let results = filter(copy(s:grep_history), "v:val =~# '^' . a:str") + if len(results) > 0 + call add(results, a:str) + let index = ((len(results) - 1) - a:num[0] + a:num[1]) % len(results) + return results[index] + else + return a:str + endif +endfunction let s:MPT._function_key = { \ "\" : function('s:next_item'), \ "\" : function('s:next_item'), @@ -344,6 +382,8 @@ let s:MPT._function_key = { \ "\" : function('s:start_replace'), \ "\" : function('s:toggle_preview'), \ "\" : function('s:toggle_expr_mode'), + \ "\" : function('s:previous_match_history'), + \ "\" : function('s:next_match_history'), \ } if has('nvim') @@ -410,7 +450,7 @@ function! SpaceVim#plugins#flygrep#lineNr() abort endif endfunction -function! SpaceVim#plugins#flygrep#mode() +function! SpaceVim#plugins#flygrep#mode() abort return s:grep_mode . (empty(s:mode) ? '' : '(' . s:mode . ')') endfunction