1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-02-23 23:21:32 +08:00
SpaceVim/autoload/SpaceVim/plugins/flygrep.vim

250 lines
6.4 KiB
VimL
Raw Normal View History

2018-01-22 23:49:27 +08:00
scriptencoding utf-8
let s:MPT = SpaceVim#api#import('prompt')
let s:JOB = SpaceVim#api#import('job')
2017-07-08 16:46:53 +08:00
let s:SYS = SpaceVim#api#import('system')
2018-01-21 23:41:07 +08:00
let s:BUFFER = SpaceVim#api#import('vim#buffer')
let s:grepid = 0
2017-12-30 19:58:10 +08:00
let s:MPT._prompt.mpt = '➭ '
2018-01-21 23:41:07 +08:00
" keys:
" files: files for grep, @buffers means listed buffer.
2018-01-22 19:40:32 +08:00
" dir: specific a directory for grep
2018-01-21 23:10:46 +08:00
function! SpaceVim#plugins#flygrep#open(agrv) abort
2017-12-05 20:49:16 +08:00
rightbelow split __flygrep__
setlocal buftype=nofile bufhidden=wipe nobuflisted nolist noswapfile nowrap cursorline nospell nonu norelativenumber
let save_tve = &t_ve
setlocal t_ve=
" setlocal nomodifiable
setf SpaceVimFlyGrep
redraw!
2018-01-21 23:10:46 +08:00
let s:MPT._prompt.begin = get(a:agrv, 'input', '')
2018-01-21 23:41:07 +08:00
let fs = get(a:agrv, 'files', '')
if fs ==# '@buffers'
let s:grep_files = map(s:BUFFER.listed_buffers(), 'bufname(v:val)')
2018-01-22 00:14:44 +08:00
elseif !empty(fs)
let s:grep_files = fs
else
let s:grep_files = ''
2018-01-21 23:41:07 +08:00
endif
2018-01-22 20:17:52 +08:00
let dir = expand(get(a:agrv, 'dir', ''))
2018-01-22 19:40:32 +08:00
if !empty(dir) && isdirectory(dir)
let s:grep_dir = dir
else
let s:grep_dir = ''
endif
2018-01-22 22:17:52 +08:00
let s:grep_exe = get(a:agrv, 'cmd', s:grep_default_exe)
let s:grep_opt = get(a:agrv, 'opt', s:grep_default_opt)
let s:grep_ropt = get(a:agrv, 'ropt', s:grep_default_ropt)
2017-12-05 20:49:16 +08:00
call s:MPT.open()
let &t_ve = save_tve
endfunction
2017-07-07 20:47:26 +08:00
let s:grep_expr = ''
2018-01-22 22:17:52 +08:00
let [s:grep_default_exe, s:grep_default_opt, s:grep_default_ropt] = SpaceVim#mapping#search#default_tool()
2017-07-07 20:47:26 +08:00
let s:grep_timer_id = 0
2017-07-07 21:05:28 +08:00
" @vimlint(EVL103, 1, a:timer)
2017-07-07 20:47:26 +08:00
function! s:grep_timer(timer) abort
2018-01-22 23:19:02 +08:00
let cmd = s:get_search_cmd(join(split(s:grep_expr), '.*'))
2018-01-22 22:08:21 +08:00
call SpaceVim#logger#info('grep cmd: ' . string(cmd))
let s:grepid = s:JOB.start(cmd, {
2017-12-05 20:49:16 +08:00
\ 'on_stdout' : function('s:grep_stdout'),
2018-01-22 20:17:52 +08:00
\ 'on_stderr' : function('s:grep_stderr'),
2017-12-05 20:49:16 +08:00
\ 'in_io' : 'null',
\ 'on_exit' : function('s:grep_exit'),
\ })
2017-07-07 20:47:26 +08:00
endfunction
2017-07-07 21:05:28 +08:00
" @vimlint(EVL103, 0, a:timer)
2017-07-07 20:47:26 +08:00
function! s:flygrep(expr) abort
2017-12-05 20:49:16 +08:00
call s:MPT._build_prompt()
if a:expr ==# ''
redrawstatus
return
endif
try
2017-12-06 00:08:08 +08:00
call matchdelete(s:hi_id)
2017-12-05 20:49:16 +08:00
catch
endtr
2018-01-22 23:35:40 +08:00
hi def link FlyGrepPattern MoreMsg
let s:hi_id = matchadd('FlyGrepPattern', '\c' . join(split(a:expr), '\|'), 1)
2017-12-05 20:49:16 +08:00
let s:grep_expr = a:expr
2018-01-22 23:52:17 +08:00
let s:grep_timer_id = timer_start(200, funcref('s:grep_timer'), {'repeat' : 1})
endfunction
let s:MPT._handle_fly = function('s:flygrep')
function! s:close_buffer() abort
2017-12-05 20:49:16 +08:00
if s:grepid != 0
call s:JOB.stop(s:grepid)
endif
if s:grep_timer_id != 0
call timer_stop(s:grep_timer_id)
endif
q
endfunction
let s:MPT._onclose = function('s:close_buffer')
function! s:close_grep_job() abort
2017-12-05 20:49:16 +08:00
if s:grepid != 0
call s:JOB.stop(s:grepid)
endif
if s:grep_timer_id != 0
call timer_stop(s:grep_timer_id)
endif
normal! "_ggdG
endfunction
let s:MPT._oninputpro = function('s:close_grep_job')
" @vimlint(EVL103, 1, a:data)
" @vimlint(EVL103, 1, a:id)
" @vimlint(EVL103, 1, a:event)
function! s:grep_stdout(id, data, event) abort
2017-12-05 20:49:16 +08:00
let datas =filter(a:data, '!empty(v:val)')
if getline(1) ==# ''
call setline(1, datas)
else
call append('$', datas)
endif
call s:MPT._build_prompt()
endfunction
2018-01-22 20:17:52 +08:00
function! s:grep_stderr(id, data, event) abort
let datas =filter(a:data, '!empty(v:val)')
if getline(1) ==# ''
call setline(1, datas)
else
call append('$', datas)
endif
2018-01-22 22:20:35 +08:00
call append('$', 'job:' . string(s:get_search_cmd(s:grep_expr)))
2018-01-22 20:17:52 +08:00
call s:MPT._build_prompt()
endfunction
function! s:grep_exit(id, data, event) abort
2017-12-05 20:49:16 +08:00
redrawstatus
let s:grepid = 0
endfunction
" @vimlint(EVL103, 0, a:data)
" @vimlint(EVL103, 0, a:id)
" @vimlint(EVL103, 0, a:event)
2018-01-22 22:02:15 +08:00
function! s:get_search_cmd(expr) abort
2018-01-22 22:08:21 +08:00
let cmd = [s:grep_exe] + s:grep_opt
2018-01-22 22:02:15 +08:00
if !empty(s:grep_files) && type(s:grep_files) == 3
return cmd + [a:expr] + s:grep_files
elseif !empty(s:grep_files) && type(s:grep_files) == 1
return cmd + [a:expr] + [s:grep_files]
elseif !empty(s:grep_dir)
return cmd + [a:expr] + [s:grep_dir]
2017-12-05 20:49:16 +08:00
else
2018-01-22 22:53:02 +08:00
return cmd + [a:expr] + s:grep_ropt
2017-12-05 20:49:16 +08:00
endif
endfunction
function! s:next_item() abort
2017-12-05 20:49:16 +08:00
if line('.') == line('$')
normal! gg
else
normal! j
endif
redrawstatus
call s:MPT._build_prompt()
endfunction
function! s:previous_item() abort
2017-12-05 20:49:16 +08:00
if line('.') == 1
normal! G
else
normal! k
endif
redrawstatus
call s:MPT._build_prompt()
endfunction
function! s:open_item() abort
2017-12-05 20:49:16 +08:00
if getline('.') !=# ''
if s:grepid != 0
call s:JOB.stop(s:grepid)
endif
2017-12-05 20:49:16 +08:00
call s:MPT._clear_prompt()
let s:MPT._quit = 1
let line = getline('.')
let filename = fnameescape(split(line, ':\d\+:')[0])
let linenr = matchstr(line, ':\d\+:')[1:-2]
q
exe 'e ' . filename
exe linenr
redraw!
2017-12-05 20:49:16 +08:00
endif
endfunction
2017-07-08 23:02:07 +08:00
function! s:double_click() abort
2017-12-05 20:49:16 +08:00
if line('.') !=# ''
if s:grepid != 0
call s:JOB.stop(s:grepid)
endif
call s:MPT._clear_prompt()
let s:MPT._quit = 1
let isfname = &isfname
if s:SYS.isWindows
set isfname-=:
2017-07-08 23:02:07 +08:00
endif
2017-12-05 20:49:16 +08:00
normal! gF
let nr = bufnr('%')
q
exe 'silent b' . nr
normal! :
let &isfname = isfname
endif
2017-07-08 23:02:07 +08:00
endfunction
function! s:move_cursor() abort
2017-12-05 20:49:16 +08:00
if v:mouse_win == winnr()
let cl = line('.')
if cl < v:mouse_lnum
exe 'normal! ' . (v:mouse_lnum - cl) . 'j'
elseif cl > v:mouse_lnum
exe 'normal! ' . (cl - v:mouse_lnum) . 'k'
2017-07-08 23:02:07 +08:00
endif
2017-12-05 20:49:16 +08:00
endif
call s:MPT._build_prompt()
2017-07-08 23:02:07 +08:00
endfunction
let s:MPT._function_key = {
2017-12-05 20:49:16 +08:00
\ "\<Tab>" : function('s:next_item'),
\ "\<ScrollWheelDown>" : function('s:next_item'),
\ "\<S-tab>" : function('s:previous_item'),
\ "\<ScrollWheelUp>" : function('s:previous_item'),
\ "\<Return>" : function('s:open_item'),
\ "\<LeftMouse>" : function('s:move_cursor'),
\ "\<2-LeftMouse>" : function('s:double_click'),
\ }
if has('nvim')
call extend(s:MPT._function_key,
\ {
\ "\x80\xfdJ" : function('s:previous_item'),
\ "\x80\xfc \x80\xfdJ" : function('s:previous_item'),
\ "\x80\xfc@\x80\xfdJ" : function('s:previous_item'),
\ "\x80\xfc`\x80\xfdJ" : function('s:previous_item'),
\ "\x80\xfdK" : function('s:next_item'),
\ "\x80\xfc \x80\xfdK" : function('s:next_item'),
\ "\x80\xfc@\x80\xfdK" : function('s:next_item'),
\ "\x80\xfc`\x80\xfdK" : function('s:next_item'),
\ }
\ )
endif
" statusline api
function! SpaceVim#plugins#flygrep#lineNr() abort
2017-12-05 20:49:16 +08:00
if getline(1) ==# ''
return ''
else
return line('.') . '/' . line('$')
endif
endfunction