Skip to content

Commit 9f84596

Browse files
Mike Sharpevim-scripts
authored andcommitted
Version 2.15
Initial support for jumping to files under the cursor. New commands IH, IHS, IHV, IHT and IHN. Added sample macros for jumping to the source file corresponding to the header file under the cursor (e.g. jumping on #include <myfile.h> will find myfile.cpp). See documentation for more details.
1 parent 88eabc4 commit 9f84596

File tree

1 file changed

+124
-2
lines changed

1 file changed

+124
-2
lines changed

plugin/a.vim

Lines changed: 124 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
if exists("loaded_alternateFile")
1717
finish
1818
endif
19+
if (v:progname == "ex")
20+
finish
21+
endif
1922
let loaded_alternateFile = 1
2023

2124
" setup the default set of alternate extensions. The user can override in thier
@@ -47,6 +50,7 @@ function! <SID>AddAlternateExtensionMapping(extension, alternates)
4750
" This code handles extensions which contains a dot. exists() fails with
4851
" such names.
4952
let v:errmsg = ""
53+
" FIXME this line causes ex to return 1 instead of 0 for some reason??
5054
silent! echo g:alternateExtensions_{a:extension}
5155
if (v:errmsg != "")
5256
let g:alternateExtensions_{a:extension} = a:alternates
@@ -58,6 +62,7 @@ function! <SID>AddAlternateExtensionMapping(extension, alternates)
5862
endif
5963
endfunction
6064

65+
6166
" Add all the default extensions
6267
" Mappings for C and C++
6368
call <SID>AddAlternateExtensionMapping('h',"c,cpp,cxx,cc,CC")
@@ -233,6 +238,47 @@ function! <SID>FindFileInSearchPath(fileName, pathList, relPathBase)
233238
return filepath
234239
endfunction
235240

241+
" Function : FindFileInSearchPathEx (PRIVATE)
242+
" Purpose : Searches for a file in the search path list
243+
" Args : filename -- name of the file to search for
244+
" pathList -- the path list to search
245+
" relPathBase -- the path which relative paths are expanded from
246+
" count -- find the count'th occurence of the file on the path
247+
" Returns : An expanded filename if found, the empty string otherwise
248+
" Author : Michael Sharpe ([email protected])
249+
" History : Based on <SID>FindFileInSearchPath() but with extensions
250+
function! <SID>FindFileInSearchPathEx(fileName, pathList, relPathBase, count)
251+
let filepath = ""
252+
let m = 1
253+
let spath = ""
254+
let pathListLen = strlen(a:pathList)
255+
if (pathListLen > 0)
256+
while (1)
257+
let pathSpec = <SID>GetNthItemFromList(a:pathList, m)
258+
if (pathSpec != "")
259+
let path = <SID>ExpandAlternatePath(pathSpec, a:relPathBase)
260+
if (spath != "")
261+
let spath = spath . ','
262+
endif
263+
let spath = spath . path
264+
else
265+
break
266+
endif
267+
let m = m + 1
268+
endwhile
269+
endif
270+
271+
if (&path != "")
272+
if (spath != "")
273+
let spath = spath . ','
274+
endif
275+
let spath = spath . &path
276+
endif
277+
278+
let filepath = findfile(a:fileName, spath, a:count)
279+
return filepath
280+
endfunction
281+
236282
" Function : EnumerateFilesByExtension (PRIVATE)
237283
" Purpose : enumerates all files by a particular list of alternate extensions.
238284
" Args : path -- path of a file (not including the file)
@@ -412,11 +458,87 @@ function! AlternateFile(splitWindow, ...)
412458
endif
413459
endfunction
414460

461+
" Function : AlternateOpenFileUnderCursor (PUBLIC)
462+
" Purpose : Opens file under the cursor
463+
" Args : splitWindow -- indicates how to open the file
464+
" Returns : Nothing
465+
" Author : Michael Sharpe ([email protected]) www.irendi.com
466+
function! AlternateOpenFileUnderCursor(splitWindow,...)
467+
let cursorFile = (a:0 > 0) ? a:1 : expand("<cfile>")
468+
let currentPath = expand("%:p:h")
469+
let openCount = 1
470+
471+
let fileName = <SID>FindFileInSearchPathEx(cursorFile, g:alternateSearchPath, currentPath, openCount)
472+
if (fileName != "")
473+
call <SID>FindOrCreateBuffer(fileName, a:splitWindow, 1)
474+
let b:openCount = openCount
475+
let b:cursorFile = cursorFile
476+
let b:currentPath = currentPath
477+
else
478+
echo "Can't find file"
479+
endif
480+
endfunction
481+
482+
" Function : AlternateOpenNextFile (PUBLIC)
483+
" Purpose : Opens the next file corresponding to the search which found the
484+
" current file
485+
" Args : bang -- indicates what to do if the current file has not been
486+
" saved
487+
" Returns : nothing
488+
" Author : Michael Sharpe ([email protected]) www.irendi.com
489+
function! AlternateOpenNextFile(bang)
490+
let cursorFile = ""
491+
if (exists("b:cursorFile"))
492+
let cursorFile = b:cursorFile
493+
endif
494+
495+
let currentPath = ""
496+
if (exists("b:currentPath"))
497+
let currentPath = b:currentPath
498+
endif
499+
500+
let openCount = 0
501+
if (exists("b:openCount"))
502+
let openCount = b:openCount + 1
503+
endif
504+
505+
if (cursorFile != "" && currentPath != "" && openCount != 0)
506+
let fileName = <SID>FindFileInSearchPathEx(cursorFile, g:alternateSearchPath, currentPath, openCount)
507+
if (fileName != "")
508+
call <SID>FindOrCreateBuffer(fileName, "n".a:bang, 0)
509+
let b:openCount = openCount
510+
let b:cursorFile = cursorFile
511+
let b:currentPath = currentPath
512+
else
513+
let fileName = <SID>FindFileInSearchPathEx(cursorFile, g:alternateSearchPath, currentPath, 1)
514+
if (fileName != "")
515+
call <SID>FindOrCreateBuffer(fileName, "n".a:bang, 0)
516+
let b:openCount = 1
517+
let b:cursorFile = cursorFile
518+
let b:currentPath = currentPath
519+
else
520+
echo "Can't find next file"
521+
endif
522+
endif
523+
endif
524+
endfunction
525+
526+
comm! -nargs=? -bang IH call AlternateOpenFileUnderCursor("n<bang>", <f-args>)
527+
comm! -nargs=? -bang IHS call AlternateOpenFileUnderCursor("h<bang>", <f-args>)
528+
comm! -nargs=? -bang IHV call AlternateOpenFileUnderCursor("v<bang>", <f-args>)
529+
comm! -nargs=? -bang IHT call AlternateOpenFileUnderCursor("t<bang>", <f-args>)
530+
comm! -nargs=? -bang IHN call AlternateOpenNextFile("<bang>")
531+
imap <Leader>ih <ESC>:IHS<CR>
532+
nmap <Leader>ih :IHS<CR>
533+
imap <Leader>is <ESC>:IHS<CR>:A<CR>
534+
nmap <Leader>is :IHS<CR>:A<CR>
535+
imap <Leader>ihn <ESC>:IHN<CR>
536+
nmap <Leader>ihn :IHN<CR>
537+
415538
"function! <SID>PrintList(theList)
416539
" let n = 1
417540
" let oneFile = <SID>GetNthItemFromList(a:theList, n)
418541
" while (oneFile != "")
419-
" Decho "list[".n."]=".oneFile
420542
" let n = n + 1
421543
" let oneFile = <SID>GetNthItemFromList(a:theList, n)
422544
" endwhile
@@ -669,7 +791,7 @@ endfunction
669791
" Function : EqualFilePaths (PRIVATE)
670792
" Purpose : Compares two paths. Do simple string comparison anywhere but on
671793
" Windows. On Windows take into account that file paths could differ
672-
" in usage of separators and the fact that case does not metter.
794+
" in usage of separators and the fact that case does not matter.
673795
" "c:\WINDOWS" is the same path as "c:/windows". has("win32unix") Vim
674796
" version does not count as one having Windows path rules.
675797
" Args : path1 (IN) -- first path

0 commit comments

Comments
 (0)