aboutsummaryrefslogtreecommitdiff
path: root/vim/autoload/filetype.vim
blob: d1e4e3d7bce4e0779ad74ed5a12ed3bdf2c9f06b (plain)
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
67
68
69
70
71
72
73
74
75
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