blob: e3767e9a2b9af094564b45dc0acdd945db89c85b [file] [log] [blame]
Bram Moolenaarb20e3342016-01-18 23:29:01 +01001" Test for reading and writing .viminfo
2
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +02003func Test_viminfo_read_and_write()
Bram Moolenaar26b654a2019-07-22 20:50:17 +02004 " First clear 'history', so that "hislen" is zero. Then set it again,
5 " simulating Vim starting up.
6 set history=0
7 wviminfo Xviminfo
8 set history=1000
9
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020010 call histdel(':')
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +020011 let @/=''
Bram Moolenaarb20e3342016-01-18 23:29:01 +010012 let lines = [
13 \ '# comment line',
14 \ '*encoding=utf-8',
15 \ '~MSle0~/asdf',
16 \ '|copied as-is',
17 \ '|and one more',
18 \ ]
Bram Moolenaar5b148ef2022-10-15 21:35:56 +010019 call writefile(lines, 'Xviminfo', 'D')
Bram Moolenaarb20e3342016-01-18 23:29:01 +010020 rviminfo Xviminfo
21 call assert_equal('asdf', @/)
22
23 wviminfo Xviminfo
24 let lines = readfile('Xviminfo')
25 let done = 0
26 for line in lines
Bram Moolenaar156919f2016-10-15 20:46:20 +020027 if line[0] == '|' && line !~ '^|[234],' && line !~ '^|<'
Bram Moolenaarb20e3342016-01-18 23:29:01 +010028 if done == 0
Bram Moolenaar2d358992016-06-12 21:20:54 +020029 call assert_equal('|1,4', line)
Bram Moolenaarb20e3342016-01-18 23:29:01 +010030 elseif done == 1
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020031 call assert_equal('|copied as-is', line)
32 elseif done == 2
Bram Moolenaarb20e3342016-01-18 23:29:01 +010033 call assert_equal('|and one more', line)
34 endif
35 let done += 1
36 endif
37 endfor
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020038 call assert_equal(3, done)
Bram Moolenaarb20e3342016-01-18 23:29:01 +010039endfunc
40
41func Test_global_vars()
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +020042 let g:MY_GLOBAL_STRING = "Vim Editor"
43 let g:MY_GLOBAL_NUM = 345
44 let g:MY_GLOBAL_FLOAT = 3.14
Bram Moolenaarb20e3342016-01-18 23:29:01 +010045 let test_dict = {'foo': 1, 'bar': 0, 'longvarible': 1000}
46 let g:MY_GLOBAL_DICT = test_dict
47 " store a really long list, so line wrapping will occur in viminfo file
48 let test_list = range(1,100)
49 let g:MY_GLOBAL_LIST = test_list
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +010050 let test_blob = 0z00112233445566778899aabbccddeeff
51 let g:MY_GLOBAL_BLOB = test_blob
52 let test_false = v:false
53 let g:MY_GLOBAL_FALSE = test_false
54 let test_true = v:true
55 let g:MY_GLOBAL_TRUE = test_true
56 let test_null = v:null
57 let g:MY_GLOBAL_NULL = test_null
58 let test_none = v:none
59 let g:MY_GLOBAL_NONE = test_none
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +020060 let g:MY_GLOBAL_FUNCREF = function('min')
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +010061
Bram Moolenaare9c07272016-03-30 20:50:46 +020062 set viminfo='100,<50,s10,h,!,nviminfo
Bram Moolenaarb20e3342016-01-18 23:29:01 +010063 wv! Xviminfo
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +010064
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +020065 unlet g:MY_GLOBAL_STRING
66 unlet g:MY_GLOBAL_NUM
67 unlet g:MY_GLOBAL_FLOAT
Bram Moolenaarb20e3342016-01-18 23:29:01 +010068 unlet g:MY_GLOBAL_DICT
69 unlet g:MY_GLOBAL_LIST
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +010070 unlet g:MY_GLOBAL_BLOB
71 unlet g:MY_GLOBAL_FALSE
72 unlet g:MY_GLOBAL_TRUE
73 unlet g:MY_GLOBAL_NULL
74 unlet g:MY_GLOBAL_NONE
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +020075 unlet g:MY_GLOBAL_FUNCREF
Bram Moolenaarb20e3342016-01-18 23:29:01 +010076
77 rv! Xviminfo
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +020078 call assert_equal("Vim Editor", g:MY_GLOBAL_STRING)
79 call assert_equal(345, g:MY_GLOBAL_NUM)
80 call assert_equal(3.14, g:MY_GLOBAL_FLOAT)
Bram Moolenaarb20e3342016-01-18 23:29:01 +010081 call assert_equal(test_dict, g:MY_GLOBAL_DICT)
82 call assert_equal(test_list, g:MY_GLOBAL_LIST)
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +010083 call assert_equal(test_blob, g:MY_GLOBAL_BLOB)
84 call assert_equal(test_false, g:MY_GLOBAL_FALSE)
85 call assert_equal(test_true, g:MY_GLOBAL_TRUE)
86 call assert_equal(test_null, g:MY_GLOBAL_NULL)
87 call assert_equal(test_none, g:MY_GLOBAL_NONE)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +020088 call assert_false(exists("g:MY_GLOBAL_FUNCREF"))
Bram Moolenaarb20e3342016-01-18 23:29:01 +010089
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +020090 " When reading global variables from viminfo, if a variable cannot be
91 " modified, then the value should not be changed.
92 unlet g:MY_GLOBAL_STRING
93 unlet g:MY_GLOBAL_NUM
94 unlet g:MY_GLOBAL_FLOAT
95 unlet g:MY_GLOBAL_DICT
96 unlet g:MY_GLOBAL_LIST
97 unlet g:MY_GLOBAL_BLOB
98
99 const g:MY_GLOBAL_STRING = 'New Value'
100 const g:MY_GLOBAL_NUM = 987
101 const g:MY_GLOBAL_FLOAT = 1.16
102 const g:MY_GLOBAL_DICT = {'editor': 'vim'}
103 const g:MY_GLOBAL_LIST = [5, 7, 13]
104 const g:MY_GLOBAL_BLOB = 0zDEADBEEF
105 call assert_fails('rv! Xviminfo', 'E741:')
106 call assert_equal('New Value', g:MY_GLOBAL_STRING)
107 call assert_equal(987, g:MY_GLOBAL_NUM)
108 call assert_equal(1.16, g:MY_GLOBAL_FLOAT)
109 call assert_equal({'editor': 'vim'}, g:MY_GLOBAL_DICT)
110 call assert_equal([5, 7 , 13], g:MY_GLOBAL_LIST)
111 call assert_equal(0zDEADBEEF, g:MY_GLOBAL_BLOB)
112
113 unlet g:MY_GLOBAL_STRING
114 unlet g:MY_GLOBAL_NUM
115 unlet g:MY_GLOBAL_FLOAT
116 unlet g:MY_GLOBAL_DICT
117 unlet g:MY_GLOBAL_LIST
118 unlet g:MY_GLOBAL_BLOB
119
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200120 " Test for invalid values for a blob, list, dict in a viminfo file
121 call writefile([
122 \ "!GLOB_BLOB_1\tBLO\t123",
123 \ "!GLOB_BLOB_2\tBLO\t012",
124 \ "!GLOB_BLOB_3\tBLO\t0z1x",
125 \ "!GLOB_BLOB_4\tBLO\t0z12 ab",
126 \ "!GLOB_LIST_1\tLIS\t1 2",
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100127 \ "!GLOB_DICT_1\tDIC\t1 2"], 'Xviminfo', 'D')
Bram Moolenaarfae55a92021-06-17 22:08:30 +0200128 call assert_fails('rv! Xviminfo', 'E488:')
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200129 call assert_equal('123', g:GLOB_BLOB_1)
130 call assert_equal(1, type(g:GLOB_BLOB_1))
131 call assert_equal('012', g:GLOB_BLOB_2)
132 call assert_equal(1, type(g:GLOB_BLOB_2))
133 call assert_equal('0z1x', g:GLOB_BLOB_3)
134 call assert_equal(1, type(g:GLOB_BLOB_3))
135 call assert_equal('0z12 ab', g:GLOB_BLOB_4)
136 call assert_equal(1, type(g:GLOB_BLOB_4))
137 call assert_equal('1 2', g:GLOB_LIST_1)
138 call assert_equal(1, type(g:GLOB_LIST_1))
139 call assert_equal('1 2', g:GLOB_DICT_1)
140 call assert_equal(1, type(g:GLOB_DICT_1))
141
Bram Moolenaarb20e3342016-01-18 23:29:01 +0100142 set viminfo-=!
143endfunc
Bram Moolenaar45d2eea2016-06-06 21:07:52 +0200144
Bram Moolenaar5b157fe2020-06-07 16:08:08 +0200145func Test_global_vars_with_circular_reference()
146 let g:MY_GLOBAL_LIST = []
147 call add(g:MY_GLOBAL_LIST, g:MY_GLOBAL_LIST)
148 let g:MY_GLOBAL_DICT = {}
149 let g:MY_GLOBAL_DICT['self'] = g:MY_GLOBAL_DICT
150
151 set viminfo='100,<50,s10,h,!,nviminfo
152 wv! Xviminfo
153 call assert_equal(v:errmsg, '')
154
155 unlet g:MY_GLOBAL_LIST
156 unlet g:MY_GLOBAL_DICT
157
158 rv! Xviminfo
159 call assert_equal(v:errmsg, '')
160 call assert_true(!exists('g:MY_GLOBAL_LIST'))
161 call assert_true(!exists('g:MY_GLOBAL_DICT'))
162
163 call delete('Xviminfo')
164 set viminfo-=!
165endfunc
166
Bram Moolenaar45d2eea2016-06-06 21:07:52 +0200167func Test_cmdline_history()
168 call histdel(':')
169 call test_settime(11)
170 call histadd(':', "echo 'one'")
171 call test_settime(12)
172 " split into two lines
173 let long800 = repeat(" 'eight'", 100)
174 call histadd(':', "echo " . long800)
175 call test_settime(13)
176 " split into three lines
177 let long1400 = repeat(" 'fourteeeeen'", 100)
178 call histadd(':', "echo " . long1400)
179 wviminfo Xviminfo
180 let lines = readfile('Xviminfo')
181 let done_colon = 0
182 let done_bar = 0
183 let lnum = 0
184 while lnum < len(lines)
185 let line = lines[lnum] | let lnum += 1
186 if line[0] == ':'
187 if done_colon == 0
188 call assert_equal(":\x161408", line)
189 let line = lines[lnum] | let lnum += 1
190 call assert_equal('<echo ' . long1400, line)
191 elseif done_colon == 1
192 call assert_equal(":\x16808", line)
193 let line = lines[lnum] | let lnum += 1
194 call assert_equal("<echo " . long800, line)
195 elseif done_colon == 2
196 call assert_equal(":echo 'one'", line)
197 endif
198 let done_colon += 1
199 elseif line[0:4] == '|2,0,'
200 if done_bar == 0
201 call assert_equal("|2,0,13,,>1407", line)
202 let line = lines[lnum] | let lnum += 1
203 call assert_equal('|<"echo ' . long1400[0:484], line)
204 let line = lines[lnum] | let lnum += 1
205 call assert_equal('|<' . long1400[485:974], line)
206 let line = lines[lnum] | let lnum += 1
207 call assert_equal('|<' . long1400[975:] . '"', line)
208 elseif done_bar == 1
209 call assert_equal('|2,0,12,,>807', line)
210 let line = lines[lnum] | let lnum += 1
211 call assert_equal('|<"echo ' . long800[0:484], line)
212 let line = lines[lnum] | let lnum += 1
213 call assert_equal('|<' . long800[485:] . '"', line)
214 elseif done_bar == 2
215 call assert_equal("|2,0,11,,\"echo 'one'\"", line)
216 endif
217 let done_bar += 1
218 endif
219 endwhile
220 call assert_equal(3, done_colon)
221 call assert_equal(3, done_bar)
222
223 call histdel(':')
224 rviminfo Xviminfo
225 call assert_equal("echo " . long1400, histget(':', -1))
226 call assert_equal("echo " . long800, histget(':', -2))
227 call assert_equal("echo 'one'", histget(':', -3))
228
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200229 " If the value for the '/' or ':' or '@' field in 'viminfo' is zero, then
230 " the corresponding history entries are not saved.
231 set viminfo='100,/0,:0,@0,<50,s10,h,!,nviminfo
232 call histdel('/')
233 call histdel(':')
234 call histdel('@')
235 call histadd('/', 'foo')
236 call histadd(':', 'bar')
237 call histadd('@', 'baz')
238 wviminfo! Xviminfo
239 call histdel('/')
240 call histdel(':')
241 call histdel('@')
242 rviminfo! Xviminfo
243 call assert_equal('', histget('/'))
244 call assert_equal('', histget(':'))
245 call assert_equal('', histget('@'))
246
Bram Moolenaar45d2eea2016-06-06 21:07:52 +0200247 call delete('Xviminfo')
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200248 set viminfo&vim
Bram Moolenaar45d2eea2016-06-06 21:07:52 +0200249endfunc
Bram Moolenaar1fd99c12016-06-09 20:24:28 +0200250
251func Test_cmdline_history_order()
252 call histdel(':')
253 call test_settime(11)
254 call histadd(':', "echo '11'")
255 call test_settime(22)
256 call histadd(':', "echo '22'")
257 call test_settime(33)
258 call histadd(':', "echo '33'")
259 wviminfo Xviminfo
260
261 call histdel(':')
262 " items go in between
263 call test_settime(15)
264 call histadd(':', "echo '15'")
265 call test_settime(27)
266 call histadd(':', "echo '27'")
267
268 rviminfo Xviminfo
269 call assert_equal("echo '33'", histget(':', -1))
270 call assert_equal("echo '27'", histget(':', -2))
271 call assert_equal("echo '22'", histget(':', -3))
272 call assert_equal("echo '15'", histget(':', -4))
273 call assert_equal("echo '11'", histget(':', -5))
274
275 call histdel(':')
276 " items go before and after
Bram Moolenaarce90e362019-09-08 18:58:44 +0200277 eval 8->test_settime()
Bram Moolenaar1fd99c12016-06-09 20:24:28 +0200278 call histadd(':', "echo '8'")
279 call test_settime(39)
280 call histadd(':', "echo '39'")
281
282 rviminfo Xviminfo
283 call assert_equal("echo '39'", histget(':', -1))
284 call assert_equal("echo '33'", histget(':', -2))
285 call assert_equal("echo '22'", histget(':', -3))
286 call assert_equal("echo '11'", histget(':', -4))
287 call assert_equal("echo '8'", histget(':', -5))
288
289 " Check sorting works when writing with merge.
290 call histdel(':')
291 call test_settime(8)
292 call histadd(':', "echo '8'")
293 call test_settime(15)
294 call histadd(':', "echo '15'")
295 call test_settime(27)
296 call histadd(':', "echo '27'")
297 call test_settime(39)
298 call histadd(':', "echo '39'")
299 wviminfo Xviminfo
Bram Moolenaar94722c52023-01-28 19:19:03 +0000300
Bram Moolenaar1fd99c12016-06-09 20:24:28 +0200301 call histdel(':')
302 rviminfo Xviminfo
303 call assert_equal("echo '39'", histget(':', -1))
304 call assert_equal("echo '33'", histget(':', -2))
305 call assert_equal("echo '27'", histget(':', -3))
306 call assert_equal("echo '22'", histget(':', -4))
307 call assert_equal("echo '15'", histget(':', -5))
308 call assert_equal("echo '11'", histget(':', -6))
309 call assert_equal("echo '8'", histget(':', -7))
310
311 call delete('Xviminfo')
312endfunc
Bram Moolenaar01227092016-06-11 14:47:40 +0200313
Bram Moolenaare80ff742016-06-11 21:14:18 +0200314func Test_viminfo_registers()
315 call test_settime(8)
316 call setreg('a', "eight", 'c')
317 call test_settime(20)
318 call setreg('b', ["twenty", "again"], 'l')
319 call test_settime(40)
320 call setreg('c', ["four", "agai"], 'b4')
321 let l = []
322 set viminfo='100,<600,s10,h,!,nviminfo
323 for i in range(500)
324 call add(l, 'something')
325 endfor
326 call setreg('d', l, 'l')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200327 call setreg('e', "abc\<C-V>xyz")
Bram Moolenaare80ff742016-06-11 21:14:18 +0200328 wviminfo Xviminfo
329
330 call test_settime(10)
331 call setreg('a', '', 'b10')
332 call test_settime(15)
333 call setreg('b', 'drop')
334 call test_settime(50)
335 call setreg('c', 'keep', 'l')
336 call test_settime(30)
337 call setreg('d', 'drop', 'l')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200338 call setreg('e', 'drop')
Bram Moolenaare80ff742016-06-11 21:14:18 +0200339 rviminfo Xviminfo
340
341 call assert_equal("", getreg('a'))
342 call assert_equal("\<C-V>10", getregtype('a'))
343 call assert_equal("twenty\nagain\n", getreg('b'))
344 call assert_equal("V", getregtype('b'))
345 call assert_equal("keep\n", getreg('c'))
346 call assert_equal("V", getregtype('c'))
347 call assert_equal(l, getreg('d', 1, 1))
348 call assert_equal("V", getregtype('d'))
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200349 call assert_equal("abc\<C-V>xyz", getreg('e'))
Bram Moolenaare80ff742016-06-11 21:14:18 +0200350
Bram Moolenaar25709572016-08-26 20:41:16 +0200351 " Length around 440 switches to line continuation.
352 let len = 434
353 while len < 445
354 let s = repeat('a', len)
355 call setreg('"', s)
356 wviminfo Xviminfo
357 call setreg('"', '')
358 rviminfo Xviminfo
359 call assert_equal(s, getreg('"'), 'wrong register at length: ' . len)
360
361 let len += 1
362 endwhile
363
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200364 " If the maximum number of lines saved for a register ('<' in 'viminfo') is
365 " zero, then register values should not be saved.
366 let @a = 'abc'
367 set viminfo='100,<0,s10,h,!,nviminfo
368 wviminfo Xviminfo
369 let @a = 'xyz'
370 rviminfo! Xviminfo
371 call assert_equal('xyz', @a)
372 " repeat the test with '"' instead of '<'
373 let @b = 'def'
374 set viminfo='100,\"0,s10,h,!,nviminfo
375 wviminfo Xviminfo
376 let @b = 'rst'
377 rviminfo! Xviminfo
378 call assert_equal('rst', @b)
379
380 " If the maximum size of an item ('s' in 'viminfo') is zero, then register
381 " values should not be saved.
382 let @c = '123'
383 set viminfo='100,<20,s0,h,!,nviminfo
384 wviminfo Xviminfo
385 let @c = '456'
386 rviminfo! Xviminfo
387 call assert_equal('456', @c)
388
Bram Moolenaare80ff742016-06-11 21:14:18 +0200389 call delete('Xviminfo')
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200390 set viminfo&vim
Bram Moolenaare80ff742016-06-11 21:14:18 +0200391endfunc
392
Bram Moolenaar2d358992016-06-12 21:20:54 +0200393func Test_viminfo_marks()
394 sp bufa
395 let bufa = bufnr('%')
396 sp bufb
397 let bufb = bufnr('%')
398
399 call test_settime(8)
400 call setpos("'A", [bufa, 1, 1, 0])
401 call test_settime(20)
402 call setpos("'B", [bufb, 9, 1, 0])
403 call setpos("'C", [bufa, 7, 1, 0])
404
405 delmark 0-9
406 call test_settime(25)
407 call setpos("'1", [bufb, 12, 1, 0])
408 call test_settime(35)
409 call setpos("'0", [bufa, 11, 1, 0])
410
411 call test_settime(45)
412 wviminfo Xviminfo
413
414 " Writing viminfo inserts the '0 mark.
415 call assert_equal([bufb, 1, 1, 0], getpos("'0"))
416 call assert_equal([bufa, 11, 1, 0], getpos("'1"))
417 call assert_equal([bufb, 12, 1, 0], getpos("'2"))
418
419 call test_settime(4)
420 call setpos("'A", [bufa, 9, 1, 0])
421 call test_settime(30)
422 call setpos("'B", [bufb, 2, 3, 0])
423 delmark C
424
425 delmark 0-9
426 call test_settime(30)
427 call setpos("'1", [bufb, 22, 1, 0])
428 call test_settime(55)
429 call setpos("'0", [bufa, 21, 1, 0])
430
431 rviminfo Xviminfo
432
433 call assert_equal([bufa, 1, 1, 0], getpos("'A"))
434 call assert_equal([bufb, 2, 3, 0], getpos("'B"))
435 call assert_equal([bufa, 7, 1, 0], getpos("'C"))
436
437 " numbered marks are merged
438 call assert_equal([bufa, 21, 1, 0], getpos("'0")) " time 55
439 call assert_equal([bufb, 1, 1, 0], getpos("'1")) " time 45
440 call assert_equal([bufa, 11, 1, 0], getpos("'2")) " time 35
441 call assert_equal([bufb, 22, 1, 0], getpos("'3")) " time 30
442 call assert_equal([bufb, 12, 1, 0], getpos("'4")) " time 25
443
Bram Moolenaar8cd6cd82019-12-27 17:33:26 +0100444 " deleted file marks are removed from viminfo
445 delmark C
446 wviminfo Xviminfo
447 rviminfo Xviminfo
448 call assert_equal([0, 0, 0, 0], getpos("'C"))
449
450 " deleted file marks stay in viminfo if defined in another vim later
451 call test_settime(70)
452 call setpos("'D", [bufb, 8, 1, 0])
453 wviminfo Xviminfo
454 call test_settime(65)
455 delmark D
456 call assert_equal([0, 0, 0, 0], getpos("'D"))
457 call test_settime(75)
458 rviminfo Xviminfo
459 call assert_equal([bufb, 8, 1, 0], getpos("'D"))
460
Bram Moolenaar2d358992016-06-12 21:20:54 +0200461 call delete('Xviminfo')
462 exe 'bwipe ' . bufa
463 exe 'bwipe ' . bufb
464endfunc
465
466func Test_viminfo_jumplist()
467 split testbuf
468 clearjumps
469 call setline(1, ['time 05', 'time 10', 'time 15', 'time 20', 'time 30', 'last pos'])
470 call cursor(2, 1)
471 call test_settime(10)
472 exe "normal /20\r"
473 call test_settime(20)
474 exe "normal /30\r"
475 call test_settime(30)
476 exe "normal /last pos\r"
477 wviminfo Xviminfo
478
479 clearjumps
480 call cursor(1, 1)
481 call test_settime(5)
482 exe "normal /15\r"
483 call test_settime(15)
484 exe "normal /last pos\r"
485 call test_settime(40)
486 exe "normal ?30\r"
487 rviminfo Xviminfo
488
489 call assert_equal('time 30', getline('.'))
490 exe "normal \<C-O>"
491 call assert_equal('last pos', getline('.'))
492 exe "normal \<C-O>"
493 " duplicate for 'time 30' was removed
494 call assert_equal('time 20', getline('.'))
495 exe "normal \<C-O>"
496 call assert_equal('time 15', getline('.'))
497 exe "normal \<C-O>"
498 call assert_equal('time 10', getline('.'))
499 exe "normal \<C-O>"
500 call assert_equal('time 05', getline('.'))
501
Bram Moolenaarece74ab2016-06-13 22:22:15 +0200502 clearjumps
503 call cursor(1, 1)
504 call test_settime(5)
505 exe "normal /15\r"
506 call test_settime(15)
507 exe "normal /last pos\r"
508 call test_settime(40)
509 exe "normal ?30\r"
510 " Test merge when writing
511 wviminfo Xviminfo
512 clearjumps
513 rviminfo Xviminfo
514
Bram Moolenaar28607ba2016-06-15 21:44:51 +0200515 let last_line = line('.')
Bram Moolenaarece74ab2016-06-13 22:22:15 +0200516 exe "normal \<C-O>"
517 call assert_equal('time 30', getline('.'))
518 exe "normal \<C-O>"
519 call assert_equal('last pos', getline('.'))
520 exe "normal \<C-O>"
521 " duplicate for 'time 30' was removed
522 call assert_equal('time 20', getline('.'))
523 exe "normal \<C-O>"
524 call assert_equal('time 15', getline('.'))
525 exe "normal \<C-O>"
526 call assert_equal('time 10', getline('.'))
527 exe "normal \<C-O>"
528 call assert_equal('time 05', getline('.'))
529
Bram Moolenaar28607ba2016-06-15 21:44:51 +0200530 " Test with jumplist full.
531 clearjumps
532 call setline(1, repeat(['match here'], 101))
533 call cursor(1, 1)
534 call test_settime(10)
535 for i in range(100)
536 exe "normal /here\r"
537 endfor
538 rviminfo Xviminfo
539
540 " must be newest mark that comes from viminfo.
541 exe "normal \<C-O>"
542 call assert_equal(last_line, line('.'))
543
Bram Moolenaar2d358992016-06-12 21:20:54 +0200544 bwipe!
545 call delete('Xviminfo')
546endfunc
547
Bram Moolenaar01227092016-06-11 14:47:40 +0200548func Test_viminfo_encoding()
Bram Moolenaar01227092016-06-11 14:47:40 +0200549 set enc=latin1
550 call histdel(':')
551 call histadd(':', "echo '\xe9'")
552 wviminfo Xviminfo
553
554 set fencs=utf-8,latin1
555 set enc=utf-8
556 sp Xviminfo
557 call assert_equal('latin1', &fenc)
558 close
Bram Moolenaar94722c52023-01-28 19:19:03 +0000559
Bram Moolenaar01227092016-06-11 14:47:40 +0200560 call histdel(':')
561 rviminfo Xviminfo
562 call assert_equal("echo 'é'", histget(':', -1))
563
564 call delete('Xviminfo')
565endfunc
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +0200566
567func Test_viminfo_bad_syntax()
568 let lines = []
569 call add(lines, '|<') " empty continuation line
570 call add(lines, '|234234234234234324,nothing')
571 call add(lines, '|1+"no comma"')
572 call add(lines, '|1,2,3,4,5,6,7') " too many items
573 call add(lines, '|1,"string version"')
574 call add(lines, '|1,>x') " bad continuation line
575 call add(lines, '|1,"x') " missing quote
576 call add(lines, '|1,"x\') " trailing backslash
577 call add(lines, '|1,,,,') "trailing comma
578 call add(lines, '|1,>234') " trailing continuation line
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100579 call writefile(lines, 'Xviminfo', 'D')
Bram Moolenaare80ff742016-06-11 21:14:18 +0200580 rviminfo Xviminfo
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +0200581
582 call delete('Xviminfo')
583endfunc
584
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200585func Test_viminfo_bad_syntax2()
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +0200586 let lines = []
587 call add(lines, '|1,4')
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200588
589 " bad viminfo syntax for history barline
590 call add(lines, '|2') " invalid number of fields in a history barline
591 call add(lines, '|2,9,1,1,"x"') " invalid value for the history type
592 call add(lines, '|2,0,,1,"x"') " no timestamp
593 call add(lines, '|2,0,1,1,10') " non-string text
594
595 " bad viminfo syntax for register barline
596 call add(lines, '|3') " invalid number of fields in a register barline
597 call add(lines, '|3,1,1,1,1,,1,"x"') " missing width field
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +0200598 call add(lines, '|3,0,80,1,1,1,1,"x"') " invalid register number
599 call add(lines, '|3,0,10,5,1,1,1,"x"') " invalid register type
600 call add(lines, '|3,0,10,1,20,1,1,"x"') " invalid line count
601 call add(lines, '|3,0,10,1,0,1,1') " zero line count
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200602
603 " bad viminfo syntax for mark barline
604 call add(lines, '|4') " invalid number of fields in a mark barline
605 call add(lines, '|4,1,1,1,1,1') " invalid value for file name
606 call add(lines, '|4,20,1,1,1,"x"') " invalid value for file name
607 call add(lines, '|4,49,0,1,1,"x"') " invalid value for line number
608
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100609 call writefile(lines, 'Xviminfo', 'D')
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +0200610 rviminfo Xviminfo
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +0200611endfunc
612
Pierre Colin0a076462023-08-19 11:56:57 +0200613" This used to crash Vim (GitHub issue #12652)
614func Test_viminfo_bad_syntax3()
615 let lines =<< trim END
616 call writefile([], 'Xvbs3.result')
617 qall!
618 END
619 call writefile(lines, 'Xvbs3script', 'D')
620
621 let lines = []
622 call add(lines, '|1,4')
623 " bad viminfo syntax for register barline
624 call add(lines, '|3,1,1,1,1,0,71489,,125') " empty line1
625 call writefile(lines, 'Xviminfo', 'D')
626
627 call RunVim([], [], '--clean -i Xviminfo -S Xvbs3script')
628 call assert_true(filereadable('Xvbs3.result'))
629
630 call delete('Xvbs3.result')
631endfunc
632
Bram Moolenaarab9c89b2016-07-03 17:47:26 +0200633func Test_viminfo_file_marks()
634 silent! bwipe test_viminfo.vim
635 silent! bwipe Xviminfo
636
637 call test_settime(10)
638 edit ten
639 call test_settime(25)
640 edit again
641 call test_settime(30)
642 edit thirty
643 wviminfo Xviminfo
644
645 call test_settime(20)
646 edit twenty
647 call test_settime(35)
648 edit again
649 call test_settime(40)
Dominique Pelle923dce22021-11-21 11:36:04 +0000650 edit forty
Bram Moolenaarab9c89b2016-07-03 17:47:26 +0200651 wviminfo Xviminfo
652
653 sp Xviminfo
654 1
Dominique Pelle923dce22021-11-21 11:36:04 +0000655 for name in ['forty', 'again', 'thirty', 'twenty', 'ten']
Bram Moolenaarab9c89b2016-07-03 17:47:26 +0200656 /^>
657 call assert_equal(name, substitute(getline('.'), '.*/', '', ''))
658 endfor
659 close
660
661 call delete('Xviminfo')
662endfunc
Bram Moolenaare59215c2016-08-14 19:08:45 +0200663
664func Test_viminfo_file_mark_tabclose()
665 tabnew Xtestfileintab
666 call setline(1, ['a','b','c','d','e'])
667 4
668 q!
669 wviminfo Xviminfo
670 sp Xviminfo
671 /^> .*Xtestfileintab
672 let lnum = line('.')
673 while 1
674 if lnum == line('$')
Bram Moolenaar37175402017-03-18 20:18:45 +0100675 call assert_report('mark not found in Xtestfileintab')
Bram Moolenaare59215c2016-08-14 19:08:45 +0200676 break
677 endif
678 let lnum += 1
679 let line = getline(lnum)
680 if line == ''
Bram Moolenaar37175402017-03-18 20:18:45 +0100681 call assert_report('mark not found in Xtestfileintab')
Bram Moolenaare59215c2016-08-14 19:08:45 +0200682 break
683 endif
684 if line =~ "^\t\""
685 call assert_equal('4', substitute(line, ".*\"\t\\(\\d\\).*", '\1', ''))
686 break
687 endif
688 endwhile
689
690 call delete('Xviminfo')
691 silent! bwipe Xtestfileintab
692endfunc
Bram Moolenaare11d61a2016-08-20 18:36:54 +0200693
Bram Moolenaar156919f2016-10-15 20:46:20 +0200694func Test_viminfo_file_mark_zero_time()
695 let lines = [
696 \ '# Viminfo version',
697 \ '|1,4',
698 \ '',
699 \ '*encoding=utf-8',
700 \ '',
701 \ '# File marks:',
702 \ "'B 1 0 /tmp/nothing",
703 \ '|4,66,1,0,0,"/tmp/nothing"',
704 \ "",
705 \ ]
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100706 call writefile(lines, 'Xviminfo', 'D')
Bram Moolenaar156919f2016-10-15 20:46:20 +0200707 delmark B
708 rviminfo Xviminfo
Bram Moolenaar156919f2016-10-15 20:46:20 +0200709 call assert_equal(1, line("'B"))
710 delmark B
711endfunc
712
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200713" Test for saving and restoring file marks in unloaded buffers
714func Test_viminfo_file_mark_unloaded_buf()
715 let save_viminfo = &viminfo
716 set viminfo&vim
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100717 call writefile(repeat(['vim'], 10), 'Xfile1', 'D')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200718 %bwipe
719 edit! Xfile1
720 call setpos("'u", [0, 3, 1, 0])
721 call setpos("'v", [0, 5, 1, 0])
722 enew
723 wviminfo Xviminfo
724 %bwipe
725 edit Xfile1
726 rviminfo! Xviminfo
727 call assert_equal([0, 3, 1, 0], getpos("'u"))
728 call assert_equal([0, 5, 1, 0], getpos("'v"))
729 %bwipe
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200730 call delete('Xviminfo')
731 let &viminfo = save_viminfo
732endfunc
733
Bram Moolenaar156919f2016-10-15 20:46:20 +0200734func Test_viminfo_oldfiles()
K.Takata0500e872022-09-08 12:28:02 +0100735 set noswapfile
Bram Moolenaare11d61a2016-08-20 18:36:54 +0200736 let v:oldfiles = []
737 let lines = [
738 \ '# comment line',
739 \ '*encoding=utf-8',
740 \ '',
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200741 \ ':h viminfo',
742 \ '?/session',
743 \ '=myvar',
744 \ '@123',
745 \ '',
746 \ "'E 2 0 /tmp/nothing",
747 \ '',
Bram Moolenaare11d61a2016-08-20 18:36:54 +0200748 \ "> /tmp/file_one.txt",
749 \ "\t\"\t11\t0",
750 \ "",
751 \ "> /tmp/file_two.txt",
752 \ "\t\"\t11\t0",
753 \ "",
754 \ "> /tmp/another.txt",
755 \ "\t\"\t11\t0",
756 \ "",
757 \ ]
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100758 call writefile(lines, 'Xviminfo', 'D')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200759 delmark E
Bram Moolenaar5affc032021-02-11 18:36:30 +0100760 edit /tmp/file_two.txt
Bram Moolenaare11d61a2016-08-20 18:36:54 +0200761 rviminfo! Xviminfo
Bram Moolenaare11d61a2016-08-20 18:36:54 +0200762
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200763 call assert_equal('h viminfo', histget(':'))
764 call assert_equal('session', histget('/'))
765 call assert_equal('myvar', histget('='))
766 call assert_equal('123', histget('@'))
767 call assert_equal(2, line("'E"))
Bram Moolenaar7b668e82016-08-23 23:51:21 +0200768 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/'}))
769 call assert_equal(['1: /tmp/file_one.txt', '2: /tmp/file_two.txt'], filter(split(execute('filter file_ oldfiles'), "\n"), {i, v -> v =~ '/tmp/'}))
770 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 +0200771
772 new
773 call feedkeys("3\<CR>", 't')
774 browse oldfiles
775 call assert_equal("/tmp/another.txt", expand("%"))
776 bwipe
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200777 delmark E
K.Takata0500e872022-09-08 12:28:02 +0100778 set swapfile&
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200779endfunc
780
781" Test for storing and restoring buffer list in 'viminfo'
782func Test_viminfo_bufferlist()
783 " If there are arguments, then :rviminfo doesn't read the buffer list.
784 " Need to delete all the arguments for :rviminfo to work.
785 %argdelete
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +0200786 set viminfo&vim
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200787
788 edit Xfile1
789 edit Xfile2
790 set viminfo-=%
791 wviminfo Xviminfo
792 %bwipe
793 rviminfo Xviminfo
794 call assert_equal(1, len(getbufinfo()))
795
796 edit Xfile1
797 edit Xfile2
798 set viminfo^=%
799 wviminfo Xviminfo
800 %bwipe
801 rviminfo Xviminfo
802 let l = getbufinfo()
803 call assert_equal(3, len(l))
804 call assert_equal('Xfile1', bufname(l[1].bufnr))
805 call assert_equal('Xfile2', bufname(l[2].bufnr))
806
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +0200807 " The quickfix, terminal, unlisted, unnamed buffers are not stored in the
808 " viminfo file
809 %bw!
810 edit Xfile1
811 new
812 setlocal nobuflisted
813 new
814 copen
815 if has('terminal')
816 terminal
817 endif
818 wviminfo! Xviminfo
819 %bwipe!
820 rviminfo Xviminfo
821 let l = getbufinfo()
822 call assert_equal(2, len(l))
823 call assert_true(bufexists('Xfile1'))
824
825 " If a count is specified for '%', then only that many buffers should be
826 " stored in the viminfo file.
827 %bw!
828 set viminfo&vim
829 new Xbuf1
830 new Xbuf2
831 set viminfo+=%1
832 wviminfo! Xviminfo
833 %bwipe!
834 rviminfo! Xviminfo
835 let l = getbufinfo()
836 call assert_equal(2, len(l))
837 call assert_true(bufexists('Xbuf1'))
838 call assert_false(bufexists('Xbuf2'))
839
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200840 call delete('Xviminfo')
841 %bwipe
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +0200842 set viminfo&vim
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200843endfunc
844
845" Test for errors in a viminfo file
846func Test_viminfo_error()
847 " Non-existing viminfo files
848 call assert_fails('rviminfo xyz', 'E195:')
849
850 " Illegal starting character
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100851 call writefile(["a 123"], 'Xviminfo', 'D')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200852 call assert_fails('rv Xviminfo', 'E575:')
853
854 " Illegal register name in the viminfo file
855 call writefile(['"@ LINE 0'], 'Xviminfo')
856 call assert_fails('rv Xviminfo', 'E577:')
857
858 " Invalid file mark line
859 call writefile(['>', '@'], 'Xviminfo')
860 call assert_fails('rv Xviminfo', 'E576:')
861
862 " Too many errors in viminfo file
863 call writefile(repeat(["a 123"], 15), 'Xviminfo')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200864 call assert_fails('rv Xviminfo', 'E575:')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200865
866 call writefile(['>'] + repeat(['@'], 10), 'Xviminfo')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200867 call assert_fails('rv Xviminfo', 'E576:')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200868
869 call writefile(repeat(['"@'], 15), 'Xviminfo')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200870 call assert_fails('rv Xviminfo', 'E577:')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200871endfunc
872
873" Test for saving and restoring last substitute string in viminfo
874func Test_viminfo_lastsub()
875 enew
876 call append(0, "blue blue blue")
877 call cursor(1, 1)
878 s/blue/green/
879 wviminfo Xviminfo
880 s/blue/yellow/
881 rviminfo! Xviminfo
882 &
883 call assert_equal("green yellow green", getline(1))
884 enew!
885 call delete('Xviminfo')
886endfunc
887
888" Test saving and restoring the register values using the older method
889func Test_viminfo_registers_old()
890 let lines = [
891 \ '# Viminfo version',
892 \ '|1,1',
893 \ '',
894 \ '*encoding=utf-8',
895 \ '',
896 \ '# Registers:',
897 \ '""0 CHAR 0',
898 \ ' Vim',
899 \ '"a CHAR 0',
900 \ ' red',
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +0200901 \ '"c BLOCK 0',
902 \ ' a',
903 \ ' d',
904 \ '"d LINE 0',
905 \ ' abc',
906 \ ' def',
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200907 \ '"m@ CHAR 0',
908 \ " :echo 'Hello'\<CR>",
909 \ "",
910 \ ]
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100911 call writefile(lines, 'Xviminfo', 'D')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200912 let @a = 'one'
913 let @b = 'two'
914 let @m = 'three'
915 let @" = 'four'
916 let @t = ":echo 'Unix'\<CR>"
917 silent! normal @t
918 rviminfo! Xviminfo
919 call assert_equal('red', getreg('a'))
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +0200920 call assert_equal("v", getregtype('a'))
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200921 call assert_equal('two', getreg('b'))
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +0200922 call assert_equal("a\nd", getreg('c'))
923 call assert_equal("\<C-V>1", getregtype('c'))
924 call assert_equal("abc\ndef\n", getreg('d'))
925 call assert_equal("V", getregtype('d'))
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200926 call assert_equal(":echo 'Hello'\<CR>", getreg('m'))
927 call assert_equal('Vim', getreg('"'))
928 call assert_equal("\nHello", execute('normal @@'))
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100929
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200930 let @" = ''
931endfunc
932
933" Test for saving and restoring large number of lines in a register
934func Test_viminfo_large_register()
935 let save_viminfo = &viminfo
936 set viminfo&vim
937 set viminfo-=<50
938 set viminfo+=<200
939 let lines = ['"r CHAR 0']
940 call extend(lines, repeat(["\tsun is rising"], 200))
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100941 call writefile(lines, 'Xviminfo', 'D')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200942 let @r = ''
943 rviminfo! Xviminfo
944 call assert_equal(join(repeat(["sun is rising"], 200), "\n"), @r)
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100945
Bram Moolenaar6bd1d772019-10-09 22:01:25 +0200946 let @r = ''
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200947 let &viminfo = save_viminfo
948endfunc
949
950" Test for setting 'viminfofile' to NONE
951func Test_viminfofile_none()
Bram Moolenaar6bd1d772019-10-09 22:01:25 +0200952 let save_vif = &viminfofile
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200953 set viminfofile=NONE
954 wviminfo Xviminfo
955 call assert_false(filereadable('Xviminfo'))
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100956 call writefile([''], 'Xviminfo', 'D')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200957 call assert_fails('rviminfo Xviminfo', 'E195:')
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100958
Bram Moolenaar6bd1d772019-10-09 22:01:25 +0200959 let &viminfofile = save_vif
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200960endfunc
961
Dominique Pelle923dce22021-11-21 11:36:04 +0000962" Test for an unwritable and unreadable 'viminfo' file
Bram Moolenaar6bd1d772019-10-09 22:01:25 +0200963func Test_viminfo_perm()
964 CheckUnix
Bram Moolenaar07282f02019-10-10 16:46:17 +0200965 CheckNotRoot
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100966 call writefile([''], 'Xviminfo', 'D')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200967 call setfperm('Xviminfo', 'r-x------')
968 call assert_fails('wviminfo Xviminfo', 'E137:')
Bram Moolenaar6bd1d772019-10-09 22:01:25 +0200969 call setfperm('Xviminfo', '--x------')
970 call assert_fails('rviminfo Xviminfo', 'E195:')
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200971
972 " Try to write the viminfo to a directory
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100973 call mkdir('Xvifdir', 'R')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100974 call assert_fails('wviminfo Xvifdir', 'E137:')
975 call assert_fails('rviminfo Xvifdir', 'E195:')
Bram Moolenaare11d61a2016-08-20 18:36:54 +0200976endfunc
Bram Moolenaar6bd1d772019-10-09 22:01:25 +0200977
978" Test for writing to an existing viminfo file merges the file marks
979func XTest_viminfo_marks_merge()
980 let save_viminfo = &viminfo
981 set viminfo&vim
982 set viminfo^=%
983 enew
984 %argdelete
985 %bwipe
986
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100987 call writefile(repeat(['editor'], 10), 'Xbufa', 'D')
988 call writefile(repeat(['Vim'], 10), 'Xbufb', 'D')
Bram Moolenaar6bd1d772019-10-09 22:01:25 +0200989
990 " set marks in buffers
991 call test_settime(10)
992 edit Xbufa
993 4mark a
994 wviminfo Xviminfo
995 edit Xbufb
996 4mark b
997 wviminfo Xviminfo
998 %bwipe
999
1000 " set marks in buffers again
1001 call test_settime(20)
1002 edit Xbufb
1003 6mark b
1004 wviminfo Xviminfo
1005 edit Xbufa
1006 6mark a
1007 wviminfo Xviminfo
1008 %bwipe
1009
1010 " Load the buffer and check the marks
1011 edit Xbufa
1012 rviminfo! Xviminfo
1013 call assert_equal(6, line("'a"))
1014 edit Xbufb
1015 rviminfo! Xviminfo
1016 call assert_equal(6, line("'b"))
1017
1018 " cleanup
1019 %bwipe
1020 call delete('Xviminfo')
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02001021 call test_settime(0)
1022 let &viminfo=save_viminfo
1023endfunc
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001024
1025" Test for errors in setting 'viminfo'
1026func Test_viminfo_option_error()
1027 " Missing number
1028 call assert_fails('set viminfo=\"', 'E526:')
1029 for c in split("'/:<@s", '\zs')
1030 call assert_fails('set viminfo=' .. c, 'E526:')
1031 endfor
1032
1033 " Missing comma
1034 call assert_fails('set viminfo=%10!', 'E527:')
1035 call assert_fails('set viminfo=!%10', 'E527:')
1036 call assert_fails('set viminfo=h%10', 'E527:')
1037 call assert_fails('set viminfo=c%10', 'E527:')
1038 call assert_fails('set viminfo=:10%10', 'E527:')
1039
1040 " Missing ' setting
1041 call assert_fails('set viminfo=%10', 'E528:')
1042endfunc
1043
Bram Moolenaar8e6be342020-11-23 22:01:26 +01001044func Test_viminfo_oldfiles_newfile()
1045 CheckRunVimInTerminal
1046
1047 let save_viminfo = &viminfo
1048 let save_viminfofile = &viminfofile
1049 set viminfo&vim
1050 let v:oldfiles = []
1051 let commands =<< trim [CODE]
1052 set viminfofile=Xviminfofile
1053 set viminfo&vim
1054 w! Xnew-file.txt
1055 qall
1056 [CODE]
Bram Moolenaar5b148ef2022-10-15 21:35:56 +01001057 call writefile(commands, 'Xviminfotest', 'D')
Bram Moolenaar8e6be342020-11-23 22:01:26 +01001058 let buf = RunVimInTerminal('-S Xviminfotest', #{wait_for_ruler: 0})
1059 call WaitForAssert({-> assert_equal("finished", term_getstatus(buf))})
1060
1061 let &viminfofile = 'Xviminfofile'
1062 rviminfo! Xviminfofile
1063 call assert_match('Xnew-file.txt$', v:oldfiles[0])
1064 call assert_equal(1, len(v:oldfiles))
Bram Moolenaar5b148ef2022-10-15 21:35:56 +01001065
Bram Moolenaar8e6be342020-11-23 22:01:26 +01001066 call delete('Xviminfofile')
Bram Moolenaar8e6be342020-11-23 22:01:26 +01001067 call delete('Xnew-file.txt')
Bram Moolenaar6fd367a2021-03-13 13:14:04 +01001068
1069 let v:oldfiles = test_null_list()
1070 call assert_equal("\nNo old files", execute('oldfiles'))
1071
Bram Moolenaar8e6be342020-11-23 22:01:26 +01001072 let &viminfo = save_viminfo
1073 let &viminfofile = save_viminfofile
1074endfunc
1075
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +02001076" When writing CTRL-V or "\n" to a viminfo file, it is converted to CTRL-V
1077" CTRL-V and CTRL-V n respectively.
1078func Test_viminfo_with_Ctrl_V()
1079 silent! exe "normal! /\<C-V>\<C-V>\n"
1080 wviminfo Xviminfo
1081 call assert_notequal(-1, readfile('Xviminfo')->index("?/\<C-V>\<C-V>"))
1082 let @/ = 'abc'
1083 rviminfo! Xviminfo
1084 call assert_equal("\<C-V>", @/)
1085 silent! exe "normal! /\<C-V>\<C-J>\n"
1086 wviminfo Xviminfo
1087 call assert_notequal(-1, readfile('Xviminfo')->index("?/\<C-V>n"))
1088 let @/ = 'abc'
1089 rviminfo! Xviminfo
1090 call assert_equal("\n", @/)
1091 call delete('Xviminfo')
1092endfunc
1093
1094" Test for the 'r' field in 'viminfo' (removal media)
1095func Test_viminfo_removable_media()
1096 CheckUnix
1097 if !isdirectory('/tmp') || getftype('/tmp') != 'dir'
1098 return
1099 endif
1100 let save_viminfo = &viminfo
1101 set viminfo+=r/tmp
1102 edit /tmp/Xvima1b2c3
1103 wviminfo Xviminfo
1104 let matches = readfile('Xviminfo')->filter("v:val =~ 'Xvima1b2c3'")
1105 call assert_equal(0, matches->len())
1106 let &viminfo = save_viminfo
1107 call delete('Xviminfo')
1108endfunc
1109
1110" Test for the 'h' flag in 'viminfo'. If 'h' is not present, then the last
1111" search pattern read from 'viminfo' should be highlighted with 'hlsearch'.
1112" If 'h' is present, then the last search pattern should not be highlighted.
1113func Test_viminfo_hlsearch()
1114 set viminfo&vim
1115
1116 new
1117 call setline(1, ['one two three'])
1118 " save the screen attribute for the Search highlighted text and the normal
1119 " text for later comparison
1120 set hlsearch
1121 let @/ = 'three'
1122 redraw!
1123 let hiSearch = screenattr(1, 9)
1124 let hiNormal = screenattr(1, 1)
1125
1126 set viminfo-=h
1127 let @/='two'
1128 wviminfo! Xviminfo
1129 let @/='one'
1130 rviminfo! Xviminfo
1131 redraw!
1132 call assert_equal(hiSearch, screenattr(1, 5))
1133 call assert_equal(hiSearch, screenattr(1, 6))
1134 call assert_equal(hiSearch, screenattr(1, 7))
1135
1136 set viminfo+=h
1137 let @/='two'
1138 wviminfo! Xviminfo
1139 let @/='one'
1140 rviminfo! Xviminfo
1141 redraw!
1142 call assert_equal(hiNormal, screenattr(1, 5))
1143 call assert_equal(hiNormal, screenattr(1, 6))
1144 call assert_equal(hiNormal, screenattr(1, 7))
1145
1146 call delete('Xviminfo')
1147 set hlsearch& viminfo&vim
1148 bw!
1149endfunc
1150
1151" Test for restoring the magicness of the last search pattern from the viminfo
1152" file.
1153func Test_viminfo_last_spat_magic()
1154 set viminfo&vim
1155 new
1156 call setline(1, ' one abc a.c')
1157
1158 " restore 'nomagic'
1159 set nomagic
1160 exe "normal gg/a.c\<CR>"
1161 wviminfo! Xviminfo
1162 set magic
1163 exe "normal gg/one\<CR>"
1164 rviminfo! Xviminfo
1165 exe "normal! gg/\<CR>"
1166 call assert_equal(10, col('.'))
1167
1168 " restore 'magic'
1169 set magic
1170 exe "normal gg/a.c\<CR>"
1171 wviminfo! Xviminfo
1172 set nomagic
1173 exe "normal gg/one\<CR>"
1174 rviminfo! Xviminfo
1175 exe "normal! gg/\<CR>"
1176 call assert_equal(6, col('.'))
1177
1178 call delete('Xviminfo')
1179 set viminfo&vim magic&
1180 bw!
1181endfunc
1182
1183" Test for restoring the smartcase of the last search pattern from the viminfo
1184" file.
1185func Test_viminfo_last_spat_smartcase()
1186 new
1187 call setline(1, ' one abc Abc')
1188 set ignorecase smartcase
1189
1190 " Searching with * should disable smartcase
1191 exe "normal! gg$b*"
1192 wviminfo! Xviminfo
1193 exe "normal gg/one\<CR>"
1194 rviminfo! Xviminfo
1195 exe "normal! gg/\<CR>"
1196 call assert_equal(6, col('.'))
1197
1198 call delete('Xviminfo')
1199 set ignorecase& smartcase& viminfo&
1200 bw!
1201endfunc
1202
1203" Test for restoring the last search pattern with a line or character offset
1204" from the viminfo file.
1205func Test_viminfo_last_spat_offset()
1206 new
1207 call setline(1, ['one', 'two', 'three', 'four', 'five'])
1208 " line offset
1209 exe "normal! /two/+2\<CR>"
1210 wviminfo! Xviminfo
1211 exe "normal gg/five\<CR>"
1212 rviminfo! Xviminfo
1213 exe "normal! gg/\<CR>"
1214 call assert_equal(4, line('.'))
1215 " character offset
1216 exe "normal! gg/^th/e+2\<CR>"
1217 wviminfo! Xviminfo
1218 exe "normal gg/two\<CR>"
1219 rviminfo! Xviminfo
1220 exe "normal! gg/\<CR>"
1221 call assert_equal([3, 4], [line('.'), col('.')])
1222 call delete('Xviminfo')
1223 bw!
1224endfunc
1225
1226" Test for saving and restoring the last executed register (@ command)
1227" from the viminfo file
1228func Test_viminfo_last_exec_reg()
1229 let g:val = 1
1230 let @a = ":let g:val += 1\n"
1231 normal! @a
1232 wviminfo! Xviminfo
1233 let @b = ''
1234 normal! @b
1235 rviminfo! Xviminfo
1236 normal @@
1237 call assert_equal(3, g:val)
1238 call delete('Xviminfo')
1239endfunc
1240
1241" Test for merging file marks in a viminfo file
1242func Test_viminfo_merge_file_marks()
1243 for [f, l, t] in [['a.txt', 5, 10], ['b.txt', 10, 20]]
1244 call test_settime(t)
1245 exe 'edit ' .. f
1246 call setline(1, range(1, 20))
1247 exe l . 'mark a'
1248 wviminfo Xviminfo
1249 bw!
1250 endfor
1251 call test_settime(30)
1252 for [f, l] in [['a.txt', 5], ['b.txt', 10]]
1253 exe 'edit ' .. f
1254 rviminfo! Xviminfo
1255 call assert_equal(l, line("'a"))
1256 bw!
1257 endfor
1258 call delete('Xviminfo')
1259 call test_settime(0)
1260endfunc
1261
1262" Test for merging file marks from a old viminfo file
1263func Test_viminfo_merge_old_filemarks()
1264 let lines = []
1265 call add(lines, '|1,4')
1266 call add(lines, '> ' .. fnamemodify('a.txt', ':p:~'))
1267 call add(lines, "\tb\t7\t0\n")
Bram Moolenaar5b148ef2022-10-15 21:35:56 +01001268 call writefile(lines, 'Xviminfo', 'D')
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +02001269 edit b.txt
1270 call setline(1, range(1, 20))
1271 12mark b
1272 wviminfo Xviminfo
1273 bw!
1274 edit a.txt
1275 rviminfo! Xviminfo
1276 call assert_equal(7, line("'b"))
1277 edit b.txt
1278 rviminfo! Xviminfo
1279 call assert_equal(12, line("'b"))
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +02001280endfunc
1281
1282" Test for merging the jump list from a old viminfo file
1283func Test_viminfo_merge_old_jumplist()
1284 let lines = []
1285 call add(lines, "-' 10 1 " .. fnamemodify('a.txt', ':p:~'))
1286 call add(lines, "-' 20 1 " .. fnamemodify('a.txt', ':p:~'))
1287 call add(lines, "-' 30 1 " .. fnamemodify('b.txt', ':p:~'))
1288 call add(lines, "-' 40 1 " .. fnamemodify('b.txt', ':p:~'))
Bram Moolenaar5b148ef2022-10-15 21:35:56 +01001289 call writefile(lines, 'Xviminfo', 'D')
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +02001290 clearjumps
1291 rviminfo! Xviminfo
1292 let l = getjumplist()[0]
1293 call assert_equal([40, 30, 20, 10], [l[0].lnum, l[1].lnum, l[2].lnum,
1294 \ l[3].lnum])
1295 bw!
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +02001296endfunc
1297
Christian Brabandt0b0f7d62024-05-19 09:11:09 +02001298func Test_viminfo_oldfiles_filter()
1299 let v:oldfiles = []
1300 let _viminfofile = &viminfofile
1301 let &viminfofile=''
1302 let lines = [
1303 \ '# comment line',
1304 \ '*encoding=utf-8',
1305 \ "> /tmp/vimrc_one.vim",
1306 \ "\t\"\t11\t0",
1307 \ "",
1308 \ "> /tmp/foobar.txt",
1309 \ "\t\"\t11\t0",
1310 \ "",
1311 \ ]
1312 call writefile(lines, 'Xviminfo1', 'D')
1313 rviminfo! Xviminfo1
1314 new
1315 " filter returns a single item
1316 let a = execute('filter /vim/ oldfiles')->split('\n')
1317 call assert_equal(1, len(a))
1318 " filter returns more than a single match
1319 let a = execute('filter #tmp# oldfiles')->split('\n')
1320 call assert_equal(2, len(a))
1321 " don't get prompted for the file, but directly open it
1322 filter /vim/ browse oldfiles
1323 call assert_equal("/tmp/vimrc_one.vim", expand("%"))
1324 bw
1325 let &viminfofile = _viminfofile
1326endfunc
1327
John Marriott10f69292025-04-14 21:19:34 +02001328func Test_viminfo_global_var()
1329 let _viminfofile = &viminfofile
1330 let _viminfo = &viminfo
1331 let &viminfofile=''
1332 set viminfo+=!
1333 let lines = [
1334 \ '# comment line',
1335 \ "",
1336 \ '# Viminfo version',
1337 \ '|1,4',
1338 \ "",
1339 \ '*encoding=utf-8',
1340 \ "",
1341 \ '# global variables:',
1342 \ "!VAL\tFLO\t-in",
1343 \ "!VAR\tFLO\t-inf",
1344 \ "",
1345 \ ]
1346 call writefile(lines, 'Xviminfo2', 'D')
1347 rviminfo! Xviminfo2
1348 call assert_equal(0.0, g:VAL)
1349 call assert_equal(str2float("-inf"), g:VAR)
1350 let &viminfofile = _viminfofile
1351 let &viminfo = _viminfo
1352endfunc
1353
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001354" vim: shiftwidth=2 sts=2 expandtab