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