aboutsummaryrefslogtreecommitdiff
path: root/vim/filetype.vim
diff options
context:
space:
mode:
Diffstat (limited to 'vim/filetype.vim')
-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