blob: 8650598216b1689f9e6dd7bfa5131e971b2fc041 [file] [log] [blame]
Bram Moolenaarb20e3342016-01-18 23:29:01 +01001" Test for reading and writing .viminfo
2
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02003source check.vim
4
Bram Moolenaar156919f2016-10-15 20:46:20 +02005function Test_viminfo_read_and_write()
Bram Moolenaar26b654a2019-07-22 20:50:17 +02006 " First clear 'history', so that "hislen" is zero. Then set it again,
7 " simulating Vim starting up.
8 set history=0
9 wviminfo Xviminfo
10 set history=1000
11
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020012 call histdel(':')
Bram Moolenaarb20e3342016-01-18 23:29:01 +010013 let lines = [
14 \ '# comment line',
15 \ '*encoding=utf-8',
16 \ '~MSle0~/asdf',
17 \ '|copied as-is',
18 \ '|and one more',
19 \ ]
20 call writefile(lines, 'Xviminfo')
21 rviminfo Xviminfo
22 call assert_equal('asdf', @/)
23
24 wviminfo Xviminfo
25 let lines = readfile('Xviminfo')
26 let done = 0
27 for line in lines
Bram Moolenaar156919f2016-10-15 20:46:20 +020028 if line[0] == '|' && line !~ '^|[234],' && line !~ '^|<'
Bram Moolenaarb20e3342016-01-18 23:29:01 +010029 if done == 0
Bram Moolenaar2d358992016-06-12 21:20:54 +020030 call assert_equal('|1,4', line)
Bram Moolenaarb20e3342016-01-18 23:29:01 +010031 elseif done == 1
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020032 call assert_equal('|copied as-is', line)
33 elseif done == 2
Bram Moolenaarb20e3342016-01-18 23:29:01 +010034 call assert_equal('|and one more', line)
35 endif
36 let done += 1
37 endif
38 endfor
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020039 call assert_equal(3, done)
Bram Moolenaarb20e3342016-01-18 23:29:01 +010040
41 call delete('Xviminfo')
42endfunc
43
44func Test_global_vars()
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +020045 let g:MY_GLOBAL_STRING = "Vim Editor"
46 let g:MY_GLOBAL_NUM = 345
47 let g:MY_GLOBAL_FLOAT = 3.14
Bram Moolenaarb20e3342016-01-18 23:29:01 +010048 let test_dict = {'foo': 1, 'bar': 0, 'longvarible': 1000}
49 let g:MY_GLOBAL_DICT = test_dict
50 " store a really long list, so line wrapping will occur in viminfo file
51 let test_list = range(1,100)
52 let g:MY_GLOBAL_LIST = test_list
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +010053 let test_blob = 0z00112233445566778899aabbccddeeff
54 let g:MY_GLOBAL_BLOB = test_blob
55 let test_false = v:false
56 let g:MY_GLOBAL_FALSE = test_false
57 let test_true = v:true
58 let g:MY_GLOBAL_TRUE = test_true
59 let test_null = v:null
60 let g:MY_GLOBAL_NULL = test_null
61 let test_none = v:none
62 let g:MY_GLOBAL_NONE = test_none
63
Bram Moolenaare9c07272016-03-30 20:50:46 +020064 set viminfo='100,<50,s10,h,!,nviminfo
Bram Moolenaarb20e3342016-01-18 23:29:01 +010065 wv! Xviminfo
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +010066
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +020067 unlet g:MY_GLOBAL_STRING
68 unlet g:MY_GLOBAL_NUM
69 unlet g:MY_GLOBAL_FLOAT
Bram Moolenaarb20e3342016-01-18 23:29:01 +010070 unlet g:MY_GLOBAL_DICT
71 unlet g:MY_GLOBAL_LIST
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +010072 unlet g:MY_GLOBAL_BLOB
73 unlet g:MY_GLOBAL_FALSE
74 unlet g:MY_GLOBAL_TRUE
75 unlet g:MY_GLOBAL_NULL
76 unlet g:MY_GLOBAL_NONE
Bram Moolenaarb20e3342016-01-18 23:29:01 +010077
78 rv! Xviminfo
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +020079 call assert_equal("Vim Editor", g:MY_GLOBAL_STRING)
80 call assert_equal(345, g:MY_GLOBAL_NUM)
81 call assert_equal(3.14, g:MY_GLOBAL_FLOAT)
Bram Moolenaarb20e3342016-01-18 23:29:01 +010082 call assert_equal(test_dict, g:MY_GLOBAL_DICT)
83 call assert_equal(test_list, g:MY_GLOBAL_LIST)
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +010084 call assert_equal(test_blob, g:MY_GLOBAL_BLOB)
85 call assert_equal(test_false, g:MY_GLOBAL_FALSE)
86 call assert_equal(test_true, g:MY_GLOBAL_TRUE)
87 call assert_equal(test_null, g:MY_GLOBAL_NULL)
88 call assert_equal(test_none, g:MY_GLOBAL_NONE)
Bram Moolenaarb20e3342016-01-18 23:29:01 +010089
90 call delete('Xviminfo')
91 set viminfo-=!
92endfunc
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020093
Bram Moolenaar5b157fe2020-06-07 16:08:08 +020094func Test_global_vars_with_circular_reference()
95 let g:MY_GLOBAL_LIST = []
96 call add(g:MY_GLOBAL_LIST, g:MY_GLOBAL_LIST)
97 let g:MY_GLOBAL_DICT = {}
98 let g:MY_GLOBAL_DICT['self'] = g:MY_GLOBAL_DICT
99
100 set viminfo='100,<50,s10,h,!,nviminfo
101 wv! Xviminfo
102 call assert_equal(v:errmsg, '')
103
104 unlet g:MY_GLOBAL_LIST
105 unlet g:MY_GLOBAL_DICT
106
107 rv! Xviminfo
108 call assert_equal(v:errmsg, '')
109 call assert_true(!exists('g:MY_GLOBAL_LIST'))
110 call assert_true(!exists('g:MY_GLOBAL_DICT'))
111
112 call delete('Xviminfo')
113 set viminfo-=!
114endfunc
115
Bram Moolenaar45d2eea2016-06-06 21:07:52 +0200116func Test_cmdline_history()
117 call histdel(':')
118 call test_settime(11)
119 call histadd(':', "echo 'one'")
120 call test_settime(12)
121 " split into two lines
122 let long800 = repeat(" 'eight'", 100)
123 call histadd(':', "echo " . long800)
124 call test_settime(13)
125 " split into three lines
126 let long1400 = repeat(" 'fourteeeeen'", 100)
127 call histadd(':', "echo " . long1400)
128 wviminfo Xviminfo
129 let lines = readfile('Xviminfo')
130 let done_colon = 0
131 let done_bar = 0
132 let lnum = 0
133 while lnum < len(lines)
134 let line = lines[lnum] | let lnum += 1
135 if line[0] == ':'
136 if done_colon == 0
137 call assert_equal(":\x161408", line)
138 let line = lines[lnum] | let lnum += 1
139 call assert_equal('<echo ' . long1400, line)
140 elseif done_colon == 1
141 call assert_equal(":\x16808", line)
142 let line = lines[lnum] | let lnum += 1
143 call assert_equal("<echo " . long800, line)
144 elseif done_colon == 2
145 call assert_equal(":echo 'one'", line)
146 endif
147 let done_colon += 1
148 elseif line[0:4] == '|2,0,'
149 if done_bar == 0
150 call assert_equal("|2,0,13,,>1407", line)
151 let line = lines[lnum] | let lnum += 1
152 call assert_equal('|<"echo ' . long1400[0:484], line)
153 let line = lines[lnum] | let lnum += 1
154 call assert_equal('|<' . long1400[485:974], line)
155 let line = lines[lnum] | let lnum += 1
156 call assert_equal('|<' . long1400[975:] . '"', line)
157 elseif done_bar == 1
158 call assert_equal('|2,0,12,,>807', line)
159 let line = lines[lnum] | let lnum += 1
160 call assert_equal('|<"echo ' . long800[0:484], line)
161 let line = lines[lnum] | let lnum += 1
162 call assert_equal('|<' . long800[485:] . '"', line)
163 elseif done_bar == 2
164 call assert_equal("|2,0,11,,\"echo 'one'\"", line)
165 endif
166 let done_bar += 1
167 endif
168 endwhile
169 call assert_equal(3, done_colon)
170 call assert_equal(3, done_bar)
171
172 call histdel(':')
173 rviminfo Xviminfo
174 call assert_equal("echo " . long1400, histget(':', -1))
175 call assert_equal("echo " . long800, histget(':', -2))
176 call assert_equal("echo 'one'", histget(':', -3))
177
178 call delete('Xviminfo')
179endfunc
Bram Moolenaar1fd99c12016-06-09 20:24:28 +0200180
181func Test_cmdline_history_order()
182 call histdel(':')
183 call test_settime(11)
184 call histadd(':', "echo '11'")
185 call test_settime(22)
186 call histadd(':', "echo '22'")
187 call test_settime(33)
188 call histadd(':', "echo '33'")
189 wviminfo Xviminfo
190
191 call histdel(':')
192 " items go in between
193 call test_settime(15)
194 call histadd(':', "echo '15'")
195 call test_settime(27)
196 call histadd(':', "echo '27'")
197
198 rviminfo Xviminfo
199 call assert_equal("echo '33'", histget(':', -1))
200 call assert_equal("echo '27'", histget(':', -2))
201 call assert_equal("echo '22'", histget(':', -3))
202 call assert_equal("echo '15'", histget(':', -4))
203 call assert_equal("echo '11'", histget(':', -5))
204
205 call histdel(':')
206 " items go before and after
Bram Moolenaarce90e362019-09-08 18:58:44 +0200207 eval 8->test_settime()
Bram Moolenaar1fd99c12016-06-09 20:24:28 +0200208 call histadd(':', "echo '8'")
209 call test_settime(39)
210 call histadd(':', "echo '39'")
211
212 rviminfo Xviminfo
213 call assert_equal("echo '39'", histget(':', -1))
214 call assert_equal("echo '33'", histget(':', -2))
215 call assert_equal("echo '22'", histget(':', -3))
216 call assert_equal("echo '11'", histget(':', -4))
217 call assert_equal("echo '8'", histget(':', -5))
218
219 " Check sorting works when writing with merge.
220 call histdel(':')
221 call test_settime(8)
222 call histadd(':', "echo '8'")
223 call test_settime(15)
224 call histadd(':', "echo '15'")
225 call test_settime(27)
226 call histadd(':', "echo '27'")
227 call test_settime(39)
228 call histadd(':', "echo '39'")
229 wviminfo Xviminfo
230
231 call histdel(':')
232 rviminfo Xviminfo
233 call assert_equal("echo '39'", histget(':', -1))
234 call assert_equal("echo '33'", histget(':', -2))
235 call assert_equal("echo '27'", histget(':', -3))
236 call assert_equal("echo '22'", histget(':', -4))
237 call assert_equal("echo '15'", histget(':', -5))
238 call assert_equal("echo '11'", histget(':', -6))
239 call assert_equal("echo '8'", histget(':', -7))
240
241 call delete('Xviminfo')
242endfunc
Bram Moolenaar01227092016-06-11 14:47:40 +0200243
Bram Moolenaare80ff742016-06-11 21:14:18 +0200244func Test_viminfo_registers()
245 call test_settime(8)
246 call setreg('a', "eight", 'c')
247 call test_settime(20)
248 call setreg('b', ["twenty", "again"], 'l')
249 call test_settime(40)
250 call setreg('c', ["four", "agai"], 'b4')
251 let l = []
252 set viminfo='100,<600,s10,h,!,nviminfo
253 for i in range(500)
254 call add(l, 'something')
255 endfor
256 call setreg('d', l, 'l')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200257 call setreg('e', "abc\<C-V>xyz")
Bram Moolenaare80ff742016-06-11 21:14:18 +0200258 wviminfo Xviminfo
259
260 call test_settime(10)
261 call setreg('a', '', 'b10')
262 call test_settime(15)
263 call setreg('b', 'drop')
264 call test_settime(50)
265 call setreg('c', 'keep', 'l')
266 call test_settime(30)
267 call setreg('d', 'drop', 'l')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200268 call setreg('e', 'drop')
Bram Moolenaare80ff742016-06-11 21:14:18 +0200269 rviminfo Xviminfo
270
271 call assert_equal("", getreg('a'))
272 call assert_equal("\<C-V>10", getregtype('a'))
273 call assert_equal("twenty\nagain\n", getreg('b'))
274 call assert_equal("V", getregtype('b'))
275 call assert_equal("keep\n", getreg('c'))
276 call assert_equal("V", getregtype('c'))
277 call assert_equal(l, getreg('d', 1, 1))
278 call assert_equal("V", getregtype('d'))
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200279 call assert_equal("abc\<C-V>xyz", getreg('e'))
Bram Moolenaare80ff742016-06-11 21:14:18 +0200280
Bram Moolenaar25709572016-08-26 20:41:16 +0200281 " Length around 440 switches to line continuation.
282 let len = 434
283 while len < 445
284 let s = repeat('a', len)
285 call setreg('"', s)
286 wviminfo Xviminfo
287 call setreg('"', '')
288 rviminfo Xviminfo
289 call assert_equal(s, getreg('"'), 'wrong register at length: ' . len)
290
291 let len += 1
292 endwhile
293
Bram Moolenaare80ff742016-06-11 21:14:18 +0200294 call delete('Xviminfo')
295endfunc
296
Bram Moolenaar2d358992016-06-12 21:20:54 +0200297func Test_viminfo_marks()
298 sp bufa
299 let bufa = bufnr('%')
300 sp bufb
301 let bufb = bufnr('%')
302
303 call test_settime(8)
304 call setpos("'A", [bufa, 1, 1, 0])
305 call test_settime(20)
306 call setpos("'B", [bufb, 9, 1, 0])
307 call setpos("'C", [bufa, 7, 1, 0])
308
309 delmark 0-9
310 call test_settime(25)
311 call setpos("'1", [bufb, 12, 1, 0])
312 call test_settime(35)
313 call setpos("'0", [bufa, 11, 1, 0])
314
315 call test_settime(45)
316 wviminfo Xviminfo
317
318 " Writing viminfo inserts the '0 mark.
319 call assert_equal([bufb, 1, 1, 0], getpos("'0"))
320 call assert_equal([bufa, 11, 1, 0], getpos("'1"))
321 call assert_equal([bufb, 12, 1, 0], getpos("'2"))
322
323 call test_settime(4)
324 call setpos("'A", [bufa, 9, 1, 0])
325 call test_settime(30)
326 call setpos("'B", [bufb, 2, 3, 0])
327 delmark C
328
329 delmark 0-9
330 call test_settime(30)
331 call setpos("'1", [bufb, 22, 1, 0])
332 call test_settime(55)
333 call setpos("'0", [bufa, 21, 1, 0])
334
335 rviminfo Xviminfo
336
337 call assert_equal([bufa, 1, 1, 0], getpos("'A"))
338 call assert_equal([bufb, 2, 3, 0], getpos("'B"))
339 call assert_equal([bufa, 7, 1, 0], getpos("'C"))
340
341 " numbered marks are merged
342 call assert_equal([bufa, 21, 1, 0], getpos("'0")) " time 55
343 call assert_equal([bufb, 1, 1, 0], getpos("'1")) " time 45
344 call assert_equal([bufa, 11, 1, 0], getpos("'2")) " time 35
345 call assert_equal([bufb, 22, 1, 0], getpos("'3")) " time 30
346 call assert_equal([bufb, 12, 1, 0], getpos("'4")) " time 25
347
Bram Moolenaar8cd6cd82019-12-27 17:33:26 +0100348 " deleted file marks are removed from viminfo
349 delmark C
350 wviminfo Xviminfo
351 rviminfo Xviminfo
352 call assert_equal([0, 0, 0, 0], getpos("'C"))
353
354 " deleted file marks stay in viminfo if defined in another vim later
355 call test_settime(70)
356 call setpos("'D", [bufb, 8, 1, 0])
357 wviminfo Xviminfo
358 call test_settime(65)
359 delmark D
360 call assert_equal([0, 0, 0, 0], getpos("'D"))
361 call test_settime(75)
362 rviminfo Xviminfo
363 call assert_equal([bufb, 8, 1, 0], getpos("'D"))
364
Bram Moolenaar2d358992016-06-12 21:20:54 +0200365 call delete('Xviminfo')
366 exe 'bwipe ' . bufa
367 exe 'bwipe ' . bufb
368endfunc
369
370func Test_viminfo_jumplist()
371 split testbuf
372 clearjumps
373 call setline(1, ['time 05', 'time 10', 'time 15', 'time 20', 'time 30', 'last pos'])
374 call cursor(2, 1)
375 call test_settime(10)
376 exe "normal /20\r"
377 call test_settime(20)
378 exe "normal /30\r"
379 call test_settime(30)
380 exe "normal /last pos\r"
381 wviminfo Xviminfo
382
383 clearjumps
384 call cursor(1, 1)
385 call test_settime(5)
386 exe "normal /15\r"
387 call test_settime(15)
388 exe "normal /last pos\r"
389 call test_settime(40)
390 exe "normal ?30\r"
391 rviminfo Xviminfo
392
393 call assert_equal('time 30', getline('.'))
394 exe "normal \<C-O>"
395 call assert_equal('last pos', getline('.'))
396 exe "normal \<C-O>"
397 " duplicate for 'time 30' was removed
398 call assert_equal('time 20', getline('.'))
399 exe "normal \<C-O>"
400 call assert_equal('time 15', getline('.'))
401 exe "normal \<C-O>"
402 call assert_equal('time 10', getline('.'))
403 exe "normal \<C-O>"
404 call assert_equal('time 05', getline('.'))
405
Bram Moolenaarece74ab2016-06-13 22:22:15 +0200406 clearjumps
407 call cursor(1, 1)
408 call test_settime(5)
409 exe "normal /15\r"
410 call test_settime(15)
411 exe "normal /last pos\r"
412 call test_settime(40)
413 exe "normal ?30\r"
414 " Test merge when writing
415 wviminfo Xviminfo
416 clearjumps
417 rviminfo Xviminfo
418
Bram Moolenaar28607ba2016-06-15 21:44:51 +0200419 let last_line = line('.')
Bram Moolenaarece74ab2016-06-13 22:22:15 +0200420 exe "normal \<C-O>"
421 call assert_equal('time 30', getline('.'))
422 exe "normal \<C-O>"
423 call assert_equal('last pos', getline('.'))
424 exe "normal \<C-O>"
425 " duplicate for 'time 30' was removed
426 call assert_equal('time 20', getline('.'))
427 exe "normal \<C-O>"
428 call assert_equal('time 15', getline('.'))
429 exe "normal \<C-O>"
430 call assert_equal('time 10', getline('.'))
431 exe "normal \<C-O>"
432 call assert_equal('time 05', getline('.'))
433
Bram Moolenaar28607ba2016-06-15 21:44:51 +0200434 " Test with jumplist full.
435 clearjumps
436 call setline(1, repeat(['match here'], 101))
437 call cursor(1, 1)
438 call test_settime(10)
439 for i in range(100)
440 exe "normal /here\r"
441 endfor
442 rviminfo Xviminfo
443
444 " must be newest mark that comes from viminfo.
445 exe "normal \<C-O>"
446 call assert_equal(last_line, line('.'))
447
Bram Moolenaar2d358992016-06-12 21:20:54 +0200448 bwipe!
449 call delete('Xviminfo')
450endfunc
451
Bram Moolenaar01227092016-06-11 14:47:40 +0200452func Test_viminfo_encoding()
Bram Moolenaar01227092016-06-11 14:47:40 +0200453 set enc=latin1
454 call histdel(':')
455 call histadd(':', "echo '\xe9'")
456 wviminfo Xviminfo
457
458 set fencs=utf-8,latin1
459 set enc=utf-8
460 sp Xviminfo
461 call assert_equal('latin1', &fenc)
462 close
463
464 call histdel(':')
465 rviminfo Xviminfo
466 call assert_equal("echo 'é'", histget(':', -1))
467
468 call delete('Xviminfo')
469endfunc
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +0200470
471func Test_viminfo_bad_syntax()
472 let lines = []
473 call add(lines, '|<') " empty continuation line
474 call add(lines, '|234234234234234324,nothing')
475 call add(lines, '|1+"no comma"')
476 call add(lines, '|1,2,3,4,5,6,7') " too many items
477 call add(lines, '|1,"string version"')
478 call add(lines, '|1,>x') " bad continuation line
479 call add(lines, '|1,"x') " missing quote
480 call add(lines, '|1,"x\') " trailing backslash
481 call add(lines, '|1,,,,') "trailing comma
482 call add(lines, '|1,>234') " trailing continuation line
483 call writefile(lines, 'Xviminfo')
Bram Moolenaare80ff742016-06-11 21:14:18 +0200484 rviminfo Xviminfo
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +0200485
486 call delete('Xviminfo')
487endfunc
488
Bram Moolenaarab9c89b2016-07-03 17:47:26 +0200489func Test_viminfo_file_marks()
490 silent! bwipe test_viminfo.vim
491 silent! bwipe Xviminfo
492
493 call test_settime(10)
494 edit ten
495 call test_settime(25)
496 edit again
497 call test_settime(30)
498 edit thirty
499 wviminfo Xviminfo
500
501 call test_settime(20)
502 edit twenty
503 call test_settime(35)
504 edit again
505 call test_settime(40)
506 edit fourty
507 wviminfo Xviminfo
508
509 sp Xviminfo
510 1
511 for name in ['fourty', 'again', 'thirty', 'twenty', 'ten']
512 /^>
513 call assert_equal(name, substitute(getline('.'), '.*/', '', ''))
514 endfor
515 close
516
517 call delete('Xviminfo')
518endfunc
Bram Moolenaare59215c2016-08-14 19:08:45 +0200519
520func Test_viminfo_file_mark_tabclose()
521 tabnew Xtestfileintab
522 call setline(1, ['a','b','c','d','e'])
523 4
524 q!
525 wviminfo Xviminfo
526 sp Xviminfo
527 /^> .*Xtestfileintab
528 let lnum = line('.')
529 while 1
530 if lnum == line('$')
Bram Moolenaar37175402017-03-18 20:18:45 +0100531 call assert_report('mark not found in Xtestfileintab')
Bram Moolenaare59215c2016-08-14 19:08:45 +0200532 break
533 endif
534 let lnum += 1
535 let line = getline(lnum)
536 if line == ''
Bram Moolenaar37175402017-03-18 20:18:45 +0100537 call assert_report('mark not found in Xtestfileintab')
Bram Moolenaare59215c2016-08-14 19:08:45 +0200538 break
539 endif
540 if line =~ "^\t\""
541 call assert_equal('4', substitute(line, ".*\"\t\\(\\d\\).*", '\1', ''))
542 break
543 endif
544 endwhile
545
546 call delete('Xviminfo')
547 silent! bwipe Xtestfileintab
548endfunc
Bram Moolenaare11d61a2016-08-20 18:36:54 +0200549
Bram Moolenaar156919f2016-10-15 20:46:20 +0200550func Test_viminfo_file_mark_zero_time()
551 let lines = [
552 \ '# Viminfo version',
553 \ '|1,4',
554 \ '',
555 \ '*encoding=utf-8',
556 \ '',
557 \ '# File marks:',
558 \ "'B 1 0 /tmp/nothing",
559 \ '|4,66,1,0,0,"/tmp/nothing"',
560 \ "",
561 \ ]
562 call writefile(lines, 'Xviminfo')
563 delmark B
564 rviminfo Xviminfo
565 call delete('Xviminfo')
566 call assert_equal(1, line("'B"))
567 delmark B
568endfunc
569
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200570" Test for saving and restoring file marks in unloaded buffers
571func Test_viminfo_file_mark_unloaded_buf()
572 let save_viminfo = &viminfo
573 set viminfo&vim
574 call writefile(repeat(['vim'], 10), 'Xfile1')
575 %bwipe
576 edit! Xfile1
577 call setpos("'u", [0, 3, 1, 0])
578 call setpos("'v", [0, 5, 1, 0])
579 enew
580 wviminfo Xviminfo
581 %bwipe
582 edit Xfile1
583 rviminfo! Xviminfo
584 call assert_equal([0, 3, 1, 0], getpos("'u"))
585 call assert_equal([0, 5, 1, 0], getpos("'v"))
586 %bwipe
587 call delete('Xfile1')
588 call delete('Xviminfo')
589 let &viminfo = save_viminfo
590endfunc
591
Bram Moolenaar156919f2016-10-15 20:46:20 +0200592func Test_viminfo_oldfiles()
Bram Moolenaare11d61a2016-08-20 18:36:54 +0200593 let v:oldfiles = []
594 let lines = [
595 \ '# comment line',
596 \ '*encoding=utf-8',
597 \ '',
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200598 \ ':h viminfo',
599 \ '?/session',
600 \ '=myvar',
601 \ '@123',
602 \ '',
603 \ "'E 2 0 /tmp/nothing",
604 \ '',
Bram Moolenaare11d61a2016-08-20 18:36:54 +0200605 \ "> /tmp/file_one.txt",
606 \ "\t\"\t11\t0",
607 \ "",
608 \ "> /tmp/file_two.txt",
609 \ "\t\"\t11\t0",
610 \ "",
611 \ "> /tmp/another.txt",
612 \ "\t\"\t11\t0",
613 \ "",
614 \ ]
615 call writefile(lines, 'Xviminfo')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200616 delmark E
Bram Moolenaare11d61a2016-08-20 18:36:54 +0200617 rviminfo! Xviminfo
618 call delete('Xviminfo')
619
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200620 call assert_equal('h viminfo', histget(':'))
621 call assert_equal('session', histget('/'))
622 call assert_equal('myvar', histget('='))
623 call assert_equal('123', histget('@'))
624 call assert_equal(2, line("'E"))
Bram Moolenaar7b668e82016-08-23 23:51:21 +0200625 call assert_equal(['1: /tmp/file_one.txt', '2: /tmp/file_two.txt', '3: /tmp/another.txt'], filter(split(execute('oldfiles'), "\n"), {i, v -> v =~ '/tmp/'}))
626 call assert_equal(['1: /tmp/file_one.txt', '2: /tmp/file_two.txt'], filter(split(execute('filter file_ oldfiles'), "\n"), {i, v -> v =~ '/tmp/'}))
627 call assert_equal(['3: /tmp/another.txt'], filter(split(execute('filter /another/ oldfiles'), "\n"), {i, v -> v =~ '/tmp/'}))
Bram Moolenaar5328cb82019-07-27 23:27:51 +0200628
629 new
630 call feedkeys("3\<CR>", 't')
631 browse oldfiles
632 call assert_equal("/tmp/another.txt", expand("%"))
633 bwipe
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200634 delmark E
635endfunc
636
637" Test for storing and restoring buffer list in 'viminfo'
638func Test_viminfo_bufferlist()
639 " If there are arguments, then :rviminfo doesn't read the buffer list.
640 " Need to delete all the arguments for :rviminfo to work.
641 %argdelete
642
643 edit Xfile1
644 edit Xfile2
645 set viminfo-=%
646 wviminfo Xviminfo
647 %bwipe
648 rviminfo Xviminfo
649 call assert_equal(1, len(getbufinfo()))
650
651 edit Xfile1
652 edit Xfile2
653 set viminfo^=%
654 wviminfo Xviminfo
655 %bwipe
656 rviminfo Xviminfo
657 let l = getbufinfo()
658 call assert_equal(3, len(l))
659 call assert_equal('Xfile1', bufname(l[1].bufnr))
660 call assert_equal('Xfile2', bufname(l[2].bufnr))
661
662 call delete('Xviminfo')
663 %bwipe
664 set viminfo-=%
665endfunc
666
667" Test for errors in a viminfo file
668func Test_viminfo_error()
669 " Non-existing viminfo files
670 call assert_fails('rviminfo xyz', 'E195:')
671
672 " Illegal starting character
673 call writefile(["a 123"], 'Xviminfo')
674 call assert_fails('rv Xviminfo', 'E575:')
675
676 " Illegal register name in the viminfo file
677 call writefile(['"@ LINE 0'], 'Xviminfo')
678 call assert_fails('rv Xviminfo', 'E577:')
679
680 " Invalid file mark line
681 call writefile(['>', '@'], 'Xviminfo')
682 call assert_fails('rv Xviminfo', 'E576:')
683
684 " Too many errors in viminfo file
685 call writefile(repeat(["a 123"], 15), 'Xviminfo')
686 call assert_fails('rv Xviminfo', 'E136:')
687
688 call writefile(['>'] + repeat(['@'], 10), 'Xviminfo')
689 call assert_fails('rv Xviminfo', 'E136:')
690
691 call writefile(repeat(['"@'], 15), 'Xviminfo')
692 call assert_fails('rv Xviminfo', 'E136:')
693
694 call delete('Xviminfo')
695endfunc
696
697" Test for saving and restoring last substitute string in viminfo
698func Test_viminfo_lastsub()
699 enew
700 call append(0, "blue blue blue")
701 call cursor(1, 1)
702 s/blue/green/
703 wviminfo Xviminfo
704 s/blue/yellow/
705 rviminfo! Xviminfo
706 &
707 call assert_equal("green yellow green", getline(1))
708 enew!
709 call delete('Xviminfo')
710endfunc
711
712" Test saving and restoring the register values using the older method
713func Test_viminfo_registers_old()
714 let lines = [
715 \ '# Viminfo version',
716 \ '|1,1',
717 \ '',
718 \ '*encoding=utf-8',
719 \ '',
720 \ '# Registers:',
721 \ '""0 CHAR 0',
722 \ ' Vim',
723 \ '"a CHAR 0',
724 \ ' red',
725 \ '"m@ CHAR 0',
726 \ " :echo 'Hello'\<CR>",
727 \ "",
728 \ ]
729 call writefile(lines, 'Xviminfo')
730 let @a = 'one'
731 let @b = 'two'
732 let @m = 'three'
733 let @" = 'four'
734 let @t = ":echo 'Unix'\<CR>"
735 silent! normal @t
736 rviminfo! Xviminfo
737 call assert_equal('red', getreg('a'))
738 call assert_equal('two', getreg('b'))
739 call assert_equal(":echo 'Hello'\<CR>", getreg('m'))
740 call assert_equal('Vim', getreg('"'))
741 call assert_equal("\nHello", execute('normal @@'))
742 call delete('Xviminfo')
743 let @" = ''
744endfunc
745
746" Test for saving and restoring large number of lines in a register
747func Test_viminfo_large_register()
748 let save_viminfo = &viminfo
749 set viminfo&vim
750 set viminfo-=<50
751 set viminfo+=<200
752 let lines = ['"r CHAR 0']
753 call extend(lines, repeat(["\tsun is rising"], 200))
754 call writefile(lines, 'Xviminfo')
755 let @r = ''
756 rviminfo! Xviminfo
757 call assert_equal(join(repeat(["sun is rising"], 200), "\n"), @r)
758 call delete('Xviminfo')
Bram Moolenaar6bd1d772019-10-09 22:01:25 +0200759 let @r = ''
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200760 let &viminfo = save_viminfo
761endfunc
762
763" Test for setting 'viminfofile' to NONE
764func Test_viminfofile_none()
Bram Moolenaar6bd1d772019-10-09 22:01:25 +0200765 let save_vif = &viminfofile
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200766 set viminfofile=NONE
767 wviminfo Xviminfo
768 call assert_false(filereadable('Xviminfo'))
769 call writefile([''], 'Xviminfo')
770 call assert_fails('rviminfo Xviminfo', 'E195:')
771 call delete('Xviminfo')
Bram Moolenaar6bd1d772019-10-09 22:01:25 +0200772 let &viminfofile = save_vif
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200773endfunc
774
Bram Moolenaar6bd1d772019-10-09 22:01:25 +0200775" Test for an unwritable and unreadble 'viminfo' file
776func Test_viminfo_perm()
777 CheckUnix
Bram Moolenaar07282f02019-10-10 16:46:17 +0200778 CheckNotRoot
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200779 call writefile([''], 'Xviminfo')
780 call setfperm('Xviminfo', 'r-x------')
781 call assert_fails('wviminfo Xviminfo', 'E137:')
Bram Moolenaar6bd1d772019-10-09 22:01:25 +0200782 call setfperm('Xviminfo', '--x------')
783 call assert_fails('rviminfo Xviminfo', 'E195:')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200784 call delete('Xviminfo')
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200785
786 " Try to write the viminfo to a directory
787 call mkdir('Xdir')
788 call assert_fails('wviminfo Xdir', 'E886:')
789 call delete('Xdir', 'rf')
Bram Moolenaare11d61a2016-08-20 18:36:54 +0200790endfunc
Bram Moolenaar6bd1d772019-10-09 22:01:25 +0200791
792" Test for writing to an existing viminfo file merges the file marks
793func XTest_viminfo_marks_merge()
794 let save_viminfo = &viminfo
795 set viminfo&vim
796 set viminfo^=%
797 enew
798 %argdelete
799 %bwipe
800
801 call writefile(repeat(['editor'], 10), 'Xbufa')
802 call writefile(repeat(['Vim'], 10), 'Xbufb')
803
804 " set marks in buffers
805 call test_settime(10)
806 edit Xbufa
807 4mark a
808 wviminfo Xviminfo
809 edit Xbufb
810 4mark b
811 wviminfo Xviminfo
812 %bwipe
813
814 " set marks in buffers again
815 call test_settime(20)
816 edit Xbufb
817 6mark b
818 wviminfo Xviminfo
819 edit Xbufa
820 6mark a
821 wviminfo Xviminfo
822 %bwipe
823
824 " Load the buffer and check the marks
825 edit Xbufa
826 rviminfo! Xviminfo
827 call assert_equal(6, line("'a"))
828 edit Xbufb
829 rviminfo! Xviminfo
830 call assert_equal(6, line("'b"))
831
832 " cleanup
833 %bwipe
834 call delete('Xviminfo')
835 call delete('Xbufa')
836 call delete('Xbufb')
837 call test_settime(0)
838 let &viminfo=save_viminfo
839endfunc
Bram Moolenaaree4e0c12020-04-06 21:35:05 +0200840
841" Test for errors in setting 'viminfo'
842func Test_viminfo_option_error()
843 " Missing number
844 call assert_fails('set viminfo=\"', 'E526:')
845 for c in split("'/:<@s", '\zs')
846 call assert_fails('set viminfo=' .. c, 'E526:')
847 endfor
848
849 " Missing comma
850 call assert_fails('set viminfo=%10!', 'E527:')
851 call assert_fails('set viminfo=!%10', 'E527:')
852 call assert_fails('set viminfo=h%10', 'E527:')
853 call assert_fails('set viminfo=c%10', 'E527:')
854 call assert_fails('set viminfo=:10%10', 'E527:')
855
856 " Missing ' setting
857 call assert_fails('set viminfo=%10', 'E528:')
858endfunc
859
860" vim: shiftwidth=2 sts=2 expandtab