diff --git a/autoload/SpaceVim/plugins/flygrep.vim b/autoload/SpaceVim/plugins/flygrep.vim index bc5a16523..5334b76a4 100644 --- a/autoload/SpaceVim/plugins/flygrep.vim +++ b/autoload/SpaceVim/plugins/flygrep.vim @@ -52,7 +52,7 @@ function! s:update_history() abort endif call add(s:grep_history, s:grep_expr) if !isdirectory(expand('~/.cache/SpaceVim')) - call mkdir(expand('~/.cache/SpaceVim')) + call mkdir(expand('~/.cache/SpaceVim')) endif call writefile([s:JSON.json_encode(s:grep_history)], expand('~/.cache/SpaceVim/flygrep_history')) endfunction @@ -243,15 +243,62 @@ function! s:start_replace() abort call matchdelete(s:hi_id) catch endtr + if s:grepid != 0 + call s:JOB.stop(s:grepid) + endif let replace_text = s:current_grep_pattern if !empty(replace_text) - call SpaceVim#plugins#iedit#start({'expr' : replace_text}, line('w0'), line('w$')) + let rst = SpaceVim#plugins#iedit#start({'expr' : replace_text}, line('w0'), line('w$')) endif - let s:hi_id = s:matchadd('FlyGrepPattern', s:expr_to_pattern(replace_text), 2) + let s:hi_id = s:matchadd('FlyGrepPattern', s:expr_to_pattern(rst), 2) redrawstatus + if rst !=# replace_text + call s:update_files(s:flygrep_result_to_files()) + checktime + endif endfunction " }}} +function! s:flygrep_result_to_files() abort + let files = [] + for line in getbufline(s:flygrep_buffer_id, 1, '$') + let filename = fnameescape(split(line, ':\d\+:')[0]) + let linenr = matchstr(line, ':\d\+:')[1:-2] + let str = matchstr(line, '\(:\d\+:\d\+:\)\@<=.*') + call add(files, [filename, linenr, str]) + endfor + return files +endfunction + +function! s:update_files(files) abort + let fname = '' + let lines = {} + for file in a:files + if file[0] == fname + call extend(lines, {file[1] : file[2]}) + else + if !empty(fname) + call s:update_file(fname, lines) + endif + let fname = file[0] + let lines = {} + call extend(lines, {file[1] : file[2]}) + endif + endfor + if !empty(fname) + call s:update_file(fname, lines) + endif +endfunction + +function! s:update_file(fname, lines) abort + let contents = readfile(a:fname, '') + for linenr in keys(a:lines) + let contents[linenr - 1] = a:lines[ linenr ] + endfor + call writefile(contents, a:fname, '') +endfunction + + " API: MPT._prompt {{{ let s:MPT._prompt.mpt = g:spacevim_commandline_prompt . ' ' " }}} diff --git a/autoload/SpaceVim/plugins/iedit.vim b/autoload/SpaceVim/plugins/iedit.vim index e14456969..0757d652e 100644 --- a/autoload/SpaceVim/plugins/iedit.vim +++ b/autoload/SpaceVim/plugins/iedit.vim @@ -118,7 +118,7 @@ function! SpaceVim#plugins#iedit#start(...) abort if s:mode ==# 'n' && char == 27 let s:mode = '' else - call s:handle(s:mode, char) + let symbol = s:handle(s:mode, char) endif endwhile let s:stack = [] @@ -135,14 +135,15 @@ function! SpaceVim#plugins#iedit#start(...) abort endtry let s:hi_id = '' let &l:cursorline = save_cl + return symbol endfunction function! s:handle(mode, char) abort if a:mode ==# 'n' - call s:handle_normal(a:char) + return s:handle_normal(a:char) elseif a:mode ==# 'i' - call s:handle_insert(a:char) + return s:handle_insert(a:char) endif endfunction @@ -329,6 +330,7 @@ function! s:handle_normal(char) abort call cursor(s:stack[s:index][0], s:stack[s:index][1] + len(s:cursor_stack[s:index].begin)) endif silent! call s:highlight_cursor() + return s:cursor_stack[0].begin . s:cursor_stack[0].cursor . s:cursor_stack[0].end endfunction if exists('*timer_start') @@ -356,7 +358,7 @@ function! s:handle_insert(char) abort silent! call s:highlight_cursor() redraw! redrawstatus! - return + return s:cursor_stack[0].begin . s:cursor_stack[0].cursor . s:cursor_stack[0].end elseif a:char ==# 23 " ctrl-w: delete word before cursor for i in range(len(s:cursor_stack)) @@ -447,6 +449,7 @@ function! s:handle_insert(char) abort call s:replace_symbol() endif silent! call s:highlight_cursor() + return s:cursor_stack[0].begin . s:cursor_stack[0].cursor . s:cursor_stack[0].end endfunction function! s:parse_symbol(begin, end, symbol, ...) abort let use_expr = get(a:000, 0, 0)