In part of a plugin I'm slowly building, I use <cfile>
to get the file pointed by the word under the cursor and then do something if it points to a directory, or open the file pointed to otherwise (so unless the word under the cursors points to a directory, it's supposed to behave exactly like gf
). It all works fine, except for suffixes.
help <cfile>
in vim says:
<cfile>
is replaced with the path name under the cursor (like whatgf
uses)
but while gf
will include a possibly auto-detected suffix
, this doesn't seem to be the case in <cfile>
. Is there some kind of workaround for this?
In case it helps clarify my question, here's the relevant code excerpt:
1 function! now#BufEnter() "{{{
2 " behaviour of <enter> while on now files (mapped on ftplugin)
3 execute "silent! normal! :set suffixesadd=" . g:NOW_suffix . "\r"
4 let l:dest = expand("<cfile>")
5 if isdirectory(l:dest)
6 " if pointing to a directory, update and enter index file there
7 execute 'normal! :cd ' . l:dest . "\r"
8 call now#MakeIndex()
9 else
10 " otherwise edit the file, whether it exists or not
11 execute 'normal! :e ' . l:dest . "\r"
12 endif
13 endfunction "}}}
Line 3 was added for debugging purposes only, copied from elsewhere. The added suffix is flawlessly detected by gf
, but not with the keymap pointing towards the above function (as the comments in the code mention, it is the <enter>
key). When no suffix is needed, the code works as expected: do the directory stuff if need, or edit the pointed file, creating it if necessary.
:normal! gf
?normal! gf
regulargf
is called and suffixes are indeed respected but if the file does not exist, it is not created (as per comment in line 10). Note that I don't expect a non-existing file to be created with a "guessed" suffix, but I do expect an existing file to be opened if there is one, seeking it with the environment suffixes. Maybe it is these two behaviours (lookup with suffix / create if not existing) that are actually incompatible with each other?if
to act differently if the file does not exist, and then: problem solved. Nevertheless, the help on <cfile> is a bit confusing (as <cfile> is clearly not quite like whatgf
uses)