blob: 7aab271336538c3833a8fceeb2d885de49e52c4d [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
Bram Moolenaar8e6be342020-11-23 22:01:26 +01004source term_util.vim
5source shared.vim
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02006
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +02007func Test_viminfo_read_and_write()
Bram Moolenaar26b654a2019-07-22 20:50:17 +02008 " First clear 'history', so that "hislen" is zero. Then set it again,
9 " simulating Vim starting up.
10 set history=0
11 wviminfo Xviminfo
12 set history=1000
13
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020014 call histdel(':')
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +020015 let @/=''
Bram Moolenaarb20e3342016-01-18 23:29:01 +010016 let lines = [
17 \ '# comment line',
18 \ '*encoding=utf-8',
19 \ '~MSle0~/asdf',
20 \ '|copied as-is',
21 \ '|and one more',
22 \ ]
Bram Moolenaar5b148ef2022-10-15 21:35:56 +010023 call writefile(lines, 'Xviminfo', 'D')
Bram Moolenaarb20e3342016-01-18 23:29:01 +010024 rviminfo Xviminfo
25 call assert_equal('asdf', @/)
26
27 wviminfo Xviminfo
28 let lines = readfile('Xviminfo')
29 let done = 0
30 for line in lines
Bram Moolenaar156919f2016-10-15 20:46:20 +020031 if line[0] == '|' && line !~ '^|[234],' && line !~ '^|<'
Bram Moolenaarb20e3342016-01-18 23:29:01 +010032 if done == 0
Bram Moolenaar2d358992016-06-12 21:20:54 +020033 call assert_equal('|1,4', line)
Bram Moolenaarb20e3342016-01-18 23:29:01 +010034 elseif done == 1
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020035 call assert_equal('|copied as-is', line)
36 elseif done == 2
Bram Moolenaarb20e3342016-01-18 23:29:01 +010037 call assert_equal('|and one more', line)
38 endif
39 let done += 1
40 endif
41 endfor
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020042 call assert_equal(3, done)
Bram Moolenaarb20e3342016-01-18 23:29:01 +010043endfunc
44
45func Test_global_vars()
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +020046 let g:MY_GLOBAL_STRING = "Vim Editor"
47 let g:MY_GLOBAL_NUM = 345
48 let g:MY_GLOBAL_FLOAT = 3.14
Bram Moolenaarb20e3342016-01-18 23:29:01 +010049 let test_dict = {'foo': 1, 'bar': 0, 'longvarible': 1000}
50 let g:MY_GLOBAL_DICT = test_dict
51 " store a really long list, so line wrapping will occur in viminfo file
52 let test_list = range(1,100)
53 let g:MY_GLOBAL_LIST = test_list
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +010054 let test_blob = 0z00112233445566778899aabbccddeeff
55 let g:MY_GLOBAL_BLOB = test_blob
56 let test_false = v:false
57 let g:MY_GLOBAL_FALSE = test_false
58 let test_true = v:true
59 let g:MY_GLOBAL_TRUE = test_true
60 let test_null = v:null
61 let g:MY_GLOBAL_NULL = test_null
62 let test_none = v:none
63 let g:MY_GLOBAL_NONE = test_none
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +020064 let g:MY_GLOBAL_FUNCREF = function('min')
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +010065
Bram Moolenaare9c07272016-03-30 20:50:46 +020066 set viminfo='100,<50,s10,h,!,nviminfo
Bram Moolenaarb20e3342016-01-18 23:29:01 +010067 wv! Xviminfo
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +010068
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +020069 unlet g:MY_GLOBAL_STRING
70 unlet g:MY_GLOBAL_NUM
71 unlet g:MY_GLOBAL_FLOAT
Bram Moolenaarb20e3342016-01-18 23:29:01 +010072 unlet g:MY_GLOBAL_DICT
73 unlet g:MY_GLOBAL_LIST
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +010074 unlet g:MY_GLOBAL_BLOB
75 unlet g:MY_GLOBAL_FALSE
76 unlet g:MY_GLOBAL_TRUE
77 unlet g:MY_GLOBAL_NULL
78 unlet g:MY_GLOBAL_NONE
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +020079 unlet g:MY_GLOBAL_FUNCREF
Bram Moolenaarb20e3342016-01-18 23:29:01 +010080
81 rv! Xviminfo
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +020082 call assert_equal("Vim Editor", g:MY_GLOBAL_STRING)
83 call assert_equal(345, g:MY_GLOBAL_NUM)
84 call assert_equal(3.14, g:MY_GLOBAL_FLOAT)
Bram Moolenaarb20e3342016-01-18 23:29:01 +010085 call assert_equal(test_dict, g:MY_GLOBAL_DICT)
86 call assert_equal(test_list, g:MY_GLOBAL_LIST)
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +010087 call assert_equal(test_blob, g:MY_GLOBAL_BLOB)
88 call assert_equal(test_false, g:MY_GLOBAL_FALSE)
89 call assert_equal(test_true, g:MY_GLOBAL_TRUE)
90 call assert_equal(test_null, g:MY_GLOBAL_NULL)
91 call assert_equal(test_none, g:MY_GLOBAL_NONE)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +020092 call assert_false(exists("g:MY_GLOBAL_FUNCREF"))
Bram Moolenaarb20e3342016-01-18 23:29:01 +010093
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +020094 " When reading global variables from viminfo, if a variable cannot be
95 " modified, then the value should not be changed.
96 unlet g:MY_GLOBAL_STRING
97 unlet g:MY_GLOBAL_NUM
98 unlet g:MY_GLOBAL_FLOAT
99 unlet g:MY_GLOBAL_DICT
100 unlet g:MY_GLOBAL_LIST
101 unlet g:MY_GLOBAL_BLOB
102
103 const g:MY_GLOBAL_STRING = 'New Value'
104 const g:MY_GLOBAL_NUM = 987
105 const g:MY_GLOBAL_FLOAT = 1.16
106 const g:MY_GLOBAL_DICT = {'editor': 'vim'}
107 const g:MY_GLOBAL_LIST = [5, 7, 13]
108 const g:MY_GLOBAL_BLOB = 0zDEADBEEF
109 call assert_fails('rv! Xviminfo', 'E741:')
110 call assert_equal('New Value', g:MY_GLOBAL_STRING)
111 call assert_equal(987, g:MY_GLOBAL_NUM)
112 call assert_equal(1.16, g:MY_GLOBAL_FLOAT)
113 call assert_equal({'editor': 'vim'}, g:MY_GLOBAL_DICT)
114 call assert_equal([5, 7 , 13], g:MY_GLOBAL_LIST)
115 call assert_equal(0zDEADBEEF, g:MY_GLOBAL_BLOB)
116
117 unlet g:MY_GLOBAL_STRING
118 unlet g:MY_GLOBAL_NUM
119 unlet g:MY_GLOBAL_FLOAT
120 unlet g:MY_GLOBAL_DICT
121 unlet g:MY_GLOBAL_LIST
122 unlet g:MY_GLOBAL_BLOB
123
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200124 " Test for invalid values for a blob, list, dict in a viminfo file
125 call writefile([
126 \ "!GLOB_BLOB_1\tBLO\t123",
127 \ "!GLOB_BLOB_2\tBLO\t012",
128 \ "!GLOB_BLOB_3\tBLO\t0z1x",
129 \ "!GLOB_BLOB_4\tBLO\t0z12 ab",
130 \ "!GLOB_LIST_1\tLIS\t1 2",
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100131 \ "!GLOB_DICT_1\tDIC\t1 2"], 'Xviminfo', 'D')
Bram Moolenaarfae55a92021-06-17 22:08:30 +0200132 call assert_fails('rv! Xviminfo', 'E488:')
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200133 call assert_equal('123', g:GLOB_BLOB_1)
134 call assert_equal(1, type(g:GLOB_BLOB_1))
135 call assert_equal('012', g:GLOB_BLOB_2)
136 call assert_equal(1, type(g:GLOB_BLOB_2))
137 call assert_equal('0z1x', g:GLOB_BLOB_3)
138 call assert_equal(1, type(g:GLOB_BLOB_3))
139 call assert_equal('0z12 ab', g:GLOB_BLOB_4)
140 call assert_equal(1, type(g:GLOB_BLOB_4))
141 call assert_equal('1 2', g:GLOB_LIST_1)
142 call assert_equal(1, type(g:GLOB_LIST_1))
143 call assert_equal('1 2', g:GLOB_DICT_1)
144 call assert_equal(1, type(g:GLOB_DICT_1))
145
Bram Moolenaarb20e3342016-01-18 23:29:01 +0100146 set viminfo-=!
147endfunc
Bram Moolenaar45d2eea2016-06-06 21:07:52 +0200148
Bram Moolenaar5b157fe2020-06-07 16:08:08 +0200149func Test_global_vars_with_circular_reference()
150 let g:MY_GLOBAL_LIST = []
151 call add(g:MY_GLOBAL_LIST, g:MY_GLOBAL_LIST)
152 let g:MY_GLOBAL_DICT = {}
153 let g:MY_GLOBAL_DICT['self'] = g:MY_GLOBAL_DICT
154
155 set viminfo='100,<50,s10,h,!,nviminfo
156 wv! Xviminfo
157 call assert_equal(v:errmsg, '')
158
159 unlet g:MY_GLOBAL_LIST
160 unlet g:MY_GLOBAL_DICT
161
162 rv! Xviminfo
163 call assert_equal(v:errmsg, '')
164 call assert_true(!exists('g:MY_GLOBAL_LIST'))
165 call assert_true(!exists('g:MY_GLOBAL_DICT'))
166
167 call delete('Xviminfo')
168 set viminfo-=!
169endfunc
170
Bram Moolenaar45d2eea2016-06-06 21:07:52 +0200171func Test_cmdline_history()
172 call histdel(':')
173 call test_settime(11)
174 call histadd(':', "echo 'one'")
175 call test_settime(12)
176 " split into two lines
177 let long800 = repeat(" 'eight'", 100)
178 call histadd(':', "echo " . long800)
179 call test_settime(13)
180 " split into three lines
181 let long1400 = repeat(" 'fourteeeeen'", 100)
182 call histadd(':', "echo " . long1400)
183 wviminfo Xviminfo
184 let lines = readfile('Xviminfo')
185 let done_colon = 0
186 let done_bar = 0
187 let lnum = 0
188 while lnum < len(lines)
189 let line = lines[lnum] | let lnum += 1
190 if line[0] == ':'
191 if done_colon == 0
192 call assert_equal(":\x161408", line)
193 let line = lines[lnum] | let lnum += 1
194 call assert_equal('<echo ' . long1400, line)
195 elseif done_colon == 1
196 call assert_equal(":\x16808", line)
197 let line = lines[lnum] | let lnum += 1
198 call assert_equal("<echo " . long800, line)
199 elseif done_colon == 2
200 call assert_equal(":echo 'one'", line)
201 endif
202 let done_colon += 1
203 elseif line[0:4] == '|2,0,'
204 if done_bar == 0
205 call assert_equal("|2,0,13,,>1407", line)
206 let line = lines[lnum] | let lnum += 1
207 call assert_equal('|<"echo ' . long1400[0:484], line)
208 let line = lines[lnum] | let lnum += 1
209 call assert_equal('|<' . long1400[485:974], line)
210 let line = lines[lnum] | let lnum += 1
211 call assert_equal('|<' . long1400[975:] . '"', line)
212 elseif done_bar == 1
213 call assert_equal('|2,0,12,,>807', line)
214 let line = lines[lnum] | let lnum += 1
215 call assert_equal('|<"echo ' . long800[0:484], line)
216 let line = lines[lnum] | let lnum += 1
217 call assert_equal('|<' . long800[485:] . '"', line)
218 elseif done_bar == 2
219 call assert_equal("|2,0,11,,\"echo 'one'\"", line)
220 endif
221 let done_bar += 1
222 endif
223 endwhile
224 call assert_equal(3, done_colon)
225 call assert_equal(3, done_bar)
226
227 call histdel(':')
228 rviminfo Xviminfo
229 call assert_equal("echo " . long1400, histget(':', -1))
230 call assert_equal("echo " . long800, histget(':', -2))
231 call assert_equal("echo 'one'", histget(':', -3))
232
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200233 " If the value for the '/' or ':' or '@' field in 'viminfo' is zero, then
234 " the corresponding history entries are not saved.
235 set viminfo='100,/0,:0,@0,<50,s10,h,!,nviminfo
236 call histdel('/')
237 call histdel(':')
238 call histdel('@')
239 call histadd('/', 'foo')
240 call histadd(':', 'bar')
241 call histadd('@', 'baz')
242 wviminfo! Xviminfo
243 call histdel('/')
244 call histdel(':')
245 call histdel('@')
246 rviminfo! Xviminfo
247 call assert_equal('', histget('/'))
248 call assert_equal('', histget(':'))
249 call assert_equal('', histget('@'))
250
Bram Moolenaar45d2eea2016-06-06 21:07:52 +0200251 call delete('Xviminfo')
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200252 set viminfo&vim
Bram Moolenaar45d2eea2016-06-06 21:07:52 +0200253endfunc
Bram Moolenaar1fd99c12016-06-09 20:24:28 +0200254
255func Test_cmdline_history_order()
256 call histdel(':')
257 call test_settime(11)
258 call histadd(':', "echo '11'")
259 call test_settime(22)
260 call histadd(':', "echo '22'")
261 call test_settime(33)
262 call histadd(':', "echo '33'")
263 wviminfo Xviminfo
264
265 call histdel(':')
266 " items go in between
267 call test_settime(15)
268 call histadd(':', "echo '15'")
269 call test_settime(27)
270 call histadd(':', "echo '27'")
271
272 rviminfo Xviminfo
273 call assert_equal("echo '33'", histget(':', -1))
274 call assert_equal("echo '27'", histget(':', -2))
275 call assert_equal("echo '22'", histget(':', -3))
276 call assert_equal("echo '15'", histget(':', -4))
277 call assert_equal("echo '11'", histget(':', -5))
278
279 call histdel(':')
280 " items go before and after
Bram Moolenaarce90e362019-09-08 18:58:44 +0200281 eval 8->test_settime()
Bram Moolenaar1fd99c12016-06-09 20:24:28 +0200282 call histadd(':', "echo '8'")
283 call test_settime(39)
284 call histadd(':', "echo '39'")
285
286 rviminfo Xviminfo
287 call assert_equal("echo '39'", histget(':', -1))
288 call assert_equal("echo '33'", histget(':', -2))
289 call assert_equal("echo '22'", histget(':', -3))
290 call assert_equal("echo '11'", histget(':', -4))
291 call assert_equal("echo '8'", histget(':', -5))
292
293 " Check sorting works when writing with merge.
294 call histdel(':')
295 call test_settime(8)
296 call histadd(':', "echo '8'")
297 call test_settime(15)
298 call histadd(':', "echo '15'")
299 call test_settime(27)
300 call histadd(':', "echo '27'")
301 call test_settime(39)
302 call histadd(':', "echo '39'")
303 wviminfo Xviminfo
Bram Moolenaar94722c52023-01-28 19:19:03 +0000304
Bram Moolenaar1fd99c12016-06-09 20:24:28 +0200305 call histdel(':')
306 rviminfo Xviminfo
307 call assert_equal("echo '39'", histget(':', -1))
308 call assert_equal("echo '33'", histget(':', -2))
309 call assert_equal("echo '27'", histget(':', -3))
310 call assert_equal("echo '22'", histget(':', -4))
311 call assert_equal("echo '15'", histget(':', -5))
312 call assert_equal("echo '11'", histget(':', -6))
313 call assert_equal("echo '8'", histget(':', -7))
314
315 call delete('Xviminfo')
316endfunc
Bram Moolenaar01227092016-06-11 14:47:40 +0200317
Bram Moolenaare80ff742016-06-11 21:14:18 +0200318func Test_viminfo_registers()
319 call test_settime(8)
320 call setreg('a', "eight", 'c')
321 call test_settime(20)
322 call setreg('b', ["twenty", "again"], 'l')
323 call test_settime(40)
324 call setreg('c', ["four", "agai"], 'b4')
325 let l = []
326 set viminfo='100,<600,s10,h,!,nviminfo
327 for i in range(500)
328 call add(l, 'something')
329 endfor
330 call setreg('d', l, 'l')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200331 call setreg('e', "abc\<C-V>xyz")
Bram Moolenaare80ff742016-06-11 21:14:18 +0200332 wviminfo Xviminfo
333
334 call test_settime(10)
335 call setreg('a', '', 'b10')
336 call test_settime(15)
337 call setreg('b', 'drop')
338 call test_settime(50)
339 call setreg('c', 'keep', 'l')
340 call test_settime(30)
341 call setreg('d', 'drop', 'l')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200342 call setreg('e', 'drop')
Bram Moolenaare80ff742016-06-11 21:14:18 +0200343 rviminfo Xviminfo
344
345 call assert_equal("", getreg('a'))
346 call assert_equal("\<C-V>10", getregtype('a'))
347 call assert_equal("twenty\nagain\n", getreg('b'))
348 call assert_equal("V", getregtype('b'))
349 call assert_equal("keep\n", getreg('c'))
350 call assert_equal("V", getregtype('c'))
351 call assert_equal(l, getreg('d', 1, 1))
352 call assert_equal("V", getregtype('d'))
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200353 call assert_equal("abc\<C-V>xyz", getreg('e'))
Bram Moolenaare80ff742016-06-11 21:14:18 +0200354
Bram Moolenaar25709572016-08-26 20:41:16 +0200355 " Length around 440 switches to line continuation.
356 let len = 434
357 while len < 445
358 let s = repeat('a', len)
359 call setreg('"', s)
360 wviminfo Xviminfo
361 call setreg('"', '')
362 rviminfo Xviminfo
363 call assert_equal(s, getreg('"'), 'wrong register at length: ' . len)
364
365 let len += 1
366 endwhile
367
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200368 " If the maximum number of lines saved for a register ('<' in 'viminfo') is
369 " zero, then register values should not be saved.
370 let @a = 'abc'
371 set viminfo='100,<0,s10,h,!,nviminfo
372 wviminfo Xviminfo
373 let @a = 'xyz'
374 rviminfo! Xviminfo
375 call assert_equal('xyz', @a)
376 " repeat the test with '"' instead of '<'
377 let @b = 'def'
378 set viminfo='100,\"0,s10,h,!,nviminfo
379 wviminfo Xviminfo
380 let @b = 'rst'
381 rviminfo! Xviminfo
382 call assert_equal('rst', @b)
383
384 " If the maximum size of an item ('s' in 'viminfo') is zero, then register
385 " values should not be saved.
386 let @c = '123'
387 set viminfo='100,<20,s0,h,!,nviminfo
388 wviminfo Xviminfo
389 let @c = '456'
390 rviminfo! Xviminfo
391 call assert_equal('456', @c)
392
Bram Moolenaare80ff742016-06-11 21:14:18 +0200393 call delete('Xviminfo')
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200394 set viminfo&vim
Bram Moolenaare80ff742016-06-11 21:14:18 +0200395endfunc
396
Bram Moolenaar2d358992016-06-12 21:20:54 +0200397func Test_viminfo_marks()
398 sp bufa
399 let bufa = bufnr('%')
400 sp bufb
401 let bufb = bufnr('%')
402
403 call test_settime(8)
404 call setpos("'A", [bufa, 1, 1, 0])
405 call test_settime(20)
406 call setpos("'B", [bufb, 9, 1, 0])
407 call setpos("'C", [bufa, 7, 1, 0])
408
409 delmark 0-9
410 call test_settime(25)
411 call setpos("'1", [bufb, 12, 1, 0])
412 call test_settime(35)
413 call setpos("'0", [bufa, 11, 1, 0])
414
415 call test_settime(45)
416 wviminfo Xviminfo
417
418 " Writing viminfo inserts the '0 mark.
419 call assert_equal([bufb, 1, 1, 0], getpos("'0"))
420 call assert_equal([bufa, 11, 1, 0], getpos("'1"))
421 call assert_equal([bufb, 12, 1, 0], getpos("'2"))
422
423 call test_settime(4)
424 call setpos("'A", [bufa, 9, 1, 0])
425 call test_settime(30)
426 call setpos("'B", [bufb, 2, 3, 0])
427 delmark C
428
429 delmark 0-9
430 call test_settime(30)
431 call setpos("'1", [bufb, 22, 1, 0])
432 call test_settime(55)
433 call setpos("'0", [bufa, 21, 1, 0])
434
435 rviminfo Xviminfo
436
437 call assert_equal([bufa, 1, 1, 0], getpos("'A"))
438 call assert_equal([bufb, 2, 3, 0], getpos("'B"))
439 call assert_equal([bufa, 7, 1, 0], getpos("'C"))
440
441 " numbered marks are merged
442 call assert_equal([bufa, 21, 1, 0], getpos("'0")) " time 55
443 call assert_equal([bufb, 1, 1, 0], getpos("'1")) " time 45
444 call assert_equal([bufa, 11, 1, 0], getpos("'2")) " time 35
445 call assert_equal([bufb, 22, 1, 0], getpos("'3")) " time 30
446 call assert_equal([bufb, 12, 1, 0], getpos("'4")) " time 25
447
Bram Moolenaar8cd6cd82019-12-27 17:33:26 +0100448 " deleted file marks are removed from viminfo
449 delmark C
450 wviminfo Xviminfo
451 rviminfo Xviminfo
452 call assert_equal([0, 0, 0, 0], getpos("'C"))
453
454 " deleted file marks stay in viminfo if defined in another vim later
455 call test_settime(70)
456 call setpos("'D", [bufb, 8, 1, 0])
457 wviminfo Xviminfo
458 call test_settime(65)
459 delmark D
460 call assert_equal([0, 0, 0, 0], getpos("'D"))
461 call test_settime(75)
462 rviminfo Xviminfo
463 call assert_equal([bufb, 8, 1, 0], getpos("'D"))
464
Bram Moolenaar2d358992016-06-12 21:20:54 +0200465 call delete('Xviminfo')
466 exe 'bwipe ' . bufa
467 exe 'bwipe ' . bufb
468endfunc
469
470func Test_viminfo_jumplist()
471 split testbuf
472 clearjumps
473 call setline(1, ['time 05', 'time 10', 'time 15', 'time 20', 'time 30', 'last pos'])
474 call cursor(2, 1)
475 call test_settime(10)
476 exe "normal /20\r"
477 call test_settime(20)
478 exe "normal /30\r"
479 call test_settime(30)
480 exe "normal /last pos\r"
481 wviminfo Xviminfo
482
483 clearjumps
484 call cursor(1, 1)
485 call test_settime(5)
486 exe "normal /15\r"
487 call test_settime(15)
488 exe "normal /last pos\r"
489 call test_settime(40)
490 exe "normal ?30\r"
491 rviminfo Xviminfo
492
493 call assert_equal('time 30', getline('.'))
494 exe "normal \<C-O>"
495 call assert_equal('last pos', getline('.'))
496 exe "normal \<C-O>"
497 " duplicate for 'time 30' was removed
498 call assert_equal('time 20', getline('.'))
499 exe "normal \<C-O>"
500 call assert_equal('time 15', getline('.'))
501 exe "normal \<C-O>"
502 call assert_equal('time 10', getline('.'))
503 exe "normal \<C-O>"
504 call assert_equal('time 05', getline('.'))
505
Bram Moolenaarece74ab2016-06-13 22:22:15 +0200506 clearjumps
507 call cursor(1, 1)
508 call test_settime(5)
509 exe "normal /15\r"
510 call test_settime(15)
511 exe "normal /last pos\r"
512 call test_settime(40)
513 exe "normal ?30\r"
514 " Test merge when writing
515 wviminfo Xviminfo
516 clearjumps
517 rviminfo Xviminfo
518
Bram Moolenaar28607ba2016-06-15 21:44:51 +0200519 let last_line = line('.')
Bram Moolenaarece74ab2016-06-13 22:22:15 +0200520 exe "normal \<C-O>"
521 call assert_equal('time 30', getline('.'))
522 exe "normal \<C-O>"
523 call assert_equal('last pos', getline('.'))
524 exe "normal \<C-O>"
525 " duplicate for 'time 30' was removed
526 call assert_equal('time 20', getline('.'))
527 exe "normal \<C-O>"
528 call assert_equal('time 15', getline('.'))
529 exe "normal \<C-O>"
530 call assert_equal('time 10', getline('.'))
531 exe "normal \<C-O>"
532 call assert_equal('time 05', getline('.'))
533
Bram Moolenaar28607ba2016-06-15 21:44:51 +0200534 " Test with jumplist full.
535 clearjumps
536 call setline(1, repeat(['match here'], 101))
537 call cursor(1, 1)
538 call test_settime(10)
539 for i in range(100)
540 exe "normal /here\r"
541 endfor
542 rviminfo Xviminfo
543
544 " must be newest mark that comes from viminfo.
545 exe "normal \<C-O>"
546 call assert_equal(last_line, line('.'))
547
Bram Moolenaar2d358992016-06-12 21:20:54 +0200548 bwipe!
549 call delete('Xviminfo')
550endfunc
551
Bram Moolenaar01227092016-06-11 14:47:40 +0200552func Test_viminfo_encoding()
Bram Moolenaar01227092016-06-11 14:47:40 +0200553 set enc=latin1
554 call histdel(':')
555 call histadd(':', "echo '\xe9'")
556 wviminfo Xviminfo
557
558 set fencs=utf-8,latin1
559 set enc=utf-8
560 sp Xviminfo
561 call assert_equal('latin1', &fenc)
562 close
Bram Moolenaar94722c52023-01-28 19:19:03 +0000563
Bram Moolenaar01227092016-06-11 14:47:40 +0200564 call histdel(':')
565 rviminfo Xviminfo
566 call assert_equal("echo 'é'", histget(':', -1))
567
568 call delete('Xviminfo')
569endfunc
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +0200570
571func Test_viminfo_bad_syntax()
572 let lines = []
573 call add(lines, '|<') " empty continuation line
574 call add(lines, '|234234234234234324,nothing')
575 call add(lines, '|1+"no comma"')
576 call add(lines, '|1,2,3,4,5,6,7') " too many items
577 call add(lines, '|1,"string version"')
578 call add(lines, '|1,>x') " bad continuation line
579 call add(lines, '|1,"x') " missing quote
580 call add(lines, '|1,"x\') " trailing backslash
581 call add(lines, '|1,,,,') "trailing comma
582 call add(lines, '|1,>234') " trailing continuation line
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100583 call writefile(lines, 'Xviminfo', 'D')
Bram Moolenaare80ff742016-06-11 21:14:18 +0200584 rviminfo Xviminfo
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +0200585
586 call delete('Xviminfo')
587endfunc
588
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200589func Test_viminfo_bad_syntax2()
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +0200590 let lines = []
591 call add(lines, '|1,4')
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200592
593 " bad viminfo syntax for history barline
594 call add(lines, '|2') " invalid number of fields in a history barline
595 call add(lines, '|2,9,1,1,"x"') " invalid value for the history type
596 call add(lines, '|2,0,,1,"x"') " no timestamp
597 call add(lines, '|2,0,1,1,10') " non-string text
598
599 " bad viminfo syntax for register barline
600 call add(lines, '|3') " invalid number of fields in a register barline
601 call add(lines, '|3,1,1,1,1,,1,"x"') " missing width field
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +0200602 call add(lines, '|3,0,80,1,1,1,1,"x"') " invalid register number
603 call add(lines, '|3,0,10,5,1,1,1,"x"') " invalid register type
604 call add(lines, '|3,0,10,1,20,1,1,"x"') " invalid line count
605 call add(lines, '|3,0,10,1,0,1,1') " zero line count
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200606
607 " bad viminfo syntax for mark barline
608 call add(lines, '|4') " invalid number of fields in a mark barline
609 call add(lines, '|4,1,1,1,1,1') " invalid value for file name
610 call add(lines, '|4,20,1,1,1,"x"') " invalid value for file name
611 call add(lines, '|4,49,0,1,1,"x"') " invalid value for line number
612
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100613 call writefile(lines, 'Xviminfo', 'D')
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +0200614 rviminfo Xviminfo
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +0200615endfunc
616
Pierre Colin0a076462023-08-19 11:56:57 +0200617" This used to crash Vim (GitHub issue #12652)
618func Test_viminfo_bad_syntax3()
619 let lines =<< trim END
620 call writefile([], 'Xvbs3.result')
621 qall!
622 END
623 call writefile(lines, 'Xvbs3script', 'D')
624
625 let lines = []
626 call add(lines, '|1,4')
627 " bad viminfo syntax for register barline
628 call add(lines, '|3,1,1,1,1,0,71489,,125') " empty line1
629 call writefile(lines, 'Xviminfo', 'D')
630
631 call RunVim([], [], '--clean -i Xviminfo -S Xvbs3script')
632 call assert_true(filereadable('Xvbs3.result'))
633
634 call delete('Xvbs3.result')
635endfunc
636
Bram Moolenaarab9c89b2016-07-03 17:47:26 +0200637func Test_viminfo_file_marks()
638 silent! bwipe test_viminfo.vim
639 silent! bwipe Xviminfo
640
641 call test_settime(10)
642 edit ten
643 call test_settime(25)
644 edit again
645 call test_settime(30)
646 edit thirty
647 wviminfo Xviminfo
648
649 call test_settime(20)
650 edit twenty
651 call test_settime(35)
652 edit again
653 call test_settime(40)
Dominique Pelle923dce22021-11-21 11:36:04 +0000654 edit forty
Bram Moolenaarab9c89b2016-07-03 17:47:26 +0200655 wviminfo Xviminfo
656
657 sp Xviminfo
658 1
Dominique Pelle923dce22021-11-21 11:36:04 +0000659 for name in ['forty', 'again', 'thirty', 'twenty', 'ten']
Bram Moolenaarab9c89b2016-07-03 17:47:26 +0200660 /^>
661 call assert_equal(name, substitute(getline('.'), '.*/', '', ''))
662 endfor
663 close
664
665 call delete('Xviminfo')
666endfunc
Bram Moolenaare59215c2016-08-14 19:08:45 +0200667
668func Test_viminfo_file_mark_tabclose()
669 tabnew Xtestfileintab
670 call setline(1, ['a','b','c','d','e'])
671 4
672 q!
673 wviminfo Xviminfo
674 sp Xviminfo
675 /^> .*Xtestfileintab
676 let lnum = line('.')
677 while 1
678 if lnum == line('$')
Bram Moolenaar37175402017-03-18 20:18:45 +0100679 call assert_report('mark not found in Xtestfileintab')
Bram Moolenaare59215c2016-08-14 19:08:45 +0200680 break
681 endif
682 let lnum += 1
683 let line = getline(lnum)
684 if line == ''
Bram Moolenaar37175402017-03-18 20:18:45 +0100685 call assert_report('mark not found in Xtestfileintab')
Bram Moolenaare59215c2016-08-14 19:08:45 +0200686 break
687 endif
688 if line =~ "^\t\""
689 call assert_equal('4', substitute(line, ".*\"\t\\(\\d\\).*", '\1', ''))
690 break
691 endif
692 endwhile
693
694 call delete('Xviminfo')
695 silent! bwipe Xtestfileintab
696endfunc
Bram Moolenaare11d61a2016-08-20 18:36:54 +0200697
Bram Moolenaar156919f2016-10-15 20:46:20 +0200698func Test_viminfo_file_mark_zero_time()
699 let lines = [
700 \ '# Viminfo version',
701 \ '|1,4',
702 \ '',
703 \ '*encoding=utf-8',
704 \ '',
705 \ '# File marks:',
706 \ "'B 1 0 /tmp/nothing",
707 \ '|4,66,1,0,0,"/tmp/nothing"',
708 \ "",
709 \ ]
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100710 call writefile(lines, 'Xviminfo', 'D')
Bram Moolenaar156919f2016-10-15 20:46:20 +0200711 delmark B
712 rviminfo Xviminfo
Bram Moolenaar156919f2016-10-15 20:46:20 +0200713 call assert_equal(1, line("'B"))
714 delmark B
715endfunc
716
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200717" Test for saving and restoring file marks in unloaded buffers
718func Test_viminfo_file_mark_unloaded_buf()
719 let save_viminfo = &viminfo
720 set viminfo&vim
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100721 call writefile(repeat(['vim'], 10), 'Xfile1', 'D')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200722 %bwipe
723 edit! Xfile1
724 call setpos("'u", [0, 3, 1, 0])
725 call setpos("'v", [0, 5, 1, 0])
726 enew
727 wviminfo Xviminfo
728 %bwipe
729 edit Xfile1
730 rviminfo! Xviminfo
731 call assert_equal([0, 3, 1, 0], getpos("'u"))
732 call assert_equal([0, 5, 1, 0], getpos("'v"))
733 %bwipe
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200734 call delete('Xviminfo')
735 let &viminfo = save_viminfo
736endfunc
737
Bram Moolenaar156919f2016-10-15 20:46:20 +0200738func Test_viminfo_oldfiles()
K.Takata0500e872022-09-08 12:28:02 +0100739 set noswapfile
Bram Moolenaare11d61a2016-08-20 18:36:54 +0200740 let v:oldfiles = []
741 let lines = [
742 \ '# comment line',
743 \ '*encoding=utf-8',
744 \ '',
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200745 \ ':h viminfo',
746 \ '?/session',
747 \ '=myvar',
748 \ '@123',
749 \ '',
750 \ "'E 2 0 /tmp/nothing",
751 \ '',
Bram Moolenaare11d61a2016-08-20 18:36:54 +0200752 \ "> /tmp/file_one.txt",
753 \ "\t\"\t11\t0",
754 \ "",
755 \ "> /tmp/file_two.txt",
756 \ "\t\"\t11\t0",
757 \ "",
758 \ "> /tmp/another.txt",
759 \ "\t\"\t11\t0",
760 \ "",
761 \ ]
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100762 call writefile(lines, 'Xviminfo', 'D')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200763 delmark E
Bram Moolenaar5affc032021-02-11 18:36:30 +0100764 edit /tmp/file_two.txt
Bram Moolenaare11d61a2016-08-20 18:36:54 +0200765 rviminfo! Xviminfo
Bram Moolenaare11d61a2016-08-20 18:36:54 +0200766
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200767 call assert_equal('h viminfo', histget(':'))
768 call assert_equal('session', histget('/'))
769 call assert_equal('myvar', histget('='))
770 call assert_equal('123', histget('@'))
771 call assert_equal(2, line("'E"))
Bram Moolenaar7b668e82016-08-23 23:51:21 +0200772 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/'}))
773 call assert_equal(['1: /tmp/file_one.txt', '2: /tmp/file_two.txt'], filter(split(execute('filter file_ oldfiles'), "\n"), {i, v -> v =~ '/tmp/'}))
774 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 +0200775
776 new
777 call feedkeys("3\<CR>", 't')
778 browse oldfiles
779 call assert_equal("/tmp/another.txt", expand("%"))
780 bwipe
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200781 delmark E
K.Takata0500e872022-09-08 12:28:02 +0100782 set swapfile&
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200783endfunc
784
785" Test for storing and restoring buffer list in 'viminfo'
786func Test_viminfo_bufferlist()
787 " If there are arguments, then :rviminfo doesn't read the buffer list.
788 " Need to delete all the arguments for :rviminfo to work.
789 %argdelete
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +0200790 set viminfo&vim
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200791
792 edit Xfile1
793 edit Xfile2
794 set viminfo-=%
795 wviminfo Xviminfo
796 %bwipe
797 rviminfo Xviminfo
798 call assert_equal(1, len(getbufinfo()))
799
800 edit Xfile1
801 edit Xfile2
802 set viminfo^=%
803 wviminfo Xviminfo
804 %bwipe
805 rviminfo Xviminfo
806 let l = getbufinfo()
807 call assert_equal(3, len(l))
808 call assert_equal('Xfile1', bufname(l[1].bufnr))
809 call assert_equal('Xfile2', bufname(l[2].bufnr))
810
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +0200811 " The quickfix, terminal, unlisted, unnamed buffers are not stored in the
812 " viminfo file
813 %bw!
814 edit Xfile1
815 new
816 setlocal nobuflisted
817 new
818 copen
819 if has('terminal')
820 terminal
821 endif
822 wviminfo! Xviminfo
823 %bwipe!
824 rviminfo Xviminfo
825 let l = getbufinfo()
826 call assert_equal(2, len(l))
827 call assert_true(bufexists('Xfile1'))
828
829 " If a count is specified for '%', then only that many buffers should be
830 " stored in the viminfo file.
831 %bw!
832 set viminfo&vim
833 new Xbuf1
834 new Xbuf2
835 set viminfo+=%1
836 wviminfo! Xviminfo
837 %bwipe!
838 rviminfo! Xviminfo
839 let l = getbufinfo()
840 call assert_equal(2, len(l))
841 call assert_true(bufexists('Xbuf1'))
842 call assert_false(bufexists('Xbuf2'))
843
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200844 call delete('Xviminfo')
845 %bwipe
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +0200846 set viminfo&vim
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200847endfunc
848
849" Test for errors in a viminfo file
850func Test_viminfo_error()
851 " Non-existing viminfo files
852 call assert_fails('rviminfo xyz', 'E195:')
853
854 " Illegal starting character
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100855 call writefile(["a 123"], 'Xviminfo', 'D')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200856 call assert_fails('rv Xviminfo', 'E575:')
857
858 " Illegal register name in the viminfo file
859 call writefile(['"@ LINE 0'], 'Xviminfo')
860 call assert_fails('rv Xviminfo', 'E577:')
861
862 " Invalid file mark line
863 call writefile(['>', '@'], 'Xviminfo')
864 call assert_fails('rv Xviminfo', 'E576:')
865
866 " Too many errors in viminfo file
867 call writefile(repeat(["a 123"], 15), 'Xviminfo')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200868 call assert_fails('rv Xviminfo', 'E575:')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200869
870 call writefile(['>'] + repeat(['@'], 10), 'Xviminfo')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200871 call assert_fails('rv Xviminfo', 'E576:')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200872
873 call writefile(repeat(['"@'], 15), 'Xviminfo')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200874 call assert_fails('rv Xviminfo', 'E577:')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200875endfunc
876
877" Test for saving and restoring last substitute string in viminfo
878func Test_viminfo_lastsub()
879 enew
880 call append(0, "blue blue blue")
881 call cursor(1, 1)
882 s/blue/green/
883 wviminfo Xviminfo
884 s/blue/yellow/
885 rviminfo! Xviminfo
886 &
887 call assert_equal("green yellow green", getline(1))
888 enew!
889 call delete('Xviminfo')
890endfunc
891
892" Test saving and restoring the register values using the older method
893func Test_viminfo_registers_old()
894 let lines = [
895 \ '# Viminfo version',
896 \ '|1,1',
897 \ '',
898 \ '*encoding=utf-8',
899 \ '',
900 \ '# Registers:',
901 \ '""0 CHAR 0',
902 \ ' Vim',
903 \ '"a CHAR 0',
904 \ ' red',
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +0200905 \ '"c BLOCK 0',
906 \ ' a',
907 \ ' d',
908 \ '"d LINE 0',
909 \ ' abc',
910 \ ' def',
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200911 \ '"m@ CHAR 0',
912 \ " :echo 'Hello'\<CR>",
913 \ "",
914 \ ]
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100915 call writefile(lines, 'Xviminfo', 'D')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200916 let @a = 'one'
917 let @b = 'two'
918 let @m = 'three'
919 let @" = 'four'
920 let @t = ":echo 'Unix'\<CR>"
921 silent! normal @t
922 rviminfo! Xviminfo
923 call assert_equal('red', getreg('a'))
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +0200924 call assert_equal("v", getregtype('a'))
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200925 call assert_equal('two', getreg('b'))
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +0200926 call assert_equal("a\nd", getreg('c'))
927 call assert_equal("\<C-V>1", getregtype('c'))
928 call assert_equal("abc\ndef\n", getreg('d'))
929 call assert_equal("V", getregtype('d'))
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200930 call assert_equal(":echo 'Hello'\<CR>", getreg('m'))
931 call assert_equal('Vim', getreg('"'))
932 call assert_equal("\nHello", execute('normal @@'))
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100933
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200934 let @" = ''
935endfunc
936
937" Test for saving and restoring large number of lines in a register
938func Test_viminfo_large_register()
939 let save_viminfo = &viminfo
940 set viminfo&vim
941 set viminfo-=<50
942 set viminfo+=<200
943 let lines = ['"r CHAR 0']
944 call extend(lines, repeat(["\tsun is rising"], 200))
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100945 call writefile(lines, 'Xviminfo', 'D')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200946 let @r = ''
947 rviminfo! Xviminfo
948 call assert_equal(join(repeat(["sun is rising"], 200), "\n"), @r)
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100949
Bram Moolenaar6bd1d772019-10-09 22:01:25 +0200950 let @r = ''
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200951 let &viminfo = save_viminfo
952endfunc
953
954" Test for setting 'viminfofile' to NONE
955func Test_viminfofile_none()
Bram Moolenaar6bd1d772019-10-09 22:01:25 +0200956 let save_vif = &viminfofile
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200957 set viminfofile=NONE
958 wviminfo Xviminfo
959 call assert_false(filereadable('Xviminfo'))
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100960 call writefile([''], 'Xviminfo', 'D')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200961 call assert_fails('rviminfo Xviminfo', 'E195:')
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100962
Bram Moolenaar6bd1d772019-10-09 22:01:25 +0200963 let &viminfofile = save_vif
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200964endfunc
965
Dominique Pelle923dce22021-11-21 11:36:04 +0000966" Test for an unwritable and unreadable 'viminfo' file
Bram Moolenaar6bd1d772019-10-09 22:01:25 +0200967func Test_viminfo_perm()
968 CheckUnix
Bram Moolenaar07282f02019-10-10 16:46:17 +0200969 CheckNotRoot
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100970 call writefile([''], 'Xviminfo', 'D')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200971 call setfperm('Xviminfo', 'r-x------')
972 call assert_fails('wviminfo Xviminfo', 'E137:')
Bram Moolenaar6bd1d772019-10-09 22:01:25 +0200973 call setfperm('Xviminfo', '--x------')
974 call assert_fails('rviminfo Xviminfo', 'E195:')
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200975
976 " Try to write the viminfo to a directory
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100977 call mkdir('Xvifdir', 'R')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100978 call assert_fails('wviminfo Xvifdir', 'E137:')
979 call assert_fails('rviminfo Xvifdir', 'E195:')
Bram Moolenaare11d61a2016-08-20 18:36:54 +0200980endfunc
Bram Moolenaar6bd1d772019-10-09 22:01:25 +0200981
982" Test for writing to an existing viminfo file merges the file marks
983func XTest_viminfo_marks_merge()
984 let save_viminfo = &viminfo
985 set viminfo&vim
986 set viminfo^=%
987 enew
988 %argdelete
989 %bwipe
990
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100991 call writefile(repeat(['editor'], 10), 'Xbufa', 'D')
992 call writefile(repeat(['Vim'], 10), 'Xbufb', 'D')
Bram Moolenaar6bd1d772019-10-09 22:01:25 +0200993
994 " set marks in buffers
995 call test_settime(10)
996 edit Xbufa
997 4mark a
998 wviminfo Xviminfo
999 edit Xbufb
1000 4mark b
1001 wviminfo Xviminfo
1002 %bwipe
1003
1004 " set marks in buffers again
1005 call test_settime(20)
1006 edit Xbufb
1007 6mark b
1008 wviminfo Xviminfo
1009 edit Xbufa
1010 6mark a
1011 wviminfo Xviminfo
1012 %bwipe
1013
1014 " Load the buffer and check the marks
1015 edit Xbufa
1016 rviminfo! Xviminfo
1017 call assert_equal(6, line("'a"))
1018 edit Xbufb
1019 rviminfo! Xviminfo
1020 call assert_equal(6, line("'b"))
1021
1022 " cleanup
1023 %bwipe
1024 call delete('Xviminfo')
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02001025 call test_settime(0)
1026 let &viminfo=save_viminfo
1027endfunc
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001028
1029" Test for errors in setting 'viminfo'
1030func Test_viminfo_option_error()
1031 " Missing number
1032 call assert_fails('set viminfo=\"', 'E526:')
1033 for c in split("'/:<@s", '\zs')
1034 call assert_fails('set viminfo=' .. c, 'E526:')
1035 endfor
1036
1037 " Missing comma
1038 call assert_fails('set viminfo=%10!', 'E527:')
1039 call assert_fails('set viminfo=!%10', 'E527:')
1040 call assert_fails('set viminfo=h%10', 'E527:')
1041 call assert_fails('set viminfo=c%10', 'E527:')
1042 call assert_fails('set viminfo=:10%10', 'E527:')
1043
1044 " Missing ' setting
1045 call assert_fails('set viminfo=%10', 'E528:')
1046endfunc
1047
Bram Moolenaar8e6be342020-11-23 22:01:26 +01001048func Test_viminfo_oldfiles_newfile()
1049 CheckRunVimInTerminal
1050
1051 let save_viminfo = &viminfo
1052 let save_viminfofile = &viminfofile
1053 set viminfo&vim
1054 let v:oldfiles = []
1055 let commands =<< trim [CODE]
1056 set viminfofile=Xviminfofile
1057 set viminfo&vim
1058 w! Xnew-file.txt
1059 qall
1060 [CODE]
Bram Moolenaar5b148ef2022-10-15 21:35:56 +01001061 call writefile(commands, 'Xviminfotest', 'D')
Bram Moolenaar8e6be342020-11-23 22:01:26 +01001062 let buf = RunVimInTerminal('-S Xviminfotest', #{wait_for_ruler: 0})
1063 call WaitForAssert({-> assert_equal("finished", term_getstatus(buf))})
1064
1065 let &viminfofile = 'Xviminfofile'
1066 rviminfo! Xviminfofile
1067 call assert_match('Xnew-file.txt$', v:oldfiles[0])
1068 call assert_equal(1, len(v:oldfiles))
Bram Moolenaar5b148ef2022-10-15 21:35:56 +01001069
Bram Moolenaar8e6be342020-11-23 22:01:26 +01001070 call delete('Xviminfofile')
Bram Moolenaar8e6be342020-11-23 22:01:26 +01001071 call delete('Xnew-file.txt')
Bram Moolenaar6fd367a2021-03-13 13:14:04 +01001072
1073 let v:oldfiles = test_null_list()
1074 call assert_equal("\nNo old files", execute('oldfiles'))
1075
Bram Moolenaar8e6be342020-11-23 22:01:26 +01001076 let &viminfo = save_viminfo
1077 let &viminfofile = save_viminfofile
1078endfunc
1079
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +02001080" When writing CTRL-V or "\n" to a viminfo file, it is converted to CTRL-V
1081" CTRL-V and CTRL-V n respectively.
1082func Test_viminfo_with_Ctrl_V()
1083 silent! exe "normal! /\<C-V>\<C-V>\n"
1084 wviminfo Xviminfo
1085 call assert_notequal(-1, readfile('Xviminfo')->index("?/\<C-V>\<C-V>"))
1086 let @/ = 'abc'
1087 rviminfo! Xviminfo
1088 call assert_equal("\<C-V>", @/)
1089 silent! exe "normal! /\<C-V>\<C-J>\n"
1090 wviminfo Xviminfo
1091 call assert_notequal(-1, readfile('Xviminfo')->index("?/\<C-V>n"))
1092 let @/ = 'abc'
1093 rviminfo! Xviminfo
1094 call assert_equal("\n", @/)
1095 call delete('Xviminfo')
1096endfunc
1097
1098" Test for the 'r' field in 'viminfo' (removal media)
1099func Test_viminfo_removable_media()
1100 CheckUnix
1101 if !isdirectory('/tmp') || getftype('/tmp') != 'dir'
1102 return
1103 endif
1104 let save_viminfo = &viminfo
1105 set viminfo+=r/tmp
1106 edit /tmp/Xvima1b2c3
1107 wviminfo Xviminfo
1108 let matches = readfile('Xviminfo')->filter("v:val =~ 'Xvima1b2c3'")
1109 call assert_equal(0, matches->len())
1110 let &viminfo = save_viminfo
1111 call delete('Xviminfo')
1112endfunc
1113
1114" Test for the 'h' flag in 'viminfo'. If 'h' is not present, then the last
1115" search pattern read from 'viminfo' should be highlighted with 'hlsearch'.
1116" If 'h' is present, then the last search pattern should not be highlighted.
1117func Test_viminfo_hlsearch()
1118 set viminfo&vim
1119
1120 new
1121 call setline(1, ['one two three'])
1122 " save the screen attribute for the Search highlighted text and the normal
1123 " text for later comparison
1124 set hlsearch
1125 let @/ = 'three'
1126 redraw!
1127 let hiSearch = screenattr(1, 9)
1128 let hiNormal = screenattr(1, 1)
1129
1130 set viminfo-=h
1131 let @/='two'
1132 wviminfo! Xviminfo
1133 let @/='one'
1134 rviminfo! Xviminfo
1135 redraw!
1136 call assert_equal(hiSearch, screenattr(1, 5))
1137 call assert_equal(hiSearch, screenattr(1, 6))
1138 call assert_equal(hiSearch, screenattr(1, 7))
1139
1140 set viminfo+=h
1141 let @/='two'
1142 wviminfo! Xviminfo
1143 let @/='one'
1144 rviminfo! Xviminfo
1145 redraw!
1146 call assert_equal(hiNormal, screenattr(1, 5))
1147 call assert_equal(hiNormal, screenattr(1, 6))
1148 call assert_equal(hiNormal, screenattr(1, 7))
1149
1150 call delete('Xviminfo')
1151 set hlsearch& viminfo&vim
1152 bw!
1153endfunc
1154
1155" Test for restoring the magicness of the last search pattern from the viminfo
1156" file.
1157func Test_viminfo_last_spat_magic()
1158 set viminfo&vim
1159 new
1160 call setline(1, ' one abc a.c')
1161
1162 " restore 'nomagic'
1163 set nomagic
1164 exe "normal gg/a.c\<CR>"
1165 wviminfo! Xviminfo
1166 set magic
1167 exe "normal gg/one\<CR>"
1168 rviminfo! Xviminfo
1169 exe "normal! gg/\<CR>"
1170 call assert_equal(10, col('.'))
1171
1172 " restore 'magic'
1173 set magic
1174 exe "normal gg/a.c\<CR>"
1175 wviminfo! Xviminfo
1176 set nomagic
1177 exe "normal gg/one\<CR>"
1178 rviminfo! Xviminfo
1179 exe "normal! gg/\<CR>"
1180 call assert_equal(6, col('.'))
1181
1182 call delete('Xviminfo')
1183 set viminfo&vim magic&
1184 bw!
1185endfunc
1186
1187" Test for restoring the smartcase of the last search pattern from the viminfo
1188" file.
1189func Test_viminfo_last_spat_smartcase()
1190 new
1191 call setline(1, ' one abc Abc')
1192 set ignorecase smartcase
1193
1194 " Searching with * should disable smartcase
1195 exe "normal! gg$b*"
1196 wviminfo! Xviminfo
1197 exe "normal gg/one\<CR>"
1198 rviminfo! Xviminfo
1199 exe "normal! gg/\<CR>"
1200 call assert_equal(6, col('.'))
1201
1202 call delete('Xviminfo')
1203 set ignorecase& smartcase& viminfo&
1204 bw!
1205endfunc
1206
1207" Test for restoring the last search pattern with a line or character offset
1208" from the viminfo file.
1209func Test_viminfo_last_spat_offset()
1210 new
1211 call setline(1, ['one', 'two', 'three', 'four', 'five'])
1212 " line offset
1213 exe "normal! /two/+2\<CR>"
1214 wviminfo! Xviminfo
1215 exe "normal gg/five\<CR>"
1216 rviminfo! Xviminfo
1217 exe "normal! gg/\<CR>"
1218 call assert_equal(4, line('.'))
1219 " character offset
1220 exe "normal! gg/^th/e+2\<CR>"
1221 wviminfo! Xviminfo
1222 exe "normal gg/two\<CR>"
1223 rviminfo! Xviminfo
1224 exe "normal! gg/\<CR>"
1225 call assert_equal([3, 4], [line('.'), col('.')])
1226 call delete('Xviminfo')
1227 bw!
1228endfunc
1229
1230" Test for saving and restoring the last executed register (@ command)
1231" from the viminfo file
1232func Test_viminfo_last_exec_reg()
1233 let g:val = 1
1234 let @a = ":let g:val += 1\n"
1235 normal! @a
1236 wviminfo! Xviminfo
1237 let @b = ''
1238 normal! @b
1239 rviminfo! Xviminfo
1240 normal @@
1241 call assert_equal(3, g:val)
1242 call delete('Xviminfo')
1243endfunc
1244
1245" Test for merging file marks in a viminfo file
1246func Test_viminfo_merge_file_marks()
1247 for [f, l, t] in [['a.txt', 5, 10], ['b.txt', 10, 20]]
1248 call test_settime(t)
1249 exe 'edit ' .. f
1250 call setline(1, range(1, 20))
1251 exe l . 'mark a'
1252 wviminfo Xviminfo
1253 bw!
1254 endfor
1255 call test_settime(30)
1256 for [f, l] in [['a.txt', 5], ['b.txt', 10]]
1257 exe 'edit ' .. f
1258 rviminfo! Xviminfo
1259 call assert_equal(l, line("'a"))
1260 bw!
1261 endfor
1262 call delete('Xviminfo')
1263 call test_settime(0)
1264endfunc
1265
1266" Test for merging file marks from a old viminfo file
1267func Test_viminfo_merge_old_filemarks()
1268 let lines = []
1269 call add(lines, '|1,4')
1270 call add(lines, '> ' .. fnamemodify('a.txt', ':p:~'))
1271 call add(lines, "\tb\t7\t0\n")
Bram Moolenaar5b148ef2022-10-15 21:35:56 +01001272 call writefile(lines, 'Xviminfo', 'D')
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +02001273 edit b.txt
1274 call setline(1, range(1, 20))
1275 12mark b
1276 wviminfo Xviminfo
1277 bw!
1278 edit a.txt
1279 rviminfo! Xviminfo
1280 call assert_equal(7, line("'b"))
1281 edit b.txt
1282 rviminfo! Xviminfo
1283 call assert_equal(12, line("'b"))
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +02001284endfunc
1285
1286" Test for merging the jump list from a old viminfo file
1287func Test_viminfo_merge_old_jumplist()
1288 let lines = []
1289 call add(lines, "-' 10 1 " .. fnamemodify('a.txt', ':p:~'))
1290 call add(lines, "-' 20 1 " .. fnamemodify('a.txt', ':p:~'))
1291 call add(lines, "-' 30 1 " .. fnamemodify('b.txt', ':p:~'))
1292 call add(lines, "-' 40 1 " .. fnamemodify('b.txt', ':p:~'))
Bram Moolenaar5b148ef2022-10-15 21:35:56 +01001293 call writefile(lines, 'Xviminfo', 'D')
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +02001294 clearjumps
1295 rviminfo! Xviminfo
1296 let l = getjumplist()[0]
1297 call assert_equal([40, 30, 20, 10], [l[0].lnum, l[1].lnum, l[2].lnum,
1298 \ l[3].lnum])
1299 bw!
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +02001300endfunc
1301
Christian Brabandt0b0f7d62024-05-19 09:11:09 +02001302func Test_viminfo_oldfiles_filter()
1303 let v:oldfiles = []
1304 let _viminfofile = &viminfofile
1305 let &viminfofile=''
1306 let lines = [
1307 \ '# comment line',
1308 \ '*encoding=utf-8',
1309 \ "> /tmp/vimrc_one.vim",
1310 \ "\t\"\t11\t0",
1311 \ "",
1312 \ "> /tmp/foobar.txt",
1313 \ "\t\"\t11\t0",
1314 \ "",
1315 \ ]
1316 call writefile(lines, 'Xviminfo1', 'D')
1317 rviminfo! Xviminfo1
1318 new
1319 " filter returns a single item
1320 let a = execute('filter /vim/ oldfiles')->split('\n')
1321 call assert_equal(1, len(a))
1322 " filter returns more than a single match
1323 let a = execute('filter #tmp# oldfiles')->split('\n')
1324 call assert_equal(2, len(a))
1325 " don't get prompted for the file, but directly open it
1326 filter /vim/ browse oldfiles
1327 call assert_equal("/tmp/vimrc_one.vim", expand("%"))
1328 bw
1329 let &viminfofile = _viminfofile
1330endfunc
1331
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001332" vim: shiftwidth=2 sts=2 expandtab