blob: 9edba8d999edb409e11e61a0562f3899be31cd10 [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/*
621 * Prepare for 'hlsearch' and match highlighting in one window line.
622 * Return TRUE if there is such highlighting and set "search_attr" to the
623 * current highlight attribute.
624 */
625 int
626prepare_search_hl_line(
627 win_T *wp,
628 linenr_T lnum,
629 colnr_T mincol,
630 char_u **line,
631 match_T *search_hl,
632 int *search_attr)
633{
634 matchitem_T *cur; // points to the match list
635 match_T *shl; // points to search_hl or a match
636 int shl_flag; // flag to indicate whether search_hl
637 // has been processed or not
638 int area_highlighting = FALSE;
639
640 // Handle highlighting the last used search pattern and matches.
641 // Do this for both search_hl and the match list.
642 // Do not use search_hl in a popup window.
643 cur = wp->w_match_head;
644 shl_flag = WIN_IS_POPUP(wp);
645 while (cur != NULL || shl_flag == FALSE)
646 {
647 if (shl_flag == FALSE)
648 {
649 shl = search_hl;
650 shl_flag = TRUE;
651 }
652 else
653 shl = &cur->hl;
654 shl->startcol = MAXCOL;
655 shl->endcol = MAXCOL;
LemonBoya4399382022-04-09 21:04:08 +0100656 shl->lines = 0;
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200657 shl->attr_cur = 0;
658 shl->is_addpos = FALSE;
Bram Moolenaar693ccd12022-04-16 12:04:37 +0100659 shl->has_cursor = FALSE;
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200660 if (cur != NULL)
661 cur->pos.cur = 0;
662 next_search_hl(wp, search_hl, shl, lnum, mincol,
663 shl == search_hl ? NULL : cur);
664
665 // Need to get the line again, a multi-line regexp may have made it
666 // invalid.
667 *line = ml_get_buf(wp->w_buffer, lnum, FALSE);
668
669 if (shl->lnum != 0 && shl->lnum <= lnum)
670 {
671 if (shl->lnum == lnum)
672 shl->startcol = shl->rm.startpos[0].col;
673 else
674 shl->startcol = 0;
675 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
676 - shl->rm.startpos[0].lnum)
677 shl->endcol = shl->rm.endpos[0].col;
678 else
679 shl->endcol = MAXCOL;
LemonBoya4399382022-04-09 21:04:08 +0100680 if (shl->rm.endpos[0].lnum != shl->rm.startpos[0].lnum)
681 shl->lines = shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
682 else
683 shl->lines = 1;
Bram Moolenaar693ccd12022-04-16 12:04:37 +0100684
685 // check if the cursor is in the match before changing the columns
686 if (wp->w_cursor.lnum >= shl->lnum
687 && wp->w_cursor.lnum
688 <= shl->lnum + shl->rm.endpos[0].lnum
689 && (wp->w_cursor.lnum > shl->lnum
690 || wp->w_cursor.col >= shl->rm.startpos[0].col)
691 && (wp->w_cursor.lnum < shl->lnum + shl->lines
692 || wp->w_cursor.col < shl->rm.endpos[0].col))
693 shl->has_cursor = TRUE;
694
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200695 // Highlight one character for an empty match.
696 if (shl->startcol == shl->endcol)
697 {
698 if (has_mbyte && (*line)[shl->endcol] != NUL)
699 shl->endcol += (*mb_ptr2len)((*line) + shl->endcol);
700 else
701 ++shl->endcol;
702 }
703 if ((long)shl->startcol < mincol) // match at leftcol
704 {
705 shl->attr_cur = shl->attr;
706 *search_attr = shl->attr;
707 }
708 area_highlighting = TRUE;
709 }
710 if (shl != search_hl && cur != NULL)
711 cur = cur->next;
712 }
713 return area_highlighting;
714}
715
716/*
717 * For a position in a line: Check for start/end of 'hlsearch' and other
718 * matches.
719 * After end, check for start/end of next match.
720 * When another match, have to check for start again.
721 * Watch out for matching an empty string!
Bram Moolenaar0c359af2021-11-29 19:18:57 +0000722 * "on_last_col" is set to TRUE with non-zero search_attr and the next column
723 * is endcol.
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200724 * Return the updated search_attr.
725 */
726 int
727update_search_hl(
728 win_T *wp,
729 linenr_T lnum,
730 colnr_T col,
731 char_u **line,
732 match_T *search_hl,
733 int *has_match_conc UNUSED,
734 int *match_conc UNUSED,
735 int did_line_attr,
Bram Moolenaar0c359af2021-11-29 19:18:57 +0000736 int lcs_eol_one,
737 int *on_last_col)
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200738{
739 matchitem_T *cur; // points to the match list
740 match_T *shl; // points to search_hl or a match
741 int shl_flag; // flag to indicate whether search_hl
742 // has been processed or not
743 int pos_inprogress; // marks that position match search is in
744 // progress
745 int search_attr = 0;
746
747
748 // Do this for 'search_hl' and the match list (ordered by priority).
749 cur = wp->w_match_head;
750 shl_flag = WIN_IS_POPUP(wp);
751 while (cur != NULL || shl_flag == FALSE)
752 {
753 if (shl_flag == FALSE
754 && (cur == NULL
755 || cur->priority > SEARCH_HL_PRIORITY))
756 {
757 shl = search_hl;
758 shl_flag = TRUE;
759 }
760 else
761 shl = &cur->hl;
762 if (cur != NULL)
763 cur->pos.cur = 0;
764 pos_inprogress = TRUE;
765 while (shl->rm.regprog != NULL || (cur != NULL && pos_inprogress))
766 {
767 if (shl->startcol != MAXCOL
768 && col >= shl->startcol
769 && col < shl->endcol)
770 {
771 int next_col = col + mb_ptr2len(*line + col);
772
773 if (shl->endcol < next_col)
774 shl->endcol = next_col;
775 shl->attr_cur = shl->attr;
776# ifdef FEAT_CONCEAL
777 // Match with the "Conceal" group results in hiding
778 // the match.
779 if (cur != NULL
780 && shl != search_hl
781 && syn_name2id((char_u *)"Conceal") == cur->hlg_id)
782 {
783 *has_match_conc = col == shl->startcol ? 2 : 1;
784 *match_conc = cur->conceal_char;
785 }
786 else
787 *has_match_conc = 0;
788# endif
LemonBoya4399382022-04-09 21:04:08 +0100789 // Highlight the match were the cursor is using the CurSearch
790 // group.
Bram Moolenaar693ccd12022-04-16 12:04:37 +0100791 if (shl == search_hl && shl->has_cursor)
LemonBoya4399382022-04-09 21:04:08 +0100792 shl->attr_cur = HL_ATTR(HLF_LC);
LemonBoya4399382022-04-09 21:04:08 +0100793
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200794 }
795 else if (col == shl->endcol)
796 {
797 shl->attr_cur = 0;
798 next_search_hl(wp, search_hl, shl, lnum, col,
799 shl == search_hl ? NULL : cur);
800 pos_inprogress = !(cur == NULL || cur->pos.cur == 0);
801
802 // Need to get the line again, a multi-line regexp may have
803 // made it invalid.
804 *line = ml_get_buf(wp->w_buffer, lnum, FALSE);
805
806 if (shl->lnum == lnum)
807 {
808 shl->startcol = shl->rm.startpos[0].col;
809 if (shl->rm.endpos[0].lnum == 0)
810 shl->endcol = shl->rm.endpos[0].col;
811 else
812 shl->endcol = MAXCOL;
813
814 if (shl->startcol == shl->endcol)
815 {
816 // highlight empty match, try again after
817 // it
818 if (has_mbyte)
Bram Moolenaar41f08952021-02-22 22:13:49 +0100819 {
820 char_u *p = *line + shl->endcol;
821
822 if (*p == NUL)
823 // consistent with non-mbyte
824 ++shl->endcol;
825 else
826 shl->endcol += (*mb_ptr2len)(p);
827 }
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200828 else
829 ++shl->endcol;
830 }
831
832 // Loop to check if the match starts at the
833 // current position
834 continue;
835 }
836 }
837 break;
838 }
839 if (shl != search_hl && cur != NULL)
840 cur = cur->next;
841 }
842
843 // Use attributes from match with highest priority among 'search_hl' and
844 // the match list.
845 cur = wp->w_match_head;
846 shl_flag = WIN_IS_POPUP(wp);
847 while (cur != NULL || shl_flag == FALSE)
848 {
849 if (shl_flag == FALSE
850 && (cur == NULL ||
851 cur->priority > SEARCH_HL_PRIORITY))
852 {
853 shl = search_hl;
854 shl_flag = TRUE;
855 }
856 else
857 shl = &cur->hl;
858 if (shl->attr_cur != 0)
Bram Moolenaar0c359af2021-11-29 19:18:57 +0000859 {
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200860 search_attr = shl->attr_cur;
Bram Moolenaar0c359af2021-11-29 19:18:57 +0000861 *on_last_col = col + 1 >= shl->endcol;
862 }
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200863 if (shl != search_hl && cur != NULL)
864 cur = cur->next;
865 }
866 // Only highlight one character after the last column.
867 if (*(*line + col) == NUL && (did_line_attr >= 1
868 || (wp->w_p_list && lcs_eol_one == -1)))
869 search_attr = 0;
870 return search_attr;
871}
872
873 int
874get_prevcol_hl_flag(win_T *wp, match_T *search_hl, long curcol)
875{
876 long prevcol = curcol;
877 int prevcol_hl_flag = FALSE;
878 matchitem_T *cur; // points to the match list
879
Bram Moolenaar41f08952021-02-22 22:13:49 +0100880#if defined(FEAT_PROP_POPUP)
881 // don't do this in a popup window
882 if (popup_is_popup(wp))
883 return FALSE;
884#endif
885
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200886 // we're not really at that column when skipping some text
887 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
888 ++prevcol;
889
Bram Moolenaar41f08952021-02-22 22:13:49 +0100890 // Highlight a character after the end of the line if the match started
891 // at the end of the line or when the match continues in the next line
892 // (match includes the line break).
893 if (!search_hl->is_addpos && (prevcol == (long)search_hl->startcol
894 || (prevcol > (long)search_hl->startcol
895 && search_hl->endcol == MAXCOL)))
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200896 prevcol_hl_flag = TRUE;
897 else
898 {
899 cur = wp->w_match_head;
900 while (cur != NULL)
901 {
Bram Moolenaar41f08952021-02-22 22:13:49 +0100902 if (!cur->hl.is_addpos && (prevcol == (long)cur->hl.startcol
903 || (prevcol > (long)cur->hl.startcol
904 && cur->hl.endcol == MAXCOL)))
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200905 {
906 prevcol_hl_flag = TRUE;
907 break;
908 }
909 cur = cur->next;
910 }
911 }
912 return prevcol_hl_flag;
913}
914
915/*
916 * Get highlighting for the char after the text in "char_attr" from 'hlsearch'
917 * or match highlighting.
918 */
919 void
920get_search_match_hl(win_T *wp, match_T *search_hl, long col, int *char_attr)
921{
922 matchitem_T *cur; // points to the match list
923 match_T *shl; // points to search_hl or a match
924 int shl_flag; // flag to indicate whether search_hl
925 // has been processed or not
926
927 cur = wp->w_match_head;
928 shl_flag = WIN_IS_POPUP(wp);
929 while (cur != NULL || shl_flag == FALSE)
930 {
931 if (shl_flag == FALSE
932 && ((cur != NULL
933 && cur->priority > SEARCH_HL_PRIORITY)
934 || cur == NULL))
935 {
936 shl = search_hl;
937 shl_flag = TRUE;
938 }
939 else
940 shl = &cur->hl;
941 if (col - 1 == (long)shl->startcol
942 && (shl == search_hl || !shl->is_addpos))
943 *char_attr = shl->attr;
944 if (shl != search_hl && cur != NULL)
945 cur = cur->next;
946 }
947}
948
949#endif // FEAT_SEARCH_EXTRA
950
951#if defined(FEAT_EVAL) || defined(PROTO)
952# ifdef FEAT_SEARCH_EXTRA
953 static int
954matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
955{
956 dictitem_T *di;
957
958 if (tv->v_type != VAR_DICT)
959 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000960 emsg(_(e_dictionary_required));
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200961 return FAIL;
962 }
963
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100964 if (dict_has_key(tv->vval.v_dict, "conceal"))
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200965 *conceal_char = dict_get_string(tv->vval.v_dict,
966 (char_u *)"conceal", FALSE);
967
968 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
969 {
970 *win = find_win_by_nr_or_id(&di->di_tv);
971 if (*win == NULL)
972 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +0000973 emsg(_(e_invalid_window_number));
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200974 return FAIL;
975 }
976 }
977
978 return OK;
979}
980#endif
981
982/*
983 * "clearmatches()" function
984 */
985 void
986f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
987{
988#ifdef FEAT_SEARCH_EXTRA
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200989 win_T *win;
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200990
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200991 if (in_vim9script() && check_for_opt_number_arg(argvars, 0) == FAIL)
992 return;
993
994 win = get_optional_window(argvars, 0);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200995 if (win != NULL)
996 clear_matches(win);
997#endif
998}
999
1000/*
1001 * "getmatches()" function
1002 */
1003 void
1004f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1005{
1006# ifdef FEAT_SEARCH_EXTRA
1007 dict_T *dict;
1008 matchitem_T *cur;
1009 int i;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001010 win_T *win;
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001011
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001012 if (in_vim9script() && check_for_opt_number_arg(argvars, 0) == FAIL)
1013 return;
1014
1015 win = get_optional_window(argvars, 0);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001016 if (rettv_list_alloc(rettv) == FAIL || win == NULL)
1017 return;
1018
1019 cur = win->w_match_head;
1020 while (cur != NULL)
1021 {
1022 dict = dict_alloc();
1023 if (dict == NULL)
1024 return;
1025 if (cur->match.regprog == NULL)
1026 {
1027 // match added with matchaddpos()
1028 for (i = 0; i < MAXPOSMATCH; ++i)
1029 {
1030 llpos_T *llpos;
1031 char buf[30]; // use 30 to avoid compiler warning
1032 list_T *l;
1033
1034 llpos = &cur->pos.pos[i];
1035 if (llpos->lnum == 0)
1036 break;
1037 l = list_alloc();
1038 if (l == NULL)
1039 break;
1040 list_append_number(l, (varnumber_T)llpos->lnum);
1041 if (llpos->col > 0)
1042 {
1043 list_append_number(l, (varnumber_T)llpos->col);
1044 list_append_number(l, (varnumber_T)llpos->len);
1045 }
1046 sprintf(buf, "pos%d", i + 1);
1047 dict_add_list(dict, buf, l);
1048 }
1049 }
1050 else
1051 {
1052 dict_add_string(dict, "pattern", cur->pattern);
1053 }
1054 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
1055 dict_add_number(dict, "priority", (long)cur->priority);
1056 dict_add_number(dict, "id", (long)cur->id);
1057# if defined(FEAT_CONCEAL)
1058 if (cur->conceal_char)
1059 {
1060 char_u buf[MB_MAXBYTES + 1];
1061
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00001062 buf[(*mb_char2bytes)(cur->conceal_char, buf)] = NUL;
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001063 dict_add_string(dict, "conceal", (char_u *)&buf);
1064 }
1065# endif
1066 list_append_dict(rettv->vval.v_list, dict);
1067 cur = cur->next;
1068 }
1069# endif
1070}
1071
1072/*
1073 * "setmatches()" function
1074 */
1075 void
1076f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1077{
1078#ifdef FEAT_SEARCH_EXTRA
1079 list_T *l;
1080 listitem_T *li;
1081 dict_T *d;
1082 list_T *s = NULL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001083 win_T *win;
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001084
1085 rettv->vval.v_number = -1;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001086
1087 if (in_vim9script()
1088 && (check_for_list_arg(argvars, 0) == FAIL
1089 || check_for_opt_number_arg(argvars, 1) == FAIL))
1090 return;
1091
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001092 if (argvars[0].v_type != VAR_LIST)
1093 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001094 emsg(_(e_list_required));
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001095 return;
1096 }
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001097 win = get_optional_window(argvars, 1);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001098 if (win == NULL)
1099 return;
1100
1101 if ((l = argvars[0].vval.v_list) != NULL)
1102 {
1103 // To some extent make sure that we are dealing with a list from
1104 // "getmatches()".
1105 li = l->lv_first;
1106 while (li != NULL)
1107 {
1108 if (li->li_tv.v_type != VAR_DICT
1109 || (d = li->li_tv.vval.v_dict) == NULL)
1110 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001111 emsg(_(e_invalid_argument));
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001112 return;
1113 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001114 if (!(dict_has_key(d, "group")
1115 && (dict_has_key(d, "pattern")
1116 || dict_has_key(d, "pos1"))
1117 && dict_has_key(d, "priority")
1118 && dict_has_key(d, "id")))
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001119 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001120 emsg(_(e_invalid_argument));
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001121 return;
1122 }
1123 li = li->li_next;
1124 }
1125
1126 clear_matches(win);
1127 li = l->lv_first;
1128 while (li != NULL)
1129 {
1130 int i = 0;
1131 char buf[30]; // use 30 to avoid compiler warning
1132 dictitem_T *di;
1133 char_u *group;
1134 int priority;
1135 int id;
1136 char_u *conceal;
1137
1138 d = li->li_tv.vval.v_dict;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001139 if (!dict_has_key(d, "pattern"))
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001140 {
1141 if (s == NULL)
1142 {
1143 s = list_alloc();
1144 if (s == NULL)
1145 return;
1146 }
1147
1148 // match from matchaddpos()
1149 for (i = 1; i < 9; i++)
1150 {
1151 sprintf((char *)buf, (char *)"pos%d", i);
1152 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
1153 {
1154 if (di->di_tv.v_type != VAR_LIST)
1155 return;
1156
1157 list_append_tv(s, &di->di_tv);
1158 s->lv_refcount++;
1159 }
1160 else
1161 break;
1162 }
1163 }
1164
1165 group = dict_get_string(d, (char_u *)"group", TRUE);
1166 priority = (int)dict_get_number(d, (char_u *)"priority");
1167 id = (int)dict_get_number(d, (char_u *)"id");
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001168 conceal = dict_has_key(d, "conceal")
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001169 ? dict_get_string(d, (char_u *)"conceal", TRUE)
1170 : NULL;
1171 if (i == 0)
1172 {
1173 match_add(win, group,
1174 dict_get_string(d, (char_u *)"pattern", FALSE),
1175 priority, id, NULL, conceal);
1176 }
1177 else
1178 {
1179 match_add(win, group, NULL, priority, id, s, conceal);
1180 list_unref(s);
1181 s = NULL;
1182 }
1183 vim_free(group);
1184 vim_free(conceal);
1185
1186 li = li->li_next;
1187 }
1188 rettv->vval.v_number = 0;
1189 }
1190#endif
1191}
1192
1193/*
1194 * "matchadd()" function
1195 */
1196 void
1197f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1198{
1199# ifdef FEAT_SEARCH_EXTRA
1200 char_u buf[NUMBUFLEN];
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02001201 char_u *grp; // group
1202 char_u *pat; // pattern
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001203 int prio = 10; // default priority
1204 int id = -1;
1205 int error = FALSE;
1206 char_u *conceal_char = NULL;
1207 win_T *win = curwin;
1208
1209 rettv->vval.v_number = -1;
1210
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02001211 if (in_vim9script()
1212 && (check_for_string_arg(argvars, 0) == FAIL
1213 || check_for_string_arg(argvars, 1) == FAIL
1214 || check_for_opt_number_arg(argvars, 2) == FAIL
1215 || (argvars[2].v_type != VAR_UNKNOWN
1216 && (check_for_opt_number_arg(argvars, 3) == FAIL
1217 || (argvars[3].v_type != VAR_UNKNOWN
1218 && check_for_opt_dict_arg(argvars, 4) == FAIL)))))
1219 return;
1220
1221 grp = tv_get_string_buf_chk(&argvars[0], buf); // group
1222 pat = tv_get_string_buf_chk(&argvars[1], buf); // pattern
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001223 if (grp == NULL || pat == NULL)
1224 return;
1225 if (argvars[2].v_type != VAR_UNKNOWN)
1226 {
1227 prio = (int)tv_get_number_chk(&argvars[2], &error);
1228 if (argvars[3].v_type != VAR_UNKNOWN)
1229 {
1230 id = (int)tv_get_number_chk(&argvars[3], &error);
1231 if (argvars[4].v_type != VAR_UNKNOWN
1232 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
1233 return;
1234 }
1235 }
1236 if (error == TRUE)
1237 return;
1238 if (id >= 1 && id <= 3)
1239 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00001240 semsg(_(e_id_is_reserved_for_match_nr), id);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001241 return;
1242 }
1243
1244 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
1245 conceal_char);
1246# endif
1247}
1248
1249/*
1250 * "matchaddpos()" function
1251 */
1252 void
1253f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1254{
1255# ifdef FEAT_SEARCH_EXTRA
1256 char_u buf[NUMBUFLEN];
1257 char_u *group;
1258 int prio = 10;
1259 int id = -1;
1260 int error = FALSE;
1261 list_T *l;
1262 char_u *conceal_char = NULL;
1263 win_T *win = curwin;
1264
1265 rettv->vval.v_number = -1;
1266
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02001267 if (in_vim9script()
1268 && (check_for_string_arg(argvars, 0) == FAIL
1269 || check_for_list_arg(argvars, 1) == FAIL
1270 || check_for_opt_number_arg(argvars, 2) == FAIL
1271 || (argvars[2].v_type != VAR_UNKNOWN
1272 && (check_for_opt_number_arg(argvars, 3) == FAIL
1273 || (argvars[3].v_type != VAR_UNKNOWN
1274 && check_for_opt_dict_arg(argvars, 4) == FAIL)))))
1275 return;
1276
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001277 group = tv_get_string_buf_chk(&argvars[0], buf);
1278 if (group == NULL)
1279 return;
1280
1281 if (argvars[1].v_type != VAR_LIST)
1282 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001283 semsg(_(e_argument_of_str_must_be_list), "matchaddpos()");
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001284 return;
1285 }
1286 l = argvars[1].vval.v_list;
1287 if (l == NULL)
1288 return;
1289
1290 if (argvars[2].v_type != VAR_UNKNOWN)
1291 {
1292 prio = (int)tv_get_number_chk(&argvars[2], &error);
1293 if (argvars[3].v_type != VAR_UNKNOWN)
1294 {
1295 id = (int)tv_get_number_chk(&argvars[3], &error);
1296
1297 if (argvars[4].v_type != VAR_UNKNOWN
1298 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
1299 return;
1300 }
1301 }
1302 if (error == TRUE)
1303 return;
1304
1305 // id == 3 is ok because matchaddpos() is supposed to substitute :3match
1306 if (id == 1 || id == 2)
1307 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00001308 semsg(_(e_id_is_reserved_for_match_nr), id);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001309 return;
1310 }
1311
1312 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
1313 conceal_char);
1314# endif
1315}
1316
1317/*
1318 * "matcharg()" function
1319 */
1320 void
1321f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
1322{
1323 if (rettv_list_alloc(rettv) == OK)
1324 {
1325# ifdef FEAT_SEARCH_EXTRA
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001326 int id;
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001327 matchitem_T *m;
1328
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001329 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
1330 return;
1331
1332 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001333 if (id >= 1 && id <= 3)
1334 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00001335 if ((m = get_match(curwin, id)) != NULL)
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001336 {
1337 list_append_string(rettv->vval.v_list,
1338 syn_id2name(m->hlg_id), -1);
1339 list_append_string(rettv->vval.v_list, m->pattern, -1);
1340 }
1341 else
1342 {
1343 list_append_string(rettv->vval.v_list, NULL, -1);
1344 list_append_string(rettv->vval.v_list, NULL, -1);
1345 }
1346 }
1347# endif
1348 }
1349}
1350
1351/*
1352 * "matchdelete()" function
1353 */
1354 void
1355f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1356{
1357# ifdef FEAT_SEARCH_EXTRA
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001358 win_T *win;
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001359
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001360 if (in_vim9script()
1361 && (check_for_number_arg(argvars, 0) == FAIL
1362 || check_for_opt_number_arg(argvars, 1) == FAIL))
1363 return;
1364
1365 win = get_optional_window(argvars, 1);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001366 if (win == NULL)
1367 rettv->vval.v_number = -1;
1368 else
1369 rettv->vval.v_number = match_delete(win,
1370 (int)tv_get_number(&argvars[0]), TRUE);
1371# endif
1372}
1373#endif
1374
1375#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
1376/*
1377 * ":[N]match {group} {pattern}"
1378 * Sets nextcmd to the start of the next command, if any. Also called when
1379 * skipping commands to find the next command.
1380 */
1381 void
1382ex_match(exarg_T *eap)
1383{
1384 char_u *p;
1385 char_u *g = NULL;
1386 char_u *end;
1387 int c;
1388 int id;
1389
1390 if (eap->line2 <= 3)
1391 id = eap->line2;
1392 else
1393 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001394 emsg(_(e_invalid_command));
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001395 return;
1396 }
1397
1398 // First clear any old pattern.
1399 if (!eap->skip)
1400 match_delete(curwin, id, FALSE);
1401
1402 if (ends_excmd2(eap->cmd, eap->arg))
1403 end = eap->arg;
1404 else if ((STRNICMP(eap->arg, "none", 4) == 0
1405 && (VIM_ISWHITE(eap->arg[4])
1406 || ends_excmd2(eap->arg, eap->arg + 4))))
1407 end = eap->arg + 4;
1408 else
1409 {
1410 p = skiptowhite(eap->arg);
1411 if (!eap->skip)
1412 g = vim_strnsave(eap->arg, p - eap->arg);
1413 p = skipwhite(p);
1414 if (*p == NUL)
1415 {
1416 // There must be two arguments.
1417 vim_free(g);
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001418 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001419 return;
1420 }
1421 end = skip_regexp(p + 1, *p, TRUE);
1422 if (!eap->skip)
1423 {
1424 if (*end != NUL && !ends_excmd2(end, skipwhite(end + 1)))
1425 {
1426 vim_free(g);
Bram Moolenaar74409f62022-01-01 15:58:22 +00001427 eap->errmsg = ex_errmsg(e_trailing_characters_str, end);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001428 return;
1429 }
1430 if (*end != *p)
1431 {
1432 vim_free(g);
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001433 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001434 return;
1435 }
1436
1437 c = *end;
1438 *end = NUL;
1439 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
1440 vim_free(g);
1441 *end = c;
1442 }
1443 }
1444 eap->nextcmd = find_nextcmd(end);
1445}
1446#endif