blob: 7f4f7e0ec9f90b86ae1490ad2ee7d2aeb6dc4158 [file] [log] [blame]
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001" Test for the search command
2
Bram Moolenaarc3c766e2017-03-08 22:55:19 +01003set belloff=all
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02004func Test_search_cmdline()
5 if !exists('+incsearch')
6 return
7 endif
8 " need to disable char_avail,
9 " so that expansion of commandline works
10 call test_disable_char_avail(1)
11 new
12 call setline(1, [' 1', ' 2 these', ' 3 the', ' 4 their', ' 5 there', ' 6 their', ' 7 the', ' 8 them', ' 9 these', ' 10 foobar'])
13 " Test 1
14 " CTRL-N / CTRL-P skips through the previous search history
15 set noincsearch
16 :1
17 call feedkeys("/foobar\<cr>", 'tx')
18 call feedkeys("/the\<cr>",'tx')
19 call assert_equal('the', @/)
Bram Moolenaar11956692016-08-27 16:26:56 +020020 call feedkeys("/thes\<C-P>\<C-P>\<cr>",'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020021 call assert_equal('foobar', @/)
22
23 " Test 2
Bram Moolenaar11956692016-08-27 16:26:56 +020024 " Ctrl-G goes from one match to the next
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020025 " until the end of the buffer
26 set incsearch nowrapscan
27 :1
28 " first match
29 call feedkeys("/the\<cr>", 'tx')
30 call assert_equal(' 2 these', getline('.'))
31 :1
32 " second match
Bram Moolenaar11956692016-08-27 16:26:56 +020033 call feedkeys("/the\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020034 call assert_equal(' 3 the', getline('.'))
Bram Moolenaardda933d2016-09-03 21:04:58 +020035 call assert_equal([0, 0, 0, 0], getpos('"'))
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020036 :1
37 " third match
Bram Moolenaar11956692016-08-27 16:26:56 +020038 call feedkeys("/the".repeat("\<C-G>", 2)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020039 call assert_equal(' 4 their', getline('.'))
40 :1
41 " fourth match
Bram Moolenaar11956692016-08-27 16:26:56 +020042 call feedkeys("/the".repeat("\<C-G>", 3)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020043 call assert_equal(' 5 there', getline('.'))
44 :1
45 " fifth match
Bram Moolenaar11956692016-08-27 16:26:56 +020046 call feedkeys("/the".repeat("\<C-G>", 4)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020047 call assert_equal(' 6 their', getline('.'))
48 :1
49 " sixth match
Bram Moolenaar11956692016-08-27 16:26:56 +020050 call feedkeys("/the".repeat("\<C-G>", 5)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020051 call assert_equal(' 7 the', getline('.'))
52 :1
53 " seventh match
Bram Moolenaar11956692016-08-27 16:26:56 +020054 call feedkeys("/the".repeat("\<C-G>", 6)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020055 call assert_equal(' 8 them', getline('.'))
56 :1
57 " eigth match
Bram Moolenaar11956692016-08-27 16:26:56 +020058 call feedkeys("/the".repeat("\<C-G>", 7)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020059 call assert_equal(' 9 these', getline('.'))
60 :1
61 " no further match
Bram Moolenaar11956692016-08-27 16:26:56 +020062 call feedkeys("/the".repeat("\<C-G>", 8)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020063 call assert_equal(' 9 these', getline('.'))
Bram Moolenaardda933d2016-09-03 21:04:58 +020064 call assert_equal([0, 0, 0, 0], getpos('"'))
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020065
66 " Test 3
Bram Moolenaar11956692016-08-27 16:26:56 +020067 " Ctrl-G goes from one match to the next
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020068 " and continues back at the top
69 set incsearch wrapscan
70 :1
71 " first match
72 call feedkeys("/the\<cr>", 'tx')
73 call assert_equal(' 2 these', getline('.'))
74 :1
75 " second match
Bram Moolenaar11956692016-08-27 16:26:56 +020076 call feedkeys("/the\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020077 call assert_equal(' 3 the', getline('.'))
78 :1
79 " third match
Bram Moolenaar11956692016-08-27 16:26:56 +020080 call feedkeys("/the".repeat("\<C-G>", 2)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020081 call assert_equal(' 4 their', getline('.'))
82 :1
83 " fourth match
Bram Moolenaar11956692016-08-27 16:26:56 +020084 call feedkeys("/the".repeat("\<C-G>", 3)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020085 call assert_equal(' 5 there', getline('.'))
86 :1
87 " fifth match
Bram Moolenaar11956692016-08-27 16:26:56 +020088 call feedkeys("/the".repeat("\<C-G>", 4)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020089 call assert_equal(' 6 their', getline('.'))
90 :1
91 " sixth match
Bram Moolenaar11956692016-08-27 16:26:56 +020092 call feedkeys("/the".repeat("\<C-G>", 5)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020093 call assert_equal(' 7 the', getline('.'))
94 :1
95 " seventh match
Bram Moolenaar11956692016-08-27 16:26:56 +020096 call feedkeys("/the".repeat("\<C-G>", 6)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +020097 call assert_equal(' 8 them', getline('.'))
98 :1
99 " eigth match
Bram Moolenaar11956692016-08-27 16:26:56 +0200100 call feedkeys("/the".repeat("\<C-G>", 7)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200101 call assert_equal(' 9 these', getline('.'))
102 :1
103 " back at first match
Bram Moolenaar11956692016-08-27 16:26:56 +0200104 call feedkeys("/the".repeat("\<C-G>", 8)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200105 call assert_equal(' 2 these', getline('.'))
106
107 " Test 4
Bram Moolenaar11956692016-08-27 16:26:56 +0200108 " CTRL-T goes to the previous match
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200109 set incsearch nowrapscan
110 $
111 " first match
112 call feedkeys("?the\<cr>", 'tx')
113 call assert_equal(' 9 these', getline('.'))
114 $
115 " first match
Bram Moolenaar11956692016-08-27 16:26:56 +0200116 call feedkeys("?the\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200117 call assert_equal(' 9 these', getline('.'))
118 $
119 " second match
Bram Moolenaar11956692016-08-27 16:26:56 +0200120 call feedkeys("?the".repeat("\<C-T>", 1)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200121 call assert_equal(' 8 them', getline('.'))
122 $
123 " last match
Bram Moolenaar11956692016-08-27 16:26:56 +0200124 call feedkeys("?the".repeat("\<C-T>", 7)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200125 call assert_equal(' 2 these', getline('.'))
126 $
127 " last match
Bram Moolenaar11956692016-08-27 16:26:56 +0200128 call feedkeys("?the".repeat("\<C-T>", 8)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200129 call assert_equal(' 2 these', getline('.'))
130
131 " Test 5
Bram Moolenaar11956692016-08-27 16:26:56 +0200132 " CTRL-T goes to the previous match
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200133 set incsearch wrapscan
134 $
135 " first match
136 call feedkeys("?the\<cr>", 'tx')
137 call assert_equal(' 9 these', getline('.'))
138 $
139 " first match at the top
Bram Moolenaar11956692016-08-27 16:26:56 +0200140 call feedkeys("?the\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200141 call assert_equal(' 2 these', getline('.'))
142 $
143 " second match
Bram Moolenaar11956692016-08-27 16:26:56 +0200144 call feedkeys("?the".repeat("\<C-T>", 1)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200145 call assert_equal(' 8 them', getline('.'))
146 $
147 " last match
Bram Moolenaar11956692016-08-27 16:26:56 +0200148 call feedkeys("?the".repeat("\<C-T>", 7)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200149 call assert_equal(' 2 these', getline('.'))
150 $
151 " back at the bottom of the buffer
Bram Moolenaar11956692016-08-27 16:26:56 +0200152 call feedkeys("?the".repeat("\<C-T>", 8)."\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200153 call assert_equal(' 9 these', getline('.'))
154
155 " Test 6
156 " CTRL-L adds to the search pattern
157 set incsearch wrapscan
158 1
159 " first match
160 call feedkeys("/the\<c-l>\<cr>", 'tx')
161 call assert_equal(' 2 these', getline('.'))
162 1
163 " go to next match of 'thes'
Bram Moolenaar11956692016-08-27 16:26:56 +0200164 call feedkeys("/the\<c-l>\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200165 call assert_equal(' 9 these', getline('.'))
166 1
167 " wrap around
Bram Moolenaar11956692016-08-27 16:26:56 +0200168 call feedkeys("/the\<c-l>\<C-G>\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200169 call assert_equal(' 2 these', getline('.'))
170 1
171 " wrap around
172 set nowrapscan
Bram Moolenaar11956692016-08-27 16:26:56 +0200173 call feedkeys("/the\<c-l>\<C-G>\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200174 call assert_equal(' 9 these', getline('.'))
175
176 " Test 7
177 " <bs> remove from match, but stay at current match
178 set incsearch wrapscan
179 1
180 " first match
181 call feedkeys("/thei\<cr>", 'tx')
182 call assert_equal(' 4 their', getline('.'))
183 1
184 " delete one char, add another
185 call feedkeys("/thei\<bs>s\<cr>", 'tx')
Bram Moolenaardda933d2016-09-03 21:04:58 +0200186 call assert_equal(' 2 these', getline('.'))
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200187 1
188 " delete one char, add another, go to previous match, add one char
Bram Moolenaar11956692016-08-27 16:26:56 +0200189 call feedkeys("/thei\<bs>s\<bs>\<C-T>\<c-l>\<cr>", 'tx')
Bram Moolenaardda933d2016-09-03 21:04:58 +0200190 call assert_equal(' 9 these', getline('.'))
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200191 1
192 " delete all chars, start from the beginning again
193 call feedkeys("/them". repeat("\<bs>",4).'the\>'."\<cr>", 'tx')
194 call assert_equal(' 3 the', getline('.'))
195
196 " clean up
197 call test_disable_char_avail(0)
198 bw!
199endfunc
200
201func Test_search_cmdline2()
202 if !exists('+incsearch')
203 return
204 endif
205 " need to disable char_avail,
206 " so that expansion of commandline works
207 call test_disable_char_avail(1)
208 new
209 call setline(1, [' 1', ' 2 these', ' 3 the theother'])
210 " Test 1
Bram Moolenaar11956692016-08-27 16:26:56 +0200211 " Ctrl-T goes correctly back and forth
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200212 set incsearch
213 1
214 " first match
215 call feedkeys("/the\<cr>", 'tx')
216 call assert_equal(' 2 these', getline('.'))
217 1
218 " go to next match (on next line)
Bram Moolenaar11956692016-08-27 16:26:56 +0200219 call feedkeys("/the\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200220 call assert_equal(' 3 the theother', getline('.'))
221 1
222 " go to next match (still on line 3)
Bram Moolenaar11956692016-08-27 16:26:56 +0200223 call feedkeys("/the\<C-G>\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200224 call assert_equal(' 3 the theother', getline('.'))
225 1
226 " go to next match (still on line 3)
Bram Moolenaar11956692016-08-27 16:26:56 +0200227 call feedkeys("/the\<C-G>\<C-G>\<C-G>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200228 call assert_equal(' 3 the theother', getline('.'))
229 1
230 " go to previous match (on line 3)
Bram Moolenaar11956692016-08-27 16:26:56 +0200231 call feedkeys("/the\<C-G>\<C-G>\<C-G>\<C-T>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200232 call assert_equal(' 3 the theother', getline('.'))
233 1
234 " go to previous match (on line 3)
Bram Moolenaar11956692016-08-27 16:26:56 +0200235 call feedkeys("/the\<C-G>\<C-G>\<C-G>\<C-T>\<C-T>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200236 call assert_equal(' 3 the theother', getline('.'))
237 1
238 " go to previous match (on line 2)
Bram Moolenaar11956692016-08-27 16:26:56 +0200239 call feedkeys("/the\<C-G>\<C-G>\<C-G>\<C-T>\<C-T>\<C-T>\<cr>", 'tx')
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200240 call assert_equal(' 2 these', getline('.'))
241
Bram Moolenaardda933d2016-09-03 21:04:58 +0200242 " Test 2: keep the view,
243 " after deleting a character from the search cmd
244 call setline(1, [' 1', ' 2 these', ' 3 the', ' 4 their', ' 5 there', ' 6 their', ' 7 the', ' 8 them', ' 9 these', ' 10 foobar'])
245 resize 5
246 1
247 call feedkeys("/foo\<bs>\<cr>", 'tx')
248 redraw
249 call assert_equal({'lnum': 10, 'leftcol': 0, 'col': 4, 'topfill': 0, 'topline': 6, 'coladd': 0, 'skipcol': 0, 'curswant': 4}, winsaveview())
250
251 " remove all history entries
252 for i in range(10)
253 call histdel('/')
254 endfor
255
256 " Test 3: reset the view,
257 " after deleting all characters from the search cmd
258 norm! 1gg0
259 " unfortunately, neither "/foo\<c-w>\<cr>", nor "/foo\<bs>\<bs>\<bs>\<cr>",
260 " nor "/foo\<c-u>\<cr>" works to delete the commandline.
261 " In that case Vim should return "E35 no previous regular expression",
262 " but it looks like Vim still sees /foo and therefore the test fails.
263 " Therefore, disableing this test
264 "call assert_fails(feedkeys("/foo\<c-w>\<cr>", 'tx'), 'E35')
265 "call assert_equal({'lnum': 1, 'leftcol': 0, 'col': 0, 'topfill': 0, 'topline': 1, 'coladd': 0, 'skipcol': 0, 'curswant': 0}, winsaveview())
266
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200267 " clean up
Bram Moolenaardda933d2016-09-03 21:04:58 +0200268 set noincsearch
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200269 call test_disable_char_avail(0)
270 bw!
271endfunc
Bram Moolenaarea683da2016-09-09 21:41:34 +0200272
273func Test_use_sub_pat()
274 split
275 let @/ = ''
276 func X()
277 s/^/a/
278 /
279 endfunc
280 call X()
281 bwipe!
282endfunc
Bram Moolenaar6e450a52017-01-06 20:03:58 +0100283
284func Test_searchpair()
285 new
286 call setline(1, ['other code here', '', '[', '" cursor here', ']'])
287 4
288 let a=searchpair('\[','',']','bW')
289 call assert_equal(3, a)
290 set nomagic
291 4
292 let a=searchpair('\[','',']','bW')
293 call assert_equal(3, a)
294 set magic
295 q!
296endfunc
297
Bram Moolenaar66727e12017-03-01 22:17:05 +0100298func Test_searchc()
299 " These commands used to cause memory overflow in searchc().
300 new
301 norm ixx
302 exe "norm 0t\u93cf"
303 bw!
304endfunc