From cfa5db021590aa356a559a6fef21b28e009cb455 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Wed, 13 Jun 2018 15:39:47 +1200 Subject: Refactor tilde and extension stripping --- vim/filetype.vim | 80 +++++++++++++++++++++++++++++++------------------------- 1 file changed, 44 insertions(+), 36 deletions(-) (limited to 'vim/filetype.vim') diff --git a/vim/filetype.vim b/vim/filetype.vim index 09f9fcb6..2dc44889 100644 --- a/vim/filetype.vim +++ b/vim/filetype.vim @@ -7,40 +7,51 @@ if !has('autocmd') || &compatible finish endif -" Use our own filetype detection rules -augroup filetypedetect - autocmd! +" 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 - " Check whether fnameescape() exists to do some basic surgery on the - " filename being matched - if exists('*fnameescape') + " Expand the match result + let l:fn = expand('') - " Chop tildes - autocmd BufNewFile,BufRead - \ ?*~ - \ execute 'doautocmd filetypedetect BufRead ' - \ . fnameescape(substitute(expand(''), '\~$', '', '')) + " Strip leading and trailing #hashes# + if l:fn =~# '\m^#\+.*#\+$' + let l:fn = substitute(l:fn, '\m^#\+\(.\+\)#\+$', '\1', '') - " Chop some generic extensions - autocmd BufNewFile,BufRead - \ ?*.bak - \,?*.in - \,?*.new - \,?*.old - \,?*.orig - \ execute 'doautocmd filetypedetect BufRead ' - \ . fnameescape(expand(':r')) + " Strip trailing tilde~ + elseif l:fn =~# '\m\~$' + let l:fn = substitute(l:fn, '\~$', '', '') - " Chop some Debian working extensions - autocmd BufNewFile,BufRead - \ ?*.dpkg-bak - \,?*.dpkg-dist - \,?*.dpkg-new - \,?*.dpkg-old - \ execute 'doautocmd filetypedetect BufRead ' - \ . fnameescape(expand(':r')) + " 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 + +" Use our own filetype detection rules +augroup filetypedetect + autocmd! + + " Unwrap hashes, tildes, generic extensions, and Debian packaging working + " extensions (if we can do so safely), and repeat the filetype detection to + " see if there's a match beneath them + autocmd BufNewFile,BufRead + \ #?*# + \,?*~ + \,?*.{bak,example,in,new,old,orig,sample,test} + \,?*.dpkg-{bak,dist,new,old} + \ call s:StripRepeat() + " Stuff Tom cares about enough and edits often enough to type based on " filename patterns follows. @@ -458,14 +469,11 @@ augroup filetypedetect " Clumsy attempt at typing files in `sudo -e` if a filename hasn't already " been found; strip temporary extension and re-run - if exists('*fnameescape') - autocmd BufNewFile,BufRead - \ /var/tmp/?*.???????? - \ if !did_filetype() - \ | execute 'doautocmd filetypedetect BufRead ' - \ . fnameescape(expand(':r')) - \ | endif - endif + autocmd BufNewFile,BufRead + \ /var/tmp/?*.???????? + \ if !did_filetype() + \ | call s:StripRepeat() + \ | endif " If we *still* don't have a filetype, run the scripts.vim file that will " examine actual file contents--but only the first one; don't load the -- cgit v1.2.3