-
Notifications
You must be signed in to change notification settings - Fork 61
/
file_line.vim
92 lines (79 loc) · 2.27 KB
/
file_line.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
" Avoid installing twice or when in unsupported Vim version.
if exists('g:loaded_file_line') || (v:version < 701)
finish
endif
let g:loaded_file_line = 1
" below regexp will separate filename and line/column number
" possible inputs to get to line 10 (and column 99) in code.cc are:
" * code.cc(10)
" * code.cc(10:99)
" * code.cc:10
" * code.cc:10:99
"
" closing braces/colons are ignored, so also acceptable are:
" * code.cc(10
" * code.cc:10:
let s:regexpressions = [ '\(.\{-1,}\)[(:]\(\d\+\)\%(:\(\d\+\):\?\)\?' ]
function! s:reopenAndGotoLine(file_name, line_num, col_num)
if !filereadable(a:file_name)
return
endif
let l:bufn = bufnr("%")
exec "keepalt edit " . fnameescape(a:file_name)
exec a:line_num
exec "normal! " . a:col_num . '|'
if foldlevel(a:line_num) > 0
exec "normal! zv"
endif
exec "normal! zz"
exec "bwipeout " l:bufn
exec "filetype detect"
endfunction
function! s:gotoline()
let file = bufname("%")
" :e command calls BufRead even though the file is a new one.
" As a workaround Jonas Pfenniger<jonas@pfenniger.name> added an
" AutoCmd BufRead, this will test if this file actually exists before
" searching for a file and line to goto.
if (filereadable(file) || file == '')
return file
endif
let l:names = []
for regexp in s:regexpressions
let l:names = matchlist(file, regexp)
if ! empty(l:names)
let file_name = l:names[1]
let line_num = l:names[2] == ''? '0' : l:names[2]
let col_num = l:names[3] == ''? '0' : l:names[3]
call s:reopenAndGotoLine(file_name, line_num, col_num)
return file_name
endif
endfor
return file
endfunction
" Handle entry in the argument list.
" This is called via `:argdo` when entering Vim.
function! s:handle_arg()
let argname = expand('%')
let fname = s:gotoline()
if fname != argname
let argidx = argidx()
exec (argidx+1).'argdelete'
exec (argidx)'argadd' fnameescape(fname)
endif
endfunction
function! s:startup()
autocmd BufNewFile * nested call s:gotoline()
autocmd BufRead * nested call s:gotoline()
if argc() > 0
let argidx=argidx()
silent call s:handle_arg()
exec (argidx+1).'argument'
" Manually call Syntax autocommands, ignored by `:argdo`.
doautocmd Syntax
doautocmd FileType
endif
endfunction
if !isdirectory(expand("%:p"))
autocmd VimEnter * call s:startup()
endif