blob: d74d8d73ff3bf3593d97079c5ab1c3325109860d [file] [log] [blame]
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * match.c: functions for highlighting matches
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
17
18# define SEARCH_HL_PRIORITY 0
19
20/*
21 * Add match to the match list of window 'wp'. The pattern 'pat' will be
22 * highlighted with the group 'grp' with priority 'prio'.
23 * Optionally, a desired ID 'id' can be specified (greater than or equal to 1).
24 * If no particular ID is desired, -1 must be specified for 'id'.
25 * Return ID of added match, -1 on failure.
26 */
27 static int
28match_add(
29 win_T *wp,
30 char_u *grp,
31 char_u *pat,
32 int prio,
33 int id,
34 list_T *pos_list,
35 char_u *conceal_char UNUSED) // pointer to conceal replacement char
36{
37 matchitem_T *cur;
38 matchitem_T *prev;
39 matchitem_T *m;
40 int hlg_id;
41 regprog_T *regprog = NULL;
42 int rtype = SOME_VALID;
43
44 if (*grp == NUL || (pat != NULL && *pat == NUL))
45 return -1;
46 if (id < -1 || id == 0)
47 {
Bram Moolenaarbb8cac52022-01-05 18:16:53 +000048 semsg(_(e_invalid_id_nr_must_be_greater_than_or_equal_to_one_1), id);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +020049 return -1;
50 }
51 if (id != -1)
52 {
53 cur = wp->w_match_head;
54 while (cur != NULL)
55 {
56 if (cur->id == id)
57 {
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +000058 semsg(_(e_id_already_taken_nr), id);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +020059 return -1;
60 }
61 cur = cur->next;
62 }
63 }
64 if ((hlg_id = syn_namen2id(grp, (int)STRLEN(grp))) == 0)
65 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +020066 semsg(_(e_no_such_highlight_group_name_str), grp);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +020067 return -1;
68 }
69 if (pat != NULL && (regprog = vim_regcomp(pat, RE_MAGIC)) == NULL)
70 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +000071 semsg(_(e_invalid_argument_str), pat);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +020072 return -1;
73 }
74
75 // Find available match ID.
76 while (id == -1)
77 {
78 cur = wp->w_match_head;
79 while (cur != NULL && cur->id != wp->w_next_match_id)
80 cur = cur->next;
81 if (cur == NULL)
82 id = wp->w_next_match_id;
83 wp->w_next_match_id++;
84 }
85
86 // Build new match.
87 m = ALLOC_CLEAR_ONE(matchitem_T);
88 m->id = id;
89 m->priority = prio;
90 m->pattern = pat == NULL ? NULL : vim_strsave(pat);
91 m->hlg_id = hlg_id;
92 m->match.regprog = regprog;
93 m->match.rmm_ic = FALSE;
94 m->match.rmm_maxcol = 0;
95# if defined(FEAT_CONCEAL)
96 m->conceal_char = 0;
97 if (conceal_char != NULL)
98 m->conceal_char = (*mb_ptr2char)(conceal_char);
99# endif
100
101 // Set up position matches
102 if (pos_list != NULL)
103 {
104 linenr_T toplnum = 0;
105 linenr_T botlnum = 0;
106 listitem_T *li;
107 int i;
108
109 CHECK_LIST_MATERIALIZE(pos_list);
110 for (i = 0, li = pos_list->lv_first; li != NULL && i < MAXPOSMATCH;
111 i++, li = li->li_next)
112 {
113 linenr_T lnum = 0;
114 colnr_T col = 0;
115 int len = 1;
116 list_T *subl;
117 listitem_T *subli;
118 int error = FALSE;
119
120 if (li->li_tv.v_type == VAR_LIST)
121 {
122 subl = li->li_tv.vval.v_list;
123 if (subl == NULL)
124 goto fail;
125 subli = subl->lv_first;
126 if (subli == NULL)
127 goto fail;
128 lnum = tv_get_number_chk(&subli->li_tv, &error);
129 if (error == TRUE)
130 goto fail;
131 if (lnum == 0)
132 {
133 --i;
134 continue;
135 }
136 m->pos.pos[i].lnum = lnum;
137 subli = subli->li_next;
138 if (subli != NULL)
139 {
140 col = tv_get_number_chk(&subli->li_tv, &error);
141 if (error == TRUE)
142 goto fail;
143 subli = subli->li_next;
144 if (subli != NULL)
145 {
146 len = tv_get_number_chk(&subli->li_tv, &error);
147 if (error == TRUE)
148 goto fail;
149 }
150 }
151 m->pos.pos[i].col = col;
152 m->pos.pos[i].len = len;
153 }
154 else if (li->li_tv.v_type == VAR_NUMBER)
155 {
156 if (li->li_tv.vval.v_number == 0)
157 {
158 --i;
159 continue;
160 }
161 m->pos.pos[i].lnum = li->li_tv.vval.v_number;
162 m->pos.pos[i].col = 0;
163 m->pos.pos[i].len = 0;
164 }
165 else
166 {
Bram Moolenaar9a846fb2022-01-01 21:59:18 +0000167 emsg(_(e_list_or_number_required));
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200168 goto fail;
169 }
170 if (toplnum == 0 || lnum < toplnum)
171 toplnum = lnum;
172 if (botlnum == 0 || lnum >= botlnum)
173 botlnum = lnum + 1;
174 }
175
176 // Calculate top and bottom lines for redrawing area
177 if (toplnum != 0)
178 {
179 if (wp->w_buffer->b_mod_set)
180 {
181 if (wp->w_buffer->b_mod_top > toplnum)
182 wp->w_buffer->b_mod_top = toplnum;
183 if (wp->w_buffer->b_mod_bot < botlnum)
184 wp->w_buffer->b_mod_bot = botlnum;
185 }
186 else
187 {
188 wp->w_buffer->b_mod_set = TRUE;
189 wp->w_buffer->b_mod_top = toplnum;
190 wp->w_buffer->b_mod_bot = botlnum;
191 wp->w_buffer->b_mod_xlines = 0;
192 }
193 m->pos.toplnum = toplnum;
194 m->pos.botlnum = botlnum;
195 rtype = VALID;
196 }
197 }
198
199 // Insert new match. The match list is in ascending order with regard to
200 // the match priorities.
201 cur = wp->w_match_head;
202 prev = cur;
203 while (cur != NULL && prio >= cur->priority)
204 {
205 prev = cur;
206 cur = cur->next;
207 }
208 if (cur == prev)
209 wp->w_match_head = m;
210 else
211 prev->next = m;
212 m->next = cur;
213
214 redraw_win_later(wp, rtype);
215 return id;
216
217fail:
218 vim_free(m);
219 return -1;
220}
221
222/*
223 * Delete match with ID 'id' in the match list of window 'wp'.
224 * Print error messages if 'perr' is TRUE.
225 */
226 static int
227match_delete(win_T *wp, int id, int perr)
228{
229 matchitem_T *cur = wp->w_match_head;
230 matchitem_T *prev = cur;
231 int rtype = SOME_VALID;
232
233 if (id < 1)
234 {
235 if (perr == TRUE)
Bram Moolenaarbb8cac52022-01-05 18:16:53 +0000236 semsg(_(e_invalid_id_nr_must_be_greater_than_or_equal_to_one_2), id);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200237 return -1;
238 }
239 while (cur != NULL && cur->id != id)
240 {
241 prev = cur;
242 cur = cur->next;
243 }
244 if (cur == NULL)
245 {
246 if (perr == TRUE)
Bram Moolenaar9d00e4a2022-01-05 17:49:15 +0000247 semsg(_(e_id_not_found_nr), id);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200248 return -1;
249 }
250 if (cur == prev)
251 wp->w_match_head = cur->next;
252 else
253 prev->next = cur->next;
254 vim_regfree(cur->match.regprog);
255 vim_free(cur->pattern);
256 if (cur->pos.toplnum != 0)
257 {
258 if (wp->w_buffer->b_mod_set)
259 {
260 if (wp->w_buffer->b_mod_top > cur->pos.toplnum)
261 wp->w_buffer->b_mod_top = cur->pos.toplnum;
262 if (wp->w_buffer->b_mod_bot < cur->pos.botlnum)
263 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
264 }
265 else
266 {
267 wp->w_buffer->b_mod_set = TRUE;
268 wp->w_buffer->b_mod_top = cur->pos.toplnum;
269 wp->w_buffer->b_mod_bot = cur->pos.botlnum;
270 wp->w_buffer->b_mod_xlines = 0;
271 }
272 rtype = VALID;
273 }
274 vim_free(cur);
275 redraw_win_later(wp, rtype);
276 return 0;
277}
278
279/*
280 * Delete all matches in the match list of window 'wp'.
281 */
282 void
283clear_matches(win_T *wp)
284{
285 matchitem_T *m;
286
287 while (wp->w_match_head != NULL)
288 {
289 m = wp->w_match_head->next;
290 vim_regfree(wp->w_match_head->match.regprog);
291 vim_free(wp->w_match_head->pattern);
292 vim_free(wp->w_match_head);
293 wp->w_match_head = m;
294 }
295 redraw_win_later(wp, SOME_VALID);
296}
297
298/*
299 * Get match from ID 'id' in window 'wp'.
300 * Return NULL if match not found.
301 */
302 static matchitem_T *
303get_match(win_T *wp, int id)
304{
305 matchitem_T *cur = wp->w_match_head;
306
307 while (cur != NULL && cur->id != id)
308 cur = cur->next;
309 return cur;
310}
311
312/*
313 * Init for calling prepare_search_hl().
314 */
315 void
316init_search_hl(win_T *wp, match_T *search_hl)
317{
318 matchitem_T *cur;
319
320 // Setup for match and 'hlsearch' highlighting. Disable any previous
321 // match
322 cur = wp->w_match_head;
323 while (cur != NULL)
324 {
325 cur->hl.rm = cur->match;
326 if (cur->hlg_id == 0)
327 cur->hl.attr = 0;
328 else
329 cur->hl.attr = syn_id2attr(cur->hlg_id);
330 cur->hl.buf = wp->w_buffer;
331 cur->hl.lnum = 0;
332 cur->hl.first_lnum = 0;
333# ifdef FEAT_RELTIME
334 // Set the time limit to 'redrawtime'.
335 profile_setlimit(p_rdt, &(cur->hl.tm));
336# endif
337 cur = cur->next;
338 }
339 search_hl->buf = wp->w_buffer;
340 search_hl->lnum = 0;
341 search_hl->first_lnum = 0;
342 // time limit is set at the toplevel, for all windows
343}
344
345/*
346 * If there is a match fill "shl" and return one.
347 * Return zero otherwise.
348 */
349 static int
350next_search_hl_pos(
351 match_T *shl, // points to a match
352 linenr_T lnum,
353 posmatch_T *posmatch, // match positions
354 colnr_T mincol) // minimal column for a match
355{
356 int i;
357 int found = -1;
358
359 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
360 {
361 llpos_T *pos = &posmatch->pos[i];
362
363 if (pos->lnum == 0)
364 break;
365 if (pos->len == 0 && pos->col < mincol)
366 continue;
367 if (pos->lnum == lnum)
368 {
369 if (found >= 0)
370 {
371 // if this match comes before the one at "found" then swap
372 // them
373 if (pos->col < posmatch->pos[found].col)
374 {
375 llpos_T tmp = *pos;
376
377 *pos = posmatch->pos[found];
378 posmatch->pos[found] = tmp;
379 }
380 }
381 else
382 found = i;
383 }
384 }
385 posmatch->cur = 0;
386 if (found >= 0)
387 {
388 colnr_T start = posmatch->pos[found].col == 0
389 ? 0 : posmatch->pos[found].col - 1;
390 colnr_T end = posmatch->pos[found].col == 0
391 ? MAXCOL : start + posmatch->pos[found].len;
392
393 shl->lnum = lnum;
394 shl->rm.startpos[0].lnum = 0;
395 shl->rm.startpos[0].col = start;
396 shl->rm.endpos[0].lnum = 0;
397 shl->rm.endpos[0].col = end;
398 shl->is_addpos = TRUE;
399 posmatch->cur = found + 1;
400 return 1;
401 }
402 return 0;
403}
404
405/*
406 * Search for a next 'hlsearch' or match.
407 * Uses shl->buf.
408 * Sets shl->lnum and shl->rm contents.
409 * Note: Assumes a previous match is always before "lnum", unless
410 * shl->lnum is zero.
411 * Careful: Any pointers for buffer lines will become invalid.
412 */
413 static void
414next_search_hl(
415 win_T *win,
416 match_T *search_hl,
417 match_T *shl, // points to search_hl or a match
418 linenr_T lnum,
419 colnr_T mincol, // minimal column for a match
420 matchitem_T *cur) // to retrieve match positions if any
421{
422 linenr_T l;
423 colnr_T matchcol;
424 long nmatched;
425 int called_emsg_before = called_emsg;
426
427 // for :{range}s/pat only highlight inside the range
Bram Moolenaar94fb8272021-12-29 19:22:44 +0000428 if ((lnum < search_first_line || lnum > search_last_line) && cur == NULL)
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200429 {
430 shl->lnum = 0;
431 return;
432 }
433
434 if (shl->lnum != 0)
435 {
436 // Check for three situations:
437 // 1. If the "lnum" is below a previous match, start a new search.
438 // 2. If the previous match includes "mincol", use it.
439 // 3. Continue after the previous match.
440 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
441 if (lnum > l)
442 shl->lnum = 0;
443 else if (lnum < l || shl->rm.endpos[0].col > mincol)
444 return;
445 }
446
447 // Repeat searching for a match until one is found that includes "mincol"
448 // or none is found in this line.
449 for (;;)
450 {
451# ifdef FEAT_RELTIME
452 // Stop searching after passing the time limit.
453 if (profile_passed_limit(&(shl->tm)))
454 {
455 shl->lnum = 0; // no match found in time
456 break;
457 }
458# endif
459 // Three situations:
460 // 1. No useful previous match: search from start of line.
461 // 2. Not Vi compatible or empty match: continue at next character.
462 // Break the loop if this is beyond the end of the line.
463 // 3. Vi compatible searching: continue at end of previous match.
464 if (shl->lnum == 0)
465 matchcol = 0;
466 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
467 || (shl->rm.endpos[0].lnum == 0
468 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
469 {
470 char_u *ml;
471
472 matchcol = shl->rm.startpos[0].col;
473 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
474 if (*ml == NUL)
475 {
476 ++matchcol;
477 shl->lnum = 0;
478 break;
479 }
480 if (has_mbyte)
481 matchcol += mb_ptr2len(ml);
482 else
483 ++matchcol;
484 }
485 else
486 matchcol = shl->rm.endpos[0].col;
487
488 shl->lnum = lnum;
489 if (shl->rm.regprog != NULL)
490 {
491 // Remember whether shl->rm is using a copy of the regprog in
492 // cur->match.
493 int regprog_is_copy = (shl != search_hl && cur != NULL
494 && shl == &cur->hl
495 && cur->match.regprog == cur->hl.rm.regprog);
496 int timed_out = FALSE;
497
498 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
499 matchcol,
500#ifdef FEAT_RELTIME
501 &(shl->tm), &timed_out
502#else
503 NULL, NULL
504#endif
505 );
506 // Copy the regprog, in case it got freed and recompiled.
507 if (regprog_is_copy)
508 cur->match.regprog = cur->hl.rm.regprog;
509
510 if (called_emsg > called_emsg_before || got_int || timed_out)
511 {
512 // Error while handling regexp: stop using this regexp.
513 if (shl == search_hl)
514 {
515 // don't free regprog in the match list, it's a copy
516 vim_regfree(shl->rm.regprog);
517 set_no_hlsearch(TRUE);
518 }
519 shl->rm.regprog = NULL;
520 shl->lnum = 0;
521 got_int = FALSE; // avoid the "Type :quit to exit Vim" message
522 break;
523 }
524 }
525 else if (cur != NULL)
526 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
527 else
528 nmatched = 0;
529 if (nmatched == 0)
530 {
531 shl->lnum = 0; // no match found
532 break;
533 }
534 if (shl->rm.startpos[0].lnum > 0
535 || shl->rm.startpos[0].col >= mincol
536 || nmatched > 1
537 || shl->rm.endpos[0].col > mincol)
538 {
539 shl->lnum += shl->rm.startpos[0].lnum;
540 break; // useful match found
541 }
542 }
543}
544
545/*
546 * Advance to the match in window "wp" line "lnum" or past it.
547 */
548 void
549prepare_search_hl(win_T *wp, match_T *search_hl, linenr_T lnum)
550{
551 matchitem_T *cur; // points to the match list
552 match_T *shl; // points to search_hl or a match
553 int shl_flag; // flag to indicate whether search_hl
554 // has been processed or not
555 int pos_inprogress; // marks that position match search is
556 // in progress
557 int n;
558
559 // When using a multi-line pattern, start searching at the top
560 // of the window or just after a closed fold.
561 // Do this both for search_hl and the match list.
562 cur = wp->w_match_head;
563 shl_flag = WIN_IS_POPUP(wp); // skip search_hl in a popup window
564 while (cur != NULL || shl_flag == FALSE)
565 {
566 if (shl_flag == FALSE)
567 {
568 shl = search_hl;
569 shl_flag = TRUE;
570 }
571 else
572 shl = &cur->hl;
573 if (shl->rm.regprog != NULL
574 && shl->lnum == 0
575 && re_multiline(shl->rm.regprog))
576 {
577 if (shl->first_lnum == 0)
578 {
579# ifdef FEAT_FOLDING
580 for (shl->first_lnum = lnum;
581 shl->first_lnum > wp->w_topline; --shl->first_lnum)
582 if (hasFoldingWin(wp, shl->first_lnum - 1,
583 NULL, NULL, TRUE, NULL))
584 break;
585# else
586 shl->first_lnum = wp->w_topline;
587# endif
588 }
589 if (cur != NULL)
590 cur->pos.cur = 0;
591 pos_inprogress = TRUE;
592 n = 0;
593 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
594 || (cur != NULL && pos_inprogress)))
595 {
596 next_search_hl(wp, search_hl, shl, shl->first_lnum, (colnr_T)n,
597 shl == search_hl ? NULL : cur);
598 pos_inprogress = cur == NULL || cur->pos.cur == 0
599 ? FALSE : TRUE;
600 if (shl->lnum != 0)
601 {
602 shl->first_lnum = shl->lnum
603 + shl->rm.endpos[0].lnum
604 - shl->rm.startpos[0].lnum;
605 n = shl->rm.endpos[0].col;
606 }
607 else
608 {
609 ++shl->first_lnum;
610 n = 0;
611 }
612 }
613 }
614 if (shl != search_hl && cur != NULL)
615 cur = cur->next;
616 }
617}
618
619/*
620 * Prepare for 'hlsearch' and match highlighting in one window line.
621 * Return TRUE if there is such highlighting and set "search_attr" to the
622 * current highlight attribute.
623 */
624 int
625prepare_search_hl_line(
626 win_T *wp,
627 linenr_T lnum,
628 colnr_T mincol,
629 char_u **line,
630 match_T *search_hl,
631 int *search_attr)
632{
633 matchitem_T *cur; // points to the match list
634 match_T *shl; // points to search_hl or a match
635 int shl_flag; // flag to indicate whether search_hl
636 // has been processed or not
637 int area_highlighting = FALSE;
638
639 // Handle highlighting the last used search pattern and matches.
640 // Do this for both search_hl and the match list.
641 // Do not use search_hl in a popup window.
642 cur = wp->w_match_head;
643 shl_flag = WIN_IS_POPUP(wp);
644 while (cur != NULL || shl_flag == FALSE)
645 {
646 if (shl_flag == FALSE)
647 {
648 shl = search_hl;
649 shl_flag = TRUE;
650 }
651 else
652 shl = &cur->hl;
653 shl->startcol = MAXCOL;
654 shl->endcol = MAXCOL;
LemonBoya4399382022-04-09 21:04:08 +0100655 shl->lines = 0;
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200656 shl->attr_cur = 0;
657 shl->is_addpos = FALSE;
658 if (cur != NULL)
659 cur->pos.cur = 0;
660 next_search_hl(wp, search_hl, shl, lnum, mincol,
661 shl == search_hl ? NULL : cur);
662
663 // Need to get the line again, a multi-line regexp may have made it
664 // invalid.
665 *line = ml_get_buf(wp->w_buffer, lnum, FALSE);
666
667 if (shl->lnum != 0 && shl->lnum <= lnum)
668 {
669 if (shl->lnum == lnum)
670 shl->startcol = shl->rm.startpos[0].col;
671 else
672 shl->startcol = 0;
673 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
674 - shl->rm.startpos[0].lnum)
675 shl->endcol = shl->rm.endpos[0].col;
676 else
677 shl->endcol = MAXCOL;
LemonBoya4399382022-04-09 21:04:08 +0100678 if (shl->rm.endpos[0].lnum != shl->rm.startpos[0].lnum)
679 shl->lines = shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
680 else
681 shl->lines = 1;
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200682 // Highlight one character for an empty match.
683 if (shl->startcol == shl->endcol)
684 {
685 if (has_mbyte && (*line)[shl->endcol] != NUL)
686 shl->endcol += (*mb_ptr2len)((*line) + shl->endcol);
687 else
688 ++shl->endcol;
689 }
690 if ((long)shl->startcol < mincol) // match at leftcol
691 {
692 shl->attr_cur = shl->attr;
693 *search_attr = shl->attr;
694 }
695 area_highlighting = TRUE;
696 }
697 if (shl != search_hl && cur != NULL)
698 cur = cur->next;
699 }
700 return area_highlighting;
701}
702
703/*
704 * For a position in a line: Check for start/end of 'hlsearch' and other
705 * matches.
706 * After end, check for start/end of next match.
707 * When another match, have to check for start again.
708 * Watch out for matching an empty string!
Bram Moolenaar0c359af2021-11-29 19:18:57 +0000709 * "on_last_col" is set to TRUE with non-zero search_attr and the next column
710 * is endcol.
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200711 * Return the updated search_attr.
712 */
713 int
714update_search_hl(
715 win_T *wp,
716 linenr_T lnum,
717 colnr_T col,
718 char_u **line,
719 match_T *search_hl,
720 int *has_match_conc UNUSED,
721 int *match_conc UNUSED,
722 int did_line_attr,
Bram Moolenaar0c359af2021-11-29 19:18:57 +0000723 int lcs_eol_one,
724 int *on_last_col)
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200725{
726 matchitem_T *cur; // points to the match list
727 match_T *shl; // points to search_hl or a match
728 int shl_flag; // flag to indicate whether search_hl
729 // has been processed or not
730 int pos_inprogress; // marks that position match search is in
731 // progress
732 int search_attr = 0;
733
734
735 // Do this for 'search_hl' and the match list (ordered by priority).
736 cur = wp->w_match_head;
737 shl_flag = WIN_IS_POPUP(wp);
738 while (cur != NULL || shl_flag == FALSE)
739 {
740 if (shl_flag == FALSE
741 && (cur == NULL
742 || cur->priority > SEARCH_HL_PRIORITY))
743 {
744 shl = search_hl;
745 shl_flag = TRUE;
746 }
747 else
748 shl = &cur->hl;
749 if (cur != NULL)
750 cur->pos.cur = 0;
751 pos_inprogress = TRUE;
752 while (shl->rm.regprog != NULL || (cur != NULL && pos_inprogress))
753 {
754 if (shl->startcol != MAXCOL
755 && col >= shl->startcol
756 && col < shl->endcol)
757 {
758 int next_col = col + mb_ptr2len(*line + col);
759
760 if (shl->endcol < next_col)
761 shl->endcol = next_col;
762 shl->attr_cur = shl->attr;
763# ifdef FEAT_CONCEAL
764 // Match with the "Conceal" group results in hiding
765 // the match.
766 if (cur != NULL
767 && shl != search_hl
768 && syn_name2id((char_u *)"Conceal") == cur->hlg_id)
769 {
770 *has_match_conc = col == shl->startcol ? 2 : 1;
771 *match_conc = cur->conceal_char;
772 }
773 else
774 *has_match_conc = 0;
775# endif
LemonBoya4399382022-04-09 21:04:08 +0100776 // Highlight the match were the cursor is using the CurSearch
777 // group.
778 if (shl == search_hl
779 && wp->w_cursor.lnum >= shl->lnum
780 && wp->w_cursor.lnum < shl->lnum + shl->lines
781 && wp->w_cursor.col >= shl->startcol
782 && wp->w_cursor.col < shl->endcol)
783 {
784 shl->attr_cur = HL_ATTR(HLF_LC);
785 }
786
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200787 }
788 else if (col == shl->endcol)
789 {
790 shl->attr_cur = 0;
791 next_search_hl(wp, search_hl, shl, lnum, col,
792 shl == search_hl ? NULL : cur);
793 pos_inprogress = !(cur == NULL || cur->pos.cur == 0);
794
795 // Need to get the line again, a multi-line regexp may have
796 // made it invalid.
797 *line = ml_get_buf(wp->w_buffer, lnum, FALSE);
798
799 if (shl->lnum == lnum)
800 {
801 shl->startcol = shl->rm.startpos[0].col;
802 if (shl->rm.endpos[0].lnum == 0)
803 shl->endcol = shl->rm.endpos[0].col;
804 else
805 shl->endcol = MAXCOL;
806
807 if (shl->startcol == shl->endcol)
808 {
809 // highlight empty match, try again after
810 // it
811 if (has_mbyte)
Bram Moolenaar41f08952021-02-22 22:13:49 +0100812 {
813 char_u *p = *line + shl->endcol;
814
815 if (*p == NUL)
816 // consistent with non-mbyte
817 ++shl->endcol;
818 else
819 shl->endcol += (*mb_ptr2len)(p);
820 }
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200821 else
822 ++shl->endcol;
823 }
824
825 // Loop to check if the match starts at the
826 // current position
827 continue;
828 }
829 }
830 break;
831 }
832 if (shl != search_hl && cur != NULL)
833 cur = cur->next;
834 }
835
836 // Use attributes from match with highest priority among 'search_hl' and
837 // the match list.
838 cur = wp->w_match_head;
839 shl_flag = WIN_IS_POPUP(wp);
840 while (cur != NULL || shl_flag == FALSE)
841 {
842 if (shl_flag == FALSE
843 && (cur == NULL ||
844 cur->priority > SEARCH_HL_PRIORITY))
845 {
846 shl = search_hl;
847 shl_flag = TRUE;
848 }
849 else
850 shl = &cur->hl;
851 if (shl->attr_cur != 0)
Bram Moolenaar0c359af2021-11-29 19:18:57 +0000852 {
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200853 search_attr = shl->attr_cur;
Bram Moolenaar0c359af2021-11-29 19:18:57 +0000854 *on_last_col = col + 1 >= shl->endcol;
855 }
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200856 if (shl != search_hl && cur != NULL)
857 cur = cur->next;
858 }
859 // Only highlight one character after the last column.
860 if (*(*line + col) == NUL && (did_line_attr >= 1
861 || (wp->w_p_list && lcs_eol_one == -1)))
862 search_attr = 0;
863 return search_attr;
864}
865
866 int
867get_prevcol_hl_flag(win_T *wp, match_T *search_hl, long curcol)
868{
869 long prevcol = curcol;
870 int prevcol_hl_flag = FALSE;
871 matchitem_T *cur; // points to the match list
872
Bram Moolenaar41f08952021-02-22 22:13:49 +0100873#if defined(FEAT_PROP_POPUP)
874 // don't do this in a popup window
875 if (popup_is_popup(wp))
876 return FALSE;
877#endif
878
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200879 // we're not really at that column when skipping some text
880 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
881 ++prevcol;
882
Bram Moolenaar41f08952021-02-22 22:13:49 +0100883 // Highlight a character after the end of the line if the match started
884 // at the end of the line or when the match continues in the next line
885 // (match includes the line break).
886 if (!search_hl->is_addpos && (prevcol == (long)search_hl->startcol
887 || (prevcol > (long)search_hl->startcol
888 && search_hl->endcol == MAXCOL)))
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200889 prevcol_hl_flag = TRUE;
890 else
891 {
892 cur = wp->w_match_head;
893 while (cur != NULL)
894 {
Bram Moolenaar41f08952021-02-22 22:13:49 +0100895 if (!cur->hl.is_addpos && (prevcol == (long)cur->hl.startcol
896 || (prevcol > (long)cur->hl.startcol
897 && cur->hl.endcol == MAXCOL)))
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200898 {
899 prevcol_hl_flag = TRUE;
900 break;
901 }
902 cur = cur->next;
903 }
904 }
905 return prevcol_hl_flag;
906}
907
908/*
909 * Get highlighting for the char after the text in "char_attr" from 'hlsearch'
910 * or match highlighting.
911 */
912 void
913get_search_match_hl(win_T *wp, match_T *search_hl, long col, int *char_attr)
914{
915 matchitem_T *cur; // points to the match list
916 match_T *shl; // points to search_hl or a match
917 int shl_flag; // flag to indicate whether search_hl
918 // has been processed or not
919
920 cur = wp->w_match_head;
921 shl_flag = WIN_IS_POPUP(wp);
922 while (cur != NULL || shl_flag == FALSE)
923 {
924 if (shl_flag == FALSE
925 && ((cur != NULL
926 && cur->priority > SEARCH_HL_PRIORITY)
927 || cur == NULL))
928 {
929 shl = search_hl;
930 shl_flag = TRUE;
931 }
932 else
933 shl = &cur->hl;
934 if (col - 1 == (long)shl->startcol
935 && (shl == search_hl || !shl->is_addpos))
936 *char_attr = shl->attr;
937 if (shl != search_hl && cur != NULL)
938 cur = cur->next;
939 }
940}
941
942#endif // FEAT_SEARCH_EXTRA
943
944#if defined(FEAT_EVAL) || defined(PROTO)
945# ifdef FEAT_SEARCH_EXTRA
946 static int
947matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
948{
949 dictitem_T *di;
950
951 if (tv->v_type != VAR_DICT)
952 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000953 emsg(_(e_dictionary_required));
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200954 return FAIL;
955 }
956
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100957 if (dict_has_key(tv->vval.v_dict, "conceal"))
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200958 *conceal_char = dict_get_string(tv->vval.v_dict,
959 (char_u *)"conceal", FALSE);
960
961 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
962 {
963 *win = find_win_by_nr_or_id(&di->di_tv);
964 if (*win == NULL)
965 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +0000966 emsg(_(e_invalid_window_number));
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200967 return FAIL;
968 }
969 }
970
971 return OK;
972}
973#endif
974
975/*
976 * "clearmatches()" function
977 */
978 void
979f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
980{
981#ifdef FEAT_SEARCH_EXTRA
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200982 win_T *win;
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200983
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200984 if (in_vim9script() && check_for_opt_number_arg(argvars, 0) == FAIL)
985 return;
986
987 win = get_optional_window(argvars, 0);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200988 if (win != NULL)
989 clear_matches(win);
990#endif
991}
992
993/*
994 * "getmatches()" function
995 */
996 void
997f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
998{
999# ifdef FEAT_SEARCH_EXTRA
1000 dict_T *dict;
1001 matchitem_T *cur;
1002 int i;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001003 win_T *win;
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001004
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001005 if (in_vim9script() && check_for_opt_number_arg(argvars, 0) == FAIL)
1006 return;
1007
1008 win = get_optional_window(argvars, 0);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001009 if (rettv_list_alloc(rettv) == FAIL || win == NULL)
1010 return;
1011
1012 cur = win->w_match_head;
1013 while (cur != NULL)
1014 {
1015 dict = dict_alloc();
1016 if (dict == NULL)
1017 return;
1018 if (cur->match.regprog == NULL)
1019 {
1020 // match added with matchaddpos()
1021 for (i = 0; i < MAXPOSMATCH; ++i)
1022 {
1023 llpos_T *llpos;
1024 char buf[30]; // use 30 to avoid compiler warning
1025 list_T *l;
1026
1027 llpos = &cur->pos.pos[i];
1028 if (llpos->lnum == 0)
1029 break;
1030 l = list_alloc();
1031 if (l == NULL)
1032 break;
1033 list_append_number(l, (varnumber_T)llpos->lnum);
1034 if (llpos->col > 0)
1035 {
1036 list_append_number(l, (varnumber_T)llpos->col);
1037 list_append_number(l, (varnumber_T)llpos->len);
1038 }
1039 sprintf(buf, "pos%d", i + 1);
1040 dict_add_list(dict, buf, l);
1041 }
1042 }
1043 else
1044 {
1045 dict_add_string(dict, "pattern", cur->pattern);
1046 }
1047 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
1048 dict_add_number(dict, "priority", (long)cur->priority);
1049 dict_add_number(dict, "id", (long)cur->id);
1050# if defined(FEAT_CONCEAL)
1051 if (cur->conceal_char)
1052 {
1053 char_u buf[MB_MAXBYTES + 1];
1054
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00001055 buf[(*mb_char2bytes)(cur->conceal_char, buf)] = NUL;
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001056 dict_add_string(dict, "conceal", (char_u *)&buf);
1057 }
1058# endif
1059 list_append_dict(rettv->vval.v_list, dict);
1060 cur = cur->next;
1061 }
1062# endif
1063}
1064
1065/*
1066 * "setmatches()" function
1067 */
1068 void
1069f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1070{
1071#ifdef FEAT_SEARCH_EXTRA
1072 list_T *l;
1073 listitem_T *li;
1074 dict_T *d;
1075 list_T *s = NULL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001076 win_T *win;
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001077
1078 rettv->vval.v_number = -1;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001079
1080 if (in_vim9script()
1081 && (check_for_list_arg(argvars, 0) == FAIL
1082 || check_for_opt_number_arg(argvars, 1) == FAIL))
1083 return;
1084
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001085 if (argvars[0].v_type != VAR_LIST)
1086 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001087 emsg(_(e_list_required));
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001088 return;
1089 }
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001090 win = get_optional_window(argvars, 1);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001091 if (win == NULL)
1092 return;
1093
1094 if ((l = argvars[0].vval.v_list) != NULL)
1095 {
1096 // To some extent make sure that we are dealing with a list from
1097 // "getmatches()".
1098 li = l->lv_first;
1099 while (li != NULL)
1100 {
1101 if (li->li_tv.v_type != VAR_DICT
1102 || (d = li->li_tv.vval.v_dict) == NULL)
1103 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001104 emsg(_(e_invalid_argument));
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001105 return;
1106 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001107 if (!(dict_has_key(d, "group")
1108 && (dict_has_key(d, "pattern")
1109 || dict_has_key(d, "pos1"))
1110 && dict_has_key(d, "priority")
1111 && dict_has_key(d, "id")))
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001112 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001113 emsg(_(e_invalid_argument));
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001114 return;
1115 }
1116 li = li->li_next;
1117 }
1118
1119 clear_matches(win);
1120 li = l->lv_first;
1121 while (li != NULL)
1122 {
1123 int i = 0;
1124 char buf[30]; // use 30 to avoid compiler warning
1125 dictitem_T *di;
1126 char_u *group;
1127 int priority;
1128 int id;
1129 char_u *conceal;
1130
1131 d = li->li_tv.vval.v_dict;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001132 if (!dict_has_key(d, "pattern"))
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001133 {
1134 if (s == NULL)
1135 {
1136 s = list_alloc();
1137 if (s == NULL)
1138 return;
1139 }
1140
1141 // match from matchaddpos()
1142 for (i = 1; i < 9; i++)
1143 {
1144 sprintf((char *)buf, (char *)"pos%d", i);
1145 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
1146 {
1147 if (di->di_tv.v_type != VAR_LIST)
1148 return;
1149
1150 list_append_tv(s, &di->di_tv);
1151 s->lv_refcount++;
1152 }
1153 else
1154 break;
1155 }
1156 }
1157
1158 group = dict_get_string(d, (char_u *)"group", TRUE);
1159 priority = (int)dict_get_number(d, (char_u *)"priority");
1160 id = (int)dict_get_number(d, (char_u *)"id");
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001161 conceal = dict_has_key(d, "conceal")
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001162 ? dict_get_string(d, (char_u *)"conceal", TRUE)
1163 : NULL;
1164 if (i == 0)
1165 {
1166 match_add(win, group,
1167 dict_get_string(d, (char_u *)"pattern", FALSE),
1168 priority, id, NULL, conceal);
1169 }
1170 else
1171 {
1172 match_add(win, group, NULL, priority, id, s, conceal);
1173 list_unref(s);
1174 s = NULL;
1175 }
1176 vim_free(group);
1177 vim_free(conceal);
1178
1179 li = li->li_next;
1180 }
1181 rettv->vval.v_number = 0;
1182 }
1183#endif
1184}
1185
1186/*
1187 * "matchadd()" function
1188 */
1189 void
1190f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1191{
1192# ifdef FEAT_SEARCH_EXTRA
1193 char_u buf[NUMBUFLEN];
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02001194 char_u *grp; // group
1195 char_u *pat; // pattern
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001196 int prio = 10; // default priority
1197 int id = -1;
1198 int error = FALSE;
1199 char_u *conceal_char = NULL;
1200 win_T *win = curwin;
1201
1202 rettv->vval.v_number = -1;
1203
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02001204 if (in_vim9script()
1205 && (check_for_string_arg(argvars, 0) == FAIL
1206 || check_for_string_arg(argvars, 1) == FAIL
1207 || check_for_opt_number_arg(argvars, 2) == FAIL
1208 || (argvars[2].v_type != VAR_UNKNOWN
1209 && (check_for_opt_number_arg(argvars, 3) == FAIL
1210 || (argvars[3].v_type != VAR_UNKNOWN
1211 && check_for_opt_dict_arg(argvars, 4) == FAIL)))))
1212 return;
1213
1214 grp = tv_get_string_buf_chk(&argvars[0], buf); // group
1215 pat = tv_get_string_buf_chk(&argvars[1], buf); // pattern
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001216 if (grp == NULL || pat == NULL)
1217 return;
1218 if (argvars[2].v_type != VAR_UNKNOWN)
1219 {
1220 prio = (int)tv_get_number_chk(&argvars[2], &error);
1221 if (argvars[3].v_type != VAR_UNKNOWN)
1222 {
1223 id = (int)tv_get_number_chk(&argvars[3], &error);
1224 if (argvars[4].v_type != VAR_UNKNOWN
1225 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
1226 return;
1227 }
1228 }
1229 if (error == TRUE)
1230 return;
1231 if (id >= 1 && id <= 3)
1232 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00001233 semsg(_(e_id_is_reserved_for_match_nr), id);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001234 return;
1235 }
1236
1237 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
1238 conceal_char);
1239# endif
1240}
1241
1242/*
1243 * "matchaddpos()" function
1244 */
1245 void
1246f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1247{
1248# ifdef FEAT_SEARCH_EXTRA
1249 char_u buf[NUMBUFLEN];
1250 char_u *group;
1251 int prio = 10;
1252 int id = -1;
1253 int error = FALSE;
1254 list_T *l;
1255 char_u *conceal_char = NULL;
1256 win_T *win = curwin;
1257
1258 rettv->vval.v_number = -1;
1259
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02001260 if (in_vim9script()
1261 && (check_for_string_arg(argvars, 0) == FAIL
1262 || check_for_list_arg(argvars, 1) == FAIL
1263 || check_for_opt_number_arg(argvars, 2) == FAIL
1264 || (argvars[2].v_type != VAR_UNKNOWN
1265 && (check_for_opt_number_arg(argvars, 3) == FAIL
1266 || (argvars[3].v_type != VAR_UNKNOWN
1267 && check_for_opt_dict_arg(argvars, 4) == FAIL)))))
1268 return;
1269
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001270 group = tv_get_string_buf_chk(&argvars[0], buf);
1271 if (group == NULL)
1272 return;
1273
1274 if (argvars[1].v_type != VAR_LIST)
1275 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001276 semsg(_(e_argument_of_str_must_be_list), "matchaddpos()");
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001277 return;
1278 }
1279 l = argvars[1].vval.v_list;
1280 if (l == NULL)
1281 return;
1282
1283 if (argvars[2].v_type != VAR_UNKNOWN)
1284 {
1285 prio = (int)tv_get_number_chk(&argvars[2], &error);
1286 if (argvars[3].v_type != VAR_UNKNOWN)
1287 {
1288 id = (int)tv_get_number_chk(&argvars[3], &error);
1289
1290 if (argvars[4].v_type != VAR_UNKNOWN
1291 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
1292 return;
1293 }
1294 }
1295 if (error == TRUE)
1296 return;
1297
1298 // id == 3 is ok because matchaddpos() is supposed to substitute :3match
1299 if (id == 1 || id == 2)
1300 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00001301 semsg(_(e_id_is_reserved_for_match_nr), id);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001302 return;
1303 }
1304
1305 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
1306 conceal_char);
1307# endif
1308}
1309
1310/*
1311 * "matcharg()" function
1312 */
1313 void
1314f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
1315{
1316 if (rettv_list_alloc(rettv) == OK)
1317 {
1318# ifdef FEAT_SEARCH_EXTRA
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001319 int id;
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001320 matchitem_T *m;
1321
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001322 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
1323 return;
1324
1325 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001326 if (id >= 1 && id <= 3)
1327 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00001328 if ((m = get_match(curwin, id)) != NULL)
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001329 {
1330 list_append_string(rettv->vval.v_list,
1331 syn_id2name(m->hlg_id), -1);
1332 list_append_string(rettv->vval.v_list, m->pattern, -1);
1333 }
1334 else
1335 {
1336 list_append_string(rettv->vval.v_list, NULL, -1);
1337 list_append_string(rettv->vval.v_list, NULL, -1);
1338 }
1339 }
1340# endif
1341 }
1342}
1343
1344/*
1345 * "matchdelete()" function
1346 */
1347 void
1348f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1349{
1350# ifdef FEAT_SEARCH_EXTRA
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001351 win_T *win;
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001352
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001353 if (in_vim9script()
1354 && (check_for_number_arg(argvars, 0) == FAIL
1355 || check_for_opt_number_arg(argvars, 1) == FAIL))
1356 return;
1357
1358 win = get_optional_window(argvars, 1);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001359 if (win == NULL)
1360 rettv->vval.v_number = -1;
1361 else
1362 rettv->vval.v_number = match_delete(win,
1363 (int)tv_get_number(&argvars[0]), TRUE);
1364# endif
1365}
1366#endif
1367
1368#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
1369/*
1370 * ":[N]match {group} {pattern}"
1371 * Sets nextcmd to the start of the next command, if any. Also called when
1372 * skipping commands to find the next command.
1373 */
1374 void
1375ex_match(exarg_T *eap)
1376{
1377 char_u *p;
1378 char_u *g = NULL;
1379 char_u *end;
1380 int c;
1381 int id;
1382
1383 if (eap->line2 <= 3)
1384 id = eap->line2;
1385 else
1386 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001387 emsg(_(e_invalid_command));
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001388 return;
1389 }
1390
1391 // First clear any old pattern.
1392 if (!eap->skip)
1393 match_delete(curwin, id, FALSE);
1394
1395 if (ends_excmd2(eap->cmd, eap->arg))
1396 end = eap->arg;
1397 else if ((STRNICMP(eap->arg, "none", 4) == 0
1398 && (VIM_ISWHITE(eap->arg[4])
1399 || ends_excmd2(eap->arg, eap->arg + 4))))
1400 end = eap->arg + 4;
1401 else
1402 {
1403 p = skiptowhite(eap->arg);
1404 if (!eap->skip)
1405 g = vim_strnsave(eap->arg, p - eap->arg);
1406 p = skipwhite(p);
1407 if (*p == NUL)
1408 {
1409 // There must be two arguments.
1410 vim_free(g);
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001411 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001412 return;
1413 }
1414 end = skip_regexp(p + 1, *p, TRUE);
1415 if (!eap->skip)
1416 {
1417 if (*end != NUL && !ends_excmd2(end, skipwhite(end + 1)))
1418 {
1419 vim_free(g);
Bram Moolenaar74409f62022-01-01 15:58:22 +00001420 eap->errmsg = ex_errmsg(e_trailing_characters_str, end);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001421 return;
1422 }
1423 if (*end != *p)
1424 {
1425 vim_free(g);
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001426 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001427 return;
1428 }
1429
1430 c = *end;
1431 *end = NUL;
1432 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
1433 vim_free(g);
1434 *end = c;
1435 }
1436 }
1437 eap->nextcmd = find_nextcmd(end);
1438}
1439#endif