robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1// match.c: functions for highlighting matches 2 3#include <assert.h> 4#include <inttypes.h> 5#include <stdbool.h> 6#include <stdio.h> 7#include <string.h> 8 9#include "nvim/ascii_defs.h" 10#include "nvim/buffer_defs.h" 11#include "nvim/charset.h" 12#include "nvim/drawscreen.h" 13#include "nvim/errors.h" 14#include "nvim/eval/funcs.h" 15#include "nvim/eval/typval.h" 16#include "nvim/eval/window.h" 17#include "nvim/ex_cmds_defs.h" 18#include "nvim/ex_docmd.h" 19#include "nvim/fold.h" 20#include "nvim/gettext_defs.h" 21#include "nvim/globals.h" 22#include "nvim/highlight.h" 23#include "nvim/highlight_defs.h" 24#include "nvim/highlight_group.h" 25#include "nvim/macros_defs.h" 26#include "nvim/match.h" 27#include "nvim/mbyte.h" 28#include "nvim/mbyte_defs.h" 29#include "nvim/memline.h" 30#include "nvim/memory.h" 31#include "nvim/message.h" 32#include "nvim/option_vars.h" 33#include "nvim/pos_defs.h" 34#include "nvim/profile.h" 35#include "nvim/regexp.h" 36#include "nvim/strings.h" 37#include "nvim/types_defs.h" 38#include "nvim/vim_defs.h" 39 40#include "match.c.generated.h" 41 42#define SEARCH_HL_PRIORITY 0 43 44/// Add match to the match list of window "wp". 45/// If "pat" is not NULL the pattern will be highlighted with the group "grp" 46/// with priority "prio". 47/// If "pos_list" is not NULL the list of positions defines the highlights. 48/// Optionally, a desired ID "id" can be specified (greater than or equal to 1). 49/// If no particular ID is desired, -1 must be specified for "id". 50/// 51/// @param[in] conceal_char pointer to conceal replacement char 52/// @return ID of added match, -1 on failure. 53static int match_add (win_T * wp ,const char * const grp ,const char * const pat ,int prio ,int id , 54list_T * pos_list ,const char * const conceal_char ) 55FUNC_ATTR_NONNULL_ARG (1 ,2 ) 56{ 57int hlg_id ; 58regprog_T * regprog = NULL ; 59int rtype = UPD_SOME_VALID ; 60 61if (* grp == NUL || (pat != NULL && * pat == NUL )) { 62return -1 ; 63 } 64if (id < -1 || id == 0 ) { 65semsg (_ ("E799: Invalid ID: %" PRId64 66" (must be greater than or equal to 1)" ), 67 (int64_t )id ); 68return -1 ; 69 } 70if (id == -1 ) { 71// use the next available match ID 72id = wp -> w_next_match_id ++ ; 73 }else { 74// check the given ID is not already in use 75for (matchitem_T * cur = wp -> w_match_head ;cur != NULL ;cur = cur -> mit_next ) { 76if (cur -> mit_id == id ) { 77semsg (_ ("E801: ID already taken: %" PRId64 ), (int64_t )id ); 78return -1 ; 79 } 80 } 81 82// Make sure the next match ID is always higher than the highest 83// manually selected ID. Add some extra in case a few more IDs are 84// added soon. 85if (wp -> w_next_match_id < id + 100 ) { 86wp -> w_next_match_id = id + 100 ; 87 } 88 } 89 90if ((hlg_id = syn_check_group (grp ,strlen (grp )))== 0 ) { 91return -1 ; 92 } 93if (pat != NULL && (regprog = vim_regcomp (pat ,RE_MAGIC ))== NULL ) { 94semsg (_ (e_invarg2 ),pat ); 95return -1 ; 96 } 97 98// Build new match. 99matchitem_T * m = xcalloc (1 ,sizeof (matchitem_T )); 100if (tv_list_len (pos_list )> 0 ) { 101m -> mit_pos_array = xcalloc ((size_t )tv_list_len (pos_list ),sizeof (llpos_T )); 102m -> mit_pos_count = tv_list_len (pos_list ); 103 } 104m -> mit_id = id ; 105m -> mit_priority = prio ; 106m -> mit_pattern = pat == NULL ?NULL :xstrdup (pat ); 107m -> mit_hlg_id = hlg_id ; 108m -> mit_match .regprog = regprog ; 109m -> mit_match .rmm_ic = false; 110m -> mit_match .rmm_maxcol = 0 ; 111m -> mit_conceal_char = 0 ; 112if (conceal_char != NULL ) { 113m -> mit_conceal_char = utf_ptr2char (conceal_char ); 114 } 115 116// Set up position matches 117if (pos_list != NULL ) { 118linenr_T toplnum = 0 ; 119linenr_T botlnum = 0 ; 120 121int i = 0 ; 122TV_LIST_ITER (pos_list ,li , { 123linenr_T lnum = 0 ; 124colnr_T col = 0 ; 125int len = 1 ; 126bool error = false; 127 128if (TV_LIST_ITEM_TV (li )-> v_type == VAR_LIST ) { 129const list_T * const subl = TV_LIST_ITEM_TV (li )-> vval .v_list ; 130const listitem_T * subli = tv_list_first (subl ); 131if (subli == NULL ) { 132semsg (_ ("E5030: Empty list at position %d" ), 133 (int )tv_list_idx_of_item (pos_list ,li )); 134 gotofail ; 135 } 136lnum = (linenr_T )tv_get_number_chk (TV_LIST_ITEM_TV (subli ),& error ); 137if (error ) { 138 gotofail ; 139 } 140if (lnum <=0 ) { 141continue ; 142 } 143m -> mit_pos_array [i ].lnum = lnum ; 144subli = TV_LIST_ITEM_NEXT (subl ,subli ); 145if (subli != NULL ) { 146col = (colnr_T )tv_get_number_chk (TV_LIST_ITEM_TV (subli ),& error ); 147if (error ) { 148 gotofail ; 149 } 150if (col < 0 ) { 151continue ; 152 } 153subli = TV_LIST_ITEM_NEXT (subl ,subli ); 154if (subli != NULL ) { 155len = (colnr_T )tv_get_number_chk (TV_LIST_ITEM_TV (subli ),& error ); 156if (len < 0 ) { 157continue ; 158 } 159if (error ) { 160 gotofail ; 161 } 162 } 163 } 164m -> mit_pos_array [i ].col = col ; 165m -> mit_pos_array [i ].len = len ; 166 }else if (TV_LIST_ITEM_TV (li )-> v_type == VAR_NUMBER ) { 167if (TV_LIST_ITEM_TV (li )-> vval .v_number <=0 ) { 168continue ; 169 } 170m -> mit_pos_array [i ].lnum = (linenr_T )TV_LIST_ITEM_TV (li )-> vval .v_number ; 171m -> mit_pos_array [i ].col = 0 ; 172m -> mit_pos_array [i ].len = 0 ; 173 }else { 174semsg (_ ("E5031: List or number required at position %d" ), 175 (int )tv_list_idx_of_item (pos_list ,li )); 176 gotofail ; 177 } 178if (toplnum == 0 || lnum < toplnum ) { 179toplnum = lnum ; 180 } 181if (botlnum == 0 || lnum >=botlnum ) { 182botlnum = lnum + 1 ; 183 } 184i ++ ; 185 }); 186 187// Calculate top and bottom lines for redrawing area 188if (toplnum != 0 ) { 189redraw_win_range_later (wp ,toplnum ,botlnum ); 190m -> mit_toplnum = toplnum ; 191m -> mit_botlnum = botlnum ; 192rtype = UPD_VALID ; 193 } 194 } 195 196// Insert new match. The match list is in ascending order with regard to 197// the match priorities. 198matchitem_T * cur = wp -> w_match_head ; 199matchitem_T * prev = cur ; 200while (cur != NULL && prio >=cur -> mit_priority ) { 201prev = cur ; 202cur = cur -> mit_next ; 203 } 204if (cur == prev ) { 205wp -> w_match_head = m ; 206 }else { 207prev -> mit_next = m ; 208 } 209m -> mit_next = cur ; 210 211redraw_later (wp ,rtype ); 212return id ; 213 214fail : 215vim_regfree (regprog ); 216xfree (m -> mit_pattern ); 217xfree (m -> mit_pos_array ); 218xfree (m ); 219return -1 ; 220} 221 222/// Delete match with ID 'id' in the match list of window 'wp'. 223/// 224/// @param perr print error messages if true. 225static int match_delete (win_T * wp ,int id ,bool perr ) 226{ 227matchitem_T * cur = wp -> w_match_head ; 228matchitem_T * prev = cur ; 229int rtype = UPD_SOME_VALID ; 230 231if (id < 1 ) { 232if (perr ) { 233semsg (_ ("E802: Invalid ID: %" PRId64 " (must be greater than or equal to 1)" ), 234 (int64_t )id ); 235 } 236return -1 ; 237 } 238while (cur != NULL && cur -> mit_id != id ) { 239prev = cur ; 240cur = cur -> mit_next ; 241 } 242if (cur == NULL ) { 243if (perr ) { 244semsg (_ ("E803: ID not found: %" PRId64 ), (int64_t )id ); 245 } 246return -1 ; 247 } 248if (cur == prev ) { 249wp -> w_match_head = cur -> mit_next ; 250 }else { 251prev -> mit_next = cur -> mit_next ; 252 } 253vim_regfree (cur -> mit_match .regprog ); 254xfree (cur -> mit_pattern ); 255if (cur -> mit_toplnum != 0 ) { 256redraw_win_range_later (wp ,cur -> mit_toplnum ,cur -> mit_botlnum ); 257rtype = UPD_VALID ; 258 } 259xfree (cur -> mit_pos_array ); 260xfree (cur ); 261redraw_later (wp ,rtype ); 262return 0 ; 263} 264 265/// Delete all matches in the match list of window 'wp'. 266void clear_matches (win_T * wp ) 267{ 268while (wp -> w_match_head != NULL ) { 269matchitem_T * m = wp -> w_match_head -> mit_next ; 270vim_regfree (wp -> w_match_head -> mit_match .regprog ); 271xfree (wp -> w_match_head -> mit_pattern ); 272xfree (wp -> w_match_head -> mit_pos_array ); 273xfree (wp -> w_match_head ); 274wp -> w_match_head = m ; 275 } 276redraw_later (wp ,UPD_SOME_VALID ); 277} 278 279/// Get match from ID 'id' in window 'wp'. 280/// Return NULL if match not found. 281static matchitem_T * get_match (win_T * wp ,int id ) 282{ 283matchitem_T * cur = wp -> w_match_head ; 284 285while (cur != NULL && cur -> mit_id != id ) { 286cur = cur -> mit_next ; 287 } 288return cur ; 289} 290 291/// Init for calling prepare_search_hl(). 292void init_search_hl (win_T * wp ,match_T * search_hl ) 293FUNC_ATTR_NONNULL_ALL 294{ 295// Setup for match and 'hlsearch' highlighting. Disable any previous 296// match 297matchitem_T * cur = wp -> w_match_head ; 298while (cur != NULL ) { 299cur -> mit_hl .rm = cur -> mit_match ; 300if (cur -> mit_hlg_id == 0 ) { 301cur -> mit_hl .attr = 0 ; 302 }else { 303cur -> mit_hl .attr = syn_id2attr (cur -> mit_hlg_id ); 304 } 305cur -> mit_hl .buf = wp -> w_buffer ; 306cur -> mit_hl .lnum = 0 ; 307cur -> mit_hl .first_lnum = 0 ; 308// Set the time limit to 'redrawtime'. 309cur -> mit_hl .tm = profile_setlimit (p_rdt ); 310cur = cur -> mit_next ; 311 } 312search_hl -> buf = wp -> w_buffer ; 313search_hl -> lnum = 0 ; 314search_hl -> first_lnum = 0 ; 315search_hl -> attr = win_hl_attr (wp ,HLF_L ); 316 317// time limit is set at the toplevel, for all windows 318} 319 320/// @param shl points to a match. Fill on match. 321/// @param posmatch match item with positions 322/// @param mincol minimal column for a match 323/// 324/// @return one on match, otherwise return zero. 325static int next_search_hl_pos (match_T * shl ,linenr_T lnum ,matchitem_T * match ,colnr_T mincol ) 326FUNC_ATTR_NONNULL_ALL 327{ 328int found = -1 ; 329 330shl -> lnum = 0 ; 331for (int i = match -> mit_pos_cur ;i < match -> mit_pos_count ;i ++ ) { 332llpos_T * pos = & match -> mit_pos_array [i ]; 333 334if (pos -> lnum == 0 ) { 335break ; 336 } 337if (pos -> len == 0 && pos -> col < mincol ) { 338continue ; 339 } 340if (pos -> lnum == lnum ) { 341if (found >=0 ) { 342// if this match comes before the one at "found" then swap them 343if (pos -> col < match -> mit_pos_array [found ].col ) { 344llpos_T tmp = * pos ; 345 346* pos = match -> mit_pos_array [found ]; 347match -> mit_pos_array [found ]= tmp ; 348 } 349 }else { 350found = i ; 351 } 352 } 353 } 354match -> mit_pos_cur = 0 ; 355if (found >=0 ) { 356colnr_T start = match -> mit_pos_array [found ].col == 0 357 ?0 :match -> mit_pos_array [found ].col - 1 ; 358colnr_T end = match -> mit_pos_array [found ].col == 0 359 ?MAXCOL :start + match -> mit_pos_array [found ].len ; 360 361shl -> lnum = lnum ; 362shl -> rm .startpos [0 ].lnum = 0 ; 363shl -> rm .startpos [0 ].col = start ; 364shl -> rm .endpos [0 ].lnum = 0 ; 365shl -> rm .endpos [0 ].col = end ; 366shl -> is_addpos = true; 367shl -> has_cursor = false; 368match -> mit_pos_cur = found + 1 ; 369return 1 ; 370 } 371return 0 ; 372} 373 374/// Search for a next 'hlsearch' or match. 375/// Uses shl->buf. 376/// Sets shl->lnum and shl->rm contents. 377/// Note: Assumes a previous match is always before "lnum", unless 378/// shl->lnum is zero. 379/// Careful: Any pointers for buffer lines will become invalid. 380/// 381/// @param shl points to search_hl or a match 382/// @param mincol minimal column for a match 383/// @param cur to retrieve match positions if any 384static void next_search_hl (win_T * win ,match_T * search_hl ,match_T * shl ,linenr_T lnum , 385colnr_T mincol ,matchitem_T * cur ) 386FUNC_ATTR_NONNULL_ARG (2 ) 387{ 388colnr_T matchcol ; 389int nmatched = 0 ; 390const int called_emsg_before = called_emsg ; 391 392// for :{range}s/pat only highlight inside the range 393if ((lnum < Search .first_line || lnum > Search .last_line )&& cur == NULL ) { 394shl -> lnum = 0 ; 395return ; 396 } 397 398if (shl -> lnum != 0 ) { 399// Check for three situations: 400// 1. If the "lnum" is below a previous match, start a new search. 401// 2. If the previous match includes "mincol", use it. 402// 3. Continue after the previous match. 403linenr_T l = shl -> lnum + shl -> rm .endpos [0 ].lnum - shl -> rm .startpos [0 ].lnum ; 404if (lnum > l ) { 405shl -> lnum = 0 ; 406 }else if (lnum < l || shl -> rm .endpos [0 ].col > mincol ) { 407return ; 408 } 409 } 410 411// Repeat searching for a match until one is found that includes "mincol" 412// or none is found in this line. 413while (true) { 414// Stop searching after passing the time limit. 415if (profile_passed_limit (shl -> tm )) { 416shl -> lnum = 0 ;// no match found in time 417break ; 418 } 419// Three situations: 420// 1. No useful previous match: search from start of line. 421// 2. Not Vi compatible or empty match: continue at next character. 422// Break the loop if this is beyond the end of the line. 423// 3. Vi compatible searching: continue at end of previous match. 424if (shl -> lnum == 0 ) { 425matchcol = 0 ; 426 }else if (vim_strchr (p_cpo ,kCpoSearch )== NULL 427|| (shl -> rm .endpos [0 ].lnum == 0 428&& shl -> rm .endpos [0 ].col <=shl -> rm .startpos [0 ].col )) { 429matchcol = shl -> rm .startpos [0 ].col ; 430char * ml = ml_get_buf (shl -> buf ,lnum )+ matchcol ; 431if (* ml == NUL ) { 432matchcol ++ ; 433shl -> lnum = 0 ; 434break ; 435 } 436matchcol += utfc_ptr2len (ml ); 437 }else { 438matchcol = shl -> rm .endpos [0 ].col ; 439 } 440 441shl -> lnum = lnum ; 442if (shl -> rm .regprog != NULL ) { 443// Remember whether shl->rm is using a copy of the regprog in 444// cur->mit_match. 445bool regprog_is_copy = (shl != search_hl && cur != NULL 446&& shl == & cur -> mit_hl 447&& cur -> mit_match .regprog == cur -> mit_hl .rm .regprog ); 448int timed_out = false; 449 450nmatched = vim_regexec_multi (& shl -> rm ,win ,shl -> buf ,lnum ,matchcol , 451& (shl -> tm ),& timed_out ); 452// Copy the regprog, in case it got freed and recompiled. 453if (regprog_is_copy ) { 454cur -> mit_match .regprog = cur -> mit_hl .rm .regprog ; 455 } 456if (called_emsg > called_emsg_before || got_int || timed_out ) { 457// Error while handling regexp: stop using this regexp. 458if (shl == search_hl ) { 459// don't free regprog in the match list, it's a copy 460vim_regfree (shl -> rm .regprog ); 461set_no_hlsearch (true); 462 } 463shl -> rm .regprog = NULL ; 464shl -> lnum = 0 ; 465got_int = false;// avoid the "Type :quit to exit Vim" message 466break ; 467 } 468 }else if (cur != NULL ) { 469nmatched = next_search_hl_pos (shl ,lnum ,cur ,matchcol ); 470 } 471if (nmatched == 0 ) { 472shl -> lnum = 0 ;// no match found 473break ; 474 } 475if (shl -> rm .startpos [0 ].lnum > 0 476|| shl -> rm .startpos [0 ].col >=mincol 477|| nmatched > 1 478|| shl -> rm .endpos [0 ].col > mincol ) { 479shl -> lnum += shl -> rm .startpos [0 ].lnum ; 480break ;// useful match found 481 } 482 } 483} 484 485/// Advance to the match in window "wp" line "lnum" or past it. 486void prepare_search_hl (win_T * wp ,match_T * search_hl ,linenr_T lnum ) 487FUNC_ATTR_NONNULL_ALL 488{ 489matchitem_T * cur = wp -> w_match_head ;// points to the match list 490match_T * shl ;// points to search_hl or a match 491bool shl_flag = false;// flag to indicate whether search_hl has been processed or not 492 493// When using a multi-line pattern, start searching at the top 494// of the window or just after a closed fold. 495// Do this both for search_hl and the match list. 496while (cur != NULL || shl_flag == false) { 497if (shl_flag == false) { 498shl = search_hl ; 499shl_flag = true; 500 }else { 501shl = & cur -> mit_hl ; 502 } 503if (shl -> rm .regprog != NULL 504&& shl -> lnum == 0 505&& re_multiline (shl -> rm .regprog )) { 506if (shl -> first_lnum == 0 ) { 507for (shl -> first_lnum = lnum ; 508shl -> first_lnum > wp -> w_topline ; 509shl -> first_lnum -- ) { 510if (hasFolding (wp ,shl -> first_lnum - 1 ,NULL ,NULL )) { 511break ; 512 } 513 } 514 } 515if (cur != NULL ) { 516cur -> mit_pos_cur = 0 ; 517 } 518bool pos_inprogress = true;// mark that a position match search is 519// in progress 520int n = 0 ; 521while (shl -> first_lnum < lnum && (shl -> rm .regprog != NULL 522|| (cur != NULL && pos_inprogress ))) { 523next_search_hl (wp ,search_hl ,shl ,shl -> first_lnum , (colnr_T )n , 524shl == search_hl ?NULL :cur ); 525pos_inprogress = !(cur == NULL || cur -> mit_pos_cur == 0 ); 526if (shl -> lnum != 0 ) { 527shl -> first_lnum = shl -> lnum 528+ shl -> rm .endpos [0 ].lnum 529- shl -> rm .startpos [0 ].lnum ; 530n = shl -> rm .endpos [0 ].col ; 531 }else { 532shl -> first_lnum ++ ; 533n = 0 ; 534 } 535 } 536 } 537if (shl != search_hl && cur != NULL ) { 538cur = cur -> mit_next ; 539 } 540 } 541} 542 543/// Update "shl->has_cursor" based on the match in "shl" and the cursor 544/// position. 545static void check_cur_search_hl (win_T * wp ,match_T * shl ) 546{ 547linenr_T linecount = shl -> rm .endpos [0 ].lnum - shl -> rm .startpos [0 ].lnum ; 548 549if (wp -> w_cursor .lnum >=shl -> lnum 550&& wp -> w_cursor .lnum <=shl -> lnum + linecount 551&& (wp -> w_cursor .lnum > shl -> lnum || wp -> w_cursor .col >=shl -> rm .startpos [0 ].col ) 552&& (wp -> w_cursor .lnum < shl -> lnum + linecount || wp -> w_cursor .col < shl -> rm .endpos [0 ].col )) { 553shl -> has_cursor = true; 554 }else { 555shl -> has_cursor = false; 556 } 557} 558 559/// Prepare for 'hlsearch' and match highlighting in one window line. 560/// 561/// @return true if there is such highlighting and set "search_attr" to the 562/// current highlight attribute. 563bool prepare_search_hl_line (win_T * wp ,linenr_T lnum ,colnr_T mincol ,char ** line , 564match_T * search_hl ,int * search_attr ,bool * search_attr_from_match ) 565{ 566matchitem_T * cur = wp -> w_match_head ;// points to the match list 567match_T * shl ;// points to search_hl or a match 568bool shl_flag = false;// flag to indicate whether search_hl 569// has been processed or not 570bool area_highlighting = false; 571 572// Handle highlighting the last used search pattern and matches. 573// Do this for both search_hl and the match list. 574while (cur != NULL || !shl_flag ) { 575if (!shl_flag ) { 576shl = search_hl ; 577shl_flag = true; 578 }else { 579shl = & cur -> mit_hl ; 580 } 581shl -> startcol = MAXCOL ; 582shl -> endcol = MAXCOL ; 583shl -> attr_cur = 0 ; 584shl -> is_addpos = false; 585shl -> has_cursor = false; 586if (cur != NULL ) { 587cur -> mit_pos_cur = 0 ; 588 } 589next_search_hl (wp ,search_hl ,shl ,lnum ,mincol , 590shl == search_hl ?NULL :cur ); 591 592// Need to get the line again, a multi-line regexp may have made it 593// invalid. 594* line = ml_get_buf (wp -> w_buffer ,lnum ); 595 596if (shl -> lnum != 0 && shl -> lnum <=lnum ) { 597if (shl -> lnum == lnum ) { 598shl -> startcol = shl -> rm .startpos [0 ].col ; 599 }else { 600shl -> startcol = 0 ; 601 } 602if (lnum == shl -> lnum + shl -> rm .endpos [0 ].lnum 603- shl -> rm .startpos [0 ].lnum ) { 604shl -> endcol = shl -> rm .endpos [0 ].col ; 605 }else { 606shl -> endcol = MAXCOL ; 607 } 608 609// check if the cursor is in the match before changing the columns 610if (shl == search_hl ) { 611check_cur_search_hl (wp ,shl ); 612 } 613 614// Highlight one character for an empty match. 615if (shl -> startcol == shl -> endcol ) { 616if ((* line )[shl -> endcol ]!= NUL ) { 617shl -> endcol += utfc_ptr2len (* line + shl -> endcol ); 618 }else { 619shl -> endcol ++ ; 620 } 621 } 622if (shl -> startcol < mincol ) {// match at leftcol 623shl -> attr_cur = shl -> attr ; 624* search_attr = shl -> attr ; 625* search_attr_from_match = shl != search_hl ; 626 } 627area_highlighting = true; 628 } 629if (shl != search_hl && cur != NULL ) { 630cur = cur -> mit_next ; 631 } 632 } 633return area_highlighting ; 634} 635 636/// For a position in a line: Check for start/end of 'hlsearch' and other 637/// matches. 638/// After end, check for start/end of next match. 639/// When another match, have to check for start again. 640/// Watch out for matching an empty string! 641/// "on_last_col" is set to true with non-zero search_attr and the next column 642/// is endcol. 643/// Return the updated search_attr. 644int update_search_hl (win_T * wp ,linenr_T lnum ,colnr_T col ,char ** line ,match_T * search_hl , 645int * has_match_conc ,int * match_conc ,bool lcs_eol_todo ,bool * on_last_col , 646bool * search_attr_from_match ) 647{ 648matchitem_T * cur = wp -> w_match_head ;// points to the match list 649match_T * shl ;// points to search_hl or a match 650bool shl_flag = false;// flag to indicate whether search_hl 651// has been processed or not 652int search_attr = 0 ; 653 654// Do this for 'search_hl' and the match list (ordered by priority). 655while (cur != NULL || !shl_flag ) { 656if (!shl_flag 657&& (cur == NULL || cur -> mit_priority > SEARCH_HL_PRIORITY )) { 658shl = search_hl ; 659shl_flag = true; 660 }else { 661shl = & cur -> mit_hl ; 662 } 663if (cur != NULL ) { 664cur -> mit_pos_cur = 0 ; 665 } 666bool pos_inprogress = true;// mark that a position match search is 667// in progress 668while (shl -> rm .regprog != NULL 669|| (cur != NULL && pos_inprogress )) { 670if (shl -> startcol != MAXCOL 671&& col >=shl -> startcol 672&& col < shl -> endcol ) { 673int next_col = col + utfc_ptr2len (* line + col ); 674 675if (shl -> endcol < next_col ) { 676shl -> endcol = next_col ; 677 } 678// Highlight the match were the cursor is using the CurSearch 679// group. 680if (shl == search_hl && shl -> has_cursor ) { 681shl -> attr_cur = win_hl_attr (wp ,HLF_LC ); 682if (shl -> attr_cur != shl -> attr ) { 683search_hl_has_cursor_lnum = lnum ; 684 } 685 }else { 686shl -> attr_cur = shl -> attr ; 687 } 688// Match with the "Conceal" group results in hiding 689// the match. 690if (cur != NULL 691&& shl != search_hl 692&& syn_name2id ("Conceal" )== cur -> mit_hlg_id ) { 693* has_match_conc = col == shl -> startcol ?2 :1 ; 694* match_conc = cur -> mit_conceal_char ; 695 }else { 696* has_match_conc = 0 ; 697 } 698 }else if (col == shl -> endcol ) { 699shl -> attr_cur = 0 ; 700 701next_search_hl (wp ,search_hl ,shl ,lnum ,col , 702shl == search_hl ?NULL :cur ); 703pos_inprogress = !(cur == NULL || cur -> mit_pos_cur == 0 ); 704 705// Need to get the line again, a multi-line regexp 706// may have made it invalid. 707* line = ml_get_buf (wp -> w_buffer ,lnum ); 708 709if (shl -> lnum == lnum ) { 710shl -> startcol = shl -> rm .startpos [0 ].col ; 711if (shl -> rm .endpos [0 ].lnum == 0 ) { 712shl -> endcol = shl -> rm .endpos [0 ].col ; 713 }else { 714shl -> endcol = MAXCOL ; 715 } 716 717// check if the cursor is in the match 718if (shl == search_hl ) { 719check_cur_search_hl (wp ,shl ); 720 } 721 722if (shl -> startcol == shl -> endcol ) { 723// highlight empty match, try again after it 724char * p = * line + shl -> endcol ; 725 726if (* p == NUL ) { 727shl -> endcol ++ ; 728 }else { 729shl -> endcol += utfc_ptr2len (p ); 730 } 731 } 732 733// Loop to check if the match starts at the 734// current position 735continue ; 736 } 737 } 738break ; 739 } 740if (shl != search_hl && cur != NULL ) { 741cur = cur -> mit_next ; 742 } 743 } 744 745// Use attributes from match with highest priority among 'search_hl' and 746// the match list. 747* search_attr_from_match = false; 748search_attr = search_hl -> attr_cur ; 749cur = wp -> w_match_head ; 750shl_flag = false; 751while (cur != NULL || !shl_flag ) { 752if (!shl_flag 753&& (cur == NULL || cur -> mit_priority > SEARCH_HL_PRIORITY )) { 754shl = search_hl ; 755shl_flag = true; 756 }else { 757shl = & cur -> mit_hl ; 758 } 759if (shl -> attr_cur != 0 ) { 760search_attr = shl -> attr_cur ; 761* on_last_col = col + 1 >=shl -> endcol ; 762* search_attr_from_match = shl != search_hl ; 763 } 764if (shl != search_hl && cur != NULL ) { 765cur = cur -> mit_next ; 766 } 767 } 768// Only highlight one character after the last column. 769if (* (* line + col )== NUL && (wp -> w_p_list && !lcs_eol_todo )) { 770search_attr = 0 ; 771 } 772return search_attr ; 773} 774 775bool get_prevcol_hl_flag (win_T * wp ,match_T * search_hl ,colnr_T curcol ) 776{ 777colnr_T prevcol = curcol ; 778 779// we're not really at that column when skipping some text 780if ((wp -> w_p_wrap ?wp -> w_skipcol :wp -> w_leftcol )> prevcol ) { 781prevcol ++ ; 782 } 783 784// Highlight a character after the end of the line if the match started 785// at the end of the line or when the match continues in the next line 786// (match includes the line break). 787if (!search_hl -> is_addpos && (prevcol == search_hl -> startcol 788|| (prevcol > search_hl -> startcol 789&& search_hl -> endcol == MAXCOL ))) { 790return true; 791 } 792matchitem_T * cur = wp -> w_match_head ;// points to the match list 793while (cur != NULL ) { 794if (!cur -> mit_hl .is_addpos && (prevcol == cur -> mit_hl .startcol 795|| (prevcol > cur -> mit_hl .startcol 796&& cur -> mit_hl .endcol == MAXCOL ))) { 797return true; 798 } 799cur = cur -> mit_next ; 800 } 801 802return false; 803} 804 805/// Get highlighting for the char after the text in "char_attr" from 'hlsearch' 806/// or match highlighting. 807void get_search_match_hl (win_T * wp ,match_T * search_hl ,colnr_T col ,int * char_attr ) 808{ 809matchitem_T * cur = wp -> w_match_head ;// points to the match list 810match_T * shl ;// points to search_hl or a match 811bool shl_flag = false;// flag to indicate whether search_hl 812// has been processed or not 813 814while (cur != NULL || !shl_flag ) { 815if (!shl_flag 816&& (cur == NULL || cur -> mit_priority > SEARCH_HL_PRIORITY )) { 817shl = search_hl ; 818shl_flag = true; 819 }else { 820shl = & cur -> mit_hl ; 821 } 822if (col - 1 == shl -> startcol 823&& (shl == search_hl || !shl -> is_addpos )) { 824* char_attr = shl -> attr ; 825 } 826if (shl != search_hl && cur != NULL ) { 827cur = cur -> mit_next ; 828 } 829 } 830} 831 832static int matchadd_dict_arg (typval_T * tv ,const char ** conceal_char ,win_T ** win ) 833{ 834dictitem_T * di ; 835 836if (tv -> v_type != VAR_DICT ) { 837emsg (_ (e_dictreq )); 838return FAIL ; 839 } 840 841if ((di = tv_dict_find (tv -> vval .v_dict ,S_LEN ("conceal" )))!= NULL ) { 842* conceal_char = tv_get_string (& di -> di_tv ); 843 } 844 845if ((di = tv_dict_find (tv -> vval .v_dict ,S_LEN ("window" )))== NULL ) { 846return OK ; 847 } 848 849* win = find_win_by_nr_or_id (& di -> di_tv ); 850if (* win == NULL ) { 851emsg (_ (e_invalwindow )); 852return FAIL ; 853 } 854 855return OK ; 856} 857 858/// "clearmatches()" function 859void f_clearmatches (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 860{ 861win_T * win = get_optional_window (argvars ,0 ); 862 863if (win != NULL ) { 864clear_matches (win ); 865 } 866} 867 868/// "getmatches()" function 869void f_getmatches (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 870{ 871win_T * win = get_optional_window (argvars ,0 ); 872 873tv_list_alloc_ret (rettv ,kListLenMayKnow ); 874if (win == NULL ) { 875return ; 876 } 877 878matchitem_T * cur = win -> w_match_head ; 879while (cur != NULL ) { 880dict_T * dict = tv_dict_alloc (); 881if (cur -> mit_match .regprog == NULL ) { 882// match added with matchaddpos() 883for (int i = 0 ;i < cur -> mit_pos_count ;i ++ ) { 884llpos_T * llpos ; 885char buf [30 ];// use 30 to avoid compiler warning 886 887llpos = & cur -> mit_pos_array [i ]; 888if (llpos -> lnum == 0 ) { 889break ; 890 } 891list_T * const l = tv_list_alloc (1 + (llpos -> col > 0 ?2 :0 )); 892tv_list_append_number (l , (varnumber_T )llpos -> lnum ); 893if (llpos -> col > 0 ) { 894tv_list_append_number (l , (varnumber_T )llpos -> col ); 895tv_list_append_number (l , (varnumber_T )llpos -> len ); 896 } 897int len = snprintf (buf ,sizeof (buf ),"pos%d" ,i + 1 ); 898assert ((size_t )len < sizeof (buf )); 899tv_dict_add_list (dict ,buf , (size_t )len ,l ); 900 } 901 }else { 902tv_dict_add_str (dict ,S_LEN ("pattern" ),cur -> mit_pattern ); 903 } 904tv_dict_add_str (dict ,S_LEN ("group" ),syn_id2name (cur -> mit_hlg_id )); 905tv_dict_add_nr (dict ,S_LEN ("priority" ), (varnumber_T )cur -> mit_priority ); 906tv_dict_add_nr (dict ,S_LEN ("id" ), (varnumber_T )cur -> mit_id ); 907 908if (cur -> mit_conceal_char ) { 909char buf [MB_MAXCHAR + 1 ]; 910int buflen = utf_char2bytes (cur -> mit_conceal_char ,buf ); 911buf [buflen ]= NUL ; 912tv_dict_add_str_len (dict ,S_LEN ("conceal" ),buf ,buflen ); 913 } 914 915tv_list_append_dict (rettv -> vval .v_list ,dict ); 916cur = cur -> mit_next ; 917 } 918} 919 920/// "setmatches()" function 921void f_setmatches (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 922{ 923dict_T * d ; 924list_T * s = NULL ; 925win_T * win = get_optional_window (argvars ,1 ); 926 927rettv -> vval .v_number = -1 ; 928if (argvars [0 ].v_type != VAR_LIST ) { 929emsg (_ (e_listreq )); 930return ; 931 } 932if (win == NULL ) { 933return ; 934 } 935 936list_T * const l = argvars [0 ].vval .v_list ; 937// To some extent make sure that we are dealing with a list from 938// "getmatches()". 939int li_idx = 0 ; 940TV_LIST_ITER_CONST (l ,li , { 941if (TV_LIST_ITEM_TV (li )-> v_type != VAR_DICT 942|| (d = TV_LIST_ITEM_TV (li )-> vval .v_dict )== NULL ) { 943semsg (_ ("E474: List item %d is either not a dictionary " 944"or an empty one" ),li_idx ); 945return ; 946 } 947if (!(tv_dict_find (d ,S_LEN ("group" ))!= NULL 948&& (tv_dict_find (d ,S_LEN ("pattern" ))!= NULL 949|| tv_dict_find (d ,S_LEN ("pos1" ))!= NULL ) 950&& tv_dict_find (d ,S_LEN ("priority" ))!= NULL 951&& tv_dict_find (d ,S_LEN ("id" ))!= NULL )) { 952semsg (_ ("E474: List item %d is missing one of the required keys" ), 953li_idx ); 954return ; 955 } 956li_idx ++ ; 957 }); 958 959clear_matches (win ); 960bool match_add_failed = false; 961TV_LIST_ITER_CONST (l ,li , { 962int i = 0 ; 963 964d = TV_LIST_ITEM_TV (li )-> vval .v_dict ; 965dictitem_T * const di = tv_dict_find (d ,S_LEN ("pattern" )); 966if (di == NULL ) { 967if (s == NULL ) { 968s = tv_list_alloc (9 ); 969tv_list_ref (s ); 970 } 971 972// match from matchaddpos() 973for (i = 1 ;i < 9 ;i ++ ) { 974char buf [30 ];// use 30 to avoid compiler warning 975snprintf (buf ,sizeof (buf ),"pos%d" ,i ); 976dictitem_T * const pos_di = tv_dict_find (d ,buf ,-1 ); 977if (pos_di != NULL ) { 978if (pos_di -> di_tv .v_type != VAR_LIST ) { 979tv_list_unref (s ); 980return ; 981 } 982 983tv_list_append_tv (s ,& pos_di -> di_tv ); 984 }else { 985break ; 986 } 987 } 988 } 989 990// Note: there are three number buffers involved: 991// - group_buf below. 992// - numbuf in tv_dict_get_string(). 993// - mybuf in tv_get_string(). 994// 995// If you change this code make sure that buffers will not get 996// accidentally reused. 997char group_buf [NUMBUFLEN ]; 998const char * const group = tv_dict_get_string_buf (d ,"group" ,group_buf ); 999const int priority = (int )tv_dict_get_number (d ,"priority" ); 1000const int id = (int )tv_dict_get_number (d ,"id" ); 1001dictitem_T * const conceal_di = tv_dict_find (d ,S_LEN ("conceal" )); 1002const char * const conceal = (conceal_di != NULL 1003 ?tv_get_string (& conceal_di -> di_tv ) 1004 :NULL ); 1005if (i == 0 ) { 1006if (match_add (win ,group , 1007tv_dict_get_string (d ,"pattern" , false), 1008priority ,id ,NULL ,conceal )!= id ) { 1009match_add_failed = true; 1010 } 1011 }else { 1012if (match_add (win ,group ,NULL ,priority ,id ,s ,conceal )!= id ) { 1013match_add_failed = true; 1014 } 1015tv_list_unref (s ); 1016s = NULL ; 1017 } 1018 }); 1019if (!match_add_failed ) { 1020rettv -> vval .v_number = 0 ; 1021 } 1022} 1023 1024/// "matchadd()" function 1025void f_matchadd (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1026{ 1027char grpbuf [NUMBUFLEN ]; 1028char patbuf [NUMBUFLEN ]; 1029// group 1030const char * const grp = tv_get_string_buf_chk (& argvars [0 ],grpbuf ); 1031// pattern 1032const char * const pat = tv_get_string_buf_chk (& argvars [1 ],patbuf ); 1033// default priority 1034int prio = 10 ; 1035int id = -1 ; 1036bool error = false; 1037const char * conceal_char = NULL ; 1038win_T * win = curwin ; 1039 1040rettv -> vval .v_number = -1 ; 1041 1042if (grp == NULL || pat == NULL ) { 1043return ; 1044 } 1045if (argvars [2 ].v_type != VAR_UNKNOWN ) { 1046prio = (int )tv_get_number_chk (& argvars [2 ],& error ); 1047if (argvars [3 ].v_type != VAR_UNKNOWN ) { 1048id = (int )tv_get_number_chk (& argvars [3 ],& error ); 1049if (argvars [4 ].v_type != VAR_UNKNOWN 1050&& matchadd_dict_arg (& argvars [4 ],& conceal_char ,& win )== FAIL ) { 1051return ; 1052 } 1053 } 1054 } 1055if (error ) { 1056return ; 1057 } 1058if (id >=1 && id <=3 ) { 1059semsg (_ ("E798: ID is reserved for \":match\": %d" ),id ); 1060return ; 1061 } 1062 1063rettv -> vval .v_number = match_add (win ,grp ,pat ,prio ,id ,NULL ,conceal_char ); 1064} 1065 1066/// "matchaddpo()" function 1067void f_matchaddpos (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1068{ 1069rettv -> vval .v_number = -1 ; 1070 1071char buf [NUMBUFLEN ]; 1072const char * const group = tv_get_string_buf_chk (& argvars [0 ],buf ); 1073if (group == NULL ) { 1074return ; 1075 } 1076 1077if (argvars [1 ].v_type != VAR_LIST ) { 1078semsg (_ (e_listarg ),"matchaddpos()" ); 1079return ; 1080 } 1081 1082list_T * l ; 1083l = argvars [1 ].vval .v_list ; 1084if (tv_list_len (l )== 0 ) { 1085return ; 1086 } 1087 1088bool error = false; 1089int prio = 10 ; 1090int id = -1 ; 1091const char * conceal_char = NULL ; 1092win_T * win = curwin ; 1093 1094if (argvars [2 ].v_type != VAR_UNKNOWN ) { 1095prio = (int )tv_get_number_chk (& argvars [2 ],& error ); 1096if (argvars [3 ].v_type != VAR_UNKNOWN ) { 1097id = (int )tv_get_number_chk (& argvars [3 ],& error ); 1098if (argvars [4 ].v_type != VAR_UNKNOWN 1099&& matchadd_dict_arg (& argvars [4 ],& conceal_char ,& win )== FAIL ) { 1100return ; 1101 } 1102 } 1103 } 1104if (error == true) { 1105return ; 1106 } 1107 1108// id == 3 is ok because matchaddpos() is supposed to substitute :3match 1109if (id == 1 || id == 2 ) { 1110semsg (_ ("E798: ID is reserved for \"match\": %d" ),id ); 1111return ; 1112 } 1113 1114rettv -> vval .v_number = match_add (win ,group ,NULL ,prio ,id ,l ,conceal_char ); 1115} 1116 1117/// "matcharg()" function 1118void f_matcharg (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1119{ 1120const int id = (int )tv_get_number (& argvars [0 ]); 1121 1122tv_list_alloc_ret (rettv , (id >=1 && id <=3 1123 ?2 1124 :0 )); 1125 1126if (id >=1 && id <=3 ) { 1127matchitem_T * const m = get_match (curwin ,id ); 1128 1129if (m != NULL ) { 1130tv_list_append_string (rettv -> vval .v_list ,syn_id2name (m -> mit_hlg_id ),-1 ); 1131tv_list_append_string (rettv -> vval .v_list ,m -> mit_pattern ,-1 ); 1132 }else { 1133tv_list_append_string (rettv -> vval .v_list ,NULL ,0 ); 1134tv_list_append_string (rettv -> vval .v_list ,NULL ,0 ); 1135 } 1136 } 1137} 1138 1139/// "matchdelete()" function 1140void f_matchdelete (typval_T * argvars ,typval_T * rettv ,EvalFuncData fptr ) 1141{ 1142win_T * win = get_optional_window (argvars ,1 ); 1143if (win == NULL ) { 1144rettv -> vval .v_number = -1 ; 1145 }else { 1146rettv -> vval .v_number = match_delete (win , 1147 (int )tv_get_number (& argvars [0 ]), true); 1148 } 1149} 1150 1151/// ":[N]match {group} {pattern}" 1152/// Sets nextcmd to the start of the next command, if any. Also called when 1153/// skipping commands to find the next command. 1154void ex_match (exarg_T * eap ) 1155{ 1156char * g = NULL ; 1157char * end ; 1158int id ; 1159 1160if (eap -> line2 <=3 ) { 1161id = (int )eap -> line2 ; 1162 }else { 1163emsg (e_invcmd ); 1164return ; 1165 } 1166 1167// First clear any old pattern. 1168if (!eap -> skip ) { 1169match_delete (curwin ,id , false); 1170 } 1171 1172if (ends_excmd (* eap -> arg )) { 1173end = eap -> arg ; 1174 }else if ((STRNICMP (eap -> arg ,"none" ,4 )== 0 1175&& (ascii_iswhite (eap -> arg [4 ])|| ends_excmd (eap -> arg [4 ])))) { 1176end = eap -> arg + 4 ; 1177 }else { 1178char * p = skiptowhite (eap -> arg ); 1179if (!eap -> skip ) { 1180g = xmemdupz (eap -> arg , (size_t )(p - eap -> arg )); 1181 } 1182p = skipwhite (p ); 1183if (* p == NUL ) { 1184// There must be two arguments. 1185xfree (g ); 1186semsg (_ (e_invarg2 ),eap -> arg ); 1187return ; 1188 } 1189end = skip_regexp (p + 1 ,* p , true); 1190if (!eap -> skip ) { 1191if (* end != NUL && !ends_excmd (* skipwhite (end + 1 ))) { 1192xfree (g ); 1193eap -> errmsg = ex_errmsg (e_trailing_arg ,end ); 1194return ; 1195 } 1196if (* end != * p ) { 1197xfree (g ); 1198semsg (_ (e_invarg2 ),p ); 1199return ; 1200 } 1201 1202int c = (uint8_t )(* end ); 1203* end = NUL ; 1204match_add (curwin ,g ,p + 1 ,10 ,id ,NULL ,NULL ); 1205xfree (g ); 1206* end = (char )c ; 1207 } 1208 } 1209eap -> nextcmd = find_nextcmd (end ); 1210}