From ac0ac71423eba77ee53d3c442435bbf964e9417c Mon Sep 17 00:00:00 2001 From: wond12 Date: Thu, 21 Nov 2019 20:46:08 +0800 Subject: [PATCH 1/3] switch in buffer --- plugin/fswitch.vim | 62 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 49 insertions(+), 13 deletions(-) diff --git a/plugin/fswitch.vim b/plugin/fswitch.vim index a0396c8..e16662b 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 " @@ -291,24 +321,30 @@ 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 + let newpathList = s:FSReturnCompanionFilenameList(a:filename, 0) + for np in newpathList + let exceptfilename = fnamemodify(np, ':t') + let i = 1 + let bufnum = bufnr(i) + while bufnum != -1 + let filename = fnamemodify(bufname(bufnum), ':p') + let justfile = fnamemodify(filename, ':t') + if justfile == exceptfilename + execute ":buffer " . filename + return + endif + let i += 1 + let bufnum = bufnr(i) + endwhile + endfor endif if openfile == 1 if newpath != '' From 852e2e2b2369b7e63afffe9309b9a5380202908f Mon Sep 17 00:00:00 2001 From: wond12 Date: Fri, 22 Nov 2019 12:46:30 +0800 Subject: [PATCH 2/3] fix a bug while switch in buffer --- plugin/fswitch.vim | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/plugin/fswitch.vim b/plugin/fswitch.vim index e16662b..e9f1ec3 100755 --- a/plugin/fswitch.vim +++ b/plugin/fswitch.vim @@ -304,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 " @@ -330,20 +343,16 @@ function! FSwitch(filename, precmd) endif if &switchbuf =~ "^use" let newpathList = s:FSReturnCompanionFilenameList(a:filename, 0) + silent let bufnames = s:GetBufferList() for np in newpathList let exceptfilename = fnamemodify(np, ':t') - let i = 1 - let bufnum = bufnr(i) - while bufnum != -1 - let filename = fnamemodify(bufname(bufnum), ':p') + for filename in bufnames let justfile = fnamemodify(filename, ':t') if justfile == exceptfilename execute ":buffer " . filename return endif - let i += 1 - let bufnum = bufnr(i) - endwhile + endfor endfor endif if openfile == 1 From 8f9a385815c70e367fb937c430298eedf82d37ac Mon Sep 17 00:00:00 2001 From: wond12 Date: Mon, 25 Nov 2019 10:52:31 +0800 Subject: [PATCH 3/3] add g:fsfindinbuffers --- doc/fswitch.txt | 7 +++++++ plugin/fswitch.vim | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) 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 e9f1ec3..4c0d844 100755 --- a/plugin/fswitch.vim +++ b/plugin/fswitch.vim @@ -341,7 +341,7 @@ function! FSwitch(filename, precmd) let openfile = 0 endif endif - if &switchbuf =~ "^use" + if exists("b:fsfindinbuffers") || exists("g:fsfindinbuffers") let newpathList = s:FSReturnCompanionFilenameList(a:filename, 0) silent let bufnames = s:GetBufferList() for np in newpathList