From 5c507d3e27133e3e3e7ad7a6e6d73a0be00c8cd5 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Thu, 22 Nov 2018 18:50:56 +1300 Subject: Make more of an effort with sudoedit(8) strips Handle both kinds of templated temporary files produced by sudoedit(8): * /var/tmp/foo.XXXXXXXX * /var/tmp/fooXXXXXXXX.bar This means that editing /etc/resolv.conf now highlights correctly, having also in this commit moved the sudo detection to *before* the .conf fallback. The hardcoded temporary path for finding the files probably needs to be either determined at runtime or made configurable by the user. --- vim/filetype.vim | 52 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 8 deletions(-) (limited to 'vim') diff --git a/vim/filetype.vim b/vim/filetype.vim index 7b4a5015..4f3839ef 100644 --- a/vim/filetype.vim +++ b/vim/filetype.vim @@ -42,6 +42,41 @@ function! s:StripRepeat() 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! s:SudoRepeat() + + " Check we have the fnameescape() function + if !exists('*fnameescape') + return + endif + + " Expand the match result + let l:fn = expand('') + + " myfileXXQGS16A.conf: strip eight chars before final period + if l:fn =~# '/[^./]\+\w\{8}\.[^./]\+$' + let l:fr = expand(':r') + let l:fe = expand(':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(':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! s:CheckShebang() @@ -493,6 +528,15 @@ augroup filetypedetect " Load any extra rules in ftdetect directories runtime! ftdetect/*.vim + " Clumsy attempt at typing files in `sudo -e` if a filename hasn't already + " been found + autocmd BufNewFile,BufRead + \ /var/tmp/?*????????.* + \,/var/tmp/?*.???????? + \ if !did_filetype() + \| call s:SudoRepeat() + \|endif + " Generic text, config, and log files, if no type assigned yet autocmd BufNewFile,BufRead \ ?*.text @@ -513,14 +557,6 @@ augroup filetypedetect \,?*.log \ setfiletype messages - " Clumsy attempt at typing files in `sudo -e` if a filename hasn't already - " been found; strip temporary extension and re-run - 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 performs " cleverer checks including looking at actual file contents--but only my " custom one; don't load the system one at all. -- cgit v1.2.3