1 " vim: set sw=4 sts=4 et ft=vim :
2 " Script: securemodelines.vim
3 " Author: Ciaran McCreesh <ciaran.mccreesh at googlemail.com>
4 " Homepage: http://github.com/ciaranm/securemodelines
6 " License: Redistribute under the same terms as Vim itself
7 " Purpose: A secure alternative to modelines
9 if &compatible || v:version < 700 || exists('g:loaded_securemodelines')
12 let g:loaded_securemodelines = 1
14 if (! exists("g:secure_modelines_allowed_items"))
15 let g:secure_modelines_allowed_items = [
17 \ "softtabstop", "sts",
20 \ "expandtab", "et", "noexpandtab", "noet",
22 \ "foldmethod", "fdm",
23 \ "readonly", "ro", "noreadonly", "noro",
24 \ "rightleft", "rl", "norightleft", "norl",
25 \ "cindent", "cin", "nocindent", "nocin",
26 \ "smartindent", "si", "nosmartindent", "nosi",
27 \ "autoindent", "ai", "noautoindent", "noai",
33 if (! exists("g:secure_modelines_verbose"))
34 let g:secure_modelines_verbose = 0
37 if (! exists("g:secure_modelines_modelines"))
38 let g:secure_modelines_modelines=5
41 if (! exists("g:secure_modelines_leave_modeline"))
44 if g:secure_modelines_verbose
46 echo "Forcibly disabling internal modelines for securemodelines.vim"
52 fun! <SID>IsInList(list, i) abort
61 fun! <SID>DoOne(item) abort
62 let l:matches = matchlist(a:item, '^\([a-z]\+\)\%([-+^]\?=[a-zA-Z0-9_\-.]\+\)\?$')
64 if <SID>IsInList(g:secure_modelines_allowed_items, l:matches[1])
65 exec "setlocal " . a:item
66 elseif g:secure_modelines_verbose
68 echo "Ignoring '" . a:item . "' in modeline"
74 fun! <SID>DoNoSetModeline(line) abort
75 for l:item in split(a:line, '[ \t:]')
76 call <SID>DoOne(l:item)
80 fun! <SID>DoSetModeline(line) abort
81 for l:item in split(a:line)
82 call <SID>DoOne(l:item)
86 fun! <SID>CheckVersion(op, ver) abort
88 return v:version != a:ver
90 return v:version < a:ver
92 return v:version >= a:ver
98 fun! <SID>DoModeline(line) abort
99 let l:matches = matchlist(a:line, '\%(\S\@<!\%(vi\|vim\([<>=]\?\)\([0-9]\+\)\?\)\|\sex\):\s*\%(set\s\+\)\?\([^:]\+\):\S\@!')
100 if len(l:matches) > 0
102 if len(l:matches[1]) > 0
103 let l:operator = l:matches[1]
105 if len(l:matches[2]) > 0
106 if <SID>CheckVersion(l:operator, l:matches[2]) ? 0 : 1
110 return <SID>DoSetModeline(l:matches[3])
113 let l:matches = matchlist(a:line, '\%(\S\@<!\%(vi\|vim\([<>=]\?\)\([0-9]\+\)\?\)\|\sex\):\(.\+\)')
114 if len(l:matches) > 0
116 if len(l:matches[1]) > 0
117 let l:operator = l:matches[1]
119 if len(l:matches[2]) > 0
120 if <SID>CheckVersion(l:operator, l:matches[2]) ? 0 : 1
124 return <SID>DoNoSetModeline(l:matches[3])
128 fun! <SID>DoModelines() abort
129 if line("$") > g:secure_modelines_modelines
131 call map(filter(getline(1, g:secure_modelines_modelines) +
132 \ getline(line("$") - g:secure_modelines_modelines, "$"),
133 \ 'v:val =~ ":"'), 'extend(l:lines, { v:val : 0 } )')
134 for l:line in keys(l:lines)
135 call <SID>DoModeline(l:line)
138 for l:line in getline(1, "$")
139 call <SID>DoModeline(l:line)
144 fun! SecureModelines_DoModelines() abort
145 call <SID>DoModelines()
150 au BufRead,StdinReadPost * :call <SID>DoModelines()