aboutsummaryrefslogtreecommitdiff
path: root/vim/autoload
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2018-12-07 13:42:30 +1300
committerTom Ryder <tom@sanctum.geek.nz>2018-12-08 11:48:00 +1300
commitc3e2ac92f72ac85967bdaf8d740d040f6e9c36b4 (patch)
treeb92dce7a46745985a48e1725ebdff5a253192598 /vim/autoload
parentFix a local var name in openssl(1ssl) completion (diff)
downloaddotfiles-c3e2ac92f72ac85967bdaf8d740d040f6e9c36b4.tar.gz
dotfiles-c3e2ac92f72ac85967bdaf8d740d040f6e9c36b4.zip
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.
Diffstat (limited to 'vim/autoload')
-rw-r--r--vim/autoload/filetype.vim76
1 files changed, 76 insertions, 0 deletions
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('<afile>')
+
+ " 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('<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
+
+" 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('<afile>')
+
+ " myfileXXQGS16A.conf: strip eight chars before final period
+ if l:fn =~# '/[^./]\+\w\{8}\.[^./]\+$'
+ let l:fr = expand('<afile>:r')
+ let l:fe = expand('<afile>: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('<afile>: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
+
+