1 " vim: set sw=4 sts=4 et ft=vim :
2 " Script: securemodelines.vim
3 " Version: 10d6c6b52fcdd12f3ba457126f66fee4ccceec04
4 " Author: Ciaran McCreesh <ciaran.mccreesh at googlemail.com>
5 " Homepage: http://github.com/ciaranm/securemodelines
7 " License: Redistribute under the same terms as Vim itself
8 " Purpose: A secure alternative to modelines
10 if &compatible || v:version < 700 || exists('g:loaded_securemodelines')
13 let g:loaded_securemodelines = 1
15 if (! exists("g:secure_modelines_allowed_items"))
16 let g:secure_modelines_allowed_items = [
18 \ "softtabstop", "sts",
21 \ "expandtab", "et", "noexpandtab", "noet",
23 \ "foldmethod", "fdm",
24 \ "readonly", "ro", "noreadonly", "noro",
25 \ "rightleft", "rl", "norightleft", "norl",
26 \ "cindent", "cin", "nocindent", "nocin",
27 \ "smartindent", "si", "nosmartindent", "nosi",
28 \ "autoindent", "ai", "noautoindent", "noai",
34 if (! exists("g:secure_modelines_verbose"))
35 let g:secure_modelines_verbose = 0
38 if (! exists("g:secure_modelines_modelines"))
39 let g:secure_modelines_modelines=5
42 if (! exists("g:secure_modelines_leave_modeline"))
45 if g:secure_modelines_verbose
47 echo "Forcibly disabling internal modelines for securemodelines.vim"
53 fun! <SID>IsInList(list, i) abort
62 fun! <SID>DoOne(item) abort
63 let l:matches = matchlist(a:item, '^\([a-z]\+\)\%([-+^]\?=[a-zA-Z0-9_\-.]\+\)\?$')
65 if <SID>IsInList(g:secure_modelines_allowed_items, l:matches[1])
66 exec "setlocal " . a:item
67 elseif g:secure_modelines_verbose
69 echo "Ignoring '" . a:item . "' in modeline"
75 fun! <SID>DoNoSetModeline(line) abort
76 for l:item in split(a:line, '[ \t:]')
77 call <SID>DoOne(l:item)
81 fun! <SID>DoSetModeline(line) abort
82 for l:item in split(a:line)
83 call <SID>DoOne(l:item)
87 fun! <SID>CheckVersion(op, ver) abort
89 return v:version != a:ver
91 return v:version < a:ver
93 return v:version >= a:ver
99 fun! <SID>DoModeline(line) abort
100 let l:matches = matchlist(a:line, '\%(\S\@<!\%(vi\|vim\([<>=]\?\)\([0-9]\+\)\?\)\|\sex\):\s*\%(set\s\+\)\?\([^:]\+\):\S\@!')
101 if len(l:matches) > 0
103 if len(l:matches[1]) > 0
104 let l:operator = l:matches[1]
106 if len(l:matches[2]) > 0
107 if <SID>CheckVersion(l:operator, l:matches[2]) ? 0 : 1
111 return <SID>DoSetModeline(l:matches[3])
114 let l:matches = matchlist(a:line, '\%(\S\@<!\%(vi\|vim\([<>=]\?\)\([0-9]\+\)\?\)\|\sex\):\(.\+\)')
115 if len(l:matches) > 0
117 if len(l:matches[1]) > 0
118 let l:operator = l:matches[1]
120 if len(l:matches[2]) > 0
121 if <SID>CheckVersion(l:operator, l:matches[2]) ? 0 : 1
125 return <SID>DoNoSetModeline(l:matches[3])
129 fun! <SID>DoModelines() abort
130 if line("$") > g:secure_modelines_modelines
132 call map(filter(getline(1, g:secure_modelines_modelines) +
133 \ getline(line("$") - g:secure_modelines_modelines, "$"),
134 \ 'v:val =~ ":"'), 'extend(l:lines, { v:val : 0 } )')
135 for l:line in keys(l:lines)
136 call <SID>DoModeline(l:line)
139 for l:line in getline(1, "$")
140 call <SID>DoModeline(l:line)
145 fun! SecureModelines_DoModelines() abort
146 call <SID>DoModelines()
151 au BufRead,StdinReadPost * :call <SID>DoModelines()