blob: 72276527d34f582d07e7a41c888029f1c7581dd5 [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;
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200333 cur = cur->next;
334 }
335 search_hl->buf = wp->w_buffer;
336 search_hl->lnum = 0;
337 search_hl->first_lnum = 0;
338 // time limit is set at the toplevel, for all windows
339}
340
341/*
342 * If there is a match fill "shl" and return one.
343 * Return zero otherwise.
344 */
345 static int
346next_search_hl_pos(
347 match_T *shl, // points to a match
348 linenr_T lnum,
349 posmatch_T *posmatch, // match positions
350 colnr_T mincol) // minimal column for a match
351{
352 int i;
353 int found = -1;
354
355 for (i = posmatch->cur; i < MAXPOSMATCH; i++)
356 {
357 llpos_T *pos = &posmatch->pos[i];
358
359 if (pos->lnum == 0)
360 break;
361 if (pos->len == 0 && pos->col < mincol)
362 continue;
363 if (pos->lnum == lnum)
364 {
365 if (found >= 0)
366 {
367 // if this match comes before the one at "found" then swap
368 // them
369 if (pos->col < posmatch->pos[found].col)
370 {
371 llpos_T tmp = *pos;
372
373 *pos = posmatch->pos[found];
374 posmatch->pos[found] = tmp;
375 }
376 }
377 else
378 found = i;
379 }
380 }
381 posmatch->cur = 0;
382 if (found >= 0)
383 {
384 colnr_T start = posmatch->pos[found].col == 0
385 ? 0 : posmatch->pos[found].col - 1;
386 colnr_T end = posmatch->pos[found].col == 0
387 ? MAXCOL : start + posmatch->pos[found].len;
388
389 shl->lnum = lnum;
390 shl->rm.startpos[0].lnum = 0;
391 shl->rm.startpos[0].col = start;
392 shl->rm.endpos[0].lnum = 0;
393 shl->rm.endpos[0].col = end;
394 shl->is_addpos = TRUE;
Bram Moolenaar693ccd12022-04-16 12:04:37 +0100395 shl->has_cursor = FALSE;
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200396 posmatch->cur = found + 1;
397 return 1;
398 }
399 return 0;
400}
401
402/*
403 * Search for a next 'hlsearch' or match.
404 * Uses shl->buf.
405 * Sets shl->lnum and shl->rm contents.
406 * Note: Assumes a previous match is always before "lnum", unless
407 * shl->lnum is zero.
408 * Careful: Any pointers for buffer lines will become invalid.
409 */
410 static void
411next_search_hl(
412 win_T *win,
413 match_T *search_hl,
414 match_T *shl, // points to search_hl or a match
415 linenr_T lnum,
416 colnr_T mincol, // minimal column for a match
417 matchitem_T *cur) // to retrieve match positions if any
418{
419 linenr_T l;
420 colnr_T matchcol;
421 long nmatched;
422 int called_emsg_before = called_emsg;
Paul Ollis65745772022-06-05 16:55:54 +0100423 int timed_out = FALSE;
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200424
425 // for :{range}s/pat only highlight inside the range
Bram Moolenaar94fb8272021-12-29 19:22:44 +0000426 if ((lnum < search_first_line || lnum > search_last_line) && cur == NULL)
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200427 {
428 shl->lnum = 0;
429 return;
430 }
431
432 if (shl->lnum != 0)
433 {
434 // Check for three situations:
435 // 1. If the "lnum" is below a previous match, start a new search.
436 // 2. If the previous match includes "mincol", use it.
437 // 3. Continue after the previous match.
438 l = shl->lnum + shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
439 if (lnum > l)
440 shl->lnum = 0;
441 else if (lnum < l || shl->rm.endpos[0].col > mincol)
442 return;
443 }
444
445 // Repeat searching for a match until one is found that includes "mincol"
446 // or none is found in this line.
447 for (;;)
448 {
449# ifdef FEAT_RELTIME
450 // Stop searching after passing the time limit.
Paul Ollis65745772022-06-05 16:55:54 +0100451 if (timed_out)
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200452 {
453 shl->lnum = 0; // no match found in time
454 break;
455 }
456# endif
457 // Three situations:
458 // 1. No useful previous match: search from start of line.
459 // 2. Not Vi compatible or empty match: continue at next character.
460 // Break the loop if this is beyond the end of the line.
461 // 3. Vi compatible searching: continue at end of previous match.
462 if (shl->lnum == 0)
463 matchcol = 0;
464 else if (vim_strchr(p_cpo, CPO_SEARCH) == NULL
465 || (shl->rm.endpos[0].lnum == 0
466 && shl->rm.endpos[0].col <= shl->rm.startpos[0].col))
467 {
468 char_u *ml;
469
470 matchcol = shl->rm.startpos[0].col;
471 ml = ml_get_buf(shl->buf, lnum, FALSE) + matchcol;
472 if (*ml == NUL)
473 {
474 ++matchcol;
475 shl->lnum = 0;
476 break;
477 }
478 if (has_mbyte)
479 matchcol += mb_ptr2len(ml);
480 else
481 ++matchcol;
482 }
483 else
484 matchcol = shl->rm.endpos[0].col;
485
486 shl->lnum = lnum;
487 if (shl->rm.regprog != NULL)
488 {
489 // Remember whether shl->rm is using a copy of the regprog in
490 // cur->match.
491 int regprog_is_copy = (shl != search_hl && cur != NULL
492 && shl == &cur->hl
493 && cur->match.regprog == cur->hl.rm.regprog);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200494
495 nmatched = vim_regexec_multi(&shl->rm, win, shl->buf, lnum,
Paul Ollis65745772022-06-05 16:55:54 +0100496 matchcol, &timed_out);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200497 // Copy the regprog, in case it got freed and recompiled.
498 if (regprog_is_copy)
499 cur->match.regprog = cur->hl.rm.regprog;
500
501 if (called_emsg > called_emsg_before || got_int || timed_out)
502 {
503 // Error while handling regexp: stop using this regexp.
504 if (shl == search_hl)
505 {
506 // don't free regprog in the match list, it's a copy
507 vim_regfree(shl->rm.regprog);
508 set_no_hlsearch(TRUE);
509 }
510 shl->rm.regprog = NULL;
511 shl->lnum = 0;
512 got_int = FALSE; // avoid the "Type :quit to exit Vim" message
513 break;
514 }
515 }
516 else if (cur != NULL)
517 nmatched = next_search_hl_pos(shl, lnum, &(cur->pos), matchcol);
518 else
519 nmatched = 0;
520 if (nmatched == 0)
521 {
522 shl->lnum = 0; // no match found
523 break;
524 }
525 if (shl->rm.startpos[0].lnum > 0
526 || shl->rm.startpos[0].col >= mincol
527 || nmatched > 1
528 || shl->rm.endpos[0].col > mincol)
529 {
530 shl->lnum += shl->rm.startpos[0].lnum;
531 break; // useful match found
532 }
533 }
534}
535
536/*
537 * Advance to the match in window "wp" line "lnum" or past it.
538 */
539 void
540prepare_search_hl(win_T *wp, match_T *search_hl, linenr_T lnum)
541{
542 matchitem_T *cur; // points to the match list
543 match_T *shl; // points to search_hl or a match
544 int shl_flag; // flag to indicate whether search_hl
545 // has been processed or not
546 int pos_inprogress; // marks that position match search is
547 // in progress
548 int n;
549
550 // When using a multi-line pattern, start searching at the top
551 // of the window or just after a closed fold.
552 // Do this both for search_hl and the match list.
553 cur = wp->w_match_head;
554 shl_flag = WIN_IS_POPUP(wp); // skip search_hl in a popup window
555 while (cur != NULL || shl_flag == FALSE)
556 {
557 if (shl_flag == FALSE)
558 {
559 shl = search_hl;
560 shl_flag = TRUE;
561 }
562 else
563 shl = &cur->hl;
564 if (shl->rm.regprog != NULL
565 && shl->lnum == 0
566 && re_multiline(shl->rm.regprog))
567 {
568 if (shl->first_lnum == 0)
569 {
570# ifdef FEAT_FOLDING
571 for (shl->first_lnum = lnum;
572 shl->first_lnum > wp->w_topline; --shl->first_lnum)
573 if (hasFoldingWin(wp, shl->first_lnum - 1,
574 NULL, NULL, TRUE, NULL))
575 break;
576# else
577 shl->first_lnum = wp->w_topline;
578# endif
579 }
580 if (cur != NULL)
581 cur->pos.cur = 0;
582 pos_inprogress = TRUE;
583 n = 0;
584 while (shl->first_lnum < lnum && (shl->rm.regprog != NULL
585 || (cur != NULL && pos_inprogress)))
586 {
587 next_search_hl(wp, search_hl, shl, shl->first_lnum, (colnr_T)n,
588 shl == search_hl ? NULL : cur);
589 pos_inprogress = cur == NULL || cur->pos.cur == 0
590 ? FALSE : TRUE;
591 if (shl->lnum != 0)
592 {
593 shl->first_lnum = shl->lnum
594 + shl->rm.endpos[0].lnum
595 - shl->rm.startpos[0].lnum;
596 n = shl->rm.endpos[0].col;
597 }
598 else
599 {
600 ++shl->first_lnum;
601 n = 0;
602 }
603 }
604 }
605 if (shl != search_hl && cur != NULL)
606 cur = cur->next;
607 }
608}
609
610/*
Bram Moolenaar9b367502022-04-22 20:07:21 +0100611 * Update "shl->has_cursor" based on the match in "shl" and the cursor
612 * position.
613 */
614 static void
615check_cur_search_hl(win_T *wp, match_T *shl)
616{
zeertzjq8279cfe2022-04-23 12:05:51 +0100617 linenr_T linecount = shl->rm.endpos[0].lnum - shl->rm.startpos[0].lnum;
Bram Moolenaar9b367502022-04-22 20:07:21 +0100618
619 if (wp->w_cursor.lnum >= shl->lnum
zeertzjq8279cfe2022-04-23 12:05:51 +0100620 && wp->w_cursor.lnum <= shl->lnum + linecount
Bram Moolenaar9b367502022-04-22 20:07:21 +0100621 && (wp->w_cursor.lnum > shl->lnum
622 || wp->w_cursor.col >= shl->rm.startpos[0].col)
623 && (wp->w_cursor.lnum < shl->lnum + linecount
624 || wp->w_cursor.col < shl->rm.endpos[0].col))
625 shl->has_cursor = TRUE;
626 else
627 shl->has_cursor = FALSE;
628}
629
630/*
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200631 * Prepare for 'hlsearch' and match highlighting in one window line.
632 * Return TRUE if there is such highlighting and set "search_attr" to the
633 * current highlight attribute.
634 */
635 int
636prepare_search_hl_line(
637 win_T *wp,
638 linenr_T lnum,
639 colnr_T mincol,
640 char_u **line,
641 match_T *search_hl,
642 int *search_attr)
643{
644 matchitem_T *cur; // points to the match list
645 match_T *shl; // points to search_hl or a match
646 int shl_flag; // flag to indicate whether search_hl
647 // has been processed or not
648 int area_highlighting = FALSE;
649
650 // Handle highlighting the last used search pattern and matches.
651 // Do this for both search_hl and the match list.
652 // Do not use search_hl in a popup window.
653 cur = wp->w_match_head;
654 shl_flag = WIN_IS_POPUP(wp);
655 while (cur != NULL || shl_flag == FALSE)
656 {
657 if (shl_flag == FALSE)
658 {
659 shl = search_hl;
660 shl_flag = TRUE;
661 }
662 else
663 shl = &cur->hl;
664 shl->startcol = MAXCOL;
665 shl->endcol = MAXCOL;
666 shl->attr_cur = 0;
667 shl->is_addpos = FALSE;
Bram Moolenaar693ccd12022-04-16 12:04:37 +0100668 shl->has_cursor = FALSE;
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200669 if (cur != NULL)
670 cur->pos.cur = 0;
671 next_search_hl(wp, search_hl, shl, lnum, mincol,
672 shl == search_hl ? NULL : cur);
673
674 // Need to get the line again, a multi-line regexp may have made it
675 // invalid.
676 *line = ml_get_buf(wp->w_buffer, lnum, FALSE);
677
678 if (shl->lnum != 0 && shl->lnum <= lnum)
679 {
680 if (shl->lnum == lnum)
681 shl->startcol = shl->rm.startpos[0].col;
682 else
683 shl->startcol = 0;
684 if (lnum == shl->lnum + shl->rm.endpos[0].lnum
685 - shl->rm.startpos[0].lnum)
686 shl->endcol = shl->rm.endpos[0].col;
687 else
688 shl->endcol = MAXCOL;
Bram Moolenaar693ccd12022-04-16 12:04:37 +0100689
690 // check if the cursor is in the match before changing the columns
Bram Moolenaar9b367502022-04-22 20:07:21 +0100691 if (shl == search_hl)
692 check_cur_search_hl(wp, shl);
Bram Moolenaar693ccd12022-04-16 12:04:37 +0100693
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200694 // Highlight one character for an empty match.
695 if (shl->startcol == shl->endcol)
696 {
697 if (has_mbyte && (*line)[shl->endcol] != NUL)
698 shl->endcol += (*mb_ptr2len)((*line) + shl->endcol);
699 else
700 ++shl->endcol;
701 }
702 if ((long)shl->startcol < mincol) // match at leftcol
703 {
704 shl->attr_cur = shl->attr;
705 *search_attr = shl->attr;
706 }
707 area_highlighting = TRUE;
708 }
709 if (shl != search_hl && cur != NULL)
710 cur = cur->next;
711 }
712 return area_highlighting;
713}
714
715/*
716 * For a position in a line: Check for start/end of 'hlsearch' and other
717 * matches.
718 * After end, check for start/end of next match.
719 * When another match, have to check for start again.
720 * Watch out for matching an empty string!
Bram Moolenaar0c359af2021-11-29 19:18:57 +0000721 * "on_last_col" is set to TRUE with non-zero search_attr and the next column
722 * is endcol.
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200723 * Return the updated search_attr.
724 */
725 int
726update_search_hl(
727 win_T *wp,
728 linenr_T lnum,
729 colnr_T col,
730 char_u **line,
731 match_T *search_hl,
732 int *has_match_conc UNUSED,
733 int *match_conc UNUSED,
734 int did_line_attr,
Bram Moolenaar0c359af2021-11-29 19:18:57 +0000735 int lcs_eol_one,
736 int *on_last_col)
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200737{
738 matchitem_T *cur; // points to the match list
739 match_T *shl; // points to search_hl or a match
740 int shl_flag; // flag to indicate whether search_hl
741 // has been processed or not
742 int pos_inprogress; // marks that position match search is in
743 // progress
744 int search_attr = 0;
745
746
747 // Do this for 'search_hl' and the match list (ordered by priority).
748 cur = wp->w_match_head;
749 shl_flag = WIN_IS_POPUP(wp);
750 while (cur != NULL || shl_flag == FALSE)
751 {
752 if (shl_flag == FALSE
753 && (cur == NULL
754 || cur->priority > SEARCH_HL_PRIORITY))
755 {
756 shl = search_hl;
757 shl_flag = TRUE;
758 }
759 else
760 shl = &cur->hl;
761 if (cur != NULL)
762 cur->pos.cur = 0;
763 pos_inprogress = TRUE;
764 while (shl->rm.regprog != NULL || (cur != NULL && pos_inprogress))
765 {
766 if (shl->startcol != MAXCOL
767 && col >= shl->startcol
768 && col < shl->endcol)
769 {
770 int next_col = col + mb_ptr2len(*line + col);
771
772 if (shl->endcol < next_col)
773 shl->endcol = next_col;
774 shl->attr_cur = shl->attr;
775# ifdef FEAT_CONCEAL
776 // Match with the "Conceal" group results in hiding
777 // the match.
778 if (cur != NULL
779 && shl != search_hl
780 && syn_name2id((char_u *)"Conceal") == cur->hlg_id)
781 {
782 *has_match_conc = col == shl->startcol ? 2 : 1;
783 *match_conc = cur->conceal_char;
784 }
785 else
786 *has_match_conc = 0;
787# endif
LemonBoya4399382022-04-09 21:04:08 +0100788 // Highlight the match were the cursor is using the CurSearch
789 // group.
Bram Moolenaar693ccd12022-04-16 12:04:37 +0100790 if (shl == search_hl && shl->has_cursor)
Bram Moolenaar368137a2022-05-31 13:43:12 +0100791 {
LemonBoya4399382022-04-09 21:04:08 +0100792 shl->attr_cur = HL_ATTR(HLF_LC);
Bram Moolenaar368137a2022-05-31 13:43:12 +0100793 if (shl->attr_cur != shl->attr)
794 search_hl_has_cursor_lnum = lnum;
795 }
LemonBoya4399382022-04-09 21:04:08 +0100796
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200797 }
798 else if (col == shl->endcol)
799 {
800 shl->attr_cur = 0;
801 next_search_hl(wp, search_hl, shl, lnum, col,
802 shl == search_hl ? NULL : cur);
803 pos_inprogress = !(cur == NULL || cur->pos.cur == 0);
804
805 // Need to get the line again, a multi-line regexp may have
806 // made it invalid.
807 *line = ml_get_buf(wp->w_buffer, lnum, FALSE);
808
809 if (shl->lnum == lnum)
810 {
811 shl->startcol = shl->rm.startpos[0].col;
812 if (shl->rm.endpos[0].lnum == 0)
813 shl->endcol = shl->rm.endpos[0].col;
814 else
815 shl->endcol = MAXCOL;
816
Bram Moolenaar9b367502022-04-22 20:07:21 +0100817 // check if the cursor is in the match
818 if (shl == search_hl)
819 check_cur_search_hl(wp, shl);
820
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200821 if (shl->startcol == shl->endcol)
822 {
823 // highlight empty match, try again after
824 // it
825 if (has_mbyte)
Bram Moolenaar41f08952021-02-22 22:13:49 +0100826 {
827 char_u *p = *line + shl->endcol;
828
829 if (*p == NUL)
830 // consistent with non-mbyte
831 ++shl->endcol;
832 else
833 shl->endcol += (*mb_ptr2len)(p);
834 }
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200835 else
836 ++shl->endcol;
837 }
838
839 // Loop to check if the match starts at the
840 // current position
841 continue;
842 }
843 }
844 break;
845 }
846 if (shl != search_hl && cur != NULL)
847 cur = cur->next;
848 }
849
850 // Use attributes from match with highest priority among 'search_hl' and
851 // the match list.
852 cur = wp->w_match_head;
853 shl_flag = WIN_IS_POPUP(wp);
854 while (cur != NULL || shl_flag == FALSE)
855 {
856 if (shl_flag == FALSE
857 && (cur == NULL ||
858 cur->priority > SEARCH_HL_PRIORITY))
859 {
860 shl = search_hl;
861 shl_flag = TRUE;
862 }
863 else
864 shl = &cur->hl;
865 if (shl->attr_cur != 0)
Bram Moolenaar0c359af2021-11-29 19:18:57 +0000866 {
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200867 search_attr = shl->attr_cur;
Bram Moolenaar0c359af2021-11-29 19:18:57 +0000868 *on_last_col = col + 1 >= shl->endcol;
869 }
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200870 if (shl != search_hl && cur != NULL)
871 cur = cur->next;
872 }
873 // Only highlight one character after the last column.
874 if (*(*line + col) == NUL && (did_line_attr >= 1
875 || (wp->w_p_list && lcs_eol_one == -1)))
876 search_attr = 0;
877 return search_attr;
878}
879
880 int
881get_prevcol_hl_flag(win_T *wp, match_T *search_hl, long curcol)
882{
883 long prevcol = curcol;
884 int prevcol_hl_flag = FALSE;
885 matchitem_T *cur; // points to the match list
886
Bram Moolenaar41f08952021-02-22 22:13:49 +0100887#if defined(FEAT_PROP_POPUP)
888 // don't do this in a popup window
889 if (popup_is_popup(wp))
890 return FALSE;
891#endif
892
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200893 // we're not really at that column when skipping some text
894 if ((long)(wp->w_p_wrap ? wp->w_skipcol : wp->w_leftcol) > prevcol)
895 ++prevcol;
896
Bram Moolenaar41f08952021-02-22 22:13:49 +0100897 // Highlight a character after the end of the line if the match started
898 // at the end of the line or when the match continues in the next line
899 // (match includes the line break).
900 if (!search_hl->is_addpos && (prevcol == (long)search_hl->startcol
901 || (prevcol > (long)search_hl->startcol
902 && search_hl->endcol == MAXCOL)))
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200903 prevcol_hl_flag = TRUE;
904 else
905 {
906 cur = wp->w_match_head;
907 while (cur != NULL)
908 {
Bram Moolenaar41f08952021-02-22 22:13:49 +0100909 if (!cur->hl.is_addpos && (prevcol == (long)cur->hl.startcol
910 || (prevcol > (long)cur->hl.startcol
911 && cur->hl.endcol == MAXCOL)))
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200912 {
913 prevcol_hl_flag = TRUE;
914 break;
915 }
916 cur = cur->next;
917 }
918 }
919 return prevcol_hl_flag;
920}
921
922/*
923 * Get highlighting for the char after the text in "char_attr" from 'hlsearch'
924 * or match highlighting.
925 */
926 void
927get_search_match_hl(win_T *wp, match_T *search_hl, long col, int *char_attr)
928{
929 matchitem_T *cur; // points to the match list
930 match_T *shl; // points to search_hl or a match
931 int shl_flag; // flag to indicate whether search_hl
932 // has been processed or not
933
934 cur = wp->w_match_head;
935 shl_flag = WIN_IS_POPUP(wp);
936 while (cur != NULL || shl_flag == FALSE)
937 {
938 if (shl_flag == FALSE
939 && ((cur != NULL
940 && cur->priority > SEARCH_HL_PRIORITY)
941 || cur == NULL))
942 {
943 shl = search_hl;
944 shl_flag = TRUE;
945 }
946 else
947 shl = &cur->hl;
948 if (col - 1 == (long)shl->startcol
949 && (shl == search_hl || !shl->is_addpos))
950 *char_attr = shl->attr;
951 if (shl != search_hl && cur != NULL)
952 cur = cur->next;
953 }
954}
955
956#endif // FEAT_SEARCH_EXTRA
957
958#if defined(FEAT_EVAL) || defined(PROTO)
959# ifdef FEAT_SEARCH_EXTRA
960 static int
961matchadd_dict_arg(typval_T *tv, char_u **conceal_char, win_T **win)
962{
963 dictitem_T *di;
964
965 if (tv->v_type != VAR_DICT)
966 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000967 emsg(_(e_dictionary_required));
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200968 return FAIL;
969 }
970
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +0100971 if (dict_has_key(tv->vval.v_dict, "conceal"))
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200972 *conceal_char = dict_get_string(tv->vval.v_dict,
973 (char_u *)"conceal", FALSE);
974
975 if ((di = dict_find(tv->vval.v_dict, (char_u *)"window", -1)) != NULL)
976 {
977 *win = find_win_by_nr_or_id(&di->di_tv);
978 if (*win == NULL)
979 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +0000980 emsg(_(e_invalid_window_number));
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200981 return FAIL;
982 }
983 }
984
985 return OK;
986}
987#endif
988
989/*
990 * "clearmatches()" function
991 */
992 void
993f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
994{
995#ifdef FEAT_SEARCH_EXTRA
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200996 win_T *win;
Bram Moolenaar06cf97e2020-06-28 13:17:26 +0200997
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200998 if (in_vim9script() && check_for_opt_number_arg(argvars, 0) == FAIL)
999 return;
1000
1001 win = get_optional_window(argvars, 0);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001002 if (win != NULL)
1003 clear_matches(win);
1004#endif
1005}
1006
1007/*
1008 * "getmatches()" function
1009 */
1010 void
1011f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1012{
1013# ifdef FEAT_SEARCH_EXTRA
1014 dict_T *dict;
1015 matchitem_T *cur;
1016 int i;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001017 win_T *win;
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001018
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001019 if (in_vim9script() && check_for_opt_number_arg(argvars, 0) == FAIL)
1020 return;
1021
1022 win = get_optional_window(argvars, 0);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001023 if (rettv_list_alloc(rettv) == FAIL || win == NULL)
1024 return;
1025
1026 cur = win->w_match_head;
1027 while (cur != NULL)
1028 {
1029 dict = dict_alloc();
1030 if (dict == NULL)
1031 return;
1032 if (cur->match.regprog == NULL)
1033 {
1034 // match added with matchaddpos()
1035 for (i = 0; i < MAXPOSMATCH; ++i)
1036 {
1037 llpos_T *llpos;
1038 char buf[30]; // use 30 to avoid compiler warning
1039 list_T *l;
1040
1041 llpos = &cur->pos.pos[i];
1042 if (llpos->lnum == 0)
1043 break;
1044 l = list_alloc();
1045 if (l == NULL)
1046 break;
1047 list_append_number(l, (varnumber_T)llpos->lnum);
1048 if (llpos->col > 0)
1049 {
1050 list_append_number(l, (varnumber_T)llpos->col);
1051 list_append_number(l, (varnumber_T)llpos->len);
1052 }
1053 sprintf(buf, "pos%d", i + 1);
1054 dict_add_list(dict, buf, l);
1055 }
1056 }
1057 else
1058 {
1059 dict_add_string(dict, "pattern", cur->pattern);
1060 }
1061 dict_add_string(dict, "group", syn_id2name(cur->hlg_id));
1062 dict_add_number(dict, "priority", (long)cur->priority);
1063 dict_add_number(dict, "id", (long)cur->id);
1064# if defined(FEAT_CONCEAL)
1065 if (cur->conceal_char)
1066 {
1067 char_u buf[MB_MAXBYTES + 1];
1068
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00001069 buf[(*mb_char2bytes)(cur->conceal_char, buf)] = NUL;
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001070 dict_add_string(dict, "conceal", (char_u *)&buf);
1071 }
1072# endif
1073 list_append_dict(rettv->vval.v_list, dict);
1074 cur = cur->next;
1075 }
1076# endif
1077}
1078
1079/*
1080 * "setmatches()" function
1081 */
1082 void
1083f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1084{
1085#ifdef FEAT_SEARCH_EXTRA
1086 list_T *l;
1087 listitem_T *li;
1088 dict_T *d;
1089 list_T *s = NULL;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001090 win_T *win;
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001091
1092 rettv->vval.v_number = -1;
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001093
1094 if (in_vim9script()
1095 && (check_for_list_arg(argvars, 0) == FAIL
1096 || check_for_opt_number_arg(argvars, 1) == FAIL))
1097 return;
1098
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001099 if (argvars[0].v_type != VAR_LIST)
1100 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001101 emsg(_(e_list_required));
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001102 return;
1103 }
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02001104 win = get_optional_window(argvars, 1);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001105 if (win == NULL)
1106 return;
1107
1108 if ((l = argvars[0].vval.v_list) != NULL)
1109 {
1110 // To some extent make sure that we are dealing with a list from
1111 // "getmatches()".
1112 li = l->lv_first;
1113 while (li != NULL)
1114 {
1115 if (li->li_tv.v_type != VAR_DICT
1116 || (d = li->li_tv.vval.v_dict) == NULL)
1117 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001118 emsg(_(e_invalid_argument));
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001119 return;
1120 }
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001121 if (!(dict_has_key(d, "group")
1122 && (dict_has_key(d, "pattern")
1123 || dict_has_key(d, "pos1"))
1124 && dict_has_key(d, "priority")
1125 && dict_has_key(d, "id")))
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001126 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001127 emsg(_(e_invalid_argument));
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001128 return;
1129 }
1130 li = li->li_next;
1131 }
1132
1133 clear_matches(win);
1134 li = l->lv_first;
1135 while (li != NULL)
1136 {
1137 int i = 0;
1138 char buf[30]; // use 30 to avoid compiler warning
1139 dictitem_T *di;
1140 char_u *group;
1141 int priority;
1142 int id;
1143 char_u *conceal;
1144
1145 d = li->li_tv.vval.v_dict;
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001146 if (!dict_has_key(d, "pattern"))
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001147 {
1148 if (s == NULL)
1149 {
1150 s = list_alloc();
1151 if (s == NULL)
1152 return;
1153 }
1154
1155 // match from matchaddpos()
1156 for (i = 1; i < 9; i++)
1157 {
1158 sprintf((char *)buf, (char *)"pos%d", i);
1159 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
1160 {
1161 if (di->di_tv.v_type != VAR_LIST)
1162 return;
1163
1164 list_append_tv(s, &di->di_tv);
1165 s->lv_refcount++;
1166 }
1167 else
1168 break;
1169 }
1170 }
1171
1172 group = dict_get_string(d, (char_u *)"group", TRUE);
1173 priority = (int)dict_get_number(d, (char_u *)"priority");
1174 id = (int)dict_get_number(d, (char_u *)"id");
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001175 conceal = dict_has_key(d, "conceal")
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001176 ? dict_get_string(d, (char_u *)"conceal", TRUE)
1177 : NULL;
1178 if (i == 0)
1179 {
1180 match_add(win, group,
1181 dict_get_string(d, (char_u *)"pattern", FALSE),
1182 priority, id, NULL, conceal);
1183 }
1184 else
1185 {
1186 match_add(win, group, NULL, priority, id, s, conceal);
1187 list_unref(s);
1188 s = NULL;
1189 }
1190 vim_free(group);
1191 vim_free(conceal);
1192
1193 li = li->li_next;
1194 }
1195 rettv->vval.v_number = 0;
1196 }
1197#endif
1198}
1199
1200/*
1201 * "matchadd()" function
1202 */
1203 void
1204f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1205{
1206# ifdef FEAT_SEARCH_EXTRA
1207 char_u buf[NUMBUFLEN];
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02001208 char_u *grp; // group
1209 char_u *pat; // pattern
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001210 int prio = 10; // default priority
1211 int id = -1;
1212 int error = FALSE;
1213 char_u *conceal_char = NULL;
1214 win_T *win = curwin;
1215
1216 rettv->vval.v_number = -1;
1217
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02001218 if (in_vim9script()
1219 && (check_for_string_arg(argvars, 0) == FAIL
1220 || check_for_string_arg(argvars, 1) == FAIL
1221 || check_for_opt_number_arg(argvars, 2) == FAIL
1222 || (argvars[2].v_type != VAR_UNKNOWN
1223 && (check_for_opt_number_arg(argvars, 3) == FAIL
1224 || (argvars[3].v_type != VAR_UNKNOWN
1225 && check_for_opt_dict_arg(argvars, 4) == FAIL)))))
1226 return;
1227
1228 grp = tv_get_string_buf_chk(&argvars[0], buf); // group
1229 pat = tv_get_string_buf_chk(&argvars[1], buf); // pattern
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001230 if (grp == NULL || pat == NULL)
1231 return;
1232 if (argvars[2].v_type != VAR_UNKNOWN)
1233 {
1234 prio = (int)tv_get_number_chk(&argvars[2], &error);
1235 if (argvars[3].v_type != VAR_UNKNOWN)
1236 {
1237 id = (int)tv_get_number_chk(&argvars[3], &error);
1238 if (argvars[4].v_type != VAR_UNKNOWN
1239 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
1240 return;
1241 }
1242 }
1243 if (error == TRUE)
1244 return;
1245 if (id >= 1 && id <= 3)
1246 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00001247 semsg(_(e_id_is_reserved_for_match_nr), id);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001248 return;
1249 }
1250
1251 rettv->vval.v_number = match_add(win, grp, pat, prio, id, NULL,
1252 conceal_char);
1253# endif
1254}
1255
1256/*
1257 * "matchaddpos()" function
1258 */
1259 void
1260f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1261{
1262# ifdef FEAT_SEARCH_EXTRA
1263 char_u buf[NUMBUFLEN];
1264 char_u *group;
1265 int prio = 10;
1266 int id = -1;
1267 int error = FALSE;
1268 list_T *l;
1269 char_u *conceal_char = NULL;
1270 win_T *win = curwin;
1271
1272 rettv->vval.v_number = -1;
1273
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +02001274 if (in_vim9script()
1275 && (check_for_string_arg(argvars, 0) == FAIL
1276 || check_for_list_arg(argvars, 1) == FAIL
1277 || check_for_opt_number_arg(argvars, 2) == FAIL
1278 || (argvars[2].v_type != VAR_UNKNOWN
1279 && (check_for_opt_number_arg(argvars, 3) == FAIL
1280 || (argvars[3].v_type != VAR_UNKNOWN
1281 && check_for_opt_dict_arg(argvars, 4) == FAIL)))))
1282 return;
1283
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001284 group = tv_get_string_buf_chk(&argvars[0], buf);
1285 if (group == NULL)
1286 return;
1287
1288 if (argvars[1].v_type != VAR_LIST)
1289 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001290 semsg(_(e_argument_of_str_must_be_list), "matchaddpos()");
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001291 return;
1292 }
1293 l = argvars[1].vval.v_list;
1294 if (l == NULL)
1295 return;
1296
1297 if (argvars[2].v_type != VAR_UNKNOWN)
1298 {
1299 prio = (int)tv_get_number_chk(&argvars[2], &error);
1300 if (argvars[3].v_type != VAR_UNKNOWN)
1301 {
1302 id = (int)tv_get_number_chk(&argvars[3], &error);
1303
1304 if (argvars[4].v_type != VAR_UNKNOWN
1305 && matchadd_dict_arg(&argvars[4], &conceal_char, &win) == FAIL)
1306 return;
1307 }
1308 }
1309 if (error == TRUE)
1310 return;
1311
1312 // id == 3 is ok because matchaddpos() is supposed to substitute :3match
1313 if (id == 1 || id == 2)
1314 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00001315 semsg(_(e_id_is_reserved_for_match_nr), id);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001316 return;
1317 }
1318
1319 rettv->vval.v_number = match_add(win, group, NULL, prio, id, l,
1320 conceal_char);
1321# endif
1322}
1323
1324/*
1325 * "matcharg()" function
1326 */
1327 void
1328f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
1329{
1330 if (rettv_list_alloc(rettv) == OK)
1331 {
1332# ifdef FEAT_SEARCH_EXTRA
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001333 int id;
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001334 matchitem_T *m;
1335
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001336 if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL)
1337 return;
1338
1339 id = (int)tv_get_number(&argvars[0]);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001340 if (id >= 1 && id <= 3)
1341 {
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +00001342 if ((m = get_match(curwin, id)) != NULL)
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001343 {
1344 list_append_string(rettv->vval.v_list,
1345 syn_id2name(m->hlg_id), -1);
1346 list_append_string(rettv->vval.v_list, m->pattern, -1);
1347 }
1348 else
1349 {
1350 list_append_string(rettv->vval.v_list, NULL, -1);
1351 list_append_string(rettv->vval.v_list, NULL, -1);
1352 }
1353 }
1354# endif
1355 }
1356}
1357
1358/*
1359 * "matchdelete()" function
1360 */
1361 void
1362f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
1363{
1364# ifdef FEAT_SEARCH_EXTRA
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001365 win_T *win;
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001366
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001367 if (in_vim9script()
1368 && (check_for_number_arg(argvars, 0) == FAIL
1369 || check_for_opt_number_arg(argvars, 1) == FAIL))
1370 return;
1371
1372 win = get_optional_window(argvars, 1);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001373 if (win == NULL)
1374 rettv->vval.v_number = -1;
1375 else
1376 rettv->vval.v_number = match_delete(win,
1377 (int)tv_get_number(&argvars[0]), TRUE);
1378# endif
1379}
1380#endif
1381
1382#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
1383/*
1384 * ":[N]match {group} {pattern}"
1385 * Sets nextcmd to the start of the next command, if any. Also called when
1386 * skipping commands to find the next command.
1387 */
1388 void
1389ex_match(exarg_T *eap)
1390{
1391 char_u *p;
1392 char_u *g = NULL;
1393 char_u *end;
1394 int c;
1395 int id;
1396
1397 if (eap->line2 <= 3)
1398 id = eap->line2;
1399 else
1400 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001401 emsg(_(e_invalid_command));
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001402 return;
1403 }
1404
1405 // First clear any old pattern.
1406 if (!eap->skip)
1407 match_delete(curwin, id, FALSE);
1408
1409 if (ends_excmd2(eap->cmd, eap->arg))
1410 end = eap->arg;
1411 else if ((STRNICMP(eap->arg, "none", 4) == 0
1412 && (VIM_ISWHITE(eap->arg[4])
1413 || ends_excmd2(eap->arg, eap->arg + 4))))
1414 end = eap->arg + 4;
1415 else
1416 {
1417 p = skiptowhite(eap->arg);
1418 if (!eap->skip)
1419 g = vim_strnsave(eap->arg, p - eap->arg);
1420 p = skipwhite(p);
1421 if (*p == NUL)
1422 {
1423 // There must be two arguments.
1424 vim_free(g);
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001425 semsg(_(e_invalid_argument_str), eap->arg);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001426 return;
1427 }
1428 end = skip_regexp(p + 1, *p, TRUE);
1429 if (!eap->skip)
1430 {
1431 if (*end != NUL && !ends_excmd2(end, skipwhite(end + 1)))
1432 {
1433 vim_free(g);
Bram Moolenaar74409f62022-01-01 15:58:22 +00001434 eap->errmsg = ex_errmsg(e_trailing_characters_str, end);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001435 return;
1436 }
1437 if (*end != *p)
1438 {
1439 vim_free(g);
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001440 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar06cf97e2020-06-28 13:17:26 +02001441 return;
1442 }
1443
1444 c = *end;
1445 *end = NUL;
1446 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
1447 vim_free(g);
1448 *end = c;
1449 }
1450 }
1451 eap->nextcmd = find_nextcmd(end);
1452}
1453#endif