diff --git a/doc/fswitch.txt b/doc/fswitch.txt index 688f7ef..68c1592 100644 --- a/doc/fswitch.txt +++ b/doc/fswitch.txt @@ -242,6 +242,13 @@ get that move on to |fswitch-configure|. choose to do this on a per-extension basis or globally. Set the global one to shut it off all the time and use the buffer version to shut it off locally. + *'fsfindinbuffers'* +'fsfindinbuffers' + string (default off) + local to buffer and global + + This variable is both global and local. If you want to find a matched + file in buffers, you can set this value. *'fsneednomatch'* 'fsneednomatch' diff --git a/plugin/fswitch.vim b/plugin/fswitch.vim index a0396c8..4c0d844 100755 --- a/plugin/fswitch.vim +++ b/plugin/fswitch.vim @@ -252,6 +252,36 @@ function! s:FSReturnCompanionFilename(filename, mustBeReadable) return newpath endfunction +function! s:FSReturnCompanionFilenameList(filename, mustBeReadable) + let fullpath = s:FSGetFullPathToDirectory(a:filename) + let ext = s:FSGetFileExtension(a:filename) + let justfile = s:FSGetFileNameWithoutExtension(a:filename) + let extensions = s:FSGetExtensions() + let filenameMutations = s:FSGetFilenameMutations() + let locations = s:FSGetLocations() + let mustmatch = s:FSGetMustMatch() + let newpath = '' + let newpathList = [] + for currentExt in extensions + for loc in locations + for filenameMutation in filenameMutations + let mutatedFilename = s:FSMutateFilename(justfile, filenameMutation) + let newpath = s:FSGetAlternateFilename(fullpath, mutatedFilename, currentExt, loc, mustmatch) + if a:mustBeReadable == 0 && newpath != '' + call add(newpathList, newpath) + elseif a:mustBeReadable == 1 + let newpath = glob(newpath) + if filereadable(newpath) + call add(newpathList, newpath) + endif + endif + endfor + endfor + endfor + + return newpathList +endfunction + " " FSReturnReadableCompanionFilename " @@ -274,6 +304,19 @@ function! FSReturnCompanionFilenameString(filename) return s:FSReturnCompanionFilename(a:filename, 0) endfunction +function! s:GetBufferList() + redir => bufoutput + buffers + redir END + let bufoutput = split(bufoutput, '\n') + let bufnames = [] + for bufn in bufoutput + let bits = split(bufn, '"') + call add(bufnames, fnamemodify(bufname(str2nr(bits[0])), ':p')) + endfor + return bufnames +endfunction + " " FSwitch " @@ -291,24 +334,26 @@ function! FSwitch(filename, precmd) let newpath = FSReturnReadableCompanionFilename(a:filename) let openfile = 1 if !filereadable(newpath) + if newpath == '' + let newpath = FSReturnCompanionFilenameString(a:filename) + endif if exists("b:fsnonewfiles") || exists("g:fsnonewfiles") let openfile = 0 - else - let newpath = FSReturnCompanionFilenameString(a:filename) endif endif - if &switchbuf =~ "^use" - let i = 1 - let bufnum = winbufnr(i) - while bufnum != -1 - let filename = fnamemodify(bufname(bufnum), ':p') - if filename == newpath - execute ":sbuffer " . filename - return - endif - let i += 1 - let bufnum = winbufnr(i) - endwhile + if exists("b:fsfindinbuffers") || exists("g:fsfindinbuffers") + let newpathList = s:FSReturnCompanionFilenameList(a:filename, 0) + silent let bufnames = s:GetBufferList() + for np in newpathList + let exceptfilename = fnamemodify(np, ':t') + for filename in bufnames + let justfile = fnamemodify(filename, ':t') + if justfile == exceptfilename + execute ":buffer " . filename + return + endif + endfor + endfor endif if openfile == 1 if newpath != ''