Skip to content

switch in buffer #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/fswitch.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
73 changes: 59 additions & 14 deletions plugin/fswitch.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
"
Expand All @@ -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
"
Expand All @@ -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 != ''
Expand Down