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 |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 20 | static void show_pat_in_path(char_u *, int, |
| 21 | 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 | |
| 35 | static void cmdline_search_stat(int dirc, pos_T *pos, pos_T *cursor_pos, int show_top_bot_msg, char_u *msgbuf, int recompute, int maxcount, long timeout); |
| 36 | static void update_search_stat(int dirc, pos_T *pos, pos_T *cursor_pos, searchstat_T *stat, int recompute, int maxcount, long timeout); |
| 37 | |
Bram Moolenaar | ea6561a | 2020-06-01 21:32:45 +0200 | [diff] [blame] | 38 | #define SEARCH_STAT_DEF_TIMEOUT 40L |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 39 | #define SEARCH_STAT_DEF_MAX_COUNT 99 |
| 40 | #define SEARCH_STAT_BUF_LEN 12 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 41 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 42 | /* |
| 43 | * This file contains various searching-related routines. These fall into |
| 44 | * three groups: |
| 45 | * 1. string searches (for /, ?, n, and N) |
| 46 | * 2. character searches within a single line (for f, F, t, T, etc) |
| 47 | * 3. "other" kinds of searches like the '%' command, and 'word' searches. |
| 48 | */ |
| 49 | |
| 50 | /* |
| 51 | * String searches |
| 52 | * |
| 53 | * The string search functions are divided into two levels: |
| 54 | * lowest: searchit(); uses an pos_T for starting position and found match. |
| 55 | * Highest: do_search(); uses curwin->w_cursor; calls searchit(). |
| 56 | * |
| 57 | * The last search pattern is remembered for repeating the same search. |
| 58 | * This pattern is shared between the :g, :s, ? and / commands. |
| 59 | * This is in search_regcomp(). |
| 60 | * |
| 61 | * The actual string matching is done using a heavily modified version of |
| 62 | * Henry Spencer's regular expression library. See regexp.c. |
| 63 | */ |
| 64 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 65 | /* |
| 66 | * Two search patterns are remembered: One for the :substitute command and |
| 67 | * one for other searches. last_idx points to the one that was used the last |
| 68 | * time. |
| 69 | */ |
Bram Moolenaar | c332816 | 2019-07-23 22:15:25 +0200 | [diff] [blame] | 70 | static spat_T spats[2] = |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 71 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 72 | {NULL, TRUE, FALSE, {'/', 0, 0, 0L}}, // last used search pat |
| 73 | {NULL, TRUE, FALSE, {'/', 0, 0, 0L}} // last used substitute pat |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 76 | static int last_idx = 0; // index in spats[] for RE_LAST |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 77 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 78 | static char_u lastc[2] = {NUL, NUL}; // last character searched for |
| 79 | static int lastcdir = FORWARD; // last direction of character search |
| 80 | static int last_t_cmd = TRUE; // last search t_cmd |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 81 | static char_u lastc_bytes[MB_MAXBYTES + 1]; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 82 | static int lastc_bytelen = 1; // >1 for multi-byte char |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 83 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 84 | // copy of spats[], for keeping the search patterns while executing autocmds |
Bram Moolenaar | c332816 | 2019-07-23 22:15:25 +0200 | [diff] [blame] | 85 | static spat_T saved_spats[2]; |
Bram Moolenaar | a2cff1d | 2021-10-15 12:51:29 +0100 | [diff] [blame] | 86 | static char_u *saved_mr_pattern = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 87 | # ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | ed8bc78 | 2018-12-01 21:08:21 +0100 | [diff] [blame] | 88 | static int saved_spats_last_idx = 0; |
| 89 | static int saved_spats_no_hlsearch = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 90 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 91 | |
Bram Moolenaar | a2cff1d | 2021-10-15 12:51:29 +0100 | [diff] [blame] | 92 | // allocated copy of pattern used by search_regcomp() |
| 93 | static char_u *mr_pattern = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 94 | |
| 95 | #ifdef FEAT_FIND_ID |
| 96 | /* |
| 97 | * Type used by find_pattern_in_path() to remember which included files have |
| 98 | * been searched already. |
| 99 | */ |
| 100 | typedef struct SearchedFile |
| 101 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 102 | FILE *fp; // File pointer |
| 103 | char_u *name; // Full name of file |
| 104 | linenr_T lnum; // Line we were up to in file |
| 105 | int matched; // Found a match in this file |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 106 | } SearchedFile; |
| 107 | #endif |
| 108 | |
| 109 | /* |
| 110 | * translate search pattern for vim_regcomp() |
| 111 | * |
| 112 | * pat_save == RE_SEARCH: save pat in spats[RE_SEARCH].pat (normal search cmd) |
| 113 | * pat_save == RE_SUBST: save pat in spats[RE_SUBST].pat (:substitute command) |
| 114 | * pat_save == RE_BOTH: save pat in both patterns (:global command) |
| 115 | * pat_use == RE_SEARCH: use previous search pattern if "pat" is NULL |
Bram Moolenaar | b8017e7 | 2007-05-10 18:59:07 +0000 | [diff] [blame] | 116 | * pat_use == RE_SUBST: use previous substitute pattern if "pat" is NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 117 | * pat_use == RE_LAST: use last used pattern if "pat" is NULL |
| 118 | * options & SEARCH_HIS: put search string in history |
| 119 | * options & SEARCH_KEEP: keep previous search pattern |
| 120 | * |
| 121 | * returns FAIL if failed, OK otherwise. |
| 122 | */ |
| 123 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 124 | search_regcomp( |
| 125 | char_u *pat, |
| 126 | int pat_save, |
| 127 | int pat_use, |
| 128 | int options, |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 129 | regmmatch_T *regmatch) // return: pattern and ignore-case flag |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 130 | { |
| 131 | int magic; |
| 132 | int i; |
| 133 | |
| 134 | rc_did_emsg = FALSE; |
Bram Moolenaar | f4e2099 | 2020-12-21 19:59:08 +0100 | [diff] [blame] | 135 | magic = magic_isset(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 136 | |
| 137 | /* |
| 138 | * If no pattern given, use a previously defined pattern. |
| 139 | */ |
| 140 | if (pat == NULL || *pat == NUL) |
| 141 | { |
| 142 | if (pat_use == RE_LAST) |
| 143 | i = last_idx; |
| 144 | else |
| 145 | i = pat_use; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 146 | if (spats[i].pat == NULL) // pattern was never defined |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 147 | { |
| 148 | if (pat_use == RE_SUBST) |
Bram Moolenaar | e29a27f | 2021-07-20 21:07:36 +0200 | [diff] [blame] | 149 | emsg(_(e_no_previous_substitute_regular_expression)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 150 | else |
Bram Moolenaar | e29a27f | 2021-07-20 21:07:36 +0200 | [diff] [blame] | 151 | emsg(_(e_no_previous_regular_expression)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 152 | rc_did_emsg = TRUE; |
| 153 | return FAIL; |
| 154 | } |
| 155 | pat = spats[i].pat; |
| 156 | magic = spats[i].magic; |
| 157 | no_smartcase = spats[i].no_scs; |
| 158 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 159 | else if (options & SEARCH_HIS) // put new pattern in history |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 160 | add_to_history(HIST_SEARCH, pat, TRUE, NUL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 161 | |
Bram Moolenaar | a2cff1d | 2021-10-15 12:51:29 +0100 | [diff] [blame] | 162 | vim_free(mr_pattern); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 163 | #ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 164 | if (curwin->w_p_rl && *curwin->w_p_rlc == 's') |
Bram Moolenaar | a2cff1d | 2021-10-15 12:51:29 +0100 | [diff] [blame] | 165 | mr_pattern = reverse_text(pat); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 166 | else |
| 167 | #endif |
Bram Moolenaar | a2cff1d | 2021-10-15 12:51:29 +0100 | [diff] [blame] | 168 | mr_pattern = vim_strsave(pat); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 169 | |
| 170 | /* |
| 171 | * Save the currently used pattern in the appropriate place, |
| 172 | * unless the pattern should not be remembered. |
| 173 | */ |
Bram Moolenaar | e100440 | 2020-10-24 20:49:43 +0200 | [diff] [blame] | 174 | if (!(options & SEARCH_KEEP) |
| 175 | && (cmdmod.cmod_flags & CMOD_KEEPPATTERNS) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 176 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 177 | // search or global command |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 178 | if (pat_save == RE_SEARCH || pat_save == RE_BOTH) |
| 179 | save_re_pat(RE_SEARCH, pat, magic); |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 180 | // substitute or global command |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 181 | if (pat_save == RE_SUBST || pat_save == RE_BOTH) |
| 182 | save_re_pat(RE_SUBST, pat, magic); |
| 183 | } |
| 184 | |
| 185 | regmatch->rmm_ic = ignorecase(pat); |
Bram Moolenaar | 3b56eb3 | 2005-07-11 22:40:32 +0000 | [diff] [blame] | 186 | regmatch->rmm_maxcol = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 187 | regmatch->regprog = vim_regcomp(pat, magic ? RE_MAGIC : 0); |
| 188 | if (regmatch->regprog == NULL) |
| 189 | return FAIL; |
| 190 | return OK; |
| 191 | } |
| 192 | |
| 193 | /* |
| 194 | * Get search pattern used by search_regcomp(). |
| 195 | */ |
| 196 | char_u * |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 197 | get_search_pat(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 198 | { |
| 199 | return mr_pattern; |
| 200 | } |
| 201 | |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 202 | #if defined(FEAT_RIGHTLEFT) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 203 | /* |
| 204 | * Reverse text into allocated memory. |
| 205 | * Returns the allocated string, NULL when out of memory. |
| 206 | */ |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 207 | char_u * |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 208 | reverse_text(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 209 | { |
| 210 | unsigned len; |
| 211 | unsigned s_i, rev_i; |
| 212 | char_u *rev; |
| 213 | |
| 214 | /* |
| 215 | * Reverse the pattern. |
| 216 | */ |
| 217 | len = (unsigned)STRLEN(s); |
| 218 | rev = alloc(len + 1); |
| 219 | if (rev != NULL) |
| 220 | { |
| 221 | rev_i = len; |
| 222 | for (s_i = 0; s_i < len; ++s_i) |
| 223 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 224 | if (has_mbyte) |
| 225 | { |
| 226 | int mb_len; |
| 227 | |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 228 | mb_len = (*mb_ptr2len)(s + s_i); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 229 | rev_i -= mb_len; |
| 230 | mch_memmove(rev + rev_i, s + s_i, mb_len); |
| 231 | s_i += mb_len - 1; |
| 232 | } |
| 233 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 234 | rev[--rev_i] = s[s_i]; |
| 235 | |
| 236 | } |
| 237 | rev[len] = NUL; |
| 238 | } |
| 239 | return rev; |
| 240 | } |
| 241 | #endif |
| 242 | |
Bram Moolenaar | cc2b9d5 | 2014-12-13 03:17:11 +0100 | [diff] [blame] | 243 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 244 | save_re_pat(int idx, char_u *pat, int magic) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 245 | { |
| 246 | if (spats[idx].pat != pat) |
| 247 | { |
| 248 | vim_free(spats[idx].pat); |
| 249 | spats[idx].pat = vim_strsave(pat); |
| 250 | spats[idx].magic = magic; |
| 251 | spats[idx].no_scs = no_smartcase; |
| 252 | last_idx = idx; |
| 253 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 254 | // If 'hlsearch' set and search pat changed: need redraw. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 255 | if (p_hls) |
Bram Moolenaar | a4d158b | 2022-08-14 14:17:45 +0100 | [diff] [blame] | 256 | redraw_all_later(UPD_SOME_VALID); |
Bram Moolenaar | 451fc7b | 2018-04-27 22:53:07 +0200 | [diff] [blame] | 257 | set_no_hlsearch(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 258 | #endif |
| 259 | } |
| 260 | } |
| 261 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 262 | /* |
| 263 | * Save the search patterns, so they can be restored later. |
| 264 | * Used before/after executing autocommands and user functions. |
| 265 | */ |
| 266 | static int save_level = 0; |
| 267 | |
| 268 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 269 | save_search_patterns(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 270 | { |
| 271 | if (save_level++ == 0) |
| 272 | { |
| 273 | saved_spats[0] = spats[0]; |
| 274 | if (spats[0].pat != NULL) |
| 275 | saved_spats[0].pat = vim_strsave(spats[0].pat); |
| 276 | saved_spats[1] = spats[1]; |
| 277 | if (spats[1].pat != NULL) |
| 278 | saved_spats[1].pat = vim_strsave(spats[1].pat); |
Bram Moolenaar | a2cff1d | 2021-10-15 12:51:29 +0100 | [diff] [blame] | 279 | if (mr_pattern == NULL) |
| 280 | saved_mr_pattern = NULL; |
| 281 | else |
| 282 | saved_mr_pattern = vim_strsave(mr_pattern); |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 283 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | ed8bc78 | 2018-12-01 21:08:21 +0100 | [diff] [blame] | 284 | saved_spats_last_idx = last_idx; |
| 285 | saved_spats_no_hlsearch = no_hlsearch; |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 286 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
| 290 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 291 | restore_search_patterns(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 292 | { |
| 293 | if (--save_level == 0) |
| 294 | { |
| 295 | vim_free(spats[0].pat); |
| 296 | spats[0] = saved_spats[0]; |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 297 | #if defined(FEAT_EVAL) |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 298 | set_vv_searchforward(); |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 299 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 300 | vim_free(spats[1].pat); |
| 301 | spats[1] = saved_spats[1]; |
Bram Moolenaar | a2cff1d | 2021-10-15 12:51:29 +0100 | [diff] [blame] | 302 | vim_free(mr_pattern); |
| 303 | mr_pattern = saved_mr_pattern; |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 304 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | ed8bc78 | 2018-12-01 21:08:21 +0100 | [diff] [blame] | 305 | last_idx = saved_spats_last_idx; |
| 306 | set_no_hlsearch(saved_spats_no_hlsearch); |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 307 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 308 | } |
| 309 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 310 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 311 | #if defined(EXITFREE) || defined(PROTO) |
| 312 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 313 | free_search_patterns(void) |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 314 | { |
| 315 | vim_free(spats[0].pat); |
| 316 | vim_free(spats[1].pat); |
Bram Moolenaar | a2cff1d | 2021-10-15 12:51:29 +0100 | [diff] [blame] | 317 | VIM_CLEAR(mr_pattern); |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 318 | } |
| 319 | #endif |
| 320 | |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 321 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | ed8bc78 | 2018-12-01 21:08:21 +0100 | [diff] [blame] | 322 | // copy of spats[RE_SEARCH], for keeping the search patterns while incremental |
| 323 | // searching |
Bram Moolenaar | c332816 | 2019-07-23 22:15:25 +0200 | [diff] [blame] | 324 | static spat_T saved_last_search_spat; |
Bram Moolenaar | ed8bc78 | 2018-12-01 21:08:21 +0100 | [diff] [blame] | 325 | static int did_save_last_search_spat = 0; |
| 326 | static int saved_last_idx = 0; |
| 327 | static int saved_no_hlsearch = 0; |
Christian Brabandt | 6dd7424 | 2022-02-14 12:44:32 +0000 | [diff] [blame] | 328 | static int saved_search_match_endcol; |
| 329 | static int saved_search_match_lines; |
Bram Moolenaar | ed8bc78 | 2018-12-01 21:08:21 +0100 | [diff] [blame] | 330 | |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 331 | /* |
| 332 | * Save and restore the search pattern for incremental highlight search |
| 333 | * feature. |
| 334 | * |
Bram Moolenaar | c4568ab | 2018-11-16 16:21:05 +0100 | [diff] [blame] | 335 | * It's similar to but different from save_search_patterns() and |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 336 | * restore_search_patterns(), because the search pattern must be restored when |
Bram Moolenaar | c4568ab | 2018-11-16 16:21:05 +0100 | [diff] [blame] | 337 | * canceling incremental searching even if it's called inside user functions. |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 338 | */ |
| 339 | void |
| 340 | save_last_search_pattern(void) |
| 341 | { |
Bram Moolenaar | 442a853 | 2020-06-04 20:56:09 +0200 | [diff] [blame] | 342 | if (++did_save_last_search_spat != 1) |
| 343 | // nested call, nothing to do |
| 344 | return; |
Bram Moolenaar | 01a060d | 2018-11-30 21:57:55 +0100 | [diff] [blame] | 345 | |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 346 | saved_last_search_spat = spats[RE_SEARCH]; |
| 347 | if (spats[RE_SEARCH].pat != NULL) |
| 348 | saved_last_search_spat.pat = vim_strsave(spats[RE_SEARCH].pat); |
| 349 | saved_last_idx = last_idx; |
| 350 | saved_no_hlsearch = no_hlsearch; |
| 351 | } |
| 352 | |
| 353 | void |
| 354 | restore_last_search_pattern(void) |
| 355 | { |
Bram Moolenaar | 442a853 | 2020-06-04 20:56:09 +0200 | [diff] [blame] | 356 | if (--did_save_last_search_spat > 0) |
| 357 | // nested call, nothing to do |
| 358 | return; |
| 359 | if (did_save_last_search_spat != 0) |
Bram Moolenaar | 01a060d | 2018-11-30 21:57:55 +0100 | [diff] [blame] | 360 | { |
Bram Moolenaar | 442a853 | 2020-06-04 20:56:09 +0200 | [diff] [blame] | 361 | 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] | 362 | return; |
| 363 | } |
Bram Moolenaar | 01a060d | 2018-11-30 21:57:55 +0100 | [diff] [blame] | 364 | |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 365 | vim_free(spats[RE_SEARCH].pat); |
| 366 | spats[RE_SEARCH] = saved_last_search_spat; |
Bram Moolenaar | 01a060d | 2018-11-30 21:57:55 +0100 | [diff] [blame] | 367 | saved_last_search_spat.pat = NULL; |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 368 | # if defined(FEAT_EVAL) |
| 369 | set_vv_searchforward(); |
| 370 | # endif |
| 371 | last_idx = saved_last_idx; |
Bram Moolenaar | 451fc7b | 2018-04-27 22:53:07 +0200 | [diff] [blame] | 372 | set_no_hlsearch(saved_no_hlsearch); |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 373 | } |
Bram Moolenaar | d048009 | 2017-11-16 22:20:39 +0100 | [diff] [blame] | 374 | |
Christian Brabandt | 6dd7424 | 2022-02-14 12:44:32 +0000 | [diff] [blame] | 375 | /* |
| 376 | * Save and restore the incsearch highlighting variables. |
| 377 | * This is required so that calling searchcount() at does not invalidate the |
| 378 | * incsearch highlighting. |
| 379 | */ |
| 380 | static void |
| 381 | save_incsearch_state(void) |
| 382 | { |
| 383 | saved_search_match_endcol = search_match_endcol; |
| 384 | saved_search_match_lines = search_match_lines; |
| 385 | } |
| 386 | |
| 387 | static void |
| 388 | restore_incsearch_state(void) |
| 389 | { |
| 390 | search_match_endcol = saved_search_match_endcol; |
| 391 | search_match_lines = saved_search_match_lines; |
| 392 | } |
| 393 | |
Bram Moolenaar | d048009 | 2017-11-16 22:20:39 +0100 | [diff] [blame] | 394 | char_u * |
| 395 | last_search_pattern(void) |
| 396 | { |
| 397 | return spats[RE_SEARCH].pat; |
| 398 | } |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 399 | #endif |
| 400 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 401 | /* |
| 402 | * Return TRUE when case should be ignored for search pattern "pat". |
| 403 | * Uses the 'ignorecase' and 'smartcase' options. |
| 404 | */ |
| 405 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 406 | ignorecase(char_u *pat) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 407 | { |
Bram Moolenaar | 66e29d7 | 2016-08-20 16:57:02 +0200 | [diff] [blame] | 408 | return ignorecase_opt(pat, p_ic, p_scs); |
| 409 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 410 | |
Bram Moolenaar | 66e29d7 | 2016-08-20 16:57:02 +0200 | [diff] [blame] | 411 | /* |
| 412 | * As ignorecase() put pass the "ic" and "scs" flags. |
| 413 | */ |
| 414 | int |
| 415 | ignorecase_opt(char_u *pat, int ic_in, int scs) |
| 416 | { |
| 417 | int ic = ic_in; |
| 418 | |
| 419 | if (ic && !no_smartcase && scs |
Bram Moolenaar | e2c453d | 2019-08-21 14:37:09 +0200 | [diff] [blame] | 420 | && !(ctrl_x_mode_not_default() && curbuf->b_p_inf)) |
Bram Moolenaar | a9dc375 | 2010-07-11 20:46:53 +0200 | [diff] [blame] | 421 | ic = !pat_has_uppercase(pat); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 422 | no_smartcase = FALSE; |
| 423 | |
| 424 | return ic; |
| 425 | } |
| 426 | |
Bram Moolenaar | a9dc375 | 2010-07-11 20:46:53 +0200 | [diff] [blame] | 427 | /* |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 428 | * Return TRUE if pattern "pat" has an uppercase character. |
Bram Moolenaar | a9dc375 | 2010-07-11 20:46:53 +0200 | [diff] [blame] | 429 | */ |
| 430 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 431 | pat_has_uppercase(char_u *pat) |
Bram Moolenaar | a9dc375 | 2010-07-11 20:46:53 +0200 | [diff] [blame] | 432 | { |
| 433 | char_u *p = pat; |
Christian Brabandt | 78ba933 | 2021-08-01 12:44:37 +0200 | [diff] [blame] | 434 | magic_T magic_val = MAGIC_ON; |
| 435 | |
| 436 | // get the magicness of the pattern |
| 437 | (void)skip_regexp_ex(pat, NUL, magic_isset(), NULL, NULL, &magic_val); |
Bram Moolenaar | a9dc375 | 2010-07-11 20:46:53 +0200 | [diff] [blame] | 438 | |
| 439 | while (*p != NUL) |
| 440 | { |
Bram Moolenaar | a9dc375 | 2010-07-11 20:46:53 +0200 | [diff] [blame] | 441 | int l; |
| 442 | |
| 443 | if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) |
| 444 | { |
| 445 | if (enc_utf8 && utf_isupper(utf_ptr2char(p))) |
| 446 | return TRUE; |
| 447 | p += l; |
| 448 | } |
Christian Brabandt | bc67e5a | 2021-08-05 15:24:59 +0200 | [diff] [blame] | 449 | else if (*p == '\\' && magic_val <= MAGIC_ON) |
Bram Moolenaar | a9dc375 | 2010-07-11 20:46:53 +0200 | [diff] [blame] | 450 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 451 | if (p[1] == '_' && p[2] != NUL) // skip "\_X" |
Bram Moolenaar | a9dc375 | 2010-07-11 20:46:53 +0200 | [diff] [blame] | 452 | p += 3; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 453 | else if (p[1] == '%' && p[2] != NUL) // skip "\%X" |
Bram Moolenaar | a9dc375 | 2010-07-11 20:46:53 +0200 | [diff] [blame] | 454 | p += 3; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 455 | else if (p[1] != NUL) // skip "\X" |
Bram Moolenaar | a9dc375 | 2010-07-11 20:46:53 +0200 | [diff] [blame] | 456 | p += 2; |
| 457 | else |
| 458 | p += 1; |
| 459 | } |
Christian Brabandt | 78ba933 | 2021-08-01 12:44:37 +0200 | [diff] [blame] | 460 | else if ((*p == '%' || *p == '_') && magic_val == MAGIC_ALL) |
| 461 | { |
| 462 | if (p[1] != NUL) // skip "_X" and %X |
| 463 | p += 2; |
Christian Brabandt | bc67e5a | 2021-08-05 15:24:59 +0200 | [diff] [blame] | 464 | else |
| 465 | p++; |
Christian Brabandt | 78ba933 | 2021-08-01 12:44:37 +0200 | [diff] [blame] | 466 | } |
Bram Moolenaar | a9dc375 | 2010-07-11 20:46:53 +0200 | [diff] [blame] | 467 | else if (MB_ISUPPER(*p)) |
| 468 | return TRUE; |
| 469 | else |
| 470 | ++p; |
| 471 | } |
| 472 | return FALSE; |
| 473 | } |
| 474 | |
Bram Moolenaar | 113e107 | 2019-01-20 15:30:40 +0100 | [diff] [blame] | 475 | #if defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 476 | char_u * |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 477 | last_csearch(void) |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 478 | { |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 479 | return lastc_bytes; |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 483 | last_csearch_forward(void) |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 484 | { |
| 485 | return lastcdir == FORWARD; |
| 486 | } |
| 487 | |
| 488 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 489 | last_csearch_until(void) |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 490 | { |
| 491 | return last_t_cmd == TRUE; |
| 492 | } |
| 493 | |
| 494 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 495 | set_last_csearch(int c, char_u *s UNUSED, int len UNUSED) |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 496 | { |
| 497 | *lastc = c; |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 498 | lastc_bytelen = len; |
| 499 | if (len) |
| 500 | memcpy(lastc_bytes, s, len); |
| 501 | else |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 502 | CLEAR_FIELD(lastc_bytes); |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 503 | } |
Bram Moolenaar | 113e107 | 2019-01-20 15:30:40 +0100 | [diff] [blame] | 504 | #endif |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 505 | |
| 506 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 507 | set_csearch_direction(int cdir) |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 508 | { |
| 509 | lastcdir = cdir; |
| 510 | } |
| 511 | |
| 512 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 513 | set_csearch_until(int t_cmd) |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 514 | { |
| 515 | last_t_cmd = t_cmd; |
| 516 | } |
| 517 | |
| 518 | char_u * |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 519 | last_search_pat(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 520 | { |
| 521 | return spats[last_idx].pat; |
| 522 | } |
| 523 | |
| 524 | /* |
| 525 | * Reset search direction to forward. For "gd" and "gD" commands. |
| 526 | */ |
| 527 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 528 | reset_search_dir(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 529 | { |
| 530 | spats[0].off.dir = '/'; |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 531 | #if defined(FEAT_EVAL) |
| 532 | set_vv_searchforward(); |
| 533 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | #if defined(FEAT_EVAL) || defined(FEAT_VIMINFO) |
| 537 | /* |
| 538 | * Set the last search pattern. For ":let @/ =" and viminfo. |
| 539 | * Also set the saved search pattern, so that this works in an autocommand. |
| 540 | */ |
| 541 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 542 | set_last_search_pat( |
| 543 | char_u *s, |
| 544 | int idx, |
| 545 | int magic, |
| 546 | int setlast) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 547 | { |
| 548 | vim_free(spats[idx].pat); |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 549 | // An empty string means that nothing should be matched. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 550 | if (*s == NUL) |
| 551 | spats[idx].pat = NULL; |
| 552 | else |
| 553 | spats[idx].pat = vim_strsave(s); |
| 554 | spats[idx].magic = magic; |
| 555 | spats[idx].no_scs = FALSE; |
| 556 | spats[idx].off.dir = '/'; |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 557 | #if defined(FEAT_EVAL) |
| 558 | set_vv_searchforward(); |
| 559 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 560 | spats[idx].off.line = FALSE; |
| 561 | spats[idx].off.end = FALSE; |
| 562 | spats[idx].off.off = 0; |
| 563 | if (setlast) |
| 564 | last_idx = idx; |
| 565 | if (save_level) |
| 566 | { |
| 567 | vim_free(saved_spats[idx].pat); |
| 568 | saved_spats[idx] = spats[0]; |
| 569 | if (spats[idx].pat == NULL) |
| 570 | saved_spats[idx].pat = NULL; |
| 571 | else |
| 572 | saved_spats[idx].pat = vim_strsave(spats[idx].pat); |
Bram Moolenaar | 975880b | 2019-03-03 14:42:11 +0100 | [diff] [blame] | 573 | # ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | ed8bc78 | 2018-12-01 21:08:21 +0100 | [diff] [blame] | 574 | saved_spats_last_idx = last_idx; |
Bram Moolenaar | 975880b | 2019-03-03 14:42:11 +0100 | [diff] [blame] | 575 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 576 | } |
| 577 | # ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 578 | // If 'hlsearch' set and search pat changed: need redraw. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 579 | if (p_hls && idx == last_idx && !no_hlsearch) |
Bram Moolenaar | a4d158b | 2022-08-14 14:17:45 +0100 | [diff] [blame] | 580 | redraw_all_later(UPD_SOME_VALID); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 581 | # endif |
| 582 | } |
| 583 | #endif |
| 584 | |
| 585 | #ifdef FEAT_SEARCH_EXTRA |
| 586 | /* |
| 587 | * Get a regexp program for the last used search pattern. |
| 588 | * This is used for highlighting all matches in a window. |
| 589 | * Values returned in regmatch->regprog and regmatch->rmm_ic. |
| 590 | */ |
| 591 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 592 | last_pat_prog(regmmatch_T *regmatch) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 593 | { |
| 594 | if (spats[last_idx].pat == NULL) |
| 595 | { |
| 596 | regmatch->regprog = NULL; |
| 597 | return; |
| 598 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 599 | ++emsg_off; // So it doesn't beep if bad expr |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 600 | (void)search_regcomp((char_u *)"", 0, last_idx, SEARCH_KEEP, regmatch); |
| 601 | --emsg_off; |
| 602 | } |
| 603 | #endif |
| 604 | |
| 605 | /* |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 606 | * Lowest level search function. |
Bram Moolenaar | 5d24a22 | 2018-12-23 19:10:09 +0100 | [diff] [blame] | 607 | * Search for 'count'th occurrence of pattern "pat" in direction "dir". |
| 608 | * Start at position "pos" and return the found position in "pos". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 609 | * |
| 610 | * if (options & SEARCH_MSG) == 0 don't give any messages |
| 611 | * if (options & SEARCH_MSG) == SEARCH_NFMSG don't give 'notfound' messages |
| 612 | * if (options & SEARCH_MSG) == SEARCH_MSG give all messages |
| 613 | * if (options & SEARCH_HIS) put search pattern in history |
| 614 | * if (options & SEARCH_END) return position at end of match |
| 615 | * if (options & SEARCH_START) accept match at pos itself |
| 616 | * if (options & SEARCH_KEEP) keep previous search pattern |
| 617 | * if (options & SEARCH_FOLD) match only once in a closed fold |
| 618 | * if (options & SEARCH_PEEK) check for typed char, cancel search |
Bram Moolenaar | ad4d8a1 | 2015-12-28 19:20:36 +0100 | [diff] [blame] | 619 | * if (options & SEARCH_COL) start at pos->col instead of zero |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 620 | * |
| 621 | * Return FAIL (zero) for failure, non-zero for success. |
| 622 | * When FEAT_EVAL is defined, returns the index of the first matching |
| 623 | * subpattern plus one; one if there was none. |
| 624 | */ |
| 625 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 626 | searchit( |
Bram Moolenaar | 92ea26b | 2019-10-18 20:53:34 +0200 | [diff] [blame] | 627 | win_T *win, // window to search in; can be NULL for a |
| 628 | // buffer without a window! |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 629 | buf_T *buf, |
| 630 | pos_T *pos, |
Bram Moolenaar | 5d24a22 | 2018-12-23 19:10:09 +0100 | [diff] [blame] | 631 | pos_T *end_pos, // set to end of the match, unless NULL |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 632 | int dir, |
| 633 | char_u *pat, |
| 634 | long count, |
| 635 | int options, |
Bram Moolenaar | 92ea26b | 2019-10-18 20:53:34 +0200 | [diff] [blame] | 636 | int pat_use, // which pattern to use when "pat" is empty |
| 637 | searchit_arg_T *extra_arg) // optional extra arguments, can be NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 638 | { |
| 639 | int found; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 640 | linenr_T lnum; // no init to shut up Apollo cc |
Bram Moolenaar | ad4d8a1 | 2015-12-28 19:20:36 +0100 | [diff] [blame] | 641 | colnr_T col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 642 | regmmatch_T regmatch; |
| 643 | char_u *ptr; |
| 644 | colnr_T matchcol; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 645 | lpos_T endpos; |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 646 | lpos_T matchpos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 647 | int loop; |
| 648 | pos_T start_pos; |
| 649 | int at_first_line; |
| 650 | int extra_col; |
Bram Moolenaar | 5f1e68b | 2015-07-10 14:43:35 +0200 | [diff] [blame] | 651 | int start_char_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 652 | int match_ok; |
| 653 | long nmatched; |
| 654 | int submatch = 0; |
Bram Moolenaar | a3dfccc | 2014-11-27 17:29:56 +0100 | [diff] [blame] | 655 | int first_match = TRUE; |
Bram Moolenaar | 5398955 | 2019-12-23 22:59:18 +0100 | [diff] [blame] | 656 | int called_emsg_before = called_emsg; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 657 | #ifdef FEAT_SEARCH_EXTRA |
| 658 | int break_loop = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 659 | #endif |
Bram Moolenaar | 92ea26b | 2019-10-18 20:53:34 +0200 | [diff] [blame] | 660 | linenr_T stop_lnum = 0; // stop after this line number when != 0 |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 661 | int unused_timeout_flag = FALSE; |
| 662 | int *timed_out = &unused_timeout_flag; // set when timed out. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 663 | |
| 664 | if (search_regcomp(pat, RE_SEARCH, pat_use, |
| 665 | (options & (SEARCH_HIS + SEARCH_KEEP)), ®match) == FAIL) |
| 666 | { |
| 667 | if ((options & SEARCH_MSG) && !rc_did_emsg) |
Bram Moolenaar | ac78dd4 | 2022-01-02 19:25:26 +0000 | [diff] [blame] | 668 | semsg(_(e_invalid_search_string_str), mr_pattern); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 669 | return FAIL; |
| 670 | } |
| 671 | |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 672 | if (extra_arg != NULL) |
| 673 | { |
| 674 | stop_lnum = extra_arg->sa_stop_lnum; |
| 675 | #ifdef FEAT_RELTIME |
| 676 | if (extra_arg->sa_tm > 0) |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 677 | init_regexp_timeout(extra_arg->sa_tm); |
Bram Moolenaar | 5ea38d1 | 2022-06-16 21:20:48 +0100 | [diff] [blame] | 678 | // Also set the pointer when sa_tm is zero, the caller may have set the |
| 679 | // timeout. |
| 680 | timed_out = &extra_arg->sa_timed_out; |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 681 | #endif |
| 682 | } |
| 683 | |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 684 | /* |
| 685 | * find the string |
| 686 | */ |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 687 | do // loop for count |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 688 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 689 | // When not accepting a match at the start position set "extra_col" to |
| 690 | // a non-zero value. Don't do that when starting at MAXCOL, since |
| 691 | // MAXCOL + 1 is zero. |
Bram Moolenaar | 5f1e68b | 2015-07-10 14:43:35 +0200 | [diff] [blame] | 692 | if (pos->col == MAXCOL) |
| 693 | start_char_len = 0; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 694 | // 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] | 695 | else if (has_mbyte |
| 696 | && pos->lnum >= 1 && pos->lnum <= buf->b_ml.ml_line_count |
| 697 | && pos->col < MAXCOL - 2) |
Bram Moolenaar | a3dfccc | 2014-11-27 17:29:56 +0100 | [diff] [blame] | 698 | { |
Bram Moolenaar | 82846a0 | 2018-02-09 18:09:54 +0100 | [diff] [blame] | 699 | ptr = ml_get_buf(buf, pos->lnum, FALSE); |
Bram Moolenaar | 8846ac5 | 2018-02-09 19:24:01 +0100 | [diff] [blame] | 700 | if ((int)STRLEN(ptr) <= pos->col) |
Bram Moolenaar | 5f1e68b | 2015-07-10 14:43:35 +0200 | [diff] [blame] | 701 | start_char_len = 1; |
Bram Moolenaar | a3dfccc | 2014-11-27 17:29:56 +0100 | [diff] [blame] | 702 | else |
Bram Moolenaar | 82846a0 | 2018-02-09 18:09:54 +0100 | [diff] [blame] | 703 | start_char_len = (*mb_ptr2len)(ptr + pos->col); |
Bram Moolenaar | a3dfccc | 2014-11-27 17:29:56 +0100 | [diff] [blame] | 704 | } |
Bram Moolenaar | a3dfccc | 2014-11-27 17:29:56 +0100 | [diff] [blame] | 705 | else |
Bram Moolenaar | 5f1e68b | 2015-07-10 14:43:35 +0200 | [diff] [blame] | 706 | start_char_len = 1; |
| 707 | if (dir == FORWARD) |
| 708 | { |
| 709 | if (options & SEARCH_START) |
| 710 | extra_col = 0; |
| 711 | else |
| 712 | extra_col = start_char_len; |
| 713 | } |
| 714 | else |
| 715 | { |
| 716 | if (options & SEARCH_START) |
| 717 | extra_col = start_char_len; |
| 718 | else |
| 719 | extra_col = 0; |
| 720 | } |
Bram Moolenaar | a3dfccc | 2014-11-27 17:29:56 +0100 | [diff] [blame] | 721 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 722 | start_pos = *pos; // remember start pos for detecting no match |
| 723 | found = 0; // default: not found |
| 724 | at_first_line = TRUE; // default: start in first line |
| 725 | if (pos->lnum == 0) // correct lnum for when starting in line 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 726 | { |
| 727 | pos->lnum = 1; |
| 728 | pos->col = 0; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 729 | at_first_line = FALSE; // not in first line now |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | /* |
| 733 | * Start searching in current line, unless searching backwards and |
| 734 | * we're in column 0. |
Bram Moolenaar | 7a42fa3 | 2007-07-10 11:28:55 +0000 | [diff] [blame] | 735 | * If we are searching backwards, in column 0, and not including the |
| 736 | * current position, gain some efficiency by skipping back a line. |
| 737 | * Otherwise begin the search in the current line. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 738 | */ |
Bram Moolenaar | 7a42fa3 | 2007-07-10 11:28:55 +0000 | [diff] [blame] | 739 | if (dir == BACKWARD && start_pos.col == 0 |
| 740 | && (options & SEARCH_START) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 741 | { |
| 742 | lnum = pos->lnum - 1; |
| 743 | at_first_line = FALSE; |
| 744 | } |
| 745 | else |
| 746 | lnum = pos->lnum; |
| 747 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 748 | for (loop = 0; loop <= 1; ++loop) // loop twice if 'wrapscan' set |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 749 | { |
| 750 | for ( ; lnum > 0 && lnum <= buf->b_ml.ml_line_count; |
| 751 | lnum += dir, at_first_line = FALSE) |
| 752 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 753 | // Stop after checking "stop_lnum", if it's set. |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 754 | if (stop_lnum != 0 && (dir == FORWARD |
| 755 | ? lnum > stop_lnum : lnum < stop_lnum)) |
| 756 | break; |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 757 | // Stop after passing the time limit. |
| 758 | if (*timed_out) |
Bram Moolenaar | 7692929 | 2008-01-06 19:07:36 +0000 | [diff] [blame] | 759 | break; |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 760 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 761 | /* |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 762 | * Look for a match somewhere in line "lnum". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 763 | */ |
Bram Moolenaar | ad4d8a1 | 2015-12-28 19:20:36 +0100 | [diff] [blame] | 764 | col = at_first_line && (options & SEARCH_COL) ? pos->col |
| 765 | : (colnr_T)0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 766 | nmatched = vim_regexec_multi(®match, win, buf, |
Yegappan Lakshmanan | 04c4c57 | 2022-08-30 19:48:24 +0100 | [diff] [blame] | 767 | lnum, col, timed_out); |
Bram Moolenaar | 795aaa1 | 2020-10-02 20:36:01 +0200 | [diff] [blame] | 768 | // vim_regexec_multi() may clear "regprog" |
| 769 | if (regmatch.regprog == NULL) |
| 770 | break; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 771 | // Abort searching on an error (e.g., out of stack). |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 772 | if (called_emsg > called_emsg_before || *timed_out) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 773 | break; |
| 774 | if (nmatched > 0) |
| 775 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 776 | // match may actually be in another line when using \zs |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 777 | matchpos = regmatch.startpos[0]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 778 | endpos = regmatch.endpos[0]; |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 779 | #ifdef FEAT_EVAL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 780 | submatch = first_submatch(®match); |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 781 | #endif |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 782 | // "lnum" may be past end of buffer for "\n\zs". |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 783 | if (lnum + matchpos.lnum > buf->b_ml.ml_line_count) |
| 784 | ptr = (char_u *)""; |
| 785 | else |
| 786 | ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 787 | |
| 788 | /* |
| 789 | * Forward search in the first line: match should be after |
| 790 | * the start position. If not, continue at the end of the |
| 791 | * match (this is vi compatible) or on the next char. |
| 792 | */ |
| 793 | if (dir == FORWARD && at_first_line) |
| 794 | { |
| 795 | match_ok = TRUE; |
| 796 | /* |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 797 | * When the match starts in a next line it's certainly |
| 798 | * past the start position. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 799 | * When match lands on a NUL the cursor will be put |
| 800 | * one back afterwards, compare with that position, |
| 801 | * otherwise "/$" will get stuck on end of line. |
| 802 | */ |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 803 | while (matchpos.lnum == 0 |
Bram Moolenaar | a3dfccc | 2014-11-27 17:29:56 +0100 | [diff] [blame] | 804 | && ((options & SEARCH_END) && first_match |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 805 | ? (nmatched == 1 |
| 806 | && (int)endpos.col - 1 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 807 | < (int)start_pos.col + extra_col) |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 808 | : ((int)matchpos.col |
| 809 | - (ptr[matchpos.col] == NUL) |
| 810 | < (int)start_pos.col + extra_col))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 811 | { |
| 812 | /* |
| 813 | * If vi-compatible searching, continue at the end |
| 814 | * of the match, otherwise continue one position |
| 815 | * forward. |
| 816 | */ |
| 817 | if (vim_strchr(p_cpo, CPO_SEARCH) != NULL) |
| 818 | { |
| 819 | if (nmatched > 1) |
| 820 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 821 | // end is in next line, thus no match in |
| 822 | // this line |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 823 | match_ok = FALSE; |
| 824 | break; |
| 825 | } |
| 826 | matchcol = endpos.col; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 827 | // for empty match: advance one char |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 828 | if (matchcol == matchpos.col |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 829 | && ptr[matchcol] != NUL) |
| 830 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 831 | if (has_mbyte) |
| 832 | matchcol += |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 833 | (*mb_ptr2len)(ptr + matchcol); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 834 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 835 | ++matchcol; |
| 836 | } |
| 837 | } |
| 838 | else |
| 839 | { |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 840 | matchcol = matchpos.col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 841 | if (ptr[matchcol] != NUL) |
| 842 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 843 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 844 | matchcol += (*mb_ptr2len)(ptr |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 845 | + matchcol); |
| 846 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 847 | ++matchcol; |
| 848 | } |
| 849 | } |
Bram Moolenaar | 7bcb30e | 2013-04-03 21:14:29 +0200 | [diff] [blame] | 850 | if (matchcol == 0 && (options & SEARCH_START)) |
Bram Moolenaar | db333a5 | 2013-03-19 15:27:48 +0100 | [diff] [blame] | 851 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 852 | if (ptr[matchcol] == NUL |
| 853 | || (nmatched = vim_regexec_multi(®match, |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 854 | win, buf, lnum + matchpos.lnum, |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 855 | matchcol, timed_out)) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 856 | { |
| 857 | match_ok = FALSE; |
| 858 | break; |
| 859 | } |
Bram Moolenaar | 795aaa1 | 2020-10-02 20:36:01 +0200 | [diff] [blame] | 860 | // vim_regexec_multi() may clear "regprog" |
| 861 | if (regmatch.regprog == NULL) |
| 862 | break; |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 863 | matchpos = regmatch.startpos[0]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 864 | endpos = regmatch.endpos[0]; |
| 865 | # ifdef FEAT_EVAL |
| 866 | submatch = first_submatch(®match); |
| 867 | # endif |
| 868 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 869 | // Need to get the line pointer again, a |
| 870 | // multi-line search may have made it invalid. |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 871 | ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 872 | } |
| 873 | if (!match_ok) |
| 874 | continue; |
| 875 | } |
| 876 | if (dir == BACKWARD) |
| 877 | { |
| 878 | /* |
| 879 | * Now, if there are multiple matches on this line, |
| 880 | * we have to get the last one. Or the last one before |
| 881 | * the cursor, if we're on that line. |
| 882 | * When putting the new cursor at the end, compare |
| 883 | * relative to the end of the match. |
| 884 | */ |
| 885 | match_ok = FALSE; |
| 886 | for (;;) |
| 887 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 888 | // Remember a position that is before the start |
| 889 | // position, we use it if it's the last match in |
| 890 | // the line. Always accept a position after |
| 891 | // wrapping around. |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 892 | if (loop |
| 893 | || ((options & SEARCH_END) |
| 894 | ? (lnum + regmatch.endpos[0].lnum |
| 895 | < start_pos.lnum |
| 896 | || (lnum + regmatch.endpos[0].lnum |
| 897 | == start_pos.lnum |
| 898 | && (int)regmatch.endpos[0].col - 1 |
Bram Moolenaar | 5f1e68b | 2015-07-10 14:43:35 +0200 | [diff] [blame] | 899 | < (int)start_pos.col |
| 900 | + extra_col)) |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 901 | : (lnum + regmatch.startpos[0].lnum |
| 902 | < start_pos.lnum |
| 903 | || (lnum + regmatch.startpos[0].lnum |
| 904 | == start_pos.lnum |
| 905 | && (int)regmatch.startpos[0].col |
Bram Moolenaar | 5f1e68b | 2015-07-10 14:43:35 +0200 | [diff] [blame] | 906 | < (int)start_pos.col |
| 907 | + extra_col)))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 908 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 909 | match_ok = TRUE; |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 910 | matchpos = regmatch.startpos[0]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 911 | endpos = regmatch.endpos[0]; |
| 912 | # ifdef FEAT_EVAL |
| 913 | submatch = first_submatch(®match); |
| 914 | # endif |
| 915 | } |
| 916 | else |
| 917 | break; |
| 918 | |
| 919 | /* |
| 920 | * We found a valid match, now check if there is |
| 921 | * another one after it. |
| 922 | * If vi-compatible searching, continue at the end |
| 923 | * of the match, otherwise continue one position |
| 924 | * forward. |
| 925 | */ |
| 926 | if (vim_strchr(p_cpo, CPO_SEARCH) != NULL) |
| 927 | { |
| 928 | if (nmatched > 1) |
| 929 | break; |
| 930 | matchcol = endpos.col; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 931 | // for empty match: advance one char |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 932 | if (matchcol == matchpos.col |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 933 | && ptr[matchcol] != NUL) |
| 934 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 935 | if (has_mbyte) |
| 936 | matchcol += |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 937 | (*mb_ptr2len)(ptr + matchcol); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 938 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 939 | ++matchcol; |
| 940 | } |
| 941 | } |
| 942 | else |
| 943 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 944 | // Stop when the match is in a next line. |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 945 | if (matchpos.lnum > 0) |
| 946 | break; |
| 947 | matchcol = matchpos.col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 948 | if (ptr[matchcol] != NUL) |
| 949 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 950 | if (has_mbyte) |
| 951 | matchcol += |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 952 | (*mb_ptr2len)(ptr + matchcol); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 953 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 954 | ++matchcol; |
| 955 | } |
| 956 | } |
| 957 | if (ptr[matchcol] == NUL |
| 958 | || (nmatched = vim_regexec_multi(®match, |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 959 | win, buf, lnum + matchpos.lnum, |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 960 | matchcol, timed_out)) == 0) |
Bram Moolenaar | 9d32276 | 2018-02-09 16:04:25 +0100 | [diff] [blame] | 961 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 962 | // If the search timed out, we did find a match |
| 963 | // but it might be the wrong one, so that's not |
| 964 | // OK. |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 965 | if (*timed_out) |
Bram Moolenaar | 9d32276 | 2018-02-09 16:04:25 +0100 | [diff] [blame] | 966 | match_ok = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 967 | break; |
Bram Moolenaar | 9d32276 | 2018-02-09 16:04:25 +0100 | [diff] [blame] | 968 | } |
Bram Moolenaar | 795aaa1 | 2020-10-02 20:36:01 +0200 | [diff] [blame] | 969 | // vim_regexec_multi() may clear "regprog" |
| 970 | if (regmatch.regprog == NULL) |
| 971 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 972 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 973 | // Need to get the line pointer again, a |
| 974 | // multi-line search may have made it invalid. |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 975 | ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | /* |
| 979 | * If there is only a match after the cursor, skip |
| 980 | * this match. |
| 981 | */ |
| 982 | if (!match_ok) |
| 983 | continue; |
| 984 | } |
| 985 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 986 | // With the SEARCH_END option move to the last character |
| 987 | // of the match. Don't do it for an empty match, end |
| 988 | // should be same as start then. |
Bram Moolenaar | 7bcb30e | 2013-04-03 21:14:29 +0200 | [diff] [blame] | 989 | if ((options & SEARCH_END) && !(options & SEARCH_NOOF) |
Bram Moolenaar | 5bcbd53 | 2008-02-20 12:43:01 +0000 | [diff] [blame] | 990 | && !(matchpos.lnum == endpos.lnum |
| 991 | && matchpos.col == endpos.col)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 992 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 993 | // For a match in the first column, set the position |
| 994 | // on the NUL in the previous line. |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 995 | pos->lnum = lnum + endpos.lnum; |
Bram Moolenaar | 5bcbd53 | 2008-02-20 12:43:01 +0000 | [diff] [blame] | 996 | pos->col = endpos.col; |
| 997 | if (endpos.col == 0) |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 998 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 999 | if (pos->lnum > 1) // just in case |
Bram Moolenaar | 5bcbd53 | 2008-02-20 12:43:01 +0000 | [diff] [blame] | 1000 | { |
| 1001 | --pos->lnum; |
| 1002 | pos->col = (colnr_T)STRLEN(ml_get_buf(buf, |
| 1003 | pos->lnum, FALSE)); |
| 1004 | } |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 1005 | } |
Bram Moolenaar | 5bcbd53 | 2008-02-20 12:43:01 +0000 | [diff] [blame] | 1006 | else |
| 1007 | { |
| 1008 | --pos->col; |
Bram Moolenaar | 5bcbd53 | 2008-02-20 12:43:01 +0000 | [diff] [blame] | 1009 | if (has_mbyte |
| 1010 | && pos->lnum <= buf->b_ml.ml_line_count) |
| 1011 | { |
| 1012 | ptr = ml_get_buf(buf, pos->lnum, FALSE); |
| 1013 | pos->col -= (*mb_head_off)(ptr, ptr + pos->col); |
| 1014 | } |
Bram Moolenaar | 5bcbd53 | 2008-02-20 12:43:01 +0000 | [diff] [blame] | 1015 | } |
Bram Moolenaar | 5d24a22 | 2018-12-23 19:10:09 +0100 | [diff] [blame] | 1016 | if (end_pos != NULL) |
| 1017 | { |
| 1018 | end_pos->lnum = lnum + matchpos.lnum; |
| 1019 | end_pos->col = matchpos.col; |
| 1020 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1021 | } |
| 1022 | else |
| 1023 | { |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 1024 | pos->lnum = lnum + matchpos.lnum; |
| 1025 | pos->col = matchpos.col; |
Bram Moolenaar | 5d24a22 | 2018-12-23 19:10:09 +0100 | [diff] [blame] | 1026 | if (end_pos != NULL) |
| 1027 | { |
| 1028 | end_pos->lnum = lnum + endpos.lnum; |
| 1029 | end_pos->col = endpos.col; |
| 1030 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1031 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1032 | pos->coladd = 0; |
Bram Moolenaar | 5d24a22 | 2018-12-23 19:10:09 +0100 | [diff] [blame] | 1033 | if (end_pos != NULL) |
| 1034 | end_pos->coladd = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1035 | found = 1; |
Bram Moolenaar | a3dfccc | 2014-11-27 17:29:56 +0100 | [diff] [blame] | 1036 | first_match = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1037 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1038 | // Set variables used for 'incsearch' highlighting. |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 1039 | search_match_lines = endpos.lnum - matchpos.lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1040 | search_match_endcol = endpos.col; |
| 1041 | break; |
| 1042 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1043 | line_breakcheck(); // stop if ctrl-C typed |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1044 | if (got_int) |
| 1045 | break; |
| 1046 | |
| 1047 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1048 | // Cancel searching if a character was typed. Used for |
| 1049 | // 'incsearch'. Don't check too often, that would slowdown |
| 1050 | // searching too much. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1051 | if ((options & SEARCH_PEEK) |
| 1052 | && ((lnum - pos->lnum) & 0x3f) == 0 |
| 1053 | && char_avail()) |
| 1054 | { |
| 1055 | break_loop = TRUE; |
| 1056 | break; |
| 1057 | } |
| 1058 | #endif |
| 1059 | |
| 1060 | if (loop && lnum == start_pos.lnum) |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1061 | break; // if second loop, stop where started |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1062 | } |
| 1063 | at_first_line = FALSE; |
| 1064 | |
Bram Moolenaar | 795aaa1 | 2020-10-02 20:36:01 +0200 | [diff] [blame] | 1065 | // vim_regexec_multi() may clear "regprog" |
| 1066 | if (regmatch.regprog == NULL) |
| 1067 | break; |
| 1068 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1069 | /* |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1070 | * Stop the search if wrapscan isn't set, "stop_lnum" is |
| 1071 | * specified, after an interrupt, after a match and after looping |
| 1072 | * twice. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1073 | */ |
Bram Moolenaar | 5398955 | 2019-12-23 22:59:18 +0100 | [diff] [blame] | 1074 | if (!p_ws || stop_lnum != 0 || got_int |
Yegappan Lakshmanan | 04c4c57 | 2022-08-30 19:48:24 +0100 | [diff] [blame] | 1075 | || called_emsg > called_emsg_before || *timed_out |
Bram Moolenaar | fbd0b0a | 2017-06-17 18:44:21 +0200 | [diff] [blame] | 1076 | #ifdef FEAT_SEARCH_EXTRA |
Yegappan Lakshmanan | 04c4c57 | 2022-08-30 19:48:24 +0100 | [diff] [blame] | 1077 | || break_loop |
Bram Moolenaar | fbd0b0a | 2017-06-17 18:44:21 +0200 | [diff] [blame] | 1078 | #endif |
Yegappan Lakshmanan | 04c4c57 | 2022-08-30 19:48:24 +0100 | [diff] [blame] | 1079 | || found || loop) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1080 | break; |
| 1081 | |
| 1082 | /* |
| 1083 | * If 'wrapscan' is set we continue at the other end of the file. |
| 1084 | * If 'shortmess' does not contain 's', we give a message. |
| 1085 | * This message is also remembered in keep_msg for when the screen |
| 1086 | * is redrawn. The keep_msg is cleared whenever another message is |
| 1087 | * written. |
| 1088 | */ |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1089 | if (dir == BACKWARD) // start second loop at the other end |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1090 | lnum = buf->b_ml.ml_line_count; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1091 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1092 | lnum = 1; |
Bram Moolenaar | 92d640f | 2005-09-05 22:11:52 +0000 | [diff] [blame] | 1093 | if (!shortmess(SHM_SEARCH) && (options & SEARCH_MSG)) |
| 1094 | give_warning((char_u *)_(dir == BACKWARD |
| 1095 | ? top_bot_msg : bot_top_msg), TRUE); |
Bram Moolenaar | 92ea26b | 2019-10-18 20:53:34 +0200 | [diff] [blame] | 1096 | if (extra_arg != NULL) |
| 1097 | extra_arg->sa_wrapped = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1098 | } |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 1099 | if (got_int || called_emsg > called_emsg_before || *timed_out |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 1100 | #ifdef FEAT_SEARCH_EXTRA |
| 1101 | || break_loop |
| 1102 | #endif |
| 1103 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1104 | break; |
| 1105 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1106 | while (--count > 0 && found); // stop after count matches or no match |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1107 | |
Bram Moolenaar | 5ea38d1 | 2022-06-16 21:20:48 +0100 | [diff] [blame] | 1108 | #ifdef FEAT_RELTIME |
| 1109 | if (extra_arg != NULL && extra_arg->sa_tm > 0) |
| 1110 | disable_regexp_timeout(); |
| 1111 | #endif |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 1112 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1113 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1114 | if (!found) // did not find it |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1115 | { |
| 1116 | if (got_int) |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 1117 | emsg(_(e_interrupted)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1118 | else if ((options & SEARCH_MSG) == SEARCH_MSG) |
| 1119 | { |
| 1120 | if (p_ws) |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 1121 | semsg(_(e_pattern_not_found_str), mr_pattern); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1122 | else if (lnum == 0) |
Bram Moolenaar | ac78dd4 | 2022-01-02 19:25:26 +0000 | [diff] [blame] | 1123 | semsg(_(e_search_hit_top_without_match_for_str), mr_pattern); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1124 | else |
Bram Moolenaar | ac78dd4 | 2022-01-02 19:25:26 +0000 | [diff] [blame] | 1125 | semsg(_(e_search_hit_bottom_without_match_for_str), mr_pattern); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1126 | } |
| 1127 | return FAIL; |
| 1128 | } |
| 1129 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1130 | // A pattern like "\n\zs" may go past the last line. |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 1131 | if (pos->lnum > buf->b_ml.ml_line_count) |
| 1132 | { |
| 1133 | pos->lnum = buf->b_ml.ml_line_count; |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1134 | pos->col = (int)STRLEN(ml_get_buf(buf, pos->lnum, FALSE)); |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 1135 | if (pos->col > 0) |
| 1136 | --pos->col; |
| 1137 | } |
| 1138 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1139 | return submatch + 1; |
| 1140 | } |
| 1141 | |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 1142 | #if defined(FEAT_EVAL) || defined(FEAT_PROTO) |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 1143 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 1144 | set_search_direction(int cdir) |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 1145 | { |
| 1146 | spats[0].off.dir = cdir; |
| 1147 | } |
| 1148 | |
| 1149 | static void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 1150 | set_vv_searchforward(void) |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 1151 | { |
| 1152 | set_vim_var_nr(VV_SEARCHFORWARD, (long)(spats[0].off.dir == '/')); |
| 1153 | } |
| 1154 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1155 | /* |
| 1156 | * Return the number of the first subpat that matched. |
Bram Moolenaar | ad4d8a1 | 2015-12-28 19:20:36 +0100 | [diff] [blame] | 1157 | * Return zero if none of them matched. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1158 | */ |
| 1159 | static int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 1160 | first_submatch(regmmatch_T *rp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1161 | { |
| 1162 | int submatch; |
| 1163 | |
| 1164 | for (submatch = 1; ; ++submatch) |
| 1165 | { |
| 1166 | if (rp->startpos[submatch].lnum >= 0) |
| 1167 | break; |
| 1168 | if (submatch == 9) |
| 1169 | { |
| 1170 | submatch = 0; |
| 1171 | break; |
| 1172 | } |
| 1173 | } |
| 1174 | return submatch; |
| 1175 | } |
| 1176 | #endif |
| 1177 | |
| 1178 | /* |
| 1179 | * Highest level string search function. |
Bram Moolenaar | b8017e7 | 2007-05-10 18:59:07 +0000 | [diff] [blame] | 1180 | * Search for the 'count'th occurrence of pattern 'pat' in direction 'dirc' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1181 | * If 'dirc' is 0: use previous dir. |
| 1182 | * If 'pat' is NULL or empty : use previous string. |
| 1183 | * If 'options & SEARCH_REV' : go in reverse of previous dir. |
| 1184 | * If 'options & SEARCH_ECHO': echo the search command and handle options |
| 1185 | * If 'options & SEARCH_MSG' : may give error message |
| 1186 | * If 'options & SEARCH_OPT' : interpret optional flags |
| 1187 | * If 'options & SEARCH_HIS' : put search pattern in history |
| 1188 | * If 'options & SEARCH_NOOF': don't add offset to position |
| 1189 | * If 'options & SEARCH_MARK': set previous context mark |
| 1190 | * If 'options & SEARCH_KEEP': keep previous search pattern |
| 1191 | * If 'options & SEARCH_START': accept match at curpos itself |
| 1192 | * If 'options & SEARCH_PEEK': check for typed char, cancel search |
| 1193 | * |
| 1194 | * Careful: If spats[0].off.line == TRUE and spats[0].off.off == 0 this |
| 1195 | * makes the movement linewise without moving the match position. |
| 1196 | * |
Bram Moolenaar | b6c2735 | 2015-03-05 19:57:49 +0100 | [diff] [blame] | 1197 | * 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] | 1198 | */ |
| 1199 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 1200 | do_search( |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1201 | oparg_T *oap, // can be NULL |
| 1202 | int dirc, // '/' or '?' |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 1203 | int search_delim, // the delimiter for the search, e.g. '%' in |
| 1204 | // s%regex%replacement% |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 1205 | char_u *pat, |
| 1206 | long count, |
| 1207 | int options, |
Bram Moolenaar | 92ea26b | 2019-10-18 20:53:34 +0200 | [diff] [blame] | 1208 | searchit_arg_T *sia) // optional arguments or NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1209 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1210 | pos_T pos; // position of the last match |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1211 | char_u *searchstr; |
Bram Moolenaar | c332816 | 2019-07-23 22:15:25 +0200 | [diff] [blame] | 1212 | soffset_T old_off; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1213 | int retval; // Return value |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1214 | char_u *p; |
| 1215 | long c; |
| 1216 | char_u *dircp; |
| 1217 | char_u *strcopy = NULL; |
| 1218 | char_u *ps; |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1219 | char_u *msgbuf = NULL; |
| 1220 | size_t len; |
Bram Moolenaar | 8f46e4c | 2019-05-24 22:08:15 +0200 | [diff] [blame] | 1221 | int has_offset = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1222 | |
| 1223 | /* |
| 1224 | * A line offset is not remembered, this is vi compatible. |
| 1225 | */ |
| 1226 | if (spats[0].off.line && vim_strchr(p_cpo, CPO_LINEOFF) != NULL) |
| 1227 | { |
| 1228 | spats[0].off.line = FALSE; |
| 1229 | spats[0].off.off = 0; |
| 1230 | } |
| 1231 | |
| 1232 | /* |
| 1233 | * Save the values for when (options & SEARCH_KEEP) is used. |
| 1234 | * (there is no "if ()" around this because gcc wants them initialized) |
| 1235 | */ |
| 1236 | old_off = spats[0].off; |
| 1237 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1238 | pos = curwin->w_cursor; // start searching at the cursor position |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1239 | |
| 1240 | /* |
| 1241 | * Find out the direction of the search. |
| 1242 | */ |
| 1243 | if (dirc == 0) |
| 1244 | dirc = spats[0].off.dir; |
| 1245 | else |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 1246 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1247 | spats[0].off.dir = dirc; |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 1248 | #if defined(FEAT_EVAL) |
| 1249 | set_vv_searchforward(); |
| 1250 | #endif |
| 1251 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1252 | if (options & SEARCH_REV) |
| 1253 | { |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 1254 | #ifdef MSWIN |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1255 | // There is a bug in the Visual C++ 2.2 compiler which means that |
| 1256 | // dirc always ends up being '/' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1257 | dirc = (dirc == '/') ? '?' : '/'; |
| 1258 | #else |
| 1259 | if (dirc == '/') |
| 1260 | dirc = '?'; |
| 1261 | else |
| 1262 | dirc = '/'; |
| 1263 | #endif |
| 1264 | } |
| 1265 | |
| 1266 | #ifdef FEAT_FOLDING |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1267 | // If the cursor is in a closed fold, don't find another match in the same |
| 1268 | // fold. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1269 | if (dirc == '/') |
| 1270 | { |
| 1271 | if (hasFolding(pos.lnum, NULL, &pos.lnum)) |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1272 | pos.col = MAXCOL - 2; // avoid overflow when adding 1 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1273 | } |
| 1274 | else |
| 1275 | { |
| 1276 | if (hasFolding(pos.lnum, &pos.lnum, NULL)) |
| 1277 | pos.col = 0; |
| 1278 | } |
| 1279 | #endif |
| 1280 | |
| 1281 | #ifdef FEAT_SEARCH_EXTRA |
| 1282 | /* |
| 1283 | * Turn 'hlsearch' highlighting back on. |
| 1284 | */ |
| 1285 | if (no_hlsearch && !(options & SEARCH_KEEP)) |
| 1286 | { |
Bram Moolenaar | a4d158b | 2022-08-14 14:17:45 +0100 | [diff] [blame] | 1287 | redraw_all_later(UPD_SOME_VALID); |
Bram Moolenaar | 451fc7b | 2018-04-27 22:53:07 +0200 | [diff] [blame] | 1288 | set_no_hlsearch(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1289 | } |
| 1290 | #endif |
| 1291 | |
| 1292 | /* |
| 1293 | * Repeat the search when pattern followed by ';', e.g. "/foo/;?bar". |
| 1294 | */ |
| 1295 | for (;;) |
| 1296 | { |
Bram Moolenaar | 92ea26b | 2019-10-18 20:53:34 +0200 | [diff] [blame] | 1297 | int show_top_bot_msg = FALSE; |
Bram Moolenaar | c7a10b3 | 2019-05-06 21:37:18 +0200 | [diff] [blame] | 1298 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1299 | searchstr = pat; |
| 1300 | dircp = NULL; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1301 | // use previous pattern |
Bram Moolenaar | c036e87 | 2020-02-21 21:30:52 +0100 | [diff] [blame] | 1302 | if (pat == NULL || *pat == NUL || *pat == search_delim) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1303 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1304 | if (spats[RE_SEARCH].pat == NULL) // no previous pattern |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1305 | { |
Bram Moolenaar | ea683da | 2016-09-09 21:41:34 +0200 | [diff] [blame] | 1306 | searchstr = spats[RE_SUBST].pat; |
| 1307 | if (searchstr == NULL) |
Bram Moolenaar | b4b0a08 | 2011-02-25 18:38:36 +0100 | [diff] [blame] | 1308 | { |
Bram Moolenaar | e29a27f | 2021-07-20 21:07:36 +0200 | [diff] [blame] | 1309 | emsg(_(e_no_previous_regular_expression)); |
Bram Moolenaar | b4b0a08 | 2011-02-25 18:38:36 +0100 | [diff] [blame] | 1310 | retval = 0; |
| 1311 | goto end_do_search; |
| 1312 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1313 | } |
Bram Moolenaar | b4b0a08 | 2011-02-25 18:38:36 +0100 | [diff] [blame] | 1314 | else |
| 1315 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1316 | // make search_regcomp() use spats[RE_SEARCH].pat |
Bram Moolenaar | b4b0a08 | 2011-02-25 18:38:36 +0100 | [diff] [blame] | 1317 | searchstr = (char_u *)""; |
| 1318 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1319 | } |
| 1320 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1321 | if (pat != NULL && *pat != NUL) // look for (new) offset |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1322 | { |
| 1323 | /* |
| 1324 | * Find end of regular expression. |
| 1325 | * If there is a matching '/' or '?', toss it. |
| 1326 | */ |
| 1327 | ps = strcopy; |
Bram Moolenaar | f4e2099 | 2020-12-21 19:59:08 +0100 | [diff] [blame] | 1328 | p = skip_regexp_ex(pat, search_delim, magic_isset(), |
Bram Moolenaar | d93a7fc | 2021-01-04 12:42:13 +0100 | [diff] [blame] | 1329 | &strcopy, NULL, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1330 | if (strcopy != ps) |
| 1331 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1332 | // made a copy of "pat" to change "\?" to "?" |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1333 | searchcmdlen += (int)(STRLEN(pat) - STRLEN(strcopy)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1334 | pat = strcopy; |
| 1335 | searchstr = strcopy; |
| 1336 | } |
Bram Moolenaar | c036e87 | 2020-02-21 21:30:52 +0100 | [diff] [blame] | 1337 | if (*p == search_delim) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1338 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1339 | dircp = p; // remember where we put the NUL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1340 | *p++ = NUL; |
| 1341 | } |
| 1342 | spats[0].off.line = FALSE; |
| 1343 | spats[0].off.end = FALSE; |
| 1344 | spats[0].off.off = 0; |
| 1345 | /* |
| 1346 | * Check for a line offset or a character offset. |
| 1347 | * For get_address (echo off) we don't check for a character |
| 1348 | * offset, because it is meaningless and the 's' could be a |
| 1349 | * substitute command. |
| 1350 | */ |
| 1351 | if (*p == '+' || *p == '-' || VIM_ISDIGIT(*p)) |
| 1352 | spats[0].off.line = TRUE; |
Dominique Pelle | 7765f5c | 2022-04-10 11:26:53 +0100 | [diff] [blame] | 1353 | else if ((options & SEARCH_OPT) |
| 1354 | && (*p == 'e' || *p == 's' || *p == 'b')) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1355 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1356 | if (*p == 'e') // end |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1357 | spats[0].off.end = SEARCH_END; |
| 1358 | ++p; |
| 1359 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1360 | if (VIM_ISDIGIT(*p) || *p == '+' || *p == '-') // got an offset |
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 | // 'nr' or '+nr' or '-nr' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1363 | if (VIM_ISDIGIT(*p) || VIM_ISDIGIT(*(p + 1))) |
| 1364 | spats[0].off.off = atol((char *)p); |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1365 | else if (*p == '-') // single '-' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1366 | spats[0].off.off = -1; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1367 | else // single '+' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1368 | spats[0].off.off = 1; |
| 1369 | ++p; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1370 | while (VIM_ISDIGIT(*p)) // skip number |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1371 | ++p; |
| 1372 | } |
| 1373 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1374 | // compute length of search command for get_address() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1375 | searchcmdlen += (int)(p - pat); |
| 1376 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1377 | pat = p; // put pat after search command |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
Dominique Pelle | 7765f5c | 2022-04-10 11:26:53 +0100 | [diff] [blame] | 1380 | if ((options & SEARCH_ECHO) && messaging() |
| 1381 | && !msg_silent |
| 1382 | && (!cmd_silent || !shortmess(SHM_SEARCHCOUNT))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1383 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1384 | char_u *trunc; |
Bram Moolenaar | 984f031 | 2019-05-24 13:11:47 +0200 | [diff] [blame] | 1385 | char_u off_buf[40]; |
Bram Moolenaar | d33a764 | 2019-05-24 17:56:14 +0200 | [diff] [blame] | 1386 | size_t off_len = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1387 | |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1388 | // Compute msg_row early. |
| 1389 | msg_start(); |
| 1390 | |
Bram Moolenaar | 984f031 | 2019-05-24 13:11:47 +0200 | [diff] [blame] | 1391 | // Get the offset, so we know how long it is. |
Bram Moolenaar | 359ad1a | 2019-09-02 21:44:59 +0200 | [diff] [blame] | 1392 | if (!cmd_silent && |
| 1393 | (spats[0].off.line || spats[0].off.end || spats[0].off.off)) |
Bram Moolenaar | 984f031 | 2019-05-24 13:11:47 +0200 | [diff] [blame] | 1394 | { |
| 1395 | p = off_buf; |
| 1396 | *p++ = dirc; |
| 1397 | if (spats[0].off.end) |
| 1398 | *p++ = 'e'; |
| 1399 | else if (!spats[0].off.line) |
| 1400 | *p++ = 's'; |
| 1401 | if (spats[0].off.off > 0 || spats[0].off.line) |
| 1402 | *p++ = '+'; |
| 1403 | *p = NUL; |
| 1404 | if (spats[0].off.off != 0 || spats[0].off.line) |
| 1405 | sprintf((char *)p, "%ld", spats[0].off.off); |
| 1406 | off_len = STRLEN(off_buf); |
| 1407 | } |
| 1408 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1409 | if (*searchstr == NUL) |
Bram Moolenaar | 2fb8f68 | 2018-12-01 13:14:45 +0100 | [diff] [blame] | 1410 | p = spats[0].pat; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1411 | else |
| 1412 | p = searchstr; |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1413 | |
Bram Moolenaar | 359ad1a | 2019-09-02 21:44:59 +0200 | [diff] [blame] | 1414 | if (!shortmess(SHM_SEARCHCOUNT) || cmd_silent) |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1415 | { |
| 1416 | // Reserve enough space for the search pattern + offset + |
Bram Moolenaar | 984f031 | 2019-05-24 13:11:47 +0200 | [diff] [blame] | 1417 | // search stat. Use all the space available, so that the |
| 1418 | // search state is right aligned. If there is not enough space |
| 1419 | // msg_strtrunc() will shorten in the middle. |
Bram Moolenaar | 19e8ac7 | 2019-09-03 22:23:38 +0200 | [diff] [blame] | 1420 | if (msg_scrolled != 0 && !cmd_silent) |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1421 | // Use all the columns. |
| 1422 | len = (int)(Rows - msg_row) * Columns - 1; |
| 1423 | else |
| 1424 | // Use up to 'showcmd' column. |
| 1425 | len = (int)(Rows - msg_row - 1) * Columns + sc_col - 1; |
Bram Moolenaar | 984f031 | 2019-05-24 13:11:47 +0200 | [diff] [blame] | 1426 | if (len < STRLEN(p) + off_len + SEARCH_STAT_BUF_LEN + 3) |
| 1427 | len = STRLEN(p) + off_len + SEARCH_STAT_BUF_LEN + 3; |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1428 | } |
| 1429 | else |
| 1430 | // Reserve enough space for the search pattern + offset. |
Bram Moolenaar | 984f031 | 2019-05-24 13:11:47 +0200 | [diff] [blame] | 1431 | len = STRLEN(p) + off_len + 3; |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1432 | |
Bram Moolenaar | 880e4d9 | 2020-04-11 21:31:28 +0200 | [diff] [blame] | 1433 | vim_free(msgbuf); |
Bram Moolenaar | 51e1438 | 2019-05-25 20:21:28 +0200 | [diff] [blame] | 1434 | msgbuf = alloc(len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1435 | if (msgbuf != NULL) |
| 1436 | { |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1437 | vim_memset(msgbuf, ' ', len); |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1438 | msgbuf[len - 1] = NUL; |
Bram Moolenaar | 359ad1a | 2019-09-02 21:44:59 +0200 | [diff] [blame] | 1439 | // do not fill the msgbuf buffer, if cmd_silent is set, leave it |
| 1440 | // empty for the search_stat feature. |
| 1441 | if (!cmd_silent) |
Bram Moolenaar | cafda4f | 2005-09-06 19:25:11 +0000 | [diff] [blame] | 1442 | { |
Bram Moolenaar | 359ad1a | 2019-09-02 21:44:59 +0200 | [diff] [blame] | 1443 | msgbuf[0] = dirc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1444 | |
Bram Moolenaar | 359ad1a | 2019-09-02 21:44:59 +0200 | [diff] [blame] | 1445 | if (enc_utf8 && utf_iscomposing(utf_ptr2char(p))) |
| 1446 | { |
| 1447 | // Use a space to draw the composing char on. |
| 1448 | msgbuf[1] = ' '; |
| 1449 | mch_memmove(msgbuf + 2, p, STRLEN(p)); |
| 1450 | } |
| 1451 | else |
| 1452 | mch_memmove(msgbuf + 1, p, STRLEN(p)); |
| 1453 | if (off_len > 0) |
| 1454 | mch_memmove(msgbuf + STRLEN(p) + 1, off_buf, off_len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1455 | |
Bram Moolenaar | 359ad1a | 2019-09-02 21:44:59 +0200 | [diff] [blame] | 1456 | trunc = msg_strtrunc(msgbuf, TRUE); |
| 1457 | if (trunc != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1458 | { |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1459 | vim_free(msgbuf); |
Bram Moolenaar | 359ad1a | 2019-09-02 21:44:59 +0200 | [diff] [blame] | 1460 | msgbuf = trunc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1461 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1462 | |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 1463 | #ifdef FEAT_RIGHTLEFT |
| 1464 | // The search pattern could be shown on the right in |
| 1465 | // rightleft mode, but the 'ruler' and 'showcmd' area use |
| 1466 | // it too, thus it would be blanked out again very soon. |
| 1467 | // Show it on the left, but do reverse the text. |
Bram Moolenaar | 359ad1a | 2019-09-02 21:44:59 +0200 | [diff] [blame] | 1468 | if (curwin->w_p_rl && *curwin->w_p_rlc == 's') |
| 1469 | { |
| 1470 | char_u *r; |
| 1471 | size_t pat_len; |
| 1472 | |
| 1473 | r = reverse_text(msgbuf); |
| 1474 | if (r != NULL) |
| 1475 | { |
| 1476 | vim_free(msgbuf); |
| 1477 | msgbuf = r; |
| 1478 | // move reversed text to beginning of buffer |
| 1479 | while (*r != NUL && *r == ' ') |
| 1480 | r++; |
| 1481 | pat_len = msgbuf + STRLEN(msgbuf) - r; |
| 1482 | mch_memmove(msgbuf, r, pat_len); |
| 1483 | // overwrite old text |
| 1484 | if ((size_t)(r - msgbuf) >= pat_len) |
| 1485 | vim_memset(r, ' ', pat_len); |
| 1486 | else |
| 1487 | vim_memset(msgbuf + pat_len, ' ', r - msgbuf); |
| 1488 | } |
| 1489 | } |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 1490 | #endif |
Bram Moolenaar | 359ad1a | 2019-09-02 21:44:59 +0200 | [diff] [blame] | 1491 | msg_outtrans(msgbuf); |
| 1492 | msg_clr_eos(); |
| 1493 | msg_check(); |
| 1494 | |
| 1495 | gotocmdline(FALSE); |
| 1496 | out_flush(); |
| 1497 | msg_nowait = TRUE; // don't wait for this message |
| 1498 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1499 | } |
| 1500 | } |
| 1501 | |
| 1502 | /* |
| 1503 | * If there is a character offset, subtract it from the current |
| 1504 | * 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] | 1505 | * Skip this if pos.col is near MAXCOL (closed fold). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1506 | * This is not done for a line offset, because then we would not be vi |
| 1507 | * compatible. |
| 1508 | */ |
Bram Moolenaar | ed20346 | 2004-06-16 11:19:22 +0000 | [diff] [blame] | 1509 | 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] | 1510 | { |
| 1511 | if (spats[0].off.off > 0) |
| 1512 | { |
| 1513 | for (c = spats[0].off.off; c; --c) |
| 1514 | if (decl(&pos) == -1) |
| 1515 | break; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1516 | if (c) // at start of buffer |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1517 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1518 | pos.lnum = 0; // allow lnum == 0 here |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1519 | pos.col = MAXCOL; |
| 1520 | } |
| 1521 | } |
| 1522 | else |
| 1523 | { |
| 1524 | for (c = spats[0].off.off; c; ++c) |
| 1525 | if (incl(&pos) == -1) |
| 1526 | break; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1527 | if (c) // at end of buffer |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1528 | { |
| 1529 | pos.lnum = curbuf->b_ml.ml_line_count + 1; |
| 1530 | pos.col = 0; |
| 1531 | } |
| 1532 | } |
| 1533 | } |
| 1534 | |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 1535 | /* |
| 1536 | * The actual search. |
| 1537 | */ |
Bram Moolenaar | 14184a3 | 2019-02-16 15:10:30 +0100 | [diff] [blame] | 1538 | c = searchit(curwin, curbuf, &pos, NULL, |
| 1539 | dirc == '/' ? FORWARD : BACKWARD, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1540 | searchstr, count, spats[0].off.end + (options & |
| 1541 | (SEARCH_KEEP + SEARCH_PEEK + SEARCH_HIS |
| 1542 | + SEARCH_MSG + SEARCH_START |
| 1543 | + ((pat != NULL && *pat == ';') ? 0 : SEARCH_NOOF))), |
Bram Moolenaar | 92ea26b | 2019-10-18 20:53:34 +0200 | [diff] [blame] | 1544 | RE_LAST, sia); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1545 | |
| 1546 | if (dircp != NULL) |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 1547 | *dircp = search_delim; // restore second '/' or '?' for normal_cmd() |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1548 | |
| 1549 | if (!shortmess(SHM_SEARCH) |
| 1550 | && ((dirc == '/' && LT_POS(pos, curwin->w_cursor)) |
| 1551 | || (dirc == '?' && LT_POS(curwin->w_cursor, pos)))) |
Bram Moolenaar | c7a10b3 | 2019-05-06 21:37:18 +0200 | [diff] [blame] | 1552 | show_top_bot_msg = TRUE; |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1553 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1554 | if (c == FAIL) |
| 1555 | { |
| 1556 | retval = 0; |
| 1557 | goto end_do_search; |
| 1558 | } |
| 1559 | if (spats[0].off.end && oap != NULL) |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1560 | oap->inclusive = TRUE; // 'e' includes last character |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1561 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1562 | retval = 1; // pattern found |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1563 | |
| 1564 | /* |
| 1565 | * Add character and/or line offset |
| 1566 | */ |
Bram Moolenaar | 9160f30 | 2006-08-29 15:58:12 +0000 | [diff] [blame] | 1567 | if (!(options & SEARCH_NOOF) || (pat != NULL && *pat == ';')) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1568 | { |
Bram Moolenaar | 8f46e4c | 2019-05-24 22:08:15 +0200 | [diff] [blame] | 1569 | pos_T org_pos = pos; |
| 1570 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1571 | if (spats[0].off.line) // Add the offset to the line number. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1572 | { |
| 1573 | c = pos.lnum + spats[0].off.off; |
| 1574 | if (c < 1) |
| 1575 | pos.lnum = 1; |
| 1576 | else if (c > curbuf->b_ml.ml_line_count) |
| 1577 | pos.lnum = curbuf->b_ml.ml_line_count; |
| 1578 | else |
| 1579 | pos.lnum = c; |
| 1580 | pos.col = 0; |
| 1581 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1582 | retval = 2; // pattern found, line offset added |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1583 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1584 | else if (pos.col < MAXCOL - 2) // just in case |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1585 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1586 | // to the right, check for end of file |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 1587 | c = spats[0].off.off; |
| 1588 | if (c > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1589 | { |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 1590 | while (c-- > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1591 | if (incl(&pos) == -1) |
| 1592 | break; |
| 1593 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1594 | // to the left, check for start of file |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1595 | else |
| 1596 | { |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 1597 | while (c++ < 0) |
| 1598 | if (decl(&pos) == -1) |
| 1599 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1600 | } |
| 1601 | } |
Bram Moolenaar | 8f46e4c | 2019-05-24 22:08:15 +0200 | [diff] [blame] | 1602 | if (!EQUAL_POS(pos, org_pos)) |
| 1603 | has_offset = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1606 | // Show [1/15] if 'S' is not in 'shortmess'. |
| 1607 | if ((options & SEARCH_ECHO) |
| 1608 | && messaging() |
Bram Moolenaar | 359ad1a | 2019-09-02 21:44:59 +0200 | [diff] [blame] | 1609 | && !msg_silent |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1610 | && c != FAIL |
| 1611 | && !shortmess(SHM_SEARCHCOUNT) |
| 1612 | && msgbuf != NULL) |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 1613 | cmdline_search_stat(dirc, &pos, &curwin->w_cursor, |
| 1614 | show_top_bot_msg, msgbuf, |
| 1615 | (count != 1 || has_offset |
Bram Moolenaar | 6cb0726 | 2020-05-29 22:49:43 +0200 | [diff] [blame] | 1616 | #ifdef FEAT_FOLDING |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 1617 | || (!(fdo_flags & FDO_SEARCH) |
| 1618 | && hasFolding(curwin->w_cursor.lnum, |
| 1619 | NULL, NULL)) |
Bram Moolenaar | 6cb0726 | 2020-05-29 22:49:43 +0200 | [diff] [blame] | 1620 | #endif |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 1621 | ), |
| 1622 | SEARCH_STAT_DEF_MAX_COUNT, |
| 1623 | SEARCH_STAT_DEF_TIMEOUT); |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1624 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1625 | /* |
| 1626 | * The search command can be followed by a ';' to do another search. |
| 1627 | * For example: "/pat/;/foo/+3;?bar" |
| 1628 | * This is like doing another search command, except: |
| 1629 | * - The remembered direction '/' or '?' is from the first search. |
| 1630 | * - When an error happens the cursor isn't moved at all. |
| 1631 | * Don't do this when called by get_address() (it handles ';' itself). |
| 1632 | */ |
| 1633 | if (!(options & SEARCH_OPT) || pat == NULL || *pat != ';') |
| 1634 | break; |
| 1635 | |
| 1636 | dirc = *++pat; |
Bram Moolenaar | c036e87 | 2020-02-21 21:30:52 +0100 | [diff] [blame] | 1637 | search_delim = dirc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1638 | if (dirc != '?' && dirc != '/') |
| 1639 | { |
| 1640 | retval = 0; |
Bram Moolenaar | ac78dd4 | 2022-01-02 19:25:26 +0000 | [diff] [blame] | 1641 | emsg(_(e_expected_question_or_slash_after_semicolon)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1642 | goto end_do_search; |
| 1643 | } |
| 1644 | ++pat; |
| 1645 | } |
| 1646 | |
| 1647 | if (options & SEARCH_MARK) |
| 1648 | setpcmark(); |
| 1649 | curwin->w_cursor = pos; |
| 1650 | curwin->w_set_curswant = TRUE; |
| 1651 | |
| 1652 | end_do_search: |
Bram Moolenaar | e100440 | 2020-10-24 20:49:43 +0200 | [diff] [blame] | 1653 | if ((options & SEARCH_KEEP) || (cmdmod.cmod_flags & CMOD_KEEPPATTERNS)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1654 | spats[0].off = old_off; |
| 1655 | vim_free(strcopy); |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1656 | vim_free(msgbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1657 | |
| 1658 | return retval; |
| 1659 | } |
| 1660 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1661 | /* |
| 1662 | * search_for_exact_line(buf, pos, dir, pat) |
| 1663 | * |
| 1664 | * Search for a line starting with the given pattern (ignoring leading |
Bram Moolenaar | 8ad80de | 2017-06-05 16:01:59 +0200 | [diff] [blame] | 1665 | * white-space), starting from pos and going in direction "dir". "pos" will |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1666 | * contain the position of the match found. Blank lines match only if |
Bram Moolenaar | 8ad80de | 2017-06-05 16:01:59 +0200 | [diff] [blame] | 1667 | * 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] | 1668 | * Return OK for success, or FAIL if no line found. |
| 1669 | */ |
| 1670 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 1671 | search_for_exact_line( |
| 1672 | buf_T *buf, |
| 1673 | pos_T *pos, |
| 1674 | int dir, |
| 1675 | char_u *pat) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1676 | { |
| 1677 | linenr_T start = 0; |
| 1678 | char_u *ptr; |
| 1679 | char_u *p; |
| 1680 | |
| 1681 | if (buf->b_ml.ml_line_count == 0) |
| 1682 | return FAIL; |
| 1683 | for (;;) |
| 1684 | { |
| 1685 | pos->lnum += dir; |
| 1686 | if (pos->lnum < 1) |
| 1687 | { |
| 1688 | if (p_ws) |
| 1689 | { |
| 1690 | pos->lnum = buf->b_ml.ml_line_count; |
| 1691 | if (!shortmess(SHM_SEARCH)) |
| 1692 | give_warning((char_u *)_(top_bot_msg), TRUE); |
| 1693 | } |
| 1694 | else |
| 1695 | { |
| 1696 | pos->lnum = 1; |
| 1697 | break; |
| 1698 | } |
| 1699 | } |
| 1700 | else if (pos->lnum > buf->b_ml.ml_line_count) |
| 1701 | { |
| 1702 | if (p_ws) |
| 1703 | { |
| 1704 | pos->lnum = 1; |
| 1705 | if (!shortmess(SHM_SEARCH)) |
| 1706 | give_warning((char_u *)_(bot_top_msg), TRUE); |
| 1707 | } |
| 1708 | else |
| 1709 | { |
| 1710 | pos->lnum = 1; |
| 1711 | break; |
| 1712 | } |
| 1713 | } |
| 1714 | if (pos->lnum == start) |
| 1715 | break; |
| 1716 | if (start == 0) |
| 1717 | start = pos->lnum; |
| 1718 | ptr = ml_get_buf(buf, pos->lnum, FALSE); |
| 1719 | p = skipwhite(ptr); |
| 1720 | pos->col = (colnr_T) (p - ptr); |
| 1721 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1722 | // when adding lines the matching line may be empty but it is not |
| 1723 | // ignored because we are interested in the next line -- Acevedo |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 1724 | if (compl_status_adding() && !compl_status_sol()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1725 | { |
| 1726 | if ((p_ic ? MB_STRICMP(p, pat) : STRCMP(p, pat)) == 0) |
| 1727 | return OK; |
| 1728 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1729 | else if (*p != NUL) // ignore empty lines |
| 1730 | { // expanding lines or words |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 1731 | if ((p_ic ? MB_STRNICMP(p, pat, ins_compl_len()) |
| 1732 | : STRNCMP(p, pat, ins_compl_len())) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1733 | return OK; |
| 1734 | } |
| 1735 | } |
| 1736 | return FAIL; |
| 1737 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1738 | |
| 1739 | /* |
| 1740 | * Character Searches |
| 1741 | */ |
| 1742 | |
| 1743 | /* |
| 1744 | * Search for a character in a line. If "t_cmd" is FALSE, move to the |
| 1745 | * position of the character, otherwise move to just before the char. |
| 1746 | * Do this "cap->count1" times. |
| 1747 | * Return FAIL or OK. |
| 1748 | */ |
| 1749 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 1750 | searchc(cmdarg_T *cap, int t_cmd) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1751 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1752 | int c = cap->nchar; // char to search for |
| 1753 | int dir = cap->arg; // TRUE for searching forward |
| 1754 | long count = cap->count1; // repeat count |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1755 | int col; |
| 1756 | char_u *p; |
| 1757 | int len; |
Bram Moolenaar | 8b3e033 | 2011-06-26 05:36:34 +0200 | [diff] [blame] | 1758 | int stop = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1759 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1760 | if (c != NUL) // normal search: remember args for repeat |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1761 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1762 | if (!KeyStuffed) // don't remember when redoing |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1763 | { |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 1764 | *lastc = c; |
| 1765 | set_csearch_direction(dir); |
| 1766 | set_csearch_until(t_cmd); |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 1767 | lastc_bytelen = (*mb_char2bytes)(c, lastc_bytes); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1768 | if (cap->ncharC1 != 0) |
| 1769 | { |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 1770 | lastc_bytelen += (*mb_char2bytes)(cap->ncharC1, |
| 1771 | lastc_bytes + lastc_bytelen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1772 | if (cap->ncharC2 != 0) |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 1773 | lastc_bytelen += (*mb_char2bytes)(cap->ncharC2, |
| 1774 | lastc_bytes + lastc_bytelen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1775 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1776 | } |
| 1777 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1778 | else // repeat previous search |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1779 | { |
Bram Moolenaar | 264b74f | 2019-01-24 17:18:42 +0100 | [diff] [blame] | 1780 | if (*lastc == NUL && lastc_bytelen == 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1781 | return FAIL; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1782 | if (dir) // repeat in opposite direction |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1783 | dir = -lastcdir; |
| 1784 | else |
| 1785 | dir = lastcdir; |
| 1786 | t_cmd = last_t_cmd; |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 1787 | c = *lastc; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1788 | // For multi-byte re-use last lastc_bytes[] and lastc_bytelen. |
Bram Moolenaar | 8b3e033 | 2011-06-26 05:36:34 +0200 | [diff] [blame] | 1789 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1790 | // Force a move of at least one char, so ";" and "," will move the |
| 1791 | // cursor, even if the cursor is right in front of char we are looking |
| 1792 | // at. |
Bram Moolenaar | 19fd09a | 2011-07-15 13:21:30 +0200 | [diff] [blame] | 1793 | if (vim_strchr(p_cpo, CPO_SCOLON) == NULL && count == 1 && t_cmd) |
Bram Moolenaar | 8b3e033 | 2011-06-26 05:36:34 +0200 | [diff] [blame] | 1794 | stop = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1795 | } |
| 1796 | |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 1797 | if (dir == BACKWARD) |
| 1798 | cap->oap->inclusive = FALSE; |
| 1799 | else |
| 1800 | cap->oap->inclusive = TRUE; |
| 1801 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1802 | p = ml_get_curline(); |
| 1803 | col = curwin->w_cursor.col; |
| 1804 | len = (int)STRLEN(p); |
| 1805 | |
| 1806 | while (count--) |
| 1807 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1808 | if (has_mbyte) |
| 1809 | { |
| 1810 | for (;;) |
| 1811 | { |
| 1812 | if (dir > 0) |
| 1813 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1814 | col += (*mb_ptr2len)(p + col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1815 | if (col >= len) |
| 1816 | return FAIL; |
| 1817 | } |
| 1818 | else |
| 1819 | { |
| 1820 | if (col == 0) |
| 1821 | return FAIL; |
| 1822 | col -= (*mb_head_off)(p, p + col - 1) + 1; |
| 1823 | } |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 1824 | if (lastc_bytelen == 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1825 | { |
Bram Moolenaar | 8b3e033 | 2011-06-26 05:36:34 +0200 | [diff] [blame] | 1826 | if (p[col] == c && stop) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1827 | break; |
| 1828 | } |
Bram Moolenaar | 66727e1 | 2017-03-01 22:17:05 +0100 | [diff] [blame] | 1829 | else if (STRNCMP(p + col, lastc_bytes, lastc_bytelen) == 0 |
Bram Moolenaar | b129a44 | 2016-12-01 17:25:20 +0100 | [diff] [blame] | 1830 | && stop) |
Bram Moolenaar | 66727e1 | 2017-03-01 22:17:05 +0100 | [diff] [blame] | 1831 | break; |
Bram Moolenaar | 8b3e033 | 2011-06-26 05:36:34 +0200 | [diff] [blame] | 1832 | stop = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1833 | } |
| 1834 | } |
| 1835 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1836 | { |
| 1837 | for (;;) |
| 1838 | { |
| 1839 | if ((col += dir) < 0 || col >= len) |
| 1840 | return FAIL; |
Bram Moolenaar | 8b3e033 | 2011-06-26 05:36:34 +0200 | [diff] [blame] | 1841 | if (p[col] == c && stop) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1842 | break; |
Bram Moolenaar | 8b3e033 | 2011-06-26 05:36:34 +0200 | [diff] [blame] | 1843 | stop = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1844 | } |
| 1845 | } |
| 1846 | } |
| 1847 | |
| 1848 | if (t_cmd) |
| 1849 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1850 | // backup to before the character (possibly double-byte) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1851 | col -= dir; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1852 | if (has_mbyte) |
| 1853 | { |
| 1854 | if (dir < 0) |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1855 | // Landed on the search char which is lastc_bytelen long |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 1856 | col += lastc_bytelen - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1857 | else |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 1858 | // To previous char, which may be multi-byte. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1859 | col -= (*mb_head_off)(p, p + col); |
| 1860 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1861 | } |
| 1862 | curwin->w_cursor.col = col; |
| 1863 | |
| 1864 | return OK; |
| 1865 | } |
| 1866 | |
| 1867 | /* |
| 1868 | * "Other" Searches |
| 1869 | */ |
| 1870 | |
| 1871 | /* |
| 1872 | * findmatch - find the matching paren or brace |
| 1873 | * |
| 1874 | * Improvement over vi: Braces inside quotes are ignored. |
| 1875 | */ |
| 1876 | pos_T * |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 1877 | findmatch(oparg_T *oap, int initc) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1878 | { |
| 1879 | return findmatchlimit(oap, initc, 0, 0); |
| 1880 | } |
| 1881 | |
| 1882 | /* |
| 1883 | * Return TRUE if the character before "linep[col]" equals "ch". |
| 1884 | * Return FALSE if "col" is zero. |
| 1885 | * Update "*prevcol" to the column of the previous character, unless "prevcol" |
| 1886 | * is NULL. |
| 1887 | * Handles multibyte string correctly. |
| 1888 | */ |
| 1889 | static int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 1890 | check_prevcol( |
| 1891 | char_u *linep, |
| 1892 | int col, |
| 1893 | int ch, |
| 1894 | int *prevcol) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1895 | { |
| 1896 | --col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1897 | if (col > 0 && has_mbyte) |
| 1898 | col -= (*mb_head_off)(linep, linep + col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1899 | if (prevcol) |
| 1900 | *prevcol = col; |
| 1901 | return (col >= 0 && linep[col] == ch) ? TRUE : FALSE; |
| 1902 | } |
| 1903 | |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 1904 | /* |
| 1905 | * Raw string start is found at linep[startpos.col - 1]. |
| 1906 | * Return TRUE if the matching end can be found between startpos and endpos. |
| 1907 | */ |
| 1908 | static int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 1909 | find_rawstring_end(char_u *linep, pos_T *startpos, pos_T *endpos) |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 1910 | { |
| 1911 | char_u *p; |
| 1912 | char_u *delim_copy; |
| 1913 | size_t delim_len; |
| 1914 | linenr_T lnum; |
| 1915 | int found = FALSE; |
| 1916 | |
| 1917 | for (p = linep + startpos->col + 1; *p && *p != '('; ++p) |
| 1918 | ; |
| 1919 | delim_len = (p - linep) - startpos->col - 1; |
Bram Moolenaar | 71ccd03 | 2020-06-12 22:59:11 +0200 | [diff] [blame] | 1920 | delim_copy = vim_strnsave(linep + startpos->col + 1, delim_len); |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 1921 | if (delim_copy == NULL) |
| 1922 | return FALSE; |
| 1923 | for (lnum = startpos->lnum; lnum <= endpos->lnum; ++lnum) |
| 1924 | { |
| 1925 | char_u *line = ml_get(lnum); |
| 1926 | |
| 1927 | for (p = line + (lnum == startpos->lnum |
| 1928 | ? startpos->col + 1 : 0); *p; ++p) |
| 1929 | { |
| 1930 | if (lnum == endpos->lnum && (colnr_T)(p - line) >= endpos->col) |
| 1931 | break; |
Bram Moolenaar | 282f9c6 | 2020-08-04 21:46:18 +0200 | [diff] [blame] | 1932 | if (*p == ')' && STRNCMP(delim_copy, p + 1, delim_len) == 0 |
| 1933 | && p[delim_len + 1] == '"') |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 1934 | { |
| 1935 | found = TRUE; |
| 1936 | break; |
| 1937 | } |
| 1938 | } |
| 1939 | if (found) |
| 1940 | break; |
| 1941 | } |
| 1942 | vim_free(delim_copy); |
| 1943 | return found; |
| 1944 | } |
| 1945 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1946 | /* |
Bram Moolenaar | 556ae8e | 2019-11-21 22:27:22 +0100 | [diff] [blame] | 1947 | * Check matchpairs option for "*initc". |
| 1948 | * If there is a match set "*initc" to the matching character and "*findc" to |
| 1949 | * the opposite character. Set "*backwards" to the direction. |
| 1950 | * When "switchit" is TRUE swap the direction. |
| 1951 | */ |
| 1952 | static void |
| 1953 | find_mps_values( |
| 1954 | int *initc, |
| 1955 | int *findc, |
| 1956 | int *backwards, |
| 1957 | int switchit) |
| 1958 | { |
| 1959 | char_u *ptr; |
| 1960 | |
| 1961 | ptr = curbuf->b_p_mps; |
| 1962 | while (*ptr != NUL) |
| 1963 | { |
| 1964 | if (has_mbyte) |
| 1965 | { |
| 1966 | char_u *prev; |
| 1967 | |
| 1968 | if (mb_ptr2char(ptr) == *initc) |
| 1969 | { |
| 1970 | if (switchit) |
| 1971 | { |
| 1972 | *findc = *initc; |
| 1973 | *initc = mb_ptr2char(ptr + mb_ptr2len(ptr) + 1); |
| 1974 | *backwards = TRUE; |
| 1975 | } |
| 1976 | else |
| 1977 | { |
| 1978 | *findc = mb_ptr2char(ptr + mb_ptr2len(ptr) + 1); |
| 1979 | *backwards = FALSE; |
| 1980 | } |
| 1981 | return; |
| 1982 | } |
| 1983 | prev = ptr; |
| 1984 | ptr += mb_ptr2len(ptr) + 1; |
| 1985 | if (mb_ptr2char(ptr) == *initc) |
| 1986 | { |
| 1987 | if (switchit) |
| 1988 | { |
| 1989 | *findc = *initc; |
| 1990 | *initc = mb_ptr2char(prev); |
| 1991 | *backwards = FALSE; |
| 1992 | } |
| 1993 | else |
| 1994 | { |
| 1995 | *findc = mb_ptr2char(prev); |
| 1996 | *backwards = TRUE; |
| 1997 | } |
| 1998 | return; |
| 1999 | } |
| 2000 | ptr += mb_ptr2len(ptr); |
| 2001 | } |
| 2002 | else |
| 2003 | { |
| 2004 | if (*ptr == *initc) |
| 2005 | { |
| 2006 | if (switchit) |
| 2007 | { |
| 2008 | *backwards = TRUE; |
| 2009 | *findc = *initc; |
| 2010 | *initc = ptr[2]; |
| 2011 | } |
| 2012 | else |
| 2013 | { |
| 2014 | *backwards = FALSE; |
| 2015 | *findc = ptr[2]; |
| 2016 | } |
| 2017 | return; |
| 2018 | } |
| 2019 | ptr += 2; |
| 2020 | if (*ptr == *initc) |
| 2021 | { |
| 2022 | if (switchit) |
| 2023 | { |
| 2024 | *backwards = FALSE; |
| 2025 | *findc = *initc; |
| 2026 | *initc = ptr[-2]; |
| 2027 | } |
| 2028 | else |
| 2029 | { |
| 2030 | *backwards = TRUE; |
| 2031 | *findc = ptr[-2]; |
| 2032 | } |
| 2033 | return; |
| 2034 | } |
| 2035 | ++ptr; |
| 2036 | } |
| 2037 | if (*ptr == ',') |
| 2038 | ++ptr; |
| 2039 | } |
| 2040 | } |
| 2041 | |
| 2042 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2043 | * findmatchlimit -- find the matching paren or brace, if it exists within |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 2044 | * maxtravel lines of the cursor. A maxtravel of 0 means search until falling |
| 2045 | * off the edge of the file. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2046 | * |
| 2047 | * "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] | 2048 | * character at or after the cursor. Special values: |
| 2049 | * '*' look for C-style comment / * |
| 2050 | * '/' look for C-style comment / *, ignoring comment-end |
| 2051 | * '#' look for preprocessor directives |
| 2052 | * 'R' look for raw string start: R"delim(text)delim" (only backwards) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2053 | * |
| 2054 | * flags: FM_BACKWARD search backwards (when initc is '/', '*' or '#') |
| 2055 | * FM_FORWARD search forwards (when initc is '/', '*' or '#') |
| 2056 | * FM_BLOCKSTOP stop at start/end of block ({ or } in column 0) |
| 2057 | * FM_SKIPCOMM skip comments (not implemented yet!) |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 2058 | * |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 2059 | * "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] | 2060 | * NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2061 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2062 | pos_T * |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 2063 | findmatchlimit( |
| 2064 | oparg_T *oap, |
| 2065 | int initc, |
| 2066 | int flags, |
| 2067 | int maxtravel) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2068 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2069 | static pos_T pos; // current search position |
| 2070 | int findc = 0; // matching brace |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2071 | int c; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2072 | int count = 0; // cumulative number of braces |
| 2073 | int backwards = FALSE; // init for gcc |
| 2074 | int raw_string = FALSE; // search for raw string |
| 2075 | int inquote = FALSE; // TRUE when inside quotes |
| 2076 | char_u *linep; // pointer to current line |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2077 | char_u *ptr; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2078 | int do_quotes; // check for quotes in current line |
| 2079 | int at_start; // do_quotes value at start position |
| 2080 | int hash_dir = 0; // Direction searched for # things |
| 2081 | int comment_dir = 0; // Direction searched for comments |
| 2082 | pos_T match_pos; // Where last slash-star was found |
| 2083 | int start_in_quotes; // start position is in quotes |
| 2084 | int traveled = 0; // how far we've searched so far |
| 2085 | int ignore_cend = FALSE; // ignore comment end |
| 2086 | int cpo_match; // vi compatible matching |
| 2087 | int cpo_bsl; // don't recognize backslashes |
| 2088 | int match_escaped = 0; // search for escaped match |
| 2089 | int dir; // Direction to search |
| 2090 | int comment_col = MAXCOL; // start of / / comment |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2091 | int lispcomm = FALSE; // inside of Lisp-style comment |
| 2092 | int lisp = curbuf->b_p_lisp; // engage Lisp-specific hacks ;) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2093 | |
| 2094 | pos = curwin->w_cursor; |
Bram Moolenaar | c56c459 | 2013-08-14 17:45:29 +0200 | [diff] [blame] | 2095 | pos.coladd = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2096 | linep = ml_get(pos.lnum); |
| 2097 | |
| 2098 | cpo_match = (vim_strchr(p_cpo, CPO_MATCH) != NULL); |
| 2099 | cpo_bsl = (vim_strchr(p_cpo, CPO_MATCHBSL) != NULL); |
| 2100 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2101 | // Direction to search when initc is '/', '*' or '#' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2102 | if (flags & FM_BACKWARD) |
| 2103 | dir = BACKWARD; |
| 2104 | else if (flags & FM_FORWARD) |
| 2105 | dir = FORWARD; |
| 2106 | else |
| 2107 | dir = 0; |
| 2108 | |
| 2109 | /* |
| 2110 | * if initc given, look in the table for the matching character |
| 2111 | * '/' and '*' are special cases: look for start or end of comment. |
| 2112 | * When '/' is used, we ignore running backwards into an star-slash, for |
| 2113 | * "[*" command, we just want to find any comment. |
| 2114 | */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 2115 | if (initc == '/' || initc == '*' || initc == 'R') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2116 | { |
| 2117 | comment_dir = dir; |
| 2118 | if (initc == '/') |
| 2119 | ignore_cend = TRUE; |
| 2120 | backwards = (dir == FORWARD) ? FALSE : TRUE; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 2121 | raw_string = (initc == 'R'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2122 | initc = NUL; |
| 2123 | } |
| 2124 | else if (initc != '#' && initc != NUL) |
| 2125 | { |
Bram Moolenaar | 8c7694a | 2013-01-17 17:02:05 +0100 | [diff] [blame] | 2126 | find_mps_values(&initc, &findc, &backwards, TRUE); |
Connor Lane Smith | b9115da | 2021-07-31 13:31:42 +0200 | [diff] [blame] | 2127 | if (dir) |
| 2128 | backwards = (dir == FORWARD) ? FALSE : TRUE; |
Bram Moolenaar | 8c7694a | 2013-01-17 17:02:05 +0100 | [diff] [blame] | 2129 | if (findc == NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2130 | return NULL; |
| 2131 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2132 | else |
| 2133 | { |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 2134 | /* |
| 2135 | * Either initc is '#', or no initc was given and we need to look |
| 2136 | * under the cursor. |
| 2137 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2138 | if (initc == '#') |
| 2139 | { |
| 2140 | hash_dir = dir; |
| 2141 | } |
| 2142 | else |
| 2143 | { |
| 2144 | /* |
| 2145 | * initc was not given, must look for something to match under |
| 2146 | * or near the cursor. |
| 2147 | * Only check for special things when 'cpo' doesn't have '%'. |
| 2148 | */ |
| 2149 | if (!cpo_match) |
| 2150 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2151 | // Are we before or at #if, #else etc.? |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2152 | ptr = skipwhite(linep); |
| 2153 | if (*ptr == '#' && pos.col <= (colnr_T)(ptr - linep)) |
| 2154 | { |
| 2155 | ptr = skipwhite(ptr + 1); |
| 2156 | if ( STRNCMP(ptr, "if", 2) == 0 |
| 2157 | || STRNCMP(ptr, "endif", 5) == 0 |
| 2158 | || STRNCMP(ptr, "el", 2) == 0) |
| 2159 | hash_dir = 1; |
| 2160 | } |
| 2161 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2162 | // Are we on a comment? |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2163 | else if (linep[pos.col] == '/') |
| 2164 | { |
| 2165 | if (linep[pos.col + 1] == '*') |
| 2166 | { |
| 2167 | comment_dir = FORWARD; |
| 2168 | backwards = FALSE; |
| 2169 | pos.col++; |
| 2170 | } |
| 2171 | else if (pos.col > 0 && linep[pos.col - 1] == '*') |
| 2172 | { |
| 2173 | comment_dir = BACKWARD; |
| 2174 | backwards = TRUE; |
| 2175 | pos.col--; |
| 2176 | } |
| 2177 | } |
| 2178 | else if (linep[pos.col] == '*') |
| 2179 | { |
| 2180 | if (linep[pos.col + 1] == '/') |
| 2181 | { |
| 2182 | comment_dir = BACKWARD; |
| 2183 | backwards = TRUE; |
| 2184 | } |
| 2185 | else if (pos.col > 0 && linep[pos.col - 1] == '/') |
| 2186 | { |
| 2187 | comment_dir = FORWARD; |
| 2188 | backwards = FALSE; |
| 2189 | } |
| 2190 | } |
| 2191 | } |
| 2192 | |
| 2193 | /* |
| 2194 | * If we are not on a comment or the # at the start of a line, then |
| 2195 | * look for brace anywhere on this line after the cursor. |
| 2196 | */ |
| 2197 | if (!hash_dir && !comment_dir) |
| 2198 | { |
| 2199 | /* |
| 2200 | * Find the brace under or after the cursor. |
| 2201 | * If beyond the end of the line, use the last character in |
| 2202 | * the line. |
| 2203 | */ |
| 2204 | if (linep[pos.col] == NUL && pos.col) |
| 2205 | --pos.col; |
| 2206 | for (;;) |
| 2207 | { |
Bram Moolenaar | 8c7694a | 2013-01-17 17:02:05 +0100 | [diff] [blame] | 2208 | initc = PTR2CHAR(linep + pos.col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2209 | if (initc == NUL) |
| 2210 | break; |
| 2211 | |
Bram Moolenaar | 8c7694a | 2013-01-17 17:02:05 +0100 | [diff] [blame] | 2212 | find_mps_values(&initc, &findc, &backwards, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2213 | if (findc) |
| 2214 | break; |
Bram Moolenaar | 1614a14 | 2019-10-06 22:00:13 +0200 | [diff] [blame] | 2215 | pos.col += mb_ptr2len(linep + pos.col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2216 | } |
| 2217 | if (!findc) |
| 2218 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2219 | // no brace in the line, maybe use " #if" then |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2220 | if (!cpo_match && *skipwhite(linep) == '#') |
| 2221 | hash_dir = 1; |
| 2222 | else |
| 2223 | return NULL; |
| 2224 | } |
| 2225 | else if (!cpo_bsl) |
| 2226 | { |
| 2227 | int col, bslcnt = 0; |
| 2228 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2229 | // Set "match_escaped" if there are an odd number of |
| 2230 | // backslashes. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2231 | for (col = pos.col; check_prevcol(linep, col, '\\', &col);) |
| 2232 | bslcnt++; |
| 2233 | match_escaped = (bslcnt & 1); |
| 2234 | } |
| 2235 | } |
| 2236 | } |
| 2237 | if (hash_dir) |
| 2238 | { |
| 2239 | /* |
| 2240 | * Look for matching #if, #else, #elif, or #endif |
| 2241 | */ |
| 2242 | if (oap != NULL) |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2243 | oap->motion_type = MLINE; // Linewise for this case only |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2244 | if (initc != '#') |
| 2245 | { |
| 2246 | ptr = skipwhite(skipwhite(linep) + 1); |
| 2247 | if (STRNCMP(ptr, "if", 2) == 0 || STRNCMP(ptr, "el", 2) == 0) |
| 2248 | hash_dir = 1; |
| 2249 | else if (STRNCMP(ptr, "endif", 5) == 0) |
| 2250 | hash_dir = -1; |
| 2251 | else |
| 2252 | return NULL; |
| 2253 | } |
| 2254 | pos.col = 0; |
| 2255 | while (!got_int) |
| 2256 | { |
| 2257 | if (hash_dir > 0) |
| 2258 | { |
| 2259 | if (pos.lnum == curbuf->b_ml.ml_line_count) |
| 2260 | break; |
| 2261 | } |
| 2262 | else if (pos.lnum == 1) |
| 2263 | break; |
| 2264 | pos.lnum += hash_dir; |
| 2265 | linep = ml_get(pos.lnum); |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2266 | line_breakcheck(); // check for CTRL-C typed |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2267 | ptr = skipwhite(linep); |
| 2268 | if (*ptr != '#') |
| 2269 | continue; |
| 2270 | pos.col = (colnr_T) (ptr - linep); |
| 2271 | ptr = skipwhite(ptr + 1); |
| 2272 | if (hash_dir > 0) |
| 2273 | { |
| 2274 | if (STRNCMP(ptr, "if", 2) == 0) |
| 2275 | count++; |
| 2276 | else if (STRNCMP(ptr, "el", 2) == 0) |
| 2277 | { |
| 2278 | if (count == 0) |
| 2279 | return &pos; |
| 2280 | } |
| 2281 | else if (STRNCMP(ptr, "endif", 5) == 0) |
| 2282 | { |
| 2283 | if (count == 0) |
| 2284 | return &pos; |
| 2285 | count--; |
| 2286 | } |
| 2287 | } |
| 2288 | else |
| 2289 | { |
| 2290 | if (STRNCMP(ptr, "if", 2) == 0) |
| 2291 | { |
| 2292 | if (count == 0) |
| 2293 | return &pos; |
| 2294 | count--; |
| 2295 | } |
| 2296 | else if (initc == '#' && STRNCMP(ptr, "el", 2) == 0) |
| 2297 | { |
| 2298 | if (count == 0) |
| 2299 | return &pos; |
| 2300 | } |
| 2301 | else if (STRNCMP(ptr, "endif", 5) == 0) |
| 2302 | count++; |
| 2303 | } |
| 2304 | } |
| 2305 | return NULL; |
| 2306 | } |
| 2307 | } |
| 2308 | |
| 2309 | #ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2310 | // This is just guessing: when 'rightleft' is set, search for a matching |
| 2311 | // paren/brace in the other direction. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2312 | if (curwin->w_p_rl && vim_strchr((char_u *)"()[]{}<>", initc) != NULL) |
| 2313 | backwards = !backwards; |
| 2314 | #endif |
| 2315 | |
| 2316 | do_quotes = -1; |
| 2317 | start_in_quotes = MAYBE; |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 2318 | CLEAR_POS(&match_pos); |
Bram Moolenaar | fd2ac76 | 2006-03-01 22:09:21 +0000 | [diff] [blame] | 2319 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2320 | // backward search: Check if this line contains a single-line comment |
Bram Moolenaar | 8e145b8 | 2022-05-21 20:17:31 +0100 | [diff] [blame] | 2321 | if ((backwards && comment_dir) || lisp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2322 | comment_col = check_linecomment(linep); |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2323 | if (lisp && comment_col != MAXCOL && pos.col > (colnr_T)comment_col) |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2324 | lispcomm = TRUE; // find match inside this comment |
Bram Moolenaar | 8e145b8 | 2022-05-21 20:17:31 +0100 | [diff] [blame] | 2325 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2326 | while (!got_int) |
| 2327 | { |
| 2328 | /* |
| 2329 | * Go to the next position, forward or backward. We could use |
| 2330 | * inc() and dec() here, but that is much slower |
| 2331 | */ |
| 2332 | if (backwards) |
| 2333 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2334 | // char to match is inside of comment, don't search outside |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2335 | if (lispcomm && pos.col < (colnr_T)comment_col) |
| 2336 | break; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2337 | if (pos.col == 0) // at start of line, go to prev. one |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2338 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2339 | if (pos.lnum == 1) // start of file |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2340 | break; |
| 2341 | --pos.lnum; |
| 2342 | |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 2343 | if (maxtravel > 0 && ++traveled > maxtravel) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2344 | break; |
| 2345 | |
| 2346 | linep = ml_get(pos.lnum); |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2347 | pos.col = (colnr_T)STRLEN(linep); // pos.col on trailing NUL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2348 | do_quotes = -1; |
| 2349 | line_breakcheck(); |
| 2350 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2351 | // Check if this line contains a single-line comment |
Bram Moolenaar | 8e145b8 | 2022-05-21 20:17:31 +0100 | [diff] [blame] | 2352 | if (comment_dir || lisp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2353 | comment_col = check_linecomment(linep); |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2354 | // skip comment |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2355 | if (lisp && comment_col != MAXCOL) |
| 2356 | pos.col = comment_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2357 | } |
| 2358 | else |
| 2359 | { |
| 2360 | --pos.col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2361 | if (has_mbyte) |
| 2362 | pos.col -= (*mb_head_off)(linep, linep + pos.col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2363 | } |
| 2364 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2365 | else // forward search |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2366 | { |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2367 | if (linep[pos.col] == NUL |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2368 | // at end of line, go to next one |
Bram Moolenaar | 8e145b8 | 2022-05-21 20:17:31 +0100 | [diff] [blame] | 2369 | // For lisp don't search for match in comment |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2370 | || (lisp && comment_col != MAXCOL |
Bram Moolenaar | 8e145b8 | 2022-05-21 20:17:31 +0100 | [diff] [blame] | 2371 | && pos.col == (colnr_T)comment_col)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2372 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2373 | if (pos.lnum == curbuf->b_ml.ml_line_count // end of file |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2374 | // line is exhausted and comment with it, |
| 2375 | // don't search for match in code |
Bram Moolenaar | 8e145b8 | 2022-05-21 20:17:31 +0100 | [diff] [blame] | 2376 | || lispcomm) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2377 | break; |
| 2378 | ++pos.lnum; |
| 2379 | |
| 2380 | if (maxtravel && traveled++ > maxtravel) |
| 2381 | break; |
| 2382 | |
| 2383 | linep = ml_get(pos.lnum); |
| 2384 | pos.col = 0; |
| 2385 | do_quotes = -1; |
| 2386 | line_breakcheck(); |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2387 | if (lisp) // find comment pos in new line |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2388 | comment_col = check_linecomment(linep); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2389 | } |
| 2390 | else |
| 2391 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2392 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2393 | pos.col += (*mb_ptr2len)(linep + pos.col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2394 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2395 | ++pos.col; |
| 2396 | } |
| 2397 | } |
| 2398 | |
| 2399 | /* |
| 2400 | * If FM_BLOCKSTOP given, stop at a '{' or '}' in column 0. |
| 2401 | */ |
Dominique Pelle | 7765f5c | 2022-04-10 11:26:53 +0100 | [diff] [blame] | 2402 | if (pos.col == 0 && (flags & FM_BLOCKSTOP) |
| 2403 | && (linep[0] == '{' || linep[0] == '}')) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2404 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2405 | if (linep[0] == findc && count == 0) // match! |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2406 | return &pos; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2407 | break; // out of scope |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2408 | } |
| 2409 | |
| 2410 | if (comment_dir) |
| 2411 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2412 | // Note: comments do not nest, and we ignore quotes in them |
| 2413 | // TODO: ignore comment brackets inside strings |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2414 | if (comment_dir == FORWARD) |
| 2415 | { |
| 2416 | if (linep[pos.col] == '*' && linep[pos.col + 1] == '/') |
| 2417 | { |
| 2418 | pos.col++; |
| 2419 | return &pos; |
| 2420 | } |
| 2421 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2422 | else // Searching backwards |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2423 | { |
| 2424 | /* |
| 2425 | * A comment may contain / * or / /, it may also start or end |
Bram Moolenaar | f8c53d3 | 2017-11-12 15:36:38 +0100 | [diff] [blame] | 2426 | * with / * /. Ignore a / * after / / and after *. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2427 | */ |
| 2428 | if (pos.col == 0) |
| 2429 | continue; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 2430 | else if (raw_string) |
| 2431 | { |
| 2432 | if (linep[pos.col - 1] == 'R' |
| 2433 | && linep[pos.col] == '"' |
| 2434 | && vim_strchr(linep + pos.col + 1, '(') != NULL) |
| 2435 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2436 | // Possible start of raw string. Now that we have the |
| 2437 | // delimiter we can check if it ends before where we |
| 2438 | // started searching, or before the previously found |
| 2439 | // raw string start. |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 2440 | if (!find_rawstring_end(linep, &pos, |
| 2441 | count > 0 ? &match_pos : &curwin->w_cursor)) |
| 2442 | { |
| 2443 | count++; |
| 2444 | match_pos = pos; |
| 2445 | match_pos.col--; |
| 2446 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2447 | linep = ml_get(pos.lnum); // may have been released |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 2448 | } |
| 2449 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2450 | else if ( linep[pos.col - 1] == '/' |
| 2451 | && linep[pos.col] == '*' |
Bram Moolenaar | f8c53d3 | 2017-11-12 15:36:38 +0100 | [diff] [blame] | 2452 | && (pos.col == 1 || linep[pos.col - 2] != '*') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2453 | && (int)pos.col < comment_col) |
| 2454 | { |
| 2455 | count++; |
| 2456 | match_pos = pos; |
| 2457 | match_pos.col--; |
| 2458 | } |
| 2459 | else if (linep[pos.col - 1] == '*' && linep[pos.col] == '/') |
| 2460 | { |
| 2461 | if (count > 0) |
| 2462 | pos = match_pos; |
| 2463 | else if (pos.col > 1 && linep[pos.col - 2] == '/' |
| 2464 | && (int)pos.col <= comment_col) |
| 2465 | pos.col -= 2; |
| 2466 | else if (ignore_cend) |
| 2467 | continue; |
| 2468 | else |
| 2469 | return NULL; |
| 2470 | return &pos; |
| 2471 | } |
| 2472 | } |
| 2473 | continue; |
| 2474 | } |
| 2475 | |
| 2476 | /* |
| 2477 | * If smart matching ('cpoptions' does not contain '%'), braces inside |
| 2478 | * of quotes are ignored, but only if there is an even number of |
| 2479 | * quotes in the line. |
| 2480 | */ |
| 2481 | if (cpo_match) |
| 2482 | do_quotes = 0; |
| 2483 | else if (do_quotes == -1) |
| 2484 | { |
| 2485 | /* |
| 2486 | * Count the number of quotes in the line, skipping \" and '"'. |
| 2487 | * Watch out for "\\". |
| 2488 | */ |
| 2489 | at_start = do_quotes; |
| 2490 | for (ptr = linep; *ptr; ++ptr) |
| 2491 | { |
| 2492 | if (ptr == linep + pos.col + backwards) |
| 2493 | at_start = (do_quotes & 1); |
| 2494 | if (*ptr == '"' |
| 2495 | && (ptr == linep || ptr[-1] != '\'' || ptr[1] != '\'')) |
| 2496 | ++do_quotes; |
| 2497 | if (*ptr == '\\' && ptr[1] != NUL) |
| 2498 | ++ptr; |
| 2499 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2500 | do_quotes &= 1; // result is 1 with even number of quotes |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2501 | |
| 2502 | /* |
| 2503 | * If we find an uneven count, check current line and previous |
| 2504 | * one for a '\' at the end. |
| 2505 | */ |
| 2506 | if (!do_quotes) |
| 2507 | { |
| 2508 | inquote = FALSE; |
| 2509 | if (ptr[-1] == '\\') |
| 2510 | { |
| 2511 | do_quotes = 1; |
| 2512 | if (start_in_quotes == MAYBE) |
| 2513 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2514 | // Do we need to use at_start here? |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2515 | inquote = TRUE; |
| 2516 | start_in_quotes = TRUE; |
| 2517 | } |
| 2518 | else if (backwards) |
| 2519 | inquote = TRUE; |
| 2520 | } |
| 2521 | if (pos.lnum > 1) |
| 2522 | { |
| 2523 | ptr = ml_get(pos.lnum - 1); |
| 2524 | if (*ptr && *(ptr + STRLEN(ptr) - 1) == '\\') |
| 2525 | { |
| 2526 | do_quotes = 1; |
| 2527 | if (start_in_quotes == MAYBE) |
| 2528 | { |
| 2529 | inquote = at_start; |
| 2530 | if (inquote) |
| 2531 | start_in_quotes = TRUE; |
| 2532 | } |
| 2533 | else if (!backwards) |
| 2534 | inquote = TRUE; |
| 2535 | } |
Bram Moolenaar | aec1179 | 2007-07-10 11:09:36 +0000 | [diff] [blame] | 2536 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2537 | // ml_get() only keeps one line, need to get linep again |
Bram Moolenaar | aec1179 | 2007-07-10 11:09:36 +0000 | [diff] [blame] | 2538 | linep = ml_get(pos.lnum); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2539 | } |
| 2540 | } |
| 2541 | } |
| 2542 | if (start_in_quotes == MAYBE) |
| 2543 | start_in_quotes = FALSE; |
| 2544 | |
| 2545 | /* |
| 2546 | * If 'smartmatch' is set: |
| 2547 | * Things inside quotes are ignored by setting 'inquote'. If we |
| 2548 | * find a quote without a preceding '\' invert 'inquote'. At the |
| 2549 | * end of a line not ending in '\' we reset 'inquote'. |
| 2550 | * |
| 2551 | * In lines with an uneven number of quotes (without preceding '\') |
| 2552 | * we do not know which part to ignore. Therefore we only set |
| 2553 | * inquote if the number of quotes in a line is even, unless this |
| 2554 | * line or the previous one ends in a '\'. Complicated, isn't it? |
| 2555 | */ |
Bram Moolenaar | 8c7694a | 2013-01-17 17:02:05 +0100 | [diff] [blame] | 2556 | c = PTR2CHAR(linep + pos.col); |
| 2557 | switch (c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2558 | { |
| 2559 | case NUL: |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2560 | // at end of line without trailing backslash, reset inquote |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2561 | if (pos.col == 0 || linep[pos.col - 1] != '\\') |
| 2562 | { |
| 2563 | inquote = FALSE; |
| 2564 | start_in_quotes = FALSE; |
| 2565 | } |
| 2566 | break; |
| 2567 | |
| 2568 | case '"': |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2569 | // a quote that is preceded with an odd number of backslashes is |
| 2570 | // ignored |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2571 | if (do_quotes) |
| 2572 | { |
| 2573 | int col; |
| 2574 | |
| 2575 | for (col = pos.col - 1; col >= 0; --col) |
| 2576 | if (linep[col] != '\\') |
| 2577 | break; |
| 2578 | if ((((int)pos.col - 1 - col) & 1) == 0) |
| 2579 | { |
| 2580 | inquote = !inquote; |
| 2581 | start_in_quotes = FALSE; |
| 2582 | } |
| 2583 | } |
| 2584 | break; |
| 2585 | |
| 2586 | /* |
| 2587 | * If smart matching ('cpoptions' does not contain '%'): |
| 2588 | * Skip things in single quotes: 'x' or '\x'. Be careful for single |
| 2589 | * single quotes, eg jon's. Things like '\233' or '\x3f' are not |
| 2590 | * skipped, there is never a brace in them. |
| 2591 | * Ignore this when finding matches for `'. |
| 2592 | */ |
| 2593 | case '\'': |
| 2594 | if (!cpo_match && initc != '\'' && findc != '\'') |
| 2595 | { |
| 2596 | if (backwards) |
| 2597 | { |
| 2598 | if (pos.col > 1) |
| 2599 | { |
| 2600 | if (linep[pos.col - 2] == '\'') |
| 2601 | { |
| 2602 | pos.col -= 2; |
| 2603 | break; |
| 2604 | } |
Dominique Pelle | 7765f5c | 2022-04-10 11:26:53 +0100 | [diff] [blame] | 2605 | else if (linep[pos.col - 2] == '\\' |
| 2606 | && pos.col > 2 && linep[pos.col - 3] == '\'') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2607 | { |
| 2608 | pos.col -= 3; |
| 2609 | break; |
| 2610 | } |
| 2611 | } |
| 2612 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2613 | else if (linep[pos.col + 1]) // forward search |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2614 | { |
Dominique Pelle | 7765f5c | 2022-04-10 11:26:53 +0100 | [diff] [blame] | 2615 | if (linep[pos.col + 1] == '\\' |
| 2616 | && linep[pos.col + 2] && linep[pos.col + 3] == '\'') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2617 | { |
| 2618 | pos.col += 3; |
| 2619 | break; |
| 2620 | } |
| 2621 | else if (linep[pos.col + 2] == '\'') |
| 2622 | { |
| 2623 | pos.col += 2; |
| 2624 | break; |
| 2625 | } |
| 2626 | } |
| 2627 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2628 | // FALLTHROUGH |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2629 | |
| 2630 | default: |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2631 | /* |
| 2632 | * For Lisp skip over backslashed (), {} and []. |
| 2633 | * (actually, we skip #\( et al) |
| 2634 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2635 | if (curbuf->b_p_lisp |
| 2636 | && vim_strchr((char_u *)"(){}[]", c) != NULL |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2637 | && pos.col > 1 |
| 2638 | && check_prevcol(linep, pos.col, '\\', NULL) |
| 2639 | && check_prevcol(linep, pos.col - 1, '#', NULL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2640 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2641 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2642 | // Check for match outside of quotes, and inside of |
| 2643 | // quotes when the start is also inside of quotes. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2644 | if ((!inquote || start_in_quotes == TRUE) |
| 2645 | && (c == initc || c == findc)) |
| 2646 | { |
| 2647 | int col, bslcnt = 0; |
| 2648 | |
| 2649 | if (!cpo_bsl) |
| 2650 | { |
| 2651 | for (col = pos.col; check_prevcol(linep, col, '\\', &col);) |
| 2652 | bslcnt++; |
| 2653 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2654 | // Only accept a match when 'M' is in 'cpo' or when escaping |
| 2655 | // is what we expect. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2656 | if (cpo_bsl || (bslcnt & 1) == match_escaped) |
| 2657 | { |
| 2658 | if (c == initc) |
| 2659 | count++; |
| 2660 | else |
| 2661 | { |
| 2662 | if (count == 0) |
| 2663 | return &pos; |
| 2664 | count--; |
| 2665 | } |
| 2666 | } |
| 2667 | } |
| 2668 | } |
| 2669 | } |
| 2670 | |
| 2671 | if (comment_dir == BACKWARD && count > 0) |
| 2672 | { |
| 2673 | pos = match_pos; |
| 2674 | return &pos; |
| 2675 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2676 | return (pos_T *)NULL; // never found it |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2677 | } |
| 2678 | |
| 2679 | /* |
| 2680 | * Check if line[] contains a / / comment. |
| 2681 | * Return MAXCOL if not, otherwise return the column. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2682 | */ |
Bram Moolenaar | 6e371ec | 2021-12-12 14:16:39 +0000 | [diff] [blame] | 2683 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 2684 | check_linecomment(char_u *line) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2685 | { |
| 2686 | char_u *p; |
| 2687 | |
| 2688 | p = line; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2689 | // skip Lispish one-line comments |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2690 | if (curbuf->b_p_lisp) |
| 2691 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2692 | if (vim_strchr(p, ';') != NULL) // there may be comments |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2693 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2694 | int in_str = FALSE; // inside of string |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2695 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2696 | p = line; // scan from start |
Bram Moolenaar | 520470a | 2005-06-16 21:59:56 +0000 | [diff] [blame] | 2697 | while ((p = vim_strpbrk(p, (char_u *)"\";")) != NULL) |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2698 | { |
| 2699 | if (*p == '"') |
| 2700 | { |
Bram Moolenaar | 70b2a56 | 2012-01-10 22:26:17 +0100 | [diff] [blame] | 2701 | if (in_str) |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2702 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2703 | if (*(p - 1) != '\\') // skip escaped quote |
Bram Moolenaar | 70b2a56 | 2012-01-10 22:26:17 +0100 | [diff] [blame] | 2704 | in_str = FALSE; |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2705 | } |
| 2706 | else if (p == line || ((p - line) >= 2 |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2707 | // skip #\" form |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2708 | && *(p - 1) != '\\' && *(p - 2) != '#')) |
Bram Moolenaar | 70b2a56 | 2012-01-10 22:26:17 +0100 | [diff] [blame] | 2709 | in_str = TRUE; |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2710 | } |
Bram Moolenaar | 70b2a56 | 2012-01-10 22:26:17 +0100 | [diff] [blame] | 2711 | else if (!in_str && ((p - line) < 2 |
Bram Moolenaar | ba26367 | 2021-12-29 18:09:13 +0000 | [diff] [blame] | 2712 | || (*(p - 1) != '\\' && *(p - 2) != '#')) |
| 2713 | && !is_pos_in_string(line, (colnr_T)(p - line))) |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2714 | break; // found! |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2715 | ++p; |
| 2716 | } |
| 2717 | } |
| 2718 | else |
| 2719 | p = NULL; |
| 2720 | } |
| 2721 | else |
Bram Moolenaar | 8e145b8 | 2022-05-21 20:17:31 +0100 | [diff] [blame] | 2722 | while ((p = vim_strchr(p, '/')) != NULL) |
| 2723 | { |
| 2724 | // Accept a double /, unless it's preceded with * and followed by |
| 2725 | // *, because * / / * is an end and start of a C comment. Only |
| 2726 | // accept the position if it is not inside a string. |
| 2727 | if (p[1] == '/' && (p == line || p[-1] != '*' || p[2] != '*') |
Bram Moolenaar | ba26367 | 2021-12-29 18:09:13 +0000 | [diff] [blame] | 2728 | && !is_pos_in_string(line, (colnr_T)(p - line))) |
Bram Moolenaar | 8e145b8 | 2022-05-21 20:17:31 +0100 | [diff] [blame] | 2729 | break; |
| 2730 | ++p; |
| 2731 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2732 | |
| 2733 | if (p == NULL) |
| 2734 | return MAXCOL; |
| 2735 | return (int)(p - line); |
| 2736 | } |
| 2737 | |
| 2738 | /* |
| 2739 | * Move cursor briefly to character matching the one under the cursor. |
| 2740 | * Used for Insert mode and "r" command. |
| 2741 | * Show the match only if it is visible on the screen. |
| 2742 | * If there isn't a match, then beep. |
| 2743 | */ |
| 2744 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 2745 | showmatch( |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2746 | int c) // char to show match for |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2747 | { |
| 2748 | pos_T *lpos, save_cursor; |
| 2749 | pos_T mpos; |
| 2750 | colnr_T vcol; |
| 2751 | long save_so; |
| 2752 | long save_siso; |
| 2753 | #ifdef CURSOR_SHAPE |
| 2754 | int save_state; |
| 2755 | #endif |
| 2756 | colnr_T save_dollar_vcol; |
| 2757 | char_u *p; |
Bram Moolenaar | 6ed545e | 2022-05-09 20:09:23 +0100 | [diff] [blame] | 2758 | long *so = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so; |
| 2759 | 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] | 2760 | |
| 2761 | /* |
| 2762 | * Only show match for chars in the 'matchpairs' option. |
| 2763 | */ |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2764 | // 'matchpairs' is "x:y,x:y" |
Bram Moolenaar | 8c7694a | 2013-01-17 17:02:05 +0100 | [diff] [blame] | 2765 | for (p = curbuf->b_p_mps; *p != NUL; ++p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2766 | { |
| 2767 | #ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | 187d3ac | 2013-02-20 18:39:13 +0100 | [diff] [blame] | 2768 | if (PTR2CHAR(p) == c && (curwin->w_p_rl ^ p_ri)) |
Bram Moolenaar | 8c7694a | 2013-01-17 17:02:05 +0100 | [diff] [blame] | 2769 | break; |
Bram Moolenaar | 187d3ac | 2013-02-20 18:39:13 +0100 | [diff] [blame] | 2770 | #endif |
Bram Moolenaar | 1614a14 | 2019-10-06 22:00:13 +0200 | [diff] [blame] | 2771 | p += mb_ptr2len(p) + 1; |
Bram Moolenaar | 8c7694a | 2013-01-17 17:02:05 +0100 | [diff] [blame] | 2772 | if (PTR2CHAR(p) == c |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2773 | #ifdef FEAT_RIGHTLEFT |
| 2774 | && !(curwin->w_p_rl ^ p_ri) |
| 2775 | #endif |
| 2776 | ) |
| 2777 | break; |
Bram Moolenaar | 1614a14 | 2019-10-06 22:00:13 +0200 | [diff] [blame] | 2778 | p += mb_ptr2len(p); |
Bram Moolenaar | 8c7694a | 2013-01-17 17:02:05 +0100 | [diff] [blame] | 2779 | if (*p == NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2780 | return; |
| 2781 | } |
Bram Moolenaar | 5b8cabf | 2021-04-02 18:55:57 +0200 | [diff] [blame] | 2782 | if (*p == NUL) |
| 2783 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2784 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2785 | if ((lpos = findmatch(NULL, NUL)) == NULL) // no match, so beep |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 2786 | vim_beep(BO_MATCH); |
Bram Moolenaar | 187d3ac | 2013-02-20 18:39:13 +0100 | [diff] [blame] | 2787 | else if (lpos->lnum >= curwin->w_topline && lpos->lnum < curwin->w_botline) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2788 | { |
| 2789 | if (!curwin->w_p_wrap) |
| 2790 | getvcol(curwin, lpos, NULL, &vcol, NULL); |
| 2791 | if (curwin->w_p_wrap || (vcol >= curwin->w_leftcol |
Bram Moolenaar | 0263146 | 2017-09-22 15:20:32 +0200 | [diff] [blame] | 2792 | && vcol < curwin->w_leftcol + curwin->w_width)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2793 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2794 | mpos = *lpos; // save the pos, update_screen() may change it |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2795 | save_cursor = curwin->w_cursor; |
Bram Moolenaar | 375e339 | 2019-01-31 18:26:10 +0100 | [diff] [blame] | 2796 | save_so = *so; |
| 2797 | save_siso = *siso; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2798 | // Handle "$" in 'cpo': If the ')' is typed on top of the "$", |
| 2799 | // stop displaying the "$". |
Bram Moolenaar | 76b9b36 | 2012-02-04 23:35:00 +0100 | [diff] [blame] | 2800 | if (dollar_vcol >= 0 && dollar_vcol == curwin->w_virtcol) |
| 2801 | dollar_vcol = -1; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2802 | ++curwin->w_virtcol; // do display ')' just before "$" |
Bram Moolenaar | a4d158b | 2022-08-14 14:17:45 +0100 | [diff] [blame] | 2803 | update_screen(UPD_VALID); // show the new char first |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2804 | |
| 2805 | save_dollar_vcol = dollar_vcol; |
| 2806 | #ifdef CURSOR_SHAPE |
| 2807 | save_state = State; |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 2808 | State = MODE_SHOWMATCH; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2809 | ui_cursor_shape(); // may show different cursor shape |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2810 | #endif |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2811 | curwin->w_cursor = mpos; // move to matching char |
| 2812 | *so = 0; // don't use 'scrolloff' here |
| 2813 | *siso = 0; // don't use 'sidescrolloff' here |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2814 | showruler(FALSE); |
| 2815 | setcursor(); |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2816 | cursor_on(); // make sure that the cursor is shown |
Bram Moolenaar | a338adc | 2018-01-31 20:51:47 +0100 | [diff] [blame] | 2817 | out_flush_cursor(TRUE, FALSE); |
| 2818 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2819 | // Restore dollar_vcol(), because setcursor() may call curs_rows() |
| 2820 | // which resets it if the matching position is in a previous line |
| 2821 | // and has a higher column number. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2822 | dollar_vcol = save_dollar_vcol; |
| 2823 | |
| 2824 | /* |
| 2825 | * brief pause, unless 'm' is present in 'cpo' and a character is |
| 2826 | * available. |
| 2827 | */ |
| 2828 | if (vim_strchr(p_cpo, CPO_SHOWMATCH) != NULL) |
Bram Moolenaar | eda1da0 | 2019-11-17 17:06:33 +0100 | [diff] [blame] | 2829 | ui_delay(p_mat * 100L + 8, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2830 | else if (!char_avail()) |
Bram Moolenaar | eda1da0 | 2019-11-17 17:06:33 +0100 | [diff] [blame] | 2831 | ui_delay(p_mat * 100L + 9, FALSE); |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2832 | curwin->w_cursor = save_cursor; // restore cursor position |
Bram Moolenaar | 375e339 | 2019-01-31 18:26:10 +0100 | [diff] [blame] | 2833 | *so = save_so; |
| 2834 | *siso = save_siso; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2835 | #ifdef CURSOR_SHAPE |
| 2836 | State = save_state; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2837 | ui_cursor_shape(); // may show different cursor shape |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2838 | #endif |
| 2839 | } |
| 2840 | } |
| 2841 | } |
| 2842 | |
| 2843 | /* |
Bram Moolenaar | 453c192 | 2019-10-26 14:42:09 +0200 | [diff] [blame] | 2844 | * Check if the pattern is zero-width. |
Bram Moolenaar | edaad6e | 2019-10-24 15:23:37 +0200 | [diff] [blame] | 2845 | * If move is TRUE, check from the beginning of the buffer, else from position |
| 2846 | * "cur". |
| 2847 | * "direction" is FORWARD or BACKWARD. |
| 2848 | * Returns TRUE, FALSE or -1 for failure. |
| 2849 | */ |
| 2850 | static int |
| 2851 | is_zero_width(char_u *pattern, int move, pos_T *cur, int direction) |
| 2852 | { |
| 2853 | regmmatch_T regmatch; |
| 2854 | int nmatched = 0; |
| 2855 | int result = -1; |
| 2856 | pos_T pos; |
Bram Moolenaar | 5398955 | 2019-12-23 22:59:18 +0100 | [diff] [blame] | 2857 | int called_emsg_before = called_emsg; |
Bram Moolenaar | edaad6e | 2019-10-24 15:23:37 +0200 | [diff] [blame] | 2858 | int flag = 0; |
| 2859 | |
| 2860 | if (pattern == NULL) |
| 2861 | pattern = spats[last_idx].pat; |
| 2862 | |
| 2863 | if (search_regcomp(pattern, RE_SEARCH, RE_SEARCH, |
| 2864 | SEARCH_KEEP, ®match) == FAIL) |
| 2865 | return -1; |
| 2866 | |
| 2867 | // init startcol correctly |
| 2868 | regmatch.startpos[0].col = -1; |
| 2869 | // move to match |
| 2870 | if (move) |
| 2871 | { |
| 2872 | CLEAR_POS(&pos); |
| 2873 | } |
| 2874 | else |
| 2875 | { |
| 2876 | pos = *cur; |
| 2877 | // accept a match at the cursor position |
| 2878 | flag = SEARCH_START; |
| 2879 | } |
| 2880 | |
| 2881 | if (searchit(curwin, curbuf, &pos, NULL, direction, pattern, 1, |
| 2882 | SEARCH_KEEP + flag, RE_SEARCH, NULL) != FAIL) |
| 2883 | { |
| 2884 | // Zero-width pattern should match somewhere, then we can check if |
| 2885 | // start and end are in the same position. |
Bram Moolenaar | edaad6e | 2019-10-24 15:23:37 +0200 | [diff] [blame] | 2886 | do |
| 2887 | { |
| 2888 | regmatch.startpos[0].col++; |
| 2889 | nmatched = vim_regexec_multi(®match, curwin, curbuf, |
Paul Ollis | 6574577 | 2022-06-05 16:55:54 +0100 | [diff] [blame] | 2890 | pos.lnum, regmatch.startpos[0].col, NULL); |
Bram Moolenaar | edaad6e | 2019-10-24 15:23:37 +0200 | [diff] [blame] | 2891 | if (nmatched != 0) |
| 2892 | break; |
Bram Moolenaar | 795aaa1 | 2020-10-02 20:36:01 +0200 | [diff] [blame] | 2893 | } while (regmatch.regprog != NULL |
| 2894 | && direction == FORWARD ? regmatch.startpos[0].col < pos.col |
Bram Moolenaar | edaad6e | 2019-10-24 15:23:37 +0200 | [diff] [blame] | 2895 | : regmatch.startpos[0].col > pos.col); |
| 2896 | |
Bram Moolenaar | 5398955 | 2019-12-23 22:59:18 +0100 | [diff] [blame] | 2897 | if (called_emsg == called_emsg_before) |
Bram Moolenaar | edaad6e | 2019-10-24 15:23:37 +0200 | [diff] [blame] | 2898 | { |
| 2899 | result = (nmatched != 0 |
| 2900 | && regmatch.startpos[0].lnum == regmatch.endpos[0].lnum |
| 2901 | && regmatch.startpos[0].col == regmatch.endpos[0].col); |
| 2902 | } |
| 2903 | } |
| 2904 | |
Bram Moolenaar | edaad6e | 2019-10-24 15:23:37 +0200 | [diff] [blame] | 2905 | vim_regfree(regmatch.regprog); |
| 2906 | return result; |
| 2907 | } |
| 2908 | |
Bram Moolenaar | dde0efe | 2012-08-23 15:53:05 +0200 | [diff] [blame] | 2909 | |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 2910 | /* |
| 2911 | * Find next search match under cursor, cursor at end. |
| 2912 | * Used while an operator is pending, and in Visual mode. |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 2913 | */ |
| 2914 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 2915 | current_search( |
| 2916 | long count, |
Bram Moolenaar | 5d24a22 | 2018-12-23 19:10:09 +0100 | [diff] [blame] | 2917 | int forward) // TRUE for forward, FALSE for backward |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 2918 | { |
Bram Moolenaar | 5d24a22 | 2018-12-23 19:10:09 +0100 | [diff] [blame] | 2919 | pos_T start_pos; // start position of the pattern match |
| 2920 | pos_T end_pos; // end position of the pattern match |
| 2921 | pos_T orig_pos; // position of the cursor at beginning |
| 2922 | pos_T pos; // position after the pattern |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 2923 | int i; |
| 2924 | int dir; |
Bram Moolenaar | 5d24a22 | 2018-12-23 19:10:09 +0100 | [diff] [blame] | 2925 | int result; // result of various function calls |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 2926 | char_u old_p_ws = p_ws; |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 2927 | int flags = 0; |
Bram Moolenaar | de9149e | 2013-07-17 19:22:13 +0200 | [diff] [blame] | 2928 | pos_T save_VIsual = VIsual; |
Bram Moolenaar | edaad6e | 2019-10-24 15:23:37 +0200 | [diff] [blame] | 2929 | int zero_width; |
Bram Moolenaar | c07b7f7 | 2020-10-11 20:44:15 +0200 | [diff] [blame] | 2930 | int skip_first_backward; |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 2931 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2932 | // Correct cursor when 'selection' is exclusive |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 2933 | if (VIsual_active && *p_sel == 'e' && LT_POS(VIsual, curwin->w_cursor)) |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 2934 | dec_cursor(); |
| 2935 | |
Bram Moolenaar | c07b7f7 | 2020-10-11 20:44:15 +0200 | [diff] [blame] | 2936 | // When searching forward and the cursor is at the start of the Visual |
| 2937 | // area, skip the first search backward, otherwise it doesn't move. |
| 2938 | skip_first_backward = forward && VIsual_active |
| 2939 | && LT_POS(curwin->w_cursor, VIsual); |
| 2940 | |
Bram Moolenaar | edaad6e | 2019-10-24 15:23:37 +0200 | [diff] [blame] | 2941 | orig_pos = pos = curwin->w_cursor; |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 2942 | if (VIsual_active) |
| 2943 | { |
Bram Moolenaar | edaad6e | 2019-10-24 15:23:37 +0200 | [diff] [blame] | 2944 | if (forward) |
| 2945 | incl(&pos); |
| 2946 | else |
| 2947 | decl(&pos); |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 2948 | } |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 2949 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2950 | // Is the pattern is zero-width?, this time, don't care about the direction |
Bram Moolenaar | edaad6e | 2019-10-24 15:23:37 +0200 | [diff] [blame] | 2951 | zero_width = is_zero_width(spats[last_idx].pat, TRUE, &curwin->w_cursor, |
Bram Moolenaar | 22ab547 | 2017-09-26 12:28:45 +0200 | [diff] [blame] | 2952 | FORWARD); |
Bram Moolenaar | edaad6e | 2019-10-24 15:23:37 +0200 | [diff] [blame] | 2953 | if (zero_width == -1) |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2954 | return FAIL; // pattern not found |
Bram Moolenaar | ba6ba36 | 2012-08-08 15:27:57 +0200 | [diff] [blame] | 2955 | |
Bram Moolenaar | ba6ba36 | 2012-08-08 15:27:57 +0200 | [diff] [blame] | 2956 | /* |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 2957 | * The trick is to first search backwards and then search forward again, |
| 2958 | * so that a match at the current cursor position will be correctly |
Bram Moolenaar | c07b7f7 | 2020-10-11 20:44:15 +0200 | [diff] [blame] | 2959 | * captured. When "forward" is false do it the other way around. |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 2960 | */ |
| 2961 | for (i = 0; i < 2; i++) |
| 2962 | { |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 2963 | if (forward) |
Bram Moolenaar | c07b7f7 | 2020-10-11 20:44:15 +0200 | [diff] [blame] | 2964 | { |
| 2965 | if (i == 0 && skip_first_backward) |
| 2966 | continue; |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 2967 | dir = i; |
Bram Moolenaar | c07b7f7 | 2020-10-11 20:44:15 +0200 | [diff] [blame] | 2968 | } |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 2969 | else |
| 2970 | dir = !i; |
Bram Moolenaar | ba6ba36 | 2012-08-08 15:27:57 +0200 | [diff] [blame] | 2971 | |
| 2972 | flags = 0; |
Bram Moolenaar | edaad6e | 2019-10-24 15:23:37 +0200 | [diff] [blame] | 2973 | if (!dir && !zero_width) |
Bram Moolenaar | ba6ba36 | 2012-08-08 15:27:57 +0200 | [diff] [blame] | 2974 | flags = SEARCH_END; |
Bram Moolenaar | 5d24a22 | 2018-12-23 19:10:09 +0100 | [diff] [blame] | 2975 | end_pos = pos; |
Bram Moolenaar | ba6ba36 | 2012-08-08 15:27:57 +0200 | [diff] [blame] | 2976 | |
Bram Moolenaar | 82cf7f6 | 2019-11-02 23:22:47 +0100 | [diff] [blame] | 2977 | // wrapping should not occur in the first round |
| 2978 | if (i == 0) |
| 2979 | p_ws = FALSE; |
| 2980 | |
Bram Moolenaar | 5d24a22 | 2018-12-23 19:10:09 +0100 | [diff] [blame] | 2981 | result = searchit(curwin, curbuf, &pos, &end_pos, |
| 2982 | (dir ? FORWARD : BACKWARD), |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 2983 | spats[last_idx].pat, (long) (i ? count : 1), |
Bram Moolenaar | 92ea26b | 2019-10-18 20:53:34 +0200 | [diff] [blame] | 2984 | SEARCH_KEEP | flags, RE_SEARCH, NULL); |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 2985 | |
Bram Moolenaar | 82cf7f6 | 2019-11-02 23:22:47 +0100 | [diff] [blame] | 2986 | p_ws = old_p_ws; |
| 2987 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 2988 | // First search may fail, but then start searching from the |
| 2989 | // beginning of the file (cursor might be on the search match) |
| 2990 | // except when Visual mode is active, so that extending the visual |
| 2991 | // selection works. |
| 2992 | if (i == 1 && !result) // not found, abort |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 2993 | { |
| 2994 | curwin->w_cursor = orig_pos; |
| 2995 | if (VIsual_active) |
| 2996 | VIsual = save_VIsual; |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 2997 | return FAIL; |
| 2998 | } |
Bram Moolenaar | 5d24a22 | 2018-12-23 19:10:09 +0100 | [diff] [blame] | 2999 | else if (i == 0 && !result) |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3000 | { |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 3001 | if (forward) |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3002 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3003 | // try again from start of buffer |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 3004 | CLEAR_POS(&pos); |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3005 | } |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 3006 | else |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3007 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3008 | // try again from end of buffer |
| 3009 | // searching backwards, so set pos to last line and col |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3010 | pos.lnum = curwin->w_buffer->b_ml.ml_line_count; |
Bram Moolenaar | 09168a7 | 2012-08-02 21:24:42 +0200 | [diff] [blame] | 3011 | pos.col = (colnr_T)STRLEN( |
| 3012 | ml_get(curwin->w_buffer->b_ml.ml_line_count)); |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3013 | } |
| 3014 | } |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3015 | } |
| 3016 | |
| 3017 | start_pos = pos; |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3018 | |
| 3019 | if (!VIsual_active) |
| 3020 | VIsual = start_pos; |
| 3021 | |
Bram Moolenaar | c07b7f7 | 2020-10-11 20:44:15 +0200 | [diff] [blame] | 3022 | // put the cursor after the match |
Bram Moolenaar | 5d24a22 | 2018-12-23 19:10:09 +0100 | [diff] [blame] | 3023 | curwin->w_cursor = end_pos; |
Bram Moolenaar | 453c192 | 2019-10-26 14:42:09 +0200 | [diff] [blame] | 3024 | if (LT_POS(VIsual, end_pos) && forward) |
Bram Moolenaar | c07b7f7 | 2020-10-11 20:44:15 +0200 | [diff] [blame] | 3025 | { |
| 3026 | if (skip_first_backward) |
| 3027 | // put the cursor on the start of the match |
| 3028 | curwin->w_cursor = pos; |
| 3029 | else |
| 3030 | // put the cursor on last character of match |
| 3031 | dec_cursor(); |
| 3032 | } |
Bram Moolenaar | 28f224b | 2020-10-10 16:45:25 +0200 | [diff] [blame] | 3033 | else if (VIsual_active && LT_POS(curwin->w_cursor, VIsual) && forward) |
Bram Moolenaar | edaad6e | 2019-10-24 15:23:37 +0200 | [diff] [blame] | 3034 | 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] | 3035 | VIsual_active = TRUE; |
| 3036 | VIsual_mode = 'v'; |
| 3037 | |
Bram Moolenaar | b763361 | 2019-02-10 21:48:25 +0100 | [diff] [blame] | 3038 | if (*p_sel == 'e') |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3039 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3040 | // Correction for exclusive selection depends on the direction. |
Bram Moolenaar | b763361 | 2019-02-10 21:48:25 +0100 | [diff] [blame] | 3041 | if (forward && LTOREQ_POS(VIsual, curwin->w_cursor)) |
| 3042 | inc_cursor(); |
| 3043 | else if (!forward && LTOREQ_POS(curwin->w_cursor, VIsual)) |
| 3044 | inc(&VIsual); |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3045 | } |
| 3046 | |
| 3047 | #ifdef FEAT_FOLDING |
| 3048 | if (fdo_flags & FDO_SEARCH && KeyTyped) |
| 3049 | foldOpenCursor(); |
| 3050 | #endif |
| 3051 | |
| 3052 | may_start_select('c'); |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3053 | setmouse(); |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3054 | #ifdef FEAT_CLIPBOARD |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3055 | // Make sure the clipboard gets updated. Needed because start and |
| 3056 | // end are still the same, and the selection needs to be owned |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3057 | clip_star.vmode = NUL; |
| 3058 | #endif |
Bram Moolenaar | a4d158b | 2022-08-14 14:17:45 +0100 | [diff] [blame] | 3059 | redraw_curbuf_later(UPD_INVERTED); |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 3060 | showmode(); |
| 3061 | |
| 3062 | return OK; |
| 3063 | } |
Bram Moolenaar | dde0efe | 2012-08-23 15:53:05 +0200 | [diff] [blame] | 3064 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3065 | /* |
| 3066 | * return TRUE if line 'lnum' is empty or has white chars only. |
| 3067 | */ |
| 3068 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 3069 | linewhite(linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3070 | { |
| 3071 | char_u *p; |
| 3072 | |
| 3073 | p = skipwhite(ml_get(lnum)); |
| 3074 | return (*p == NUL); |
| 3075 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3076 | |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 3077 | /* |
| 3078 | * Add the search count "[3/19]" to "msgbuf". |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 3079 | * See update_search_stat() for other arguments. |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 3080 | */ |
| 3081 | static void |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 3082 | cmdline_search_stat( |
| 3083 | int dirc, |
| 3084 | pos_T *pos, |
| 3085 | pos_T *cursor_pos, |
| 3086 | int show_top_bot_msg, |
| 3087 | char_u *msgbuf, |
| 3088 | int recompute, |
| 3089 | int maxcount, |
| 3090 | long timeout) |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 3091 | { |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 3092 | searchstat_T stat; |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 3093 | |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 3094 | update_search_stat(dirc, pos, cursor_pos, &stat, recompute, maxcount, |
| 3095 | timeout); |
| 3096 | if (stat.cur > 0) |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 3097 | { |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 3098 | char t[SEARCH_STAT_BUF_LEN]; |
Bram Moolenaar | e2ad826 | 2019-05-24 13:22:22 +0200 | [diff] [blame] | 3099 | size_t len; |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 3100 | |
| 3101 | #ifdef FEAT_RIGHTLEFT |
| 3102 | if (curwin->w_p_rl && *curwin->w_p_rlc == 's') |
| 3103 | { |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 3104 | if (stat.incomplete == 1) |
Bram Moolenaar | b6cb26f | 2019-05-07 21:34:37 +0200 | [diff] [blame] | 3105 | vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[?/??]"); |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 3106 | else if (stat.cnt > maxcount && stat.cur > maxcount) |
| 3107 | vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/>%d]", |
| 3108 | maxcount, maxcount); |
| 3109 | else if (stat.cnt > maxcount) |
| 3110 | vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/%d]", |
| 3111 | maxcount, stat.cur); |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 3112 | else |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 3113 | vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/%d]", |
| 3114 | stat.cnt, stat.cur); |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 3115 | } |
| 3116 | else |
| 3117 | #endif |
| 3118 | { |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 3119 | if (stat.incomplete == 1) |
Bram Moolenaar | b6cb26f | 2019-05-07 21:34:37 +0200 | [diff] [blame] | 3120 | vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[?/??]"); |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 3121 | else if (stat.cnt > maxcount && stat.cur > maxcount) |
| 3122 | vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>%d/>%d]", |
| 3123 | maxcount, maxcount); |
| 3124 | else if (stat.cnt > maxcount) |
| 3125 | vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/>%d]", |
| 3126 | stat.cur, maxcount); |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 3127 | else |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 3128 | vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/%d]", |
| 3129 | stat.cur, stat.cnt); |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 3130 | } |
Bram Moolenaar | c7a10b3 | 2019-05-06 21:37:18 +0200 | [diff] [blame] | 3131 | |
| 3132 | len = STRLEN(t); |
Bram Moolenaar | dc6855a | 2019-05-18 19:26:29 +0200 | [diff] [blame] | 3133 | if (show_top_bot_msg && len + 2 < SEARCH_STAT_BUF_LEN) |
Bram Moolenaar | c7a10b3 | 2019-05-06 21:37:18 +0200 | [diff] [blame] | 3134 | { |
Bram Moolenaar | 16b58ae | 2019-09-06 20:40:21 +0200 | [diff] [blame] | 3135 | mch_memmove(t + 2, t, len); |
| 3136 | t[0] = 'W'; |
| 3137 | t[1] = ' '; |
Bram Moolenaar | c7a10b3 | 2019-05-06 21:37:18 +0200 | [diff] [blame] | 3138 | len += 2; |
| 3139 | } |
| 3140 | |
| 3141 | mch_memmove(msgbuf + STRLEN(msgbuf) - len, t, len); |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 3142 | if (dirc == '?' && stat.cur == maxcount + 1) |
| 3143 | stat.cur = -1; |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 3144 | |
Bram Moolenaar | 984f031 | 2019-05-24 13:11:47 +0200 | [diff] [blame] | 3145 | // keep the message even after redraw, but don't put in history |
| 3146 | msg_hist_off = TRUE; |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 3147 | give_warning(msgbuf, FALSE); |
Bram Moolenaar | 984f031 | 2019-05-24 13:11:47 +0200 | [diff] [blame] | 3148 | msg_hist_off = FALSE; |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 3149 | } |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 3150 | } |
| 3151 | |
| 3152 | /* |
| 3153 | * Add the search count information to "stat". |
| 3154 | * "stat" must not be NULL. |
| 3155 | * When "recompute" is TRUE always recompute the numbers. |
| 3156 | * dirc == 0: don't find the next/previous match (only set the result to "stat") |
| 3157 | * dirc == '/': find the next match |
| 3158 | * dirc == '?': find the previous match |
| 3159 | */ |
| 3160 | static void |
| 3161 | update_search_stat( |
| 3162 | int dirc, |
| 3163 | pos_T *pos, |
| 3164 | pos_T *cursor_pos, |
| 3165 | searchstat_T *stat, |
| 3166 | int recompute, |
| 3167 | int maxcount, |
Bram Moolenaar | f9ca08e | 2020-06-01 18:56:03 +0200 | [diff] [blame] | 3168 | long timeout UNUSED) |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 3169 | { |
| 3170 | int save_ws = p_ws; |
| 3171 | int wraparound = FALSE; |
| 3172 | pos_T p = (*pos); |
Bram Moolenaar | 1468162 | 2020-06-03 22:57:39 +0200 | [diff] [blame] | 3173 | static pos_T lastpos = {0, 0, 0}; |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 3174 | static int cur = 0; |
| 3175 | static int cnt = 0; |
| 3176 | static int exact_match = FALSE; |
| 3177 | static int incomplete = 0; |
| 3178 | static int last_maxcount = SEARCH_STAT_DEF_MAX_COUNT; |
| 3179 | static int chgtick = 0; |
| 3180 | static char_u *lastpat = NULL; |
| 3181 | static buf_T *lbuf = NULL; |
| 3182 | #ifdef FEAT_RELTIME |
| 3183 | proftime_T start; |
| 3184 | #endif |
| 3185 | |
| 3186 | vim_memset(stat, 0, sizeof(searchstat_T)); |
| 3187 | |
| 3188 | if (dirc == 0 && !recompute && !EMPTY_POS(lastpos)) |
| 3189 | { |
| 3190 | stat->cur = cur; |
| 3191 | stat->cnt = cnt; |
| 3192 | stat->exact_match = exact_match; |
| 3193 | stat->incomplete = incomplete; |
| 3194 | stat->last_maxcount = last_maxcount; |
| 3195 | return; |
| 3196 | } |
| 3197 | last_maxcount = maxcount; |
| 3198 | |
| 3199 | wraparound = ((dirc == '?' && LT_POS(lastpos, p)) |
| 3200 | || (dirc == '/' && LT_POS(p, lastpos))); |
| 3201 | |
| 3202 | // If anything relevant changed the count has to be recomputed. |
| 3203 | // MB_STRNICMP ignores case, but we should not ignore case. |
| 3204 | // Unfortunately, there is no MB_STRNICMP function. |
| 3205 | // XXX: above comment should be "no MB_STRCMP function" ? |
| 3206 | if (!(chgtick == CHANGEDTICK(curbuf) |
| 3207 | && MB_STRNICMP(lastpat, spats[last_idx].pat, STRLEN(lastpat)) == 0 |
| 3208 | && STRLEN(lastpat) == STRLEN(spats[last_idx].pat) |
| 3209 | && EQUAL_POS(lastpos, *cursor_pos) |
| 3210 | && lbuf == curbuf) || wraparound || cur < 0 |
| 3211 | || (maxcount > 0 && cur > maxcount) || recompute) |
| 3212 | { |
| 3213 | cur = 0; |
| 3214 | cnt = 0; |
| 3215 | exact_match = FALSE; |
| 3216 | incomplete = 0; |
| 3217 | CLEAR_POS(&lastpos); |
| 3218 | lbuf = curbuf; |
| 3219 | } |
| 3220 | |
| 3221 | if (EQUAL_POS(lastpos, *cursor_pos) && !wraparound |
| 3222 | && (dirc == 0 || dirc == '/' ? cur < cnt : cur > 0)) |
| 3223 | cur += dirc == 0 ? 0 : dirc == '/' ? 1 : -1; |
| 3224 | else |
| 3225 | { |
| 3226 | int done_search = FALSE; |
| 3227 | pos_T endpos = {0, 0, 0}; |
| 3228 | |
| 3229 | p_ws = FALSE; |
| 3230 | #ifdef FEAT_RELTIME |
| 3231 | if (timeout > 0) |
| 3232 | profile_setlimit(timeout, &start); |
| 3233 | #endif |
| 3234 | while (!got_int && searchit(curwin, curbuf, &lastpos, &endpos, |
| 3235 | FORWARD, NULL, 1, SEARCH_KEEP, RE_LAST, NULL) != FAIL) |
| 3236 | { |
| 3237 | done_search = TRUE; |
| 3238 | #ifdef FEAT_RELTIME |
| 3239 | // Stop after passing the time limit. |
| 3240 | if (timeout > 0 && profile_passed_limit(&start)) |
| 3241 | { |
| 3242 | incomplete = 1; |
| 3243 | break; |
| 3244 | } |
| 3245 | #endif |
| 3246 | cnt++; |
| 3247 | if (LTOREQ_POS(lastpos, p)) |
| 3248 | { |
| 3249 | cur = cnt; |
Bram Moolenaar | 57f75a5 | 2020-06-02 22:06:21 +0200 | [diff] [blame] | 3250 | if (LT_POS(p, endpos)) |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 3251 | exact_match = TRUE; |
| 3252 | } |
| 3253 | fast_breakcheck(); |
| 3254 | if (maxcount > 0 && cnt > maxcount) |
| 3255 | { |
| 3256 | incomplete = 2; // max count exceeded |
| 3257 | break; |
| 3258 | } |
| 3259 | } |
| 3260 | if (got_int) |
| 3261 | cur = -1; // abort |
| 3262 | if (done_search) |
| 3263 | { |
| 3264 | vim_free(lastpat); |
| 3265 | lastpat = vim_strsave(spats[last_idx].pat); |
| 3266 | chgtick = CHANGEDTICK(curbuf); |
| 3267 | lbuf = curbuf; |
| 3268 | lastpos = p; |
| 3269 | } |
| 3270 | } |
| 3271 | stat->cur = cur; |
| 3272 | stat->cnt = cnt; |
| 3273 | stat->exact_match = exact_match; |
| 3274 | stat->incomplete = incomplete; |
| 3275 | stat->last_maxcount = last_maxcount; |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 3276 | p_ws = save_ws; |
| 3277 | } |
| 3278 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3279 | #if defined(FEAT_FIND_ID) || defined(PROTO) |
Bram Moolenaar | 409510c | 2022-06-01 15:23:13 +0100 | [diff] [blame] | 3280 | |
| 3281 | /* |
| 3282 | * Get line "lnum" and copy it into "buf[LSIZE]". |
| 3283 | * The copy is made because the regexp may make the line invalid when using a |
| 3284 | * mark. |
| 3285 | */ |
| 3286 | static char_u * |
| 3287 | get_line_and_copy(linenr_T lnum, char_u *buf) |
| 3288 | { |
| 3289 | char_u *line = ml_get(lnum); |
| 3290 | |
| 3291 | vim_strncpy(buf, line, LSIZE - 1); |
| 3292 | return buf; |
| 3293 | } |
| 3294 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3295 | /* |
| 3296 | * Find identifiers or defines in included files. |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3297 | * If p_ic && compl_status_sol() then ptr must be in lowercase. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3298 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3299 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 3300 | find_pattern_in_path( |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3301 | char_u *ptr, // pointer to search pattern |
| 3302 | int dir UNUSED, // direction of expansion |
| 3303 | int len, // length of search pattern |
| 3304 | int whole, // match whole words only |
| 3305 | int skip_comments, // don't match inside comments |
| 3306 | int type, // Type of search; are we looking for a type? |
| 3307 | // a macro? |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 3308 | long count, |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3309 | int action, // What to do when we find it |
| 3310 | linenr_T start_lnum, // first line to start searching |
| 3311 | linenr_T end_lnum) // last line for searching |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3312 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3313 | SearchedFile *files; // Stack of included files |
| 3314 | SearchedFile *bigger; // When we need more space |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3315 | int max_path_depth = 50; |
| 3316 | long match_count = 1; |
| 3317 | |
| 3318 | char_u *pat; |
| 3319 | char_u *new_fname; |
| 3320 | char_u *curr_fname = curbuf->b_fname; |
| 3321 | char_u *prev_fname = NULL; |
| 3322 | linenr_T lnum; |
| 3323 | int depth; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3324 | int depth_displayed; // For type==CHECK_PATH |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3325 | int old_files; |
| 3326 | int already_searched; |
| 3327 | char_u *file_line; |
| 3328 | char_u *line; |
| 3329 | char_u *p; |
| 3330 | char_u save_char; |
| 3331 | int define_matched; |
| 3332 | regmatch_T regmatch; |
| 3333 | regmatch_T incl_regmatch; |
| 3334 | regmatch_T def_regmatch; |
| 3335 | int matched = FALSE; |
| 3336 | int did_show = FALSE; |
| 3337 | int found = FALSE; |
| 3338 | int i; |
| 3339 | char_u *already = NULL; |
| 3340 | char_u *startp = NULL; |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 3341 | char_u *inc_opt = NULL; |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 3342 | #if defined(FEAT_QUICKFIX) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3343 | win_T *curwin_save = NULL; |
| 3344 | #endif |
| 3345 | |
| 3346 | regmatch.regprog = NULL; |
| 3347 | incl_regmatch.regprog = NULL; |
| 3348 | def_regmatch.regprog = NULL; |
| 3349 | |
| 3350 | file_line = alloc(LSIZE); |
| 3351 | if (file_line == NULL) |
| 3352 | return; |
| 3353 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3354 | if (type != CHECK_PATH && type != FIND_DEFINE |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3355 | // when CONT_SOL is set compare "ptr" with the beginning of the |
| 3356 | // line is faster than quote_meta/regcomp/regexec "ptr" -- Acevedo |
| 3357 | && !compl_status_sol()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3358 | { |
| 3359 | pat = alloc(len + 5); |
| 3360 | if (pat == NULL) |
| 3361 | goto fpip_end; |
| 3362 | sprintf((char *)pat, whole ? "\\<%.*s\\>" : "%.*s", len, ptr); |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3363 | // ignore case according to p_ic, p_scs and pat |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3364 | regmatch.rm_ic = ignorecase(pat); |
Bram Moolenaar | f4e2099 | 2020-12-21 19:59:08 +0100 | [diff] [blame] | 3365 | regmatch.regprog = vim_regcomp(pat, magic_isset() ? RE_MAGIC : 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3366 | vim_free(pat); |
| 3367 | if (regmatch.regprog == NULL) |
| 3368 | goto fpip_end; |
| 3369 | } |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 3370 | inc_opt = (*curbuf->b_p_inc == NUL) ? p_inc : curbuf->b_p_inc; |
| 3371 | if (*inc_opt != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3372 | { |
Bram Moolenaar | f4e2099 | 2020-12-21 19:59:08 +0100 | [diff] [blame] | 3373 | incl_regmatch.regprog = vim_regcomp(inc_opt, |
| 3374 | magic_isset() ? RE_MAGIC : 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3375 | if (incl_regmatch.regprog == NULL) |
| 3376 | goto fpip_end; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3377 | incl_regmatch.rm_ic = FALSE; // don't ignore case in incl. pat. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3378 | } |
| 3379 | if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL)) |
| 3380 | { |
| 3381 | def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL |
Bram Moolenaar | f4e2099 | 2020-12-21 19:59:08 +0100 | [diff] [blame] | 3382 | ? p_def : curbuf->b_p_def, |
| 3383 | magic_isset() ? RE_MAGIC : 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3384 | if (def_regmatch.regprog == NULL) |
| 3385 | goto fpip_end; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3386 | def_regmatch.rm_ic = FALSE; // don't ignore case in define pat. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3387 | } |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 3388 | files = lalloc_clear(max_path_depth * sizeof(SearchedFile), TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3389 | if (files == NULL) |
| 3390 | goto fpip_end; |
| 3391 | old_files = max_path_depth; |
| 3392 | depth = depth_displayed = -1; |
| 3393 | |
| 3394 | lnum = start_lnum; |
| 3395 | if (end_lnum > curbuf->b_ml.ml_line_count) |
| 3396 | end_lnum = curbuf->b_ml.ml_line_count; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3397 | if (lnum > end_lnum) // do at least one line |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3398 | lnum = end_lnum; |
Bram Moolenaar | 409510c | 2022-06-01 15:23:13 +0100 | [diff] [blame] | 3399 | line = get_line_and_copy(lnum, file_line); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3400 | |
| 3401 | for (;;) |
| 3402 | { |
| 3403 | if (incl_regmatch.regprog != NULL |
| 3404 | && vim_regexec(&incl_regmatch, line, (colnr_T)0)) |
| 3405 | { |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 3406 | char_u *p_fname = (curr_fname == curbuf->b_fname) |
| 3407 | ? curbuf->b_ffname : curr_fname; |
| 3408 | |
| 3409 | if (inc_opt != NULL && strstr((char *)inc_opt, "\\zs") != NULL) |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3410 | // Use text from '\zs' to '\ze' (or end) of 'include'. |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 3411 | new_fname = find_file_name_in_path(incl_regmatch.startp[0], |
Bram Moolenaar | c84e3c1 | 2013-07-03 22:28:36 +0200 | [diff] [blame] | 3412 | (int)(incl_regmatch.endp[0] - incl_regmatch.startp[0]), |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 3413 | FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname); |
| 3414 | else |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3415 | // Use text after match with 'include'. |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 3416 | new_fname = file_name_in_line(incl_regmatch.endp[0], 0, |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 3417 | FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3418 | already_searched = FALSE; |
| 3419 | if (new_fname != NULL) |
| 3420 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3421 | // Check whether we have already searched in this file |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3422 | for (i = 0;; i++) |
| 3423 | { |
| 3424 | if (i == depth + 1) |
| 3425 | i = old_files; |
| 3426 | if (i == max_path_depth) |
| 3427 | break; |
Bram Moolenaar | 99499b1 | 2019-05-23 21:35:48 +0200 | [diff] [blame] | 3428 | if (fullpathcmp(new_fname, files[i].name, TRUE, TRUE) |
| 3429 | & FPC_SAME) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3430 | { |
Dominique Pelle | 7765f5c | 2022-04-10 11:26:53 +0100 | [diff] [blame] | 3431 | if (type != CHECK_PATH |
| 3432 | && action == ACTION_SHOW_ALL |
| 3433 | && files[i].matched) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3434 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3435 | msg_putchar('\n'); // cursor below last one |
| 3436 | if (!got_int) // don't display if 'q' |
| 3437 | // typed at "--more--" |
| 3438 | // message |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3439 | { |
| 3440 | msg_home_replace_hl(new_fname); |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 3441 | msg_puts(_(" (includes previously listed match)")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3442 | prev_fname = NULL; |
| 3443 | } |
| 3444 | } |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 3445 | VIM_CLEAR(new_fname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3446 | already_searched = TRUE; |
| 3447 | break; |
| 3448 | } |
| 3449 | } |
| 3450 | } |
| 3451 | |
| 3452 | if (type == CHECK_PATH && (action == ACTION_SHOW_ALL |
| 3453 | || (new_fname == NULL && !already_searched))) |
| 3454 | { |
| 3455 | if (did_show) |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3456 | msg_putchar('\n'); // cursor below last one |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3457 | else |
| 3458 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3459 | gotocmdline(TRUE); // cursor at status line |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 3460 | msg_puts_title(_("--- Included files ")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3461 | if (action != ACTION_SHOW_ALL) |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 3462 | msg_puts_title(_("not found ")); |
| 3463 | msg_puts_title(_("in path ---\n")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3464 | } |
| 3465 | did_show = TRUE; |
| 3466 | while (depth_displayed < depth && !got_int) |
| 3467 | { |
| 3468 | ++depth_displayed; |
| 3469 | for (i = 0; i < depth_displayed; i++) |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 3470 | msg_puts(" "); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3471 | msg_home_replace(files[depth_displayed].name); |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 3472 | msg_puts(" -->\n"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3473 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3474 | if (!got_int) // don't display if 'q' typed |
| 3475 | // for "--more--" message |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3476 | { |
| 3477 | for (i = 0; i <= depth_displayed; i++) |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 3478 | msg_puts(" "); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3479 | if (new_fname != NULL) |
| 3480 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3481 | // using "new_fname" is more reliable, e.g., when |
| 3482 | // 'includeexpr' is set. |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 3483 | msg_outtrans_attr(new_fname, HL_ATTR(HLF_D)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3484 | } |
| 3485 | else |
| 3486 | { |
| 3487 | /* |
| 3488 | * Isolate the file name. |
| 3489 | * Include the surrounding "" or <> if present. |
| 3490 | */ |
Bram Moolenaar | 058bdcf | 2012-07-25 13:46:30 +0200 | [diff] [blame] | 3491 | if (inc_opt != NULL |
| 3492 | && strstr((char *)inc_opt, "\\zs") != NULL) |
| 3493 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3494 | // pattern contains \zs, use the match |
Bram Moolenaar | 058bdcf | 2012-07-25 13:46:30 +0200 | [diff] [blame] | 3495 | p = incl_regmatch.startp[0]; |
| 3496 | i = (int)(incl_regmatch.endp[0] |
| 3497 | - incl_regmatch.startp[0]); |
| 3498 | } |
| 3499 | else |
| 3500 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3501 | // find the file name after the end of the match |
Bram Moolenaar | 058bdcf | 2012-07-25 13:46:30 +0200 | [diff] [blame] | 3502 | for (p = incl_regmatch.endp[0]; |
| 3503 | *p && !vim_isfilec(*p); p++) |
| 3504 | ; |
| 3505 | for (i = 0; vim_isfilec(p[i]); i++) |
| 3506 | ; |
| 3507 | } |
| 3508 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3509 | if (i == 0) |
| 3510 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3511 | // Nothing found, use the rest of the line. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3512 | p = incl_regmatch.endp[0]; |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 3513 | i = (int)STRLEN(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3514 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3515 | // Avoid checking before the start of the line, can |
| 3516 | // happen if \zs appears in the regexp. |
Bram Moolenaar | 058bdcf | 2012-07-25 13:46:30 +0200 | [diff] [blame] | 3517 | else if (p > line) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3518 | { |
| 3519 | if (p[-1] == '"' || p[-1] == '<') |
| 3520 | { |
| 3521 | --p; |
| 3522 | ++i; |
| 3523 | } |
| 3524 | if (p[i] == '"' || p[i] == '>') |
| 3525 | ++i; |
| 3526 | } |
| 3527 | save_char = p[i]; |
| 3528 | p[i] = NUL; |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 3529 | msg_outtrans_attr(p, HL_ATTR(HLF_D)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3530 | p[i] = save_char; |
| 3531 | } |
| 3532 | |
| 3533 | if (new_fname == NULL && action == ACTION_SHOW_ALL) |
| 3534 | { |
| 3535 | if (already_searched) |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 3536 | msg_puts(_(" (Already listed)")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3537 | else |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 3538 | msg_puts(_(" NOT FOUND")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3539 | } |
| 3540 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3541 | out_flush(); // output each line directly |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3542 | } |
| 3543 | |
| 3544 | if (new_fname != NULL) |
| 3545 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3546 | // Push the new file onto the file stack |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3547 | if (depth + 1 == old_files) |
| 3548 | { |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 3549 | bigger = ALLOC_MULT(SearchedFile, max_path_depth * 2); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3550 | if (bigger != NULL) |
| 3551 | { |
| 3552 | for (i = 0; i <= depth; i++) |
| 3553 | bigger[i] = files[i]; |
| 3554 | for (i = depth + 1; i < old_files + max_path_depth; i++) |
| 3555 | { |
| 3556 | bigger[i].fp = NULL; |
| 3557 | bigger[i].name = NULL; |
| 3558 | bigger[i].lnum = 0; |
| 3559 | bigger[i].matched = FALSE; |
| 3560 | } |
| 3561 | for (i = old_files; i < max_path_depth; i++) |
| 3562 | bigger[i + max_path_depth] = files[i]; |
| 3563 | old_files += max_path_depth; |
| 3564 | max_path_depth *= 2; |
| 3565 | vim_free(files); |
| 3566 | files = bigger; |
| 3567 | } |
| 3568 | } |
| 3569 | if ((files[depth + 1].fp = mch_fopen((char *)new_fname, "r")) |
| 3570 | == NULL) |
| 3571 | vim_free(new_fname); |
| 3572 | else |
| 3573 | { |
| 3574 | if (++depth == old_files) |
| 3575 | { |
| 3576 | /* |
| 3577 | * lalloc() for 'bigger' must have failed above. We |
| 3578 | * will forget one of our already visited files now. |
| 3579 | */ |
| 3580 | vim_free(files[old_files].name); |
| 3581 | ++old_files; |
| 3582 | } |
| 3583 | files[depth].name = curr_fname = new_fname; |
| 3584 | files[depth].lnum = 0; |
| 3585 | files[depth].matched = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3586 | if (action == ACTION_EXPAND) |
| 3587 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3588 | msg_hist_off = TRUE; // reset in msg_trunc_attr() |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 3589 | vim_snprintf((char*)IObuff, IOSIZE, |
| 3590 | _("Scanning included file: %s"), |
| 3591 | (char *)new_fname); |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 3592 | msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3593 | } |
Bram Moolenaar | e2c453d | 2019-08-21 14:37:09 +0200 | [diff] [blame] | 3594 | else if (p_verbose >= 5) |
Bram Moolenaar | 87b5ca5 | 2006-03-04 21:55:31 +0000 | [diff] [blame] | 3595 | { |
| 3596 | verbose_enter(); |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 3597 | smsg(_("Searching included file %s"), |
Bram Moolenaar | 87b5ca5 | 2006-03-04 21:55:31 +0000 | [diff] [blame] | 3598 | (char *)new_fname); |
| 3599 | verbose_leave(); |
| 3600 | } |
| 3601 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3602 | } |
| 3603 | } |
| 3604 | } |
| 3605 | else |
| 3606 | { |
| 3607 | /* |
| 3608 | * Check if the line is a define (type == FIND_DEFINE) |
| 3609 | */ |
| 3610 | p = line; |
| 3611 | search_line: |
| 3612 | define_matched = FALSE; |
| 3613 | if (def_regmatch.regprog != NULL |
| 3614 | && vim_regexec(&def_regmatch, line, (colnr_T)0)) |
| 3615 | { |
| 3616 | /* |
| 3617 | * Pattern must be first identifier after 'define', so skip |
| 3618 | * to that position before checking for match of pattern. Also |
| 3619 | * don't let it match beyond the end of this identifier. |
| 3620 | */ |
| 3621 | p = def_regmatch.endp[0]; |
| 3622 | while (*p && !vim_iswordc(*p)) |
| 3623 | p++; |
| 3624 | define_matched = TRUE; |
| 3625 | } |
| 3626 | |
| 3627 | /* |
| 3628 | * Look for a match. Don't do this if we are looking for a |
| 3629 | * define and this line didn't match define_prog above. |
| 3630 | */ |
| 3631 | if (def_regmatch.regprog == NULL || define_matched) |
| 3632 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3633 | if (define_matched || compl_status_sol()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3634 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3635 | // compare the first "len" chars from "ptr" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3636 | startp = skipwhite(p); |
| 3637 | if (p_ic) |
| 3638 | matched = !MB_STRNICMP(startp, ptr, len); |
| 3639 | else |
| 3640 | matched = !STRNCMP(startp, ptr, len); |
| 3641 | if (matched && define_matched && whole |
| 3642 | && vim_iswordc(startp[len])) |
| 3643 | matched = FALSE; |
| 3644 | } |
| 3645 | else if (regmatch.regprog != NULL |
| 3646 | && vim_regexec(®match, line, (colnr_T)(p - line))) |
| 3647 | { |
| 3648 | matched = TRUE; |
| 3649 | startp = regmatch.startp[0]; |
| 3650 | /* |
| 3651 | * Check if the line is not a comment line (unless we are |
| 3652 | * looking for a define). A line starting with "# define" |
| 3653 | * is not considered to be a comment line. |
| 3654 | */ |
| 3655 | if (!define_matched && skip_comments) |
| 3656 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3657 | if ((*line != '#' || |
| 3658 | STRNCMP(skipwhite(line + 1), "define", 6) != 0) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 3659 | && get_leader_len(line, NULL, FALSE, TRUE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3660 | matched = FALSE; |
| 3661 | |
| 3662 | /* |
| 3663 | * Also check for a "/ *" or "/ /" before the match. |
| 3664 | * Skips lines like "int backwards; / * normal index |
| 3665 | * * /" when looking for "normal". |
| 3666 | * Note: Doesn't skip "/ *" in comments. |
| 3667 | */ |
| 3668 | p = skipwhite(line); |
| 3669 | if (matched |
| 3670 | || (p[0] == '/' && p[1] == '*') || p[0] == '*') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3671 | for (p = line; *p && p < startp; ++p) |
| 3672 | { |
| 3673 | if (matched |
| 3674 | && p[0] == '/' |
| 3675 | && (p[1] == '*' || p[1] == '/')) |
| 3676 | { |
| 3677 | matched = FALSE; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3678 | // After "//" all text is comment |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3679 | if (p[1] == '/') |
| 3680 | break; |
| 3681 | ++p; |
| 3682 | } |
| 3683 | else if (!matched && p[0] == '*' && p[1] == '/') |
| 3684 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3685 | // Can find match after "* /". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3686 | matched = TRUE; |
| 3687 | ++p; |
| 3688 | } |
| 3689 | } |
| 3690 | } |
| 3691 | } |
| 3692 | } |
| 3693 | } |
| 3694 | if (matched) |
| 3695 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3696 | if (action == ACTION_EXPAND) |
| 3697 | { |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 3698 | int cont_s_ipos = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3699 | int add_r; |
| 3700 | char_u *aux; |
| 3701 | |
| 3702 | if (depth == -1 && lnum == curwin->w_cursor.lnum) |
| 3703 | break; |
| 3704 | found = TRUE; |
| 3705 | aux = p = startp; |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3706 | if (compl_status_adding()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3707 | { |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3708 | p += ins_compl_len(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3709 | if (vim_iswordp(p)) |
| 3710 | goto exit_matched; |
| 3711 | p = find_word_start(p); |
| 3712 | } |
| 3713 | p = find_word_end(p); |
| 3714 | i = (int)(p - aux); |
| 3715 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3716 | if (compl_status_adding() && i == ins_compl_len()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3717 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3718 | // IOSIZE > compl_length, so the STRNCPY works |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3719 | STRNCPY(IObuff, aux, i); |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 3720 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3721 | // Get the next line: when "depth" < 0 from the current |
| 3722 | // buffer, otherwise from the included file. Jump to |
| 3723 | // exit_matched when past the last line. |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 3724 | if (depth < 0) |
| 3725 | { |
| 3726 | if (lnum >= end_lnum) |
| 3727 | goto exit_matched; |
Bram Moolenaar | 409510c | 2022-06-01 15:23:13 +0100 | [diff] [blame] | 3728 | line = get_line_and_copy(++lnum, file_line); |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 3729 | } |
| 3730 | else if (vim_fgets(line = file_line, |
| 3731 | LSIZE, files[depth].fp)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3732 | goto exit_matched; |
| 3733 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3734 | // we read a line, set "already" to check this "line" later |
| 3735 | // if depth >= 0 we'll increase files[depth].lnum far |
Bram Moolenaar | 8e7d622 | 2020-12-18 19:49:56 +0100 | [diff] [blame] | 3736 | // below -- Acevedo |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3737 | already = aux = p = skipwhite(line); |
| 3738 | p = find_word_start(p); |
| 3739 | p = find_word_end(p); |
| 3740 | if (p > aux) |
| 3741 | { |
| 3742 | if (*aux != ')' && IObuff[i-1] != TAB) |
| 3743 | { |
| 3744 | if (IObuff[i-1] != ' ') |
| 3745 | IObuff[i++] = ' '; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3746 | // IObuf =~ "\(\k\|\i\).* ", thus i >= 2 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3747 | if (p_js |
| 3748 | && (IObuff[i-2] == '.' |
| 3749 | || (vim_strchr(p_cpo, CPO_JOINSP) == NULL |
| 3750 | && (IObuff[i-2] == '?' |
| 3751 | || IObuff[i-2] == '!')))) |
| 3752 | IObuff[i++] = ' '; |
| 3753 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3754 | // copy as much as possible of the new word |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3755 | if (p - aux >= IOSIZE - i) |
| 3756 | p = aux + IOSIZE - i - 1; |
| 3757 | STRNCPY(IObuff + i, aux, p - aux); |
| 3758 | i += (int)(p - aux); |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 3759 | cont_s_ipos = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3760 | } |
| 3761 | IObuff[i] = NUL; |
| 3762 | aux = IObuff; |
| 3763 | |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3764 | if (i == ins_compl_len()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3765 | goto exit_matched; |
| 3766 | } |
| 3767 | |
Bram Moolenaar | e8c3a14 | 2006-08-29 14:30:35 +0000 | [diff] [blame] | 3768 | add_r = ins_compl_add_infercase(aux, i, p_ic, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3769 | curr_fname == curbuf->b_fname ? NULL : curr_fname, |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 3770 | dir, cont_s_ipos); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3771 | if (add_r == OK) |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3772 | // if dir was BACKWARD then honor it just once |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3773 | dir = FORWARD; |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3774 | else if (add_r == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3775 | break; |
| 3776 | } |
Bram Moolenaar | e2c453d | 2019-08-21 14:37:09 +0200 | [diff] [blame] | 3777 | else if (action == ACTION_SHOW_ALL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3778 | { |
| 3779 | found = TRUE; |
| 3780 | if (!did_show) |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3781 | gotocmdline(TRUE); // cursor at status line |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3782 | if (curr_fname != prev_fname) |
| 3783 | { |
| 3784 | if (did_show) |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3785 | msg_putchar('\n'); // cursor below last one |
| 3786 | if (!got_int) // don't display if 'q' typed |
| 3787 | // at "--more--" message |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3788 | msg_home_replace_hl(curr_fname); |
| 3789 | prev_fname = curr_fname; |
| 3790 | } |
| 3791 | did_show = TRUE; |
| 3792 | if (!got_int) |
| 3793 | show_pat_in_path(line, type, TRUE, action, |
| 3794 | (depth == -1) ? NULL : files[depth].fp, |
| 3795 | (depth == -1) ? &lnum : &files[depth].lnum, |
| 3796 | match_count++); |
| 3797 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3798 | // Set matched flag for this file and all the ones that |
| 3799 | // include it |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3800 | for (i = 0; i <= depth; ++i) |
| 3801 | files[i].matched = TRUE; |
| 3802 | } |
| 3803 | else if (--count <= 0) |
| 3804 | { |
| 3805 | found = TRUE; |
| 3806 | if (depth == -1 && lnum == curwin->w_cursor.lnum |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 3807 | #if defined(FEAT_QUICKFIX) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3808 | && g_do_tagpreview == 0 |
| 3809 | #endif |
| 3810 | ) |
Bram Moolenaar | ac78dd4 | 2022-01-02 19:25:26 +0000 | [diff] [blame] | 3811 | emsg(_(e_match_is_on_current_line)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3812 | else if (action == ACTION_SHOW) |
| 3813 | { |
| 3814 | show_pat_in_path(line, type, did_show, action, |
| 3815 | (depth == -1) ? NULL : files[depth].fp, |
| 3816 | (depth == -1) ? &lnum : &files[depth].lnum, 1L); |
| 3817 | did_show = TRUE; |
| 3818 | } |
| 3819 | else |
| 3820 | { |
| 3821 | #ifdef FEAT_GUI |
| 3822 | need_mouse_correct = TRUE; |
| 3823 | #endif |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 3824 | #if defined(FEAT_QUICKFIX) |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3825 | // ":psearch" uses the preview window |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3826 | if (g_do_tagpreview != 0) |
| 3827 | { |
| 3828 | curwin_save = curwin; |
Bram Moolenaar | 576a4a6 | 2019-08-18 15:25:17 +0200 | [diff] [blame] | 3829 | prepare_tagpreview(TRUE, TRUE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3830 | } |
| 3831 | #endif |
| 3832 | if (action == ACTION_SPLIT) |
| 3833 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3834 | if (win_split(0, 0) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3835 | break; |
Bram Moolenaar | 3368ea2 | 2010-09-21 16:56:35 +0200 | [diff] [blame] | 3836 | RESET_BINDING(curwin); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3837 | } |
| 3838 | if (depth == -1) |
| 3839 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3840 | // match in current file |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 3841 | #if defined(FEAT_QUICKFIX) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3842 | if (g_do_tagpreview != 0) |
| 3843 | { |
Bram Moolenaar | 92bb83e | 2021-02-03 23:04:46 +0100 | [diff] [blame] | 3844 | if (!win_valid(curwin_save)) |
| 3845 | break; |
Bram Moolenaar | 8ad80de | 2017-06-05 16:01:59 +0200 | [diff] [blame] | 3846 | if (!GETFILE_SUCCESS(getfile( |
Bram Moolenaar | c31f9ae | 2017-07-23 22:02:02 +0200 | [diff] [blame] | 3847 | curwin_save->w_buffer->b_fnum, NULL, |
Bram Moolenaar | 8ad80de | 2017-06-05 16:01:59 +0200 | [diff] [blame] | 3848 | NULL, TRUE, lnum, FALSE))) |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3849 | break; // failed to jump to file |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3850 | } |
| 3851 | else |
| 3852 | #endif |
| 3853 | setpcmark(); |
| 3854 | curwin->w_cursor.lnum = lnum; |
Bram Moolenaar | c31f9ae | 2017-07-23 22:02:02 +0200 | [diff] [blame] | 3855 | check_cursor(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3856 | } |
| 3857 | else |
| 3858 | { |
Bram Moolenaar | 8ad80de | 2017-06-05 16:01:59 +0200 | [diff] [blame] | 3859 | if (!GETFILE_SUCCESS(getfile( |
| 3860 | 0, files[depth].name, NULL, TRUE, |
| 3861 | files[depth].lnum, FALSE))) |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3862 | break; // failed to jump to file |
| 3863 | // autocommands may have changed the lnum, we don't |
| 3864 | // want that here |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3865 | curwin->w_cursor.lnum = files[depth].lnum; |
| 3866 | } |
| 3867 | } |
| 3868 | if (action != ACTION_SHOW) |
| 3869 | { |
Bram Moolenaar | fe81d45 | 2009-04-22 14:44:41 +0000 | [diff] [blame] | 3870 | curwin->w_cursor.col = (colnr_T)(startp - line); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3871 | curwin->w_set_curswant = TRUE; |
| 3872 | } |
| 3873 | |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 3874 | #if defined(FEAT_QUICKFIX) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3875 | if (g_do_tagpreview != 0 |
Bram Moolenaar | 997fb4b | 2006-02-17 21:53:23 +0000 | [diff] [blame] | 3876 | && curwin != curwin_save && win_valid(curwin_save)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3877 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3878 | // Return cursor to where we were |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3879 | validate_cursor(); |
Bram Moolenaar | a4d158b | 2022-08-14 14:17:45 +0100 | [diff] [blame] | 3880 | redraw_later(UPD_VALID); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3881 | win_enter(curwin_save, TRUE); |
| 3882 | } |
Bram Moolenaar | 05ad5ff | 2019-11-30 22:48:27 +0100 | [diff] [blame] | 3883 | # ifdef FEAT_PROP_POPUP |
Bram Moolenaar | 1b6d9c4 | 2019-08-05 21:52:04 +0200 | [diff] [blame] | 3884 | else if (WIN_IS_POPUP(curwin)) |
| 3885 | // can't keep focus in popup window |
| 3886 | win_enter(firstwin, TRUE); |
| 3887 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3888 | #endif |
| 3889 | break; |
| 3890 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3891 | exit_matched: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3892 | matched = FALSE; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3893 | // look for other matches in the rest of the line if we |
| 3894 | // are not at the end of it already |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3895 | if (def_regmatch.regprog == NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3896 | && action == ACTION_EXPAND |
Yegappan Lakshmanan | d94fbfc | 2022-01-04 17:01:44 +0000 | [diff] [blame] | 3897 | && !compl_status_sol() |
Bram Moolenaar | fe81d45 | 2009-04-22 14:44:41 +0000 | [diff] [blame] | 3898 | && *startp != NUL |
Bram Moolenaar | 1614a14 | 2019-10-06 22:00:13 +0200 | [diff] [blame] | 3899 | && *(p = startp + mb_ptr2len(startp)) != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3900 | goto search_line; |
| 3901 | } |
| 3902 | line_breakcheck(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3903 | if (action == ACTION_EXPAND) |
Bram Moolenaar | 472e859 | 2016-10-15 17:06:47 +0200 | [diff] [blame] | 3904 | ins_compl_check_keys(30, FALSE); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3905 | if (got_int || ins_compl_interrupted()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3906 | break; |
| 3907 | |
| 3908 | /* |
| 3909 | * Read the next line. When reading an included file and encountering |
| 3910 | * end-of-file, close the file and continue in the file that included |
| 3911 | * it. |
| 3912 | */ |
| 3913 | while (depth >= 0 && !already |
| 3914 | && vim_fgets(line = file_line, LSIZE, files[depth].fp)) |
| 3915 | { |
| 3916 | fclose(files[depth].fp); |
| 3917 | --old_files; |
| 3918 | files[old_files].name = files[depth].name; |
| 3919 | files[old_files].matched = files[depth].matched; |
| 3920 | --depth; |
| 3921 | curr_fname = (depth == -1) ? curbuf->b_fname |
| 3922 | : files[depth].name; |
| 3923 | if (depth < depth_displayed) |
| 3924 | depth_displayed = depth; |
| 3925 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3926 | if (depth >= 0) // we could read the line |
Bram Moolenaar | c84e3c1 | 2013-07-03 22:28:36 +0200 | [diff] [blame] | 3927 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3928 | files[depth].lnum++; |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3929 | // Remove any CR and LF from the line. |
Bram Moolenaar | c84e3c1 | 2013-07-03 22:28:36 +0200 | [diff] [blame] | 3930 | i = (int)STRLEN(line); |
| 3931 | if (i > 0 && line[i - 1] == '\n') |
| 3932 | line[--i] = NUL; |
| 3933 | if (i > 0 && line[i - 1] == '\r') |
| 3934 | line[--i] = NUL; |
| 3935 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3936 | else if (!already) |
| 3937 | { |
| 3938 | if (++lnum > end_lnum) |
| 3939 | break; |
Bram Moolenaar | 409510c | 2022-06-01 15:23:13 +0100 | [diff] [blame] | 3940 | line = get_line_and_copy(lnum, file_line); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3941 | } |
| 3942 | already = NULL; |
| 3943 | } |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3944 | // End of big for (;;) loop. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3945 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3946 | // Close any files that are still open. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3947 | for (i = 0; i <= depth; i++) |
| 3948 | { |
| 3949 | fclose(files[i].fp); |
| 3950 | vim_free(files[i].name); |
| 3951 | } |
| 3952 | for (i = old_files; i < max_path_depth; i++) |
| 3953 | vim_free(files[i].name); |
| 3954 | vim_free(files); |
| 3955 | |
| 3956 | if (type == CHECK_PATH) |
| 3957 | { |
| 3958 | if (!did_show) |
| 3959 | { |
| 3960 | if (action != ACTION_SHOW_ALL) |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 3961 | msg(_("All included files were found")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3962 | else |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 3963 | msg(_("No included files")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3964 | } |
| 3965 | } |
Bram Moolenaar | e2c453d | 2019-08-21 14:37:09 +0200 | [diff] [blame] | 3966 | else if (!found && action != ACTION_EXPAND) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3967 | { |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 3968 | if (got_int || ins_compl_interrupted()) |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 3969 | emsg(_(e_interrupted)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3970 | else if (type == FIND_DEFINE) |
Bram Moolenaar | ac78dd4 | 2022-01-02 19:25:26 +0000 | [diff] [blame] | 3971 | emsg(_(e_couldnt_find_definition)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3972 | else |
Bram Moolenaar | ac78dd4 | 2022-01-02 19:25:26 +0000 | [diff] [blame] | 3973 | emsg(_(e_couldnt_find_pattern)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3974 | } |
| 3975 | if (action == ACTION_SHOW || action == ACTION_SHOW_ALL) |
| 3976 | msg_end(); |
| 3977 | |
| 3978 | fpip_end: |
| 3979 | vim_free(file_line); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 3980 | vim_regfree(regmatch.regprog); |
| 3981 | vim_regfree(incl_regmatch.regprog); |
| 3982 | vim_regfree(def_regmatch.regprog); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3983 | } |
| 3984 | |
| 3985 | static void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 3986 | show_pat_in_path( |
| 3987 | char_u *line, |
| 3988 | int type, |
| 3989 | int did_show, |
| 3990 | int action, |
| 3991 | FILE *fp, |
| 3992 | linenr_T *lnum, |
| 3993 | long count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3994 | { |
| 3995 | char_u *p; |
| 3996 | |
| 3997 | if (did_show) |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 3998 | msg_putchar('\n'); // cursor below last one |
Bram Moolenaar | 91170f8 | 2006-05-05 21:15:17 +0000 | [diff] [blame] | 3999 | else if (!msg_silent) |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 4000 | gotocmdline(TRUE); // cursor at status line |
| 4001 | if (got_int) // 'q' typed at "--more--" message |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4002 | return; |
| 4003 | for (;;) |
| 4004 | { |
| 4005 | p = line + STRLEN(line) - 1; |
| 4006 | if (fp != NULL) |
| 4007 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 4008 | // We used fgets(), so get rid of newline at end |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4009 | if (p >= line && *p == '\n') |
| 4010 | --p; |
| 4011 | if (p >= line && *p == '\r') |
| 4012 | --p; |
| 4013 | *(p + 1) = NUL; |
| 4014 | } |
| 4015 | if (action == ACTION_SHOW_ALL) |
| 4016 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 4017 | sprintf((char *)IObuff, "%3ld: ", count); // show match nr |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 4018 | msg_puts((char *)IObuff); |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 4019 | sprintf((char *)IObuff, "%4ld", *lnum); // show line nr |
| 4020 | // Highlight line numbers |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 4021 | msg_puts_attr((char *)IObuff, HL_ATTR(HLF_N)); |
| 4022 | msg_puts(" "); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4023 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4024 | msg_prt_line(line, FALSE); |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 4025 | out_flush(); // show one line at a time |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4026 | |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 4027 | // Definition continues until line that doesn't end with '\' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4028 | if (got_int || type != FIND_DEFINE || p < line || *p != '\\') |
| 4029 | break; |
| 4030 | |
| 4031 | if (fp != NULL) |
| 4032 | { |
Bram Moolenaar | 63d9e73 | 2019-12-05 21:10:38 +0100 | [diff] [blame] | 4033 | if (vim_fgets(line, LSIZE, fp)) // end of file |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4034 | break; |
| 4035 | ++*lnum; |
| 4036 | } |
| 4037 | else |
| 4038 | { |
| 4039 | if (++*lnum > curbuf->b_ml.ml_line_count) |
| 4040 | break; |
| 4041 | line = ml_get(*lnum); |
| 4042 | } |
| 4043 | msg_putchar('\n'); |
| 4044 | } |
| 4045 | } |
| 4046 | #endif |
| 4047 | |
| 4048 | #ifdef FEAT_VIMINFO |
Bram Moolenaar | 6bd1d77 | 2019-10-09 22:01:25 +0200 | [diff] [blame] | 4049 | /* |
| 4050 | * Return the last used search pattern at "idx". |
| 4051 | */ |
Bram Moolenaar | c332816 | 2019-07-23 22:15:25 +0200 | [diff] [blame] | 4052 | spat_T * |
| 4053 | get_spat(int idx) |
| 4054 | { |
| 4055 | return &spats[idx]; |
| 4056 | } |
| 4057 | |
Bram Moolenaar | 6bd1d77 | 2019-10-09 22:01:25 +0200 | [diff] [blame] | 4058 | /* |
| 4059 | * Return the last used search pattern index. |
| 4060 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4061 | int |
Bram Moolenaar | c332816 | 2019-07-23 22:15:25 +0200 | [diff] [blame] | 4062 | get_spat_last_idx(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4063 | { |
Bram Moolenaar | c332816 | 2019-07-23 22:15:25 +0200 | [diff] [blame] | 4064 | return last_idx; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4065 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4066 | #endif |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 4067 | |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 4068 | #if defined(FEAT_EVAL) || defined(FEAT_PROTO) |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 4069 | /* |
| 4070 | * "searchcount()" function |
| 4071 | */ |
| 4072 | void |
| 4073 | f_searchcount(typval_T *argvars, typval_T *rettv) |
| 4074 | { |
| 4075 | pos_T pos = curwin->w_cursor; |
| 4076 | char_u *pattern = NULL; |
| 4077 | int maxcount = SEARCH_STAT_DEF_MAX_COUNT; |
| 4078 | long timeout = SEARCH_STAT_DEF_TIMEOUT; |
Bram Moolenaar | 4140c4f | 2020-09-05 23:16:00 +0200 | [diff] [blame] | 4079 | int recompute = TRUE; |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 4080 | searchstat_T stat; |
| 4081 | |
| 4082 | if (rettv_dict_alloc(rettv) == FAIL) |
| 4083 | return; |
| 4084 | |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 4085 | if (in_vim9script() && check_for_opt_dict_arg(argvars, 0) == FAIL) |
| 4086 | return; |
| 4087 | |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 4088 | if (shortmess(SHM_SEARCHCOUNT)) // 'shortmess' contains 'S' flag |
| 4089 | recompute = TRUE; |
| 4090 | |
| 4091 | if (argvars[0].v_type != VAR_UNKNOWN) |
| 4092 | { |
Bram Moolenaar | 1468162 | 2020-06-03 22:57:39 +0200 | [diff] [blame] | 4093 | dict_T *dict; |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 4094 | dictitem_T *di; |
| 4095 | listitem_T *li; |
| 4096 | int error = FALSE; |
| 4097 | |
Yegappan Lakshmanan | 04c4c57 | 2022-08-30 19:48:24 +0100 | [diff] [blame] | 4098 | if (check_for_nonnull_dict_arg(argvars, 0) == FAIL) |
Bram Moolenaar | 1468162 | 2020-06-03 22:57:39 +0200 | [diff] [blame] | 4099 | return; |
Bram Moolenaar | 1468162 | 2020-06-03 22:57:39 +0200 | [diff] [blame] | 4100 | dict = argvars[0].vval.v_dict; |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 4101 | di = dict_find(dict, (char_u *)"timeout", -1); |
| 4102 | if (di != NULL) |
| 4103 | { |
| 4104 | timeout = (long)tv_get_number_chk(&di->di_tv, &error); |
| 4105 | if (error) |
| 4106 | return; |
| 4107 | } |
| 4108 | di = dict_find(dict, (char_u *)"maxcount", -1); |
| 4109 | if (di != NULL) |
| 4110 | { |
| 4111 | maxcount = (int)tv_get_number_chk(&di->di_tv, &error); |
| 4112 | if (error) |
| 4113 | return; |
| 4114 | } |
Bram Moolenaar | d61efa5 | 2022-07-23 09:52:04 +0100 | [diff] [blame] | 4115 | recompute = dict_get_bool(dict, "recompute", recompute); |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 4116 | di = dict_find(dict, (char_u *)"pattern", -1); |
| 4117 | if (di != NULL) |
| 4118 | { |
| 4119 | pattern = tv_get_string_chk(&di->di_tv); |
| 4120 | if (pattern == NULL) |
| 4121 | return; |
| 4122 | } |
| 4123 | di = dict_find(dict, (char_u *)"pos", -1); |
| 4124 | if (di != NULL) |
| 4125 | { |
| 4126 | if (di->di_tv.v_type != VAR_LIST) |
| 4127 | { |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 4128 | semsg(_(e_invalid_argument_str), "pos"); |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 4129 | return; |
| 4130 | } |
| 4131 | if (list_len(di->di_tv.vval.v_list) != 3) |
| 4132 | { |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 4133 | semsg(_(e_invalid_argument_str), "List format should be [lnum, col, off]"); |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 4134 | return; |
| 4135 | } |
| 4136 | li = list_find(di->di_tv.vval.v_list, 0L); |
| 4137 | if (li != NULL) |
| 4138 | { |
| 4139 | pos.lnum = tv_get_number_chk(&li->li_tv, &error); |
| 4140 | if (error) |
| 4141 | return; |
| 4142 | } |
| 4143 | li = list_find(di->di_tv.vval.v_list, 1L); |
| 4144 | if (li != NULL) |
| 4145 | { |
Bram Moolenaar | 6ed545e | 2022-05-09 20:09:23 +0100 | [diff] [blame] | 4146 | pos.col = tv_get_number_chk(&li->li_tv, &error) - 1; |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 4147 | if (error) |
| 4148 | return; |
| 4149 | } |
| 4150 | li = list_find(di->di_tv.vval.v_list, 2L); |
| 4151 | if (li != NULL) |
| 4152 | { |
Bram Moolenaar | 6ed545e | 2022-05-09 20:09:23 +0100 | [diff] [blame] | 4153 | pos.coladd = tv_get_number_chk(&li->li_tv, &error); |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 4154 | if (error) |
| 4155 | return; |
| 4156 | } |
| 4157 | } |
| 4158 | } |
| 4159 | |
| 4160 | save_last_search_pattern(); |
Christian Brabandt | 6dd7424 | 2022-02-14 12:44:32 +0000 | [diff] [blame] | 4161 | #ifdef FEAT_SEARCH_EXTRA |
| 4162 | save_incsearch_state(); |
| 4163 | #endif |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 4164 | if (pattern != NULL) |
| 4165 | { |
| 4166 | if (*pattern == NUL) |
| 4167 | goto the_end; |
Bram Moolenaar | 109aece | 2020-06-01 19:08:54 +0200 | [diff] [blame] | 4168 | vim_free(spats[last_idx].pat); |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 4169 | spats[last_idx].pat = vim_strsave(pattern); |
| 4170 | } |
| 4171 | if (spats[last_idx].pat == NULL || *spats[last_idx].pat == NUL) |
| 4172 | goto the_end; // the previous pattern was never defined |
| 4173 | |
| 4174 | update_search_stat(0, &pos, &pos, &stat, recompute, maxcount, timeout); |
| 4175 | |
| 4176 | dict_add_number(rettv->vval.v_dict, "current", stat.cur); |
| 4177 | dict_add_number(rettv->vval.v_dict, "total", stat.cnt); |
| 4178 | dict_add_number(rettv->vval.v_dict, "exact_match", stat.exact_match); |
| 4179 | dict_add_number(rettv->vval.v_dict, "incomplete", stat.incomplete); |
| 4180 | dict_add_number(rettv->vval.v_dict, "maxcount", stat.last_maxcount); |
| 4181 | |
| 4182 | the_end: |
| 4183 | restore_last_search_pattern(); |
Christian Brabandt | 6dd7424 | 2022-02-14 12:44:32 +0000 | [diff] [blame] | 4184 | #ifdef FEAT_SEARCH_EXTRA |
| 4185 | restore_incsearch_state(); |
| 4186 | #endif |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 4187 | } |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 4188 | #endif |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4189 | |
| 4190 | /* |
| 4191 | * Fuzzy string matching |
| 4192 | * |
| 4193 | * Ported from the lib_fts library authored by Forrest Smith. |
| 4194 | * https://github.com/forrestthewoods/lib_fts/tree/master/code |
| 4195 | * |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4196 | * The following blog describes the fuzzy matching algorithm: |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4197 | * https://www.forrestthewoods.com/blog/reverse_engineering_sublime_texts_fuzzy_match/ |
| 4198 | * |
| 4199 | * Each matching string is assigned a score. The following factors are checked: |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4200 | * - Matched letter |
| 4201 | * - Unmatched letter |
| 4202 | * - Consecutively matched letters |
| 4203 | * - Proximity to start |
| 4204 | * - Letter following a separator (space, underscore) |
| 4205 | * - Uppercase letter following lowercase (aka CamelCase) |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4206 | * |
| 4207 | * Matched letters are good. Unmatched letters are bad. Matching near the start |
| 4208 | * is good. Matching the first letter in the middle of a phrase is good. |
| 4209 | * Matching the uppercase letters in camel case entries is good. |
| 4210 | * |
| 4211 | * The score assigned for each factor is explained below. |
| 4212 | * File paths are different from file names. File extensions may be ignorable. |
| 4213 | * Single words care about consecutive matches but not separators or camel |
| 4214 | * case. |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4215 | * Score starts at 100 |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4216 | * Matched letter: +0 points |
| 4217 | * Unmatched letter: -1 point |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4218 | * Consecutive match bonus: +15 points |
| 4219 | * First letter bonus: +15 points |
| 4220 | * Separator bonus: +30 points |
| 4221 | * Camel case bonus: +30 points |
| 4222 | * Unmatched leading letter: -5 points (max: -15) |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4223 | * |
| 4224 | * 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] | 4225 | * 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] | 4226 | * lower minimum score due to unmatched letter penalty. Longer search patterns |
| 4227 | * have a higher maximum score due to match bonuses. |
| 4228 | * |
| 4229 | * Separator and camel case bonus is worth a LOT. Consecutive matches are worth |
| 4230 | * quite a bit. |
| 4231 | * |
| 4232 | * There is a penalty if you DON’T match the first three letters. Which |
| 4233 | * effectively rewards matching near the start. However there’s no difference |
| 4234 | * in matching between the middle and end. |
| 4235 | * |
| 4236 | * There is not an explicit bonus for an exact match. Unmatched letters receive |
| 4237 | * a penalty. So shorter strings and closer matches are worth more. |
| 4238 | */ |
| 4239 | typedef struct |
| 4240 | { |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4241 | int idx; // used for stable sort |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4242 | listitem_T *item; |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4243 | int score; |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4244 | list_T *lmatchpos; |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4245 | } fuzzyItem_T; |
| 4246 | |
Bram Moolenaar | e9f9f16 | 2020-10-20 19:01:30 +0200 | [diff] [blame] | 4247 | // bonus for adjacent matches; this is higher than SEPARATOR_BONUS so that |
| 4248 | // matching a whole word is preferred. |
| 4249 | #define SEQUENTIAL_BONUS 40 |
Bram Moolenaar | dcdd42a | 2020-10-29 18:58:01 +0100 | [diff] [blame] | 4250 | // bonus if match occurs after a path separator |
| 4251 | #define PATH_SEPARATOR_BONUS 30 |
| 4252 | // bonus if match occurs after a word separator |
| 4253 | #define WORD_SEPARATOR_BONUS 25 |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4254 | // bonus if match is uppercase and prev is lower |
| 4255 | #define CAMEL_BONUS 30 |
| 4256 | // bonus if the first letter is matched |
| 4257 | #define FIRST_LETTER_BONUS 15 |
| 4258 | // penalty applied for every letter in str before the first match |
kylo252 | ae6f1d8 | 2022-02-16 19:24:07 +0000 | [diff] [blame] | 4259 | #define LEADING_LETTER_PENALTY (-5) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4260 | // maximum penalty for leading letters |
kylo252 | ae6f1d8 | 2022-02-16 19:24:07 +0000 | [diff] [blame] | 4261 | #define MAX_LEADING_LETTER_PENALTY (-15) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4262 | // penalty for every letter that doesn't match |
kylo252 | ae6f1d8 | 2022-02-16 19:24:07 +0000 | [diff] [blame] | 4263 | #define UNMATCHED_LETTER_PENALTY (-1) |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4264 | // penalty for gap in matching positions (-2 * k) |
kylo252 | ae6f1d8 | 2022-02-16 19:24:07 +0000 | [diff] [blame] | 4265 | #define GAP_PENALTY (-2) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4266 | // Score for a string that doesn't fuzzy match the pattern |
kylo252 | ae6f1d8 | 2022-02-16 19:24:07 +0000 | [diff] [blame] | 4267 | #define SCORE_NONE (-9999) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4268 | |
| 4269 | #define FUZZY_MATCH_RECURSION_LIMIT 10 |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4270 | |
| 4271 | /* |
| 4272 | * Compute a score for a fuzzy matched string. The matching character locations |
| 4273 | * are in 'matches'. |
| 4274 | */ |
| 4275 | static int |
| 4276 | fuzzy_match_compute_score( |
| 4277 | char_u *str, |
| 4278 | int strSz, |
Yegappan Lakshmanan | bb01a1e | 2021-04-26 21:17:52 +0200 | [diff] [blame] | 4279 | int_u *matches, |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4280 | int numMatches) |
| 4281 | { |
| 4282 | int score; |
| 4283 | int penalty; |
| 4284 | int unmatched; |
| 4285 | int i; |
| 4286 | char_u *p = str; |
Yegappan Lakshmanan | bb01a1e | 2021-04-26 21:17:52 +0200 | [diff] [blame] | 4287 | int_u sidx = 0; |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4288 | |
| 4289 | // Initialize score |
| 4290 | score = 100; |
| 4291 | |
| 4292 | // Apply leading letter penalty |
| 4293 | penalty = LEADING_LETTER_PENALTY * matches[0]; |
| 4294 | if (penalty < MAX_LEADING_LETTER_PENALTY) |
| 4295 | penalty = MAX_LEADING_LETTER_PENALTY; |
| 4296 | score += penalty; |
| 4297 | |
| 4298 | // Apply unmatched penalty |
| 4299 | unmatched = strSz - numMatches; |
| 4300 | score += UNMATCHED_LETTER_PENALTY * unmatched; |
| 4301 | |
| 4302 | // Apply ordering bonuses |
| 4303 | for (i = 0; i < numMatches; ++i) |
| 4304 | { |
Yegappan Lakshmanan | bb01a1e | 2021-04-26 21:17:52 +0200 | [diff] [blame] | 4305 | int_u currIdx = matches[i]; |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4306 | |
| 4307 | if (i > 0) |
| 4308 | { |
Yegappan Lakshmanan | bb01a1e | 2021-04-26 21:17:52 +0200 | [diff] [blame] | 4309 | int_u prevIdx = matches[i - 1]; |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4310 | |
| 4311 | // Sequential |
| 4312 | if (currIdx == (prevIdx + 1)) |
| 4313 | score += SEQUENTIAL_BONUS; |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4314 | else |
| 4315 | score += GAP_PENALTY * (currIdx - prevIdx); |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4316 | } |
| 4317 | |
| 4318 | // Check for bonuses based on neighbor character value |
| 4319 | if (currIdx > 0) |
| 4320 | { |
| 4321 | // Camel case |
Bram Moolenaar | c53e9c5 | 2020-09-22 22:08:32 +0200 | [diff] [blame] | 4322 | int neighbor = ' '; |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4323 | int curr; |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4324 | |
| 4325 | if (has_mbyte) |
| 4326 | { |
| 4327 | while (sidx < currIdx) |
| 4328 | { |
| 4329 | neighbor = (*mb_ptr2char)(p); |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4330 | MB_PTR_ADV(p); |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4331 | sidx++; |
| 4332 | } |
| 4333 | curr = (*mb_ptr2char)(p); |
| 4334 | } |
| 4335 | else |
| 4336 | { |
| 4337 | neighbor = str[currIdx - 1]; |
| 4338 | curr = str[currIdx]; |
| 4339 | } |
| 4340 | |
| 4341 | if (vim_islower(neighbor) && vim_isupper(curr)) |
| 4342 | score += CAMEL_BONUS; |
| 4343 | |
Bram Moolenaar | dcdd42a | 2020-10-29 18:58:01 +0100 | [diff] [blame] | 4344 | // Bonus if the match follows a separator character |
| 4345 | if (neighbor == '/' || neighbor == '\\') |
| 4346 | score += PATH_SEPARATOR_BONUS; |
| 4347 | else if (neighbor == ' ' || neighbor == '_') |
| 4348 | score += WORD_SEPARATOR_BONUS; |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4349 | } |
| 4350 | else |
| 4351 | { |
| 4352 | // First letter |
| 4353 | score += FIRST_LETTER_BONUS; |
| 4354 | } |
| 4355 | } |
| 4356 | return score; |
| 4357 | } |
| 4358 | |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4359 | /* |
| 4360 | * Perform a recursive search for fuzzy matching 'fuzpat' in 'str'. |
| 4361 | * Return the number of matching characters. |
| 4362 | */ |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4363 | static int |
| 4364 | fuzzy_match_recursive( |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4365 | char_u *fuzpat, |
| 4366 | char_u *str, |
Yegappan Lakshmanan | bb01a1e | 2021-04-26 21:17:52 +0200 | [diff] [blame] | 4367 | int_u strIdx, |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4368 | int *outScore, |
| 4369 | char_u *strBegin, |
| 4370 | int strLen, |
Yegappan Lakshmanan | bb01a1e | 2021-04-26 21:17:52 +0200 | [diff] [blame] | 4371 | int_u *srcMatches, |
| 4372 | int_u *matches, |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4373 | int maxMatches, |
| 4374 | int nextMatch, |
| 4375 | int *recursionCount) |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4376 | { |
| 4377 | // Recursion params |
| 4378 | int recursiveMatch = FALSE; |
Yegappan Lakshmanan | bb01a1e | 2021-04-26 21:17:52 +0200 | [diff] [blame] | 4379 | int_u bestRecursiveMatches[MAX_FUZZY_MATCHES]; |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4380 | int bestRecursiveScore = 0; |
| 4381 | int first_match; |
| 4382 | int matched; |
| 4383 | |
| 4384 | // Count recursions |
| 4385 | ++*recursionCount; |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4386 | if (*recursionCount >= FUZZY_MATCH_RECURSION_LIMIT) |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4387 | return 0; |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4388 | |
| 4389 | // Detect end of strings |
Yegappan Lakshmanan | bb01a1e | 2021-04-26 21:17:52 +0200 | [diff] [blame] | 4390 | if (*fuzpat == NUL || *str == NUL) |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4391 | return 0; |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4392 | |
| 4393 | // Loop through fuzpat and str looking for a match |
| 4394 | first_match = TRUE; |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4395 | while (*fuzpat != NUL && *str != NUL) |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4396 | { |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4397 | int c1; |
| 4398 | int c2; |
| 4399 | |
| 4400 | c1 = PTR2CHAR(fuzpat); |
| 4401 | c2 = PTR2CHAR(str); |
| 4402 | |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4403 | // Found match |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4404 | if (vim_tolower(c1) == vim_tolower(c2)) |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4405 | { |
Yegappan Lakshmanan | bb01a1e | 2021-04-26 21:17:52 +0200 | [diff] [blame] | 4406 | int_u recursiveMatches[MAX_FUZZY_MATCHES]; |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4407 | int recursiveScore = 0; |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4408 | char_u *next_char; |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4409 | |
| 4410 | // Supplied matches buffer was too short |
| 4411 | if (nextMatch >= maxMatches) |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4412 | return 0; |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4413 | |
| 4414 | // "Copy-on-Write" srcMatches into matches |
| 4415 | if (first_match && srcMatches) |
| 4416 | { |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4417 | memcpy(matches, srcMatches, nextMatch * sizeof(srcMatches[0])); |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4418 | first_match = FALSE; |
| 4419 | } |
| 4420 | |
| 4421 | // Recursive call that "skips" this match |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4422 | if (has_mbyte) |
| 4423 | next_char = str + (*mb_ptr2len)(str); |
| 4424 | else |
| 4425 | next_char = str + 1; |
| 4426 | if (fuzzy_match_recursive(fuzpat, next_char, strIdx + 1, |
| 4427 | &recursiveScore, strBegin, strLen, matches, |
| 4428 | recursiveMatches, |
K.Takata | eeec254 | 2021-06-02 13:28:16 +0200 | [diff] [blame] | 4429 | ARRAY_LENGTH(recursiveMatches), |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4430 | nextMatch, recursionCount)) |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4431 | { |
| 4432 | // Pick best recursive score |
| 4433 | if (!recursiveMatch || recursiveScore > bestRecursiveScore) |
| 4434 | { |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4435 | memcpy(bestRecursiveMatches, recursiveMatches, |
Yegappan Lakshmanan | bb01a1e | 2021-04-26 21:17:52 +0200 | [diff] [blame] | 4436 | MAX_FUZZY_MATCHES * sizeof(recursiveMatches[0])); |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4437 | bestRecursiveScore = recursiveScore; |
| 4438 | } |
| 4439 | recursiveMatch = TRUE; |
| 4440 | } |
| 4441 | |
| 4442 | // Advance |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4443 | matches[nextMatch++] = strIdx; |
| 4444 | if (has_mbyte) |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4445 | MB_PTR_ADV(fuzpat); |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4446 | else |
| 4447 | ++fuzpat; |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4448 | } |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4449 | if (has_mbyte) |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4450 | MB_PTR_ADV(str); |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4451 | else |
| 4452 | ++str; |
| 4453 | strIdx++; |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4454 | } |
| 4455 | |
| 4456 | // Determine if full fuzpat was matched |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4457 | matched = *fuzpat == NUL ? TRUE : FALSE; |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4458 | |
| 4459 | // Calculate score |
| 4460 | if (matched) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4461 | *outScore = fuzzy_match_compute_score(strBegin, strLen, matches, |
| 4462 | nextMatch); |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4463 | |
| 4464 | // Return best result |
| 4465 | if (recursiveMatch && (!matched || bestRecursiveScore > *outScore)) |
| 4466 | { |
| 4467 | // Recursive score is better than "this" |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4468 | memcpy(matches, bestRecursiveMatches, maxMatches * sizeof(matches[0])); |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4469 | *outScore = bestRecursiveScore; |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4470 | return nextMatch; |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4471 | } |
| 4472 | else if (matched) |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4473 | return nextMatch; // "this" score is better than recursive |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4474 | |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4475 | return 0; // no match |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4476 | } |
| 4477 | |
| 4478 | /* |
| 4479 | * fuzzy_match() |
| 4480 | * |
| 4481 | * Performs exhaustive search via recursion to find all possible matches and |
| 4482 | * match with highest score. |
| 4483 | * Scores values have no intrinsic meaning. Possible score range is not |
| 4484 | * normalized and varies with pattern. |
| 4485 | * Recursion is limited internally (default=10) to prevent degenerate cases |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4486 | * (pat_arg="aaaaaa" str="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"). |
Yegappan Lakshmanan | bb01a1e | 2021-04-26 21:17:52 +0200 | [diff] [blame] | 4487 | * Uses char_u for match indices. Therefore patterns are limited to |
| 4488 | * MAX_FUZZY_MATCHES characters. |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4489 | * |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4490 | * Returns TRUE if 'pat_arg' matches 'str'. Also returns the match score in |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4491 | * 'outScore' and the matching character positions in 'matches'. |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4492 | */ |
Yegappan Lakshmanan | bb01a1e | 2021-04-26 21:17:52 +0200 | [diff] [blame] | 4493 | int |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4494 | fuzzy_match( |
| 4495 | char_u *str, |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4496 | char_u *pat_arg, |
| 4497 | int matchseq, |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4498 | int *outScore, |
Yegappan Lakshmanan | bb01a1e | 2021-04-26 21:17:52 +0200 | [diff] [blame] | 4499 | int_u *matches, |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4500 | int maxMatches) |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4501 | { |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4502 | int recursionCount = 0; |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4503 | int len = MB_CHARLEN(str); |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4504 | char_u *save_pat; |
| 4505 | char_u *pat; |
| 4506 | char_u *p; |
| 4507 | int complete = FALSE; |
| 4508 | int score = 0; |
| 4509 | int numMatches = 0; |
| 4510 | int matchCount; |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4511 | |
| 4512 | *outScore = 0; |
| 4513 | |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4514 | save_pat = vim_strsave(pat_arg); |
| 4515 | if (save_pat == NULL) |
| 4516 | return FALSE; |
| 4517 | pat = save_pat; |
| 4518 | p = pat; |
| 4519 | |
| 4520 | // Try matching each word in 'pat_arg' in 'str' |
| 4521 | while (TRUE) |
| 4522 | { |
| 4523 | if (matchseq) |
| 4524 | complete = TRUE; |
| 4525 | else |
| 4526 | { |
| 4527 | // Extract one word from the pattern (separated by space) |
| 4528 | p = skipwhite(p); |
| 4529 | if (*p == NUL) |
| 4530 | break; |
| 4531 | pat = p; |
| 4532 | while (*p != NUL && !VIM_ISWHITE(PTR2CHAR(p))) |
| 4533 | { |
| 4534 | if (has_mbyte) |
| 4535 | MB_PTR_ADV(p); |
| 4536 | else |
| 4537 | ++p; |
| 4538 | } |
| 4539 | if (*p == NUL) // processed all the words |
| 4540 | complete = TRUE; |
| 4541 | *p = NUL; |
| 4542 | } |
| 4543 | |
| 4544 | score = 0; |
| 4545 | recursionCount = 0; |
| 4546 | matchCount = fuzzy_match_recursive(pat, str, 0, &score, str, len, NULL, |
| 4547 | matches + numMatches, maxMatches - numMatches, |
| 4548 | 0, &recursionCount); |
| 4549 | if (matchCount == 0) |
| 4550 | { |
| 4551 | numMatches = 0; |
| 4552 | break; |
| 4553 | } |
| 4554 | |
| 4555 | // Accumulate the match score and the number of matches |
| 4556 | *outScore += score; |
| 4557 | numMatches += matchCount; |
| 4558 | |
| 4559 | if (complete) |
| 4560 | break; |
| 4561 | |
| 4562 | // try matching the next word |
| 4563 | ++p; |
| 4564 | } |
| 4565 | |
| 4566 | vim_free(save_pat); |
| 4567 | return numMatches != 0; |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4568 | } |
| 4569 | |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 4570 | #if defined(FEAT_EVAL) || defined(FEAT_PROTO) |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4571 | /* |
| 4572 | * Sort the fuzzy matches in the descending order of the match score. |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4573 | * 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] | 4574 | */ |
| 4575 | static int |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4576 | fuzzy_match_item_compare(const void *s1, const void *s2) |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4577 | { |
| 4578 | int v1 = ((fuzzyItem_T *)s1)->score; |
| 4579 | int v2 = ((fuzzyItem_T *)s2)->score; |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4580 | int idx1 = ((fuzzyItem_T *)s1)->idx; |
| 4581 | int idx2 = ((fuzzyItem_T *)s2)->idx; |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4582 | |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4583 | return v1 == v2 ? (idx1 - idx2) : v1 > v2 ? -1 : 1; |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4584 | } |
| 4585 | |
| 4586 | /* |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4587 | * Fuzzy search the string 'str' in a list of 'items' and return the matching |
| 4588 | * strings in 'fmatchlist'. |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4589 | * If 'matchseq' is TRUE, then for multi-word search strings, match all the |
| 4590 | * words in sequence. |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4591 | * If 'items' is a list of strings, then search for 'str' in the list. |
| 4592 | * If 'items' is a list of dicts, then either use 'key' to lookup the string |
| 4593 | * for each item or use 'item_cb' Funcref function to get the string. |
| 4594 | * If 'retmatchpos' is TRUE, then return a list of positions where 'str' |
| 4595 | * matches for each item. |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4596 | */ |
| 4597 | static void |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4598 | fuzzy_match_in_list( |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4599 | list_T *l, |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4600 | char_u *str, |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4601 | int matchseq, |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4602 | char_u *key, |
| 4603 | callback_T *item_cb, |
| 4604 | int retmatchpos, |
Yasuhiro Matsumoto | 9029a6e | 2022-04-16 12:35:35 +0100 | [diff] [blame] | 4605 | list_T *fmatchlist, |
| 4606 | long max_matches) |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4607 | { |
| 4608 | long len; |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4609 | fuzzyItem_T *items; |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4610 | listitem_T *li; |
| 4611 | long i = 0; |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4612 | long match_count = 0; |
Yegappan Lakshmanan | bb01a1e | 2021-04-26 21:17:52 +0200 | [diff] [blame] | 4613 | int_u matches[MAX_FUZZY_MATCHES]; |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4614 | |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4615 | len = list_len(l); |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4616 | if (len == 0) |
| 4617 | return; |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4618 | if (max_matches > 0 && len > max_matches) |
| 4619 | len = max_matches; |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4620 | |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4621 | items = ALLOC_CLEAR_MULT(fuzzyItem_T, len); |
| 4622 | if (items == NULL) |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4623 | return; |
| 4624 | |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4625 | // For all the string items in items, get the fuzzy matching score |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4626 | FOR_ALL_LIST_ITEMS(l, li) |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4627 | { |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4628 | int score; |
| 4629 | char_u *itemstr; |
| 4630 | typval_T rettv; |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4631 | |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4632 | if (max_matches > 0 && match_count >= max_matches) |
| 4633 | break; |
Yasuhiro Matsumoto | 9029a6e | 2022-04-16 12:35:35 +0100 | [diff] [blame] | 4634 | |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4635 | itemstr = NULL; |
| 4636 | rettv.v_type = VAR_UNKNOWN; |
| 4637 | if (li->li_tv.v_type == VAR_STRING) // list of strings |
| 4638 | itemstr = li->li_tv.vval.v_string; |
Dominique Pelle | 7765f5c | 2022-04-10 11:26:53 +0100 | [diff] [blame] | 4639 | else if (li->li_tv.v_type == VAR_DICT |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4640 | && (key != NULL || item_cb->cb_name != NULL)) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4641 | { |
| 4642 | // For a dict, either use the specified key to lookup the string or |
| 4643 | // use the specified callback function to get the string. |
| 4644 | if (key != NULL) |
Bram Moolenaar | d61efa5 | 2022-07-23 09:52:04 +0100 | [diff] [blame] | 4645 | itemstr = dict_get_string(li->li_tv.vval.v_dict, |
| 4646 | (char *)key, FALSE); |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4647 | else |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4648 | { |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4649 | typval_T argv[2]; |
| 4650 | |
| 4651 | // Invoke the supplied callback (if any) to get the dict item |
| 4652 | li->li_tv.vval.v_dict->dv_refcount++; |
| 4653 | argv[0].v_type = VAR_DICT; |
| 4654 | argv[0].vval.v_dict = li->li_tv.vval.v_dict; |
| 4655 | argv[1].v_type = VAR_UNKNOWN; |
| 4656 | if (call_callback(item_cb, -1, &rettv, 1, argv) != FAIL) |
| 4657 | { |
| 4658 | if (rettv.v_type == VAR_STRING) |
| 4659 | itemstr = rettv.vval.v_string; |
| 4660 | } |
| 4661 | dict_unref(li->li_tv.vval.v_dict); |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4662 | } |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4663 | } |
| 4664 | |
| 4665 | if (itemstr != NULL |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4666 | && fuzzy_match(itemstr, str, matchseq, &score, matches, |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4667 | MAX_FUZZY_MATCHES)) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4668 | { |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4669 | items[match_count].idx = match_count; |
| 4670 | items[match_count].item = li; |
| 4671 | items[match_count].score = score; |
| 4672 | |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4673 | // Copy the list of matching positions in itemstr to a list, if |
| 4674 | // 'retmatchpos' is set. |
| 4675 | if (retmatchpos) |
| 4676 | { |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4677 | int j = 0; |
| 4678 | char_u *p; |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4679 | |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4680 | items[match_count].lmatchpos = list_alloc(); |
| 4681 | if (items[match_count].lmatchpos == NULL) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4682 | goto done; |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4683 | |
| 4684 | p = str; |
| 4685 | while (*p != NUL) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4686 | { |
zeertzjq | 9af2bc0 | 2022-05-11 14:15:37 +0100 | [diff] [blame] | 4687 | if (!VIM_ISWHITE(PTR2CHAR(p)) || matchseq) |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4688 | { |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4689 | if (list_append_number(items[match_count].lmatchpos, |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4690 | matches[j]) == FAIL) |
| 4691 | goto done; |
| 4692 | j++; |
| 4693 | } |
| 4694 | if (has_mbyte) |
| 4695 | MB_PTR_ADV(p); |
| 4696 | else |
| 4697 | ++p; |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4698 | } |
| 4699 | } |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4700 | ++match_count; |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4701 | } |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4702 | clear_tv(&rettv); |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4703 | } |
| 4704 | |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4705 | if (match_count > 0) |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4706 | { |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4707 | list_T *retlist; |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4708 | |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4709 | // Sort the list by the descending order of the match score |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4710 | qsort((void *)items, (size_t)match_count, sizeof(fuzzyItem_T), |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4711 | fuzzy_match_item_compare); |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4712 | |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4713 | // For matchfuzzy(), return a list of matched strings. |
| 4714 | // ['str1', 'str2', 'str3'] |
Bram Moolenaar | 9d19e4f | 2021-01-02 18:31:32 +0100 | [diff] [blame] | 4715 | // For matchfuzzypos(), return a list with three items. |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4716 | // The first item is a list of matched strings. The second item |
| 4717 | // 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] | 4718 | // character positions. The third item is a list of matching scores. |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4719 | // [['str1', 'str2', 'str3'], [[1, 3], [1, 3], [1, 3]]] |
| 4720 | if (retmatchpos) |
| 4721 | { |
| 4722 | li = list_find(fmatchlist, 0); |
| 4723 | if (li == NULL || li->li_tv.vval.v_list == NULL) |
| 4724 | goto done; |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4725 | retlist = li->li_tv.vval.v_list; |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4726 | } |
| 4727 | else |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4728 | retlist = fmatchlist; |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4729 | |
| 4730 | // Copy the matching strings with a valid score to the return list |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4731 | for (i = 0; i < match_count; i++) |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4732 | { |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4733 | if (items[i].score == SCORE_NONE) |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4734 | break; |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4735 | list_append_tv(retlist, &items[i].item->li_tv); |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4736 | } |
| 4737 | |
| 4738 | // next copy the list of matching positions |
| 4739 | if (retmatchpos) |
| 4740 | { |
Bram Moolenaar | 9d19e4f | 2021-01-02 18:31:32 +0100 | [diff] [blame] | 4741 | li = list_find(fmatchlist, -2); |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4742 | if (li == NULL || li->li_tv.vval.v_list == NULL) |
| 4743 | goto done; |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4744 | retlist = li->li_tv.vval.v_list; |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4745 | |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4746 | for (i = 0; i < match_count; i++) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4747 | { |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4748 | if (items[i].score == SCORE_NONE) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4749 | break; |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4750 | if (items[i].lmatchpos != NULL |
| 4751 | && list_append_list(retlist, items[i].lmatchpos) |
| 4752 | == FAIL) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4753 | goto done; |
| 4754 | } |
Bram Moolenaar | 9d19e4f | 2021-01-02 18:31:32 +0100 | [diff] [blame] | 4755 | |
| 4756 | // copy the matching scores |
| 4757 | li = list_find(fmatchlist, -1); |
| 4758 | if (li == NULL || li->li_tv.vval.v_list == NULL) |
| 4759 | goto done; |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4760 | retlist = li->li_tv.vval.v_list; |
| 4761 | for (i = 0; i < match_count; i++) |
Bram Moolenaar | 9d19e4f | 2021-01-02 18:31:32 +0100 | [diff] [blame] | 4762 | { |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4763 | if (items[i].score == SCORE_NONE) |
Bram Moolenaar | 9d19e4f | 2021-01-02 18:31:32 +0100 | [diff] [blame] | 4764 | break; |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4765 | if (list_append_number(retlist, items[i].score) == FAIL) |
Bram Moolenaar | 9d19e4f | 2021-01-02 18:31:32 +0100 | [diff] [blame] | 4766 | goto done; |
| 4767 | } |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4768 | } |
| 4769 | } |
| 4770 | |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4771 | done: |
Yegappan Lakshmanan | 047a701 | 2022-04-16 20:42:40 +0100 | [diff] [blame] | 4772 | vim_free(items); |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4773 | } |
| 4774 | |
| 4775 | /* |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4776 | * Do fuzzy matching. Returns the list of matched strings in 'rettv'. |
| 4777 | * If 'retmatchpos' is TRUE, also returns the matching character positions. |
| 4778 | */ |
| 4779 | static void |
| 4780 | do_fuzzymatch(typval_T *argvars, typval_T *rettv, int retmatchpos) |
| 4781 | { |
| 4782 | callback_T cb; |
| 4783 | char_u *key = NULL; |
| 4784 | int ret; |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4785 | int matchseq = FALSE; |
Yasuhiro Matsumoto | 9029a6e | 2022-04-16 12:35:35 +0100 | [diff] [blame] | 4786 | long max_matches = 0; |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4787 | |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 4788 | if (in_vim9script() |
| 4789 | && (check_for_list_arg(argvars, 0) == FAIL |
| 4790 | || check_for_string_arg(argvars, 1) == FAIL |
| 4791 | || check_for_opt_dict_arg(argvars, 2) == FAIL)) |
| 4792 | return; |
| 4793 | |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4794 | CLEAR_POINTER(&cb); |
| 4795 | |
| 4796 | // validate and get the arguments |
| 4797 | if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL) |
| 4798 | { |
Bram Moolenaar | 3a846e6 | 2022-01-01 16:21:00 +0000 | [diff] [blame] | 4799 | semsg(_(e_argument_of_str_must_be_list), |
| 4800 | retmatchpos ? "matchfuzzypos()" : "matchfuzzy()"); |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4801 | return; |
| 4802 | } |
| 4803 | if (argvars[1].v_type != VAR_STRING |
| 4804 | || argvars[1].vval.v_string == NULL) |
| 4805 | { |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 4806 | semsg(_(e_invalid_argument_str), tv_get_string(&argvars[1])); |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4807 | return; |
| 4808 | } |
| 4809 | |
| 4810 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 4811 | { |
| 4812 | dict_T *d; |
| 4813 | dictitem_T *di; |
| 4814 | |
Yegappan Lakshmanan | 04c4c57 | 2022-08-30 19:48:24 +0100 | [diff] [blame] | 4815 | if (check_for_nonnull_dict_arg(argvars, 2) == FAIL) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4816 | return; |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4817 | |
| 4818 | // To search a dict, either a callback function or a key can be |
| 4819 | // specified. |
| 4820 | d = argvars[2].vval.v_dict; |
| 4821 | if ((di = dict_find(d, (char_u *)"key", -1)) != NULL) |
| 4822 | { |
| 4823 | if (di->di_tv.v_type != VAR_STRING |
| 4824 | || di->di_tv.vval.v_string == NULL |
| 4825 | || *di->di_tv.vval.v_string == NUL) |
| 4826 | { |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 4827 | semsg(_(e_invalid_argument_str), tv_get_string(&di->di_tv)); |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4828 | return; |
| 4829 | } |
| 4830 | key = tv_get_string(&di->di_tv); |
| 4831 | } |
| 4832 | else if ((di = dict_find(d, (char_u *)"text_cb", -1)) != NULL) |
| 4833 | { |
| 4834 | cb = get_callback(&di->di_tv); |
| 4835 | if (cb.cb_name == NULL) |
| 4836 | { |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 4837 | semsg(_(e_invalid_value_for_argument_str), "text_cb"); |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4838 | return; |
| 4839 | } |
| 4840 | } |
Kazuyuki Miyagi | 47f1a55 | 2022-06-17 18:30:03 +0100 | [diff] [blame] | 4841 | |
| 4842 | if ((di = dict_find(d, (char_u *)"limit", -1)) != NULL) |
Yasuhiro Matsumoto | 9029a6e | 2022-04-16 12:35:35 +0100 | [diff] [blame] | 4843 | { |
| 4844 | if (di->di_tv.v_type != VAR_NUMBER) |
| 4845 | { |
| 4846 | semsg(_(e_invalid_argument_str), tv_get_string(&di->di_tv)); |
| 4847 | return; |
| 4848 | } |
| 4849 | max_matches = (long)tv_get_number_chk(&di->di_tv, NULL); |
| 4850 | } |
| 4851 | |
Yegappan Lakshmanan | 4829c1c | 2022-04-04 15:16:54 +0100 | [diff] [blame] | 4852 | if (dict_has_key(d, "matchseq")) |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4853 | matchseq = TRUE; |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4854 | } |
| 4855 | |
| 4856 | // get the fuzzy matches |
| 4857 | ret = rettv_list_alloc(rettv); |
Bram Moolenaar | 5ea38d1 | 2022-06-16 21:20:48 +0100 | [diff] [blame] | 4858 | if (ret == FAIL) |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4859 | goto done; |
| 4860 | if (retmatchpos) |
| 4861 | { |
| 4862 | list_T *l; |
| 4863 | |
Bram Moolenaar | 9d19e4f | 2021-01-02 18:31:32 +0100 | [diff] [blame] | 4864 | // For matchfuzzypos(), a list with three items are returned. First |
| 4865 | // item is a list of matching strings, the second item is a list of |
| 4866 | // lists with matching positions within each string and the third item |
| 4867 | // is the list of scores of the matches. |
| 4868 | l = list_alloc(); |
| 4869 | if (l == NULL) |
| 4870 | goto done; |
| 4871 | if (list_append_list(rettv->vval.v_list, l) == FAIL) |
| 4872 | goto done; |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4873 | l = list_alloc(); |
| 4874 | if (l == NULL) |
| 4875 | goto done; |
| 4876 | if (list_append_list(rettv->vval.v_list, l) == FAIL) |
| 4877 | goto done; |
| 4878 | l = list_alloc(); |
| 4879 | if (l == NULL) |
| 4880 | goto done; |
| 4881 | if (list_append_list(rettv->vval.v_list, l) == FAIL) |
| 4882 | goto done; |
| 4883 | } |
| 4884 | |
Bram Moolenaar | 8ded5b6 | 2020-10-23 16:50:30 +0200 | [diff] [blame] | 4885 | 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] | 4886 | matchseq, key, &cb, retmatchpos, rettv->vval.v_list, max_matches); |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4887 | |
| 4888 | done: |
| 4889 | free_callback(&cb); |
| 4890 | } |
| 4891 | |
| 4892 | /* |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4893 | * "matchfuzzy()" function |
| 4894 | */ |
| 4895 | void |
| 4896 | f_matchfuzzy(typval_T *argvars, typval_T *rettv) |
| 4897 | { |
Bram Moolenaar | 4f73b8e | 2020-09-22 20:33:50 +0200 | [diff] [blame] | 4898 | do_fuzzymatch(argvars, rettv, FALSE); |
| 4899 | } |
| 4900 | |
| 4901 | /* |
| 4902 | * "matchfuzzypos()" function |
| 4903 | */ |
| 4904 | void |
| 4905 | f_matchfuzzypos(typval_T *argvars, typval_T *rettv) |
| 4906 | { |
| 4907 | do_fuzzymatch(argvars, rettv, TRUE); |
Bram Moolenaar | 635414d | 2020-09-11 22:25:15 +0200 | [diff] [blame] | 4908 | } |
Bram Moolenaar | e8f5ec0 | 2020-06-01 17:28:35 +0200 | [diff] [blame] | 4909 | #endif |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 4910 | |
| 4911 | /* |
| 4912 | * Same as fuzzy_match_item_compare() except for use with a string match |
| 4913 | */ |
| 4914 | static int |
| 4915 | fuzzy_match_str_compare(const void *s1, const void *s2) |
| 4916 | { |
| 4917 | int v1 = ((fuzmatch_str_T *)s1)->score; |
| 4918 | int v2 = ((fuzmatch_str_T *)s2)->score; |
| 4919 | int idx1 = ((fuzmatch_str_T *)s1)->idx; |
| 4920 | int idx2 = ((fuzmatch_str_T *)s2)->idx; |
| 4921 | |
| 4922 | return v1 == v2 ? (idx1 - idx2) : v1 > v2 ? -1 : 1; |
| 4923 | } |
| 4924 | |
| 4925 | /* |
| 4926 | * Sort fuzzy matches by score |
| 4927 | */ |
| 4928 | static void |
| 4929 | fuzzy_match_str_sort(fuzmatch_str_T *fm, int sz) |
| 4930 | { |
| 4931 | // Sort the list by the descending order of the match score |
| 4932 | qsort((void *)fm, (size_t)sz, sizeof(fuzmatch_str_T), |
| 4933 | fuzzy_match_str_compare); |
| 4934 | } |
| 4935 | |
| 4936 | /* |
| 4937 | * Same as fuzzy_match_item_compare() except for use with a function name |
| 4938 | * string match. <SNR> functions should be sorted to the end. |
| 4939 | */ |
| 4940 | static int |
| 4941 | fuzzy_match_func_compare(const void *s1, const void *s2) |
| 4942 | { |
| 4943 | int v1 = ((fuzmatch_str_T *)s1)->score; |
| 4944 | int v2 = ((fuzmatch_str_T *)s2)->score; |
| 4945 | int idx1 = ((fuzmatch_str_T *)s1)->idx; |
| 4946 | int idx2 = ((fuzmatch_str_T *)s2)->idx; |
| 4947 | char_u *str1 = ((fuzmatch_str_T *)s1)->str; |
| 4948 | char_u *str2 = ((fuzmatch_str_T *)s2)->str; |
| 4949 | |
| 4950 | if (*str1 != '<' && *str2 == '<') return -1; |
| 4951 | if (*str1 == '<' && *str2 != '<') return 1; |
| 4952 | return v1 == v2 ? (idx1 - idx2) : v1 > v2 ? -1 : 1; |
| 4953 | } |
| 4954 | |
| 4955 | /* |
| 4956 | * Sort fuzzy matches of function names by score. |
| 4957 | * <SNR> functions should be sorted to the end. |
| 4958 | */ |
| 4959 | static void |
| 4960 | fuzzy_match_func_sort(fuzmatch_str_T *fm, int sz) |
| 4961 | { |
| 4962 | // Sort the list by the descending order of the match score |
| 4963 | qsort((void *)fm, (size_t)sz, sizeof(fuzmatch_str_T), |
| 4964 | fuzzy_match_func_compare); |
| 4965 | } |
| 4966 | |
| 4967 | /* |
| 4968 | * Fuzzy match 'pat' in 'str'. Returns 0 if there is no match. Otherwise, |
| 4969 | * returns the match score. |
| 4970 | */ |
| 4971 | int |
| 4972 | fuzzy_match_str(char_u *str, char_u *pat) |
| 4973 | { |
| 4974 | int score = 0; |
Yegappan Lakshmanan | 5ec633b | 2022-02-25 15:24:24 +0000 | [diff] [blame] | 4975 | int_u matchpos[MAX_FUZZY_MATCHES]; |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 4976 | |
| 4977 | if (str == NULL || pat == NULL) |
| 4978 | return 0; |
| 4979 | |
Yegappan Lakshmanan | 6caeda2 | 2022-02-27 12:07:30 +0000 | [diff] [blame] | 4980 | fuzzy_match(str, pat, TRUE, &score, matchpos, |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 4981 | sizeof(matchpos) / sizeof(matchpos[0])); |
| 4982 | |
| 4983 | return score; |
| 4984 | } |
| 4985 | |
| 4986 | /* |
Bram Moolenaar | c6e0a5e | 2022-04-10 18:09:06 +0100 | [diff] [blame] | 4987 | * Free an array of fuzzy string matches "fuzmatch[count]". |
| 4988 | */ |
| 4989 | void |
| 4990 | fuzmatch_str_free(fuzmatch_str_T *fuzmatch, int count) |
| 4991 | { |
| 4992 | int i; |
| 4993 | |
| 4994 | if (fuzmatch == NULL) |
| 4995 | return; |
| 4996 | for (i = 0; i < count; ++i) |
| 4997 | vim_free(fuzmatch[i].str); |
| 4998 | vim_free(fuzmatch); |
| 4999 | } |
| 5000 | |
| 5001 | /* |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 5002 | * Copy a list of fuzzy matches into a string list after sorting the matches by |
| 5003 | * the fuzzy score. Frees the memory allocated for 'fuzmatch'. |
| 5004 | * Returns OK on success and FAIL on memory allocation failure. |
| 5005 | */ |
| 5006 | int |
| 5007 | fuzzymatches_to_strmatches( |
| 5008 | fuzmatch_str_T *fuzmatch, |
| 5009 | char_u ***matches, |
| 5010 | int count, |
| 5011 | int funcsort) |
| 5012 | { |
| 5013 | int i; |
| 5014 | |
| 5015 | if (count <= 0) |
| 5016 | return OK; |
| 5017 | |
| 5018 | *matches = ALLOC_MULT(char_u *, count); |
| 5019 | if (*matches == NULL) |
| 5020 | { |
Bram Moolenaar | c6e0a5e | 2022-04-10 18:09:06 +0100 | [diff] [blame] | 5021 | fuzmatch_str_free(fuzmatch, count); |
Yegappan Lakshmanan | 38b85cb | 2022-02-24 13:28:41 +0000 | [diff] [blame] | 5022 | return FAIL; |
| 5023 | } |
| 5024 | |
| 5025 | // Sort the list by the descending order of the match score |
| 5026 | if (funcsort) |
| 5027 | fuzzy_match_func_sort((void *)fuzmatch, (size_t)count); |
| 5028 | else |
| 5029 | fuzzy_match_str_sort((void *)fuzmatch, (size_t)count); |
| 5030 | |
| 5031 | for (i = 0; i < count; i++) |
| 5032 | (*matches)[i] = fuzmatch[i].str; |
| 5033 | vim_free(fuzmatch); |
| 5034 | |
| 5035 | return OK; |
| 5036 | } |