Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1 | " Test for the search command |
| 2 | |
| 3 | func Test_search_cmdline() |
| 4 | if !exists('+incsearch') |
| 5 | return |
| 6 | endif |
| 7 | " need to disable char_avail, |
| 8 | " so that expansion of commandline works |
| 9 | call test_disable_char_avail(1) |
| 10 | new |
| 11 | call setline(1, [' 1', ' 2 these', ' 3 the', ' 4 their', ' 5 there', ' 6 their', ' 7 the', ' 8 them', ' 9 these', ' 10 foobar']) |
| 12 | " Test 1 |
| 13 | " CTRL-N / CTRL-P skips through the previous search history |
| 14 | set noincsearch |
| 15 | :1 |
| 16 | call feedkeys("/foobar\<cr>", 'tx') |
| 17 | call feedkeys("/the\<cr>",'tx') |
| 18 | call assert_equal('the', @/) |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 19 | call feedkeys("/thes\<C-P>\<C-P>\<cr>",'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 20 | call assert_equal('foobar', @/) |
| 21 | |
| 22 | " Test 2 |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 23 | " Ctrl-G goes from one match to the next |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 24 | " until the end of the buffer |
| 25 | set incsearch nowrapscan |
| 26 | :1 |
| 27 | " first match |
| 28 | call feedkeys("/the\<cr>", 'tx') |
| 29 | call assert_equal(' 2 these', getline('.')) |
| 30 | :1 |
| 31 | " second match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 32 | call feedkeys("/the\<C-G>\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 33 | call assert_equal(' 3 the', getline('.')) |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 34 | call assert_equal([0, 0, 0, 0], getpos('"')) |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 35 | :1 |
| 36 | " third match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 37 | call feedkeys("/the".repeat("\<C-G>", 2)."\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 38 | call assert_equal(' 4 their', getline('.')) |
| 39 | :1 |
| 40 | " fourth match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 41 | call feedkeys("/the".repeat("\<C-G>", 3)."\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 42 | call assert_equal(' 5 there', getline('.')) |
| 43 | :1 |
| 44 | " fifth match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 45 | call feedkeys("/the".repeat("\<C-G>", 4)."\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 46 | call assert_equal(' 6 their', getline('.')) |
| 47 | :1 |
| 48 | " sixth match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 49 | call feedkeys("/the".repeat("\<C-G>", 5)."\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 50 | call assert_equal(' 7 the', getline('.')) |
| 51 | :1 |
| 52 | " seventh match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 53 | call feedkeys("/the".repeat("\<C-G>", 6)."\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 54 | call assert_equal(' 8 them', getline('.')) |
| 55 | :1 |
| 56 | " eigth match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 57 | call feedkeys("/the".repeat("\<C-G>", 7)."\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 58 | call assert_equal(' 9 these', getline('.')) |
| 59 | :1 |
| 60 | " no further match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 61 | call feedkeys("/the".repeat("\<C-G>", 8)."\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 62 | call assert_equal(' 9 these', getline('.')) |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 63 | call assert_equal([0, 0, 0, 0], getpos('"')) |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 64 | |
| 65 | " Test 3 |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 66 | " Ctrl-G goes from one match to the next |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 67 | " and continues back at the top |
| 68 | set incsearch wrapscan |
| 69 | :1 |
| 70 | " first match |
| 71 | call feedkeys("/the\<cr>", 'tx') |
| 72 | call assert_equal(' 2 these', getline('.')) |
| 73 | :1 |
| 74 | " second match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 75 | call feedkeys("/the\<C-G>\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 76 | call assert_equal(' 3 the', getline('.')) |
| 77 | :1 |
| 78 | " third match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 79 | call feedkeys("/the".repeat("\<C-G>", 2)."\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 80 | call assert_equal(' 4 their', getline('.')) |
| 81 | :1 |
| 82 | " fourth match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 83 | call feedkeys("/the".repeat("\<C-G>", 3)."\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 84 | call assert_equal(' 5 there', getline('.')) |
| 85 | :1 |
| 86 | " fifth match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 87 | call feedkeys("/the".repeat("\<C-G>", 4)."\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 88 | call assert_equal(' 6 their', getline('.')) |
| 89 | :1 |
| 90 | " sixth match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 91 | call feedkeys("/the".repeat("\<C-G>", 5)."\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 92 | call assert_equal(' 7 the', getline('.')) |
| 93 | :1 |
| 94 | " seventh match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 95 | call feedkeys("/the".repeat("\<C-G>", 6)."\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 96 | call assert_equal(' 8 them', getline('.')) |
| 97 | :1 |
| 98 | " eigth match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 99 | call feedkeys("/the".repeat("\<C-G>", 7)."\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 100 | call assert_equal(' 9 these', getline('.')) |
| 101 | :1 |
| 102 | " back at first match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 103 | call feedkeys("/the".repeat("\<C-G>", 8)."\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 104 | call assert_equal(' 2 these', getline('.')) |
| 105 | |
| 106 | " Test 4 |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 107 | " CTRL-T goes to the previous match |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 108 | set incsearch nowrapscan |
| 109 | $ |
| 110 | " first match |
| 111 | call feedkeys("?the\<cr>", 'tx') |
| 112 | call assert_equal(' 9 these', getline('.')) |
| 113 | $ |
| 114 | " first match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 115 | call feedkeys("?the\<C-G>\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 116 | call assert_equal(' 9 these', getline('.')) |
| 117 | $ |
| 118 | " second match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 119 | call feedkeys("?the".repeat("\<C-T>", 1)."\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 120 | call assert_equal(' 8 them', getline('.')) |
| 121 | $ |
| 122 | " last match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 123 | call feedkeys("?the".repeat("\<C-T>", 7)."\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 124 | call assert_equal(' 2 these', getline('.')) |
| 125 | $ |
| 126 | " last match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 127 | call feedkeys("?the".repeat("\<C-T>", 8)."\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 128 | call assert_equal(' 2 these', getline('.')) |
| 129 | |
| 130 | " Test 5 |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 131 | " CTRL-T goes to the previous match |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 132 | set incsearch wrapscan |
| 133 | $ |
| 134 | " first match |
| 135 | call feedkeys("?the\<cr>", 'tx') |
| 136 | call assert_equal(' 9 these', getline('.')) |
| 137 | $ |
| 138 | " first match at the top |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 139 | call feedkeys("?the\<C-G>\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 140 | call assert_equal(' 2 these', getline('.')) |
| 141 | $ |
| 142 | " second match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 143 | call feedkeys("?the".repeat("\<C-T>", 1)."\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 144 | call assert_equal(' 8 them', getline('.')) |
| 145 | $ |
| 146 | " last match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 147 | call feedkeys("?the".repeat("\<C-T>", 7)."\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 148 | call assert_equal(' 2 these', getline('.')) |
| 149 | $ |
| 150 | " back at the bottom of the buffer |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 151 | call feedkeys("?the".repeat("\<C-T>", 8)."\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 152 | call assert_equal(' 9 these', getline('.')) |
| 153 | |
| 154 | " Test 6 |
| 155 | " CTRL-L adds to the search pattern |
| 156 | set incsearch wrapscan |
| 157 | 1 |
| 158 | " first match |
| 159 | call feedkeys("/the\<c-l>\<cr>", 'tx') |
| 160 | call assert_equal(' 2 these', getline('.')) |
| 161 | 1 |
| 162 | " go to next match of 'thes' |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 163 | call feedkeys("/the\<c-l>\<C-G>\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 164 | call assert_equal(' 9 these', getline('.')) |
| 165 | 1 |
| 166 | " wrap around |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 167 | call feedkeys("/the\<c-l>\<C-G>\<C-G>\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 168 | call assert_equal(' 2 these', getline('.')) |
| 169 | 1 |
| 170 | " wrap around |
| 171 | set nowrapscan |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 172 | call feedkeys("/the\<c-l>\<C-G>\<C-G>\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 173 | call assert_equal(' 9 these', getline('.')) |
| 174 | |
| 175 | " Test 7 |
| 176 | " <bs> remove from match, but stay at current match |
| 177 | set incsearch wrapscan |
| 178 | 1 |
| 179 | " first match |
| 180 | call feedkeys("/thei\<cr>", 'tx') |
| 181 | call assert_equal(' 4 their', getline('.')) |
| 182 | 1 |
| 183 | " delete one char, add another |
| 184 | call feedkeys("/thei\<bs>s\<cr>", 'tx') |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 185 | call assert_equal(' 2 these', getline('.')) |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 186 | 1 |
| 187 | " delete one char, add another, go to previous match, add one char |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 188 | call feedkeys("/thei\<bs>s\<bs>\<C-T>\<c-l>\<cr>", 'tx') |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 189 | call assert_equal(' 9 these', getline('.')) |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 190 | 1 |
| 191 | " delete all chars, start from the beginning again |
| 192 | call feedkeys("/them". repeat("\<bs>",4).'the\>'."\<cr>", 'tx') |
| 193 | call assert_equal(' 3 the', getline('.')) |
| 194 | |
| 195 | " clean up |
| 196 | call test_disable_char_avail(0) |
| 197 | bw! |
| 198 | endfunc |
| 199 | |
| 200 | func Test_search_cmdline2() |
| 201 | if !exists('+incsearch') |
| 202 | return |
| 203 | endif |
| 204 | " need to disable char_avail, |
| 205 | " so that expansion of commandline works |
| 206 | call test_disable_char_avail(1) |
| 207 | new |
| 208 | call setline(1, [' 1', ' 2 these', ' 3 the theother']) |
| 209 | " Test 1 |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 210 | " Ctrl-T goes correctly back and forth |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 211 | set incsearch |
| 212 | 1 |
| 213 | " first match |
| 214 | call feedkeys("/the\<cr>", 'tx') |
| 215 | call assert_equal(' 2 these', getline('.')) |
| 216 | 1 |
| 217 | " go to next match (on next line) |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 218 | call feedkeys("/the\<C-G>\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 219 | call assert_equal(' 3 the theother', getline('.')) |
| 220 | 1 |
| 221 | " go to next match (still on line 3) |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 222 | call feedkeys("/the\<C-G>\<C-G>\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 223 | call assert_equal(' 3 the theother', getline('.')) |
| 224 | 1 |
| 225 | " go to next match (still on line 3) |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 226 | call feedkeys("/the\<C-G>\<C-G>\<C-G>\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 227 | call assert_equal(' 3 the theother', getline('.')) |
| 228 | 1 |
| 229 | " go to previous match (on line 3) |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 230 | call feedkeys("/the\<C-G>\<C-G>\<C-G>\<C-T>\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 231 | call assert_equal(' 3 the theother', getline('.')) |
| 232 | 1 |
| 233 | " go to previous match (on line 3) |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 234 | call feedkeys("/the\<C-G>\<C-G>\<C-G>\<C-T>\<C-T>\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 235 | call assert_equal(' 3 the theother', getline('.')) |
| 236 | 1 |
| 237 | " go to previous match (on line 2) |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 238 | call feedkeys("/the\<C-G>\<C-G>\<C-G>\<C-T>\<C-T>\<C-T>\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 239 | call assert_equal(' 2 these', getline('.')) |
| 240 | |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 241 | " Test 2: keep the view, |
| 242 | " after deleting a character from the search cmd |
| 243 | call setline(1, [' 1', ' 2 these', ' 3 the', ' 4 their', ' 5 there', ' 6 their', ' 7 the', ' 8 them', ' 9 these', ' 10 foobar']) |
| 244 | resize 5 |
| 245 | 1 |
| 246 | call feedkeys("/foo\<bs>\<cr>", 'tx') |
| 247 | redraw |
| 248 | call assert_equal({'lnum': 10, 'leftcol': 0, 'col': 4, 'topfill': 0, 'topline': 6, 'coladd': 0, 'skipcol': 0, 'curswant': 4}, winsaveview()) |
| 249 | |
| 250 | " remove all history entries |
| 251 | for i in range(10) |
| 252 | call histdel('/') |
| 253 | endfor |
| 254 | |
| 255 | " Test 3: reset the view, |
| 256 | " after deleting all characters from the search cmd |
| 257 | norm! 1gg0 |
| 258 | " unfortunately, neither "/foo\<c-w>\<cr>", nor "/foo\<bs>\<bs>\<bs>\<cr>", |
| 259 | " nor "/foo\<c-u>\<cr>" works to delete the commandline. |
| 260 | " In that case Vim should return "E35 no previous regular expression", |
| 261 | " but it looks like Vim still sees /foo and therefore the test fails. |
| 262 | " Therefore, disableing this test |
| 263 | "call assert_fails(feedkeys("/foo\<c-w>\<cr>", 'tx'), 'E35') |
| 264 | "call assert_equal({'lnum': 1, 'leftcol': 0, 'col': 0, 'topfill': 0, 'topline': 1, 'coladd': 0, 'skipcol': 0, 'curswant': 0}, winsaveview()) |
| 265 | |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 266 | " clean up |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 267 | set noincsearch |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 268 | call test_disable_char_avail(0) |
| 269 | bw! |
| 270 | endfunc |