xref: /vim-8.2.3635/src/testdir/test_marks.vim (revision 8b633135)
1
2" Test that a deleted mark is restored after delete-undo-redo-undo.
3func Test_Restore_DelMark()
4  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!
14endfunc
15
16" Test that CTRL-A and CTRL-X updates last changed mark '[, '].
17func Test_Incr_Marks()
18  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!
26endfunc
27
28func Test_setpos()
29  new Xone
30  let onebuf = bufnr('%')
31  let onewin = win_getid()
32  call setline(1, ['aaa', 'bbb', 'ccc'])
33  new Xtwo
34  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
66  " try invalid column and check virtcol()
67  call win_gotoid(onewin)
68  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
84  " 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
93  bwipe!
94  call win_gotoid(twowin)
95  bwipe!
96endfunc
97
98func Test_marks_cmd()
99  new Xone
100  call setline(1, ['aaa', 'bbb'])
101  norm! maG$mB
102  w!
103  new Xtwo
104  call setline(1, ['ccc', 'ddd'])
105  norm! $mcGmD
106  w!
107
108  b Xone
109  let a = split(execute('marks'), "\n")
110  call assert_equal(9, len(a))
111  call assert_equal('mark line  col file/text', a[0])
112  call assert_equal(" '      2    0 bbb", a[1])
113  call assert_equal(' a      1    0 aaa', a[2])
114  call assert_equal(' B      2    2 bbb', a[3])
115  call assert_equal(' D      2    0 Xtwo', a[4])
116  call assert_equal(' "      1    0 aaa', a[5])
117  call assert_equal(' [      1    0 aaa', a[6])
118  call assert_equal(' ]      2    0 bbb', a[7])
119  call assert_equal(' .      2    0 bbb', a[8])
120
121  b Xtwo
122  let a = split(execute('marks'), "\n")
123  call assert_equal(9, len(a))
124  call assert_equal('mark line  col file/text', a[0])
125  call assert_equal(" '      1    0 ccc", a[1])
126  call assert_equal(' c      1    2 ccc', a[2])
127  call assert_equal(' B      2    2 Xone', a[3])
128  call assert_equal(' D      2    0 ddd', a[4])
129  call assert_equal(' "      2    0 ddd', a[5])
130  call assert_equal(' [      1    0 ccc', a[6])
131  call assert_equal(' ]      2    0 ddd', a[7])
132  call assert_equal(' .      2    0 ddd', a[8])
133
134  b Xone
135  delmarks aB
136  let a = split(execute('marks aBcD'), "\n")
137  call assert_equal(2, len(a))
138  call assert_equal('mark line  col file/text', a[0])
139  call assert_equal(' D      2    0 Xtwo', a[1])
140
141  b Xtwo
142  delmarks cD
143  call assert_fails('marks aBcD', 'E283:')
144
145  call delete('Xone')
146  call delete('Xtwo')
147  %bwipe
148endfunc
149
150func Test_marks_cmd_multibyte()
151  new Xone
152  call setline(1, [repeat('á', &columns)])
153  norm! ma
154
155  let a = split(execute('marks a'), "\n")
156  call assert_equal(2, len(a))
157  let expected = ' a      1    0 ' . repeat('á', &columns - 16)
158  call assert_equal(expected, a[1])
159
160  bwipe!
161endfunc
162
163func Test_delmarks()
164  new
165  norm mx
166  norm `x
167  delmarks x
168  call assert_fails('norm `x', 'E20:')
169
170  " Deleting an already deleted mark should not fail.
171  delmarks x
172
173  " getpos() should return all zeros after deleting a filemark.
174  norm mA
175  delmarks A
176  call assert_equal([0, 0, 0, 0], getpos("'A"))
177
178  " Test deleting a range of marks.
179  norm ma
180  norm mb
181  norm mc
182  norm mz
183  delmarks b-z
184  norm `a
185  call assert_fails('norm `b', 'E20:')
186  call assert_fails('norm `c', 'E20:')
187  call assert_fails('norm `z', 'E20:')
188  call assert_fails('delmarks z-b', 'E475:')
189
190  call assert_fails('delmarks', 'E471:')
191  call assert_fails('delmarks /', 'E475:')
192
193  " Test delmarks!
194  norm mx
195  norm `x
196  delmarks!
197  call assert_fails('norm `x', 'E20:')
198  call assert_fails('delmarks! x', 'E474:')
199
200  bwipe!
201endfunc
202
203func Test_mark_error()
204  call assert_fails('mark', 'E471:')
205  call assert_fails('mark xx', 'E488:')
206  call assert_fails('mark _', 'E191:')
207  call assert_beeps('normal! m~')
208endfunc
209
210" Test for :lockmarks when pasting content
211func Test_lockmarks_with_put()
212  new
213  call append(0, repeat(['sky is blue'], 4))
214  normal gg
215  1,2yank r
216  put r
217  normal G
218  lockmarks put r
219  call assert_equal(2, line("'["))
220  call assert_equal(3, line("']"))
221
222  bwipe!
223endfunc
224
225" Test for :k command to set a mark
226func Test_marks_k_cmd()
227  new
228  call setline(1, ['foo', 'bar', 'baz', 'qux'])
229  1,3kr
230  call assert_equal([0, 3, 1, 0], getpos("'r"))
231  close!
232endfunc
233
234" Test for file marks (A-Z)
235func Test_file_mark()
236  new Xone
237  call setline(1, ['aaa', 'bbb'])
238  norm! G$mB
239  w!
240  new Xtwo
241  call setline(1, ['ccc', 'ddd'])
242  norm! GmD
243  w!
244
245  enew
246  normal! `B
247  call assert_equal('Xone', bufname())
248  call assert_equal([2, 3], [line('.'), col('.')])
249  normal! 'D
250  call assert_equal('Xtwo', bufname())
251  call assert_equal([2, 1], [line('.'), col('.')])
252
253  call delete('Xone')
254  call delete('Xtwo')
255endfunc
256
257" vim: shiftwidth=2 sts=2 expandtab
258