blob: 494ac873bbd9cb4febe7af66cb32e521734680d9 [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 +02006source check.vim
7CheckFeature mksession
Bram Moolenaareca626f2016-12-01 18:47:38 +01008
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01009source shared.vim
Bram Moolenaar7a39dd72019-06-23 00:50:15 +020010source term_util.vim
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011
Bram Moolenaar1e15e612019-11-09 17:18:52 +010012" Test for storing global and local argument list in a session file
13" This one must be done first.
14func Test__mksession_arglocal()
15 enew | only
16 n a b c
17 new
18 arglocal
19 mksession! Xtest_mks.out
20
21 %bwipe!
22 %argdelete
23 argglobal
24 source Xtest_mks.out
25 call assert_equal(2, winnr('$'))
26 call assert_equal(2, arglistid(1))
27 call assert_equal(0, arglistid(2))
28
29 %bwipe!
30 %argdelete
31 argglobal
32 call delete('Xtest_mks.out')
33endfunc
34
Bram Moolenaareca626f2016-12-01 18:47:38 +010035func Test_mksession()
36 tabnew
37 let wrap_save = &wrap
38 set sessionoptions=buffers splitbelow fileencoding=latin1
39 call setline(1, [
40 \ 'start:',
41 \ 'no multibyte chAracter',
42 \ ' one leaDing tab',
43 \ ' four leadinG spaces',
44 \ 'two consecutive tabs',
45 \ 'two tabs in one line',
46 \ 'one ä multibyteCharacter',
47 \ 'aä Ä two multiByte characters',
Bram Moolenaar92c1b692018-08-29 21:42:42 +020048 \ 'Aäöü three mulTibyte characters',
49 \ 'short line',
Bram Moolenaareca626f2016-12-01 18:47:38 +010050 \ ])
Bram Moolenaarc9b56b22017-01-29 14:14:09 +010051 let tmpfile = 'Xtemp'
Bram Moolenaareca626f2016-12-01 18:47:38 +010052 exec 'w! ' . tmpfile
53 /^start:
54 set wrap
55 vsplit
56 norm! j16|
57 split
58 norm! j16|
59 split
60 norm! j16|
61 split
62 norm! j8|
63 split
64 norm! j8|
65 split
66 norm! j16|
67 split
68 norm! j16|
69 split
70 norm! j16|
Bram Moolenaar92c1b692018-08-29 21:42:42 +020071 split
72 norm! j$
Bram Moolenaareca626f2016-12-01 18:47:38 +010073 wincmd l
74
75 set nowrap
76 /^start:
77 norm! j16|3zl
78 split
79 norm! j016|3zl
80 split
81 norm! j016|3zl
82 split
83 norm! j08|3zl
84 split
85 norm! j08|3zl
86 split
87 norm! j016|3zl
88 split
89 norm! j016|3zl
90 split
91 norm! j016|3zl
92 split
93 call wincol()
Bram Moolenaarc9b56b22017-01-29 14:14:09 +010094 mksession! Xtest_mks.out
Bram Moolenaar92c1b692018-08-29 21:42:42 +020095 let li = filter(readfile('Xtest_mks.out'), 'v:val =~# "\\(^ *normal! [0$]\\|^ *exe ''normal!\\)"')
Bram Moolenaareca626f2016-12-01 18:47:38 +010096 let expected = [
97 \ 'normal! 016|',
98 \ 'normal! 016|',
99 \ 'normal! 016|',
100 \ 'normal! 08|',
101 \ 'normal! 08|',
102 \ 'normal! 016|',
103 \ 'normal! 016|',
104 \ 'normal! 016|',
Bram Moolenaar92c1b692018-08-29 21:42:42 +0200105 \ 'normal! $',
Bram Moolenaareca626f2016-12-01 18:47:38 +0100106 \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
107 \ " normal! 016|",
108 \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
109 \ " normal! 016|",
110 \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
111 \ " normal! 016|",
112 \ " exe 'normal! ' . s:c . '|zs' . 8 . '|'",
113 \ " normal! 08|",
114 \ " exe 'normal! ' . s:c . '|zs' . 8 . '|'",
115 \ " normal! 08|",
116 \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
117 \ " normal! 016|",
118 \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
119 \ " normal! 016|",
120 \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
121 \ " normal! 016|",
122 \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
123 \ " normal! 016|"
124 \ ]
125 call assert_equal(expected, li)
126 tabclose!
127
Bram Moolenaarc9b56b22017-01-29 14:14:09 +0100128 call delete('Xtest_mks.out')
Bram Moolenaareca626f2016-12-01 18:47:38 +0100129 call delete(tmpfile)
130 let &wrap = wrap_save
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100131 set sessionoptions&
Bram Moolenaareca626f2016-12-01 18:47:38 +0100132endfunc
133
Bram Moolenaar635bd602021-04-16 19:58:22 +0200134def Test_mksession_skiprtp()
135 mksession! Xtest_mks.out
Bram Moolenaard23b7142021-04-17 21:04:34 +0200136 var found_rtp = 0
137 var found_pp = 0
Bram Moolenaar635bd602021-04-16 19:58:22 +0200138 for line in readfile('Xtest_mks.out')
139 if line =~ 'set runtimepath'
Bram Moolenaard23b7142021-04-17 21:04:34 +0200140 found_rtp += 1
141 endif
142 if line =~ 'set packpath'
143 found_pp += 1
Bram Moolenaar635bd602021-04-16 19:58:22 +0200144 endif
145 endfor
Bram Moolenaard23b7142021-04-17 21:04:34 +0200146 assert_equal(1, found_rtp)
147 assert_equal(1, found_pp)
Bram Moolenaar635bd602021-04-16 19:58:22 +0200148 delete('Xtest_mks.out')
149
150 set sessionoptions+=skiprtp
151 mksession! Xtest_mks.out
Bram Moolenaard23b7142021-04-17 21:04:34 +0200152 var found = 0
Bram Moolenaar635bd602021-04-16 19:58:22 +0200153 for line in readfile('Xtest_mks.out')
Bram Moolenaard23b7142021-04-17 21:04:34 +0200154 if line =~ 'set \(runtimepath\|packpath\)'
Bram Moolenaar635bd602021-04-16 19:58:22 +0200155 found = 1
156 break
157 endif
158 endfor
159 assert_equal(0, found)
160 delete('Xtest_mks.out')
161 set sessionoptions&
162enddef
163
Bram Moolenaar36ae89c2017-01-28 17:11:14 +0100164func Test_mksession_winheight()
165 new
Bram Moolenaar1c3c1042018-06-12 16:49:30 +0200166 set winheight=10
167 set winminheight=2
Bram Moolenaarc9b56b22017-01-29 14:14:09 +0100168 mksession! Xtest_mks.out
169 source Xtest_mks.out
Bram Moolenaar36ae89c2017-01-28 17:11:14 +0100170
Bram Moolenaarc9b56b22017-01-29 14:14:09 +0100171 call delete('Xtest_mks.out')
Bram Moolenaar36ae89c2017-01-28 17:11:14 +0100172endfunc
173
Bram Moolenaar1c3c1042018-06-12 16:49:30 +0200174func Test_mksession_large_winheight()
175 set winheight=999
176 mksession! Xtest_mks_winheight.out
177 set winheight&
178 source Xtest_mks_winheight.out
179 call delete('Xtest_mks_winheight.out')
180endfunc
181
Bram Moolenaarb6c2e9a2021-04-30 21:37:51 +0200182func Test_mksession_zero_winheight()
183 set winminheight=0
184 edit SomeFile
185 split
186 wincmd _
187 mksession! Xtest_mks_zero
188 set winminheight&
189 let text = readfile('Xtest_mks_zero')->join()
Dominique Pelle5f8ed742021-05-03 19:08:37 +0200190 call delete('Xtest_mks_zero')
Bram Moolenaarb6c2e9a2021-04-30 21:37:51 +0200191 close
Dominique Pelle5f8ed742021-05-03 19:08:37 +0200192 " check there is no divide by zero
Bram Moolenaarb6c2e9a2021-04-30 21:37:51 +0200193 call assert_notmatch('/ 0[^0-9]', text)
194endfunc
195
Bram Moolenaared18f2c2019-01-24 20:30:52 +0100196func Test_mksession_rtp()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200197 " TODO: fix problem with backslashes on Win32
198 CheckNotMSWindows
199
Bram Moolenaared18f2c2019-01-24 20:30:52 +0100200 new
201 let _rtp=&rtp
202 " Make a real long (invalid) runtimepath value,
203 " that should exceed PATH_MAX (hopefully)
204 let newrtp=&rtp.',~'.repeat('/foobar', 1000)
205 let newrtp.=",".expand("$HOME")."/.vim"
206 let &rtp=newrtp
207
208 " determine expected value
209 let expected=split(&rtp, ',')
210 let expected = map(expected, '"set runtimepath+=".v:val')
211 let expected = ['set runtimepath='] + expected
212 let expected = map(expected, {v,w -> substitute(w, $HOME, "~", "g")})
213
214 mksession! Xtest_mks.out
215 let &rtp=_rtp
216 let li = filter(readfile('Xtest_mks.out'), 'v:val =~# "runtimepath"')
217 call assert_equal(expected, li)
218
219 call delete('Xtest_mks.out')
220endfunc
221
Bram Moolenaar79da5632017-02-01 22:52:44 +0100222func Test_mksession_arglist()
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200223 %argdel
Bram Moolenaar79da5632017-02-01 22:52:44 +0100224 next file1 file2 file3 file4
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200225 new
226 next | next
Bram Moolenaar79da5632017-02-01 22:52:44 +0100227 mksession! Xtest_mks.out
228 source Xtest_mks.out
229 call assert_equal(['file1', 'file2', 'file3', 'file4'], argv())
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200230 call assert_equal(2, argidx())
231 wincmd w
232 call assert_equal(0, argidx())
Bram Moolenaar79da5632017-02-01 22:52:44 +0100233
234 call delete('Xtest_mks.out')
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200235 enew | only
Bram Moolenaar79da5632017-02-01 22:52:44 +0100236 argdel *
237endfunc
238
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200239func Test_mksession_one_buffer_two_windows()
240 edit Xtest1
241 new Xtest2
242 split
243 mksession! Xtest_mks.out
244 let lines = readfile('Xtest_mks.out')
245 let count1 = 0
246 let count2 = 0
247 let count2buf = 0
248 for line in lines
249 if line =~ 'edit \f*Xtest1$'
250 let count1 += 1
251 endif
252 if line =~ 'edit \f\{-}Xtest2'
253 let count2 += 1
254 endif
255 if line =~ 'buffer \f\{-}Xtest2'
256 let count2buf += 1
257 endif
258 endfor
259 call assert_equal(1, count1, 'Xtest1 count')
260 call assert_equal(2, count2, 'Xtest2 count')
261 call assert_equal(2, count2buf, 'Xtest2 buffer count')
262
263 close
264 bwipe!
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200265 call delete('Xtest_mks.out')
266endfunc
267
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200268func Test_mksession_lcd_multiple_tabs()
269 tabnew
270 tabnew
Bram Moolenaar81e2ac72018-07-04 22:44:08 +0200271 lcd .
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200272 tabfirst
Bram Moolenaar81e2ac72018-07-04 22:44:08 +0200273 lcd .
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200274 mksession! Xtest_mks.out
275 tabonly
276 source Xtest_mks.out
277 call assert_true(haslocaldir(), 'Tab 1 localdir')
278 tabnext 2
279 call assert_true(!haslocaldir(), 'Tab 2 localdir')
280 tabnext 3
281 call assert_true(haslocaldir(), 'Tab 3 localdir')
282 call delete('Xtest_mks.out')
283endfunc
284
Bram Moolenaar00aa0692019-04-27 20:37:57 +0200285" Test for tabpage-local directory
286func Test_mksession_tcd_multiple_tabs()
287 let save_cwd = getcwd()
288 call mkdir('Xtopdir')
289 cd Xtopdir
290 call mkdir('Xtabdir1')
291 call mkdir('Xtabdir2')
292 call mkdir('Xtabdir3')
293 call mkdir('Xwindir1')
294 call mkdir('Xwindir2')
295 call mkdir('Xwindir3')
296 tcd Xtabdir1
297 botright new
298 wincmd t
299 lcd ../Xwindir1
300 tabnew
301 tcd ../Xtabdir2
302 botright new
303 lcd ../Xwindir2
304 tabnew
305 tcd ../Xtabdir3
306 botright new
307 lcd ../Xwindir3
308 tabfirst
309 1wincmd w
310 mksession! Xtest_mks.out
311 only | tabonly
312 source Xtest_mks.out
313 call assert_equal('Xtabdir1', fnamemodify(getcwd(-1, 1), ':t'))
314 call assert_equal('Xwindir1', fnamemodify(getcwd(1, 1), ':t'))
315 call assert_equal('Xtabdir1', fnamemodify(getcwd(2, 1), ':t'))
316 call assert_equal('Xtabdir2', fnamemodify(getcwd(-1, 2), ':t'))
317 call assert_equal('Xtabdir2', fnamemodify(getcwd(1, 2), ':t'))
318 call assert_equal('Xwindir2', fnamemodify(getcwd(2, 2), ':t'))
319 call assert_equal('Xtabdir3', fnamemodify(getcwd(-1, 3), ':t'))
320 call assert_equal('Xtabdir3', fnamemodify(getcwd(1, 3), ':t'))
321 call assert_equal('Xwindir3', fnamemodify(getcwd(2, 3), ':t'))
Bram Moolenaar5bf46e92019-11-09 18:06:04 +0100322 %bwipe
323 call chdir(save_cwd)
Bram Moolenaar00aa0692019-04-27 20:37:57 +0200324 call delete("Xtopdir", "rf")
325endfunc
326
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200327func Test_mksession_blank_tabs()
328 tabnew
329 tabnew
330 tabnew
331 tabnext 3
332 mksession! Xtest_mks.out
333 tabnew
334 tabnew
335 tabnext 2
336 source Xtest_mks.out
337 call assert_equal(4, tabpagenr('$'), 'session restore should restore number of tabs')
338 call assert_equal(3, tabpagenr(), 'session restore should restore the active tab')
339 call delete('Xtest_mks.out')
340endfunc
341
Bram Moolenaard39e2752019-01-26 20:07:38 +0100342func Test_mksession_buffer_count()
343 set hidden
344
345 " Edit exactly three files in the current session.
346 %bwipe!
347 e Xfoo | tabe Xbar | tabe Xbaz
348 tabdo write
349 mksession! Xtest_mks.out
350
351 " Verify that loading the session does not create additional buffers.
352 %bwipe!
353 source Xtest_mks.out
354 call assert_equal(3, len(getbufinfo()))
355
356 " Clean up.
357 call delete('Xfoo')
358 call delete('Xbar')
359 call delete('Xbaz')
360 call delete('Xtest_mks.out')
361 %bwipe!
362 set hidden&
363endfunc
364
Evgeni Chasnovski26ebf1f2022-01-14 13:19:43 +0000365func Test_mksession_buffer_order()
366 %bwipe!
367 e Xfoo | e Xbar | e Xbaz | e Xqux
368 bufdo write
369 mksession! Xtest_mks.out
370
371 " Verify that loading the session preserves order of buffers
372 %bwipe!
373 source Xtest_mks.out
374
375 let s:buf_info = getbufinfo()
376 call assert_true(s:buf_info[0]['name'] =~# 'Xfoo$')
377 call assert_true(s:buf_info[1]['name'] =~# 'Xbar$')
378 call assert_true(s:buf_info[2]['name'] =~# 'Xbaz$')
379 call assert_true(s:buf_info[3]['name'] =~# 'Xqux$')
380
381 " Clean up.
382 call delete('Xfoo')
383 call delete('Xbar')
384 call delete('Xbaz')
385 call delete('Xqux')
386 call delete('Xtest_mks.out')
387 %bwipe!
388endfunc
389
Bram Moolenaare3c74d22019-01-12 16:29:30 +0100390if has('extra_search')
391
392func Test_mksession_hlsearch()
393 set hlsearch
394 mksession! Xtest_mks.out
395 nohlsearch
396 source Xtest_mks.out
397 call assert_equal(1, v:hlsearch, 'session should restore search highlighting state')
398 nohlsearch
399 mksession! Xtest_mks.out
400 source Xtest_mks.out
401 call assert_equal(0, v:hlsearch, 'session should restore search highlighting state')
402 call delete('Xtest_mks.out')
403endfunc
404
405endif
406
407
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200408func Test_mksession_blank_windows()
409 split
410 split
411 split
412 3 wincmd w
413 mksession! Xtest_mks.out
414 split
415 split
416 2 wincmd w
417 source Xtest_mks.out
418 call assert_equal(4, winnr('$'), 'session restore should restore number of windows')
419 call assert_equal(3, winnr(), 'session restore should restore the active window')
420 call delete('Xtest_mks.out')
421endfunc
422
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100423func Test_mksession_terminal_shell()
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200424 CheckFeature terminal
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100425 CheckFeature quickfix
426
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100427 terminal
428 mksession! Xtest_mks.out
429 let lines = readfile('Xtest_mks.out')
430 let term_cmd = ''
431 for line in lines
432 if line =~ '^terminal'
433 let term_cmd = line
434 elseif line =~ 'badd.*' . &shell
435 call assert_report('unexpected shell line: ' . line)
436 endif
437 endfor
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100438 call assert_match('terminal ++curwin ++cols=\d\+ ++rows=\d\+\s*.*$', term_cmd)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100439
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200440 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100441 call delete('Xtest_mks.out')
442endfunc
443
444func Test_mksession_terminal_no_restore_cmdarg()
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200445 CheckFeature terminal
446
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100447 terminal ++norestore
448 mksession! Xtest_mks.out
449 let lines = readfile('Xtest_mks.out')
450 let term_cmd = ''
451 for line in lines
452 if line =~ '^terminal'
Bram Moolenaarac665c22020-12-08 20:39:15 +0100453 call assert_report('session must not restore terminal')
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100454 endif
455 endfor
456
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200457 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100458 call delete('Xtest_mks.out')
459endfunc
460
461func Test_mksession_terminal_no_restore_funcarg()
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200462 CheckFeature terminal
463
Bram Moolenaar3a6aadb2021-04-04 15:28:59 +0200464 let buf = Run_shell_in_terminal({'norestore': 1})
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100465 mksession! Xtest_mks.out
466 let lines = readfile('Xtest_mks.out')
467 let term_cmd = ''
468 for line in lines
469 if line =~ '^terminal'
Bram Moolenaarac665c22020-12-08 20:39:15 +0100470 call assert_report('session must not restore terminal')
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100471 endif
472 endfor
473
Bram Moolenaar3a6aadb2021-04-04 15:28:59 +0200474 call StopShellInTerminal(buf)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100475 call delete('Xtest_mks.out')
476endfunc
477
478func Test_mksession_terminal_no_restore_func()
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200479 CheckFeature terminal
480
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100481 terminal
482 call term_setrestore(bufnr('%'), 'NONE')
483 mksession! Xtest_mks.out
484 let lines = readfile('Xtest_mks.out')
485 let term_cmd = ''
486 for line in lines
487 if line =~ '^terminal'
Bram Moolenaarac665c22020-12-08 20:39:15 +0100488 call assert_report('session must not restore terminal')
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100489 endif
490 endfor
491
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200492 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100493 call delete('Xtest_mks.out')
494endfunc
495
496func Test_mksession_terminal_no_ssop()
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200497 CheckFeature terminal
498
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100499 terminal
500 set sessionoptions-=terminal
501 mksession! Xtest_mks.out
502 let lines = readfile('Xtest_mks.out')
503 let term_cmd = ''
504 for line in lines
505 if line =~ '^terminal'
Bram Moolenaarac665c22020-12-08 20:39:15 +0100506 call assert_report('session must not restore terminal')
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100507 endif
508 endfor
509
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200510 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100511 call delete('Xtest_mks.out')
512 set sessionoptions&
513endfunc
514
515func Test_mksession_terminal_restore_other()
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200516 CheckFeature terminal
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100517 CheckFeature quickfix
518
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100519 terminal
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200520 eval bufnr('%')->term_setrestore('other')
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100521 mksession! Xtest_mks.out
522 let lines = readfile('Xtest_mks.out')
523 let term_cmd = ''
524 for line in lines
525 if line =~ '^terminal'
526 let term_cmd = line
527 endif
528 endfor
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100529 call assert_match('terminal ++curwin ++cols=\d\+ ++rows=\d\+.*other', term_cmd)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100530
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200531 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100532 call delete('Xtest_mks.out')
533endfunc
534
Bram Moolenaar0e655112020-09-11 20:36:36 +0200535func Test_mksession_terminal_shared_windows()
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200536 CheckFeature terminal
537
Bram Moolenaar0e655112020-09-11 20:36:36 +0200538 terminal
539 let term_buf = bufnr()
540 new
541 execute "buffer" term_buf
542 mksession! Xtest_mks.out
543
544 let lines = readfile('Xtest_mks.out')
545 let found_creation = 0
546 let found_use = 0
547
548 for line in lines
549 if line =~ '^terminal'
550 let found_creation = 1
551 call assert_match('terminal ++curwin ++cols=\d\+ ++rows=\d\+', line)
552 elseif line =~ "^execute 'buffer ' . s:term_buf_" . term_buf . "$"
553 let found_use = 1
554 endif
555 endfor
556
557 call assert_true(found_creation && found_use)
558
559 call StopShellInTerminal(term_buf)
560 call delete('Xtest_mks.out')
561endfunc
562
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200563func Test_mkview_terminal_windows()
564 CheckFeature terminal
565
566 " create two window on the same terminal to check this is handled OK
567 terminal
568 let term_buf = bufnr()
569 exe 'sbuf ' .. term_buf
570 mkview! Xtestview
571
572 call StopShellInTerminal(term_buf)
573 call delete('Xtestview')
574endfunc
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200575
Bram Moolenaarf9547eb2021-02-01 19:24:55 +0100576func Test_mkview_open_folds()
577 enew!
578
579 call append(0, ['a', 'b', 'c'])
580 1,3fold
581 " zR affects 'foldlevel', make sure the option is applied after the folds
582 " have been recreated.
583 normal zR
584 write! Xtestfile
585
586 call assert_equal(-1, foldclosed(1))
587 call assert_equal(-1, foldclosed(2))
588 call assert_equal(-1, foldclosed(3))
589
590 mkview! Xtestview
591 source Xtestview
592
593 call assert_equal(-1, foldclosed(1))
594 call assert_equal(-1, foldclosed(2))
595 call assert_equal(-1, foldclosed(3))
596
597 call delete('Xtestview')
598 call delete('Xtestfile')
599 %bwipe
600endfunc
601
Bram Moolenaar139348f2021-02-05 21:55:53 +0100602func Test_mkview_no_balt()
603 edit Xtestfile1
604 edit Xtestfile2
605
606 mkview! Xtestview
607 bdelete Xtestfile1
608
609 source Xtestview
610 call assert_equal(0, buflisted('Xtestfile1'))
611
612 call delete('Xtestview')
613 %bwipe
614endfunc
615
Bram Moolenaar0756f752021-03-13 13:52:33 +0100616func Test_mksession_no_balt()
617 edit Xtestfile1
618 edit Xtestfile2
619
620 bdelete Xtestfile1
621 mksession! Xtestview
622
623 source Xtestview
624 call assert_equal(0, buflisted('Xtestfile1'))
625
626 call delete('Xtestview')
627 %bwipe
628endfunc
629
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200630" Test :mkview with a file argument.
631func Test_mkview_file()
632 " Create a view with line number and a fold.
633 help :mkview
634 set number
Bram Moolenaar2bf4fe02018-08-30 14:04:25 +0200635 norm! V}zf0
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200636 let pos = getpos('.')
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200637 let linefoldclosed1 = foldclosed('.')
638 mkview! Xview
639 set nonumber
640 norm! zrj
641 " We can close the help window, as mkview with a file name should
642 " generate a command to edit the file.
643 helpclose
644
645 source Xview
646 call assert_equal(1, &number)
647 call assert_match('\*:mkview\*$', getline('.'))
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200648 call assert_equal(pos, getpos('.'))
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200649 call assert_equal(linefoldclosed1, foldclosed('.'))
650
651 " Creating a view again with the same file name should fail (file
652 " already exists). But with a !, the previous view should be
653 " overwritten without error.
654 help :loadview
655 call assert_fails('mkview Xview', 'E189:')
656 call assert_match('\*:loadview\*$', getline('.'))
657 mkview! Xview
658 call assert_match('\*:loadview\*$', getline('.'))
659
660 call delete('Xview')
661 bwipe
662endfunc
663
664" Test :mkview and :loadview with a custom 'viewdir'.
665func Test_mkview_loadview_with_viewdir()
666 set viewdir=Xviewdir
667
668 help :mkview
669 set number
670 norm! V}zf
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200671 let pos = getpos('.')
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200672 let linefoldclosed1 = foldclosed('.')
673 mkview 1
674 set nonumber
675 norm! zrj
676
677 loadview 1
678
679 " The directory Xviewdir/ should have been created and the view
680 " should be stored in that directory.
681 call assert_equal('Xviewdir/' .
682 \ substitute(
683 \ substitute(
684 \ expand('%:p'), '/', '=+', 'g'), ':', '=-', 'g') . '=1.vim',
685 \ glob('Xviewdir/*'))
686 call assert_equal(1, &number)
687 call assert_match('\*:mkview\*$', getline('.'))
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200688 call assert_equal(pos, getpos('.'))
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200689 call assert_equal(linefoldclosed1, foldclosed('.'))
690
691 call delete('Xviewdir', 'rf')
692 set viewdir&
693 helpclose
694endfunc
695
696func Test_mkview_no_file_name()
697 new
698 " :mkview or :mkview {nr} should fail in a unnamed buffer.
699 call assert_fails('mkview', 'E32:')
700 call assert_fails('mkview 1', 'E32:')
701
702 " :mkview {file} should succeed in a unnamed buffer.
703 mkview Xview
704 help
705 source Xview
706 call assert_equal('', bufname('%'))
707
708 call delete('Xview')
709 %bwipe
710endfunc
Bram Moolenaar79da5632017-02-01 22:52:44 +0100711
Bram Moolenaar3482be62020-11-27 11:00:38 +0100712func Test_mkview_loadview_jumplist()
713 set viewdir=Xviewdir
714 au BufWinLeave * silent mkview
715 au BufWinEnter * silent loadview
716
717 edit Xfile1
718 call setline(1, ['a', 'bbbbbbb', 'c'])
719 normal j3l
720 call assert_equal([2, 4], getcurpos()[1:2])
721 write
722
723 edit Xfile2
724 call setline(1, ['d', 'eeeeeee', 'f'])
725 normal j5l
726 call assert_equal([2, 6], getcurpos()[1:2])
727 write
728
729 edit Xfile3
730 call setline(1, ['g', 'h', 'iiiii'])
731 normal jj3l
732 call assert_equal([3, 4], getcurpos()[1:2])
733 write
734
735 edit Xfile1
736 call assert_equal([2, 4], getcurpos()[1:2])
737 edit Xfile2
738 call assert_equal([2, 6], getcurpos()[1:2])
739 edit Xfile3
740 call assert_equal([3, 4], getcurpos()[1:2])
741
742 exe "normal \<C-O>"
743 call assert_equal('Xfile2', expand('%'))
744 call assert_equal([2, 6], getcurpos()[1:2])
745 exe "normal \<C-O>"
746 call assert_equal('Xfile1', expand('%'))
747 call assert_equal([2, 4], getcurpos()[1:2])
748
749 au! BufWinLeave
750 au! BufWinEnter
751 bwipe!
752 call delete('Xviewdir', 'rf')
753 call delete('Xfile1')
754 call delete('Xfile2')
755 call delete('Xfile3')
756 set viewdir&
757endfunc
758
Bram Moolenaar555de4e2019-01-21 23:03:49 +0100759" A clean session (one empty buffer, one window, and one tab) should not
760" set any error messages when sourced because no commands should fail.
761func Test_mksession_no_errmsg()
762 let v:errmsg = ''
763 %bwipe!
764 mksession! Xtest_mks.out
765 source Xtest_mks.out
766 call assert_equal('', v:errmsg)
767 call delete('Xtest_mks.out')
768endfunc
769
Bram Moolenaarad36a352019-01-24 13:34:42 +0100770func Test_mksession_quote_in_filename()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200771 " only Unix can handle this weird filename
772 CheckUnix
773
Bram Moolenaarad36a352019-01-24 13:34:42 +0100774 let v:errmsg = ''
775 %bwipe!
776 split another
777 split x'y\"z
778 mksession! Xtest_mks_quoted.out
779 %bwipe!
780 source Xtest_mks_quoted.out
781 call assert_true(bufexists("x'y\"z"))
782
783 %bwipe!
784 call delete('Xtest_mks_quoted.out')
785endfunc
786
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200787" Test for storing global variables in a session file
788func Test_mksession_globals()
789 set sessionoptions+=globals
790
791 " create different global variables
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200792 let g:Global_string = "Sun is shining\r\n"
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200793 let g:Global_count = 100
794 let g:Global_pi = 3.14
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200795 let g:Global_neg_float = -2.68
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200796
797 mksession! Xtest_mks.out
798
799 unlet g:Global_string
800 unlet g:Global_count
801 unlet g:Global_pi
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200802 unlet g:Global_neg_float
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200803
804 source Xtest_mks.out
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200805 call assert_equal("Sun is shining\r\n", g:Global_string)
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200806 call assert_equal(100, g:Global_count)
807 call assert_equal(3.14, g:Global_pi)
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200808 call assert_equal(-2.68, g:Global_neg_float)
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200809
810 unlet g:Global_string
811 unlet g:Global_count
812 unlet g:Global_pi
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200813 unlet g:Global_neg_float
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200814 call delete('Xtest_mks.out')
815 set sessionoptions&
816endfunc
817
818" Test for changing backslash to forward slash in filenames
819func Test_mksession_slash()
Bram Moolenaar37ac4a52019-09-28 19:29:04 +0200820 if exists('+shellslash')
821 throw 'Skipped: cannot use backslash in file name'
822 endif
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200823 enew
824 %bwipe!
825 e a\\b\\c
826 mksession! Xtest_mks1.out
827 set sessionoptions+=slash
828 mksession! Xtest_mks2.out
829
830 %bwipe!
831 source Xtest_mks1.out
832 call assert_equal('a\b\c', bufname(''))
833 %bwipe!
834 source Xtest_mks2.out
835 call assert_equal('a/b/c', bufname(''))
836
837 %bwipe!
838 call delete('Xtest_mks1.out')
839 call delete('Xtest_mks2.out')
840 set sessionoptions&
841endfunc
842
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200843" Test for changing directory to the session file directory
844func Test_mksession_sesdir()
Bram Moolenaar5bf46e92019-11-09 18:06:04 +0100845 let save_cwd = getcwd()
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200846 call mkdir('Xproj')
847 mksession! Xproj/Xtest_mks1.out
848 set sessionoptions-=curdir
849 set sessionoptions+=sesdir
850 mksession! Xproj/Xtest_mks2.out
851
852 source Xproj/Xtest_mks1.out
853 call assert_equal('testdir', fnamemodify(getcwd(), ':t'))
854 source Xproj/Xtest_mks2.out
855 call assert_equal('Xproj', fnamemodify(getcwd(), ':t'))
Bram Moolenaar5bf46e92019-11-09 18:06:04 +0100856 call chdir(save_cwd)
857 %bwipe
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200858
859 set sessionoptions&
860 call delete('Xproj', 'rf')
861endfunc
862
863" Test for storing the 'lines' and 'columns' settings
864func Test_mksession_resize()
865 mksession! Xtest_mks1.out
866 set sessionoptions+=resize
867 mksession! Xtest_mks2.out
868
869 let lines = readfile('Xtest_mks1.out')
870 let found_resize = v:false
871 for line in lines
872 if line =~ '^set lines='
873 let found_resize = v:true
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200874 break
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200875 endif
876 endfor
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200877 call assert_false(found_resize)
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200878 let lines = readfile('Xtest_mks2.out')
879 let found_resize = v:false
880 for line in lines
881 if line =~ '^set lines='
882 let found_resize = v:true
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200883 break
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200884 endif
885 endfor
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200886 call assert_true(found_resize)
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200887
888 call delete('Xtest_mks1.out')
889 call delete('Xtest_mks2.out')
890 set sessionoptions&
891endfunc
892
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200893" Test for mksession with a named scratch buffer
894func Test_mksession_scratch()
895 enew | only
896 file Xscratch
897 set buftype=nofile
898 mksession! Xtest_mks.out
899 %bwipe
900 source Xtest_mks.out
901 call assert_equal('Xscratch', bufname(''))
902 call assert_equal('nofile', &buftype)
903 %bwipe
904 call delete('Xtest_mks.out')
905endfunc
906
907" Test for mksession with fold options
908func Test_mksession_foldopt()
909 set sessionoptions-=options
910 set sessionoptions+=folds
911 new
912 setlocal foldenable
913 setlocal foldmethod=expr
914 setlocal foldmarker=<<<,>>>
915 setlocal foldignore=%
916 setlocal foldlevel=2
917 setlocal foldminlines=10
918 setlocal foldnestmax=15
919 mksession! Xtest_mks.out
920 close
921 %bwipe
922
923 source Xtest_mks.out
924 call assert_true(&foldenable)
925 call assert_equal('expr', &foldmethod)
926 call assert_equal('<<<,>>>', &foldmarker)
927 call assert_equal('%', &foldignore)
928 call assert_equal(2, &foldlevel)
929 call assert_equal(10, &foldminlines)
930 call assert_equal(15, &foldnestmax)
931
932 close
933 %bwipe
934 set sessionoptions&
935endfunc
936
matveyt8e7d9db2022-01-05 14:01:30 +0000937" Test for mksession with "help" but not "options" in 'sessionoptions'
938func Test_mksession_help_noopt()
939 set sessionoptions-=options
940 set sessionoptions+=help
941 help
942 let fname = expand('%')
943 mksession! Xtest_mks.out
944 bwipe
945
946 source Xtest_mks.out
947 call assert_equal('help', &buftype)
948 call assert_equal('help', &filetype)
949 call assert_equal(fname, expand('%'))
950 call assert_false(&modifiable)
951 call assert_true(&readonly)
952
953 helpclose
954 help index
955 let fname = expand('%')
956 mksession! Xtest_mks.out
957 bwipe
958
959 source Xtest_mks.out
960 call assert_equal('help', &buftype)
961 call assert_equal(fname, expand('%'))
962
963 call delete('Xtest_mks.out')
964 set sessionoptions&
965endfunc
966
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200967" Test for mksession with window position
968func Test_mksession_winpos()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200969 " Only applicable in GUI Vim
970 CheckGui
971
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200972 set sessionoptions+=winpos
973 mksession! Xtest_mks.out
974 let found_winpos = v:false
975 let lines = readfile('Xtest_mks.out')
976 for line in lines
977 if line =~ '^winpos '
978 let found_winpos = v:true
979 break
980 endif
981 endfor
982 call assert_true(found_winpos)
983 call delete('Xtest_mks.out')
984 set sessionoptions&
985endfunc
986
Bram Moolenaar0995c812021-04-17 18:38:54 +0200987" Test for mksession without options restores winminheight
988func Test_mksession_winminheight()
989 set sessionoptions-=options
990 split
991 mksession! Xtest_mks.out
992 let found_restore = 0
993 let lines = readfile('Xtest_mks.out')
994 for line in lines
995 if line =~ '= s:save_winmin\(width\|height\)'
996 let found_restore += 1
997 endif
998 endfor
999 call assert_equal(2, found_restore)
1000 call delete('Xtest_mks.out')
1001 close
1002 set sessionoptions&
1003endfunc
1004
Bram Moolenaaree1a2b52019-09-29 14:23:39 +02001005" Test for mksession with 'compatible' option
1006func Test_mksession_compatible()
1007 mksession! Xtest_mks1.out
1008 set compatible
1009 mksession! Xtest_mks2.out
1010 set nocp
1011
1012 let test_success = v:false
1013 let lines = readfile('Xtest_mks1.out')
1014 for line in lines
1015 if line =~ '^if &cp | set nocp | endif'
1016 let test_success = v:true
1017 break
1018 endif
1019 endfor
1020 call assert_true(test_success)
1021
1022 let test_success = v:false
1023 let lines = readfile('Xtest_mks2.out')
1024 for line in lines
1025 if line =~ '^if !&cp | set cp | endif'
1026 let test_success = v:true
1027 break
1028 endif
1029 endfor
1030 call assert_true(test_success)
1031
1032 call delete('Xtest_mks1.out')
1033 call delete('Xtest_mks2.out')
1034 set compatible&
1035 set sessionoptions&
1036endfunc
1037
Bram Moolenaar87500262019-08-01 23:05:49 +02001038func s:ClearMappings()
1039 mapclear
1040 omapclear
1041 mapclear!
1042 lmapclear
1043 tmapclear
1044endfunc
1045
1046func Test_mkvimrc()
1047 let entries = [
1048 \ ['', 'nothing', '<Nop>'],
1049 \ ['n', 'normal', 'NORMAL'],
1050 \ ['v', 'visual', 'VISUAL'],
1051 \ ['s', 'select', 'SELECT'],
1052 \ ['x', 'visualonly', 'VISUALONLY'],
1053 \ ['o', 'operator', 'OPERATOR'],
1054 \ ['i', 'insert', 'INSERT'],
1055 \ ['l', 'lang', 'LANG'],
1056 \ ['c', 'command', 'COMMAND'],
1057 \ ['t', 'terminal', 'TERMINAL'],
1058 \ ]
1059 for entry in entries
1060 exe entry[0] .. 'map ' .. entry[1] .. ' ' .. entry[2]
1061 endfor
1062
1063 mkvimrc Xtestvimrc
1064
1065 call s:ClearMappings()
1066 for entry in entries
1067 call assert_equal('', maparg(entry[1], entry[0]))
1068 endfor
1069
1070 source Xtestvimrc
1071
1072 for entry in entries
1073 call assert_equal(entry[2], maparg(entry[1], entry[0]))
1074 endfor
1075
1076 call s:ClearMappings()
Yegappan Lakshmanan59585492021-06-12 13:46:41 +02001077
1078 " the 'pastetoggle', 'wildchar' and 'wildcharm' option values should be
1079 " stored as key names in the vimrc file
1080 set pastetoggle=<F5>
1081 set wildchar=<F6>
1082 set wildcharm=<F7>
1083 call assert_fails('mkvimrc Xtestvimrc')
1084 mkvimrc! Xtestvimrc
1085 call assert_notequal(-1, index(readfile('Xtestvimrc'), 'set pastetoggle=<F5>'))
1086 call assert_notequal(-1, index(readfile('Xtestvimrc'), 'set wildchar=<F6>'))
1087 call assert_notequal(-1, index(readfile('Xtestvimrc'), 'set wildcharm=<F7>'))
1088 set pastetoggle& wildchar& wildcharm&
1089
Bram Moolenaar87500262019-08-01 23:05:49 +02001090 call delete('Xtestvimrc')
1091endfunc
1092
Bram Moolenaar38890832020-11-01 17:40:54 +01001093func Test_scrolloff()
1094 set sessionoptions+=localoptions
1095 setlocal so=1 siso=1
1096 mksession! Xtest_mks.out
1097 setlocal so=-1 siso=-1
1098 source Xtest_mks.out
1099 call assert_equal(1, &l:so)
1100 call assert_equal(1, &l:siso)
1101 call delete('Xtest_mks.out')
1102 setlocal so& siso&
1103 set sessionoptions&
1104endfunc
1105
Bram Moolenaar3482be62020-11-27 11:00:38 +01001106func Test_altfile()
1107 edit Xone
1108 split Xtwo
1109 edit Xtwoalt
1110 edit #
1111 wincmd w
1112 edit Xonealt
1113 edit #
1114 mksession! Xtest_altfile
1115 only
1116 bwipe Xonealt
1117 bwipe Xtwoalt
1118 bwipe!
1119 source Xtest_altfile
1120 call assert_equal('Xone', bufname())
1121 call assert_equal('Xonealt', bufname('#'))
1122 wincmd w
1123 call assert_equal('Xtwo', bufname())
1124 call assert_equal('Xtwoalt', bufname('#'))
1125 only
1126 bwipe!
Bram Moolenaarac665c22020-12-08 20:39:15 +01001127 call delete('Xtest_altfile')
Bram Moolenaar3482be62020-11-27 11:00:38 +01001128endfunc
1129
Bram Moolenaar5c504f62021-04-01 13:39:51 +02001130" Test for creating views with manual folds
1131func Test_mkview_manual_fold()
1132 call writefile(range(1,10), 'Xfile')
1133 new Xfile
1134 " create recursive folds
1135 5,6fold
1136 4,7fold
1137 mkview Xview
1138 normal zE
1139 source Xview
1140 call assert_equal([-1, 4, 4, 4, 4, -1], [foldclosed(3), foldclosed(4),
1141 \ foldclosed(5), foldclosed(6), foldclosed(7), foldclosed(8)])
1142 " open one level of fold
1143 4foldopen
1144 mkview! Xview
1145 normal zE
1146 source Xview
1147 call assert_equal([-1, -1, 5, 5, -1, -1], [foldclosed(3), foldclosed(4),
1148 \ foldclosed(5), foldclosed(6), foldclosed(7), foldclosed(8)])
1149 " open all the folds
1150 %foldopen!
1151 mkview! Xview
1152 normal zE
1153 source Xview
1154 call assert_equal([-1, -1, -1, -1, -1, -1], [foldclosed(3), foldclosed(4),
1155 \ foldclosed(5), foldclosed(6), foldclosed(7), foldclosed(8)])
1156 call delete('Xfile')
1157 call delete('Xview')
1158 bw!
1159endfunc
1160
Bram Moolenaareca626f2016-12-01 18:47:38 +01001161" vim: shiftwidth=2 sts=2 expandtab