blob: c4d8fd7ee57a076245f7e39bdf9727fb018c379f [file] [log] [blame]
Bram Moolenaareca626f2016-12-01 18:47:38 +01001" Test for :mksession, :mkview and :loadview in latin1 encoding
2
3set encoding=latin1
4scriptencoding latin1
5
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02006CheckFeature mksession
Bram Moolenaareca626f2016-12-01 18:47:38 +01007
Bram Moolenaar1e15e612019-11-09 17:18:52 +01008" Test for storing global and local argument list in a session file
9" This one must be done first.
10func Test__mksession_arglocal()
11 enew | only
12 n a b c
13 new
14 arglocal
15 mksession! Xtest_mks.out
16
17 %bwipe!
18 %argdelete
19 argglobal
20 source Xtest_mks.out
21 call assert_equal(2, winnr('$'))
22 call assert_equal(2, arglistid(1))
23 call assert_equal(0, arglistid(2))
24
25 %bwipe!
26 %argdelete
27 argglobal
28 call delete('Xtest_mks.out')
29endfunc
30
zeertzjqa304e492025-06-10 20:31:44 +020031func Test_mksession_arglocal_localdir()
32 call mkdir('Xa', 'R')
33 call writefile(['This is Xb'], 'Xa/Xb.txt', 'D')
34 let olddir = getcwd()
35 let oldargs = argv()
36
37 for tabpage in [v:false, v:true]
38 let msg = tabpage ? 'tabpage-local' : 'window-local'
39
40 exe tabpage ? 'tabnew' : 'botright new'
41 exe tabpage ? 'tcd Xa' : 'lcd Xa'
42 let localdir = getcwd()
43 arglocal
44 $argadd Xb.txt
45 let localargs = argv()
46 exe tabpage ? 'tabprev' : 'wincmd p'
47 call assert_equal(olddir, getcwd(), msg)
48 call assert_equal(oldargs, argv(), msg)
49 mksession! Xtest_mks_localdir.out
50 exe tabpage ? '+tabclose' : '$close'
51 bwipe! Xa/Xb.txt
52
53 source Xtest_mks_localdir.out
54 exe tabpage ? 'tabnext' : 'wincmd b'
55 call assert_equal(localdir, getcwd(), msg)
56 call assert_equal(localargs, argv(), msg)
57 $argument
58 call assert_equal('This is Xb', getline(1), msg)
59
60 bwipe!
61 call assert_equal(olddir, getcwd(), msg)
62 call assert_equal(oldargs, argv(), msg)
63 call delete('Xtest_mks_localdir.out')
64 endfor
65endfunc
66
Bram Moolenaareca626f2016-12-01 18:47:38 +010067func Test_mksession()
68 tabnew
69 let wrap_save = &wrap
70 set sessionoptions=buffers splitbelow fileencoding=latin1
71 call setline(1, [
72 \ 'start:',
73 \ 'no multibyte chAracter',
74 \ ' one leaDing tab',
75 \ ' four leadinG spaces',
76 \ 'two consecutive tabs',
77 \ 'two tabs in one line',
78 \ 'one ä multibyteCharacter',
79 \ 'aä Ä two multiByte characters',
Bram Moolenaar92c1b692018-08-29 21:42:42 +020080 \ 'Aäöü three mulTibyte characters',
81 \ 'short line',
Bram Moolenaareca626f2016-12-01 18:47:38 +010082 \ ])
Bram Moolenaarc9b56b22017-01-29 14:14:09 +010083 let tmpfile = 'Xtemp'
Bram Moolenaareca626f2016-12-01 18:47:38 +010084 exec 'w! ' . tmpfile
85 /^start:
86 set wrap
87 vsplit
88 norm! j16|
89 split
90 norm! j16|
91 split
92 norm! j16|
93 split
94 norm! j8|
95 split
96 norm! j8|
97 split
98 norm! j16|
99 split
100 norm! j16|
101 split
102 norm! j16|
Bram Moolenaar92c1b692018-08-29 21:42:42 +0200103 split
104 norm! j$
Bram Moolenaareca626f2016-12-01 18:47:38 +0100105 wincmd l
106
107 set nowrap
108 /^start:
109 norm! j16|3zl
110 split
111 norm! j016|3zl
112 split
113 norm! j016|3zl
114 split
115 norm! j08|3zl
116 split
117 norm! j08|3zl
118 split
119 norm! j016|3zl
120 split
121 norm! j016|3zl
122 split
123 norm! j016|3zl
124 split
125 call wincol()
Bram Moolenaarc9b56b22017-01-29 14:14:09 +0100126 mksession! Xtest_mks.out
Bram Moolenaar92c1b692018-08-29 21:42:42 +0200127 let li = filter(readfile('Xtest_mks.out'), 'v:val =~# "\\(^ *normal! [0$]\\|^ *exe ''normal!\\)"')
Bram Moolenaareca626f2016-12-01 18:47:38 +0100128 let expected = [
129 \ 'normal! 016|',
130 \ 'normal! 016|',
131 \ 'normal! 016|',
132 \ 'normal! 08|',
133 \ 'normal! 08|',
134 \ 'normal! 016|',
135 \ 'normal! 016|',
136 \ 'normal! 016|',
Bram Moolenaar92c1b692018-08-29 21:42:42 +0200137 \ 'normal! $',
Bram Moolenaareca626f2016-12-01 18:47:38 +0100138 \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
139 \ " normal! 016|",
140 \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
141 \ " normal! 016|",
142 \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
143 \ " normal! 016|",
144 \ " exe 'normal! ' . s:c . '|zs' . 8 . '|'",
145 \ " normal! 08|",
146 \ " exe 'normal! ' . s:c . '|zs' . 8 . '|'",
147 \ " normal! 08|",
148 \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
149 \ " normal! 016|",
150 \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
151 \ " normal! 016|",
152 \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
153 \ " normal! 016|",
154 \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
155 \ " normal! 016|"
156 \ ]
157 call assert_equal(expected, li)
158 tabclose!
159
Bram Moolenaarc9b56b22017-01-29 14:14:09 +0100160 call delete('Xtest_mks.out')
Bram Moolenaareca626f2016-12-01 18:47:38 +0100161 call delete(tmpfile)
162 let &wrap = wrap_save
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100163 set sessionoptions&
Bram Moolenaareca626f2016-12-01 18:47:38 +0100164endfunc
165
Bram Moolenaar635bd602021-04-16 19:58:22 +0200166def Test_mksession_skiprtp()
167 mksession! Xtest_mks.out
Bram Moolenaard23b7142021-04-17 21:04:34 +0200168 var found_rtp = 0
169 var found_pp = 0
Bram Moolenaar635bd602021-04-16 19:58:22 +0200170 for line in readfile('Xtest_mks.out')
171 if line =~ 'set runtimepath'
Bram Moolenaard23b7142021-04-17 21:04:34 +0200172 found_rtp += 1
173 endif
174 if line =~ 'set packpath'
175 found_pp += 1
Bram Moolenaar635bd602021-04-16 19:58:22 +0200176 endif
177 endfor
Bram Moolenaard23b7142021-04-17 21:04:34 +0200178 assert_equal(1, found_rtp)
179 assert_equal(1, found_pp)
Bram Moolenaar635bd602021-04-16 19:58:22 +0200180 delete('Xtest_mks.out')
181
182 set sessionoptions+=skiprtp
183 mksession! Xtest_mks.out
Bram Moolenaard23b7142021-04-17 21:04:34 +0200184 var found = 0
Bram Moolenaar635bd602021-04-16 19:58:22 +0200185 for line in readfile('Xtest_mks.out')
Bram Moolenaard23b7142021-04-17 21:04:34 +0200186 if line =~ 'set \(runtimepath\|packpath\)'
Bram Moolenaar635bd602021-04-16 19:58:22 +0200187 found = 1
188 break
189 endif
190 endfor
191 assert_equal(0, found)
192 delete('Xtest_mks.out')
193 set sessionoptions&
194enddef
195
Bram Moolenaar36ae89c2017-01-28 17:11:14 +0100196func Test_mksession_winheight()
197 new
Bram Moolenaar1c3c1042018-06-12 16:49:30 +0200198 set winheight=10
199 set winminheight=2
Bram Moolenaarc9b56b22017-01-29 14:14:09 +0100200 mksession! Xtest_mks.out
201 source Xtest_mks.out
Bram Moolenaar36ae89c2017-01-28 17:11:14 +0100202
Bram Moolenaarc9b56b22017-01-29 14:14:09 +0100203 call delete('Xtest_mks.out')
Bram Moolenaar36ae89c2017-01-28 17:11:14 +0100204endfunc
205
Bram Moolenaar1c3c1042018-06-12 16:49:30 +0200206func Test_mksession_large_winheight()
207 set winheight=999
208 mksession! Xtest_mks_winheight.out
209 set winheight&
210 source Xtest_mks_winheight.out
211 call delete('Xtest_mks_winheight.out')
212endfunc
213
Bram Moolenaarb6c2e9a2021-04-30 21:37:51 +0200214func Test_mksession_zero_winheight()
215 set winminheight=0
216 edit SomeFile
217 split
218 wincmd _
219 mksession! Xtest_mks_zero
220 set winminheight&
221 let text = readfile('Xtest_mks_zero')->join()
Dominique Pelle5f8ed742021-05-03 19:08:37 +0200222 call delete('Xtest_mks_zero')
Bram Moolenaarb6c2e9a2021-04-30 21:37:51 +0200223 close
Dominique Pelle5f8ed742021-05-03 19:08:37 +0200224 " check there is no divide by zero
Bram Moolenaarb6c2e9a2021-04-30 21:37:51 +0200225 call assert_notmatch('/ 0[^0-9]', text)
226endfunc
227
Bram Moolenaared18f2c2019-01-24 20:30:52 +0100228func Test_mksession_rtp()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200229 " TODO: fix problem with backslashes on Win32
230 CheckNotMSWindows
231
Bram Moolenaared18f2c2019-01-24 20:30:52 +0100232 new
233 let _rtp=&rtp
234 " Make a real long (invalid) runtimepath value,
235 " that should exceed PATH_MAX (hopefully)
236 let newrtp=&rtp.',~'.repeat('/foobar', 1000)
237 let newrtp.=",".expand("$HOME")."/.vim"
238 let &rtp=newrtp
239
240 " determine expected value
241 let expected=split(&rtp, ',')
242 let expected = map(expected, '"set runtimepath+=".v:val')
243 let expected = ['set runtimepath='] + expected
244 let expected = map(expected, {v,w -> substitute(w, $HOME, "~", "g")})
245
246 mksession! Xtest_mks.out
247 let &rtp=_rtp
248 let li = filter(readfile('Xtest_mks.out'), 'v:val =~# "runtimepath"')
249 call assert_equal(expected, li)
250
251 call delete('Xtest_mks.out')
252endfunc
253
Bram Moolenaar79da5632017-02-01 22:52:44 +0100254func Test_mksession_arglist()
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200255 %argdel
Bram Moolenaar79da5632017-02-01 22:52:44 +0100256 next file1 file2 file3 file4
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200257 new
258 next | next
Bram Moolenaar79da5632017-02-01 22:52:44 +0100259 mksession! Xtest_mks.out
260 source Xtest_mks.out
261 call assert_equal(['file1', 'file2', 'file3', 'file4'], argv())
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200262 call assert_equal(2, argidx())
263 wincmd w
264 call assert_equal(0, argidx())
Bram Moolenaar79da5632017-02-01 22:52:44 +0100265
266 call delete('Xtest_mks.out')
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200267 enew | only
Bram Moolenaar79da5632017-02-01 22:52:44 +0100268 argdel *
269endfunc
270
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200271func Test_mksession_one_buffer_two_windows()
272 edit Xtest1
273 new Xtest2
274 split
275 mksession! Xtest_mks.out
276 let lines = readfile('Xtest_mks.out')
277 let count1 = 0
278 let count2 = 0
279 let count2buf = 0
James Cherti7d428402022-03-14 20:24:51 +0000280 let bufexists = 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200281 for line in lines
282 if line =~ 'edit \f*Xtest1$'
283 let count1 += 1
284 endif
285 if line =~ 'edit \f\{-}Xtest2'
286 let count2 += 1
287 endif
288 if line =~ 'buffer \f\{-}Xtest2'
289 let count2buf += 1
290 endif
James Cherti7d428402022-03-14 20:24:51 +0000291 if line =~ 'bufexists(fnamemodify(.*, ":p")'
292 let bufexists += 1
293 endif
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200294 endfor
295 call assert_equal(1, count1, 'Xtest1 count')
296 call assert_equal(2, count2, 'Xtest2 count')
297 call assert_equal(2, count2buf, 'Xtest2 buffer count')
James Cherti7d428402022-03-14 20:24:51 +0000298 call assert_equal(2, bufexists)
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200299
300 close
301 bwipe!
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200302 call delete('Xtest_mks.out')
303endfunc
304
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200305func Test_mksession_lcd_multiple_tabs()
306 tabnew
307 tabnew
Bram Moolenaar81e2ac72018-07-04 22:44:08 +0200308 lcd .
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200309 tabfirst
Bram Moolenaar81e2ac72018-07-04 22:44:08 +0200310 lcd .
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200311 mksession! Xtest_mks.out
312 tabonly
313 source Xtest_mks.out
314 call assert_true(haslocaldir(), 'Tab 1 localdir')
315 tabnext 2
316 call assert_true(!haslocaldir(), 'Tab 2 localdir')
317 tabnext 3
318 call assert_true(haslocaldir(), 'Tab 3 localdir')
319 call delete('Xtest_mks.out')
320endfunc
321
Bram Moolenaar00aa0692019-04-27 20:37:57 +0200322" Test for tabpage-local directory
323func Test_mksession_tcd_multiple_tabs()
324 let save_cwd = getcwd()
325 call mkdir('Xtopdir')
326 cd Xtopdir
327 call mkdir('Xtabdir1')
328 call mkdir('Xtabdir2')
329 call mkdir('Xtabdir3')
330 call mkdir('Xwindir1')
331 call mkdir('Xwindir2')
332 call mkdir('Xwindir3')
333 tcd Xtabdir1
334 botright new
335 wincmd t
336 lcd ../Xwindir1
337 tabnew
338 tcd ../Xtabdir2
339 botright new
340 lcd ../Xwindir2
341 tabnew
342 tcd ../Xtabdir3
343 botright new
344 lcd ../Xwindir3
345 tabfirst
346 1wincmd w
347 mksession! Xtest_mks.out
348 only | tabonly
349 source Xtest_mks.out
350 call assert_equal('Xtabdir1', fnamemodify(getcwd(-1, 1), ':t'))
351 call assert_equal('Xwindir1', fnamemodify(getcwd(1, 1), ':t'))
352 call assert_equal('Xtabdir1', fnamemodify(getcwd(2, 1), ':t'))
353 call assert_equal('Xtabdir2', fnamemodify(getcwd(-1, 2), ':t'))
354 call assert_equal('Xtabdir2', fnamemodify(getcwd(1, 2), ':t'))
355 call assert_equal('Xwindir2', fnamemodify(getcwd(2, 2), ':t'))
356 call assert_equal('Xtabdir3', fnamemodify(getcwd(-1, 3), ':t'))
357 call assert_equal('Xtabdir3', fnamemodify(getcwd(1, 3), ':t'))
358 call assert_equal('Xwindir3', fnamemodify(getcwd(2, 3), ':t'))
Bram Moolenaar5bf46e92019-11-09 18:06:04 +0100359 %bwipe
360 call chdir(save_cwd)
Bram Moolenaar00aa0692019-04-27 20:37:57 +0200361 call delete("Xtopdir", "rf")
362endfunc
363
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200364func Test_mksession_blank_tabs()
365 tabnew
366 tabnew
367 tabnew
368 tabnext 3
369 mksession! Xtest_mks.out
370 tabnew
371 tabnew
372 tabnext 2
373 source Xtest_mks.out
374 call assert_equal(4, tabpagenr('$'), 'session restore should restore number of tabs')
375 call assert_equal(3, tabpagenr(), 'session restore should restore the active tab')
376 call delete('Xtest_mks.out')
377endfunc
378
Bram Moolenaard39e2752019-01-26 20:07:38 +0100379func Test_mksession_buffer_count()
380 set hidden
381
382 " Edit exactly three files in the current session.
383 %bwipe!
384 e Xfoo | tabe Xbar | tabe Xbaz
385 tabdo write
386 mksession! Xtest_mks.out
387
388 " Verify that loading the session does not create additional buffers.
389 %bwipe!
390 source Xtest_mks.out
391 call assert_equal(3, len(getbufinfo()))
392
393 " Clean up.
394 call delete('Xfoo')
395 call delete('Xbar')
396 call delete('Xbaz')
397 call delete('Xtest_mks.out')
398 %bwipe!
399 set hidden&
400endfunc
401
Evgeni Chasnovski26ebf1f2022-01-14 13:19:43 +0000402func Test_mksession_buffer_order()
403 %bwipe!
404 e Xfoo | e Xbar | e Xbaz | e Xqux
405 bufdo write
406 mksession! Xtest_mks.out
407
408 " Verify that loading the session preserves order of buffers
409 %bwipe!
410 source Xtest_mks.out
411
412 let s:buf_info = getbufinfo()
413 call assert_true(s:buf_info[0]['name'] =~# 'Xfoo$')
414 call assert_true(s:buf_info[1]['name'] =~# 'Xbar$')
415 call assert_true(s:buf_info[2]['name'] =~# 'Xbaz$')
416 call assert_true(s:buf_info[3]['name'] =~# 'Xqux$')
417
418 " Clean up.
419 call delete('Xfoo')
420 call delete('Xbar')
421 call delete('Xbaz')
422 call delete('Xqux')
423 call delete('Xtest_mks.out')
424 %bwipe!
425endfunc
426
Bram Moolenaare3c74d22019-01-12 16:29:30 +0100427if has('extra_search')
428
429func Test_mksession_hlsearch()
430 set hlsearch
431 mksession! Xtest_mks.out
432 nohlsearch
433 source Xtest_mks.out
434 call assert_equal(1, v:hlsearch, 'session should restore search highlighting state')
435 nohlsearch
436 mksession! Xtest_mks.out
437 source Xtest_mks.out
438 call assert_equal(0, v:hlsearch, 'session should restore search highlighting state')
439 call delete('Xtest_mks.out')
440endfunc
441
442endif
443
444
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200445func Test_mksession_blank_windows()
446 split
447 split
448 split
449 3 wincmd w
450 mksession! Xtest_mks.out
451 split
452 split
453 2 wincmd w
454 source Xtest_mks.out
455 call assert_equal(4, winnr('$'), 'session restore should restore number of windows')
456 call assert_equal(3, winnr(), 'session restore should restore the active window')
457 call delete('Xtest_mks.out')
458endfunc
459
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100460func Test_mksession_terminal_shell()
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200461 CheckFeature terminal
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100462 CheckFeature quickfix
463
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100464 terminal
465 mksession! Xtest_mks.out
466 let lines = readfile('Xtest_mks.out')
467 let term_cmd = ''
468 for line in lines
469 if line =~ '^terminal'
470 let term_cmd = line
471 elseif line =~ 'badd.*' . &shell
472 call assert_report('unexpected shell line: ' . line)
473 endif
474 endfor
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100475 call assert_match('terminal ++curwin ++cols=\d\+ ++rows=\d\+\s*.*$', term_cmd)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100476
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200477 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100478 call delete('Xtest_mks.out')
479endfunc
480
481func Test_mksession_terminal_no_restore_cmdarg()
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200482 CheckFeature terminal
483
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100484 terminal ++norestore
485 mksession! Xtest_mks.out
486 let lines = readfile('Xtest_mks.out')
487 let term_cmd = ''
488 for line in lines
489 if line =~ '^terminal'
Bram Moolenaarac665c22020-12-08 20:39:15 +0100490 call assert_report('session must not restore terminal')
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100491 endif
492 endfor
493
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200494 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100495 call delete('Xtest_mks.out')
496endfunc
497
498func Test_mksession_terminal_no_restore_funcarg()
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200499 CheckFeature terminal
500
Bram Moolenaar3a6aadb2021-04-04 15:28:59 +0200501 let buf = Run_shell_in_terminal({'norestore': 1})
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100502 mksession! Xtest_mks.out
503 let lines = readfile('Xtest_mks.out')
504 let term_cmd = ''
505 for line in lines
506 if line =~ '^terminal'
Bram Moolenaarac665c22020-12-08 20:39:15 +0100507 call assert_report('session must not restore terminal')
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100508 endif
509 endfor
510
Bram Moolenaar3a6aadb2021-04-04 15:28:59 +0200511 call StopShellInTerminal(buf)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100512 call delete('Xtest_mks.out')
513endfunc
514
515func Test_mksession_terminal_no_restore_func()
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200516 CheckFeature terminal
517
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100518 terminal
519 call term_setrestore(bufnr('%'), 'NONE')
520 mksession! Xtest_mks.out
521 let lines = readfile('Xtest_mks.out')
522 let term_cmd = ''
523 for line in lines
524 if line =~ '^terminal'
Bram Moolenaarac665c22020-12-08 20:39:15 +0100525 call assert_report('session must not restore terminal')
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100526 endif
527 endfor
528
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200529 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100530 call delete('Xtest_mks.out')
531endfunc
532
533func Test_mksession_terminal_no_ssop()
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200534 CheckFeature terminal
535
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100536 terminal
537 set sessionoptions-=terminal
538 mksession! Xtest_mks.out
539 let lines = readfile('Xtest_mks.out')
540 let term_cmd = ''
541 for line in lines
542 if line =~ '^terminal'
Bram Moolenaarac665c22020-12-08 20:39:15 +0100543 call assert_report('session must not restore terminal')
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100544 endif
545 endfor
546
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200547 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100548 call delete('Xtest_mks.out')
549 set sessionoptions&
550endfunc
551
552func Test_mksession_terminal_restore_other()
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200553 CheckFeature terminal
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100554 CheckFeature quickfix
555
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100556 terminal
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200557 eval bufnr('%')->term_setrestore('other')
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100558 mksession! Xtest_mks.out
559 let lines = readfile('Xtest_mks.out')
560 let term_cmd = ''
561 for line in lines
562 if line =~ '^terminal'
563 let term_cmd = line
564 endif
565 endfor
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100566 call assert_match('terminal ++curwin ++cols=\d\+ ++rows=\d\+.*other', term_cmd)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100567
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200568 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100569 call delete('Xtest_mks.out')
570endfunc
571
Bram Moolenaar0e655112020-09-11 20:36:36 +0200572func Test_mksession_terminal_shared_windows()
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200573 CheckFeature terminal
574
Bram Moolenaar0e655112020-09-11 20:36:36 +0200575 terminal
576 let term_buf = bufnr()
577 new
578 execute "buffer" term_buf
579 mksession! Xtest_mks.out
580
581 let lines = readfile('Xtest_mks.out')
582 let found_creation = 0
583 let found_use = 0
584
585 for line in lines
586 if line =~ '^terminal'
587 let found_creation = 1
588 call assert_match('terminal ++curwin ++cols=\d\+ ++rows=\d\+', line)
589 elseif line =~ "^execute 'buffer ' . s:term_buf_" . term_buf . "$"
590 let found_use = 1
591 endif
592 endfor
593
594 call assert_true(found_creation && found_use)
595
596 call StopShellInTerminal(term_buf)
597 call delete('Xtest_mks.out')
598endfunc
599
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200600func Test_mkview_terminal_windows()
601 CheckFeature terminal
602
Christian Brabandtee17b6f2023-09-09 11:23:50 +0200603 " create two windows on the same terminal to check this is handled OK
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200604 terminal
605 let term_buf = bufnr()
606 exe 'sbuf ' .. term_buf
607 mkview! Xtestview
608
609 call StopShellInTerminal(term_buf)
610 call delete('Xtestview')
611endfunc
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200612
Bram Moolenaarf9547eb2021-02-01 19:24:55 +0100613func Test_mkview_open_folds()
614 enew!
615
616 call append(0, ['a', 'b', 'c'])
617 1,3fold
Bram Moolenaarf9547eb2021-02-01 19:24:55 +0100618 write! Xtestfile
619
James McCoyc829faa2022-06-19 17:16:18 +0100620 call assert_notequal(-1, foldclosed(1))
621 call assert_notequal(-1, foldclosed(2))
622 call assert_notequal(-1, foldclosed(3))
623
624 " Save the view with folds closed
625 mkview! Xtestview
626
627 " zR affects 'foldlevel', make sure the option is applied after the folds
628 " have been recreated.
629 " Open folds to ensure they get closed when restoring the view
630 normal zR
631
Bram Moolenaarf9547eb2021-02-01 19:24:55 +0100632 call assert_equal(-1, foldclosed(1))
633 call assert_equal(-1, foldclosed(2))
634 call assert_equal(-1, foldclosed(3))
635
Bram Moolenaarf9547eb2021-02-01 19:24:55 +0100636 source Xtestview
637
James McCoyc829faa2022-06-19 17:16:18 +0100638 call assert_notequal(-1, foldclosed(1))
639 call assert_notequal(-1, foldclosed(2))
640 call assert_notequal(-1, foldclosed(3))
Bram Moolenaarf9547eb2021-02-01 19:24:55 +0100641
642 call delete('Xtestview')
643 call delete('Xtestfile')
644 %bwipe
645endfunc
646
Bram Moolenaar139348f2021-02-05 21:55:53 +0100647func Test_mkview_no_balt()
648 edit Xtestfile1
649 edit Xtestfile2
650
651 mkview! Xtestview
652 bdelete Xtestfile1
653
654 source Xtestview
655 call assert_equal(0, buflisted('Xtestfile1'))
656
657 call delete('Xtestview')
658 %bwipe
659endfunc
660
Bram Moolenaar0756f752021-03-13 13:52:33 +0100661func Test_mksession_no_balt()
662 edit Xtestfile1
663 edit Xtestfile2
664
665 bdelete Xtestfile1
666 mksession! Xtestview
667
668 source Xtestview
669 call assert_equal(0, buflisted('Xtestfile1'))
670
671 call delete('Xtestview')
672 %bwipe
673endfunc
674
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200675" Test :mkview with a file argument.
676func Test_mkview_file()
677 " Create a view with line number and a fold.
678 help :mkview
679 set number
Bram Moolenaar2bf4fe02018-08-30 14:04:25 +0200680 norm! V}zf0
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200681 let pos = getpos('.')
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200682 let linefoldclosed1 = foldclosed('.')
683 mkview! Xview
684 set nonumber
685 norm! zrj
686 " We can close the help window, as mkview with a file name should
687 " generate a command to edit the file.
688 helpclose
689
690 source Xview
691 call assert_equal(1, &number)
692 call assert_match('\*:mkview\*$', getline('.'))
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200693 call assert_equal(pos, getpos('.'))
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200694 call assert_equal(linefoldclosed1, foldclosed('.'))
695
696 " Creating a view again with the same file name should fail (file
697 " already exists). But with a !, the previous view should be
698 " overwritten without error.
699 help :loadview
700 call assert_fails('mkview Xview', 'E189:')
701 call assert_match('\*:loadview\*$', getline('.'))
702 mkview! Xview
703 call assert_match('\*:loadview\*$', getline('.'))
704
705 call delete('Xview')
706 bwipe
707endfunc
708
709" Test :mkview and :loadview with a custom 'viewdir'.
710func Test_mkview_loadview_with_viewdir()
711 set viewdir=Xviewdir
712
713 help :mkview
714 set number
715 norm! V}zf
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200716 let pos = getpos('.')
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200717 let linefoldclosed1 = foldclosed('.')
718 mkview 1
719 set nonumber
720 norm! zrj
721
722 loadview 1
723
724 " The directory Xviewdir/ should have been created and the view
725 " should be stored in that directory.
726 call assert_equal('Xviewdir/' .
727 \ substitute(
728 \ substitute(
729 \ expand('%:p'), '/', '=+', 'g'), ':', '=-', 'g') . '=1.vim',
730 \ glob('Xviewdir/*'))
731 call assert_equal(1, &number)
732 call assert_match('\*:mkview\*$', getline('.'))
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200733 call assert_equal(pos, getpos('.'))
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200734 call assert_equal(linefoldclosed1, foldclosed('.'))
735
736 call delete('Xviewdir', 'rf')
737 set viewdir&
738 helpclose
739endfunc
740
741func Test_mkview_no_file_name()
742 new
zeertzjqc029c132024-03-28 11:37:26 +0100743 " :mkview or :mkview {nr} should fail in an unnamed buffer.
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200744 call assert_fails('mkview', 'E32:')
745 call assert_fails('mkview 1', 'E32:')
746
zeertzjqc029c132024-03-28 11:37:26 +0100747 " :mkview {file} should succeed in an unnamed buffer.
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200748 mkview Xview
749 help
750 source Xview
751 call assert_equal('', bufname('%'))
752
753 call delete('Xview')
754 %bwipe
755endfunc
Bram Moolenaar79da5632017-02-01 22:52:44 +0100756
Bram Moolenaar3482be62020-11-27 11:00:38 +0100757func Test_mkview_loadview_jumplist()
758 set viewdir=Xviewdir
759 au BufWinLeave * silent mkview
760 au BufWinEnter * silent loadview
761
762 edit Xfile1
763 call setline(1, ['a', 'bbbbbbb', 'c'])
764 normal j3l
765 call assert_equal([2, 4], getcurpos()[1:2])
766 write
767
768 edit Xfile2
769 call setline(1, ['d', 'eeeeeee', 'f'])
770 normal j5l
771 call assert_equal([2, 6], getcurpos()[1:2])
772 write
773
774 edit Xfile3
775 call setline(1, ['g', 'h', 'iiiii'])
776 normal jj3l
777 call assert_equal([3, 4], getcurpos()[1:2])
778 write
779
780 edit Xfile1
781 call assert_equal([2, 4], getcurpos()[1:2])
782 edit Xfile2
783 call assert_equal([2, 6], getcurpos()[1:2])
784 edit Xfile3
785 call assert_equal([3, 4], getcurpos()[1:2])
786
787 exe "normal \<C-O>"
788 call assert_equal('Xfile2', expand('%'))
789 call assert_equal([2, 6], getcurpos()[1:2])
790 exe "normal \<C-O>"
791 call assert_equal('Xfile1', expand('%'))
792 call assert_equal([2, 4], getcurpos()[1:2])
793
794 au! BufWinLeave
795 au! BufWinEnter
796 bwipe!
797 call delete('Xviewdir', 'rf')
798 call delete('Xfile1')
799 call delete('Xfile2')
800 call delete('Xfile3')
801 set viewdir&
802endfunc
803
Bram Moolenaar555de4e2019-01-21 23:03:49 +0100804" A clean session (one empty buffer, one window, and one tab) should not
805" set any error messages when sourced because no commands should fail.
806func Test_mksession_no_errmsg()
807 let v:errmsg = ''
808 %bwipe!
809 mksession! Xtest_mks.out
810 source Xtest_mks.out
811 call assert_equal('', v:errmsg)
812 call delete('Xtest_mks.out')
813endfunc
814
Bram Moolenaarad36a352019-01-24 13:34:42 +0100815func Test_mksession_quote_in_filename()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200816 " only Unix can handle this weird filename
817 CheckUnix
818
Bram Moolenaarad36a352019-01-24 13:34:42 +0100819 let v:errmsg = ''
820 %bwipe!
821 split another
822 split x'y\"z
823 mksession! Xtest_mks_quoted.out
824 %bwipe!
825 source Xtest_mks_quoted.out
826 call assert_true(bufexists("x'y\"z"))
827
828 %bwipe!
829 call delete('Xtest_mks_quoted.out')
830endfunc
831
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200832" Test for storing global variables in a session file
833func Test_mksession_globals()
834 set sessionoptions+=globals
835
836 " create different global variables
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200837 let g:Global_string = "Sun is shining\r\n"
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200838 let g:Global_count = 100
839 let g:Global_pi = 3.14
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200840 let g:Global_neg_float = -2.68
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200841
842 mksession! Xtest_mks.out
843
844 unlet g:Global_string
845 unlet g:Global_count
846 unlet g:Global_pi
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200847 unlet g:Global_neg_float
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200848
849 source Xtest_mks.out
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200850 call assert_equal("Sun is shining\r\n", g:Global_string)
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200851 call assert_equal(100, g:Global_count)
852 call assert_equal(3.14, g:Global_pi)
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200853 call assert_equal(-2.68, g:Global_neg_float)
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200854
855 unlet g:Global_string
856 unlet g:Global_count
857 unlet g:Global_pi
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200858 unlet g:Global_neg_float
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200859 call delete('Xtest_mks.out')
860 set sessionoptions&
861endfunc
862
863" Test for changing backslash to forward slash in filenames
864func Test_mksession_slash()
Bram Moolenaar37ac4a52019-09-28 19:29:04 +0200865 if exists('+shellslash')
866 throw 'Skipped: cannot use backslash in file name'
867 endif
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200868 enew
869 %bwipe!
870 e a\\b\\c
871 mksession! Xtest_mks1.out
872 set sessionoptions+=slash
873 mksession! Xtest_mks2.out
874
875 %bwipe!
876 source Xtest_mks1.out
877 call assert_equal('a\b\c', bufname(''))
878 %bwipe!
879 source Xtest_mks2.out
880 call assert_equal('a/b/c', bufname(''))
881
882 %bwipe!
883 call delete('Xtest_mks1.out')
884 call delete('Xtest_mks2.out')
885 set sessionoptions&
886endfunc
887
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200888" Test for changing directory to the session file directory
889func Test_mksession_sesdir()
Bram Moolenaar5bf46e92019-11-09 18:06:04 +0100890 let save_cwd = getcwd()
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200891 call mkdir('Xproj')
892 mksession! Xproj/Xtest_mks1.out
893 set sessionoptions-=curdir
894 set sessionoptions+=sesdir
895 mksession! Xproj/Xtest_mks2.out
896
897 source Xproj/Xtest_mks1.out
898 call assert_equal('testdir', fnamemodify(getcwd(), ':t'))
899 source Xproj/Xtest_mks2.out
900 call assert_equal('Xproj', fnamemodify(getcwd(), ':t'))
Bram Moolenaar5bf46e92019-11-09 18:06:04 +0100901 call chdir(save_cwd)
902 %bwipe
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200903
904 set sessionoptions&
905 call delete('Xproj', 'rf')
906endfunc
907
LemonBoyd7c95642022-04-30 16:10:27 +0100908" Test for saving and restoring the tab-local working directory when there is
909" only a single tab and 'tabpages' is not in 'sessionoptions'.
910func Test_mksession_tcd_single_tabs()
911 only | tabonly
912
913 let save_cwd = getcwd()
914 set sessionoptions-=tabpages
915 set sessionoptions+=curdir
916 call mkdir('Xtopdir1')
917 call mkdir('Xtopdir2')
918
919 " There are two tab pages, the current one has local cwd set to 'Xtopdir2'.
920 exec 'tcd ' .. save_cwd .. '/Xtopdir1'
921 tabnew
922 exec 'tcd ' .. save_cwd .. '/Xtopdir2'
923 mksession! Xtest_tcd_single
924
925 source Xtest_tcd_single
926 call assert_equal(2, haslocaldir())
927 call assert_equal('Xtopdir2', fnamemodify(getcwd(-1, 0), ':t'))
928 %bwipe
929
930 set sessionoptions&
931 call chdir(save_cwd)
932 call delete('Xtopdir1', 'rf')
933 call delete('Xtopdir2', 'rf')
934endfunc
935
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200936" Test for storing the 'lines' and 'columns' settings
937func Test_mksession_resize()
938 mksession! Xtest_mks1.out
939 set sessionoptions+=resize
940 mksession! Xtest_mks2.out
941
942 let lines = readfile('Xtest_mks1.out')
943 let found_resize = v:false
944 for line in lines
945 if line =~ '^set lines='
946 let found_resize = v:true
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200947 break
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200948 endif
949 endfor
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200950 call assert_false(found_resize)
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200951 let lines = readfile('Xtest_mks2.out')
952 let found_resize = v:false
953 for line in lines
954 if line =~ '^set lines='
955 let found_resize = v:true
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200956 break
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200957 endif
958 endfor
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200959 call assert_true(found_resize)
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200960
961 call delete('Xtest_mks1.out')
962 call delete('Xtest_mks2.out')
963 set sessionoptions&
964endfunc
965
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200966" Test for mksession with a named scratch buffer
967func Test_mksession_scratch()
968 enew | only
969 file Xscratch
970 set buftype=nofile
971 mksession! Xtest_mks.out
972 %bwipe
973 source Xtest_mks.out
974 call assert_equal('Xscratch', bufname(''))
975 call assert_equal('nofile', &buftype)
976 %bwipe
977 call delete('Xtest_mks.out')
978endfunc
979
980" Test for mksession with fold options
981func Test_mksession_foldopt()
982 set sessionoptions-=options
983 set sessionoptions+=folds
984 new
985 setlocal foldenable
986 setlocal foldmethod=expr
987 setlocal foldmarker=<<<,>>>
988 setlocal foldignore=%
989 setlocal foldlevel=2
990 setlocal foldminlines=10
991 setlocal foldnestmax=15
992 mksession! Xtest_mks.out
993 close
994 %bwipe
995
996 source Xtest_mks.out
997 call assert_true(&foldenable)
998 call assert_equal('expr', &foldmethod)
999 call assert_equal('<<<,>>>', &foldmarker)
1000 call assert_equal('%', &foldignore)
1001 call assert_equal(2, &foldlevel)
1002 call assert_equal(10, &foldminlines)
1003 call assert_equal(15, &foldnestmax)
1004
1005 close
1006 %bwipe
1007 set sessionoptions&
Christian Brabandt84bc00e2023-07-13 11:45:54 +02001008 call delete('Xtest_mks.out')
Bram Moolenaaree1a2b52019-09-29 14:23:39 +02001009endfunc
1010
matveyt8e7d9db2022-01-05 14:01:30 +00001011" Test for mksession with "help" but not "options" in 'sessionoptions'
1012func Test_mksession_help_noopt()
1013 set sessionoptions-=options
1014 set sessionoptions+=help
1015 help
1016 let fname = expand('%')
1017 mksession! Xtest_mks.out
1018 bwipe
1019
1020 source Xtest_mks.out
1021 call assert_equal('help', &buftype)
1022 call assert_equal('help', &filetype)
1023 call assert_equal(fname, expand('%'))
1024 call assert_false(&modifiable)
1025 call assert_true(&readonly)
1026
1027 helpclose
1028 help index
1029 let fname = expand('%')
1030 mksession! Xtest_mks.out
1031 bwipe
1032
1033 source Xtest_mks.out
1034 call assert_equal('help', &buftype)
1035 call assert_equal(fname, expand('%'))
1036
1037 call delete('Xtest_mks.out')
1038 set sessionoptions&
1039endfunc
1040
Bram Moolenaaree1a2b52019-09-29 14:23:39 +02001041" Test for mksession with window position
1042func Test_mksession_winpos()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001043 " Only applicable in GUI Vim
1044 CheckGui
1045
Bram Moolenaaree1a2b52019-09-29 14:23:39 +02001046 set sessionoptions+=winpos
1047 mksession! Xtest_mks.out
1048 let found_winpos = v:false
1049 let lines = readfile('Xtest_mks.out')
1050 for line in lines
1051 if line =~ '^winpos '
1052 let found_winpos = v:true
1053 break
1054 endif
1055 endfor
1056 call assert_true(found_winpos)
1057 call delete('Xtest_mks.out')
1058 set sessionoptions&
1059endfunc
1060
Bram Moolenaar0995c812021-04-17 18:38:54 +02001061" Test for mksession without options restores winminheight
1062func Test_mksession_winminheight()
1063 set sessionoptions-=options
1064 split
1065 mksession! Xtest_mks.out
1066 let found_restore = 0
1067 let lines = readfile('Xtest_mks.out')
1068 for line in lines
1069 if line =~ '= s:save_winmin\(width\|height\)'
1070 let found_restore += 1
1071 endif
1072 endfor
1073 call assert_equal(2, found_restore)
1074 call delete('Xtest_mks.out')
1075 close
1076 set sessionoptions&
1077endfunc
1078
James Chertifd012802022-03-29 12:02:57 +01001079" Test for mksession with and without options restores shortmess
1080func Test_mksession_shortmess()
1081 " Without options
1082 set sessionoptions-=options
1083 split
1084 mksession! Xtest_mks.out
1085 let found_save = 0
1086 let found_restore = 0
1087 let lines = readfile('Xtest_mks.out')
1088 for line in lines
1089 let line = trim(line)
1090
1091 if line ==# 'let s:shortmess_save = &shortmess'
1092 let found_save += 1
1093 endif
1094
1095 if found_save !=# 0 && line ==# 'let &shortmess = s:shortmess_save'
1096 let found_restore += 1
1097 endif
1098 endfor
1099 call assert_equal(1, found_save)
1100 call assert_equal(1, found_restore)
1101 call delete('Xtest_mks.out')
1102 close
1103 set sessionoptions&
1104
1105 " With options
1106 set sessionoptions+=options
1107 split
1108 mksession! Xtest_mks.out
1109 let found_restore = 0
1110 let lines = readfile('Xtest_mks.out')
1111 for line in lines
1112 if line =~# 's:shortmess_save'
1113 let found_restore += 1
1114 endif
1115 endfor
1116 call assert_equal(0, found_restore)
1117 call delete('Xtest_mks.out')
1118 close
1119 set sessionoptions&
1120endfunc
1121
Bram Moolenaaraaadb5b2022-05-18 22:07:47 +01001122" Test that when Vim loading session has 'A' in 'shortmess' it does not
1123" complain about an existing swapfile.
1124func Test_mksession_shortmess_with_A()
1125 edit Xtestfile
1126 write
1127 let fname = swapname('%')
1128 let cont = readblob(fname)
1129 set sessionoptions-=options
1130 mksession Xtestsession
1131 bwipe!
1132
1133 " Recreate the swap file to pretend the file is being edited
Bram Moolenaarb152b6a2022-09-29 21:37:33 +01001134 call writefile(cont, fname, 'D')
Bram Moolenaaraaadb5b2022-05-18 22:07:47 +01001135 set shortmess+=A
1136 source Xtestsession
1137
1138 set shortmess&
1139 set sessionoptions&
1140 call delete('Xtestsession')
Christian Brabandt84bc00e2023-07-13 11:45:54 +02001141 call delete('Xtestfile')
Bram Moolenaaraaadb5b2022-05-18 22:07:47 +01001142endfunc
1143
Bram Moolenaaree1a2b52019-09-29 14:23:39 +02001144" Test for mksession with 'compatible' option
1145func Test_mksession_compatible()
1146 mksession! Xtest_mks1.out
1147 set compatible
1148 mksession! Xtest_mks2.out
1149 set nocp
1150
1151 let test_success = v:false
1152 let lines = readfile('Xtest_mks1.out')
1153 for line in lines
1154 if line =~ '^if &cp | set nocp | endif'
1155 let test_success = v:true
1156 break
1157 endif
1158 endfor
1159 call assert_true(test_success)
1160
1161 let test_success = v:false
1162 let lines = readfile('Xtest_mks2.out')
1163 for line in lines
1164 if line =~ '^if !&cp | set cp | endif'
1165 let test_success = v:true
1166 break
1167 endif
1168 endfor
1169 call assert_true(test_success)
1170
1171 call delete('Xtest_mks1.out')
1172 call delete('Xtest_mks2.out')
1173 set compatible&
1174 set sessionoptions&
1175endfunc
1176
Bram Moolenaar87500262019-08-01 23:05:49 +02001177func s:ClearMappings()
1178 mapclear
1179 omapclear
1180 mapclear!
1181 lmapclear
1182 tmapclear
1183endfunc
1184
1185func Test_mkvimrc()
1186 let entries = [
1187 \ ['', 'nothing', '<Nop>'],
zeertzjq9ff1e592025-05-23 17:16:17 +02001188 \ ['n', 'normal', 'NORMAL<Up>'],
1189 \ ['v', 'visual', 'VISUAL<S-Down>'],
1190 \ ['s', 'select', 'SELECT<C-Left>'],
1191 \ ['x', 'visualonly', 'VISUALONLY<M-Right>'],
Bram Moolenaar87500262019-08-01 23:05:49 +02001192 \ ['o', 'operator', 'OPERATOR'],
1193 \ ['i', 'insert', 'INSERT'],
1194 \ ['l', 'lang', 'LANG'],
1195 \ ['c', 'command', 'COMMAND'],
1196 \ ['t', 'terminal', 'TERMINAL'],
1197 \ ]
1198 for entry in entries
1199 exe entry[0] .. 'map ' .. entry[1] .. ' ' .. entry[2]
1200 endfor
1201
1202 mkvimrc Xtestvimrc
1203
1204 call s:ClearMappings()
1205 for entry in entries
1206 call assert_equal('', maparg(entry[1], entry[0]))
1207 endfor
1208
1209 source Xtestvimrc
1210
1211 for entry in entries
1212 call assert_equal(entry[2], maparg(entry[1], entry[0]))
1213 endfor
1214
1215 call s:ClearMappings()
Yegappan Lakshmanan59585492021-06-12 13:46:41 +02001216
1217 " the 'pastetoggle', 'wildchar' and 'wildcharm' option values should be
1218 " stored as key names in the vimrc file
1219 set pastetoggle=<F5>
1220 set wildchar=<F6>
1221 set wildcharm=<F7>
1222 call assert_fails('mkvimrc Xtestvimrc')
1223 mkvimrc! Xtestvimrc
1224 call assert_notequal(-1, index(readfile('Xtestvimrc'), 'set pastetoggle=<F5>'))
1225 call assert_notequal(-1, index(readfile('Xtestvimrc'), 'set wildchar=<F6>'))
1226 call assert_notequal(-1, index(readfile('Xtestvimrc'), 'set wildcharm=<F7>'))
1227 set pastetoggle& wildchar& wildcharm&
1228
Bram Moolenaar87500262019-08-01 23:05:49 +02001229 call delete('Xtestvimrc')
1230endfunc
1231
Bram Moolenaar38890832020-11-01 17:40:54 +01001232func Test_scrolloff()
1233 set sessionoptions+=localoptions
1234 setlocal so=1 siso=1
1235 mksession! Xtest_mks.out
1236 setlocal so=-1 siso=-1
1237 source Xtest_mks.out
1238 call assert_equal(1, &l:so)
1239 call assert_equal(1, &l:siso)
1240 call delete('Xtest_mks.out')
1241 setlocal so& siso&
1242 set sessionoptions&
1243endfunc
1244
Bram Moolenaar3482be62020-11-27 11:00:38 +01001245func Test_altfile()
1246 edit Xone
1247 split Xtwo
1248 edit Xtwoalt
1249 edit #
1250 wincmd w
1251 edit Xonealt
1252 edit #
1253 mksession! Xtest_altfile
1254 only
1255 bwipe Xonealt
1256 bwipe Xtwoalt
1257 bwipe!
1258 source Xtest_altfile
1259 call assert_equal('Xone', bufname())
1260 call assert_equal('Xonealt', bufname('#'))
1261 wincmd w
1262 call assert_equal('Xtwo', bufname())
1263 call assert_equal('Xtwoalt', bufname('#'))
1264 only
1265 bwipe!
Bram Moolenaarac665c22020-12-08 20:39:15 +01001266 call delete('Xtest_altfile')
Bram Moolenaar3482be62020-11-27 11:00:38 +01001267endfunc
1268
Bram Moolenaar5c504f62021-04-01 13:39:51 +02001269" Test for creating views with manual folds
1270func Test_mkview_manual_fold()
Bram Moolenaarb152b6a2022-09-29 21:37:33 +01001271 call writefile(range(1,10), 'Xmkvfile', 'D')
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001272 new Xmkvfile
Bram Moolenaar5c504f62021-04-01 13:39:51 +02001273 " create recursive folds
1274 5,6fold
1275 4,7fold
1276 mkview Xview
1277 normal zE
1278 source Xview
1279 call assert_equal([-1, 4, 4, 4, 4, -1], [foldclosed(3), foldclosed(4),
1280 \ foldclosed(5), foldclosed(6), foldclosed(7), foldclosed(8)])
1281 " open one level of fold
1282 4foldopen
1283 mkview! Xview
1284 normal zE
1285 source Xview
1286 call assert_equal([-1, -1, 5, 5, -1, -1], [foldclosed(3), foldclosed(4),
1287 \ foldclosed(5), foldclosed(6), foldclosed(7), foldclosed(8)])
1288 " open all the folds
1289 %foldopen!
1290 mkview! Xview
1291 normal zE
1292 source Xview
1293 call assert_equal([-1, -1, -1, -1, -1, -1], [foldclosed(3), foldclosed(4),
1294 \ foldclosed(5), foldclosed(6), foldclosed(7), foldclosed(8)])
Bram Moolenaar5c504f62021-04-01 13:39:51 +02001295 call delete('Xview')
1296 bw!
1297endfunc
1298
Igor Lacerdacca5dea2025-04-17 20:41:31 +02001299" Test for handling invalid folds within views
1300func Test_mkview_ignore_invalid_folds()
1301 call writefile(range(1,10), 'Xmkvfile', 'D')
1302 new Xmkvfile
1303 " create some folds
1304 5,6fold
1305 4,7fold
1306 mkview Xview
1307 normal zE
1308 " delete lines to make folds invalid
1309 call deletebufline('', 6, '$')
1310 source Xview
1311 call assert_equal([-1, -1, -1, -1, -1, -1], [foldclosed(3), foldclosed(4),
1312 \ foldclosed(5), foldclosed(6), foldclosed(7), foldclosed(8)])
1313 call delete('Xview')
1314 bw!
1315endfunc
1316
Christian Brabandtb8b1c8e2023-06-23 22:23:01 +01001317" Test default 'viewdir' value
1318func Test_mkview_default_home()
1319 if has('win32')
Bram Moolenaar813b7a82023-06-23 22:56:47 +01001320 " use escape() to handle backslash path separators
1321 call assert_match('^' .. escape($ORIGHOME, '\') .. '/vimfiles', &viewdir)
Christian Brabandtb8b1c8e2023-06-23 22:23:01 +01001322 elseif has('unix')
John M Devin5b9237c2024-09-08 20:00:38 +02001323 call assert_match(
1324 \ '^' .. $ORIGHOME .. '/.vim\|' ..
1325 \ '^' .. $XDG_CONFIG_HOME .. '/vim'
1326 \ , &viewdir)
Christian Brabandtb8b1c8e2023-06-23 22:23:01 +01001327 elseif has('amiga')
1328 call assert_match('^home:vimfiles', &viewdir)
1329 elseif has('mac')
1330 call assert_match('^' .. $VIM .. '/vimfiles', &viewdir)
1331 endif
1332endfunc
1333
Bram Moolenaareca626f2016-12-01 18:47:38 +01001334" vim: shiftwidth=2 sts=2 expandtab