public
Description: My dotfiles
Homepage: http://ciaranm.wordpress.com/
Clone URL: git://github.com/ciaranm/dotfiles-ciaranm.git
ciaranm (author)
Sun May 03 17:22:17 -0700 2009
commit  16df96a05e26dffff552d72c4c0ea4aa5b3eb5ca
tree    0c0909e31ee3e040f67c457149661db7cc43ea10
parent  a45e872888224d725e38a16910f8d80f88cff485
dotfiles-ciaranm / securemodelines.vim
100644 150 lines (133 sloc) 4.259 kb
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
" vim: set sw=4 sts=4 et ft=vim :
" Script: securemodelines.vim
" Version: 20070518
" Author: Ciaran McCreesh <ciaranm@ciaranm.org>
" Homepage: http://ciaranm.org/tag/securemodelines
" Requires: Vim 7
" License: Redistribute under the same terms as Vim itself
" Purpose: A secure alternative to modelines
 
if &compatible || v:version < 700
    finish
endif
 
if (! exists("g:secure_modelines_allowed_items"))
    let g:secure_modelines_allowed_items = [
                \ "textwidth", "tw",
                \ "softtabstop", "sts",
                \ "tabstop", "ts",
                \ "shiftwidth", "sw",
                \ "expandtab", "et", "noexpandtab", "noet",
                \ "filetype", "ft",
                \ "foldmethod", "fdm",
                \ "readonly", "ro", "noreadonly", "noro",
                \ "rightleft", "rl", "norightleft", "norl",
                \ "spell",
                \ "spelllang"
                \ ]
endif
 
if (! exists("g:secure_modelines_verbose"))
    let g:secure_modelines_verbose = 0
endif
 
if (! exists("g:secure_modelines_modelines"))
    let g:secure_modelines_modelines=5
endif
 
if (! exists("g:secure_modelines_leave_modeline"))
    if &modeline
        set nomodeline
        if g:secure_modelines_verbose
            echohl WarningMsg
            echo "Forcibly disabling internal modelines for securemodelines.vim"
            echohl None
        endif
    endif
endif
 
fun! <SID>IsInList(list, i) abort
    for l:item in a:list
        if a:i == l:item
            return 1
        endif
    endfor
    return 0
endfun
 
fun! <SID>DoOne(item) abort
    let l:matches = matchlist(a:item, '^\([a-z]\+\)\%(=[a-zA-Z0-9_\-.]\+\)\?$')
    if len(l:matches) > 0
        if <SID>IsInList(g:secure_modelines_allowed_items, l:matches[1])
            exec "setlocal " . a:item
        elseif g:secure_modelines_verbose
            echohl WarningMsg
            echo "Ignoring '" . a:item . "' in modeline"
            echohl None
        endif
    endif
endfun
 
fun! <SID>DoNoSetModeline(line) abort
    for l:item in split(a:line, '[ \t:]')
        call <SID>DoOne(l:item)
    endfor
endfun
 
fun! <SID>DoSetModeline(line) abort
    for l:item in split(a:line)
        call <SID>DoOne(l:item)
    endfor
endfun
 
fun! <SID>CheckVersion(op, ver) abort
    if a:op == "="
        return v:version != a:ver
    elseif a:op == "<"
        return v:version < a:ver
    elseif a:op == ">"
        return v:version >= a:ver
    else
        return 0
    endif
endfun
 
fun! <SID>DoModeline(line) abort
    let l:matches = matchlist(a:line, '\%(\S\@<!\%(vi\|vim\([<>=]\?\)\([0-9]\+\)\?\)\|\sex\):\s\+set\?\s\+\([^:]\+\):\S\@!')
    if len(l:matches) > 0
        let l:operator = ">"
        if len(l:matches[1]) > 0
            let l:operator = l:matches[1]
        endif
        if len(l:matches[2]) > 0
            if <SID>CheckVersion(l:operator, l:matches[2]) ? 0 : 1
                return
            endif
        endif
        return <SID>DoSetModeline(l:matches[3])
    endif
 
    let l:matches = matchlist(a:line, '\%(\S\@<!\%(vi\|vim\([<>=]\?\)\([0-9]\+\)\?\)\|\sex\):\(.\+\)')
    if len(l:matches) > 0
        let l:operator = ">"
        if len(l:matches[1]) > 0
            let l:operator = l:matches[1]
        endif
        if len(l:matches[2]) > 0
            if <SID>CheckVersion(l:operator, l:matches[2]) ? 0 : 1
                return
            endif
        endif
        return <SID>DoNoSetModeline(l:matches[3])
    endif
endfun
 
fun! <SID>DoModelines() abort
    if line("$") > g:secure_modelines_modelines
        let l:lines={ }
        call map(filter(getline(1, g:secure_modelines_modelines) +
                    \ getline(line("$") - g:secure_modelines_modelines, "$"),
                    \ 'v:val =~ ":"'), 'extend(l:lines, { v:val : 0 } )')
        for l:line in keys(l:lines)
            call <SID>DoModeline(l:line)
        endfor
    else
        for l:line in getline(1, "$")
            call <SID>DoModeline(l:line)
        endfor
    endif
endfun
 
fun! SecureModelines_DoModelines() abort
    call <SID>DoModelines()
endfun
 
aug SecureModeLines
    au!
    au BufRead * :call <SID>DoModelines()
aug END