blob: 50f005ad18873567d62bcf13af40d95538518d8b [file] [log] [blame]
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001" Test for marks
Bram Moolenaar19a16692016-09-01 22:19:47 +02002
3" Test that a deleted mark is restored after delete-undo-redo-undo.
Bram Moolenaar1e115362019-01-09 23:01:02 +01004func Test_Restore_DelMark()
Bram Moolenaar19a16692016-09-01 22:19:47 +02005 enew!
6 call append(0, [" textline A", " textline B", " textline C"])
7 normal! 2gg
8 set nocp viminfo+=nviminfo
9 exe "normal! i\<C-G>u\<Esc>"
10 exe "normal! maddu\<C-R>u"
11 let pos = getpos("'a")
12 call assert_equal(2, pos[1])
13 call assert_equal(1, pos[2])
14 enew!
Bram Moolenaar1e115362019-01-09 23:01:02 +010015endfunc
Bram Moolenaar19a16692016-09-01 22:19:47 +020016
17" Test that CTRL-A and CTRL-X updates last changed mark '[, '].
Bram Moolenaar1e115362019-01-09 23:01:02 +010018func Test_Incr_Marks()
Bram Moolenaar19a16692016-09-01 22:19:47 +020019 enew!
20 call append(0, ["123 123 123", "123 123 123", "123 123 123"])
21 normal! gg
22 execute "normal! \<C-A>`[v`]rAjwvjw\<C-X>`[v`]rX"
23 call assert_equal("AAA 123 123", getline(1))
24 call assert_equal("123 XXXXXXX", getline(2))
25 call assert_equal("XXX 123 123", getline(3))
26 enew!
Bram Moolenaar1e115362019-01-09 23:01:02 +010027endfunc
Bram Moolenaarf13e00b2017-01-28 18:23:54 +010028
Bram Moolenaare08aee62021-10-17 21:53:58 +010029func Test_previous_jump_mark()
30 new
31 call setline(1, ['']->repeat(6))
32 normal Ggg
33 call assert_equal(6, getpos("''")[1])
34 normal jjjjj
35 call assert_equal(6, getpos("''")[1])
36 bwipe!
37endfunc
38
Bram Moolenaarf13e00b2017-01-28 18:23:54 +010039func Test_setpos()
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010040 new Xone
Bram Moolenaarf13e00b2017-01-28 18:23:54 +010041 let onebuf = bufnr('%')
42 let onewin = win_getid()
43 call setline(1, ['aaa', 'bbb', 'ccc'])
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010044 new Xtwo
Bram Moolenaarf13e00b2017-01-28 18:23:54 +010045 let twobuf = bufnr('%')
46 let twowin = win_getid()
47 call setline(1, ['aaa', 'bbb', 'ccc'])
48
49 " for the cursor the buffer number is ignored
50 call setpos(".", [0, 2, 1, 0])
51 call assert_equal([0, 2, 1, 0], getpos("."))
52 call setpos(".", [onebuf, 3, 3, 0])
53 call assert_equal([0, 3, 3, 0], getpos("."))
54
55 call setpos("''", [0, 1, 3, 0])
56 call assert_equal([0, 1, 3, 0], getpos("''"))
57 call setpos("''", [onebuf, 2, 2, 0])
58 call assert_equal([0, 2, 2, 0], getpos("''"))
59
60 " buffer-local marks
61 for mark in ["'a", "'\"", "'[", "']", "'<", "'>"]
62 call win_gotoid(twowin)
63 call setpos(mark, [0, 2, 1, 0])
64 call assert_equal([0, 2, 1, 0], getpos(mark), "for mark " . mark)
65 call setpos(mark, [onebuf, 1, 3, 0])
66 call win_gotoid(onewin)
67 call assert_equal([0, 1, 3, 0], getpos(mark), "for mark " . mark)
68 endfor
69
70 " global marks
71 call win_gotoid(twowin)
72 call setpos("'N", [0, 2, 1, 0])
73 call assert_equal([twobuf, 2, 1, 0], getpos("'N"))
74 call setpos("'N", [onebuf, 1, 3, 0])
75 call assert_equal([onebuf, 1, 3, 0], getpos("'N"))
76
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010077 " try invalid column and check virtcol()
Bram Moolenaarf13e00b2017-01-28 18:23:54 +010078 call win_gotoid(onewin)
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010079 call setpos("'a", [0, 1, 2, 0])
80 call assert_equal([0, 1, 2, 0], getpos("'a"))
81 call setpos("'a", [0, 1, -5, 0])
82 call assert_equal([0, 1, 2, 0], getpos("'a"))
83 call setpos("'a", [0, 1, 0, 0])
84 call assert_equal([0, 1, 1, 0], getpos("'a"))
85 call setpos("'a", [0, 1, 4, 0])
86 call assert_equal([0, 1, 4, 0], getpos("'a"))
87 call assert_equal(4, virtcol("'a"))
88 call setpos("'a", [0, 1, 5, 0])
89 call assert_equal([0, 1, 5, 0], getpos("'a"))
90 call assert_equal(4, virtcol("'a"))
91 call setpos("'a", [0, 1, 21341234, 0])
92 call assert_equal([0, 1, 21341234, 0], getpos("'a"))
93 call assert_equal(4, virtcol("'a"))
94
Bram Moolenaar8b633132020-03-20 18:20:51 +010095 " Test with invalid buffer number, line number and column number
96 call cursor(2, 2)
97 call setpos('.', [-1, 1, 1, 0])
98 call assert_equal([2, 2], [line('.'), col('.')])
99 call setpos('.', [0, -1, 1, 0])
100 call assert_equal([2, 2], [line('.'), col('.')])
101 call setpos('.', [0, 1, -1, 0])
102 call assert_equal([2, 2], [line('.'), col('.')])
103
Bram Moolenaar0e05de42020-03-25 22:23:46 +0100104 call assert_fails("call setpos('ab', [0, 1, 1, 0])", 'E474:')
105
Bram Moolenaarf13e00b2017-01-28 18:23:54 +0100106 bwipe!
107 call win_gotoid(twowin)
108 bwipe!
109endfunc
Bram Moolenaar9b69f222017-10-08 21:53:15 +0200110
111func Test_marks_cmd()
112 new Xone
113 call setline(1, ['aaa', 'bbb'])
114 norm! maG$mB
115 w!
116 new Xtwo
117 call setline(1, ['ccc', 'ddd'])
118 norm! $mcGmD
Bram Moolenaar54c3fcd2020-07-19 22:09:06 +0200119 exe "norm! GVgg\<Esc>G"
Bram Moolenaar9b69f222017-10-08 21:53:15 +0200120 w!
121
122 b Xone
Bram Moolenaar9d5185b2018-07-08 17:57:34 +0200123 let a = split(execute('marks'), "\n")
Bram Moolenaar9b69f222017-10-08 21:53:15 +0200124 call assert_equal(9, len(a))
Bram Moolenaar54c3fcd2020-07-19 22:09:06 +0200125 call assert_equal(['mark line col file/text',
126 \ " ' 2 0 bbb",
127 \ ' a 1 0 aaa',
128 \ ' B 2 2 bbb',
129 \ ' D 2 0 Xtwo',
130 \ ' " 1 0 aaa',
131 \ ' [ 1 0 aaa',
132 \ ' ] 2 0 bbb',
133 \ ' . 2 0 bbb'], a)
Bram Moolenaar9b69f222017-10-08 21:53:15 +0200134
135 b Xtwo
Bram Moolenaar9d5185b2018-07-08 17:57:34 +0200136 let a = split(execute('marks'), "\n")
Bram Moolenaar54c3fcd2020-07-19 22:09:06 +0200137 call assert_equal(11, len(a))
138 call assert_equal(['mark line col file/text',
139 \ " ' 1 0 ccc",
140 \ ' c 1 2 ccc',
141 \ ' B 2 2 Xone',
142 \ ' D 2 0 ddd',
143 \ ' " 2 0 ddd',
144 \ ' [ 1 0 ccc',
145 \ ' ] 2 0 ddd',
146 \ ' . 2 0 ddd',
147 \ ' < 1 0 ccc',
148 \ ' > 2 0 ddd'], a)
149 norm! Gdd
150 w!
151 let a = split(execute('marks <>'), "\n")
152 call assert_equal(3, len(a))
153 call assert_equal(['mark line col file/text',
154 \ ' < 1 0 ccc',
155 \ ' > 2 0 -invalid-'], a)
Bram Moolenaar9b69f222017-10-08 21:53:15 +0200156
157 b Xone
158 delmarks aB
Bram Moolenaar9d5185b2018-07-08 17:57:34 +0200159 let a = split(execute('marks aBcD'), "\n")
Bram Moolenaar9b69f222017-10-08 21:53:15 +0200160 call assert_equal(2, len(a))
161 call assert_equal('mark line col file/text', a[0])
162 call assert_equal(' D 2 0 Xtwo', a[1])
163
164 b Xtwo
165 delmarks cD
166 call assert_fails('marks aBcD', 'E283:')
167
168 call delete('Xone')
169 call delete('Xtwo')
170 %bwipe
171endfunc
Bram Moolenaar9d5185b2018-07-08 17:57:34 +0200172
173func Test_marks_cmd_multibyte()
Bram Moolenaar9d5185b2018-07-08 17:57:34 +0200174 new Xone
Bram Moolenaarbde14d82018-07-10 15:22:32 +0200175 call setline(1, [repeat('á', &columns)])
Bram Moolenaar9d5185b2018-07-08 17:57:34 +0200176 norm! ma
177
178 let a = split(execute('marks a'), "\n")
179 call assert_equal(2, len(a))
Bram Moolenaarbde14d82018-07-10 15:22:32 +0200180 let expected = ' a 1 0 ' . repeat('á', &columns - 16)
Bram Moolenaar9d5185b2018-07-08 17:57:34 +0200181 call assert_equal(expected, a[1])
182
183 bwipe!
184endfunc
Bram Moolenaar71b13e92019-02-04 21:14:45 +0100185
186func Test_delmarks()
187 new
188 norm mx
189 norm `x
190 delmarks x
191 call assert_fails('norm `x', 'E20:')
192
193 " Deleting an already deleted mark should not fail.
194 delmarks x
195
Bram Moolenaar8cd6cd82019-12-27 17:33:26 +0100196 " getpos() should return all zeros after deleting a filemark.
197 norm mA
198 delmarks A
199 call assert_equal([0, 0, 0, 0], getpos("'A"))
200
Bram Moolenaar71b13e92019-02-04 21:14:45 +0100201 " Test deleting a range of marks.
202 norm ma
203 norm mb
204 norm mc
205 norm mz
206 delmarks b-z
207 norm `a
208 call assert_fails('norm `b', 'E20:')
209 call assert_fails('norm `c', 'E20:')
210 call assert_fails('norm `z', 'E20:')
211 call assert_fails('delmarks z-b', 'E475:')
212
213 call assert_fails('delmarks', 'E471:')
214 call assert_fails('delmarks /', 'E475:')
215
216 " Test delmarks!
217 norm mx
218 norm `x
219 delmarks!
220 call assert_fails('norm `x', 'E20:')
221 call assert_fails('delmarks! x', 'E474:')
222
223 bwipe!
224endfunc
225
226func Test_mark_error()
227 call assert_fails('mark', 'E471:')
228 call assert_fails('mark xx', 'E488:')
229 call assert_fails('mark _', 'E191:')
Bram Moolenaar1671f442020-03-10 07:48:13 +0100230 call assert_beeps('normal! m~')
Bram Moolenaar4d23c522020-04-09 18:42:11 +0200231
232 call setpos("'k", [0, 100, 1, 0])
233 call assert_fails("normal 'k", 'E19:')
Bram Moolenaar71b13e92019-02-04 21:14:45 +0100234endfunc
Bram Moolenaar54c8d222019-12-02 20:41:39 +0100235
236" Test for :lockmarks when pasting content
237func Test_lockmarks_with_put()
238 new
239 call append(0, repeat(['sky is blue'], 4))
240 normal gg
241 1,2yank r
242 put r
243 normal G
244 lockmarks put r
245 call assert_equal(2, line("'["))
246 call assert_equal(3, line("']"))
247
248 bwipe!
249endfunc
250
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100251" Test for :k command to set a mark
252func Test_marks_k_cmd()
253 new
254 call setline(1, ['foo', 'bar', 'baz', 'qux'])
255 1,3kr
256 call assert_equal([0, 3, 1, 0], getpos("'r"))
257 close!
258endfunc
259
Bram Moolenaar1671f442020-03-10 07:48:13 +0100260" Test for file marks (A-Z)
261func Test_file_mark()
262 new Xone
263 call setline(1, ['aaa', 'bbb'])
264 norm! G$mB
265 w!
266 new Xtwo
267 call setline(1, ['ccc', 'ddd'])
268 norm! GmD
269 w!
270
271 enew
272 normal! `B
273 call assert_equal('Xone', bufname())
274 call assert_equal([2, 3], [line('.'), col('.')])
275 normal! 'D
276 call assert_equal('Xtwo', bufname())
277 call assert_equal([2, 1], [line('.'), col('.')])
278
279 call delete('Xone')
280 call delete('Xtwo')
281endfunc
282
Bram Moolenaarcfb4b472020-05-31 15:41:57 +0200283" Test for the getmarklist() function
284func Test_getmarklist()
285 new
286 " global marks
287 delmarks A-Z 0-9 \" ^.[]
288 call assert_equal([], getmarklist())
289 call setline(1, ['one', 'two', 'three'])
290 mark A
291 call cursor(3, 5)
292 normal mN
Bram Moolenaarf17e7ea2020-06-01 14:14:44 +0200293 call assert_equal([{'file' : '', 'mark' : "'A", 'pos' : [bufnr(), 1, 1, 0]},
294 \ {'file' : '', 'mark' : "'N", 'pos' : [bufnr(), 3, 5, 0]}],
Bram Moolenaarcfb4b472020-05-31 15:41:57 +0200295 \ getmarklist())
296 " buffer local marks
297 delmarks!
Bram Moolenaarf17e7ea2020-06-01 14:14:44 +0200298 call assert_equal([{'mark' : "''", 'pos' : [bufnr(), 1, 1, 0]},
299 \ {'mark' : "'\"", 'pos' : [bufnr(), 1, 1, 0]}], getmarklist(bufnr()))
Bram Moolenaarcfb4b472020-05-31 15:41:57 +0200300 call cursor(2, 2)
301 normal mr
Bram Moolenaarf17e7ea2020-06-01 14:14:44 +0200302 call assert_equal({'mark' : "'r", 'pos' : [bufnr(), 2, 2, 0]},
303 \ bufnr()->getmarklist()[0])
304 call assert_equal([], {}->getmarklist())
zeertzjq93318a92025-06-16 20:10:38 +0200305 normal! yy
306 call assert_equal([
307 \ {'mark': "'[", 'pos': [bufnr(), 2, 1, 0]},
308 \ {'mark': "']", 'pos': [bufnr(), 2, v:maxcol, 0]},
309 \ ], getmarklist(bufnr())[-2:])
Bram Moolenaarcfb4b472020-05-31 15:41:57 +0200310 close!
311endfunc
312
Bram Moolenaar8ecfa2c2022-09-21 13:07:22 +0100313" This was using freed memory
314func Test_jump_mark_autocmd()
315 next 00
316 edit 0
317 sargument
318 au BufEnter 0 all
319 sil norm 
320
321 au! BufEnter
322 bwipe!
323endfunc
324
325
Bram Moolenaar54c8d222019-12-02 20:41:39 +0100326" vim: shiftwidth=2 sts=2 expandtab