Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1 | " Test for the search command |
| 2 | |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 3 | source shared.vim |
| 4 | |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 5 | func Test_search_cmdline() |
| 6 | if !exists('+incsearch') |
| 7 | return |
| 8 | endif |
| 9 | " need to disable char_avail, |
| 10 | " so that expansion of commandline works |
Bram Moolenaar | eb992cb | 2017-03-09 18:20:16 +0100 | [diff] [blame] | 11 | call test_override("char_avail", 1) |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 12 | new |
| 13 | call setline(1, [' 1', ' 2 these', ' 3 the', ' 4 their', ' 5 there', ' 6 their', ' 7 the', ' 8 them', ' 9 these', ' 10 foobar']) |
| 14 | " Test 1 |
| 15 | " CTRL-N / CTRL-P skips through the previous search history |
| 16 | set noincsearch |
| 17 | :1 |
| 18 | call feedkeys("/foobar\<cr>", 'tx') |
| 19 | call feedkeys("/the\<cr>",'tx') |
| 20 | call assert_equal('the', @/) |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 21 | call feedkeys("/thes\<C-P>\<C-P>\<cr>",'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 22 | call assert_equal('foobar', @/) |
| 23 | |
| 24 | " Test 2 |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 25 | " Ctrl-G goes from one match to the next |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 26 | " until the end of the buffer |
| 27 | set incsearch nowrapscan |
| 28 | :1 |
| 29 | " first match |
| 30 | call feedkeys("/the\<cr>", 'tx') |
| 31 | call assert_equal(' 2 these', getline('.')) |
| 32 | :1 |
| 33 | " second match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 34 | call feedkeys("/the\<C-G>\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 35 | call assert_equal(' 3 the', getline('.')) |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 36 | call assert_equal([0, 0, 0, 0], getpos('"')) |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 37 | :1 |
| 38 | " third match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 39 | call feedkeys("/the".repeat("\<C-G>", 2)."\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 40 | call assert_equal(' 4 their', getline('.')) |
| 41 | :1 |
| 42 | " fourth match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 43 | call feedkeys("/the".repeat("\<C-G>", 3)."\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 44 | call assert_equal(' 5 there', getline('.')) |
| 45 | :1 |
| 46 | " fifth match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 47 | call feedkeys("/the".repeat("\<C-G>", 4)."\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 48 | call assert_equal(' 6 their', getline('.')) |
| 49 | :1 |
| 50 | " sixth match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 51 | call feedkeys("/the".repeat("\<C-G>", 5)."\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 52 | call assert_equal(' 7 the', getline('.')) |
| 53 | :1 |
| 54 | " seventh match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 55 | call feedkeys("/the".repeat("\<C-G>", 6)."\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 56 | call assert_equal(' 8 them', getline('.')) |
| 57 | :1 |
| 58 | " eigth match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 59 | call feedkeys("/the".repeat("\<C-G>", 7)."\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 60 | call assert_equal(' 9 these', getline('.')) |
| 61 | :1 |
| 62 | " no further match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 63 | call feedkeys("/the".repeat("\<C-G>", 8)."\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 64 | call assert_equal(' 9 these', getline('.')) |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 65 | call assert_equal([0, 0, 0, 0], getpos('"')) |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 66 | |
| 67 | " Test 3 |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 68 | " Ctrl-G goes from one match to the next |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 69 | " and continues back at the top |
| 70 | set incsearch wrapscan |
| 71 | :1 |
| 72 | " first match |
| 73 | call feedkeys("/the\<cr>", 'tx') |
| 74 | call assert_equal(' 2 these', getline('.')) |
| 75 | :1 |
| 76 | " second match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 77 | call feedkeys("/the\<C-G>\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 78 | call assert_equal(' 3 the', getline('.')) |
| 79 | :1 |
| 80 | " third match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 81 | call feedkeys("/the".repeat("\<C-G>", 2)."\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 82 | call assert_equal(' 4 their', getline('.')) |
| 83 | :1 |
| 84 | " fourth match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 85 | call feedkeys("/the".repeat("\<C-G>", 3)."\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 86 | call assert_equal(' 5 there', getline('.')) |
| 87 | :1 |
| 88 | " fifth match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 89 | call feedkeys("/the".repeat("\<C-G>", 4)."\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 90 | call assert_equal(' 6 their', getline('.')) |
| 91 | :1 |
| 92 | " sixth match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 93 | call feedkeys("/the".repeat("\<C-G>", 5)."\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 94 | call assert_equal(' 7 the', getline('.')) |
| 95 | :1 |
| 96 | " seventh match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 97 | call feedkeys("/the".repeat("\<C-G>", 6)."\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 98 | call assert_equal(' 8 them', getline('.')) |
| 99 | :1 |
| 100 | " eigth match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 101 | call feedkeys("/the".repeat("\<C-G>", 7)."\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 102 | call assert_equal(' 9 these', getline('.')) |
| 103 | :1 |
| 104 | " back at first match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 105 | call feedkeys("/the".repeat("\<C-G>", 8)."\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 106 | call assert_equal(' 2 these', getline('.')) |
| 107 | |
| 108 | " Test 4 |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 109 | " CTRL-T goes to the previous match |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 110 | set incsearch nowrapscan |
| 111 | $ |
| 112 | " first match |
| 113 | call feedkeys("?the\<cr>", 'tx') |
| 114 | call assert_equal(' 9 these', getline('.')) |
| 115 | $ |
| 116 | " first match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 117 | call feedkeys("?the\<C-G>\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 118 | call assert_equal(' 9 these', getline('.')) |
| 119 | $ |
| 120 | " second match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 121 | call feedkeys("?the".repeat("\<C-T>", 1)."\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 122 | call assert_equal(' 8 them', getline('.')) |
| 123 | $ |
| 124 | " last match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 125 | call feedkeys("?the".repeat("\<C-T>", 7)."\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 126 | call assert_equal(' 2 these', getline('.')) |
| 127 | $ |
| 128 | " last match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 129 | call feedkeys("?the".repeat("\<C-T>", 8)."\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 130 | call assert_equal(' 2 these', getline('.')) |
| 131 | |
| 132 | " Test 5 |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 133 | " CTRL-T goes to the previous match |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 134 | set incsearch wrapscan |
| 135 | $ |
| 136 | " first match |
| 137 | call feedkeys("?the\<cr>", 'tx') |
| 138 | call assert_equal(' 9 these', getline('.')) |
| 139 | $ |
| 140 | " first match at the top |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 141 | call feedkeys("?the\<C-G>\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 142 | call assert_equal(' 2 these', getline('.')) |
| 143 | $ |
| 144 | " second match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 145 | call feedkeys("?the".repeat("\<C-T>", 1)."\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 146 | call assert_equal(' 8 them', getline('.')) |
| 147 | $ |
| 148 | " last match |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 149 | call feedkeys("?the".repeat("\<C-T>", 7)."\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 150 | call assert_equal(' 2 these', getline('.')) |
| 151 | $ |
| 152 | " back at the bottom of the buffer |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 153 | call feedkeys("?the".repeat("\<C-T>", 8)."\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 154 | call assert_equal(' 9 these', getline('.')) |
| 155 | |
| 156 | " Test 6 |
| 157 | " CTRL-L adds to the search pattern |
| 158 | set incsearch wrapscan |
| 159 | 1 |
| 160 | " first match |
| 161 | call feedkeys("/the\<c-l>\<cr>", 'tx') |
| 162 | call assert_equal(' 2 these', getline('.')) |
| 163 | 1 |
| 164 | " go to next match of 'thes' |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 165 | call feedkeys("/the\<c-l>\<C-G>\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 166 | call assert_equal(' 9 these', getline('.')) |
| 167 | 1 |
| 168 | " wrap around |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 169 | call feedkeys("/the\<c-l>\<C-G>\<C-G>\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 170 | call assert_equal(' 2 these', getline('.')) |
| 171 | 1 |
| 172 | " wrap around |
| 173 | set nowrapscan |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 174 | call feedkeys("/the\<c-l>\<C-G>\<C-G>\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 175 | call assert_equal(' 9 these', getline('.')) |
| 176 | |
| 177 | " Test 7 |
| 178 | " <bs> remove from match, but stay at current match |
| 179 | set incsearch wrapscan |
| 180 | 1 |
| 181 | " first match |
| 182 | call feedkeys("/thei\<cr>", 'tx') |
| 183 | call assert_equal(' 4 their', getline('.')) |
| 184 | 1 |
| 185 | " delete one char, add another |
| 186 | call feedkeys("/thei\<bs>s\<cr>", 'tx') |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 187 | call assert_equal(' 2 these', getline('.')) |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 188 | 1 |
| 189 | " delete one char, add another, go to previous match, add one char |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 190 | call feedkeys("/thei\<bs>s\<bs>\<C-T>\<c-l>\<cr>", 'tx') |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 191 | call assert_equal(' 9 these', getline('.')) |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 192 | 1 |
| 193 | " delete all chars, start from the beginning again |
| 194 | call feedkeys("/them". repeat("\<bs>",4).'the\>'."\<cr>", 'tx') |
| 195 | call assert_equal(' 3 the', getline('.')) |
| 196 | |
| 197 | " clean up |
Bram Moolenaar | eb992cb | 2017-03-09 18:20:16 +0100 | [diff] [blame] | 198 | call test_override("char_avail", 0) |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 199 | bw! |
| 200 | endfunc |
| 201 | |
| 202 | func Test_search_cmdline2() |
| 203 | if !exists('+incsearch') |
| 204 | return |
| 205 | endif |
| 206 | " need to disable char_avail, |
| 207 | " so that expansion of commandline works |
Bram Moolenaar | eb992cb | 2017-03-09 18:20:16 +0100 | [diff] [blame] | 208 | call test_override("char_avail", 1) |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 209 | new |
| 210 | call setline(1, [' 1', ' 2 these', ' 3 the theother']) |
| 211 | " Test 1 |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 212 | " Ctrl-T goes correctly back and forth |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 213 | set incsearch |
| 214 | 1 |
| 215 | " first match |
| 216 | call feedkeys("/the\<cr>", 'tx') |
| 217 | call assert_equal(' 2 these', getline('.')) |
| 218 | 1 |
| 219 | " go to next match (on next line) |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 220 | call feedkeys("/the\<C-G>\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 221 | call assert_equal(' 3 the theother', getline('.')) |
| 222 | 1 |
| 223 | " go to next match (still on line 3) |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 224 | call feedkeys("/the\<C-G>\<C-G>\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 225 | call assert_equal(' 3 the theother', getline('.')) |
| 226 | 1 |
| 227 | " go to next match (still on line 3) |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 228 | call feedkeys("/the\<C-G>\<C-G>\<C-G>\<cr>", 'tx') |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 229 | call assert_equal(' 3 the theother', getline('.')) |
| 230 | 1 |
| 231 | " go to previous match (on line 3) |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 232 | 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] | 233 | call assert_equal(' 3 the theother', getline('.')) |
| 234 | 1 |
| 235 | " go to previous match (on line 3) |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 236 | 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] | 237 | call assert_equal(' 3 the theother', getline('.')) |
| 238 | 1 |
| 239 | " go to previous match (on line 2) |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 240 | 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] | 241 | call assert_equal(' 2 these', getline('.')) |
| 242 | |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 243 | " Test 2: keep the view, |
| 244 | " after deleting a character from the search cmd |
| 245 | call setline(1, [' 1', ' 2 these', ' 3 the', ' 4 their', ' 5 there', ' 6 their', ' 7 the', ' 8 them', ' 9 these', ' 10 foobar']) |
| 246 | resize 5 |
| 247 | 1 |
| 248 | call feedkeys("/foo\<bs>\<cr>", 'tx') |
| 249 | redraw |
| 250 | call assert_equal({'lnum': 10, 'leftcol': 0, 'col': 4, 'topfill': 0, 'topline': 6, 'coladd': 0, 'skipcol': 0, 'curswant': 4}, winsaveview()) |
| 251 | |
| 252 | " remove all history entries |
| 253 | for i in range(10) |
| 254 | call histdel('/') |
| 255 | endfor |
| 256 | |
| 257 | " Test 3: reset the view, |
| 258 | " after deleting all characters from the search cmd |
| 259 | norm! 1gg0 |
| 260 | " unfortunately, neither "/foo\<c-w>\<cr>", nor "/foo\<bs>\<bs>\<bs>\<cr>", |
| 261 | " nor "/foo\<c-u>\<cr>" works to delete the commandline. |
| 262 | " In that case Vim should return "E35 no previous regular expression", |
| 263 | " but it looks like Vim still sees /foo and therefore the test fails. |
| 264 | " Therefore, disableing this test |
| 265 | "call assert_fails(feedkeys("/foo\<c-w>\<cr>", 'tx'), 'E35') |
| 266 | "call assert_equal({'lnum': 1, 'leftcol': 0, 'col': 0, 'topfill': 0, 'topline': 1, 'coladd': 0, 'skipcol': 0, 'curswant': 0}, winsaveview()) |
| 267 | |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 268 | " clean up |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 269 | set noincsearch |
Bram Moolenaar | eb992cb | 2017-03-09 18:20:16 +0100 | [diff] [blame] | 270 | call test_override("char_avail", 0) |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 271 | bw! |
| 272 | endfunc |
Bram Moolenaar | ea683da | 2016-09-09 21:41:34 +0200 | [diff] [blame] | 273 | |
| 274 | func Test_use_sub_pat() |
| 275 | split |
| 276 | let @/ = '' |
| 277 | func X() |
| 278 | s/^/a/ |
| 279 | / |
| 280 | endfunc |
| 281 | call X() |
| 282 | bwipe! |
| 283 | endfunc |
Bram Moolenaar | 6e450a5 | 2017-01-06 20:03:58 +0100 | [diff] [blame] | 284 | |
| 285 | func Test_searchpair() |
| 286 | new |
| 287 | call setline(1, ['other code here', '', '[', '" cursor here', ']']) |
| 288 | 4 |
| 289 | let a=searchpair('\[','',']','bW') |
| 290 | call assert_equal(3, a) |
| 291 | set nomagic |
| 292 | 4 |
| 293 | let a=searchpair('\[','',']','bW') |
| 294 | call assert_equal(3, a) |
| 295 | set magic |
| 296 | q! |
| 297 | endfunc |
| 298 | |
Bram Moolenaar | 4857048 | 2017-10-30 21:48:41 +0100 | [diff] [blame] | 299 | func Test_searchpair_skip() |
| 300 | func Zero() |
| 301 | return 0 |
| 302 | endfunc |
| 303 | func Partial(x) |
| 304 | return a:x |
| 305 | endfunc |
| 306 | new |
| 307 | call setline(1, ['{', 'foo', 'foo', 'foo', '}']) |
| 308 | 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', '')) |
| 309 | 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', '0')) |
| 310 | 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', {-> 0})) |
| 311 | 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', function('Zero'))) |
| 312 | 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', function('Partial', [0]))) |
| 313 | " invalid argument |
| 314 | 3 | call assert_equal(0, searchpair('{', '', '}', 'bWn', 0)) |
| 315 | bw! |
| 316 | endfunc |
| 317 | |
Bram Moolenaar | 66727e1 | 2017-03-01 22:17:05 +0100 | [diff] [blame] | 318 | func Test_searchc() |
| 319 | " These commands used to cause memory overflow in searchc(). |
| 320 | new |
| 321 | norm ixx |
| 322 | exe "norm 0t\u93cf" |
| 323 | bw! |
| 324 | endfunc |
Bram Moolenaar | a693d05 | 2017-06-29 22:23:06 +0200 | [diff] [blame] | 325 | |
| 326 | func Test_search_cmdline3() |
| 327 | if !exists('+incsearch') |
| 328 | return |
| 329 | endif |
| 330 | " need to disable char_avail, |
| 331 | " so that expansion of commandline works |
| 332 | call test_override("char_avail", 1) |
| 333 | new |
| 334 | call setline(1, [' 1', ' 2 the~e', ' 3 the theother']) |
| 335 | set incsearch |
| 336 | 1 |
| 337 | " first match |
| 338 | call feedkeys("/the\<c-l>\<cr>", 'tx') |
| 339 | call assert_equal(' 2 the~e', getline('.')) |
| 340 | " clean up |
| 341 | set noincsearch |
| 342 | call test_override("char_avail", 0) |
| 343 | bw! |
| 344 | endfunc |
Bram Moolenaar | da5116d | 2017-07-01 23:11:17 +0200 | [diff] [blame] | 345 | |
| 346 | func Test_search_cmdline4() |
| 347 | if !exists('+incsearch') |
| 348 | return |
| 349 | endif |
| 350 | " need to disable char_avail, |
| 351 | " so that expansion of commandline works |
| 352 | call test_override("char_avail", 1) |
| 353 | new |
| 354 | call setline(1, [' 1 the first', ' 2 the second', ' 3 the third']) |
| 355 | set incsearch |
| 356 | $ |
| 357 | call feedkeys("?the\<c-g>\<cr>", 'tx') |
| 358 | call assert_equal(' 3 the third', getline('.')) |
| 359 | $ |
| 360 | call feedkeys("?the\<c-g>\<c-g>\<cr>", 'tx') |
| 361 | call assert_equal(' 1 the first', getline('.')) |
| 362 | $ |
| 363 | call feedkeys("?the\<c-g>\<c-g>\<c-g>\<cr>", 'tx') |
| 364 | call assert_equal(' 2 the second', getline('.')) |
| 365 | $ |
| 366 | call feedkeys("?the\<c-t>\<cr>", 'tx') |
| 367 | call assert_equal(' 1 the first', getline('.')) |
| 368 | $ |
| 369 | call feedkeys("?the\<c-t>\<c-t>\<cr>", 'tx') |
| 370 | call assert_equal(' 3 the third', getline('.')) |
| 371 | $ |
| 372 | call feedkeys("?the\<c-t>\<c-t>\<c-t>\<cr>", 'tx') |
| 373 | call assert_equal(' 2 the second', getline('.')) |
| 374 | " clean up |
| 375 | set noincsearch |
| 376 | call test_override("char_avail", 0) |
| 377 | bw! |
| 378 | endfunc |
Bram Moolenaar | db51007 | 2017-09-28 21:52:17 +0200 | [diff] [blame] | 379 | |
Bram Moolenaar | f8e8c06 | 2017-10-22 14:44:17 +0200 | [diff] [blame] | 380 | func Test_search_cmdline5() |
| 381 | if !exists('+incsearch') |
| 382 | return |
| 383 | endif |
| 384 | " Do not call test_override("char_avail", 1) so that <C-g> and <C-t> work |
| 385 | " regardless char_avail. |
| 386 | new |
| 387 | call setline(1, [' 1 the first', ' 2 the second', ' 3 the third']) |
| 388 | set incsearch |
| 389 | 1 |
| 390 | call feedkeys("/the\<c-g>\<c-g>\<cr>", 'tx') |
| 391 | call assert_equal(' 3 the third', getline('.')) |
| 392 | $ |
| 393 | call feedkeys("?the\<c-t>\<c-t>\<c-t>\<cr>", 'tx') |
| 394 | call assert_equal(' 2 the second', getline('.')) |
| 395 | " clean up |
| 396 | set noincsearch |
| 397 | bw! |
| 398 | endfunc |
| 399 | |
Bram Moolenaar | db51007 | 2017-09-28 21:52:17 +0200 | [diff] [blame] | 400 | " Tests for regexp with various magic settings |
| 401 | func Test_search_regexp() |
| 402 | enew! |
| 403 | |
| 404 | put ='1 a aa abb abbccc' |
| 405 | exe 'normal! /a*b\{2}c\+/e' . "\<CR>" |
| 406 | call assert_equal([0, 2, 17, 0], getpos('.')) |
| 407 | |
| 408 | put ='2 d dd dee deefff' |
| 409 | exe 'normal! /\Md\*e\{2}f\+/e' . "\<CR>" |
| 410 | call assert_equal([0, 3, 17, 0], getpos('.')) |
| 411 | |
| 412 | set nomagic |
| 413 | put ='3 g gg ghh ghhiii' |
| 414 | exe 'normal! /g\*h\{2}i\+/e' . "\<CR>" |
| 415 | call assert_equal([0, 4, 17, 0], getpos('.')) |
| 416 | |
| 417 | put ='4 j jj jkk jkklll' |
| 418 | exe 'normal! /\mj*k\{2}l\+/e' . "\<CR>" |
| 419 | call assert_equal([0, 5, 17, 0], getpos('.')) |
| 420 | |
| 421 | put ='5 m mm mnn mnnooo' |
| 422 | exe 'normal! /\vm*n{2}o+/e' . "\<CR>" |
| 423 | call assert_equal([0, 6, 17, 0], getpos('.')) |
| 424 | |
| 425 | put ='6 x ^aa$ x' |
| 426 | exe 'normal! /\V^aa$' . "\<CR>" |
| 427 | call assert_equal([0, 7, 5, 0], getpos('.')) |
| 428 | |
| 429 | set magic |
| 430 | put ='7 (a)(b) abbaa' |
| 431 | exe 'normal! /\v(a)(b)\2\1\1/e' . "\<CR>" |
| 432 | call assert_equal([0, 8, 14, 0], getpos('.')) |
| 433 | |
| 434 | put ='8 axx [ab]xx' |
| 435 | exe 'normal! /\V[ab]\(\[xy]\)\1' . "\<CR>" |
| 436 | call assert_equal([0, 9, 7, 0], getpos('.')) |
| 437 | |
| 438 | set undolevels=100 |
| 439 | put ='9 foobar' |
| 440 | put ='' |
| 441 | exe "normal! a\<C-G>u\<Esc>" |
| 442 | normal G |
| 443 | exe 'normal! dv?bar?' . "\<CR>" |
| 444 | call assert_equal('9 foo', getline('.')) |
| 445 | call assert_equal([0, 10, 5, 0], getpos('.')) |
| 446 | call assert_equal(10, line('$')) |
| 447 | normal u |
| 448 | call assert_equal('9 foobar', getline('.')) |
| 449 | call assert_equal([0, 10, 6, 0], getpos('.')) |
| 450 | call assert_equal(11, line('$')) |
| 451 | |
| 452 | set undolevels& |
| 453 | enew! |
| 454 | endfunc |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 455 | |
| 456 | func Test_search_cmdline_incsearch_highlight() |
| 457 | if !exists('+incsearch') |
| 458 | return |
| 459 | endif |
| 460 | set incsearch hlsearch |
| 461 | " need to disable char_avail, |
| 462 | " so that expansion of commandline works |
| 463 | call test_override("char_avail", 1) |
| 464 | new |
| 465 | call setline(1, ['aaa 1 the first', ' 2 the second', ' 3 the third']) |
| 466 | |
| 467 | 1 |
| 468 | call feedkeys("/second\<cr>", 'tx') |
| 469 | call assert_equal('second', @/) |
| 470 | call assert_equal(' 2 the second', getline('.')) |
| 471 | |
| 472 | " Canceling search won't change @/ |
| 473 | 1 |
| 474 | let @/ = 'last pattern' |
| 475 | call feedkeys("/third\<C-c>", 'tx') |
| 476 | call assert_equal('last pattern', @/) |
| 477 | call feedkeys("/third\<Esc>", 'tx') |
| 478 | call assert_equal('last pattern', @/) |
| 479 | call feedkeys("/3\<bs>\<bs>", 'tx') |
| 480 | call assert_equal('last pattern', @/) |
| 481 | call feedkeys("/third\<c-g>\<c-t>\<Esc>", 'tx') |
| 482 | call assert_equal('last pattern', @/) |
| 483 | |
| 484 | " clean up |
| 485 | set noincsearch nohlsearch |
| 486 | bw! |
| 487 | endfunc |
| 488 | |
| 489 | func Test_search_cmdline_incsearch_highlight_attr() |
| 490 | if !exists('+incsearch') || !has('terminal') || has('gui_running') |
| 491 | return |
| 492 | endif |
| 493 | let h = winheight(0) |
| 494 | if h < 3 |
| 495 | return |
| 496 | endif |
| 497 | let g:buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3}) |
| 498 | |
| 499 | " Prepare buffer text |
| 500 | let lines = ['abb vim vim vi', 'vimvivim'] |
| 501 | call term_sendkeys(g:buf, 'i' . join(lines, "\n") . "\<esc>gg0") |
| 502 | call term_wait(g:buf, 200) |
| 503 | call assert_equal(lines[0], term_getline(g:buf, 1)) |
| 504 | |
| 505 | " Get attr of normal(a0), incsearch(a1), hlsearch(a2) highlight |
| 506 | call term_sendkeys(g:buf, ":set incsearch hlsearch\<cr>") |
| 507 | call term_sendkeys(g:buf, '/b') |
| 508 | call term_wait(g:buf, 200) |
| 509 | let screen_line1 = term_scrape(g:buf, 1) |
| 510 | call assert_true(len(screen_line1) > 2) |
| 511 | " a0: attr_normal |
| 512 | let a0 = screen_line1[0].attr |
| 513 | " a1: attr_incsearch |
| 514 | let a1 = screen_line1[1].attr |
| 515 | " a2: attr_hlsearch |
| 516 | let a2 = screen_line1[2].attr |
| 517 | call assert_notequal(a0, a1) |
| 518 | call assert_notequal(a0, a2) |
| 519 | call assert_notequal(a1, a2) |
| 520 | call term_sendkeys(g:buf, "\<cr>gg0") |
| 521 | |
| 522 | " Test incremental highlight search |
| 523 | call term_sendkeys(g:buf, "/vim") |
| 524 | call term_wait(g:buf, 200) |
| 525 | " Buffer: |
| 526 | " abb vim vim vi |
| 527 | " vimvivim |
| 528 | " Search: /vim |
| 529 | let attr_line1 = [a0,a0,a0,a0,a1,a1,a1,a0,a2,a2,a2,a0,a0,a0] |
| 530 | let attr_line2 = [a2,a2,a2,a0,a0,a2,a2,a2] |
| 531 | call assert_equal(attr_line1, map(term_scrape(g:buf, 1)[:len(attr_line1)-1], 'v:val.attr')) |
| 532 | call assert_equal(attr_line2, map(term_scrape(g:buf, 2)[:len(attr_line2)-1], 'v:val.attr')) |
| 533 | |
| 534 | " Test <C-g> |
| 535 | call term_sendkeys(g:buf, "\<C-g>\<C-g>") |
| 536 | call term_wait(g:buf, 200) |
| 537 | let attr_line1 = [a0,a0,a0,a0,a2,a2,a2,a0,a2,a2,a2,a0,a0,a0] |
| 538 | let attr_line2 = [a1,a1,a1,a0,a0,a2,a2,a2] |
| 539 | call assert_equal(attr_line1, map(term_scrape(g:buf, 1)[:len(attr_line1)-1], 'v:val.attr')) |
| 540 | call assert_equal(attr_line2, map(term_scrape(g:buf, 2)[:len(attr_line2)-1], 'v:val.attr')) |
| 541 | |
| 542 | " Test <C-t> |
| 543 | call term_sendkeys(g:buf, "\<C-t>") |
| 544 | call term_wait(g:buf, 200) |
| 545 | let attr_line1 = [a0,a0,a0,a0,a2,a2,a2,a0,a1,a1,a1,a0,a0,a0] |
| 546 | let attr_line2 = [a2,a2,a2,a0,a0,a2,a2,a2] |
| 547 | call assert_equal(attr_line1, map(term_scrape(g:buf, 1)[:len(attr_line1)-1], 'v:val.attr')) |
| 548 | call assert_equal(attr_line2, map(term_scrape(g:buf, 2)[:len(attr_line2)-1], 'v:val.attr')) |
| 549 | |
| 550 | " Type Enter and a1(incsearch highlight) should become a2(hlsearch highlight) |
| 551 | call term_sendkeys(g:buf, "\<cr>") |
| 552 | call term_wait(g:buf, 200) |
| 553 | let attr_line1 = [a0,a0,a0,a0,a2,a2,a2,a0,a2,a2,a2,a0,a0,a0] |
| 554 | let attr_line2 = [a2,a2,a2,a0,a0,a2,a2,a2] |
| 555 | call assert_equal(attr_line1, map(term_scrape(g:buf, 1)[:len(attr_line1)-1], 'v:val.attr')) |
| 556 | call assert_equal(attr_line2, map(term_scrape(g:buf, 2)[:len(attr_line2)-1], 'v:val.attr')) |
| 557 | |
| 558 | " Test nohlsearch. a2(hlsearch highlight) should become a0(normal highlight) |
| 559 | call term_sendkeys(g:buf, ":1\<cr>") |
| 560 | call term_sendkeys(g:buf, ":set nohlsearch\<cr>") |
| 561 | call term_sendkeys(g:buf, "/vim") |
| 562 | call term_wait(g:buf, 200) |
| 563 | let attr_line1 = [a0,a0,a0,a0,a1,a1,a1,a0,a0,a0,a0,a0,a0,a0] |
| 564 | let attr_line2 = [a0,a0,a0,a0,a0,a0,a0,a0] |
| 565 | call assert_equal(attr_line1, map(term_scrape(g:buf, 1)[:len(attr_line1)-1], 'v:val.attr')) |
| 566 | call assert_equal(attr_line2, map(term_scrape(g:buf, 2)[:len(attr_line2)-1], 'v:val.attr')) |
| 567 | |
| 568 | bwipe! |
| 569 | endfunc |