Skip to content

Commit 9969c8a

Browse files
committed
Merge branch 'release-1.2.0'
2 parents 4c42f4c + a95ba2c commit 9969c8a

File tree

3 files changed

+99
-21
lines changed

3 files changed

+99
-21
lines changed

autoload/fetch.vim

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
" AUTOLOAD FUNCTION LIBRARY FOR VIM-FETCH
2+
let s:cpo = &cpo
3+
set cpo&vim
24

35
" Position specs Dictionary:
46
let s:specs = {}
@@ -27,23 +29,64 @@ function! s:specs.plan9.parse(file) abort
2729
\ [matchlist(a:file, self.pattern)[1]]]
2830
endfunction
2931

32+
" Detection methods for buffers that bypass `filereadable()`:
33+
let s:ignore = []
34+
35+
" - non-file buffer types
36+
call add(s:ignore, {})
37+
function! s:ignore[-1].detect(buffer) abort
38+
return !empty(getbufvar(a:buffer, '&buftype'))
39+
endfunction
40+
41+
" - non-document file types that do not trigger the above
42+
" not needed for: Unite / VimFiler / VimShell / CtrlP / Conque-Shell
43+
call add(s:ignore, {'types': ['netrw']})
44+
function! s:ignore[-1].detect(buffer) abort
45+
return index(self.types, getbufvar(a:buffer, '&filetype')) isnot -1
46+
endfunction
47+
48+
" - redirected buffers
49+
call add(s:ignore, {'bufvars': ['netrw_lastfile']})
50+
function! s:ignore[-1].detect(buffer) abort
51+
for l:var in self.bufvars
52+
if !empty(getbufvar(a:buffer, l:var))
53+
return 1
54+
endif
55+
endfor
56+
return 0
57+
endfunction
58+
3059
" Edit {file}, placing the cursor at the line and column indicated by {spec}:
3160
" @signature: fetch#edit({file:String}, {spec:String})
32-
" @notes: won't work from a |BufReadCmd| event as it does not load non-spec files
61+
" @returns: Boolean indicating if a spec path has been detected and processed
62+
" @notes: - won't work from a |BufReadCmd| event as it doesn't load non-spec'ed files
63+
" - won't work from events fired before the spec'ed file is loaded into
64+
" the buffer (i.e. before '%' is set to the spec'ed file) like |BufNew|
65+
" as it won't be able to wipe the spurious new spec'ed buffer
3366
function! fetch#edit(file, spec) abort
34-
let l:spec = get(s:specs, a:spec, {})
67+
" naive early exit on obvious non-matches
68+
if filereadable(a:file) || match(a:file, s:specs[a:spec].pattern) is -1
69+
return 0
70+
endif
3571

36-
" get spec data if needed, else bail
37-
if empty(l:spec) || filereadable(a:file) || match(a:file, l:spec.pattern) is -1
38-
return
72+
" check for unspec'ed editable file
73+
let [l:file, l:pos] = s:specs[a:spec].parse(a:file)
74+
if !filereadable(l:file)
75+
return 0 " in doubt, end with invalid user input
3976
endif
40-
let [l:file, l:pos] = l:spec.parse(a:file)
41-
let l:cmd = ''
4277

43-
" get rid of the spec'ed buffer
78+
" processing setup
79+
let l:pre = '' " will be prefixed to edit command
80+
81+
" if current buffer is spec'ed and invalid set it up for wiping
4482
if expand('%:p') is fnamemodify(a:file, ':p')
83+
for l:ignore in s:ignore
84+
if l:ignore.detect(bufnr('%')) is 1
85+
return 0
86+
endif
87+
endfor
4588
set bufhidden=wipe " avoid issues with |bwipeout|
46-
let l:cmd .= 'keepalt ' " don't mess up alternate file on switch
89+
let l:pre .= 'keepalt ' " don't mess up alternate file on switch
4790
endif
4891

4992
" clean up argument list
@@ -54,14 +97,19 @@ function! fetch#edit(file, spec) abort
5497
execute l:argidx.'argadd' fnameescape(l:file)
5598
endif
5699
if index(argv(), l:file) isnot -1
57-
let l:cmd .= 'arg' " set arglist index to edited file
100+
let l:pre .= 'arg' " set arglist index to edited file
58101
endif
59102
endif
60103

61104
" open correct file and place cursor at position spec
62-
execute l:cmd.'edit!' fnameescape(l:file)
63-
call cursor(max([l:pos[0], 1]), max([get(l:pos, 1, 0), 1]))
105+
execute l:pre.'edit!' fnameescape(l:file)
106+
let b:fetch_lastpos = [max([l:pos[0], 1]), max([get(l:pos, 1, 0), 1])]
107+
call cursor(b:fetch_lastpos[0], b:fetch_lastpos[1])
64108
silent! normal! zO
109+
return 1
65110
endfunction
66111

