aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2018-06-13 15:39:47 +1200
committerTom Ryder <tom@sanctum.geek.nz>2018-06-13 15:46:22 +1200
commitcfa5db021590aa356a559a6fef21b28e009cb455 (patch)
treec184378473bd88448229554db53dca3f29cf6d6e
parent26b290ad99521fedabde3a5f7d2b3c92f9479225 (diff)
downloaddotfiles-cfa5db021590aa356a559a6fef21b28e009cb455.tar.gz
Refactor tilde and extension stripping
-rw-r--r--vim/filetype.vim80
1 files changed, 44 insertions, 36 deletions
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('<afile>')
- " Chop tildes
- autocmd BufNewFile,BufRead
- \ ?*~
- \ execute 'doautocmd filetypedetect BufRead '
- \ . fnameescape(substitute(expand('<afile>'), '\~$', '', ''))
+ " 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('<afile>: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('<afile>:r'))
+ " Strip generic .extension
+ else
+ let l:fn = expand('<afile>: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('<afile>: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