From c3e2ac92f72ac85967bdaf8d740d040f6e9c36b4 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Fri, 7 Dec 2018 13:42:30 +1300 Subject: Move filetype.vim helper funcs into autoload May as well, now that we've dropped support for versions of Vim that don't have it. --- vim/autoload/filetype.vim | 76 ++++++++++++++++++++++++++++++++++++++++++++ vim/filetype.vim | 81 ++--------------------------------------------- 2 files changed, 79 insertions(+), 78 deletions(-) create mode 100644 vim/autoload/filetype.vim diff --git a/vim/autoload/filetype.vim b/vim/autoload/filetype.vim new file mode 100644 index 00000000..d1e4e3d7 --- /dev/null +++ b/vim/autoload/filetype.vim @@ -0,0 +1,76 @@ +" Helper function to run the 'filetypedetect' group on a file with its +" extension stripped off +function! filetype#StripRepeat() abort + + " Check we have the fnameescape() function + if !exists('*fnameescape') + return + endif + + " Expand the match result + let l:fn = expand('') + + " Strip leading and trailing #hashes# + if l:fn =~# '^#\+.*#\+$' + let l:fn = substitute(l:fn, '^#\+\(.\+\)#\+$', '\1', '') + + " Strip trailing tilde~ + elseif l:fn =~# '\~$' + let l:fn = substitute(l:fn, '\~$', '', '') + + " Strip generic .extension + else + let l:fn = expand(':r') + endif + + " Re-run the group if there's anything left + if strlen(l:fn) + execute 'doautocmd filetypedetect BufRead ' . fnameescape(l:fn) + endif + +endfunction + +" Helper function to run the 'filetypedetect' group on a file in a temporary +" sudoedit(8) directory, modifying it with an attempt to reverse the temporary +" filename change +function! filetype#SudoRepeat() abort + + " Check we have the fnameescape() function + if !exists('*fnameescape') + return + endif + + " Expand the match result + let l:fn = expand('') + + " myfileXXQGS16A.conf: strip eight chars before final period + if l:fn =~# '/[^./]\+\w\{8}\.[^./]\+$' + let l:fr = expand(':r') + let l:fe = expand(':e') + let l:fn = strpart(l:fr, -8, strlen(l:fr)) . '.' . l:fe + + " myfile.XXQGS16A: strip extension + elseif l:fn =~# '/[^./]\+\.\w\{8}$' + let l:fn = expand(':r') + + " Unrecognised pattern; return, don't repeat + else + return + endif + + " Re-run the group if there's anything left + if strlen(l:fn) + execute 'doautocmd filetypedetect BufRead ' . fnameescape(l:fn) + endif + +endfunction + +" Check whether the first line was changed and looks like a shebang, and if +" so, re-run filetype detection +function! filetype#CheckShebang() abort + if line('''[') == 1 && getline(1) =~# '^#!' + doautocmd filetypedetect BufRead + endif +endfunction + + diff --git a/vim/filetype.vim b/vim/filetype.vim index cc0de4e4..3ac816d4 100644 --- a/vim/filetype.vim +++ b/vim/filetype.vim @@ -10,81 +10,6 @@ if !has('autocmd') || &compatible finish endif -" Helper function to run the 'filetypedetect' group on a file with its -" extension stripped off -function! s:StripRepeat() - - " Check we have the fnameescape() function - if !exists('*fnameescape') - return - endif - - " Expand the match result - let l:fn = expand('') - - " Strip leading and trailing #hashes# - if l:fn =~# '^#\+.*#\+$' - let l:fn = substitute(l:fn, '^#\+\(.\+\)#\+$', '\1', '') - - " Strip trailing tilde~ - elseif l:fn =~# '\~$' - let l:fn = substitute(l:fn, '\~$', '', '') - - " Strip generic .extension - else - let l:fn = expand(':r') - endif - - " Re-run the group if there's anything left - if strlen(l:fn) - execute 'doautocmd filetypedetect BufRead ' . fnameescape(l:fn) - endif - -endfunction - -" Helper function to run the 'filetypedetect' group on a file in a temporary -" sudoedit(8) directory, modifying it with an attempt to reverse the temporary -" filename change -function! s:SudoRepeat() - - " Check we have the fnameescape() function - if !exists('*fnameescape') - return - endif - - " Expand the match result - let l:fn = expand('') - - " myfileXXQGS16A.conf: strip eight chars before final period - if l:fn =~# '/[^./]\+\w\{8}\.[^./]\+$' - let l:fr = expand(':r') - let l:fe = expand(':e') - let l:fn = strpart(l:fr, -8, strlen(l:fr)) . '.' . l:fe - - " myfile.XXQGS16A: strip extension - elseif l:fn =~# '/[^./]\+\.\w\{8}$' - let l:fn = expand(':r') - - " Unrecognised pattern; return, don't repeat - else - return - endif - - " Re-run the group if there's anything left - if strlen(l:fn) - execute 'doautocmd filetypedetect BufRead ' . fnameescape(l:fn) - endif - -endfunction - -" Check whether the first line was changed and looks like a shebang, and if -" so, re-run filetype detection -function! s:CheckShebang() - if line('''[') == 1 && getline(1) =~# '^#!' - doautocmd filetypedetect BufRead - endif -endfunction - " Use our own filetype detection rules augroup filetypedetect autocmd! @@ -97,7 +22,7 @@ augroup filetypedetect \,?*~ \,?*.{bak,example,in,new,old,orig,sample,test} \,?*.dpkg-{bak,dist,new,old} - \ call s:StripRepeat() + \ call filetype#StripRepeat() " Stuff Tom cares about enough and edits often enough to type based on " filename patterns follows. @@ -534,7 +459,7 @@ augroup filetypedetect \ /var/tmp/?*????????.* \,/var/tmp/?*.???????? \ if !did_filetype() - \| call s:SudoRepeat() + \| call filetype#SudoRepeat() \|endif " Generic text, config, and log files, if no type assigned yet @@ -568,6 +493,6 @@ augroup filetypedetect " On leaving insert mode, check whether the first line was changed and looks " like a shebang format, and if so, re-run filetype detection - autocmd InsertLeave * call s:CheckShebang() + autocmd InsertLeave * call filetype#CheckShebang() augroup END -- cgit v1.2.3