blob: 1078424af27d16e30986820463c89624fbb391e4 [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;
Bram Moolenaar693ccd12022-04-16 12:04:37 +0100399 shl->has_cursor = FALSE;
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200400 posmatch->cur = found + 1;
401 return 1;
402 }
403 return 0;
404}
405
406/*
407 * Search for a next 'hlsearch' or match.
408 * Uses shl->buf.
409 * Sets shl->lnum and shl->rm contents.
410 * Note: Assumes a previous match is always before "lnum", unless
411 * shl->lnum is zero.
412 * Careful: Any pointers for buffer lines will become invalid.
413 */
414 static void
415next_search_hl(
416 win_T *win,
417 match_T *search_hl,
418 match_T *shl, // points to search_hl or a match
419 linenr_T lnum,
420 colnr_T mincol, // minimal column for a match
421 matchitem_T *cur) // to retrieve match positions if any
422{
423 linenr_T l;
424 colnr_T matchcol;
425 long nmatched;
426 int called_emsg_before = called_emsg;
427
428 // for :{range}s/pat only highlight inside the range
Bram Moolenaar94fb8272021-12-29 19:22:44 +0000429 if ((lnum < search_first_line || lnum > search_last_line) && cur == NULL)
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200430 {
431 shl->lnum = 0;
432 return;
433 }
434
435 if (shl->lnum != 0)
436 {
437 // Check for three situations:
438 // 1. If the "lnum" is below a previous match, start a new search.
439 // 2. If the previous match includes "mincol", use it.
440 // 3. Continue after the previous match.
441 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
442 if (lnum > l)
443 shl->lnum = 0;
444 else if (lnum < l || shl->rm.endpos[0].col > mincol)
445 return;
446 }
447
448 // Repeat searching for a match until one is found that includes "mincol"
449 // or none is found in this line.
450 for (;;)
451 {
452# ifdef FEAT_RELTIME
453 // Stop searching after passing the time limit.
454 if (profile_passed_limit(&(shl->tm)))
455 {
456 shl->lnum = 0; // no match found in time
457 break;
458 }
459# endif
460 // Three situations:
461 // 1. No useful previous match: search from start of line.
462 // 2. Not Vi compatible or empty match: continue at next character.
463 // Break the loop if this is beyond the end of the line.
464 // 3. Vi compatible searching: continue at end of previous match.
465 if (shl->lnum == 0)
466 matchcol = 0;
467 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
468 || (shl->rm.endpos[0].lnum == 0
469 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
470 {
471 char_u *ml;
472
473 matchcol = shl->rm.startpos[0].col;
474 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
475 if (*ml == NUL)
476 {
477 ++matchcol;
478 shl->lnum = 0;
479 break;
480 }
481 if (has_mbyte)
482 matchcol += mb_ptr2len(ml);
483 else
484 ++matchcol;
485 }
486 else
487 matchcol = shl->rm.endpos[0].col;
488
489 shl->lnum = lnum;
490 if (shl->rm.regprog != NULL)
491 {
492 // Remember whether shl->rm is using a copy of the regprog in
493 // cur->match.
494 int regprog_is_copy = (shl != search_hl && cur != NULL
495 && shl == &cur->hl
496 && cur->match.regprog == cur->hl.rm.regprog);
497 int timed_out = FALSE;
498
499 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
500 matchcol,
501#ifdef FEAT_RELTIME
502 &(shl->tm), &timed_out
503#else
504 NULL, NULL
505#endif
506 );
507 // Copy the regprog, in case it got freed and recompiled.
508 if (regprog_is_copy)
509 cur->match.regprog = cur->hl.rm.regprog;
510
511 if (called_emsg > called_emsg_before || got_int || timed_out)
512 {
513 // Error while handling regexp: stop using this regexp.
514 if (shl == search_hl)
515 {
516 // don't free regprog in the match list, it's a copy
517 vim_regfree(shl->rm.regprog);
518 set_no_hlsearch(TRUE);
519 }
520 shl->rm.regprog = NULL;
521 shl->lnum = 0;
522 got_int = FALSE; // avoid the "Type :quit to exit Vim" message
523 break;
524 }
525 }
526 else if (cur != NULL)
527 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
528 else
529 nmatched = 0;
530 if (nmatched == 0)
531 {
532 shl->lnum = 0; // no match found
533 break;
534 }
535 if (shl->rm.startpos[0].lnum > 0
536 || shl->rm.startpos[0].col >= mincol
537 || nmatched > 1
538 || shl->rm.endpos[0].col > mincol)
539 {
540 shl->lnum += shl->rm.startpos[0].lnum;
541 break; // useful match found
542 }
543 }
544}
545
546/*
547 * Advance to the match in window "wp" line "lnum" or past it.
548 */
549 void
550prepare_search_hl(win_T *wp, match_T *search_hl, linenr_T lnum)
551{
552 matchitem_T *cur; // points to the match list
553 match_T *shl; // points to search_hl or a match
554 int shl_flag; // flag to indicate whether search_hl
555 // has been processed or not
556 int pos_inprogress; // marks that position match search is
557 // in progress
558 int n;
559
560 // When using a multi-line pattern, start searching at the top
561 // of the window or just after a closed fold.
562 // Do this both for search_hl and the match list.
563 cur = wp->w_match_head;
564 shl_flag = WIN_IS_POPUP(wp); // skip search_hl in a popup window
565 while (cur != NULL || shl_flag == FALSE)
566 {
567 if (shl_flag == FALSE)
568 {
569 shl = search_hl;
570 shl_flag = TRUE;
571 }
572 else
573 shl = &cur->hl;
574 if (shl->rm.regprog != NULL
575 && shl->lnum == 0
576 && re_multiline(shl->rm.regprog))
577 {
578 if (shl->first_lnum == 0)
579 {
580# ifdef FEAT_FOLDING
581 for (shl->first_lnum = lnum;
582 shl->first_lnum > wp->w_topline; --shl->first_lnum)
583 if (hasFoldingWin(wp, shl->first_lnum - 1,
584 NULL, NULL, TRUE, NULL))
585 break;
586# else
587 shl->first_lnum = wp->w_topline;
588# endif
589 }
590 if (cur != NULL)
591 cur->pos.cur = 0;
592 pos_inprogress = TRUE;
593 n = 0;
594 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
595 || (cur != NULL && pos_inprogress)))
596 {
597 next_search_hl(wp, search_hl, shl, shl->first_lnum, (colnr_T)n,
598 shl == search_hl ? NULL : cur);
599 pos_inprogress = cur == NULL || cur->pos.cur == 0
600 ? FALSE : TRUE;
601 if (shl->lnum != 0)
602 {
603 shl->first_lnum = shl->lnum
604 + shl->rm.endpos[0].lnum
605 - shl->rm.startpos[0].lnum;
606 n = shl->rm.endpos[0].col;
607 }
608 else
609 {
610 ++shl->first_lnum;
611 n = 0;
612 }
613 }
614 }
615 if (shl != search_hl && cur != NULL)
616 cur = cur->next;
617 }
618}
619
620/*
Bram Moolenaar9b367502022-04-22 20:07:21 +0100621 * Update "shl->has_cursor" based on the match in "shl" and the cursor
622 * position.
623 */
624 static void
625check_cur_search_hl(win_T *wp, match_T *shl)
626{
zeertzjq8279cfe2022-04-23 12:05:51 +0100627 linenr_T linecount = shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
Bram Moolenaar9b367502022-04-22 20:07:21 +0100628
629 if (wp->w_cursor.lnum >= shl->lnum
zeertzjq8279cfe2022-04-23 12:05:51 +0100630 && wp->w_cursor.lnum <= shl->lnum + linecount
Bram Moolenaar9b367502022-04-22 20:07:21 +0100631 && (wp->w_cursor.lnum > shl->lnum
632 || wp->w_cursor.col >= shl->rm.startpos[0].col)
633 && (wp->w_cursor.lnum < shl->lnum + linecount
634 || wp->w_cursor.col < shl->rm.endpos[0].col))
635 shl->has_cursor = TRUE;
636 else
637 shl->has_cursor = FALSE;
638}
639
640/*
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200641 * Prepare for 'hlsearch' and match highlighting in one window line.
642 * Return TRUE if there is such highlighting and set "search_attr" to the
643 * current highlight attribute.
644 */
645 int
646prepare_search_hl_line(
647 win_T *wp,
648 linenr_T lnum,
649 colnr_T mincol,
650 char_u **line,
651 match_T *search_hl,
652 int *search_attr)
653{
654 matchitem_T *cur; // points to the match list
655 match_T *shl; // points to search_hl or a match
656 int shl_flag; // flag to indicate whether search_hl
657 // has been processed or not
658 int area_highlighting = FALSE;
659
660 // Handle highlighting the last used search pattern and matches.
661 // Do this for both search_hl and the match list.
662 // Do not use search_hl in a popup window.
663 cur = wp->w_match_head;
664 shl_flag = WIN_IS_POPUP(wp);
665 while (cur != NULL || shl_flag == FALSE)
666 {
667 if (shl_flag == FALSE)
668 {
669 shl = search_hl;
670 shl_flag = TRUE;
671 }
672 else
673 shl = &cur->hl;
674 shl->startcol = MAXCOL;
675 shl->endcol = MAXCOL;
676 shl->attr_cur = 0;
677 shl->is_addpos = FALSE;
Bram Moolenaar693ccd12022-04-16 12:04:37 +0100678 shl->has_cursor = FALSE;
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200679 if (cur != NULL)
680 cur->pos.cur = 0;
681 next_search_hl(wp, search_hl, shl, lnum, mincol,
682 shl == search_hl ? NULL : cur);
683
684 // Need to get the line again, a multi-line regexp may have made it
685 // invalid.
686 *line = ml_get_buf(wp->w_buffer, lnum, FALSE);
687
688 if (shl->lnum != 0 && shl->lnum <= lnum)
689 {
690 if (shl->lnum == lnum)
691 shl->startcol = shl->rm.startpos[0].col;
692 else
693 shl->startcol = 0;
694 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
695 - shl->rm.startpos[0].lnum)
696 shl->endcol = shl->rm.endpos[0].col;
697 else
698 shl->endcol = MAXCOL;
Bram Moolenaar693ccd12022-04-16 12:04:37 +0100699
700 // check if the cursor is in the match before changing the columns
Bram Moolenaar9b367502022-04-22 20:07:21 +0100701 if (shl == search_hl)
702 check_cur_search_hl(wp, shl);
Bram Moolenaar693ccd12022-04-16 12:04:37 +0100703
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200704 // Highlight one character for an empty match.
705 if (shl->startcol == shl->endcol)
706 {
707 if (has_mbyte && (*line)[shl->endcol] != NUL)
708 shl->endcol += (*mb_ptr2len)((*line) + shl->endcol);
709 else
710 ++shl->endcol;
711 }
712 if ((long)shl->startcol < mincol) // match at leftcol
713 {
714 shl->attr_cur = shl->attr;
715 *search_attr = shl->attr;
716 }
717 area_highlighting = TRUE;
718 }
719 if (shl != search_hl && cur != NULL)
720 cur = cur->next;
721 }
722 return area_highlighting;
723}
724
725/*
726 * For a position in a line: Check for start/end of 'hlsearch' and other
727 * matches.
728 * After end, check for start/end of next match.
729 * When another match, have to check for start again.
730 * Watch out for matching an empty string!
Bram Moolenaar0c359af2021-11-29 19:18:57 +0000731 * "on_last_col" is set to TRUE with non-zero search_attr and the next column
732 * is endcol.
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200733 * Return the updated search_attr.
734 */
735 int
736update_search_hl(
737 win_T *wp,
738 linenr_T lnum,
739 colnr_T col,
740 char_u **line,
741 match_T *search_hl,
742 int *has_match_conc UNUSED,
743 int *match_conc UNUSED,
744 int did_line_attr,
Bram Moolenaar0c359af2021-11-29 19:18:57 +0000745 int lcs_eol_one,
746 int *on_last_col)
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200747{
748 matchitem_T *cur; // points to the match list
749 match_T *shl; // points to search_hl or a match
750 int shl_flag; // flag to indicate whether search_hl
751 // has been processed or not
752 int pos_inprogress; // marks that position match search is in
753 // progress
754 int search_attr = 0;
755
756
757 // Do this for 'search_hl' and the match list (ordered by priority).
758 cur = wp->w_match_head;
759 shl_flag = WIN_IS_POPUP(wp);
760 while (cur != NULL || shl_flag == FALSE)
761 {
762 if (shl_flag == FALSE
763 && (cur == NULL
764 || cur->priority > SEARCH_HL_PRIORITY))
765 {
766 shl = search_hl;
767 shl_flag = TRUE;
768 }
769 else
770 shl = &cur->hl;
771 if (cur != NULL)
772 cur->pos.cur = 0;
773 pos_inprogress = TRUE;
774 while (shl->rm.regprog != NULL || (cur != NULL && pos_inprogress))
775 {
776 if (shl->startcol != MAXCOL
777 && col >= shl->startcol
778 && col < shl->endcol)
779 {
780 int next_col = col + mb_ptr2len(*line + col);
781
782 if (shl->endcol < next_col)
783 shl->endcol = next_col;
784 shl->attr_cur = shl->attr;
785# ifdef FEAT_CONCEAL
786 // Match with the "Conceal" group results in hiding
787 // the match.
788 if (cur != NULL
789 && shl != search_hl
790 && syn_name2id((char_u *)"Conceal") == cur->hlg_id)
791 {
792 *has_match_conc = col == shl->startcol ? 2 : 1;
793 *match_conc = cur->conceal_char;
794 }
795 else
796 *has_match_conc = 0;
797# endif
LemonBoya4399382022-04-09 21:04:08 +0100798 // Highlight the match were the cursor is using the CurSearch
799 // group.
Bram Moolenaar693ccd12022-04-16 12:04:37 +0100800 if (shl == search_hl && shl->has_cursor)
LemonBoya4399382022-04-09 21:04:08 +0100801 shl->attr_cur = HL_ATTR(HLF_LC);
LemonBoya4399382022-04-09 21:04:08 +0100802
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200803 }
804 else if (col == shl->endcol)
805 {
806 shl->attr_cur = 0;
807 next_search_hl(wp, search_hl, shl, lnum, col,
808 shl == search_hl ? NULL : cur);
809 pos_inprogress = !(cur == NULL || cur->pos.cur == 0);
810
811 // Need to get the line again, a multi-line regexp may have
812 // made it invalid.
813 *line = ml_get_buf(wp->w_buffer, lnum, FALSE);
814
815 if (shl->lnum == lnum)
816 {
817 shl->startcol = shl->rm.startpos[0].col;
818 if (shl->rm.endpos[0].lnum == 0)
819 shl->endcol = shl->rm.endpos[0].col;
820 else
821 shl->endcol = MAXCOL;
822
Bram Moolenaar9b367502022-04-22 20:07:21 +0100823 // check if the cursor is in the match
824 if (shl == search_hl)
825 check_cur_search_hl(wp, shl);
826
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200827 if (shl->startcol == shl->endcol)
828 {
829 // highlight empty match, try again after
830 // it
831 if (has_mbyte)
Bram Moolenaar41f08952021-02-22 22:13:49 +0100832 {
833 char_u *p = *line + shl->endcol;
834
835 if (*p == NUL)
836 // consistent with non-mbyte
837 ++shl->endcol;
838 else
839 shl->endcol += (*mb_ptr2len)(p);
840 }
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200841 else
842 ++shl->endcol;
843 }
844
845 // Loop to check if the match starts at the
846 // current position
847 continue;
848 }
849 }
850 break;
851 }
852 if (shl != search_hl && cur != NULL)
853 cur = cur->next;
854 }
855
856 // Use attributes from match with highest priority among 'search_hl' and
857 // the match list.
858 cur = wp->w_match_head;
859 shl_flag = WIN_IS_POPUP(wp);
860 while (cur != NULL || shl_flag == FALSE)
861 {
862 if (shl_flag == FALSE
863 && (cur == NULL ||
864 cur->priority > SEARCH_HL_PRIORITY))
865 {
866 shl = search_hl;
867 shl_flag = TRUE;
868 }
869 else
870 shl = &cur->hl;
871 if (shl->attr_cur != 0)
Bram Moolenaar0c359af2021-11-29 19:18:57 +0000872 {
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200873 search_attr = shl->attr_cur;
Bram Moolenaar0c359af2021-11-29 19:18:57 +0000874 *on_last_col = col + 1 >= shl->endcol;
875 }
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200876 if (shl != search_hl && cur != NULL)
877 cur = cur->next;
878 }
879 // Only highlight one character after the last column.
880 if (*(*line + col) == NUL && (did_line_attr >= 1
881 || (wp->w_p_list && lcs_eol_one == -1)))
882 search_attr = 0;
883 return search_attr;
884}
885
886 int
887get_prevcol_hl_flag(win_T *wp, match_T *search_hl, long curcol)
888{
889 long prevcol = curcol;
890 int prevcol_hl_flag = FALSE;
891 matchitem_T *cur; // points to the match list
892
Bram Moolenaar41f08952021-02-22 22:13:49 +0100893#if defined(FEAT_PROP_POPUP)
894 // don't do this in a popup window
895 if (popup_is_popup(wp))
896 return FALSE;
897#endif
898
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200899 // we're not really at that column when skipping some text
900 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
901 ++prevcol;
902
Bram Moolenaar41f08952021-02-22 22:13:49 +0100903 // Highlight a character after the end of the line if the match started
904 // at the end of the line or when the match continues in the next line
905 // (match includes the line break).
906 if (!search_hl->is_addpos && (prevcol == (long)search_hl->startcol
907 || (prevcol > (long)search_hl->startcol
908 && search_hl->endcol == MAXCOL)))
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200909 prevcol_hl_flag = TRUE;
910 else
911 {
912 cur = wp->w_match_head;
913 while (cur != NULL)
914 {
Bram Moolenaar41f08952021-02-22 22:13:49 +0100915 if (!cur->hl.is_addpos && (prevcol == (long)cur->hl.startcol
916 || (prevcol > (long)cur->hl.startcol
917 && cur->hl.endcol == MAXCOL)))
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200918 {
919 prevcol_hl_flag = TRUE;
920 break;
921 }
922 cur = cur->next;
923 }
924 }
925 return prevcol_hl_flag;
926}
927
928/*
929 * Get highlighting for the char after the text in "char_attr" from 'hlsearch'
930 * or match highlighting.
931 */
932 void
933get_search_match_hl(win_T *wp, match_T *search_hl, long col, int *char_attr)
934{
935 matchitem_T *cur; // points to the match list
936 match_T *shl; // points to search_hl or a match
937 int shl_flag; // flag to indicate whether search_hl
938 // has been processed or not
939
940 cur = wp->w_match_head;
941 shl_flag = WIN_IS_POPUP(wp);
942 while (cur != NULL || shl_flag == FALSE)
943 {
944 if (shl_flag == FALSE
945 && ((cur != NULL
946 && cur->priority > SEARCH_HL_PRIORITY)
947 || cur == NULL))
948 {
949 shl = search_hl;
950 shl_flag = TRUE;
951 }
952 else
953 shl = &cur->hl;
954 if (col - 1 == (long)shl->startcol
955 && (shl == search_hl || !shl->is_addpos))
956 *char_attr = shl->attr;
957 if (shl != search_hl && cur != NULL)
958 cur = cur->next;
959 }
960}
961
962#endif // FEAT_SEARCH_EXTRA
963
964#if defined(FEAT_EVAL) || defined(PROTO)
965# ifdef FEAT_SEARCH_EXTRA
966 static int
967matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
968{
969 dictitem_T *di;
970
971 if (tv->v_type != VAR_DICT)
972 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000973 emsg(_(e_dictionary_required));
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200974 return FAIL;
975 }
976
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100977 if (dict_has_key(tv->vval.v_dict, "conceal"))
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200978 *conceal_char = dict_get_string(tv->vval.v_dict,
979 (char_u *)"conceal", FALSE);
980
981 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
982 {
983 *win = find_win_by_nr_or_id(&di->di_tv);
984 if (*win == NULL)
985 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +0000986 emsg(_(e_invalid_window_number));
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200987 return FAIL;
988 }
989 }
990
991 return OK;
992}
993#endif
994
995/*
996 * "clearmatches()" function
997 */
998 void
999f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1000{
1001#ifdef FEAT_SEARCH_EXTRA
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001002 win_T *win;
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001003
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001004 if (in_vim9script() && check_for_opt_number_arg(argvars, 0) == FAIL)
1005 return;
1006
1007 win = get_optional_window(argvars, 0);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001008 if (win != NULL)
1009 clear_matches(win);
1010#endif
1011}
1012
1013/*
1014 * "getmatches()" function
1015 */
1016 void
1017f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1018{
1019# ifdef FEAT_SEARCH_EXTRA
1020 dict_T *dict;
1021 matchitem_T *cur;
1022 int i;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001023 win_T *win;
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001024
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001025 if (in_vim9script() && check_for_opt_number_arg(argvars, 0) == FAIL)
1026 return;
1027
1028 win = get_optional_window(argvars, 0);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001029 if (rettv_list_alloc(rettv) == FAIL || win == NULL)
1030 return;
1031
1032 cur = win->w_match_head;
1033 while (cur != NULL)
1034 {
1035 dict = dict_alloc();
1036 if (dict == NULL)
1037 return;
1038 if (cur->match.regprog == NULL)
1039 {
1040 // match added with matchaddpos()
1041 for (i = 0; i < MAXPOSMATCH; ++i)
1042 {
1043 llpos_T *llpos;
1044 char buf[30]; // use 30 to avoid compiler warning
1045 list_T *l;
1046
1047 llpos = &cur->pos.pos[i];
1048 if (llpos->lnum == 0)
1049 break;
1050 l = list_alloc();
1051 if (l == NULL)
1052 break;
1053 list_append_number(l, (varnumber_T)llpos->lnum);
1054 if (llpos->col > 0)
1055 {
1056 list_append_number(l, (varnumber_T)llpos->col);
1057 list_append_number(l, (varnumber_T)llpos->len);
1058 }
1059 sprintf(buf, "pos%d", i + 1);
1060 dict_add_list(dict, buf, l);
1061 }
1062 }
1063 else
1064 {
1065 dict_add_string(dict, "pattern", cur->pattern);
1066 }
1067 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
1068 dict_add_number(dict, "priority", (long)cur->priority);
1069 dict_add_number(dict, "id", (long)cur->id);
1070# if defined(FEAT_CONCEAL)
1071 if (cur->conceal_char)
1072 {
1073 char_u buf[MB_MAXBYTES + 1];
1074
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00001075 buf[(*mb_char2bytes)(cur->conceal_char, buf)] = NUL;
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001076 dict_add_string(dict, "conceal", (char_u *)&buf);
1077 }
1078# endif
1079 list_append_dict(rettv->vval.v_list, dict);
1080 cur = cur->next;
1081 }
1082# endif
1083}
1084
1085/*
1086 * "setmatches()" function
1087 */
1088 void
1089f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1090{
1091#ifdef FEAT_SEARCH_EXTRA
1092 list_T *l;
1093 listitem_T *li;
1094 dict_T *d;
1095 list_T *s = NULL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001096 win_T *win;
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001097
1098 rettv->vval.v_number = -1;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001099
1100 if (in_vim9script()
1101 && (check_for_list_arg(argvars, 0) == FAIL
1102 || check_for_opt_number_arg(argvars, 1) == FAIL))
1103 return;
1104
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001105 if (argvars[0].v_type != VAR_LIST)
1106 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001107 emsg(_(e_list_required));
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001108 return;
1109 }
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001110 win = get_optional_window(argvars, 1);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001111 if (win == NULL)
1112 return;
1113
1114 if ((l = argvars[0].vval.v_list) != NULL)
1115 {
1116 // To some extent make sure that we are dealing with a list from
1117 // "getmatches()".
1118 li = l->lv_first;
1119 while (li != NULL)
1120 {
1121 if (li->li_tv.v_type != VAR_DICT
1122 || (d = li->li_tv.vval.v_dict) == NULL)
1123 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001124 emsg(_(e_invalid_argument));
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001125 return;
1126 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001127 if (!(dict_has_key(d, "group")
1128 && (dict_has_key(d, "pattern")
1129 || dict_has_key(d, "pos1"))
1130 && dict_has_key(d, "priority")
1131 && dict_has_key(d, "id")))
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001132 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001133 emsg(_(e_invalid_argument));
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001134 return;
1135 }
1136 li = li->li_next;
1137 }
1138
1139 clear_matches(win);
1140 li = l->lv_first;
1141 while (li != NULL)
1142 {
1143 int i = 0;
1144 char buf[30]; // use 30 to avoid compiler warning
1145 dictitem_T *di;
1146 char_u *group;
1147 int priority;
1148 int id;
1149 char_u *conceal;
1150
1151 d = li->li_tv.vval.v_dict;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001152 if (!dict_has_key(d, "pattern"))
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001153 {
1154 if (s == NULL)
1155 {
1156 s = list_alloc();
1157 if (s == NULL)
1158 return;
1159 }
1160
1161 // match from matchaddpos()
1162 for (i = 1; i < 9; i++)
1163 {
1164 sprintf((char *)buf, (char *)"pos%d", i);
1165 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
1166 {
1167 if (di->di_tv.v_type != VAR_LIST)
1168 return;
1169
1170 list_append_tv(s, &di->di_tv);
1171 s->lv_refcount++;
1172 }
1173 else
1174 break;
1175 }
1176 }
1177
1178 group = dict_get_string(d, (char_u *)"group", TRUE);
1179 priority = (int)dict_get_number(d, (char_u *)"priority");
1180 id = (int)dict_get_number(d, (char_u *)"id");
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001181 conceal = dict_has_key(d, "conceal")
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001182 ? dict_get_string(d, (char_u *)"conceal", TRUE)
1183 : NULL;
1184 if (i == 0)
1185 {
1186 match_add(win, group,
1187 dict_get_string(d, (char_u *)"pattern", FALSE),
1188 priority, id, NULL, conceal);
1189 }
1190 else
1191 {
1192 match_add(win, group, NULL, priority, id, s, conceal);
1193 list_unref(s);
1194 s = NULL;
1195 }
1196 vim_free(group);
1197 vim_free(conceal);
1198
1199 li = li->li_next;
1200 }
1201 rettv->vval.v_number = 0;
1202 }
1203#endif
1204}
1205
1206/*
1207 * "matchadd()" function
1208 */
1209 void
1210f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1211{
1212# ifdef FEAT_SEARCH_EXTRA
1213 char_u buf[NUMBUFLEN];
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02001214 char_u *grp; // group
1215 char_u *pat; // pattern
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001216 int prio = 10; // default priority
1217 int id = -1;
1218 int error = FALSE;
1219 char_u *conceal_char = NULL;
1220 win_T *win = curwin;
1221
1222 rettv->vval.v_number = -1;
1223
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02001224 if (in_vim9script()
1225 && (check_for_string_arg(argvars, 0) == FAIL
1226 || check_for_string_arg(argvars, 1) == FAIL
1227 || check_for_opt_number_arg(argvars, 2) == FAIL
1228 || (argvars[2].v_type != VAR_UNKNOWN
1229 && (check_for_opt_number_arg(argvars, 3) == FAIL
1230 || (argvars[3].v_type != VAR_UNKNOWN
1231 && check_for_opt_dict_arg(argvars, 4) == FAIL)))))
1232 return;
1233
1234 grp = tv_get_string_buf_chk(&argvars[0], buf); // group
1235 pat = tv_get_string_buf_chk(&argvars[1], buf); // pattern
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001236 if (grp == NULL || pat == NULL)
1237 return;
1238 if (argvars[2].v_type != VAR_UNKNOWN)
1239 {
1240 prio = (int)tv_get_number_chk(&argvars[2], &error);
1241 if (argvars[3].v_type != VAR_UNKNOWN)
1242 {
1243 id = (int)tv_get_number_chk(&argvars[3], &error);
1244 if (argvars[4].v_type != VAR_UNKNOWN
1245 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
1246 return;
1247 }
1248 }
1249 if (error == TRUE)
1250 return;
1251 if (id >= 1 && id <= 3)
1252 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00001253 semsg(_(e_id_is_reserved_for_match_nr), id);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001254 return;
1255 }
1256
1257 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
1258 conceal_char);
1259# endif
1260}
1261
1262/*
1263 * "matchaddpos()" function
1264 */
1265 void
1266f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1267{
1268# ifdef FEAT_SEARCH_EXTRA
1269 char_u buf[NUMBUFLEN];
1270 char_u *group;
1271 int prio = 10;
1272 int id = -1;
1273 int error = FALSE;
1274 list_T *l;
1275 char_u *conceal_char = NULL;
1276 win_T *win = curwin;
1277
1278 rettv->vval.v_number = -1;
1279
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02001280 if (in_vim9script()
1281 && (check_for_string_arg(argvars, 0) == FAIL
1282 || check_for_list_arg(argvars, 1) == FAIL
1283 || check_for_opt_number_arg(argvars, 2) == FAIL
1284 || (argvars[2].v_type != VAR_UNKNOWN
1285 && (check_for_opt_number_arg(argvars, 3) == FAIL
1286 || (argvars[3].v_type != VAR_UNKNOWN
1287 && check_for_opt_dict_arg(argvars, 4) == FAIL)))))
1288 return;
1289
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001290 group = tv_get_string_buf_chk(&argvars[0], buf);
1291 if (group == NULL)
1292 return;
1293
1294 if (argvars[1].v_type != VAR_LIST)
1295 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001296 semsg(_(e_argument_of_str_must_be_list), "matchaddpos()");
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001297 return;
1298 }
1299 l = argvars[1].vval.v_list;
1300 if (l == NULL)
1301 return;
1302
1303 if (argvars[2].v_type != VAR_UNKNOWN)
1304 {
1305 prio = (int)tv_get_number_chk(&argvars[2], &error);
1306 if (argvars[3].v_type != VAR_UNKNOWN)
1307 {
1308 id = (int)tv_get_number_chk(&argvars[3], &error);
1309
1310 if (argvars[4].v_type != VAR_UNKNOWN
1311 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
1312 return;
1313 }
1314 }
1315 if (error == TRUE)
1316 return;
1317
1318 // id == 3 is ok because matchaddpos() is supposed to substitute :3match
1319 if (id == 1 || id == 2)
1320 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00001321 semsg(_(e_id_is_reserved_for_match_nr), id);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001322 return;
1323 }
1324
1325 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
1326 conceal_char);
1327# endif
1328}
1329
1330/*
1331 * "matcharg()" function
1332 */
1333 void
1334f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
1335{
1336 if (rettv_list_alloc(rettv) == OK)
1337 {
1338# ifdef FEAT_SEARCH_EXTRA
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001339 int id;
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001340 matchitem_T *m;
1341
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001342 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
1343 return;
1344
1345 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001346 if (id >= 1 && id <= 3)
1347 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00001348 if ((m = get_match(curwin, id)) != NULL)
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001349 {
1350 list_append_string(rettv->vval.v_list,
1351 syn_id2name(m->hlg_id), -1);
1352 list_append_string(rettv->vval.v_list, m->pattern, -1);
1353 }
1354 else
1355 {
1356 list_append_string(rettv->vval.v_list, NULL, -1);
1357 list_append_string(rettv->vval.v_list, NULL, -1);
1358 }
1359 }
1360# endif
1361 }
1362}
1363
1364/*
1365 * "matchdelete()" function
1366 */
1367 void
1368f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1369{
1370# ifdef FEAT_SEARCH_EXTRA
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001371 win_T *win;
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001372
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001373 if (in_vim9script()
1374 && (check_for_number_arg(argvars, 0) == FAIL
1375 || check_for_opt_number_arg(argvars, 1) == FAIL))
1376 return;
1377
1378 win = get_optional_window(argvars, 1);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001379 if (win == NULL)
1380 rettv->vval.v_number = -1;
1381 else
1382 rettv->vval.v_number = match_delete(win,
1383 (int)tv_get_number(&argvars[0]), TRUE);
1384# endif
1385}
1386#endif
1387
1388#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
1389/*
1390 * ":[N]match {group} {pattern}"
1391 * Sets nextcmd to the start of the next command, if any. Also called when
1392 * skipping commands to find the next command.
1393 */
1394 void
1395ex_match(exarg_T *eap)
1396{
1397 char_u *p;
1398 char_u *g = NULL;
1399 char_u *end;
1400 int c;
1401 int id;
1402
1403 if (eap->line2 <= 3)
1404 id = eap->line2;
1405 else
1406 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001407 emsg(_(e_invalid_command));
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001408 return;
1409 }
1410
1411 // First clear any old pattern.
1412 if (!eap->skip)
1413 match_delete(curwin, id, FALSE);
1414
1415 if (ends_excmd2(eap->cmd, eap->arg))
1416 end = eap->arg;
1417 else if ((STRNICMP(eap->arg, "none", 4) == 0
1418 && (VIM_ISWHITE(eap->arg[4])
1419 || ends_excmd2(eap->arg, eap->arg + 4))))
1420 end = eap->arg + 4;
1421 else
1422 {
1423 p = skiptowhite(eap->arg);
1424 if (!eap->skip)
1425 g = vim_strnsave(eap->arg, p - eap->arg);
1426 p = skipwhite(p);
1427 if (*p == NUL)
1428 {
1429 // There must be two arguments.
1430 vim_free(g);
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001431 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001432 return;
1433 }
1434 end = skip_regexp(p + 1, *p, TRUE);
1435 if (!eap->skip)
1436 {
1437 if (*end != NUL && !ends_excmd2(end, skipwhite(end + 1)))
1438 {
1439 vim_free(g);
Bram Moolenaar74409f62022-01-01 15:58:22 +00001440 eap->errmsg = ex_errmsg(e_trailing_characters_str, end);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001441 return;
1442 }
1443 if (*end != *p)
1444 {
1445 vim_free(g);
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001446 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001447 return;
1448 }
1449
1450 c = *end;
1451 *end = NUL;
1452 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
1453 vim_free(g);
1454 *end = c;
1455 }
1456 }
1457 eap->nextcmd = find_nextcmd(end);
1458}
1459#endif