Bram Moolenaar | edf3f97 | 2016-08-29 22:49:24 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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 | * search.c: code for normal mode searching commands |
| 11 | */ |
| 12 | |
| 13 | #include "vim.h" |
| 14 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 15 | #ifdef FEAT_EVAL |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 16 | static void set_vv_searchforward(void); |
| 17 | static int first_submatch(regmmatch_T *rp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 18 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 19 | #ifdef FEAT_FIND_ID |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 20 | static char_u *get_line_and_copy(linenr_T lnum, char_u *buf); |
| 21 | static void show_pat_in_path(char_u *, int, int, int, FILE *, linenr_T *, long); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 22 | #endif |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 23 | |
| 24 | typedef struct searchstat |
| 25 | { |
| 26 | int cur; // current position of found words |
| 27 | int cnt; // total count of found words |
| 28 | int exact_match; // TRUE if matched exactly on specified position |
| 29 | int incomplete; // 0: search was fully completed |
| 30 | // 1: recomputing was timed out |
| 31 | // 2: max count exceeded |
| 32 | int last_maxcount; // the max count of the last search |
| 33 | } searchstat_T; |
| 34 | |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 35 | #ifdef FEAT_SEARCH_EXTRA |
| 36 | static void save_incsearch_state(void); |
| 37 | static void restore_incsearch_state(void); |
| 38 | #endif |
| 39 | static int check_prevcol(char_u *linep, int col, int ch, int *prevcol); |
| 40 | static int find_rawstring_end(char_u *linep, pos_T *startpos, pos_T *endpos); |
| 41 | static void find_mps_values(int *initc, int *findc, int *backwards, int switchit); |
| 42 | static int is_zero_width(char_u *pattern, size_t patternlen, int move, pos_T *cur, int direction); |
| 43 | static void cmdline_search_stat(int dirc, pos_T *pos, pos_T *cursor_pos, int show_top_bot_msg, char_u *msgbuf, size_t msgbuflen, int recompute, int maxcount, long timeout); |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 44 | static void update_search_stat(int dirc, pos_T *pos, pos_T *cursor_pos, searchstat_T *stat, int recompute, int maxcount, long timeout); |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 45 | static int fuzzy_match_compute_score(char_u *str, int strSz, int_u *matches, int numMatches); |
| 46 | static int fuzzy_match_recursive(char_u *fuzpat, char_u *str, int_u strIdx, int *outScore, char_u *strBegin, int strLen, int_u *srcMatches, int_u *matches, int maxMatches, int nextMatch, int *recursionCount); |
| 47 | #if defined(FEAT_EVAL) || defined(FEAT_PROTO) |
| 48 | static int fuzzy_match_item_compare(const void *s1, const void *s2); |
| 49 | static void fuzzy_match_in_list(list_T *l, char_u *str, int matchseq, char_u *key, callback_T *item_cb, int retmatchpos, list_T *fmatchlist, long max_matches); |
| 50 | static void do_fuzzymatch(typval_T *argvars, typval_T *rettv, int retmatchpos); |
| 51 | #endif |
| 52 | static int fuzzy_match_str_compare(const void *s1, const void *s2); |
| 53 | static void fuzzy_match_str_sort(fuzmatch_str_T *fm, int sz); |
| 54 | static int fuzzy_match_func_compare(const void *s1, const void *s2); |
| 55 | static void fuzzy_match_func_sort(fuzmatch_str_T *fm, int sz); |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 56 | static int fuzzy_match_str_in_line(char_u **ptr, char_u *pat, int *len, pos_T *current_pos); |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 57 | |
Bram Moolenaar | ea6561a | 2020-06-01 21:32:45 +0200 | [diff] [blame] | 58 | #define SEARCH_STAT_DEF_TIMEOUT 40L |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 59 | #define SEARCH_STAT_DEF_MAX_COUNT 99 |
| 60 | #define SEARCH_STAT_BUF_LEN 12 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 61 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 62 | /* |
| 63 | * This file contains various searching-related routines. These fall into |
| 64 | * three groups: |
| 65 | * 1. string searches (for /, ?, n, and N) |
| 66 | * 2. character searches within a single line (for f, F, t, T, etc) |
| 67 | * 3. "other" kinds of searches like the '%' command, and 'word' searches. |
| 68 | */ |
| 69 | |
| 70 | /* |
| 71 | * String searches |
| 72 | * |
| 73 | * The string search functions are divided into two levels: |
| 74 | * lowest: searchit(); uses an pos_T for starting position and found match. |
| 75 | * Highest: do_search(); uses curwin->w_cursor; calls searchit(). |
| 76 | * |
| 77 | * The last search pattern is remembered for repeating the same search. |
| 78 | * This pattern is shared between the :g, :s, ? and / commands. |
| 79 | * This is in search_regcomp(). |
| 80 | * |
| 81 | * The actual string matching is done using a heavily modified version of |
| 82 | * Henry Spencer's regular expression library. See regexp.c. |
| 83 | */ |
| 84 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 85 | /* |
| 86 | * Two search patterns are remembered: One for the :substitute command and |
| 87 | * one for other searches. last_idx points to the one that was used the last |
| 88 | * time. |
| 89 | */ |
Bram Moolenaar | c332816 | 2019-07-23 22:15:25 +0200 | [diff] [blame] | 90 | static spat_T spats[2] = |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 91 | { |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 92 | {NULL, 0, TRUE, FALSE, {'/', 0, 0, 0L}}, // last used search pat |
| 93 | {NULL, 0, TRUE, FALSE, {'/', 0, 0, 0L}} // last used substitute pat |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 96 | static int last_idx = 0; // index in spats[] for RE_LAST |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 97 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 98 | static char_u lastc[2] = {NUL, NUL}; // last character searched for |
| 99 | static int lastcdir = FORWARD; // last direction of character search |
| 100 | static int last_t_cmd = TRUE; // last search t_cmd |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 101 | static char_u lastc_bytes[MB_MAXBYTES + 1]; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 102 | static int lastc_bytelen = 1; // >1 for multi-byte char |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 103 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 104 | // copy of spats[], for keeping the search patterns while executing autocmds |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 105 | static spat_T saved_spats[ARRAY_LENGTH(spats)]; |
Bram Moolenaar | a2cff1d | 2021-10-15 12:51:29 +0100 | [diff] [blame] | 106 | static char_u *saved_mr_pattern = NULL; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 107 | static size_t saved_mr_patternlen = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 108 | # ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | ed8bc78 | 2018-12-01 21:08:21 +0100 | [diff] [blame] | 109 | static int saved_spats_last_idx = 0; |
| 110 | static int saved_spats_no_hlsearch = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 111 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 112 | |
Bram Moolenaar | a2cff1d | 2021-10-15 12:51:29 +0100 | [diff] [blame] | 113 | // allocated copy of pattern used by search_regcomp() |
| 114 | static char_u *mr_pattern = NULL; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 115 | static size_t mr_patternlen = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 116 | |
| 117 | #ifdef FEAT_FIND_ID |
| 118 | /* |
| 119 | * Type used by find_pattern_in_path() to remember which included files have |
| 120 | * been searched already. |
| 121 | */ |
| 122 | typedef struct SearchedFile |
| 123 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 124 | FILE *fp; // File pointer |
| 125 | char_u *name; // Full name of file |
| 126 | linenr_T lnum; // Line we were up to in file |
| 127 | int matched; // Found a match in this file |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 128 | } SearchedFile; |
| 129 | #endif |
| 130 | |
| 131 | /* |
| 132 | * translate search pattern for vim_regcomp() |
| 133 | * |
| 134 | * pat_save == RE_SEARCH: save pat in spats[RE_SEARCH].pat (normal search cmd) |
| 135 | * pat_save == RE_SUBST: save pat in spats[RE_SUBST].pat (:substitute command) |
| 136 | * pat_save == RE_BOTH: save pat in both patterns (:global command) |
| 137 | * pat_use == RE_SEARCH: use previous search pattern if "pat" is NULL |
Bram Moolenaar | b8017e7 | 2007-05-10 18:59:07 +0000 | [diff] [blame] | 138 | * pat_use == RE_SUBST: use previous substitute pattern if "pat" is NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 139 | * pat_use == RE_LAST: use last used pattern if "pat" is NULL |
| 140 | * options & SEARCH_HIS: put search string in history |
| 141 | * options & SEARCH_KEEP: keep previous search pattern |
| 142 | * |
| 143 | * returns FAIL if failed, OK otherwise. |
| 144 | */ |
| 145 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 146 | search_regcomp( |
| 147 | char_u *pat, |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 148 | size_t patlen, |
Rob Pilling | e86190e | 2022-12-23 19:06:04 +0000 | [diff] [blame] | 149 | char_u **used_pat, |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 150 | int pat_save, |
| 151 | int pat_use, |
| 152 | int options, |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 153 | regmmatch_T *regmatch) // return: pattern and ignore-case flag |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 154 | { |
| 155 | int magic; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 156 | |
| 157 | rc_did_emsg = FALSE; |
Bram Moolenaar | f4e2099 | 2020-12-21 19:59:08 +0100 | [diff] [blame] | 158 | magic = magic_isset(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 159 | |
| 160 | /* |
| 161 | * If no pattern given, use a previously defined pattern. |
| 162 | */ |
| 163 | if (pat == NULL || *pat == NUL) |
| 164 | { |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 165 | int i; |
| 166 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 167 | if (pat_use == RE_LAST) |
| 168 | i = last_idx; |
| 169 | else |
| 170 | i = pat_use; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 171 | if (spats[i].pat == NULL) // pattern was never defined |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 172 | { |
| 173 | if (pat_use == RE_SUBST) |
Bram Moolenaar | e29a27f | 2021-07-20 21:07:36 +0200 | [diff] [blame] | 174 | emsg(_(e_no_previous_substitute_regular_expression)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 175 | else |
Bram Moolenaar | e29a27f | 2021-07-20 21:07:36 +0200 | [diff] [blame] | 176 | emsg(_(e_no_previous_regular_expression)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 177 | rc_did_emsg = TRUE; |
| 178 | return FAIL; |
| 179 | } |
| 180 | pat = spats[i].pat; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 181 | patlen = spats[i].patlen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 182 | magic = spats[i].magic; |
| 183 | no_smartcase = spats[i].no_scs; |
| 184 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 185 | else if (options & SEARCH_HIS) // put new pattern in history |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 186 | add_to_history(HIST_SEARCH, pat, patlen, TRUE, NUL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 187 | |
Rob Pilling | e86190e | 2022-12-23 19:06:04 +0000 | [diff] [blame] | 188 | if (used_pat) |
Bram Moolenaar | ebfec1c | 2023-01-22 21:14:53 +0000 | [diff] [blame] | 189 | *used_pat = pat; |
Rob Pilling | e86190e | 2022-12-23 19:06:04 +0000 | [diff] [blame] | 190 | |
Bram Moolenaar | a2cff1d | 2021-10-15 12:51:29 +0100 | [diff] [blame] | 191 | vim_free(mr_pattern); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 192 | #ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 193 | if (curwin->w_p_rl && *curwin->w_p_rlc == 's') |
Bram Moolenaar | a2cff1d | 2021-10-15 12:51:29 +0100 | [diff] [blame] | 194 | mr_pattern = reverse_text(pat); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 195 | else |
| 196 | #endif |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 197 | mr_pattern = vim_strnsave(pat, patlen); |
| 198 | if (mr_pattern == NULL) |
| 199 | mr_patternlen = 0; |
| 200 | else |
| 201 | mr_patternlen = patlen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 202 | |
| 203 | /* |
| 204 | * Save the currently used pattern in the appropriate place, |
| 205 | * unless the pattern should not be remembered. |
| 206 | */ |
Bram Moolenaar | e100440 | 2020-10-24 20:49:43 +0200 | [diff] [blame] | 207 | if (!(options & SEARCH_KEEP) |
| 208 | && (cmdmod.cmod_flags & CMOD_KEEPPATTERNS) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 209 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 210 | // search or global command |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 211 | if (pat_save == RE_SEARCH || pat_save == RE_BOTH) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 212 | save_re_pat(RE_SEARCH, pat, patlen, magic); |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 213 | // substitute or global command |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 214 | if (pat_save == RE_SUBST || pat_save == RE_BOTH) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 215 | save_re_pat(RE_SUBST, pat, patlen, magic); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | regmatch->rmm_ic = ignorecase(pat); |
Bram Moolenaar | 3b56eb3 | 2005-07-11 22:40:32 +0000 | [diff] [blame] | 219 | regmatch->rmm_maxcol = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 220 | regmatch->regprog = vim_regcomp(pat, magic ? RE_MAGIC : 0); |
| 221 | if (regmatch->regprog == NULL) |
| 222 | return FAIL; |
| 223 | return OK; |
| 224 | } |
| 225 | |
| 226 | /* |
| 227 | * Get search pattern used by search_regcomp(). |
| 228 | */ |
| 229 | char_u * |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 230 | get_search_pat(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 231 | { |
| 232 | return mr_pattern; |
| 233 | } |
| 234 | |
Bram Moolenaar | cc2b9d5 | 2014-12-13 03:17:11 +0100 | [diff] [blame] | 235 | void |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 236 | save_re_pat(int idx, char_u *pat, size_t patlen, int magic) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 237 | { |
Yegappan Lakshmanan | 6ec6666 | 2023-01-23 20:46:21 +0000 | [diff] [blame] | 238 | if (spats[idx].pat == pat) |
| 239 | return; |
| 240 | |
| 241 | vim_free(spats[idx].pat); |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 242 | spats[idx].pat = vim_strnsave(pat, patlen); |
| 243 | if (spats[idx].pat == NULL) |
| 244 | spats[idx].patlen = 0; |
| 245 | else |
| 246 | spats[idx].patlen = patlen; |
Yegappan Lakshmanan | 6ec6666 | 2023-01-23 20:46:21 +0000 | [diff] [blame] | 247 | spats[idx].magic = magic; |
| 248 | spats[idx].no_scs = no_smartcase; |
| 249 | last_idx = idx; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 250 | #ifdef FEAT_SEARCH_EXTRA |
Yegappan Lakshmanan | 6ec6666 | 2023-01-23 20:46:21 +0000 | [diff] [blame] | 251 | // If 'hlsearch' set and search pat changed: need redraw. |
| 252 | if (p_hls) |
| 253 | redraw_all_later(UPD_SOME_VALID); |
| 254 | set_no_hlsearch(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 255 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 258 | /* |
| 259 | * Save the search patterns, so they can be restored later. |
| 260 | * Used before/after executing autocommands and user functions. |
| 261 | */ |
| 262 | static int save_level = 0; |
| 263 | |
| 264 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 265 | save_search_patterns(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 266 | { |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 267 | int i; |
| 268 | |
Yegappan Lakshmanan | 6ec6666 | 2023-01-23 20:46:21 +0000 | [diff] [blame] | 269 | if (save_level++ != 0) |
| 270 | return; |
| 271 | |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 272 | for (i = 0; i < (int)ARRAY_LENGTH(spats); ++i) |
| 273 | { |
| 274 | saved_spats[i] = spats[i]; |
| 275 | if (spats[i].pat != NULL) |
| 276 | { |
| 277 | saved_spats[i].pat = vim_strnsave(spats[i].pat, spats[i].patlen); |
| 278 | if (saved_spats[i].pat == NULL) |
| 279 | saved_spats[i].patlen = 0; |
| 280 | else |
| 281 | saved_spats[i].patlen = spats[i].patlen; |
| 282 | } |
| 283 | } |
Yegappan Lakshmanan | 6ec6666 | 2023-01-23 20:46:21 +0000 | [diff] [blame] | 284 | if (mr_pattern == NULL) |
| 285 | saved_mr_pattern = NULL; |
| 286 | else |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 287 | saved_mr_pattern = vim_strnsave(mr_pattern, mr_patternlen); |
| 288 | if (saved_mr_pattern == NULL) |
| 289 | saved_mr_patternlen = 0; |
| 290 | else |
| 291 | saved_mr_patternlen = mr_patternlen; |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 292 | #ifdef FEAT_SEARCH_EXTRA |
Yegappan Lakshmanan | 6ec6666 | 2023-01-23 20:46:21 +0000 | [diff] [blame] | 293 | saved_spats_last_idx = last_idx; |
| 294 | saved_spats_no_hlsearch = no_hlsearch; |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 295 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 299 | restore_search_patterns(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 300 | { |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 301 | int i; |
| 302 | |
Yegappan Lakshmanan | 6ec6666 | 2023-01-23 20:46:21 +0000 | [diff] [blame] | 303 | if (--save_level != 0) |
| 304 | return; |
| 305 | |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 306 | for (i = 0; i < (int)ARRAY_LENGTH(spats); ++i) |
| 307 | { |
| 308 | vim_free(spats[i].pat); |
| 309 | spats[i] = saved_spats[i]; |
| 310 | } |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 311 | #if defined(FEAT_EVAL) |
Yegappan Lakshmanan | 6ec6666 | 2023-01-23 20:46:21 +0000 | [diff] [blame] | 312 | set_vv_searchforward(); |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 313 | #endif |
Yegappan Lakshmanan | 6ec6666 | 2023-01-23 20:46:21 +0000 | [diff] [blame] | 314 | vim_free(mr_pattern); |
| 315 | mr_pattern = saved_mr_pattern; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 316 | mr_patternlen = saved_mr_patternlen; |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 317 | #ifdef FEAT_SEARCH_EXTRA |
Yegappan Lakshmanan | 6ec6666 | 2023-01-23 20:46:21 +0000 | [diff] [blame] | 318 | last_idx = saved_spats_last_idx; |
| 319 | set_no_hlsearch(saved_spats_no_hlsearch); |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 320 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 321 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 322 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 323 | #if defined(EXITFREE) || defined(PROTO) |
| 324 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 325 | free_search_patterns(void) |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 326 | { |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 327 | int i; |
| 328 | |
| 329 | for (i = 0; i < (int)ARRAY_LENGTH(spats); ++i) |
| 330 | { |
| 331 | VIM_CLEAR(spats[i].pat); |
| 332 | spats[i].patlen = 0; |
| 333 | } |
Bram Moolenaar | a2cff1d | 2021-10-15 12:51:29 +0100 | [diff] [blame] | 334 | VIM_CLEAR(mr_pattern); |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 335 | mr_patternlen = 0; |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 336 | } |
| 337 | #endif |
| 338 | |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 339 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | ed8bc78 | 2018-12-01 21:08:21 +0100 | [diff] [blame] | 340 | // copy of spats[RE_SEARCH], for keeping the search patterns while incremental |
| 341 | // searching |
Bram Moolenaar | c332816 | 2019-07-23 22:15:25 +0200 | [diff] [blame] | 342 | static spat_T saved_last_search_spat; |
Bram Moolenaar | ed8bc78 | 2018-12-01 21:08:21 +0100 | [diff] [blame] | 343 | static int did_save_last_search_spat = 0; |
| 344 | static int saved_last_idx = 0; |
| 345 | static int saved_no_hlsearch = 0; |
Christian Brabandt | 6dd7424 | 2022-02-14 12:44:32 +0000 | [diff] [blame] | 346 | static int saved_search_match_endcol; |
| 347 | static int saved_search_match_lines; |
Bram Moolenaar | ed8bc78 | 2018-12-01 21:08:21 +0100 | [diff] [blame] | 348 | |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 349 | /* |
| 350 | * Save and restore the search pattern for incremental highlight search |
| 351 | * feature. |
| 352 | * |
Bram Moolenaar | c4568ab | 2018-11-16 16:21:05 +0100 | [diff] [blame] | 353 | * It's similar to but different from save_search_patterns() and |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 354 | * restore_search_patterns(), because the search pattern must be restored when |
Bram Moolenaar | c4568ab | 2018-11-16 16:21:05 +0100 | [diff] [blame] | 355 | * canceling incremental searching even if it's called inside user functions. |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 356 | */ |
| 357 | void |
| 358 | save_last_search_pattern(void) |
| 359 | { |
Bram Moolenaar | 442a853 | 2020-06-04 20:56:09 +0200 | [diff] [blame] | 360 | if (++did_save_last_search_spat != 1) |
| 361 | // nested call, nothing to do |
| 362 | return; |
Bram Moolenaar | 01a060d | 2018-11-30 21:57:55 +0100 | [diff] [blame] | 363 | |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 364 | saved_last_search_spat = spats[RE_SEARCH]; |
| 365 | if (spats[RE_SEARCH].pat != NULL) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 366 | { |
| 367 | saved_last_search_spat.pat = vim_strnsave(spats[RE_SEARCH].pat, spats[RE_SEARCH].patlen); |
| 368 | if (saved_last_search_spat.pat == NULL) |
| 369 | saved_last_search_spat.patlen = 0; |
| 370 | else |
| 371 | saved_last_search_spat.patlen = spats[RE_SEARCH].patlen; |
| 372 | } |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 373 | saved_last_idx = last_idx; |
| 374 | saved_no_hlsearch = no_hlsearch; |
| 375 | } |
| 376 | |
| 377 | void |
| 378 | restore_last_search_pattern(void) |
| 379 | { |
Bram Moolenaar | 442a853 | 2020-06-04 20:56:09 +0200 | [diff] [blame] | 380 | if (--did_save_last_search_spat > 0) |
| 381 | // nested call, nothing to do |
| 382 | return; |
| 383 | if (did_save_last_search_spat != 0) |
Bram Moolenaar | 01a060d | 2018-11-30 21:57:55 +0100 | [diff] [blame] | 384 | { |
Bram Moolenaar | 442a853 | 2020-06-04 20:56:09 +0200 | [diff] [blame] | 385 | iemsg("restore_last_search_pattern() called more often than save_last_search_pattern()"); |
Bram Moolenaar | 01a060d | 2018-11-30 21:57:55 +0100 | [diff] [blame] | 386 | return; |
| 387 | } |
Bram Moolenaar | 01a060d | 2018-11-30 21:57:55 +0100 | [diff] [blame] | 388 | |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 389 | vim_free(spats[RE_SEARCH].pat); |
| 390 | spats[RE_SEARCH] = saved_last_search_spat; |
Bram Moolenaar | 01a060d | 2018-11-30 21:57:55 +0100 | [diff] [blame] | 391 | saved_last_search_spat.pat = NULL; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 392 | saved_last_search_spat.patlen = 0; |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 393 | # if defined(FEAT_EVAL) |
| 394 | set_vv_searchforward(); |
| 395 | # endif |
| 396 | last_idx = saved_last_idx; |
Bram Moolenaar | 451fc7b | 2018-04-27 22:53:07 +0200 | [diff] [blame] | 397 | set_no_hlsearch(saved_no_hlsearch); |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 398 | } |
Bram Moolenaar | d048009 | 2017-11-16 22:20:39 +0100 | [diff] [blame] | 399 | |
Christian Brabandt | 6dd7424 | 2022-02-14 12:44:32 +0000 | [diff] [blame] | 400 | /* |
| 401 | * Save and restore the incsearch highlighting variables. |
| 402 | * This is required so that calling searchcount() at does not invalidate the |
| 403 | * incsearch highlighting. |
| 404 | */ |
| 405 | static void |
| 406 | save_incsearch_state(void) |
| 407 | { |
| 408 | saved_search_match_endcol = search_match_endcol; |
| 409 | saved_search_match_lines = search_match_lines; |
| 410 | } |
| 411 | |
| 412 | static void |
| 413 | restore_incsearch_state(void) |
| 414 | { |
| 415 | search_match_endcol = saved_search_match_endcol; |
| 416 | search_match_lines = saved_search_match_lines; |
| 417 | } |
| 418 | |
Bram Moolenaar | d048009 | 2017-11-16 22:20:39 +0100 | [diff] [blame] | 419 | char_u * |
| 420 | last_search_pattern(void) |
| 421 | { |
| 422 | return spats[RE_SEARCH].pat; |
| 423 | } |
John Marriott | ccf8907 | 2024-10-07 21:40:39 +0200 | [diff] [blame] | 424 | |
| 425 | size_t |
| 426 | last_search_pattern_len(void) |
| 427 | { |
| 428 | return spats[RE_SEARCH].patlen; |
| 429 | } |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 430 | #endif |
| 431 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 432 | /* |
| 433 | * Return TRUE when case should be ignored for search pattern "pat". |
| 434 | * Uses the 'ignorecase' and 'smartcase' options. |
| 435 | */ |
| 436 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 437 | ignorecase(char_u *pat) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 438 | { |
Bram Moolenaar | 66e29d7 | 2016-08-20 16:57:02 +0200 | [diff] [blame] | 439 | return ignorecase_opt(pat, p_ic, p_scs); |
| 440 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 441 | |
Bram Moolenaar | 66e29d7 | 2016-08-20 16:57:02 +0200 | [diff] [blame] | 442 | /* |
| 443 | * As ignorecase() put pass the "ic" and "scs" flags. |
| 444 | */ |
| 445 | int |
| 446 | ignorecase_opt(char_u *pat, int ic_in, int scs) |
| 447 | { |
| 448 | int ic = ic_in; |
| 449 | |
| 450 | if (ic && !no_smartcase && scs |
Bram Moolenaar | e2c453d | 2019-08-21 14:37:09 +0200 | [diff] [blame] | 451 | && !(ctrl_x_mode_not_default() && curbuf->b_p_inf)) |
Bram Moolenaar | a9dc375 | 2010-07-11 20:46:53 +0200 | [diff] [blame] | 452 | ic = !pat_has_uppercase(pat); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 453 | no_smartcase = FALSE; |
| 454 | |
| 455 | return ic; |
| 456 | } |
| 457 | |
Bram Moolenaar | a9dc375 | 2010-07-11 20:46:53 +0200 | [diff] [blame] | 458 | /* |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 459 | * Return TRUE if pattern "pat" has an uppercase character. |
Bram Moolenaar | a9dc375 | 2010-07-11 20:46:53 +0200 | [diff] [blame] | 460 | */ |
| 461 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 462 | pat_has_uppercase(char_u *pat) |
Bram Moolenaar | a9dc375 | 2010-07-11 20:46:53 +0200 | [diff] [blame] | 463 | { |
| 464 | char_u *p = pat; |
Christian Brabandt | 78ba933 | 2021-08-01 12:44:37 +0200 | [diff] [blame] | 465 | magic_T magic_val = MAGIC_ON; |
| 466 | |
| 467 | // get the magicness of the pattern |
| 468 | (void)skip_regexp_ex(pat, NUL, magic_isset(), NULL, NULL, &magic_val); |
Bram Moolenaar | a9dc375 | 2010-07-11 20:46:53 +0200 | [diff] [blame] | 469 | |
| 470 | while (*p != NUL) |
| 471 | { |
Bram Moolenaar | a9dc375 | 2010-07-11 20:46:53 +0200 | [diff] [blame] | 472 | int l; |
| 473 | |
| 474 | if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) |
| 475 | { |
| 476 | if (enc_utf8 && utf_isupper(utf_ptr2char(p))) |
| 477 | return TRUE; |
| 478 | p += l; |
| 479 | } |
Christian Brabandt | bc67e5a | 2021-08-05 15:24:59 +0200 | [diff] [blame] | 480 | else if (*p == '\\' && magic_val <= MAGIC_ON) |
Bram Moolenaar | a9dc375 | 2010-07-11 20:46:53 +0200 | [diff] [blame] | 481 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 482 | if (p[1] == '_' && p[2] != NUL) // skip "\_X" |
Bram Moolenaar | a9dc375 | 2010-07-11 20:46:53 +0200 | [diff] [blame] | 483 | p += 3; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 484 | else if (p[1] == '%' && p[2] != NUL) // skip "\%X" |
Bram Moolenaar | a9dc375 | 2010-07-11 20:46:53 +0200 | [diff] [blame] | 485 | p += 3; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 486 | else if (p[1] != NUL) // skip "\X" |
Bram Moolenaar | a9dc375 | 2010-07-11 20:46:53 +0200 | [diff] [blame] | 487 | p += 2; |
| 488 | else |
| 489 | p += 1; |
| 490 | } |
Christian Brabandt | 78ba933 | 2021-08-01 12:44:37 +0200 | [diff] [blame] | 491 | else if ((*p == '%' || *p == '_') && magic_val == MAGIC_ALL) |
| 492 | { |
| 493 | if (p[1] != NUL) // skip "_X" and %X |
| 494 | p += 2; |
Christian Brabandt | bc67e5a | 2021-08-05 15:24:59 +0200 | [diff] [blame] | 495 | else |
| 496 | p++; |
Christian Brabandt | 78ba933 | 2021-08-01 12:44:37 +0200 | [diff] [blame] | 497 | } |
Bram Moolenaar | a9dc375 | 2010-07-11 20:46:53 +0200 | [diff] [blame] | 498 | else if (MB_ISUPPER(*p)) |
| 499 | return TRUE; |
| 500 | else |
| 501 | ++p; |
| 502 | } |
| 503 | return FALSE; |
| 504 | } |
| 505 | |
Bram Moolenaar | 113e107 | 2019-01-20 15:30:40 +0100 | [diff] [blame] | 506 | #if defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 507 | char_u * |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 508 | last_csearch(void) |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 509 | { |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 510 | return lastc_bytes; |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 514 | last_csearch_forward(void) |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 515 | { |
| 516 | return lastcdir == FORWARD; |
| 517 | } |
| 518 | |
| 519 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 520 | last_csearch_until(void) |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 521 | { |
| 522 | return last_t_cmd == TRUE; |
| 523 | } |
| 524 | |
| 525 | void |
zeertzjq | e5d91ba | 2023-05-14 17:39:18 +0100 | [diff] [blame] | 526 | set_last_csearch(int c, char_u *s, int len) |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 527 | { |
| 528 | *lastc = c; |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 529 | lastc_bytelen = len; |
| 530 | if (len) |
| 531 | memcpy(lastc_bytes, s, len); |
| 532 | else |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 533 | CLEAR_FIELD(lastc_bytes); |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 534 | } |
Bram Moolenaar | 113e107 | 2019-01-20 15:30:40 +0100 | [diff] [blame] | 535 | #endif |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 536 | |
| 537 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 538 | set_csearch_direction(int cdir) |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 539 | { |
| 540 | lastcdir = cdir; |
| 541 | } |
| 542 | |
| 543 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 544 | set_csearch_until(int t_cmd) |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 545 | { |
| 546 | last_t_cmd = t_cmd; |
| 547 | } |
| 548 | |
| 549 | char_u * |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 550 | last_search_pat(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 551 | { |
| 552 | return spats[last_idx].pat; |
| 553 | } |
| 554 | |
| 555 | /* |
| 556 | * Reset search direction to forward. For "gd" and "gD" commands. |
| 557 | */ |
| 558 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 559 | reset_search_dir(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 560 | { |
| 561 | spats[0].off.dir = '/'; |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 562 | #if defined(FEAT_EVAL) |
| 563 | set_vv_searchforward(); |
| 564 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | #if defined(FEAT_EVAL) || defined(FEAT_VIMINFO) |
| 568 | /* |
| 569 | * Set the last search pattern. For ":let @/ =" and viminfo. |
| 570 | * Also set the saved search pattern, so that this works in an autocommand. |
| 571 | */ |
| 572 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 573 | set_last_search_pat( |
| 574 | char_u *s, |
| 575 | int idx, |
| 576 | int magic, |
| 577 | int setlast) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 578 | { |
| 579 | vim_free(spats[idx].pat); |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 580 | // An empty string means that nothing should be matched. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 581 | if (*s == NUL) |
| 582 | spats[idx].pat = NULL; |
| 583 | else |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 584 | { |
| 585 | spats[idx].patlen = STRLEN(s); |
| 586 | spats[idx].pat = vim_strnsave(s, spats[idx].patlen); |
| 587 | } |
| 588 | if (spats[idx].pat == NULL) |
| 589 | spats[idx].patlen = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 590 | spats[idx].magic = magic; |
| 591 | spats[idx].no_scs = FALSE; |
| 592 | spats[idx].off.dir = '/'; |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 593 | #if defined(FEAT_EVAL) |
| 594 | set_vv_searchforward(); |
| 595 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 596 | spats[idx].off.line = FALSE; |
| 597 | spats[idx].off.end = FALSE; |
| 598 | spats[idx].off.off = 0; |
| 599 | if (setlast) |
| 600 | last_idx = idx; |
| 601 | if (save_level) |
| 602 | { |
| 603 | vim_free(saved_spats[idx].pat); |
| 604 | saved_spats[idx] = spats[0]; |
| 605 | if (spats[idx].pat == NULL) |
| 606 | saved_spats[idx].pat = NULL; |
| 607 | else |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 608 | saved_spats[idx].pat = vim_strnsave(spats[idx].pat, spats[idx].patlen); |
| 609 | if (saved_spats[idx].pat == NULL) |
| 610 | saved_spats[idx].patlen = 0; |
| 611 | else |
| 612 | saved_spats[idx].patlen = spats[idx].patlen; |
Bram Moolenaar | 975880b | 2019-03-03 14:42:11 +0100 | [diff] [blame] | 613 | # ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | ed8bc78 | 2018-12-01 21:08:21 +0100 | [diff] [blame] | 614 | saved_spats_last_idx = last_idx; |
Bram Moolenaar | 975880b | 2019-03-03 14:42:11 +0100 | [diff] [blame] | 615 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 616 | } |
| 617 | # ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 618 | // If 'hlsearch' set and search pat changed: need redraw. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 619 | if (p_hls && idx == last_idx && !no_hlsearch) |
Bram Moolenaar | a4d158b | 2022-08-14 14:17:45 +0100 | [diff] [blame] | 620 | redraw_all_later(UPD_SOME_VALID); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 621 | # endif |
| 622 | } |
| 623 | #endif |
| 624 | |
| 625 | #ifdef FEAT_SEARCH_EXTRA |
| 626 | /* |
| 627 | * Get a regexp program for the last used search pattern. |
| 628 | * This is used for highlighting all matches in a window. |
| 629 | * Values returned in regmatch->regprog and regmatch->rmm_ic. |
| 630 | */ |
| 631 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 632 | last_pat_prog(regmmatch_T *regmatch) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 633 | { |
| 634 | if (spats[last_idx].pat == NULL) |
| 635 | { |
| 636 | regmatch->regprog = NULL; |
| 637 | return; |
| 638 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 639 | ++emsg_off; // So it doesn't beep if bad expr |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 640 | (void)search_regcomp((char_u *)"", 0, NULL, 0, last_idx, SEARCH_KEEP, regmatch); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 641 | --emsg_off; |
| 642 | } |
| 643 | #endif |
| 644 | |
| 645 | /* |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 646 | * Lowest level search function. |
Bram Moolenaar | 5d24a22 | 2018-12-23 19:10:09 +0100 | [diff] [blame] | 647 | * Search for 'count'th occurrence of pattern "pat" in direction "dir". |
| 648 | * Start at position "pos" and return the found position in "pos". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 649 | * |
| 650 | * if (options & SEARCH_MSG) == 0 don't give any messages |
| 651 | * if (options & SEARCH_MSG) == SEARCH_NFMSG don't give 'notfound' messages |
| 652 | * if (options & SEARCH_MSG) == SEARCH_MSG give all messages |
| 653 | * if (options & SEARCH_HIS) put search pattern in history |
| 654 | * if (options & SEARCH_END) return position at end of match |
| 655 | * if (options & SEARCH_START) accept match at pos itself |
| 656 | * if (options & SEARCH_KEEP) keep previous search pattern |
| 657 | * if (options & SEARCH_FOLD) match only once in a closed fold |
| 658 | * if (options & SEARCH_PEEK) check for typed char, cancel search |
Bram Moolenaar | ad4d8a1 | 2015-12-28 19:20:36 +0100 | [diff] [blame] | 659 | * if (options & SEARCH_COL) start at pos->col instead of zero |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 660 | * |
| 661 | * Return FAIL (zero) for failure, non-zero for success. |
| 662 | * When FEAT_EVAL is defined, returns the index of the first matching |
| 663 | * subpattern plus one; one if there was none. |
| 664 | */ |
| 665 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 666 | searchit( |
Bram Moolenaar | 92ea26b | 2019-10-18 20:53:34 +0200 | [diff] [blame] | 667 | win_T *win, // window to search in; can be NULL for a |
| 668 | // buffer without a window! |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 669 | buf_T *buf, |
| 670 | pos_T *pos, |
Bram Moolenaar | 5d24a22 | 2018-12-23 19:10:09 +0100 | [diff] [blame] | 671 | pos_T *end_pos, // set to end of the match, unless NULL |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 672 | int dir, |
| 673 | char_u *pat, |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 674 | size_t patlen, |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 675 | long count, |
| 676 | int options, |
Bram Moolenaar | 92ea26b | 2019-10-18 20:53:34 +0200 | [diff] [blame] | 677 | int pat_use, // which pattern to use when "pat" is empty |
| 678 | searchit_arg_T *extra_arg) // optional extra arguments, can be NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 679 | { |
| 680 | int found; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 681 | linenr_T lnum; // no init to shut up Apollo cc |
Bram Moolenaar | ad4d8a1 | 2015-12-28 19:20:36 +0100 | [diff] [blame] | 682 | colnr_T col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 683 | regmmatch_T regmatch; |
| 684 | char_u *ptr; |
| 685 | colnr_T matchcol; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 686 | lpos_T endpos; |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 687 | lpos_T matchpos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 688 | int loop; |
| 689 | pos_T start_pos; |
| 690 | int at_first_line; |
| 691 | int extra_col; |
Bram Moolenaar | 5f1e68b | 2015-07-10 14:43:35 +0200 | [diff] [blame] | 692 | int start_char_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 693 | int match_ok; |
| 694 | long nmatched; |
| 695 | int submatch = 0; |
Bram Moolenaar | a3dfccc | 2014-11-27 17:29:56 +0100 | [diff] [blame] | 696 | int first_match = TRUE; |
Bram Moolenaar | 5398955 | 2019-12-23 22:59:18 +0100 | [diff] [blame] | 697 | int called_emsg_before = called_emsg; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 698 | #ifdef FEAT_SEARCH_EXTRA |
| 699 | int break_loop = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 700 | #endif |
Bram Moolenaar | 92ea26b | 2019-10-18 20:53:34 +0200 | [diff] [blame] | 701 | linenr_T stop_lnum = 0; // stop after this line number when != 0 |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 702 | int unused_timeout_flag = FALSE; |
| 703 | int *timed_out = &unused_timeout_flag; // set when timed out. |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 704 | int search_from_match_end; // vi-compatible search? |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 705 | |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 706 | if (search_regcomp(pat, patlen, NULL, RE_SEARCH, pat_use, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 707 | (options & (SEARCH_HIS + SEARCH_KEEP)), ®match) == FAIL) |
| 708 | { |
| 709 | if ((options & SEARCH_MSG) && !rc_did_emsg) |
Bram Moolenaar | ac78dd4 | 2022-01-02 19:25:26 +0000 | [diff] [blame] | 710 | semsg(_(e_invalid_search_string_str), mr_pattern); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 711 | return FAIL; |
| 712 | } |
| 713 | |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 714 | search_from_match_end = vim_strchr(p_cpo, CPO_SEARCH) != NULL; |
| 715 | |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 716 | if (extra_arg != NULL) |
| 717 | { |
| 718 | stop_lnum = extra_arg->sa_stop_lnum; |
| 719 | #ifdef FEAT_RELTIME |
| 720 | if (extra_arg->sa_tm > 0) |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 721 | init_regexp_timeout(extra_arg->sa_tm); |
Bram Moolenaar | 5ea38d1 | 2022-06-16 21:20:48 +0100 | [diff] [blame] | 722 | // Also set the pointer when sa_tm is zero, the caller may have set the |
| 723 | // timeout. |
| 724 | timed_out = &extra_arg->sa_timed_out; |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 725 | #endif |
| 726 | } |
| 727 | |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 728 | /* |
| 729 | * find the string |
| 730 | */ |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 731 | do // loop for count |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 732 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 733 | // When not accepting a match at the start position set "extra_col" to |
| 734 | // a non-zero value. Don't do that when starting at MAXCOL, since |
| 735 | // MAXCOL + 1 is zero. |
Bram Moolenaar | 5f1e68b | 2015-07-10 14:43:35 +0200 | [diff] [blame] | 736 | if (pos->col == MAXCOL) |
| 737 | start_char_len = 0; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 738 | // Watch out for the "col" being MAXCOL - 2, used in a closed fold. |
Bram Moolenaar | 5f1e68b | 2015-07-10 14:43:35 +0200 | [diff] [blame] | 739 | else if (has_mbyte |
| 740 | && pos->lnum >= 1 && pos->lnum <= buf->b_ml.ml_line_count |
| 741 | && pos->col < MAXCOL - 2) |
Bram Moolenaar | a3dfccc | 2014-11-27 17:29:56 +0100 | [diff] [blame] | 742 | { |
Bram Moolenaar | 82846a0 | 2018-02-09 18:09:54 +0100 | [diff] [blame] | 743 | ptr = ml_get_buf(buf, pos->lnum, FALSE); |
zeertzjq | 94b7c32 | 2024-03-12 21:50:32 +0100 | [diff] [blame] | 744 | if (ml_get_buf_len(buf, pos->lnum) <= pos->col) |
Bram Moolenaar | 5f1e68b | 2015-07-10 14:43:35 +0200 | [diff] [blame] | 745 | start_char_len = 1; |
Bram Moolenaar | a3dfccc | 2014-11-27 17:29:56 +0100 | [diff] [blame] | 746 | else |
Bram Moolenaar | 82846a0 | 2018-02-09 18:09:54 +0100 | [diff] [blame] | 747 | start_char_len = (*mb_ptr2len)(ptr + pos->col); |
Bram Moolenaar | a3dfccc | 2014-11-27 17:29:56 +0100 | [diff] [blame] | 748 | } |
Bram Moolenaar | a3dfccc | 2014-11-27 17:29:56 +0100 | [diff] [blame] | 749 | else |
Bram Moolenaar | 5f1e68b | 2015-07-10 14:43:35 +0200 | [diff] [blame] | 750 | start_char_len = 1; |
| 751 | if (dir == FORWARD) |
| 752 | { |
| 753 | if (options & SEARCH_START) |
| 754 | extra_col = 0; |
| 755 | else |
| 756 | extra_col = start_char_len; |
| 757 | } |
| 758 | else |
| 759 | { |
| 760 | if (options & SEARCH_START) |
| 761 | extra_col = start_char_len; |
| 762 | else |
| 763 | extra_col = 0; |
| 764 | } |
Bram Moolenaar | a3dfccc | 2014-11-27 17:29:56 +0100 | [diff] [blame] | 765 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 766 | start_pos = *pos; // remember start pos for detecting no match |
| 767 | found = 0; // default: not found |
| 768 | at_first_line = TRUE; // default: start in first line |
| 769 | if (pos->lnum == 0) // correct lnum for when starting in line 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 770 | { |
| 771 | pos->lnum = 1; |
| 772 | pos->col = 0; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 773 | at_first_line = FALSE; // not in first line now |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | /* |
| 777 | * Start searching in current line, unless searching backwards and |
| 778 | * we're in column 0. |
Bram Moolenaar | 7a42fa3 | 2007-07-10 11:28:55 +0000 | [diff] [blame] | 779 | * If we are searching backwards, in column 0, and not including the |
| 780 | * current position, gain some efficiency by skipping back a line. |
| 781 | * Otherwise begin the search in the current line. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 782 | */ |
Bram Moolenaar | 7a42fa3 | 2007-07-10 11:28:55 +0000 | [diff] [blame] | 783 | if (dir == BACKWARD && start_pos.col == 0 |
| 784 | && (options & SEARCH_START) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 785 | { |
| 786 | lnum = pos->lnum - 1; |
| 787 | at_first_line = FALSE; |
| 788 | } |
| 789 | else |
| 790 | lnum = pos->lnum; |
| 791 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 792 | for (loop = 0; loop <= 1; ++loop) // loop twice if 'wrapscan' set |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 793 | { |
| 794 | for ( ; lnum > 0 && lnum <= buf->b_ml.ml_line_count; |
| 795 | lnum += dir, at_first_line = FALSE) |
| 796 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 797 | // Stop after checking "stop_lnum", if it's set. |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 798 | if (stop_lnum != 0 && (dir == FORWARD |
| 799 | ? lnum > stop_lnum : lnum < stop_lnum)) |
| 800 | break; |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 801 | // Stop after passing the time limit. |
| 802 | if (*timed_out) |
Bram Moolenaar | 7692929 | 2008-01-06 19:07:36 +0000 | [diff] [blame] | 803 | break; |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 804 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 805 | /* |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 806 | * Look for a match somewhere in line "lnum". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 807 | */ |
Bram Moolenaar | ad4d8a1 | 2015-12-28 19:20:36 +0100 | [diff] [blame] | 808 | col = at_first_line && (options & SEARCH_COL) ? pos->col |
| 809 | : (colnr_T)0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 810 | nmatched = vim_regexec_multi(®match, win, buf, |
Yegappan Lakshmanan | 04c4c57 | 2022-08-30 19:48:24 +0100 | [diff] [blame] | 811 | lnum, col, timed_out); |
Bram Moolenaar | 795aaa1 | 2020-10-02 20:36:01 +0200 | [diff] [blame] | 812 | // vim_regexec_multi() may clear "regprog" |
| 813 | if (regmatch.regprog == NULL) |
| 814 | break; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 815 | // Abort searching on an error (e.g., out of stack). |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 816 | if (called_emsg > called_emsg_before || *timed_out) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 817 | break; |
| 818 | if (nmatched > 0) |
| 819 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 820 | // match may actually be in another line when using \zs |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 821 | matchpos = regmatch.startpos[0]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 822 | endpos = regmatch.endpos[0]; |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 823 | #ifdef FEAT_EVAL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 824 | submatch = first_submatch(®match); |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 825 | #endif |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 826 | // "lnum" may be past end of buffer for "\n\zs". |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 827 | if (lnum + matchpos.lnum > buf->b_ml.ml_line_count) |
| 828 | ptr = (char_u *)""; |
| 829 | else |
| 830 | ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 831 | |
| 832 | /* |
| 833 | * Forward search in the first line: match should be after |
| 834 | * the start position. If not, continue at the end of the |
| 835 | * match (this is vi compatible) or on the next char. |
| 836 | */ |
| 837 | if (dir == FORWARD && at_first_line) |
| 838 | { |
| 839 | match_ok = TRUE; |
Bram Moolenaar | c96311b | 2022-11-25 21:13:47 +0000 | [diff] [blame] | 840 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 841 | /* |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 842 | * When the match starts in a next line it's certainly |
| 843 | * past the start position. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 844 | * When match lands on a NUL the cursor will be put |
| 845 | * one back afterwards, compare with that position, |
| 846 | * otherwise "/$" will get stuck on end of line. |
| 847 | */ |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 848 | while (matchpos.lnum == 0 |
Bram Moolenaar | a3dfccc | 2014-11-27 17:29:56 +0100 | [diff] [blame] | 849 | && ((options & SEARCH_END) && first_match |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 850 | ? (nmatched == 1 |
| 851 | && (int)endpos.col - 1 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 852 | < (int)start_pos.col + extra_col) |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 853 | : ((int)matchpos.col |
| 854 | - (ptr[matchpos.col] == NUL) |
| 855 | < (int)start_pos.col + extra_col))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 856 | { |
| 857 | /* |
| 858 | * If vi-compatible searching, continue at the end |
| 859 | * of the match, otherwise continue one position |
| 860 | * forward. |
| 861 | */ |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 862 | if (search_from_match_end) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 863 | { |
| 864 | if (nmatched > 1) |
| 865 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 866 | // end is in next line, thus no match in |
| 867 | // this line |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 868 | match_ok = FALSE; |
| 869 | break; |
| 870 | } |
| 871 | matchcol = endpos.col; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 872 | // for empty match: advance one char |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 873 | if (matchcol == matchpos.col |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 874 | && ptr[matchcol] != NUL) |
| 875 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 876 | if (has_mbyte) |
| 877 | matchcol += |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 878 | (*mb_ptr2len)(ptr + matchcol); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 879 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 880 | ++matchcol; |
| 881 | } |
| 882 | } |
| 883 | else |
| 884 | { |
Bram Moolenaar | c96311b | 2022-11-25 21:13:47 +0000 | [diff] [blame] | 885 | // Advance "matchcol" to the next character. |
Bram Moolenaar | 837ca8f | 2022-11-26 18:59:19 +0000 | [diff] [blame] | 886 | // This uses rmm_matchcol, the actual start of |
| 887 | // the match, ignoring "\zs". |
| 888 | matchcol = regmatch.rmm_matchcol; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 889 | if (ptr[matchcol] != NUL) |
| 890 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 891 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 892 | matchcol += (*mb_ptr2len)(ptr |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 893 | + matchcol); |
| 894 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 895 | ++matchcol; |
| 896 | } |
| 897 | } |
Bram Moolenaar | 7bcb30e | 2013-04-03 21:14:29 +0200 | [diff] [blame] | 898 | if (matchcol == 0 && (options & SEARCH_START)) |
Bram Moolenaar | db333a5 | 2013-03-19 15:27:48 +0100 | [diff] [blame] | 899 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 900 | if (ptr[matchcol] == NUL |
| 901 | || (nmatched = vim_regexec_multi(®match, |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 902 | win, buf, lnum + matchpos.lnum, |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 903 | matchcol, timed_out)) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 904 | { |
| 905 | match_ok = FALSE; |
| 906 | break; |
| 907 | } |
Bram Moolenaar | 795aaa1 | 2020-10-02 20:36:01 +0200 | [diff] [blame] | 908 | // vim_regexec_multi() may clear "regprog" |
| 909 | if (regmatch.regprog == NULL) |
| 910 | break; |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 911 | matchpos = regmatch.startpos[0]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 912 | endpos = regmatch.endpos[0]; |
| 913 | # ifdef FEAT_EVAL |
| 914 | submatch = first_submatch(®match); |
| 915 | # endif |
| 916 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 917 | // Need to get the line pointer again, a |
| 918 | // multi-line search may have made it invalid. |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 919 | ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 920 | } |
| 921 | if (!match_ok) |
| 922 | continue; |
| 923 | } |
| 924 | if (dir == BACKWARD) |
| 925 | { |
| 926 | /* |
| 927 | * Now, if there are multiple matches on this line, |
| 928 | * we have to get the last one. Or the last one before |
| 929 | * the cursor, if we're on that line. |
| 930 | * When putting the new cursor at the end, compare |
| 931 | * relative to the end of the match. |
| 932 | */ |
| 933 | match_ok = FALSE; |
| 934 | for (;;) |
| 935 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 936 | // Remember a position that is before the start |
| 937 | // position, we use it if it's the last match in |
| 938 | // the line. Always accept a position after |
| 939 | // wrapping around. |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 940 | if (loop |
| 941 | || ((options & SEARCH_END) |
| 942 | ? (lnum + regmatch.endpos[0].lnum |
| 943 | < start_pos.lnum |
| 944 | || (lnum + regmatch.endpos[0].lnum |
| 945 | == start_pos.lnum |
| 946 | && (int)regmatch.endpos[0].col - 1 |
Bram Moolenaar | 5f1e68b | 2015-07-10 14:43:35 +0200 | [diff] [blame] | 947 | < (int)start_pos.col |
| 948 | + extra_col)) |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 949 | : (lnum + regmatch.startpos[0].lnum |
| 950 | < start_pos.lnum |
| 951 | || (lnum + regmatch.startpos[0].lnum |
| 952 | == start_pos.lnum |
| 953 | && (int)regmatch.startpos[0].col |
Bram Moolenaar | 5f1e68b | 2015-07-10 14:43:35 +0200 | [diff] [blame] | 954 | < (int)start_pos.col |
| 955 | + extra_col)))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 956 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 957 | match_ok = TRUE; |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 958 | matchpos = regmatch.startpos[0]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 959 | endpos = regmatch.endpos[0]; |
| 960 | # ifdef FEAT_EVAL |
| 961 | submatch = first_submatch(®match); |
| 962 | # endif |
| 963 | } |
| 964 | else |
| 965 | break; |
| 966 | |
| 967 | /* |
| 968 | * We found a valid match, now check if there is |
| 969 | * another one after it. |
| 970 | * If vi-compatible searching, continue at the end |
| 971 | * of the match, otherwise continue one position |
| 972 | * forward. |
| 973 | */ |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 974 | if (search_from_match_end) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 975 | { |
| 976 | if (nmatched > 1) |
| 977 | break; |
| 978 | matchcol = endpos.col; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 979 | // for empty match: advance one char |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 980 | if (matchcol == matchpos.col |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 981 | && ptr[matchcol] != NUL) |
| 982 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 983 | if (has_mbyte) |
| 984 | matchcol += |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 985 | (*mb_ptr2len)(ptr + matchcol); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 986 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 987 | ++matchcol; |
| 988 | } |
| 989 | } |
| 990 | else |
| 991 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 992 | // Stop when the match is in a next line. |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 993 | if (matchpos.lnum > 0) |
| 994 | break; |
| 995 | matchcol = matchpos.col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 996 | if (ptr[matchcol] != NUL) |
| 997 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 998 | if (has_mbyte) |
| 999 | matchcol += |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1000 | (*mb_ptr2len)(ptr + matchcol); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1001 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1002 | ++matchcol; |
| 1003 | } |
| 1004 | } |
| 1005 | if (ptr[matchcol] == NUL |
| 1006 | || (nmatched = vim_regexec_multi(®match, |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 1007 | win, buf, lnum + matchpos.lnum, |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 1008 | matchcol, timed_out)) == 0) |
Bram Moolenaar | 9d32276 | 2018-02-09 16:04:25 +0100 | [diff] [blame] | 1009 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1010 | // If the search timed out, we did find a match |
| 1011 | // but it might be the wrong one, so that's not |
| 1012 | // OK. |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 1013 | if (*timed_out) |
Bram Moolenaar | 9d32276 | 2018-02-09 16:04:25 +0100 | [diff] [blame] | 1014 | match_ok = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1015 | break; |
Bram Moolenaar | 9d32276 | 2018-02-09 16:04:25 +0100 | [diff] [blame] | 1016 | } |
Bram Moolenaar | 795aaa1 | 2020-10-02 20:36:01 +0200 | [diff] [blame] | 1017 | // vim_regexec_multi() may clear "regprog" |
| 1018 | if (regmatch.regprog == NULL) |
| 1019 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1020 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1021 | // Need to get the line pointer again, a |
| 1022 | // multi-line search may have made it invalid. |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 1023 | ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | /* |
| 1027 | * If there is only a match after the cursor, skip |
| 1028 | * this match. |
| 1029 | */ |
| 1030 | if (!match_ok) |
| 1031 | continue; |
| 1032 | } |
| 1033 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1034 | // With the SEARCH_END option move to the last character |
| 1035 | // of the match. Don't do it for an empty match, end |
| 1036 | // should be same as start then. |
Bram Moolenaar | 7bcb30e | 2013-04-03 21:14:29 +0200 | [diff] [blame] | 1037 | if ((options & SEARCH_END) && !(options & SEARCH_NOOF) |
Bram Moolenaar | 5bcbd53 | 2008-02-20 12:43:01 +0000 | [diff] [blame] | 1038 | && !(matchpos.lnum == endpos.lnum |
| 1039 | && matchpos.col == endpos.col)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1040 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1041 | // For a match in the first column, set the position |
| 1042 | // on the NUL in the previous line. |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 1043 | pos->lnum = lnum + endpos.lnum; |
Bram Moolenaar | 5bcbd53 | 2008-02-20 12:43:01 +0000 | [diff] [blame] | 1044 | pos->col = endpos.col; |
| 1045 | if (endpos.col == 0) |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 1046 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1047 | if (pos->lnum > 1) // just in case |
Bram Moolenaar | 5bcbd53 | 2008-02-20 12:43:01 +0000 | [diff] [blame] | 1048 | { |
| 1049 | --pos->lnum; |
zeertzjq | 94b7c32 | 2024-03-12 21:50:32 +0100 | [diff] [blame] | 1050 | pos->col = ml_get_buf_len(buf, pos->lnum); |
Bram Moolenaar | 5bcbd53 | 2008-02-20 12:43:01 +0000 | [diff] [blame] | 1051 | } |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 1052 | } |
Bram Moolenaar | 5bcbd53 | 2008-02-20 12:43:01 +0000 | [diff] [blame] | 1053 | else |
| 1054 | { |
| 1055 | --pos->col; |
Bram Moolenaar | 5bcbd53 | 2008-02-20 12:43:01 +0000 | [diff] [blame] | 1056 | if (has_mbyte |
| 1057 | && pos->lnum <= buf->b_ml.ml_line_count) |
| 1058 | { |
| 1059 | ptr = ml_get_buf(buf, pos->lnum, FALSE); |
| 1060 | pos->col -= (*mb_head_off)(ptr, ptr + pos->col); |
| 1061 | } |
Bram Moolenaar | 5bcbd53 | 2008-02-20 12:43:01 +0000 | [diff] [blame] | 1062 | } |
Bram Moolenaar | 5d24a22 | 2018-12-23 19:10:09 +0100 | [diff] [blame] | 1063 | if (end_pos != NULL) |
| 1064 | { |
| 1065 | end_pos->lnum = lnum + matchpos.lnum; |
| 1066 | end_pos->col = matchpos.col; |
| 1067 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1068 | } |
| 1069 | else |
| 1070 | { |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 1071 | pos->lnum = lnum + matchpos.lnum; |
| 1072 | pos->col = matchpos.col; |
Bram Moolenaar | 5d24a22 | 2018-12-23 19:10:09 +0100 | [diff] [blame] | 1073 | if (end_pos != NULL) |
| 1074 | { |
| 1075 | end_pos->lnum = lnum + endpos.lnum; |
| 1076 | end_pos->col = endpos.col; |
| 1077 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1078 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1079 | pos->coladd = 0; |
Bram Moolenaar | 5d24a22 | 2018-12-23 19:10:09 +0100 | [diff] [blame] | 1080 | if (end_pos != NULL) |
| 1081 | end_pos->coladd = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1082 | found = 1; |
Bram Moolenaar | a3dfccc | 2014-11-27 17:29:56 +0100 | [diff] [blame] | 1083 | first_match = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1084 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1085 | // Set variables used for 'incsearch' highlighting. |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 1086 | search_match_lines = endpos.lnum - matchpos.lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1087 | search_match_endcol = endpos.col; |
| 1088 | break; |
| 1089 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1090 | line_breakcheck(); // stop if ctrl-C typed |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1091 | if (got_int) |
| 1092 | break; |
| 1093 | |
| 1094 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1095 | // Cancel searching if a character was typed. Used for |
| 1096 | // 'incsearch'. Don't check too often, that would slowdown |
| 1097 | // searching too much. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1098 | if ((options & SEARCH_PEEK) |
| 1099 | && ((lnum - pos->lnum) & 0x3f) == 0 |
| 1100 | && char_avail()) |
| 1101 | { |
| 1102 | break_loop = TRUE; |
| 1103 | break; |
| 1104 | } |
| 1105 | #endif |
| 1106 | |
| 1107 | if (loop && lnum == start_pos.lnum) |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1108 | break; // if second loop, stop where started |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1109 | } |
| 1110 | at_first_line = FALSE; |
| 1111 | |
Bram Moolenaar | 795aaa1 | 2020-10-02 20:36:01 +0200 | [diff] [blame] | 1112 | // vim_regexec_multi() may clear "regprog" |
| 1113 | if (regmatch.regprog == NULL) |
| 1114 | break; |
| 1115 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1116 | /* |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1117 | * Stop the search if wrapscan isn't set, "stop_lnum" is |
| 1118 | * specified, after an interrupt, after a match and after looping |
| 1119 | * twice. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1120 | */ |
Bram Moolenaar | 5398955 | 2019-12-23 22:59:18 +0100 | [diff] [blame] | 1121 | if (!p_ws || stop_lnum != 0 || got_int |
Yegappan Lakshmanan | 04c4c57 | 2022-08-30 19:48:24 +0100 | [diff] [blame] | 1122 | || called_emsg > called_emsg_before || *timed_out |
Bram Moolenaar | fbd0b0a | 2017-06-17 18:44:21 +0200 | [diff] [blame] | 1123 | #ifdef FEAT_SEARCH_EXTRA |
Yegappan Lakshmanan | 04c4c57 | 2022-08-30 19:48:24 +0100 | [diff] [blame] | 1124 | || break_loop |
Bram Moolenaar | fbd0b0a | 2017-06-17 18:44:21 +0200 | [diff] [blame] | 1125 | #endif |
Yegappan Lakshmanan | 04c4c57 | 2022-08-30 19:48:24 +0100 | [diff] [blame] | 1126 | || found || loop) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1127 | break; |
| 1128 | |
| 1129 | /* |
| 1130 | * If 'wrapscan' is set we continue at the other end of the file. |
Christian Brabandt | 34a6a36 | 2023-05-06 19:20:20 +0100 | [diff] [blame] | 1131 | * If 'shortmess' does not contain 's', we give a message, but |
| 1132 | * only, if we won't show the search stat later anyhow, |
| 1133 | * (so SEARCH_COUNT must be absent). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1134 | * This message is also remembered in keep_msg for when the screen |
| 1135 | * is redrawn. The keep_msg is cleared whenever another message is |
| 1136 | * written. |
| 1137 | */ |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1138 | if (dir == BACKWARD) // start second loop at the other end |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1139 | lnum = buf->b_ml.ml_line_count; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1140 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1141 | lnum = 1; |
Christian Brabandt | 34a6a36 | 2023-05-06 19:20:20 +0100 | [diff] [blame] | 1142 | if (!shortmess(SHM_SEARCH) |
| 1143 | && shortmess(SHM_SEARCHCOUNT) |
| 1144 | && (options & SEARCH_MSG)) |
Bram Moolenaar | 92d640f | 2005-09-05 22:11:52 +0000 | [diff] [blame] | 1145 | give_warning((char_u *)_(dir == BACKWARD |
| 1146 | ? top_bot_msg : bot_top_msg), TRUE); |
Bram Moolenaar | 92ea26b | 2019-10-18 20:53:34 +0200 | [diff] [blame] | 1147 | if (extra_arg != NULL) |
| 1148 | extra_arg->sa_wrapped = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1149 | } |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 1150 | if (got_int || called_emsg > called_emsg_before || *timed_out |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 1151 | #ifdef FEAT_SEARCH_EXTRA |
| 1152 | || break_loop |
| 1153 | #endif |
| 1154 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1155 | break; |
| 1156 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1157 | while (--count > 0 && found); // stop after count matches or no match |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1158 | |
Bram Moolenaar | 5ea38d1 | 2022-06-16 21:20:48 +0100 | [diff] [blame] | 1159 | #ifdef FEAT_RELTIME |
| 1160 | if (extra_arg != NULL && extra_arg->sa_tm > 0) |
| 1161 | disable_regexp_timeout(); |
| 1162 | #endif |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 1163 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1164 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1165 | if (!found) // did not find it |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1166 | { |
| 1167 | if (got_int) |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 1168 | emsg(_(e_interrupted)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1169 | else if ((options & SEARCH_MSG) == SEARCH_MSG) |
| 1170 | { |
| 1171 | if (p_ws) |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 1172 | semsg(_(e_pattern_not_found_str), mr_pattern); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1173 | else if (lnum == 0) |
Bram Moolenaar | ac78dd4 | 2022-01-02 19:25:26 +0000 | [diff] [blame] | 1174 | semsg(_(e_search_hit_top_without_match_for_str), mr_pattern); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1175 | else |
Bram Moolenaar | ac78dd4 | 2022-01-02 19:25:26 +0000 | [diff] [blame] | 1176 | semsg(_(e_search_hit_bottom_without_match_for_str), mr_pattern); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1177 | } |
| 1178 | return FAIL; |
| 1179 | } |
| 1180 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1181 | // A pattern like "\n\zs" may go past the last line. |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 1182 | if (pos->lnum > buf->b_ml.ml_line_count) |
| 1183 | { |
| 1184 | pos->lnum = buf->b_ml.ml_line_count; |
zeertzjq | 94b7c32 | 2024-03-12 21:50:32 +0100 | [diff] [blame] | 1185 | pos->col = ml_get_buf_len(buf, pos->lnum); |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 1186 | if (pos->col > 0) |
| 1187 | --pos->col; |
| 1188 | } |
| 1189 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1190 | return submatch + 1; |
| 1191 | } |
| 1192 | |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 1193 | #if defined(FEAT_EVAL) || defined(FEAT_PROTO) |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 1194 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 1195 | set_search_direction(int cdir) |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 1196 | { |
| 1197 | spats[0].off.dir = cdir; |
| 1198 | } |
| 1199 | |
| 1200 | static void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 1201 | set_vv_searchforward(void) |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 1202 | { |
| 1203 | set_vim_var_nr(VV_SEARCHFORWARD, (long)(spats[0].off.dir == '/')); |
| 1204 | } |
| 1205 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1206 | /* |
| 1207 | * Return the number of the first subpat that matched. |
Bram Moolenaar | ad4d8a1 | 2015-12-28 19:20:36 +0100 | [diff] [blame] | 1208 | * Return zero if none of them matched. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1209 | */ |
| 1210 | static int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 1211 | first_submatch(regmmatch_T *rp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1212 | { |
| 1213 | int submatch; |
| 1214 | |
| 1215 | for (submatch = 1; ; ++submatch) |
| 1216 | { |
| 1217 | if (rp->startpos[submatch].lnum >= 0) |
| 1218 | break; |
| 1219 | if (submatch == 9) |
| 1220 | { |
| 1221 | submatch = 0; |
| 1222 | break; |
| 1223 | } |
| 1224 | } |
| 1225 | return submatch; |
| 1226 | } |
| 1227 | #endif |
| 1228 | |
| 1229 | /* |
| 1230 | * Highest level string search function. |
Bram Moolenaar | b8017e7 | 2007-05-10 18:59:07 +0000 | [diff] [blame] | 1231 | * Search for the 'count'th occurrence of pattern 'pat' in direction 'dirc' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1232 | * If 'dirc' is 0: use previous dir. |
| 1233 | * If 'pat' is NULL or empty : use previous string. |
| 1234 | * If 'options & SEARCH_REV' : go in reverse of previous dir. |
| 1235 | * If 'options & SEARCH_ECHO': echo the search command and handle options |
| 1236 | * If 'options & SEARCH_MSG' : may give error message |
| 1237 | * If 'options & SEARCH_OPT' : interpret optional flags |
| 1238 | * If 'options & SEARCH_HIS' : put search pattern in history |
| 1239 | * If 'options & SEARCH_NOOF': don't add offset to position |
| 1240 | * If 'options & SEARCH_MARK': set previous context mark |
| 1241 | * If 'options & SEARCH_KEEP': keep previous search pattern |
| 1242 | * If 'options & SEARCH_START': accept match at curpos itself |
| 1243 | * If 'options & SEARCH_PEEK': check for typed char, cancel search |
| 1244 | * |
| 1245 | * Careful: If spats[0].off.line == TRUE and spats[0].off.off == 0 this |
| 1246 | * makes the movement linewise without moving the match position. |
| 1247 | * |
Bram Moolenaar | b6c2735 | 2015-03-05 19:57:49 +0100 | [diff] [blame] | 1248 | * Return 0 for failure, 1 for found, 2 for found and line offset added. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1249 | */ |
| 1250 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 1251 | do_search( |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1252 | oparg_T *oap, // can be NULL |
| 1253 | int dirc, // '/' or '?' |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 1254 | int search_delim, // the delimiter for the search, e.g. '%' in |
| 1255 | // s%regex%replacement% |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 1256 | char_u *pat, |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 1257 | size_t patlen, |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 1258 | long count, |
| 1259 | int options, |
Bram Moolenaar | 92ea26b | 2019-10-18 20:53:34 +0200 | [diff] [blame] | 1260 | searchit_arg_T *sia) // optional arguments or NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1261 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1262 | pos_T pos; // position of the last match |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1263 | char_u *searchstr; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 1264 | size_t searchstrlen; |
Bram Moolenaar | c332816 | 2019-07-23 22:15:25 +0200 | [diff] [blame] | 1265 | soffset_T old_off; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1266 | int retval; // Return value |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1267 | char_u *p; |
| 1268 | long c; |
| 1269 | char_u *dircp; |
| 1270 | char_u *strcopy = NULL; |
| 1271 | char_u *ps; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 1272 | int show_search_stats; |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1273 | char_u *msgbuf = NULL; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 1274 | size_t msgbuflen = 0; |
Bram Moolenaar | 8f46e4c | 2019-05-24 22:08:15 +0200 | [diff] [blame] | 1275 | int has_offset = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1276 | |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 1277 | searchcmdlen = 0; |
| 1278 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1279 | /* |
| 1280 | * A line offset is not remembered, this is vi compatible. |
| 1281 | */ |
| 1282 | if (spats[0].off.line && vim_strchr(p_cpo, CPO_LINEOFF) != NULL) |
| 1283 | { |
| 1284 | spats[0].off.line = FALSE; |
| 1285 | spats[0].off.off = 0; |
| 1286 | } |
| 1287 | |
| 1288 | /* |
| 1289 | * Save the values for when (options & SEARCH_KEEP) is used. |
| 1290 | * (there is no "if ()" around this because gcc wants them initialized) |
| 1291 | */ |
| 1292 | old_off = spats[0].off; |
| 1293 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1294 | pos = curwin->w_cursor; // start searching at the cursor position |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1295 | |
| 1296 | /* |
| 1297 | * Find out the direction of the search. |
| 1298 | */ |
| 1299 | if (dirc == 0) |
| 1300 | dirc = spats[0].off.dir; |
| 1301 | else |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 1302 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1303 | spats[0].off.dir = dirc; |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 1304 | #if defined(FEAT_EVAL) |
| 1305 | set_vv_searchforward(); |
| 1306 | #endif |
| 1307 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1308 | if (options & SEARCH_REV) |
| 1309 | { |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 1310 | #ifdef MSWIN |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1311 | // There is a bug in the Visual C++ 2.2 compiler which means that |
| 1312 | // dirc always ends up being '/' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1313 | dirc = (dirc == '/') ? '?' : '/'; |
| 1314 | #else |
| 1315 | if (dirc == '/') |
| 1316 | dirc = '?'; |
| 1317 | else |
| 1318 | dirc = '/'; |
| 1319 | #endif |
| 1320 | } |
| 1321 | |
| 1322 | #ifdef FEAT_FOLDING |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1323 | // If the cursor is in a closed fold, don't find another match in the same |
| 1324 | // fold. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1325 | if (dirc == '/') |
| 1326 | { |
| 1327 | if (hasFolding(pos.lnum, NULL, &pos.lnum)) |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1328 | pos.col = MAXCOL - 2; // avoid overflow when adding 1 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1329 | } |
| 1330 | else |
| 1331 | { |
| 1332 | if (hasFolding(pos.lnum, &pos.lnum, NULL)) |
| 1333 | pos.col = 0; |
| 1334 | } |
| 1335 | #endif |
| 1336 | |
| 1337 | #ifdef FEAT_SEARCH_EXTRA |
| 1338 | /* |
| 1339 | * Turn 'hlsearch' highlighting back on. |
| 1340 | */ |
| 1341 | if (no_hlsearch && !(options & SEARCH_KEEP)) |
| 1342 | { |
Bram Moolenaar | a4d158b | 2022-08-14 14:17:45 +0100 | [diff] [blame] | 1343 | redraw_all_later(UPD_SOME_VALID); |
Bram Moolenaar | 451fc7b | 2018-04-27 22:53:07 +0200 | [diff] [blame] | 1344 | set_no_hlsearch(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1345 | } |
| 1346 | #endif |
| 1347 | |
| 1348 | /* |
| 1349 | * Repeat the search when pattern followed by ';', e.g. "/foo/;?bar". |
| 1350 | */ |
| 1351 | for (;;) |
| 1352 | { |
Bram Moolenaar | 92ea26b | 2019-10-18 20:53:34 +0200 | [diff] [blame] | 1353 | int show_top_bot_msg = FALSE; |
Bram Moolenaar | c7a10b3 | 2019-05-06 21:37:18 +0200 | [diff] [blame] | 1354 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1355 | searchstr = pat; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 1356 | searchstrlen = patlen; |
| 1357 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1358 | dircp = NULL; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1359 | // use previous pattern |
Bram Moolenaar | c036e87 | 2020-02-21 21:30:52 +0100 | [diff] [blame] | 1360 | if (pat == NULL || *pat == NUL || *pat == search_delim) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1361 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1362 | if (spats[RE_SEARCH].pat == NULL) // no previous pattern |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1363 | { |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 1364 | if (spats[RE_SUBST].pat == NULL) |
Bram Moolenaar | b4b0a08 | 2011-02-25 18:38:36 +0100 | [diff] [blame] | 1365 | { |
Bram Moolenaar | e29a27f | 2021-07-20 21:07:36 +0200 | [diff] [blame] | 1366 | emsg(_(e_no_previous_regular_expression)); |
Bram Moolenaar | b4b0a08 | 2011-02-25 18:38:36 +0100 | [diff] [blame] | 1367 | retval = 0; |
| 1368 | goto end_do_search; |
| 1369 | } |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 1370 | searchstr = spats[RE_SUBST].pat; |
| 1371 | searchstrlen = spats[RE_SUBST].patlen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1372 | } |
Bram Moolenaar | b4b0a08 | 2011-02-25 18:38:36 +0100 | [diff] [blame] | 1373 | else |
| 1374 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1375 | // make search_regcomp() use spats[RE_SEARCH].pat |
Bram Moolenaar | b4b0a08 | 2011-02-25 18:38:36 +0100 | [diff] [blame] | 1376 | searchstr = (char_u *)""; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 1377 | searchstrlen = 0; |
Bram Moolenaar | b4b0a08 | 2011-02-25 18:38:36 +0100 | [diff] [blame] | 1378 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1379 | } |
| 1380 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1381 | if (pat != NULL && *pat != NUL) // look for (new) offset |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1382 | { |
| 1383 | /* |
| 1384 | * Find end of regular expression. |
| 1385 | * If there is a matching '/' or '?', toss it. |
| 1386 | */ |
| 1387 | ps = strcopy; |
Bram Moolenaar | f4e2099 | 2020-12-21 19:59:08 +0100 | [diff] [blame] | 1388 | p = skip_regexp_ex(pat, search_delim, magic_isset(), |
Bram Moolenaar | d93a7fc | 2021-01-04 12:42:13 +0100 | [diff] [blame] | 1389 | &strcopy, NULL, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1390 | if (strcopy != ps) |
| 1391 | { |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 1392 | size_t len = STRLEN(strcopy); |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1393 | // made a copy of "pat" to change "\?" to "?" |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 1394 | searchcmdlen += (int)(patlen - len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1395 | pat = strcopy; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 1396 | patlen = len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1397 | searchstr = strcopy; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 1398 | searchstrlen = len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1399 | } |
Bram Moolenaar | c036e87 | 2020-02-21 21:30:52 +0100 | [diff] [blame] | 1400 | if (*p == search_delim) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1401 | { |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 1402 | searchstrlen = p - pat; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1403 | dircp = p; // remember where we put the NUL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1404 | *p++ = NUL; |
| 1405 | } |
| 1406 | spats[0].off.line = FALSE; |
| 1407 | spats[0].off.end = FALSE; |
| 1408 | spats[0].off.off = 0; |
| 1409 | /* |
| 1410 | * Check for a line offset or a character offset. |
| 1411 | * For get_address (echo off) we don't check for a character |
| 1412 | * offset, because it is meaningless and the 's' could be a |
| 1413 | * substitute command. |
| 1414 | */ |
| 1415 | if (*p == '+' || *p == '-' || VIM_ISDIGIT(*p)) |
| 1416 | spats[0].off.line = TRUE; |
Dominique Pelle | 7765f5c | 2022-04-10 11:26:53 +0100 | [diff] [blame] | 1417 | else if ((options & SEARCH_OPT) |
| 1418 | && (*p == 'e' || *p == 's' || *p == 'b')) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1419 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1420 | if (*p == 'e') // end |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1421 | spats[0].off.end = SEARCH_END; |
| 1422 | ++p; |
| 1423 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1424 | if (VIM_ISDIGIT(*p) || *p == '+' || *p == '-') // got an offset |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1425 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1426 | // 'nr' or '+nr' or '-nr' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1427 | if (VIM_ISDIGIT(*p) || VIM_ISDIGIT(*(p + 1))) |
| 1428 | spats[0].off.off = atol((char *)p); |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1429 | else if (*p == '-') // single '-' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1430 | spats[0].off.off = -1; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1431 | else // single '+' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1432 | spats[0].off.off = 1; |
| 1433 | ++p; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1434 | while (VIM_ISDIGIT(*p)) // skip number |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1435 | ++p; |
| 1436 | } |
| 1437 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1438 | // compute length of search command for get_address() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1439 | searchcmdlen += (int)(p - pat); |
| 1440 | |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 1441 | patlen -= p - pat; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1442 | pat = p; // put pat after search command |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1443 | } |
| 1444 | |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 1445 | show_search_stats = FALSE; |
Dominique Pelle | 7765f5c | 2022-04-10 11:26:53 +0100 | [diff] [blame] | 1446 | if ((options & SEARCH_ECHO) && messaging() |
| 1447 | && !msg_silent |
| 1448 | && (!cmd_silent || !shortmess(SHM_SEARCHCOUNT))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1449 | { |
Bram Moolenaar | 984f031 | 2019-05-24 13:11:47 +0200 | [diff] [blame] | 1450 | char_u off_buf[40]; |
Bram Moolenaar | d33a764 | 2019-05-24 17:56:14 +0200 | [diff] [blame] | 1451 | size_t off_len = 0; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 1452 | size_t plen; |
| 1453 | size_t msgbufsize; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1454 | |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1455 | // Compute msg_row early. |
| 1456 | msg_start(); |
| 1457 | |
Bram Moolenaar | 984f031 | 2019-05-24 13:11:47 +0200 | [diff] [blame] | 1458 | // Get the offset, so we know how long it is. |
Bram Moolenaar | 359ad1a | 2019-09-02 21:44:59 +0200 | [diff] [blame] | 1459 | if (!cmd_silent && |
| 1460 | (spats[0].off.line || spats[0].off.end || spats[0].off.off)) |
Bram Moolenaar | 984f031 | 2019-05-24 13:11:47 +0200 | [diff] [blame] | 1461 | { |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 1462 | off_buf[off_len++] = dirc; |
Bram Moolenaar | 984f031 | 2019-05-24 13:11:47 +0200 | [diff] [blame] | 1463 | if (spats[0].off.end) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 1464 | off_buf[off_len++] = 'e'; |
Bram Moolenaar | 984f031 | 2019-05-24 13:11:47 +0200 | [diff] [blame] | 1465 | else if (!spats[0].off.line) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 1466 | off_buf[off_len++] = 's'; |
Bram Moolenaar | 984f031 | 2019-05-24 13:11:47 +0200 | [diff] [blame] | 1467 | if (spats[0].off.off > 0 || spats[0].off.line) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 1468 | off_buf[off_len++] = '+'; |
| 1469 | off_buf[off_len] = NUL; |
Bram Moolenaar | 984f031 | 2019-05-24 13:11:47 +0200 | [diff] [blame] | 1470 | if (spats[0].off.off != 0 || spats[0].off.line) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 1471 | off_len += vim_snprintf((char *)off_buf + off_len, sizeof(off_buf) - off_len, "%ld", spats[0].off.off); |
Bram Moolenaar | 984f031 | 2019-05-24 13:11:47 +0200 | [diff] [blame] | 1472 | } |
| 1473 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1474 | if (*searchstr == NUL) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 1475 | { |
Bram Moolenaar | 2fb8f68 | 2018-12-01 13:14:45 +0100 | [diff] [blame] | 1476 | p = spats[0].pat; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 1477 | plen = spats[0].patlen; |
| 1478 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1479 | else |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 1480 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1481 | p = searchstr; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 1482 | plen = searchstrlen; |
| 1483 | } |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1484 | |
Bram Moolenaar | 359ad1a | 2019-09-02 21:44:59 +0200 | [diff] [blame] | 1485 | if (!shortmess(SHM_SEARCHCOUNT) || cmd_silent) |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1486 | { |
| 1487 | // Reserve enough space for the search pattern + offset + |
Bram Moolenaar | 984f031 | 2019-05-24 13:11:47 +0200 | [diff] [blame] | 1488 | // search stat. Use all the space available, so that the |
| 1489 | // search state is right aligned. If there is not enough space |
| 1490 | // msg_strtrunc() will shorten in the middle. |
Bram Moolenaar | 19e8ac7 | 2019-09-03 22:23:38 +0200 | [diff] [blame] | 1491 | if (msg_scrolled != 0 && !cmd_silent) |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1492 | // Use all the columns. |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 1493 | msgbufsize = (int)(Rows - msg_row) * Columns - 1; |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1494 | else |
| 1495 | // Use up to 'showcmd' column. |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 1496 | msgbufsize = (int)(Rows - msg_row - 1) * Columns + sc_col - 1; |
| 1497 | if (msgbufsize < plen + off_len + SEARCH_STAT_BUF_LEN + 3) |
| 1498 | msgbufsize = plen + off_len + SEARCH_STAT_BUF_LEN + 3; |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1499 | } |
| 1500 | else |
| 1501 | // Reserve enough space for the search pattern + offset. |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 1502 | msgbufsize = plen + off_len + 3; |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1503 | |
Bram Moolenaar | 880e4d9 | 2020-04-11 21:31:28 +0200 | [diff] [blame] | 1504 | vim_free(msgbuf); |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 1505 | msgbuf = alloc(msgbufsize); |
| 1506 | if (msgbuf == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1507 | { |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 1508 | msgbuflen = 0; |
| 1509 | } |
| 1510 | else |
| 1511 | { |
| 1512 | vim_memset(msgbuf, ' ', msgbufsize); |
| 1513 | msgbuflen = msgbufsize - 1; |
| 1514 | msgbuf[msgbuflen] = NUL; |
Bram Moolenaar | 359ad1a | 2019-09-02 21:44:59 +0200 | [diff] [blame] | 1515 | // do not fill the msgbuf buffer, if cmd_silent is set, leave it |
| 1516 | // empty for the search_stat feature. |
| 1517 | if (!cmd_silent) |
Bram Moolenaar | cafda4f | 2005-09-06 19:25:11 +0000 | [diff] [blame] | 1518 | { |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 1519 | char_u *trunc; |
| 1520 | |
Bram Moolenaar | 359ad1a | 2019-09-02 21:44:59 +0200 | [diff] [blame] | 1521 | msgbuf[0] = dirc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1522 | |
Bram Moolenaar | 359ad1a | 2019-09-02 21:44:59 +0200 | [diff] [blame] | 1523 | if (enc_utf8 && utf_iscomposing(utf_ptr2char(p))) |
| 1524 | { |
| 1525 | // Use a space to draw the composing char on. |
| 1526 | msgbuf[1] = ' '; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 1527 | mch_memmove(msgbuf + 2, p, plen); |
Bram Moolenaar | 359ad1a | 2019-09-02 21:44:59 +0200 | [diff] [blame] | 1528 | } |
| 1529 | else |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 1530 | mch_memmove(msgbuf + 1, p, plen); |
Bram Moolenaar | 359ad1a | 2019-09-02 21:44:59 +0200 | [diff] [blame] | 1531 | if (off_len > 0) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 1532 | mch_memmove(msgbuf + plen + 1, off_buf, off_len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1533 | |
Bram Moolenaar | 359ad1a | 2019-09-02 21:44:59 +0200 | [diff] [blame] | 1534 | trunc = msg_strtrunc(msgbuf, TRUE); |
| 1535 | if (trunc != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1536 | { |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1537 | vim_free(msgbuf); |
Bram Moolenaar | 359ad1a | 2019-09-02 21:44:59 +0200 | [diff] [blame] | 1538 | msgbuf = trunc; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 1539 | msgbuflen = STRLEN(msgbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1540 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1541 | |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 1542 | #ifdef FEAT_RIGHTLEFT |
| 1543 | // The search pattern could be shown on the right in |
| 1544 | // rightleft mode, but the 'ruler' and 'showcmd' area use |
| 1545 | // it too, thus it would be blanked out again very soon. |
| 1546 | // Show it on the left, but do reverse the text. |
Bram Moolenaar | 359ad1a | 2019-09-02 21:44:59 +0200 | [diff] [blame] | 1547 | if (curwin->w_p_rl && *curwin->w_p_rlc == 's') |
| 1548 | { |
| 1549 | char_u *r; |
| 1550 | size_t pat_len; |
| 1551 | |
| 1552 | r = reverse_text(msgbuf); |
| 1553 | if (r != NULL) |
| 1554 | { |
| 1555 | vim_free(msgbuf); |
| 1556 | msgbuf = r; |
Christian Brabandt | cacb669 | 2024-08-22 21:40:14 +0200 | [diff] [blame] | 1557 | msgbuflen = STRLEN(msgbuf); |
Bram Moolenaar | 359ad1a | 2019-09-02 21:44:59 +0200 | [diff] [blame] | 1558 | // move reversed text to beginning of buffer |
| 1559 | while (*r != NUL && *r == ' ') |
| 1560 | r++; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 1561 | pat_len = msgbuf + msgbuflen - r; |
Bram Moolenaar | 359ad1a | 2019-09-02 21:44:59 +0200 | [diff] [blame] | 1562 | mch_memmove(msgbuf, r, pat_len); |
| 1563 | // overwrite old text |
| 1564 | if ((size_t)(r - msgbuf) >= pat_len) |
| 1565 | vim_memset(r, ' ', pat_len); |
| 1566 | else |
| 1567 | vim_memset(msgbuf + pat_len, ' ', r - msgbuf); |
| 1568 | } |
| 1569 | } |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 1570 | #endif |
Bram Moolenaar | 359ad1a | 2019-09-02 21:44:59 +0200 | [diff] [blame] | 1571 | msg_outtrans(msgbuf); |
| 1572 | msg_clr_eos(); |
| 1573 | msg_check(); |
| 1574 | |
| 1575 | gotocmdline(FALSE); |
| 1576 | out_flush(); |
| 1577 | msg_nowait = TRUE; // don't wait for this message |
| 1578 | } |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 1579 | |
| 1580 | if (!shortmess(SHM_SEARCHCOUNT)) |
| 1581 | show_search_stats = TRUE; |
| 1582 | } // msgbuf != NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1583 | } |
| 1584 | |
| 1585 | /* |
| 1586 | * If there is a character offset, subtract it from the current |
| 1587 | * position, so we don't get stuck at "?pat?e+2" or "/pat/s-2". |
Bram Moolenaar | ed20346 | 2004-06-16 11:19:22 +0000 | [diff] [blame] | 1588 | * Skip this if pos.col is near MAXCOL (closed fold). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1589 | * This is not done for a line offset, because then we would not be vi |
| 1590 | * compatible. |
| 1591 | */ |
Bram Moolenaar | ed20346 | 2004-06-16 11:19:22 +0000 | [diff] [blame] | 1592 | if (!spats[0].off.line && spats[0].off.off && pos.col < MAXCOL - 2) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1593 | { |
| 1594 | if (spats[0].off.off > 0) |
| 1595 | { |
| 1596 | for (c = spats[0].off.off; c; --c) |
| 1597 | if (decl(&pos) == -1) |
| 1598 | break; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1599 | if (c) // at start of buffer |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1600 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1601 | pos.lnum = 0; // allow lnum == 0 here |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1602 | pos.col = MAXCOL; |
| 1603 | } |
| 1604 | } |
| 1605 | else |
| 1606 | { |
| 1607 | for (c = spats[0].off.off; c; ++c) |
| 1608 | if (incl(&pos) == -1) |
| 1609 | break; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1610 | if (c) // at end of buffer |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1611 | { |
| 1612 | pos.lnum = curbuf->b_ml.ml_line_count + 1; |
| 1613 | pos.col = 0; |
| 1614 | } |
| 1615 | } |
| 1616 | } |
| 1617 | |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 1618 | /* |
| 1619 | * The actual search. |
| 1620 | */ |
Bram Moolenaar | 14184a3 | 2019-02-16 15:10:30 +0100 | [diff] [blame] | 1621 | c = searchit(curwin, curbuf, &pos, NULL, |
| 1622 | dirc == '/' ? FORWARD : BACKWARD, |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 1623 | searchstr, searchstrlen, count, spats[0].off.end + (options & |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1624 | (SEARCH_KEEP + SEARCH_PEEK + SEARCH_HIS |
| 1625 | + SEARCH_MSG + SEARCH_START |
| 1626 | + ((pat != NULL && *pat == ';') ? 0 : SEARCH_NOOF))), |
Bram Moolenaar | 92ea26b | 2019-10-18 20:53:34 +0200 | [diff] [blame] | 1627 | RE_LAST, sia); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1628 | |
| 1629 | if (dircp != NULL) |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 1630 | *dircp = search_delim; // restore second '/' or '?' for normal_cmd() |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1631 | |
| 1632 | if (!shortmess(SHM_SEARCH) |
| 1633 | && ((dirc == '/' && LT_POS(pos, curwin->w_cursor)) |
| 1634 | || (dirc == '?' && LT_POS(curwin->w_cursor, pos)))) |
Bram Moolenaar | c7a10b3 | 2019-05-06 21:37:18 +0200 | [diff] [blame] | 1635 | show_top_bot_msg = TRUE; |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1636 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1637 | if (c == FAIL) |
| 1638 | { |
| 1639 | retval = 0; |
| 1640 | goto end_do_search; |
| 1641 | } |
| 1642 | if (spats[0].off.end && oap != NULL) |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1643 | oap->inclusive = TRUE; // 'e' includes last character |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1644 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1645 | retval = 1; // pattern found |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1646 | |
| 1647 | /* |
| 1648 | * Add character and/or line offset |
| 1649 | */ |
Bram Moolenaar | 9160f30 | 2006-08-29 15:58:12 +0000 | [diff] [blame] | 1650 | if (!(options & SEARCH_NOOF) || (pat != NULL && *pat == ';')) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1651 | { |
Bram Moolenaar | 8f46e4c | 2019-05-24 22:08:15 +0200 | [diff] [blame] | 1652 | pos_T org_pos = pos; |
| 1653 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1654 | if (spats[0].off.line) // Add the offset to the line number. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1655 | { |
| 1656 | c = pos.lnum + spats[0].off.off; |
| 1657 | if (c < 1) |
| 1658 | pos.lnum = 1; |
| 1659 | else if (c > curbuf->b_ml.ml_line_count) |
| 1660 | pos.lnum = curbuf->b_ml.ml_line_count; |
| 1661 | else |
| 1662 | pos.lnum = c; |
| 1663 | pos.col = 0; |
| 1664 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1665 | retval = 2; // pattern found, line offset added |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1666 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1667 | else if (pos.col < MAXCOL - 2) // just in case |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1668 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1669 | // to the right, check for end of file |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 1670 | c = spats[0].off.off; |
| 1671 | if (c > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1672 | { |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 1673 | while (c-- > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1674 | if (incl(&pos) == -1) |
| 1675 | break; |
| 1676 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1677 | // to the left, check for start of file |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1678 | else |
| 1679 | { |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 1680 | while (c++ < 0) |
| 1681 | if (decl(&pos) == -1) |
| 1682 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1683 | } |
| 1684 | } |
Bram Moolenaar | 8f46e4c | 2019-05-24 22:08:15 +0200 | [diff] [blame] | 1685 | if (!EQUAL_POS(pos, org_pos)) |
| 1686 | has_offset = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1687 | } |
| 1688 | |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1689 | // Show [1/15] if 'S' is not in 'shortmess'. |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 1690 | if (show_search_stats) |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 1691 | cmdline_search_stat(dirc, &pos, &curwin->w_cursor, |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 1692 | show_top_bot_msg, msgbuf, msgbuflen, |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 1693 | (count != 1 || has_offset |
Bram Moolenaar | 6cb0726 | 2020-05-29 22:49:43 +0200 | [diff] [blame] | 1694 | #ifdef FEAT_FOLDING |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 1695 | || (!(fdo_flags & FDO_SEARCH) |
| 1696 | && hasFolding(curwin->w_cursor.lnum, |
| 1697 | NULL, NULL)) |
Bram Moolenaar | 6cb0726 | 2020-05-29 22:49:43 +0200 | [diff] [blame] | 1698 | #endif |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 1699 | ), |
| 1700 | SEARCH_STAT_DEF_MAX_COUNT, |
| 1701 | SEARCH_STAT_DEF_TIMEOUT); |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1702 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1703 | /* |
| 1704 | * The search command can be followed by a ';' to do another search. |
| 1705 | * For example: "/pat/;/foo/+3;?bar" |
| 1706 | * This is like doing another search command, except: |
| 1707 | * - The remembered direction '/' or '?' is from the first search. |
| 1708 | * - When an error happens the cursor isn't moved at all. |
| 1709 | * Don't do this when called by get_address() (it handles ';' itself). |
| 1710 | */ |
| 1711 | if (!(options & SEARCH_OPT) || pat == NULL || *pat != ';') |
| 1712 | break; |
| 1713 | |
| 1714 | dirc = *++pat; |
Bram Moolenaar | c036e87 | 2020-02-21 21:30:52 +0100 | [diff] [blame] | 1715 | search_delim = dirc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1716 | if (dirc != '?' && dirc != '/') |
| 1717 | { |
| 1718 | retval = 0; |
Bram Moolenaar | ac78dd4 | 2022-01-02 19:25:26 +0000 | [diff] [blame] | 1719 | emsg(_(e_expected_question_or_slash_after_semicolon)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1720 | goto end_do_search; |
| 1721 | } |
| 1722 | ++pat; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 1723 | --patlen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1724 | } |
| 1725 | |
| 1726 | if (options & SEARCH_MARK) |
| 1727 | setpcmark(); |
| 1728 | curwin->w_cursor = pos; |
| 1729 | curwin->w_set_curswant = TRUE; |
| 1730 | |
| 1731 | end_do_search: |
Bram Moolenaar | e100440 | 2020-10-24 20:49:43 +0200 | [diff] [blame] | 1732 | if ((options & SEARCH_KEEP) || (cmdmod.cmod_flags & CMOD_KEEPPATTERNS)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1733 | spats[0].off = old_off; |
| 1734 | vim_free(strcopy); |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1735 | vim_free(msgbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1736 | |
| 1737 | return retval; |
| 1738 | } |
| 1739 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1740 | /* |
| 1741 | * search_for_exact_line(buf, pos, dir, pat) |
| 1742 | * |
| 1743 | * Search for a line starting with the given pattern (ignoring leading |
Bram Moolenaar | 8ad80de | 2017-06-05 16:01:59 +0200 | [diff] [blame] | 1744 | * white-space), starting from pos and going in direction "dir". "pos" will |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1745 | * contain the position of the match found. Blank lines match only if |
Bram Moolenaar | 8ad80de | 2017-06-05 16:01:59 +0200 | [diff] [blame] | 1746 | * ADDING is set. If p_ic is set then the pattern must be in lowercase. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1747 | * Return OK for success, or FAIL if no line found. |
| 1748 | */ |
| 1749 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 1750 | search_for_exact_line( |
| 1751 | buf_T *buf, |
| 1752 | pos_T *pos, |
| 1753 | int dir, |
| 1754 | char_u *pat) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1755 | { |
| 1756 | linenr_T start = 0; |
| 1757 | char_u *ptr; |
| 1758 | char_u *p; |
| 1759 | |
| 1760 | if (buf->b_ml.ml_line_count == 0) |
| 1761 | return FAIL; |
| 1762 | for (;;) |
| 1763 | { |
| 1764 | pos->lnum += dir; |
| 1765 | if (pos->lnum < 1) |
| 1766 | { |
| 1767 | if (p_ws) |
| 1768 | { |
| 1769 | pos->lnum = buf->b_ml.ml_line_count; |
| 1770 | if (!shortmess(SHM_SEARCH)) |
| 1771 | give_warning((char_u *)_(top_bot_msg), TRUE); |
| 1772 | } |
| 1773 | else |
| 1774 | { |
| 1775 | pos->lnum = 1; |
| 1776 | break; |
| 1777 | } |
| 1778 | } |
| 1779 | else if (pos->lnum > buf->b_ml.ml_line_count) |
| 1780 | { |
| 1781 | if (p_ws) |
| 1782 | { |
| 1783 | pos->lnum = 1; |
| 1784 | if (!shortmess(SHM_SEARCH)) |
| 1785 | give_warning((char_u *)_(bot_top_msg), TRUE); |
| 1786 | } |
| 1787 | else |
| 1788 | { |
| 1789 | pos->lnum = 1; |
| 1790 | break; |
| 1791 | } |
| 1792 | } |
| 1793 | if (pos->lnum == start) |
| 1794 | break; |
| 1795 | if (start == 0) |
| 1796 | start = pos->lnum; |
| 1797 | ptr = ml_get_buf(buf, pos->lnum, FALSE); |
| 1798 | p = skipwhite(ptr); |
| 1799 | pos->col = (colnr_T) (p - ptr); |
| 1800 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1801 | // when adding lines the matching line may be empty but it is not |
| 1802 | // ignored because we are interested in the next line -- Acevedo |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 1803 | if (compl_status_adding() && !compl_status_sol()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1804 | { |
| 1805 | if ((p_ic ? MB_STRICMP(p, pat) : STRCMP(p, pat)) == 0) |
| 1806 | return OK; |
| 1807 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1808 | else if (*p != NUL) // ignore empty lines |
| 1809 | { // expanding lines or words |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 1810 | if ((p_ic ? MB_STRNICMP(p, pat, ins_compl_len()) |
| 1811 | : STRNCMP(p, pat, ins_compl_len())) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1812 | return OK; |
| 1813 | } |
| 1814 | } |
| 1815 | return FAIL; |
| 1816 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1817 | |
| 1818 | /* |
| 1819 | * Character Searches |
| 1820 | */ |
| 1821 | |
| 1822 | /* |
| 1823 | * Search for a character in a line. If "t_cmd" is FALSE, move to the |
| 1824 | * position of the character, otherwise move to just before the char. |
| 1825 | * Do this "cap->count1" times. |
| 1826 | * Return FAIL or OK. |
| 1827 | */ |
| 1828 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 1829 | searchc(cmdarg_T *cap, int t_cmd) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1830 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1831 | int c = cap->nchar; // char to search for |
| 1832 | int dir = cap->arg; // TRUE for searching forward |
| 1833 | long count = cap->count1; // repeat count |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1834 | int col; |
| 1835 | char_u *p; |
| 1836 | int len; |
Bram Moolenaar | 8b3e033 | 2011-06-26 05:36:34 +0200 | [diff] [blame] | 1837 | int stop = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1838 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1839 | if (c != NUL) // normal search: remember args for repeat |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1840 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1841 | if (!KeyStuffed) // don't remember when redoing |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1842 | { |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 1843 | *lastc = c; |
| 1844 | set_csearch_direction(dir); |
| 1845 | set_csearch_until(t_cmd); |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 1846 | lastc_bytelen = (*mb_char2bytes)(c, lastc_bytes); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1847 | if (cap->ncharC1 != 0) |
| 1848 | { |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 1849 | lastc_bytelen += (*mb_char2bytes)(cap->ncharC1, |
| 1850 | lastc_bytes + lastc_bytelen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1851 | if (cap->ncharC2 != 0) |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 1852 | lastc_bytelen += (*mb_char2bytes)(cap->ncharC2, |
| 1853 | lastc_bytes + lastc_bytelen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1854 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1855 | } |
| 1856 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1857 | else // repeat previous search |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1858 | { |
zeertzjq | e5d91ba | 2023-05-14 17:39:18 +0100 | [diff] [blame] | 1859 | if (*lastc == NUL && lastc_bytelen <= 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1860 | return FAIL; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1861 | if (dir) // repeat in opposite direction |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1862 | dir = -lastcdir; |
| 1863 | else |
| 1864 | dir = lastcdir; |
| 1865 | t_cmd = last_t_cmd; |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 1866 | c = *lastc; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1867 | // For multi-byte re-use last lastc_bytes[] and lastc_bytelen. |
Bram Moolenaar | 8b3e033 | 2011-06-26 05:36:34 +0200 | [diff] [blame] | 1868 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1869 | // Force a move of at least one char, so ";" and "," will move the |
| 1870 | // cursor, even if the cursor is right in front of char we are looking |
| 1871 | // at. |
Bram Moolenaar | 19fd09a | 2011-07-15 13:21:30 +0200 | [diff] [blame] | 1872 | if (vim_strchr(p_cpo, CPO_SCOLON) == NULL && count == 1 && t_cmd) |
Bram Moolenaar | 8b3e033 | 2011-06-26 05:36:34 +0200 | [diff] [blame] | 1873 | stop = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1874 | } |
| 1875 | |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 1876 | if (dir == BACKWARD) |
| 1877 | cap->oap->inclusive = FALSE; |
| 1878 | else |
| 1879 | cap->oap->inclusive = TRUE; |
| 1880 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1881 | p = ml_get_curline(); |
| 1882 | col = curwin->w_cursor.col; |
zeertzjq | 94b7c32 | 2024-03-12 21:50:32 +0100 | [diff] [blame] | 1883 | len = ml_get_curline_len(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1884 | |
| 1885 | while (count--) |
| 1886 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1887 | if (has_mbyte) |
| 1888 | { |
| 1889 | for (;;) |
| 1890 | { |
| 1891 | if (dir > 0) |
| 1892 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1893 | col += (*mb_ptr2len)(p + col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1894 | if (col >= len) |
| 1895 | return FAIL; |
| 1896 | } |
| 1897 | else |
| 1898 | { |
| 1899 | if (col == 0) |
| 1900 | return FAIL; |
| 1901 | col -= (*mb_head_off)(p, p + col - 1) + 1; |
| 1902 | } |
zeertzjq | e5d91ba | 2023-05-14 17:39:18 +0100 | [diff] [blame] | 1903 | if (lastc_bytelen <= 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1904 | { |
Bram Moolenaar | 8b3e033 | 2011-06-26 05:36:34 +0200 | [diff] [blame] | 1905 | if (p[col] == c && stop) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1906 | break; |
| 1907 | } |
Bram Moolenaar | 66727e1 | 2017-03-01 22:17:05 +0100 | [diff] [blame] | 1908 | else if (STRNCMP(p + col, lastc_bytes, lastc_bytelen) == 0 |
Bram Moolenaar | b129a44 | 2016-12-01 17:25:20 +0100 | [diff] [blame] | 1909 | && stop) |
Bram Moolenaar | 66727e1 | 2017-03-01 22:17:05 +0100 | [diff] [blame] | 1910 | break; |
Bram Moolenaar | 8b3e033 | 2011-06-26 05:36:34 +0200 | [diff] [blame] | 1911 | stop = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1912 | } |
| 1913 | } |
| 1914 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1915 | { |
| 1916 | for (;;) |
| 1917 | { |
| 1918 | if ((col += dir) < 0 || col >= len) |
| 1919 | return FAIL; |
Bram Moolenaar | 8b3e033 | 2011-06-26 05:36:34 +0200 | [diff] [blame] | 1920 | if (p[col] == c && stop) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1921 | break; |
Bram Moolenaar | 8b3e033 | 2011-06-26 05:36:34 +0200 | [diff] [blame] | 1922 | stop = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1923 | } |
| 1924 | } |
| 1925 | } |
| 1926 | |
| 1927 | if (t_cmd) |
| 1928 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1929 | // backup to before the character (possibly double-byte) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1930 | col -= dir; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1931 | if (has_mbyte) |
| 1932 | { |
| 1933 | if (dir < 0) |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1934 | // Landed on the search char which is lastc_bytelen long |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 1935 | col += lastc_bytelen - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1936 | else |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1937 | // To previous char, which may be multi-byte. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1938 | col -= (*mb_head_off)(p, p + col); |
| 1939 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1940 | } |
| 1941 | curwin->w_cursor.col = col; |
| 1942 | |
| 1943 | return OK; |
| 1944 | } |
| 1945 | |
| 1946 | /* |
| 1947 | * "Other" Searches |
| 1948 | */ |
| 1949 | |
| 1950 | /* |
| 1951 | * findmatch - find the matching paren or brace |
| 1952 | * |
| 1953 | * Improvement over vi: Braces inside quotes are ignored. |
| 1954 | */ |
| 1955 | pos_T * |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 1956 | findmatch(oparg_T *oap, int initc) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1957 | { |
| 1958 | return findmatchlimit(oap, initc, 0, 0); |
| 1959 | } |
| 1960 | |
| 1961 | /* |
| 1962 | * Return TRUE if the character before "linep[col]" equals "ch". |
| 1963 | * Return FALSE if "col" is zero. |
| 1964 | * Update "*prevcol" to the column of the previous character, unless "prevcol" |
| 1965 | * is NULL. |
| 1966 | * Handles multibyte string correctly. |
| 1967 | */ |
| 1968 | static int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 1969 | check_prevcol( |
| 1970 | char_u *linep, |
| 1971 | int col, |
| 1972 | int ch, |
| 1973 | int *prevcol) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1974 | { |
| 1975 | --col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1976 | if (col > 0 && has_mbyte) |
| 1977 | col -= (*mb_head_off)(linep, linep + col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1978 | if (prevcol) |
| 1979 | *prevcol = col; |
| 1980 | return (col >= 0 && linep[col] == ch) ? TRUE : FALSE; |
| 1981 | } |
| 1982 | |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 1983 | /* |
| 1984 | * Raw string start is found at linep[startpos.col - 1]. |
| 1985 | * Return TRUE if the matching end can be found between startpos and endpos. |
| 1986 | */ |
| 1987 | static int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 1988 | find_rawstring_end(char_u *linep, pos_T *startpos, pos_T *endpos) |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 1989 | { |
| 1990 | char_u *p; |
| 1991 | char_u *delim_copy; |
| 1992 | size_t delim_len; |
| 1993 | linenr_T lnum; |
| 1994 | int found = FALSE; |
| 1995 | |
| 1996 | for (p = linep + startpos->col + 1; *p && *p != '('; ++p) |
| 1997 | ; |
| 1998 | delim_len = (p - linep) - startpos->col - 1; |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1999 | delim_copy = vim_strnsave(linep + startpos->col + 1, delim_len); |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 2000 | if (delim_copy == NULL) |
| 2001 | return FALSE; |
| 2002 | for (lnum = startpos->lnum; lnum <= endpos->lnum; ++lnum) |
| 2003 | { |
| 2004 | char_u *line = ml_get(lnum); |
| 2005 | |
| 2006 | for (p = line + (lnum == startpos->lnum |
| 2007 | ? startpos->col + 1 : 0); *p; ++p) |
| 2008 | { |
| 2009 | if (lnum == endpos->lnum && (colnr_T)(p - line) >= endpos->col) |
| 2010 | break; |
Bram Moolenaar | 282f9c6 | 2020-08-04 21:46:18 +0200 | [diff] [blame] | 2011 | if (*p == ')' && STRNCMP(delim_copy, p + 1, delim_len) == 0 |
| 2012 | && p[delim_len + 1] == '"') |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 2013 | { |
| 2014 | found = TRUE; |
| 2015 | break; |
| 2016 | } |
| 2017 | } |
| 2018 | if (found) |
| 2019 | break; |
| 2020 | } |
| 2021 | vim_free(delim_copy); |
| 2022 | return found; |
| 2023 | } |
| 2024 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2025 | /* |
Bram Moolenaar | 556ae8e | 2019-11-21 22:27:22 +0100 | [diff] [blame] | 2026 | * Check matchpairs option for "*initc". |
| 2027 | * If there is a match set "*initc" to the matching character and "*findc" to |
| 2028 | * the opposite character. Set "*backwards" to the direction. |
| 2029 | * When "switchit" is TRUE swap the direction. |
| 2030 | */ |
| 2031 | static void |
| 2032 | find_mps_values( |
| 2033 | int *initc, |
| 2034 | int *findc, |
| 2035 | int *backwards, |
| 2036 | int switchit) |
| 2037 | { |
| 2038 | char_u *ptr; |
| 2039 | |
| 2040 | ptr = curbuf->b_p_mps; |
| 2041 | while (*ptr != NUL) |
| 2042 | { |
| 2043 | if (has_mbyte) |
| 2044 | { |
| 2045 | char_u *prev; |
| 2046 | |
| 2047 | if (mb_ptr2char(ptr) == *initc) |
| 2048 | { |
| 2049 | if (switchit) |
| 2050 | { |
| 2051 | *findc = *initc; |
| 2052 | *initc = mb_ptr2char(ptr + mb_ptr2len(ptr) + 1); |
| 2053 | *backwards = TRUE; |
| 2054 | } |
| 2055 | else |
| 2056 | { |
| 2057 | *findc = mb_ptr2char(ptr + mb_ptr2len(ptr) + 1); |
| 2058 | *backwards = FALSE; |
| 2059 | } |
| 2060 | return; |
| 2061 | } |
| 2062 | prev = ptr; |
| 2063 | ptr += mb_ptr2len(ptr) + 1; |
| 2064 | if (mb_ptr2char(ptr) == *initc) |
| 2065 | { |
| 2066 | if (switchit) |
| 2067 | { |
| 2068 | *findc = *initc; |
| 2069 | *initc = mb_ptr2char(prev); |
| 2070 | *backwards = FALSE; |
| 2071 | } |
| 2072 | else |
| 2073 | { |
| 2074 | *findc = mb_ptr2char(prev); |
| 2075 | *backwards = TRUE; |
| 2076 | } |
| 2077 | return; |
| 2078 | } |
| 2079 | ptr += mb_ptr2len(ptr); |
| 2080 | } |
| 2081 | else |
| 2082 | { |
| 2083 | if (*ptr == *initc) |
| 2084 | { |
| 2085 | if (switchit) |
| 2086 | { |
| 2087 | *backwards = TRUE; |
| 2088 | *findc = *initc; |
| 2089 | *initc = ptr[2]; |
| 2090 | } |
| 2091 | else |
| 2092 | { |
| 2093 | *backwards = FALSE; |
| 2094 | *findc = ptr[2]; |
| 2095 | } |
| 2096 | return; |
| 2097 | } |
| 2098 | ptr += 2; |
| 2099 | if (*ptr == *initc) |
| 2100 | { |
| 2101 | if (switchit) |
| 2102 | { |
| 2103 | *backwards = FALSE; |
| 2104 | *findc = *initc; |
| 2105 | *initc = ptr[-2]; |
| 2106 | } |
| 2107 | else |
| 2108 | { |
| 2109 | *backwards = TRUE; |
| 2110 | *findc = ptr[-2]; |
| 2111 | } |
| 2112 | return; |
| 2113 | } |
| 2114 | ++ptr; |
| 2115 | } |
| 2116 | if (*ptr == ',') |
| 2117 | ++ptr; |
| 2118 | } |
| 2119 | } |
| 2120 | |
| 2121 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2122 | * findmatchlimit -- find the matching paren or brace, if it exists within |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 2123 | * maxtravel lines of the cursor. A maxtravel of 0 means search until falling |
| 2124 | * off the edge of the file. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2125 | * |
| 2126 | * "initc" is the character to find a match for. NUL means to find the |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 2127 | * character at or after the cursor. Special values: |
| 2128 | * '*' look for C-style comment / * |
| 2129 | * '/' look for C-style comment / *, ignoring comment-end |
| 2130 | * '#' look for preprocessor directives |
| 2131 | * 'R' look for raw string start: R"delim(text)delim" (only backwards) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2132 | * |
| 2133 | * flags: FM_BACKWARD search backwards (when initc is '/', '*' or '#') |
| 2134 | * FM_FORWARD search forwards (when initc is '/', '*' or '#') |
| 2135 | * FM_BLOCKSTOP stop at start/end of block ({ or } in column 0) |
| 2136 | * FM_SKIPCOMM skip comments (not implemented yet!) |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 2137 | * |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 2138 | * "oap" is only used to set oap->motion_type for a linewise motion, it can be |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 2139 | * NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2140 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2141 | pos_T * |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 2142 | findmatchlimit( |
| 2143 | oparg_T *oap, |
| 2144 | int initc, |
| 2145 | int flags, |
| 2146 | int maxtravel) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2147 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2148 | static pos_T pos; // current search position |
| 2149 | int findc = 0; // matching brace |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2150 | int c; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2151 | int count = 0; // cumulative number of braces |
| 2152 | int backwards = FALSE; // init for gcc |
| 2153 | int raw_string = FALSE; // search for raw string |
| 2154 | int inquote = FALSE; // TRUE when inside quotes |
| 2155 | char_u *linep; // pointer to current line |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2156 | char_u *ptr; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2157 | int do_quotes; // check for quotes in current line |
| 2158 | int at_start; // do_quotes value at start position |
| 2159 | int hash_dir = 0; // Direction searched for # things |
| 2160 | int comment_dir = 0; // Direction searched for comments |
| 2161 | pos_T match_pos; // Where last slash-star was found |
| 2162 | int start_in_quotes; // start position is in quotes |
| 2163 | int traveled = 0; // how far we've searched so far |
| 2164 | int ignore_cend = FALSE; // ignore comment end |
| 2165 | int cpo_match; // vi compatible matching |
| 2166 | int cpo_bsl; // don't recognize backslashes |
| 2167 | int match_escaped = 0; // search for escaped match |
| 2168 | int dir; // Direction to search |
| 2169 | int comment_col = MAXCOL; // start of / / comment |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2170 | int lispcomm = FALSE; // inside of Lisp-style comment |
| 2171 | int lisp = curbuf->b_p_lisp; // engage Lisp-specific hacks ;) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2172 | |
| 2173 | pos = curwin->w_cursor; |
Bram Moolenaar | c56c459 | 2013-08-14 17:45:29 +0200 | [diff] [blame] | 2174 | pos.coladd = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2175 | linep = ml_get(pos.lnum); |
| 2176 | |
| 2177 | cpo_match = (vim_strchr(p_cpo, CPO_MATCH) != NULL); |
| 2178 | cpo_bsl = (vim_strchr(p_cpo, CPO_MATCHBSL) != NULL); |
| 2179 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2180 | // Direction to search when initc is '/', '*' or '#' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2181 | if (flags & FM_BACKWARD) |
| 2182 | dir = BACKWARD; |
| 2183 | else if (flags & FM_FORWARD) |
| 2184 | dir = FORWARD; |
| 2185 | else |
| 2186 | dir = 0; |
| 2187 | |
| 2188 | /* |
| 2189 | * if initc given, look in the table for the matching character |
| 2190 | * '/' and '*' are special cases: look for start or end of comment. |
| 2191 | * When '/' is used, we ignore running backwards into an star-slash, for |
| 2192 | * "[*" command, we just want to find any comment. |
| 2193 | */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 2194 | if (initc == '/' || initc == '*' || initc == 'R') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2195 | { |
| 2196 | comment_dir = dir; |
| 2197 | if (initc == '/') |
| 2198 | ignore_cend = TRUE; |
| 2199 | backwards = (dir == FORWARD) ? FALSE : TRUE; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 2200 | raw_string = (initc == 'R'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2201 | initc = NUL; |
| 2202 | } |
| 2203 | else if (initc != '#' && initc != NUL) |
| 2204 | { |
Bram Moolenaar | 8c7694a | 2013-01-17 17:02:05 +0100 | [diff] [blame] | 2205 | find_mps_values(&initc, &findc, &backwards, TRUE); |
Connor Lane Smith | b9115da | 2021-07-31 13:31:42 +0200 | [diff] [blame] | 2206 | if (dir) |
| 2207 | backwards = (dir == FORWARD) ? FALSE : TRUE; |
Bram Moolenaar | 8c7694a | 2013-01-17 17:02:05 +0100 | [diff] [blame] | 2208 | if (findc == NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2209 | return NULL; |
| 2210 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2211 | else |
| 2212 | { |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 2213 | /* |
| 2214 | * Either initc is '#', or no initc was given and we need to look |
| 2215 | * under the cursor. |
| 2216 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2217 | if (initc == '#') |
| 2218 | { |
| 2219 | hash_dir = dir; |
| 2220 | } |
| 2221 | else |
| 2222 | { |
| 2223 | /* |
| 2224 | * initc was not given, must look for something to match under |
| 2225 | * or near the cursor. |
| 2226 | * Only check for special things when 'cpo' doesn't have '%'. |
| 2227 | */ |
| 2228 | if (!cpo_match) |
| 2229 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2230 | // Are we before or at #if, #else etc.? |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2231 | ptr = skipwhite(linep); |
| 2232 | if (*ptr == '#' && pos.col <= (colnr_T)(ptr - linep)) |
| 2233 | { |
| 2234 | ptr = skipwhite(ptr + 1); |
| 2235 | if ( STRNCMP(ptr, "if", 2) == 0 |
| 2236 | || STRNCMP(ptr, "endif", 5) == 0 |
| 2237 | || STRNCMP(ptr, "el", 2) == 0) |
| 2238 | hash_dir = 1; |
| 2239 | } |
| 2240 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2241 | // Are we on a comment? |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2242 | else if (linep[pos.col] == '/') |
| 2243 | { |
| 2244 | if (linep[pos.col + 1] == '*') |
| 2245 | { |
| 2246 | comment_dir = FORWARD; |
| 2247 | backwards = FALSE; |
| 2248 | pos.col++; |
| 2249 | } |
| 2250 | else if (pos.col > 0 && linep[pos.col - 1] == '*') |
| 2251 | { |
| 2252 | comment_dir = BACKWARD; |
| 2253 | backwards = TRUE; |
| 2254 | pos.col--; |
| 2255 | } |
| 2256 | } |
| 2257 | else if (linep[pos.col] == '*') |
| 2258 | { |
| 2259 | if (linep[pos.col + 1] == '/') |
| 2260 | { |
| 2261 | comment_dir = BACKWARD; |
| 2262 | backwards = TRUE; |
| 2263 | } |
| 2264 | else if (pos.col > 0 && linep[pos.col - 1] == '/') |
| 2265 | { |
| 2266 | comment_dir = FORWARD; |
| 2267 | backwards = FALSE; |
| 2268 | } |
| 2269 | } |
| 2270 | } |
| 2271 | |
| 2272 | /* |
| 2273 | * If we are not on a comment or the # at the start of a line, then |
| 2274 | * look for brace anywhere on this line after the cursor. |
| 2275 | */ |
| 2276 | if (!hash_dir && !comment_dir) |
| 2277 | { |
| 2278 | /* |
| 2279 | * Find the brace under or after the cursor. |
| 2280 | * If beyond the end of the line, use the last character in |
| 2281 | * the line. |
| 2282 | */ |
| 2283 | if (linep[pos.col] == NUL && pos.col) |
| 2284 | --pos.col; |
| 2285 | for (;;) |
| 2286 | { |
Bram Moolenaar | 8c7694a | 2013-01-17 17:02:05 +0100 | [diff] [blame] | 2287 | initc = PTR2CHAR(linep + pos.col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2288 | if (initc == NUL) |
| 2289 | break; |
| 2290 | |
Bram Moolenaar | 8c7694a | 2013-01-17 17:02:05 +0100 | [diff] [blame] | 2291 | find_mps_values(&initc, &findc, &backwards, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2292 | if (findc) |
| 2293 | break; |
Bram Moolenaar | 1614a14 | 2019-10-06 22:00:13 +0200 | [diff] [blame] | 2294 | pos.col += mb_ptr2len(linep + pos.col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2295 | } |
| 2296 | if (!findc) |
| 2297 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2298 | // no brace in the line, maybe use " #if" then |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2299 | if (!cpo_match && *skipwhite(linep) == '#') |
| 2300 | hash_dir = 1; |
| 2301 | else |
| 2302 | return NULL; |
| 2303 | } |
| 2304 | else if (!cpo_bsl) |
| 2305 | { |
| 2306 | int col, bslcnt = 0; |
| 2307 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2308 | // Set "match_escaped" if there are an odd number of |
| 2309 | // backslashes. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2310 | for (col = pos.col; check_prevcol(linep, col, '\\', &col);) |
| 2311 | bslcnt++; |
| 2312 | match_escaped = (bslcnt & 1); |
| 2313 | } |
| 2314 | } |
| 2315 | } |
| 2316 | if (hash_dir) |
| 2317 | { |
| 2318 | /* |
| 2319 | * Look for matching #if, #else, #elif, or #endif |
| 2320 | */ |
| 2321 | if (oap != NULL) |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2322 | oap->motion_type = MLINE; // Linewise for this case only |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2323 | if (initc != '#') |
| 2324 | { |
| 2325 | ptr = skipwhite(skipwhite(linep) + 1); |
| 2326 | if (STRNCMP(ptr, "if", 2) == 0 || STRNCMP(ptr, "el", 2) == 0) |
| 2327 | hash_dir = 1; |
| 2328 | else if (STRNCMP(ptr, "endif", 5) == 0) |
| 2329 | hash_dir = -1; |
| 2330 | else |
| 2331 | return NULL; |
| 2332 | } |
| 2333 | pos.col = 0; |
| 2334 | while (!got_int) |
| 2335 | { |
| 2336 | if (hash_dir > 0) |
| 2337 | { |
| 2338 | if (pos.lnum == curbuf->b_ml.ml_line_count) |
| 2339 | break; |
| 2340 | } |
| 2341 | else if (pos.lnum == 1) |
| 2342 | break; |
| 2343 | pos.lnum += hash_dir; |
| 2344 | linep = ml_get(pos.lnum); |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2345 | line_breakcheck(); // check for CTRL-C typed |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2346 | ptr = skipwhite(linep); |
| 2347 | if (*ptr != '#') |
| 2348 | continue; |
| 2349 | pos.col = (colnr_T) (ptr - linep); |
| 2350 | ptr = skipwhite(ptr + 1); |
| 2351 | if (hash_dir > 0) |
| 2352 | { |
| 2353 | if (STRNCMP(ptr, "if", 2) == 0) |
| 2354 | count++; |
| 2355 | else if (STRNCMP(ptr, "el", 2) == 0) |
| 2356 | { |
| 2357 | if (count == 0) |
| 2358 | return &pos; |
| 2359 | } |
| 2360 | else if (STRNCMP(ptr, "endif", 5) == 0) |
| 2361 | { |
| 2362 | if (count == 0) |
| 2363 | return &pos; |
| 2364 | count--; |
| 2365 | } |
| 2366 | } |
| 2367 | else |
| 2368 | { |
| 2369 | if (STRNCMP(ptr, "if", 2) == 0) |
| 2370 | { |
| 2371 | if (count == 0) |
| 2372 | return &pos; |
| 2373 | count--; |
| 2374 | } |
| 2375 | else if (initc == '#' && STRNCMP(ptr, "el", 2) == 0) |
| 2376 | { |
| 2377 | if (count == 0) |
| 2378 | return &pos; |
| 2379 | } |
| 2380 | else if (STRNCMP(ptr, "endif", 5) == 0) |
| 2381 | count++; |
| 2382 | } |
| 2383 | } |
| 2384 | return NULL; |
| 2385 | } |
| 2386 | } |
| 2387 | |
| 2388 | #ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2389 | // This is just guessing: when 'rightleft' is set, search for a matching |
| 2390 | // paren/brace in the other direction. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2391 | if (curwin->w_p_rl && vim_strchr((char_u *)"()[]{}<>", initc) != NULL) |
| 2392 | backwards = !backwards; |
| 2393 | #endif |
| 2394 | |
| 2395 | do_quotes = -1; |
| 2396 | start_in_quotes = MAYBE; |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 2397 | CLEAR_POS(&match_pos); |
Bram Moolenaar | fd2ac76 | 2006-03-01 22:09:21 +0000 | [diff] [blame] | 2398 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2399 | // backward search: Check if this line contains a single-line comment |
Bram Moolenaar | 8e145b8 | 2022-05-21 20:17:31 +0100 | [diff] [blame] | 2400 | if ((backwards && comment_dir) || lisp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2401 | comment_col = check_linecomment(linep); |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2402 | if (lisp && comment_col != MAXCOL && pos.col > (colnr_T)comment_col) |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2403 | lispcomm = TRUE; // find match inside this comment |
Bram Moolenaar | 8e145b8 | 2022-05-21 20:17:31 +0100 | [diff] [blame] | 2404 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2405 | while (!got_int) |
| 2406 | { |
| 2407 | /* |
| 2408 | * Go to the next position, forward or backward. We could use |
| 2409 | * inc() and dec() here, but that is much slower |
| 2410 | */ |
| 2411 | if (backwards) |
| 2412 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2413 | // char to match is inside of comment, don't search outside |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2414 | if (lispcomm && pos.col < (colnr_T)comment_col) |
| 2415 | break; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2416 | if (pos.col == 0) // at start of line, go to prev. one |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2417 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2418 | if (pos.lnum == 1) // start of file |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2419 | break; |
| 2420 | --pos.lnum; |
| 2421 | |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 2422 | if (maxtravel > 0 && ++traveled > maxtravel) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2423 | break; |
| 2424 | |
| 2425 | linep = ml_get(pos.lnum); |
zeertzjq | 94b7c32 | 2024-03-12 21:50:32 +0100 | [diff] [blame] | 2426 | pos.col = ml_get_len(pos.lnum); // pos.col on trailing NUL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2427 | do_quotes = -1; |
| 2428 | line_breakcheck(); |
| 2429 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2430 | // Check if this line contains a single-line comment |
Bram Moolenaar | 8e145b8 | 2022-05-21 20:17:31 +0100 | [diff] [blame] | 2431 | if (comment_dir || lisp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2432 | comment_col = check_linecomment(linep); |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2433 | // skip comment |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2434 | if (lisp && comment_col != MAXCOL) |
| 2435 | pos.col = comment_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2436 | } |
| 2437 | else |
| 2438 | { |
| 2439 | --pos.col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2440 | if (has_mbyte) |
| 2441 | pos.col -= (*mb_head_off)(linep, linep + pos.col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2442 | } |
| 2443 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2444 | else // forward search |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2445 | { |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2446 | if (linep[pos.col] == NUL |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2447 | // at end of line, go to next one |
Bram Moolenaar | 8e145b8 | 2022-05-21 20:17:31 +0100 | [diff] [blame] | 2448 | // For lisp don't search for match in comment |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2449 | || (lisp && comment_col != MAXCOL |
Bram Moolenaar | 8e145b8 | 2022-05-21 20:17:31 +0100 | [diff] [blame] | 2450 | && pos.col == (colnr_T)comment_col)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2451 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2452 | if (pos.lnum == curbuf->b_ml.ml_line_count // end of file |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2453 | // line is exhausted and comment with it, |
| 2454 | // don't search for match in code |
Bram Moolenaar | 8e145b8 | 2022-05-21 20:17:31 +0100 | [diff] [blame] | 2455 | || lispcomm) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2456 | break; |
| 2457 | ++pos.lnum; |
| 2458 | |
| 2459 | if (maxtravel && traveled++ > maxtravel) |
| 2460 | break; |
| 2461 | |
| 2462 | linep = ml_get(pos.lnum); |
| 2463 | pos.col = 0; |
| 2464 | do_quotes = -1; |
| 2465 | line_breakcheck(); |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2466 | if (lisp) // find comment pos in new line |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2467 | comment_col = check_linecomment(linep); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2468 | } |
| 2469 | else |
| 2470 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2471 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2472 | pos.col += (*mb_ptr2len)(linep + pos.col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2473 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2474 | ++pos.col; |
| 2475 | } |
| 2476 | } |
| 2477 | |
| 2478 | /* |
| 2479 | * If FM_BLOCKSTOP given, stop at a '{' or '}' in column 0. |
| 2480 | */ |
Dominique Pelle | 7765f5c | 2022-04-10 11:26:53 +0100 | [diff] [blame] | 2481 | if (pos.col == 0 && (flags & FM_BLOCKSTOP) |
| 2482 | && (linep[0] == '{' || linep[0] == '}')) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2483 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2484 | if (linep[0] == findc && count == 0) // match! |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2485 | return &pos; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2486 | break; // out of scope |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2487 | } |
| 2488 | |
| 2489 | if (comment_dir) |
| 2490 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2491 | // Note: comments do not nest, and we ignore quotes in them |
| 2492 | // TODO: ignore comment brackets inside strings |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2493 | if (comment_dir == FORWARD) |
| 2494 | { |
| 2495 | if (linep[pos.col] == '*' && linep[pos.col + 1] == '/') |
| 2496 | { |
| 2497 | pos.col++; |
| 2498 | return &pos; |
| 2499 | } |
| 2500 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2501 | else // Searching backwards |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2502 | { |
| 2503 | /* |
| 2504 | * A comment may contain / * or / /, it may also start or end |
Bram Moolenaar | f8c53d3 | 2017-11-12 15:36:38 +0100 | [diff] [blame] | 2505 | * with / * /. Ignore a / * after / / and after *. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2506 | */ |
| 2507 | if (pos.col == 0) |
| 2508 | continue; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 2509 | else if (raw_string) |
| 2510 | { |
| 2511 | if (linep[pos.col - 1] == 'R' |
| 2512 | && linep[pos.col] == '"' |
| 2513 | && vim_strchr(linep + pos.col + 1, '(') != NULL) |
| 2514 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2515 | // Possible start of raw string. Now that we have the |
| 2516 | // delimiter we can check if it ends before where we |
| 2517 | // started searching, or before the previously found |
| 2518 | // raw string start. |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 2519 | if (!find_rawstring_end(linep, &pos, |
| 2520 | count > 0 ? &match_pos : &curwin->w_cursor)) |
| 2521 | { |
| 2522 | count++; |
| 2523 | match_pos = pos; |
| 2524 | match_pos.col--; |
| 2525 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2526 | linep = ml_get(pos.lnum); // may have been released |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 2527 | } |
| 2528 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2529 | else if ( linep[pos.col - 1] == '/' |
| 2530 | && linep[pos.col] == '*' |
Bram Moolenaar | f8c53d3 | 2017-11-12 15:36:38 +0100 | [diff] [blame] | 2531 | && (pos.col == 1 || linep[pos.col - 2] != '*') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2532 | && (int)pos.col < comment_col) |
| 2533 | { |
| 2534 | count++; |
| 2535 | match_pos = pos; |
| 2536 | match_pos.col--; |
| 2537 | } |
| 2538 | else if (linep[pos.col - 1] == '*' && linep[pos.col] == '/') |
| 2539 | { |
| 2540 | if (count > 0) |
| 2541 | pos = match_pos; |
| 2542 | else if (pos.col > 1 && linep[pos.col - 2] == '/' |
| 2543 | && (int)pos.col <= comment_col) |
| 2544 | pos.col -= 2; |
| 2545 | else if (ignore_cend) |
| 2546 | continue; |
| 2547 | else |
| 2548 | return NULL; |
| 2549 | return &pos; |
| 2550 | } |
| 2551 | } |
| 2552 | continue; |
| 2553 | } |
| 2554 | |
| 2555 | /* |
| 2556 | * If smart matching ('cpoptions' does not contain '%'), braces inside |
| 2557 | * of quotes are ignored, but only if there is an even number of |
| 2558 | * quotes in the line. |
| 2559 | */ |
| 2560 | if (cpo_match) |
| 2561 | do_quotes = 0; |
| 2562 | else if (do_quotes == -1) |
| 2563 | { |
| 2564 | /* |
| 2565 | * Count the number of quotes in the line, skipping \" and '"'. |
| 2566 | * Watch out for "\\". |
| 2567 | */ |
| 2568 | at_start = do_quotes; |
| 2569 | for (ptr = linep; *ptr; ++ptr) |
| 2570 | { |
| 2571 | if (ptr == linep + pos.col + backwards) |
| 2572 | at_start = (do_quotes & 1); |
| 2573 | if (*ptr == '"' |
| 2574 | && (ptr == linep || ptr[-1] != '\'' || ptr[1] != '\'')) |
| 2575 | ++do_quotes; |
| 2576 | if (*ptr == '\\' && ptr[1] != NUL) |
| 2577 | ++ptr; |
| 2578 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2579 | do_quotes &= 1; // result is 1 with even number of quotes |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2580 | |
| 2581 | /* |
| 2582 | * If we find an uneven count, check current line and previous |
| 2583 | * one for a '\' at the end. |
| 2584 | */ |
| 2585 | if (!do_quotes) |
| 2586 | { |
| 2587 | inquote = FALSE; |
| 2588 | if (ptr[-1] == '\\') |
| 2589 | { |
| 2590 | do_quotes = 1; |
| 2591 | if (start_in_quotes == MAYBE) |
| 2592 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2593 | // Do we need to use at_start here? |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2594 | inquote = TRUE; |
| 2595 | start_in_quotes = TRUE; |
| 2596 | } |
| 2597 | else if (backwards) |
| 2598 | inquote = TRUE; |
| 2599 | } |
| 2600 | if (pos.lnum > 1) |
| 2601 | { |
| 2602 | ptr = ml_get(pos.lnum - 1); |
zeertzjq | 94b7c32 | 2024-03-12 21:50:32 +0100 | [diff] [blame] | 2603 | if (*ptr && *(ptr + ml_get_len(pos.lnum - 1) - 1) == '\\') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2604 | { |
| 2605 | do_quotes = 1; |
| 2606 | if (start_in_quotes == MAYBE) |
| 2607 | { |
| 2608 | inquote = at_start; |
| 2609 | if (inquote) |
| 2610 | start_in_quotes = TRUE; |
| 2611 | } |
| 2612 | else if (!backwards) |
| 2613 | inquote = TRUE; |
| 2614 | } |
Bram Moolenaar | aec1179 | 2007-07-10 11:09:36 +0000 | [diff] [blame] | 2615 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2616 | // ml_get() only keeps one line, need to get linep again |
Bram Moolenaar | aec1179 | 2007-07-10 11:09:36 +0000 | [diff] [blame] | 2617 | linep = ml_get(pos.lnum); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2618 | } |
| 2619 | } |
| 2620 | } |
| 2621 | if (start_in_quotes == MAYBE) |
| 2622 | start_in_quotes = FALSE; |
| 2623 | |
| 2624 | /* |
| 2625 | * If 'smartmatch' is set: |
| 2626 | * Things inside quotes are ignored by setting 'inquote'. If we |
| 2627 | * find a quote without a preceding '\' invert 'inquote'. At the |
| 2628 | * end of a line not ending in '\' we reset 'inquote'. |
| 2629 | * |
| 2630 | * In lines with an uneven number of quotes (without preceding '\') |
| 2631 | * we do not know which part to ignore. Therefore we only set |
| 2632 | * inquote if the number of quotes in a line is even, unless this |
| 2633 | * line or the previous one ends in a '\'. Complicated, isn't it? |
| 2634 | */ |
Bram Moolenaar | 8c7694a | 2013-01-17 17:02:05 +0100 | [diff] [blame] | 2635 | c = PTR2CHAR(linep + pos.col); |
| 2636 | switch (c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2637 | { |
| 2638 | case NUL: |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2639 | // at end of line without trailing backslash, reset inquote |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2640 | if (pos.col == 0 || linep[pos.col - 1] != '\\') |
| 2641 | { |
| 2642 | inquote = FALSE; |
| 2643 | start_in_quotes = FALSE; |
| 2644 | } |
| 2645 | break; |
| 2646 | |
| 2647 | case '"': |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2648 | // a quote that is preceded with an odd number of backslashes is |
| 2649 | // ignored |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2650 | if (do_quotes) |
| 2651 | { |
| 2652 | int col; |
| 2653 | |
| 2654 | for (col = pos.col - 1; col >= 0; --col) |
| 2655 | if (linep[col] != '\\') |
| 2656 | break; |
| 2657 | if ((((int)pos.col - 1 - col) & 1) == 0) |
| 2658 | { |
| 2659 | inquote = !inquote; |
| 2660 | start_in_quotes = FALSE; |
| 2661 | } |
| 2662 | } |
| 2663 | break; |
| 2664 | |
| 2665 | /* |
| 2666 | * If smart matching ('cpoptions' does not contain '%'): |
| 2667 | * Skip things in single quotes: 'x' or '\x'. Be careful for single |
| 2668 | * single quotes, eg jon's. Things like '\233' or '\x3f' are not |
| 2669 | * skipped, there is never a brace in them. |
| 2670 | * Ignore this when finding matches for `'. |
| 2671 | */ |
| 2672 | case '\'': |
| 2673 | if (!cpo_match && initc != '\'' && findc != '\'') |
| 2674 | { |
| 2675 | if (backwards) |
| 2676 | { |
| 2677 | if (pos.col > 1) |
| 2678 | { |
| 2679 | if (linep[pos.col - 2] == '\'') |
| 2680 | { |
| 2681 | pos.col -= 2; |
| 2682 | break; |
| 2683 | } |
Dominique Pelle | 7765f5c | 2022-04-10 11:26:53 +0100 | [diff] [blame] | 2684 | else if (linep[pos.col - 2] == '\\' |
| 2685 | && pos.col > 2 && linep[pos.col - 3] == '\'') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2686 | { |
| 2687 | pos.col -= 3; |
| 2688 | break; |
| 2689 | } |
| 2690 | } |
| 2691 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2692 | else if (linep[pos.col + 1]) // forward search |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2693 | { |
Dominique Pelle | 7765f5c | 2022-04-10 11:26:53 +0100 | [diff] [blame] | 2694 | if (linep[pos.col + 1] == '\\' |
| 2695 | && linep[pos.col + 2] && linep[pos.col + 3] == '\'') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2696 | { |
| 2697 | pos.col += 3; |
| 2698 | break; |
| 2699 | } |
| 2700 | else if (linep[pos.col + 2] == '\'') |
| 2701 | { |
| 2702 | pos.col += 2; |
| 2703 | break; |
| 2704 | } |
| 2705 | } |
| 2706 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2707 | // FALLTHROUGH |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2708 | |
| 2709 | default: |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2710 | /* |
| 2711 | * For Lisp skip over backslashed (), {} and []. |
| 2712 | * (actually, we skip #\( et al) |
| 2713 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2714 | if (curbuf->b_p_lisp |
Bram Moolenaar | ebfec1c | 2023-01-22 21:14:53 +0000 | [diff] [blame] | 2715 | && vim_strchr((char_u *)"{}()[]", c) != NULL |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2716 | && pos.col > 1 |
| 2717 | && check_prevcol(linep, pos.col, '\\', NULL) |
| 2718 | && check_prevcol(linep, pos.col - 1, '#', NULL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2719 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2720 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2721 | // Check for match outside of quotes, and inside of |
| 2722 | // quotes when the start is also inside of quotes. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2723 | if ((!inquote || start_in_quotes == TRUE) |
| 2724 | && (c == initc || c == findc)) |
| 2725 | { |
| 2726 | int col, bslcnt = 0; |
| 2727 | |
| 2728 | if (!cpo_bsl) |
| 2729 | { |
| 2730 | for (col = pos.col; check_prevcol(linep, col, '\\', &col);) |
| 2731 | bslcnt++; |
| 2732 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2733 | // Only accept a match when 'M' is in 'cpo' or when escaping |
| 2734 | // is what we expect. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2735 | if (cpo_bsl || (bslcnt & 1) == match_escaped) |
| 2736 | { |
| 2737 | if (c == initc) |
| 2738 | count++; |
| 2739 | else |
| 2740 | { |
| 2741 | if (count == 0) |
| 2742 | return &pos; |
| 2743 | count--; |
| 2744 | } |
| 2745 | } |
| 2746 | } |
| 2747 | } |
| 2748 | } |
| 2749 | |
| 2750 | if (comment_dir == BACKWARD && count > 0) |
| 2751 | { |
| 2752 | pos = match_pos; |
| 2753 | return &pos; |
| 2754 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2755 | return (pos_T *)NULL; // never found it |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2756 | } |
| 2757 | |
| 2758 | /* |
| 2759 | * Check if line[] contains a / / comment. |
| 2760 | * Return MAXCOL if not, otherwise return the column. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2761 | */ |
Bram Moolenaar | 6e371ec | 2021-12-12 14:16:39 +0000 | [diff] [blame] | 2762 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 2763 | check_linecomment(char_u *line) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2764 | { |
| 2765 | char_u *p; |
| 2766 | |
| 2767 | p = line; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2768 | // skip Lispish one-line comments |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2769 | if (curbuf->b_p_lisp) |
| 2770 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2771 | if (vim_strchr(p, ';') != NULL) // there may be comments |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2772 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2773 | int in_str = FALSE; // inside of string |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2774 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2775 | p = line; // scan from start |
Bram Moolenaar | 520470a | 2005-06-16 21:59:56 +0000 | [diff] [blame] | 2776 | while ((p = vim_strpbrk(p, (char_u *)"\";")) != NULL) |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2777 | { |
| 2778 | if (*p == '"') |
| 2779 | { |
Bram Moolenaar | 70b2a56 | 2012-01-10 22:26:17 +0100 | [diff] [blame] | 2780 | if (in_str) |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2781 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2782 | if (*(p - 1) != '\\') // skip escaped quote |
Bram Moolenaar | 70b2a56 | 2012-01-10 22:26:17 +0100 | [diff] [blame] | 2783 | in_str = FALSE; |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2784 | } |
| 2785 | else if (p == line || ((p - line) >= 2 |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2786 | // skip #\" form |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2787 | && *(p - 1) != '\\' && *(p - 2) != '#')) |
Bram Moolenaar | 70b2a56 | 2012-01-10 22:26:17 +0100 | [diff] [blame] | 2788 | in_str = TRUE; |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2789 | } |
Bram Moolenaar | 70b2a56 | 2012-01-10 22:26:17 +0100 | [diff] [blame] | 2790 | else if (!in_str && ((p - line) < 2 |
Bram Moolenaar | ba26367 | 2021-12-29 18:09:13 +0000 | [diff] [blame] | 2791 | || (*(p - 1) != '\\' && *(p - 2) != '#')) |
| 2792 | && !is_pos_in_string(line, (colnr_T)(p - line))) |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2793 | break; // found! |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2794 | ++p; |
| 2795 | } |
| 2796 | } |
| 2797 | else |
| 2798 | p = NULL; |
| 2799 | } |
| 2800 | else |
Bram Moolenaar | 8e145b8 | 2022-05-21 20:17:31 +0100 | [diff] [blame] | 2801 | while ((p = vim_strchr(p, '/')) != NULL) |
| 2802 | { |
| 2803 | // Accept a double /, unless it's preceded with * and followed by |
| 2804 | // *, because * / / * is an end and start of a C comment. Only |
| 2805 | // accept the position if it is not inside a string. |
| 2806 | if (p[1] == '/' && (p == line || p[-1] != '*' || p[2] != '*') |
Bram Moolenaar | ba26367 | 2021-12-29 18:09:13 +0000 | [diff] [blame] | 2807 | && !is_pos_in_string(line, (colnr_T)(p - line))) |
Bram Moolenaar | 8e145b8 | 2022-05-21 20:17:31 +0100 | [diff] [blame] | 2808 | break; |
| 2809 | ++p; |
| 2810 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2811 | |
| 2812 | if (p == NULL) |
| 2813 | return MAXCOL; |
| 2814 | return (int)(p - line); |
| 2815 | } |
| 2816 | |
| 2817 | /* |
| 2818 | * Move cursor briefly to character matching the one under the cursor. |
| 2819 | * Used for Insert mode and "r" command. |
| 2820 | * Show the match only if it is visible on the screen. |
| 2821 | * If there isn't a match, then beep. |
| 2822 | */ |
| 2823 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 2824 | showmatch( |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2825 | int c) // char to show match for |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2826 | { |
| 2827 | pos_T *lpos, save_cursor; |
| 2828 | pos_T mpos; |
| 2829 | colnr_T vcol; |
| 2830 | long save_so; |
| 2831 | long save_siso; |
| 2832 | #ifdef CURSOR_SHAPE |
| 2833 | int save_state; |
| 2834 | #endif |
| 2835 | colnr_T save_dollar_vcol; |
| 2836 | char_u *p; |
Bram Moolenaar | 6ed545e | 2022-05-09 20:09:23 +0100 | [diff] [blame] | 2837 | long *so = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so; |
| 2838 | long *siso = curwin->w_p_siso >= 0 ? &curwin->w_p_siso : &p_siso; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2839 | |
| 2840 | /* |
| 2841 | * Only show match for chars in the 'matchpairs' option. |
| 2842 | */ |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2843 | // 'matchpairs' is "x:y,x:y" |
Bram Moolenaar | 8c7694a | 2013-01-17 17:02:05 +0100 | [diff] [blame] | 2844 | for (p = curbuf->b_p_mps; *p != NUL; ++p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2845 | { |
| 2846 | #ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | 187d3ac | 2013-02-20 18:39:13 +0100 | [diff] [blame] | 2847 | if (PTR2CHAR(p) == c && (curwin->w_p_rl ^ p_ri)) |
Bram Moolenaar | 8c7694a | 2013-01-17 17:02:05 +0100 | [diff] [blame] | 2848 | break; |
Bram Moolenaar | 187d3ac | 2013-02-20 18:39:13 +0100 | [diff] [blame] | 2849 | #endif |
Bram Moolenaar | 1614a14 | 2019-10-06 22:00:13 +0200 | [diff] [blame] | 2850 | p += mb_ptr2len(p) + 1; |
Bram Moolenaar | 8c7694a | 2013-01-17 17:02:05 +0100 | [diff] [blame] | 2851 | if (PTR2CHAR(p) == c |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2852 | #ifdef FEAT_RIGHTLEFT |
| 2853 | && !(curwin->w_p_rl ^ p_ri) |
| 2854 | #endif |
| 2855 | ) |
| 2856 | break; |
Bram Moolenaar | 1614a14 | 2019-10-06 22:00:13 +0200 | [diff] [blame] | 2857 | p += mb_ptr2len(p); |
Bram Moolenaar | 8c7694a | 2013-01-17 17:02:05 +0100 | [diff] [blame] | 2858 | if (*p == NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2859 | return; |
| 2860 | } |
Bram Moolenaar | 5b8cabf | 2021-04-02 18:55:57 +0200 | [diff] [blame] | 2861 | if (*p == NUL) |
| 2862 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2863 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2864 | if ((lpos = findmatch(NULL, NUL)) == NULL) // no match, so beep |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2865 | { |
Yegappan Lakshmanan | 6ec6666 | 2023-01-23 20:46:21 +0000 | [diff] [blame] | 2866 | vim_beep(BO_MATCH); |
| 2867 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2868 | } |
Yegappan Lakshmanan | 6ec6666 | 2023-01-23 20:46:21 +0000 | [diff] [blame] | 2869 | |
| 2870 | if (lpos->lnum < curwin->w_topline || lpos->lnum >= curwin->w_botline) |
| 2871 | return; |
| 2872 | |
| 2873 | if (!curwin->w_p_wrap) |
| 2874 | getvcol(curwin, lpos, NULL, &vcol, NULL); |
| 2875 | |
| 2876 | int col_visible = (curwin->w_p_wrap |
| 2877 | || (vcol >= curwin->w_leftcol |
| 2878 | && vcol < curwin->w_leftcol + curwin->w_width)); |
| 2879 | if (!col_visible) |
| 2880 | return; |
| 2881 | |
| 2882 | mpos = *lpos; // save the pos, update_screen() may change it |
| 2883 | save_cursor = curwin->w_cursor; |
| 2884 | save_so = *so; |
| 2885 | save_siso = *siso; |
| 2886 | // Handle "$" in 'cpo': If the ')' is typed on top of the "$", |
| 2887 | // stop displaying the "$". |
| 2888 | if (dollar_vcol >= 0 && dollar_vcol == curwin->w_virtcol) |
| 2889 | dollar_vcol = -1; |
| 2890 | ++curwin->w_virtcol; // do display ')' just before "$" |
| 2891 | update_screen(UPD_VALID); // show the new char first |
| 2892 | |
| 2893 | save_dollar_vcol = dollar_vcol; |
| 2894 | #ifdef CURSOR_SHAPE |
| 2895 | save_state = State; |
| 2896 | State = MODE_SHOWMATCH; |
| 2897 | ui_cursor_shape(); // may show different cursor shape |
| 2898 | #endif |
| 2899 | curwin->w_cursor = mpos; // move to matching char |
| 2900 | *so = 0; // don't use 'scrolloff' here |
| 2901 | *siso = 0; // don't use 'sidescrolloff' here |
| 2902 | showruler(FALSE); |
| 2903 | setcursor(); |
| 2904 | cursor_on(); // make sure that the cursor is shown |
| 2905 | out_flush_cursor(TRUE, FALSE); |
| 2906 | |
| 2907 | // Restore dollar_vcol(), because setcursor() may call curs_rows() |
| 2908 | // which resets it if the matching position is in a previous line |
| 2909 | // and has a higher column number. |
| 2910 | dollar_vcol = save_dollar_vcol; |
| 2911 | |
| 2912 | /* |
| 2913 | * brief pause, unless 'm' is present in 'cpo' and a character is |
| 2914 | * available. |
| 2915 | */ |
| 2916 | if (vim_strchr(p_cpo, CPO_SHOWMATCH) != NULL) |
| 2917 | ui_delay(p_mat * 100L + 8, TRUE); |
| 2918 | else if (!char_avail()) |
| 2919 | ui_delay(p_mat * 100L + 9, FALSE); |
| 2920 | curwin->w_cursor = save_cursor; // restore cursor position |
| 2921 | *so = save_so; |
| 2922 | *siso = save_siso; |
| 2923 | #ifdef CURSOR_SHAPE |
| 2924 | State = save_state; |
| 2925 | ui_cursor_shape(); // may show different cursor shape |
| 2926 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2927 | } |
| 2928 | |
| 2929 | /* |
Bram Moolenaar | 453c192 | 2019-10-26 14:42:09 +0200 | [diff] [blame] | 2930 | * Check if the pattern is zero-width. |
Bram Moolenaar | edaad6e | 2019-10-24 15:23:37 +0200 | [diff] [blame] | 2931 | * If move is TRUE, check from the beginning of the buffer, else from position |
| 2932 | * "cur". |
| 2933 | * "direction" is FORWARD or BACKWARD. |
| 2934 | * Returns TRUE, FALSE or -1 for failure. |
| 2935 | */ |
| 2936 | static int |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 2937 | is_zero_width(char_u *pattern, size_t patternlen, int move, pos_T *cur, int direction) |
Bram Moolenaar | edaad6e | 2019-10-24 15:23:37 +0200 | [diff] [blame] | 2938 | { |
| 2939 | regmmatch_T regmatch; |
| 2940 | int nmatched = 0; |
| 2941 | int result = -1; |
| 2942 | pos_T pos; |
Bram Moolenaar | 5398955 | 2019-12-23 22:59:18 +0100 | [diff] [blame] | 2943 | int called_emsg_before = called_emsg; |
Bram Moolenaar | edaad6e | 2019-10-24 15:23:37 +0200 | [diff] [blame] | 2944 | int flag = 0; |
| 2945 | |
| 2946 | if (pattern == NULL) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 2947 | { |
Bram Moolenaar | edaad6e | 2019-10-24 15:23:37 +0200 | [diff] [blame] | 2948 | pattern = spats[last_idx].pat; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 2949 | patternlen = spats[last_idx].patlen; |
| 2950 | } |
Bram Moolenaar | edaad6e | 2019-10-24 15:23:37 +0200 | [diff] [blame] | 2951 | |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 2952 | if (search_regcomp(pattern, patternlen, NULL, RE_SEARCH, RE_SEARCH, |
Bram Moolenaar | edaad6e | 2019-10-24 15:23:37 +0200 | [diff] [blame] | 2953 | SEARCH_KEEP, ®match) == FAIL) |
| 2954 | return -1; |
| 2955 | |
| 2956 | // init startcol correctly |
| 2957 | regmatch.startpos[0].col = -1; |
| 2958 | // move to match |
| 2959 | if (move) |
| 2960 | { |
| 2961 | CLEAR_POS(&pos); |
| 2962 | } |
| 2963 | else |
| 2964 | { |
| 2965 | pos = *cur; |
| 2966 | // accept a match at the cursor position |
| 2967 | flag = SEARCH_START; |
| 2968 | } |
| 2969 | |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 2970 | if (searchit(curwin, curbuf, &pos, NULL, direction, pattern, patternlen, 1, |
Bram Moolenaar | edaad6e | 2019-10-24 15:23:37 +0200 | [diff] [blame] | 2971 | SEARCH_KEEP + flag, RE_SEARCH, NULL) != FAIL) |
| 2972 | { |
| 2973 | // Zero-width pattern should match somewhere, then we can check if |
| 2974 | // start and end are in the same position. |
Bram Moolenaar | edaad6e | 2019-10-24 15:23:37 +0200 | [diff] [blame] | 2975 | do |
| 2976 | { |
| 2977 | regmatch.startpos[0].col++; |
| 2978 | nmatched = vim_regexec_multi(®match, curwin, curbuf, |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 2979 | pos.lnum, regmatch.startpos[0].col, NULL); |
Bram Moolenaar | edaad6e | 2019-10-24 15:23:37 +0200 | [diff] [blame] | 2980 | if (nmatched != 0) |
| 2981 | break; |
Bram Moolenaar | 795aaa1 | 2020-10-02 20:36:01 +0200 | [diff] [blame] | 2982 | } while (regmatch.regprog != NULL |
| 2983 | && direction == FORWARD ? regmatch.startpos[0].col < pos.col |
Bram Moolenaar | edaad6e | 2019-10-24 15:23:37 +0200 | [diff] [blame] | 2984 | : regmatch.startpos[0].col > pos.col); |
| 2985 | |
Bram Moolenaar | 5398955 | 2019-12-23 22:59:18 +0100 | [diff] [blame] | 2986 | if (called_emsg == called_emsg_before) |
Bram Moolenaar | edaad6e | 2019-10-24 15:23:37 +0200 | [diff] [blame] | 2987 | { |
| 2988 | result = (nmatched != 0 |
| 2989 | && regmatch.startpos[0].lnum == regmatch.endpos[0].lnum |
| 2990 | && regmatch.startpos[0].col == regmatch.endpos[0].col); |
| 2991 | } |
| 2992 | } |
| 2993 | |
Bram Moolenaar | edaad6e | 2019-10-24 15:23:37 +0200 | [diff] [blame] | 2994 | vim_regfree(regmatch.regprog); |
| 2995 | return result; |
| 2996 | } |
| 2997 | |
Bram Moolenaar | dde0efe | 2012-08-23 15:53:05 +0200 | [diff] [blame] | 2998 | |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 2999 | /* |
| 3000 | * Find next search match under cursor, cursor at end. |
| 3001 | * Used while an operator is pending, and in Visual mode. |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3002 | */ |
| 3003 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 3004 | current_search( |
| 3005 | long count, |
Bram Moolenaar | 5d24a22 | 2018-12-23 19:10:09 +0100 | [diff] [blame] | 3006 | int forward) // TRUE for forward, FALSE for backward |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3007 | { |
Bram Moolenaar | 5d24a22 | 2018-12-23 19:10:09 +0100 | [diff] [blame] | 3008 | pos_T start_pos; // start position of the pattern match |
| 3009 | pos_T end_pos; // end position of the pattern match |
| 3010 | pos_T orig_pos; // position of the cursor at beginning |
| 3011 | pos_T pos; // position after the pattern |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3012 | int i; |
| 3013 | int dir; |
Bram Moolenaar | 5d24a22 | 2018-12-23 19:10:09 +0100 | [diff] [blame] | 3014 | int result; // result of various function calls |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3015 | char_u old_p_ws = p_ws; |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3016 | int flags = 0; |
Bram Moolenaar | de9149e | 2013-07-17 19:22:13 +0200 | [diff] [blame] | 3017 | pos_T save_VIsual = VIsual; |
Bram Moolenaar | edaad6e | 2019-10-24 15:23:37 +0200 | [diff] [blame] | 3018 | int zero_width; |
Bram Moolenaar | c07b7f7 | 2020-10-11 20:44:15 +0200 | [diff] [blame] | 3019 | int skip_first_backward; |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3020 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3021 | // Correct cursor when 'selection' is exclusive |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 3022 | if (VIsual_active && *p_sel == 'e' && LT_POS(VIsual, curwin->w_cursor)) |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3023 | dec_cursor(); |
| 3024 | |
Bram Moolenaar | c07b7f7 | 2020-10-11 20:44:15 +0200 | [diff] [blame] | 3025 | // When searching forward and the cursor is at the start of the Visual |
| 3026 | // area, skip the first search backward, otherwise it doesn't move. |
| 3027 | skip_first_backward = forward && VIsual_active |
| 3028 | && LT_POS(curwin->w_cursor, VIsual); |
| 3029 | |
Bram Moolenaar | edaad6e | 2019-10-24 15:23:37 +0200 | [diff] [blame] | 3030 | orig_pos = pos = curwin->w_cursor; |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3031 | if (VIsual_active) |
| 3032 | { |
Bram Moolenaar | edaad6e | 2019-10-24 15:23:37 +0200 | [diff] [blame] | 3033 | if (forward) |
| 3034 | incl(&pos); |
| 3035 | else |
| 3036 | decl(&pos); |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3037 | } |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3038 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3039 | // Is the pattern is zero-width?, this time, don't care about the direction |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 3040 | zero_width = is_zero_width(spats[last_idx].pat, spats[last_idx].patlen, |
| 3041 | TRUE, &curwin->w_cursor, FORWARD); |
Bram Moolenaar | edaad6e | 2019-10-24 15:23:37 +0200 | [diff] [blame] | 3042 | if (zero_width == -1) |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3043 | return FAIL; // pattern not found |
Bram Moolenaar | ba6ba36 | 2012-08-08 15:27:57 +0200 | [diff] [blame] | 3044 | |
Bram Moolenaar | ba6ba36 | 2012-08-08 15:27:57 +0200 | [diff] [blame] | 3045 | /* |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3046 | * The trick is to first search backwards and then search forward again, |
| 3047 | * so that a match at the current cursor position will be correctly |
Bram Moolenaar | c07b7f7 | 2020-10-11 20:44:15 +0200 | [diff] [blame] | 3048 | * captured. When "forward" is false do it the other way around. |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3049 | */ |
| 3050 | for (i = 0; i < 2; i++) |
| 3051 | { |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3052 | if (forward) |
Bram Moolenaar | c07b7f7 | 2020-10-11 20:44:15 +0200 | [diff] [blame] | 3053 | { |
| 3054 | if (i == 0 && skip_first_backward) |
| 3055 | continue; |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3056 | dir = i; |
Bram Moolenaar | c07b7f7 | 2020-10-11 20:44:15 +0200 | [diff] [blame] | 3057 | } |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3058 | else |
| 3059 | dir = !i; |
Bram Moolenaar | ba6ba36 | 2012-08-08 15:27:57 +0200 | [diff] [blame] | 3060 | |
| 3061 | flags = 0; |
Bram Moolenaar | edaad6e | 2019-10-24 15:23:37 +0200 | [diff] [blame] | 3062 | if (!dir && !zero_width) |
Bram Moolenaar | ba6ba36 | 2012-08-08 15:27:57 +0200 | [diff] [blame] | 3063 | flags = SEARCH_END; |
Bram Moolenaar | 5d24a22 | 2018-12-23 19:10:09 +0100 | [diff] [blame] | 3064 | end_pos = pos; |
Bram Moolenaar | ba6ba36 | 2012-08-08 15:27:57 +0200 | [diff] [blame] | 3065 | |
Bram Moolenaar | 82cf7f6 | 2019-11-02 23:22:47 +0100 | [diff] [blame] | 3066 | // wrapping should not occur in the first round |
| 3067 | if (i == 0) |
| 3068 | p_ws = FALSE; |
| 3069 | |
Bram Moolenaar | 5d24a22 | 2018-12-23 19:10:09 +0100 | [diff] [blame] | 3070 | result = searchit(curwin, curbuf, &pos, &end_pos, |
| 3071 | (dir ? FORWARD : BACKWARD), |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 3072 | spats[last_idx].pat, spats[last_idx].patlen, (long) (i ? count : 1), |
Bram Moolenaar | 92ea26b | 2019-10-18 20:53:34 +0200 | [diff] [blame] | 3073 | SEARCH_KEEP | flags, RE_SEARCH, NULL); |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3074 | |
Bram Moolenaar | 82cf7f6 | 2019-11-02 23:22:47 +0100 | [diff] [blame] | 3075 | p_ws = old_p_ws; |
| 3076 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3077 | // First search may fail, but then start searching from the |
| 3078 | // beginning of the file (cursor might be on the search match) |
| 3079 | // except when Visual mode is active, so that extending the visual |
| 3080 | // selection works. |
| 3081 | if (i == 1 && !result) // not found, abort |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3082 | { |
| 3083 | curwin->w_cursor = orig_pos; |
| 3084 | if (VIsual_active) |
| 3085 | VIsual = save_VIsual; |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3086 | return FAIL; |
| 3087 | } |
Bram Moolenaar | 5d24a22 | 2018-12-23 19:10:09 +0100 | [diff] [blame] | 3088 | else if (i == 0 && !result) |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3089 | { |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 3090 | if (forward) |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3091 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3092 | // try again from start of buffer |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 3093 | CLEAR_POS(&pos); |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3094 | } |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 3095 | else |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3096 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3097 | // try again from end of buffer |
| 3098 | // searching backwards, so set pos to last line and col |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3099 | pos.lnum = curwin->w_buffer->b_ml.ml_line_count; |
zeertzjq | 94b7c32 | 2024-03-12 21:50:32 +0100 | [diff] [blame] | 3100 | pos.col = ml_get_len(curwin->w_buffer->b_ml.ml_line_count); |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3101 | } |
| 3102 | } |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3103 | } |
| 3104 | |
| 3105 | start_pos = pos; |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3106 | |
| 3107 | if (!VIsual_active) |
| 3108 | VIsual = start_pos; |
| 3109 | |
Bram Moolenaar | c07b7f7 | 2020-10-11 20:44:15 +0200 | [diff] [blame] | 3110 | // put the cursor after the match |
Bram Moolenaar | 5d24a22 | 2018-12-23 19:10:09 +0100 | [diff] [blame] | 3111 | curwin->w_cursor = end_pos; |
Bram Moolenaar | 453c192 | 2019-10-26 14:42:09 +0200 | [diff] [blame] | 3112 | if (LT_POS(VIsual, end_pos) && forward) |
Bram Moolenaar | c07b7f7 | 2020-10-11 20:44:15 +0200 | [diff] [blame] | 3113 | { |
| 3114 | if (skip_first_backward) |
| 3115 | // put the cursor on the start of the match |
| 3116 | curwin->w_cursor = pos; |
| 3117 | else |
| 3118 | // put the cursor on last character of match |
| 3119 | dec_cursor(); |
| 3120 | } |
Bram Moolenaar | 28f224b | 2020-10-10 16:45:25 +0200 | [diff] [blame] | 3121 | else if (VIsual_active && LT_POS(curwin->w_cursor, VIsual) && forward) |
Bram Moolenaar | edaad6e | 2019-10-24 15:23:37 +0200 | [diff] [blame] | 3122 | curwin->w_cursor = pos; // put the cursor on the start of the match |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3123 | VIsual_active = TRUE; |
| 3124 | VIsual_mode = 'v'; |
| 3125 | |
Bram Moolenaar | b763361 | 2019-02-10 21:48:25 +0100 | [diff] [blame] | 3126 | if (*p_sel == 'e') |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3127 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3128 | // Correction for exclusive selection depends on the direction. |
Bram Moolenaar | b763361 | 2019-02-10 21:48:25 +0100 | [diff] [blame] | 3129 | if (forward && LTOREQ_POS(VIsual, curwin->w_cursor)) |
| 3130 | inc_cursor(); |
| 3131 | else if (!forward && LTOREQ_POS(curwin->w_cursor, VIsual)) |
| 3132 | inc(&VIsual); |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3133 | } |
| 3134 | |
| 3135 | #ifdef FEAT_FOLDING |
| 3136 | if (fdo_flags & FDO_SEARCH && KeyTyped) |
| 3137 | foldOpenCursor(); |
| 3138 | #endif |
| 3139 | |
| 3140 | may_start_select('c'); |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3141 | setmouse(); |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3142 | #ifdef FEAT_CLIPBOARD |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3143 | // Make sure the clipboard gets updated. Needed because start and |
| 3144 | // end are still the same, and the selection needs to be owned |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3145 | clip_star.vmode = NUL; |
| 3146 | #endif |
Bram Moolenaar | a4d158b | 2022-08-14 14:17:45 +0100 | [diff] [blame] | 3147 | redraw_curbuf_later(UPD_INVERTED); |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3148 | showmode(); |
| 3149 | |
| 3150 | return OK; |
| 3151 | } |
Bram Moolenaar | dde0efe | 2012-08-23 15:53:05 +0200 | [diff] [blame] | 3152 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3153 | /* |
| 3154 | * return TRUE if line 'lnum' is empty or has white chars only. |
| 3155 | */ |
| 3156 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 3157 | linewhite(linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3158 | { |
| 3159 | char_u *p; |
| 3160 | |
| 3161 | p = skipwhite(ml_get(lnum)); |
| 3162 | return (*p == NUL); |
| 3163 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3164 | |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 3165 | /* |
| 3166 | * Add the search count "[3/19]" to "msgbuf". |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 3167 | * See update_search_stat() for other arguments. |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 3168 | */ |
| 3169 | static void |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 3170 | cmdline_search_stat( |
| 3171 | int dirc, |
| 3172 | pos_T *pos, |
| 3173 | pos_T *cursor_pos, |
| 3174 | int show_top_bot_msg, |
| 3175 | char_u *msgbuf, |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 3176 | size_t msgbuflen, |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 3177 | int recompute, |
| 3178 | int maxcount, |
| 3179 | long timeout) |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 3180 | { |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 3181 | searchstat_T stat; |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 3182 | |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 3183 | update_search_stat(dirc, pos, cursor_pos, &stat, recompute, maxcount, |
| 3184 | timeout); |
Yegappan Lakshmanan | 6ec6666 | 2023-01-23 20:46:21 +0000 | [diff] [blame] | 3185 | if (stat.cur <= 0) |
| 3186 | return; |
| 3187 | |
| 3188 | char t[SEARCH_STAT_BUF_LEN]; |
| 3189 | size_t len; |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 3190 | |
| 3191 | #ifdef FEAT_RIGHTLEFT |
Yegappan Lakshmanan | 6ec6666 | 2023-01-23 20:46:21 +0000 | [diff] [blame] | 3192 | if (curwin->w_p_rl && *curwin->w_p_rlc == 's') |
| 3193 | { |
| 3194 | if (stat.incomplete == 1) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 3195 | len = vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[?/??]"); |
Yegappan Lakshmanan | 6ec6666 | 2023-01-23 20:46:21 +0000 | [diff] [blame] | 3196 | else if (stat.cnt > maxcount && stat.cur > maxcount) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 3197 | len = vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/>%d]", |
Yegappan Lakshmanan | 6ec6666 | 2023-01-23 20:46:21 +0000 | [diff] [blame] | 3198 | maxcount, maxcount); |
| 3199 | else if (stat.cnt > maxcount) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 3200 | len = vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/%d]", |
Yegappan Lakshmanan | 6ec6666 | 2023-01-23 20:46:21 +0000 | [diff] [blame] | 3201 | maxcount, stat.cur); |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 3202 | else |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 3203 | len = vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/%d]", |
Yegappan Lakshmanan | 6ec6666 | 2023-01-23 20:46:21 +0000 | [diff] [blame] | 3204 | stat.cnt, stat.cur); |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 3205 | } |
Yegappan Lakshmanan | 6ec6666 | 2023-01-23 20:46:21 +0000 | [diff] [blame] | 3206 | else |
| 3207 | #endif |
| 3208 | { |
| 3209 | if (stat.incomplete == 1) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 3210 | len = vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[?/??]"); |
Yegappan Lakshmanan | 6ec6666 | 2023-01-23 20:46:21 +0000 | [diff] [blame] | 3211 | else if (stat.cnt > maxcount && stat.cur > maxcount) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 3212 | len = vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/>%d]", |
Yegappan Lakshmanan | 6ec6666 | 2023-01-23 20:46:21 +0000 | [diff] [blame] | 3213 | maxcount, maxcount); |
| 3214 | else if (stat.cnt > maxcount) |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 3215 | len = vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/>%d]", |
Yegappan Lakshmanan | 6ec6666 | 2023-01-23 20:46:21 +0000 | [diff] [blame] | 3216 | stat.cur, maxcount); |
| 3217 | else |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 3218 | len = vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/%d]", |
Yegappan Lakshmanan | 6ec6666 | 2023-01-23 20:46:21 +0000 | [diff] [blame] | 3219 | stat.cur, stat.cnt); |
| 3220 | } |
| 3221 | |
Yegappan Lakshmanan | 6ec6666 | 2023-01-23 20:46:21 +0000 | [diff] [blame] | 3222 | if (show_top_bot_msg && len + 2 < SEARCH_STAT_BUF_LEN) |
| 3223 | { |
| 3224 | mch_memmove(t + 2, t, len); |
| 3225 | t[0] = 'W'; |
| 3226 | t[1] = ' '; |
| 3227 | len += 2; |
| 3228 | } |
| 3229 | |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 3230 | if (len > msgbuflen) |
| 3231 | len = msgbuflen; |
| 3232 | mch_memmove(msgbuf + msgbuflen - len, t, len); |
zeertzjq | a7d36b7 | 2023-01-31 21:13:38 +0000 | [diff] [blame] | 3233 | |
Yegappan Lakshmanan | 6ec6666 | 2023-01-23 20:46:21 +0000 | [diff] [blame] | 3234 | if (dirc == '?' && stat.cur == maxcount + 1) |
| 3235 | stat.cur = -1; |
| 3236 | |
| 3237 | // keep the message even after redraw, but don't put in history |
| 3238 | msg_hist_off = TRUE; |
| 3239 | give_warning(msgbuf, FALSE); |
| 3240 | msg_hist_off = FALSE; |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 3241 | } |
| 3242 | |
| 3243 | /* |
| 3244 | * Add the search count information to "stat". |
| 3245 | * "stat" must not be NULL. |
| 3246 | * When "recompute" is TRUE always recompute the numbers. |
| 3247 | * dirc == 0: don't find the next/previous match (only set the result to "stat") |
| 3248 | * dirc == '/': find the next match |
| 3249 | * dirc == '?': find the previous match |
| 3250 | */ |
| 3251 | static void |
| 3252 | update_search_stat( |
| 3253 | int dirc, |
| 3254 | pos_T *pos, |
| 3255 | pos_T *cursor_pos, |
| 3256 | searchstat_T *stat, |
| 3257 | int recompute, |
| 3258 | int maxcount, |
Bram Moolenaar | f9ca08e | 2020-06-01 18:56:03 +0200 | [diff] [blame] | 3259 | long timeout UNUSED) |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 3260 | { |
| 3261 | int save_ws = p_ws; |
| 3262 | int wraparound = FALSE; |
| 3263 | pos_T p = (*pos); |
Bram Moolenaar | 1468162 | 2020-06-03 22:57:39 +0200 | [diff] [blame] | 3264 | static pos_T lastpos = {0, 0, 0}; |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 3265 | static int cur = 0; |
| 3266 | static int cnt = 0; |
| 3267 | static int exact_match = FALSE; |
| 3268 | static int incomplete = 0; |
| 3269 | static int last_maxcount = SEARCH_STAT_DEF_MAX_COUNT; |
| 3270 | static int chgtick = 0; |
| 3271 | static char_u *lastpat = NULL; |
| 3272 | static buf_T *lbuf = NULL; |
| 3273 | #ifdef FEAT_RELTIME |
| 3274 | proftime_T start; |
| 3275 | #endif |
| 3276 | |
Yegappan Lakshmanan | 960dcbd | 2023-03-07 17:45:11 +0000 | [diff] [blame] | 3277 | CLEAR_POINTER(stat); |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 3278 | |
| 3279 | if (dirc == 0 && !recompute && !EMPTY_POS(lastpos)) |
| 3280 | { |
| 3281 | stat->cur = cur; |
| 3282 | stat->cnt = cnt; |
| 3283 | stat->exact_match = exact_match; |
| 3284 | stat->incomplete = incomplete; |
| 3285 | stat->last_maxcount = last_maxcount; |
| 3286 | return; |
| 3287 | } |
| 3288 | last_maxcount = maxcount; |
| 3289 | |
| 3290 | wraparound = ((dirc == '?' && LT_POS(lastpos, p)) |
| 3291 | || (dirc == '/' && LT_POS(p, lastpos))); |
| 3292 | |
| 3293 | // If anything relevant changed the count has to be recomputed. |
| 3294 | // MB_STRNICMP ignores case, but we should not ignore case. |
| 3295 | // Unfortunately, there is no MB_STRNICMP function. |
| 3296 | // XXX: above comment should be "no MB_STRCMP function" ? |
| 3297 | if (!(chgtick == CHANGEDTICK(curbuf) |
| 3298 | && MB_STRNICMP(lastpat, spats[last_idx].pat, STRLEN(lastpat)) == 0 |
| 3299 | && STRLEN(lastpat) == STRLEN(spats[last_idx].pat) |
| 3300 | && EQUAL_POS(lastpos, *cursor_pos) |
| 3301 | && lbuf == curbuf) || wraparound || cur < 0 |
| 3302 | || (maxcount > 0 && cur > maxcount) || recompute) |
| 3303 | { |
| 3304 | cur = 0; |
| 3305 | cnt = 0; |
| 3306 | exact_match = FALSE; |
| 3307 | incomplete = 0; |
| 3308 | CLEAR_POS(&lastpos); |
| 3309 | lbuf = curbuf; |
| 3310 | } |
| 3311 | |
Christian Brabandt | 34a6a36 | 2023-05-06 19:20:20 +0100 | [diff] [blame] | 3312 | // when searching backwards and having jumped to the first occurrence, |
| 3313 | // cur must remain greater than 1 |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 3314 | if (EQUAL_POS(lastpos, *cursor_pos) && !wraparound |
Christian Brabandt | 34a6a36 | 2023-05-06 19:20:20 +0100 | [diff] [blame] | 3315 | && (dirc == 0 || dirc == '/' ? cur < cnt : cur > 1)) |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 3316 | cur += dirc == 0 ? 0 : dirc == '/' ? 1 : -1; |
| 3317 | else |
| 3318 | { |
| 3319 | int done_search = FALSE; |
| 3320 | pos_T endpos = {0, 0, 0}; |
| 3321 | |
| 3322 | p_ws = FALSE; |
| 3323 | #ifdef FEAT_RELTIME |
| 3324 | if (timeout > 0) |
| 3325 | profile_setlimit(timeout, &start); |
| 3326 | #endif |
| 3327 | while (!got_int && searchit(curwin, curbuf, &lastpos, &endpos, |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 3328 | FORWARD, NULL, 0, 1, SEARCH_KEEP, RE_LAST, NULL) != FAIL) |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 3329 | { |
| 3330 | done_search = TRUE; |
| 3331 | #ifdef FEAT_RELTIME |
| 3332 | // Stop after passing the time limit. |
| 3333 | if (timeout > 0 && profile_passed_limit(&start)) |
| 3334 | { |
| 3335 | incomplete = 1; |
| 3336 | break; |
| 3337 | } |
| 3338 | #endif |
| 3339 | cnt++; |
| 3340 | if (LTOREQ_POS(lastpos, p)) |
| 3341 | { |
| 3342 | cur = cnt; |
Bram Moolenaar | 57f75a5 | 2020-06-02 22:06:21 +0200 | [diff] [blame] | 3343 | if (LT_POS(p, endpos)) |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 3344 | exact_match = TRUE; |
| 3345 | } |
| 3346 | fast_breakcheck(); |
| 3347 | if (maxcount > 0 && cnt > maxcount) |
| 3348 | { |
| 3349 | incomplete = 2; // max count exceeded |
| 3350 | break; |
| 3351 | } |
| 3352 | } |
| 3353 | if (got_int) |
| 3354 | cur = -1; // abort |
| 3355 | if (done_search) |
| 3356 | { |
| 3357 | vim_free(lastpat); |
| 3358 | lastpat = vim_strsave(spats[last_idx].pat); |
| 3359 | chgtick = CHANGEDTICK(curbuf); |
| 3360 | lbuf = curbuf; |
| 3361 | lastpos = p; |
| 3362 | } |
| 3363 | } |
| 3364 | stat->cur = cur; |
| 3365 | stat->cnt = cnt; |
| 3366 | stat->exact_match = exact_match; |
| 3367 | stat->incomplete = incomplete; |
| 3368 | stat->last_maxcount = last_maxcount; |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 3369 | p_ws = save_ws; |
| 3370 | } |
| 3371 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3372 | #if defined(FEAT_FIND_ID) || defined(PROTO) |
Bram Moolenaar | 409510c | 2022-06-01 15:23:13 +0100 | [diff] [blame] | 3373 | |
| 3374 | /* |
| 3375 | * Get line "lnum" and copy it into "buf[LSIZE]". |
| 3376 | * The copy is made because the regexp may make the line invalid when using a |
| 3377 | * mark. |
| 3378 | */ |
| 3379 | static char_u * |
| 3380 | get_line_and_copy(linenr_T lnum, char_u *buf) |
| 3381 | { |
| 3382 | char_u *line = ml_get(lnum); |
| 3383 | |
| 3384 | vim_strncpy(buf, line, LSIZE - 1); |
| 3385 | return buf; |
| 3386 | } |
| 3387 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3388 | /* |
| 3389 | * Find identifiers or defines in included files. |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3390 | * If p_ic && compl_status_sol() then ptr must be in lowercase. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3391 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3392 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 3393 | find_pattern_in_path( |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3394 | char_u *ptr, // pointer to search pattern |
| 3395 | int dir UNUSED, // direction of expansion |
| 3396 | int len, // length of search pattern |
| 3397 | int whole, // match whole words only |
| 3398 | int skip_comments, // don't match inside comments |
| 3399 | int type, // Type of search; are we looking for a type? |
| 3400 | // a macro? |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 3401 | long count, |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3402 | int action, // What to do when we find it |
| 3403 | linenr_T start_lnum, // first line to start searching |
Colin Kennedy | 2157035 | 2024-03-03 16:16:47 +0100 | [diff] [blame] | 3404 | linenr_T end_lnum, // last line for searching |
| 3405 | int forceit) // If true, always switch to the found path |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3406 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3407 | SearchedFile *files; // Stack of included files |
| 3408 | SearchedFile *bigger; // When we need more space |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3409 | int max_path_depth = 50; |
| 3410 | long match_count = 1; |
| 3411 | |
| 3412 | char_u *pat; |
| 3413 | char_u *new_fname; |
| 3414 | char_u *curr_fname = curbuf->b_fname; |
| 3415 | char_u *prev_fname = NULL; |
| 3416 | linenr_T lnum; |
| 3417 | int depth; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3418 | int depth_displayed; // For type==CHECK_PATH |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3419 | int old_files; |
| 3420 | int already_searched; |
| 3421 | char_u *file_line; |
| 3422 | char_u *line; |
| 3423 | char_u *p; |
| 3424 | char_u save_char; |
| 3425 | int define_matched; |
| 3426 | regmatch_T regmatch; |
| 3427 | regmatch_T incl_regmatch; |
| 3428 | regmatch_T def_regmatch; |
| 3429 | int matched = FALSE; |
| 3430 | int did_show = FALSE; |
| 3431 | int found = FALSE; |
| 3432 | int i; |
| 3433 | char_u *already = NULL; |
| 3434 | char_u *startp = NULL; |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 3435 | char_u *inc_opt = NULL; |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 3436 | #if defined(FEAT_QUICKFIX) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3437 | win_T *curwin_save = NULL; |
| 3438 | #endif |
| 3439 | |
| 3440 | regmatch.regprog = NULL; |
| 3441 | incl_regmatch.regprog = NULL; |
| 3442 | def_regmatch.regprog = NULL; |
| 3443 | |
| 3444 | file_line = alloc(LSIZE); |
| 3445 | if (file_line == NULL) |
| 3446 | return; |
| 3447 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3448 | if (type != CHECK_PATH && type != FIND_DEFINE |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3449 | // when CONT_SOL is set compare "ptr" with the beginning of the |
| 3450 | // line is faster than quote_meta/regcomp/regexec "ptr" -- Acevedo |
| 3451 | && !compl_status_sol()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3452 | { |
| 3453 | pat = alloc(len + 5); |
| 3454 | if (pat == NULL) |
| 3455 | goto fpip_end; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 3456 | vim_snprintf((char *)pat, len + 5, whole ? "\\<%.*s\\>" : "%.*s", len, ptr); |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3457 | // ignore case according to p_ic, p_scs and pat |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3458 | regmatch.rm_ic = ignorecase(pat); |
Bram Moolenaar | f4e2099 | 2020-12-21 19:59:08 +0100 | [diff] [blame] | 3459 | regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3460 | vim_free(pat); |
| 3461 | if (regmatch.regprog == NULL) |
| 3462 | goto fpip_end; |
| 3463 | } |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 3464 | inc_opt = (*curbuf->b_p_inc == NUL) ? p_inc : curbuf->b_p_inc; |
| 3465 | if (*inc_opt != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3466 | { |
Bram Moolenaar | f4e2099 | 2020-12-21 19:59:08 +0100 | [diff] [blame] | 3467 | incl_regmatch.regprog = vim_regcomp(inc_opt, |
| 3468 | magic_isset() ? RE_MAGIC : 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3469 | if (incl_regmatch.regprog == NULL) |
| 3470 | goto fpip_end; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3471 | incl_regmatch.rm_ic = FALSE; // don't ignore case in incl. pat. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3472 | } |
| 3473 | if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL)) |
| 3474 | { |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 3475 | def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL ? p_def : curbuf->b_p_def, |
Bram Moolenaar | f4e2099 | 2020-12-21 19:59:08 +0100 | [diff] [blame] | 3476 | magic_isset() ? RE_MAGIC : 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3477 | if (def_regmatch.regprog == NULL) |
| 3478 | goto fpip_end; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3479 | def_regmatch.rm_ic = FALSE; // don't ignore case in define pat. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3480 | } |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 3481 | files = lalloc_clear(max_path_depth * sizeof(SearchedFile), TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3482 | if (files == NULL) |
| 3483 | goto fpip_end; |
| 3484 | old_files = max_path_depth; |
| 3485 | depth = depth_displayed = -1; |
| 3486 | |
| 3487 | lnum = start_lnum; |
| 3488 | if (end_lnum > curbuf->b_ml.ml_line_count) |
| 3489 | end_lnum = curbuf->b_ml.ml_line_count; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3490 | if (lnum > end_lnum) // do at least one line |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3491 | lnum = end_lnum; |
Bram Moolenaar | 409510c | 2022-06-01 15:23:13 +0100 | [diff] [blame] | 3492 | line = get_line_and_copy(lnum, file_line); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3493 | |
| 3494 | for (;;) |
| 3495 | { |
| 3496 | if (incl_regmatch.regprog != NULL |
| 3497 | && vim_regexec(&incl_regmatch, line, (colnr_T)0)) |
| 3498 | { |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 3499 | char_u *p_fname = (curr_fname == curbuf->b_fname) |
| 3500 | ? curbuf->b_ffname : curr_fname; |
| 3501 | |
| 3502 | if (inc_opt != NULL && strstr((char *)inc_opt, "\\zs") != NULL) |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3503 | // Use text from '\zs' to '\ze' (or end) of 'include'. |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 3504 | new_fname = find_file_name_in_path(incl_regmatch.startp[0], |
Bram Moolenaar | c84e3c1 | 2013-07-03 22:28:36 +0200 | [diff] [blame] | 3505 | (int)(incl_regmatch.endp[0] - incl_regmatch.startp[0]), |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 3506 | FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname); |
| 3507 | else |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3508 | // Use text after match with 'include'. |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 3509 | new_fname = file_name_in_line(incl_regmatch.endp[0], 0, |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 3510 | FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3511 | already_searched = FALSE; |
| 3512 | if (new_fname != NULL) |
| 3513 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3514 | // Check whether we have already searched in this file |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3515 | for (i = 0;; i++) |
| 3516 | { |
| 3517 | if (i == depth + 1) |
| 3518 | i = old_files; |
| 3519 | if (i == max_path_depth) |
| 3520 | break; |
Bram Moolenaar | 99499b1 | 2019-05-23 21:35:48 +0200 | [diff] [blame] | 3521 | if (fullpathcmp(new_fname, files[i].name, TRUE, TRUE) |
| 3522 | & FPC_SAME) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3523 | { |
Dominique Pelle | 7765f5c | 2022-04-10 11:26:53 +0100 | [diff] [blame] | 3524 | if (type != CHECK_PATH |
| 3525 | && action == ACTION_SHOW_ALL |
| 3526 | && files[i].matched) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3527 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3528 | msg_putchar('\n'); // cursor below last one |
| 3529 | if (!got_int) // don't display if 'q' |
| 3530 | // typed at "--more--" |
| 3531 | // message |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3532 | { |
| 3533 | msg_home_replace_hl(new_fname); |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 3534 | msg_puts(_(" (includes previously listed match)")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3535 | prev_fname = NULL; |
| 3536 | } |
| 3537 | } |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 3538 | VIM_CLEAR(new_fname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3539 | already_searched = TRUE; |
| 3540 | break; |
| 3541 | } |
| 3542 | } |
| 3543 | } |
| 3544 | |
| 3545 | if (type == CHECK_PATH && (action == ACTION_SHOW_ALL |
| 3546 | || (new_fname == NULL && !already_searched))) |
| 3547 | { |
| 3548 | if (did_show) |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3549 | msg_putchar('\n'); // cursor below last one |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3550 | else |
| 3551 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3552 | gotocmdline(TRUE); // cursor at status line |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 3553 | msg_puts_title(_("--- Included files ")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3554 | if (action != ACTION_SHOW_ALL) |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 3555 | msg_puts_title(_("not found ")); |
| 3556 | msg_puts_title(_("in path ---\n")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3557 | } |
| 3558 | did_show = TRUE; |
| 3559 | while (depth_displayed < depth && !got_int) |
| 3560 | { |
| 3561 | ++depth_displayed; |
| 3562 | for (i = 0; i < depth_displayed; i++) |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 3563 | msg_puts(" "); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3564 | msg_home_replace(files[depth_displayed].name); |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 3565 | msg_puts(" -->\n"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3566 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3567 | if (!got_int) // don't display if 'q' typed |
| 3568 | // for "--more--" message |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3569 | { |
| 3570 | for (i = 0; i <= depth_displayed; i++) |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 3571 | msg_puts(" "); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3572 | if (new_fname != NULL) |
| 3573 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3574 | // using "new_fname" is more reliable, e.g., when |
| 3575 | // 'includeexpr' is set. |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 3576 | msg_outtrans_attr(new_fname, HL_ATTR(HLF_D)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3577 | } |
| 3578 | else |
| 3579 | { |
| 3580 | /* |
| 3581 | * Isolate the file name. |
| 3582 | * Include the surrounding "" or <> if present. |
| 3583 | */ |
Bram Moolenaar | 058bdcf | 2012-07-25 13:46:30 +0200 | [diff] [blame] | 3584 | if (inc_opt != NULL |
| 3585 | && strstr((char *)inc_opt, "\\zs") != NULL) |
| 3586 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3587 | // pattern contains \zs, use the match |
Bram Moolenaar | 058bdcf | 2012-07-25 13:46:30 +0200 | [diff] [blame] | 3588 | p = incl_regmatch.startp[0]; |
| 3589 | i = (int)(incl_regmatch.endp[0] |
| 3590 | - incl_regmatch.startp[0]); |
| 3591 | } |
| 3592 | else |
| 3593 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3594 | // find the file name after the end of the match |
Bram Moolenaar | 058bdcf | 2012-07-25 13:46:30 +0200 | [diff] [blame] | 3595 | for (p = incl_regmatch.endp[0]; |
| 3596 | *p && !vim_isfilec(*p); p++) |
| 3597 | ; |
| 3598 | for (i = 0; vim_isfilec(p[i]); i++) |
| 3599 | ; |
| 3600 | } |
| 3601 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3602 | if (i == 0) |
| 3603 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3604 | // Nothing found, use the rest of the line. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3605 | p = incl_regmatch.endp[0]; |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 3606 | i = (int)STRLEN(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3607 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3608 | // Avoid checking before the start of the line, can |
| 3609 | // happen if \zs appears in the regexp. |
Bram Moolenaar | 058bdcf | 2012-07-25 13:46:30 +0200 | [diff] [blame] | 3610 | else if (p > line) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3611 | { |
| 3612 | if (p[-1] == '"' || p[-1] == '<') |
| 3613 | { |
| 3614 | --p; |
| 3615 | ++i; |
| 3616 | } |
| 3617 | if (p[i] == '"' || p[i] == '>') |
| 3618 | ++i; |
| 3619 | } |
| 3620 | save_char = p[i]; |
| 3621 | p[i] = NUL; |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 3622 | msg_outtrans_attr(p, HL_ATTR(HLF_D)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3623 | p[i] = save_char; |
| 3624 | } |
| 3625 | |
| 3626 | if (new_fname == NULL && action == ACTION_SHOW_ALL) |
| 3627 | { |
| 3628 | if (already_searched) |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 3629 | msg_puts(_(" (Already listed)")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3630 | else |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 3631 | msg_puts(_(" NOT FOUND")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3632 | } |
| 3633 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3634 | out_flush(); // output each line directly |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3635 | } |
| 3636 | |
| 3637 | if (new_fname != NULL) |
| 3638 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3639 | // Push the new file onto the file stack |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3640 | if (depth + 1 == old_files) |
| 3641 | { |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 3642 | bigger = ALLOC_MULT(SearchedFile, max_path_depth * 2); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3643 | if (bigger != NULL) |
| 3644 | { |
| 3645 | for (i = 0; i <= depth; i++) |
| 3646 | bigger[i] = files[i]; |
| 3647 | for (i = depth + 1; i < old_files + max_path_depth; i++) |
| 3648 | { |
| 3649 | bigger[i].fp = NULL; |
| 3650 | bigger[i].name = NULL; |
| 3651 | bigger[i].lnum = 0; |
| 3652 | bigger[i].matched = FALSE; |
| 3653 | } |
| 3654 | for (i = old_files; i < max_path_depth; i++) |
| 3655 | bigger[i + max_path_depth] = files[i]; |
| 3656 | old_files += max_path_depth; |
| 3657 | max_path_depth *= 2; |
| 3658 | vim_free(files); |
| 3659 | files = bigger; |
| 3660 | } |
| 3661 | } |
| 3662 | if ((files[depth + 1].fp = mch_fopen((char *)new_fname, "r")) |
| 3663 | == NULL) |
| 3664 | vim_free(new_fname); |
| 3665 | else |
| 3666 | { |
| 3667 | if (++depth == old_files) |
| 3668 | { |
| 3669 | /* |
| 3670 | * lalloc() for 'bigger' must have failed above. We |
| 3671 | * will forget one of our already visited files now. |
| 3672 | */ |
| 3673 | vim_free(files[old_files].name); |
| 3674 | ++old_files; |
| 3675 | } |
| 3676 | files[depth].name = curr_fname = new_fname; |
| 3677 | files[depth].lnum = 0; |
| 3678 | files[depth].matched = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3679 | if (action == ACTION_EXPAND) |
| 3680 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3681 | msg_hist_off = TRUE; // reset in msg_trunc_attr() |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 3682 | vim_snprintf((char*)IObuff, IOSIZE, |
| 3683 | _("Scanning included file: %s"), |
| 3684 | (char *)new_fname); |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 3685 | msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3686 | } |
Bram Moolenaar | e2c453d | 2019-08-21 14:37:09 +0200 | [diff] [blame] | 3687 | else if (p_verbose >= 5) |
Bram Moolenaar | 87b5ca5 | 2006-03-04 21:55:31 +0000 | [diff] [blame] | 3688 | { |
| 3689 | verbose_enter(); |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 3690 | smsg(_("Searching included file %s"), |
Bram Moolenaar | 87b5ca5 | 2006-03-04 21:55:31 +0000 | [diff] [blame] | 3691 | (char *)new_fname); |
| 3692 | verbose_leave(); |
| 3693 | } |
| 3694 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3695 | } |
| 3696 | } |
| 3697 | } |
| 3698 | else |
| 3699 | { |
| 3700 | /* |
| 3701 | * Check if the line is a define (type == FIND_DEFINE) |
| 3702 | */ |
| 3703 | p = line; |
| 3704 | search_line: |
| 3705 | define_matched = FALSE; |
| 3706 | if (def_regmatch.regprog != NULL |
| 3707 | && vim_regexec(&def_regmatch, line, (colnr_T)0)) |
| 3708 | { |
| 3709 | /* |
| 3710 | * Pattern must be first identifier after 'define', so skip |
| 3711 | * to that position before checking for match of pattern. Also |
| 3712 | * don't let it match beyond the end of this identifier. |
| 3713 | */ |
| 3714 | p = def_regmatch.endp[0]; |
| 3715 | while (*p && !vim_iswordc(*p)) |
| 3716 | p++; |
| 3717 | define_matched = TRUE; |
| 3718 | } |
| 3719 | |
| 3720 | /* |
| 3721 | * Look for a match. Don't do this if we are looking for a |
| 3722 | * define and this line didn't match define_prog above. |
| 3723 | */ |
| 3724 | if (def_regmatch.regprog == NULL || define_matched) |
| 3725 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3726 | if (define_matched || compl_status_sol()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3727 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3728 | // compare the first "len" chars from "ptr" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3729 | startp = skipwhite(p); |
| 3730 | if (p_ic) |
| 3731 | matched = !MB_STRNICMP(startp, ptr, len); |
| 3732 | else |
| 3733 | matched = !STRNCMP(startp, ptr, len); |
| 3734 | if (matched && define_matched && whole |
| 3735 | && vim_iswordc(startp[len])) |
| 3736 | matched = FALSE; |
| 3737 | } |
| 3738 | else if (regmatch.regprog != NULL |
| 3739 | && vim_regexec(®match, line, (colnr_T)(p - line))) |
| 3740 | { |
| 3741 | matched = TRUE; |
| 3742 | startp = regmatch.startp[0]; |
| 3743 | /* |
| 3744 | * Check if the line is not a comment line (unless we are |
| 3745 | * looking for a define). A line starting with "# define" |
| 3746 | * is not considered to be a comment line. |
| 3747 | */ |
| 3748 | if (!define_matched && skip_comments) |
| 3749 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3750 | if ((*line != '#' || |
| 3751 | STRNCMP(skipwhite(line + 1), "define", 6) != 0) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 3752 | && get_leader_len(line, NULL, FALSE, TRUE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3753 | matched = FALSE; |
| 3754 | |
| 3755 | /* |
| 3756 | * Also check for a "/ *" or "/ /" before the match. |
| 3757 | * Skips lines like "int backwards; / * normal index |
| 3758 | * * /" when looking for "normal". |
| 3759 | * Note: Doesn't skip "/ *" in comments. |
| 3760 | */ |
| 3761 | p = skipwhite(line); |
| 3762 | if (matched |
| 3763 | || (p[0] == '/' && p[1] == '*') || p[0] == '*') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3764 | for (p = line; *p && p < startp; ++p) |
| 3765 | { |
| 3766 | if (matched |
| 3767 | && p[0] == '/' |
| 3768 | && (p[1] == '*' || p[1] == '/')) |
| 3769 | { |
| 3770 | matched = FALSE; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3771 | // After "//" all text is comment |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3772 | if (p[1] == '/') |
| 3773 | break; |
| 3774 | ++p; |
| 3775 | } |
| 3776 | else if (!matched && p[0] == '*' && p[1] == '/') |
| 3777 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3778 | // Can find match after "* /". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3779 | matched = TRUE; |
| 3780 | ++p; |
| 3781 | } |
| 3782 | } |
| 3783 | } |
| 3784 | } |
| 3785 | } |
| 3786 | } |
| 3787 | if (matched) |
| 3788 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3789 | if (action == ACTION_EXPAND) |
| 3790 | { |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 3791 | int cont_s_ipos = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3792 | int add_r; |
| 3793 | char_u *aux; |
| 3794 | |
| 3795 | if (depth == -1 && lnum == curwin->w_cursor.lnum) |
| 3796 | break; |
| 3797 | found = TRUE; |
| 3798 | aux = p = startp; |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3799 | if (compl_status_adding()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3800 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3801 | p += ins_compl_len(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3802 | if (vim_iswordp(p)) |
| 3803 | goto exit_matched; |
| 3804 | p = find_word_start(p); |
| 3805 | } |
| 3806 | p = find_word_end(p); |
| 3807 | i = (int)(p - aux); |
| 3808 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3809 | if (compl_status_adding() && i == ins_compl_len()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3810 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3811 | // IOSIZE > compl_length, so the STRNCPY works |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3812 | STRNCPY(IObuff, aux, i); |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 3813 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3814 | // Get the next line: when "depth" < 0 from the current |
| 3815 | // buffer, otherwise from the included file. Jump to |
| 3816 | // exit_matched when past the last line. |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 3817 | if (depth < 0) |
| 3818 | { |
| 3819 | if (lnum >= end_lnum) |
| 3820 | goto exit_matched; |
Bram Moolenaar | 409510c | 2022-06-01 15:23:13 +0100 | [diff] [blame] | 3821 | line = get_line_and_copy(++lnum, file_line); |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 3822 | } |
| 3823 | else if (vim_fgets(line = file_line, |
| 3824 | LSIZE, files[depth].fp)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3825 | goto exit_matched; |
| 3826 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3827 | // we read a line, set "already" to check this "line" later |
| 3828 | // if depth >= 0 we'll increase files[depth].lnum far |
Bram Moolenaar | 8e7d622 | 2020-12-18 19:49:56 +0100 | [diff] [blame] | 3829 | // below -- Acevedo |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3830 | already = aux = p = skipwhite(line); |
| 3831 | p = find_word_start(p); |
| 3832 | p = find_word_end(p); |
| 3833 | if (p > aux) |
| 3834 | { |
| 3835 | if (*aux != ')' && IObuff[i-1] != TAB) |
| 3836 | { |
| 3837 | if (IObuff[i-1] != ' ') |
| 3838 | IObuff[i++] = ' '; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3839 | // IObuf =~ "\(\k\|\i\).* ", thus i >= 2 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3840 | if (p_js |
| 3841 | && (IObuff[i-2] == '.' |
| 3842 | || (vim_strchr(p_cpo, CPO_JOINSP) == NULL |
| 3843 | && (IObuff[i-2] == '?' |
| 3844 | || IObuff[i-2] == '!')))) |
| 3845 | IObuff[i++] = ' '; |
| 3846 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3847 | // copy as much as possible of the new word |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3848 | if (p - aux >= IOSIZE - i) |
| 3849 | p = aux + IOSIZE - i - 1; |
| 3850 | STRNCPY(IObuff + i, aux, p - aux); |
| 3851 | i += (int)(p - aux); |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 3852 | cont_s_ipos = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3853 | } |
| 3854 | IObuff[i] = NUL; |
| 3855 | aux = IObuff; |
| 3856 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3857 | if (i == ins_compl_len()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3858 | goto exit_matched; |
| 3859 | } |
| 3860 | |
Bram Moolenaar | e8c3a14 | 2006-08-29 14:30:35 +0000 | [diff] [blame] | 3861 | add_r = ins_compl_add_infercase(aux, i, p_ic, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3862 | curr_fname == curbuf->b_fname ? NULL : curr_fname, |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 3863 | dir, cont_s_ipos); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3864 | if (add_r == OK) |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3865 | // if dir was BACKWARD then honor it just once |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3866 | dir = FORWARD; |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3867 | else if (add_r == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3868 | break; |
| 3869 | } |
Bram Moolenaar | e2c453d | 2019-08-21 14:37:09 +0200 | [diff] [blame] | 3870 | else if (action == ACTION_SHOW_ALL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3871 | { |
| 3872 | found = TRUE; |
| 3873 | if (!did_show) |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3874 | gotocmdline(TRUE); // cursor at status line |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3875 | if (curr_fname != prev_fname) |
| 3876 | { |
| 3877 | if (did_show) |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3878 | msg_putchar('\n'); // cursor below last one |
| 3879 | if (!got_int) // don't display if 'q' typed |
| 3880 | // at "--more--" message |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3881 | msg_home_replace_hl(curr_fname); |
| 3882 | prev_fname = curr_fname; |
| 3883 | } |
| 3884 | did_show = TRUE; |
| 3885 | if (!got_int) |
| 3886 | show_pat_in_path(line, type, TRUE, action, |
| 3887 | (depth == -1) ? NULL : files[depth].fp, |
| 3888 | (depth == -1) ? &lnum : &files[depth].lnum, |
| 3889 | match_count++); |
| 3890 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3891 | // Set matched flag for this file and all the ones that |
| 3892 | // include it |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3893 | for (i = 0; i <= depth; ++i) |
| 3894 | files[i].matched = TRUE; |
| 3895 | } |
| 3896 | else if (--count <= 0) |
| 3897 | { |
| 3898 | found = TRUE; |
| 3899 | if (depth == -1 && lnum == curwin->w_cursor.lnum |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 3900 | #if defined(FEAT_QUICKFIX) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3901 | && g_do_tagpreview == 0 |
| 3902 | #endif |
| 3903 | ) |
Bram Moolenaar | ac78dd4 | 2022-01-02 19:25:26 +0000 | [diff] [blame] | 3904 | emsg(_(e_match_is_on_current_line)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3905 | else if (action == ACTION_SHOW) |
| 3906 | { |
| 3907 | show_pat_in_path(line, type, did_show, action, |
| 3908 | (depth == -1) ? NULL : files[depth].fp, |
| 3909 | (depth == -1) ? &lnum : &files[depth].lnum, 1L); |
| 3910 | did_show = TRUE; |
| 3911 | } |
| 3912 | else |
| 3913 | { |
| 3914 | #ifdef FEAT_GUI |
| 3915 | need_mouse_correct = TRUE; |
| 3916 | #endif |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 3917 | #if defined(FEAT_QUICKFIX) |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3918 | // ":psearch" uses the preview window |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3919 | if (g_do_tagpreview != 0) |
| 3920 | { |
| 3921 | curwin_save = curwin; |
Bram Moolenaar | 576a4a6 | 2019-08-18 15:25:17 +0200 | [diff] [blame] | 3922 | prepare_tagpreview(TRUE, TRUE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3923 | } |
| 3924 | #endif |
| 3925 | if (action == ACTION_SPLIT) |
| 3926 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3927 | if (win_split(0, 0) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3928 | break; |
Bram Moolenaar | 3368ea2 | 2010-09-21 16:56:35 +0200 | [diff] [blame] | 3929 | RESET_BINDING(curwin); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3930 | } |
| 3931 | if (depth == -1) |
| 3932 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3933 | // match in current file |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 3934 | #if defined(FEAT_QUICKFIX) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3935 | if (g_do_tagpreview != 0) |
| 3936 | { |
Bram Moolenaar | 92bb83e | 2021-02-03 23:04:46 +0100 | [diff] [blame] | 3937 | if (!win_valid(curwin_save)) |
| 3938 | break; |
Bram Moolenaar | 8ad80de | 2017-06-05 16:01:59 +0200 | [diff] [blame] | 3939 | if (!GETFILE_SUCCESS(getfile( |
Bram Moolenaar | c31f9ae | 2017-07-23 22:02:02 +0200 | [diff] [blame] | 3940 | curwin_save->w_buffer->b_fnum, NULL, |
Colin Kennedy | 2157035 | 2024-03-03 16:16:47 +0100 | [diff] [blame] | 3941 | NULL, TRUE, lnum, forceit))) |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3942 | break; // failed to jump to file |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3943 | } |
| 3944 | else |
| 3945 | #endif |
| 3946 | setpcmark(); |
| 3947 | curwin->w_cursor.lnum = lnum; |
Bram Moolenaar | c31f9ae | 2017-07-23 22:02:02 +0200 | [diff] [blame] | 3948 | check_cursor(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3949 | } |
| 3950 | else |
| 3951 | { |
Bram Moolenaar | 8ad80de | 2017-06-05 16:01:59 +0200 | [diff] [blame] | 3952 | if (!GETFILE_SUCCESS(getfile( |
| 3953 | 0, files[depth].name, NULL, TRUE, |
Colin Kennedy | 2157035 | 2024-03-03 16:16:47 +0100 | [diff] [blame] | 3954 | files[depth].lnum, forceit))) |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3955 | break; // failed to jump to file |
| 3956 | // autocommands may have changed the lnum, we don't |
| 3957 | // want that here |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3958 | curwin->w_cursor.lnum = files[depth].lnum; |
| 3959 | } |
| 3960 | } |
| 3961 | if (action != ACTION_SHOW) |
| 3962 | { |
Bram Moolenaar | fe81d45 | 2009-04-22 14:44:41 +0000 | [diff] [blame] | 3963 | curwin->w_cursor.col = (colnr_T)(startp - line); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3964 | curwin->w_set_curswant = TRUE; |
| 3965 | } |
| 3966 | |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 3967 | #if defined(FEAT_QUICKFIX) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3968 | if (g_do_tagpreview != 0 |
Bram Moolenaar | 997fb4b | 2006-02-17 21:53:23 +0000 | [diff] [blame] | 3969 | && curwin != curwin_save && win_valid(curwin_save)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3970 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3971 | // Return cursor to where we were |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3972 | validate_cursor(); |
Bram Moolenaar | a4d158b | 2022-08-14 14:17:45 +0100 | [diff] [blame] | 3973 | redraw_later(UPD_VALID); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3974 | win_enter(curwin_save, TRUE); |
| 3975 | } |
Bram Moolenaar | 05ad5ff | 2019-11-30 22:48:27 +0100 | [diff] [blame] | 3976 | # ifdef FEAT_PROP_POPUP |
Bram Moolenaar | 1b6d9c4 | 2019-08-05 21:52:04 +0200 | [diff] [blame] | 3977 | else if (WIN_IS_POPUP(curwin)) |
| 3978 | // can't keep focus in popup window |
| 3979 | win_enter(firstwin, TRUE); |
| 3980 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3981 | #endif |
| 3982 | break; |
| 3983 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3984 | exit_matched: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3985 | matched = FALSE; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3986 | // look for other matches in the rest of the line if we |
| 3987 | // are not at the end of it already |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3988 | if (def_regmatch.regprog == NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3989 | && action == ACTION_EXPAND |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3990 | && !compl_status_sol() |
Bram Moolenaar | fe81d45 | 2009-04-22 14:44:41 +0000 | [diff] [blame] | 3991 | && *startp != NUL |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 3992 | && *(startp + mb_ptr2len(startp)) != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3993 | goto search_line; |
| 3994 | } |
| 3995 | line_breakcheck(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3996 | if (action == ACTION_EXPAND) |
Bram Moolenaar | 472e859 | 2016-10-15 17:06:47 +0200 | [diff] [blame] | 3997 | ins_compl_check_keys(30, FALSE); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3998 | if (got_int || ins_compl_interrupted()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3999 | break; |
| 4000 | |
| 4001 | /* |
| 4002 | * Read the next line. When reading an included file and encountering |
| 4003 | * end-of-file, close the file and continue in the file that included |
| 4004 | * it. |
| 4005 | */ |
| 4006 | while (depth >= 0 && !already |
| 4007 | && vim_fgets(line = file_line, LSIZE, files[depth].fp)) |
| 4008 | { |
| 4009 | fclose(files[depth].fp); |
| 4010 | --old_files; |
| 4011 | files[old_files].name = files[depth].name; |
| 4012 | files[old_files].matched = files[depth].matched; |
| 4013 | --depth; |
| 4014 | curr_fname = (depth == -1) ? curbuf->b_fname |
| 4015 | : files[depth].name; |
| 4016 | if (depth < depth_displayed) |
| 4017 | depth_displayed = depth; |
| 4018 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 4019 | if (depth >= 0) // we could read the line |
Bram Moolenaar | c84e3c1 | 2013-07-03 22:28:36 +0200 | [diff] [blame] | 4020 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4021 | files[depth].lnum++; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 4022 | // Remove any CR and LF from the line. |
Bram Moolenaar | c84e3c1 | 2013-07-03 22:28:36 +0200 | [diff] [blame] | 4023 | i = (int)STRLEN(line); |
| 4024 | if (i > 0 && line[i - 1] == '\n') |
| 4025 | line[--i] = NUL; |
| 4026 | if (i > 0 && line[i - 1] == '\r') |
| 4027 | line[--i] = NUL; |
| 4028 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4029 | else if (!already) |
| 4030 | { |
| 4031 | if (++lnum > end_lnum) |
| 4032 | break; |
Bram Moolenaar | 409510c | 2022-06-01 15:23:13 +0100 | [diff] [blame] | 4033 | line = get_line_and_copy(lnum, file_line); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4034 | } |
| 4035 | already = NULL; |
| 4036 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 4037 | // End of big for (;;) loop. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4038 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 4039 | // Close any files that are still open. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4040 | for (i = 0; i <= depth; i++) |
| 4041 | { |
| 4042 | fclose(files[i].fp); |
| 4043 | vim_free(files[i].name); |
| 4044 | } |
| 4045 | for (i = old_files; i < max_path_depth; i++) |
| 4046 | vim_free(files[i].name); |
| 4047 | vim_free(files); |
| 4048 | |
| 4049 | if (type == CHECK_PATH) |
| 4050 | { |
| 4051 | if (!did_show) |
| 4052 | { |
| 4053 | if (action != ACTION_SHOW_ALL) |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 4054 | msg(_("All included files were found")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4055 | else |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 4056 | msg(_("No included files")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4057 | } |
| 4058 | } |
Bram Moolenaar | e2c453d | 2019-08-21 14:37:09 +0200 | [diff] [blame] | 4059 | else if (!found && action != ACTION_EXPAND) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4060 | { |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 4061 | if (got_int || ins_compl_interrupted()) |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 4062 | emsg(_(e_interrupted)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4063 | else if (type == FIND_DEFINE) |
Bram Moolenaar | ac78dd4 | 2022-01-02 19:25:26 +0000 | [diff] [blame] | 4064 | emsg(_(e_couldnt_find_definition)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4065 | else |
Bram Moolenaar | ac78dd4 | 2022-01-02 19:25:26 +0000 | [diff] [blame] | 4066 | emsg(_(e_couldnt_find_pattern)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4067 | } |
| 4068 | if (action == ACTION_SHOW || action == ACTION_SHOW_ALL) |
| 4069 | msg_end(); |
| 4070 | |
| 4071 | fpip_end: |
| 4072 | vim_free(file_line); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 4073 | vim_regfree(regmatch.regprog); |
| 4074 | vim_regfree(incl_regmatch.regprog); |
| 4075 | vim_regfree(def_regmatch.regprog); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4076 | } |
| 4077 | |
| 4078 | static void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 4079 | show_pat_in_path( |
| 4080 | char_u *line, |
| 4081 | int type, |
| 4082 | int did_show, |
| 4083 | int action, |
| 4084 | FILE *fp, |
| 4085 | linenr_T *lnum, |
| 4086 | long count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4087 | { |
| 4088 | char_u *p; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 4089 | size_t linelen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4090 | |
| 4091 | if (did_show) |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 4092 | msg_putchar('\n'); // cursor below last one |
Bram Moolenaar | 91170f8 | 2006-05-05 21:15:17 +0000 | [diff] [blame] | 4093 | else if (!msg_silent) |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 4094 | gotocmdline(TRUE); // cursor at status line |
| 4095 | if (got_int) // 'q' typed at "--more--" message |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4096 | return; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 4097 | linelen = STRLEN(line); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4098 | for (;;) |
| 4099 | { |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 4100 | p = line + linelen - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4101 | if (fp != NULL) |
| 4102 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 4103 | // We used fgets(), so get rid of newline at end |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4104 | if (p >= line && *p == '\n') |
| 4105 | --p; |
| 4106 | if (p >= line && *p == '\r') |
| 4107 | --p; |
| 4108 | *(p + 1) = NUL; |
| 4109 | } |
| 4110 | if (action == ACTION_SHOW_ALL) |
| 4111 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 4112 | sprintf((char *)IObuff, "%3ld: ", count); // show match nr |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 4113 | msg_puts((char *)IObuff); |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 4114 | sprintf((char *)IObuff, "%4ld", *lnum); // show line nr |
| 4115 | // Highlight line numbers |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 4116 | msg_puts_attr((char *)IObuff, HL_ATTR(HLF_N)); |
| 4117 | msg_puts(" "); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4118 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4119 | msg_prt_line(line, FALSE); |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 4120 | out_flush(); // show one line at a time |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4121 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 4122 | // Definition continues until line that doesn't end with '\' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4123 | if (got_int || type != FIND_DEFINE || p < line || *p != '\\') |
| 4124 | break; |
| 4125 | |
| 4126 | if (fp != NULL) |
| 4127 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 4128 | if (vim_fgets(line, LSIZE, fp)) // end of file |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4129 | break; |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 4130 | linelen = STRLEN(line); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4131 | ++*lnum; |
| 4132 | } |
| 4133 | else |
| 4134 | { |
| 4135 | if (++*lnum > curbuf->b_ml.ml_line_count) |
| 4136 | break; |
| 4137 | line = ml_get(*lnum); |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 4138 | linelen = ml_get_len(*lnum); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4139 | } |
| 4140 | msg_putchar('\n'); |
| 4141 | } |
| 4142 | } |
| 4143 | #endif |
| 4144 | |
| 4145 | #ifdef FEAT_VIMINFO |
Bram Moolenaar | 6bd1d77 | 2019-10-09 22:01:25 +0200 | [diff] [blame] | 4146 | /* |
| 4147 | * Return the last used search pattern at "idx". |
| 4148 | */ |
Bram Moolenaar | c332816 | 2019-07-23 22:15:25 +0200 | [diff] [blame] | 4149 | spat_T * |
| 4150 | get_spat(int idx) |
| 4151 | { |
| 4152 | return &spats[idx]; |
| 4153 | } |
| 4154 | |
Bram Moolenaar | 6bd1d77 | 2019-10-09 22:01:25 +0200 | [diff] [blame] | 4155 | /* |
| 4156 | * Return the last used search pattern index. |
| 4157 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4158 | int |
Bram Moolenaar | c332816 | 2019-07-23 22:15:25 +0200 | [diff] [blame] | 4159 | get_spat_last_idx(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4160 | { |
Bram Moolenaar | c332816 | 2019-07-23 22:15:25 +0200 | [diff] [blame] | 4161 | return last_idx; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4162 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4163 | #endif |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 4164 | |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 4165 | #if defined(FEAT_EVAL) || defined(FEAT_PROTO) |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 4166 | /* |
| 4167 | * "searchcount()" function |
| 4168 | */ |
| 4169 | void |
| 4170 | f_searchcount(typval_T *argvars, typval_T *rettv) |
| 4171 | { |
| 4172 | pos_T pos = curwin->w_cursor; |
| 4173 | char_u *pattern = NULL; |
| 4174 | int maxcount = SEARCH_STAT_DEF_MAX_COUNT; |
| 4175 | long timeout = SEARCH_STAT_DEF_TIMEOUT; |
Bram Moolenaar | 4140c4f | 2020-09-05 23:16:00 +0200 | [diff] [blame] | 4176 | int recompute = TRUE; |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 4177 | searchstat_T stat; |
| 4178 | |
| 4179 | if (rettv_dict_alloc(rettv) == FAIL) |
| 4180 | return; |
| 4181 | |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 4182 | if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL) |
| 4183 | return; |
| 4184 | |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 4185 | if (shortmess(SHM_SEARCHCOUNT)) // 'shortmess' contains 'S' flag |
| 4186 | recompute = TRUE; |
| 4187 | |
| 4188 | if (argvars[0].v_type != VAR_UNKNOWN) |
| 4189 | { |
Bram Moolenaar | 1468162 | 2020-06-03 22:57:39 +0200 | [diff] [blame] | 4190 | dict_T *dict; |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 4191 | dictitem_T *di; |
| 4192 | listitem_T *li; |
| 4193 | int error = FALSE; |
| 4194 | |
Yegappan Lakshmanan | 04c4c57 | 2022-08-30 19:48:24 +0100 | [diff] [blame] | 4195 | if (check_for_nonnull_dict_arg(argvars, 0) == FAIL) |
Bram Moolenaar | 1468162 | 2020-06-03 22:57:39 +0200 | [diff] [blame] | 4196 | return; |
Bram Moolenaar | 1468162 | 2020-06-03 22:57:39 +0200 | [diff] [blame] | 4197 | dict = argvars[0].vval.v_dict; |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 4198 | di = dict_find(dict, (char_u *)"timeout", -1); |
| 4199 | if (di != NULL) |
| 4200 | { |
| 4201 | timeout = (long)tv_get_number_chk(&di->di_tv, &error); |
| 4202 | if (error) |
| 4203 | return; |
| 4204 | } |
| 4205 | di = dict_find(dict, (char_u *)"maxcount", -1); |
| 4206 | if (di != NULL) |
| 4207 | { |
| 4208 | maxcount = (int)tv_get_number_chk(&di->di_tv, &error); |
| 4209 | if (error) |
| 4210 | return; |
| 4211 | } |
Bram Moolenaar | d61efa5 | 2022-07-23 09:52:04 +0100 | [diff] [blame] | 4212 | recompute = dict_get_bool(dict, "recompute", recompute); |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 4213 | di = dict_find(dict, (char_u *)"pattern", -1); |
| 4214 | if (di != NULL) |
| 4215 | { |
| 4216 | pattern = tv_get_string_chk(&di->di_tv); |
| 4217 | if (pattern == NULL) |
| 4218 | return; |
| 4219 | } |
| 4220 | di = dict_find(dict, (char_u *)"pos", -1); |
| 4221 | if (di != NULL) |
| 4222 | { |
| 4223 | if (di->di_tv.v_type != VAR_LIST) |
| 4224 | { |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 4225 | semsg(_(e_invalid_argument_str), "pos"); |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 4226 | return; |
| 4227 | } |
| 4228 | if (list_len(di->di_tv.vval.v_list) != 3) |
| 4229 | { |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 4230 | semsg(_(e_invalid_argument_str), "List format should be [lnum, col, off]"); |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 4231 | return; |
| 4232 | } |
| 4233 | li = list_find(di->di_tv.vval.v_list, 0L); |
| 4234 | if (li != NULL) |
| 4235 | { |
| 4236 | pos.lnum = tv_get_number_chk(&li->li_tv, &error); |
| 4237 | if (error) |
| 4238 | return; |
| 4239 | } |
| 4240 | li = list_find(di->di_tv.vval.v_list, 1L); |
| 4241 | if (li != NULL) |
| 4242 | { |
Bram Moolenaar | 6ed545e | 2022-05-09 20:09:23 +0100 | [diff] [blame] | 4243 | pos.col = tv_get_number_chk(&li->li_tv, &error) - 1; |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 4244 | if (error) |
| 4245 | return; |
| 4246 | } |
| 4247 | li = list_find(di->di_tv.vval.v_list, 2L); |
| 4248 | if (li != NULL) |
| 4249 | { |
Bram Moolenaar | 6ed545e | 2022-05-09 20:09:23 +0100 | [diff] [blame] | 4250 | pos.coladd = tv_get_number_chk(&li->li_tv, &error); |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 4251 | if (error) |
| 4252 | return; |
| 4253 | } |
| 4254 | } |
| 4255 | } |
| 4256 | |
| 4257 | save_last_search_pattern(); |
Christian Brabandt | 6dd7424 | 2022-02-14 12:44:32 +0000 | [diff] [blame] | 4258 | #ifdef FEAT_SEARCH_EXTRA |
| 4259 | save_incsearch_state(); |
| 4260 | #endif |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 4261 | if (pattern != NULL) |
| 4262 | { |
| 4263 | if (*pattern == NUL) |
| 4264 | goto the_end; |
Bram Moolenaar | 109aece | 2020-06-01 19:08:54 +0200 | [diff] [blame] | 4265 | vim_free(spats[last_idx].pat); |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 4266 | spats[last_idx].patlen = STRLEN(pattern); |
| 4267 | spats[last_idx].pat = vim_strnsave(pattern, spats[last_idx].patlen); |
| 4268 | if (spats[last_idx].pat == NULL) |
| 4269 | spats[last_idx].patlen = 0; |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 4270 | } |
| 4271 | if (spats[last_idx].pat == NULL || *spats[last_idx].pat == NUL) |
| 4272 | goto the_end; // the previous pattern was never defined |
| 4273 | |
| 4274 | update_search_stat(0, &pos, &pos, &stat, recompute, maxcount, timeout); |
| 4275 | |
| 4276 | dict_add_number(rettv->vval.v_dict, "current", stat.cur); |
| 4277 | dict_add_number(rettv->vval.v_dict, "total", stat.cnt); |
| 4278 | dict_add_number(rettv->vval.v_dict, "exact_match", stat.exact_match); |
| 4279 | dict_add_number(rettv->vval.v_dict, "incomplete", stat.incomplete); |
| 4280 | dict_add_number(rettv->vval.v_dict, "maxcount", stat.last_maxcount); |
| 4281 | |
| 4282 | the_end: |
| 4283 | restore_last_search_pattern(); |
Christian Brabandt | 6dd7424 | 2022-02-14 12:44:32 +0000 | [diff] [blame] | 4284 | #ifdef FEAT_SEARCH_EXTRA |
| 4285 | restore_incsearch_state(); |
| 4286 | #endif |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 4287 | } |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 4288 | #endif |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4289 | |
| 4290 | /* |
| 4291 | * Fuzzy string matching |
| 4292 | * |
| 4293 | * Ported from the lib_fts library authored by Forrest Smith. |
| 4294 | * https://github.com/forrestthewoods/lib_fts/tree/master/code |
| 4295 | * |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4296 | * The following blog describes the fuzzy matching algorithm: |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4297 | * https://www.forrestthewoods.com/blog/reverse_engineering_sublime_texts_fuzzy_match/ |
| 4298 | * |
| 4299 | * Each matching string is assigned a score. The following factors are checked: |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4300 | * - Matched letter |
| 4301 | * - Unmatched letter |
| 4302 | * - Consecutively matched letters |
| 4303 | * - Proximity to start |
| 4304 | * - Letter following a separator (space, underscore) |
| 4305 | * - Uppercase letter following lowercase (aka CamelCase) |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4306 | * |
| 4307 | * Matched letters are good. Unmatched letters are bad. Matching near the start |
| 4308 | * is good. Matching the first letter in the middle of a phrase is good. |
| 4309 | * Matching the uppercase letters in camel case entries is good. |
| 4310 | * |
| 4311 | * The score assigned for each factor is explained below. |
| 4312 | * File paths are different from file names. File extensions may be ignorable. |
| 4313 | * Single words care about consecutive matches but not separators or camel |
| 4314 | * case. |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4315 | * Score starts at 100 |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4316 | * Matched letter: +0 points |
| 4317 | * Unmatched letter: -1 point |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4318 | * Consecutive match bonus: +15 points |
| 4319 | * First letter bonus: +15 points |
| 4320 | * Separator bonus: +30 points |
| 4321 | * Camel case bonus: +30 points |
| 4322 | * Unmatched leading letter: -5 points (max: -15) |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4323 | * |
| 4324 | * There is some nuance to this. Scores don’t have an intrinsic meaning. The |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4325 | * score range isn’t 0 to 100. It’s roughly [50, 150]. Longer words have a |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4326 | * lower minimum score due to unmatched letter penalty. Longer search patterns |
| 4327 | * have a higher maximum score due to match bonuses. |
| 4328 | * |
| 4329 | * Separator and camel case bonus is worth a LOT. Consecutive matches are worth |
| 4330 | * quite a bit. |
| 4331 | * |
| 4332 | * There is a penalty if you DON’T match the first three letters. Which |
| 4333 | * effectively rewards matching near the start. However there’s no difference |
| 4334 | * in matching between the middle and end. |
| 4335 | * |
| 4336 | * There is not an explicit bonus for an exact match. Unmatched letters receive |
| 4337 | * a penalty. So shorter strings and closer matches are worth more. |
| 4338 | */ |
| 4339 | typedef struct |
| 4340 | { |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4341 | int idx; // used for stable sort |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4342 | listitem_T *item; |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4343 | int score; |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4344 | list_T *lmatchpos; |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4345 | } fuzzyItem_T; |
| 4346 | |
Bram Moolenaar | e9f9f16 | 2020-10-20 19:01:30 +0200 | [diff] [blame] | 4347 | // bonus for adjacent matches; this is higher than SEPARATOR_BONUS so that |
| 4348 | // matching a whole word is preferred. |
| 4349 | #define SEQUENTIAL_BONUS 40 |
Bram Moolenaar | dcdd42a | 2020-10-29 18:58:01 +0100 | [diff] [blame] | 4350 | // bonus if match occurs after a path separator |
| 4351 | #define PATH_SEPARATOR_BONUS 30 |
| 4352 | // bonus if match occurs after a word separator |
| 4353 | #define WORD_SEPARATOR_BONUS 25 |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4354 | // bonus if match is uppercase and prev is lower |
| 4355 | #define CAMEL_BONUS 30 |
| 4356 | // bonus if the first letter is matched |
| 4357 | #define FIRST_LETTER_BONUS 15 |
| 4358 | // penalty applied for every letter in str before the first match |
kylo252 | ae6f1d8 | 2022-02-16 19:24:07 +0000 | [diff] [blame] | 4359 | #define LEADING_LETTER_PENALTY (-5) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4360 | // maximum penalty for leading letters |
kylo252 | ae6f1d8 | 2022-02-16 19:24:07 +0000 | [diff] [blame] | 4361 | #define MAX_LEADING_LETTER_PENALTY (-15) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4362 | // penalty for every letter that doesn't match |
kylo252 | ae6f1d8 | 2022-02-16 19:24:07 +0000 | [diff] [blame] | 4363 | #define UNMATCHED_LETTER_PENALTY (-1) |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4364 | // penalty for gap in matching positions (-2 * k) |
kylo252 | ae6f1d8 | 2022-02-16 19:24:07 +0000 | [diff] [blame] | 4365 | #define GAP_PENALTY (-2) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4366 | // Score for a string that doesn't fuzzy match the pattern |
kylo252 | ae6f1d8 | 2022-02-16 19:24:07 +0000 | [diff] [blame] | 4367 | #define SCORE_NONE (-9999) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4368 | |
| 4369 | #define FUZZY_MATCH_RECURSION_LIMIT 10 |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4370 | |
| 4371 | /* |
| 4372 | * Compute a score for a fuzzy matched string. The matching character locations |
| 4373 | * are in 'matches'. |
| 4374 | */ |
| 4375 | static int |
| 4376 | fuzzy_match_compute_score( |
| 4377 | char_u *str, |
| 4378 | int strSz, |
Yegappan Lakshmanan | bb01a1e | 2021-04-26 21:17:52 +0200 | [diff] [blame] | 4379 | int_u *matches, |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4380 | int numMatches) |
| 4381 | { |
| 4382 | int score; |
| 4383 | int penalty; |
| 4384 | int unmatched; |
| 4385 | int i; |
| 4386 | char_u *p = str; |
Yegappan Lakshmanan | bb01a1e | 2021-04-26 21:17:52 +0200 | [diff] [blame] | 4387 | int_u sidx = 0; |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4388 | |
| 4389 | // Initialize score |
| 4390 | score = 100; |
| 4391 | |
| 4392 | // Apply leading letter penalty |
| 4393 | penalty = LEADING_LETTER_PENALTY * matches[0]; |
| 4394 | if (penalty < MAX_LEADING_LETTER_PENALTY) |
| 4395 | penalty = MAX_LEADING_LETTER_PENALTY; |
| 4396 | score += penalty; |
| 4397 | |
| 4398 | // Apply unmatched penalty |
| 4399 | unmatched = strSz - numMatches; |
| 4400 | score += UNMATCHED_LETTER_PENALTY * unmatched; |
| 4401 | |
| 4402 | // Apply ordering bonuses |
| 4403 | for (i = 0; i < numMatches; ++i) |
| 4404 | { |
Yegappan Lakshmanan | bb01a1e | 2021-04-26 21:17:52 +0200 | [diff] [blame] | 4405 | int_u currIdx = matches[i]; |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4406 | |
| 4407 | if (i > 0) |
| 4408 | { |
Yegappan Lakshmanan | bb01a1e | 2021-04-26 21:17:52 +0200 | [diff] [blame] | 4409 | int_u prevIdx = matches[i - 1]; |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4410 | |
| 4411 | // Sequential |
| 4412 | if (currIdx == (prevIdx + 1)) |
| 4413 | score += SEQUENTIAL_BONUS; |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4414 | else |
| 4415 | score += GAP_PENALTY * (currIdx - prevIdx); |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4416 | } |
| 4417 | |
| 4418 | // Check for bonuses based on neighbor character value |
| 4419 | if (currIdx > 0) |
| 4420 | { |
| 4421 | // Camel case |
Bram Moolenaar | c53e9c5 | 2020-09-22 22:08:32 +0200 | [diff] [blame] | 4422 | int neighbor = ' '; |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4423 | int curr; |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4424 | |
| 4425 | if (has_mbyte) |
| 4426 | { |
| 4427 | while (sidx < currIdx) |
| 4428 | { |
| 4429 | neighbor = (*mb_ptr2char)(p); |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4430 | MB_PTR_ADV(p); |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4431 | sidx++; |
| 4432 | } |
| 4433 | curr = (*mb_ptr2char)(p); |
| 4434 | } |
| 4435 | else |
| 4436 | { |
| 4437 | neighbor = str[currIdx - 1]; |
| 4438 | curr = str[currIdx]; |
| 4439 | } |
| 4440 | |
| 4441 | if (vim_islower(neighbor) && vim_isupper(curr)) |
| 4442 | score += CAMEL_BONUS; |
| 4443 | |
Bram Moolenaar | dcdd42a | 2020-10-29 18:58:01 +0100 | [diff] [blame] | 4444 | // Bonus if the match follows a separator character |
| 4445 | if (neighbor == '/' || neighbor == '\\') |
| 4446 | score += PATH_SEPARATOR_BONUS; |
| 4447 | else if (neighbor == ' ' || neighbor == '_') |
| 4448 | score += WORD_SEPARATOR_BONUS; |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4449 | } |
| 4450 | else |
| 4451 | { |
| 4452 | // First letter |
| 4453 | score += FIRST_LETTER_BONUS; |
| 4454 | } |
| 4455 | } |
| 4456 | return score; |
| 4457 | } |
| 4458 | |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4459 | /* |
| 4460 | * Perform a recursive search for fuzzy matching 'fuzpat' in 'str'. |
| 4461 | * Return the number of matching characters. |
| 4462 | */ |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4463 | static int |
| 4464 | fuzzy_match_recursive( |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4465 | char_u *fuzpat, |
| 4466 | char_u *str, |
Yegappan Lakshmanan | bb01a1e | 2021-04-26 21:17:52 +0200 | [diff] [blame] | 4467 | int_u strIdx, |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4468 | int *outScore, |
| 4469 | char_u *strBegin, |
| 4470 | int strLen, |
Yegappan Lakshmanan | bb01a1e | 2021-04-26 21:17:52 +0200 | [diff] [blame] | 4471 | int_u *srcMatches, |
| 4472 | int_u *matches, |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4473 | int maxMatches, |
| 4474 | int nextMatch, |
| 4475 | int *recursionCount) |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4476 | { |
| 4477 | // Recursion params |
| 4478 | int recursiveMatch = FALSE; |
Yegappan Lakshmanan | bb01a1e | 2021-04-26 21:17:52 +0200 | [diff] [blame] | 4479 | int_u bestRecursiveMatches[MAX_FUZZY_MATCHES]; |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4480 | int bestRecursiveScore = 0; |
| 4481 | int first_match; |
| 4482 | int matched; |
| 4483 | |
| 4484 | // Count recursions |
| 4485 | ++*recursionCount; |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4486 | if (*recursionCount >= FUZZY_MATCH_RECURSION_LIMIT) |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4487 | return 0; |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4488 | |
| 4489 | // Detect end of strings |
Yegappan Lakshmanan | bb01a1e | 2021-04-26 21:17:52 +0200 | [diff] [blame] | 4490 | if (*fuzpat == NUL || *str == NUL) |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4491 | return 0; |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4492 | |
| 4493 | // Loop through fuzpat and str looking for a match |
| 4494 | first_match = TRUE; |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4495 | while (*fuzpat != NUL && *str != NUL) |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4496 | { |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4497 | int c1; |
| 4498 | int c2; |
| 4499 | |
| 4500 | c1 = PTR2CHAR(fuzpat); |
| 4501 | c2 = PTR2CHAR(str); |
| 4502 | |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4503 | // Found match |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4504 | if (vim_tolower(c1) == vim_tolower(c2)) |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4505 | { |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4506 | // Supplied matches buffer was too short |
| 4507 | if (nextMatch >= maxMatches) |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4508 | return 0; |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4509 | |
Bram Moolenaar | caf642c | 2023-04-29 21:38:04 +0100 | [diff] [blame] | 4510 | int recursiveScore = 0; |
| 4511 | int_u recursiveMatches[MAX_FUZZY_MATCHES]; |
| 4512 | CLEAR_FIELD(recursiveMatches); |
| 4513 | |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4514 | // "Copy-on-Write" srcMatches into matches |
| 4515 | if (first_match && srcMatches) |
| 4516 | { |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4517 | memcpy(matches, srcMatches, nextMatch * sizeof(srcMatches[0])); |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4518 | first_match = FALSE; |
| 4519 | } |
| 4520 | |
| 4521 | // Recursive call that "skips" this match |
Bram Moolenaar | caf642c | 2023-04-29 21:38:04 +0100 | [diff] [blame] | 4522 | char_u *next_char = str + (has_mbyte ? (*mb_ptr2len)(str) : 1); |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4523 | if (fuzzy_match_recursive(fuzpat, next_char, strIdx + 1, |
| 4524 | &recursiveScore, strBegin, strLen, matches, |
| 4525 | recursiveMatches, |
K.Takata | eeec254 | 2021-06-02 13:28:16 +0200 | [diff] [blame] | 4526 | ARRAY_LENGTH(recursiveMatches), |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4527 | nextMatch, recursionCount)) |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4528 | { |
| 4529 | // Pick best recursive score |
| 4530 | if (!recursiveMatch || recursiveScore > bestRecursiveScore) |
| 4531 | { |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4532 | memcpy(bestRecursiveMatches, recursiveMatches, |
Yegappan Lakshmanan | bb01a1e | 2021-04-26 21:17:52 +0200 | [diff] [blame] | 4533 | MAX_FUZZY_MATCHES * sizeof(recursiveMatches[0])); |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4534 | bestRecursiveScore = recursiveScore; |
| 4535 | } |
| 4536 | recursiveMatch = TRUE; |
| 4537 | } |
| 4538 | |
| 4539 | // Advance |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4540 | matches[nextMatch++] = strIdx; |
| 4541 | if (has_mbyte) |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4542 | MB_PTR_ADV(fuzpat); |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4543 | else |
| 4544 | ++fuzpat; |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4545 | } |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4546 | if (has_mbyte) |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4547 | MB_PTR_ADV(str); |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4548 | else |
| 4549 | ++str; |
| 4550 | strIdx++; |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4551 | } |
| 4552 | |
| 4553 | // Determine if full fuzpat was matched |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4554 | matched = *fuzpat == NUL ? TRUE : FALSE; |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4555 | |
| 4556 | // Calculate score |
| 4557 | if (matched) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4558 | *outScore = fuzzy_match_compute_score(strBegin, strLen, matches, |
| 4559 | nextMatch); |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4560 | |
| 4561 | // Return best result |
| 4562 | if (recursiveMatch && (!matched || bestRecursiveScore > *outScore)) |
| 4563 | { |
| 4564 | // Recursive score is better than "this" |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4565 | memcpy(matches, bestRecursiveMatches, maxMatches * sizeof(matches[0])); |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4566 | *outScore = bestRecursiveScore; |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4567 | return nextMatch; |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4568 | } |
| 4569 | else if (matched) |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4570 | return nextMatch; // "this" score is better than recursive |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4571 | |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4572 | return 0; // no match |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4573 | } |
| 4574 | |
| 4575 | /* |
| 4576 | * fuzzy_match() |
| 4577 | * |
| 4578 | * Performs exhaustive search via recursion to find all possible matches and |
| 4579 | * match with highest score. |
| 4580 | * Scores values have no intrinsic meaning. Possible score range is not |
| 4581 | * normalized and varies with pattern. |
| 4582 | * Recursion is limited internally (default=10) to prevent degenerate cases |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4583 | * (pat_arg="aaaaaa" str="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"). |
Yegappan Lakshmanan | bb01a1e | 2021-04-26 21:17:52 +0200 | [diff] [blame] | 4584 | * Uses char_u for match indices. Therefore patterns are limited to |
| 4585 | * MAX_FUZZY_MATCHES characters. |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4586 | * |
Bram Moolenaar | caf642c | 2023-04-29 21:38:04 +0100 | [diff] [blame] | 4587 | * Returns TRUE if "pat_arg" matches "str". Also returns the match score in |
| 4588 | * "outScore" and the matching character positions in "matches". |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4589 | */ |
Yegappan Lakshmanan | bb01a1e | 2021-04-26 21:17:52 +0200 | [diff] [blame] | 4590 | int |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4591 | fuzzy_match( |
| 4592 | char_u *str, |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4593 | char_u *pat_arg, |
| 4594 | int matchseq, |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4595 | int *outScore, |
Yegappan Lakshmanan | bb01a1e | 2021-04-26 21:17:52 +0200 | [diff] [blame] | 4596 | int_u *matches, |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4597 | int maxMatches) |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4598 | { |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4599 | int recursionCount = 0; |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4600 | int len = MB_CHARLEN(str); |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4601 | char_u *save_pat; |
| 4602 | char_u *pat; |
| 4603 | char_u *p; |
| 4604 | int complete = FALSE; |
| 4605 | int score = 0; |
| 4606 | int numMatches = 0; |
| 4607 | int matchCount; |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4608 | |
| 4609 | *outScore = 0; |
| 4610 | |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4611 | save_pat = vim_strsave(pat_arg); |
| 4612 | if (save_pat == NULL) |
| 4613 | return FALSE; |
| 4614 | pat = save_pat; |
| 4615 | p = pat; |
| 4616 | |
| 4617 | // Try matching each word in 'pat_arg' in 'str' |
| 4618 | while (TRUE) |
| 4619 | { |
| 4620 | if (matchseq) |
| 4621 | complete = TRUE; |
| 4622 | else |
| 4623 | { |
| 4624 | // Extract one word from the pattern (separated by space) |
| 4625 | p = skipwhite(p); |
| 4626 | if (*p == NUL) |
| 4627 | break; |
| 4628 | pat = p; |
| 4629 | while (*p != NUL && !VIM_ISWHITE(PTR2CHAR(p))) |
| 4630 | { |
| 4631 | if (has_mbyte) |
| 4632 | MB_PTR_ADV(p); |
| 4633 | else |
| 4634 | ++p; |
| 4635 | } |
| 4636 | if (*p == NUL) // processed all the words |
| 4637 | complete = TRUE; |
| 4638 | *p = NUL; |
| 4639 | } |
| 4640 | |
| 4641 | score = 0; |
| 4642 | recursionCount = 0; |
| 4643 | matchCount = fuzzy_match_recursive(pat, str, 0, &score, str, len, NULL, |
| 4644 | matches + numMatches, maxMatches - numMatches, |
| 4645 | 0, &recursionCount); |
| 4646 | if (matchCount == 0) |
| 4647 | { |
| 4648 | numMatches = 0; |
| 4649 | break; |
| 4650 | } |
| 4651 | |
| 4652 | // Accumulate the match score and the number of matches |
| 4653 | *outScore += score; |
| 4654 | numMatches += matchCount; |
| 4655 | |
| 4656 | if (complete) |
| 4657 | break; |
| 4658 | |
| 4659 | // try matching the next word |
| 4660 | ++p; |
| 4661 | } |
| 4662 | |
| 4663 | vim_free(save_pat); |
| 4664 | return numMatches != 0; |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4665 | } |
| 4666 | |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 4667 | #if defined(FEAT_EVAL) || defined(FEAT_PROTO) |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4668 | /* |
| 4669 | * Sort the fuzzy matches in the descending order of the match score. |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4670 | * For items with same score, retain the order using the index (stable sort) |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4671 | */ |
| 4672 | static int |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4673 | fuzzy_match_item_compare(const void *s1, const void *s2) |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4674 | { |
| 4675 | int v1 = ((fuzzyItem_T *)s1)->score; |
| 4676 | int v2 = ((fuzzyItem_T *)s2)->score; |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4677 | int idx1 = ((fuzzyItem_T *)s1)->idx; |
| 4678 | int idx2 = ((fuzzyItem_T *)s2)->idx; |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4679 | |
zeertzjq | 7707827 | 2024-02-10 13:24:03 +0100 | [diff] [blame] | 4680 | if (v1 == v2) |
| 4681 | return idx1 == idx2 ? 0 : idx1 > idx2 ? 1 : -1; |
| 4682 | else |
| 4683 | return v1 > v2 ? -1 : 1; |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4684 | } |
| 4685 | |
| 4686 | /* |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4687 | * Fuzzy search the string 'str' in a list of 'items' and return the matching |
| 4688 | * strings in 'fmatchlist'. |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4689 | * If 'matchseq' is TRUE, then for multi-word search strings, match all the |
| 4690 | * words in sequence. |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4691 | * If 'items' is a list of strings, then search for 'str' in the list. |
| 4692 | * If 'items' is a list of dicts, then either use 'key' to lookup the string |
| 4693 | * for each item or use 'item_cb' Funcref function to get the string. |
| 4694 | * If 'retmatchpos' is TRUE, then return a list of positions where 'str' |
| 4695 | * matches for each item. |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4696 | */ |
| 4697 | static void |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4698 | fuzzy_match_in_list( |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4699 | list_T *l, |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4700 | char_u *str, |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4701 | int matchseq, |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4702 | char_u *key, |
| 4703 | callback_T *item_cb, |
| 4704 | int retmatchpos, |
Yasuhiro Matsumoto | 9029a6e | 2022-04-16 12:35:35 +0100 | [diff] [blame] | 4705 | list_T *fmatchlist, |
| 4706 | long max_matches) |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4707 | { |
| 4708 | long len; |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4709 | fuzzyItem_T *items; |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4710 | listitem_T *li; |
| 4711 | long i = 0; |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4712 | long match_count = 0; |
Yegappan Lakshmanan | bb01a1e | 2021-04-26 21:17:52 +0200 | [diff] [blame] | 4713 | int_u matches[MAX_FUZZY_MATCHES]; |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4714 | |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4715 | len = list_len(l); |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4716 | if (len == 0) |
| 4717 | return; |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4718 | if (max_matches > 0 && len > max_matches) |
| 4719 | len = max_matches; |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4720 | |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4721 | items = ALLOC_CLEAR_MULT(fuzzyItem_T, len); |
| 4722 | if (items == NULL) |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4723 | return; |
| 4724 | |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4725 | // For all the string items in items, get the fuzzy matching score |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4726 | FOR_ALL_LIST_ITEMS(l, li) |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4727 | { |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4728 | int score; |
| 4729 | char_u *itemstr; |
| 4730 | typval_T rettv; |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4731 | |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4732 | if (max_matches > 0 && match_count >= max_matches) |
| 4733 | break; |
Yasuhiro Matsumoto | 9029a6e | 2022-04-16 12:35:35 +0100 | [diff] [blame] | 4734 | |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4735 | itemstr = NULL; |
| 4736 | rettv.v_type = VAR_UNKNOWN; |
| 4737 | if (li->li_tv.v_type == VAR_STRING) // list of strings |
| 4738 | itemstr = li->li_tv.vval.v_string; |
Dominique Pelle | 7765f5c | 2022-04-10 11:26:53 +0100 | [diff] [blame] | 4739 | else if (li->li_tv.v_type == VAR_DICT |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4740 | && (key != NULL || item_cb->cb_name != NULL)) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4741 | { |
| 4742 | // For a dict, either use the specified key to lookup the string or |
| 4743 | // use the specified callback function to get the string. |
| 4744 | if (key != NULL) |
Bram Moolenaar | d61efa5 | 2022-07-23 09:52:04 +0100 | [diff] [blame] | 4745 | itemstr = dict_get_string(li->li_tv.vval.v_dict, |
| 4746 | (char *)key, FALSE); |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4747 | else |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4748 | { |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4749 | typval_T argv[2]; |
| 4750 | |
| 4751 | // Invoke the supplied callback (if any) to get the dict item |
| 4752 | li->li_tv.vval.v_dict->dv_refcount++; |
| 4753 | argv[0].v_type = VAR_DICT; |
| 4754 | argv[0].vval.v_dict = li->li_tv.vval.v_dict; |
| 4755 | argv[1].v_type = VAR_UNKNOWN; |
| 4756 | if (call_callback(item_cb, -1, &rettv, 1, argv) != FAIL) |
| 4757 | { |
| 4758 | if (rettv.v_type == VAR_STRING) |
| 4759 | itemstr = rettv.vval.v_string; |
| 4760 | } |
| 4761 | dict_unref(li->li_tv.vval.v_dict); |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4762 | } |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4763 | } |
| 4764 | |
| 4765 | if (itemstr != NULL |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4766 | && fuzzy_match(itemstr, str, matchseq, &score, matches, |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4767 | MAX_FUZZY_MATCHES)) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4768 | { |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4769 | items[match_count].idx = match_count; |
| 4770 | items[match_count].item = li; |
| 4771 | items[match_count].score = score; |
| 4772 | |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4773 | // Copy the list of matching positions in itemstr to a list, if |
| 4774 | // 'retmatchpos' is set. |
| 4775 | if (retmatchpos) |
| 4776 | { |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4777 | int j = 0; |
| 4778 | char_u *p; |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4779 | |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4780 | items[match_count].lmatchpos = list_alloc(); |
| 4781 | if (items[match_count].lmatchpos == NULL) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4782 | goto done; |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4783 | |
| 4784 | p = str; |
| 4785 | while (*p != NUL) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4786 | { |
zeertzjq | 9af2bc0 | 2022-05-11 14:15:37 +0100 | [diff] [blame] | 4787 | if (!VIM_ISWHITE(PTR2CHAR(p)) || matchseq) |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4788 | { |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4789 | if (list_append_number(items[match_count].lmatchpos, |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4790 | matches[j]) == FAIL) |
| 4791 | goto done; |
| 4792 | j++; |
| 4793 | } |
| 4794 | if (has_mbyte) |
| 4795 | MB_PTR_ADV(p); |
| 4796 | else |
| 4797 | ++p; |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4798 | } |
| 4799 | } |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4800 | ++match_count; |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4801 | } |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4802 | clear_tv(&rettv); |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4803 | } |
| 4804 | |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4805 | if (match_count > 0) |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4806 | { |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4807 | list_T *retlist; |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4808 | |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4809 | // Sort the list by the descending order of the match score |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4810 | qsort((void *)items, (size_t)match_count, sizeof(fuzzyItem_T), |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4811 | fuzzy_match_item_compare); |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4812 | |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4813 | // For matchfuzzy(), return a list of matched strings. |
| 4814 | // ['str1', 'str2', 'str3'] |
Bram Moolenaar | 9d19e4f | 2021-01-02 18:31:32 +0100 | [diff] [blame] | 4815 | // For matchfuzzypos(), return a list with three items. |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4816 | // The first item is a list of matched strings. The second item |
| 4817 | // is a list of lists where each list item is a list of matched |
Bram Moolenaar | 9d19e4f | 2021-01-02 18:31:32 +0100 | [diff] [blame] | 4818 | // character positions. The third item is a list of matching scores. |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4819 | // [['str1', 'str2', 'str3'], [[1, 3], [1, 3], [1, 3]]] |
| 4820 | if (retmatchpos) |
| 4821 | { |
| 4822 | li = list_find(fmatchlist, 0); |
| 4823 | if (li == NULL || li->li_tv.vval.v_list == NULL) |
| 4824 | goto done; |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4825 | retlist = li->li_tv.vval.v_list; |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4826 | } |
| 4827 | else |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4828 | retlist = fmatchlist; |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4829 | |
| 4830 | // Copy the matching strings with a valid score to the return list |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4831 | for (i = 0; i < match_count; i++) |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4832 | { |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4833 | if (items[i].score == SCORE_NONE) |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4834 | break; |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4835 | list_append_tv(retlist, &items[i].item->li_tv); |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4836 | } |
| 4837 | |
| 4838 | // next copy the list of matching positions |
| 4839 | if (retmatchpos) |
| 4840 | { |
Bram Moolenaar | 9d19e4f | 2021-01-02 18:31:32 +0100 | [diff] [blame] | 4841 | li = list_find(fmatchlist, -2); |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4842 | if (li == NULL || li->li_tv.vval.v_list == NULL) |
| 4843 | goto done; |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4844 | retlist = li->li_tv.vval.v_list; |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4845 | |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4846 | for (i = 0; i < match_count; i++) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4847 | { |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4848 | if (items[i].score == SCORE_NONE) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4849 | break; |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4850 | if (items[i].lmatchpos != NULL |
Bram Moolenaar | 9ba6194 | 2022-08-31 11:25:06 +0100 | [diff] [blame] | 4851 | && list_append_list(retlist, items[i].lmatchpos) == FAIL) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4852 | goto done; |
| 4853 | } |
Bram Moolenaar | 9d19e4f | 2021-01-02 18:31:32 +0100 | [diff] [blame] | 4854 | |
| 4855 | // copy the matching scores |
| 4856 | li = list_find(fmatchlist, -1); |
| 4857 | if (li == NULL || li->li_tv.vval.v_list == NULL) |
| 4858 | goto done; |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4859 | retlist = li->li_tv.vval.v_list; |
| 4860 | for (i = 0; i < match_count; i++) |
Bram Moolenaar | 9d19e4f | 2021-01-02 18:31:32 +0100 | [diff] [blame] | 4861 | { |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4862 | if (items[i].score == SCORE_NONE) |
Bram Moolenaar | 9d19e4f | 2021-01-02 18:31:32 +0100 | [diff] [blame] | 4863 | break; |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4864 | if (list_append_number(retlist, items[i].score) == FAIL) |
Bram Moolenaar | 9d19e4f | 2021-01-02 18:31:32 +0100 | [diff] [blame] | 4865 | goto done; |
| 4866 | } |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4867 | } |
| 4868 | } |
| 4869 | |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4870 | done: |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4871 | vim_free(items); |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4872 | } |
| 4873 | |
| 4874 | /* |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4875 | * Do fuzzy matching. Returns the list of matched strings in 'rettv'. |
| 4876 | * If 'retmatchpos' is TRUE, also returns the matching character positions. |
| 4877 | */ |
| 4878 | static void |
| 4879 | do_fuzzymatch(typval_T *argvars, typval_T *rettv, int retmatchpos) |
| 4880 | { |
| 4881 | callback_T cb; |
| 4882 | char_u *key = NULL; |
| 4883 | int ret; |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4884 | int matchseq = FALSE; |
Yasuhiro Matsumoto | 9029a6e | 2022-04-16 12:35:35 +0100 | [diff] [blame] | 4885 | long max_matches = 0; |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4886 | |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 4887 | if (in_vim9script() |
| 4888 | && (check_for_list_arg(argvars, 0) == FAIL |
| 4889 | || check_for_string_arg(argvars, 1) == FAIL |
| 4890 | || check_for_opt_dict_arg(argvars, 2) == FAIL)) |
| 4891 | return; |
| 4892 | |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4893 | CLEAR_POINTER(&cb); |
| 4894 | |
| 4895 | // validate and get the arguments |
| 4896 | if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL) |
| 4897 | { |
Bram Moolenaar | 3a846e6 | 2022-01-01 16:21:00 +0000 | [diff] [blame] | 4898 | semsg(_(e_argument_of_str_must_be_list), |
| 4899 | retmatchpos ? "matchfuzzypos()" : "matchfuzzy()"); |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4900 | return; |
| 4901 | } |
| 4902 | if (argvars[1].v_type != VAR_STRING |
| 4903 | || argvars[1].vval.v_string == NULL) |
| 4904 | { |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 4905 | semsg(_(e_invalid_argument_str), tv_get_string(&argvars[1])); |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4906 | return; |
| 4907 | } |
| 4908 | |
| 4909 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 4910 | { |
| 4911 | dict_T *d; |
| 4912 | dictitem_T *di; |
| 4913 | |
Yegappan Lakshmanan | 04c4c57 | 2022-08-30 19:48:24 +0100 | [diff] [blame] | 4914 | if (check_for_nonnull_dict_arg(argvars, 2) == FAIL) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4915 | return; |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4916 | |
| 4917 | // To search a dict, either a callback function or a key can be |
| 4918 | // specified. |
| 4919 | d = argvars[2].vval.v_dict; |
| 4920 | if ((di = dict_find(d, (char_u *)"key", -1)) != NULL) |
| 4921 | { |
| 4922 | if (di->di_tv.v_type != VAR_STRING |
| 4923 | || di->di_tv.vval.v_string == NULL |
| 4924 | || *di->di_tv.vval.v_string == NUL) |
| 4925 | { |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 4926 | semsg(_(e_invalid_argument_str), tv_get_string(&di->di_tv)); |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4927 | return; |
| 4928 | } |
| 4929 | key = tv_get_string(&di->di_tv); |
| 4930 | } |
| 4931 | else if ((di = dict_find(d, (char_u *)"text_cb", -1)) != NULL) |
| 4932 | { |
| 4933 | cb = get_callback(&di->di_tv); |
| 4934 | if (cb.cb_name == NULL) |
| 4935 | { |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 4936 | semsg(_(e_invalid_value_for_argument_str), "text_cb"); |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4937 | return; |
| 4938 | } |
| 4939 | } |
Kazuyuki Miyagi | 47f1a55 | 2022-06-17 18:30:03 +0100 | [diff] [blame] | 4940 | |
| 4941 | if ((di = dict_find(d, (char_u *)"limit", -1)) != NULL) |
Yasuhiro Matsumoto | 9029a6e | 2022-04-16 12:35:35 +0100 | [diff] [blame] | 4942 | { |
| 4943 | if (di->di_tv.v_type != VAR_NUMBER) |
| 4944 | { |
| 4945 | semsg(_(e_invalid_argument_str), tv_get_string(&di->di_tv)); |
| 4946 | return; |
| 4947 | } |
| 4948 | max_matches = (long)tv_get_number_chk(&di->di_tv, NULL); |
| 4949 | } |
| 4950 | |
Yegappan Lakshmanan | 4829c1c | 2022-04-04 15:16:54 +0100 | [diff] [blame] | 4951 | if (dict_has_key(d, "matchseq")) |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4952 | matchseq = TRUE; |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4953 | } |
| 4954 | |
| 4955 | // get the fuzzy matches |
| 4956 | ret = rettv_list_alloc(rettv); |
Bram Moolenaar | 5ea38d1 | 2022-06-16 21:20:48 +0100 | [diff] [blame] | 4957 | if (ret == FAIL) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4958 | goto done; |
| 4959 | if (retmatchpos) |
| 4960 | { |
| 4961 | list_T *l; |
| 4962 | |
Bram Moolenaar | 9d19e4f | 2021-01-02 18:31:32 +0100 | [diff] [blame] | 4963 | // For matchfuzzypos(), a list with three items are returned. First |
| 4964 | // item is a list of matching strings, the second item is a list of |
| 4965 | // lists with matching positions within each string and the third item |
| 4966 | // is the list of scores of the matches. |
| 4967 | l = list_alloc(); |
| 4968 | if (l == NULL) |
| 4969 | goto done; |
| 4970 | if (list_append_list(rettv->vval.v_list, l) == FAIL) |
Bram Moolenaar | 9ba6194 | 2022-08-31 11:25:06 +0100 | [diff] [blame] | 4971 | { |
| 4972 | vim_free(l); |
Bram Moolenaar | 9d19e4f | 2021-01-02 18:31:32 +0100 | [diff] [blame] | 4973 | goto done; |
Bram Moolenaar | 9ba6194 | 2022-08-31 11:25:06 +0100 | [diff] [blame] | 4974 | } |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4975 | l = list_alloc(); |
| 4976 | if (l == NULL) |
| 4977 | goto done; |
| 4978 | if (list_append_list(rettv->vval.v_list, l) == FAIL) |
Bram Moolenaar | 9ba6194 | 2022-08-31 11:25:06 +0100 | [diff] [blame] | 4979 | { |
| 4980 | vim_free(l); |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4981 | goto done; |
Bram Moolenaar | 9ba6194 | 2022-08-31 11:25:06 +0100 | [diff] [blame] | 4982 | } |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4983 | l = list_alloc(); |
| 4984 | if (l == NULL) |
| 4985 | goto done; |
| 4986 | if (list_append_list(rettv->vval.v_list, l) == FAIL) |
Bram Moolenaar | 9ba6194 | 2022-08-31 11:25:06 +0100 | [diff] [blame] | 4987 | { |
| 4988 | vim_free(l); |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4989 | goto done; |
Bram Moolenaar | 9ba6194 | 2022-08-31 11:25:06 +0100 | [diff] [blame] | 4990 | } |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4991 | } |
| 4992 | |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4993 | fuzzy_match_in_list(argvars[0].vval.v_list, tv_get_string(&argvars[1]), |
Yasuhiro Matsumoto | 9029a6e | 2022-04-16 12:35:35 +0100 | [diff] [blame] | 4994 | matchseq, key, &cb, retmatchpos, rettv->vval.v_list, max_matches); |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4995 | |
| 4996 | done: |
| 4997 | free_callback(&cb); |
| 4998 | } |
| 4999 | |
| 5000 | /* |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 5001 | * "matchfuzzy()" function |
| 5002 | */ |
| 5003 | void |
| 5004 | f_matchfuzzy(typval_T *argvars, typval_T *rettv) |
| 5005 | { |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 5006 | do_fuzzymatch(argvars, rettv, FALSE); |
| 5007 | } |
| 5008 | |
| 5009 | /* |
| 5010 | * "matchfuzzypos()" function |
| 5011 | */ |
| 5012 | void |
| 5013 | f_matchfuzzypos(typval_T *argvars, typval_T *rettv) |
| 5014 | { |
| 5015 | do_fuzzymatch(argvars, rettv, TRUE); |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 5016 | } |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 5017 | #endif |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 5018 | |
| 5019 | /* |
| 5020 | * Same as fuzzy_match_item_compare() except for use with a string match |
| 5021 | */ |
| 5022 | static int |
| 5023 | fuzzy_match_str_compare(const void *s1, const void *s2) |
| 5024 | { |
| 5025 | int v1 = ((fuzmatch_str_T *)s1)->score; |
| 5026 | int v2 = ((fuzmatch_str_T *)s2)->score; |
| 5027 | int idx1 = ((fuzmatch_str_T *)s1)->idx; |
| 5028 | int idx2 = ((fuzmatch_str_T *)s2)->idx; |
| 5029 | |
Christian Brabandt | e06e437 | 2024-02-09 19:39:14 +0100 | [diff] [blame] | 5030 | if (v1 == v2) |
| 5031 | return idx1 == idx2 ? 0 : idx1 > idx2 ? 1 : -1; |
| 5032 | else |
| 5033 | return v1 > v2 ? -1 : 1; |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 5034 | } |
| 5035 | |
| 5036 | /* |
| 5037 | * Sort fuzzy matches by score |
| 5038 | */ |
| 5039 | static void |
| 5040 | fuzzy_match_str_sort(fuzmatch_str_T *fm, int sz) |
| 5041 | { |
| 5042 | // Sort the list by the descending order of the match score |
| 5043 | qsort((void *)fm, (size_t)sz, sizeof(fuzmatch_str_T), |
| 5044 | fuzzy_match_str_compare); |
| 5045 | } |
| 5046 | |
| 5047 | /* |
| 5048 | * Same as fuzzy_match_item_compare() except for use with a function name |
| 5049 | * string match. <SNR> functions should be sorted to the end. |
| 5050 | */ |
| 5051 | static int |
| 5052 | fuzzy_match_func_compare(const void *s1, const void *s2) |
| 5053 | { |
| 5054 | int v1 = ((fuzmatch_str_T *)s1)->score; |
| 5055 | int v2 = ((fuzmatch_str_T *)s2)->score; |
| 5056 | int idx1 = ((fuzmatch_str_T *)s1)->idx; |
| 5057 | int idx2 = ((fuzmatch_str_T *)s2)->idx; |
| 5058 | char_u *str1 = ((fuzmatch_str_T *)s1)->str; |
| 5059 | char_u *str2 = ((fuzmatch_str_T *)s2)->str; |
| 5060 | |
Christian Brabandt | e06e437 | 2024-02-09 19:39:14 +0100 | [diff] [blame] | 5061 | if (*str1 != '<' && *str2 == '<') |
| 5062 | return -1; |
| 5063 | if (*str1 == '<' && *str2 != '<') |
| 5064 | return 1; |
| 5065 | if (v1 == v2) |
| 5066 | return idx1 == idx2 ? 0 : idx1 > idx2 ? 1 : -1; |
| 5067 | else |
| 5068 | return v1 > v2 ? -1 : 1; |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 5069 | } |
| 5070 | |
| 5071 | /* |
| 5072 | * Sort fuzzy matches of function names by score. |
| 5073 | * <SNR> functions should be sorted to the end. |
| 5074 | */ |
| 5075 | static void |
| 5076 | fuzzy_match_func_sort(fuzmatch_str_T *fm, int sz) |
| 5077 | { |
| 5078 | // Sort the list by the descending order of the match score |
| 5079 | qsort((void *)fm, (size_t)sz, sizeof(fuzmatch_str_T), |
| 5080 | fuzzy_match_func_compare); |
| 5081 | } |
| 5082 | |
| 5083 | /* |
| 5084 | * Fuzzy match 'pat' in 'str'. Returns 0 if there is no match. Otherwise, |
| 5085 | * returns the match score. |
| 5086 | */ |
| 5087 | int |
| 5088 | fuzzy_match_str(char_u *str, char_u *pat) |
| 5089 | { |
| 5090 | int score = 0; |
Yegappan Lakshmanan | 5ec633b | 2022-02-25 15:24:24 +0000 | [diff] [blame] | 5091 | int_u matchpos[MAX_FUZZY_MATCHES]; |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 5092 | |
| 5093 | if (str == NULL || pat == NULL) |
| 5094 | return 0; |
| 5095 | |
Yegappan Lakshmanan | 6caeda2 | 2022-02-27 12:07:30 +0000 | [diff] [blame] | 5096 | fuzzy_match(str, pat, TRUE, &score, matchpos, |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 5097 | sizeof(matchpos) / sizeof(matchpos[0])); |
| 5098 | |
| 5099 | return score; |
| 5100 | } |
| 5101 | |
| 5102 | /* |
glepnir | 40c1c33 | 2024-06-11 19:37:04 +0200 | [diff] [blame] | 5103 | * Fuzzy match the position of string 'pat' in string 'str'. |
| 5104 | * Returns a dynamic array of matching positions. If there is no match, |
| 5105 | * returns NULL. |
| 5106 | */ |
| 5107 | garray_T * |
| 5108 | fuzzy_match_str_with_pos(char_u *str UNUSED, char_u *pat UNUSED) |
| 5109 | { |
| 5110 | #ifdef FEAT_SEARCH_EXTRA |
| 5111 | int score = 0; |
zeertzjq | 2f95ca9 | 2024-06-13 17:14:27 +0200 | [diff] [blame] | 5112 | garray_T *match_positions = NULL; |
| 5113 | int_u matches[MAX_FUZZY_MATCHES]; |
| 5114 | int j = 0; |
glepnir | 40c1c33 | 2024-06-11 19:37:04 +0200 | [diff] [blame] | 5115 | |
zeertzjq | 2f95ca9 | 2024-06-13 17:14:27 +0200 | [diff] [blame] | 5116 | if (str == NULL || pat == NULL) |
zeertzjq | d9be94c | 2024-07-14 10:20:20 +0200 | [diff] [blame] | 5117 | return NULL; |
zeertzjq | 2f95ca9 | 2024-06-13 17:14:27 +0200 | [diff] [blame] | 5118 | |
| 5119 | match_positions = ALLOC_ONE(garray_T); |
glepnir | 40c1c33 | 2024-06-11 19:37:04 +0200 | [diff] [blame] | 5120 | if (match_positions == NULL) |
zeertzjq | d9be94c | 2024-07-14 10:20:20 +0200 | [diff] [blame] | 5121 | return NULL; |
zeertzjq | 2f95ca9 | 2024-06-13 17:14:27 +0200 | [diff] [blame] | 5122 | ga_init2(match_positions, sizeof(int_u), 10); |
| 5123 | |
| 5124 | if (!fuzzy_match(str, pat, FALSE, &score, matches, MAX_FUZZY_MATCHES) |
| 5125 | || score == 0) |
glepnir | 40c1c33 | 2024-06-11 19:37:04 +0200 | [diff] [blame] | 5126 | { |
zeertzjq | 2f95ca9 | 2024-06-13 17:14:27 +0200 | [diff] [blame] | 5127 | ga_clear(match_positions); |
| 5128 | vim_free(match_positions); |
| 5129 | return NULL; |
glepnir | 40c1c33 | 2024-06-11 19:37:04 +0200 | [diff] [blame] | 5130 | } |
| 5131 | |
zeertzjq | 2f95ca9 | 2024-06-13 17:14:27 +0200 | [diff] [blame] | 5132 | for (char_u *p = pat; *p != NUL; MB_PTR_ADV(p)) |
glepnir | 40c1c33 | 2024-06-11 19:37:04 +0200 | [diff] [blame] | 5133 | { |
zeertzjq | 2f95ca9 | 2024-06-13 17:14:27 +0200 | [diff] [blame] | 5134 | if (!VIM_ISWHITE(PTR2CHAR(p))) |
| 5135 | { |
| 5136 | ga_grow(match_positions, 1); |
| 5137 | ((int_u *)match_positions->ga_data)[match_positions->ga_len] = |
| 5138 | matches[j]; |
| 5139 | match_positions->ga_len++; |
| 5140 | j++; |
| 5141 | } |
glepnir | 40c1c33 | 2024-06-11 19:37:04 +0200 | [diff] [blame] | 5142 | } |
| 5143 | |
glepnir | 40c1c33 | 2024-06-11 19:37:04 +0200 | [diff] [blame] | 5144 | return match_positions; |
glepnir | 40c1c33 | 2024-06-11 19:37:04 +0200 | [diff] [blame] | 5145 | #else |
| 5146 | return NULL; |
| 5147 | #endif |
| 5148 | } |
| 5149 | |
| 5150 | /* |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 5151 | * This function searches for a fuzzy match of the pattern `pat` within the |
| 5152 | * line pointed to by `*ptr`. It splits the line into words, performs fuzzy |
| 5153 | * matching on each word, and returns the length and position of the first |
| 5154 | * matched word. |
| 5155 | */ |
| 5156 | static int |
| 5157 | fuzzy_match_str_in_line(char_u **ptr, char_u *pat, int *len, pos_T *current_pos) |
| 5158 | { |
| 5159 | char_u *str = *ptr; |
| 5160 | char_u *strBegin = str; |
| 5161 | char_u *end = NULL; |
| 5162 | char_u *start = NULL; |
| 5163 | int found = FALSE; |
| 5164 | int result; |
| 5165 | char save_end; |
| 5166 | |
| 5167 | if (str == NULL || pat == NULL) |
| 5168 | return found; |
| 5169 | |
| 5170 | while (*str != NUL) |
| 5171 | { |
| 5172 | // Skip non-word characters |
| 5173 | start = find_word_start(str); |
| 5174 | if (*start == NUL) |
| 5175 | break; |
| 5176 | end = find_word_end(start); |
| 5177 | |
| 5178 | // Extract the word from start to end |
| 5179 | save_end = *end; |
| 5180 | *end = NUL; |
| 5181 | |
| 5182 | // Perform fuzzy match |
| 5183 | result = fuzzy_match_str(start, pat); |
| 5184 | *end = save_end; |
| 5185 | |
| 5186 | if (result > 0) |
| 5187 | { |
| 5188 | *len = (int)(end - start); |
| 5189 | current_pos->col += (int)(end - strBegin); |
| 5190 | found = TRUE; |
| 5191 | *ptr = start; |
| 5192 | break; |
| 5193 | } |
| 5194 | |
| 5195 | // Move to the end of the current word for the next iteration |
| 5196 | str = end; |
| 5197 | // Ensure we continue searching after the current word |
| 5198 | while (*str != NUL && !vim_iswordp(str)) |
| 5199 | MB_PTR_ADV(str); |
| 5200 | } |
| 5201 | |
| 5202 | return found; |
| 5203 | } |
| 5204 | |
| 5205 | /* |
| 5206 | * Search for the next fuzzy match in the specified buffer. |
| 5207 | * This function attempts to find the next occurrence of the given pattern |
| 5208 | * in the buffer, starting from the current position. It handles line wrapping |
| 5209 | * and direction of search. |
| 5210 | * |
| 5211 | * Return TRUE if a match is found, otherwise FALSE. |
| 5212 | */ |
| 5213 | int |
| 5214 | search_for_fuzzy_match( |
| 5215 | buf_T *buf, |
| 5216 | pos_T *pos, |
| 5217 | char_u *pattern, |
| 5218 | int dir, |
| 5219 | pos_T *start_pos, |
| 5220 | int *len, |
| 5221 | char_u **ptr, |
| 5222 | int whole_line) |
| 5223 | { |
| 5224 | pos_T current_pos = *pos; |
| 5225 | pos_T circly_end; |
zeertzjq | 58d7052 | 2024-08-31 17:05:39 +0200 | [diff] [blame] | 5226 | int found_new_match = FALSE; |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 5227 | int looped_around = FALSE; |
glepnir | 7cfe693 | 2024-09-15 20:06:28 +0200 | [diff] [blame] | 5228 | char_u *next_word_end = NULL; |
| 5229 | char_u *match_word = NULL; |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 5230 | |
| 5231 | if (whole_line) |
| 5232 | current_pos.lnum += dir; |
| 5233 | |
glepnir | 0be03e1 | 2024-07-19 16:45:05 +0200 | [diff] [blame] | 5234 | if (buf == curbuf) |
| 5235 | circly_end = *start_pos; |
| 5236 | else |
| 5237 | { |
| 5238 | circly_end.lnum = buf->b_ml.ml_line_count; |
| 5239 | circly_end.col = 0; |
| 5240 | circly_end.coladd = 0; |
| 5241 | } |
| 5242 | |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 5243 | do { |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 5244 | |
| 5245 | // Check if looped around and back to start position |
| 5246 | if (looped_around && EQUAL_POS(current_pos, circly_end)) |
| 5247 | break; |
| 5248 | |
| 5249 | // Ensure current_pos is valid |
| 5250 | if (current_pos.lnum >= 1 && current_pos.lnum <= buf->b_ml.ml_line_count) |
| 5251 | { |
| 5252 | // Get the current line buffer |
| 5253 | *ptr = ml_get_buf(buf, current_pos.lnum, FALSE); |
| 5254 | // If ptr is end of line is reached, move to next line |
| 5255 | // or previous line based on direction |
| 5256 | if (**ptr != NUL) |
| 5257 | { |
| 5258 | if (!whole_line) |
| 5259 | { |
| 5260 | *ptr += current_pos.col; |
| 5261 | // Try to find a fuzzy match in the current line starting from current position |
| 5262 | found_new_match = fuzzy_match_str_in_line(ptr, pattern, len, ¤t_pos); |
| 5263 | if (found_new_match) |
| 5264 | { |
glepnir | 7cfe693 | 2024-09-15 20:06:28 +0200 | [diff] [blame] | 5265 | if (ctrl_x_mode_normal()) |
| 5266 | { |
| 5267 | match_word = vim_strnsave(*ptr, *len); |
| 5268 | if (STRCMP(match_word, pattern) == 0) |
| 5269 | { |
| 5270 | next_word_end = find_word_start(*ptr + *len); |
| 5271 | if (*next_word_end != NUL && *next_word_end != NL) |
| 5272 | { |
| 5273 | // Find end of the word. |
| 5274 | if (has_mbyte) |
| 5275 | while (*next_word_end != NUL) |
| 5276 | { |
| 5277 | int l = (*mb_ptr2len)(next_word_end); |
| 5278 | |
| 5279 | if (l < 2 && !vim_iswordc(*next_word_end)) |
| 5280 | break; |
| 5281 | next_word_end += l; |
| 5282 | } |
| 5283 | else |
| 5284 | next_word_end = find_word_end(next_word_end); |
| 5285 | } |
| 5286 | else if (looped_around) |
| 5287 | found_new_match = FALSE; |
| 5288 | |
| 5289 | *len = next_word_end - *ptr; |
| 5290 | current_pos.col = *len; |
| 5291 | } |
| 5292 | vim_free(match_word); |
| 5293 | } |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 5294 | *pos = current_pos; |
| 5295 | break; |
| 5296 | } |
glepnir | 0be03e1 | 2024-07-19 16:45:05 +0200 | [diff] [blame] | 5297 | else if (looped_around && current_pos.lnum == circly_end.lnum) |
| 5298 | break; |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 5299 | } |
| 5300 | else |
| 5301 | { |
| 5302 | if (fuzzy_match_str(*ptr, pattern) > 0) |
| 5303 | { |
| 5304 | found_new_match = TRUE; |
| 5305 | *pos = current_pos; |
Ken Takata | 073cb02 | 2024-07-28 17:08:15 +0200 | [diff] [blame] | 5306 | *len = (int)STRLEN(*ptr); |
glepnir | 8159fb1 | 2024-07-17 20:32:54 +0200 | [diff] [blame] | 5307 | break; |
| 5308 | } |
| 5309 | } |
| 5310 | } |
| 5311 | } |
| 5312 | |
| 5313 | // Move to the next line or previous line based on direction |
| 5314 | if (dir == FORWARD) |
| 5315 | { |
| 5316 | if (++current_pos.lnum > buf->b_ml.ml_line_count) |
| 5317 | { |
| 5318 | if (p_ws) |
| 5319 | { |
| 5320 | current_pos.lnum = 1; |
| 5321 | looped_around = TRUE; |
| 5322 | } |
| 5323 | else |
| 5324 | break; |
| 5325 | } |
| 5326 | } |
| 5327 | else |
| 5328 | { |
| 5329 | if (--current_pos.lnum < 1) |
| 5330 | { |
| 5331 | if (p_ws) |
| 5332 | { |
| 5333 | current_pos.lnum = buf->b_ml.ml_line_count; |
| 5334 | looped_around = TRUE; |
| 5335 | } |
| 5336 | else |
| 5337 | break; |
| 5338 | |
| 5339 | } |
| 5340 | } |
| 5341 | current_pos.col = 0; |
| 5342 | } while (TRUE); |
| 5343 | |
| 5344 | return found_new_match; |
| 5345 | } |
| 5346 | |
| 5347 | /* |
Bram Moolenaar | c6e0a5e | 2022-04-10 18:09:06 +0100 | [diff] [blame] | 5348 | * Free an array of fuzzy string matches "fuzmatch[count]". |
| 5349 | */ |
| 5350 | void |
| 5351 | fuzmatch_str_free(fuzmatch_str_T *fuzmatch, int count) |
| 5352 | { |
| 5353 | int i; |
| 5354 | |
| 5355 | if (fuzmatch == NULL) |
| 5356 | return; |
| 5357 | for (i = 0; i < count; ++i) |
| 5358 | vim_free(fuzmatch[i].str); |
| 5359 | vim_free(fuzmatch); |
| 5360 | } |
| 5361 | |
| 5362 | /* |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 5363 | * Copy a list of fuzzy matches into a string list after sorting the matches by |
| 5364 | * the fuzzy score. Frees the memory allocated for 'fuzmatch'. |
| 5365 | * Returns OK on success and FAIL on memory allocation failure. |
| 5366 | */ |
| 5367 | int |
| 5368 | fuzzymatches_to_strmatches( |
| 5369 | fuzmatch_str_T *fuzmatch, |
| 5370 | char_u ***matches, |
| 5371 | int count, |
| 5372 | int funcsort) |
| 5373 | { |
| 5374 | int i; |
| 5375 | |
| 5376 | if (count <= 0) |
| 5377 | return OK; |
| 5378 | |
| 5379 | *matches = ALLOC_MULT(char_u *, count); |
| 5380 | if (*matches == NULL) |
| 5381 | { |
Bram Moolenaar | c6e0a5e | 2022-04-10 18:09:06 +0100 | [diff] [blame] | 5382 | fuzmatch_str_free(fuzmatch, count); |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 5383 | return FAIL; |
| 5384 | } |
| 5385 | |
| 5386 | // Sort the list by the descending order of the match score |
| 5387 | if (funcsort) |
| 5388 | fuzzy_match_func_sort((void *)fuzmatch, (size_t)count); |
| 5389 | else |
| 5390 | fuzzy_match_str_sort((void *)fuzmatch, (size_t)count); |
| 5391 | |
| 5392 | for (i = 0; i < count; i++) |
| 5393 | (*matches)[i] = fuzmatch[i].str; |
| 5394 | vim_free(fuzmatch); |
| 5395 | |
| 5396 | return OK; |
| 5397 | } |