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 | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 19 | static int check_linecomment(char_u *line); |
| 20 | static int cls(void); |
| 21 | static int skip_chars(int, int); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 22 | #ifdef FEAT_FIND_ID |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 23 | static void show_pat_in_path(char_u *, int, |
| 24 | int, int, FILE *, linenr_T *, long); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 25 | #endif |
Bram Moolenaar | 8f46e4c | 2019-05-24 22:08:15 +0200 | [diff] [blame] | 26 | static void search_stat(int dirc, pos_T *pos, int show_top_bot_msg, char_u *msgbuf, int recompute); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 27 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 28 | /* |
| 29 | * This file contains various searching-related routines. These fall into |
| 30 | * three groups: |
| 31 | * 1. string searches (for /, ?, n, and N) |
| 32 | * 2. character searches within a single line (for f, F, t, T, etc) |
| 33 | * 3. "other" kinds of searches like the '%' command, and 'word' searches. |
| 34 | */ |
| 35 | |
| 36 | /* |
| 37 | * String searches |
| 38 | * |
| 39 | * The string search functions are divided into two levels: |
| 40 | * lowest: searchit(); uses an pos_T for starting position and found match. |
| 41 | * Highest: do_search(); uses curwin->w_cursor; calls searchit(). |
| 42 | * |
| 43 | * The last search pattern is remembered for repeating the same search. |
| 44 | * This pattern is shared between the :g, :s, ? and / commands. |
| 45 | * This is in search_regcomp(). |
| 46 | * |
| 47 | * The actual string matching is done using a heavily modified version of |
| 48 | * Henry Spencer's regular expression library. See regexp.c. |
| 49 | */ |
| 50 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 51 | /* |
| 52 | * Two search patterns are remembered: One for the :substitute command and |
| 53 | * one for other searches. last_idx points to the one that was used the last |
| 54 | * time. |
| 55 | */ |
Bram Moolenaar | c332816 | 2019-07-23 22:15:25 +0200 | [diff] [blame] | 56 | static spat_T spats[2] = |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 57 | { |
| 58 | {NULL, TRUE, FALSE, {'/', 0, 0, 0L}}, /* last used search pat */ |
| 59 | {NULL, TRUE, FALSE, {'/', 0, 0, 0L}} /* last used substitute pat */ |
| 60 | }; |
| 61 | |
| 62 | static int last_idx = 0; /* index in spats[] for RE_LAST */ |
| 63 | |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 64 | static char_u lastc[2] = {NUL, NUL}; /* last character searched for */ |
| 65 | static int lastcdir = FORWARD; /* last direction of character search */ |
| 66 | static int last_t_cmd = TRUE; /* last search t_cmd */ |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 67 | static char_u lastc_bytes[MB_MAXBYTES + 1]; |
| 68 | static int lastc_bytelen = 1; /* >1 for multi-byte char */ |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 69 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 70 | /* copy of spats[], for keeping the search patterns while executing autocmds */ |
Bram Moolenaar | c332816 | 2019-07-23 22:15:25 +0200 | [diff] [blame] | 71 | static spat_T saved_spats[2]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 72 | # ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | ed8bc78 | 2018-12-01 21:08:21 +0100 | [diff] [blame] | 73 | static int saved_spats_last_idx = 0; |
| 74 | static int saved_spats_no_hlsearch = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 75 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 76 | |
| 77 | static char_u *mr_pattern = NULL; /* pattern used by search_regcomp() */ |
| 78 | #ifdef FEAT_RIGHTLEFT |
| 79 | static int mr_pattern_alloced = FALSE; /* mr_pattern was allocated */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 80 | #endif |
| 81 | |
| 82 | #ifdef FEAT_FIND_ID |
| 83 | /* |
| 84 | * Type used by find_pattern_in_path() to remember which included files have |
| 85 | * been searched already. |
| 86 | */ |
| 87 | typedef struct SearchedFile |
| 88 | { |
| 89 | FILE *fp; /* File pointer */ |
| 90 | char_u *name; /* Full name of file */ |
| 91 | linenr_T lnum; /* Line we were up to in file */ |
| 92 | int matched; /* Found a match in this file */ |
| 93 | } SearchedFile; |
| 94 | #endif |
| 95 | |
| 96 | /* |
| 97 | * translate search pattern for vim_regcomp() |
| 98 | * |
| 99 | * pat_save == RE_SEARCH: save pat in spats[RE_SEARCH].pat (normal search cmd) |
| 100 | * pat_save == RE_SUBST: save pat in spats[RE_SUBST].pat (:substitute command) |
| 101 | * pat_save == RE_BOTH: save pat in both patterns (:global command) |
| 102 | * pat_use == RE_SEARCH: use previous search pattern if "pat" is NULL |
Bram Moolenaar | b8017e7 | 2007-05-10 18:59:07 +0000 | [diff] [blame] | 103 | * pat_use == RE_SUBST: use previous substitute pattern if "pat" is NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 104 | * pat_use == RE_LAST: use last used pattern if "pat" is NULL |
| 105 | * options & SEARCH_HIS: put search string in history |
| 106 | * options & SEARCH_KEEP: keep previous search pattern |
| 107 | * |
| 108 | * returns FAIL if failed, OK otherwise. |
| 109 | */ |
| 110 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 111 | search_regcomp( |
| 112 | char_u *pat, |
| 113 | int pat_save, |
| 114 | int pat_use, |
| 115 | int options, |
| 116 | regmmatch_T *regmatch) /* return: pattern and ignore-case flag */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 117 | { |
| 118 | int magic; |
| 119 | int i; |
| 120 | |
| 121 | rc_did_emsg = FALSE; |
| 122 | magic = p_magic; |
| 123 | |
| 124 | /* |
| 125 | * If no pattern given, use a previously defined pattern. |
| 126 | */ |
| 127 | if (pat == NULL || *pat == NUL) |
| 128 | { |
| 129 | if (pat_use == RE_LAST) |
| 130 | i = last_idx; |
| 131 | else |
| 132 | i = pat_use; |
| 133 | if (spats[i].pat == NULL) /* pattern was never defined */ |
| 134 | { |
| 135 | if (pat_use == RE_SUBST) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 136 | emsg(_(e_nopresub)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 137 | else |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 138 | emsg(_(e_noprevre)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 139 | rc_did_emsg = TRUE; |
| 140 | return FAIL; |
| 141 | } |
| 142 | pat = spats[i].pat; |
| 143 | magic = spats[i].magic; |
| 144 | no_smartcase = spats[i].no_scs; |
| 145 | } |
| 146 | #ifdef FEAT_CMDHIST |
| 147 | else if (options & SEARCH_HIS) /* put new pattern in history */ |
| 148 | add_to_history(HIST_SEARCH, pat, TRUE, NUL); |
| 149 | #endif |
| 150 | |
| 151 | #ifdef FEAT_RIGHTLEFT |
| 152 | if (mr_pattern_alloced) |
| 153 | { |
| 154 | vim_free(mr_pattern); |
| 155 | mr_pattern_alloced = FALSE; |
| 156 | } |
| 157 | |
| 158 | if (curwin->w_p_rl && *curwin->w_p_rlc == 's') |
| 159 | { |
| 160 | char_u *rev_pattern; |
| 161 | |
| 162 | rev_pattern = reverse_text(pat); |
| 163 | if (rev_pattern == NULL) |
| 164 | mr_pattern = pat; /* out of memory, keep normal pattern. */ |
| 165 | else |
| 166 | { |
| 167 | mr_pattern = rev_pattern; |
| 168 | mr_pattern_alloced = TRUE; |
| 169 | } |
| 170 | } |
| 171 | else |
| 172 | #endif |
| 173 | mr_pattern = pat; |
| 174 | |
| 175 | /* |
| 176 | * Save the currently used pattern in the appropriate place, |
| 177 | * unless the pattern should not be remembered. |
| 178 | */ |
Bram Moolenaar | 14177b7 | 2014-01-14 15:53:51 +0100 | [diff] [blame] | 179 | if (!(options & SEARCH_KEEP) && !cmdmod.keeppatterns) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 180 | { |
| 181 | /* search or global command */ |
| 182 | if (pat_save == RE_SEARCH || pat_save == RE_BOTH) |
| 183 | save_re_pat(RE_SEARCH, pat, magic); |
| 184 | /* substitute or global command */ |
| 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 | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 206 | #if defined(FEAT_RIGHTLEFT) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 207 | /* |
| 208 | * Reverse text into allocated memory. |
| 209 | * Returns the allocated string, NULL when out of memory. |
| 210 | */ |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 211 | char_u * |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 212 | reverse_text(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 213 | { |
| 214 | unsigned len; |
| 215 | unsigned s_i, rev_i; |
| 216 | char_u *rev; |
| 217 | |
| 218 | /* |
| 219 | * Reverse the pattern. |
| 220 | */ |
| 221 | len = (unsigned)STRLEN(s); |
| 222 | rev = alloc(len + 1); |
| 223 | if (rev != NULL) |
| 224 | { |
| 225 | rev_i = len; |
| 226 | for (s_i = 0; s_i < len; ++s_i) |
| 227 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 228 | if (has_mbyte) |
| 229 | { |
| 230 | int mb_len; |
| 231 | |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 232 | mb_len = (*mb_ptr2len)(s + s_i); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 233 | rev_i -= mb_len; |
| 234 | mch_memmove(rev + rev_i, s + s_i, mb_len); |
| 235 | s_i += mb_len - 1; |
| 236 | } |
| 237 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 238 | rev[--rev_i] = s[s_i]; |
| 239 | |
| 240 | } |
| 241 | rev[len] = NUL; |
| 242 | } |
| 243 | return rev; |
| 244 | } |
| 245 | #endif |
| 246 | |
Bram Moolenaar | cc2b9d5 | 2014-12-13 03:17:11 +0100 | [diff] [blame] | 247 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 248 | save_re_pat(int idx, char_u *pat, int magic) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 249 | { |
| 250 | if (spats[idx].pat != pat) |
| 251 | { |
| 252 | vim_free(spats[idx].pat); |
| 253 | spats[idx].pat = vim_strsave(pat); |
| 254 | spats[idx].magic = magic; |
| 255 | spats[idx].no_scs = no_smartcase; |
| 256 | last_idx = idx; |
| 257 | #ifdef FEAT_SEARCH_EXTRA |
| 258 | /* If 'hlsearch' set and search pat changed: need redraw. */ |
| 259 | if (p_hls) |
Bram Moolenaar | 1c8f93f | 2006-03-12 22:10:07 +0000 | [diff] [blame] | 260 | redraw_all_later(SOME_VALID); |
Bram Moolenaar | 451fc7b | 2018-04-27 22:53:07 +0200 | [diff] [blame] | 261 | set_no_hlsearch(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 262 | #endif |
| 263 | } |
| 264 | } |
| 265 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 266 | /* |
| 267 | * Save the search patterns, so they can be restored later. |
| 268 | * Used before/after executing autocommands and user functions. |
| 269 | */ |
| 270 | static int save_level = 0; |
| 271 | |
| 272 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 273 | save_search_patterns(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 274 | { |
| 275 | if (save_level++ == 0) |
| 276 | { |
| 277 | saved_spats[0] = spats[0]; |
| 278 | if (spats[0].pat != NULL) |
| 279 | saved_spats[0].pat = vim_strsave(spats[0].pat); |
| 280 | saved_spats[1] = spats[1]; |
| 281 | if (spats[1].pat != NULL) |
| 282 | saved_spats[1].pat = vim_strsave(spats[1].pat); |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 283 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | ed8bc78 | 2018-12-01 21:08:21 +0100 | [diff] [blame] | 284 | saved_spats_last_idx = last_idx; |
| 285 | saved_spats_no_hlsearch = no_hlsearch; |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 286 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
| 290 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 291 | restore_search_patterns(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 292 | { |
| 293 | if (--save_level == 0) |
| 294 | { |
| 295 | vim_free(spats[0].pat); |
| 296 | spats[0] = saved_spats[0]; |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 297 | #if defined(FEAT_EVAL) |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 298 | set_vv_searchforward(); |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 299 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 300 | vim_free(spats[1].pat); |
| 301 | spats[1] = saved_spats[1]; |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 302 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | ed8bc78 | 2018-12-01 21:08:21 +0100 | [diff] [blame] | 303 | last_idx = saved_spats_last_idx; |
| 304 | set_no_hlsearch(saved_spats_no_hlsearch); |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 305 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 306 | } |
| 307 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 308 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 309 | #if defined(EXITFREE) || defined(PROTO) |
| 310 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 311 | free_search_patterns(void) |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 312 | { |
| 313 | vim_free(spats[0].pat); |
| 314 | vim_free(spats[1].pat); |
Bram Moolenaar | f242762 | 2009-04-22 16:45:21 +0000 | [diff] [blame] | 315 | |
| 316 | # ifdef FEAT_RIGHTLEFT |
| 317 | if (mr_pattern_alloced) |
| 318 | { |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 319 | vim_free(mr_pattern); |
| 320 | mr_pattern_alloced = FALSE; |
| 321 | mr_pattern = NULL; |
Bram Moolenaar | f242762 | 2009-04-22 16:45:21 +0000 | [diff] [blame] | 322 | } |
| 323 | # endif |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 324 | } |
| 325 | #endif |
| 326 | |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 327 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | ed8bc78 | 2018-12-01 21:08:21 +0100 | [diff] [blame] | 328 | // copy of spats[RE_SEARCH], for keeping the search patterns while incremental |
| 329 | // searching |
Bram Moolenaar | c332816 | 2019-07-23 22:15:25 +0200 | [diff] [blame] | 330 | static spat_T saved_last_search_spat; |
Bram Moolenaar | ed8bc78 | 2018-12-01 21:08:21 +0100 | [diff] [blame] | 331 | static int did_save_last_search_spat = 0; |
| 332 | static int saved_last_idx = 0; |
| 333 | static int saved_no_hlsearch = 0; |
| 334 | |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 335 | /* |
| 336 | * Save and restore the search pattern for incremental highlight search |
| 337 | * feature. |
| 338 | * |
Bram Moolenaar | c4568ab | 2018-11-16 16:21:05 +0100 | [diff] [blame] | 339 | * It's similar to but different from save_search_patterns() and |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 340 | * restore_search_patterns(), because the search pattern must be restored when |
Bram Moolenaar | c4568ab | 2018-11-16 16:21:05 +0100 | [diff] [blame] | 341 | * canceling incremental searching even if it's called inside user functions. |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 342 | */ |
| 343 | void |
| 344 | save_last_search_pattern(void) |
| 345 | { |
Bram Moolenaar | 01a060d | 2018-11-30 21:57:55 +0100 | [diff] [blame] | 346 | if (did_save_last_search_spat != 0) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 347 | iemsg("did_save_last_search_spat is not zero"); |
Bram Moolenaar | 01a060d | 2018-11-30 21:57:55 +0100 | [diff] [blame] | 348 | else |
| 349 | ++did_save_last_search_spat; |
| 350 | |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 351 | saved_last_search_spat = spats[RE_SEARCH]; |
| 352 | if (spats[RE_SEARCH].pat != NULL) |
| 353 | saved_last_search_spat.pat = vim_strsave(spats[RE_SEARCH].pat); |
| 354 | saved_last_idx = last_idx; |
| 355 | saved_no_hlsearch = no_hlsearch; |
| 356 | } |
| 357 | |
| 358 | void |
| 359 | restore_last_search_pattern(void) |
| 360 | { |
Bram Moolenaar | 01a060d | 2018-11-30 21:57:55 +0100 | [diff] [blame] | 361 | if (did_save_last_search_spat != 1) |
| 362 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 363 | iemsg("did_save_last_search_spat is not one"); |
Bram Moolenaar | 01a060d | 2018-11-30 21:57:55 +0100 | [diff] [blame] | 364 | return; |
| 365 | } |
| 366 | --did_save_last_search_spat; |
| 367 | |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 368 | vim_free(spats[RE_SEARCH].pat); |
| 369 | spats[RE_SEARCH] = saved_last_search_spat; |
Bram Moolenaar | 01a060d | 2018-11-30 21:57:55 +0100 | [diff] [blame] | 370 | saved_last_search_spat.pat = NULL; |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 371 | # if defined(FEAT_EVAL) |
| 372 | set_vv_searchforward(); |
| 373 | # endif |
| 374 | last_idx = saved_last_idx; |
Bram Moolenaar | 451fc7b | 2018-04-27 22:53:07 +0200 | [diff] [blame] | 375 | set_no_hlsearch(saved_no_hlsearch); |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 376 | } |
Bram Moolenaar | d048009 | 2017-11-16 22:20:39 +0100 | [diff] [blame] | 377 | |
| 378 | char_u * |
| 379 | last_search_pattern(void) |
| 380 | { |
| 381 | return spats[RE_SEARCH].pat; |
| 382 | } |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 383 | #endif |
| 384 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 385 | /* |
| 386 | * Return TRUE when case should be ignored for search pattern "pat". |
| 387 | * Uses the 'ignorecase' and 'smartcase' options. |
| 388 | */ |
| 389 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 390 | ignorecase(char_u *pat) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 391 | { |
Bram Moolenaar | 66e29d7 | 2016-08-20 16:57:02 +0200 | [diff] [blame] | 392 | return ignorecase_opt(pat, p_ic, p_scs); |
| 393 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 394 | |
Bram Moolenaar | 66e29d7 | 2016-08-20 16:57:02 +0200 | [diff] [blame] | 395 | /* |
| 396 | * As ignorecase() put pass the "ic" and "scs" flags. |
| 397 | */ |
| 398 | int |
| 399 | ignorecase_opt(char_u *pat, int ic_in, int scs) |
| 400 | { |
| 401 | int ic = ic_in; |
| 402 | |
| 403 | if (ic && !no_smartcase && scs |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 404 | #ifdef FEAT_INS_EXPAND |
Bram Moolenaar | bc0e9ad | 2018-02-09 12:13:34 +0100 | [diff] [blame] | 405 | && !(ctrl_x_mode_not_default() && curbuf->b_p_inf) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 406 | #endif |
| 407 | ) |
Bram Moolenaar | a9dc375 | 2010-07-11 20:46:53 +0200 | [diff] [blame] | 408 | ic = !pat_has_uppercase(pat); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 409 | no_smartcase = FALSE; |
| 410 | |
| 411 | return ic; |
| 412 | } |
| 413 | |
Bram Moolenaar | a9dc375 | 2010-07-11 20:46:53 +0200 | [diff] [blame] | 414 | /* |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 415 | * Return TRUE if pattern "pat" has an uppercase character. |
Bram Moolenaar | a9dc375 | 2010-07-11 20:46:53 +0200 | [diff] [blame] | 416 | */ |
| 417 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 418 | pat_has_uppercase(char_u *pat) |
Bram Moolenaar | a9dc375 | 2010-07-11 20:46:53 +0200 | [diff] [blame] | 419 | { |
| 420 | char_u *p = pat; |
| 421 | |
| 422 | while (*p != NUL) |
| 423 | { |
Bram Moolenaar | a9dc375 | 2010-07-11 20:46:53 +0200 | [diff] [blame] | 424 | int l; |
| 425 | |
| 426 | if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) |
| 427 | { |
| 428 | if (enc_utf8 && utf_isupper(utf_ptr2char(p))) |
| 429 | return TRUE; |
| 430 | p += l; |
| 431 | } |
Bram Moolenaar | 264b74f | 2019-01-24 17:18:42 +0100 | [diff] [blame] | 432 | else if (*p == '\\') |
Bram Moolenaar | a9dc375 | 2010-07-11 20:46:53 +0200 | [diff] [blame] | 433 | { |
| 434 | if (p[1] == '_' && p[2] != NUL) /* skip "\_X" */ |
| 435 | p += 3; |
| 436 | else if (p[1] == '%' && p[2] != NUL) /* skip "\%X" */ |
| 437 | p += 3; |
| 438 | else if (p[1] != NUL) /* skip "\X" */ |
| 439 | p += 2; |
| 440 | else |
| 441 | p += 1; |
| 442 | } |
| 443 | else if (MB_ISUPPER(*p)) |
| 444 | return TRUE; |
| 445 | else |
| 446 | ++p; |
| 447 | } |
| 448 | return FALSE; |
| 449 | } |
| 450 | |
Bram Moolenaar | 113e107 | 2019-01-20 15:30:40 +0100 | [diff] [blame] | 451 | #if defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 452 | char_u * |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 453 | last_csearch(void) |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 454 | { |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 455 | return lastc_bytes; |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 459 | last_csearch_forward(void) |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 460 | { |
| 461 | return lastcdir == FORWARD; |
| 462 | } |
| 463 | |
| 464 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 465 | last_csearch_until(void) |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 466 | { |
| 467 | return last_t_cmd == TRUE; |
| 468 | } |
| 469 | |
| 470 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 471 | set_last_csearch(int c, char_u *s UNUSED, int len UNUSED) |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 472 | { |
| 473 | *lastc = c; |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 474 | lastc_bytelen = len; |
| 475 | if (len) |
| 476 | memcpy(lastc_bytes, s, len); |
| 477 | else |
| 478 | vim_memset(lastc_bytes, 0, sizeof(lastc_bytes)); |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 479 | } |
Bram Moolenaar | 113e107 | 2019-01-20 15:30:40 +0100 | [diff] [blame] | 480 | #endif |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 481 | |
| 482 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 483 | set_csearch_direction(int cdir) |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 484 | { |
| 485 | lastcdir = cdir; |
| 486 | } |
| 487 | |
| 488 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 489 | set_csearch_until(int t_cmd) |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 490 | { |
| 491 | last_t_cmd = t_cmd; |
| 492 | } |
| 493 | |
| 494 | char_u * |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 495 | last_search_pat(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 496 | { |
| 497 | return spats[last_idx].pat; |
| 498 | } |
| 499 | |
| 500 | /* |
| 501 | * Reset search direction to forward. For "gd" and "gD" commands. |
| 502 | */ |
| 503 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 504 | reset_search_dir(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 505 | { |
| 506 | spats[0].off.dir = '/'; |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 507 | #if defined(FEAT_EVAL) |
| 508 | set_vv_searchforward(); |
| 509 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | #if defined(FEAT_EVAL) || defined(FEAT_VIMINFO) |
| 513 | /* |
| 514 | * Set the last search pattern. For ":let @/ =" and viminfo. |
| 515 | * Also set the saved search pattern, so that this works in an autocommand. |
| 516 | */ |
| 517 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 518 | set_last_search_pat( |
| 519 | char_u *s, |
| 520 | int idx, |
| 521 | int magic, |
| 522 | int setlast) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 523 | { |
| 524 | vim_free(spats[idx].pat); |
| 525 | /* An empty string means that nothing should be matched. */ |
| 526 | if (*s == NUL) |
| 527 | spats[idx].pat = NULL; |
| 528 | else |
| 529 | spats[idx].pat = vim_strsave(s); |
| 530 | spats[idx].magic = magic; |
| 531 | spats[idx].no_scs = FALSE; |
| 532 | spats[idx].off.dir = '/'; |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 533 | #if defined(FEAT_EVAL) |
| 534 | set_vv_searchforward(); |
| 535 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 536 | spats[idx].off.line = FALSE; |
| 537 | spats[idx].off.end = FALSE; |
| 538 | spats[idx].off.off = 0; |
| 539 | if (setlast) |
| 540 | last_idx = idx; |
| 541 | if (save_level) |
| 542 | { |
| 543 | vim_free(saved_spats[idx].pat); |
| 544 | saved_spats[idx] = spats[0]; |
| 545 | if (spats[idx].pat == NULL) |
| 546 | saved_spats[idx].pat = NULL; |
| 547 | else |
| 548 | saved_spats[idx].pat = vim_strsave(spats[idx].pat); |
Bram Moolenaar | 975880b | 2019-03-03 14:42:11 +0100 | [diff] [blame] | 549 | # ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | ed8bc78 | 2018-12-01 21:08:21 +0100 | [diff] [blame] | 550 | saved_spats_last_idx = last_idx; |
Bram Moolenaar | 975880b | 2019-03-03 14:42:11 +0100 | [diff] [blame] | 551 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 552 | } |
| 553 | # ifdef FEAT_SEARCH_EXTRA |
| 554 | /* If 'hlsearch' set and search pat changed: need redraw. */ |
| 555 | if (p_hls && idx == last_idx && !no_hlsearch) |
Bram Moolenaar | 1c8f93f | 2006-03-12 22:10:07 +0000 | [diff] [blame] | 556 | redraw_all_later(SOME_VALID); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 557 | # endif |
| 558 | } |
| 559 | #endif |
| 560 | |
| 561 | #ifdef FEAT_SEARCH_EXTRA |
| 562 | /* |
| 563 | * Get a regexp program for the last used search pattern. |
| 564 | * This is used for highlighting all matches in a window. |
| 565 | * Values returned in regmatch->regprog and regmatch->rmm_ic. |
| 566 | */ |
| 567 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 568 | last_pat_prog(regmmatch_T *regmatch) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 569 | { |
| 570 | if (spats[last_idx].pat == NULL) |
| 571 | { |
| 572 | regmatch->regprog = NULL; |
| 573 | return; |
| 574 | } |
| 575 | ++emsg_off; /* So it doesn't beep if bad expr */ |
| 576 | (void)search_regcomp((char_u *)"", 0, last_idx, SEARCH_KEEP, regmatch); |
| 577 | --emsg_off; |
| 578 | } |
| 579 | #endif |
| 580 | |
| 581 | /* |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 582 | * Lowest level search function. |
Bram Moolenaar | 5d24a22 | 2018-12-23 19:10:09 +0100 | [diff] [blame] | 583 | * Search for 'count'th occurrence of pattern "pat" in direction "dir". |
| 584 | * Start at position "pos" and return the found position in "pos". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 585 | * |
| 586 | * if (options & SEARCH_MSG) == 0 don't give any messages |
| 587 | * if (options & SEARCH_MSG) == SEARCH_NFMSG don't give 'notfound' messages |
| 588 | * if (options & SEARCH_MSG) == SEARCH_MSG give all messages |
| 589 | * if (options & SEARCH_HIS) put search pattern in history |
| 590 | * if (options & SEARCH_END) return position at end of match |
| 591 | * if (options & SEARCH_START) accept match at pos itself |
| 592 | * if (options & SEARCH_KEEP) keep previous search pattern |
| 593 | * if (options & SEARCH_FOLD) match only once in a closed fold |
| 594 | * if (options & SEARCH_PEEK) check for typed char, cancel search |
Bram Moolenaar | ad4d8a1 | 2015-12-28 19:20:36 +0100 | [diff] [blame] | 595 | * if (options & SEARCH_COL) start at pos->col instead of zero |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 596 | * |
| 597 | * Return FAIL (zero) for failure, non-zero for success. |
| 598 | * When FEAT_EVAL is defined, returns the index of the first matching |
| 599 | * subpattern plus one; one if there was none. |
| 600 | */ |
| 601 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 602 | searchit( |
| 603 | win_T *win, /* window to search in; can be NULL for a |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 604 | buffer without a window! */ |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 605 | buf_T *buf, |
| 606 | pos_T *pos, |
Bram Moolenaar | 5d24a22 | 2018-12-23 19:10:09 +0100 | [diff] [blame] | 607 | pos_T *end_pos, // set to end of the match, unless NULL |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 608 | int dir, |
| 609 | char_u *pat, |
| 610 | long count, |
| 611 | int options, |
| 612 | int pat_use, /* which pattern to use when "pat" is empty */ |
| 613 | linenr_T stop_lnum, /* stop after this line number when != 0 */ |
Bram Moolenaar | fbd0b0a | 2017-06-17 18:44:21 +0200 | [diff] [blame] | 614 | proftime_T *tm UNUSED, /* timeout limit or NULL */ |
| 615 | int *timed_out UNUSED) /* set when timed out or NULL */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 616 | { |
| 617 | int found; |
| 618 | linenr_T lnum; /* no init to shut up Apollo cc */ |
Bram Moolenaar | ad4d8a1 | 2015-12-28 19:20:36 +0100 | [diff] [blame] | 619 | colnr_T col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 620 | regmmatch_T regmatch; |
| 621 | char_u *ptr; |
| 622 | colnr_T matchcol; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 623 | lpos_T endpos; |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 624 | lpos_T matchpos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 625 | int loop; |
| 626 | pos_T start_pos; |
| 627 | int at_first_line; |
| 628 | int extra_col; |
Bram Moolenaar | 5f1e68b | 2015-07-10 14:43:35 +0200 | [diff] [blame] | 629 | int start_char_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 630 | int match_ok; |
| 631 | long nmatched; |
| 632 | int submatch = 0; |
Bram Moolenaar | a3dfccc | 2014-11-27 17:29:56 +0100 | [diff] [blame] | 633 | int first_match = TRUE; |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 634 | int save_called_emsg = called_emsg; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 635 | #ifdef FEAT_SEARCH_EXTRA |
| 636 | int break_loop = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 637 | #endif |
| 638 | |
| 639 | if (search_regcomp(pat, RE_SEARCH, pat_use, |
| 640 | (options & (SEARCH_HIS + SEARCH_KEEP)), ®match) == FAIL) |
| 641 | { |
| 642 | if ((options & SEARCH_MSG) && !rc_did_emsg) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 643 | semsg(_("E383: Invalid search string: %s"), mr_pattern); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 644 | return FAIL; |
| 645 | } |
| 646 | |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 647 | /* |
| 648 | * find the string |
| 649 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 650 | called_emsg = FALSE; |
| 651 | do /* loop for count */ |
| 652 | { |
Bram Moolenaar | a3dfccc | 2014-11-27 17:29:56 +0100 | [diff] [blame] | 653 | /* When not accepting a match at the start position set "extra_col" to |
| 654 | * a non-zero value. Don't do that when starting at MAXCOL, since |
| 655 | * MAXCOL + 1 is zero. */ |
Bram Moolenaar | 5f1e68b | 2015-07-10 14:43:35 +0200 | [diff] [blame] | 656 | if (pos->col == MAXCOL) |
| 657 | start_char_len = 0; |
Bram Moolenaar | a3dfccc | 2014-11-27 17:29:56 +0100 | [diff] [blame] | 658 | /* 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] | 659 | else if (has_mbyte |
| 660 | && pos->lnum >= 1 && pos->lnum <= buf->b_ml.ml_line_count |
| 661 | && pos->col < MAXCOL - 2) |
Bram Moolenaar | a3dfccc | 2014-11-27 17:29:56 +0100 | [diff] [blame] | 662 | { |
Bram Moolenaar | 82846a0 | 2018-02-09 18:09:54 +0100 | [diff] [blame] | 663 | ptr = ml_get_buf(buf, pos->lnum, FALSE); |
Bram Moolenaar | 8846ac5 | 2018-02-09 19:24:01 +0100 | [diff] [blame] | 664 | if ((int)STRLEN(ptr) <= pos->col) |
Bram Moolenaar | 5f1e68b | 2015-07-10 14:43:35 +0200 | [diff] [blame] | 665 | start_char_len = 1; |
Bram Moolenaar | a3dfccc | 2014-11-27 17:29:56 +0100 | [diff] [blame] | 666 | else |
Bram Moolenaar | 82846a0 | 2018-02-09 18:09:54 +0100 | [diff] [blame] | 667 | start_char_len = (*mb_ptr2len)(ptr + pos->col); |
Bram Moolenaar | a3dfccc | 2014-11-27 17:29:56 +0100 | [diff] [blame] | 668 | } |
Bram Moolenaar | a3dfccc | 2014-11-27 17:29:56 +0100 | [diff] [blame] | 669 | else |
Bram Moolenaar | 5f1e68b | 2015-07-10 14:43:35 +0200 | [diff] [blame] | 670 | start_char_len = 1; |
| 671 | if (dir == FORWARD) |
| 672 | { |
| 673 | if (options & SEARCH_START) |
| 674 | extra_col = 0; |
| 675 | else |
| 676 | extra_col = start_char_len; |
| 677 | } |
| 678 | else |
| 679 | { |
| 680 | if (options & SEARCH_START) |
| 681 | extra_col = start_char_len; |
| 682 | else |
| 683 | extra_col = 0; |
| 684 | } |
Bram Moolenaar | a3dfccc | 2014-11-27 17:29:56 +0100 | [diff] [blame] | 685 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 686 | start_pos = *pos; /* remember start pos for detecting no match */ |
| 687 | found = 0; /* default: not found */ |
| 688 | at_first_line = TRUE; /* default: start in first line */ |
| 689 | if (pos->lnum == 0) /* correct lnum for when starting in line 0 */ |
| 690 | { |
| 691 | pos->lnum = 1; |
| 692 | pos->col = 0; |
| 693 | at_first_line = FALSE; /* not in first line now */ |
| 694 | } |
| 695 | |
| 696 | /* |
| 697 | * Start searching in current line, unless searching backwards and |
| 698 | * we're in column 0. |
Bram Moolenaar | 7a42fa3 | 2007-07-10 11:28:55 +0000 | [diff] [blame] | 699 | * If we are searching backwards, in column 0, and not including the |
| 700 | * current position, gain some efficiency by skipping back a line. |
| 701 | * Otherwise begin the search in the current line. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 702 | */ |
Bram Moolenaar | 7a42fa3 | 2007-07-10 11:28:55 +0000 | [diff] [blame] | 703 | if (dir == BACKWARD && start_pos.col == 0 |
| 704 | && (options & SEARCH_START) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 705 | { |
| 706 | lnum = pos->lnum - 1; |
| 707 | at_first_line = FALSE; |
| 708 | } |
| 709 | else |
| 710 | lnum = pos->lnum; |
| 711 | |
| 712 | for (loop = 0; loop <= 1; ++loop) /* loop twice if 'wrapscan' set */ |
| 713 | { |
| 714 | for ( ; lnum > 0 && lnum <= buf->b_ml.ml_line_count; |
| 715 | lnum += dir, at_first_line = FALSE) |
| 716 | { |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 717 | /* Stop after checking "stop_lnum", if it's set. */ |
| 718 | if (stop_lnum != 0 && (dir == FORWARD |
| 719 | ? lnum > stop_lnum : lnum < stop_lnum)) |
| 720 | break; |
Bram Moolenaar | 7692929 | 2008-01-06 19:07:36 +0000 | [diff] [blame] | 721 | #ifdef FEAT_RELTIME |
| 722 | /* Stop after passing the "tm" time limit. */ |
| 723 | if (tm != NULL && profile_passed_limit(tm)) |
| 724 | break; |
| 725 | #endif |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 726 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 727 | /* |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 728 | * Look for a match somewhere in line "lnum". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 729 | */ |
Bram Moolenaar | ad4d8a1 | 2015-12-28 19:20:36 +0100 | [diff] [blame] | 730 | col = at_first_line && (options & SEARCH_COL) ? pos->col |
| 731 | : (colnr_T)0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 732 | nmatched = vim_regexec_multi(®match, win, buf, |
Bram Moolenaar | ad4d8a1 | 2015-12-28 19:20:36 +0100 | [diff] [blame] | 733 | lnum, col, |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 734 | #ifdef FEAT_RELTIME |
Bram Moolenaar | fbd0b0a | 2017-06-17 18:44:21 +0200 | [diff] [blame] | 735 | tm, timed_out |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 736 | #else |
Bram Moolenaar | fbd0b0a | 2017-06-17 18:44:21 +0200 | [diff] [blame] | 737 | NULL, NULL |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 738 | #endif |
| 739 | ); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 740 | /* Abort searching on an error (e.g., out of stack). */ |
Bram Moolenaar | fbd0b0a | 2017-06-17 18:44:21 +0200 | [diff] [blame] | 741 | if (called_emsg |
| 742 | #ifdef FEAT_RELTIME |
| 743 | || (timed_out != NULL && *timed_out) |
| 744 | #endif |
| 745 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 746 | break; |
| 747 | if (nmatched > 0) |
| 748 | { |
| 749 | /* match may actually be in another line when using \zs */ |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 750 | matchpos = regmatch.startpos[0]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 751 | endpos = regmatch.endpos[0]; |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 752 | #ifdef FEAT_EVAL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 753 | submatch = first_submatch(®match); |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 754 | #endif |
Bram Moolenaar | 5bcbd53 | 2008-02-20 12:43:01 +0000 | [diff] [blame] | 755 | /* "lnum" may be past end of buffer for "\n\zs". */ |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 756 | if (lnum + matchpos.lnum > buf->b_ml.ml_line_count) |
| 757 | ptr = (char_u *)""; |
| 758 | else |
| 759 | ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 760 | |
| 761 | /* |
| 762 | * Forward search in the first line: match should be after |
| 763 | * the start position. If not, continue at the end of the |
| 764 | * match (this is vi compatible) or on the next char. |
| 765 | */ |
| 766 | if (dir == FORWARD && at_first_line) |
| 767 | { |
| 768 | match_ok = TRUE; |
| 769 | /* |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 770 | * When the match starts in a next line it's certainly |
| 771 | * past the start position. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 772 | * When match lands on a NUL the cursor will be put |
| 773 | * one back afterwards, compare with that position, |
| 774 | * otherwise "/$" will get stuck on end of line. |
| 775 | */ |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 776 | while (matchpos.lnum == 0 |
Bram Moolenaar | a3dfccc | 2014-11-27 17:29:56 +0100 | [diff] [blame] | 777 | && ((options & SEARCH_END) && first_match |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 778 | ? (nmatched == 1 |
| 779 | && (int)endpos.col - 1 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 780 | < (int)start_pos.col + extra_col) |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 781 | : ((int)matchpos.col |
| 782 | - (ptr[matchpos.col] == NUL) |
| 783 | < (int)start_pos.col + extra_col))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 784 | { |
| 785 | /* |
| 786 | * If vi-compatible searching, continue at the end |
| 787 | * of the match, otherwise continue one position |
| 788 | * forward. |
| 789 | */ |
| 790 | if (vim_strchr(p_cpo, CPO_SEARCH) != NULL) |
| 791 | { |
| 792 | if (nmatched > 1) |
| 793 | { |
| 794 | /* end is in next line, thus no match in |
| 795 | * this line */ |
| 796 | match_ok = FALSE; |
| 797 | break; |
| 798 | } |
| 799 | matchcol = endpos.col; |
| 800 | /* for empty match: advance one char */ |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 801 | if (matchcol == matchpos.col |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 802 | && ptr[matchcol] != NUL) |
| 803 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 804 | if (has_mbyte) |
| 805 | matchcol += |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 806 | (*mb_ptr2len)(ptr + matchcol); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 807 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 808 | ++matchcol; |
| 809 | } |
| 810 | } |
| 811 | else |
| 812 | { |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 813 | matchcol = matchpos.col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 814 | if (ptr[matchcol] != NUL) |
| 815 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 816 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 817 | matchcol += (*mb_ptr2len)(ptr |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 818 | + matchcol); |
| 819 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 820 | ++matchcol; |
| 821 | } |
| 822 | } |
Bram Moolenaar | 7bcb30e | 2013-04-03 21:14:29 +0200 | [diff] [blame] | 823 | if (matchcol == 0 && (options & SEARCH_START)) |
Bram Moolenaar | db333a5 | 2013-03-19 15:27:48 +0100 | [diff] [blame] | 824 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 825 | if (ptr[matchcol] == NUL |
| 826 | || (nmatched = vim_regexec_multi(®match, |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 827 | win, buf, lnum + matchpos.lnum, |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 828 | matchcol, |
| 829 | #ifdef FEAT_RELTIME |
Bram Moolenaar | fbd0b0a | 2017-06-17 18:44:21 +0200 | [diff] [blame] | 830 | tm, timed_out |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 831 | #else |
Bram Moolenaar | fbd0b0a | 2017-06-17 18:44:21 +0200 | [diff] [blame] | 832 | NULL, NULL |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 833 | #endif |
| 834 | )) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 835 | { |
| 836 | match_ok = FALSE; |
| 837 | break; |
| 838 | } |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 839 | matchpos = regmatch.startpos[0]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 840 | endpos = regmatch.endpos[0]; |
| 841 | # ifdef FEAT_EVAL |
| 842 | submatch = first_submatch(®match); |
| 843 | # endif |
| 844 | |
| 845 | /* Need to get the line pointer again, a |
| 846 | * multi-line search may have made it invalid. */ |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 847 | ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 848 | } |
| 849 | if (!match_ok) |
| 850 | continue; |
| 851 | } |
| 852 | if (dir == BACKWARD) |
| 853 | { |
| 854 | /* |
| 855 | * Now, if there are multiple matches on this line, |
| 856 | * we have to get the last one. Or the last one before |
| 857 | * the cursor, if we're on that line. |
| 858 | * When putting the new cursor at the end, compare |
| 859 | * relative to the end of the match. |
| 860 | */ |
| 861 | match_ok = FALSE; |
| 862 | for (;;) |
| 863 | { |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 864 | /* Remember a position that is before the start |
| 865 | * position, we use it if it's the last match in |
| 866 | * the line. Always accept a position after |
| 867 | * wrapping around. */ |
| 868 | if (loop |
| 869 | || ((options & SEARCH_END) |
| 870 | ? (lnum + regmatch.endpos[0].lnum |
| 871 | < start_pos.lnum |
| 872 | || (lnum + regmatch.endpos[0].lnum |
| 873 | == start_pos.lnum |
| 874 | && (int)regmatch.endpos[0].col - 1 |
Bram Moolenaar | 5f1e68b | 2015-07-10 14:43:35 +0200 | [diff] [blame] | 875 | < (int)start_pos.col |
| 876 | + extra_col)) |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 877 | : (lnum + regmatch.startpos[0].lnum |
| 878 | < start_pos.lnum |
| 879 | || (lnum + regmatch.startpos[0].lnum |
| 880 | == start_pos.lnum |
| 881 | && (int)regmatch.startpos[0].col |
Bram Moolenaar | 5f1e68b | 2015-07-10 14:43:35 +0200 | [diff] [blame] | 882 | < (int)start_pos.col |
| 883 | + extra_col)))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 884 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 885 | match_ok = TRUE; |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 886 | matchpos = regmatch.startpos[0]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 887 | endpos = regmatch.endpos[0]; |
| 888 | # ifdef FEAT_EVAL |
| 889 | submatch = first_submatch(®match); |
| 890 | # endif |
| 891 | } |
| 892 | else |
| 893 | break; |
| 894 | |
| 895 | /* |
| 896 | * We found a valid match, now check if there is |
| 897 | * another one after it. |
| 898 | * If vi-compatible searching, continue at the end |
| 899 | * of the match, otherwise continue one position |
| 900 | * forward. |
| 901 | */ |
| 902 | if (vim_strchr(p_cpo, CPO_SEARCH) != NULL) |
| 903 | { |
| 904 | if (nmatched > 1) |
| 905 | break; |
| 906 | matchcol = endpos.col; |
| 907 | /* for empty match: advance one char */ |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 908 | if (matchcol == matchpos.col |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 909 | && ptr[matchcol] != NUL) |
| 910 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 911 | if (has_mbyte) |
| 912 | matchcol += |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 913 | (*mb_ptr2len)(ptr + matchcol); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 914 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 915 | ++matchcol; |
| 916 | } |
| 917 | } |
| 918 | else |
| 919 | { |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 920 | /* Stop when the match is in a next line. */ |
| 921 | if (matchpos.lnum > 0) |
| 922 | break; |
| 923 | matchcol = matchpos.col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 924 | if (ptr[matchcol] != NUL) |
| 925 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 926 | if (has_mbyte) |
| 927 | matchcol += |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 928 | (*mb_ptr2len)(ptr + matchcol); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 929 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 930 | ++matchcol; |
| 931 | } |
| 932 | } |
| 933 | if (ptr[matchcol] == NUL |
| 934 | || (nmatched = vim_regexec_multi(®match, |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 935 | win, buf, lnum + matchpos.lnum, |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 936 | matchcol, |
| 937 | #ifdef FEAT_RELTIME |
Bram Moolenaar | fbd0b0a | 2017-06-17 18:44:21 +0200 | [diff] [blame] | 938 | tm, timed_out |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 939 | #else |
Bram Moolenaar | fbd0b0a | 2017-06-17 18:44:21 +0200 | [diff] [blame] | 940 | NULL, NULL |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 941 | #endif |
| 942 | )) == 0) |
Bram Moolenaar | 9d32276 | 2018-02-09 16:04:25 +0100 | [diff] [blame] | 943 | { |
| 944 | #ifdef FEAT_RELTIME |
| 945 | /* If the search timed out, we did find a match |
| 946 | * but it might be the wrong one, so that's not |
| 947 | * OK. */ |
| 948 | if (timed_out != NULL && *timed_out) |
| 949 | match_ok = FALSE; |
| 950 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 951 | break; |
Bram Moolenaar | 9d32276 | 2018-02-09 16:04:25 +0100 | [diff] [blame] | 952 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 953 | |
| 954 | /* Need to get the line pointer again, a |
| 955 | * multi-line search may have made it invalid. */ |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 956 | ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | /* |
| 960 | * If there is only a match after the cursor, skip |
| 961 | * this match. |
| 962 | */ |
| 963 | if (!match_ok) |
| 964 | continue; |
| 965 | } |
| 966 | |
Bram Moolenaar | 5bcbd53 | 2008-02-20 12:43:01 +0000 | [diff] [blame] | 967 | /* With the SEARCH_END option move to the last character |
| 968 | * of the match. Don't do it for an empty match, end |
| 969 | * should be same as start then. */ |
Bram Moolenaar | 7bcb30e | 2013-04-03 21:14:29 +0200 | [diff] [blame] | 970 | if ((options & SEARCH_END) && !(options & SEARCH_NOOF) |
Bram Moolenaar | 5bcbd53 | 2008-02-20 12:43:01 +0000 | [diff] [blame] | 971 | && !(matchpos.lnum == endpos.lnum |
| 972 | && matchpos.col == endpos.col)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 973 | { |
Bram Moolenaar | 5bcbd53 | 2008-02-20 12:43:01 +0000 | [diff] [blame] | 974 | /* For a match in the first column, set the position |
| 975 | * on the NUL in the previous line. */ |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 976 | pos->lnum = lnum + endpos.lnum; |
Bram Moolenaar | 5bcbd53 | 2008-02-20 12:43:01 +0000 | [diff] [blame] | 977 | pos->col = endpos.col; |
| 978 | if (endpos.col == 0) |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 979 | { |
Bram Moolenaar | 5bcbd53 | 2008-02-20 12:43:01 +0000 | [diff] [blame] | 980 | if (pos->lnum > 1) /* just in case */ |
| 981 | { |
| 982 | --pos->lnum; |
| 983 | pos->col = (colnr_T)STRLEN(ml_get_buf(buf, |
| 984 | pos->lnum, FALSE)); |
| 985 | } |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 986 | } |
Bram Moolenaar | 5bcbd53 | 2008-02-20 12:43:01 +0000 | [diff] [blame] | 987 | else |
| 988 | { |
| 989 | --pos->col; |
Bram Moolenaar | 5bcbd53 | 2008-02-20 12:43:01 +0000 | [diff] [blame] | 990 | if (has_mbyte |
| 991 | && pos->lnum <= buf->b_ml.ml_line_count) |
| 992 | { |
| 993 | ptr = ml_get_buf(buf, pos->lnum, FALSE); |
| 994 | pos->col -= (*mb_head_off)(ptr, ptr + pos->col); |
| 995 | } |
Bram Moolenaar | 5bcbd53 | 2008-02-20 12:43:01 +0000 | [diff] [blame] | 996 | } |
Bram Moolenaar | 5d24a22 | 2018-12-23 19:10:09 +0100 | [diff] [blame] | 997 | if (end_pos != NULL) |
| 998 | { |
| 999 | end_pos->lnum = lnum + matchpos.lnum; |
| 1000 | end_pos->col = matchpos.col; |
| 1001 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1002 | } |
| 1003 | else |
| 1004 | { |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 1005 | pos->lnum = lnum + matchpos.lnum; |
| 1006 | pos->col = matchpos.col; |
Bram Moolenaar | 5d24a22 | 2018-12-23 19:10:09 +0100 | [diff] [blame] | 1007 | if (end_pos != NULL) |
| 1008 | { |
| 1009 | end_pos->lnum = lnum + endpos.lnum; |
| 1010 | end_pos->col = endpos.col; |
| 1011 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1012 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1013 | pos->coladd = 0; |
Bram Moolenaar | 5d24a22 | 2018-12-23 19:10:09 +0100 | [diff] [blame] | 1014 | if (end_pos != NULL) |
| 1015 | end_pos->coladd = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1016 | found = 1; |
Bram Moolenaar | a3dfccc | 2014-11-27 17:29:56 +0100 | [diff] [blame] | 1017 | first_match = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1018 | |
| 1019 | /* Set variables used for 'incsearch' highlighting. */ |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 1020 | search_match_lines = endpos.lnum - matchpos.lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1021 | search_match_endcol = endpos.col; |
| 1022 | break; |
| 1023 | } |
| 1024 | line_breakcheck(); /* stop if ctrl-C typed */ |
| 1025 | if (got_int) |
| 1026 | break; |
| 1027 | |
| 1028 | #ifdef FEAT_SEARCH_EXTRA |
| 1029 | /* Cancel searching if a character was typed. Used for |
| 1030 | * 'incsearch'. Don't check too often, that would slowdown |
| 1031 | * searching too much. */ |
| 1032 | if ((options & SEARCH_PEEK) |
| 1033 | && ((lnum - pos->lnum) & 0x3f) == 0 |
| 1034 | && char_avail()) |
| 1035 | { |
| 1036 | break_loop = TRUE; |
| 1037 | break; |
| 1038 | } |
| 1039 | #endif |
| 1040 | |
| 1041 | if (loop && lnum == start_pos.lnum) |
| 1042 | break; /* if second loop, stop where started */ |
| 1043 | } |
| 1044 | at_first_line = FALSE; |
| 1045 | |
| 1046 | /* |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1047 | * Stop the search if wrapscan isn't set, "stop_lnum" is |
| 1048 | * specified, after an interrupt, after a match and after looping |
| 1049 | * twice. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1050 | */ |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1051 | if (!p_ws || stop_lnum != 0 || got_int || called_emsg |
Bram Moolenaar | fbd0b0a | 2017-06-17 18:44:21 +0200 | [diff] [blame] | 1052 | #ifdef FEAT_RELTIME |
| 1053 | || (timed_out != NULL && *timed_out) |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 1054 | #endif |
Bram Moolenaar | fbd0b0a | 2017-06-17 18:44:21 +0200 | [diff] [blame] | 1055 | #ifdef FEAT_SEARCH_EXTRA |
| 1056 | || break_loop |
| 1057 | #endif |
| 1058 | || found || loop) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1059 | break; |
| 1060 | |
| 1061 | /* |
| 1062 | * If 'wrapscan' is set we continue at the other end of the file. |
| 1063 | * If 'shortmess' does not contain 's', we give a message. |
| 1064 | * This message is also remembered in keep_msg for when the screen |
| 1065 | * is redrawn. The keep_msg is cleared whenever another message is |
| 1066 | * written. |
| 1067 | */ |
| 1068 | if (dir == BACKWARD) /* start second loop at the other end */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1069 | lnum = buf->b_ml.ml_line_count; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1070 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1071 | lnum = 1; |
Bram Moolenaar | 92d640f | 2005-09-05 22:11:52 +0000 | [diff] [blame] | 1072 | if (!shortmess(SHM_SEARCH) && (options & SEARCH_MSG)) |
| 1073 | give_warning((char_u *)_(dir == BACKWARD |
| 1074 | ? top_bot_msg : bot_top_msg), TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1075 | } |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 1076 | if (got_int || called_emsg |
Bram Moolenaar | fbd0b0a | 2017-06-17 18:44:21 +0200 | [diff] [blame] | 1077 | #ifdef FEAT_RELTIME |
| 1078 | || (timed_out != NULL && *timed_out) |
| 1079 | #endif |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 1080 | #ifdef FEAT_SEARCH_EXTRA |
| 1081 | || break_loop |
| 1082 | #endif |
| 1083 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1084 | break; |
| 1085 | } |
| 1086 | while (--count > 0 && found); /* stop after count matches or no match */ |
| 1087 | |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 1088 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1089 | |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 1090 | called_emsg |= save_called_emsg; |
| 1091 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1092 | if (!found) /* did not find it */ |
| 1093 | { |
| 1094 | if (got_int) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 1095 | emsg(_(e_interr)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1096 | else if ((options & SEARCH_MSG) == SEARCH_MSG) |
| 1097 | { |
| 1098 | if (p_ws) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 1099 | semsg(_(e_patnotf2), mr_pattern); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1100 | else if (lnum == 0) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 1101 | semsg(_("E384: search hit TOP without match for: %s"), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1102 | mr_pattern); |
| 1103 | else |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 1104 | semsg(_("E385: search hit BOTTOM without match for: %s"), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1105 | mr_pattern); |
| 1106 | } |
| 1107 | return FAIL; |
| 1108 | } |
| 1109 | |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 1110 | /* A pattern like "\n\zs" may go past the last line. */ |
| 1111 | if (pos->lnum > buf->b_ml.ml_line_count) |
| 1112 | { |
| 1113 | pos->lnum = buf->b_ml.ml_line_count; |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1114 | pos->col = (int)STRLEN(ml_get_buf(buf, pos->lnum, FALSE)); |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 1115 | if (pos->col > 0) |
| 1116 | --pos->col; |
| 1117 | } |
| 1118 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1119 | return submatch + 1; |
| 1120 | } |
| 1121 | |
| 1122 | #ifdef FEAT_EVAL |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 1123 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 1124 | set_search_direction(int cdir) |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 1125 | { |
| 1126 | spats[0].off.dir = cdir; |
| 1127 | } |
| 1128 | |
| 1129 | static void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 1130 | set_vv_searchforward(void) |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 1131 | { |
| 1132 | set_vim_var_nr(VV_SEARCHFORWARD, (long)(spats[0].off.dir == '/')); |
| 1133 | } |
| 1134 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1135 | /* |
| 1136 | * Return the number of the first subpat that matched. |
Bram Moolenaar | ad4d8a1 | 2015-12-28 19:20:36 +0100 | [diff] [blame] | 1137 | * Return zero if none of them matched. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1138 | */ |
| 1139 | static int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 1140 | first_submatch(regmmatch_T *rp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1141 | { |
| 1142 | int submatch; |
| 1143 | |
| 1144 | for (submatch = 1; ; ++submatch) |
| 1145 | { |
| 1146 | if (rp->startpos[submatch].lnum >= 0) |
| 1147 | break; |
| 1148 | if (submatch == 9) |
| 1149 | { |
| 1150 | submatch = 0; |
| 1151 | break; |
| 1152 | } |
| 1153 | } |
| 1154 | return submatch; |
| 1155 | } |
| 1156 | #endif |
| 1157 | |
| 1158 | /* |
| 1159 | * Highest level string search function. |
Bram Moolenaar | b8017e7 | 2007-05-10 18:59:07 +0000 | [diff] [blame] | 1160 | * Search for the 'count'th occurrence of pattern 'pat' in direction 'dirc' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1161 | * If 'dirc' is 0: use previous dir. |
| 1162 | * If 'pat' is NULL or empty : use previous string. |
| 1163 | * If 'options & SEARCH_REV' : go in reverse of previous dir. |
| 1164 | * If 'options & SEARCH_ECHO': echo the search command and handle options |
| 1165 | * If 'options & SEARCH_MSG' : may give error message |
| 1166 | * If 'options & SEARCH_OPT' : interpret optional flags |
| 1167 | * If 'options & SEARCH_HIS' : put search pattern in history |
| 1168 | * If 'options & SEARCH_NOOF': don't add offset to position |
| 1169 | * If 'options & SEARCH_MARK': set previous context mark |
| 1170 | * If 'options & SEARCH_KEEP': keep previous search pattern |
| 1171 | * If 'options & SEARCH_START': accept match at curpos itself |
| 1172 | * If 'options & SEARCH_PEEK': check for typed char, cancel search |
| 1173 | * |
| 1174 | * Careful: If spats[0].off.line == TRUE and spats[0].off.off == 0 this |
| 1175 | * makes the movement linewise without moving the match position. |
| 1176 | * |
Bram Moolenaar | b6c2735 | 2015-03-05 19:57:49 +0100 | [diff] [blame] | 1177 | * 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] | 1178 | */ |
| 1179 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 1180 | do_search( |
| 1181 | oparg_T *oap, /* can be NULL */ |
| 1182 | int dirc, /* '/' or '?' */ |
| 1183 | char_u *pat, |
| 1184 | long count, |
| 1185 | int options, |
Bram Moolenaar | fbd0b0a | 2017-06-17 18:44:21 +0200 | [diff] [blame] | 1186 | proftime_T *tm, /* timeout limit or NULL */ |
| 1187 | int *timed_out) /* flag set on timeout or NULL */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1188 | { |
| 1189 | pos_T pos; /* position of the last match */ |
| 1190 | char_u *searchstr; |
Bram Moolenaar | c332816 | 2019-07-23 22:15:25 +0200 | [diff] [blame] | 1191 | soffset_T old_off; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1192 | int retval; /* Return value */ |
| 1193 | char_u *p; |
| 1194 | long c; |
| 1195 | char_u *dircp; |
| 1196 | char_u *strcopy = NULL; |
| 1197 | char_u *ps; |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1198 | char_u *msgbuf = NULL; |
| 1199 | size_t len; |
Bram Moolenaar | 8f46e4c | 2019-05-24 22:08:15 +0200 | [diff] [blame] | 1200 | int has_offset = FALSE; |
Bram Moolenaar | b6cb26f | 2019-05-07 21:34:37 +0200 | [diff] [blame] | 1201 | #define SEARCH_STAT_BUF_LEN 12 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1202 | |
| 1203 | /* |
| 1204 | * A line offset is not remembered, this is vi compatible. |
| 1205 | */ |
| 1206 | if (spats[0].off.line && vim_strchr(p_cpo, CPO_LINEOFF) != NULL) |
| 1207 | { |
| 1208 | spats[0].off.line = FALSE; |
| 1209 | spats[0].off.off = 0; |
| 1210 | } |
| 1211 | |
| 1212 | /* |
| 1213 | * Save the values for when (options & SEARCH_KEEP) is used. |
| 1214 | * (there is no "if ()" around this because gcc wants them initialized) |
| 1215 | */ |
| 1216 | old_off = spats[0].off; |
| 1217 | |
| 1218 | pos = curwin->w_cursor; /* start searching at the cursor position */ |
| 1219 | |
| 1220 | /* |
| 1221 | * Find out the direction of the search. |
| 1222 | */ |
| 1223 | if (dirc == 0) |
| 1224 | dirc = spats[0].off.dir; |
| 1225 | else |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 1226 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1227 | spats[0].off.dir = dirc; |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 1228 | #if defined(FEAT_EVAL) |
| 1229 | set_vv_searchforward(); |
| 1230 | #endif |
| 1231 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1232 | if (options & SEARCH_REV) |
| 1233 | { |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 1234 | #ifdef MSWIN |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1235 | /* There is a bug in the Visual C++ 2.2 compiler which means that |
| 1236 | * dirc always ends up being '/' */ |
| 1237 | dirc = (dirc == '/') ? '?' : '/'; |
| 1238 | #else |
| 1239 | if (dirc == '/') |
| 1240 | dirc = '?'; |
| 1241 | else |
| 1242 | dirc = '/'; |
| 1243 | #endif |
| 1244 | } |
| 1245 | |
| 1246 | #ifdef FEAT_FOLDING |
| 1247 | /* If the cursor is in a closed fold, don't find another match in the same |
| 1248 | * fold. */ |
| 1249 | if (dirc == '/') |
| 1250 | { |
| 1251 | if (hasFolding(pos.lnum, NULL, &pos.lnum)) |
| 1252 | pos.col = MAXCOL - 2; /* avoid overflow when adding 1 */ |
| 1253 | } |
| 1254 | else |
| 1255 | { |
| 1256 | if (hasFolding(pos.lnum, &pos.lnum, NULL)) |
| 1257 | pos.col = 0; |
| 1258 | } |
| 1259 | #endif |
| 1260 | |
| 1261 | #ifdef FEAT_SEARCH_EXTRA |
| 1262 | /* |
| 1263 | * Turn 'hlsearch' highlighting back on. |
| 1264 | */ |
| 1265 | if (no_hlsearch && !(options & SEARCH_KEEP)) |
| 1266 | { |
Bram Moolenaar | 1c8f93f | 2006-03-12 22:10:07 +0000 | [diff] [blame] | 1267 | redraw_all_later(SOME_VALID); |
Bram Moolenaar | 451fc7b | 2018-04-27 22:53:07 +0200 | [diff] [blame] | 1268 | set_no_hlsearch(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1269 | } |
| 1270 | #endif |
| 1271 | |
| 1272 | /* |
| 1273 | * Repeat the search when pattern followed by ';', e.g. "/foo/;?bar". |
| 1274 | */ |
| 1275 | for (;;) |
| 1276 | { |
Bram Moolenaar | c7a10b3 | 2019-05-06 21:37:18 +0200 | [diff] [blame] | 1277 | int show_top_bot_msg = FALSE; |
| 1278 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1279 | searchstr = pat; |
| 1280 | dircp = NULL; |
| 1281 | /* use previous pattern */ |
| 1282 | if (pat == NULL || *pat == NUL || *pat == dirc) |
| 1283 | { |
| 1284 | if (spats[RE_SEARCH].pat == NULL) /* no previous pattern */ |
| 1285 | { |
Bram Moolenaar | ea683da | 2016-09-09 21:41:34 +0200 | [diff] [blame] | 1286 | searchstr = spats[RE_SUBST].pat; |
| 1287 | if (searchstr == NULL) |
Bram Moolenaar | b4b0a08 | 2011-02-25 18:38:36 +0100 | [diff] [blame] | 1288 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 1289 | emsg(_(e_noprevre)); |
Bram Moolenaar | b4b0a08 | 2011-02-25 18:38:36 +0100 | [diff] [blame] | 1290 | retval = 0; |
| 1291 | goto end_do_search; |
| 1292 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1293 | } |
Bram Moolenaar | b4b0a08 | 2011-02-25 18:38:36 +0100 | [diff] [blame] | 1294 | else |
| 1295 | { |
| 1296 | /* make search_regcomp() use spats[RE_SEARCH].pat */ |
| 1297 | searchstr = (char_u *)""; |
| 1298 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | if (pat != NULL && *pat != NUL) /* look for (new) offset */ |
| 1302 | { |
| 1303 | /* |
| 1304 | * Find end of regular expression. |
| 1305 | * If there is a matching '/' or '?', toss it. |
| 1306 | */ |
| 1307 | ps = strcopy; |
| 1308 | p = skip_regexp(pat, dirc, (int)p_magic, &strcopy); |
| 1309 | if (strcopy != ps) |
| 1310 | { |
| 1311 | /* made a copy of "pat" to change "\?" to "?" */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 1312 | searchcmdlen += (int)(STRLEN(pat) - STRLEN(strcopy)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1313 | pat = strcopy; |
| 1314 | searchstr = strcopy; |
| 1315 | } |
| 1316 | if (*p == dirc) |
| 1317 | { |
| 1318 | dircp = p; /* remember where we put the NUL */ |
| 1319 | *p++ = NUL; |
| 1320 | } |
| 1321 | spats[0].off.line = FALSE; |
| 1322 | spats[0].off.end = FALSE; |
| 1323 | spats[0].off.off = 0; |
| 1324 | /* |
| 1325 | * Check for a line offset or a character offset. |
| 1326 | * For get_address (echo off) we don't check for a character |
| 1327 | * offset, because it is meaningless and the 's' could be a |
| 1328 | * substitute command. |
| 1329 | */ |
| 1330 | if (*p == '+' || *p == '-' || VIM_ISDIGIT(*p)) |
| 1331 | spats[0].off.line = TRUE; |
| 1332 | else if ((options & SEARCH_OPT) && |
| 1333 | (*p == 'e' || *p == 's' || *p == 'b')) |
| 1334 | { |
| 1335 | if (*p == 'e') /* end */ |
| 1336 | spats[0].off.end = SEARCH_END; |
| 1337 | ++p; |
| 1338 | } |
| 1339 | if (VIM_ISDIGIT(*p) || *p == '+' || *p == '-') /* got an offset */ |
| 1340 | { |
| 1341 | /* 'nr' or '+nr' or '-nr' */ |
| 1342 | if (VIM_ISDIGIT(*p) || VIM_ISDIGIT(*(p + 1))) |
| 1343 | spats[0].off.off = atol((char *)p); |
| 1344 | else if (*p == '-') /* single '-' */ |
| 1345 | spats[0].off.off = -1; |
| 1346 | else /* single '+' */ |
| 1347 | spats[0].off.off = 1; |
| 1348 | ++p; |
| 1349 | while (VIM_ISDIGIT(*p)) /* skip number */ |
| 1350 | ++p; |
| 1351 | } |
| 1352 | |
| 1353 | /* compute length of search command for get_address() */ |
| 1354 | searchcmdlen += (int)(p - pat); |
| 1355 | |
| 1356 | pat = p; /* put pat after search command */ |
| 1357 | } |
| 1358 | |
| 1359 | if ((options & SEARCH_ECHO) && messaging() |
| 1360 | && !cmd_silent && msg_silent == 0) |
| 1361 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1362 | char_u *trunc; |
Bram Moolenaar | 984f031 | 2019-05-24 13:11:47 +0200 | [diff] [blame] | 1363 | char_u off_buf[40]; |
Bram Moolenaar | d33a764 | 2019-05-24 17:56:14 +0200 | [diff] [blame] | 1364 | size_t off_len = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1365 | |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1366 | // Compute msg_row early. |
| 1367 | msg_start(); |
| 1368 | |
Bram Moolenaar | 984f031 | 2019-05-24 13:11:47 +0200 | [diff] [blame] | 1369 | // Get the offset, so we know how long it is. |
| 1370 | if (spats[0].off.line || spats[0].off.end || spats[0].off.off) |
| 1371 | { |
| 1372 | p = off_buf; |
| 1373 | *p++ = dirc; |
| 1374 | if (spats[0].off.end) |
| 1375 | *p++ = 'e'; |
| 1376 | else if (!spats[0].off.line) |
| 1377 | *p++ = 's'; |
| 1378 | if (spats[0].off.off > 0 || spats[0].off.line) |
| 1379 | *p++ = '+'; |
| 1380 | *p = NUL; |
| 1381 | if (spats[0].off.off != 0 || spats[0].off.line) |
| 1382 | sprintf((char *)p, "%ld", spats[0].off.off); |
| 1383 | off_len = STRLEN(off_buf); |
| 1384 | } |
| 1385 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1386 | if (*searchstr == NUL) |
Bram Moolenaar | 2fb8f68 | 2018-12-01 13:14:45 +0100 | [diff] [blame] | 1387 | p = spats[0].pat; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1388 | else |
| 1389 | p = searchstr; |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1390 | |
| 1391 | if (!shortmess(SHM_SEARCHCOUNT)) |
| 1392 | { |
| 1393 | // Reserve enough space for the search pattern + offset + |
Bram Moolenaar | 984f031 | 2019-05-24 13:11:47 +0200 | [diff] [blame] | 1394 | // search stat. Use all the space available, so that the |
| 1395 | // search state is right aligned. If there is not enough space |
| 1396 | // msg_strtrunc() will shorten in the middle. |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1397 | if (msg_scrolled != 0) |
| 1398 | // Use all the columns. |
| 1399 | len = (int)(Rows - msg_row) * Columns - 1; |
| 1400 | else |
| 1401 | // Use up to 'showcmd' column. |
| 1402 | len = (int)(Rows - msg_row - 1) * Columns + sc_col - 1; |
Bram Moolenaar | 984f031 | 2019-05-24 13:11:47 +0200 | [diff] [blame] | 1403 | if (len < STRLEN(p) + off_len + SEARCH_STAT_BUF_LEN + 3) |
| 1404 | len = STRLEN(p) + off_len + SEARCH_STAT_BUF_LEN + 3; |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1405 | } |
| 1406 | else |
| 1407 | // Reserve enough space for the search pattern + offset. |
Bram Moolenaar | 984f031 | 2019-05-24 13:11:47 +0200 | [diff] [blame] | 1408 | len = STRLEN(p) + off_len + 3; |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1409 | |
Bram Moolenaar | 51e1438 | 2019-05-25 20:21:28 +0200 | [diff] [blame] | 1410 | msgbuf = alloc(len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1411 | if (msgbuf != NULL) |
| 1412 | { |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1413 | vim_memset(msgbuf, ' ', len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1414 | msgbuf[0] = dirc; |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1415 | msgbuf[len - 1] = NUL; |
| 1416 | |
Bram Moolenaar | cafda4f | 2005-09-06 19:25:11 +0000 | [diff] [blame] | 1417 | if (enc_utf8 && utf_iscomposing(utf_ptr2char(p))) |
| 1418 | { |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1419 | // Use a space to draw the composing char on. |
Bram Moolenaar | cafda4f | 2005-09-06 19:25:11 +0000 | [diff] [blame] | 1420 | msgbuf[1] = ' '; |
Bram Moolenaar | b3de6c4 | 2019-05-05 13:02:28 +0200 | [diff] [blame] | 1421 | mch_memmove(msgbuf + 2, p, STRLEN(p)); |
Bram Moolenaar | cafda4f | 2005-09-06 19:25:11 +0000 | [diff] [blame] | 1422 | } |
| 1423 | else |
Bram Moolenaar | b3de6c4 | 2019-05-05 13:02:28 +0200 | [diff] [blame] | 1424 | mch_memmove(msgbuf + 1, p, STRLEN(p)); |
Bram Moolenaar | 984f031 | 2019-05-24 13:11:47 +0200 | [diff] [blame] | 1425 | if (off_len > 0) |
| 1426 | mch_memmove(msgbuf + STRLEN(p) + 1, off_buf, off_len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1427 | |
Bram Moolenaar | 984f031 | 2019-05-24 13:11:47 +0200 | [diff] [blame] | 1428 | trunc = msg_strtrunc(msgbuf, TRUE); |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1429 | if (trunc != NULL) |
| 1430 | { |
| 1431 | vim_free(msgbuf); |
| 1432 | msgbuf = trunc; |
| 1433 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1434 | |
| 1435 | #ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1436 | // The search pattern could be shown on the right in rightleft |
| 1437 | // mode, but the 'ruler' and 'showcmd' area use it too, thus |
| 1438 | // it would be blanked out again very soon. Show it on the |
| 1439 | // left, but do reverse the text. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1440 | if (curwin->w_p_rl && *curwin->w_p_rlc == 's') |
| 1441 | { |
| 1442 | char_u *r; |
Bram Moolenaar | db294ad | 2019-06-06 12:49:29 +0200 | [diff] [blame] | 1443 | size_t pat_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1444 | |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1445 | r = reverse_text(msgbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1446 | if (r != NULL) |
| 1447 | { |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1448 | vim_free(msgbuf); |
| 1449 | msgbuf = r; |
| 1450 | // move reversed text to beginning of buffer |
| 1451 | while (*r != NUL && *r == ' ') |
| 1452 | r++; |
Bram Moolenaar | db294ad | 2019-06-06 12:49:29 +0200 | [diff] [blame] | 1453 | pat_len = msgbuf + STRLEN(msgbuf) - r; |
| 1454 | mch_memmove(msgbuf, r, pat_len); |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1455 | // overwrite old text |
Bram Moolenaar | db294ad | 2019-06-06 12:49:29 +0200 | [diff] [blame] | 1456 | if ((size_t)(r - msgbuf) >= pat_len) |
| 1457 | vim_memset(r, ' ', pat_len); |
| 1458 | else |
| 1459 | vim_memset(msgbuf + pat_len, ' ', r - msgbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1460 | } |
| 1461 | } |
| 1462 | #endif |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1463 | msg_outtrans(msgbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1464 | msg_clr_eos(); |
| 1465 | msg_check(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1466 | |
| 1467 | gotocmdline(FALSE); |
| 1468 | out_flush(); |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1469 | msg_nowait = TRUE; // don't wait for this message |
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; |
| 1487 | if (c) /* at start of buffer */ |
| 1488 | { |
| 1489 | pos.lnum = 0; /* allow lnum == 0 here */ |
| 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; |
| 1498 | if (c) /* at end of buffer */ |
| 1499 | { |
| 1500 | pos.lnum = curbuf->b_ml.ml_line_count + 1; |
| 1501 | pos.col = 0; |
| 1502 | } |
| 1503 | } |
| 1504 | } |
| 1505 | |
Bram Moolenaar | 14184a3 | 2019-02-16 15:10:30 +0100 | [diff] [blame] | 1506 | c = searchit(curwin, curbuf, &pos, NULL, |
| 1507 | dirc == '/' ? FORWARD : BACKWARD, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1508 | searchstr, count, spats[0].off.end + (options & |
| 1509 | (SEARCH_KEEP + SEARCH_PEEK + SEARCH_HIS |
| 1510 | + SEARCH_MSG + SEARCH_START |
| 1511 | + ((pat != NULL && *pat == ';') ? 0 : SEARCH_NOOF))), |
Bram Moolenaar | fbd0b0a | 2017-06-17 18:44:21 +0200 | [diff] [blame] | 1512 | RE_LAST, (linenr_T)0, tm, timed_out); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1513 | |
| 1514 | if (dircp != NULL) |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1515 | *dircp = dirc; // restore second '/' or '?' for normal_cmd() |
| 1516 | |
| 1517 | if (!shortmess(SHM_SEARCH) |
| 1518 | && ((dirc == '/' && LT_POS(pos, curwin->w_cursor)) |
| 1519 | || (dirc == '?' && LT_POS(curwin->w_cursor, pos)))) |
Bram Moolenaar | c7a10b3 | 2019-05-06 21:37:18 +0200 | [diff] [blame] | 1520 | show_top_bot_msg = TRUE; |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1521 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1522 | if (c == FAIL) |
| 1523 | { |
| 1524 | retval = 0; |
| 1525 | goto end_do_search; |
| 1526 | } |
| 1527 | if (spats[0].off.end && oap != NULL) |
| 1528 | oap->inclusive = TRUE; /* 'e' includes last character */ |
| 1529 | |
| 1530 | retval = 1; /* pattern found */ |
| 1531 | |
| 1532 | /* |
| 1533 | * Add character and/or line offset |
| 1534 | */ |
Bram Moolenaar | 9160f30 | 2006-08-29 15:58:12 +0000 | [diff] [blame] | 1535 | if (!(options & SEARCH_NOOF) || (pat != NULL && *pat == ';')) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1536 | { |
Bram Moolenaar | 8f46e4c | 2019-05-24 22:08:15 +0200 | [diff] [blame] | 1537 | pos_T org_pos = pos; |
| 1538 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1539 | if (spats[0].off.line) /* Add the offset to the line number. */ |
| 1540 | { |
| 1541 | c = pos.lnum + spats[0].off.off; |
| 1542 | if (c < 1) |
| 1543 | pos.lnum = 1; |
| 1544 | else if (c > curbuf->b_ml.ml_line_count) |
| 1545 | pos.lnum = curbuf->b_ml.ml_line_count; |
| 1546 | else |
| 1547 | pos.lnum = c; |
| 1548 | pos.col = 0; |
| 1549 | |
| 1550 | retval = 2; /* pattern found, line offset added */ |
| 1551 | } |
Bram Moolenaar | ed20346 | 2004-06-16 11:19:22 +0000 | [diff] [blame] | 1552 | else if (pos.col < MAXCOL - 2) /* just in case */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1553 | { |
| 1554 | /* to the right, check for end of file */ |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 1555 | c = spats[0].off.off; |
| 1556 | if (c > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1557 | { |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 1558 | while (c-- > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1559 | if (incl(&pos) == -1) |
| 1560 | break; |
| 1561 | } |
| 1562 | /* to the left, check for start of file */ |
| 1563 | else |
| 1564 | { |
Bram Moolenaar | 8c8de83 | 2008-06-24 22:58:06 +0000 | [diff] [blame] | 1565 | while (c++ < 0) |
| 1566 | if (decl(&pos) == -1) |
| 1567 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1568 | } |
| 1569 | } |
Bram Moolenaar | 8f46e4c | 2019-05-24 22:08:15 +0200 | [diff] [blame] | 1570 | if (!EQUAL_POS(pos, org_pos)) |
| 1571 | has_offset = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1572 | } |
| 1573 | |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1574 | // Show [1/15] if 'S' is not in 'shortmess'. |
| 1575 | if ((options & SEARCH_ECHO) |
| 1576 | && messaging() |
| 1577 | && !(cmd_silent + msg_silent) |
| 1578 | && c != FAIL |
| 1579 | && !shortmess(SHM_SEARCHCOUNT) |
| 1580 | && msgbuf != NULL) |
Bram Moolenaar | 8f46e4c | 2019-05-24 22:08:15 +0200 | [diff] [blame] | 1581 | search_stat(dirc, &pos, show_top_bot_msg, msgbuf, |
| 1582 | (count != 1 || has_offset)); |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1583 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1584 | /* |
| 1585 | * The search command can be followed by a ';' to do another search. |
| 1586 | * For example: "/pat/;/foo/+3;?bar" |
| 1587 | * This is like doing another search command, except: |
| 1588 | * - The remembered direction '/' or '?' is from the first search. |
| 1589 | * - When an error happens the cursor isn't moved at all. |
| 1590 | * Don't do this when called by get_address() (it handles ';' itself). |
| 1591 | */ |
| 1592 | if (!(options & SEARCH_OPT) || pat == NULL || *pat != ';') |
| 1593 | break; |
| 1594 | |
| 1595 | dirc = *++pat; |
| 1596 | if (dirc != '?' && dirc != '/') |
| 1597 | { |
| 1598 | retval = 0; |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 1599 | emsg(_("E386: Expected '?' or '/' after ';'")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1600 | goto end_do_search; |
| 1601 | } |
| 1602 | ++pat; |
| 1603 | } |
| 1604 | |
| 1605 | if (options & SEARCH_MARK) |
| 1606 | setpcmark(); |
| 1607 | curwin->w_cursor = pos; |
| 1608 | curwin->w_set_curswant = TRUE; |
| 1609 | |
| 1610 | end_do_search: |
Bram Moolenaar | ac8400d | 2014-01-14 21:31:34 +0100 | [diff] [blame] | 1611 | if ((options & SEARCH_KEEP) || cmdmod.keeppatterns) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1612 | spats[0].off = old_off; |
| 1613 | vim_free(strcopy); |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 1614 | vim_free(msgbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1615 | |
| 1616 | return retval; |
| 1617 | } |
| 1618 | |
| 1619 | #if defined(FEAT_INS_EXPAND) || defined(PROTO) |
| 1620 | /* |
| 1621 | * search_for_exact_line(buf, pos, dir, pat) |
| 1622 | * |
| 1623 | * Search for a line starting with the given pattern (ignoring leading |
Bram Moolenaar | 8ad80de | 2017-06-05 16:01:59 +0200 | [diff] [blame] | 1624 | * white-space), starting from pos and going in direction "dir". "pos" will |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1625 | * contain the position of the match found. Blank lines match only if |
Bram Moolenaar | 8ad80de | 2017-06-05 16:01:59 +0200 | [diff] [blame] | 1626 | * 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] | 1627 | * Return OK for success, or FAIL if no line found. |
| 1628 | */ |
| 1629 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 1630 | search_for_exact_line( |
| 1631 | buf_T *buf, |
| 1632 | pos_T *pos, |
| 1633 | int dir, |
| 1634 | char_u *pat) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1635 | { |
| 1636 | linenr_T start = 0; |
| 1637 | char_u *ptr; |
| 1638 | char_u *p; |
| 1639 | |
| 1640 | if (buf->b_ml.ml_line_count == 0) |
| 1641 | return FAIL; |
| 1642 | for (;;) |
| 1643 | { |
| 1644 | pos->lnum += dir; |
| 1645 | if (pos->lnum < 1) |
| 1646 | { |
| 1647 | if (p_ws) |
| 1648 | { |
| 1649 | pos->lnum = buf->b_ml.ml_line_count; |
| 1650 | if (!shortmess(SHM_SEARCH)) |
| 1651 | give_warning((char_u *)_(top_bot_msg), TRUE); |
| 1652 | } |
| 1653 | else |
| 1654 | { |
| 1655 | pos->lnum = 1; |
| 1656 | break; |
| 1657 | } |
| 1658 | } |
| 1659 | else if (pos->lnum > buf->b_ml.ml_line_count) |
| 1660 | { |
| 1661 | if (p_ws) |
| 1662 | { |
| 1663 | pos->lnum = 1; |
| 1664 | if (!shortmess(SHM_SEARCH)) |
| 1665 | give_warning((char_u *)_(bot_top_msg), TRUE); |
| 1666 | } |
| 1667 | else |
| 1668 | { |
| 1669 | pos->lnum = 1; |
| 1670 | break; |
| 1671 | } |
| 1672 | } |
| 1673 | if (pos->lnum == start) |
| 1674 | break; |
| 1675 | if (start == 0) |
| 1676 | start = pos->lnum; |
| 1677 | ptr = ml_get_buf(buf, pos->lnum, FALSE); |
| 1678 | p = skipwhite(ptr); |
| 1679 | pos->col = (colnr_T) (p - ptr); |
| 1680 | |
| 1681 | /* when adding lines the matching line may be empty but it is not |
| 1682 | * ignored because we are interested in the next line -- Acevedo */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1683 | if ((compl_cont_status & CONT_ADDING) |
| 1684 | && !(compl_cont_status & CONT_SOL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1685 | { |
| 1686 | if ((p_ic ? MB_STRICMP(p, pat) : STRCMP(p, pat)) == 0) |
| 1687 | return OK; |
| 1688 | } |
| 1689 | else if (*p != NUL) /* ignore empty lines */ |
| 1690 | { /* expanding lines or words */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1691 | if ((p_ic ? MB_STRNICMP(p, pat, compl_length) |
| 1692 | : STRNCMP(p, pat, compl_length)) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1693 | return OK; |
| 1694 | } |
| 1695 | } |
| 1696 | return FAIL; |
| 1697 | } |
| 1698 | #endif /* FEAT_INS_EXPAND */ |
| 1699 | |
| 1700 | /* |
| 1701 | * Character Searches |
| 1702 | */ |
| 1703 | |
| 1704 | /* |
| 1705 | * Search for a character in a line. If "t_cmd" is FALSE, move to the |
| 1706 | * position of the character, otherwise move to just before the char. |
| 1707 | * Do this "cap->count1" times. |
| 1708 | * Return FAIL or OK. |
| 1709 | */ |
| 1710 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 1711 | searchc(cmdarg_T *cap, int t_cmd) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1712 | { |
| 1713 | int c = cap->nchar; /* char to search for */ |
| 1714 | int dir = cap->arg; /* TRUE for searching forward */ |
| 1715 | long count = cap->count1; /* repeat count */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1716 | int col; |
| 1717 | char_u *p; |
| 1718 | int len; |
Bram Moolenaar | 8b3e033 | 2011-06-26 05:36:34 +0200 | [diff] [blame] | 1719 | int stop = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1720 | |
| 1721 | if (c != NUL) /* normal search: remember args for repeat */ |
| 1722 | { |
| 1723 | if (!KeyStuffed) /* don't remember when redoing */ |
| 1724 | { |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 1725 | *lastc = c; |
| 1726 | set_csearch_direction(dir); |
| 1727 | set_csearch_until(t_cmd); |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 1728 | lastc_bytelen = (*mb_char2bytes)(c, lastc_bytes); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1729 | if (cap->ncharC1 != 0) |
| 1730 | { |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 1731 | lastc_bytelen += (*mb_char2bytes)(cap->ncharC1, |
| 1732 | lastc_bytes + lastc_bytelen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1733 | if (cap->ncharC2 != 0) |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 1734 | lastc_bytelen += (*mb_char2bytes)(cap->ncharC2, |
| 1735 | lastc_bytes + lastc_bytelen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1736 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1737 | } |
| 1738 | } |
| 1739 | else /* repeat previous search */ |
| 1740 | { |
Bram Moolenaar | 264b74f | 2019-01-24 17:18:42 +0100 | [diff] [blame] | 1741 | if (*lastc == NUL && lastc_bytelen == 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1742 | return FAIL; |
| 1743 | if (dir) /* repeat in opposite direction */ |
| 1744 | dir = -lastcdir; |
| 1745 | else |
| 1746 | dir = lastcdir; |
| 1747 | t_cmd = last_t_cmd; |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 1748 | c = *lastc; |
| 1749 | /* For multi-byte re-use last lastc_bytes[] and lastc_bytelen. */ |
Bram Moolenaar | 8b3e033 | 2011-06-26 05:36:34 +0200 | [diff] [blame] | 1750 | |
| 1751 | /* Force a move of at least one char, so ";" and "," will move the |
| 1752 | * cursor, even if the cursor is right in front of char we are looking |
| 1753 | * at. */ |
Bram Moolenaar | 19fd09a | 2011-07-15 13:21:30 +0200 | [diff] [blame] | 1754 | if (vim_strchr(p_cpo, CPO_SCOLON) == NULL && count == 1 && t_cmd) |
Bram Moolenaar | 8b3e033 | 2011-06-26 05:36:34 +0200 | [diff] [blame] | 1755 | stop = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1756 | } |
| 1757 | |
Bram Moolenaar | 60a795a | 2005-09-16 21:55:43 +0000 | [diff] [blame] | 1758 | if (dir == BACKWARD) |
| 1759 | cap->oap->inclusive = FALSE; |
| 1760 | else |
| 1761 | cap->oap->inclusive = TRUE; |
| 1762 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1763 | p = ml_get_curline(); |
| 1764 | col = curwin->w_cursor.col; |
| 1765 | len = (int)STRLEN(p); |
| 1766 | |
| 1767 | while (count--) |
| 1768 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1769 | if (has_mbyte) |
| 1770 | { |
| 1771 | for (;;) |
| 1772 | { |
| 1773 | if (dir > 0) |
| 1774 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1775 | col += (*mb_ptr2len)(p + col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1776 | if (col >= len) |
| 1777 | return FAIL; |
| 1778 | } |
| 1779 | else |
| 1780 | { |
| 1781 | if (col == 0) |
| 1782 | return FAIL; |
| 1783 | col -= (*mb_head_off)(p, p + col - 1) + 1; |
| 1784 | } |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 1785 | if (lastc_bytelen == 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1786 | { |
Bram Moolenaar | 8b3e033 | 2011-06-26 05:36:34 +0200 | [diff] [blame] | 1787 | if (p[col] == c && stop) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1788 | break; |
| 1789 | } |
Bram Moolenaar | 66727e1 | 2017-03-01 22:17:05 +0100 | [diff] [blame] | 1790 | else if (STRNCMP(p + col, lastc_bytes, lastc_bytelen) == 0 |
Bram Moolenaar | b129a44 | 2016-12-01 17:25:20 +0100 | [diff] [blame] | 1791 | && stop) |
Bram Moolenaar | 66727e1 | 2017-03-01 22:17:05 +0100 | [diff] [blame] | 1792 | break; |
Bram Moolenaar | 8b3e033 | 2011-06-26 05:36:34 +0200 | [diff] [blame] | 1793 | stop = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1794 | } |
| 1795 | } |
| 1796 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1797 | { |
| 1798 | for (;;) |
| 1799 | { |
| 1800 | if ((col += dir) < 0 || col >= len) |
| 1801 | return FAIL; |
Bram Moolenaar | 8b3e033 | 2011-06-26 05:36:34 +0200 | [diff] [blame] | 1802 | if (p[col] == c && stop) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1803 | break; |
Bram Moolenaar | 8b3e033 | 2011-06-26 05:36:34 +0200 | [diff] [blame] | 1804 | stop = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1805 | } |
| 1806 | } |
| 1807 | } |
| 1808 | |
| 1809 | if (t_cmd) |
| 1810 | { |
| 1811 | /* backup to before the character (possibly double-byte) */ |
| 1812 | col -= dir; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1813 | if (has_mbyte) |
| 1814 | { |
| 1815 | if (dir < 0) |
Bram Moolenaar | dbd24b5 | 2015-08-11 14:26:19 +0200 | [diff] [blame] | 1816 | /* Landed on the search char which is lastc_bytelen long */ |
| 1817 | col += lastc_bytelen - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1818 | else |
| 1819 | /* To previous char, which may be multi-byte. */ |
| 1820 | col -= (*mb_head_off)(p, p + col); |
| 1821 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1822 | } |
| 1823 | curwin->w_cursor.col = col; |
| 1824 | |
| 1825 | return OK; |
| 1826 | } |
| 1827 | |
| 1828 | /* |
| 1829 | * "Other" Searches |
| 1830 | */ |
| 1831 | |
| 1832 | /* |
| 1833 | * findmatch - find the matching paren or brace |
| 1834 | * |
| 1835 | * Improvement over vi: Braces inside quotes are ignored. |
| 1836 | */ |
| 1837 | pos_T * |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 1838 | findmatch(oparg_T *oap, int initc) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1839 | { |
| 1840 | return findmatchlimit(oap, initc, 0, 0); |
| 1841 | } |
| 1842 | |
| 1843 | /* |
| 1844 | * Return TRUE if the character before "linep[col]" equals "ch". |
| 1845 | * Return FALSE if "col" is zero. |
| 1846 | * Update "*prevcol" to the column of the previous character, unless "prevcol" |
| 1847 | * is NULL. |
| 1848 | * Handles multibyte string correctly. |
| 1849 | */ |
| 1850 | static int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 1851 | check_prevcol( |
| 1852 | char_u *linep, |
| 1853 | int col, |
| 1854 | int ch, |
| 1855 | int *prevcol) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1856 | { |
| 1857 | --col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1858 | if (col > 0 && has_mbyte) |
| 1859 | col -= (*mb_head_off)(linep, linep + col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1860 | if (prevcol) |
| 1861 | *prevcol = col; |
| 1862 | return (col >= 0 && linep[col] == ch) ? TRUE : FALSE; |
| 1863 | } |
| 1864 | |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 1865 | /* |
| 1866 | * Raw string start is found at linep[startpos.col - 1]. |
| 1867 | * Return TRUE if the matching end can be found between startpos and endpos. |
| 1868 | */ |
| 1869 | static int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 1870 | find_rawstring_end(char_u *linep, pos_T *startpos, pos_T *endpos) |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 1871 | { |
| 1872 | char_u *p; |
| 1873 | char_u *delim_copy; |
| 1874 | size_t delim_len; |
| 1875 | linenr_T lnum; |
| 1876 | int found = FALSE; |
| 1877 | |
| 1878 | for (p = linep + startpos->col + 1; *p && *p != '('; ++p) |
| 1879 | ; |
| 1880 | delim_len = (p - linep) - startpos->col - 1; |
Bram Moolenaar | 6ed535d | 2015-08-26 23:01:21 +0200 | [diff] [blame] | 1881 | delim_copy = vim_strnsave(linep + startpos->col + 1, (int)delim_len); |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 1882 | if (delim_copy == NULL) |
| 1883 | return FALSE; |
| 1884 | for (lnum = startpos->lnum; lnum <= endpos->lnum; ++lnum) |
| 1885 | { |
| 1886 | char_u *line = ml_get(lnum); |
| 1887 | |
| 1888 | for (p = line + (lnum == startpos->lnum |
| 1889 | ? startpos->col + 1 : 0); *p; ++p) |
| 1890 | { |
| 1891 | if (lnum == endpos->lnum && (colnr_T)(p - line) >= endpos->col) |
| 1892 | break; |
| 1893 | if (*p == ')' && p[delim_len + 1] == '"' |
| 1894 | && STRNCMP(delim_copy, p + 1, delim_len) == 0) |
| 1895 | { |
| 1896 | found = TRUE; |
| 1897 | break; |
| 1898 | } |
| 1899 | } |
| 1900 | if (found) |
| 1901 | break; |
| 1902 | } |
| 1903 | vim_free(delim_copy); |
| 1904 | return found; |
| 1905 | } |
| 1906 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1907 | /* |
| 1908 | * findmatchlimit -- find the matching paren or brace, if it exists within |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 1909 | * maxtravel lines of the cursor. A maxtravel of 0 means search until falling |
| 1910 | * off the edge of the file. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1911 | * |
| 1912 | * "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] | 1913 | * character at or after the cursor. Special values: |
| 1914 | * '*' look for C-style comment / * |
| 1915 | * '/' look for C-style comment / *, ignoring comment-end |
| 1916 | * '#' look for preprocessor directives |
| 1917 | * 'R' look for raw string start: R"delim(text)delim" (only backwards) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1918 | * |
| 1919 | * flags: FM_BACKWARD search backwards (when initc is '/', '*' or '#') |
| 1920 | * FM_FORWARD search forwards (when initc is '/', '*' or '#') |
| 1921 | * FM_BLOCKSTOP stop at start/end of block ({ or } in column 0) |
| 1922 | * FM_SKIPCOMM skip comments (not implemented yet!) |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 1923 | * |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 1924 | * "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] | 1925 | * NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1926 | */ |
| 1927 | |
| 1928 | pos_T * |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 1929 | findmatchlimit( |
| 1930 | oparg_T *oap, |
| 1931 | int initc, |
| 1932 | int flags, |
| 1933 | int maxtravel) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1934 | { |
| 1935 | static pos_T pos; /* current search position */ |
| 1936 | int findc = 0; /* matching brace */ |
| 1937 | int c; |
| 1938 | int count = 0; /* cumulative number of braces */ |
| 1939 | int backwards = FALSE; /* init for gcc */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 1940 | int raw_string = FALSE; /* search for raw string */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1941 | int inquote = FALSE; /* TRUE when inside quotes */ |
| 1942 | char_u *linep; /* pointer to current line */ |
| 1943 | char_u *ptr; |
| 1944 | int do_quotes; /* check for quotes in current line */ |
| 1945 | int at_start; /* do_quotes value at start position */ |
| 1946 | int hash_dir = 0; /* Direction searched for # things */ |
| 1947 | int comment_dir = 0; /* Direction searched for comments */ |
| 1948 | pos_T match_pos; /* Where last slash-star was found */ |
| 1949 | int start_in_quotes; /* start position is in quotes */ |
| 1950 | int traveled = 0; /* how far we've searched so far */ |
| 1951 | int ignore_cend = FALSE; /* ignore comment end */ |
| 1952 | int cpo_match; /* vi compatible matching */ |
| 1953 | int cpo_bsl; /* don't recognize backslashes */ |
| 1954 | int match_escaped = 0; /* search for escaped match */ |
| 1955 | int dir; /* Direction to search */ |
| 1956 | int comment_col = MAXCOL; /* start of / / comment */ |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 1957 | #ifdef FEAT_LISP |
| 1958 | int lispcomm = FALSE; /* inside of Lisp-style comment */ |
| 1959 | int lisp = curbuf->b_p_lisp; /* engage Lisp-specific hacks ;) */ |
| 1960 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1961 | |
| 1962 | pos = curwin->w_cursor; |
Bram Moolenaar | c56c459 | 2013-08-14 17:45:29 +0200 | [diff] [blame] | 1963 | pos.coladd = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1964 | linep = ml_get(pos.lnum); |
| 1965 | |
| 1966 | cpo_match = (vim_strchr(p_cpo, CPO_MATCH) != NULL); |
| 1967 | cpo_bsl = (vim_strchr(p_cpo, CPO_MATCHBSL) != NULL); |
| 1968 | |
| 1969 | /* Direction to search when initc is '/', '*' or '#' */ |
| 1970 | if (flags & FM_BACKWARD) |
| 1971 | dir = BACKWARD; |
| 1972 | else if (flags & FM_FORWARD) |
| 1973 | dir = FORWARD; |
| 1974 | else |
| 1975 | dir = 0; |
| 1976 | |
| 1977 | /* |
| 1978 | * if initc given, look in the table for the matching character |
| 1979 | * '/' and '*' are special cases: look for start or end of comment. |
| 1980 | * When '/' is used, we ignore running backwards into an star-slash, for |
| 1981 | * "[*" command, we just want to find any comment. |
| 1982 | */ |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 1983 | if (initc == '/' || initc == '*' || initc == 'R') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1984 | { |
| 1985 | comment_dir = dir; |
| 1986 | if (initc == '/') |
| 1987 | ignore_cend = TRUE; |
| 1988 | backwards = (dir == FORWARD) ? FALSE : TRUE; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 1989 | raw_string = (initc == 'R'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1990 | initc = NUL; |
| 1991 | } |
| 1992 | else if (initc != '#' && initc != NUL) |
| 1993 | { |
Bram Moolenaar | 8c7694a | 2013-01-17 17:02:05 +0100 | [diff] [blame] | 1994 | find_mps_values(&initc, &findc, &backwards, TRUE); |
| 1995 | if (findc == NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1996 | return NULL; |
| 1997 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1998 | else |
| 1999 | { |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 2000 | /* |
| 2001 | * Either initc is '#', or no initc was given and we need to look |
| 2002 | * under the cursor. |
| 2003 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2004 | if (initc == '#') |
| 2005 | { |
| 2006 | hash_dir = dir; |
| 2007 | } |
| 2008 | else |
| 2009 | { |
| 2010 | /* |
| 2011 | * initc was not given, must look for something to match under |
| 2012 | * or near the cursor. |
| 2013 | * Only check for special things when 'cpo' doesn't have '%'. |
| 2014 | */ |
| 2015 | if (!cpo_match) |
| 2016 | { |
| 2017 | /* Are we before or at #if, #else etc.? */ |
| 2018 | ptr = skipwhite(linep); |
| 2019 | if (*ptr == '#' && pos.col <= (colnr_T)(ptr - linep)) |
| 2020 | { |
| 2021 | ptr = skipwhite(ptr + 1); |
| 2022 | if ( STRNCMP(ptr, "if", 2) == 0 |
| 2023 | || STRNCMP(ptr, "endif", 5) == 0 |
| 2024 | || STRNCMP(ptr, "el", 2) == 0) |
| 2025 | hash_dir = 1; |
| 2026 | } |
| 2027 | |
| 2028 | /* Are we on a comment? */ |
| 2029 | else if (linep[pos.col] == '/') |
| 2030 | { |
| 2031 | if (linep[pos.col + 1] == '*') |
| 2032 | { |
| 2033 | comment_dir = FORWARD; |
| 2034 | backwards = FALSE; |
| 2035 | pos.col++; |
| 2036 | } |
| 2037 | else if (pos.col > 0 && linep[pos.col - 1] == '*') |
| 2038 | { |
| 2039 | comment_dir = BACKWARD; |
| 2040 | backwards = TRUE; |
| 2041 | pos.col--; |
| 2042 | } |
| 2043 | } |
| 2044 | else if (linep[pos.col] == '*') |
| 2045 | { |
| 2046 | if (linep[pos.col + 1] == '/') |
| 2047 | { |
| 2048 | comment_dir = BACKWARD; |
| 2049 | backwards = TRUE; |
| 2050 | } |
| 2051 | else if (pos.col > 0 && linep[pos.col - 1] == '/') |
| 2052 | { |
| 2053 | comment_dir = FORWARD; |
| 2054 | backwards = FALSE; |
| 2055 | } |
| 2056 | } |
| 2057 | } |
| 2058 | |
| 2059 | /* |
| 2060 | * If we are not on a comment or the # at the start of a line, then |
| 2061 | * look for brace anywhere on this line after the cursor. |
| 2062 | */ |
| 2063 | if (!hash_dir && !comment_dir) |
| 2064 | { |
| 2065 | /* |
| 2066 | * Find the brace under or after the cursor. |
| 2067 | * If beyond the end of the line, use the last character in |
| 2068 | * the line. |
| 2069 | */ |
| 2070 | if (linep[pos.col] == NUL && pos.col) |
| 2071 | --pos.col; |
| 2072 | for (;;) |
| 2073 | { |
Bram Moolenaar | 8c7694a | 2013-01-17 17:02:05 +0100 | [diff] [blame] | 2074 | initc = PTR2CHAR(linep + pos.col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2075 | if (initc == NUL) |
| 2076 | break; |
| 2077 | |
Bram Moolenaar | 8c7694a | 2013-01-17 17:02:05 +0100 | [diff] [blame] | 2078 | find_mps_values(&initc, &findc, &backwards, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2079 | if (findc) |
| 2080 | break; |
Bram Moolenaar | 8c7694a | 2013-01-17 17:02:05 +0100 | [diff] [blame] | 2081 | pos.col += MB_PTR2LEN(linep + pos.col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2082 | } |
| 2083 | if (!findc) |
| 2084 | { |
| 2085 | /* no brace in the line, maybe use " #if" then */ |
| 2086 | if (!cpo_match && *skipwhite(linep) == '#') |
| 2087 | hash_dir = 1; |
| 2088 | else |
| 2089 | return NULL; |
| 2090 | } |
| 2091 | else if (!cpo_bsl) |
| 2092 | { |
| 2093 | int col, bslcnt = 0; |
| 2094 | |
| 2095 | /* Set "match_escaped" if there are an odd number of |
| 2096 | * backslashes. */ |
| 2097 | for (col = pos.col; check_prevcol(linep, col, '\\', &col);) |
| 2098 | bslcnt++; |
| 2099 | match_escaped = (bslcnt & 1); |
| 2100 | } |
| 2101 | } |
| 2102 | } |
| 2103 | if (hash_dir) |
| 2104 | { |
| 2105 | /* |
| 2106 | * Look for matching #if, #else, #elif, or #endif |
| 2107 | */ |
| 2108 | if (oap != NULL) |
| 2109 | oap->motion_type = MLINE; /* Linewise for this case only */ |
| 2110 | if (initc != '#') |
| 2111 | { |
| 2112 | ptr = skipwhite(skipwhite(linep) + 1); |
| 2113 | if (STRNCMP(ptr, "if", 2) == 0 || STRNCMP(ptr, "el", 2) == 0) |
| 2114 | hash_dir = 1; |
| 2115 | else if (STRNCMP(ptr, "endif", 5) == 0) |
| 2116 | hash_dir = -1; |
| 2117 | else |
| 2118 | return NULL; |
| 2119 | } |
| 2120 | pos.col = 0; |
| 2121 | while (!got_int) |
| 2122 | { |
| 2123 | if (hash_dir > 0) |
| 2124 | { |
| 2125 | if (pos.lnum == curbuf->b_ml.ml_line_count) |
| 2126 | break; |
| 2127 | } |
| 2128 | else if (pos.lnum == 1) |
| 2129 | break; |
| 2130 | pos.lnum += hash_dir; |
| 2131 | linep = ml_get(pos.lnum); |
| 2132 | line_breakcheck(); /* check for CTRL-C typed */ |
| 2133 | ptr = skipwhite(linep); |
| 2134 | if (*ptr != '#') |
| 2135 | continue; |
| 2136 | pos.col = (colnr_T) (ptr - linep); |
| 2137 | ptr = skipwhite(ptr + 1); |
| 2138 | if (hash_dir > 0) |
| 2139 | { |
| 2140 | if (STRNCMP(ptr, "if", 2) == 0) |
| 2141 | count++; |
| 2142 | else if (STRNCMP(ptr, "el", 2) == 0) |
| 2143 | { |
| 2144 | if (count == 0) |
| 2145 | return &pos; |
| 2146 | } |
| 2147 | else if (STRNCMP(ptr, "endif", 5) == 0) |
| 2148 | { |
| 2149 | if (count == 0) |
| 2150 | return &pos; |
| 2151 | count--; |
| 2152 | } |
| 2153 | } |
| 2154 | else |
| 2155 | { |
| 2156 | if (STRNCMP(ptr, "if", 2) == 0) |
| 2157 | { |
| 2158 | if (count == 0) |
| 2159 | return &pos; |
| 2160 | count--; |
| 2161 | } |
| 2162 | else if (initc == '#' && STRNCMP(ptr, "el", 2) == 0) |
| 2163 | { |
| 2164 | if (count == 0) |
| 2165 | return &pos; |
| 2166 | } |
| 2167 | else if (STRNCMP(ptr, "endif", 5) == 0) |
| 2168 | count++; |
| 2169 | } |
| 2170 | } |
| 2171 | return NULL; |
| 2172 | } |
| 2173 | } |
| 2174 | |
| 2175 | #ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | abc9773 | 2007-08-08 20:49:37 +0000 | [diff] [blame] | 2176 | /* This is just guessing: when 'rightleft' is set, search for a matching |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2177 | * paren/brace in the other direction. */ |
| 2178 | if (curwin->w_p_rl && vim_strchr((char_u *)"()[]{}<>", initc) != NULL) |
| 2179 | backwards = !backwards; |
| 2180 | #endif |
| 2181 | |
| 2182 | do_quotes = -1; |
| 2183 | start_in_quotes = MAYBE; |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 2184 | CLEAR_POS(&match_pos); |
Bram Moolenaar | fd2ac76 | 2006-03-01 22:09:21 +0000 | [diff] [blame] | 2185 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2186 | /* backward search: Check if this line contains a single-line comment */ |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2187 | if ((backwards && comment_dir) |
| 2188 | #ifdef FEAT_LISP |
| 2189 | || lisp |
| 2190 | #endif |
| 2191 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2192 | comment_col = check_linecomment(linep); |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2193 | #ifdef FEAT_LISP |
| 2194 | if (lisp && comment_col != MAXCOL && pos.col > (colnr_T)comment_col) |
| 2195 | lispcomm = TRUE; /* find match inside this comment */ |
| 2196 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2197 | while (!got_int) |
| 2198 | { |
| 2199 | /* |
| 2200 | * Go to the next position, forward or backward. We could use |
| 2201 | * inc() and dec() here, but that is much slower |
| 2202 | */ |
| 2203 | if (backwards) |
| 2204 | { |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2205 | #ifdef FEAT_LISP |
| 2206 | /* char to match is inside of comment, don't search outside */ |
| 2207 | if (lispcomm && pos.col < (colnr_T)comment_col) |
| 2208 | break; |
| 2209 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2210 | if (pos.col == 0) /* at start of line, go to prev. one */ |
| 2211 | { |
| 2212 | if (pos.lnum == 1) /* start of file */ |
| 2213 | break; |
| 2214 | --pos.lnum; |
| 2215 | |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 2216 | if (maxtravel > 0 && ++traveled > maxtravel) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2217 | break; |
| 2218 | |
| 2219 | linep = ml_get(pos.lnum); |
| 2220 | pos.col = (colnr_T)STRLEN(linep); /* pos.col on trailing NUL */ |
| 2221 | do_quotes = -1; |
| 2222 | line_breakcheck(); |
| 2223 | |
| 2224 | /* Check if this line contains a single-line comment */ |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2225 | if (comment_dir |
| 2226 | #ifdef FEAT_LISP |
| 2227 | || lisp |
| 2228 | #endif |
| 2229 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2230 | comment_col = check_linecomment(linep); |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2231 | #ifdef FEAT_LISP |
| 2232 | /* skip comment */ |
| 2233 | if (lisp && comment_col != MAXCOL) |
| 2234 | pos.col = comment_col; |
| 2235 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2236 | } |
| 2237 | else |
| 2238 | { |
| 2239 | --pos.col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2240 | if (has_mbyte) |
| 2241 | pos.col -= (*mb_head_off)(linep, linep + pos.col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2242 | } |
| 2243 | } |
| 2244 | else /* forward search */ |
| 2245 | { |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2246 | if (linep[pos.col] == NUL |
| 2247 | /* at end of line, go to next one */ |
| 2248 | #ifdef FEAT_LISP |
| 2249 | /* don't search for match in comment */ |
| 2250 | || (lisp && comment_col != MAXCOL |
| 2251 | && pos.col == (colnr_T)comment_col) |
| 2252 | #endif |
| 2253 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2254 | { |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2255 | if (pos.lnum == curbuf->b_ml.ml_line_count /* end of file */ |
| 2256 | #ifdef FEAT_LISP |
| 2257 | /* line is exhausted and comment with it, |
| 2258 | * don't search for match in code */ |
| 2259 | || lispcomm |
| 2260 | #endif |
| 2261 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2262 | break; |
| 2263 | ++pos.lnum; |
| 2264 | |
| 2265 | if (maxtravel && traveled++ > maxtravel) |
| 2266 | break; |
| 2267 | |
| 2268 | linep = ml_get(pos.lnum); |
| 2269 | pos.col = 0; |
| 2270 | do_quotes = -1; |
| 2271 | line_breakcheck(); |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2272 | #ifdef FEAT_LISP |
| 2273 | if (lisp) /* find comment pos in new line */ |
| 2274 | comment_col = check_linecomment(linep); |
| 2275 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2276 | } |
| 2277 | else |
| 2278 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2279 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2280 | pos.col += (*mb_ptr2len)(linep + pos.col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2281 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2282 | ++pos.col; |
| 2283 | } |
| 2284 | } |
| 2285 | |
| 2286 | /* |
| 2287 | * If FM_BLOCKSTOP given, stop at a '{' or '}' in column 0. |
| 2288 | */ |
| 2289 | if (pos.col == 0 && (flags & FM_BLOCKSTOP) && |
| 2290 | (linep[0] == '{' || linep[0] == '}')) |
| 2291 | { |
| 2292 | if (linep[0] == findc && count == 0) /* match! */ |
| 2293 | return &pos; |
| 2294 | break; /* out of scope */ |
| 2295 | } |
| 2296 | |
| 2297 | if (comment_dir) |
| 2298 | { |
| 2299 | /* Note: comments do not nest, and we ignore quotes in them */ |
| 2300 | /* TODO: ignore comment brackets inside strings */ |
| 2301 | if (comment_dir == FORWARD) |
| 2302 | { |
| 2303 | if (linep[pos.col] == '*' && linep[pos.col + 1] == '/') |
| 2304 | { |
| 2305 | pos.col++; |
| 2306 | return &pos; |
| 2307 | } |
| 2308 | } |
| 2309 | else /* Searching backwards */ |
| 2310 | { |
| 2311 | /* |
| 2312 | * A comment may contain / * or / /, it may also start or end |
Bram Moolenaar | f8c53d3 | 2017-11-12 15:36:38 +0100 | [diff] [blame] | 2313 | * with / * /. Ignore a / * after / / and after *. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2314 | */ |
| 2315 | if (pos.col == 0) |
| 2316 | continue; |
Bram Moolenaar | f7bb86d | 2015-07-28 21:17:36 +0200 | [diff] [blame] | 2317 | else if (raw_string) |
| 2318 | { |
| 2319 | if (linep[pos.col - 1] == 'R' |
| 2320 | && linep[pos.col] == '"' |
| 2321 | && vim_strchr(linep + pos.col + 1, '(') != NULL) |
| 2322 | { |
| 2323 | /* Possible start of raw string. Now that we have the |
| 2324 | * delimiter we can check if it ends before where we |
| 2325 | * started searching, or before the previously found |
| 2326 | * raw string start. */ |
| 2327 | if (!find_rawstring_end(linep, &pos, |
| 2328 | count > 0 ? &match_pos : &curwin->w_cursor)) |
| 2329 | { |
| 2330 | count++; |
| 2331 | match_pos = pos; |
| 2332 | match_pos.col--; |
| 2333 | } |
| 2334 | linep = ml_get(pos.lnum); /* may have been released */ |
| 2335 | } |
| 2336 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2337 | else if ( linep[pos.col - 1] == '/' |
| 2338 | && linep[pos.col] == '*' |
Bram Moolenaar | f8c53d3 | 2017-11-12 15:36:38 +0100 | [diff] [blame] | 2339 | && (pos.col == 1 || linep[pos.col - 2] != '*') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2340 | && (int)pos.col < comment_col) |
| 2341 | { |
| 2342 | count++; |
| 2343 | match_pos = pos; |
| 2344 | match_pos.col--; |
| 2345 | } |
| 2346 | else if (linep[pos.col - 1] == '*' && linep[pos.col] == '/') |
| 2347 | { |
| 2348 | if (count > 0) |
| 2349 | pos = match_pos; |
| 2350 | else if (pos.col > 1 && linep[pos.col - 2] == '/' |
| 2351 | && (int)pos.col <= comment_col) |
| 2352 | pos.col -= 2; |
| 2353 | else if (ignore_cend) |
| 2354 | continue; |
| 2355 | else |
| 2356 | return NULL; |
| 2357 | return &pos; |
| 2358 | } |
| 2359 | } |
| 2360 | continue; |
| 2361 | } |
| 2362 | |
| 2363 | /* |
| 2364 | * If smart matching ('cpoptions' does not contain '%'), braces inside |
| 2365 | * of quotes are ignored, but only if there is an even number of |
| 2366 | * quotes in the line. |
| 2367 | */ |
| 2368 | if (cpo_match) |
| 2369 | do_quotes = 0; |
| 2370 | else if (do_quotes == -1) |
| 2371 | { |
| 2372 | /* |
| 2373 | * Count the number of quotes in the line, skipping \" and '"'. |
| 2374 | * Watch out for "\\". |
| 2375 | */ |
| 2376 | at_start = do_quotes; |
| 2377 | for (ptr = linep; *ptr; ++ptr) |
| 2378 | { |
| 2379 | if (ptr == linep + pos.col + backwards) |
| 2380 | at_start = (do_quotes & 1); |
| 2381 | if (*ptr == '"' |
| 2382 | && (ptr == linep || ptr[-1] != '\'' || ptr[1] != '\'')) |
| 2383 | ++do_quotes; |
| 2384 | if (*ptr == '\\' && ptr[1] != NUL) |
| 2385 | ++ptr; |
| 2386 | } |
| 2387 | do_quotes &= 1; /* result is 1 with even number of quotes */ |
| 2388 | |
| 2389 | /* |
| 2390 | * If we find an uneven count, check current line and previous |
| 2391 | * one for a '\' at the end. |
| 2392 | */ |
| 2393 | if (!do_quotes) |
| 2394 | { |
| 2395 | inquote = FALSE; |
| 2396 | if (ptr[-1] == '\\') |
| 2397 | { |
| 2398 | do_quotes = 1; |
| 2399 | if (start_in_quotes == MAYBE) |
| 2400 | { |
| 2401 | /* Do we need to use at_start here? */ |
| 2402 | inquote = TRUE; |
| 2403 | start_in_quotes = TRUE; |
| 2404 | } |
| 2405 | else if (backwards) |
| 2406 | inquote = TRUE; |
| 2407 | } |
| 2408 | if (pos.lnum > 1) |
| 2409 | { |
| 2410 | ptr = ml_get(pos.lnum - 1); |
| 2411 | if (*ptr && *(ptr + STRLEN(ptr) - 1) == '\\') |
| 2412 | { |
| 2413 | do_quotes = 1; |
| 2414 | if (start_in_quotes == MAYBE) |
| 2415 | { |
| 2416 | inquote = at_start; |
| 2417 | if (inquote) |
| 2418 | start_in_quotes = TRUE; |
| 2419 | } |
| 2420 | else if (!backwards) |
| 2421 | inquote = TRUE; |
| 2422 | } |
Bram Moolenaar | aec1179 | 2007-07-10 11:09:36 +0000 | [diff] [blame] | 2423 | |
| 2424 | /* ml_get() only keeps one line, need to get linep again */ |
| 2425 | linep = ml_get(pos.lnum); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2426 | } |
| 2427 | } |
| 2428 | } |
| 2429 | if (start_in_quotes == MAYBE) |
| 2430 | start_in_quotes = FALSE; |
| 2431 | |
| 2432 | /* |
| 2433 | * If 'smartmatch' is set: |
| 2434 | * Things inside quotes are ignored by setting 'inquote'. If we |
| 2435 | * find a quote without a preceding '\' invert 'inquote'. At the |
| 2436 | * end of a line not ending in '\' we reset 'inquote'. |
| 2437 | * |
| 2438 | * In lines with an uneven number of quotes (without preceding '\') |
| 2439 | * we do not know which part to ignore. Therefore we only set |
| 2440 | * inquote if the number of quotes in a line is even, unless this |
| 2441 | * line or the previous one ends in a '\'. Complicated, isn't it? |
| 2442 | */ |
Bram Moolenaar | 8c7694a | 2013-01-17 17:02:05 +0100 | [diff] [blame] | 2443 | c = PTR2CHAR(linep + pos.col); |
| 2444 | switch (c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2445 | { |
| 2446 | case NUL: |
| 2447 | /* at end of line without trailing backslash, reset inquote */ |
| 2448 | if (pos.col == 0 || linep[pos.col - 1] != '\\') |
| 2449 | { |
| 2450 | inquote = FALSE; |
| 2451 | start_in_quotes = FALSE; |
| 2452 | } |
| 2453 | break; |
| 2454 | |
| 2455 | case '"': |
| 2456 | /* a quote that is preceded with an odd number of backslashes is |
| 2457 | * ignored */ |
| 2458 | if (do_quotes) |
| 2459 | { |
| 2460 | int col; |
| 2461 | |
| 2462 | for (col = pos.col - 1; col >= 0; --col) |
| 2463 | if (linep[col] != '\\') |
| 2464 | break; |
| 2465 | if ((((int)pos.col - 1 - col) & 1) == 0) |
| 2466 | { |
| 2467 | inquote = !inquote; |
| 2468 | start_in_quotes = FALSE; |
| 2469 | } |
| 2470 | } |
| 2471 | break; |
| 2472 | |
| 2473 | /* |
| 2474 | * If smart matching ('cpoptions' does not contain '%'): |
| 2475 | * Skip things in single quotes: 'x' or '\x'. Be careful for single |
| 2476 | * single quotes, eg jon's. Things like '\233' or '\x3f' are not |
| 2477 | * skipped, there is never a brace in them. |
| 2478 | * Ignore this when finding matches for `'. |
| 2479 | */ |
| 2480 | case '\'': |
| 2481 | if (!cpo_match && initc != '\'' && findc != '\'') |
| 2482 | { |
| 2483 | if (backwards) |
| 2484 | { |
| 2485 | if (pos.col > 1) |
| 2486 | { |
| 2487 | if (linep[pos.col - 2] == '\'') |
| 2488 | { |
| 2489 | pos.col -= 2; |
| 2490 | break; |
| 2491 | } |
| 2492 | else if (linep[pos.col - 2] == '\\' && |
| 2493 | pos.col > 2 && linep[pos.col - 3] == '\'') |
| 2494 | { |
| 2495 | pos.col -= 3; |
| 2496 | break; |
| 2497 | } |
| 2498 | } |
| 2499 | } |
| 2500 | else if (linep[pos.col + 1]) /* forward search */ |
| 2501 | { |
| 2502 | if (linep[pos.col + 1] == '\\' && |
| 2503 | linep[pos.col + 2] && linep[pos.col + 3] == '\'') |
| 2504 | { |
| 2505 | pos.col += 3; |
| 2506 | break; |
| 2507 | } |
| 2508 | else if (linep[pos.col + 2] == '\'') |
| 2509 | { |
| 2510 | pos.col += 2; |
| 2511 | break; |
| 2512 | } |
| 2513 | } |
| 2514 | } |
| 2515 | /* FALLTHROUGH */ |
| 2516 | |
| 2517 | default: |
| 2518 | #ifdef FEAT_LISP |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2519 | /* |
| 2520 | * For Lisp skip over backslashed (), {} and []. |
| 2521 | * (actually, we skip #\( et al) |
| 2522 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2523 | if (curbuf->b_p_lisp |
| 2524 | && vim_strchr((char_u *)"(){}[]", c) != NULL |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2525 | && pos.col > 1 |
| 2526 | && check_prevcol(linep, pos.col, '\\', NULL) |
| 2527 | && check_prevcol(linep, pos.col - 1, '#', NULL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2528 | break; |
| 2529 | #endif |
| 2530 | |
| 2531 | /* Check for match outside of quotes, and inside of |
| 2532 | * quotes when the start is also inside of quotes. */ |
| 2533 | if ((!inquote || start_in_quotes == TRUE) |
| 2534 | && (c == initc || c == findc)) |
| 2535 | { |
| 2536 | int col, bslcnt = 0; |
| 2537 | |
| 2538 | if (!cpo_bsl) |
| 2539 | { |
| 2540 | for (col = pos.col; check_prevcol(linep, col, '\\', &col);) |
| 2541 | bslcnt++; |
| 2542 | } |
Bram Moolenaar | fe81d45 | 2009-04-22 14:44:41 +0000 | [diff] [blame] | 2543 | /* Only accept a match when 'M' is in 'cpo' or when escaping |
| 2544 | * is what we expect. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2545 | if (cpo_bsl || (bslcnt & 1) == match_escaped) |
| 2546 | { |
| 2547 | if (c == initc) |
| 2548 | count++; |
| 2549 | else |
| 2550 | { |
| 2551 | if (count == 0) |
| 2552 | return &pos; |
| 2553 | count--; |
| 2554 | } |
| 2555 | } |
| 2556 | } |
| 2557 | } |
| 2558 | } |
| 2559 | |
| 2560 | if (comment_dir == BACKWARD && count > 0) |
| 2561 | { |
| 2562 | pos = match_pos; |
| 2563 | return &pos; |
| 2564 | } |
| 2565 | return (pos_T *)NULL; /* never found it */ |
| 2566 | } |
| 2567 | |
| 2568 | /* |
| 2569 | * Check if line[] contains a / / comment. |
| 2570 | * Return MAXCOL if not, otherwise return the column. |
| 2571 | * TODO: skip strings. |
| 2572 | */ |
| 2573 | static int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 2574 | check_linecomment(char_u *line) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2575 | { |
| 2576 | char_u *p; |
| 2577 | |
| 2578 | p = line; |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2579 | #ifdef FEAT_LISP |
| 2580 | /* skip Lispish one-line comments */ |
| 2581 | if (curbuf->b_p_lisp) |
| 2582 | { |
| 2583 | if (vim_strchr(p, ';') != NULL) /* there may be comments */ |
| 2584 | { |
Bram Moolenaar | 70b2a56 | 2012-01-10 22:26:17 +0100 | [diff] [blame] | 2585 | int in_str = FALSE; /* inside of string */ |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2586 | |
| 2587 | p = line; /* scan from start */ |
Bram Moolenaar | 520470a | 2005-06-16 21:59:56 +0000 | [diff] [blame] | 2588 | while ((p = vim_strpbrk(p, (char_u *)"\";")) != NULL) |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2589 | { |
| 2590 | if (*p == '"') |
| 2591 | { |
Bram Moolenaar | 70b2a56 | 2012-01-10 22:26:17 +0100 | [diff] [blame] | 2592 | if (in_str) |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2593 | { |
| 2594 | if (*(p - 1) != '\\') /* skip escaped quote */ |
Bram Moolenaar | 70b2a56 | 2012-01-10 22:26:17 +0100 | [diff] [blame] | 2595 | in_str = FALSE; |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2596 | } |
| 2597 | else if (p == line || ((p - line) >= 2 |
| 2598 | /* skip #\" form */ |
| 2599 | && *(p - 1) != '\\' && *(p - 2) != '#')) |
Bram Moolenaar | 70b2a56 | 2012-01-10 22:26:17 +0100 | [diff] [blame] | 2600 | in_str = TRUE; |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2601 | } |
Bram Moolenaar | 70b2a56 | 2012-01-10 22:26:17 +0100 | [diff] [blame] | 2602 | else if (!in_str && ((p - line) < 2 |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2603 | || (*(p - 1) != '\\' && *(p - 2) != '#'))) |
| 2604 | break; /* found! */ |
| 2605 | ++p; |
| 2606 | } |
| 2607 | } |
| 2608 | else |
| 2609 | p = NULL; |
| 2610 | } |
| 2611 | else |
| 2612 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2613 | while ((p = vim_strchr(p, '/')) != NULL) |
| 2614 | { |
Bram Moolenaar | 78d4aba | 2008-01-01 14:43:35 +0000 | [diff] [blame] | 2615 | /* accept a double /, unless it's preceded with * and followed by *, |
| 2616 | * because * / / * is an end and start of a C comment */ |
| 2617 | if (p[1] == '/' && (p == line || p[-1] != '*' || p[2] != '*')) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2618 | break; |
| 2619 | ++p; |
| 2620 | } |
| 2621 | |
| 2622 | if (p == NULL) |
| 2623 | return MAXCOL; |
| 2624 | return (int)(p - line); |
| 2625 | } |
| 2626 | |
| 2627 | /* |
| 2628 | * Move cursor briefly to character matching the one under the cursor. |
| 2629 | * Used for Insert mode and "r" command. |
| 2630 | * Show the match only if it is visible on the screen. |
| 2631 | * If there isn't a match, then beep. |
| 2632 | */ |
| 2633 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 2634 | showmatch( |
| 2635 | int c) /* char to show match for */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2636 | { |
| 2637 | pos_T *lpos, save_cursor; |
| 2638 | pos_T mpos; |
| 2639 | colnr_T vcol; |
| 2640 | long save_so; |
| 2641 | long save_siso; |
| 2642 | #ifdef CURSOR_SHAPE |
| 2643 | int save_state; |
| 2644 | #endif |
| 2645 | colnr_T save_dollar_vcol; |
| 2646 | char_u *p; |
Bram Moolenaar | 375e339 | 2019-01-31 18:26:10 +0100 | [diff] [blame] | 2647 | long *so = curwin->w_p_so >= 0 ? &curwin->w_p_so : &p_so; |
| 2648 | 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] | 2649 | |
| 2650 | /* |
| 2651 | * Only show match for chars in the 'matchpairs' option. |
| 2652 | */ |
| 2653 | /* 'matchpairs' is "x:y,x:y" */ |
Bram Moolenaar | 8c7694a | 2013-01-17 17:02:05 +0100 | [diff] [blame] | 2654 | for (p = curbuf->b_p_mps; *p != NUL; ++p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2655 | { |
| 2656 | #ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | 187d3ac | 2013-02-20 18:39:13 +0100 | [diff] [blame] | 2657 | if (PTR2CHAR(p) == c && (curwin->w_p_rl ^ p_ri)) |
Bram Moolenaar | 8c7694a | 2013-01-17 17:02:05 +0100 | [diff] [blame] | 2658 | break; |
Bram Moolenaar | 187d3ac | 2013-02-20 18:39:13 +0100 | [diff] [blame] | 2659 | #endif |
Bram Moolenaar | 8c7694a | 2013-01-17 17:02:05 +0100 | [diff] [blame] | 2660 | p += MB_PTR2LEN(p) + 1; |
| 2661 | if (PTR2CHAR(p) == c |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2662 | #ifdef FEAT_RIGHTLEFT |
| 2663 | && !(curwin->w_p_rl ^ p_ri) |
| 2664 | #endif |
| 2665 | ) |
| 2666 | break; |
Bram Moolenaar | 8c7694a | 2013-01-17 17:02:05 +0100 | [diff] [blame] | 2667 | p += MB_PTR2LEN(p); |
| 2668 | if (*p == NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2669 | return; |
| 2670 | } |
| 2671 | |
| 2672 | if ((lpos = findmatch(NULL, NUL)) == NULL) /* no match, so beep */ |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 2673 | vim_beep(BO_MATCH); |
Bram Moolenaar | 187d3ac | 2013-02-20 18:39:13 +0100 | [diff] [blame] | 2674 | else if (lpos->lnum >= curwin->w_topline && lpos->lnum < curwin->w_botline) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2675 | { |
| 2676 | if (!curwin->w_p_wrap) |
| 2677 | getvcol(curwin, lpos, NULL, &vcol, NULL); |
| 2678 | if (curwin->w_p_wrap || (vcol >= curwin->w_leftcol |
Bram Moolenaar | 0263146 | 2017-09-22 15:20:32 +0200 | [diff] [blame] | 2679 | && vcol < curwin->w_leftcol + curwin->w_width)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2680 | { |
| 2681 | mpos = *lpos; /* save the pos, update_screen() may change it */ |
| 2682 | save_cursor = curwin->w_cursor; |
Bram Moolenaar | 375e339 | 2019-01-31 18:26:10 +0100 | [diff] [blame] | 2683 | save_so = *so; |
| 2684 | save_siso = *siso; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2685 | /* Handle "$" in 'cpo': If the ')' is typed on top of the "$", |
| 2686 | * stop displaying the "$". */ |
Bram Moolenaar | 76b9b36 | 2012-02-04 23:35:00 +0100 | [diff] [blame] | 2687 | if (dollar_vcol >= 0 && dollar_vcol == curwin->w_virtcol) |
| 2688 | dollar_vcol = -1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2689 | ++curwin->w_virtcol; /* do display ')' just before "$" */ |
| 2690 | update_screen(VALID); /* show the new char first */ |
| 2691 | |
| 2692 | save_dollar_vcol = dollar_vcol; |
| 2693 | #ifdef CURSOR_SHAPE |
| 2694 | save_state = State; |
| 2695 | State = SHOWMATCH; |
| 2696 | ui_cursor_shape(); /* may show different cursor shape */ |
| 2697 | #endif |
| 2698 | curwin->w_cursor = mpos; /* move to matching char */ |
Bram Moolenaar | 375e339 | 2019-01-31 18:26:10 +0100 | [diff] [blame] | 2699 | *so = 0; /* don't use 'scrolloff' here */ |
| 2700 | *siso = 0; /* don't use 'sidescrolloff' here */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2701 | showruler(FALSE); |
| 2702 | setcursor(); |
| 2703 | cursor_on(); /* make sure that the cursor is shown */ |
Bram Moolenaar | a338adc | 2018-01-31 20:51:47 +0100 | [diff] [blame] | 2704 | out_flush_cursor(TRUE, FALSE); |
| 2705 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2706 | /* Restore dollar_vcol(), because setcursor() may call curs_rows() |
| 2707 | * which resets it if the matching position is in a previous line |
| 2708 | * and has a higher column number. */ |
| 2709 | dollar_vcol = save_dollar_vcol; |
| 2710 | |
| 2711 | /* |
| 2712 | * brief pause, unless 'm' is present in 'cpo' and a character is |
| 2713 | * available. |
| 2714 | */ |
| 2715 | if (vim_strchr(p_cpo, CPO_SHOWMATCH) != NULL) |
| 2716 | ui_delay(p_mat * 100L, TRUE); |
| 2717 | else if (!char_avail()) |
| 2718 | ui_delay(p_mat * 100L, FALSE); |
| 2719 | curwin->w_cursor = save_cursor; /* restore cursor position */ |
Bram Moolenaar | 375e339 | 2019-01-31 18:26:10 +0100 | [diff] [blame] | 2720 | *so = save_so; |
| 2721 | *siso = save_siso; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2722 | #ifdef CURSOR_SHAPE |
| 2723 | State = save_state; |
| 2724 | ui_cursor_shape(); /* may show different cursor shape */ |
| 2725 | #endif |
| 2726 | } |
| 2727 | } |
| 2728 | } |
| 2729 | |
| 2730 | /* |
Bram Moolenaar | 8516071 | 2018-06-19 18:27:41 +0200 | [diff] [blame] | 2731 | * Find the start of the next sentence, searching in the direction specified |
| 2732 | * by the "dir" argument. The cursor is positioned on the start of the next |
| 2733 | * sentence when found. If the next sentence is found, return OK. Return FAIL |
| 2734 | * otherwise. See ":h sentence" for the precise definition of a "sentence" |
| 2735 | * text object. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2736 | */ |
| 2737 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 2738 | findsent(int dir, long count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2739 | { |
| 2740 | pos_T pos, tpos; |
| 2741 | int c; |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 2742 | int (*func)(pos_T *); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2743 | int startlnum; |
| 2744 | int noskip = FALSE; /* do not skip blanks */ |
| 2745 | int cpo_J; |
Bram Moolenaar | def9e82 | 2004-12-31 20:58:58 +0000 | [diff] [blame] | 2746 | int found_dot; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2747 | |
| 2748 | pos = curwin->w_cursor; |
| 2749 | if (dir == FORWARD) |
| 2750 | func = incl; |
| 2751 | else |
| 2752 | func = decl; |
| 2753 | |
| 2754 | while (count--) |
| 2755 | { |
| 2756 | /* |
| 2757 | * if on an empty line, skip upto a non-empty line |
| 2758 | */ |
| 2759 | if (gchar_pos(&pos) == NUL) |
| 2760 | { |
| 2761 | do |
| 2762 | if ((*func)(&pos) == -1) |
| 2763 | break; |
| 2764 | while (gchar_pos(&pos) == NUL); |
| 2765 | if (dir == FORWARD) |
| 2766 | goto found; |
| 2767 | } |
| 2768 | /* |
| 2769 | * if on the start of a paragraph or a section and searching forward, |
| 2770 | * go to the next line |
| 2771 | */ |
| 2772 | else if (dir == FORWARD && pos.col == 0 && |
| 2773 | startPS(pos.lnum, NUL, FALSE)) |
| 2774 | { |
| 2775 | if (pos.lnum == curbuf->b_ml.ml_line_count) |
| 2776 | return FAIL; |
| 2777 | ++pos.lnum; |
| 2778 | goto found; |
| 2779 | } |
| 2780 | else if (dir == BACKWARD) |
| 2781 | decl(&pos); |
| 2782 | |
Bram Moolenaar | 8516071 | 2018-06-19 18:27:41 +0200 | [diff] [blame] | 2783 | // go back to the previous non-white non-punctuation character |
Bram Moolenaar | def9e82 | 2004-12-31 20:58:58 +0000 | [diff] [blame] | 2784 | found_dot = FALSE; |
Bram Moolenaar | 8516071 | 2018-06-19 18:27:41 +0200 | [diff] [blame] | 2785 | while (c = gchar_pos(&pos), VIM_ISWHITE(c) |
| 2786 | || vim_strchr((char_u *)".!?)]\"'", c) != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2787 | { |
Bram Moolenaar | 8516071 | 2018-06-19 18:27:41 +0200 | [diff] [blame] | 2788 | tpos = pos; |
| 2789 | if (decl(&tpos) == -1 || (LINEEMPTY(tpos.lnum) && dir == FORWARD)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2790 | break; |
Bram Moolenaar | 8516071 | 2018-06-19 18:27:41 +0200 | [diff] [blame] | 2791 | |
| 2792 | if (found_dot) |
| 2793 | break; |
| 2794 | if (vim_strchr((char_u *) ".!?", c) != NULL) |
| 2795 | found_dot = TRUE; |
| 2796 | |
| 2797 | if (vim_strchr((char_u *) ")]\"'", c) != NULL |
| 2798 | && vim_strchr((char_u *) ".!?)]\"'", gchar_pos(&tpos)) == NULL) |
| 2799 | break; |
| 2800 | |
| 2801 | decl(&pos); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2802 | } |
| 2803 | |
| 2804 | /* remember the line where the search started */ |
| 2805 | startlnum = pos.lnum; |
| 2806 | cpo_J = vim_strchr(p_cpo, CPO_ENDOFSENT) != NULL; |
| 2807 | |
| 2808 | for (;;) /* find end of sentence */ |
| 2809 | { |
| 2810 | c = gchar_pos(&pos); |
| 2811 | if (c == NUL || (pos.col == 0 && startPS(pos.lnum, NUL, FALSE))) |
| 2812 | { |
| 2813 | if (dir == BACKWARD && pos.lnum != startlnum) |
| 2814 | ++pos.lnum; |
| 2815 | break; |
| 2816 | } |
| 2817 | if (c == '.' || c == '!' || c == '?') |
| 2818 | { |
| 2819 | tpos = pos; |
| 2820 | do |
| 2821 | if ((c = inc(&tpos)) == -1) |
| 2822 | break; |
| 2823 | while (vim_strchr((char_u *)")]\"'", c = gchar_pos(&tpos)) |
| 2824 | != NULL); |
| 2825 | if (c == -1 || (!cpo_J && (c == ' ' || c == '\t')) || c == NUL |
| 2826 | || (cpo_J && (c == ' ' && inc(&tpos) >= 0 |
| 2827 | && gchar_pos(&tpos) == ' '))) |
| 2828 | { |
| 2829 | pos = tpos; |
| 2830 | if (gchar_pos(&pos) == NUL) /* skip NUL at EOL */ |
| 2831 | inc(&pos); |
| 2832 | break; |
| 2833 | } |
| 2834 | } |
| 2835 | if ((*func)(&pos) == -1) |
| 2836 | { |
| 2837 | if (count) |
| 2838 | return FAIL; |
| 2839 | noskip = TRUE; |
| 2840 | break; |
| 2841 | } |
| 2842 | } |
| 2843 | found: |
| 2844 | /* skip white space */ |
| 2845 | while (!noskip && ((c = gchar_pos(&pos)) == ' ' || c == '\t')) |
| 2846 | if (incl(&pos) == -1) |
| 2847 | break; |
| 2848 | } |
| 2849 | |
| 2850 | setpcmark(); |
| 2851 | curwin->w_cursor = pos; |
| 2852 | return OK; |
| 2853 | } |
| 2854 | |
| 2855 | /* |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2856 | * Find the next paragraph or section in direction 'dir'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2857 | * Paragraphs are currently supposed to be separated by empty lines. |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2858 | * If 'what' is NUL we go to the next paragraph. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2859 | * If 'what' is '{' or '}' we go to the next section. |
| 2860 | * If 'both' is TRUE also stop at '}'. |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2861 | * Return TRUE if the next paragraph or section was found. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2862 | */ |
| 2863 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 2864 | findpar( |
| 2865 | int *pincl, /* Return: TRUE if last char is to be included */ |
| 2866 | int dir, |
| 2867 | long count, |
| 2868 | int what, |
| 2869 | int both) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2870 | { |
| 2871 | linenr_T curr; |
| 2872 | int did_skip; /* TRUE after separating lines have been skipped */ |
| 2873 | int first; /* TRUE on first line */ |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2874 | int posix = (vim_strchr(p_cpo, CPO_PARA) != NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2875 | #ifdef FEAT_FOLDING |
| 2876 | linenr_T fold_first; /* first line of a closed fold */ |
| 2877 | linenr_T fold_last; /* last line of a closed fold */ |
| 2878 | int fold_skipped; /* TRUE if a closed fold was skipped this |
| 2879 | iteration */ |
| 2880 | #endif |
| 2881 | |
| 2882 | curr = curwin->w_cursor.lnum; |
| 2883 | |
| 2884 | while (count--) |
| 2885 | { |
| 2886 | did_skip = FALSE; |
| 2887 | for (first = TRUE; ; first = FALSE) |
| 2888 | { |
| 2889 | if (*ml_get(curr) != NUL) |
| 2890 | did_skip = TRUE; |
| 2891 | |
| 2892 | #ifdef FEAT_FOLDING |
| 2893 | /* skip folded lines */ |
| 2894 | fold_skipped = FALSE; |
| 2895 | if (first && hasFolding(curr, &fold_first, &fold_last)) |
| 2896 | { |
| 2897 | curr = ((dir > 0) ? fold_last : fold_first) + dir; |
| 2898 | fold_skipped = TRUE; |
| 2899 | } |
| 2900 | #endif |
| 2901 | |
Bram Moolenaar | c4568ab | 2018-11-16 16:21:05 +0100 | [diff] [blame] | 2902 | /* POSIX has its own ideas of what a paragraph boundary is and it |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2903 | * doesn't match historical Vi: It also stops at a "{" in the |
| 2904 | * first column and at an empty line. */ |
| 2905 | if (!first && did_skip && (startPS(curr, what, both) |
| 2906 | || (posix && what == NUL && *ml_get(curr) == '{'))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2907 | break; |
| 2908 | |
| 2909 | #ifdef FEAT_FOLDING |
| 2910 | if (fold_skipped) |
| 2911 | curr -= dir; |
| 2912 | #endif |
| 2913 | if ((curr += dir) < 1 || curr > curbuf->b_ml.ml_line_count) |
| 2914 | { |
| 2915 | if (count) |
| 2916 | return FALSE; |
| 2917 | curr -= dir; |
| 2918 | break; |
| 2919 | } |
| 2920 | } |
| 2921 | } |
| 2922 | setpcmark(); |
| 2923 | if (both && *ml_get(curr) == '}') /* include line with '}' */ |
| 2924 | ++curr; |
| 2925 | curwin->w_cursor.lnum = curr; |
| 2926 | if (curr == curbuf->b_ml.ml_line_count && what != '}') |
| 2927 | { |
Bram Moolenaar | bf3d580 | 2017-03-29 19:48:11 +0200 | [diff] [blame] | 2928 | char_u *line = ml_get(curr); |
| 2929 | |
| 2930 | /* Put the cursor on the last character in the last line and make the |
| 2931 | * motion inclusive. */ |
| 2932 | if ((curwin->w_cursor.col = (colnr_T)STRLEN(line)) != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2933 | { |
| 2934 | --curwin->w_cursor.col; |
Bram Moolenaar | bf3d580 | 2017-03-29 19:48:11 +0200 | [diff] [blame] | 2935 | curwin->w_cursor.col -= |
| 2936 | (*mb_head_off)(line, line + curwin->w_cursor.col); |
Bram Moolenaar | 92d640f | 2005-09-05 22:11:52 +0000 | [diff] [blame] | 2937 | *pincl = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2938 | } |
| 2939 | } |
| 2940 | else |
| 2941 | curwin->w_cursor.col = 0; |
| 2942 | return TRUE; |
| 2943 | } |
| 2944 | |
| 2945 | /* |
| 2946 | * check if the string 's' is a nroff macro that is in option 'opt' |
| 2947 | */ |
| 2948 | static int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 2949 | inmacro(char_u *opt, char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2950 | { |
| 2951 | char_u *macro; |
| 2952 | |
| 2953 | for (macro = opt; macro[0]; ++macro) |
| 2954 | { |
| 2955 | /* Accept two characters in the option being equal to two characters |
| 2956 | * in the line. A space in the option matches with a space in the |
| 2957 | * line or the line having ended. */ |
| 2958 | if ( (macro[0] == s[0] |
| 2959 | || (macro[0] == ' ' |
| 2960 | && (s[0] == NUL || s[0] == ' '))) |
| 2961 | && (macro[1] == s[1] |
| 2962 | || ((macro[1] == NUL || macro[1] == ' ') |
| 2963 | && (s[0] == NUL || s[1] == NUL || s[1] == ' ')))) |
| 2964 | break; |
| 2965 | ++macro; |
| 2966 | if (macro[0] == NUL) |
| 2967 | break; |
| 2968 | } |
| 2969 | return (macro[0] != NUL); |
| 2970 | } |
| 2971 | |
| 2972 | /* |
| 2973 | * startPS: return TRUE if line 'lnum' is the start of a section or paragraph. |
| 2974 | * If 'para' is '{' or '}' only check for sections. |
| 2975 | * If 'both' is TRUE also stop at '}' |
| 2976 | */ |
| 2977 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 2978 | startPS(linenr_T lnum, int para, int both) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2979 | { |
| 2980 | char_u *s; |
| 2981 | |
| 2982 | s = ml_get(lnum); |
| 2983 | if (*s == para || *s == '\f' || (both && *s == '}')) |
| 2984 | return TRUE; |
| 2985 | if (*s == '.' && (inmacro(p_sections, s + 1) || |
| 2986 | (!para && inmacro(p_para, s + 1)))) |
| 2987 | return TRUE; |
| 2988 | return FALSE; |
| 2989 | } |
| 2990 | |
| 2991 | /* |
| 2992 | * The following routines do the word searches performed by the 'w', 'W', |
| 2993 | * 'b', 'B', 'e', and 'E' commands. |
| 2994 | */ |
| 2995 | |
| 2996 | /* |
| 2997 | * To perform these searches, characters are placed into one of three |
| 2998 | * classes, and transitions between classes determine word boundaries. |
| 2999 | * |
| 3000 | * The classes are: |
| 3001 | * |
| 3002 | * 0 - white space |
| 3003 | * 1 - punctuation |
| 3004 | * 2 or higher - keyword characters (letters, digits and underscore) |
| 3005 | */ |
| 3006 | |
| 3007 | static int cls_bigword; /* TRUE for "W", "B" or "E" */ |
| 3008 | |
| 3009 | /* |
| 3010 | * cls() - returns the class of character at curwin->w_cursor |
| 3011 | * |
| 3012 | * If a 'W', 'B', or 'E' motion is being done (cls_bigword == TRUE), chars |
| 3013 | * from class 2 and higher are reported as class 1 since only white space |
| 3014 | * boundaries are of interest. |
| 3015 | */ |
| 3016 | static int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 3017 | cls(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3018 | { |
| 3019 | int c; |
| 3020 | |
| 3021 | c = gchar_cursor(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3022 | if (c == ' ' || c == '\t' || c == NUL) |
| 3023 | return 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3024 | if (enc_dbcs != 0 && c > 0xFF) |
| 3025 | { |
| 3026 | /* If cls_bigword, report multi-byte chars as class 1. */ |
| 3027 | if (enc_dbcs == DBCS_KOR && cls_bigword) |
| 3028 | return 1; |
| 3029 | |
| 3030 | /* process code leading/trailing bytes */ |
| 3031 | return dbcs_class(((unsigned)c >> 8), (c & 0xFF)); |
| 3032 | } |
| 3033 | if (enc_utf8) |
| 3034 | { |
| 3035 | c = utf_class(c); |
| 3036 | if (c != 0 && cls_bigword) |
| 3037 | return 1; |
| 3038 | return c; |
| 3039 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3040 | |
| 3041 | /* If cls_bigword is TRUE, report all non-blanks as class 1. */ |
| 3042 | if (cls_bigword) |
| 3043 | return 1; |
| 3044 | |
| 3045 | if (vim_iswordc(c)) |
| 3046 | return 2; |
| 3047 | return 1; |
| 3048 | } |
| 3049 | |
| 3050 | |
| 3051 | /* |
| 3052 | * fwd_word(count, type, eol) - move forward one word |
| 3053 | * |
| 3054 | * Returns FAIL if the cursor was already at the end of the file. |
| 3055 | * If eol is TRUE, last word stops at end of line (for operators). |
| 3056 | */ |
| 3057 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 3058 | fwd_word( |
| 3059 | long count, |
| 3060 | int bigword, /* "W", "E" or "B" */ |
| 3061 | int eol) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3062 | { |
| 3063 | int sclass; /* starting class */ |
| 3064 | int i; |
| 3065 | int last_line; |
| 3066 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3067 | curwin->w_cursor.coladd = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3068 | cls_bigword = bigword; |
| 3069 | while (--count >= 0) |
| 3070 | { |
| 3071 | #ifdef FEAT_FOLDING |
| 3072 | /* When inside a range of folded lines, move to the last char of the |
| 3073 | * last line. */ |
| 3074 | if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum)) |
| 3075 | coladvance((colnr_T)MAXCOL); |
| 3076 | #endif |
| 3077 | sclass = cls(); |
| 3078 | |
| 3079 | /* |
| 3080 | * We always move at least one character, unless on the last |
| 3081 | * character in the buffer. |
| 3082 | */ |
| 3083 | last_line = (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count); |
| 3084 | i = inc_cursor(); |
| 3085 | if (i == -1 || (i >= 1 && last_line)) /* started at last char in file */ |
| 3086 | return FAIL; |
Bram Moolenaar | 9a14979 | 2007-07-10 10:38:02 +0000 | [diff] [blame] | 3087 | if (i >= 1 && eol && count == 0) /* started at last char in line */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3088 | return OK; |
| 3089 | |
| 3090 | /* |
| 3091 | * Go one char past end of current word (if any) |
| 3092 | */ |
| 3093 | if (sclass != 0) |
| 3094 | while (cls() == sclass) |
| 3095 | { |
| 3096 | i = inc_cursor(); |
| 3097 | if (i == -1 || (i >= 1 && eol && count == 0)) |
| 3098 | return OK; |
| 3099 | } |
| 3100 | |
| 3101 | /* |
| 3102 | * go to next non-white |
| 3103 | */ |
| 3104 | while (cls() == 0) |
| 3105 | { |
| 3106 | /* |
| 3107 | * We'll stop if we land on a blank line |
| 3108 | */ |
| 3109 | if (curwin->w_cursor.col == 0 && *ml_get_curline() == NUL) |
| 3110 | break; |
| 3111 | |
| 3112 | i = inc_cursor(); |
| 3113 | if (i == -1 || (i >= 1 && eol && count == 0)) |
| 3114 | return OK; |
| 3115 | } |
| 3116 | } |
| 3117 | return OK; |
| 3118 | } |
| 3119 | |
| 3120 | /* |
| 3121 | * bck_word() - move backward 'count' words |
| 3122 | * |
| 3123 | * If stop is TRUE and we are already on the start of a word, move one less. |
| 3124 | * |
| 3125 | * Returns FAIL if top of the file was reached. |
| 3126 | */ |
| 3127 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 3128 | bck_word(long count, int bigword, int stop) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3129 | { |
| 3130 | int sclass; /* starting class */ |
| 3131 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3132 | curwin->w_cursor.coladd = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3133 | cls_bigword = bigword; |
| 3134 | while (--count >= 0) |
| 3135 | { |
| 3136 | #ifdef FEAT_FOLDING |
| 3137 | /* When inside a range of folded lines, move to the first char of the |
| 3138 | * first line. */ |
| 3139 | if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum, NULL)) |
| 3140 | curwin->w_cursor.col = 0; |
| 3141 | #endif |
| 3142 | sclass = cls(); |
| 3143 | if (dec_cursor() == -1) /* started at start of file */ |
| 3144 | return FAIL; |
| 3145 | |
| 3146 | if (!stop || sclass == cls() || sclass == 0) |
| 3147 | { |
| 3148 | /* |
| 3149 | * Skip white space before the word. |
| 3150 | * Stop on an empty line. |
| 3151 | */ |
| 3152 | while (cls() == 0) |
| 3153 | { |
| 3154 | if (curwin->w_cursor.col == 0 |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 3155 | && LINEEMPTY(curwin->w_cursor.lnum)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3156 | goto finished; |
| 3157 | if (dec_cursor() == -1) /* hit start of file, stop here */ |
| 3158 | return OK; |
| 3159 | } |
| 3160 | |
| 3161 | /* |
| 3162 | * Move backward to start of this word. |
| 3163 | */ |
| 3164 | if (skip_chars(cls(), BACKWARD)) |
| 3165 | return OK; |
| 3166 | } |
| 3167 | |
| 3168 | inc_cursor(); /* overshot - forward one */ |
| 3169 | finished: |
| 3170 | stop = FALSE; |
| 3171 | } |
| 3172 | return OK; |
| 3173 | } |
| 3174 | |
| 3175 | /* |
| 3176 | * end_word() - move to the end of the word |
| 3177 | * |
| 3178 | * There is an apparent bug in the 'e' motion of the real vi. At least on the |
| 3179 | * System V Release 3 version for the 80386. Unlike 'b' and 'w', the 'e' |
| 3180 | * motion crosses blank lines. When the real vi crosses a blank line in an |
| 3181 | * 'e' motion, the cursor is placed on the FIRST character of the next |
| 3182 | * non-blank line. The 'E' command, however, works correctly. Since this |
| 3183 | * appears to be a bug, I have not duplicated it here. |
| 3184 | * |
| 3185 | * Returns FAIL if end of the file was reached. |
| 3186 | * |
| 3187 | * If stop is TRUE and we are already on the end of a word, move one less. |
| 3188 | * If empty is TRUE stop on an empty line. |
| 3189 | */ |
| 3190 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 3191 | end_word( |
| 3192 | long count, |
| 3193 | int bigword, |
| 3194 | int stop, |
| 3195 | int empty) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3196 | { |
| 3197 | int sclass; /* starting class */ |
| 3198 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3199 | curwin->w_cursor.coladd = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3200 | cls_bigword = bigword; |
| 3201 | while (--count >= 0) |
| 3202 | { |
| 3203 | #ifdef FEAT_FOLDING |
| 3204 | /* When inside a range of folded lines, move to the last char of the |
| 3205 | * last line. */ |
| 3206 | if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum)) |
| 3207 | coladvance((colnr_T)MAXCOL); |
| 3208 | #endif |
| 3209 | sclass = cls(); |
| 3210 | if (inc_cursor() == -1) |
| 3211 | return FAIL; |
| 3212 | |
| 3213 | /* |
| 3214 | * If we're in the middle of a word, we just have to move to the end |
| 3215 | * of it. |
| 3216 | */ |
| 3217 | if (cls() == sclass && sclass != 0) |
| 3218 | { |
| 3219 | /* |
| 3220 | * Move forward to end of the current word |
| 3221 | */ |
| 3222 | if (skip_chars(sclass, FORWARD)) |
| 3223 | return FAIL; |
| 3224 | } |
| 3225 | else if (!stop || sclass == 0) |
| 3226 | { |
| 3227 | /* |
| 3228 | * We were at the end of a word. Go to the end of the next word. |
| 3229 | * First skip white space, if 'empty' is TRUE, stop at empty line. |
| 3230 | */ |
| 3231 | while (cls() == 0) |
| 3232 | { |
| 3233 | if (empty && curwin->w_cursor.col == 0 |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 3234 | && LINEEMPTY(curwin->w_cursor.lnum)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3235 | goto finished; |
| 3236 | if (inc_cursor() == -1) /* hit end of file, stop here */ |
| 3237 | return FAIL; |
| 3238 | } |
| 3239 | |
| 3240 | /* |
| 3241 | * Move forward to the end of this word. |
| 3242 | */ |
| 3243 | if (skip_chars(cls(), FORWARD)) |
| 3244 | return FAIL; |
| 3245 | } |
| 3246 | dec_cursor(); /* overshot - one char backward */ |
| 3247 | finished: |
| 3248 | stop = FALSE; /* we move only one word less */ |
| 3249 | } |
| 3250 | return OK; |
| 3251 | } |
| 3252 | |
| 3253 | /* |
| 3254 | * Move back to the end of the word. |
| 3255 | * |
| 3256 | * Returns FAIL if start of the file was reached. |
| 3257 | */ |
| 3258 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 3259 | bckend_word( |
| 3260 | long count, |
| 3261 | int bigword, /* TRUE for "B" */ |
| 3262 | int eol) /* TRUE: stop at end of line. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3263 | { |
| 3264 | int sclass; /* starting class */ |
| 3265 | int i; |
| 3266 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3267 | curwin->w_cursor.coladd = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3268 | cls_bigword = bigword; |
| 3269 | while (--count >= 0) |
| 3270 | { |
| 3271 | sclass = cls(); |
| 3272 | if ((i = dec_cursor()) == -1) |
| 3273 | return FAIL; |
| 3274 | if (eol && i == 1) |
| 3275 | return OK; |
| 3276 | |
| 3277 | /* |
| 3278 | * Move backward to before the start of this word. |
| 3279 | */ |
| 3280 | if (sclass != 0) |
| 3281 | { |
| 3282 | while (cls() == sclass) |
| 3283 | if ((i = dec_cursor()) == -1 || (eol && i == 1)) |
| 3284 | return OK; |
| 3285 | } |
| 3286 | |
| 3287 | /* |
| 3288 | * Move backward to end of the previous word |
| 3289 | */ |
| 3290 | while (cls() == 0) |
| 3291 | { |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 3292 | if (curwin->w_cursor.col == 0 && LINEEMPTY(curwin->w_cursor.lnum)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3293 | break; |
| 3294 | if ((i = dec_cursor()) == -1 || (eol && i == 1)) |
| 3295 | return OK; |
| 3296 | } |
| 3297 | } |
| 3298 | return OK; |
| 3299 | } |
| 3300 | |
| 3301 | /* |
| 3302 | * Skip a row of characters of the same class. |
| 3303 | * Return TRUE when end-of-file reached, FALSE otherwise. |
| 3304 | */ |
| 3305 | static int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 3306 | skip_chars(int cclass, int dir) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3307 | { |
| 3308 | while (cls() == cclass) |
| 3309 | if ((dir == FORWARD ? inc_cursor() : dec_cursor()) == -1) |
| 3310 | return TRUE; |
| 3311 | return FALSE; |
| 3312 | } |
| 3313 | |
| 3314 | #ifdef FEAT_TEXTOBJ |
| 3315 | /* |
| 3316 | * Go back to the start of the word or the start of white space |
| 3317 | */ |
| 3318 | static void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 3319 | back_in_line(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3320 | { |
| 3321 | int sclass; /* starting class */ |
| 3322 | |
| 3323 | sclass = cls(); |
| 3324 | for (;;) |
| 3325 | { |
| 3326 | if (curwin->w_cursor.col == 0) /* stop at start of line */ |
| 3327 | break; |
| 3328 | dec_cursor(); |
| 3329 | if (cls() != sclass) /* stop at start of word */ |
| 3330 | { |
| 3331 | inc_cursor(); |
| 3332 | break; |
| 3333 | } |
| 3334 | } |
| 3335 | } |
| 3336 | |
| 3337 | static void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 3338 | find_first_blank(pos_T *posp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3339 | { |
| 3340 | int c; |
| 3341 | |
| 3342 | while (decl(posp) != -1) |
| 3343 | { |
| 3344 | c = gchar_pos(posp); |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 3345 | if (!VIM_ISWHITE(c)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3346 | { |
| 3347 | incl(posp); |
| 3348 | break; |
| 3349 | } |
| 3350 | } |
| 3351 | } |
| 3352 | |
| 3353 | /* |
| 3354 | * Skip count/2 sentences and count/2 separating white spaces. |
| 3355 | */ |
| 3356 | static void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 3357 | findsent_forward( |
| 3358 | long count, |
| 3359 | int at_start_sent) /* cursor is at start of sentence */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3360 | { |
| 3361 | while (count--) |
| 3362 | { |
| 3363 | findsent(FORWARD, 1L); |
| 3364 | if (at_start_sent) |
| 3365 | find_first_blank(&curwin->w_cursor); |
| 3366 | if (count == 0 || at_start_sent) |
| 3367 | decl(&curwin->w_cursor); |
| 3368 | at_start_sent = !at_start_sent; |
| 3369 | } |
| 3370 | } |
| 3371 | |
| 3372 | /* |
| 3373 | * Find word under cursor, cursor at end. |
| 3374 | * Used while an operator is pending, and in Visual mode. |
| 3375 | */ |
| 3376 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 3377 | current_word( |
| 3378 | oparg_T *oap, |
| 3379 | long count, |
| 3380 | int include, /* TRUE: include word and white space */ |
| 3381 | int bigword) /* FALSE == word, TRUE == WORD */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3382 | { |
| 3383 | pos_T start_pos; |
| 3384 | pos_T pos; |
| 3385 | int inclusive = TRUE; |
| 3386 | int include_white = FALSE; |
| 3387 | |
| 3388 | cls_bigword = bigword; |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 3389 | CLEAR_POS(&start_pos); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3390 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3391 | /* Correct cursor when 'selection' is exclusive */ |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 3392 | if (VIsual_active && *p_sel == 'e' && LT_POS(VIsual, curwin->w_cursor)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3393 | dec_cursor(); |
| 3394 | |
| 3395 | /* |
| 3396 | * When Visual mode is not active, or when the VIsual area is only one |
| 3397 | * character, select the word and/or white space under the cursor. |
| 3398 | */ |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 3399 | if (!VIsual_active || EQUAL_POS(curwin->w_cursor, VIsual)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3400 | { |
| 3401 | /* |
| 3402 | * Go to start of current word or white space. |
| 3403 | */ |
| 3404 | back_in_line(); |
| 3405 | start_pos = curwin->w_cursor; |
| 3406 | |
| 3407 | /* |
| 3408 | * If the start is on white space, and white space should be included |
| 3409 | * (" word"), or start is not on white space, and white space should |
| 3410 | * not be included ("word"), find end of word. |
| 3411 | */ |
| 3412 | if ((cls() == 0) == include) |
| 3413 | { |
| 3414 | if (end_word(1L, bigword, TRUE, TRUE) == FAIL) |
| 3415 | return FAIL; |
| 3416 | } |
| 3417 | else |
| 3418 | { |
| 3419 | /* |
| 3420 | * If the start is not on white space, and white space should be |
| 3421 | * included ("word "), or start is on white space and white |
| 3422 | * space should not be included (" "), find start of word. |
| 3423 | * If we end up in the first column of the next line (single char |
| 3424 | * word) back up to end of the line. |
| 3425 | */ |
| 3426 | fwd_word(1L, bigword, TRUE); |
| 3427 | if (curwin->w_cursor.col == 0) |
| 3428 | decl(&curwin->w_cursor); |
| 3429 | else |
| 3430 | oneleft(); |
| 3431 | |
| 3432 | if (include) |
| 3433 | include_white = TRUE; |
| 3434 | } |
| 3435 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3436 | if (VIsual_active) |
| 3437 | { |
| 3438 | /* should do something when inclusive == FALSE ! */ |
| 3439 | VIsual = start_pos; |
| 3440 | redraw_curbuf_later(INVERTED); /* update the inversion */ |
| 3441 | } |
| 3442 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3443 | { |
| 3444 | oap->start = start_pos; |
| 3445 | oap->motion_type = MCHAR; |
| 3446 | } |
| 3447 | --count; |
| 3448 | } |
| 3449 | |
| 3450 | /* |
| 3451 | * When count is still > 0, extend with more objects. |
| 3452 | */ |
| 3453 | while (count > 0) |
| 3454 | { |
| 3455 | inclusive = TRUE; |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 3456 | if (VIsual_active && LT_POS(curwin->w_cursor, VIsual)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3457 | { |
| 3458 | /* |
| 3459 | * In Visual mode, with cursor at start: move cursor back. |
| 3460 | */ |
| 3461 | if (decl(&curwin->w_cursor) == -1) |
| 3462 | return FAIL; |
| 3463 | if (include != (cls() != 0)) |
| 3464 | { |
| 3465 | if (bck_word(1L, bigword, TRUE) == FAIL) |
| 3466 | return FAIL; |
| 3467 | } |
| 3468 | else |
| 3469 | { |
| 3470 | if (bckend_word(1L, bigword, TRUE) == FAIL) |
| 3471 | return FAIL; |
| 3472 | (void)incl(&curwin->w_cursor); |
| 3473 | } |
| 3474 | } |
| 3475 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3476 | { |
| 3477 | /* |
| 3478 | * Move cursor forward one word and/or white area. |
| 3479 | */ |
| 3480 | if (incl(&curwin->w_cursor) == -1) |
| 3481 | return FAIL; |
| 3482 | if (include != (cls() == 0)) |
| 3483 | { |
Bram Moolenaar | 2a41f3a | 2005-01-11 21:30:59 +0000 | [diff] [blame] | 3484 | if (fwd_word(1L, bigword, TRUE) == FAIL && count > 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3485 | return FAIL; |
| 3486 | /* |
| 3487 | * If end is just past a new-line, we don't want to include |
Bram Moolenaar | 2a41f3a | 2005-01-11 21:30:59 +0000 | [diff] [blame] | 3488 | * the first character on the line. |
| 3489 | * Put cursor on last char of white. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3490 | */ |
Bram Moolenaar | 2a41f3a | 2005-01-11 21:30:59 +0000 | [diff] [blame] | 3491 | if (oneleft() == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3492 | inclusive = FALSE; |
| 3493 | } |
| 3494 | else |
| 3495 | { |
| 3496 | if (end_word(1L, bigword, TRUE, TRUE) == FAIL) |
| 3497 | return FAIL; |
| 3498 | } |
| 3499 | } |
| 3500 | --count; |
| 3501 | } |
| 3502 | |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 3503 | if (include_white && (cls() != 0 |
| 3504 | || (curwin->w_cursor.col == 0 && !inclusive))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3505 | { |
| 3506 | /* |
| 3507 | * If we don't include white space at the end, move the start |
| 3508 | * to include some white space there. This makes "daw" work |
| 3509 | * better on the last word in a sentence (and "2daw" on last-but-one |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 3510 | * word). Also when "2daw" deletes "word." at the end of the line |
| 3511 | * (cursor is at start of next line). |
| 3512 | * But don't delete white space at start of line (indent). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3513 | */ |
| 3514 | pos = curwin->w_cursor; /* save cursor position */ |
| 3515 | curwin->w_cursor = start_pos; |
| 3516 | if (oneleft() == OK) |
| 3517 | { |
| 3518 | back_in_line(); |
| 3519 | if (cls() == 0 && curwin->w_cursor.col > 0) |
| 3520 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3521 | if (VIsual_active) |
| 3522 | VIsual = curwin->w_cursor; |
| 3523 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3524 | oap->start = curwin->w_cursor; |
| 3525 | } |
| 3526 | } |
| 3527 | curwin->w_cursor = pos; /* put cursor back at end */ |
| 3528 | } |
| 3529 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3530 | if (VIsual_active) |
| 3531 | { |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 3532 | if (*p_sel == 'e' && inclusive && LTOREQ_POS(VIsual, curwin->w_cursor)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3533 | inc_cursor(); |
| 3534 | if (VIsual_mode == 'V') |
| 3535 | { |
| 3536 | VIsual_mode = 'v'; |
| 3537 | redraw_cmdline = TRUE; /* show mode later */ |
| 3538 | } |
| 3539 | } |
| 3540 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3541 | oap->inclusive = inclusive; |
| 3542 | |
| 3543 | return OK; |
| 3544 | } |
| 3545 | |
| 3546 | /* |
| 3547 | * Find sentence(s) under the cursor, cursor at end. |
| 3548 | * When Visual active, extend it by one or more sentences. |
| 3549 | */ |
| 3550 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 3551 | current_sent(oparg_T *oap, long count, int include) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3552 | { |
| 3553 | pos_T start_pos; |
| 3554 | pos_T pos; |
| 3555 | int start_blank; |
| 3556 | int c; |
| 3557 | int at_start_sent; |
| 3558 | long ncount; |
| 3559 | |
| 3560 | start_pos = curwin->w_cursor; |
| 3561 | pos = start_pos; |
| 3562 | findsent(FORWARD, 1L); /* Find start of next sentence. */ |
| 3563 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3564 | /* |
Bram Moolenaar | 641e286 | 2012-07-25 15:06:34 +0200 | [diff] [blame] | 3565 | * When the Visual area is bigger than one character: Extend it. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3566 | */ |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 3567 | if (VIsual_active && !EQUAL_POS(start_pos, VIsual)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3568 | { |
| 3569 | extend: |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 3570 | if (LT_POS(start_pos, VIsual)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3571 | { |
| 3572 | /* |
| 3573 | * Cursor at start of Visual area. |
| 3574 | * Find out where we are: |
| 3575 | * - in the white space before a sentence |
| 3576 | * - in a sentence or just after it |
| 3577 | * - at the start of a sentence |
| 3578 | */ |
| 3579 | at_start_sent = TRUE; |
| 3580 | decl(&pos); |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 3581 | while (LT_POS(pos, curwin->w_cursor)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3582 | { |
| 3583 | c = gchar_pos(&pos); |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 3584 | if (!VIM_ISWHITE(c)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3585 | { |
| 3586 | at_start_sent = FALSE; |
| 3587 | break; |
| 3588 | } |
| 3589 | incl(&pos); |
| 3590 | } |
| 3591 | if (!at_start_sent) |
| 3592 | { |
| 3593 | findsent(BACKWARD, 1L); |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 3594 | if (EQUAL_POS(curwin->w_cursor, start_pos)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3595 | at_start_sent = TRUE; /* exactly at start of sentence */ |
| 3596 | else |
| 3597 | /* inside a sentence, go to its end (start of next) */ |
| 3598 | findsent(FORWARD, 1L); |
| 3599 | } |
| 3600 | if (include) /* "as" gets twice as much as "is" */ |
| 3601 | count *= 2; |
| 3602 | while (count--) |
| 3603 | { |
| 3604 | if (at_start_sent) |
| 3605 | find_first_blank(&curwin->w_cursor); |
| 3606 | c = gchar_cursor(); |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 3607 | if (!at_start_sent || (!include && !VIM_ISWHITE(c))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3608 | findsent(BACKWARD, 1L); |
| 3609 | at_start_sent = !at_start_sent; |
| 3610 | } |
| 3611 | } |
| 3612 | else |
| 3613 | { |
| 3614 | /* |
| 3615 | * Cursor at end of Visual area. |
| 3616 | * Find out where we are: |
| 3617 | * - just before a sentence |
| 3618 | * - just before or in the white space before a sentence |
| 3619 | * - in a sentence |
| 3620 | */ |
| 3621 | incl(&pos); |
| 3622 | at_start_sent = TRUE; |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 3623 | /* not just before a sentence */ |
| 3624 | if (!EQUAL_POS(pos, curwin->w_cursor)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3625 | { |
| 3626 | at_start_sent = FALSE; |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 3627 | while (LT_POS(pos, curwin->w_cursor)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3628 | { |
| 3629 | c = gchar_pos(&pos); |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 3630 | if (!VIM_ISWHITE(c)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3631 | { |
| 3632 | at_start_sent = TRUE; |
| 3633 | break; |
| 3634 | } |
| 3635 | incl(&pos); |
| 3636 | } |
| 3637 | if (at_start_sent) /* in the sentence */ |
| 3638 | findsent(BACKWARD, 1L); |
| 3639 | else /* in/before white before a sentence */ |
| 3640 | curwin->w_cursor = start_pos; |
| 3641 | } |
| 3642 | |
| 3643 | if (include) /* "as" gets twice as much as "is" */ |
| 3644 | count *= 2; |
| 3645 | findsent_forward(count, at_start_sent); |
| 3646 | if (*p_sel == 'e') |
| 3647 | ++curwin->w_cursor.col; |
| 3648 | } |
| 3649 | return OK; |
| 3650 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3651 | |
| 3652 | /* |
Bram Moolenaar | 641e286 | 2012-07-25 15:06:34 +0200 | [diff] [blame] | 3653 | * If the cursor started on a blank, check if it is just before the start |
| 3654 | * of the next sentence. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3655 | */ |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 3656 | while (c = gchar_pos(&pos), VIM_ISWHITE(c)) /* VIM_ISWHITE() is a macro */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3657 | incl(&pos); |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 3658 | if (EQUAL_POS(pos, curwin->w_cursor)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3659 | { |
| 3660 | start_blank = TRUE; |
| 3661 | find_first_blank(&start_pos); /* go back to first blank */ |
| 3662 | } |
| 3663 | else |
| 3664 | { |
| 3665 | start_blank = FALSE; |
| 3666 | findsent(BACKWARD, 1L); |
| 3667 | start_pos = curwin->w_cursor; |
| 3668 | } |
| 3669 | if (include) |
| 3670 | ncount = count * 2; |
| 3671 | else |
| 3672 | { |
| 3673 | ncount = count; |
| 3674 | if (start_blank) |
| 3675 | --ncount; |
| 3676 | } |
Bram Moolenaar | def9e82 | 2004-12-31 20:58:58 +0000 | [diff] [blame] | 3677 | if (ncount > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3678 | findsent_forward(ncount, TRUE); |
| 3679 | else |
| 3680 | decl(&curwin->w_cursor); |
| 3681 | |
| 3682 | if (include) |
| 3683 | { |
| 3684 | /* |
| 3685 | * If the blank in front of the sentence is included, exclude the |
| 3686 | * blanks at the end of the sentence, go back to the first blank. |
| 3687 | * If there are no trailing blanks, try to include leading blanks. |
| 3688 | */ |
| 3689 | if (start_blank) |
| 3690 | { |
| 3691 | find_first_blank(&curwin->w_cursor); |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 3692 | c = gchar_pos(&curwin->w_cursor); /* VIM_ISWHITE() is a macro */ |
| 3693 | if (VIM_ISWHITE(c)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3694 | decl(&curwin->w_cursor); |
| 3695 | } |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 3696 | else if (c = gchar_cursor(), !VIM_ISWHITE(c)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3697 | find_first_blank(&start_pos); |
| 3698 | } |
| 3699 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3700 | if (VIsual_active) |
| 3701 | { |
Bram Moolenaar | 641e286 | 2012-07-25 15:06:34 +0200 | [diff] [blame] | 3702 | /* Avoid getting stuck with "is" on a single space before a sentence. */ |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 3703 | if (EQUAL_POS(start_pos, curwin->w_cursor)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3704 | goto extend; |
| 3705 | if (*p_sel == 'e') |
| 3706 | ++curwin->w_cursor.col; |
| 3707 | VIsual = start_pos; |
| 3708 | VIsual_mode = 'v'; |
Bram Moolenaar | 779f2fc | 2016-09-01 20:58:24 +0200 | [diff] [blame] | 3709 | redraw_cmdline = TRUE; /* show mode later */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3710 | redraw_curbuf_later(INVERTED); /* update the inversion */ |
| 3711 | } |
| 3712 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3713 | { |
| 3714 | /* include a newline after the sentence, if there is one */ |
| 3715 | if (incl(&curwin->w_cursor) == -1) |
| 3716 | oap->inclusive = TRUE; |
| 3717 | else |
| 3718 | oap->inclusive = FALSE; |
| 3719 | oap->start = start_pos; |
| 3720 | oap->motion_type = MCHAR; |
| 3721 | } |
| 3722 | return OK; |
| 3723 | } |
| 3724 | |
Bram Moolenaar | d6c04cd | 2005-07-19 22:18:49 +0000 | [diff] [blame] | 3725 | /* |
| 3726 | * Find block under the cursor, cursor at end. |
Bram Moolenaar | 84a05ac | 2013-05-06 04:24:17 +0200 | [diff] [blame] | 3727 | * "what" and "other" are two matching parenthesis/brace/etc. |
Bram Moolenaar | d6c04cd | 2005-07-19 22:18:49 +0000 | [diff] [blame] | 3728 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3729 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 3730 | current_block( |
| 3731 | oparg_T *oap, |
| 3732 | long count, |
| 3733 | int include, /* TRUE == include white space */ |
| 3734 | int what, /* '(', '{', etc. */ |
| 3735 | int other) /* ')', '}', etc. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3736 | { |
| 3737 | pos_T old_pos; |
| 3738 | pos_T *pos = NULL; |
| 3739 | pos_T start_pos; |
| 3740 | pos_T *end_pos; |
| 3741 | pos_T old_start, old_end; |
| 3742 | char_u *save_cpo; |
Bram Moolenaar | d6c04cd | 2005-07-19 22:18:49 +0000 | [diff] [blame] | 3743 | int sol = FALSE; /* '{' at start of line */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3744 | |
| 3745 | old_pos = curwin->w_cursor; |
Bram Moolenaar | d6c04cd | 2005-07-19 22:18:49 +0000 | [diff] [blame] | 3746 | old_end = curwin->w_cursor; /* remember where we started */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3747 | old_start = old_end; |
| 3748 | |
| 3749 | /* |
| 3750 | * If we start on '(', '{', ')', '}', etc., use the whole block inclusive. |
| 3751 | */ |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 3752 | if (!VIsual_active || EQUAL_POS(VIsual, curwin->w_cursor)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3753 | { |
| 3754 | setpcmark(); |
Bram Moolenaar | d6c04cd | 2005-07-19 22:18:49 +0000 | [diff] [blame] | 3755 | if (what == '{') /* ignore indent */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3756 | while (inindent(1)) |
| 3757 | if (inc_cursor() != 0) |
| 3758 | break; |
Bram Moolenaar | d6c04cd | 2005-07-19 22:18:49 +0000 | [diff] [blame] | 3759 | if (gchar_cursor() == what) |
| 3760 | /* cursor on '(' or '{', move cursor just after it */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3761 | ++curwin->w_cursor.col; |
| 3762 | } |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 3763 | else if (LT_POS(VIsual, curwin->w_cursor)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3764 | { |
| 3765 | old_start = VIsual; |
| 3766 | curwin->w_cursor = VIsual; /* cursor at low end of Visual */ |
| 3767 | } |
| 3768 | else |
| 3769 | old_end = VIsual; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3770 | |
| 3771 | /* |
| 3772 | * Search backwards for unclosed '(', '{', etc.. |
| 3773 | * Put this position in start_pos. |
Bram Moolenaar | 438b64a | 2015-03-13 15:03:00 +0100 | [diff] [blame] | 3774 | * Ignore quotes here. Keep the "M" flag in 'cpo', as that is what the |
| 3775 | * user wants. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3776 | */ |
| 3777 | save_cpo = p_cpo; |
Bram Moolenaar | 438b64a | 2015-03-13 15:03:00 +0100 | [diff] [blame] | 3778 | p_cpo = (char_u *)(vim_strchr(p_cpo, CPO_MATCHBSL) != NULL ? "%M" : "%"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3779 | while (count-- > 0) |
| 3780 | { |
| 3781 | if ((pos = findmatch(NULL, what)) == NULL) |
| 3782 | break; |
| 3783 | curwin->w_cursor = *pos; |
| 3784 | start_pos = *pos; /* the findmatch for end_pos will overwrite *pos */ |
| 3785 | } |
| 3786 | p_cpo = save_cpo; |
| 3787 | |
| 3788 | /* |
| 3789 | * Search for matching ')', '}', etc. |
| 3790 | * Put this position in curwin->w_cursor. |
| 3791 | */ |
| 3792 | if (pos == NULL || (end_pos = findmatch(NULL, other)) == NULL) |
| 3793 | { |
| 3794 | curwin->w_cursor = old_pos; |
| 3795 | return FAIL; |
| 3796 | } |
| 3797 | curwin->w_cursor = *end_pos; |
| 3798 | |
| 3799 | /* |
| 3800 | * Try to exclude the '(', '{', ')', '}', etc. when "include" is FALSE. |
Bram Moolenaar | 7a54a90 | 2014-06-17 13:50:13 +0200 | [diff] [blame] | 3801 | * If the ending '}', ')' or ']' is only preceded by indent, skip that |
| 3802 | * indent. But only if the resulting area is not smaller than what we |
| 3803 | * started with. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3804 | */ |
| 3805 | while (!include) |
| 3806 | { |
| 3807 | incl(&start_pos); |
| 3808 | sol = (curwin->w_cursor.col == 0); |
| 3809 | decl(&curwin->w_cursor); |
Bram Moolenaar | 7a54a90 | 2014-06-17 13:50:13 +0200 | [diff] [blame] | 3810 | while (inindent(1)) |
| 3811 | { |
| 3812 | sol = TRUE; |
| 3813 | if (decl(&curwin->w_cursor) != 0) |
| 3814 | break; |
| 3815 | } |
| 3816 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3817 | /* |
| 3818 | * In Visual mode, when the resulting area is not bigger than what we |
| 3819 | * started with, extend it to the next block, and then exclude again. |
| 3820 | */ |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 3821 | if (!LT_POS(start_pos, old_start) && !LT_POS(old_end, curwin->w_cursor) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3822 | && VIsual_active) |
| 3823 | { |
| 3824 | curwin->w_cursor = old_start; |
| 3825 | decl(&curwin->w_cursor); |
| 3826 | if ((pos = findmatch(NULL, what)) == NULL) |
| 3827 | { |
| 3828 | curwin->w_cursor = old_pos; |
| 3829 | return FAIL; |
| 3830 | } |
| 3831 | start_pos = *pos; |
| 3832 | curwin->w_cursor = *pos; |
| 3833 | if ((end_pos = findmatch(NULL, other)) == NULL) |
| 3834 | { |
| 3835 | curwin->w_cursor = old_pos; |
| 3836 | return FAIL; |
| 3837 | } |
| 3838 | curwin->w_cursor = *end_pos; |
| 3839 | } |
| 3840 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3841 | break; |
| 3842 | } |
| 3843 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3844 | if (VIsual_active) |
| 3845 | { |
| 3846 | if (*p_sel == 'e') |
Bram Moolenaar | 8667d66 | 2015-09-01 18:27:49 +0200 | [diff] [blame] | 3847 | inc(&curwin->w_cursor); |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 3848 | if (sol && gchar_cursor() != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3849 | inc(&curwin->w_cursor); /* include the line break */ |
| 3850 | VIsual = start_pos; |
| 3851 | VIsual_mode = 'v'; |
| 3852 | redraw_curbuf_later(INVERTED); /* update the inversion */ |
| 3853 | showmode(); |
| 3854 | } |
| 3855 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3856 | { |
| 3857 | oap->start = start_pos; |
| 3858 | oap->motion_type = MCHAR; |
Bram Moolenaar | 1864a4e | 2007-06-19 10:56:05 +0000 | [diff] [blame] | 3859 | oap->inclusive = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3860 | if (sol) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3861 | incl(&curwin->w_cursor); |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 3862 | else if (LTOREQ_POS(start_pos, curwin->w_cursor)) |
Bram Moolenaar | 1864a4e | 2007-06-19 10:56:05 +0000 | [diff] [blame] | 3863 | /* Include the character under the cursor. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3864 | oap->inclusive = TRUE; |
Bram Moolenaar | 1864a4e | 2007-06-19 10:56:05 +0000 | [diff] [blame] | 3865 | else |
| 3866 | /* End is before the start (no text in between <>, [], etc.): don't |
| 3867 | * operate on any text. */ |
| 3868 | curwin->w_cursor = start_pos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3869 | } |
| 3870 | |
| 3871 | return OK; |
| 3872 | } |
| 3873 | |
Bram Moolenaar | d6c04cd | 2005-07-19 22:18:49 +0000 | [diff] [blame] | 3874 | /* |
| 3875 | * Return TRUE if the cursor is on a "<aaa>" tag. Ignore "<aaa/>". |
| 3876 | * When "end_tag" is TRUE return TRUE if the cursor is on "</aaa>". |
| 3877 | */ |
| 3878 | static int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 3879 | in_html_tag( |
| 3880 | int end_tag) |
Bram Moolenaar | d6c04cd | 2005-07-19 22:18:49 +0000 | [diff] [blame] | 3881 | { |
| 3882 | char_u *line = ml_get_curline(); |
| 3883 | char_u *p; |
| 3884 | int c; |
| 3885 | int lc = NUL; |
| 3886 | pos_T pos; |
| 3887 | |
Bram Moolenaar | d6c04cd | 2005-07-19 22:18:49 +0000 | [diff] [blame] | 3888 | if (enc_dbcs) |
| 3889 | { |
| 3890 | char_u *lp = NULL; |
| 3891 | |
| 3892 | /* We search forward until the cursor, because searching backwards is |
| 3893 | * very slow for DBCS encodings. */ |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 3894 | for (p = line; p < line + curwin->w_cursor.col; MB_PTR_ADV(p)) |
Bram Moolenaar | d6c04cd | 2005-07-19 22:18:49 +0000 | [diff] [blame] | 3895 | if (*p == '>' || *p == '<') |
| 3896 | { |
| 3897 | lc = *p; |
| 3898 | lp = p; |
| 3899 | } |
| 3900 | if (*p != '<') /* check for '<' under cursor */ |
| 3901 | { |
| 3902 | if (lc != '<') |
| 3903 | return FALSE; |
| 3904 | p = lp; |
| 3905 | } |
| 3906 | } |
| 3907 | else |
Bram Moolenaar | d6c04cd | 2005-07-19 22:18:49 +0000 | [diff] [blame] | 3908 | { |
| 3909 | for (p = line + curwin->w_cursor.col; p > line; ) |
| 3910 | { |
| 3911 | if (*p == '<') /* find '<' under/before cursor */ |
| 3912 | break; |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 3913 | MB_PTR_BACK(line, p); |
Bram Moolenaar | d6c04cd | 2005-07-19 22:18:49 +0000 | [diff] [blame] | 3914 | if (*p == '>') /* find '>' before cursor */ |
| 3915 | break; |
| 3916 | } |
| 3917 | if (*p != '<') |
| 3918 | return FALSE; |
| 3919 | } |
| 3920 | |
| 3921 | pos.lnum = curwin->w_cursor.lnum; |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 3922 | pos.col = (colnr_T)(p - line); |
Bram Moolenaar | d6c04cd | 2005-07-19 22:18:49 +0000 | [diff] [blame] | 3923 | |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 3924 | MB_PTR_ADV(p); |
Bram Moolenaar | d6c04cd | 2005-07-19 22:18:49 +0000 | [diff] [blame] | 3925 | if (end_tag) |
| 3926 | /* check that there is a '/' after the '<' */ |
| 3927 | return *p == '/'; |
| 3928 | |
| 3929 | /* check that there is no '/' after the '<' */ |
| 3930 | if (*p == '/') |
| 3931 | return FALSE; |
| 3932 | |
| 3933 | /* check that the matching '>' is not preceded by '/' */ |
| 3934 | for (;;) |
| 3935 | { |
| 3936 | if (inc(&pos) < 0) |
| 3937 | return FALSE; |
| 3938 | c = *ml_get_pos(&pos); |
| 3939 | if (c == '>') |
| 3940 | break; |
| 3941 | lc = c; |
| 3942 | } |
| 3943 | return lc != '/'; |
| 3944 | } |
| 3945 | |
| 3946 | /* |
| 3947 | * Find tag block under the cursor, cursor at end. |
| 3948 | */ |
| 3949 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 3950 | current_tagblock( |
| 3951 | oparg_T *oap, |
| 3952 | long count_arg, |
| 3953 | int include) /* TRUE == include white space */ |
Bram Moolenaar | d6c04cd | 2005-07-19 22:18:49 +0000 | [diff] [blame] | 3954 | { |
| 3955 | long count = count_arg; |
| 3956 | long n; |
| 3957 | pos_T old_pos; |
| 3958 | pos_T start_pos; |
| 3959 | pos_T end_pos; |
| 3960 | pos_T old_start, old_end; |
| 3961 | char_u *spat, *epat; |
| 3962 | char_u *p; |
| 3963 | char_u *cp; |
| 3964 | int len; |
| 3965 | int r; |
| 3966 | int do_include = include; |
| 3967 | int save_p_ws = p_ws; |
| 3968 | int retval = FAIL; |
Bram Moolenaar | b6c2735 | 2015-03-05 19:57:49 +0100 | [diff] [blame] | 3969 | int is_inclusive = TRUE; |
Bram Moolenaar | d6c04cd | 2005-07-19 22:18:49 +0000 | [diff] [blame] | 3970 | |
| 3971 | p_ws = FALSE; |
| 3972 | |
| 3973 | old_pos = curwin->w_cursor; |
| 3974 | old_end = curwin->w_cursor; /* remember where we started */ |
| 3975 | old_start = old_end; |
Bram Moolenaar | 2a6f211 | 2008-01-26 20:15:46 +0000 | [diff] [blame] | 3976 | if (!VIsual_active || *p_sel == 'e') |
Bram Moolenaar | 2a6f211 | 2008-01-26 20:15:46 +0000 | [diff] [blame] | 3977 | decl(&old_end); /* old_end is inclusive */ |
Bram Moolenaar | d6c04cd | 2005-07-19 22:18:49 +0000 | [diff] [blame] | 3978 | |
| 3979 | /* |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 3980 | * If we start on "<aaa>" select that block. |
Bram Moolenaar | d6c04cd | 2005-07-19 22:18:49 +0000 | [diff] [blame] | 3981 | */ |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 3982 | if (!VIsual_active || EQUAL_POS(VIsual, curwin->w_cursor)) |
Bram Moolenaar | d6c04cd | 2005-07-19 22:18:49 +0000 | [diff] [blame] | 3983 | { |
| 3984 | setpcmark(); |
| 3985 | |
| 3986 | /* ignore indent */ |
| 3987 | while (inindent(1)) |
| 3988 | if (inc_cursor() != 0) |
| 3989 | break; |
| 3990 | |
| 3991 | if (in_html_tag(FALSE)) |
| 3992 | { |
Bram Moolenaar | 1864a4e | 2007-06-19 10:56:05 +0000 | [diff] [blame] | 3993 | /* cursor on start tag, move to its '>' */ |
Bram Moolenaar | d6c04cd | 2005-07-19 22:18:49 +0000 | [diff] [blame] | 3994 | while (*ml_get_cursor() != '>') |
| 3995 | if (inc_cursor() < 0) |
| 3996 | break; |
| 3997 | } |
| 3998 | else if (in_html_tag(TRUE)) |
| 3999 | { |
| 4000 | /* cursor on end tag, move to just before it */ |
| 4001 | while (*ml_get_cursor() != '<') |
| 4002 | if (dec_cursor() < 0) |
| 4003 | break; |
| 4004 | dec_cursor(); |
| 4005 | old_end = curwin->w_cursor; |
| 4006 | } |
| 4007 | } |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 4008 | else if (LT_POS(VIsual, curwin->w_cursor)) |
Bram Moolenaar | d6c04cd | 2005-07-19 22:18:49 +0000 | [diff] [blame] | 4009 | { |
| 4010 | old_start = VIsual; |
| 4011 | curwin->w_cursor = VIsual; /* cursor at low end of Visual */ |
| 4012 | } |
| 4013 | else |
| 4014 | old_end = VIsual; |
Bram Moolenaar | d6c04cd | 2005-07-19 22:18:49 +0000 | [diff] [blame] | 4015 | |
| 4016 | again: |
| 4017 | /* |
| 4018 | * Search backwards for unclosed "<aaa>". |
| 4019 | * Put this position in start_pos. |
| 4020 | */ |
| 4021 | for (n = 0; n < count; ++n) |
| 4022 | { |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 4023 | if (do_searchpair((char_u *)"<[^ \t>/!]\\+\\%(\\_s\\_[^>]\\{-}[^/]>\\|$\\|\\_s\\=>\\)", |
Bram Moolenaar | d6c04cd | 2005-07-19 22:18:49 +0000 | [diff] [blame] | 4024 | (char_u *)"", |
Bram Moolenaar | 4857048 | 2017-10-30 21:48:41 +0100 | [diff] [blame] | 4025 | (char_u *)"</[^>]*>", BACKWARD, NULL, 0, |
Bram Moolenaar | 7692929 | 2008-01-06 19:07:36 +0000 | [diff] [blame] | 4026 | NULL, (linenr_T)0, 0L) <= 0) |
Bram Moolenaar | d6c04cd | 2005-07-19 22:18:49 +0000 | [diff] [blame] | 4027 | { |
| 4028 | curwin->w_cursor = old_pos; |
| 4029 | goto theend; |
| 4030 | } |
| 4031 | } |
| 4032 | start_pos = curwin->w_cursor; |
| 4033 | |
| 4034 | /* |
| 4035 | * Search for matching "</aaa>". First isolate the "aaa". |
| 4036 | */ |
| 4037 | inc_cursor(); |
| 4038 | p = ml_get_cursor(); |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 4039 | for (cp = p; *cp != NUL && *cp != '>' && !VIM_ISWHITE(*cp); MB_PTR_ADV(cp)) |
Bram Moolenaar | d6c04cd | 2005-07-19 22:18:49 +0000 | [diff] [blame] | 4040 | ; |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 4041 | len = (int)(cp - p); |
Bram Moolenaar | d6c04cd | 2005-07-19 22:18:49 +0000 | [diff] [blame] | 4042 | if (len == 0) |
| 4043 | { |
| 4044 | curwin->w_cursor = old_pos; |
| 4045 | goto theend; |
| 4046 | } |
Bram Moolenaar | 16c31fe | 2012-01-26 20:58:26 +0100 | [diff] [blame] | 4047 | spat = alloc(len + 31); |
Bram Moolenaar | d6c04cd | 2005-07-19 22:18:49 +0000 | [diff] [blame] | 4048 | epat = alloc(len + 9); |
| 4049 | if (spat == NULL || epat == NULL) |
| 4050 | { |
| 4051 | vim_free(spat); |
| 4052 | vim_free(epat); |
| 4053 | curwin->w_cursor = old_pos; |
| 4054 | goto theend; |
| 4055 | } |
Bram Moolenaar | 16c31fe | 2012-01-26 20:58:26 +0100 | [diff] [blame] | 4056 | sprintf((char *)spat, "<%.*s\\>\\%%(\\s\\_[^>]\\{-}[^/]>\\|>\\)\\c", len, p); |
Bram Moolenaar | d6c04cd | 2005-07-19 22:18:49 +0000 | [diff] [blame] | 4057 | sprintf((char *)epat, "</%.*s>\\c", len, p); |
| 4058 | |
Bram Moolenaar | 4857048 | 2017-10-30 21:48:41 +0100 | [diff] [blame] | 4059 | r = do_searchpair(spat, (char_u *)"", epat, FORWARD, NULL, |
Bram Moolenaar | 7692929 | 2008-01-06 19:07:36 +0000 | [diff] [blame] | 4060 | 0, NULL, (linenr_T)0, 0L); |
Bram Moolenaar | d6c04cd | 2005-07-19 22:18:49 +0000 | [diff] [blame] | 4061 | |
| 4062 | vim_free(spat); |
| 4063 | vim_free(epat); |
| 4064 | |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 4065 | if (r < 1 || LT_POS(curwin->w_cursor, old_end)) |
Bram Moolenaar | d6c04cd | 2005-07-19 22:18:49 +0000 | [diff] [blame] | 4066 | { |
| 4067 | /* Can't find other end or it's before the previous end. Could be a |
| 4068 | * HTML tag that doesn't have a matching end. Search backwards for |
| 4069 | * another starting tag. */ |
| 4070 | count = 1; |
| 4071 | curwin->w_cursor = start_pos; |
| 4072 | goto again; |
| 4073 | } |
| 4074 | |
Bram Moolenaar | 1c17ffa | 2018-04-24 15:19:04 +0200 | [diff] [blame] | 4075 | if (do_include) |
Bram Moolenaar | d6c04cd | 2005-07-19 22:18:49 +0000 | [diff] [blame] | 4076 | { |
| 4077 | /* Include up to the '>'. */ |
| 4078 | while (*ml_get_cursor() != '>') |
| 4079 | if (inc_cursor() < 0) |
| 4080 | break; |
| 4081 | } |
| 4082 | else |
| 4083 | { |
Bram Moolenaar | b6c2735 | 2015-03-05 19:57:49 +0100 | [diff] [blame] | 4084 | char_u *c = ml_get_cursor(); |
| 4085 | |
| 4086 | /* Exclude the '<' of the end tag. |
| 4087 | * If the closing tag is on new line, do not decrement cursor, but |
| 4088 | * make operation exclusive, so that the linefeed will be selected */ |
| 4089 | if (*c == '<' && !VIsual_active && curwin->w_cursor.col == 0) |
| 4090 | /* do not decrement cursor */ |
| 4091 | is_inclusive = FALSE; |
| 4092 | else if (*c == '<') |
Bram Moolenaar | d6c04cd | 2005-07-19 22:18:49 +0000 | [diff] [blame] | 4093 | dec_cursor(); |
| 4094 | } |
| 4095 | end_pos = curwin->w_cursor; |
| 4096 | |
| 4097 | if (!do_include) |
| 4098 | { |
| 4099 | /* Exclude the start tag. */ |
| 4100 | curwin->w_cursor = start_pos; |
| 4101 | while (inc_cursor() >= 0) |
Bram Moolenaar | 1864a4e | 2007-06-19 10:56:05 +0000 | [diff] [blame] | 4102 | if (*ml_get_cursor() == '>') |
Bram Moolenaar | d6c04cd | 2005-07-19 22:18:49 +0000 | [diff] [blame] | 4103 | { |
| 4104 | inc_cursor(); |
| 4105 | start_pos = curwin->w_cursor; |
| 4106 | break; |
| 4107 | } |
| 4108 | curwin->w_cursor = end_pos; |
| 4109 | |
Bram Moolenaar | b476cb7 | 2018-08-16 21:37:50 +0200 | [diff] [blame] | 4110 | // If we are in Visual mode and now have the same text as before set |
| 4111 | // "do_include" and try again. |
| 4112 | if (VIsual_active && EQUAL_POS(start_pos, old_start) |
| 4113 | && EQUAL_POS(end_pos, old_end)) |
Bram Moolenaar | d6c04cd | 2005-07-19 22:18:49 +0000 | [diff] [blame] | 4114 | { |
| 4115 | do_include = TRUE; |
| 4116 | curwin->w_cursor = old_start; |
| 4117 | count = count_arg; |
| 4118 | goto again; |
| 4119 | } |
| 4120 | } |
| 4121 | |
Bram Moolenaar | d6c04cd | 2005-07-19 22:18:49 +0000 | [diff] [blame] | 4122 | if (VIsual_active) |
| 4123 | { |
Bram Moolenaar | 1864a4e | 2007-06-19 10:56:05 +0000 | [diff] [blame] | 4124 | /* If the end is before the start there is no text between tags, select |
| 4125 | * the char under the cursor. */ |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 4126 | if (LT_POS(end_pos, start_pos)) |
Bram Moolenaar | 1864a4e | 2007-06-19 10:56:05 +0000 | [diff] [blame] | 4127 | curwin->w_cursor = start_pos; |
| 4128 | else if (*p_sel == 'e') |
Bram Moolenaar | 8340dd9 | 2014-12-13 20:11:33 +0100 | [diff] [blame] | 4129 | inc_cursor(); |
Bram Moolenaar | d6c04cd | 2005-07-19 22:18:49 +0000 | [diff] [blame] | 4130 | VIsual = start_pos; |
| 4131 | VIsual_mode = 'v'; |
| 4132 | redraw_curbuf_later(INVERTED); /* update the inversion */ |
| 4133 | showmode(); |
| 4134 | } |
| 4135 | else |
Bram Moolenaar | d6c04cd | 2005-07-19 22:18:49 +0000 | [diff] [blame] | 4136 | { |
| 4137 | oap->start = start_pos; |
| 4138 | oap->motion_type = MCHAR; |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 4139 | if (LT_POS(end_pos, start_pos)) |
Bram Moolenaar | 1864a4e | 2007-06-19 10:56:05 +0000 | [diff] [blame] | 4140 | { |
| 4141 | /* End is before the start: there is no text between tags; operate |
| 4142 | * on an empty area. */ |
| 4143 | curwin->w_cursor = start_pos; |
| 4144 | oap->inclusive = FALSE; |
| 4145 | } |
| 4146 | else |
Bram Moolenaar | b6c2735 | 2015-03-05 19:57:49 +0100 | [diff] [blame] | 4147 | oap->inclusive = is_inclusive; |
Bram Moolenaar | d6c04cd | 2005-07-19 22:18:49 +0000 | [diff] [blame] | 4148 | } |
| 4149 | retval = OK; |
| 4150 | |
| 4151 | theend: |
| 4152 | p_ws = save_p_ws; |
| 4153 | return retval; |
| 4154 | } |
| 4155 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4156 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 4157 | current_par( |
| 4158 | oparg_T *oap, |
| 4159 | long count, |
| 4160 | int include, /* TRUE == include white space */ |
| 4161 | int type) /* 'p' for paragraph, 'S' for section */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4162 | { |
| 4163 | linenr_T start_lnum; |
| 4164 | linenr_T end_lnum; |
| 4165 | int white_in_front; |
| 4166 | int dir; |
| 4167 | int start_is_white; |
| 4168 | int prev_start_is_white; |
| 4169 | int retval = OK; |
| 4170 | int do_white = FALSE; |
| 4171 | int t; |
| 4172 | int i; |
| 4173 | |
| 4174 | if (type == 'S') /* not implemented yet */ |
| 4175 | return FAIL; |
| 4176 | |
| 4177 | start_lnum = curwin->w_cursor.lnum; |
| 4178 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4179 | /* |
| 4180 | * When visual area is more than one line: extend it. |
| 4181 | */ |
| 4182 | if (VIsual_active && start_lnum != VIsual.lnum) |
| 4183 | { |
| 4184 | extend: |
| 4185 | if (start_lnum < VIsual.lnum) |
| 4186 | dir = BACKWARD; |
| 4187 | else |
| 4188 | dir = FORWARD; |
| 4189 | for (i = count; --i >= 0; ) |
| 4190 | { |
| 4191 | if (start_lnum == |
| 4192 | (dir == BACKWARD ? 1 : curbuf->b_ml.ml_line_count)) |
| 4193 | { |
| 4194 | retval = FAIL; |
| 4195 | break; |
| 4196 | } |
| 4197 | |
| 4198 | prev_start_is_white = -1; |
| 4199 | for (t = 0; t < 2; ++t) |
| 4200 | { |
| 4201 | start_lnum += dir; |
| 4202 | start_is_white = linewhite(start_lnum); |
| 4203 | if (prev_start_is_white == start_is_white) |
| 4204 | { |
| 4205 | start_lnum -= dir; |
| 4206 | break; |
| 4207 | } |
| 4208 | for (;;) |
| 4209 | { |
| 4210 | if (start_lnum == (dir == BACKWARD |
| 4211 | ? 1 : curbuf->b_ml.ml_line_count)) |
| 4212 | break; |
| 4213 | if (start_is_white != linewhite(start_lnum + dir) |
| 4214 | || (!start_is_white |
| 4215 | && startPS(start_lnum + (dir > 0 |
| 4216 | ? 1 : 0), 0, 0))) |
| 4217 | break; |
| 4218 | start_lnum += dir; |
| 4219 | } |
| 4220 | if (!include) |
| 4221 | break; |
| 4222 | if (start_lnum == (dir == BACKWARD |
| 4223 | ? 1 : curbuf->b_ml.ml_line_count)) |
| 4224 | break; |
| 4225 | prev_start_is_white = start_is_white; |
| 4226 | } |
| 4227 | } |
| 4228 | curwin->w_cursor.lnum = start_lnum; |
| 4229 | curwin->w_cursor.col = 0; |
| 4230 | return retval; |
| 4231 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4232 | |
| 4233 | /* |
| 4234 | * First move back to the start_lnum of the paragraph or white lines |
| 4235 | */ |
| 4236 | white_in_front = linewhite(start_lnum); |
| 4237 | while (start_lnum > 1) |
| 4238 | { |
| 4239 | if (white_in_front) /* stop at first white line */ |
| 4240 | { |
| 4241 | if (!linewhite(start_lnum - 1)) |
| 4242 | break; |
| 4243 | } |
| 4244 | else /* stop at first non-white line of start of paragraph */ |
| 4245 | { |
| 4246 | if (linewhite(start_lnum - 1) || startPS(start_lnum, 0, 0)) |
| 4247 | break; |
| 4248 | } |
| 4249 | --start_lnum; |
| 4250 | } |
| 4251 | |
| 4252 | /* |
| 4253 | * Move past the end of any white lines. |
| 4254 | */ |
| 4255 | end_lnum = start_lnum; |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 4256 | while (end_lnum <= curbuf->b_ml.ml_line_count && linewhite(end_lnum)) |
| 4257 | ++end_lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4258 | |
| 4259 | --end_lnum; |
| 4260 | i = count; |
| 4261 | if (!include && white_in_front) |
| 4262 | --i; |
| 4263 | while (i--) |
| 4264 | { |
| 4265 | if (end_lnum == curbuf->b_ml.ml_line_count) |
| 4266 | return FAIL; |
| 4267 | |
| 4268 | if (!include) |
| 4269 | do_white = linewhite(end_lnum + 1); |
| 4270 | |
| 4271 | if (include || !do_white) |
| 4272 | { |
| 4273 | ++end_lnum; |
| 4274 | /* |
| 4275 | * skip to end of paragraph |
| 4276 | */ |
| 4277 | while (end_lnum < curbuf->b_ml.ml_line_count |
| 4278 | && !linewhite(end_lnum + 1) |
| 4279 | && !startPS(end_lnum + 1, 0, 0)) |
| 4280 | ++end_lnum; |
| 4281 | } |
| 4282 | |
| 4283 | if (i == 0 && white_in_front && include) |
| 4284 | break; |
| 4285 | |
| 4286 | /* |
| 4287 | * skip to end of white lines after paragraph |
| 4288 | */ |
| 4289 | if (include || do_white) |
| 4290 | while (end_lnum < curbuf->b_ml.ml_line_count |
| 4291 | && linewhite(end_lnum + 1)) |
| 4292 | ++end_lnum; |
| 4293 | } |
| 4294 | |
| 4295 | /* |
| 4296 | * If there are no empty lines at the end, try to find some empty lines at |
| 4297 | * the start (unless that has been done already). |
| 4298 | */ |
| 4299 | if (!white_in_front && !linewhite(end_lnum) && include) |
| 4300 | while (start_lnum > 1 && linewhite(start_lnum - 1)) |
| 4301 | --start_lnum; |
| 4302 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4303 | if (VIsual_active) |
| 4304 | { |
| 4305 | /* Problem: when doing "Vipipip" nothing happens in a single white |
| 4306 | * line, we get stuck there. Trap this here. */ |
| 4307 | if (VIsual_mode == 'V' && start_lnum == curwin->w_cursor.lnum) |
| 4308 | goto extend; |
Bram Moolenaar | 84b2a38 | 2017-02-17 11:40:00 +0100 | [diff] [blame] | 4309 | if (VIsual.lnum != start_lnum) |
| 4310 | { |
| 4311 | VIsual.lnum = start_lnum; |
| 4312 | VIsual.col = 0; |
| 4313 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4314 | VIsual_mode = 'V'; |
| 4315 | redraw_curbuf_later(INVERTED); /* update the inversion */ |
| 4316 | showmode(); |
| 4317 | } |
| 4318 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4319 | { |
| 4320 | oap->start.lnum = start_lnum; |
| 4321 | oap->start.col = 0; |
| 4322 | oap->motion_type = MLINE; |
| 4323 | } |
| 4324 | curwin->w_cursor.lnum = end_lnum; |
| 4325 | curwin->w_cursor.col = 0; |
| 4326 | |
| 4327 | return OK; |
| 4328 | } |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4329 | |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4330 | /* |
| 4331 | * Search quote char from string line[col]. |
| 4332 | * Quote character escaped by one of the characters in "escape" is not counted |
| 4333 | * as a quote. |
| 4334 | * Returns column number of "quotechar" or -1 when not found. |
| 4335 | */ |
| 4336 | static int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 4337 | find_next_quote( |
| 4338 | char_u *line, |
| 4339 | int col, |
| 4340 | int quotechar, |
| 4341 | char_u *escape) /* escape characters, can be NULL */ |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4342 | { |
| 4343 | int c; |
| 4344 | |
Bram Moolenaar | d8e9bb2 | 2005-07-09 21:14:46 +0000 | [diff] [blame] | 4345 | for (;;) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4346 | { |
| 4347 | c = line[col]; |
| 4348 | if (c == NUL) |
| 4349 | return -1; |
| 4350 | else if (escape != NULL && vim_strchr(escape, c)) |
| 4351 | ++col; |
| 4352 | else if (c == quotechar) |
| 4353 | break; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4354 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 4355 | col += (*mb_ptr2len)(line + col); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4356 | else |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4357 | ++col; |
| 4358 | } |
| 4359 | return col; |
| 4360 | } |
| 4361 | |
| 4362 | /* |
| 4363 | * Search backwards in "line" from column "col_start" to find "quotechar". |
| 4364 | * Quote character escaped by one of the characters in "escape" is not counted |
| 4365 | * as a quote. |
| 4366 | * Return the found column or zero. |
| 4367 | */ |
| 4368 | static int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 4369 | find_prev_quote( |
| 4370 | char_u *line, |
| 4371 | int col_start, |
| 4372 | int quotechar, |
| 4373 | char_u *escape) /* escape characters, can be NULL */ |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4374 | { |
| 4375 | int n; |
| 4376 | |
| 4377 | while (col_start > 0) |
| 4378 | { |
| 4379 | --col_start; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4380 | col_start -= (*mb_head_off)(line, line + col_start); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4381 | n = 0; |
| 4382 | if (escape != NULL) |
| 4383 | while (col_start - n > 0 && vim_strchr(escape, |
| 4384 | line[col_start - n - 1]) != NULL) |
| 4385 | ++n; |
| 4386 | if (n & 1) |
| 4387 | col_start -= n; /* uneven number of escape chars, skip it */ |
| 4388 | else if (line[col_start] == quotechar) |
| 4389 | break; |
| 4390 | } |
| 4391 | return col_start; |
| 4392 | } |
| 4393 | |
| 4394 | /* |
| 4395 | * Find quote under the cursor, cursor at end. |
| 4396 | * Returns TRUE if found, else FALSE. |
| 4397 | */ |
| 4398 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 4399 | current_quote( |
| 4400 | oparg_T *oap, |
| 4401 | long count, |
| 4402 | int include, /* TRUE == include quote char */ |
| 4403 | int quotechar) /* Quote character */ |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4404 | { |
| 4405 | char_u *line = ml_get_curline(); |
| 4406 | int col_end; |
| 4407 | int col_start = curwin->w_cursor.col; |
| 4408 | int inclusive = FALSE; |
Bram Moolenaar | 55d3bdb | 2019-02-22 15:04:17 +0100 | [diff] [blame] | 4409 | int vis_empty = TRUE; // Visual selection <= 1 char |
| 4410 | int vis_bef_curs = FALSE; // Visual starts before cursor |
| 4411 | int inside_quotes = FALSE; // Looks like "i'" done before |
| 4412 | int selected_quote = FALSE; // Has quote inside selection |
Bram Moolenaar | ab19481 | 2005-09-14 21:40:12 +0000 | [diff] [blame] | 4413 | int i; |
Bram Moolenaar | 55d3bdb | 2019-02-22 15:04:17 +0100 | [diff] [blame] | 4414 | int restore_vis_bef = FALSE; // restore VIsual on abort |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4415 | |
Bram Moolenaar | c5e2b04 | 2017-06-05 16:37:07 +0200 | [diff] [blame] | 4416 | /* Correct cursor when 'selection' is "exclusive". */ |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4417 | if (VIsual_active) |
| 4418 | { |
Bram Moolenaar | 46522af | 2017-02-18 23:12:01 +0100 | [diff] [blame] | 4419 | /* this only works within one line */ |
| 4420 | if (VIsual.lnum != curwin->w_cursor.lnum) |
| 4421 | return FALSE; |
| 4422 | |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 4423 | vis_bef_curs = LT_POS(VIsual, curwin->w_cursor); |
Bram Moolenaar | c5e2b04 | 2017-06-05 16:37:07 +0200 | [diff] [blame] | 4424 | if (*p_sel == 'e') |
| 4425 | { |
| 4426 | if (!vis_bef_curs) |
| 4427 | { |
Bram Moolenaar | 55d3bdb | 2019-02-22 15:04:17 +0100 | [diff] [blame] | 4428 | // VIsual needs to be the start of Visual selection. |
Bram Moolenaar | c5e2b04 | 2017-06-05 16:37:07 +0200 | [diff] [blame] | 4429 | pos_T t = curwin->w_cursor; |
| 4430 | |
| 4431 | curwin->w_cursor = VIsual; |
| 4432 | VIsual = t; |
| 4433 | vis_bef_curs = TRUE; |
Bram Moolenaar | 55d3bdb | 2019-02-22 15:04:17 +0100 | [diff] [blame] | 4434 | restore_vis_bef = TRUE; |
Bram Moolenaar | c5e2b04 | 2017-06-05 16:37:07 +0200 | [diff] [blame] | 4435 | } |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4436 | dec_cursor(); |
Bram Moolenaar | c5e2b04 | 2017-06-05 16:37:07 +0200 | [diff] [blame] | 4437 | } |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 4438 | vis_empty = EQUAL_POS(VIsual, curwin->w_cursor); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4439 | } |
Bram Moolenaar | ab19481 | 2005-09-14 21:40:12 +0000 | [diff] [blame] | 4440 | |
| 4441 | if (!vis_empty) |
| 4442 | { |
| 4443 | /* Check if the existing selection exactly spans the text inside |
| 4444 | * quotes. */ |
| 4445 | if (vis_bef_curs) |
| 4446 | { |
| 4447 | inside_quotes = VIsual.col > 0 |
| 4448 | && line[VIsual.col - 1] == quotechar |
| 4449 | && line[curwin->w_cursor.col] != NUL |
| 4450 | && line[curwin->w_cursor.col + 1] == quotechar; |
| 4451 | i = VIsual.col; |
| 4452 | col_end = curwin->w_cursor.col; |
| 4453 | } |
| 4454 | else |
| 4455 | { |
| 4456 | inside_quotes = curwin->w_cursor.col > 0 |
| 4457 | && line[curwin->w_cursor.col - 1] == quotechar |
| 4458 | && line[VIsual.col] != NUL |
| 4459 | && line[VIsual.col + 1] == quotechar; |
| 4460 | i = curwin->w_cursor.col; |
| 4461 | col_end = VIsual.col; |
| 4462 | } |
| 4463 | |
| 4464 | /* Find out if we have a quote in the selection. */ |
| 4465 | while (i <= col_end) |
| 4466 | if (line[i++] == quotechar) |
| 4467 | { |
| 4468 | selected_quote = TRUE; |
| 4469 | break; |
| 4470 | } |
| 4471 | } |
| 4472 | |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4473 | if (!vis_empty && line[col_start] == quotechar) |
| 4474 | { |
| 4475 | /* Already selecting something and on a quote character. Find the |
| 4476 | * next quoted string. */ |
| 4477 | if (vis_bef_curs) |
| 4478 | { |
| 4479 | /* Assume we are on a closing quote: move to after the next |
| 4480 | * opening quote. */ |
| 4481 | col_start = find_next_quote(line, col_start + 1, quotechar, NULL); |
| 4482 | if (col_start < 0) |
Bram Moolenaar | 55d3bdb | 2019-02-22 15:04:17 +0100 | [diff] [blame] | 4483 | goto abort_search; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4484 | col_end = find_next_quote(line, col_start + 1, quotechar, |
| 4485 | curbuf->b_p_qe); |
| 4486 | if (col_end < 0) |
| 4487 | { |
| 4488 | /* We were on a starting quote perhaps? */ |
| 4489 | col_end = col_start; |
| 4490 | col_start = curwin->w_cursor.col; |
| 4491 | } |
| 4492 | } |
| 4493 | else |
| 4494 | { |
| 4495 | col_end = find_prev_quote(line, col_start, quotechar, NULL); |
| 4496 | if (line[col_end] != quotechar) |
Bram Moolenaar | 55d3bdb | 2019-02-22 15:04:17 +0100 | [diff] [blame] | 4497 | goto abort_search; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4498 | col_start = find_prev_quote(line, col_end, quotechar, |
| 4499 | curbuf->b_p_qe); |
| 4500 | if (line[col_start] != quotechar) |
| 4501 | { |
| 4502 | /* We were on an ending quote perhaps? */ |
| 4503 | col_start = col_end; |
| 4504 | col_end = curwin->w_cursor.col; |
| 4505 | } |
| 4506 | } |
| 4507 | } |
| 4508 | else |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4509 | |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 4510 | if (line[col_start] == quotechar || !vis_empty) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4511 | { |
| 4512 | int first_col = col_start; |
| 4513 | |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4514 | if (!vis_empty) |
| 4515 | { |
| 4516 | if (vis_bef_curs) |
| 4517 | first_col = find_next_quote(line, col_start, quotechar, NULL); |
| 4518 | else |
| 4519 | first_col = find_prev_quote(line, col_start, quotechar, NULL); |
| 4520 | } |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 4521 | |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4522 | /* The cursor is on a quote, we don't know if it's the opening or |
| 4523 | * closing quote. Search from the start of the line to find out. |
| 4524 | * Also do this when there is a Visual area, a' may leave the cursor |
| 4525 | * in between two strings. */ |
| 4526 | col_start = 0; |
Bram Moolenaar | d8e9bb2 | 2005-07-09 21:14:46 +0000 | [diff] [blame] | 4527 | for (;;) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4528 | { |
| 4529 | /* Find open quote character. */ |
| 4530 | col_start = find_next_quote(line, col_start, quotechar, NULL); |
| 4531 | if (col_start < 0 || col_start > first_col) |
Bram Moolenaar | 55d3bdb | 2019-02-22 15:04:17 +0100 | [diff] [blame] | 4532 | goto abort_search; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4533 | /* Find close quote character. */ |
| 4534 | col_end = find_next_quote(line, col_start + 1, quotechar, |
| 4535 | curbuf->b_p_qe); |
| 4536 | if (col_end < 0) |
Bram Moolenaar | 55d3bdb | 2019-02-22 15:04:17 +0100 | [diff] [blame] | 4537 | goto abort_search; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4538 | /* If is cursor between start and end quote character, it is |
| 4539 | * target text object. */ |
| 4540 | if (col_start <= first_col && first_col <= col_end) |
| 4541 | break; |
| 4542 | col_start = col_end + 1; |
| 4543 | } |
| 4544 | } |
| 4545 | else |
| 4546 | { |
| 4547 | /* Search backward for a starting quote. */ |
| 4548 | col_start = find_prev_quote(line, col_start, quotechar, curbuf->b_p_qe); |
| 4549 | if (line[col_start] != quotechar) |
| 4550 | { |
| 4551 | /* No quote before the cursor, look after the cursor. */ |
| 4552 | col_start = find_next_quote(line, col_start, quotechar, NULL); |
| 4553 | if (col_start < 0) |
Bram Moolenaar | 55d3bdb | 2019-02-22 15:04:17 +0100 | [diff] [blame] | 4554 | goto abort_search; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4555 | } |
| 4556 | |
| 4557 | /* Find close quote character. */ |
| 4558 | col_end = find_next_quote(line, col_start + 1, quotechar, |
| 4559 | curbuf->b_p_qe); |
| 4560 | if (col_end < 0) |
Bram Moolenaar | 55d3bdb | 2019-02-22 15:04:17 +0100 | [diff] [blame] | 4561 | goto abort_search; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4562 | } |
| 4563 | |
| 4564 | /* When "include" is TRUE, include spaces after closing quote or before |
| 4565 | * the starting quote. */ |
| 4566 | if (include) |
| 4567 | { |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 4568 | if (VIM_ISWHITE(line[col_end + 1])) |
| 4569 | while (VIM_ISWHITE(line[col_end + 1])) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4570 | ++col_end; |
| 4571 | else |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 4572 | while (col_start > 0 && VIM_ISWHITE(line[col_start - 1])) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4573 | --col_start; |
| 4574 | } |
| 4575 | |
Bram Moolenaar | ab19481 | 2005-09-14 21:40:12 +0000 | [diff] [blame] | 4576 | /* Set start position. After vi" another i" must include the ". |
| 4577 | * For v2i" include the quotes. */ |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 4578 | if (!include && count < 2 && (vis_empty || !inside_quotes)) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4579 | ++col_start; |
| 4580 | curwin->w_cursor.col = col_start; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4581 | if (VIsual_active) |
| 4582 | { |
Bram Moolenaar | ab19481 | 2005-09-14 21:40:12 +0000 | [diff] [blame] | 4583 | /* Set the start of the Visual area when the Visual area was empty, we |
| 4584 | * were just inside quotes or the Visual area didn't start at a quote |
| 4585 | * and didn't include a quote. |
| 4586 | */ |
| 4587 | if (vis_empty |
| 4588 | || (vis_bef_curs |
| 4589 | && !selected_quote |
| 4590 | && (inside_quotes |
| 4591 | || (line[VIsual.col] != quotechar |
| 4592 | && (VIsual.col == 0 |
| 4593 | || line[VIsual.col - 1] != quotechar))))) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4594 | { |
| 4595 | VIsual = curwin->w_cursor; |
| 4596 | redraw_curbuf_later(INVERTED); |
| 4597 | } |
| 4598 | } |
| 4599 | else |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4600 | { |
| 4601 | oap->start = curwin->w_cursor; |
| 4602 | oap->motion_type = MCHAR; |
| 4603 | } |
| 4604 | |
| 4605 | /* Set end position. */ |
| 4606 | curwin->w_cursor.col = col_end; |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 4607 | if ((include || count > 1 /* After vi" another i" must include the ". */ |
Bram Moolenaar | ab19481 | 2005-09-14 21:40:12 +0000 | [diff] [blame] | 4608 | || (!vis_empty && inside_quotes) |
Bram Moolenaar | ab19481 | 2005-09-14 21:40:12 +0000 | [diff] [blame] | 4609 | ) && inc_cursor() == 2) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4610 | inclusive = TRUE; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4611 | if (VIsual_active) |
| 4612 | { |
| 4613 | if (vis_empty || vis_bef_curs) |
| 4614 | { |
| 4615 | /* decrement cursor when 'selection' is not exclusive */ |
| 4616 | if (*p_sel != 'e') |
| 4617 | dec_cursor(); |
| 4618 | } |
| 4619 | else |
| 4620 | { |
Bram Moolenaar | ab19481 | 2005-09-14 21:40:12 +0000 | [diff] [blame] | 4621 | /* Cursor is at start of Visual area. Set the end of the Visual |
| 4622 | * area when it was just inside quotes or it didn't end at a |
| 4623 | * quote. */ |
| 4624 | if (inside_quotes |
| 4625 | || (!selected_quote |
| 4626 | && line[VIsual.col] != quotechar |
| 4627 | && (line[VIsual.col] == NUL |
| 4628 | || line[VIsual.col + 1] != quotechar))) |
| 4629 | { |
| 4630 | dec_cursor(); |
| 4631 | VIsual = curwin->w_cursor; |
| 4632 | } |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4633 | curwin->w_cursor.col = col_start; |
| 4634 | } |
| 4635 | if (VIsual_mode == 'V') |
| 4636 | { |
| 4637 | VIsual_mode = 'v'; |
| 4638 | redraw_cmdline = TRUE; /* show mode later */ |
| 4639 | } |
| 4640 | } |
| 4641 | else |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4642 | { |
| 4643 | /* Set inclusive and other oap's flags. */ |
| 4644 | oap->inclusive = inclusive; |
| 4645 | } |
| 4646 | |
| 4647 | return OK; |
Bram Moolenaar | 55d3bdb | 2019-02-22 15:04:17 +0100 | [diff] [blame] | 4648 | |
| 4649 | abort_search: |
| 4650 | if (VIsual_active && *p_sel == 'e') |
| 4651 | { |
| 4652 | inc_cursor(); |
| 4653 | if (restore_vis_bef) |
| 4654 | { |
| 4655 | pos_T t = curwin->w_cursor; |
| 4656 | |
| 4657 | curwin->w_cursor = VIsual; |
| 4658 | VIsual = t; |
| 4659 | } |
| 4660 | } |
| 4661 | return FALSE; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4662 | } |
| 4663 | |
| 4664 | #endif /* FEAT_TEXTOBJ */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4665 | |
Bram Moolenaar | 22ab547 | 2017-09-26 12:28:45 +0200 | [diff] [blame] | 4666 | static int is_one_char(char_u *pattern, int move, pos_T *cur, int direction); |
Bram Moolenaar | dde0efe | 2012-08-23 15:53:05 +0200 | [diff] [blame] | 4667 | |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 4668 | /* |
| 4669 | * Find next search match under cursor, cursor at end. |
| 4670 | * Used while an operator is pending, and in Visual mode. |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 4671 | */ |
| 4672 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 4673 | current_search( |
| 4674 | long count, |
Bram Moolenaar | 5d24a22 | 2018-12-23 19:10:09 +0100 | [diff] [blame] | 4675 | int forward) // TRUE for forward, FALSE for backward |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 4676 | { |
Bram Moolenaar | 5d24a22 | 2018-12-23 19:10:09 +0100 | [diff] [blame] | 4677 | pos_T start_pos; // start position of the pattern match |
| 4678 | pos_T end_pos; // end position of the pattern match |
| 4679 | pos_T orig_pos; // position of the cursor at beginning |
| 4680 | pos_T pos; // position after the pattern |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 4681 | int i; |
| 4682 | int dir; |
Bram Moolenaar | 5d24a22 | 2018-12-23 19:10:09 +0100 | [diff] [blame] | 4683 | int result; // result of various function calls |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 4684 | char_u old_p_ws = p_ws; |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 4685 | int flags = 0; |
Bram Moolenaar | de9149e | 2013-07-17 19:22:13 +0200 | [diff] [blame] | 4686 | pos_T save_VIsual = VIsual; |
Bram Moolenaar | e78495d | 2013-06-30 14:46:53 +0200 | [diff] [blame] | 4687 | int one_char; |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 4688 | |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 4689 | /* wrapping should not occur */ |
| 4690 | p_ws = FALSE; |
| 4691 | |
| 4692 | /* Correct cursor when 'selection' is exclusive */ |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 4693 | if (VIsual_active && *p_sel == 'e' && LT_POS(VIsual, curwin->w_cursor)) |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 4694 | dec_cursor(); |
| 4695 | |
| 4696 | if (VIsual_active) |
| 4697 | { |
| 4698 | orig_pos = curwin->w_cursor; |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 4699 | |
| 4700 | pos = curwin->w_cursor; |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 4701 | |
| 4702 | /* make sure, searching further will extend the match */ |
| 4703 | if (VIsual_active) |
| 4704 | { |
| 4705 | if (forward) |
| 4706 | incl(&pos); |
| 4707 | else |
| 4708 | decl(&pos); |
| 4709 | } |
| 4710 | } |
| 4711 | else |
Bram Moolenaar | 268a06c | 2016-04-21 09:07:01 +0200 | [diff] [blame] | 4712 | orig_pos = pos = curwin->w_cursor; |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 4713 | |
Bram Moolenaar | 22ab547 | 2017-09-26 12:28:45 +0200 | [diff] [blame] | 4714 | /* Is the pattern is zero-width?, this time, don't care about the direction |
| 4715 | */ |
| 4716 | one_char = is_one_char(spats[last_idx].pat, TRUE, &curwin->w_cursor, |
| 4717 | FORWARD); |
Bram Moolenaar | e78495d | 2013-06-30 14:46:53 +0200 | [diff] [blame] | 4718 | if (one_char == -1) |
Bram Moolenaar | ba2d44f | 2013-11-28 19:27:30 +0100 | [diff] [blame] | 4719 | { |
| 4720 | p_ws = old_p_ws; |
| 4721 | return FAIL; /* pattern not found */ |
| 4722 | } |
Bram Moolenaar | ba6ba36 | 2012-08-08 15:27:57 +0200 | [diff] [blame] | 4723 | |
Bram Moolenaar | ba6ba36 | 2012-08-08 15:27:57 +0200 | [diff] [blame] | 4724 | /* |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 4725 | * The trick is to first search backwards and then search forward again, |
| 4726 | * so that a match at the current cursor position will be correctly |
| 4727 | * captured. |
| 4728 | */ |
| 4729 | for (i = 0; i < 2; i++) |
| 4730 | { |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 4731 | if (forward) |
| 4732 | dir = i; |
| 4733 | else |
| 4734 | dir = !i; |
Bram Moolenaar | ba6ba36 | 2012-08-08 15:27:57 +0200 | [diff] [blame] | 4735 | |
| 4736 | flags = 0; |
Bram Moolenaar | e78495d | 2013-06-30 14:46:53 +0200 | [diff] [blame] | 4737 | if (!dir && !one_char) |
Bram Moolenaar | ba6ba36 | 2012-08-08 15:27:57 +0200 | [diff] [blame] | 4738 | flags = SEARCH_END; |
Bram Moolenaar | 5d24a22 | 2018-12-23 19:10:09 +0100 | [diff] [blame] | 4739 | end_pos = pos; |
Bram Moolenaar | ba6ba36 | 2012-08-08 15:27:57 +0200 | [diff] [blame] | 4740 | |
Bram Moolenaar | 5d24a22 | 2018-12-23 19:10:09 +0100 | [diff] [blame] | 4741 | result = searchit(curwin, curbuf, &pos, &end_pos, |
| 4742 | (dir ? FORWARD : BACKWARD), |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 4743 | spats[last_idx].pat, (long) (i ? count : 1), |
Bram Moolenaar | fbd0b0a | 2017-06-17 18:44:21 +0200 | [diff] [blame] | 4744 | SEARCH_KEEP | flags, RE_SEARCH, 0, NULL, NULL); |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 4745 | |
| 4746 | /* First search may fail, but then start searching from the |
| 4747 | * beginning of the file (cursor might be on the search match) |
| 4748 | * except when Visual mode is active, so that extending the visual |
| 4749 | * selection works. */ |
Bram Moolenaar | 5d24a22 | 2018-12-23 19:10:09 +0100 | [diff] [blame] | 4750 | if (i == 1 && !result) /* not found, abort */ |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 4751 | { |
| 4752 | curwin->w_cursor = orig_pos; |
| 4753 | if (VIsual_active) |
| 4754 | VIsual = save_VIsual; |
| 4755 | p_ws = old_p_ws; |
| 4756 | return FAIL; |
| 4757 | } |
Bram Moolenaar | 5d24a22 | 2018-12-23 19:10:09 +0100 | [diff] [blame] | 4758 | else if (i == 0 && !result) |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 4759 | { |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 4760 | if (forward) |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 4761 | { |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 4762 | /* try again from start of buffer */ |
| 4763 | CLEAR_POS(&pos); |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 4764 | } |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 4765 | else |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 4766 | { |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 4767 | /* try again from end of buffer */ |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 4768 | /* searching backwards, so set pos to last line and col */ |
| 4769 | pos.lnum = curwin->w_buffer->b_ml.ml_line_count; |
Bram Moolenaar | 09168a7 | 2012-08-02 21:24:42 +0200 | [diff] [blame] | 4770 | pos.col = (colnr_T)STRLEN( |
| 4771 | ml_get(curwin->w_buffer->b_ml.ml_line_count)); |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 4772 | } |
| 4773 | } |
Bram Moolenaar | fcea03d | 2013-11-07 04:46:48 +0100 | [diff] [blame] | 4774 | p_ws = old_p_ws; |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 4775 | } |
| 4776 | |
| 4777 | start_pos = pos; |
Bram Moolenaar | bdb6579 | 2018-05-22 17:50:42 +0200 | [diff] [blame] | 4778 | p_ws = old_p_ws; |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 4779 | |
| 4780 | if (!VIsual_active) |
| 4781 | VIsual = start_pos; |
| 4782 | |
Bram Moolenaar | 5d24a22 | 2018-12-23 19:10:09 +0100 | [diff] [blame] | 4783 | // put cursor on last character of match |
| 4784 | curwin->w_cursor = end_pos; |
| 4785 | if (LT_POS(VIsual, end_pos)) |
| 4786 | dec_cursor(); |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 4787 | VIsual_active = TRUE; |
| 4788 | VIsual_mode = 'v'; |
| 4789 | |
Bram Moolenaar | b763361 | 2019-02-10 21:48:25 +0100 | [diff] [blame] | 4790 | redraw_curbuf_later(INVERTED); /* update the inversion */ |
| 4791 | if (*p_sel == 'e') |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 4792 | { |
Bram Moolenaar | b763361 | 2019-02-10 21:48:25 +0100 | [diff] [blame] | 4793 | /* Correction for exclusive selection depends on the direction. */ |
| 4794 | if (forward && LTOREQ_POS(VIsual, curwin->w_cursor)) |
| 4795 | inc_cursor(); |
| 4796 | else if (!forward && LTOREQ_POS(curwin->w_cursor, VIsual)) |
| 4797 | inc(&VIsual); |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 4798 | } |
| 4799 | |
| 4800 | #ifdef FEAT_FOLDING |
| 4801 | if (fdo_flags & FDO_SEARCH && KeyTyped) |
| 4802 | foldOpenCursor(); |
| 4803 | #endif |
| 4804 | |
| 4805 | may_start_select('c'); |
| 4806 | #ifdef FEAT_MOUSE |
| 4807 | setmouse(); |
| 4808 | #endif |
| 4809 | #ifdef FEAT_CLIPBOARD |
| 4810 | /* Make sure the clipboard gets updated. Needed because start and |
| 4811 | * end are still the same, and the selection needs to be owned */ |
| 4812 | clip_star.vmode = NUL; |
| 4813 | #endif |
| 4814 | redraw_curbuf_later(INVERTED); |
| 4815 | showmode(); |
| 4816 | |
| 4817 | return OK; |
| 4818 | } |
Bram Moolenaar | dde0efe | 2012-08-23 15:53:05 +0200 | [diff] [blame] | 4819 | |
| 4820 | /* |
Bram Moolenaar | 6835dc6 | 2016-07-24 17:33:05 +0200 | [diff] [blame] | 4821 | * Check if the pattern is one character long or zero-width. |
Bram Moolenaar | add8dce | 2017-06-05 19:56:04 +0200 | [diff] [blame] | 4822 | * If move is TRUE, check from the beginning of the buffer, else from position |
| 4823 | * "cur". |
Bram Moolenaar | 22ab547 | 2017-09-26 12:28:45 +0200 | [diff] [blame] | 4824 | * "direction" is FORWARD or BACKWARD. |
Bram Moolenaar | dde0efe | 2012-08-23 15:53:05 +0200 | [diff] [blame] | 4825 | * Returns TRUE, FALSE or -1 for failure. |
| 4826 | */ |
| 4827 | static int |
Bram Moolenaar | 22ab547 | 2017-09-26 12:28:45 +0200 | [diff] [blame] | 4828 | is_one_char(char_u *pattern, int move, pos_T *cur, int direction) |
Bram Moolenaar | dde0efe | 2012-08-23 15:53:05 +0200 | [diff] [blame] | 4829 | { |
| 4830 | regmmatch_T regmatch; |
| 4831 | int nmatched = 0; |
| 4832 | int result = -1; |
Bram Moolenaar | 57c0ea8 | 2012-09-05 12:16:45 +0200 | [diff] [blame] | 4833 | pos_T pos; |
| 4834 | int save_called_emsg = called_emsg; |
Bram Moolenaar | b12db9f | 2014-12-13 22:00:22 +0100 | [diff] [blame] | 4835 | int flag = 0; |
Bram Moolenaar | dde0efe | 2012-08-23 15:53:05 +0200 | [diff] [blame] | 4836 | |
Bram Moolenaar | 6835dc6 | 2016-07-24 17:33:05 +0200 | [diff] [blame] | 4837 | if (pattern == NULL) |
| 4838 | pattern = spats[last_idx].pat; |
| 4839 | |
Bram Moolenaar | dde0efe | 2012-08-23 15:53:05 +0200 | [diff] [blame] | 4840 | if (search_regcomp(pattern, RE_SEARCH, RE_SEARCH, |
| 4841 | SEARCH_KEEP, ®match) == FAIL) |
| 4842 | return -1; |
| 4843 | |
Bram Moolenaar | 6835dc6 | 2016-07-24 17:33:05 +0200 | [diff] [blame] | 4844 | /* init startcol correctly */ |
| 4845 | regmatch.startpos[0].col = -1; |
Bram Moolenaar | dde0efe | 2012-08-23 15:53:05 +0200 | [diff] [blame] | 4846 | /* move to match */ |
Bram Moolenaar | b12db9f | 2014-12-13 22:00:22 +0100 | [diff] [blame] | 4847 | if (move) |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 4848 | { |
| 4849 | CLEAR_POS(&pos); |
| 4850 | } |
Bram Moolenaar | b12db9f | 2014-12-13 22:00:22 +0100 | [diff] [blame] | 4851 | else |
| 4852 | { |
Bram Moolenaar | add8dce | 2017-06-05 19:56:04 +0200 | [diff] [blame] | 4853 | pos = *cur; |
Bram Moolenaar | b12db9f | 2014-12-13 22:00:22 +0100 | [diff] [blame] | 4854 | /* accept a match at the cursor position */ |
| 4855 | flag = SEARCH_START; |
| 4856 | } |
| 4857 | |
Bram Moolenaar | 5d24a22 | 2018-12-23 19:10:09 +0100 | [diff] [blame] | 4858 | if (searchit(curwin, curbuf, &pos, NULL, direction, pattern, 1, |
Bram Moolenaar | fbd0b0a | 2017-06-17 18:44:21 +0200 | [diff] [blame] | 4859 | SEARCH_KEEP + flag, RE_SEARCH, 0, NULL, NULL) != FAIL) |
Bram Moolenaar | dde0efe | 2012-08-23 15:53:05 +0200 | [diff] [blame] | 4860 | { |
| 4861 | /* Zero-width pattern should match somewhere, then we can check if |
| 4862 | * start and end are in the same position. */ |
Bram Moolenaar | 57c0ea8 | 2012-09-05 12:16:45 +0200 | [diff] [blame] | 4863 | called_emsg = FALSE; |
Bram Moolenaar | 6835dc6 | 2016-07-24 17:33:05 +0200 | [diff] [blame] | 4864 | do |
| 4865 | { |
| 4866 | regmatch.startpos[0].col++; |
| 4867 | nmatched = vim_regexec_multi(®match, curwin, curbuf, |
Bram Moolenaar | fbd0b0a | 2017-06-17 18:44:21 +0200 | [diff] [blame] | 4868 | pos.lnum, regmatch.startpos[0].col, NULL, NULL); |
Bram Moolenaar | 6835dc6 | 2016-07-24 17:33:05 +0200 | [diff] [blame] | 4869 | if (!nmatched) |
| 4870 | break; |
Bram Moolenaar | 22ab547 | 2017-09-26 12:28:45 +0200 | [diff] [blame] | 4871 | } while (direction == FORWARD ? regmatch.startpos[0].col < pos.col |
| 4872 | : regmatch.startpos[0].col > pos.col); |
Bram Moolenaar | dde0efe | 2012-08-23 15:53:05 +0200 | [diff] [blame] | 4873 | |
| 4874 | if (!called_emsg) |
Bram Moolenaar | 6835dc6 | 2016-07-24 17:33:05 +0200 | [diff] [blame] | 4875 | { |
Bram Moolenaar | dde0efe | 2012-08-23 15:53:05 +0200 | [diff] [blame] | 4876 | result = (nmatched != 0 |
Bram Moolenaar | 57c0ea8 | 2012-09-05 12:16:45 +0200 | [diff] [blame] | 4877 | && regmatch.startpos[0].lnum == regmatch.endpos[0].lnum |
| 4878 | && regmatch.startpos[0].col == regmatch.endpos[0].col); |
Bram Moolenaar | 6835dc6 | 2016-07-24 17:33:05 +0200 | [diff] [blame] | 4879 | /* one char width */ |
| 4880 | if (!result && inc(&pos) >= 0 && pos.col == regmatch.endpos[0].col) |
| 4881 | result = TRUE; |
| 4882 | } |
Bram Moolenaar | dde0efe | 2012-08-23 15:53:05 +0200 | [diff] [blame] | 4883 | } |
| 4884 | |
Bram Moolenaar | 57c0ea8 | 2012-09-05 12:16:45 +0200 | [diff] [blame] | 4885 | called_emsg |= save_called_emsg; |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 4886 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | dde0efe | 2012-08-23 15:53:05 +0200 | [diff] [blame] | 4887 | return result; |
| 4888 | } |
Bram Moolenaar | 8a0f3c7 | 2012-07-29 12:55:32 +0200 | [diff] [blame] | 4889 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4890 | #if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(FEAT_TEXTOBJ) \ |
| 4891 | || defined(PROTO) |
| 4892 | /* |
| 4893 | * return TRUE if line 'lnum' is empty or has white chars only. |
| 4894 | */ |
| 4895 | int |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 4896 | linewhite(linenr_T lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4897 | { |
| 4898 | char_u *p; |
| 4899 | |
| 4900 | p = skipwhite(ml_get(lnum)); |
| 4901 | return (*p == NUL); |
| 4902 | } |
| 4903 | #endif |
| 4904 | |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 4905 | /* |
| 4906 | * Add the search count "[3/19]" to "msgbuf". |
Bram Moolenaar | 8f46e4c | 2019-05-24 22:08:15 +0200 | [diff] [blame] | 4907 | * When "recompute" is TRUE always recompute the numbers. |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 4908 | */ |
| 4909 | static void |
| 4910 | search_stat( |
| 4911 | int dirc, |
| 4912 | pos_T *pos, |
Bram Moolenaar | c7a10b3 | 2019-05-06 21:37:18 +0200 | [diff] [blame] | 4913 | int show_top_bot_msg, |
Bram Moolenaar | 8f46e4c | 2019-05-24 22:08:15 +0200 | [diff] [blame] | 4914 | char_u *msgbuf, |
| 4915 | int recompute) |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 4916 | { |
| 4917 | int save_ws = p_ws; |
| 4918 | int wraparound = FALSE; |
| 4919 | pos_T p = (*pos); |
| 4920 | static pos_T lastpos = {0, 0, 0}; |
| 4921 | static int cur = 0; |
| 4922 | static int cnt = 0; |
| 4923 | static int chgtick = 0; |
| 4924 | static char_u *lastpat = NULL; |
| 4925 | static buf_T *lbuf = NULL; |
| 4926 | #ifdef FEAT_RELTIME |
| 4927 | proftime_T start; |
| 4928 | #endif |
| 4929 | #define OUT_OF_TIME 999 |
| 4930 | |
| 4931 | wraparound = ((dirc == '?' && LT_POS(lastpos, p)) |
| 4932 | || (dirc == '/' && LT_POS(p, lastpos))); |
| 4933 | |
| 4934 | // If anything relevant changed the count has to be recomputed. |
| 4935 | // MB_STRNICMP ignores case, but we should not ignore case. |
| 4936 | // Unfortunately, there is no MB_STRNICMP function. |
| 4937 | if (!(chgtick == CHANGEDTICK(curbuf) |
| 4938 | && MB_STRNICMP(lastpat, spats[last_idx].pat, STRLEN(lastpat)) == 0 |
| 4939 | && STRLEN(lastpat) == STRLEN(spats[last_idx].pat) |
| 4940 | && EQUAL_POS(lastpos, curwin->w_cursor) |
Bram Moolenaar | 8f46e4c | 2019-05-24 22:08:15 +0200 | [diff] [blame] | 4941 | && lbuf == curbuf) || wraparound || cur < 0 || cur > 99 || recompute) |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 4942 | { |
| 4943 | cur = 0; |
| 4944 | cnt = 0; |
| 4945 | CLEAR_POS(&lastpos); |
| 4946 | lbuf = curbuf; |
| 4947 | } |
| 4948 | |
| 4949 | if (EQUAL_POS(lastpos, curwin->w_cursor) && !wraparound |
| 4950 | && (dirc == '/' ? cur < cnt : cur > 0)) |
| 4951 | cur += dirc == '/' ? 1 : -1; |
| 4952 | else |
| 4953 | { |
| 4954 | p_ws = FALSE; |
| 4955 | #ifdef FEAT_RELTIME |
| 4956 | profile_setlimit(20L, &start); |
| 4957 | #endif |
| 4958 | while (!got_int && searchit(curwin, curbuf, &lastpos, NULL, |
Bram Moolenaar | 9ce3fa8 | 2019-05-07 21:29:11 +0200 | [diff] [blame] | 4959 | FORWARD, NULL, 1, SEARCH_KEEP, RE_LAST, |
| 4960 | (linenr_T)0, NULL, NULL) != FAIL) |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 4961 | { |
| 4962 | #ifdef FEAT_RELTIME |
| 4963 | // Stop after passing the time limit. |
| 4964 | if (profile_passed_limit(&start)) |
| 4965 | { |
| 4966 | cnt = OUT_OF_TIME; |
| 4967 | cur = OUT_OF_TIME; |
| 4968 | break; |
| 4969 | } |
| 4970 | #endif |
| 4971 | cnt++; |
| 4972 | if (LTOREQ_POS(lastpos, p)) |
| 4973 | cur++; |
| 4974 | fast_breakcheck(); |
| 4975 | if (cnt > 99) |
| 4976 | break; |
| 4977 | } |
| 4978 | if (got_int) |
| 4979 | cur = -1; // abort |
| 4980 | } |
| 4981 | if (cur > 0) |
| 4982 | { |
Bram Moolenaar | b6cb26f | 2019-05-07 21:34:37 +0200 | [diff] [blame] | 4983 | char t[SEARCH_STAT_BUF_LEN] = ""; |
Bram Moolenaar | e2ad826 | 2019-05-24 13:22:22 +0200 | [diff] [blame] | 4984 | size_t len; |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 4985 | |
| 4986 | #ifdef FEAT_RIGHTLEFT |
| 4987 | if (curwin->w_p_rl && *curwin->w_p_rlc == 's') |
| 4988 | { |
| 4989 | if (cur == OUT_OF_TIME) |
Bram Moolenaar | b6cb26f | 2019-05-07 21:34:37 +0200 | [diff] [blame] | 4990 | vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[?/??]"); |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 4991 | else if (cnt > 99 && cur > 99) |
Bram Moolenaar | b6cb26f | 2019-05-07 21:34:37 +0200 | [diff] [blame] | 4992 | vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>99/>99]"); |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 4993 | else if (cnt > 99) |
Bram Moolenaar | b6cb26f | 2019-05-07 21:34:37 +0200 | [diff] [blame] | 4994 | vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>99/%d]", cur); |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 4995 | else |
Bram Moolenaar | b6cb26f | 2019-05-07 21:34:37 +0200 | [diff] [blame] | 4996 | vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/%d]", cnt, cur); |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 4997 | } |
| 4998 | else |
| 4999 | #endif |
| 5000 | { |
| 5001 | if (cur == OUT_OF_TIME) |
Bram Moolenaar | b6cb26f | 2019-05-07 21:34:37 +0200 | [diff] [blame] | 5002 | vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[?/??]"); |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 5003 | else if (cnt > 99 && cur > 99) |
Bram Moolenaar | b6cb26f | 2019-05-07 21:34:37 +0200 | [diff] [blame] | 5004 | vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[>99/>99]"); |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 5005 | else if (cnt > 99) |
Bram Moolenaar | b6cb26f | 2019-05-07 21:34:37 +0200 | [diff] [blame] | 5006 | vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/>99]", cur); |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 5007 | else |
Bram Moolenaar | b6cb26f | 2019-05-07 21:34:37 +0200 | [diff] [blame] | 5008 | vim_snprintf(t, SEARCH_STAT_BUF_LEN, "[%d/%d]", cur, cnt); |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 5009 | } |
Bram Moolenaar | c7a10b3 | 2019-05-06 21:37:18 +0200 | [diff] [blame] | 5010 | |
| 5011 | len = STRLEN(t); |
Bram Moolenaar | dc6855a | 2019-05-18 19:26:29 +0200 | [diff] [blame] | 5012 | if (show_top_bot_msg && len + 2 < SEARCH_STAT_BUF_LEN) |
Bram Moolenaar | c7a10b3 | 2019-05-06 21:37:18 +0200 | [diff] [blame] | 5013 | { |
| 5014 | STRCPY(t + len, " W"); |
| 5015 | len += 2; |
| 5016 | } |
| 5017 | |
| 5018 | mch_memmove(msgbuf + STRLEN(msgbuf) - len, t, len); |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 5019 | if (dirc == '?' && cur == 100) |
| 5020 | cur = -1; |
| 5021 | |
| 5022 | vim_free(lastpat); |
| 5023 | lastpat = vim_strsave(spats[last_idx].pat); |
| 5024 | chgtick = CHANGEDTICK(curbuf); |
| 5025 | lbuf = curbuf; |
| 5026 | lastpos = p; |
| 5027 | |
Bram Moolenaar | 984f031 | 2019-05-24 13:11:47 +0200 | [diff] [blame] | 5028 | // keep the message even after redraw, but don't put in history |
| 5029 | msg_hist_off = TRUE; |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 5030 | give_warning(msgbuf, FALSE); |
Bram Moolenaar | 984f031 | 2019-05-24 13:11:47 +0200 | [diff] [blame] | 5031 | msg_hist_off = FALSE; |
Bram Moolenaar | 9dfa313 | 2019-05-04 21:08:40 +0200 | [diff] [blame] | 5032 | } |
| 5033 | p_ws = save_ws; |
| 5034 | } |
| 5035 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5036 | #if defined(FEAT_FIND_ID) || defined(PROTO) |
| 5037 | /* |
| 5038 | * Find identifiers or defines in included files. |
Bram Moolenaar | b4b0a08 | 2011-02-25 18:38:36 +0100 | [diff] [blame] | 5039 | * If p_ic && (compl_cont_status & CONT_SOL) then ptr must be in lowercase. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5040 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5041 | void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 5042 | find_pattern_in_path( |
| 5043 | char_u *ptr, /* pointer to search pattern */ |
| 5044 | int dir UNUSED, /* direction of expansion */ |
| 5045 | int len, /* length of search pattern */ |
| 5046 | int whole, /* match whole words only */ |
| 5047 | int skip_comments, /* don't match inside comments */ |
| 5048 | int type, /* Type of search; are we looking for a type? |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5049 | a macro? */ |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 5050 | long count, |
| 5051 | int action, /* What to do when we find it */ |
| 5052 | linenr_T start_lnum, /* first line to start searching */ |
| 5053 | linenr_T end_lnum) /* last line for searching */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5054 | { |
| 5055 | SearchedFile *files; /* Stack of included files */ |
| 5056 | SearchedFile *bigger; /* When we need more space */ |
| 5057 | int max_path_depth = 50; |
| 5058 | long match_count = 1; |
| 5059 | |
| 5060 | char_u *pat; |
| 5061 | char_u *new_fname; |
| 5062 | char_u *curr_fname = curbuf->b_fname; |
| 5063 | char_u *prev_fname = NULL; |
| 5064 | linenr_T lnum; |
| 5065 | int depth; |
| 5066 | int depth_displayed; /* For type==CHECK_PATH */ |
| 5067 | int old_files; |
| 5068 | int already_searched; |
| 5069 | char_u *file_line; |
| 5070 | char_u *line; |
| 5071 | char_u *p; |
| 5072 | char_u save_char; |
| 5073 | int define_matched; |
| 5074 | regmatch_T regmatch; |
| 5075 | regmatch_T incl_regmatch; |
| 5076 | regmatch_T def_regmatch; |
| 5077 | int matched = FALSE; |
| 5078 | int did_show = FALSE; |
| 5079 | int found = FALSE; |
| 5080 | int i; |
| 5081 | char_u *already = NULL; |
| 5082 | char_u *startp = NULL; |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 5083 | char_u *inc_opt = NULL; |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 5084 | #if defined(FEAT_QUICKFIX) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5085 | win_T *curwin_save = NULL; |
| 5086 | #endif |
| 5087 | |
| 5088 | regmatch.regprog = NULL; |
| 5089 | incl_regmatch.regprog = NULL; |
| 5090 | def_regmatch.regprog = NULL; |
| 5091 | |
| 5092 | file_line = alloc(LSIZE); |
| 5093 | if (file_line == NULL) |
| 5094 | return; |
| 5095 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5096 | if (type != CHECK_PATH && type != FIND_DEFINE |
| 5097 | #ifdef FEAT_INS_EXPAND |
| 5098 | /* when CONT_SOL is set compare "ptr" with the beginning of the line |
| 5099 | * is faster than quote_meta/regcomp/regexec "ptr" -- Acevedo */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5100 | && !(compl_cont_status & CONT_SOL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5101 | #endif |
| 5102 | ) |
| 5103 | { |
| 5104 | pat = alloc(len + 5); |
| 5105 | if (pat == NULL) |
| 5106 | goto fpip_end; |
| 5107 | sprintf((char *)pat, whole ? "\\<%.*s\\>" : "%.*s", len, ptr); |
| 5108 | /* ignore case according to p_ic, p_scs and pat */ |
| 5109 | regmatch.rm_ic = ignorecase(pat); |
| 5110 | regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0); |
| 5111 | vim_free(pat); |
| 5112 | if (regmatch.regprog == NULL) |
| 5113 | goto fpip_end; |
| 5114 | } |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 5115 | inc_opt = (*curbuf->b_p_inc == NUL) ? p_inc : curbuf->b_p_inc; |
| 5116 | if (*inc_opt != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5117 | { |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 5118 | incl_regmatch.regprog = vim_regcomp(inc_opt, p_magic ? RE_MAGIC : 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5119 | if (incl_regmatch.regprog == NULL) |
| 5120 | goto fpip_end; |
| 5121 | incl_regmatch.rm_ic = FALSE; /* don't ignore case in incl. pat. */ |
| 5122 | } |
| 5123 | if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL)) |
| 5124 | { |
| 5125 | def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL |
| 5126 | ? p_def : curbuf->b_p_def, p_magic ? RE_MAGIC : 0); |
| 5127 | if (def_regmatch.regprog == NULL) |
| 5128 | goto fpip_end; |
| 5129 | def_regmatch.rm_ic = FALSE; /* don't ignore case in define pat. */ |
| 5130 | } |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 5131 | files = lalloc_clear(max_path_depth * sizeof(SearchedFile), TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5132 | if (files == NULL) |
| 5133 | goto fpip_end; |
| 5134 | old_files = max_path_depth; |
| 5135 | depth = depth_displayed = -1; |
| 5136 | |
| 5137 | lnum = start_lnum; |
| 5138 | if (end_lnum > curbuf->b_ml.ml_line_count) |
| 5139 | end_lnum = curbuf->b_ml.ml_line_count; |
| 5140 | if (lnum > end_lnum) /* do at least one line */ |
| 5141 | lnum = end_lnum; |
| 5142 | line = ml_get(lnum); |
| 5143 | |
| 5144 | for (;;) |
| 5145 | { |
| 5146 | if (incl_regmatch.regprog != NULL |
| 5147 | && vim_regexec(&incl_regmatch, line, (colnr_T)0)) |
| 5148 | { |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 5149 | char_u *p_fname = (curr_fname == curbuf->b_fname) |
| 5150 | ? curbuf->b_ffname : curr_fname; |
| 5151 | |
| 5152 | if (inc_opt != NULL && strstr((char *)inc_opt, "\\zs") != NULL) |
| 5153 | /* Use text from '\zs' to '\ze' (or end) of 'include'. */ |
| 5154 | new_fname = find_file_name_in_path(incl_regmatch.startp[0], |
Bram Moolenaar | c84e3c1 | 2013-07-03 22:28:36 +0200 | [diff] [blame] | 5155 | (int)(incl_regmatch.endp[0] - incl_regmatch.startp[0]), |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 5156 | FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname); |
| 5157 | else |
| 5158 | /* Use text after match with 'include'. */ |
| 5159 | new_fname = file_name_in_line(incl_regmatch.endp[0], 0, |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 5160 | FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5161 | already_searched = FALSE; |
| 5162 | if (new_fname != NULL) |
| 5163 | { |
| 5164 | /* Check whether we have already searched in this file */ |
| 5165 | for (i = 0;; i++) |
| 5166 | { |
| 5167 | if (i == depth + 1) |
| 5168 | i = old_files; |
| 5169 | if (i == max_path_depth) |
| 5170 | break; |
Bram Moolenaar | 99499b1 | 2019-05-23 21:35:48 +0200 | [diff] [blame] | 5171 | if (fullpathcmp(new_fname, files[i].name, TRUE, TRUE) |
| 5172 | & FPC_SAME) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5173 | { |
| 5174 | if (type != CHECK_PATH && |
| 5175 | action == ACTION_SHOW_ALL && files[i].matched) |
| 5176 | { |
| 5177 | msg_putchar('\n'); /* cursor below last one */ |
| 5178 | if (!got_int) /* don't display if 'q' |
| 5179 | typed at "--more--" |
Bram Moolenaar | fe81d45 | 2009-04-22 14:44:41 +0000 | [diff] [blame] | 5180 | message */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5181 | { |
| 5182 | msg_home_replace_hl(new_fname); |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 5183 | msg_puts(_(" (includes previously listed match)")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5184 | prev_fname = NULL; |
| 5185 | } |
| 5186 | } |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 5187 | VIM_CLEAR(new_fname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5188 | already_searched = TRUE; |
| 5189 | break; |
| 5190 | } |
| 5191 | } |
| 5192 | } |
| 5193 | |
| 5194 | if (type == CHECK_PATH && (action == ACTION_SHOW_ALL |
| 5195 | || (new_fname == NULL && !already_searched))) |
| 5196 | { |
| 5197 | if (did_show) |
| 5198 | msg_putchar('\n'); /* cursor below last one */ |
| 5199 | else |
| 5200 | { |
| 5201 | gotocmdline(TRUE); /* cursor at status line */ |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 5202 | msg_puts_title(_("--- Included files ")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5203 | if (action != ACTION_SHOW_ALL) |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 5204 | msg_puts_title(_("not found ")); |
| 5205 | msg_puts_title(_("in path ---\n")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5206 | } |
| 5207 | did_show = TRUE; |
| 5208 | while (depth_displayed < depth && !got_int) |
| 5209 | { |
| 5210 | ++depth_displayed; |
| 5211 | for (i = 0; i < depth_displayed; i++) |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 5212 | msg_puts(" "); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5213 | msg_home_replace(files[depth_displayed].name); |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 5214 | msg_puts(" -->\n"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5215 | } |
| 5216 | if (!got_int) /* don't display if 'q' typed |
| 5217 | for "--more--" message */ |
| 5218 | { |
| 5219 | for (i = 0; i <= depth_displayed; i++) |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 5220 | msg_puts(" "); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5221 | if (new_fname != NULL) |
| 5222 | { |
| 5223 | /* using "new_fname" is more reliable, e.g., when |
| 5224 | * 'includeexpr' is set. */ |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 5225 | msg_outtrans_attr(new_fname, HL_ATTR(HLF_D)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5226 | } |
| 5227 | else |
| 5228 | { |
| 5229 | /* |
| 5230 | * Isolate the file name. |
| 5231 | * Include the surrounding "" or <> if present. |
| 5232 | */ |
Bram Moolenaar | 058bdcf | 2012-07-25 13:46:30 +0200 | [diff] [blame] | 5233 | if (inc_opt != NULL |
| 5234 | && strstr((char *)inc_opt, "\\zs") != NULL) |
| 5235 | { |
| 5236 | /* pattern contains \zs, use the match */ |
| 5237 | p = incl_regmatch.startp[0]; |
| 5238 | i = (int)(incl_regmatch.endp[0] |
| 5239 | - incl_regmatch.startp[0]); |
| 5240 | } |
| 5241 | else |
| 5242 | { |
| 5243 | /* find the file name after the end of the match */ |
| 5244 | for (p = incl_regmatch.endp[0]; |
| 5245 | *p && !vim_isfilec(*p); p++) |
| 5246 | ; |
| 5247 | for (i = 0; vim_isfilec(p[i]); i++) |
| 5248 | ; |
| 5249 | } |
| 5250 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5251 | if (i == 0) |
| 5252 | { |
| 5253 | /* Nothing found, use the rest of the line. */ |
| 5254 | p = incl_regmatch.endp[0]; |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 5255 | i = (int)STRLEN(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5256 | } |
Bram Moolenaar | 058bdcf | 2012-07-25 13:46:30 +0200 | [diff] [blame] | 5257 | /* Avoid checking before the start of the line, can |
| 5258 | * happen if \zs appears in the regexp. */ |
| 5259 | else if (p > line) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5260 | { |
| 5261 | if (p[-1] == '"' || p[-1] == '<') |
| 5262 | { |
| 5263 | --p; |
| 5264 | ++i; |
| 5265 | } |
| 5266 | if (p[i] == '"' || p[i] == '>') |
| 5267 | ++i; |
| 5268 | } |
| 5269 | save_char = p[i]; |
| 5270 | p[i] = NUL; |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 5271 | msg_outtrans_attr(p, HL_ATTR(HLF_D)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5272 | p[i] = save_char; |
| 5273 | } |
| 5274 | |
| 5275 | if (new_fname == NULL && action == ACTION_SHOW_ALL) |
| 5276 | { |
| 5277 | if (already_searched) |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 5278 | msg_puts(_(" (Already listed)")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5279 | else |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 5280 | msg_puts(_(" NOT FOUND")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5281 | } |
| 5282 | } |
| 5283 | out_flush(); /* output each line directly */ |
| 5284 | } |
| 5285 | |
| 5286 | if (new_fname != NULL) |
| 5287 | { |
| 5288 | /* Push the new file onto the file stack */ |
| 5289 | if (depth + 1 == old_files) |
| 5290 | { |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 5291 | bigger = ALLOC_MULT(SearchedFile, max_path_depth * 2); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5292 | if (bigger != NULL) |
| 5293 | { |
| 5294 | for (i = 0; i <= depth; i++) |
| 5295 | bigger[i] = files[i]; |
| 5296 | for (i = depth + 1; i < old_files + max_path_depth; i++) |
| 5297 | { |
| 5298 | bigger[i].fp = NULL; |
| 5299 | bigger[i].name = NULL; |
| 5300 | bigger[i].lnum = 0; |
| 5301 | bigger[i].matched = FALSE; |
| 5302 | } |
| 5303 | for (i = old_files; i < max_path_depth; i++) |
| 5304 | bigger[i + max_path_depth] = files[i]; |
| 5305 | old_files += max_path_depth; |
| 5306 | max_path_depth *= 2; |
| 5307 | vim_free(files); |
| 5308 | files = bigger; |
| 5309 | } |
| 5310 | } |
| 5311 | if ((files[depth + 1].fp = mch_fopen((char *)new_fname, "r")) |
| 5312 | == NULL) |
| 5313 | vim_free(new_fname); |
| 5314 | else |
| 5315 | { |
| 5316 | if (++depth == old_files) |
| 5317 | { |
| 5318 | /* |
| 5319 | * lalloc() for 'bigger' must have failed above. We |
| 5320 | * will forget one of our already visited files now. |
| 5321 | */ |
| 5322 | vim_free(files[old_files].name); |
| 5323 | ++old_files; |
| 5324 | } |
| 5325 | files[depth].name = curr_fname = new_fname; |
| 5326 | files[depth].lnum = 0; |
| 5327 | files[depth].matched = FALSE; |
| 5328 | #ifdef FEAT_INS_EXPAND |
| 5329 | if (action == ACTION_EXPAND) |
| 5330 | { |
Bram Moolenaar | df40adf | 2006-10-14 12:32:39 +0000 | [diff] [blame] | 5331 | msg_hist_off = TRUE; /* reset in msg_trunc_attr() */ |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 5332 | vim_snprintf((char*)IObuff, IOSIZE, |
| 5333 | _("Scanning included file: %s"), |
| 5334 | (char *)new_fname); |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 5335 | msg_trunc_attr((char *)IObuff, TRUE, HL_ATTR(HLF_R)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5336 | } |
Bram Moolenaar | 87b5ca5 | 2006-03-04 21:55:31 +0000 | [diff] [blame] | 5337 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5338 | #endif |
Bram Moolenaar | 87b5ca5 | 2006-03-04 21:55:31 +0000 | [diff] [blame] | 5339 | if (p_verbose >= 5) |
| 5340 | { |
| 5341 | verbose_enter(); |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 5342 | smsg(_("Searching included file %s"), |
Bram Moolenaar | 87b5ca5 | 2006-03-04 21:55:31 +0000 | [diff] [blame] | 5343 | (char *)new_fname); |
| 5344 | verbose_leave(); |
| 5345 | } |
| 5346 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5347 | } |
| 5348 | } |
| 5349 | } |
| 5350 | else |
| 5351 | { |
| 5352 | /* |
| 5353 | * Check if the line is a define (type == FIND_DEFINE) |
| 5354 | */ |
| 5355 | p = line; |
| 5356 | search_line: |
| 5357 | define_matched = FALSE; |
| 5358 | if (def_regmatch.regprog != NULL |
| 5359 | && vim_regexec(&def_regmatch, line, (colnr_T)0)) |
| 5360 | { |
| 5361 | /* |
| 5362 | * Pattern must be first identifier after 'define', so skip |
| 5363 | * to that position before checking for match of pattern. Also |
| 5364 | * don't let it match beyond the end of this identifier. |
| 5365 | */ |
| 5366 | p = def_regmatch.endp[0]; |
| 5367 | while (*p && !vim_iswordc(*p)) |
| 5368 | p++; |
| 5369 | define_matched = TRUE; |
| 5370 | } |
| 5371 | |
| 5372 | /* |
| 5373 | * Look for a match. Don't do this if we are looking for a |
| 5374 | * define and this line didn't match define_prog above. |
| 5375 | */ |
| 5376 | if (def_regmatch.regprog == NULL || define_matched) |
| 5377 | { |
| 5378 | if (define_matched |
| 5379 | #ifdef FEAT_INS_EXPAND |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5380 | || (compl_cont_status & CONT_SOL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5381 | #endif |
| 5382 | ) |
| 5383 | { |
| 5384 | /* compare the first "len" chars from "ptr" */ |
| 5385 | startp = skipwhite(p); |
| 5386 | if (p_ic) |
| 5387 | matched = !MB_STRNICMP(startp, ptr, len); |
| 5388 | else |
| 5389 | matched = !STRNCMP(startp, ptr, len); |
| 5390 | if (matched && define_matched && whole |
| 5391 | && vim_iswordc(startp[len])) |
| 5392 | matched = FALSE; |
| 5393 | } |
| 5394 | else if (regmatch.regprog != NULL |
| 5395 | && vim_regexec(®match, line, (colnr_T)(p - line))) |
| 5396 | { |
| 5397 | matched = TRUE; |
| 5398 | startp = regmatch.startp[0]; |
| 5399 | /* |
| 5400 | * Check if the line is not a comment line (unless we are |
| 5401 | * looking for a define). A line starting with "# define" |
| 5402 | * is not considered to be a comment line. |
| 5403 | */ |
| 5404 | if (!define_matched && skip_comments) |
| 5405 | { |
| 5406 | #ifdef FEAT_COMMENTS |
| 5407 | if ((*line != '#' || |
| 5408 | STRNCMP(skipwhite(line + 1), "define", 6) != 0) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 5409 | && get_leader_len(line, NULL, FALSE, TRUE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5410 | matched = FALSE; |
| 5411 | |
| 5412 | /* |
| 5413 | * Also check for a "/ *" or "/ /" before the match. |
| 5414 | * Skips lines like "int backwards; / * normal index |
| 5415 | * * /" when looking for "normal". |
| 5416 | * Note: Doesn't skip "/ *" in comments. |
| 5417 | */ |
| 5418 | p = skipwhite(line); |
| 5419 | if (matched |
| 5420 | || (p[0] == '/' && p[1] == '*') || p[0] == '*') |
| 5421 | #endif |
| 5422 | for (p = line; *p && p < startp; ++p) |
| 5423 | { |
| 5424 | if (matched |
| 5425 | && p[0] == '/' |
| 5426 | && (p[1] == '*' || p[1] == '/')) |
| 5427 | { |
| 5428 | matched = FALSE; |
| 5429 | /* After "//" all text is comment */ |
| 5430 | if (p[1] == '/') |
| 5431 | break; |
| 5432 | ++p; |
| 5433 | } |
| 5434 | else if (!matched && p[0] == '*' && p[1] == '/') |
| 5435 | { |
| 5436 | /* Can find match after "* /". */ |
| 5437 | matched = TRUE; |
| 5438 | ++p; |
| 5439 | } |
| 5440 | } |
| 5441 | } |
| 5442 | } |
| 5443 | } |
| 5444 | } |
| 5445 | if (matched) |
| 5446 | { |
| 5447 | #ifdef FEAT_INS_EXPAND |
| 5448 | if (action == ACTION_EXPAND) |
| 5449 | { |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 5450 | int cont_s_ipos = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5451 | int add_r; |
| 5452 | char_u *aux; |
| 5453 | |
| 5454 | if (depth == -1 && lnum == curwin->w_cursor.lnum) |
| 5455 | break; |
| 5456 | found = TRUE; |
| 5457 | aux = p = startp; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5458 | if (compl_cont_status & CONT_ADDING) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5459 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5460 | p += compl_length; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5461 | if (vim_iswordp(p)) |
| 5462 | goto exit_matched; |
| 5463 | p = find_word_start(p); |
| 5464 | } |
| 5465 | p = find_word_end(p); |
| 5466 | i = (int)(p - aux); |
| 5467 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5468 | if ((compl_cont_status & CONT_ADDING) && i == compl_length) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5469 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5470 | /* IOSIZE > compl_length, so the STRNCPY works */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5471 | STRNCPY(IObuff, aux, i); |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 5472 | |
| 5473 | /* Get the next line: when "depth" < 0 from the current |
| 5474 | * buffer, otherwise from the included file. Jump to |
| 5475 | * exit_matched when past the last line. */ |
| 5476 | if (depth < 0) |
| 5477 | { |
| 5478 | if (lnum >= end_lnum) |
| 5479 | goto exit_matched; |
| 5480 | line = ml_get(++lnum); |
| 5481 | } |
| 5482 | else if (vim_fgets(line = file_line, |
| 5483 | LSIZE, files[depth].fp)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5484 | goto exit_matched; |
| 5485 | |
| 5486 | /* we read a line, set "already" to check this "line" later |
| 5487 | * if depth >= 0 we'll increase files[depth].lnum far |
| 5488 | * bellow -- Acevedo */ |
| 5489 | already = aux = p = skipwhite(line); |
| 5490 | p = find_word_start(p); |
| 5491 | p = find_word_end(p); |
| 5492 | if (p > aux) |
| 5493 | { |
| 5494 | if (*aux != ')' && IObuff[i-1] != TAB) |
| 5495 | { |
| 5496 | if (IObuff[i-1] != ' ') |
| 5497 | IObuff[i++] = ' '; |
| 5498 | /* IObuf =~ "\(\k\|\i\).* ", thus i >= 2*/ |
| 5499 | if (p_js |
| 5500 | && (IObuff[i-2] == '.' |
| 5501 | || (vim_strchr(p_cpo, CPO_JOINSP) == NULL |
| 5502 | && (IObuff[i-2] == '?' |
| 5503 | || IObuff[i-2] == '!')))) |
| 5504 | IObuff[i++] = ' '; |
| 5505 | } |
Bram Moolenaar | fe81d45 | 2009-04-22 14:44:41 +0000 | [diff] [blame] | 5506 | /* copy as much as possible of the new word */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5507 | if (p - aux >= IOSIZE - i) |
| 5508 | p = aux + IOSIZE - i - 1; |
| 5509 | STRNCPY(IObuff + i, aux, p - aux); |
| 5510 | i += (int)(p - aux); |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 5511 | cont_s_ipos = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5512 | } |
| 5513 | IObuff[i] = NUL; |
| 5514 | aux = IObuff; |
| 5515 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5516 | if (i == compl_length) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5517 | goto exit_matched; |
| 5518 | } |
| 5519 | |
Bram Moolenaar | e8c3a14 | 2006-08-29 14:30:35 +0000 | [diff] [blame] | 5520 | add_r = ins_compl_add_infercase(aux, i, p_ic, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5521 | curr_fname == curbuf->b_fname ? NULL : curr_fname, |
Bram Moolenaar | d9eefe3 | 2019-04-06 14:22:21 +0200 | [diff] [blame] | 5522 | dir, cont_s_ipos); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5523 | if (add_r == OK) |
| 5524 | /* if dir was BACKWARD then honor it just once */ |
| 5525 | dir = FORWARD; |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5526 | else if (add_r == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5527 | break; |
| 5528 | } |
| 5529 | else |
| 5530 | #endif |
| 5531 | if (action == ACTION_SHOW_ALL) |
| 5532 | { |
| 5533 | found = TRUE; |
| 5534 | if (!did_show) |
| 5535 | gotocmdline(TRUE); /* cursor at status line */ |
| 5536 | if (curr_fname != prev_fname) |
| 5537 | { |
| 5538 | if (did_show) |
| 5539 | msg_putchar('\n'); /* cursor below last one */ |
| 5540 | if (!got_int) /* don't display if 'q' typed |
Bram Moolenaar | fe81d45 | 2009-04-22 14:44:41 +0000 | [diff] [blame] | 5541 | at "--more--" message */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5542 | msg_home_replace_hl(curr_fname); |
| 5543 | prev_fname = curr_fname; |
| 5544 | } |
| 5545 | did_show = TRUE; |
| 5546 | if (!got_int) |
| 5547 | show_pat_in_path(line, type, TRUE, action, |
| 5548 | (depth == -1) ? NULL : files[depth].fp, |
| 5549 | (depth == -1) ? &lnum : &files[depth].lnum, |
| 5550 | match_count++); |
| 5551 | |
| 5552 | /* Set matched flag for this file and all the ones that |
| 5553 | * include it */ |
| 5554 | for (i = 0; i <= depth; ++i) |
| 5555 | files[i].matched = TRUE; |
| 5556 | } |
| 5557 | else if (--count <= 0) |
| 5558 | { |
| 5559 | found = TRUE; |
| 5560 | if (depth == -1 && lnum == curwin->w_cursor.lnum |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 5561 | #if defined(FEAT_QUICKFIX) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5562 | && g_do_tagpreview == 0 |
| 5563 | #endif |
| 5564 | ) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 5565 | emsg(_("E387: Match is on current line")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5566 | else if (action == ACTION_SHOW) |
| 5567 | { |
| 5568 | show_pat_in_path(line, type, did_show, action, |
| 5569 | (depth == -1) ? NULL : files[depth].fp, |
| 5570 | (depth == -1) ? &lnum : &files[depth].lnum, 1L); |
| 5571 | did_show = TRUE; |
| 5572 | } |
| 5573 | else |
| 5574 | { |
| 5575 | #ifdef FEAT_GUI |
| 5576 | need_mouse_correct = TRUE; |
| 5577 | #endif |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 5578 | #if defined(FEAT_QUICKFIX) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5579 | /* ":psearch" uses the preview window */ |
| 5580 | if (g_do_tagpreview != 0) |
| 5581 | { |
| 5582 | curwin_save = curwin; |
Bram Moolenaar | d2cec5b | 2006-03-28 21:08:56 +0000 | [diff] [blame] | 5583 | prepare_tagpreview(TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5584 | } |
| 5585 | #endif |
| 5586 | if (action == ACTION_SPLIT) |
| 5587 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5588 | if (win_split(0, 0) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5589 | break; |
Bram Moolenaar | 3368ea2 | 2010-09-21 16:56:35 +0200 | [diff] [blame] | 5590 | RESET_BINDING(curwin); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5591 | } |
| 5592 | if (depth == -1) |
| 5593 | { |
| 5594 | /* match in current file */ |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 5595 | #if defined(FEAT_QUICKFIX) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5596 | if (g_do_tagpreview != 0) |
| 5597 | { |
Bram Moolenaar | 8ad80de | 2017-06-05 16:01:59 +0200 | [diff] [blame] | 5598 | if (!GETFILE_SUCCESS(getfile( |
Bram Moolenaar | c31f9ae | 2017-07-23 22:02:02 +0200 | [diff] [blame] | 5599 | curwin_save->w_buffer->b_fnum, NULL, |
Bram Moolenaar | 8ad80de | 2017-06-05 16:01:59 +0200 | [diff] [blame] | 5600 | NULL, TRUE, lnum, FALSE))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5601 | break; /* failed to jump to file */ |
| 5602 | } |
| 5603 | else |
| 5604 | #endif |
| 5605 | setpcmark(); |
| 5606 | curwin->w_cursor.lnum = lnum; |
Bram Moolenaar | c31f9ae | 2017-07-23 22:02:02 +0200 | [diff] [blame] | 5607 | check_cursor(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5608 | } |
| 5609 | else |
| 5610 | { |
Bram Moolenaar | 8ad80de | 2017-06-05 16:01:59 +0200 | [diff] [blame] | 5611 | if (!GETFILE_SUCCESS(getfile( |
| 5612 | 0, files[depth].name, NULL, TRUE, |
| 5613 | files[depth].lnum, FALSE))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5614 | break; /* failed to jump to file */ |
| 5615 | /* autocommands may have changed the lnum, we don't |
| 5616 | * want that here */ |
| 5617 | curwin->w_cursor.lnum = files[depth].lnum; |
| 5618 | } |
| 5619 | } |
| 5620 | if (action != ACTION_SHOW) |
| 5621 | { |
Bram Moolenaar | fe81d45 | 2009-04-22 14:44:41 +0000 | [diff] [blame] | 5622 | curwin->w_cursor.col = (colnr_T)(startp - line); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5623 | curwin->w_set_curswant = TRUE; |
| 5624 | } |
| 5625 | |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 5626 | #if defined(FEAT_QUICKFIX) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5627 | if (g_do_tagpreview != 0 |
Bram Moolenaar | 997fb4b | 2006-02-17 21:53:23 +0000 | [diff] [blame] | 5628 | && curwin != curwin_save && win_valid(curwin_save)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5629 | { |
| 5630 | /* Return cursor to where we were */ |
| 5631 | validate_cursor(); |
| 5632 | redraw_later(VALID); |
| 5633 | win_enter(curwin_save, TRUE); |
| 5634 | } |
| 5635 | #endif |
| 5636 | break; |
| 5637 | } |
| 5638 | #ifdef FEAT_INS_EXPAND |
| 5639 | exit_matched: |
| 5640 | #endif |
| 5641 | matched = FALSE; |
| 5642 | /* look for other matches in the rest of the line if we |
| 5643 | * are not at the end of it already */ |
| 5644 | if (def_regmatch.regprog == NULL |
| 5645 | #ifdef FEAT_INS_EXPAND |
| 5646 | && action == ACTION_EXPAND |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5647 | && !(compl_cont_status & CONT_SOL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5648 | #endif |
Bram Moolenaar | fe81d45 | 2009-04-22 14:44:41 +0000 | [diff] [blame] | 5649 | && *startp != NUL |
Bram Moolenaar | 94c465c | 2012-07-19 17:18:26 +0200 | [diff] [blame] | 5650 | && *(p = startp + MB_PTR2LEN(startp)) != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5651 | goto search_line; |
| 5652 | } |
| 5653 | line_breakcheck(); |
| 5654 | #ifdef FEAT_INS_EXPAND |
| 5655 | if (action == ACTION_EXPAND) |
Bram Moolenaar | 472e859 | 2016-10-15 17:06:47 +0200 | [diff] [blame] | 5656 | ins_compl_check_keys(30, FALSE); |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5657 | if (got_int || ins_compl_interrupted()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5658 | #else |
| 5659 | if (got_int) |
| 5660 | #endif |
| 5661 | break; |
| 5662 | |
| 5663 | /* |
| 5664 | * Read the next line. When reading an included file and encountering |
| 5665 | * end-of-file, close the file and continue in the file that included |
| 5666 | * it. |
| 5667 | */ |
| 5668 | while (depth >= 0 && !already |
| 5669 | && vim_fgets(line = file_line, LSIZE, files[depth].fp)) |
| 5670 | { |
| 5671 | fclose(files[depth].fp); |
| 5672 | --old_files; |
| 5673 | files[old_files].name = files[depth].name; |
| 5674 | files[old_files].matched = files[depth].matched; |
| 5675 | --depth; |
| 5676 | curr_fname = (depth == -1) ? curbuf->b_fname |
| 5677 | : files[depth].name; |
| 5678 | if (depth < depth_displayed) |
| 5679 | depth_displayed = depth; |
| 5680 | } |
| 5681 | if (depth >= 0) /* we could read the line */ |
Bram Moolenaar | c84e3c1 | 2013-07-03 22:28:36 +0200 | [diff] [blame] | 5682 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5683 | files[depth].lnum++; |
Bram Moolenaar | c84e3c1 | 2013-07-03 22:28:36 +0200 | [diff] [blame] | 5684 | /* Remove any CR and LF from the line. */ |
| 5685 | i = (int)STRLEN(line); |
| 5686 | if (i > 0 && line[i - 1] == '\n') |
| 5687 | line[--i] = NUL; |
| 5688 | if (i > 0 && line[i - 1] == '\r') |
| 5689 | line[--i] = NUL; |
| 5690 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5691 | else if (!already) |
| 5692 | { |
| 5693 | if (++lnum > end_lnum) |
| 5694 | break; |
| 5695 | line = ml_get(lnum); |
| 5696 | } |
| 5697 | already = NULL; |
| 5698 | } |
| 5699 | /* End of big for (;;) loop. */ |
| 5700 | |
| 5701 | /* Close any files that are still open. */ |
| 5702 | for (i = 0; i <= depth; i++) |
| 5703 | { |
| 5704 | fclose(files[i].fp); |
| 5705 | vim_free(files[i].name); |
| 5706 | } |
| 5707 | for (i = old_files; i < max_path_depth; i++) |
| 5708 | vim_free(files[i].name); |
| 5709 | vim_free(files); |
| 5710 | |
| 5711 | if (type == CHECK_PATH) |
| 5712 | { |
| 5713 | if (!did_show) |
| 5714 | { |
| 5715 | if (action != ACTION_SHOW_ALL) |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 5716 | msg(_("All included files were found")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5717 | else |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 5718 | msg(_("No included files")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5719 | } |
| 5720 | } |
| 5721 | else if (!found |
| 5722 | #ifdef FEAT_INS_EXPAND |
| 5723 | && action != ACTION_EXPAND |
| 5724 | #endif |
| 5725 | ) |
| 5726 | { |
| 5727 | #ifdef FEAT_INS_EXPAND |
Bram Moolenaar | 7591bb3 | 2019-03-30 13:53:47 +0100 | [diff] [blame] | 5728 | if (got_int || ins_compl_interrupted()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5729 | #else |
| 5730 | if (got_int) |
| 5731 | #endif |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 5732 | emsg(_(e_interr)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5733 | else if (type == FIND_DEFINE) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 5734 | emsg(_("E388: Couldn't find definition")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5735 | else |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 5736 | emsg(_("E389: Couldn't find pattern")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5737 | } |
| 5738 | if (action == ACTION_SHOW || action == ACTION_SHOW_ALL) |
| 5739 | msg_end(); |
| 5740 | |
| 5741 | fpip_end: |
| 5742 | vim_free(file_line); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 5743 | vim_regfree(regmatch.regprog); |
| 5744 | vim_regfree(incl_regmatch.regprog); |
| 5745 | vim_regfree(def_regmatch.regprog); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5746 | } |
| 5747 | |
| 5748 | static void |
Bram Moolenaar | 764b23c | 2016-01-30 21:10:09 +0100 | [diff] [blame] | 5749 | show_pat_in_path( |
| 5750 | char_u *line, |
| 5751 | int type, |
| 5752 | int did_show, |
| 5753 | int action, |
| 5754 | FILE *fp, |
| 5755 | linenr_T *lnum, |
| 5756 | long count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5757 | { |
| 5758 | char_u *p; |
| 5759 | |
| 5760 | if (did_show) |
| 5761 | msg_putchar('\n'); /* cursor below last one */ |
Bram Moolenaar | 91170f8 | 2006-05-05 21:15:17 +0000 | [diff] [blame] | 5762 | else if (!msg_silent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5763 | gotocmdline(TRUE); /* cursor at status line */ |
| 5764 | if (got_int) /* 'q' typed at "--more--" message */ |
| 5765 | return; |
| 5766 | for (;;) |
| 5767 | { |
| 5768 | p = line + STRLEN(line) - 1; |
| 5769 | if (fp != NULL) |
| 5770 | { |
| 5771 | /* We used fgets(), so get rid of newline at end */ |
| 5772 | if (p >= line && *p == '\n') |
| 5773 | --p; |
| 5774 | if (p >= line && *p == '\r') |
| 5775 | --p; |
| 5776 | *(p + 1) = NUL; |
| 5777 | } |
| 5778 | if (action == ACTION_SHOW_ALL) |
| 5779 | { |
| 5780 | sprintf((char *)IObuff, "%3ld: ", count); /* show match nr */ |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 5781 | msg_puts((char *)IObuff); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5782 | sprintf((char *)IObuff, "%4ld", *lnum); /* show line nr */ |
| 5783 | /* Highlight line numbers */ |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 5784 | msg_puts_attr((char *)IObuff, HL_ATTR(HLF_N)); |
| 5785 | msg_puts(" "); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5786 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 5787 | msg_prt_line(line, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5788 | out_flush(); /* show one line at a time */ |
| 5789 | |
| 5790 | /* Definition continues until line that doesn't end with '\' */ |
| 5791 | if (got_int || type != FIND_DEFINE || p < line || *p != '\\') |
| 5792 | break; |
| 5793 | |
| 5794 | if (fp != NULL) |
| 5795 | { |
| 5796 | if (vim_fgets(line, LSIZE, fp)) /* end of file */ |
| 5797 | break; |
| 5798 | ++*lnum; |
| 5799 | } |
| 5800 | else |
| 5801 | { |
| 5802 | if (++*lnum > curbuf->b_ml.ml_line_count) |
| 5803 | break; |
| 5804 | line = ml_get(*lnum); |
| 5805 | } |
| 5806 | msg_putchar('\n'); |
| 5807 | } |
| 5808 | } |
| 5809 | #endif |
| 5810 | |
| 5811 | #ifdef FEAT_VIMINFO |
Bram Moolenaar | c332816 | 2019-07-23 22:15:25 +0200 | [diff] [blame] | 5812 | spat_T * |
| 5813 | get_spat(int idx) |
| 5814 | { |
| 5815 | return &spats[idx]; |
| 5816 | } |
| 5817 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5818 | int |
Bram Moolenaar | c332816 | 2019-07-23 22:15:25 +0200 | [diff] [blame] | 5819 | get_spat_last_idx(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5820 | { |
Bram Moolenaar | c332816 | 2019-07-23 22:15:25 +0200 | [diff] [blame] | 5821 | return last_idx; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5822 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5823 | #endif |