diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2018-12-14 13:14:00 +1300 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2018-12-14 13:14:00 +1300 |
commit | e0ff7ac01d6439d826ff9ef6f134a7638ade714f (patch) | |
tree | 781004065fefcbfd8e543a61de3d168383e71bde /vim/autoload/filetype.vim | |
parent | Merge branch 'release/v3.1.0' (diff) | |
parent | Bump VERSION (diff) | |
download | dotfiles-3.2.0.tar.gz (sig) dotfiles-3.2.0.zip |
Merge branch 'release/v3.2.0'v3.2.0
* release/v3.2.0:
Bump VERSION
Refactor some conditionals
Factor out zsh ENV hack into one file
Refactor "path list" not to require a subshell
Correct completion for deep pass(1) directories
Move filetype.vim helper funcs into autoload
Fix a local var name in openssl(1ssl) completion
Correct a variable ref in openssl(1ssl) completion
Disable shellcheck rules for missed definition
Add filenames treatment to mex(1df) completion
Remove unneeded declaration
Refactor some completions to avoid loops
Remove unneeded stdout redirect
Remove unneeded semicolon from sh "for VAR ; do"
Substitute bad `continue` for `return`
Add actual completion matching to git completion
Apply much simpler completion to Git
Diffstat (limited to 'vim/autoload/filetype.vim')
-rw-r--r-- | vim/autoload/filetype.vim | 76 |
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 + + |