Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 1 | " Test for marks |
Bram Moolenaar | 19a1669 | 2016-09-01 22:19:47 +0200 | [diff] [blame] | 2 | |
| 3 | " Test that a deleted mark is restored after delete-undo-redo-undo. |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 4 | func Test_Restore_DelMark() |
Bram Moolenaar | 19a1669 | 2016-09-01 22:19:47 +0200 | [diff] [blame] | 5 | 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 Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 15 | endfunc |
Bram Moolenaar | 19a1669 | 2016-09-01 22:19:47 +0200 | [diff] [blame] | 16 | |
| 17 | " Test that CTRL-A and CTRL-X updates last changed mark '[, ']. |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 18 | func Test_Incr_Marks() |
Bram Moolenaar | 19a1669 | 2016-09-01 22:19:47 +0200 | [diff] [blame] | 19 | 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 Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 27 | endfunc |
Bram Moolenaar | f13e00b | 2017-01-28 18:23:54 +0100 | [diff] [blame] | 28 | |
Bram Moolenaar | e08aee6 | 2021-10-17 21:53:58 +0100 | [diff] [blame] | 29 | func 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! |
| 37 | endfunc |
| 38 | |
Bram Moolenaar | f13e00b | 2017-01-28 18:23:54 +0100 | [diff] [blame] | 39 | func Test_setpos() |
Bram Moolenaar | b3d33d8 | 2020-01-15 20:36:55 +0100 | [diff] [blame] | 40 | new Xone |
Bram Moolenaar | f13e00b | 2017-01-28 18:23:54 +0100 | [diff] [blame] | 41 | let onebuf = bufnr('%') |
| 42 | let onewin = win_getid() |
| 43 | call setline(1, ['aaa', 'bbb', 'ccc']) |
Bram Moolenaar | b3d33d8 | 2020-01-15 20:36:55 +0100 | [diff] [blame] | 44 | new Xtwo |
Bram Moolenaar | f13e00b | 2017-01-28 18:23:54 +0100 | [diff] [blame] | 45 | 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 Moolenaar | b3d33d8 | 2020-01-15 20:36:55 +0100 | [diff] [blame] | 77 | " try invalid column and check virtcol() |
Bram Moolenaar | f13e00b | 2017-01-28 18:23:54 +0100 | [diff] [blame] | 78 | call win_gotoid(onewin) |
Bram Moolenaar | b3d33d8 | 2020-01-15 20:36:55 +0100 | [diff] [blame] | 79 | 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 Moolenaar | 8b63313 | 2020-03-20 18:20:51 +0100 | [diff] [blame] | 95 | " 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 Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 104 | call assert_fails("call setpos('ab', [0, 1, 1, 0])", 'E474:') |
| 105 | |
Bram Moolenaar | f13e00b | 2017-01-28 18:23:54 +0100 | [diff] [blame] | 106 | bwipe! |
| 107 | call win_gotoid(twowin) |
| 108 | bwipe! |
| 109 | endfunc |
Bram Moolenaar | 9b69f22 | 2017-10-08 21:53:15 +0200 | [diff] [blame] | 110 | |
| 111 | func 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 Moolenaar | 54c3fcd | 2020-07-19 22:09:06 +0200 | [diff] [blame] | 119 | exe "norm! GVgg\<Esc>G" |
Bram Moolenaar | 9b69f22 | 2017-10-08 21:53:15 +0200 | [diff] [blame] | 120 | w! |
| 121 | |
| 122 | b Xone |
Bram Moolenaar | 9d5185b | 2018-07-08 17:57:34 +0200 | [diff] [blame] | 123 | let a = split(execute('marks'), "\n") |
Bram Moolenaar | 9b69f22 | 2017-10-08 21:53:15 +0200 | [diff] [blame] | 124 | call assert_equal(9, len(a)) |
Bram Moolenaar | 54c3fcd | 2020-07-19 22:09:06 +0200 | [diff] [blame] | 125 | 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 Moolenaar | 9b69f22 | 2017-10-08 21:53:15 +0200 | [diff] [blame] | 134 | |
| 135 | b Xtwo |
Bram Moolenaar | 9d5185b | 2018-07-08 17:57:34 +0200 | [diff] [blame] | 136 | let a = split(execute('marks'), "\n") |
Bram Moolenaar | 54c3fcd | 2020-07-19 22:09:06 +0200 | [diff] [blame] | 137 | 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 Moolenaar | 9b69f22 | 2017-10-08 21:53:15 +0200 | [diff] [blame] | 156 | |
| 157 | b Xone |
| 158 | delmarks aB |
Bram Moolenaar | 9d5185b | 2018-07-08 17:57:34 +0200 | [diff] [blame] | 159 | let a = split(execute('marks aBcD'), "\n") |
Bram Moolenaar | 9b69f22 | 2017-10-08 21:53:15 +0200 | [diff] [blame] | 160 | 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 |
| 171 | endfunc |
Bram Moolenaar | 9d5185b | 2018-07-08 17:57:34 +0200 | [diff] [blame] | 172 | |
| 173 | func Test_marks_cmd_multibyte() |
Bram Moolenaar | 9d5185b | 2018-07-08 17:57:34 +0200 | [diff] [blame] | 174 | new Xone |
Bram Moolenaar | bde14d8 | 2018-07-10 15:22:32 +0200 | [diff] [blame] | 175 | call setline(1, [repeat('á', &columns)]) |
Bram Moolenaar | 9d5185b | 2018-07-08 17:57:34 +0200 | [diff] [blame] | 176 | norm! ma |
| 177 | |
| 178 | let a = split(execute('marks a'), "\n") |
| 179 | call assert_equal(2, len(a)) |
Bram Moolenaar | bde14d8 | 2018-07-10 15:22:32 +0200 | [diff] [blame] | 180 | let expected = ' a 1 0 ' . repeat('á', &columns - 16) |
Bram Moolenaar | 9d5185b | 2018-07-08 17:57:34 +0200 | [diff] [blame] | 181 | call assert_equal(expected, a[1]) |
| 182 | |
| 183 | bwipe! |
| 184 | endfunc |
Bram Moolenaar | 71b13e9 | 2019-02-04 21:14:45 +0100 | [diff] [blame] | 185 | |
| 186 | func 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 Moolenaar | 8cd6cd8 | 2019-12-27 17:33:26 +0100 | [diff] [blame] | 196 | " 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 Moolenaar | 71b13e9 | 2019-02-04 21:14:45 +0100 | [diff] [blame] | 201 | " 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! |
| 224 | endfunc |
| 225 | |
| 226 | func Test_mark_error() |
| 227 | call assert_fails('mark', 'E471:') |
| 228 | call assert_fails('mark xx', 'E488:') |
| 229 | call assert_fails('mark _', 'E191:') |
Bram Moolenaar | 1671f44 | 2020-03-10 07:48:13 +0100 | [diff] [blame] | 230 | call assert_beeps('normal! m~') |
Bram Moolenaar | 4d23c52 | 2020-04-09 18:42:11 +0200 | [diff] [blame] | 231 | |
| 232 | call setpos("'k", [0, 100, 1, 0]) |
| 233 | call assert_fails("normal 'k", 'E19:') |
Bram Moolenaar | 71b13e9 | 2019-02-04 21:14:45 +0100 | [diff] [blame] | 234 | endfunc |
Bram Moolenaar | 54c8d22 | 2019-12-02 20:41:39 +0100 | [diff] [blame] | 235 | |
| 236 | " Test for :lockmarks when pasting content |
| 237 | func 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! |
| 249 | endfunc |
| 250 | |
Bram Moolenaar | f0cee19 | 2020-02-16 13:33:56 +0100 | [diff] [blame] | 251 | " Test for :k command to set a mark |
| 252 | func 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! |
| 258 | endfunc |
| 259 | |
Bram Moolenaar | 1671f44 | 2020-03-10 07:48:13 +0100 | [diff] [blame] | 260 | " Test for file marks (A-Z) |
| 261 | func 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') |
| 281 | endfunc |
| 282 | |
Bram Moolenaar | cfb4b47 | 2020-05-31 15:41:57 +0200 | [diff] [blame] | 283 | " Test for the getmarklist() function |
| 284 | func 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 Moolenaar | f17e7ea | 2020-06-01 14:14:44 +0200 | [diff] [blame] | 293 | call assert_equal([{'file' : '', 'mark' : "'A", 'pos' : [bufnr(), 1, 1, 0]}, |
| 294 | \ {'file' : '', 'mark' : "'N", 'pos' : [bufnr(), 3, 5, 0]}], |
Bram Moolenaar | cfb4b47 | 2020-05-31 15:41:57 +0200 | [diff] [blame] | 295 | \ getmarklist()) |
| 296 | " buffer local marks |
| 297 | delmarks! |
Bram Moolenaar | f17e7ea | 2020-06-01 14:14:44 +0200 | [diff] [blame] | 298 | call assert_equal([{'mark' : "''", 'pos' : [bufnr(), 1, 1, 0]}, |
| 299 | \ {'mark' : "'\"", 'pos' : [bufnr(), 1, 1, 0]}], getmarklist(bufnr())) |
Bram Moolenaar | cfb4b47 | 2020-05-31 15:41:57 +0200 | [diff] [blame] | 300 | call cursor(2, 2) |
| 301 | normal mr |
Bram Moolenaar | f17e7ea | 2020-06-01 14:14:44 +0200 | [diff] [blame] | 302 | call assert_equal({'mark' : "'r", 'pos' : [bufnr(), 2, 2, 0]}, |
| 303 | \ bufnr()->getmarklist()[0]) |
| 304 | call assert_equal([], {}->getmarklist()) |
zeertzjq | 93318a9 | 2025-06-16 20:10:38 +0200 | [diff] [blame] | 305 | 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 Moolenaar | cfb4b47 | 2020-05-31 15:41:57 +0200 | [diff] [blame] | 310 | close! |
| 311 | endfunc |
| 312 | |
Bram Moolenaar | 8ecfa2c | 2022-09-21 13:07:22 +0100 | [diff] [blame] | 313 | " This was using freed memory |
| 314 | func 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! |
| 323 | endfunc |
| 324 | |
| 325 | |
Bram Moolenaar | 54c8d22 | 2019-12-02 20:41:39 +0100 | [diff] [blame] | 326 | " vim: shiftwidth=2 sts=2 expandtab |