blob: 0551ea1b494bdb1353d4f95598afd5466faebfcd [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
Bram Moolenaarab9c89b2016-07-03 17:47:26 +0200617func Test_viminfo_file_marks()
618 silent! bwipe test_viminfo.vim
619 silent! bwipe Xviminfo
620
621 call test_settime(10)
622 edit ten
623 call test_settime(25)
624 edit again
625 call test_settime(30)
626 edit thirty
627 wviminfo Xviminfo
628
629 call test_settime(20)
630 edit twenty
631 call test_settime(35)
632 edit again
633 call test_settime(40)
Dominique Pelle923dce22021-11-21 11:36:04 +0000634 edit forty
Bram Moolenaarab9c89b2016-07-03 17:47:26 +0200635 wviminfo Xviminfo
636
637 sp Xviminfo
638 1
Dominique Pelle923dce22021-11-21 11:36:04 +0000639 for name in ['forty', 'again', 'thirty', 'twenty', 'ten']
Bram Moolenaarab9c89b2016-07-03 17:47:26 +0200640 /^>
641 call assert_equal(name, substitute(getline('.'), '.*/', '', ''))
642 endfor
643 close
644
645 call delete('Xviminfo')
646endfunc
Bram Moolenaare59215c2016-08-14 19:08:45 +0200647
648func Test_viminfo_file_mark_tabclose()
649 tabnew Xtestfileintab
650 call setline(1, ['a','b','c','d','e'])
651 4
652 q!
653 wviminfo Xviminfo
654 sp Xviminfo
655 /^> .*Xtestfileintab
656 let lnum = line('.')
657 while 1
658 if lnum == line('$')
Bram Moolenaar37175402017-03-18 20:18:45 +0100659 call assert_report('mark not found in Xtestfileintab')
Bram Moolenaare59215c2016-08-14 19:08:45 +0200660 break
661 endif
662 let lnum += 1
663 let line = getline(lnum)
664 if line == ''
Bram Moolenaar37175402017-03-18 20:18:45 +0100665 call assert_report('mark not found in Xtestfileintab')
Bram Moolenaare59215c2016-08-14 19:08:45 +0200666 break
667 endif
668 if line =~ "^\t\""
669 call assert_equal('4', substitute(line, ".*\"\t\\(\\d\\).*", '\1', ''))
670 break
671 endif
672 endwhile
673
674 call delete('Xviminfo')
675 silent! bwipe Xtestfileintab
676endfunc
Bram Moolenaare11d61a2016-08-20 18:36:54 +0200677
Bram Moolenaar156919f2016-10-15 20:46:20 +0200678func Test_viminfo_file_mark_zero_time()
679 let lines = [
680 \ '# Viminfo version',
681 \ '|1,4',
682 \ '',
683 \ '*encoding=utf-8',
684 \ '',
685 \ '# File marks:',
686 \ "'B 1 0 /tmp/nothing",
687 \ '|4,66,1,0,0,"/tmp/nothing"',
688 \ "",
689 \ ]
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100690 call writefile(lines, 'Xviminfo', 'D')
Bram Moolenaar156919f2016-10-15 20:46:20 +0200691 delmark B
692 rviminfo Xviminfo
Bram Moolenaar156919f2016-10-15 20:46:20 +0200693 call assert_equal(1, line("'B"))
694 delmark B
695endfunc
696
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200697" Test for saving and restoring file marks in unloaded buffers
698func Test_viminfo_file_mark_unloaded_buf()
699 let save_viminfo = &viminfo
700 set viminfo&vim
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100701 call writefile(repeat(['vim'], 10), 'Xfile1', 'D')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200702 %bwipe
703 edit! Xfile1
704 call setpos("'u", [0, 3, 1, 0])
705 call setpos("'v", [0, 5, 1, 0])
706 enew
707 wviminfo Xviminfo
708 %bwipe
709 edit Xfile1
710 rviminfo! Xviminfo
711 call assert_equal([0, 3, 1, 0], getpos("'u"))
712 call assert_equal([0, 5, 1, 0], getpos("'v"))
713 %bwipe
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200714 call delete('Xviminfo')
715 let &viminfo = save_viminfo
716endfunc
717
Bram Moolenaar156919f2016-10-15 20:46:20 +0200718func Test_viminfo_oldfiles()
K.Takata0500e872022-09-08 12:28:02 +0100719 set noswapfile
Bram Moolenaare11d61a2016-08-20 18:36:54 +0200720 let v:oldfiles = []
721 let lines = [
722 \ '# comment line',
723 \ '*encoding=utf-8',
724 \ '',
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200725 \ ':h viminfo',
726 \ '?/session',
727 \ '=myvar',
728 \ '@123',
729 \ '',
730 \ "'E 2 0 /tmp/nothing",
731 \ '',
Bram Moolenaare11d61a2016-08-20 18:36:54 +0200732 \ "> /tmp/file_one.txt",
733 \ "\t\"\t11\t0",
734 \ "",
735 \ "> /tmp/file_two.txt",
736 \ "\t\"\t11\t0",
737 \ "",
738 \ "> /tmp/another.txt",
739 \ "\t\"\t11\t0",
740 \ "",
741 \ ]
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100742 call writefile(lines, 'Xviminfo', 'D')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200743 delmark E
Bram Moolenaar5affc032021-02-11 18:36:30 +0100744 edit /tmp/file_two.txt
Bram Moolenaare11d61a2016-08-20 18:36:54 +0200745 rviminfo! Xviminfo
Bram Moolenaare11d61a2016-08-20 18:36:54 +0200746
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200747 call assert_equal('h viminfo', histget(':'))
748 call assert_equal('session', histget('/'))
749 call assert_equal('myvar', histget('='))
750 call assert_equal('123', histget('@'))
751 call assert_equal(2, line("'E"))
Bram Moolenaar7b668e82016-08-23 23:51:21 +0200752 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/'}))
753 call assert_equal(['1: /tmp/file_one.txt', '2: /tmp/file_two.txt'], filter(split(execute('filter file_ oldfiles'), "\n"), {i, v -> v =~ '/tmp/'}))
754 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 +0200755
756 new
757 call feedkeys("3\<CR>", 't')
758 browse oldfiles
759 call assert_equal("/tmp/another.txt", expand("%"))
760 bwipe
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200761 delmark E
K.Takata0500e872022-09-08 12:28:02 +0100762 set swapfile&
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200763endfunc
764
765" Test for storing and restoring buffer list in 'viminfo'
766func Test_viminfo_bufferlist()
767 " If there are arguments, then :rviminfo doesn't read the buffer list.
768 " Need to delete all the arguments for :rviminfo to work.
769 %argdelete
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +0200770 set viminfo&vim
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200771
772 edit Xfile1
773 edit Xfile2
774 set viminfo-=%
775 wviminfo Xviminfo
776 %bwipe
777 rviminfo Xviminfo
778 call assert_equal(1, len(getbufinfo()))
779
780 edit Xfile1
781 edit Xfile2
782 set viminfo^=%
783 wviminfo Xviminfo
784 %bwipe
785 rviminfo Xviminfo
786 let l = getbufinfo()
787 call assert_equal(3, len(l))
788 call assert_equal('Xfile1', bufname(l[1].bufnr))
789 call assert_equal('Xfile2', bufname(l[2].bufnr))
790
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +0200791 " The quickfix, terminal, unlisted, unnamed buffers are not stored in the
792 " viminfo file
793 %bw!
794 edit Xfile1
795 new
796 setlocal nobuflisted
797 new
798 copen
799 if has('terminal')
800 terminal
801 endif
802 wviminfo! Xviminfo
803 %bwipe!
804 rviminfo Xviminfo
805 let l = getbufinfo()
806 call assert_equal(2, len(l))
807 call assert_true(bufexists('Xfile1'))
808
809 " If a count is specified for '%', then only that many buffers should be
810 " stored in the viminfo file.
811 %bw!
812 set viminfo&vim
813 new Xbuf1
814 new Xbuf2
815 set viminfo+=%1
816 wviminfo! Xviminfo
817 %bwipe!
818 rviminfo! Xviminfo
819 let l = getbufinfo()
820 call assert_equal(2, len(l))
821 call assert_true(bufexists('Xbuf1'))
822 call assert_false(bufexists('Xbuf2'))
823
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200824 call delete('Xviminfo')
825 %bwipe
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +0200826 set viminfo&vim
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200827endfunc
828
829" Test for errors in a viminfo file
830func Test_viminfo_error()
831 " Non-existing viminfo files
832 call assert_fails('rviminfo xyz', 'E195:')
833
834 " Illegal starting character
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100835 call writefile(["a 123"], 'Xviminfo', 'D')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200836 call assert_fails('rv Xviminfo', 'E575:')
837
838 " Illegal register name in the viminfo file
839 call writefile(['"@ LINE 0'], 'Xviminfo')
840 call assert_fails('rv Xviminfo', 'E577:')
841
842 " Invalid file mark line
843 call writefile(['>', '@'], 'Xviminfo')
844 call assert_fails('rv Xviminfo', 'E576:')
845
846 " Too many errors in viminfo file
847 call writefile(repeat(["a 123"], 15), 'Xviminfo')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200848 call assert_fails('rv Xviminfo', 'E575:')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200849
850 call writefile(['>'] + repeat(['@'], 10), 'Xviminfo')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200851 call assert_fails('rv Xviminfo', 'E576:')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200852
853 call writefile(repeat(['"@'], 15), 'Xviminfo')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200854 call assert_fails('rv Xviminfo', 'E577:')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200855endfunc
856
857" Test for saving and restoring last substitute string in viminfo
858func Test_viminfo_lastsub()
859 enew
860 call append(0, "blue blue blue")
861 call cursor(1, 1)
862 s/blue/green/
863 wviminfo Xviminfo
864 s/blue/yellow/
865 rviminfo! Xviminfo
866 &
867 call assert_equal("green yellow green", getline(1))
868 enew!
869 call delete('Xviminfo')
870endfunc
871
872" Test saving and restoring the register values using the older method
873func Test_viminfo_registers_old()
874 let lines = [
875 \ '# Viminfo version',
876 \ '|1,1',
877 \ '',
878 \ '*encoding=utf-8',
879 \ '',
880 \ '# Registers:',
881 \ '""0 CHAR 0',
882 \ ' Vim',
883 \ '"a CHAR 0',
884 \ ' red',
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +0200885 \ '"c BLOCK 0',
886 \ ' a',
887 \ ' d',
888 \ '"d LINE 0',
889 \ ' abc',
890 \ ' def',
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200891 \ '"m@ CHAR 0',
892 \ " :echo 'Hello'\<CR>",
893 \ "",
894 \ ]
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100895 call writefile(lines, 'Xviminfo', 'D')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200896 let @a = 'one'
897 let @b = 'two'
898 let @m = 'three'
899 let @" = 'four'
900 let @t = ":echo 'Unix'\<CR>"
901 silent! normal @t
902 rviminfo! Xviminfo
903 call assert_equal('red', getreg('a'))
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +0200904 call assert_equal("v", getregtype('a'))
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200905 call assert_equal('two', getreg('b'))
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +0200906 call assert_equal("a\nd", getreg('c'))
907 call assert_equal("\<C-V>1", getregtype('c'))
908 call assert_equal("abc\ndef\n", getreg('d'))
909 call assert_equal("V", getregtype('d'))
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200910 call assert_equal(":echo 'Hello'\<CR>", getreg('m'))
911 call assert_equal('Vim', getreg('"'))
912 call assert_equal("\nHello", execute('normal @@'))
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100913
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200914 let @" = ''
915endfunc
916
917" Test for saving and restoring large number of lines in a register
918func Test_viminfo_large_register()
919 let save_viminfo = &viminfo
920 set viminfo&vim
921 set viminfo-=<50
922 set viminfo+=<200
923 let lines = ['"r CHAR 0']
924 call extend(lines, repeat(["\tsun is rising"], 200))
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100925 call writefile(lines, 'Xviminfo', 'D')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200926 let @r = ''
927 rviminfo! Xviminfo
928 call assert_equal(join(repeat(["sun is rising"], 200), "\n"), @r)
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100929
Bram Moolenaar6bd1d772019-10-09 22:01:25 +0200930 let @r = ''
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200931 let &viminfo = save_viminfo
932endfunc
933
934" Test for setting 'viminfofile' to NONE
935func Test_viminfofile_none()
Bram Moolenaar6bd1d772019-10-09 22:01:25 +0200936 let save_vif = &viminfofile
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200937 set viminfofile=NONE
938 wviminfo Xviminfo
939 call assert_false(filereadable('Xviminfo'))
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100940 call writefile([''], 'Xviminfo', 'D')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200941 call assert_fails('rviminfo Xviminfo', 'E195:')
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100942
Bram Moolenaar6bd1d772019-10-09 22:01:25 +0200943 let &viminfofile = save_vif
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200944endfunc
945
Dominique Pelle923dce22021-11-21 11:36:04 +0000946" Test for an unwritable and unreadable 'viminfo' file
Bram Moolenaar6bd1d772019-10-09 22:01:25 +0200947func Test_viminfo_perm()
948 CheckUnix
Bram Moolenaar07282f02019-10-10 16:46:17 +0200949 CheckNotRoot
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100950 call writefile([''], 'Xviminfo', 'D')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200951 call setfperm('Xviminfo', 'r-x------')
952 call assert_fails('wviminfo Xviminfo', 'E137:')
Bram Moolenaar6bd1d772019-10-09 22:01:25 +0200953 call setfperm('Xviminfo', '--x------')
954 call assert_fails('rviminfo Xviminfo', 'E195:')
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200955
956 " Try to write the viminfo to a directory
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100957 call mkdir('Xvifdir', 'R')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100958 call assert_fails('wviminfo Xvifdir', 'E137:')
959 call assert_fails('rviminfo Xvifdir', 'E195:')
Bram Moolenaare11d61a2016-08-20 18:36:54 +0200960endfunc
Bram Moolenaar6bd1d772019-10-09 22:01:25 +0200961
962" Test for writing to an existing viminfo file merges the file marks
963func XTest_viminfo_marks_merge()
964 let save_viminfo = &viminfo
965 set viminfo&vim
966 set viminfo^=%
967 enew
968 %argdelete
969 %bwipe
970
Bram Moolenaar5b148ef2022-10-15 21:35:56 +0100971 call writefile(repeat(['editor'], 10), 'Xbufa', 'D')
972 call writefile(repeat(['Vim'], 10), 'Xbufb', 'D')
Bram Moolenaar6bd1d772019-10-09 22:01:25 +0200973
974 " set marks in buffers
975 call test_settime(10)
976 edit Xbufa
977 4mark a
978 wviminfo Xviminfo
979 edit Xbufb
980 4mark b
981 wviminfo Xviminfo
982 %bwipe
983
984 " set marks in buffers again
985 call test_settime(20)
986 edit Xbufb
987 6mark b
988 wviminfo Xviminfo
989 edit Xbufa
990 6mark a
991 wviminfo Xviminfo
992 %bwipe
993
994 " Load the buffer and check the marks
995 edit Xbufa
996 rviminfo! Xviminfo
997 call assert_equal(6, line("'a"))
998 edit Xbufb
999 rviminfo! Xviminfo
1000 call assert_equal(6, line("'b"))
1001
1002 " cleanup
1003 %bwipe
1004 call delete('Xviminfo')
Bram Moolenaar6bd1d772019-10-09 22:01:25 +02001005 call test_settime(0)
1006 let &viminfo=save_viminfo
1007endfunc
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001008
1009" Test for errors in setting 'viminfo'
1010func Test_viminfo_option_error()
1011 " Missing number
1012 call assert_fails('set viminfo=\"', 'E526:')
1013 for c in split("'/:<@s", '\zs')
1014 call assert_fails('set viminfo=' .. c, 'E526:')
1015 endfor
1016
1017 " Missing comma
1018 call assert_fails('set viminfo=%10!', 'E527:')
1019 call assert_fails('set viminfo=!%10', 'E527:')
1020 call assert_fails('set viminfo=h%10', 'E527:')
1021 call assert_fails('set viminfo=c%10', 'E527:')
1022 call assert_fails('set viminfo=:10%10', 'E527:')
1023
1024 " Missing ' setting
1025 call assert_fails('set viminfo=%10', 'E528:')
1026endfunc
1027
Bram Moolenaar8e6be342020-11-23 22:01:26 +01001028func Test_viminfo_oldfiles_newfile()
1029 CheckRunVimInTerminal
1030
1031 let save_viminfo = &viminfo
1032 let save_viminfofile = &viminfofile
1033 set viminfo&vim
1034 let v:oldfiles = []
1035 let commands =<< trim [CODE]
1036 set viminfofile=Xviminfofile
1037 set viminfo&vim
1038 w! Xnew-file.txt
1039 qall
1040 [CODE]
Bram Moolenaar5b148ef2022-10-15 21:35:56 +01001041 call writefile(commands, 'Xviminfotest', 'D')
Bram Moolenaar8e6be342020-11-23 22:01:26 +01001042 let buf = RunVimInTerminal('-S Xviminfotest', #{wait_for_ruler: 0})
1043 call WaitForAssert({-> assert_equal("finished", term_getstatus(buf))})
1044
1045 let &viminfofile = 'Xviminfofile'
1046 rviminfo! Xviminfofile
1047 call assert_match('Xnew-file.txt$', v:oldfiles[0])
1048 call assert_equal(1, len(v:oldfiles))
Bram Moolenaar5b148ef2022-10-15 21:35:56 +01001049
Bram Moolenaar8e6be342020-11-23 22:01:26 +01001050 call delete('Xviminfofile')
Bram Moolenaar8e6be342020-11-23 22:01:26 +01001051 call delete('Xnew-file.txt')
Bram Moolenaar6fd367a2021-03-13 13:14:04 +01001052
1053 let v:oldfiles = test_null_list()
1054 call assert_equal("\nNo old files", execute('oldfiles'))
1055
Bram Moolenaar8e6be342020-11-23 22:01:26 +01001056 let &viminfo = save_viminfo
1057 let &viminfofile = save_viminfofile
1058endfunc
1059
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +02001060" When writing CTRL-V or "\n" to a viminfo file, it is converted to CTRL-V
1061" CTRL-V and CTRL-V n respectively.
1062func Test_viminfo_with_Ctrl_V()
1063 silent! exe "normal! /\<C-V>\<C-V>\n"
1064 wviminfo Xviminfo
1065 call assert_notequal(-1, readfile('Xviminfo')->index("?/\<C-V>\<C-V>"))
1066 let @/ = 'abc'
1067 rviminfo! Xviminfo
1068 call assert_equal("\<C-V>", @/)
1069 silent! exe "normal! /\<C-V>\<C-J>\n"
1070 wviminfo Xviminfo
1071 call assert_notequal(-1, readfile('Xviminfo')->index("?/\<C-V>n"))
1072 let @/ = 'abc'
1073 rviminfo! Xviminfo
1074 call assert_equal("\n", @/)
1075 call delete('Xviminfo')
1076endfunc
1077
1078" Test for the 'r' field in 'viminfo' (removal media)
1079func Test_viminfo_removable_media()
1080 CheckUnix
1081 if !isdirectory('/tmp') || getftype('/tmp') != 'dir'
1082 return
1083 endif
1084 let save_viminfo = &viminfo
1085 set viminfo+=r/tmp
1086 edit /tmp/Xvima1b2c3
1087 wviminfo Xviminfo
1088 let matches = readfile('Xviminfo')->filter("v:val =~ 'Xvima1b2c3'")
1089 call assert_equal(0, matches->len())
1090 let &viminfo = save_viminfo
1091 call delete('Xviminfo')
1092endfunc
1093
1094" Test for the 'h' flag in 'viminfo'. If 'h' is not present, then the last
1095" search pattern read from 'viminfo' should be highlighted with 'hlsearch'.
1096" If 'h' is present, then the last search pattern should not be highlighted.
1097func Test_viminfo_hlsearch()
1098 set viminfo&vim
1099
1100 new
1101 call setline(1, ['one two three'])
1102 " save the screen attribute for the Search highlighted text and the normal
1103 " text for later comparison
1104 set hlsearch
1105 let @/ = 'three'
1106 redraw!
1107 let hiSearch = screenattr(1, 9)
1108 let hiNormal = screenattr(1, 1)
1109
1110 set viminfo-=h
1111 let @/='two'
1112 wviminfo! Xviminfo
1113 let @/='one'
1114 rviminfo! Xviminfo
1115 redraw!
1116 call assert_equal(hiSearch, screenattr(1, 5))
1117 call assert_equal(hiSearch, screenattr(1, 6))
1118 call assert_equal(hiSearch, screenattr(1, 7))
1119
1120 set viminfo+=h
1121 let @/='two'
1122 wviminfo! Xviminfo
1123 let @/='one'
1124 rviminfo! Xviminfo
1125 redraw!
1126 call assert_equal(hiNormal, screenattr(1, 5))
1127 call assert_equal(hiNormal, screenattr(1, 6))
1128 call assert_equal(hiNormal, screenattr(1, 7))
1129
1130 call delete('Xviminfo')
1131 set hlsearch& viminfo&vim
1132 bw!
1133endfunc
1134
1135" Test for restoring the magicness of the last search pattern from the viminfo
1136" file.
1137func Test_viminfo_last_spat_magic()
1138 set viminfo&vim
1139 new
1140 call setline(1, ' one abc a.c')
1141
1142 " restore 'nomagic'
1143 set nomagic
1144 exe "normal gg/a.c\<CR>"
1145 wviminfo! Xviminfo
1146 set magic
1147 exe "normal gg/one\<CR>"
1148 rviminfo! Xviminfo
1149 exe "normal! gg/\<CR>"
1150 call assert_equal(10, col('.'))
1151
1152 " restore 'magic'
1153 set magic
1154 exe "normal gg/a.c\<CR>"
1155 wviminfo! Xviminfo
1156 set nomagic
1157 exe "normal gg/one\<CR>"
1158 rviminfo! Xviminfo
1159 exe "normal! gg/\<CR>"
1160 call assert_equal(6, col('.'))
1161
1162 call delete('Xviminfo')
1163 set viminfo&vim magic&
1164 bw!
1165endfunc
1166
1167" Test for restoring the smartcase of the last search pattern from the viminfo
1168" file.
1169func Test_viminfo_last_spat_smartcase()
1170 new
1171 call setline(1, ' one abc Abc')
1172 set ignorecase smartcase
1173
1174 " Searching with * should disable smartcase
1175 exe "normal! gg$b*"
1176 wviminfo! Xviminfo
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 ignorecase& smartcase& viminfo&
1184 bw!
1185endfunc
1186
1187" Test for restoring the last search pattern with a line or character offset
1188" from the viminfo file.
1189func Test_viminfo_last_spat_offset()
1190 new
1191 call setline(1, ['one', 'two', 'three', 'four', 'five'])
1192 " line offset
1193 exe "normal! /two/+2\<CR>"
1194 wviminfo! Xviminfo
1195 exe "normal gg/five\<CR>"
1196 rviminfo! Xviminfo
1197 exe "normal! gg/\<CR>"
1198 call assert_equal(4, line('.'))
1199 " character offset
1200 exe "normal! gg/^th/e+2\<CR>"
1201 wviminfo! Xviminfo
1202 exe "normal gg/two\<CR>"
1203 rviminfo! Xviminfo
1204 exe "normal! gg/\<CR>"
1205 call assert_equal([3, 4], [line('.'), col('.')])
1206 call delete('Xviminfo')
1207 bw!
1208endfunc
1209
1210" Test for saving and restoring the last executed register (@ command)
1211" from the viminfo file
1212func Test_viminfo_last_exec_reg()
1213 let g:val = 1
1214 let @a = ":let g:val += 1\n"
1215 normal! @a
1216 wviminfo! Xviminfo
1217 let @b = ''
1218 normal! @b
1219 rviminfo! Xviminfo
1220 normal @@
1221 call assert_equal(3, g:val)
1222 call delete('Xviminfo')
1223endfunc
1224
1225" Test for merging file marks in a viminfo file
1226func Test_viminfo_merge_file_marks()
1227 for [f, l, t] in [['a.txt', 5, 10], ['b.txt', 10, 20]]
1228 call test_settime(t)
1229 exe 'edit ' .. f
1230 call setline(1, range(1, 20))
1231 exe l . 'mark a'
1232 wviminfo Xviminfo
1233 bw!
1234 endfor
1235 call test_settime(30)
1236 for [f, l] in [['a.txt', 5], ['b.txt', 10]]
1237 exe 'edit ' .. f
1238 rviminfo! Xviminfo
1239 call assert_equal(l, line("'a"))
1240 bw!
1241 endfor
1242 call delete('Xviminfo')
1243 call test_settime(0)
1244endfunc
1245
1246" Test for merging file marks from a old viminfo file
1247func Test_viminfo_merge_old_filemarks()
1248 let lines = []
1249 call add(lines, '|1,4')
1250 call add(lines, '> ' .. fnamemodify('a.txt', ':p:~'))
1251 call add(lines, "\tb\t7\t0\n")
Bram Moolenaar5b148ef2022-10-15 21:35:56 +01001252 call writefile(lines, 'Xviminfo', 'D')
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +02001253 edit b.txt
1254 call setline(1, range(1, 20))
1255 12mark b
1256 wviminfo Xviminfo
1257 bw!
1258 edit a.txt
1259 rviminfo! Xviminfo
1260 call assert_equal(7, line("'b"))
1261 edit b.txt
1262 rviminfo! Xviminfo
1263 call assert_equal(12, line("'b"))
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +02001264endfunc
1265
1266" Test for merging the jump list from a old viminfo file
1267func Test_viminfo_merge_old_jumplist()
1268 let lines = []
1269 call add(lines, "-' 10 1 " .. fnamemodify('a.txt', ':p:~'))
1270 call add(lines, "-' 20 1 " .. fnamemodify('a.txt', ':p:~'))
1271 call add(lines, "-' 30 1 " .. fnamemodify('b.txt', ':p:~'))
1272 call add(lines, "-' 40 1 " .. fnamemodify('b.txt', ':p:~'))
Bram Moolenaar5b148ef2022-10-15 21:35:56 +01001273 call writefile(lines, 'Xviminfo', 'D')
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +02001274 clearjumps
1275 rviminfo! Xviminfo
1276 let l = getjumplist()[0]
1277 call assert_equal([40, 30, 20, 10], [l[0].lnum, l[1].lnum, l[2].lnum,
1278 \ l[3].lnum])
1279 bw!
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +02001280endfunc
1281
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001282" vim: shiftwidth=2 sts=2 expandtab