123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841 |
-
-
-
-
-
-
-
-
-
- " "as is
-
-
-
-
-
-
-
-
- if exists("loaded_alternateFile")
- finish
- endif
- if (v:progname == "ex")
- finish
- endif
- let loaded_alternateFile = 1
-
- let alternateExtensionsDict = {}
-
-
-
-
-
- " E.g. let g:alternateExtensions_CPP = "inc,h,H,HPP,hpp
- " let g:alternateExtensions_{'aspx.cs'} = "aspx
-
-
-
-
- let s:maxDotsInExtension = 1
-
-
-
-
-
-
-
-
- function! <SID>AddAlternateExtensionMapping(extension, alternates)
-
- "let varName = "g:alternateExtensions_
-
-
-
-
-
-
- "let v:errmsg = "
-
-
- "if (v:errmsg != "
-
-
-
- let g:alternateExtensionsDict[a:extension] = a:alternates
- let dotsNumber = strlen(substitute(a:extension, "[^.]", "", "g"))
- if s:maxDotsInExtension < dotsNumber
- let s:maxDotsInExtension = dotsNumber
- endif
- endfunction
-
-
-
-
- call <SID>AddAlternateExtensionMapping('h',"c,cpp,cxx,cc,CC")
- call <SID>AddAlternateExtensionMapping('hh',"c,cpp,cxx,cc,CC")
- call <SID>AddAlternateExtensionMapping('H',"C,CPP,CXX,CC")
- call <SID>AddAlternateExtensionMapping('hpp',"cpp,c")
- call <SID>AddAlternateExtensionMapping('HPP',"CPP,C")
- call <SID>AddAlternateExtensionMapping('c',"h")
- call <SID>AddAlternateExtensionMapping('C',"H")
- call <SID>AddAlternateExtensionMapping('cpp',"h,hh,hpp")
- call <SID>AddAlternateExtensionMapping('CPP',"H,HPP")
- call <SID>AddAlternateExtensionMapping('cc',"h")
- call <SID>AddAlternateExtensionMapping('CC',"H,h")
- call <SID>AddAlternateExtensionMapping('cxx',"h")
- call <SID>AddAlternateExtensionMapping('CXX',"H")
-
- call <SID>AddAlternateExtensionMapping('psl',"ph")
- call <SID>AddAlternateExtensionMapping('ph',"psl")
-
- call <SID>AddAlternateExtensionMapping('adb',"ads")
- call <SID>AddAlternateExtensionMapping('ads',"adb")
-
- call <SID>AddAlternateExtensionMapping('l',"y,yacc,ypp")
- call <SID>AddAlternateExtensionMapping('lex',"yacc,y,ypp")
- call <SID>AddAlternateExtensionMapping('lpp',"ypp,y,yacc")
- call <SID>AddAlternateExtensionMapping('y',"l,lex,lpp")
- call <SID>AddAlternateExtensionMapping('yacc',"lex,l,lpp")
- call <SID>AddAlternateExtensionMapping('ypp',"lpp,l,lex")
-
- call <SID>AddAlternateExtensionMapping('ml',"mli")
- call <SID>AddAlternateExtensionMapping('mli',"ml")
-
- call <SID>AddAlternateExtensionMapping('aspx.cs', 'aspx')
- call <SID>AddAlternateExtensionMapping('aspx.vb', 'aspx')
- call <SID>AddAlternateExtensionMapping('aspx', 'aspx.cs,aspx.vb')
-
-
-
- if (!exists('g:alternateSearchPath'))
- let g:alternateSearchPath = 'sfr:../source,sfr:../src,sfr:../include,sfr:../inc'
- endif
-
-
-
-
- if (!exists('g:alternateNoDefaultAlternate'))
-
- let g:alternateNoDefaultAlternate = 0
- endif
-
-
-
-
- if (!exists('g:alternateRelativeFiles'))
-
-
- let g:alternateRelativeFiles = 0
- endif
-
-
-
-
-
-
-
-
-
- " spec), or "
-
-
-
- function! <SID>GetNthItemFromList(list, n)
- let itemStart = 0
- let itemEnd = -1
- let pos = 0
- let item = ""
- let i = 0
- while (i != a:n)
- let itemStart = itemEnd + 1
- let itemEnd = match(a:list, ",", itemStart)
- let i = i + 1
- if (itemEnd == -1)
- if (i == a:n)
- let itemEnd = strlen(a:list)
- endif
- break
- endif
- endwhile
- if (itemEnd != -1)
- let item = strpart(a:list, itemStart, itemEnd - itemStart)
- endif
- return item
- endfunction
-
-
- " Purpose : Expand path info. A path with a prefix of "wdr:
-
- " directory where vim was started.) A path prefix of "abs:
- " be treated as absolute. No prefix or "sfr:
-
-
-
- " A prefix of "reg:
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- function! <SID>ExpandAlternatePath(pathSpec, sfPath)
- let prfx = strpart(a:pathSpec, 0, 4)
- if (prfx == "wdr:" || prfx == "abs:")
- let path = strpart(a:pathSpec, 4)
- elseif (prfx == "reg:")
- let re = strpart(a:pathSpec, 4)
- let sep = strpart(re, 0, 1)
- let patend = match(re, sep, 1)
- let pat = strpart(re, 1, patend - 1)
- let subend = match(re, sep, patend + 1)
- let sub = strpart(re, patend+1, subend - patend - 1)
- let flag = strpart(re, strlen(re) - 2)
- if (flag == sep)
- let flag = ''
- endif
- let path = substitute(a:sfPath, pat, sub, flag)
-
-
- else
- let path = a:pathSpec
- if (prfx == "sfr:")
- let path = strpart(path, 4)
- endif
- let path = a:sfPath . "/" . path
- endif
- return path
- endfunction
-
-
-
-
-
-
-
-
-
- function! <SID>FindFileInSearchPath(fileName, pathList, relPathBase)
- let filepath = ""
- let m = 1
- let pathListLen = strlen(a:pathList)
- if (pathListLen > 0)
- while (1)
- let pathSpec = <SID>GetNthItemFromList(a:pathList, m)
- if (pathSpec != "")
- let path = <SID>ExpandAlternatePath(pathSpec, a:relPathBase)
- let fullname = path . "/" . a:fileName
- let foundMatch = <SID>BufferOrFileExists(fullname)
- if (foundMatch)
- let filepath = fullname
- break
- endif
- else
- break
- endif
- let m = m + 1
- endwhile
- endif
- return filepath
- endfunction
-
-
-
-
-
-
-
-
-
-
- function! <SID>FindFileInSearchPathEx(fileName, pathList, relPathBase, count)
- let filepath = ""
- let m = 1
- let spath = ""
- let pathListLen = strlen(a:pathList)
- if (pathListLen > 0)
- while (1)
- let pathSpec = <SID>GetNthItemFromList(a:pathList, m)
- if (pathSpec != "")
- let path = <SID>ExpandAlternatePath(pathSpec, a:relPathBase)
- if (spath != "")
- let spath = spath . ','
- endif
- let spath = spath . path
- else
- break
- endif
- let m = m + 1
- endwhile
- endif
-
- if (&path != "")
- if (spath != "")
- let spath = spath . ','
- endif
- let spath = spath . &path
- endif
-
- let filepath = findfile(a:fileName, spath, a:count)
- return filepath
- endfunction
-
-
-
-
-
-
-
-
- function! EnumerateFilesByExtension(path, baseName, extension)
- let enumeration = ""
- let extSpec = ""
- let v:errmsg = ""
- silent! echo g:alternateExtensions_{a:extension}
- if (v:errmsg == "")
- let extSpec = g:alternateExtensions_{a:extension}
- endif
- if (extSpec == "")
- if (has_key(g:alternateExtensionsDict, a:extension))
- let extSpec = g:alternateExtensionsDict[a:extension]
- endif
- endif
- if (extSpec != "")
- let n = 1
- let done = 0
- while (!done)
- let ext = <SID>GetNthItemFromList(extSpec, n)
- if (ext != "")
- if (a:path != "")
- let newFilename = a:path . "/" . a:baseName . "." . ext
- else
- let newFilename = a:baseName . "." . ext
- endif
- if (enumeration == "")
- let enumeration = newFilename
- else
- let enumeration = enumeration . "," . newFilename
- endif
- else
- let done = 1
- endif
- let n = n + 1
- endwhile
- endif
- return enumeration
- endfunction
-
-
-
-
-
-
-
-
-
-
-
- function! EnumerateFilesByExtensionInPath(baseName, extension, pathList, relPathBase)
- let enumeration = ""
- let filepath = ""
- let m = 1
- let pathListLen = strlen(a:pathList)
- if (pathListLen > 0)
- while (1)
- let pathSpec = <SID>GetNthItemFromList(a:pathList, m)
- if (pathSpec != "")
- let path = <SID>ExpandAlternatePath(pathSpec, a:relPathBase)
- let pe = EnumerateFilesByExtension(path, a:baseName, a:extension)
- if (enumeration == "")
- let enumeration = pe
- else
- let enumeration = enumeration . "," . pe
- endif
- else
- break
- endif
- let m = m + 1
- endwhile
- endif
- return enumeration
- endfunction
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- " exists("foo_c.e.f
-
-
-
-
-
-
-
- function! DetermineExtension(path)
- let mods = ":t"
- let i = 0
- while i <= s:maxDotsInExtension
- let mods = mods . ":e"
- let extension = fnamemodify(a:path, mods)
- if (has_key(g:alternateExtensionsDict, extension))
- return extension
- endif
- let v:errmsg = ""
- silent! echo g:alternateExtensions_{extension}
- if (v:errmsg == "")
- return extension
- endif
- let i = i + 1
- endwhile
- return ""
- endfunction
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- function! AlternateFile(splitWindow, ...)
- let extension = DetermineExtension(expand("%:p"))
- let baseName = substitute(expand("%:t"), "\." . extension . '$', "", "")
- let currentPath = expand("%:p:h")
-
- if (a:0 != 0)
- let newFullname = currentPath . "/" . baseName . "." . a:1
- call <SID>FindOrCreateBuffer(newFullname, a:splitWindow, 0)
- else
- let allfiles = ""
- if (extension != "")
- let allfiles1 = EnumerateFilesByExtension(currentPath, baseName, extension)
- let allfiles2 = EnumerateFilesByExtensionInPath(baseName, extension, g:alternateSearchPath, currentPath)
-
- if (allfiles1 != "")
- if (allfiles2 != "")
- let allfiles = allfiles1 . ',' . allfiles2
- else
- let allfiles = allfiles1
- endif
- else
- let allfiles = allfiles2
- endif
- endif
-
- if (allfiles != "")
- let bestFile = ""
- let bestScore = 0
- let score = 0
- let n = 1
-
- let onefile = <SID>GetNthItemFromList(allfiles, n)
- let bestFile = onefile
- while (onefile != "" && score < 2)
- let score = <SID>BufferOrFileExists(onefile)
- if (score > bestScore)
- let bestScore = score
- let bestFile = onefile
- endif
- let n = n + 1
- let onefile = <SID>GetNthItemFromList(allfiles, n)
- endwhile
-
- if (bestScore == 0 && g:alternateNoDefaultAlternate == 1)
- echo "No existing alternate available"
- else
- call <SID>FindOrCreateBuffer(bestFile, a:splitWindow, 1)
- let b:AlternateAllFiles = allfiles
- endif
- else
- echo "No alternate file/buffer available"
- endif
- endif
- endfunction
-
-
-
-
-
-
- function! AlternateOpenFileUnderCursor(splitWindow,...)
- let cursorFile = (a:0 > 0) ? a:1 : expand("<cfile>")
- let currentPath = expand("%:p:h")
- let openCount = 1
-
- let fileName = <SID>FindFileInSearchPathEx(cursorFile, g:alternateSearchPath, currentPath, openCount)
- if (fileName != "")
- call <SID>FindOrCreateBuffer(fileName, a:splitWindow, 1)
- let b:openCount = openCount
- let b:cursorFile = cursorFile
- let b:currentPath = currentPath
- else
- echo "Can't find file"
- endif
- endfunction
-
-
-
-
-
-
-
-
- function! AlternateOpenNextFile(bang)
- let cursorFile = ""
- if (exists("b:cursorFile"))
- let cursorFile = b:cursorFile
- endif
-
- let currentPath = ""
- if (exists("b:currentPath"))
- let currentPath = b:currentPath
- endif
-
- let openCount = 0
- if (exists("b:openCount"))
- let openCount = b:openCount + 1
- endif
-
- if (cursorFile != "" && currentPath != "" && openCount != 0)
- let fileName = <SID>FindFileInSearchPathEx(cursorFile, g:alternateSearchPath, currentPath, openCount)
- if (fileName != "")
- call <SID>FindOrCreateBuffer(fileName, "n".a:bang, 0)
- let b:openCount = openCount
- let b:cursorFile = cursorFile
- let b:currentPath = currentPath
- else
- let fileName = <SID>FindFileInSearchPathEx(cursorFile, g:alternateSearchPath, currentPath, 1)
- if (fileName != "")
- call <SID>FindOrCreateBuffer(fileName, "n".a:bang, 0)
- let b:openCount = 1
- let b:cursorFile = cursorFile
- let b:currentPath = currentPath
- else
- echo "Can't find next file"
- endif
- endif
- endif
- endfunction
-
- comm! -nargs=? -bang IH call AlternateOpenFileUnderCursor("n<bang>", <f-args>)
- comm! -nargs=? -bang IHS call AlternateOpenFileUnderCursor("h<bang>", <f-args>)
- comm! -nargs=? -bang IHV call AlternateOpenFileUnderCursor("v<bang>", <f-args>)
- comm! -nargs=? -bang IHT call AlternateOpenFileUnderCursor("t<bang>", <f-args>)
- comm! -nargs=? -bang IHN call AlternateOpenNextFile("<bang>")
- imap <Leader>ih <ESC>:IHS<CR>
- nmap <Leader>ih :IHS<CR>
- imap <Leader>is <ESC>:IHS<CR>:A<CR>
- nmap <Leader>is :IHS<CR>:A<CR>
- imap <Leader>ihn <ESC>:IHN<CR>
- nmap <Leader>ihn :IHN<CR>
-
-
-
-
- " while (oneFile != "
-
-
-
-
-
-
-
-
-
-
-
- function! NextAlternate(bang)
- if (exists('b:AlternateAllFiles'))
- let currentFile = expand("%")
- let n = 1
- let onefile = <SID>GetNthItemFromList(b:AlternateAllFiles, n)
- while (onefile != "" && !<SID>EqualFilePaths(fnamemodify(onefile,":p"), fnamemodify(currentFile,":p")))
- let n = n + 1
- let onefile = <SID>GetNthItemFromList(b:AlternateAllFiles, n)
- endwhile
-
- if (onefile != "")
- let stop = n
- let n = n + 1
- let foundAlternate = 0
- let nextAlternate = ""
- while (n != stop)
- let nextAlternate = <SID>GetNthItemFromList(b:AlternateAllFiles, n)
- if (nextAlternate == "")
- let n = 1
- continue
- endif
- let n = n + 1
- if (<SID>EqualFilePaths(fnamemodify(nextAlternate, ":p"), fnamemodify(currentFile, ":p")))
- continue
- endif
- if (filereadable(nextAlternate))
- " on cygwin filereadable("foo.H") returns true if "foo.h
- if (has("unix") && $WINDIR != "" && fnamemodify(nextAlternate, ":p") ==? fnamemodify(currentFile, ":p"))
- continue
- endif
- let foundAlternate = 1
- break
- endif
- endwhile
- if (foundAlternate == 1)
- let s:AlternateAllFiles = b:AlternateAllFiles
- "silent! execute ":e".a:bang."
- call <SID>FindOrCreateBuffer(nextAlternate, "n".a:bang, 0)
- let b:AlternateAllFiles = s:AlternateAllFiles
- else
- echo "Only this alternate file exists"
- endif
- else
- echo "Could not find current file in alternates list"
- endif
- else
- echo "No other alternate files exist"
- endif
- endfunction
-
- comm! -nargs=? -bang A call AlternateFile("n<bang>", <f-args>)
- comm! -nargs=? -bang AS call AlternateFile("h<bang>", <f-args>)
- comm! -nargs=? -bang AV call AlternateFile("v<bang>", <f-args>)
- comm! -nargs=? -bang AT call AlternateFile("t<bang>", <f-args>)
- comm! -nargs=? -bang AN call NextAlternate("<bang>")
-
-
-
-
-
-
-
-
- function! <SID>BufferOrFileExists(fileName)
- let result = 0
-
- let lastBuffer = bufnr("$")
- let i = 1
- while i <= lastBuffer
- if <SID>EqualFilePaths(expand("#".i.":p"), a:fileName)
- let result = 2
- break
- endif
- let i = i + 1
- endwhile
-
- if (!result)
- let bufName = fnamemodify(a:fileName,":t")
- let memBufName = bufname(bufName)
- if (memBufName != "")
- let memBufBasename = fnamemodify(memBufName, ":t")
- if (bufName == memBufBasename)
- let result = 2
- endif
- endif
-
- if (!result)
- let result = bufexists(bufName) || bufexists(a:fileName) || filereadable(a:fileName)
- endif
- endif
-
- if (!result)
- let result = filereadable(a:fileName)
- endif
- return result
- endfunction
-
-
-
-
-
-
-
-
-
- " ("v", "h", "n", "v!", "h!", "n!", "t", "t!
-
-
-
-
-
-
-
-
-
-
-
- function! <SID>FindOrCreateBuffer(fileName, doSplit, findSimilar)
-
- let FILENAME = escape(a:fileName, ' ')
- let bufNr = -1
- let lastBuffer = bufnr("$")
- let i = 1
- if (a:findSimilar)
- while i <= lastBuffer
- if <SID>EqualFilePaths(expand("#".i.":p"), a:fileName)
- let bufNr = i
- break
- endif
- let i = i + 1
- endwhile
-
- if (bufNr == -1)
- let bufName = bufname(a:fileName)
- let bufFilename = fnamemodify(a:fileName,":t")
-
- if (bufName == "")
- let bufName = bufname(bufFilename)
- endif
-
- if (bufName != "")
- let tail = fnamemodify(bufName, ":t")
- if (tail != bufFilename)
- let bufName = ""
- endif
- endif
- if (bufName != "")
- let bufNr = bufnr(bufName)
- let FILENAME = bufName
- endif
- endif
- endif
-
- if (g:alternateRelativeFiles == 1)
- let FILENAME = fnamemodify(FILENAME, ":p:.")
- endif
-
- let splitType = a:doSplit[0]
- let bang = a:doSplit[1]
- if (bufNr == -1)
-
- let v:errmsg=""
- if (splitType == "h")
- silent! execute ":split".bang." " . FILENAME
- elseif (splitType == "v")
- silent! execute ":vsplit".bang." " . FILENAME
- elseif (splitType == "t")
- silent! execute ":tab split".bang." " . FILENAME
- else
- silent! execute ":e".bang." " . FILENAME
- endif
- if (v:errmsg != "")
- echo v:errmsg
- endif
- else
-
-
- let tabNr = -1
-
- for i in range(tabpagenr('$'))
-
- let tabList = tabpagebuflist(i + 1)
- let idx = 0
-
- while idx < len(tabList)
-
- if (tabList[idx] == bufNr)
-
- let tabNr = i + 1
- break
- endif
- let idx = idx + 1
- endwhile
- if (tabNr != -1)
- break
- endif
- endfor
-
- if (tabNr != -1)
- execute "tabn ".tabNr
- endif
-
-
- let bufWindow = bufwinnr(bufNr)
- if (bufWindow == -1)
-
- let v:errmsg=""
- if (splitType == "h")
- silent! execute ":sbuffer".bang." " . FILENAME
- elseif (splitType == "v")
- silent! execute ":vert sbuffer " . FILENAME
- elseif (splitType == "t")
- silent! execute ":tab sbuffer " . FILENAME
- else
- silent! execute ":buffer".bang." " . FILENAME
- endif
- if (v:errmsg != "")
- echo v:errmsg
- endif
- else
-
- execute bufWindow."wincmd w"
- if (bufWindow != winnr())
-
- let v:errmsg=""
- if (splitType == "h")
- silent! execute ":split".bang." " . FILENAME
- elseif (splitType == "v")
- silent! execute ":vsplit".bang." " . FILENAME
- elseif (splitType == "t")
- silent! execute ":tab split".bang." " . FILENAME
- else
- silent! execute ":e".bang." " . FILENAME
- endif
- if (v:errmsg != "")
- echo v:errmsg
- endif
- endif
- endif
- endif
- endfunction
-
-
-
-
-
- " "c:\WINDOWS" is the same path as "c:/windows". has("win32unix
-
-
-
-
-
- function! <SID>EqualFilePaths(path1, path2)
- if has("win16") || has("win32") || has("win64") || has("win95")
- return substitute(a:path1, "\/", "\\", "g") ==? substitute(a:path2, "\/", "\\", "g
- else
- return a:path1 == a:path2
- endif
- endfunction
|