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