blob: bb5bc6b515de4825c26a26ae821785b8dea13069 [file] [log] [blame]
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001" Tests for parsing the modeline.
2
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02003source check.vim
4
Bram Moolenaarcd96eef2018-07-23 04:49:23 +02005func Test_modeline_invalid()
Bram Moolenaar9691f822018-11-03 19:06:25 +01006 " This was reading allocated memory in the past.
Bram Moolenaarb152b6a2022-09-29 21:37:33 +01007 call writefile(['vi:0', 'nothing'], 'Xmodeline', 'D')
Bram Moolenaar9691f822018-11-03 19:06:25 +01008 let modeline = &modeline
9 set modeline
Bram Moolenaar9cf4b502018-07-23 04:12:03 +020010 call assert_fails('split Xmodeline', 'E518:')
Bram Moolenaar916a8182018-11-25 02:18:29 +010011
Bram Moolenaare74331d2019-12-17 19:22:40 +010012 " Missing end colon (ignored).
13 call writefile(['// vim: set ts=2'], 'Xmodeline')
14 edit Xmodeline_version
15 call assert_equal(8, &ts)
16 bwipe!
17
18 " Missing colon at beginning (ignored).
19 call writefile(['// vim set ts=2:'], 'Xmodeline')
20 edit Xmodeline_version
21 call assert_equal(8, &ts)
22 bwipe!
23
24 " Missing space after vim (ignored).
25 call writefile(['// vim:ts=2:'], 'Xmodeline')
26 edit Xmodeline_version
27 call assert_equal(8, &ts)
28 bwipe!
29
Bram Moolenaar9691f822018-11-03 19:06:25 +010030 let &modeline = modeline
Bram Moolenaar9cf4b502018-07-23 04:12:03 +020031 bwipe!
Bram Moolenaar9cf4b502018-07-23 04:12:03 +020032endfunc
Bram Moolenaar916a8182018-11-25 02:18:29 +010033
34func Test_modeline_filetype()
Bram Moolenaarb152b6a2022-09-29 21:37:33 +010035 call writefile(['vim: set ft=c :', 'nothing'], 'Xmodeline_filetype', 'D')
Bram Moolenaar916a8182018-11-25 02:18:29 +010036 let modeline = &modeline
37 set modeline
38 filetype plugin on
39 split Xmodeline_filetype
40 call assert_equal("c", &filetype)
41 call assert_equal(1, b:did_ftplugin)
42 call assert_equal("ccomplete#Complete", &ofu)
43
44 bwipe!
Bram Moolenaar916a8182018-11-25 02:18:29 +010045 let &modeline = modeline
46 filetype plugin off
47endfunc
48
49func Test_modeline_syntax()
Bram Moolenaarb152b6a2022-09-29 21:37:33 +010050 call writefile(['vim: set syn=c :', 'nothing'], 'Xmodeline_syntax', 'D')
Bram Moolenaar916a8182018-11-25 02:18:29 +010051 let modeline = &modeline
52 set modeline
53 syntax enable
54 split Xmodeline_syntax
55 call assert_equal("c", &syntax)
56 call assert_equal("c", b:current_syntax)
57
58 bwipe!
Bram Moolenaar916a8182018-11-25 02:18:29 +010059 let &modeline = modeline
60 syntax off
61endfunc
62
63func Test_modeline_keymap()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020064 CheckFeature keymap
Bram Moolenaarb152b6a2022-09-29 21:37:33 +010065 call writefile(['vim: set keymap=greek :', 'nothing'], 'Xmodeline_keymap', 'D')
Bram Moolenaar916a8182018-11-25 02:18:29 +010066 let modeline = &modeline
67 set modeline
68 split Xmodeline_keymap
69 call assert_equal("greek", &keymap)
70 call assert_match('greek\|grk', b:keymap_name)
71
72 bwipe!
Bram Moolenaar916a8182018-11-25 02:18:29 +010073 let &modeline = modeline
74 set keymap= iminsert=0 imsearch=-1
75endfunc
76
Bram Moolenaare74331d2019-12-17 19:22:40 +010077func Test_modeline_version()
78 let modeline = &modeline
79 set modeline
80
81 " Test with vim:{vers}: (version {vers} or later).
Bram Moolenaarb152b6a2022-09-29 21:37:33 +010082 call writefile(['// vim' .. v:version .. ': ts=2:'], 'Xmodeline_version', 'D')
Bram Moolenaare74331d2019-12-17 19:22:40 +010083 edit Xmodeline_version
84 call assert_equal(2, &ts)
85 bwipe!
86
87 call writefile(['// vim' .. (v:version - 100) .. ': ts=2:'], 'Xmodeline_version')
88 edit Xmodeline_version
89 call assert_equal(2, &ts)
90 bwipe!
91
92 call writefile(['// vim' .. (v:version + 100) .. ': ts=2:'], 'Xmodeline_version')
93 edit Xmodeline_version
94 call assert_equal(8, &ts)
95 bw!
96
97 " Test with vim>{vers}: (version after {vers}).
98 call writefile(['// vim>' .. v:version .. ': ts=2:'], 'Xmodeline_version')
99 edit Xmodeline_version
100 call assert_equal(8, &ts)
101 bwipe!
102
103 call writefile(['// vim>' .. (v:version - 100) .. ': ts=2:'], 'Xmodeline_version')
104 edit Xmodeline_version
105 call assert_equal(2, &ts)
106 bwipe!
107
108 call writefile(['// vim>' .. (v:version + 100) .. ': ts=2:'], 'Xmodeline_version')
109 edit Xmodeline_version
110 call assert_equal(8, &ts)
111 bwipe!
112
113 " Test with vim<{vers}: (version before {vers}).
114 call writefile(['// vim<' .. v:version .. ': ts=2:'], 'Xmodeline_version')
115 edit Xmodeline_version
116 call assert_equal(8, &ts)
117 bwipe!
118
119 call writefile(['// vim<' .. (v:version - 100) .. ': ts=2:'], 'Xmodeline_version')
120 edit Xmodeline_version
121 call assert_equal(8, &ts)
122 bwipe!
123
124 call writefile(['// vim<' .. (v:version + 100) .. ': ts=2:'], 'Xmodeline_version')
125 edit Xmodeline_version
126 call assert_equal(2, &ts)
127 bwipe!
128
129 " Test with vim={vers}: (version {vers} only).
130 call writefile(['// vim=' .. v:version .. ': ts=2:'], 'Xmodeline_version')
131 edit Xmodeline_version
132 call assert_equal(2, &ts)
133 bwipe!
134
135 call writefile(['// vim=' .. (v:version - 100) .. ': ts=2:'], 'Xmodeline_version')
136 edit Xmodeline_version
137 call assert_equal(8, &ts)
138 bwipe!
139
140 call writefile(['// vim=' .. (v:version + 100) .. ': ts=2:'], 'Xmodeline_version')
141 edit Xmodeline_version
142 call assert_equal(8, &ts)
143 bwipe!
144
145 let &modeline = modeline
Bram Moolenaare74331d2019-12-17 19:22:40 +0100146endfunc
147
148func Test_modeline_colon()
149 let modeline = &modeline
150 set modeline
151
Bram Moolenaarb152b6a2022-09-29 21:37:33 +0100152 call writefile(['// vim: set showbreak=\: ts=2: sw=2'], 'Xmodeline_colon', 'D')
Bram Moolenaare74331d2019-12-17 19:22:40 +0100153 edit Xmodeline_colon
154
155 " backlash colon should become colon.
156 call assert_equal(':', &showbreak)
157
158 " 'ts' should be set.
159 " 'sw' should be ignored because it is after the end colon.
160 call assert_equal(2, &ts)
161 call assert_equal(8, &sw)
162
163 let &modeline = modeline
Bram Moolenaare74331d2019-12-17 19:22:40 +0100164endfunc
165
Bram Moolenaar110289e2019-05-23 15:38:06 +0200166func s:modeline_fails(what, text, error)
zeertzjq4d62a2f2023-11-08 20:48:05 +0100167 " Don't use CheckOption(), it would skip the whole test
168 " just for a single un-supported option
169 if !exists('+' .. a:what)
170 return
171 endif
Bram Moolenaar916a8182018-11-25 02:18:29 +0100172 let fname = "Xmodeline_fails_" . a:what
Bram Moolenaarb152b6a2022-09-29 21:37:33 +0100173 call writefile(['vim: set ' . a:text . ' :', 'nothing'], fname, 'D')
Bram Moolenaar916a8182018-11-25 02:18:29 +0100174 let modeline = &modeline
175 set modeline
176 filetype plugin on
177 syntax enable
Bram Moolenaar110289e2019-05-23 15:38:06 +0200178 call assert_fails('split ' . fname, a:error)
Bram Moolenaar916a8182018-11-25 02:18:29 +0100179 call assert_equal("", &filetype)
180 call assert_equal("", &syntax)
181
182 bwipe!
Bram Moolenaar916a8182018-11-25 02:18:29 +0100183 let &modeline = modeline
184 filetype plugin off
185 syntax off
186endfunc
187
188func Test_modeline_filetype_fails()
Bram Moolenaar110289e2019-05-23 15:38:06 +0200189 call s:modeline_fails('filetype', 'ft=evil$CMD', 'E474:')
Bram Moolenaar916a8182018-11-25 02:18:29 +0100190endfunc
191
192func Test_modeline_syntax_fails()
Bram Moolenaar110289e2019-05-23 15:38:06 +0200193 call s:modeline_fails('syntax', 'syn=evil$CMD', 'E474:')
Bram Moolenaar916a8182018-11-25 02:18:29 +0100194endfunc
195
196func Test_modeline_keymap_fails()
Bram Moolenaar110289e2019-05-23 15:38:06 +0200197 call s:modeline_fails('keymap', 'keymap=evil$CMD', 'E474:')
198endfunc
199
200func Test_modeline_fails_always()
201 call s:modeline_fails('backupdir', 'backupdir=Something()', 'E520:')
202 call s:modeline_fails('cdpath', 'cdpath=Something()', 'E520:')
203 call s:modeline_fails('charconvert', 'charconvert=Something()', 'E520:')
204 call s:modeline_fails('completefunc', 'completefunc=Something()', 'E520:')
205 call s:modeline_fails('cscopeprg', 'cscopeprg=Something()', 'E520:')
206 call s:modeline_fails('diffexpr', 'diffexpr=Something()', 'E520:')
207 call s:modeline_fails('directory', 'directory=Something()', 'E520:')
208 call s:modeline_fails('equalprg', 'equalprg=Something()', 'E520:')
209 call s:modeline_fails('errorfile', 'errorfile=Something()', 'E520:')
210 call s:modeline_fails('exrc', 'exrc=Something()', 'E520:')
Yegappan Lakshmananaeb1c972024-10-22 23:42:20 +0200211 call s:modeline_fails('findexpr', 'findexpr=Something()', 'E520:')
Bram Moolenaar110289e2019-05-23 15:38:06 +0200212 call s:modeline_fails('formatprg', 'formatprg=Something()', 'E520:')
213 call s:modeline_fails('fsync', 'fsync=Something()', 'E520:')
214 call s:modeline_fails('grepprg', 'grepprg=Something()', 'E520:')
215 call s:modeline_fails('helpfile', 'helpfile=Something()', 'E520:')
216 call s:modeline_fails('imactivatefunc', 'imactivatefunc=Something()', 'E520:')
217 call s:modeline_fails('imstatusfunc', 'imstatusfunc=Something()', 'E520:')
218 call s:modeline_fails('imstyle', 'imstyle=Something()', 'E520:')
219 call s:modeline_fails('keywordprg', 'keywordprg=Something()', 'E520:')
220 call s:modeline_fails('langmap', 'langmap=Something()', 'E520:')
221 call s:modeline_fails('luadll', 'luadll=Something()', 'E520:')
222 call s:modeline_fails('makeef', 'makeef=Something()', 'E520:')
223 call s:modeline_fails('makeprg', 'makeprg=Something()', 'E520:')
Bram Moolenaar07607392019-05-26 19:20:43 +0200224 call s:modeline_fails('mkspellmem', 'mkspellmem=Something()', 'E520:')
Bram Moolenaar110289e2019-05-23 15:38:06 +0200225 call s:modeline_fails('mzschemedll', 'mzschemedll=Something()', 'E520:')
226 call s:modeline_fails('mzschemegcdll', 'mzschemegcdll=Something()', 'E520:')
Bram Moolenaar7e800c62019-05-23 17:08:49 +0200227 call s:modeline_fails('modelineexpr', 'modelineexpr', 'E520:')
Bram Moolenaar110289e2019-05-23 15:38:06 +0200228 call s:modeline_fails('omnifunc', 'omnifunc=Something()', 'E520:')
229 call s:modeline_fails('operatorfunc', 'operatorfunc=Something()', 'E520:')
230 call s:modeline_fails('perldll', 'perldll=Something()', 'E520:')
231 call s:modeline_fails('printdevice', 'printdevice=Something()', 'E520:')
232 call s:modeline_fails('patchexpr', 'patchexpr=Something()', 'E520:')
233 call s:modeline_fails('printexpr', 'printexpr=Something()', 'E520:')
234 call s:modeline_fails('pythondll', 'pythondll=Something()', 'E520:')
Bram Moolenaare09244e2019-05-23 17:35:55 +0200235 call s:modeline_fails('pythonhome', 'pythonhome=Something()', 'E520:')
Bram Moolenaar110289e2019-05-23 15:38:06 +0200236 call s:modeline_fails('pythonthreedll', 'pythonthreedll=Something()', 'E520:')
237 call s:modeline_fails('pythonthreehome', 'pythonthreehome=Something()', 'E520:')
238 call s:modeline_fails('pyxversion', 'pyxversion=Something()', 'E520:')
239 call s:modeline_fails('rubydll', 'rubydll=Something()', 'E520:')
240 call s:modeline_fails('runtimepath', 'runtimepath=Something()', 'E520:')
241 call s:modeline_fails('secure', 'secure=Something()', 'E520:')
242 call s:modeline_fails('shell', 'shell=Something()', 'E520:')
243 call s:modeline_fails('shellcmdflag', 'shellcmdflag=Something()', 'E520:')
244 call s:modeline_fails('shellpipe', 'shellpipe=Something()', 'E520:')
245 call s:modeline_fails('shellquote', 'shellquote=Something()', 'E520:')
246 call s:modeline_fails('shellredir', 'shellredir=Something()', 'E520:')
247 call s:modeline_fails('shellxquote', 'shellxquote=Something()', 'E520:')
248 call s:modeline_fails('spellfile', 'spellfile=Something()', 'E520:')
249 call s:modeline_fails('spellsuggest', 'spellsuggest=Something()', 'E520:')
250 call s:modeline_fails('tcldll', 'tcldll=Something()', 'E520:')
251 call s:modeline_fails('titleold', 'titleold=Something()', 'E520:')
252 call s:modeline_fails('viewdir', 'viewdir=Something()', 'E520:')
253 call s:modeline_fails('viminfo', 'viminfo=Something()', 'E520:')
254 call s:modeline_fails('viminfofile', 'viminfofile=Something()', 'E520:')
255 call s:modeline_fails('winptydll', 'winptydll=Something()', 'E520:')
256 call s:modeline_fails('undodir', 'undodir=Something()', 'E520:')
257 " only check a few terminal options
258 call s:modeline_fails('t_AB', 't_AB=Something()', 'E520:')
259 call s:modeline_fails('t_ce', 't_ce=Something()', 'E520:')
260 call s:modeline_fails('t_sr', 't_sr=Something()', 'E520:')
261 call s:modeline_fails('t_8b', 't_8b=Something()', 'E520:')
262endfunc
263
264func Test_modeline_fails_modelineexpr()
265 call s:modeline_fails('balloonexpr', 'balloonexpr=Something()', 'E992:')
266 call s:modeline_fails('foldexpr', 'foldexpr=Something()', 'E992:')
267 call s:modeline_fails('foldtext', 'foldtext=Something()', 'E992:')
268 call s:modeline_fails('formatexpr', 'formatexpr=Something()', 'E992:')
269 call s:modeline_fails('guitablabel', 'guitablabel=Something()', 'E992:')
270 call s:modeline_fails('iconstring', 'iconstring=Something()', 'E992:')
271 call s:modeline_fails('includeexpr', 'includeexpr=Something()', 'E992:')
272 call s:modeline_fails('indentexpr', 'indentexpr=Something()', 'E992:')
273 call s:modeline_fails('rulerformat', 'rulerformat=Something()', 'E992:')
274 call s:modeline_fails('statusline', 'statusline=Something()', 'E992:')
275 call s:modeline_fails('tabline', 'tabline=Something()', 'E992:')
276 call s:modeline_fails('titlestring', 'titlestring=Something()', 'E992:')
Bram Moolenaar916a8182018-11-25 02:18:29 +0100277endfunc
Bram Moolenaar51258742020-05-03 17:19:33 +0200278
279func Test_modeline_setoption_verbose()
280 let modeline = &modeline
281 set modeline
282
283 let lines =<< trim END
284 1 vim:ts=2
285 2 two
286 3 three
287 4 four
288 5 five
289 6 six
290 7 seven
291 8 eight
292 END
Bram Moolenaarb152b6a2022-09-29 21:37:33 +0100293 call writefile(lines, 'Xmodeline', 'D')
Bram Moolenaar51258742020-05-03 17:19:33 +0200294 edit Xmodeline
295 let info = split(execute('verbose set tabstop?'), "\n")
296 call assert_match('^\s*Last set from modeline line 1$', info[-1])
297 bwipe!
298
299 let lines =<< trim END
300 1 one
301 2 two
302 3 three
303 4 vim:ts=4
304 5 five
305 6 six
306 7 seven
307 8 eight
308 END
309 call writefile(lines, 'Xmodeline')
310 edit Xmodeline
311 let info = split(execute('verbose set tabstop?'), "\n")
312 call assert_match('^\s*Last set from modeline line 4$', info[-1])
313 bwipe!
314
315 let lines =<< trim END
316 1 one
317 2 two
318 3 three
319 4 four
320 5 five
321 6 six
322 7 seven
323 8 vim:ts=8
324 END
325 call writefile(lines, 'Xmodeline')
326 edit Xmodeline
327 let info = split(execute('verbose set tabstop?'), "\n")
328 call assert_match('^\s*Last set from modeline line 8$', info[-1])
329 bwipe!
330
331 let &modeline = modeline
Bram Moolenaar51258742020-05-03 17:19:33 +0200332endfunc
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200333
Yegappan Lakshmanan2d6d7182021-06-13 21:52:48 +0200334" Test for the 'modeline' default value in compatible and non-compatible modes
335" for root and non-root accounts
336func Test_modeline_default()
337 set compatible
338 call assert_false(&modeline)
339 set nocompatible
340 call assert_equal(IsRoot() ? 0 : 1, &modeline)
341 set compatible&vi
342 call assert_false(&modeline)
343 set compatible&vim
344 call assert_equal(IsRoot() ? 0 : 1, &modeline)
345 set compatible& modeline&
346endfunc
347
348" Some options cannot be set from the modeline when 'diff' option is set
349func Test_modeline_diff_buffer()
Bram Moolenaarb152b6a2022-09-29 21:37:33 +0100350 call writefile(['vim: diff foldmethod=marker wrap'], 'Xmdifile', 'D')
Yegappan Lakshmanan2d6d7182021-06-13 21:52:48 +0200351 set foldmethod& nowrap
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100352 new Xmdifile
Yegappan Lakshmanan2d6d7182021-06-13 21:52:48 +0200353 call assert_equal('manual', &foldmethod)
354 call assert_false(&wrap)
355 set wrap&
Yegappan Lakshmanan2d6d7182021-06-13 21:52:48 +0200356 bw
357endfunc
358
Hu Jialun9dcd3492021-08-28 20:42:50 +0200359func Test_modeline_disable()
360 set modeline
Bram Moolenaarb152b6a2022-09-29 21:37:33 +0100361 call writefile(['vim: sw=2', 'vim: nomodeline', 'vim: sw=3'], 'Xmodeline_disable', 'D')
Hu Jialun9dcd3492021-08-28 20:42:50 +0200362 edit Xmodeline_disable
363 call assert_equal(2, &sw)
Hu Jialun9dcd3492021-08-28 20:42:50 +0200364endfunc
365
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200366" vim: shiftwidth=2 sts=2 expandtab