aboutsummaryrefslogtreecommitdiff
path: root/vim/autoload/filetype.vim
blob: 6a66d62edb61c932c4d9203b4ee11ccad7fb5215 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
" 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 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#SudoRepeat() 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