blob: 40242c083bfedc9ba1fd0e8206b2385163048ec8 [file] [log] [blame]
Bram Moolenaar19a16692016-09-01 22:19:47 +02001
2" Test that a deleted mark is restored after delete-undo-redo-undo.
Bram Moolenaar1e115362019-01-09 23:01:02 +01003func Test_Restore_DelMark()
Bram Moolenaar19a16692016-09-01 22:19:47 +02004 enew!
5 call append(0, [" textline A", " textline B", " textline C"])
6 normal! 2gg
7 set nocp viminfo+=nviminfo
8 exe "normal! i\<C-G>u\<Esc>"
9 exe "normal! maddu\<C-R>u"
10 let pos = getpos("'a")
11 call assert_equal(2, pos[1])
12 call assert_equal(1, pos[2])
13 enew!
Bram Moolenaar1e115362019-01-09 23:01:02 +010014endfunc
Bram Moolenaar19a16692016-09-01 22:19:47 +020015
16" Test that CTRL-A and CTRL-X updates last changed mark '[, '].
Bram Moolenaar1e115362019-01-09 23:01:02 +010017func Test_Incr_Marks()
Bram Moolenaar19a16692016-09-01 22:19:47 +020018 enew!
19 call append(0, ["123 123 123", "123 123 123", "123 123 123"])
20 normal! gg
21 execute "normal! \<C-A>`[v`]rAjwvjw\<C-X>`[v`]rX"
22 call assert_equal("AAA 123 123", getline(1))
23 call assert_equal("123 XXXXXXX", getline(2))
24 call assert_equal("XXX 123 123", getline(3))
25 enew!
Bram Moolenaar1e115362019-01-09 23:01:02 +010026endfunc
Bram Moolenaarf13e00b2017-01-28 18:23:54 +010027
28func Test_setpos()
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010029 new Xone
Bram Moolenaarf13e00b2017-01-28 18:23:54 +010030 let onebuf = bufnr('%')
31 let onewin = win_getid()
32 call setline(1, ['aaa', 'bbb', 'ccc'])
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010033 new Xtwo
Bram Moolenaarf13e00b2017-01-28 18:23:54 +010034 let twobuf = bufnr('%')
35 let twowin = win_getid()
36 call setline(1, ['aaa', 'bbb', 'ccc'])
37
38 " for the cursor the buffer number is ignored
39 call setpos(".", [0, 2, 1, 0])
40 call assert_equal([0, 2, 1, 0], getpos("."))
41 call setpos(".", [onebuf, 3, 3, 0])
42 call assert_equal([0, 3, 3, 0], getpos("."))
43
44 call setpos("''", [0, 1, 3, 0])
45 call assert_equal([0, 1, 3, 0], getpos("''"))
46 call setpos("''", [onebuf, 2, 2, 0])
47 call assert_equal([0, 2, 2, 0], getpos("''"))
48
49 " buffer-local marks
50 for mark in ["'a", "'\"", "'[", "']", "'<", "'>"]
51 call win_gotoid(twowin)
52 call setpos(mark, [0, 2, 1, 0])
53 call assert_equal([0, 2, 1, 0], getpos(mark), "for mark " . mark)
54 call setpos(mark, [onebuf, 1, 3, 0])
55 call win_gotoid(onewin)
56 call assert_equal([0, 1, 3, 0], getpos(mark), "for mark " . mark)
57 endfor
58
59 " global marks
60 call win_gotoid(twowin)
61 call setpos("'N", [0, 2, 1, 0])
62 call assert_equal([twobuf, 2, 1, 0], getpos("'N"))
63 call setpos("'N", [onebuf, 1, 3, 0])
64 call assert_equal([onebuf, 1, 3, 0], getpos("'N"))
65
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010066 " try invalid column and check virtcol()
Bram Moolenaarf13e00b2017-01-28 18:23:54 +010067 call win_gotoid(onewin)
Bram Moolenaarb3d33d82020-01-15 20:36:55 +010068 call setpos("'a", [0, 1, 2, 0])
69 call assert_equal([0, 1, 2, 0], getpos("'a"))
70 call setpos("'a", [0, 1, -5, 0])
71 call assert_equal([0, 1, 2, 0], getpos("'a"))
72 call setpos("'a", [0, 1, 0, 0])
73 call assert_equal([0, 1, 1, 0], getpos("'a"))
74 call setpos("'a", [0, 1, 4, 0])
75 call assert_equal([0, 1, 4, 0], getpos("'a"))
76 call assert_equal(4, virtcol("'a"))
77 call setpos("'a", [0, 1, 5, 0])
78 call assert_equal([0, 1, 5, 0], getpos("'a"))
79 call assert_equal(4, virtcol("'a"))
80 call setpos("'a", [0, 1, 21341234, 0])
81 call assert_equal([0, 1, 21341234, 0], getpos("'a"))
82 call assert_equal(4, virtcol("'a"))
83
Bram Moolenaar8b633132020-03-20 18:20:51 +010084 " Test with invalid buffer number, line number and column number
85 call cursor(2, 2)
86 call setpos('.', [-1, 1, 1, 0])
87 call assert_equal([2, 2], [line('.'), col('.')])
88 call setpos('.', [0, -1, 1, 0])
89 call assert_equal([2, 2], [line('.'), col('.')])
90 call setpos('.', [0, 1, -1, 0])
91 call assert_equal([2, 2], [line('.'), col('.')])
92
Bram Moolenaar0e05de42020-03-25 22:23:46 +010093 call assert_fails("call setpos('ab', [0, 1, 1, 0])", 'E474:')
94
Bram Moolenaarf13e00b2017-01-28 18:23:54 +010095 bwipe!
96 call win_gotoid(twowin)
97 bwipe!
98endfunc
Bram Moolenaar9b69f222017-10-08 21:53:15 +020099
100func Test_marks_cmd()
101 new Xone
102 call setline(1, ['aaa', 'bbb'])
103 norm! maG$mB
104 w!
105 new Xtwo
106 call setline(1, ['ccc', 'ddd'])
107 norm! $mcGmD
Bram Moolenaar54c3fcd2020-07-19 22:09:06 +0200108 exe "norm! GVgg\<Esc>G"
Bram Moolenaar9b69f222017-10-08 21:53:15 +0200109 w!
110
111 b Xone
Bram Moolenaar9d5185b2018-07-08 17:57:34 +0200112 let a = split(execute('marks'), "\n")
Bram Moolenaar9b69f222017-10-08 21:53:15 +0200113 call assert_equal(9, len(a))
Bram Moolenaar54c3fcd2020-07-19 22:09:06 +0200114 call assert_equal(['mark line col file/text',
115 \ " ' 2 0 bbb",
116 \ ' a 1 0 aaa',
117 \ ' B 2 2 bbb',
118 \ ' D 2 0 Xtwo',
119 \ ' " 1 0 aaa',
120 \ ' [ 1 0 aaa',
121 \ ' ] 2 0 bbb',
122 \ ' . 2 0 bbb'], a)
Bram Moolenaar9b69f222017-10-08 21:53:15 +0200123
124 b Xtwo
Bram Moolenaar9d5185b2018-07-08 17:57:34 +0200125 let a = split(execute('marks'), "\n")
Bram Moolenaar54c3fcd2020-07-19 22:09:06 +0200126 call assert_equal(11, len(a))
127 call assert_equal(['mark line col file/text',
128 \ " ' 1 0 ccc",
129 \ ' c 1 2 ccc',
130 \ ' B 2 2 Xone',
131 \ ' D 2 0 ddd',
132 \ ' " 2 0 ddd',
133 \ ' [ 1 0 ccc',
134 \ ' ] 2 0 ddd',
135 \ ' . 2 0 ddd',
136 \ ' < 1 0 ccc',
137 \ ' > 2 0 ddd'], a)
138 norm! Gdd
139 w!
140 let a = split(execute('marks <>'), "\n")
141 call assert_equal(3, len(a))
142 call assert_equal(['mark line col file/text',
143 \ ' < 1 0 ccc',
144 \ ' > 2 0 -invalid-'], a)
Bram Moolenaar9b69f222017-10-08 21:53:15 +0200145
146 b Xone
147 delmarks aB
Bram Moolenaar9d5185b2018-07-08 17:57:34 +0200148 let a = split(execute('marks aBcD'), "\n")
Bram Moolenaar9b69f222017-10-08 21:53:15 +0200149 call assert_equal(2, len(a))
150 call assert_equal('mark line col file/text', a[0])
151 call assert_equal(' D 2 0 Xtwo', a[1])
152
153 b Xtwo
154 delmarks cD
155 call assert_fails('marks aBcD', 'E283:')
156
157 call delete('Xone')
158 call delete('Xtwo')
159 %bwipe
160endfunc
Bram Moolenaar9d5185b2018-07-08 17:57:34 +0200161
162func Test_marks_cmd_multibyte()
Bram Moolenaar9d5185b2018-07-08 17:57:34 +0200163 new Xone
Bram Moolenaarbde14d82018-07-10 15:22:32 +0200164 call setline(1, [repeat('á', &columns)])
Bram Moolenaar9d5185b2018-07-08 17:57:34 +0200165 norm! ma
166
167 let a = split(execute('marks a'), "\n")
168 call assert_equal(2, len(a))
Bram Moolenaarbde14d82018-07-10 15:22:32 +0200169 let expected = ' a 1 0 ' . repeat('á', &columns - 16)
Bram Moolenaar9d5185b2018-07-08 17:57:34 +0200170 call assert_equal(expected, a[1])
171
172 bwipe!
173endfunc
Bram Moolenaar71b13e92019-02-04 21:14:45 +0100174
175func Test_delmarks()
176 new
177 norm mx
178 norm `x
179 delmarks x
180 call assert_fails('norm `x', 'E20:')
181
182 " Deleting an already deleted mark should not fail.
183 delmarks x
184
Bram Moolenaar8cd6cd82019-12-27 17:33:26 +0100185 " getpos() should return all zeros after deleting a filemark.
186 norm mA
187 delmarks A
188 call assert_equal([0, 0, 0, 0], getpos("'A"))
189
Bram Moolenaar71b13e92019-02-04 21:14:45 +0100190 " Test deleting a range of marks.
191 norm ma
192 norm mb
193 norm mc
194 norm mz
195 delmarks b-z
196 norm `a
197 call assert_fails('norm `b', 'E20:')
198 call assert_fails('norm `c', 'E20:')
199 call assert_fails('norm `z', 'E20:')
200 call assert_fails('delmarks z-b', 'E475:')
201
202 call assert_fails('delmarks', 'E471:')
203 call assert_fails('delmarks /', 'E475:')
204
205 " Test delmarks!
206 norm mx
207 norm `x
208 delmarks!
209 call assert_fails('norm `x', 'E20:')
210 call assert_fails('delmarks! x', 'E474:')
211
212 bwipe!
213endfunc
214
215func Test_mark_error()
216 call assert_fails('mark', 'E471:')
217 call assert_fails('mark xx', 'E488:')
218 call assert_fails('mark _', 'E191:')
Bram Moolenaar1671f442020-03-10 07:48:13 +0100219 call assert_beeps('normal! m~')
Bram Moolenaar4d23c522020-04-09 18:42:11 +0200220
221 call setpos("'k", [0, 100, 1, 0])
222 call assert_fails("normal 'k", 'E19:')
Bram Moolenaar71b13e92019-02-04 21:14:45 +0100223endfunc
Bram Moolenaar54c8d222019-12-02 20:41:39 +0100224
225" Test for :lockmarks when pasting content
226func Test_lockmarks_with_put()
227 new
228 call append(0, repeat(['sky is blue'], 4))
229 normal gg
230 1,2yank r
231 put r
232 normal G
233 lockmarks put r
234 call assert_equal(2, line("'["))
235 call assert_equal(3, line("']"))
236
237 bwipe!
238endfunc
239
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100240" Test for :k command to set a mark
241func Test_marks_k_cmd()
242 new
243 call setline(1, ['foo', 'bar', 'baz', 'qux'])
244 1,3kr
245 call assert_equal([0, 3, 1, 0], getpos("'r"))
246 close!
247endfunc
248
Bram Moolenaar1671f442020-03-10 07:48:13 +0100249" Test for file marks (A-Z)
250func Test_file_mark()
251 new Xone
252 call setline(1, ['aaa', 'bbb'])
253 norm! G$mB
254 w!
255 new Xtwo
256 call setline(1, ['ccc', 'ddd'])
257 norm! GmD
258 w!
259
260 enew
261 normal! `B
262 call assert_equal('Xone', bufname())
263 call assert_equal([2, 3], [line('.'), col('.')])
264 normal! 'D
265 call assert_equal('Xtwo', bufname())
266 call assert_equal([2, 1], [line('.'), col('.')])
267
268 call delete('Xone')
269 call delete('Xtwo')
270endfunc
271
Bram Moolenaarcfb4b472020-05-31 15:41:57 +0200272" Test for the getmarklist() function
273func Test_getmarklist()
274 new
275 " global marks
276 delmarks A-Z 0-9 \" ^.[]
277 call assert_equal([], getmarklist())
278 call setline(1, ['one', 'two', 'three'])
279 mark A
280 call cursor(3, 5)
281 normal mN
Bram Moolenaarf17e7ea2020-06-01 14:14:44 +0200282 call assert_equal([{'file' : '', 'mark' : "'A", 'pos' : [bufnr(), 1, 1, 0]},
283 \ {'file' : '', 'mark' : "'N", 'pos' : [bufnr(), 3, 5, 0]}],
Bram Moolenaarcfb4b472020-05-31 15:41:57 +0200284 \ getmarklist())
285 " buffer local marks
286 delmarks!
Bram Moolenaarf17e7ea2020-06-01 14:14:44 +0200287 call assert_equal([{'mark' : "''", 'pos' : [bufnr(), 1, 1, 0]},
288 \ {'mark' : "'\"", 'pos' : [bufnr(), 1, 1, 0]}], getmarklist(bufnr()))
Bram Moolenaarcfb4b472020-05-31 15:41:57 +0200289 call cursor(2, 2)
290 normal mr
Bram Moolenaarf17e7ea2020-06-01 14:14:44 +0200291 call assert_equal({'mark' : "'r", 'pos' : [bufnr(), 2, 2, 0]},
292 \ bufnr()->getmarklist()[0])
293 call assert_equal([], {}->getmarklist())
Bram Moolenaarcfb4b472020-05-31 15:41:57 +0200294 close!
295endfunc
296
Bram Moolenaar54c8d222019-12-02 20:41:39 +0100297" vim: shiftwidth=2 sts=2 expandtab