From 1167bf8d4f478288482f2e72718bdf24971aa332 Mon Sep 17 00:00:00 2001 From: Mohamed BOUGHABA Date: Mon, 30 Oct 2017 20:04:49 +0100 Subject: [PATCH 1/4] Add autoload support Add autoload folder including all exposed and scoped functions. Add loading and autoloading guards. Bump version and last update date. --- autoload/fswitch.vim | 324 +++++++++++++++++++++++++++++++++++++++ plugin/fswitch.vim | 349 +++---------------------------------------- 2 files changed, 348 insertions(+), 325 deletions(-) create mode 100644 autoload/fswitch.vim diff --git a/autoload/fswitch.vim b/autoload/fswitch.vim new file mode 100644 index 0000000..1a46c41 --- /dev/null +++ b/autoload/fswitch.vim @@ -0,0 +1,324 @@ +" ============================================================================ +" File: fswitch.vim +" +" Description: Vim global plugin that provides decent companion source file +" switching +" +" Maintainer: Derek Wyatt +" +" Last Change: October 30th 2017 +" +" License: This program is free software. It comes without any warranty, +" to the extent permitted by applicable law. You can redistribute +" it and/or modify it under the terms of the Do What The Fuck You +" Want To Public License, Version 2, as published by Sam Hocevar. +" See http://sam.zoy.org/wtfpl/COPYING for more details. +" ============================================================================ +if exists('g:autoloaded_fswitch') || &cp || version < 700 + finish +endif +let g:autoloaded_fswitch = 1 + +" +" s:SetVariables +" +" There are two variables that need to be set in the buffer in order for things +" to work correctly. Because we're using an autocmd to set things up we need to +" be sure that the user hasn't already set them for us explicitly so we have +" this function just to check and make sure. If the user's autocmd runs after +" ours then they will override the value anyway. +" +function! s:SetVariables(dst, locs) + if !exists("b:fswitchdst") + let b:fswitchdst = a:dst + endif + if !exists("b:fswitchlocs") + let b:fswitchlocs = a:locs + endif +endfunction + +" +" s:FSGetLocations +" +" Return the list of possible locations +" +function! s:FSGetLocations() + let locations = [] + if exists("b:fswitchlocs") + let locations = split(b:fswitchlocs, ',') + endif + if !exists("b:fsdisablegloc") || b:fsdisablegloc == 0 + let locations += split(s:fswitch_global_locs, ',') + endif + + return locations +endfunction + +" +" s:FSGetExtensions +" +" Return the list of destination extensions +" +function! s:FSGetExtensions() + return split(b:fswitchdst, ',') +endfunction + +" +" s:FSGetFilenameMutations +" +" Return the list of possible filename mutations +" +function! s:FSGetFilenameMutations() + if !exists("b:fswitchfnames") + " For backward-compatibility out default mutation is an identity. + return ['/^//'] + else + return split(b:fswitchfnames, ',') + endif +endfunction + +" +" s:FSGetMustMatch +" +" Return a boolean on whether or not the regex must match +" +function! s:FSGetMustMatch() + let mustmatch = 1 + if exists("b:fsneednomatch") && b:fsneednomatch != 0 + let mustmatch = 0 + endif + + return mustmatch +endfunction + +" +" s:FSGetFullPathToDirectory +" +" Given the filename, return the fully qualified directory portion +" +function! s:FSGetFullPathToDirectory(filename) + return expand(a:filename . ':p:h') +endfunction + +" +" s:FSGetFileExtension +" +" Given the filename, returns the extension +" +function! s:FSGetFileExtension(filename) + return expand(a:filename . ':e') +endfunction + +" +" s:FSGetFileNameWithoutExtension +" +" Given the filename, returns just the file name without the path or extension +" +function! s:FSGetFileNameWithoutExtension(filename) + return expand(a:filename . ':t:r') +endfunction + +" +" s:FSMutateFilename +" +" Takes a filename and a filename mutation directive and applies the mutation +" to it. +function! s:FSMutateFilename(filename, directive) + let separator = strpart(a:directive, 0, 1) + let dirparts = split(strpart(a:directive, 1), separator) + if len(dirparts) < 2 || len(dirparts) > 3 + throw 'Bad mutation directive "' . a:directive . '".' + else + let flags = '' + if len(dirparts) == 3 + let flags = dirparts[2] + endif + return substitute(a:filename, dirparts[0], dirparts[1], flags) + endif +endfunction + +" +" s:FSGetAlternateFilename +" +" Takes the path, name and extension of the file in the current buffer and +" applies the location to it. If the location is a regular expression pattern +" then it will split that up and apply it accordingly. If the location pattern +" is actually an explicit relative path or an implicit one (default) then it +" will simply apply that to the file directly. +" +function! s:FSGetAlternateFilename(filepath, filename, newextension, location, mustmatch) + let parts = split(a:location, ':') + let cmd = 'rel' + let directive = parts[0] + if len(parts) == 2 + let cmd = parts[0] + let directive = parts[1] + endif + if cmd == 'reg' || cmd == 'ifrel' || cmd == 'ifabs' + if strlen(directive) < 3 + throw 'Bad directive "' . a:location . '".' + else + let separator = strpart(directive, 0, 1) + let dirparts = split(strpart(directive, 1), separator) + if len(dirparts) < 2 || len(dirparts) > 3 + throw 'Bad directive "' . a:location . '".' + else + let part1 = dirparts[0] + let part2 = dirparts[1] + let flags = '' + if len(dirparts) == 3 + let flags = dirparts[2] + endif + if cmd == 'reg' + if a:mustmatch == 1 && match(a:filepath, part1) == -1 + let path = "" + else + let path = substitute(a:filepath, part1, part2, flags) . s:os_slash . + \ a:filename . '.' . a:newextension + endif + elseif cmd == 'ifrel' + if match(a:filepath, part1) == -1 + let path = "" + else + let path = a:filepath . s:os_slash . part2 . + \ s:os_slash . a:filename . '.' . a:newextension + endif + elseif cmd == 'ifabs' + if match(a:filepath, part1) == -1 + let path = "" + else + let path = part2 . s:os_slash . a:filename . '.' . a:newextension + endif + endif + endif + endif + elseif cmd == 'rel' + let path = a:filepath . s:os_slash . directive . s:os_slash . a:filename . '.' . a:newextension + elseif cmd == 'abs' + let path = directive . s:os_slash . a:filename . '.' . a:newextension + endif + + return simplify(path) +endfunction + +" +" s:FSReturnCompanionFilename +" +" This function will return a path that is the best candidate for the companion +" file to switch to. If mustBeReadable == 1 when then the companion file will +" only be returned if it is readable on the filesystem, otherwise it will be +" returned so long as it is non-empty. +" +function! s:FSReturnCompanionFilename(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 = '' + 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 != '' + return newpath + elseif a:mustBeReadable == 1 + let newpath = glob(newpath) + if filereadable(newpath) + return newpath + endif + endif + endfor + endfor + endfor + + return newpath +endfunction + +" +" FSReturnReadableCompanionFilename +" +" This function will return a path that is the best candidate for the companion +" file to switch to, so long as that file actually exists on the filesystem and +" is readable. +" +function! FSReturnReadableCompanionFilename(filename) + return s:FSReturnCompanionFilename(a:filename, 1) +endfunction + +" +" FSReturnCompanionFilenameString +" +" This function will return a path that is the best candidate for the companion +" file to switch to. The file does not need to actually exist on the +" filesystem in order to qualify as a proper companion. +" +function! FSReturnCompanionFilenameString(filename) + return s:FSReturnCompanionFilename(a:filename, 0) +endfunction + +" +" FTInit +" +" Wrapper function to be used to set companions +" +function! FTInit(dst, locs) + s:SetVariables(dst, locs) +endfunction + +" +" FSwitch +" +" This is the only externally accessible function and is what we use to switch +" to the alternate file. +" +function! FSwitch(filename, precmd) + if !exists("b:fswitchdst") || strlen(b:fswitchdst) == 0 + throw 'b:fswitchdst not set - read :help fswitch' + endif + if (!exists("b:fswitchlocs") || strlen(b:fswitchlocs) == 0) && + \ (!exists("b:fsdisablegloc") || b:fsdisablegloc == 0) + throw "There are no locations defined (see :h fswitchlocs and :h fsdisablegloc)" + endif + let newpath = FSReturnReadableCompanionFilename(a:filename) + let openfile = 1 + if !filereadable(newpath) + 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 + endif + if openfile == 1 + if newpath != '' + if strlen(a:precmd) != 0 + execute a:precmd + endif + if bufexists(newpath) + execute 'buffer ' . fnameescape(newpath) + else + execute 'edit ' . fnameescape(newpath) + endif + else + echoerr "Alternate has evaluated to nothing. See :h fswitch-empty for more info." + endif + else + echoerr "No alternate file found. 'fsnonewfiles' is set which denies creation." + endif +endfunction diff --git a/plugin/fswitch.vim b/plugin/fswitch.vim index 68fd958..e71fdc5 100755 --- a/plugin/fswitch.vim +++ b/plugin/fswitch.vim @@ -6,7 +6,7 @@ " " Maintainer: Derek Wyatt " -" Last Change: March 23rd 2009 +" Last Change: October 30th 2017 " " License: This program is free software. It comes without any warranty, " to the extent permitted by applicable law. You can redistribute @@ -14,18 +14,13 @@ " Want To Public License, Version 2, as published by Sam Hocevar. " See http://sam.zoy.org/wtfpl/COPYING for more details. " ============================================================================ - -if exists("g:disable_fswitch") +if exists('g:loaded_fswitch') || exists("g:disable_fswitch") || &cp || version < 700 finish endif - -if v:version < 700 - echoerr "FSwitch requires Vim 7.0 or higher." - finish -endif +let g:loaded_fswitch = 1 " Version -let s:fswitch_version = '0.9.5' +let s:fswitch_version = '0.9.6' " Get the path separator right let s:os_slash = &ssl == 0 && (has("win16") || has("win32") || has("win64")) ? '\' : '/' @@ -33,329 +28,33 @@ let s:os_slash = &ssl == 0 && (has("win16") || has("win32") || has("win64")) ? ' " Default locations - appended to buffer locations unless otherwise specified let s:fswitch_global_locs = '.' . s:os_slash -" -" s:SetVariables -" -" There are two variables that need to be set in the buffer in order for things -" to work correctly. Because we're using an autocmd to set things up we need to -" be sure that the user hasn't already set them for us explicitly so we have -" this function just to check and make sure. If the user's autocmd runs after -" ours then they will override the value anyway. -" -function! s:SetVariables(dst, locs) - if !exists("b:fswitchdst") - let b:fswitchdst = a:dst - endif - if !exists("b:fswitchlocs") - let b:fswitchlocs = a:locs - endif -endfunction - -" -" s:FSGetLocations -" -" Return the list of possible locations -" -function! s:FSGetLocations() - let locations = [] - if exists("b:fswitchlocs") - let locations = split(b:fswitchlocs, ',') - endif - if !exists("b:fsdisablegloc") || b:fsdisablegloc == 0 - let locations += split(s:fswitch_global_locs, ',') - endif - - return locations -endfunction - -" -" s:FSGetExtensions -" -" Return the list of destination extensions -" -function! s:FSGetExtensions() - return split(b:fswitchdst, ',') -endfunction - -" -" s:FSGetFilenameMutations -" -" Return the list of possible filename mutations -" -function! s:FSGetFilenameMutations() - if !exists("b:fswitchfnames") - " For backward-compatibility out default mutation is an identity. - return ['/^//'] - else - return split(b:fswitchfnames, ',') - endif -endfunction - -" -" s:FSGetMustMatch -" -" Return a boolean on whether or not the regex must match -" -function! s:FSGetMustMatch() - let mustmatch = 1 - if exists("b:fsneednomatch") && b:fsneednomatch != 0 - let mustmatch = 0 - endif - - return mustmatch -endfunction - -" -" s:FSGetFullPathToDirectory -" -" Given the filename, return the fully qualified directory portion -" -function! s:FSGetFullPathToDirectory(filename) - return expand(a:filename . ':p:h') -endfunction - -" -" s:FSGetFileExtension -" -" Given the filename, returns the extension -" -function! s:FSGetFileExtension(filename) - return expand(a:filename . ':e') -endfunction - -" -" s:FSGetFileNameWithoutExtension -" -" Given the filename, returns just the file name without the path or extension -" -function! s:FSGetFileNameWithoutExtension(filename) - return expand(a:filename . ':t:r') -endfunction - -" -" s:FSMutateFilename -" -" Takes a filename and a filename mutation directive and applies the mutation -" to it. -function! s:FSMutateFilename(filename, directive) - let separator = strpart(a:directive, 0, 1) - let dirparts = split(strpart(a:directive, 1), separator) - if len(dirparts) < 2 || len(dirparts) > 3 - throw 'Bad mutation directive "' . a:directive . '".' - else - let flags = '' - if len(dirparts) == 3 - let flags = dirparts[2] - endif - return substitute(a:filename, dirparts[0], dirparts[1], flags) - endif -endfunction - -" -" s:FSGetAlternateFilename -" -" Takes the path, name and extension of the file in the current buffer and -" applies the location to it. If the location is a regular expression pattern -" then it will split that up and apply it accordingly. If the location pattern -" is actually an explicit relative path or an implicit one (default) then it -" will simply apply that to the file directly. -" -function! s:FSGetAlternateFilename(filepath, filename, newextension, location, mustmatch) - let parts = split(a:location, ':') - let cmd = 'rel' - let directive = parts[0] - if len(parts) == 2 - let cmd = parts[0] - let directive = parts[1] - endif - if cmd == 'reg' || cmd == 'ifrel' || cmd == 'ifabs' - if strlen(directive) < 3 - throw 'Bad directive "' . a:location . '".' - else - let separator = strpart(directive, 0, 1) - let dirparts = split(strpart(directive, 1), separator) - if len(dirparts) < 2 || len(dirparts) > 3 - throw 'Bad directive "' . a:location . '".' - else - let part1 = dirparts[0] - let part2 = dirparts[1] - let flags = '' - if len(dirparts) == 3 - let flags = dirparts[2] - endif - if cmd == 'reg' - if a:mustmatch == 1 && match(a:filepath, part1) == -1 - let path = "" - else - let path = substitute(a:filepath, part1, part2, flags) . s:os_slash . - \ a:filename . '.' . a:newextension - endif - elseif cmd == 'ifrel' - if match(a:filepath, part1) == -1 - let path = "" - else - let path = a:filepath . s:os_slash . part2 . - \ s:os_slash . a:filename . '.' . a:newextension - endif - elseif cmd == 'ifabs' - if match(a:filepath, part1) == -1 - let path = "" - else - let path = part2 . s:os_slash . a:filename . '.' . a:newextension - endif - endif - endif - endif - elseif cmd == 'rel' - let path = a:filepath . s:os_slash . directive . s:os_slash . a:filename . '.' . a:newextension - elseif cmd == 'abs' - let path = directive . s:os_slash . a:filename . '.' . a:newextension - endif - - return simplify(path) -endfunction - -" -" s:FSReturnCompanionFilename -" -" This function will return a path that is the best candidate for the companion -" file to switch to. If mustBeReadable == 1 when then the companion file will -" only be returned if it is readable on the filesystem, otherwise it will be -" returned so long as it is non-empty. -" -function! s:FSReturnCompanionFilename(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 = '' - 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 != '' - return newpath - elseif a:mustBeReadable == 1 - let newpath = glob(newpath) - if filereadable(newpath) - return newpath - endif - endif - endfor - endfor - endfor - - return newpath -endfunction - -" -" FSReturnReadableCompanionFilename -" -" This function will return a path that is the best candidate for the companion -" file to switch to, so long as that file actually exists on the filesystem and -" is readable. -" -function! FSReturnReadableCompanionFilename(filename) - return s:FSReturnCompanionFilename(a:filename, 1) -endfunction - -" -" FSReturnCompanionFilenameString -" -" This function will return a path that is the best candidate for the companion -" file to switch to. The file does not need to actually exist on the -" filesystem in order to qualify as a proper companion. -" -function! FSReturnCompanionFilenameString(filename) - return s:FSReturnCompanionFilename(a:filename, 0) -endfunction - -" -" FSwitch -" -" This is the only externally accessible function and is what we use to switch -" to the alternate file. -" -function! FSwitch(filename, precmd) - if !exists("b:fswitchdst") || strlen(b:fswitchdst) == 0 - throw 'b:fswitchdst not set - read :help fswitch' - endif - if (!exists("b:fswitchlocs") || strlen(b:fswitchlocs) == 0) && - \ (!exists("b:fsdisablegloc") || b:fsdisablegloc == 0) - throw "There are no locations defined (see :h fswitchlocs and :h fsdisablegloc)" - endif - let newpath = FSReturnReadableCompanionFilename(a:filename) - let openfile = 1 - if !filereadable(newpath) - 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 - endif - if openfile == 1 - if newpath != '' - if strlen(a:precmd) != 0 - execute a:precmd - endif - if bufexists(newpath) - execute 'buffer ' . fnameescape(newpath) - else - execute 'edit ' . fnameescape(newpath) - endif - else - echoerr "Alternate has evaluated to nothing. See :h fswitch-empty for more info." - endif - else - echoerr "No alternate file found. 'fsnonewfiles' is set which denies creation." - endif -endfunction - " " The autocmds we set up to set up the buffer variables for us. " augroup fswitch_au_group au! - au BufEnter *.c call s:SetVariables('h', 'reg:/src/include/,reg:|src|include/**|,ifrel:|/src/|../include|') - au BufEnter *.cc call s:SetVariables('hh', 'reg:/src/include/,reg:|src|include/**|,ifrel:|/src/|../include|') - au BufEnter *.cpp call s:SetVariables('hpp,h', 'reg:/src/include/,reg:|src|include/**|,ifrel:|/src/|../include|') - au BufEnter *.cxx call s:SetVariables('hxx', 'reg:/src/include/,reg:|src|include/**|,ifrel:|/src/|../include|') - au BufEnter *.C call s:SetVariables('H', 'reg:/src/include/,reg:|src|include/**|,ifrel:|/src/|../include|') - au BufEnter *.m call s:SetVariables('h', 'reg:/src/include/,reg:|src|include/**|,ifrel:|/src/|../include|') - au BufEnter *.h call s:SetVariables('c,cpp,m', 'reg:/include/src/,reg:/include.*/src/,ifrel:|/include/|../src|') - au BufEnter *.hh call s:SetVariables('cc', 'reg:/include/src/,reg:/include.*/src/,ifrel:|/include/|../src|') - au BufEnter *.hpp call s:SetVariables('cpp', 'reg:/include/src/,reg:/include.*/src/,ifrel:|/include/|../src|') - au BufEnter *.hxx call s:SetVariables('cxx', 'reg:/include/src/,reg:/include.*/src/,ifrel:|/include/|../src|') - au BufEnter *.H call s:SetVariables('C', 'reg:/include/src/,reg:/include.*/src/,ifrel:|/include/|../src|') + au BufEnter *.c call fswitch#FTInit('h', 'reg:/src/include/,reg:|src|include/**|,ifrel:|/src/|../include|') + au BufEnter *.cc call fswitch#FTInit('hh', 'reg:/src/include/,reg:|src|include/**|,ifrel:|/src/|../include|') + au BufEnter *.cpp call fswitch#FTInit('hpp,h', 'reg:/src/include/,reg:|src|include/**|,ifrel:|/src/|../include|') + au BufEnter *.cxx call fswitch#FTInit('hxx', 'reg:/src/include/,reg:|src|include/**|,ifrel:|/src/|../include|') + au BufEnter *.C call fswitch#FTInit('H', 'reg:/src/include/,reg:|src|include/**|,ifrel:|/src/|../include|') + au BufEnter *.m call fswitch#FTInit('h', 'reg:/src/include/,reg:|src|include/**|,ifrel:|/src/|../include|') + au BufEnter *.h call fswitch#FTInit('c,cpp,m', 'reg:/include/src/,reg:/include.*/src/,ifrel:|/include/|../src|') + au BufEnter *.hh call fswitch#FTInit('cc', 'reg:/include/src/,reg:/include.*/src/,ifrel:|/include/|../src|') + au BufEnter *.hpp call fswitch#FTInit('cpp', 'reg:/include/src/,reg:/include.*/src/,ifrel:|/include/|../src|') + au BufEnter *.hxx call fswitch#FTInit('cxx', 'reg:/include/src/,reg:/include.*/src/,ifrel:|/include/|../src|') + au BufEnter *.H call fswitch#FTInit('C', 'reg:/include/src/,reg:/include.*/src/,ifrel:|/include/|../src|') augroup END " " The mappings used to do the good work " -com! FSHere :call FSwitch('%', '') -com! FSRight :call FSwitch('%', 'wincmd l') -com! FSSplitRight :call FSwitch('%', 'let curspr=&spr | set nospr | vsplit | wincmd l | if curspr | set spr | endif') -com! FSLeft :call FSwitch('%', 'wincmd h') -com! FSSplitLeft :call FSwitch('%', 'let curspr=&spr | set nospr | vsplit | if curspr | set spr | endif') -com! FSAbove :call FSwitch('%', 'wincmd k') -com! FSSplitAbove :call FSwitch('%', 'let cursb=&sb | set nosb | split | if cursb | set sb | endif') -com! FSBelow :call FSwitch('%', 'wincmd j') -com! FSSplitBelow :call FSwitch('%', 'let cursb=&sb | set nosb | split | wincmd j | if cursb | set sb | endif') - +com! FSHere :call fswitch#FSwitch('%', '') +com! FSRight :call fswitch#FSwitch('%', 'wincmd l') +com! FSSplitRight :call fswitch#FSwitch('%', 'let curspr=&spr | set nospr | vsplit | wincmd l | if curspr | set spr | endif') +com! FSLeft :call fswitch#FSwitch('%', 'wincmd h') +com! FSSplitLeft :call fswitch#FSwitch('%', 'let curspr=&spr | set nospr | vsplit | if curspr | set spr | endif') +com! FSAbove :call fswitch#FSwitch('%', 'wincmd k') +com! FSSplitAbove :call fswitch#FSwitch('%', 'let cursb=&sb | set nosb | split | if cursb | set sb | endif') +com! FSBelow :call fswitch#FSwitch('%', 'wincmd j') +com! FSSplitBelow :call fswitch#FSwitch('%', 'let cursb=&sb | set nosb | split | wincmd j | if cursb | set sb | endif') From f4bf2a68109ee08c5bc71fa75e84544d6506b5b4 Mon Sep 17 00:00:00 2001 From: Mohamed BOUGHABA Date: Mon, 30 Oct 2017 20:11:41 +0100 Subject: [PATCH 2/4] Add autoload support Add missing namespaces --- autoload/fswitch.vim | 4 ++-- doc/fswitch.txt | 25 ++++++++++++++----------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/autoload/fswitch.vim b/autoload/fswitch.vim index 1a46c41..4428363 100644 --- a/autoload/fswitch.vim +++ b/autoload/fswitch.vim @@ -265,7 +265,7 @@ endfunction " " Wrapper function to be used to set companions " -function! FTInit(dst, locs) +function! fswitch#FTInit(dst, locs) s:SetVariables(dst, locs) endfunction @@ -275,7 +275,7 @@ endfunction " This is the only externally accessible function and is what we use to switch " to the alternate file. " -function! FSwitch(filename, precmd) +function! fswitch#FSwitch(filename, precmd) if !exists("b:fswitchdst") || strlen(b:fswitchdst) == 0 throw 'b:fswitchdst not set - read :help fswitch' endif diff --git a/doc/fswitch.txt b/doc/fswitch.txt index 688f7ef..21542bb 100644 --- a/doc/fswitch.txt +++ b/doc/fswitch.txt @@ -1,7 +1,7 @@ *fswitch.txt* For Vim version 7.2 and above Last change: 2009 Mar 23 --------------- - File Switcher + File Switcher --------------- Author: Derek Wyatt (derek at myfirstnamemylastname dot org) @@ -131,27 +131,27 @@ get that move on to |fswitch-configure|. {delim}{pat}{delim}{globsub}{delim} < Where: - + {delim} is something that doesn't appear in {pat} or {globsub} used to delimit the {pat} and {globsub} {pat} is a standard pattern to search on {globsub} is a substitution string that will be run through - the |glob()| function. + the |glob()| function. *fswitch_rel* rel:~ A relative path. The {rel:} is actually optional. If you leave this off, then FSwitch will assume that the string is denoting a relative path. - + *fswitch_ifrel* ifrel:~ Takes the same form as {:reg} but the {globsub} part of the directive is a relative path. The relative path is only used if the {pat} matches the existing path of the buffer. - + *fswitch_abs* abs:~ An absolute path. I have no idea why you'd ever want to do @@ -162,7 +162,7 @@ get that move on to |fswitch-configure|. Takes the same form as {:reg} but the {globsub} part of the directive is an absolute path. The absolute path is only used if the {pat} matches the existing path of the buffer. - + Why use the "if" variants? Here's the situation: You've got the following file: @@ -199,7 +199,7 @@ get that move on to |fswitch-configure|. 'fsdisablegloc' string (default off) local to buffer - + Disables the appending of the default global locations to the local buffer definition. Normally when processing alternate file locations FSwitch will append some default values to the list of locations. If @@ -221,7 +221,7 @@ get that move on to |fswitch-configure|. This may be useful when the companion file may can be formulated by adding a suffix to the base filename. For instance, when the companion of MyClass.java is MyClassTest.java. - + For example: > " Create the destination filename by appending 'Test' @@ -236,7 +236,7 @@ get that move on to |fswitch-configure|. 'fsnonewfiles' string (default off) local to buffer and global - + This variable is both global and local. If you want to disable the creation of the alternate file when it doesn't already exist you can choose to do this on a per-extension basis or globally. Set the @@ -247,7 +247,7 @@ get that move on to |fswitch-configure|. 'fsneednomatch' string (default off) local to buffer and global - + Normally when doing a regular expression alteration of the path (see {reg:} in 'fswitchdst' the pattern you're going to substitute the value with must actually match in the string. When it doesn't matter @@ -498,7 +498,7 @@ The fswitchlocs setting allows for the following: possibility: - Files are in {include/name/space} and {src} respectively - + ./~ This one's a hiddden option. The default location is the current directory already so we don't explicitly have to state @@ -570,6 +570,9 @@ path. *fswitch-changes* A. Change History~ +0.9.6 + - Added autoload support. Now most of the plugin code is lazy + loaded. 0.9.5 - Modified the autocommands to handle the myriad different formulations of C++ files per Hong Xu's request. See: From 54c8bbf3f9ab1c5f755bda6f55e8228dca56cbbe Mon Sep 17 00:00:00 2001 From: Mohamed BOUGHABA Date: Mon, 30 Oct 2017 20:17:00 +0100 Subject: [PATCH 3/4] Add autoload support --- autoload/fswitch.vim | 35 +++++++++++++---------------------- 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/autoload/fswitch.vim b/autoload/fswitch.vim index 4428363..0f91850 100644 --- a/autoload/fswitch.vim +++ b/autoload/fswitch.vim @@ -19,24 +19,6 @@ if exists('g:autoloaded_fswitch') || &cp || version < 700 endif let g:autoloaded_fswitch = 1 -" -" s:SetVariables -" -" There are two variables that need to be set in the buffer in order for things -" to work correctly. Because we're using an autocmd to set things up we need to -" be sure that the user hasn't already set them for us explicitly so we have -" this function just to check and make sure. If the user's autocmd runs after -" ours then they will override the value anyway. -" -function! s:SetVariables(dst, locs) - if !exists("b:fswitchdst") - let b:fswitchdst = a:dst - endif - if !exists("b:fswitchlocs") - let b:fswitchlocs = a:locs - endif -endfunction - " " s:FSGetLocations " @@ -181,7 +163,7 @@ function! s:FSGetAlternateFilename(filepath, filename, newextension, location, m let path = "" else let path = a:filepath . s:os_slash . part2 . - \ s:os_slash . a:filename . '.' . a:newextension + \ s:os_slash . a:filename . '.' . a:newextension endif elseif cmd == 'ifabs' if match(a:filepath, part1) == -1 @@ -263,10 +245,19 @@ endfunction " " FTInit " -" Wrapper function to be used to set companions +" There are two variables that need to be set in the buffer in order for things +" to work correctly. Because we're using an autocmd to set things up we need to +" be sure that the user hasn't already set them for us explicitly so we have +" this function just to check and make sure. If the user's autocmd runs after +" ours then they will override the value anyway. " function! fswitch#FTInit(dst, locs) - s:SetVariables(dst, locs) + if !exists("b:fswitchdst") + let b:fswitchdst = a:dst + endif + if !exists("b:fswitchlocs") + let b:fswitchlocs = a:locs + endif endfunction " @@ -280,7 +271,7 @@ function! fswitch#FSwitch(filename, precmd) throw 'b:fswitchdst not set - read :help fswitch' endif if (!exists("b:fswitchlocs") || strlen(b:fswitchlocs) == 0) && - \ (!exists("b:fsdisablegloc") || b:fsdisablegloc == 0) + \ (!exists("b:fsdisablegloc") || b:fsdisablegloc == 0) throw "There are no locations defined (see :h fswitchlocs and :h fsdisablegloc)" endif let newpath = FSReturnReadableCompanionFilename(a:filename) From 76cb726e6bf557a86e0c1c0f907971c854c114fe Mon Sep 17 00:00:00 2001 From: Mohamed BOUGHABA Date: Mon, 30 Oct 2017 21:03:44 +0100 Subject: [PATCH 4/4] Move local variable to autoload --- autoload/fswitch.vim | 11 ++++++++++- plugin/fswitch.vim | 11 +---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/autoload/fswitch.vim b/autoload/fswitch.vim index 0f91850..c8e75c3 100644 --- a/autoload/fswitch.vim +++ b/autoload/fswitch.vim @@ -17,7 +17,15 @@ if exists('g:autoloaded_fswitch') || &cp || version < 700 finish endif -let g:autoloaded_fswitch = 1 + +" Version +let s:fswitch_version = '0.9.6' + +" Get the path separator right +let s:os_slash = &ssl == 0 && (has("win16") || has("win32") || has("win64")) ? '\' : '/' + +" Default locations - appended to buffer locations unless otherwise specified +let s:fswitch_global_locs = '.' . s:os_slash " " s:FSGetLocations @@ -313,3 +321,4 @@ function! fswitch#FSwitch(filename, precmd) echoerr "No alternate file found. 'fsnonewfiles' is set which denies creation." endif endfunction +let g:autoloaded_fswitch = 1 diff --git a/plugin/fswitch.vim b/plugin/fswitch.vim index e71fdc5..4e66567 100755 --- a/plugin/fswitch.vim +++ b/plugin/fswitch.vim @@ -17,16 +17,6 @@ if exists('g:loaded_fswitch') || exists("g:disable_fswitch") || &cp || version < 700 finish endif -let g:loaded_fswitch = 1 - -" Version -let s:fswitch_version = '0.9.6' - -" Get the path separator right -let s:os_slash = &ssl == 0 && (has("win16") || has("win32") || has("win64")) ? '\' : '/' - -" Default locations - appended to buffer locations unless otherwise specified -let s:fswitch_global_locs = '.' . s:os_slash " " The autocmds we set up to set up the buffer variables for us. @@ -58,3 +48,4 @@ com! FSAbove :call fswitch#FSwitch('%', 'wincmd k') com! FSSplitAbove :call fswitch#FSwitch('%', 'let cursb=&sb | set nosb | split | if cursb | set sb | endif') com! FSBelow :call fswitch#FSwitch('%', 'wincmd j') com! FSSplitBelow :call fswitch#FSwitch('%', 'let cursb=&sb | set nosb | split | wincmd j | if cursb | set sb | endif') +let g:loaded_fswitch = 1