blob: 1f8686328a5956126ee8787f261ec6605be85562 [file] [log] [blame]
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02001" Tests for parsing the modeline.
2
Bram Moolenaarcd96eef2018-07-23 04:49:23 +02003func Test_modeline_invalid()
Bram Moolenaar9691f822018-11-03 19:06:25 +01004 " This was reading allocated memory in the past.
Bram Moolenaarb152b6a2022-09-29 21:37:33 +01005 call writefile(['vi:0', 'nothing'], 'Xmodeline', 'D')
Bram Moolenaar9691f822018-11-03 19:06:25 +01006 let modeline = &modeline
7 set modeline
Bram Moolenaar9cf4b502018-07-23 04:12:03 +02008 call assert_fails('split Xmodeline', 'E518:')
Bram Moolenaar916a8182018-11-25 02:18:29 +01009
Bram Moolenaare74331d2019-12-17 19:22:40 +010010 " Missing end colon (ignored).
11 call writefile(['// vim: set ts=2'], 'Xmodeline')
12 edit Xmodeline_version
13 call assert_equal(8, &ts)
14 bwipe!
15
16 " Missing colon at beginning (ignored).
17 call writefile(['// vim set ts=2:'], 'Xmodeline')
18 edit Xmodeline_version
19 call assert_equal(8, &ts)
20 bwipe!
21
22 " Missing space after vim (ignored).
23 call writefile(['// vim:ts=2:'], 'Xmodeline')
24 edit Xmodeline_version
25 call assert_equal(8, &ts)
26 bwipe!
27
Bram Moolenaar9691f822018-11-03 19:06:25 +010028 let &modeline = modeline
Bram Moolenaar9cf4b502018-07-23 04:12:03 +020029 bwipe!
Bram Moolenaar9cf4b502018-07-23 04:12:03 +020030endfunc
Bram Moolenaar916a8182018-11-25 02:18:29 +010031
32func Test_modeline_filetype()
Bram Moolenaarb152b6a2022-09-29 21:37:33 +010033 call writefile(['vim: set ft=c :', 'nothing'], 'Xmodeline_filetype', 'D')
Bram Moolenaar916a8182018-11-25 02:18:29 +010034 let modeline = &modeline
35 set modeline
36 filetype plugin on
37 split Xmodeline_filetype
38 call assert_equal("c", &filetype)
39 call assert_equal(1, b:did_ftplugin)
40 call assert_equal("ccomplete#Complete", &ofu)
41
42 bwipe!
Bram Moolenaar916a8182018-11-25 02:18:29 +010043 let &modeline = modeline
44 filetype plugin off
45endfunc
46
47func Test_modeline_syntax()
Bram Moolenaarb152b6a2022-09-29 21:37:33 +010048 call writefile(['vim: set syn=c :', 'nothing'], 'Xmodeline_syntax', 'D')
Bram Moolenaar916a8182018-11-25 02:18:29 +010049 let modeline = &modeline
50 set modeline
51 syntax enable
52 split Xmodeline_syntax
53 call assert_equal("c", &syntax)
54 call assert_equal("c", b:current_syntax)
55
56 bwipe!
Bram Moolenaar916a8182018-11-25 02:18:29 +010057 let &modeline = modeline
58 syntax off
59endfunc
60
61func Test_modeline_keymap()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020062 CheckFeature keymap
Bram Moolenaarb152b6a2022-09-29 21:37:33 +010063 call writefile(['vim: set keymap=greek :', 'nothing'], 'Xmodeline_keymap', 'D')
Bram Moolenaar916a8182018-11-25 02:18:29 +010064 let modeline = &modeline
65 set modeline
66 split Xmodeline_keymap
67 call assert_equal("greek", &keymap)
68 call assert_match('greek\|grk', b:keymap_name)
69
70 bwipe!
Bram Moolenaar916a8182018-11-25 02:18:29 +010071 let &modeline = modeline
72 set keymap= iminsert=0 imsearch=-1
73endfunc
74
Bram Moolenaare74331d2019-12-17 19:22:40 +010075func Test_modeline_version()
76 let modeline = &modeline
77 set modeline
78
79 " Test with vim:{vers}: (version {vers} or later).
Bram Moolenaarb152b6a2022-09-29 21:37:33 +010080 call writefile(['// vim' .. v:version .. ': ts=2:'], 'Xmodeline_version', 'D')
Bram Moolenaare74331d2019-12-17 19:22:40 +010081 edit Xmodeline_version
82 call assert_equal(2, &ts)
83 bwipe!
84
85 call writefile(['// vim' .. (v:version - 100) .. ': ts=2:'], 'Xmodeline_version')
86 edit Xmodeline_version
87 call assert_equal(2, &ts)
88 bwipe!
89
90 call writefile(['// vim' .. (v:version + 100) .. ': ts=2:'], 'Xmodeline_version')
91 edit Xmodeline_version
92 call assert_equal(8, &ts)
93 bw!
94
95 " Test with vim>{vers}: (version after {vers}).
96 call writefile(['// vim>' .. v:version .. ': ts=2:'], 'Xmodeline_version')
97 edit Xmodeline_version
98 call assert_equal(8, &ts)
99 bwipe!
100
101 call writefile(['// vim>' .. (v:version - 100) .. ': ts=2:'], 'Xmodeline_version')
102 edit Xmodeline_version
103 call assert_equal(2, &ts)
104 bwipe!
105
106 call writefile(['// vim>' .. (v:version + 100) .. ': ts=2:'], 'Xmodeline_version')
107 edit Xmodeline_version
108 call assert_equal(8, &ts)
109 bwipe!
110
111 " Test with vim<{vers}: (version before {vers}).
112 call writefile(['// vim<' .. v:version .. ': ts=2:'], 'Xmodeline_version')
113 edit Xmodeline_version
114 call assert_equal(8, &ts)
115 bwipe!
116
117 call writefile(['// vim<' .. (v:version - 100) .. ': ts=2:'], 'Xmodeline_version')
118 edit Xmodeline_version
119 call assert_equal(8, &ts)
120 bwipe!
121
122 call writefile(['// vim<' .. (v:version + 100) .. ': ts=2:'], 'Xmodeline_version')
123 edit Xmodeline_version
124 call assert_equal(2, &ts)
125 bwipe!
126
127 " Test with vim={vers}: (version {vers} only).
128 call writefile(['// vim=' .. v:version .. ': ts=2:'], 'Xmodeline_version')
129 edit Xmodeline_version
130 call assert_equal(2, &ts)
131 bwipe!
132
133 call writefile(['// vim=' .. (v:version - 100) .. ': ts=2:'], 'Xmodeline_version')
134 edit Xmodeline_version
135 call assert_equal(8, &ts)
136 bwipe!
137
138 call writefile(['// vim=' .. (v:version + 100) .. ': ts=2:'], 'Xmodeline_version')
139 edit Xmodeline_version
140 call assert_equal(8, &ts)
141 bwipe!
142
143 let &modeline = modeline
Bram Moolenaare74331d2019-12-17 19:22:40 +0100144endfunc
145
146func Test_modeline_colon()
147 let modeline = &modeline
148 set modeline
149
Bram Moolenaarb152b6a2022-09-29 21:37:33 +0100150 call writefile(['// vim: set showbreak=\: ts=2: sw=2'], 'Xmodeline_colon', 'D')
Bram Moolenaare74331d2019-12-17 19:22:40 +0100151 edit Xmodeline_colon
152
153 " backlash colon should become colon.
154 call assert_equal(':', &showbreak)
155
156 " 'ts' should be set.
157 " 'sw' should be ignored because it is after the end colon.
158 call assert_equal(2, &ts)
159 call assert_equal(8, &sw)
160
161 let &modeline = modeline
Bram Moolenaare74331d2019-12-17 19:22:40 +0100162endfunc
163
Bram Moolenaar110289e2019-05-23 15:38:06 +0200164func s:modeline_fails(what, text, error)
zeertzjq4d62a2f2023-11-08 20:48:05 +0100165 " Don't use CheckOption(), it would skip the whole test
166 " just for a single un-supported option
167 if !exists('+' .. a:what)
168 return
169 endif
Bram Moolenaar916a8182018-11-25 02:18:29 +0100170 let fname = "Xmodeline_fails_" . a:what
Bram Moolenaarb152b6a2022-09-29 21:37:33 +0100171 call writefile(['vim: set ' . a:text . ' :', 'nothing'], fname, 'D')
Bram Moolenaar916a8182018-11-25 02:18:29 +0100172 let modeline = &modeline
173 set modeline
174 filetype plugin on
175 syntax enable
Bram Moolenaar110289e2019-05-23 15:38:06 +0200176 call assert_fails('split ' . fname, a:error)
Bram Moolenaar916a8182018-11-25 02:18:29 +0100177 call assert_equal("", &filetype)
178 call assert_equal("", &syntax)
179
180 bwipe!
Bram Moolenaar916a8182018-11-25 02:18:29 +0100181 let &modeline = modeline
182 filetype plugin off
183 syntax off
184endfunc
185
186func Test_modeline_filetype_fails()
Bram Moolenaar110289e2019-05-23 15:38:06 +0200187 call s:modeline_fails('filetype', 'ft=evil$CMD', 'E474:')
Bram Moolenaar916a8182018-11-25 02:18:29 +0100188endfunc
189
190func Test_modeline_syntax_fails()
Bram Moolenaar110289e2019-05-23 15:38:06 +0200191 call s:modeline_fails('syntax', 'syn=evil$CMD', 'E474:')
Bram Moolenaar916a8182018-11-25 02:18:29 +0100192endfunc
193
194func Test_modeline_keymap_fails()
Bram Moolenaar110289e2019-05-23 15:38:06 +0200195 call s:modeline_fails('keymap', 'keymap=evil$CMD', 'E474:')
196endfunc
197
198func Test_modeline_fails_always()
199 call s:modeline_fails('backupdir', 'backupdir=Something()', 'E520:')
200 call s:modeline_fails('cdpath', 'cdpath=Something()', 'E520:')
201 call s:modeline_fails('charconvert', 'charconvert=Something()', 'E520:')
202 call s:modeline_fails('completefunc', 'completefunc=Something()', 'E520:')
203 call s:modeline_fails('cscopeprg', 'cscopeprg=Something()', 'E520:')
204 call s:modeline_fails('diffexpr', 'diffexpr=Something()', 'E520:')
205 call s:modeline_fails('directory', 'directory=Something()', 'E520:')
206 call s:modeline_fails('equalprg', 'equalprg=Something()', 'E520:')
207 call s:modeline_fails('errorfile', 'errorfile=Something()', 'E520:')
208 call s:modeline_fails('exrc', 'exrc=Something()', 'E520:')
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +0100209 call s:modeline_fails('findfunc', 'findfunc=Something', 'E520:')
Bram Moolenaar110289e2019-05-23 15:38:06 +0200210 call s:modeline_fails('formatprg', 'formatprg=Something()', 'E520:')
211 call s:modeline_fails('fsync', 'fsync=Something()', 'E520:')
212 call s:modeline_fails('grepprg', 'grepprg=Something()', 'E520:')
213 call s:modeline_fails('helpfile', 'helpfile=Something()', 'E520:')
214 call s:modeline_fails('imactivatefunc', 'imactivatefunc=Something()', 'E520:')
215 call s:modeline_fails('imstatusfunc', 'imstatusfunc=Something()', 'E520:')
216 call s:modeline_fails('imstyle', 'imstyle=Something()', 'E520:')
217 call s:modeline_fails('keywordprg', 'keywordprg=Something()', 'E520:')
218 call s:modeline_fails('langmap', 'langmap=Something()', 'E520:')
219 call s:modeline_fails('luadll', 'luadll=Something()', 'E520:')
220 call s:modeline_fails('makeef', 'makeef=Something()', 'E520:')
221 call s:modeline_fails('makeprg', 'makeprg=Something()', 'E520:')
Bram Moolenaar07607392019-05-26 19:20:43 +0200222 call s:modeline_fails('mkspellmem', 'mkspellmem=Something()', 'E520:')
Bram Moolenaar110289e2019-05-23 15:38:06 +0200223 call s:modeline_fails('mzschemedll', 'mzschemedll=Something()', 'E520:')
224 call s:modeline_fails('mzschemegcdll', 'mzschemegcdll=Something()', 'E520:')
Bram Moolenaar7e800c62019-05-23 17:08:49 +0200225 call s:modeline_fails('modelineexpr', 'modelineexpr', 'E520:')
Bram Moolenaar110289e2019-05-23 15:38:06 +0200226 call s:modeline_fails('omnifunc', 'omnifunc=Something()', 'E520:')
227 call s:modeline_fails('operatorfunc', 'operatorfunc=Something()', 'E520:')
228 call s:modeline_fails('perldll', 'perldll=Something()', 'E520:')
229 call s:modeline_fails('printdevice', 'printdevice=Something()', 'E520:')
230 call s:modeline_fails('patchexpr', 'patchexpr=Something()', 'E520:')
231 call s:modeline_fails('printexpr', 'printexpr=Something()', 'E520:')
232 call s:modeline_fails('pythondll', 'pythondll=Something()', 'E520:')
Bram Moolenaare09244e2019-05-23 17:35:55 +0200233 call s:modeline_fails('pythonhome', 'pythonhome=Something()', 'E520:')
Bram Moolenaar110289e2019-05-23 15:38:06 +0200234 call s:modeline_fails('pythonthreedll', 'pythonthreedll=Something()', 'E520:')
235 call s:modeline_fails('pythonthreehome', 'pythonthreehome=Something()', 'E520:')
236 call s:modeline_fails('pyxversion', 'pyxversion=Something()', 'E520:')
237 call s:modeline_fails('rubydll', 'rubydll=Something()', 'E520:')
238 call s:modeline_fails('runtimepath', 'runtimepath=Something()', 'E520:')
239 call s:modeline_fails('secure', 'secure=Something()', 'E520:')
240 call s:modeline_fails('shell', 'shell=Something()', 'E520:')
241 call s:modeline_fails('shellcmdflag', 'shellcmdflag=Something()', 'E520:')
242 call s:modeline_fails('shellpipe', 'shellpipe=Something()', 'E520:')
243 call s:modeline_fails('shellquote', 'shellquote=Something()', 'E520:')
244 call s:modeline_fails('shellredir', 'shellredir=Something()', 'E520:')
245 call s:modeline_fails('shellxquote', 'shellxquote=Something()', 'E520:')
246 call s:modeline_fails('spellfile', 'spellfile=Something()', 'E520:')
247 call s:modeline_fails('spellsuggest', 'spellsuggest=Something()', 'E520:')
248 call s:modeline_fails('tcldll', 'tcldll=Something()', 'E520:')
249 call s:modeline_fails('titleold', 'titleold=Something()', 'E520:')
250 call s:modeline_fails('viewdir', 'viewdir=Something()', 'E520:')
251 call s:modeline_fails('viminfo', 'viminfo=Something()', 'E520:')
252 call s:modeline_fails('viminfofile', 'viminfofile=Something()', 'E520:')
253 call s:modeline_fails('winptydll', 'winptydll=Something()', 'E520:')
254 call s:modeline_fails('undodir', 'undodir=Something()', 'E520:')
255 " only check a few terminal options
256 call s:modeline_fails('t_AB', 't_AB=Something()', 'E520:')
257 call s:modeline_fails('t_ce', 't_ce=Something()', 'E520:')
258 call s:modeline_fails('t_sr', 't_sr=Something()', 'E520:')
259 call s:modeline_fails('t_8b', 't_8b=Something()', 'E520:')
260endfunc
261
262func Test_modeline_fails_modelineexpr()
263 call s:modeline_fails('balloonexpr', 'balloonexpr=Something()', 'E992:')
264 call s:modeline_fails('foldexpr', 'foldexpr=Something()', 'E992:')
265 call s:modeline_fails('foldtext', 'foldtext=Something()', 'E992:')
266 call s:modeline_fails('formatexpr', 'formatexpr=Something()', 'E992:')
267 call s:modeline_fails('guitablabel', 'guitablabel=Something()', 'E992:')
268 call s:modeline_fails('iconstring', 'iconstring=Something()', 'E992:')
269 call s:modeline_fails('includeexpr', 'includeexpr=Something()', 'E992:')
270 call s:modeline_fails('indentexpr', 'indentexpr=Something()', 'E992:')
271 call s:modeline_fails('rulerformat', 'rulerformat=Something()', 'E992:')
272 call s:modeline_fails('statusline', 'statusline=Something()', 'E992:')
273 call s:modeline_fails('tabline', 'tabline=Something()', 'E992:')
274 call s:modeline_fails('titlestring', 'titlestring=Something()', 'E992:')
Bram Moolenaar916a8182018-11-25 02:18:29 +0100275endfunc
Bram Moolenaar51258742020-05-03 17:19:33 +0200276
277func Test_modeline_setoption_verbose()
278 let modeline = &modeline
279 set modeline
280
281 let lines =<< trim END
282 1 vim:ts=2
283 2 two
284 3 three
285 4 four
286 5 five
287 6 six
288 7 seven
289 8 eight
290 END
Bram Moolenaarb152b6a2022-09-29 21:37:33 +0100291 call writefile(lines, 'Xmodeline', 'D')
Bram Moolenaar51258742020-05-03 17:19:33 +0200292 edit Xmodeline
293 let info = split(execute('verbose set tabstop?'), "\n")
294 call assert_match('^\s*Last set from modeline line 1$', info[-1])
295 bwipe!
296
297 let lines =<< trim END
298 1 one
299 2 two
300 3 three
301 4 vim:ts=4
302 5 five
303 6 six
304 7 seven
305 8 eight
306 END
307 call writefile(lines, 'Xmodeline')
308 edit Xmodeline
309 let info = split(execute('verbose set tabstop?'), "\n")
310 call assert_match('^\s*Last set from modeline line 4$', info[-1])
311 bwipe!
312
313 let lines =<< trim END
314 1 one
315 2 two
316 3 three
317 4 four
318 5 five
319 6 six
320 7 seven
321 8 vim:ts=8
322 END
323 call writefile(lines, 'Xmodeline')
324 edit Xmodeline
325 let info = split(execute('verbose set tabstop?'), "\n")
326 call assert_match('^\s*Last set from modeline line 8$', info[-1])
327 bwipe!
328
329 let &modeline = modeline
Bram Moolenaar51258742020-05-03 17:19:33 +0200330endfunc
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200331
Yegappan Lakshmanan2d6d7182021-06-13 21:52:48 +0200332" Test for the 'modeline' default value in compatible and non-compatible modes
333" for root and non-root accounts
334func Test_modeline_default()
335 set compatible
336 call assert_false(&modeline)
337 set nocompatible
338 call assert_equal(IsRoot() ? 0 : 1, &modeline)
339 set compatible&vi
340 call assert_false(&modeline)
341 set compatible&vim
342 call assert_equal(IsRoot() ? 0 : 1, &modeline)
343 set compatible& modeline&
344endfunc
345
346" Some options cannot be set from the modeline when 'diff' option is set
347func Test_modeline_diff_buffer()
Bram Moolenaarb152b6a2022-09-29 21:37:33 +0100348 call writefile(['vim: diff foldmethod=marker wrap'], 'Xmdifile', 'D')
Yegappan Lakshmanan2d6d7182021-06-13 21:52:48 +0200349 set foldmethod& nowrap
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100350 new Xmdifile
Yegappan Lakshmanan2d6d7182021-06-13 21:52:48 +0200351 call assert_equal('manual', &foldmethod)
352 call assert_false(&wrap)
353 set wrap&
Yegappan Lakshmanan2d6d7182021-06-13 21:52:48 +0200354 bw
355endfunc
356
Hu Jialun9dcd3492021-08-28 20:42:50 +0200357func Test_modeline_disable()
358 set modeline
Bram Moolenaarb152b6a2022-09-29 21:37:33 +0100359 call writefile(['vim: sw=2', 'vim: nomodeline', 'vim: sw=3'], 'Xmodeline_disable', 'D')
Hu Jialun9dcd3492021-08-28 20:42:50 +0200360 edit Xmodeline_disable
361 call assert_equal(2, &sw)
Hu Jialun9dcd3492021-08-28 20:42:50 +0200362endfunc
363
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200364" vim: shiftwidth=2 sts=2 expandtab