blob: dcf4904d06bbecead5c80839fec9616743387e2f [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
29func Test_setpos()
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010030 new Xone
Bram Moolenaarf13e00b2017-01-28 18:23:54 +010031 let onebuf = bufnr('%')
32 let onewin = win_getid()
33 call setline(1, ['aaa', 'bbb', 'ccc'])
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010034 new Xtwo
Bram Moolenaarf13e00b2017-01-28 18:23:54 +010035 let twobuf = bufnr('%')
36 let twowin = win_getid()
37 call setline(1, ['aaa', 'bbb', 'ccc'])
38
39 " for the cursor the buffer number is ignored
40 call setpos(".", [0, 2, 1, 0])
41 call assert_equal([0, 2, 1, 0], getpos("."))
42 call setpos(".", [onebuf, 3, 3, 0])
43 call assert_equal([0, 3, 3, 0], getpos("."))
44
45 call setpos("''", [0, 1, 3, 0])
46 call assert_equal([0, 1, 3, 0], getpos("''"))
47 call setpos("''", [onebuf, 2, 2, 0])
48 call assert_equal([0, 2, 2, 0], getpos("''"))
49
50 " buffer-local marks
51 for mark in ["'a", "'\"", "'[", "']", "'<", "'>"]
52 call win_gotoid(twowin)
53 call setpos(mark, [0, 2, 1, 0])
54 call assert_equal([0, 2, 1, 0], getpos(mark), "for mark " . mark)
55 call setpos(mark, [onebuf, 1, 3, 0])
56 call win_gotoid(onewin)
57 call assert_equal([0, 1, 3, 0], getpos(mark), "for mark " . mark)
58 endfor
59
60 " global marks
61 call win_gotoid(twowin)
62 call setpos("'N", [0, 2, 1, 0])
63 call assert_equal([twobuf, 2, 1, 0], getpos("'N"))
64 call setpos("'N", [onebuf, 1, 3, 0])
65 call assert_equal([onebuf, 1, 3, 0], getpos("'N"))
66
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010067 " try invalid column and check virtcol()
Bram Moolenaarf13e00b2017-01-28 18:23:54 +010068 call win_gotoid(onewin)
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010069 call setpos("'a", [0, 1, 2, 0])
70 call assert_equal([0, 1, 2, 0], getpos("'a"))
71 call setpos("'a", [0, 1, -5, 0])
72 call assert_equal([0, 1, 2, 0], getpos("'a"))
73 call setpos("'a", [0, 1, 0, 0])
74 call assert_equal([0, 1, 1, 0], getpos("'a"))
75 call setpos("'a", [0, 1, 4, 0])
76 call assert_equal([0, 1, 4, 0], getpos("'a"))
77 call assert_equal(4, virtcol("'a"))
78 call setpos("'a", [0, 1, 5, 0])
79 call assert_equal([0, 1, 5, 0], getpos("'a"))
80 call assert_equal(4, virtcol("'a"))
81 call setpos("'a", [0, 1, 21341234, 0])
82 call assert_equal([0, 1, 21341234, 0], getpos("'a"))
83 call assert_equal(4, virtcol("'a"))
84
Bram Moolenaar8b633132020-03-20 18:20:51 +010085 " Test with invalid buffer number, line number and column number
86 call cursor(2, 2)
87 call setpos('.', [-1, 1, 1, 0])
88 call assert_equal([2, 2], [line('.'), col('.')])
89 call setpos('.', [0, -1, 1, 0])
90 call assert_equal([2, 2], [line('.'), col('.')])
91 call setpos('.', [0, 1, -1, 0])
92 call assert_equal([2, 2], [line('.'), col('.')])
93
Bram Moolenaar0e05de42020-03-25 22:23:46 +010094 call assert_fails("call setpos('ab', [0, 1, 1, 0])", 'E474:')
95
Bram Moolenaarf13e00b2017-01-28 18:23:54 +010096 bwipe!
97 call win_gotoid(twowin)
98 bwipe!
99endfunc
Bram Moolenaar9b69f222017-10-08 21:53:15 +0200100
101func Test_marks_cmd()
102 new Xone
103 call setline(1, ['aaa', 'bbb'])
104 norm! maG$mB
105 w!
106 new Xtwo
107 call setline(1, ['ccc', 'ddd'])
108 norm! $mcGmD
Bram Moolenaar54c3fcd2020-07-19 22:09:06 +0200109 exe "norm! GVgg\<Esc>G"
Bram Moolenaar9b69f222017-10-08 21:53:15 +0200110 w!
111
112 b Xone
Bram Moolenaar9d5185b2018-07-08 17:57:34 +0200113 let a = split(execute('marks'), "\n")
Bram Moolenaar9b69f222017-10-08 21:53:15 +0200114 call assert_equal(9, len(a))
Bram Moolenaar54c3fcd2020-07-19 22:09:06 +0200115 call assert_equal(['mark line col file/text',
116 \ " ' 2 0 bbb",
117 \ ' a 1 0 aaa',
118 \ ' B 2 2 bbb',
119 \ ' D 2 0 Xtwo',
120 \ ' " 1 0 aaa',
121 \ ' [ 1 0 aaa',
122 \ ' ] 2 0 bbb',
123 \ ' . 2 0 bbb'], a)
Bram Moolenaar9b69f222017-10-08 21:53:15 +0200124
125 b Xtwo
Bram Moolenaar9d5185b2018-07-08 17:57:34 +0200126 let a = split(execute('marks'), "\n")
Bram Moolenaar54c3fcd2020-07-19 22:09:06 +0200127 call assert_equal(11, len(a))
128 call assert_equal(['mark line col file/text',
129 \ " ' 1 0 ccc",
130 \ ' c 1 2 ccc',
131 \ ' B 2 2 Xone',
132 \ ' D 2 0 ddd',
133 \ ' " 2 0 ddd',
134 \ ' [ 1 0 ccc',
135 \ ' ] 2 0 ddd',
136 \ ' . 2 0 ddd',
137 \ ' < 1 0 ccc',
138 \ ' > 2 0 ddd'], a)
139 norm! Gdd
140 w!
141 let a = split(execute('marks <>'), "\n")
142 call assert_equal(3, len(a))
143 call assert_equal(['mark line col file/text',
144 \ ' < 1 0 ccc',
145 \ ' > 2 0 -invalid-'], a)
Bram Moolenaar9b69f222017-10-08 21:53:15 +0200146
147 b Xone
148 delmarks aB
Bram Moolenaar9d5185b2018-07-08 17:57:34 +0200149 let a = split(execute('marks aBcD'), "\n")
Bram Moolenaar9b69f222017-10-08 21:53:15 +0200150 call assert_equal(2, len(a))
151 call assert_equal('mark line col file/text', a[0])
152 call assert_equal(' D 2 0 Xtwo', a[1])
153
154 b Xtwo
155 delmarks cD
156 call assert_fails('marks aBcD', 'E283:')
157
158 call delete('Xone')
159 call delete('Xtwo')
160 %bwipe
161endfunc
Bram Moolenaar9d5185b2018-07-08 17:57:34 +0200162
163func Test_marks_cmd_multibyte()
Bram Moolenaar9d5185b2018-07-08 17:57:34 +0200164 new Xone
Bram Moolenaarbde14d82018-07-10 15:22:32 +0200165 call setline(1, [repeat('á', &columns)])
Bram Moolenaar9d5185b2018-07-08 17:57:34 +0200166 norm! ma
167
168 let a = split(execute('marks a'), "\n")
169 call assert_equal(2, len(a))
Bram Moolenaarbde14d82018-07-10 15:22:32 +0200170 let expected = ' a 1 0 ' . repeat('á', &columns - 16)
Bram Moolenaar9d5185b2018-07-08 17:57:34 +0200171 call assert_equal(expected, a[1])
172
173 bwipe!
174endfunc
Bram Moolenaar71b13e92019-02-04 21:14:45 +0100175
176func Test_delmarks()
177 new
178 norm mx
179 norm `x
180 delmarks x
181 call assert_fails('norm `x', 'E20:')
182
183 " Deleting an already deleted mark should not fail.
184 delmarks x
185
Bram Moolenaar8cd6cd82019-12-27 17:33:26 +0100186 " getpos() should return all zeros after deleting a filemark.
187 norm mA
188 delmarks A
189 call assert_equal([0, 0, 0, 0], getpos("'A"))
190
Bram Moolenaar71b13e92019-02-04 21:14:45 +0100191 " Test deleting a range of marks.
192 norm ma
193 norm mb
194 norm mc
195 norm mz
196 delmarks b-z
197 norm `a
198 call assert_fails('norm `b', 'E20:')
199 call assert_fails('norm `c', 'E20:')
200 call assert_fails('norm `z', 'E20:')
201 call assert_fails('delmarks z-b', 'E475:')
202
203 call assert_fails('delmarks', 'E471:')
204 call assert_fails('delmarks /', 'E475:')
205
206 " Test delmarks!
207 norm mx
208 norm `x
209 delmarks!
210 call assert_fails('norm `x', 'E20:')
211 call assert_fails('delmarks! x', 'E474:')
212
213 bwipe!
214endfunc
215
216func Test_mark_error()
217 call assert_fails('mark', 'E471:')
218 call assert_fails('mark xx', 'E488:')
219 call assert_fails('mark _', 'E191:')
Bram Moolenaar1671f442020-03-10 07:48:13 +0100220 call assert_beeps('normal! m~')
Bram Moolenaar4d23c522020-04-09 18:42:11 +0200221
222 call setpos("'k", [0, 100, 1, 0])
223 call assert_fails("normal 'k", 'E19:')
Bram Moolenaar71b13e92019-02-04 21:14:45 +0100224endfunc
Bram Moolenaar54c8d222019-12-02 20:41:39 +0100225
226" Test for :lockmarks when pasting content
227func Test_lockmarks_with_put()
228 new
229 call append(0, repeat(['sky is blue'], 4))
230 normal gg
231 1,2yank r
232 put r
233 normal G
234 lockmarks put r
235 call assert_equal(2, line("'["))
236 call assert_equal(3, line("']"))
237
238 bwipe!
239endfunc
240
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100241" Test for :k command to set a mark
242func Test_marks_k_cmd()
243 new
244 call setline(1, ['foo', 'bar', 'baz', 'qux'])
245 1,3kr
246 call assert_equal([0, 3, 1, 0], getpos("'r"))
247 close!
248endfunc
249
Bram Moolenaar1671f442020-03-10 07:48:13 +0100250" Test for file marks (A-Z)
251func Test_file_mark()
252 new Xone
253 call setline(1, ['aaa', 'bbb'])
254 norm! G$mB
255 w!
256 new Xtwo
257 call setline(1, ['ccc', 'ddd'])
258 norm! GmD
259 w!
260
261 enew
262 normal! `B
263 call assert_equal('Xone', bufname())
264 call assert_equal([2, 3], [line('.'), col('.')])
265 normal! 'D
266 call assert_equal('Xtwo', bufname())
267 call assert_equal([2, 1], [line('.'), col('.')])
268
269 call delete('Xone')
270 call delete('Xtwo')
271endfunc
272
Bram Moolenaarcfb4b472020-05-31 15:41:57 +0200273" Test for the getmarklist() function
274func Test_getmarklist()
275 new
276 " global marks
277 delmarks A-Z 0-9 \" ^.[]
278 call assert_equal([], getmarklist())
279 call setline(1, ['one', 'two', 'three'])
280 mark A
281 call cursor(3, 5)
282 normal mN
Bram Moolenaarf17e7ea2020-06-01 14:14:44 +0200283 call assert_equal([{'file' : '', 'mark' : "'A", 'pos' : [bufnr(), 1, 1, 0]},
284 \ {'file' : '', 'mark' : "'N", 'pos' : [bufnr(), 3, 5, 0]}],
Bram Moolenaarcfb4b472020-05-31 15:41:57 +0200285 \ getmarklist())
286 " buffer local marks
287 delmarks!
Bram Moolenaarf17e7ea2020-06-01 14:14:44 +0200288 call assert_equal([{'mark' : "''", 'pos' : [bufnr(), 1, 1, 0]},
289 \ {'mark' : "'\"", 'pos' : [bufnr(), 1, 1, 0]}], getmarklist(bufnr()))
Bram Moolenaarcfb4b472020-05-31 15:41:57 +0200290 call cursor(2, 2)
291 normal mr
Bram Moolenaarf17e7ea2020-06-01 14:14:44 +0200292 call assert_equal({'mark' : "'r", 'pos' : [bufnr(), 2, 2, 0]},
293 \ bufnr()->getmarklist()[0])
294 call assert_equal([], {}->getmarklist())
Bram Moolenaarcfb4b472020-05-31 15:41:57 +0200295 close!
296endfunc
297
Bram Moolenaar54c8d222019-12-02 20:41:39 +0100298" vim: shiftwidth=2 sts=2 expandtab