blob: 71017279ddacd25f46b5bc1063b5f6fa3db23b34 [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 \ ]
23 call writefile(lines, 'Xviminfo')
24 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 +010043
44 call delete('Xviminfo')
45endfunc
46
47func Test_global_vars()
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +020048 let g:MY_GLOBAL_STRING = "Vim Editor"
49 let g:MY_GLOBAL_NUM = 345
50 let g:MY_GLOBAL_FLOAT = 3.14
Bram Moolenaarb20e3342016-01-18 23:29:01 +010051 let test_dict = {'foo': 1, 'bar': 0, 'longvarible': 1000}
52 let g:MY_GLOBAL_DICT = test_dict
53 " store a really long list, so line wrapping will occur in viminfo file
54 let test_list = range(1,100)
55 let g:MY_GLOBAL_LIST = test_list
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +010056 let test_blob = 0z00112233445566778899aabbccddeeff
57 let g:MY_GLOBAL_BLOB = test_blob
58 let test_false = v:false
59 let g:MY_GLOBAL_FALSE = test_false
60 let test_true = v:true
61 let g:MY_GLOBAL_TRUE = test_true
62 let test_null = v:null
63 let g:MY_GLOBAL_NULL = test_null
64 let test_none = v:none
65 let g:MY_GLOBAL_NONE = test_none
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +020066 let g:MY_GLOBAL_FUNCREF = function('min')
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +010067
Bram Moolenaare9c07272016-03-30 20:50:46 +020068 set viminfo='100,<50,s10,h,!,nviminfo
Bram Moolenaarb20e3342016-01-18 23:29:01 +010069 wv! Xviminfo
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +010070
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +020071 unlet g:MY_GLOBAL_STRING
72 unlet g:MY_GLOBAL_NUM
73 unlet g:MY_GLOBAL_FLOAT
Bram Moolenaarb20e3342016-01-18 23:29:01 +010074 unlet g:MY_GLOBAL_DICT
75 unlet g:MY_GLOBAL_LIST
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +010076 unlet g:MY_GLOBAL_BLOB
77 unlet g:MY_GLOBAL_FALSE
78 unlet g:MY_GLOBAL_TRUE
79 unlet g:MY_GLOBAL_NULL
80 unlet g:MY_GLOBAL_NONE
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +020081 unlet g:MY_GLOBAL_FUNCREF
Bram Moolenaarb20e3342016-01-18 23:29:01 +010082
83 rv! Xviminfo
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +020084 call assert_equal("Vim Editor", g:MY_GLOBAL_STRING)
85 call assert_equal(345, g:MY_GLOBAL_NUM)
86 call assert_equal(3.14, g:MY_GLOBAL_FLOAT)
Bram Moolenaarb20e3342016-01-18 23:29:01 +010087 call assert_equal(test_dict, g:MY_GLOBAL_DICT)
88 call assert_equal(test_list, g:MY_GLOBAL_LIST)
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +010089 call assert_equal(test_blob, g:MY_GLOBAL_BLOB)
90 call assert_equal(test_false, g:MY_GLOBAL_FALSE)
91 call assert_equal(test_true, g:MY_GLOBAL_TRUE)
92 call assert_equal(test_null, g:MY_GLOBAL_NULL)
93 call assert_equal(test_none, g:MY_GLOBAL_NONE)
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +020094 call assert_false(exists("g:MY_GLOBAL_FUNCREF"))
Bram Moolenaarb20e3342016-01-18 23:29:01 +010095
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +020096 " When reading global variables from viminfo, if a variable cannot be
97 " modified, then the value should not be changed.
98 unlet g:MY_GLOBAL_STRING
99 unlet g:MY_GLOBAL_NUM
100 unlet g:MY_GLOBAL_FLOAT
101 unlet g:MY_GLOBAL_DICT
102 unlet g:MY_GLOBAL_LIST
103 unlet g:MY_GLOBAL_BLOB
104
105 const g:MY_GLOBAL_STRING = 'New Value'
106 const g:MY_GLOBAL_NUM = 987
107 const g:MY_GLOBAL_FLOAT = 1.16
108 const g:MY_GLOBAL_DICT = {'editor': 'vim'}
109 const g:MY_GLOBAL_LIST = [5, 7, 13]
110 const g:MY_GLOBAL_BLOB = 0zDEADBEEF
111 call assert_fails('rv! Xviminfo', 'E741:')
112 call assert_equal('New Value', g:MY_GLOBAL_STRING)
113 call assert_equal(987, g:MY_GLOBAL_NUM)
114 call assert_equal(1.16, g:MY_GLOBAL_FLOAT)
115 call assert_equal({'editor': 'vim'}, g:MY_GLOBAL_DICT)
116 call assert_equal([5, 7 , 13], g:MY_GLOBAL_LIST)
117 call assert_equal(0zDEADBEEF, g:MY_GLOBAL_BLOB)
118
119 unlet g:MY_GLOBAL_STRING
120 unlet g:MY_GLOBAL_NUM
121 unlet g:MY_GLOBAL_FLOAT
122 unlet g:MY_GLOBAL_DICT
123 unlet g:MY_GLOBAL_LIST
124 unlet g:MY_GLOBAL_BLOB
125
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200126 " Test for invalid values for a blob, list, dict in a viminfo file
127 call writefile([
128 \ "!GLOB_BLOB_1\tBLO\t123",
129 \ "!GLOB_BLOB_2\tBLO\t012",
130 \ "!GLOB_BLOB_3\tBLO\t0z1x",
131 \ "!GLOB_BLOB_4\tBLO\t0z12 ab",
132 \ "!GLOB_LIST_1\tLIS\t1 2",
133 \ "!GLOB_DICT_1\tDIC\t1 2"], 'Xviminfo')
Bram Moolenaarfae55a92021-06-17 22:08:30 +0200134 call assert_fails('rv! Xviminfo', 'E488:')
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200135 call assert_equal('123', g:GLOB_BLOB_1)
136 call assert_equal(1, type(g:GLOB_BLOB_1))
137 call assert_equal('012', g:GLOB_BLOB_2)
138 call assert_equal(1, type(g:GLOB_BLOB_2))
139 call assert_equal('0z1x', g:GLOB_BLOB_3)
140 call assert_equal(1, type(g:GLOB_BLOB_3))
141 call assert_equal('0z12 ab', g:GLOB_BLOB_4)
142 call assert_equal(1, type(g:GLOB_BLOB_4))
143 call assert_equal('1 2', g:GLOB_LIST_1)
144 call assert_equal(1, type(g:GLOB_LIST_1))
145 call assert_equal('1 2', g:GLOB_DICT_1)
146 call assert_equal(1, type(g:GLOB_DICT_1))
147
Bram Moolenaarb20e3342016-01-18 23:29:01 +0100148 call delete('Xviminfo')
149 set viminfo-=!
150endfunc
Bram Moolenaar45d2eea2016-06-06 21:07:52 +0200151
Bram Moolenaar5b157fe2020-06-07 16:08:08 +0200152func Test_global_vars_with_circular_reference()
153 let g:MY_GLOBAL_LIST = []
154 call add(g:MY_GLOBAL_LIST, g:MY_GLOBAL_LIST)
155 let g:MY_GLOBAL_DICT = {}
156 let g:MY_GLOBAL_DICT['self'] = g:MY_GLOBAL_DICT
157
158 set viminfo='100,<50,s10,h,!,nviminfo
159 wv! Xviminfo
160 call assert_equal(v:errmsg, '')
161
162 unlet g:MY_GLOBAL_LIST
163 unlet g:MY_GLOBAL_DICT
164
165 rv! Xviminfo
166 call assert_equal(v:errmsg, '')
167 call assert_true(!exists('g:MY_GLOBAL_LIST'))
168 call assert_true(!exists('g:MY_GLOBAL_DICT'))
169
170 call delete('Xviminfo')
171 set viminfo-=!
172endfunc
173
Bram Moolenaar45d2eea2016-06-06 21:07:52 +0200174func Test_cmdline_history()
175 call histdel(':')
176 call test_settime(11)
177 call histadd(':', "echo 'one'")
178 call test_settime(12)
179 " split into two lines
180 let long800 = repeat(" 'eight'", 100)
181 call histadd(':', "echo " . long800)
182 call test_settime(13)
183 " split into three lines
184 let long1400 = repeat(" 'fourteeeeen'", 100)
185 call histadd(':', "echo " . long1400)
186 wviminfo Xviminfo
187 let lines = readfile('Xviminfo')
188 let done_colon = 0
189 let done_bar = 0
190 let lnum = 0
191 while lnum < len(lines)
192 let line = lines[lnum] | let lnum += 1
193 if line[0] == ':'
194 if done_colon == 0
195 call assert_equal(":\x161408", line)
196 let line = lines[lnum] | let lnum += 1
197 call assert_equal('<echo ' . long1400, line)
198 elseif done_colon == 1
199 call assert_equal(":\x16808", line)
200 let line = lines[lnum] | let lnum += 1
201 call assert_equal("<echo " . long800, line)
202 elseif done_colon == 2
203 call assert_equal(":echo 'one'", line)
204 endif
205 let done_colon += 1
206 elseif line[0:4] == '|2,0,'
207 if done_bar == 0
208 call assert_equal("|2,0,13,,>1407", line)
209 let line = lines[lnum] | let lnum += 1
210 call assert_equal('|<"echo ' . long1400[0:484], line)
211 let line = lines[lnum] | let lnum += 1
212 call assert_equal('|<' . long1400[485:974], line)
213 let line = lines[lnum] | let lnum += 1
214 call assert_equal('|<' . long1400[975:] . '"', line)
215 elseif done_bar == 1
216 call assert_equal('|2,0,12,,>807', line)
217 let line = lines[lnum] | let lnum += 1
218 call assert_equal('|<"echo ' . long800[0:484], line)
219 let line = lines[lnum] | let lnum += 1
220 call assert_equal('|<' . long800[485:] . '"', line)
221 elseif done_bar == 2
222 call assert_equal("|2,0,11,,\"echo 'one'\"", line)
223 endif
224 let done_bar += 1
225 endif
226 endwhile
227 call assert_equal(3, done_colon)
228 call assert_equal(3, done_bar)
229
230 call histdel(':')
231 rviminfo Xviminfo
232 call assert_equal("echo " . long1400, histget(':', -1))
233 call assert_equal("echo " . long800, histget(':', -2))
234 call assert_equal("echo 'one'", histget(':', -3))
235
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200236 " If the value for the '/' or ':' or '@' field in 'viminfo' is zero, then
237 " the corresponding history entries are not saved.
238 set viminfo='100,/0,:0,@0,<50,s10,h,!,nviminfo
239 call histdel('/')
240 call histdel(':')
241 call histdel('@')
242 call histadd('/', 'foo')
243 call histadd(':', 'bar')
244 call histadd('@', 'baz')
245 wviminfo! Xviminfo
246 call histdel('/')
247 call histdel(':')
248 call histdel('@')
249 rviminfo! Xviminfo
250 call assert_equal('', histget('/'))
251 call assert_equal('', histget(':'))
252 call assert_equal('', histget('@'))
253
Bram Moolenaar45d2eea2016-06-06 21:07:52 +0200254 call delete('Xviminfo')
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200255 set viminfo&vim
Bram Moolenaar45d2eea2016-06-06 21:07:52 +0200256endfunc
Bram Moolenaar1fd99c12016-06-09 20:24:28 +0200257
258func Test_cmdline_history_order()
259 call histdel(':')
260 call test_settime(11)
261 call histadd(':', "echo '11'")
262 call test_settime(22)
263 call histadd(':', "echo '22'")
264 call test_settime(33)
265 call histadd(':', "echo '33'")
266 wviminfo Xviminfo
267
268 call histdel(':')
269 " items go in between
270 call test_settime(15)
271 call histadd(':', "echo '15'")
272 call test_settime(27)
273 call histadd(':', "echo '27'")
274
275 rviminfo Xviminfo
276 call assert_equal("echo '33'", histget(':', -1))
277 call assert_equal("echo '27'", histget(':', -2))
278 call assert_equal("echo '22'", histget(':', -3))
279 call assert_equal("echo '15'", histget(':', -4))
280 call assert_equal("echo '11'", histget(':', -5))
281
282 call histdel(':')
283 " items go before and after
Bram Moolenaarce90e362019-09-08 18:58:44 +0200284 eval 8->test_settime()
Bram Moolenaar1fd99c12016-06-09 20:24:28 +0200285 call histadd(':', "echo '8'")
286 call test_settime(39)
287 call histadd(':', "echo '39'")
288
289 rviminfo Xviminfo
290 call assert_equal("echo '39'", histget(':', -1))
291 call assert_equal("echo '33'", histget(':', -2))
292 call assert_equal("echo '22'", histget(':', -3))
293 call assert_equal("echo '11'", histget(':', -4))
294 call assert_equal("echo '8'", histget(':', -5))
295
296 " Check sorting works when writing with merge.
297 call histdel(':')
298 call test_settime(8)
299 call histadd(':', "echo '8'")
300 call test_settime(15)
301 call histadd(':', "echo '15'")
302 call test_settime(27)
303 call histadd(':', "echo '27'")
304 call test_settime(39)
305 call histadd(':', "echo '39'")
306 wviminfo Xviminfo
307
308 call histdel(':')
309 rviminfo Xviminfo
310 call assert_equal("echo '39'", histget(':', -1))
311 call assert_equal("echo '33'", histget(':', -2))
312 call assert_equal("echo '27'", histget(':', -3))
313 call assert_equal("echo '22'", histget(':', -4))
314 call assert_equal("echo '15'", histget(':', -5))
315 call assert_equal("echo '11'", histget(':', -6))
316 call assert_equal("echo '8'", histget(':', -7))
317
318 call delete('Xviminfo')
319endfunc
Bram Moolenaar01227092016-06-11 14:47:40 +0200320
Bram Moolenaare80ff742016-06-11 21:14:18 +0200321func Test_viminfo_registers()
322 call test_settime(8)
323 call setreg('a', "eight", 'c')
324 call test_settime(20)
325 call setreg('b', ["twenty", "again"], 'l')
326 call test_settime(40)
327 call setreg('c', ["four", "agai"], 'b4')
328 let l = []
329 set viminfo='100,<600,s10,h,!,nviminfo
330 for i in range(500)
331 call add(l, 'something')
332 endfor
333 call setreg('d', l, 'l')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200334 call setreg('e', "abc\<C-V>xyz")
Bram Moolenaare80ff742016-06-11 21:14:18 +0200335 wviminfo Xviminfo
336
337 call test_settime(10)
338 call setreg('a', '', 'b10')
339 call test_settime(15)
340 call setreg('b', 'drop')
341 call test_settime(50)
342 call setreg('c', 'keep', 'l')
343 call test_settime(30)
344 call setreg('d', 'drop', 'l')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200345 call setreg('e', 'drop')
Bram Moolenaare80ff742016-06-11 21:14:18 +0200346 rviminfo Xviminfo
347
348 call assert_equal("", getreg('a'))
349 call assert_equal("\<C-V>10", getregtype('a'))
350 call assert_equal("twenty\nagain\n", getreg('b'))
351 call assert_equal("V", getregtype('b'))
352 call assert_equal("keep\n", getreg('c'))
353 call assert_equal("V", getregtype('c'))
354 call assert_equal(l, getreg('d', 1, 1))
355 call assert_equal("V", getregtype('d'))
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200356 call assert_equal("abc\<C-V>xyz", getreg('e'))
Bram Moolenaare80ff742016-06-11 21:14:18 +0200357
Bram Moolenaar25709572016-08-26 20:41:16 +0200358 " Length around 440 switches to line continuation.
359 let len = 434
360 while len < 445
361 let s = repeat('a', len)
362 call setreg('"', s)
363 wviminfo Xviminfo
364 call setreg('"', '')
365 rviminfo Xviminfo
366 call assert_equal(s, getreg('"'), 'wrong register at length: ' . len)
367
368 let len += 1
369 endwhile
370
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200371 " If the maximum number of lines saved for a register ('<' in 'viminfo') is
372 " zero, then register values should not be saved.
373 let @a = 'abc'
374 set viminfo='100,<0,s10,h,!,nviminfo
375 wviminfo Xviminfo
376 let @a = 'xyz'
377 rviminfo! Xviminfo
378 call assert_equal('xyz', @a)
379 " repeat the test with '"' instead of '<'
380 let @b = 'def'
381 set viminfo='100,\"0,s10,h,!,nviminfo
382 wviminfo Xviminfo
383 let @b = 'rst'
384 rviminfo! Xviminfo
385 call assert_equal('rst', @b)
386
387 " If the maximum size of an item ('s' in 'viminfo') is zero, then register
388 " values should not be saved.
389 let @c = '123'
390 set viminfo='100,<20,s0,h,!,nviminfo
391 wviminfo Xviminfo
392 let @c = '456'
393 rviminfo! Xviminfo
394 call assert_equal('456', @c)
395
Bram Moolenaare80ff742016-06-11 21:14:18 +0200396 call delete('Xviminfo')
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200397 set viminfo&vim
Bram Moolenaare80ff742016-06-11 21:14:18 +0200398endfunc
399
Bram Moolenaar2d358992016-06-12 21:20:54 +0200400func Test_viminfo_marks()
401 sp bufa
402 let bufa = bufnr('%')
403 sp bufb
404 let bufb = bufnr('%')
405
406 call test_settime(8)
407 call setpos("'A", [bufa, 1, 1, 0])
408 call test_settime(20)
409 call setpos("'B", [bufb, 9, 1, 0])
410 call setpos("'C", [bufa, 7, 1, 0])
411
412 delmark 0-9
413 call test_settime(25)
414 call setpos("'1", [bufb, 12, 1, 0])
415 call test_settime(35)
416 call setpos("'0", [bufa, 11, 1, 0])
417
418 call test_settime(45)
419 wviminfo Xviminfo
420
421 " Writing viminfo inserts the '0 mark.
422 call assert_equal([bufb, 1, 1, 0], getpos("'0"))
423 call assert_equal([bufa, 11, 1, 0], getpos("'1"))
424 call assert_equal([bufb, 12, 1, 0], getpos("'2"))
425
426 call test_settime(4)
427 call setpos("'A", [bufa, 9, 1, 0])
428 call test_settime(30)
429 call setpos("'B", [bufb, 2, 3, 0])
430 delmark C
431
432 delmark 0-9
433 call test_settime(30)
434 call setpos("'1", [bufb, 22, 1, 0])
435 call test_settime(55)
436 call setpos("'0", [bufa, 21, 1, 0])
437
438 rviminfo Xviminfo
439
440 call assert_equal([bufa, 1, 1, 0], getpos("'A"))
441 call assert_equal([bufb, 2, 3, 0], getpos("'B"))
442 call assert_equal([bufa, 7, 1, 0], getpos("'C"))
443
444 " numbered marks are merged
445 call assert_equal([bufa, 21, 1, 0], getpos("'0")) " time 55
446 call assert_equal([bufb, 1, 1, 0], getpos("'1")) " time 45
447 call assert_equal([bufa, 11, 1, 0], getpos("'2")) " time 35
448 call assert_equal([bufb, 22, 1, 0], getpos("'3")) " time 30
449 call assert_equal([bufb, 12, 1, 0], getpos("'4")) " time 25
450
Bram Moolenaar8cd6cd82019-12-27 17:33:26 +0100451 " deleted file marks are removed from viminfo
452 delmark C
453 wviminfo Xviminfo
454 rviminfo Xviminfo
455 call assert_equal([0, 0, 0, 0], getpos("'C"))
456
457 " deleted file marks stay in viminfo if defined in another vim later
458 call test_settime(70)
459 call setpos("'D", [bufb, 8, 1, 0])
460 wviminfo Xviminfo
461 call test_settime(65)
462 delmark D
463 call assert_equal([0, 0, 0, 0], getpos("'D"))
464 call test_settime(75)
465 rviminfo Xviminfo
466 call assert_equal([bufb, 8, 1, 0], getpos("'D"))
467
Bram Moolenaar2d358992016-06-12 21:20:54 +0200468 call delete('Xviminfo')
469 exe 'bwipe ' . bufa
470 exe 'bwipe ' . bufb
471endfunc
472
473func Test_viminfo_jumplist()
474 split testbuf
475 clearjumps
476 call setline(1, ['time 05', 'time 10', 'time 15', 'time 20', 'time 30', 'last pos'])
477 call cursor(2, 1)
478 call test_settime(10)
479 exe "normal /20\r"
480 call test_settime(20)
481 exe "normal /30\r"
482 call test_settime(30)
483 exe "normal /last pos\r"
484 wviminfo Xviminfo
485
486 clearjumps
487 call cursor(1, 1)
488 call test_settime(5)
489 exe "normal /15\r"
490 call test_settime(15)
491 exe "normal /last pos\r"
492 call test_settime(40)
493 exe "normal ?30\r"
494 rviminfo Xviminfo
495
496 call assert_equal('time 30', getline('.'))
497 exe "normal \<C-O>"
498 call assert_equal('last pos', getline('.'))
499 exe "normal \<C-O>"
500 " duplicate for 'time 30' was removed
501 call assert_equal('time 20', getline('.'))
502 exe "normal \<C-O>"
503 call assert_equal('time 15', getline('.'))
504 exe "normal \<C-O>"
505 call assert_equal('time 10', getline('.'))
506 exe "normal \<C-O>"
507 call assert_equal('time 05', getline('.'))
508
Bram Moolenaarece74ab2016-06-13 22:22:15 +0200509 clearjumps
510 call cursor(1, 1)
511 call test_settime(5)
512 exe "normal /15\r"
513 call test_settime(15)
514 exe "normal /last pos\r"
515 call test_settime(40)
516 exe "normal ?30\r"
517 " Test merge when writing
518 wviminfo Xviminfo
519 clearjumps
520 rviminfo Xviminfo
521
Bram Moolenaar28607ba2016-06-15 21:44:51 +0200522 let last_line = line('.')
Bram Moolenaarece74ab2016-06-13 22:22:15 +0200523 exe "normal \<C-O>"
524 call assert_equal('time 30', getline('.'))
525 exe "normal \<C-O>"
526 call assert_equal('last pos', getline('.'))
527 exe "normal \<C-O>"
528 " duplicate for 'time 30' was removed
529 call assert_equal('time 20', getline('.'))
530 exe "normal \<C-O>"
531 call assert_equal('time 15', getline('.'))
532 exe "normal \<C-O>"
533 call assert_equal('time 10', getline('.'))
534 exe "normal \<C-O>"
535 call assert_equal('time 05', getline('.'))
536
Bram Moolenaar28607ba2016-06-15 21:44:51 +0200537 " Test with jumplist full.
538 clearjumps
539 call setline(1, repeat(['match here'], 101))
540 call cursor(1, 1)
541 call test_settime(10)
542 for i in range(100)
543 exe "normal /here\r"
544 endfor
545 rviminfo Xviminfo
546
547 " must be newest mark that comes from viminfo.
548 exe "normal \<C-O>"
549 call assert_equal(last_line, line('.'))
550
Bram Moolenaar2d358992016-06-12 21:20:54 +0200551 bwipe!
552 call delete('Xviminfo')
553endfunc
554
Bram Moolenaar01227092016-06-11 14:47:40 +0200555func Test_viminfo_encoding()
Bram Moolenaar01227092016-06-11 14:47:40 +0200556 set enc=latin1
557 call histdel(':')
558 call histadd(':', "echo '\xe9'")
559 wviminfo Xviminfo
560
561 set fencs=utf-8,latin1
562 set enc=utf-8
563 sp Xviminfo
564 call assert_equal('latin1', &fenc)
565 close
566
567 call histdel(':')
568 rviminfo Xviminfo
569 call assert_equal("echo 'é'", histget(':', -1))
570
571 call delete('Xviminfo')
572endfunc
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +0200573
574func Test_viminfo_bad_syntax()
575 let lines = []
576 call add(lines, '|<') " empty continuation line
577 call add(lines, '|234234234234234324,nothing')
578 call add(lines, '|1+"no comma"')
579 call add(lines, '|1,2,3,4,5,6,7') " too many items
580 call add(lines, '|1,"string version"')
581 call add(lines, '|1,>x') " bad continuation line
582 call add(lines, '|1,"x') " missing quote
583 call add(lines, '|1,"x\') " trailing backslash
584 call add(lines, '|1,,,,') "trailing comma
585 call add(lines, '|1,>234') " trailing continuation line
586 call writefile(lines, 'Xviminfo')
Bram Moolenaare80ff742016-06-11 21:14:18 +0200587 rviminfo Xviminfo
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +0200588
589 call delete('Xviminfo')
590endfunc
591
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200592func Test_viminfo_bad_syntax2()
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +0200593 let lines = []
594 call add(lines, '|1,4')
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200595
596 " bad viminfo syntax for history barline
597 call add(lines, '|2') " invalid number of fields in a history barline
598 call add(lines, '|2,9,1,1,"x"') " invalid value for the history type
599 call add(lines, '|2,0,,1,"x"') " no timestamp
600 call add(lines, '|2,0,1,1,10') " non-string text
601
602 " bad viminfo syntax for register barline
603 call add(lines, '|3') " invalid number of fields in a register barline
604 call add(lines, '|3,1,1,1,1,,1,"x"') " missing width field
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +0200605 call add(lines, '|3,0,80,1,1,1,1,"x"') " invalid register number
606 call add(lines, '|3,0,10,5,1,1,1,"x"') " invalid register type
607 call add(lines, '|3,0,10,1,20,1,1,"x"') " invalid line count
608 call add(lines, '|3,0,10,1,0,1,1') " zero line count
Yegappan Lakshmananf1e74492021-06-21 18:44:26 +0200609
610 " bad viminfo syntax for mark barline
611 call add(lines, '|4') " invalid number of fields in a mark barline
612 call add(lines, '|4,1,1,1,1,1') " invalid value for file name
613 call add(lines, '|4,20,1,1,1,"x"') " invalid value for file name
614 call add(lines, '|4,49,0,1,1,"x"') " invalid value for line number
615
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +0200616 call writefile(lines, 'Xviminfo')
617 rviminfo Xviminfo
618 call delete('Xviminfo')
619endfunc
620
Bram Moolenaarab9c89b2016-07-03 17:47:26 +0200621func Test_viminfo_file_marks()
622 silent! bwipe test_viminfo.vim
623 silent! bwipe Xviminfo
624
625 call test_settime(10)
626 edit ten
627 call test_settime(25)
628 edit again
629 call test_settime(30)
630 edit thirty
631 wviminfo Xviminfo
632
633 call test_settime(20)
634 edit twenty
635 call test_settime(35)
636 edit again
637 call test_settime(40)
Dominique Pelle923dce22021-11-21 11:36:04 +0000638 edit forty
Bram Moolenaarab9c89b2016-07-03 17:47:26 +0200639 wviminfo Xviminfo
640
641 sp Xviminfo
642 1
Dominique Pelle923dce22021-11-21 11:36:04 +0000643 for name in ['forty', 'again', 'thirty', 'twenty', 'ten']
Bram Moolenaarab9c89b2016-07-03 17:47:26 +0200644 /^>
645 call assert_equal(name, substitute(getline('.'), '.*/', '', ''))
646 endfor
647 close
648
649 call delete('Xviminfo')
650endfunc
Bram Moolenaare59215c2016-08-14 19:08:45 +0200651
652func Test_viminfo_file_mark_tabclose()
653 tabnew Xtestfileintab
654 call setline(1, ['a','b','c','d','e'])
655 4
656 q!
657 wviminfo Xviminfo
658 sp Xviminfo
659 /^> .*Xtestfileintab
660 let lnum = line('.')
661 while 1
662 if lnum == line('$')
Bram Moolenaar37175402017-03-18 20:18:45 +0100663 call assert_report('mark not found in Xtestfileintab')
Bram Moolenaare59215c2016-08-14 19:08:45 +0200664 break
665 endif
666 let lnum += 1
667 let line = getline(lnum)
668 if line == ''
Bram Moolenaar37175402017-03-18 20:18:45 +0100669 call assert_report('mark not found in Xtestfileintab')
Bram Moolenaare59215c2016-08-14 19:08:45 +0200670 break
671 endif
672 if line =~ "^\t\""
673 call assert_equal('4', substitute(line, ".*\"\t\\(\\d\\).*", '\1', ''))
674 break
675 endif
676 endwhile
677
678 call delete('Xviminfo')
679 silent! bwipe Xtestfileintab
680endfunc
Bram Moolenaare11d61a2016-08-20 18:36:54 +0200681
Bram Moolenaar156919f2016-10-15 20:46:20 +0200682func Test_viminfo_file_mark_zero_time()
683 let lines = [
684 \ '# Viminfo version',
685 \ '|1,4',
686 \ '',
687 \ '*encoding=utf-8',
688 \ '',
689 \ '# File marks:',
690 \ "'B 1 0 /tmp/nothing",
691 \ '|4,66,1,0,0,"/tmp/nothing"',
692 \ "",
693 \ ]
694 call writefile(lines, 'Xviminfo')
695 delmark B
696 rviminfo Xviminfo
697 call delete('Xviminfo')
698 call assert_equal(1, line("'B"))
699 delmark B
700endfunc
701
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200702" Test for saving and restoring file marks in unloaded buffers
703func Test_viminfo_file_mark_unloaded_buf()
704 let save_viminfo = &viminfo
705 set viminfo&vim
706 call writefile(repeat(['vim'], 10), 'Xfile1')
707 %bwipe
708 edit! Xfile1
709 call setpos("'u", [0, 3, 1, 0])
710 call setpos("'v", [0, 5, 1, 0])
711 enew
712 wviminfo Xviminfo
713 %bwipe
714 edit Xfile1
715 rviminfo! Xviminfo
716 call assert_equal([0, 3, 1, 0], getpos("'u"))
717 call assert_equal([0, 5, 1, 0], getpos("'v"))
718 %bwipe
719 call delete('Xfile1')
720 call delete('Xviminfo')
721 let &viminfo = save_viminfo
722endfunc
723
Bram Moolenaar156919f2016-10-15 20:46:20 +0200724func Test_viminfo_oldfiles()
K.Takata0500e872022-09-08 12:28:02 +0100725 set noswapfile
Bram Moolenaare11d61a2016-08-20 18:36:54 +0200726 let v:oldfiles = []
727 let lines = [
728 \ '# comment line',
729 \ '*encoding=utf-8',
730 \ '',
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200731 \ ':h viminfo',
732 \ '?/session',
733 \ '=myvar',
734 \ '@123',
735 \ '',
736 \ "'E 2 0 /tmp/nothing",
737 \ '',
Bram Moolenaare11d61a2016-08-20 18:36:54 +0200738 \ "> /tmp/file_one.txt",
739 \ "\t\"\t11\t0",
740 \ "",
741 \ "> /tmp/file_two.txt",
742 \ "\t\"\t11\t0",
743 \ "",
744 \ "> /tmp/another.txt",
745 \ "\t\"\t11\t0",
746 \ "",
747 \ ]
748 call writefile(lines, 'Xviminfo')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200749 delmark E
Bram Moolenaar5affc032021-02-11 18:36:30 +0100750 edit /tmp/file_two.txt
Bram Moolenaare11d61a2016-08-20 18:36:54 +0200751 rviminfo! Xviminfo
752 call delete('Xviminfo')
753
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200754 call assert_equal('h viminfo', histget(':'))
755 call assert_equal('session', histget('/'))
756 call assert_equal('myvar', histget('='))
757 call assert_equal('123', histget('@'))
758 call assert_equal(2, line("'E"))
Bram Moolenaar7b668e82016-08-23 23:51:21 +0200759 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/'}))
760 call assert_equal(['1: /tmp/file_one.txt', '2: /tmp/file_two.txt'], filter(split(execute('filter file_ oldfiles'), "\n"), {i, v -> v =~ '/tmp/'}))
761 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 +0200762
763 new
764 call feedkeys("3\<CR>", 't')
765 browse oldfiles
766 call assert_equal("/tmp/another.txt", expand("%"))
767 bwipe
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200768 delmark E
K.Takata0500e872022-09-08 12:28:02 +0100769 set swapfile&
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200770endfunc
771
772" Test for storing and restoring buffer list in 'viminfo'
773func Test_viminfo_bufferlist()
774 " If there are arguments, then :rviminfo doesn't read the buffer list.
775 " Need to delete all the arguments for :rviminfo to work.
776 %argdelete
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +0200777 set viminfo&vim
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200778
779 edit Xfile1
780 edit Xfile2
781 set viminfo-=%
782 wviminfo Xviminfo
783 %bwipe
784 rviminfo Xviminfo
785 call assert_equal(1, len(getbufinfo()))
786
787 edit Xfile1
788 edit Xfile2
789 set viminfo^=%
790 wviminfo Xviminfo
791 %bwipe
792 rviminfo Xviminfo
793 let l = getbufinfo()
794 call assert_equal(3, len(l))
795 call assert_equal('Xfile1', bufname(l[1].bufnr))
796 call assert_equal('Xfile2', bufname(l[2].bufnr))
797
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +0200798 " The quickfix, terminal, unlisted, unnamed buffers are not stored in the
799 " viminfo file
800 %bw!
801 edit Xfile1
802 new
803 setlocal nobuflisted
804 new
805 copen
806 if has('terminal')
807 terminal
808 endif
809 wviminfo! Xviminfo
810 %bwipe!
811 rviminfo Xviminfo
812 let l = getbufinfo()
813 call assert_equal(2, len(l))
814 call assert_true(bufexists('Xfile1'))
815
816 " If a count is specified for '%', then only that many buffers should be
817 " stored in the viminfo file.
818 %bw!
819 set viminfo&vim
820 new Xbuf1
821 new Xbuf2
822 set viminfo+=%1
823 wviminfo! Xviminfo
824 %bwipe!
825 rviminfo! Xviminfo
826 let l = getbufinfo()
827 call assert_equal(2, len(l))
828 call assert_true(bufexists('Xbuf1'))
829 call assert_false(bufexists('Xbuf2'))
830
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200831 call delete('Xviminfo')
832 %bwipe
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +0200833 set viminfo&vim
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200834endfunc
835
836" Test for errors in a viminfo file
837func Test_viminfo_error()
838 " Non-existing viminfo files
839 call assert_fails('rviminfo xyz', 'E195:')
840
841 " Illegal starting character
842 call writefile(["a 123"], 'Xviminfo')
843 call assert_fails('rv Xviminfo', 'E575:')
844
845 " Illegal register name in the viminfo file
846 call writefile(['"@ LINE 0'], 'Xviminfo')
847 call assert_fails('rv Xviminfo', 'E577:')
848
849 " Invalid file mark line
850 call writefile(['>', '@'], 'Xviminfo')
851 call assert_fails('rv Xviminfo', 'E576:')
852
853 " Too many errors in viminfo file
854 call writefile(repeat(["a 123"], 15), 'Xviminfo')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200855 call assert_fails('rv Xviminfo', 'E575:')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200856
857 call writefile(['>'] + repeat(['@'], 10), 'Xviminfo')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200858 call assert_fails('rv Xviminfo', 'E576:')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200859
860 call writefile(repeat(['"@'], 15), 'Xviminfo')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200861 call assert_fails('rv Xviminfo', 'E577:')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200862
863 call delete('Xviminfo')
864endfunc
865
866" Test for saving and restoring last substitute string in viminfo
867func Test_viminfo_lastsub()
868 enew
869 call append(0, "blue blue blue")
870 call cursor(1, 1)
871 s/blue/green/
872 wviminfo Xviminfo
873 s/blue/yellow/
874 rviminfo! Xviminfo
875 &
876 call assert_equal("green yellow green", getline(1))
877 enew!
878 call delete('Xviminfo')
879endfunc
880
881" Test saving and restoring the register values using the older method
882func Test_viminfo_registers_old()
883 let lines = [
884 \ '# Viminfo version',
885 \ '|1,1',
886 \ '',
887 \ '*encoding=utf-8',
888 \ '',
889 \ '# Registers:',
890 \ '""0 CHAR 0',
891 \ ' Vim',
892 \ '"a CHAR 0',
893 \ ' red',
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +0200894 \ '"c BLOCK 0',
895 \ ' a',
896 \ ' d',
897 \ '"d LINE 0',
898 \ ' abc',
899 \ ' def',
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200900 \ '"m@ CHAR 0',
901 \ " :echo 'Hello'\<CR>",
902 \ "",
903 \ ]
904 call writefile(lines, 'Xviminfo')
905 let @a = 'one'
906 let @b = 'two'
907 let @m = 'three'
908 let @" = 'four'
909 let @t = ":echo 'Unix'\<CR>"
910 silent! normal @t
911 rviminfo! Xviminfo
912 call assert_equal('red', getreg('a'))
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +0200913 call assert_equal("v", getregtype('a'))
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200914 call assert_equal('two', getreg('b'))
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +0200915 call assert_equal("a\nd", getreg('c'))
916 call assert_equal("\<C-V>1", getregtype('c'))
917 call assert_equal("abc\ndef\n", getreg('d'))
918 call assert_equal("V", getregtype('d'))
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200919 call assert_equal(":echo 'Hello'\<CR>", getreg('m'))
920 call assert_equal('Vim', getreg('"'))
921 call assert_equal("\nHello", execute('normal @@'))
922 call delete('Xviminfo')
923 let @" = ''
924endfunc
925
926" Test for saving and restoring large number of lines in a register
927func Test_viminfo_large_register()
928 let save_viminfo = &viminfo
929 set viminfo&vim
930 set viminfo-=<50
931 set viminfo+=<200
932 let lines = ['"r CHAR 0']
933 call extend(lines, repeat(["\tsun is rising"], 200))
934 call writefile(lines, 'Xviminfo')
935 let @r = ''
936 rviminfo! Xviminfo
937 call assert_equal(join(repeat(["sun is rising"], 200), "\n"), @r)
938 call delete('Xviminfo')
Bram Moolenaar6bd1d772019-10-09 22:01:25 +0200939 let @r = ''
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200940 let &viminfo = save_viminfo
941endfunc
942
943" Test for setting 'viminfofile' to NONE
944func Test_viminfofile_none()
Bram Moolenaar6bd1d772019-10-09 22:01:25 +0200945 let save_vif = &viminfofile
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200946 set viminfofile=NONE
947 wviminfo Xviminfo
948 call assert_false(filereadable('Xviminfo'))
949 call writefile([''], 'Xviminfo')
950 call assert_fails('rviminfo Xviminfo', 'E195:')
951 call delete('Xviminfo')
Bram Moolenaar6bd1d772019-10-09 22:01:25 +0200952 let &viminfofile = save_vif
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200953endfunc
954
Dominique Pelle923dce22021-11-21 11:36:04 +0000955" Test for an unwritable and unreadable 'viminfo' file
Bram Moolenaar6bd1d772019-10-09 22:01:25 +0200956func Test_viminfo_perm()
957 CheckUnix
Bram Moolenaar07282f02019-10-10 16:46:17 +0200958 CheckNotRoot
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200959 call writefile([''], 'Xviminfo')
960 call setfperm('Xviminfo', 'r-x------')
961 call assert_fails('wviminfo Xviminfo', 'E137:')
Bram Moolenaar6bd1d772019-10-09 22:01:25 +0200962 call setfperm('Xviminfo', '--x------')
963 call assert_fails('rviminfo Xviminfo', 'E195:')
Bram Moolenaar2a8d3b82019-10-04 21:20:25 +0200964 call delete('Xviminfo')
Bram Moolenaarcde0ff32020-04-04 14:00:39 +0200965
966 " Try to write the viminfo to a directory
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100967 call mkdir('Xvifdir')
968 call assert_fails('wviminfo Xvifdir', 'E137:')
969 call assert_fails('rviminfo Xvifdir', 'E195:')
970 call delete('Xvifdir', 'rf')
Bram Moolenaare11d61a2016-08-20 18:36:54 +0200971endfunc
Bram Moolenaar6bd1d772019-10-09 22:01:25 +0200972
973" Test for writing to an existing viminfo file merges the file marks
974func XTest_viminfo_marks_merge()
975 let save_viminfo = &viminfo
976 set viminfo&vim
977 set viminfo^=%
978 enew
979 %argdelete
980 %bwipe
981
982 call writefile(repeat(['editor'], 10), 'Xbufa')
983 call writefile(repeat(['Vim'], 10), 'Xbufb')
984
985 " set marks in buffers
986 call test_settime(10)
987 edit Xbufa
988 4mark a
989 wviminfo Xviminfo
990 edit Xbufb
991 4mark b
992 wviminfo Xviminfo
993 %bwipe
994
995 " set marks in buffers again
996 call test_settime(20)
997 edit Xbufb
998 6mark b
999 wviminfo Xviminfo
1000 edit Xbufa
1001 6mark a
1002 wviminfo Xviminfo
1003 %bwipe
1004
1005 " Load the buffer and check the marks
1006 edit Xbufa
1007 rviminfo! Xviminfo
1008 call assert_equal(6, line("'a"))
1009 edit Xbufb
1010 rviminfo! Xviminfo
1011 call assert_equal(6, line("'b"))
1012
1013 " cleanup
1014 %bwipe
1015 call delete('Xviminfo')
1016 call delete('Xbufa')
1017 call delete('Xbufb')
1018 call test_settime(0)
1019 let &viminfo=save_viminfo
1020endfunc
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001021
1022" Test for errors in setting 'viminfo'
1023func Test_viminfo_option_error()
1024 " Missing number
1025 call assert_fails('set viminfo=\"', 'E526:')
1026 for c in split("'/:<@s", '\zs')
1027 call assert_fails('set viminfo=' .. c, 'E526:')
1028 endfor
1029
1030 " Missing comma
1031 call assert_fails('set viminfo=%10!', 'E527:')
1032 call assert_fails('set viminfo=!%10', 'E527:')
1033 call assert_fails('set viminfo=h%10', 'E527:')
1034 call assert_fails('set viminfo=c%10', 'E527:')
1035 call assert_fails('set viminfo=:10%10', 'E527:')
1036
1037 " Missing ' setting
1038 call assert_fails('set viminfo=%10', 'E528:')
1039endfunc
1040
Bram Moolenaar8e6be342020-11-23 22:01:26 +01001041func Test_viminfo_oldfiles_newfile()
1042 CheckRunVimInTerminal
1043
1044 let save_viminfo = &viminfo
1045 let save_viminfofile = &viminfofile
1046 set viminfo&vim
1047 let v:oldfiles = []
1048 let commands =<< trim [CODE]
1049 set viminfofile=Xviminfofile
1050 set viminfo&vim
1051 w! Xnew-file.txt
1052 qall
1053 [CODE]
1054 call writefile(commands, 'Xviminfotest')
1055 let buf = RunVimInTerminal('-S Xviminfotest', #{wait_for_ruler: 0})
1056 call WaitForAssert({-> assert_equal("finished", term_getstatus(buf))})
1057
1058 let &viminfofile = 'Xviminfofile'
1059 rviminfo! Xviminfofile
1060 call assert_match('Xnew-file.txt$', v:oldfiles[0])
1061 call assert_equal(1, len(v:oldfiles))
1062 call delete('Xviminfofile')
1063 call delete('Xviminfotest')
1064 call delete('Xnew-file.txt')
Bram Moolenaar6fd367a2021-03-13 13:14:04 +01001065
1066 let v:oldfiles = test_null_list()
1067 call assert_equal("\nNo old files", execute('oldfiles'))
1068
Bram Moolenaar8e6be342020-11-23 22:01:26 +01001069 let &viminfo = save_viminfo
1070 let &viminfofile = save_viminfofile
1071endfunc
1072
Yegappan Lakshmanan41a7f822021-06-16 15:53:17 +02001073" When writing CTRL-V or "\n" to a viminfo file, it is converted to CTRL-V
1074" CTRL-V and CTRL-V n respectively.
1075func Test_viminfo_with_Ctrl_V()
1076 silent! exe "normal! /\<C-V>\<C-V>\n"
1077 wviminfo Xviminfo
1078 call assert_notequal(-1, readfile('Xviminfo')->index("?/\<C-V>\<C-V>"))
1079 let @/ = 'abc'
1080 rviminfo! Xviminfo
1081 call assert_equal("\<C-V>", @/)
1082 silent! exe "normal! /\<C-V>\<C-J>\n"
1083 wviminfo Xviminfo
1084 call assert_notequal(-1, readfile('Xviminfo')->index("?/\<C-V>n"))
1085 let @/ = 'abc'
1086 rviminfo! Xviminfo
1087 call assert_equal("\n", @/)
1088 call delete('Xviminfo')
1089endfunc
1090
1091" Test for the 'r' field in 'viminfo' (removal media)
1092func Test_viminfo_removable_media()
1093 CheckUnix
1094 if !isdirectory('/tmp') || getftype('/tmp') != 'dir'
1095 return
1096 endif
1097 let save_viminfo = &viminfo
1098 set viminfo+=r/tmp
1099 edit /tmp/Xvima1b2c3
1100 wviminfo Xviminfo
1101 let matches = readfile('Xviminfo')->filter("v:val =~ 'Xvima1b2c3'")
1102 call assert_equal(0, matches->len())
1103 let &viminfo = save_viminfo
1104 call delete('Xviminfo')
1105endfunc
1106
1107" Test for the 'h' flag in 'viminfo'. If 'h' is not present, then the last
1108" search pattern read from 'viminfo' should be highlighted with 'hlsearch'.
1109" If 'h' is present, then the last search pattern should not be highlighted.
1110func Test_viminfo_hlsearch()
1111 set viminfo&vim
1112
1113 new
1114 call setline(1, ['one two three'])
1115 " save the screen attribute for the Search highlighted text and the normal
1116 " text for later comparison
1117 set hlsearch
1118 let @/ = 'three'
1119 redraw!
1120 let hiSearch = screenattr(1, 9)
1121 let hiNormal = screenattr(1, 1)
1122
1123 set viminfo-=h
1124 let @/='two'
1125 wviminfo! Xviminfo
1126 let @/='one'
1127 rviminfo! Xviminfo
1128 redraw!
1129 call assert_equal(hiSearch, screenattr(1, 5))
1130 call assert_equal(hiSearch, screenattr(1, 6))
1131 call assert_equal(hiSearch, screenattr(1, 7))
1132
1133 set viminfo+=h
1134 let @/='two'
1135 wviminfo! Xviminfo
1136 let @/='one'
1137 rviminfo! Xviminfo
1138 redraw!
1139 call assert_equal(hiNormal, screenattr(1, 5))
1140 call assert_equal(hiNormal, screenattr(1, 6))
1141 call assert_equal(hiNormal, screenattr(1, 7))
1142
1143 call delete('Xviminfo')
1144 set hlsearch& viminfo&vim
1145 bw!
1146endfunc
1147
1148" Test for restoring the magicness of the last search pattern from the viminfo
1149" file.
1150func Test_viminfo_last_spat_magic()
1151 set viminfo&vim
1152 new
1153 call setline(1, ' one abc a.c')
1154
1155 " restore 'nomagic'
1156 set nomagic
1157 exe "normal gg/a.c\<CR>"
1158 wviminfo! Xviminfo
1159 set magic
1160 exe "normal gg/one\<CR>"
1161 rviminfo! Xviminfo
1162 exe "normal! gg/\<CR>"
1163 call assert_equal(10, col('.'))
1164
1165 " restore 'magic'
1166 set magic
1167 exe "normal gg/a.c\<CR>"
1168 wviminfo! Xviminfo
1169 set nomagic
1170 exe "normal gg/one\<CR>"
1171 rviminfo! Xviminfo
1172 exe "normal! gg/\<CR>"
1173 call assert_equal(6, col('.'))
1174
1175 call delete('Xviminfo')
1176 set viminfo&vim magic&
1177 bw!
1178endfunc
1179
1180" Test for restoring the smartcase of the last search pattern from the viminfo
1181" file.
1182func Test_viminfo_last_spat_smartcase()
1183 new
1184 call setline(1, ' one abc Abc')
1185 set ignorecase smartcase
1186
1187 " Searching with * should disable smartcase
1188 exe "normal! gg$b*"
1189 wviminfo! Xviminfo
1190 exe "normal gg/one\<CR>"
1191 rviminfo! Xviminfo
1192 exe "normal! gg/\<CR>"
1193 call assert_equal(6, col('.'))
1194
1195 call delete('Xviminfo')
1196 set ignorecase& smartcase& viminfo&
1197 bw!
1198endfunc
1199
1200" Test for restoring the last search pattern with a line or character offset
1201" from the viminfo file.
1202func Test_viminfo_last_spat_offset()
1203 new
1204 call setline(1, ['one', 'two', 'three', 'four', 'five'])
1205 " line offset
1206 exe "normal! /two/+2\<CR>"
1207 wviminfo! Xviminfo
1208 exe "normal gg/five\<CR>"
1209 rviminfo! Xviminfo
1210 exe "normal! gg/\<CR>"
1211 call assert_equal(4, line('.'))
1212 " character offset
1213 exe "normal! gg/^th/e+2\<CR>"
1214 wviminfo! Xviminfo
1215 exe "normal gg/two\<CR>"
1216 rviminfo! Xviminfo
1217 exe "normal! gg/\<CR>"
1218 call assert_equal([3, 4], [line('.'), col('.')])
1219 call delete('Xviminfo')
1220 bw!
1221endfunc
1222
1223" Test for saving and restoring the last executed register (@ command)
1224" from the viminfo file
1225func Test_viminfo_last_exec_reg()
1226 let g:val = 1
1227 let @a = ":let g:val += 1\n"
1228 normal! @a
1229 wviminfo! Xviminfo
1230 let @b = ''
1231 normal! @b
1232 rviminfo! Xviminfo
1233 normal @@
1234 call assert_equal(3, g:val)
1235 call delete('Xviminfo')
1236endfunc
1237
1238" Test for merging file marks in a viminfo file
1239func Test_viminfo_merge_file_marks()
1240 for [f, l, t] in [['a.txt', 5, 10], ['b.txt', 10, 20]]
1241 call test_settime(t)
1242 exe 'edit ' .. f
1243 call setline(1, range(1, 20))
1244 exe l . 'mark a'
1245 wviminfo Xviminfo
1246 bw!
1247 endfor
1248 call test_settime(30)
1249 for [f, l] in [['a.txt', 5], ['b.txt', 10]]
1250 exe 'edit ' .. f
1251 rviminfo! Xviminfo
1252 call assert_equal(l, line("'a"))
1253 bw!
1254 endfor
1255 call delete('Xviminfo')
1256 call test_settime(0)
1257endfunc
1258
1259" Test for merging file marks from a old viminfo file
1260func Test_viminfo_merge_old_filemarks()
1261 let lines = []
1262 call add(lines, '|1,4')
1263 call add(lines, '> ' .. fnamemodify('a.txt', ':p:~'))
1264 call add(lines, "\tb\t7\t0\n")
1265 call writefile(lines, 'Xviminfo')
1266 edit b.txt
1267 call setline(1, range(1, 20))
1268 12mark b
1269 wviminfo Xviminfo
1270 bw!
1271 edit a.txt
1272 rviminfo! Xviminfo
1273 call assert_equal(7, line("'b"))
1274 edit b.txt
1275 rviminfo! Xviminfo
1276 call assert_equal(12, line("'b"))
1277 call delete('Xviminfo')
1278endfunc
1279
1280" Test for merging the jump list from a old viminfo file
1281func Test_viminfo_merge_old_jumplist()
1282 let lines = []
1283 call add(lines, "-' 10 1 " .. fnamemodify('a.txt', ':p:~'))
1284 call add(lines, "-' 20 1 " .. fnamemodify('a.txt', ':p:~'))
1285 call add(lines, "-' 30 1 " .. fnamemodify('b.txt', ':p:~'))
1286 call add(lines, "-' 40 1 " .. fnamemodify('b.txt', ':p:~'))
1287 call writefile(lines, 'Xviminfo')
1288 clearjumps
1289 rviminfo! Xviminfo
1290 let l = getjumplist()[0]
1291 call assert_equal([40, 30, 20, 10], [l[0].lnum, l[1].lnum, l[2].lnum,
1292 \ l[3].lnum])
1293 bw!
1294 call delete('Xviminfo')
1295endfunc
1296
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02001297" vim: shiftwidth=2 sts=2 expandtab