1414
1515" TODO: a) :AN command "next alternate"
1616" b) Priorities for search paths
17- " c) Error instead of defaulting when alternate does not exist
18- " d) <leader>A on a #include line should go to that header
17+ " c) <leader>A on a #include line should go to that header
1918
19+ " Do not load a.vim if is has already been loaded.
2020if exists (" loaded_alternateFile" )
2121 finish
2222endif
@@ -27,6 +27,7 @@ let loaded_alternateFile = 1
2727" g:alternateExtensions_<EXT> variable to a comma separated list of alternates,
2828" where <EXT> is the extension to map.
2929" E.g. let g:alternateExtensions_CPP = "inc,h,H,HPP,hpp"
30+ " let g:alternateExtensions_{'aspx.cs'} = "aspx"
3031
3132
3233" Function : AddAlternateExtensionMapping (PRIVATE)
@@ -37,8 +38,17 @@ let loaded_alternateFile = 1
3738" Returns : nothing
3839" Author : Michael Sharpe <feline@irendi.com>
3940function ! <SID> AddAlternateExtensionMapping (extension, alternates)
40- let varName = " g:alternateExtensions_" . a: extension
41- if (! exists (varName))
41+ " This code does not actually work for variables like foo{'a.b.c.d.e'}
42+ " let varName = "g:alternateExtensions_" . a:extension
43+ " if (!exists(varName))
44+ " let g:alternateExtensions_{a:extension} = a:alternates
45+ " endif
46+
47+ " This code handles extensions which contains a dot. exists() fails with
48+ " such names.
49+ let v: errmsg = " "
50+ silent ! echo g: alternateExtensions_ {extension}
51+ if (v: errmsg != " " )
4252 let g: alternateExtensions_ {a: extension } = a: alternates
4353 endif
4454endfunction
@@ -73,10 +83,18 @@ call <SID>AddAlternateExtensionMapping('ypp',"lpp,l,lex")
7383
7484" Setup default search path, unless the user has specified
7585" a path in their [._]vimrc.
76- if ! exists (' g:alternateSearchPath' )
86+ if ( ! exists (' g:alternateSearchPath' ) )
7787 let g: alternateSearchPath = ' sfr:../source,sfr:../src,sfr:../include,sfr:../inc'
7888endif
7989
90+ " If this variable is true then a.vim will not alternate to a file/buffer which
91+ " does not exist. E.g while editing a.c and the :A will not swtich to a.h
92+ " unless it exists.
93+ if (! exists (' g:alternateNoDefaultAlternate' ))
94+ " by default a.vim will alternate to a file which does not exist
95+ let g: alternateNoDefaultAlternate = 1
96+ endif
97+
8098" Function : GetNthItemFromList (PRIVATE)
8199" Purpose : Support reading items from a comma seperated list
82100" Used to iterate all the extensions in an extension spec
@@ -300,23 +318,23 @@ endfunction
300318" variables echo the curly brace variable and look for an error
301319" message.
302320function ! DetermineExtension (path )
303- let extension = expand ( " % :t:e" )
321+ let extension = fnamemodify ( a: path , " :t:e" )
304322 let v: errmsg = " "
305323 silent ! echo g: alternateExtensions_ {extension}
306324 if (v: errmsg != " " )
307- let extension = expand ( " % :t:e:e" )
325+ let extension = fnamemodify ( a: path , " :t:e:e" )
308326 let v: errmsg = " "
309327 silent ! echo g: alternateExtensions_ {extension}
310328 if (v: errmsg != " " )
311- let extension = expand ( " % :t:e:e:e" )
329+ let extension = fnamemodify ( a: path , " :t:e:e:e" )
312330 let v: errmsg = " "
313331 silent ! echo g: alternateExtensions_ {extension}
314332 if (v: errmsg != " " )
315- let extension = expand ( " % :t:e:e:e:e" )
333+ let extension = fnamemodify ( a: path , " :t:e:e:e:e" )
316334 let v: errmsg = " "
317335 silent ! echo g: alternateExtensions_ {extension}
318336 if (v: errmsg != " " )
319- let extension = expand ( " % :t:e:e:e:e:e" )
337+ let extension = fnamemodify ( a: path , " :t:e:e:e:e:e" )
320338 let v: errmsg = " "
321339 silent ! echo g: alternateExtensions_ {extension}
322340 if (v: errmsg != " " )
@@ -385,9 +403,13 @@ function! AlternateFile(splitWindow, ...)
385403 let onefile = <SID> GetNthItemFromList (allfiles, n )
386404 endwhile
387405
388- call <SID> FindOrCreateBuffer (bestFile, a: splitWindow )
406+ if (bestScore == 0 && g: alternateNoDefaultAlternate == 1 )
407+ echo " No existing alternate available"
408+ else
409+ call <SID> FindOrCreateBuffer (bestFile, a: splitWindow )
410+ endif
389411 else
390- echo " No alternate file available"
412+ echo " No alternate file/buffer available"
391413 endif
392414 endif
393415endfunction
0 commit comments