aboutsummaryrefslogtreecommitdiff
path: root/vim/autoload/filetype/repeat.vim
diff options
context:
space:
mode:
Diffstat (limited to 'vim/autoload/filetype/repeat.vim')
-rw-r--r--vim/autoload/filetype/repeat.vim66
1 files changed, 66 insertions, 0 deletions
diff --git a/vim/autoload/filetype/repeat.vim b/vim/autoload/filetype/repeat.vim
new file mode 100644
index 00000000..f681932b
--- /dev/null
+++ b/vim/autoload/filetype/repeat.vim
@@ -0,0 +1,66 @@
+" Helper function to run the 'filetypedetect' group on a file with its
+" extension stripped off
+function! filetype#repeat#Strip() abort
+
+ " Check we have the fnameescape() function
+ if !exists('*fnameescape')
+ return
+ endif
+
+ " Expand the match result
+ let fn = expand('<afile>')
+
+ " Strip leading and trailing #hashes#
+ if fn =~# '^#\+.*#\+$'
+ let fn = substitute(fn, '^#\+\(.\+\)#\+$', '\1', '')
+
+ " Strip trailing tilde~
+ elseif fn =~# '\~$'
+ let fn = substitute(fn, '\~$', '', '')
+
+ " Strip generic .extension
+ else
+ let fn = expand('<afile>:r')
+ endif
+
+ " Re-run the group if there's anything left
+ if strlen(fn)
+ execute 'doautocmd filetypedetect BufRead ' . fnameescape(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#repeat#Sudo() abort
+
+ " Check we have the fnameescape() function
+ if !exists('*fnameescape')
+ return
+ endif
+
+ " Expand the match result
+ let fn = expand('<afile>')
+
+ " myfileXXQGS16A.conf: strip eight chars before final period
+ if fn =~# '/[^/]\+\w\{8}\.[^./]\+$'
+ let fr = expand('<afile>:r')
+ let fe = expand('<afile>:e')
+ let fn = strpart(fr, -8, strlen(fr)) . '.' . fe
+
+ " myfile.XXQGS16A: strip extension
+ elseif fn =~# '/[^/]\+\.\w\{8}$'
+ let fn = expand('<afile>:r')
+
+ " Unrecognised pattern; return, don't repeat
+ else
+ return
+ endif
+
+ " Re-run the group if there's anything left
+ if strlen(fn)
+ execute 'doautocmd filetypedetect BufRead ' . fnameescape(fn)
+ endif
+
+endfunction