112+
let &cpo = s:cpo
113+
unlet! s:cpo
114+
67115
" vim:set sw=2 sts=2 ts=2 et fdm=marker fmr={{{,}}}:

doc/vim-fetch.txt

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*vim-fetch.txt* For Vim version 7.0 or better version 1.0.0
1+
*vim-fetch.txt* For Vim version 7.0 or better version 1.2.0
22

33

44
VIM REFERENCE for the Fetch plug-in
@@ -28,9 +28,9 @@ given) instead of displaying an empty, new file.
2828

2929
FROM OUTSIDE VIM:
3030
>
31-
vim path/to/file.c:12:3
31+
vim path/to/file.ext:12:3
3232
<
33-
will open `file.c` and jump to line 12, column 3.
33+
will open `file.ext` and jump to line 12, column 3.
3434

3535
This works for multiple files (|arglist| passing) in any mix of spec'ed and
3636
unspec'ed paths, and with all Vim |windows-opening| switches ('-o', '-O', and
@@ -39,12 +39,22 @@ unspec'ed paths, and with all Vim |windows-opening| switches ('-o', '-O', and
3939

4040
FROM INSIDE VIM:
4141
>
42-
:e[dit] path/to/file.rb:100:12
42+
:e[dit] path/to/file.ext:100:12
4343
<
44-
will open `file.rb` and jump to line 100, column 12.
44+
will open `file.ext` and jump to line 100, column 12.
4545

46-
This works for any command that opens files with |edit| semantics, notably
47-
|argedit| and |diffsplit|.
46+
This works for any command that opens files with |edit| semantics, including
47+
|argedit|, |pedit| and |diffsplit| on local file systems (e.g. not for |netrw|
48+
remote editing).
49+
50+
51+
INTEGRATION:
52+
53+
After processing a spec for a buffer, vim-spec sets a buffer-local variable,
54+
>
55+
let b:fetch_lastpos = [lnum, colnum]
56+
<
57+
You can use the presence (or values) of that variable to integrate with vim-fetch.
4858

4959
==============================================================================
5060
3. Position specifications *vim-fetch-specs*
@@ -73,6 +83,13 @@ PLAN 9 STYLE
7383
Note: `#` is the alternate file token and needs to be escaped to be used on
7484
the command line (see |cmdline-special|).
7585

86+
OTHER SPEC TYPES
87+
88+
If you would you like to see other specs in vim-fetch, open an issue (or even
89+
better: send a PR) for them at
90+
91+
https://github.com/kopischke/vim-fetch
92+
7693
==============================================================================
7794
4. Troubleshooting *vim-fetch-troubleshooting*
7895

@@ -99,6 +116,13 @@ I TRIED TO EDIT A FILE WITH A PLAN 9 STYLE SPEC AND GOT ANOTHER FILE (OR E194)
99116
:e path/to/file.ext:\#lnum
100117
<
101118

119+
I CAN'T JUMP TO LINES IN REMOTE FILES VIA NETRW
120+
121+
This is by design: vim-fetch needs to check for readable files in both spec'ed
122+
and unspec'ed form. Doing that remotely, even when possible, would add too
123+
much latency to file opening.
124+
125+
102126
MY PROBLEM ISN'T LISTED HERE
103127

104128
You might have found a bug. Please open an issue at

plugin/fetch.vim

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
" SIMPLIFIED TAKE ON BOGADO/FILE-LINE (HOPEFULLY) WITHOUT THE WARTS
22
" Maintainer: Martin Kopischke <[email protected]>
33
" License: MIT (see LICENSE.md)
4-
" Version: 1.0.0
5-
if &compatible
4+
" Version: 1.2.0
5+
if &compatible || !has('autocmd') || v:version < 700
66
finish
77
endif
88

9+
let s:cpo = &cpo
10+
set cpo&vim
11+
912
" Based on |BufWinEnter| to correctly process all buffers in the initial
1013
" |arglist| (see |windows-starting| for some background, though that omits to
1114
" mention that |BufRead| events are also skipped, as is |BufNewFile|, that
@@ -35,4 +38,7 @@ augroup fetch
3538
endfor
3639
augroup END
3740

41+
let &cpo = s:cpo
42+
unlet! s:cpo
43+
3844
" vim:set sw=2 sts=2 ts=2 et fdm=marker fmr={{{,}}}:

0 commit comments

Comments
 (0)