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