-
Notifications
You must be signed in to change notification settings - Fork 2
/
navi.vim
72 lines (63 loc) · 1.82 KB
/
navi.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
" Navi for Vim.
" Set `g:NaviBin` path below.
" Mapped to Ctrl+K (you can change bellow).
" On Normal Mode passes the current line.
" Or select many lines.
let g:NaviBin = '_'
let g:gretting = '...waiting for Navi...'
let g:selectionCallCalled = 0
autocmd CursorMoved * let g:selectionCallCalled = 0
nnoremap <C-k> :<C-u>call SingleLineCall()<CR>
xnoremap <C-k> :<C-u>call SelectionCall()<CR>
function! EscapeSpecialChars(str)
let escapedStr = substitute(a:str, "'", "''", 'g')
let escapedStr = substitute(escapedStr, '"', '\\"', 'g')
let escapedStr = substitute(escapedStr, "`", "\\`", 'g')
return escapedStr
endfunction
function! CleanAnsi(str)
let clean = substitute(a:str, '[\033\e\x1B]\?\[[0-9;]*m', '', 'g')
let clean = substitute(clean, '^>> \+', '', 'g')
return clean
endfunction
fun! Navi(s)
" echo "Original string: " . a:s
let s = EscapeSpecialChars(a:s)
" echo "Escaped string: " . s
let return_from_navi = system(g:NaviBin . " \'" . s . "\'")
let return_from_navi = trim(return_from_navi)
let lines = split(return_from_navi, '\v\n')
let out = []
for line in lines
call add(out, CleanAnsi(line))
endfor
return out
endfunction
function! PrintOut(out)
let current_line = line('.')
call append(current_line, '<<<<<<<')
call append(current_line, a:out)
call append(current_line, '>>>>>>>')
endfunction
function! SelectionCall()
if g:selectionCallCalled == 1
return
endif
g:selectionCallCalled = 1
echo g:gretting
let start_pos = getpos("'<")
let end_pos = getpos("'>")
let start_line = start_pos[1]
let end_line = end_pos[1]
execute "normal " . end_line . "G"
let lines = getline(start_line, end_line)
let line = join(lines, '\n')
let out = Navi(line)
call PrintOut(out)
endfunction
function! SingleLineCall()
echo g:gretting
let line = getline('.')
let out = Navi(line)
call PrintOut(out)
endfunction