blob: 0884912bf3fb97e21445ebfdcb2d924966ca2ae2 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * Handling of regular expressions: vim_regcomp(), vim_regexec(), vim_regsub()
4 *
5 * NOTICE:
6 *
7 * This is NOT the original regular expression code as written by Henry
8 * Spencer. This code has been modified specifically for use with the VIM
9 * editor, and should not be used separately from Vim. If you want a good
10 * regular expression library, get the original code. The copyright notice
11 * that follows is from the original.
12 *
13 * END NOTICE
14 *
15 * Copyright (c) 1986 by University of Toronto.
16 * Written by Henry Spencer. Not derived from licensed software.
17 *
18 * Permission is granted to anyone to use this software for any
19 * purpose on any computer system, and to redistribute it freely,
20 * subject to the following restrictions:
21 *
22 * 1. The author is not responsible for the consequences of use of
23 * this software, no matter how awful, even if they arise
24 * from defects in it.
25 *
26 * 2. The origin of this software must not be misrepresented, either
27 * by explicit claim or by omission.
28 *
29 * 3. Altered versions must be plainly marked as such, and must not
30 * be misrepresented as being the original software.
31 *
32 * Beware that some of this code is subtly aware of the way operator
33 * precedence is structured in regular expressions. Serious changes in
34 * regular-expression syntax might require a total rethink.
35 *
Bram Moolenaarc0197e22004-09-13 20:26:32 +000036 * Changes have been made by Tony Andrews, Olaf 'Rhialto' Seibert, Robert
37 * Webb, Ciaran McCreesh and Bram Moolenaar.
Bram Moolenaar071d4272004-06-13 20:20:40 +000038 * Named character class support added by Walter Briscoe (1998 Jul 01)
39 */
40
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020041/* Uncomment the first if you do not want to see debugging logs or files
42 * related to regular expressions, even when compiling with -DDEBUG.
43 * Uncomment the second to get the regexp debugging. */
44/* #undef DEBUG */
45/* #define DEBUG */
46
Bram Moolenaar071d4272004-06-13 20:20:40 +000047#include "vim.h"
48
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020049#ifdef DEBUG
50/* show/save debugging data when BT engine is used */
51# define BT_REGEXP_DUMP
52/* save the debugging data to a file instead of displaying it */
53# define BT_REGEXP_LOG
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +020054# define BT_REGEXP_DEBUG_LOG
55# define BT_REGEXP_DEBUG_LOG_NAME "bt_regexp_debug.log"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020056#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000057
58/*
59 * The "internal use only" fields in regexp.h are present to pass info from
60 * compile to execute that permits the execute phase to run lots faster on
61 * simple cases. They are:
62 *
63 * regstart char that must begin a match; NUL if none obvious; Can be a
64 * multi-byte character.
65 * reganch is the match anchored (at beginning-of-line only)?
66 * regmust string (pointer into program) that match must include, or NULL
67 * regmlen length of regmust string
68 * regflags RF_ values or'ed together
69 *
70 * Regstart and reganch permit very fast decisions on suitable starting points
71 * for a match, cutting down the work a lot. Regmust permits fast rejection
72 * of lines that cannot possibly match. The regmust tests are costly enough
73 * that vim_regcomp() supplies a regmust only if the r.e. contains something
74 * potentially expensive (at present, the only such thing detected is * or +
75 * at the start of the r.e., which can involve a lot of backup). Regmlen is
76 * supplied because the test in vim_regexec() needs it and vim_regcomp() is
77 * computing it anyway.
78 */
79
80/*
81 * Structure for regexp "program". This is essentially a linear encoding
82 * of a nondeterministic finite-state machine (aka syntax charts or
83 * "railroad normal form" in parsing technology). Each node is an opcode
84 * plus a "next" pointer, possibly plus an operand. "Next" pointers of
85 * all nodes except BRANCH and BRACES_COMPLEX implement concatenation; a "next"
86 * pointer with a BRANCH on both ends of it is connecting two alternatives.
87 * (Here we have one of the subtle syntax dependencies: an individual BRANCH
88 * (as opposed to a collection of them) is never concatenated with anything
89 * because of operator precedence). The "next" pointer of a BRACES_COMPLEX
Bram Moolenaardf177f62005-02-22 08:39:57 +000090 * node points to the node after the stuff to be repeated.
91 * The operand of some types of node is a literal string; for others, it is a
92 * node leading into a sub-FSM. In particular, the operand of a BRANCH node
93 * is the first node of the branch.
94 * (NB this is *not* a tree structure: the tail of the branch connects to the
95 * thing following the set of BRANCHes.)
Bram Moolenaar071d4272004-06-13 20:20:40 +000096 *
97 * pattern is coded like:
98 *
99 * +-----------------+
100 * | V
101 * <aa>\|<bb> BRANCH <aa> BRANCH <bb> --> END
102 * | ^ | ^
103 * +------+ +----------+
104 *
105 *
106 * +------------------+
107 * V |
108 * <aa>* BRANCH BRANCH <aa> --> BACK BRANCH --> NOTHING --> END
109 * | | ^ ^
110 * | +---------------+ |
111 * +---------------------------------------------+
112 *
113 *
Bram Moolenaardf177f62005-02-22 08:39:57 +0000114 * +----------------------+
115 * V |
Bram Moolenaar582fd852005-03-28 20:58:01 +0000116 * <aa>\+ BRANCH <aa> --> BRANCH --> BACK BRANCH --> NOTHING --> END
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000117 * | | ^ ^
118 * | +-----------+ |
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000119 * +--------------------------------------------------+
Bram Moolenaardf177f62005-02-22 08:39:57 +0000120 *
121 *
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122 * +-------------------------+
123 * V |
124 * <aa>\{} BRANCH BRACE_LIMITS --> BRACE_COMPLEX <aa> --> BACK END
125 * | | ^
126 * | +----------------+
127 * +-----------------------------------------------+
128 *
129 *
130 * <aa>\@!<bb> BRANCH NOMATCH <aa> --> END <bb> --> END
131 * | | ^ ^
132 * | +----------------+ |
133 * +--------------------------------+
134 *
135 * +---------+
136 * | V
137 * \z[abc] BRANCH BRANCH a BRANCH b BRANCH c BRANCH NOTHING --> END
138 * | | | | ^ ^
139 * | | | +-----+ |
140 * | | +----------------+ |
141 * | +---------------------------+ |
142 * +------------------------------------------------------+
143 *
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +0000144 * They all start with a BRANCH for "\|" alternatives, even when there is only
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145 * one alternative.
146 */
147
148/*
149 * The opcodes are:
150 */
151
152/* definition number opnd? meaning */
153#define END 0 /* End of program or NOMATCH operand. */
154#define BOL 1 /* Match "" at beginning of line. */
155#define EOL 2 /* Match "" at end of line. */
156#define BRANCH 3 /* node Match this alternative, or the
157 * next... */
158#define BACK 4 /* Match "", "next" ptr points backward. */
159#define EXACTLY 5 /* str Match this string. */
160#define NOTHING 6 /* Match empty string. */
161#define STAR 7 /* node Match this (simple) thing 0 or more
162 * times. */
163#define PLUS 8 /* node Match this (simple) thing 1 or more
164 * times. */
165#define MATCH 9 /* node match the operand zero-width */
166#define NOMATCH 10 /* node check for no match with operand */
167#define BEHIND 11 /* node look behind for a match with operand */
168#define NOBEHIND 12 /* node look behind for no match with operand */
169#define SUBPAT 13 /* node match the operand here */
170#define BRACE_SIMPLE 14 /* node Match this (simple) thing between m and
171 * n times (\{m,n\}). */
172#define BOW 15 /* Match "" after [^a-zA-Z0-9_] */
173#define EOW 16 /* Match "" at [^a-zA-Z0-9_] */
174#define BRACE_LIMITS 17 /* nr nr define the min & max for BRACE_SIMPLE
175 * and BRACE_COMPLEX. */
176#define NEWL 18 /* Match line-break */
177#define BHPOS 19 /* End position for BEHIND or NOBEHIND */
178
179
180/* character classes: 20-48 normal, 50-78 include a line-break */
181#define ADD_NL 30
182#define FIRST_NL ANY + ADD_NL
183#define ANY 20 /* Match any one character. */
184#define ANYOF 21 /* str Match any character in this string. */
185#define ANYBUT 22 /* str Match any character not in this
186 * string. */
187#define IDENT 23 /* Match identifier char */
188#define SIDENT 24 /* Match identifier char but no digit */
189#define KWORD 25 /* Match keyword char */
190#define SKWORD 26 /* Match word char but no digit */
191#define FNAME 27 /* Match file name char */
192#define SFNAME 28 /* Match file name char but no digit */
193#define PRINT 29 /* Match printable char */
194#define SPRINT 30 /* Match printable char but no digit */
195#define WHITE 31 /* Match whitespace char */
196#define NWHITE 32 /* Match non-whitespace char */
197#define DIGIT 33 /* Match digit char */
198#define NDIGIT 34 /* Match non-digit char */
199#define HEX 35 /* Match hex char */
200#define NHEX 36 /* Match non-hex char */
201#define OCTAL 37 /* Match octal char */
202#define NOCTAL 38 /* Match non-octal char */
203#define WORD 39 /* Match word char */
204#define NWORD 40 /* Match non-word char */
205#define HEAD 41 /* Match head char */
206#define NHEAD 42 /* Match non-head char */
207#define ALPHA 43 /* Match alpha char */
208#define NALPHA 44 /* Match non-alpha char */
209#define LOWER 45 /* Match lowercase char */
210#define NLOWER 46 /* Match non-lowercase char */
211#define UPPER 47 /* Match uppercase char */
212#define NUPPER 48 /* Match non-uppercase char */
213#define LAST_NL NUPPER + ADD_NL
214#define WITH_NL(op) ((op) >= FIRST_NL && (op) <= LAST_NL)
215
216#define MOPEN 80 /* -89 Mark this point in input as start of
217 * \( subexpr. MOPEN + 0 marks start of
218 * match. */
219#define MCLOSE 90 /* -99 Analogous to MOPEN. MCLOSE + 0 marks
220 * end of match. */
221#define BACKREF 100 /* -109 node Match same string again \1-\9 */
222
223#ifdef FEAT_SYN_HL
224# define ZOPEN 110 /* -119 Mark this point in input as start of
225 * \z( subexpr. */
226# define ZCLOSE 120 /* -129 Analogous to ZOPEN. */
227# define ZREF 130 /* -139 node Match external submatch \z1-\z9 */
228#endif
229
230#define BRACE_COMPLEX 140 /* -149 node Match nodes between m & n times */
231
232#define NOPEN 150 /* Mark this point in input as start of
233 \%( subexpr. */
234#define NCLOSE 151 /* Analogous to NOPEN. */
235
236#define MULTIBYTECODE 200 /* mbc Match one multi-byte character */
237#define RE_BOF 201 /* Match "" at beginning of file. */
238#define RE_EOF 202 /* Match "" at end of file. */
239#define CURSOR 203 /* Match location of cursor. */
240
241#define RE_LNUM 204 /* nr cmp Match line number */
242#define RE_COL 205 /* nr cmp Match column number */
243#define RE_VCOL 206 /* nr cmp Match virtual column number */
244
Bram Moolenaar71fe80d2006-01-22 23:25:56 +0000245#define RE_MARK 207 /* mark cmp Match mark position */
246#define RE_VISUAL 208 /* Match Visual area */
Bram Moolenaar8df5acf2014-05-13 19:37:29 +0200247#define RE_COMPOSING 209 /* any composing characters */
Bram Moolenaar71fe80d2006-01-22 23:25:56 +0000248
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249/*
250 * Magic characters have a special meaning, they don't match literally.
251 * Magic characters are negative. This separates them from literal characters
252 * (possibly multi-byte). Only ASCII characters can be Magic.
253 */
254#define Magic(x) ((int)(x) - 256)
255#define un_Magic(x) ((x) + 256)
256#define is_Magic(x) ((x) < 0)
257
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100259no_Magic(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260{
261 if (is_Magic(x))
262 return un_Magic(x);
263 return x;
264}
265
266 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100267toggle_Magic(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268{
269 if (is_Magic(x))
270 return un_Magic(x);
271 return Magic(x);
272}
273
274/*
275 * The first byte of the regexp internal "program" is actually this magic
276 * number; the start node begins in the second byte. It's used to catch the
277 * most severe mutilation of the program by the caller.
278 */
279
280#define REGMAGIC 0234
281
282/*
283 * Opcode notes:
284 *
285 * BRANCH The set of branches constituting a single choice are hooked
286 * together with their "next" pointers, since precedence prevents
287 * anything being concatenated to any individual branch. The
288 * "next" pointer of the last BRANCH in a choice points to the
289 * thing following the whole choice. This is also where the
290 * final "next" pointer of each individual branch points; each
291 * branch starts with the operand node of a BRANCH node.
292 *
293 * BACK Normal "next" pointers all implicitly point forward; BACK
294 * exists to make loop structures possible.
295 *
296 * STAR,PLUS '=', and complex '*' and '+', are implemented as circular
297 * BRANCH structures using BACK. Simple cases (one character
298 * per match) are implemented with STAR and PLUS for speed
299 * and to minimize recursive plunges.
300 *
301 * BRACE_LIMITS This is always followed by a BRACE_SIMPLE or BRACE_COMPLEX
302 * node, and defines the min and max limits to be used for that
303 * node.
304 *
305 * MOPEN,MCLOSE ...are numbered at compile time.
306 * ZOPEN,ZCLOSE ...ditto
307 */
308
309/*
310 * A node is one char of opcode followed by two chars of "next" pointer.
311 * "Next" pointers are stored as two 8-bit bytes, high order first. The
312 * value is a positive offset from the opcode of the node containing it.
313 * An operand, if any, simply follows the node. (Note that much of the
314 * code generation knows about this implicit relationship.)
315 *
316 * Using two bytes for the "next" pointer is vast overkill for most things,
317 * but allows patterns to get big without disasters.
318 */
319#define OP(p) ((int)*(p))
320#define NEXT(p) (((*((p) + 1) & 0377) << 8) + (*((p) + 2) & 0377))
321#define OPERAND(p) ((p) + 3)
322/* Obtain an operand that was stored as four bytes, MSB first. */
323#define OPERAND_MIN(p) (((long)(p)[3] << 24) + ((long)(p)[4] << 16) \
324 + ((long)(p)[5] << 8) + (long)(p)[6])
325/* Obtain a second operand stored as four bytes. */
326#define OPERAND_MAX(p) OPERAND_MIN((p) + 4)
327/* Obtain a second single-byte operand stored after a four bytes operand. */
328#define OPERAND_CMP(p) (p)[7]
329
330/*
331 * Utility definitions.
332 */
333#define UCHARAT(p) ((int)*(char_u *)(p))
334
335/* Used for an error (down from) vim_regcomp(): give the error message, set
336 * rc_did_emsg and return NULL */
Bram Moolenaar98692072006-02-04 00:57:42 +0000337#define EMSG_RET_NULL(m) return (EMSG(m), rc_did_emsg = TRUE, (void *)NULL)
Bram Moolenaar45eeb132005-06-06 21:59:07 +0000338#define EMSG_RET_FAIL(m) return (EMSG(m), rc_did_emsg = TRUE, FAIL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200339#define EMSG2_RET_NULL(m, c) return (EMSG2((m), (c) ? "" : "\\"), rc_did_emsg = TRUE, (void *)NULL)
340#define EMSG2_RET_FAIL(m, c) return (EMSG2((m), (c) ? "" : "\\"), rc_did_emsg = TRUE, FAIL)
341#define EMSG_ONE_RET_NULL EMSG2_RET_NULL(_("E369: invalid item in %s%%[]"), reg_magic == MAGIC_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342
343#define MAX_LIMIT (32767L << 16L)
344
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100345static int re_multi_type(int);
346static int cstrncmp(char_u *s1, char_u *s2, int *n);
347static char_u *cstrchr(char_u *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200349#ifdef BT_REGEXP_DUMP
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100350static void regdump(char_u *, bt_regprog_T *);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200351#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352#ifdef DEBUG
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100353static char_u *regprop(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354#endif
355
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100356static int re_mult_next(char *what);
Bram Moolenaarfb031402014-09-09 17:18:49 +0200357
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200358static char_u e_missingbracket[] = N_("E769: Missing ] after %s[");
359static char_u e_unmatchedpp[] = N_("E53: Unmatched %s%%(");
360static char_u e_unmatchedp[] = N_("E54: Unmatched %s(");
361static char_u e_unmatchedpar[] = N_("E55: Unmatched %s)");
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200362#ifdef FEAT_SYN_HL
Bram Moolenaar5de820b2013-06-02 15:01:57 +0200363static char_u e_z_not_allowed[] = N_("E66: \\z( not allowed here");
364static char_u e_z1_not_allowed[] = N_("E67: \\z1 et al. not allowed here");
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200365#endif
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +0200366static char_u e_missing_sb[] = N_("E69: Missing ] after %s%%[");
Bram Moolenaar2976c022013-06-05 21:30:37 +0200367static char_u e_empty_sb[] = N_("E70: Empty %s%%[]");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368#define NOT_MULTI 0
369#define MULTI_ONE 1
370#define MULTI_MULT 2
371/*
372 * Return NOT_MULTI if c is not a "multi" operator.
373 * Return MULTI_ONE if c is a single "multi" operator.
374 * Return MULTI_MULT if c is a multi "multi" operator.
375 */
376 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100377re_multi_type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378{
379 if (c == Magic('@') || c == Magic('=') || c == Magic('?'))
380 return MULTI_ONE;
381 if (c == Magic('*') || c == Magic('+') || c == Magic('{'))
382 return MULTI_MULT;
383 return NOT_MULTI;
384}
385
386/*
387 * Flags to be passed up and down.
388 */
389#define HASWIDTH 0x1 /* Known never to match null string. */
390#define SIMPLE 0x2 /* Simple enough to be STAR/PLUS operand. */
391#define SPSTART 0x4 /* Starts with * or +. */
392#define HASNL 0x8 /* Contains some \n. */
393#define HASLOOKBH 0x10 /* Contains "\@<=" or "\@<!". */
394#define WORST 0 /* Worst case. */
395
396/*
397 * When regcode is set to this value, code is not emitted and size is computed
398 * instead.
399 */
400#define JUST_CALC_SIZE ((char_u *) -1)
401
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000402static char_u *reg_prev_sub = NULL;
403
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404/*
405 * REGEXP_INRANGE contains all characters which are always special in a []
406 * range after '\'.
407 * REGEXP_ABBR contains all characters which act as abbreviations after '\'.
408 * These are:
409 * \n - New line (NL).
410 * \r - Carriage Return (CR).
411 * \t - Tab (TAB).
412 * \e - Escape (ESC).
413 * \b - Backspace (Ctrl_H).
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000414 * \d - Character code in decimal, eg \d123
415 * \o - Character code in octal, eg \o80
416 * \x - Character code in hex, eg \x4a
417 * \u - Multibyte character code, eg \u20ac
418 * \U - Long multibyte character code, eg \U12345678
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 */
420static char_u REGEXP_INRANGE[] = "]^-n\\";
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000421static char_u REGEXP_ABBR[] = "nrtebdoxuU";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100423static int backslash_trans(int c);
424static int get_char_class(char_u **pp);
425static int get_equi_class(char_u **pp);
426static void reg_equi_class(int c);
427static int get_coll_element(char_u **pp);
428static char_u *skip_anyof(char_u *p);
429static void init_class_tab(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430
431/*
432 * Translate '\x' to its control character, except "\n", which is Magic.
433 */
434 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100435backslash_trans(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436{
437 switch (c)
438 {
439 case 'r': return CAR;
440 case 't': return TAB;
441 case 'e': return ESC;
442 case 'b': return BS;
443 }
444 return c;
445}
446
447/*
Bram Moolenaardf177f62005-02-22 08:39:57 +0000448 * Check for a character class name "[:name:]". "pp" points to the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 * Returns one of the CLASS_ items. CLASS_NONE means that no item was
450 * recognized. Otherwise "pp" is advanced to after the item.
451 */
452 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100453get_char_class(char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454{
455 static const char *(class_names[]) =
456 {
457 "alnum:]",
458#define CLASS_ALNUM 0
459 "alpha:]",
460#define CLASS_ALPHA 1
461 "blank:]",
462#define CLASS_BLANK 2
463 "cntrl:]",
464#define CLASS_CNTRL 3
465 "digit:]",
466#define CLASS_DIGIT 4
467 "graph:]",
468#define CLASS_GRAPH 5
469 "lower:]",
470#define CLASS_LOWER 6
471 "print:]",
472#define CLASS_PRINT 7
473 "punct:]",
474#define CLASS_PUNCT 8
475 "space:]",
476#define CLASS_SPACE 9
477 "upper:]",
478#define CLASS_UPPER 10
479 "xdigit:]",
480#define CLASS_XDIGIT 11
481 "tab:]",
482#define CLASS_TAB 12
483 "return:]",
484#define CLASS_RETURN 13
485 "backspace:]",
486#define CLASS_BACKSPACE 14
487 "escape:]",
488#define CLASS_ESCAPE 15
489 };
490#define CLASS_NONE 99
491 int i;
492
493 if ((*pp)[1] == ':')
494 {
Bram Moolenaar78a15312009-05-15 19:33:18 +0000495 for (i = 0; i < (int)(sizeof(class_names) / sizeof(*class_names)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 if (STRNCMP(*pp + 2, class_names[i], STRLEN(class_names[i])) == 0)
497 {
498 *pp += STRLEN(class_names[i]) + 2;
499 return i;
500 }
501 }
502 return CLASS_NONE;
503}
504
505/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 * Specific version of character class functions.
507 * Using a table to keep this fast.
508 */
509static short class_tab[256];
510
511#define RI_DIGIT 0x01
512#define RI_HEX 0x02
513#define RI_OCTAL 0x04
514#define RI_WORD 0x08
515#define RI_HEAD 0x10
516#define RI_ALPHA 0x20
517#define RI_LOWER 0x40
518#define RI_UPPER 0x80
519#define RI_WHITE 0x100
520
521 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100522init_class_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523{
524 int i;
525 static int done = FALSE;
526
527 if (done)
528 return;
529
530 for (i = 0; i < 256; ++i)
531 {
532 if (i >= '0' && i <= '7')
533 class_tab[i] = RI_DIGIT + RI_HEX + RI_OCTAL + RI_WORD;
534 else if (i >= '8' && i <= '9')
535 class_tab[i] = RI_DIGIT + RI_HEX + RI_WORD;
536 else if (i >= 'a' && i <= 'f')
537 class_tab[i] = RI_HEX + RI_WORD + RI_HEAD + RI_ALPHA + RI_LOWER;
538#ifdef EBCDIC
539 else if ((i >= 'g' && i <= 'i') || (i >= 'j' && i <= 'r')
540 || (i >= 's' && i <= 'z'))
541#else
542 else if (i >= 'g' && i <= 'z')
543#endif
544 class_tab[i] = RI_WORD + RI_HEAD + RI_ALPHA + RI_LOWER;
545 else if (i >= 'A' && i <= 'F')
546 class_tab[i] = RI_HEX + RI_WORD + RI_HEAD + RI_ALPHA + RI_UPPER;
547#ifdef EBCDIC
548 else if ((i >= 'G' && i <= 'I') || ( i >= 'J' && i <= 'R')
549 || (i >= 'S' && i <= 'Z'))
550#else
551 else if (i >= 'G' && i <= 'Z')
552#endif
553 class_tab[i] = RI_WORD + RI_HEAD + RI_ALPHA + RI_UPPER;
554 else if (i == '_')
555 class_tab[i] = RI_WORD + RI_HEAD;
556 else
557 class_tab[i] = 0;
558 }
559 class_tab[' '] |= RI_WHITE;
560 class_tab['\t'] |= RI_WHITE;
561 done = TRUE;
562}
563
564#ifdef FEAT_MBYTE
565# define ri_digit(c) (c < 0x100 && (class_tab[c] & RI_DIGIT))
566# define ri_hex(c) (c < 0x100 && (class_tab[c] & RI_HEX))
567# define ri_octal(c) (c < 0x100 && (class_tab[c] & RI_OCTAL))
568# define ri_word(c) (c < 0x100 && (class_tab[c] & RI_WORD))
569# define ri_head(c) (c < 0x100 && (class_tab[c] & RI_HEAD))
570# define ri_alpha(c) (c < 0x100 && (class_tab[c] & RI_ALPHA))
571# define ri_lower(c) (c < 0x100 && (class_tab[c] & RI_LOWER))
572# define ri_upper(c) (c < 0x100 && (class_tab[c] & RI_UPPER))
573# define ri_white(c) (c < 0x100 && (class_tab[c] & RI_WHITE))
574#else
575# define ri_digit(c) (class_tab[c] & RI_DIGIT)
576# define ri_hex(c) (class_tab[c] & RI_HEX)
577# define ri_octal(c) (class_tab[c] & RI_OCTAL)
578# define ri_word(c) (class_tab[c] & RI_WORD)
579# define ri_head(c) (class_tab[c] & RI_HEAD)
580# define ri_alpha(c) (class_tab[c] & RI_ALPHA)
581# define ri_lower(c) (class_tab[c] & RI_LOWER)
582# define ri_upper(c) (class_tab[c] & RI_UPPER)
583# define ri_white(c) (class_tab[c] & RI_WHITE)
584#endif
585
586/* flags for regflags */
587#define RF_ICASE 1 /* ignore case */
588#define RF_NOICASE 2 /* don't ignore case */
589#define RF_HASNL 4 /* can match a NL */
590#define RF_ICOMBINE 8 /* ignore combining characters */
591#define RF_LOOKBH 16 /* uses "\@<=" or "\@<!" */
592
593/*
594 * Global work variables for vim_regcomp().
595 */
596
597static char_u *regparse; /* Input-scan pointer. */
598static int prevchr_len; /* byte length of previous char */
599static int num_complex_braces; /* Complex \{...} count */
600static int regnpar; /* () count. */
601#ifdef FEAT_SYN_HL
602static int regnzpar; /* \z() count. */
603static int re_has_z; /* \z item detected */
604#endif
605static char_u *regcode; /* Code-emit pointer, or JUST_CALC_SIZE */
606static long regsize; /* Code size. */
Bram Moolenaard3005802009-11-25 17:21:32 +0000607static int reg_toolong; /* TRUE when offset out of range */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608static char_u had_endbrace[NSUBEXP]; /* flags, TRUE if end of () found */
609static unsigned regflags; /* RF_ flags for prog */
610static long brace_min[10]; /* Minimums for complex brace repeats */
611static long brace_max[10]; /* Maximums for complex brace repeats */
612static int brace_count[10]; /* Current counts for complex brace repeats */
613#if defined(FEAT_SYN_HL) || defined(PROTO)
614static int had_eol; /* TRUE when EOL found by vim_regcomp() */
615#endif
616static int one_exactly = FALSE; /* only do one char for EXACTLY */
617
618static int reg_magic; /* magicness of the pattern: */
619#define MAGIC_NONE 1 /* "\V" very unmagic */
620#define MAGIC_OFF 2 /* "\M" or 'magic' off */
621#define MAGIC_ON 3 /* "\m" or 'magic' */
622#define MAGIC_ALL 4 /* "\v" very magic */
623
624static int reg_string; /* matching with a string instead of a buffer
625 line */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000626static int reg_strict; /* "[abc" is illegal */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627
628/*
629 * META contains all characters that may be magic, except '^' and '$'.
630 */
631
632#ifdef EBCDIC
633static char_u META[] = "%&()*+.123456789<=>?@ACDFHIKLMOPSUVWX[_acdfhiklmnopsuvwxz{|~";
634#else
635/* META[] is used often enough to justify turning it into a table. */
636static char_u META_flags[] = {
637 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
638 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
639/* % & ( ) * + . */
640 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0,
641/* 1 2 3 4 5 6 7 8 9 < = > ? */
642 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1,
643/* @ A C D F H I K L M O */
644 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1,
645/* P S U V W X Z [ _ */
646 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1,
647/* a c d f h i k l m n o */
648 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1,
649/* p s u v w x z { | ~ */
650 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1
651};
652#endif
653
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200654static int curchr; /* currently parsed character */
655/* Previous character. Note: prevchr is sometimes -1 when we are not at the
656 * start, eg in /[ ^I]^ the pattern was never found even if it existed,
657 * because ^ was taken to be magic -- webb */
658static int prevchr;
659static int prevprevchr; /* previous-previous character */
660static int nextchr; /* used for ungetchr() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661
662/* arguments for reg() */
663#define REG_NOPAREN 0 /* toplevel reg() */
664#define REG_PAREN 1 /* \(\) */
665#define REG_ZPAREN 2 /* \z(\) */
666#define REG_NPAREN 3 /* \%(\) */
667
Bram Moolenaar3737fc12013-06-01 14:42:56 +0200668typedef struct
669{
670 char_u *regparse;
671 int prevchr_len;
672 int curchr;
673 int prevchr;
674 int prevprevchr;
675 int nextchr;
676 int at_start;
677 int prev_at_start;
678 int regnpar;
679} parse_state_T;
680
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681/*
682 * Forward declarations for vim_regcomp()'s friends.
683 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100684static void initchr(char_u *);
685static void save_parse_state(parse_state_T *ps);
686static void restore_parse_state(parse_state_T *ps);
687static int getchr(void);
688static void skipchr_keepstart(void);
689static int peekchr(void);
690static void skipchr(void);
691static void ungetchr(void);
692static int gethexchrs(int maxinputlen);
693static int getoctchrs(void);
694static int getdecchrs(void);
695static int coll_get_char(void);
696static void regcomp_start(char_u *expr, int flags);
697static char_u *reg(int, int *);
698static char_u *regbranch(int *flagp);
699static char_u *regconcat(int *flagp);
700static char_u *regpiece(int *);
701static char_u *regatom(int *);
702static char_u *regnode(int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000703#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100704static int use_multibytecode(int c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000705#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100706static int prog_magic_wrong(void);
707static char_u *regnext(char_u *);
708static void regc(int b);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100710static void regmbc(int c);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200711# define REGMBC(x) regmbc(x);
712# define CASEMBC(x) case x:
Bram Moolenaardf177f62005-02-22 08:39:57 +0000713#else
714# define regmbc(c) regc(c)
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200715# define REGMBC(x)
716# define CASEMBC(x)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100718static void reginsert(int, char_u *);
719static void reginsert_nr(int op, long val, char_u *opnd);
720static void reginsert_limits(int, long, long, char_u *);
721static char_u *re_put_long(char_u *pr, long_u val);
722static int read_limits(long *, long *);
723static void regtail(char_u *, char_u *);
724static void regoptail(char_u *, char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200726static regengine_T bt_regengine;
727static regengine_T nfa_regengine;
728
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729/*
730 * Return TRUE if compiled regular expression "prog" can match a line break.
731 */
732 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100733re_multiline(regprog_T *prog)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734{
735 return (prog->regflags & RF_HASNL);
736}
737
738/*
739 * Return TRUE if compiled regular expression "prog" looks before the start
740 * position (pattern contains "\@<=" or "\@<!").
741 */
742 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100743re_lookbehind(regprog_T *prog)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744{
745 return (prog->regflags & RF_LOOKBH);
746}
747
748/*
Bram Moolenaardf177f62005-02-22 08:39:57 +0000749 * Check for an equivalence class name "[=a=]". "pp" points to the '['.
750 * Returns a character representing the class. Zero means that no item was
751 * recognized. Otherwise "pp" is advanced to after the item.
752 */
753 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100754get_equi_class(char_u **pp)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000755{
756 int c;
757 int l = 1;
758 char_u *p = *pp;
759
760 if (p[1] == '=')
761 {
762#ifdef FEAT_MBYTE
763 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000764 l = (*mb_ptr2len)(p + 2);
Bram Moolenaardf177f62005-02-22 08:39:57 +0000765#endif
766 if (p[l + 2] == '=' && p[l + 3] == ']')
767 {
768#ifdef FEAT_MBYTE
769 if (has_mbyte)
770 c = mb_ptr2char(p + 2);
771 else
772#endif
773 c = p[2];
774 *pp += l + 4;
775 return c;
776 }
777 }
778 return 0;
779}
780
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200781#ifdef EBCDIC
782/*
783 * Table for equivalence class "c". (IBM-1047)
784 */
785char *EQUIVAL_CLASS_C[16] = {
786 "A\x62\x63\x64\x65\x66\x67",
787 "C\x68",
788 "E\x71\x72\x73\x74",
789 "I\x75\x76\x77\x78",
790 "N\x69",
Bram Moolenaar22e42152016-04-03 14:02:02 +0200791 "O\xEB\xEC\xED\xEE\xEF\x80",
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200792 "U\xFB\xFC\xFD\xFE",
793 "Y\xBA",
794 "a\x42\x43\x44\x45\x46\x47",
795 "c\x48",
796 "e\x51\x52\x53\x54",
797 "i\x55\x56\x57\x58",
798 "n\x49",
Bram Moolenaar22e42152016-04-03 14:02:02 +0200799 "o\xCB\xCC\xCD\xCE\xCF\x70",
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200800 "u\xDB\xDC\xDD\xDE",
801 "y\x8D\xDF",
802};
803#endif
804
Bram Moolenaardf177f62005-02-22 08:39:57 +0000805/*
806 * Produce the bytes for equivalence class "c".
807 * Currently only handles latin1, latin9 and utf-8.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200808 * NOTE: When changing this function, also change nfa_emit_equi_class()
Bram Moolenaardf177f62005-02-22 08:39:57 +0000809 */
810 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100811reg_equi_class(int c)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000812{
813#ifdef FEAT_MBYTE
814 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
Bram Moolenaar78622822005-08-23 21:00:13 +0000815 || STRCMP(p_enc, "iso-8859-15") == 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000816#endif
817 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200818#ifdef EBCDIC
819 int i;
820
821 /* This might be slower than switch/case below. */
822 for (i = 0; i < 16; i++)
823 {
824 if (vim_strchr(EQUIVAL_CLASS_C[i], c) != NULL)
825 {
826 char *p = EQUIVAL_CLASS_C[i];
827
828 while (*p != 0)
829 regmbc(*p++);
830 return;
831 }
832 }
833#else
Bram Moolenaardf177f62005-02-22 08:39:57 +0000834 switch (c)
835 {
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200836 /* Do not use '\300' style, it results in a negative number. */
837 case 'A': case 0xc0: case 0xc1: case 0xc2:
838 case 0xc3: case 0xc4: case 0xc5:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200839 CASEMBC(0x100) CASEMBC(0x102) CASEMBC(0x104) CASEMBC(0x1cd)
840 CASEMBC(0x1de) CASEMBC(0x1e0) CASEMBC(0x1ea2)
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200841 regmbc('A'); regmbc(0xc0); regmbc(0xc1);
842 regmbc(0xc2); regmbc(0xc3); regmbc(0xc4);
843 regmbc(0xc5);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200844 REGMBC(0x100) REGMBC(0x102) REGMBC(0x104)
845 REGMBC(0x1cd) REGMBC(0x1de) REGMBC(0x1e0)
846 REGMBC(0x1ea2)
847 return;
848 case 'B': CASEMBC(0x1e02) CASEMBC(0x1e06)
849 regmbc('B'); REGMBC(0x1e02) REGMBC(0x1e06)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000850 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200851 case 'C': case 0xc7:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200852 CASEMBC(0x106) CASEMBC(0x108) CASEMBC(0x10a) CASEMBC(0x10c)
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200853 regmbc('C'); regmbc(0xc7);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200854 REGMBC(0x106) REGMBC(0x108) REGMBC(0x10a)
855 REGMBC(0x10c)
856 return;
857 case 'D': CASEMBC(0x10e) CASEMBC(0x110) CASEMBC(0x1e0a)
858 CASEMBC(0x1e0e) CASEMBC(0x1e10)
859 regmbc('D'); REGMBC(0x10e) REGMBC(0x110)
860 REGMBC(0x1e0a) REGMBC(0x1e0e) REGMBC(0x1e10)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000861 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200862 case 'E': case 0xc8: case 0xc9: case 0xca: case 0xcb:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200863 CASEMBC(0x112) CASEMBC(0x114) CASEMBC(0x116) CASEMBC(0x118)
864 CASEMBC(0x11a) CASEMBC(0x1eba) CASEMBC(0x1ebc)
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200865 regmbc('E'); regmbc(0xc8); regmbc(0xc9);
866 regmbc(0xca); regmbc(0xcb);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200867 REGMBC(0x112) REGMBC(0x114) REGMBC(0x116)
868 REGMBC(0x118) REGMBC(0x11a) REGMBC(0x1eba)
869 REGMBC(0x1ebc)
870 return;
871 case 'F': CASEMBC(0x1e1e)
872 regmbc('F'); REGMBC(0x1e1e)
873 return;
874 case 'G': CASEMBC(0x11c) CASEMBC(0x11e) CASEMBC(0x120)
875 CASEMBC(0x122) CASEMBC(0x1e4) CASEMBC(0x1e6) CASEMBC(0x1f4)
876 CASEMBC(0x1e20)
877 regmbc('G'); REGMBC(0x11c) REGMBC(0x11e)
878 REGMBC(0x120) REGMBC(0x122) REGMBC(0x1e4)
879 REGMBC(0x1e6) REGMBC(0x1f4) REGMBC(0x1e20)
880 return;
881 case 'H': CASEMBC(0x124) CASEMBC(0x126) CASEMBC(0x1e22)
882 CASEMBC(0x1e26) CASEMBC(0x1e28)
883 regmbc('H'); REGMBC(0x124) REGMBC(0x126)
884 REGMBC(0x1e22) REGMBC(0x1e26) REGMBC(0x1e28)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000885 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200886 case 'I': case 0xcc: case 0xcd: case 0xce: case 0xcf:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200887 CASEMBC(0x128) CASEMBC(0x12a) CASEMBC(0x12c) CASEMBC(0x12e)
888 CASEMBC(0x130) CASEMBC(0x1cf) CASEMBC(0x1ec8)
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200889 regmbc('I'); regmbc(0xcc); regmbc(0xcd);
890 regmbc(0xce); regmbc(0xcf);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200891 REGMBC(0x128) REGMBC(0x12a) REGMBC(0x12c)
892 REGMBC(0x12e) REGMBC(0x130) REGMBC(0x1cf)
893 REGMBC(0x1ec8)
894 return;
895 case 'J': CASEMBC(0x134)
896 regmbc('J'); REGMBC(0x134)
897 return;
898 case 'K': CASEMBC(0x136) CASEMBC(0x1e8) CASEMBC(0x1e30)
899 CASEMBC(0x1e34)
900 regmbc('K'); REGMBC(0x136) REGMBC(0x1e8)
901 REGMBC(0x1e30) REGMBC(0x1e34)
902 return;
903 case 'L': CASEMBC(0x139) CASEMBC(0x13b) CASEMBC(0x13d)
904 CASEMBC(0x13f) CASEMBC(0x141) CASEMBC(0x1e3a)
905 regmbc('L'); REGMBC(0x139) REGMBC(0x13b)
906 REGMBC(0x13d) REGMBC(0x13f) REGMBC(0x141)
907 REGMBC(0x1e3a)
908 return;
909 case 'M': CASEMBC(0x1e3e) CASEMBC(0x1e40)
910 regmbc('M'); REGMBC(0x1e3e) REGMBC(0x1e40)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000911 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200912 case 'N': case 0xd1:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200913 CASEMBC(0x143) CASEMBC(0x145) CASEMBC(0x147) CASEMBC(0x1e44)
914 CASEMBC(0x1e48)
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200915 regmbc('N'); regmbc(0xd1);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200916 REGMBC(0x143) REGMBC(0x145) REGMBC(0x147)
917 REGMBC(0x1e44) REGMBC(0x1e48)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000918 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200919 case 'O': case 0xd2: case 0xd3: case 0xd4: case 0xd5:
920 case 0xd6: case 0xd8:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200921 CASEMBC(0x14c) CASEMBC(0x14e) CASEMBC(0x150) CASEMBC(0x1a0)
922 CASEMBC(0x1d1) CASEMBC(0x1ea) CASEMBC(0x1ec) CASEMBC(0x1ece)
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200923 regmbc('O'); regmbc(0xd2); regmbc(0xd3);
924 regmbc(0xd4); regmbc(0xd5); regmbc(0xd6);
925 regmbc(0xd8);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200926 REGMBC(0x14c) REGMBC(0x14e) REGMBC(0x150)
927 REGMBC(0x1a0) REGMBC(0x1d1) REGMBC(0x1ea)
928 REGMBC(0x1ec) REGMBC(0x1ece)
929 return;
930 case 'P': case 0x1e54: case 0x1e56:
931 regmbc('P'); REGMBC(0x1e54) REGMBC(0x1e56)
932 return;
933 case 'R': CASEMBC(0x154) CASEMBC(0x156) CASEMBC(0x158)
934 CASEMBC(0x1e58) CASEMBC(0x1e5e)
935 regmbc('R'); REGMBC(0x154) REGMBC(0x156) REGMBC(0x158)
936 REGMBC(0x1e58) REGMBC(0x1e5e)
937 return;
938 case 'S': CASEMBC(0x15a) CASEMBC(0x15c) CASEMBC(0x15e)
939 CASEMBC(0x160) CASEMBC(0x1e60)
940 regmbc('S'); REGMBC(0x15a) REGMBC(0x15c)
941 REGMBC(0x15e) REGMBC(0x160) REGMBC(0x1e60)
942 return;
943 case 'T': CASEMBC(0x162) CASEMBC(0x164) CASEMBC(0x166)
944 CASEMBC(0x1e6a) CASEMBC(0x1e6e)
945 regmbc('T'); REGMBC(0x162) REGMBC(0x164)
946 REGMBC(0x166) REGMBC(0x1e6a) REGMBC(0x1e6e)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000947 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200948 case 'U': case 0xd9: case 0xda: case 0xdb: case 0xdc:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200949 CASEMBC(0x168) CASEMBC(0x16a) CASEMBC(0x16c) CASEMBC(0x16e)
950 CASEMBC(0x170) CASEMBC(0x172) CASEMBC(0x1af) CASEMBC(0x1d3)
951 CASEMBC(0x1ee6)
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200952 regmbc('U'); regmbc(0xd9); regmbc(0xda);
953 regmbc(0xdb); regmbc(0xdc);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200954 REGMBC(0x168) REGMBC(0x16a) REGMBC(0x16c)
955 REGMBC(0x16e) REGMBC(0x170) REGMBC(0x172)
956 REGMBC(0x1af) REGMBC(0x1d3) REGMBC(0x1ee6)
957 return;
958 case 'V': CASEMBC(0x1e7c)
959 regmbc('V'); REGMBC(0x1e7c)
960 return;
961 case 'W': CASEMBC(0x174) CASEMBC(0x1e80) CASEMBC(0x1e82)
962 CASEMBC(0x1e84) CASEMBC(0x1e86)
963 regmbc('W'); REGMBC(0x174) REGMBC(0x1e80)
964 REGMBC(0x1e82) REGMBC(0x1e84) REGMBC(0x1e86)
965 return;
966 case 'X': CASEMBC(0x1e8a) CASEMBC(0x1e8c)
967 regmbc('X'); REGMBC(0x1e8a) REGMBC(0x1e8c)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000968 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200969 case 'Y': case 0xdd:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200970 CASEMBC(0x176) CASEMBC(0x178) CASEMBC(0x1e8e) CASEMBC(0x1ef2)
971 CASEMBC(0x1ef6) CASEMBC(0x1ef8)
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200972 regmbc('Y'); regmbc(0xdd);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200973 REGMBC(0x176) REGMBC(0x178) REGMBC(0x1e8e)
974 REGMBC(0x1ef2) REGMBC(0x1ef6) REGMBC(0x1ef8)
975 return;
976 case 'Z': CASEMBC(0x179) CASEMBC(0x17b) CASEMBC(0x17d)
977 CASEMBC(0x1b5) CASEMBC(0x1e90) CASEMBC(0x1e94)
978 regmbc('Z'); REGMBC(0x179) REGMBC(0x17b)
979 REGMBC(0x17d) REGMBC(0x1b5) REGMBC(0x1e90)
980 REGMBC(0x1e94)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000981 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200982 case 'a': case 0xe0: case 0xe1: case 0xe2:
983 case 0xe3: case 0xe4: case 0xe5:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200984 CASEMBC(0x101) CASEMBC(0x103) CASEMBC(0x105) CASEMBC(0x1ce)
985 CASEMBC(0x1df) CASEMBC(0x1e1) CASEMBC(0x1ea3)
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200986 regmbc('a'); regmbc(0xe0); regmbc(0xe1);
987 regmbc(0xe2); regmbc(0xe3); regmbc(0xe4);
988 regmbc(0xe5);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200989 REGMBC(0x101) REGMBC(0x103) REGMBC(0x105)
990 REGMBC(0x1ce) REGMBC(0x1df) REGMBC(0x1e1)
991 REGMBC(0x1ea3)
992 return;
993 case 'b': CASEMBC(0x1e03) CASEMBC(0x1e07)
994 regmbc('b'); REGMBC(0x1e03) REGMBC(0x1e07)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000995 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200996 case 'c': case 0xe7:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200997 CASEMBC(0x107) CASEMBC(0x109) CASEMBC(0x10b) CASEMBC(0x10d)
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200998 regmbc('c'); regmbc(0xe7);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200999 REGMBC(0x107) REGMBC(0x109) REGMBC(0x10b)
1000 REGMBC(0x10d)
1001 return;
Bram Moolenaar2c61ec62015-07-10 19:16:34 +02001002 case 'd': CASEMBC(0x10f) CASEMBC(0x111) CASEMBC(0x1e0b)
1003 CASEMBC(0x1e0f) CASEMBC(0x1e11)
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001004 regmbc('d'); REGMBC(0x10f) REGMBC(0x111)
Bram Moolenaar2c61ec62015-07-10 19:16:34 +02001005 REGMBC(0x1e0b) REGMBC(0x1e0f) REGMBC(0x1e11)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001006 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001007 case 'e': case 0xe8: case 0xe9: case 0xea: case 0xeb:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001008 CASEMBC(0x113) CASEMBC(0x115) CASEMBC(0x117) CASEMBC(0x119)
1009 CASEMBC(0x11b) CASEMBC(0x1ebb) CASEMBC(0x1ebd)
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001010 regmbc('e'); regmbc(0xe8); regmbc(0xe9);
1011 regmbc(0xea); regmbc(0xeb);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001012 REGMBC(0x113) REGMBC(0x115) REGMBC(0x117)
1013 REGMBC(0x119) REGMBC(0x11b) REGMBC(0x1ebb)
1014 REGMBC(0x1ebd)
1015 return;
1016 case 'f': CASEMBC(0x1e1f)
1017 regmbc('f'); REGMBC(0x1e1f)
1018 return;
1019 case 'g': CASEMBC(0x11d) CASEMBC(0x11f) CASEMBC(0x121)
1020 CASEMBC(0x123) CASEMBC(0x1e5) CASEMBC(0x1e7) CASEMBC(0x1f5)
1021 CASEMBC(0x1e21)
1022 regmbc('g'); REGMBC(0x11d) REGMBC(0x11f)
1023 REGMBC(0x121) REGMBC(0x123) REGMBC(0x1e5)
1024 REGMBC(0x1e7) REGMBC(0x1f5) REGMBC(0x1e21)
1025 return;
1026 case 'h': CASEMBC(0x125) CASEMBC(0x127) CASEMBC(0x1e23)
1027 CASEMBC(0x1e27) CASEMBC(0x1e29) CASEMBC(0x1e96)
1028 regmbc('h'); REGMBC(0x125) REGMBC(0x127)
1029 REGMBC(0x1e23) REGMBC(0x1e27) REGMBC(0x1e29)
1030 REGMBC(0x1e96)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001031 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001032 case 'i': case 0xec: case 0xed: case 0xee: case 0xef:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001033 CASEMBC(0x129) CASEMBC(0x12b) CASEMBC(0x12d) CASEMBC(0x12f)
1034 CASEMBC(0x1d0) CASEMBC(0x1ec9)
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001035 regmbc('i'); regmbc(0xec); regmbc(0xed);
1036 regmbc(0xee); regmbc(0xef);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001037 REGMBC(0x129) REGMBC(0x12b) REGMBC(0x12d)
1038 REGMBC(0x12f) REGMBC(0x1d0) REGMBC(0x1ec9)
1039 return;
1040 case 'j': CASEMBC(0x135) CASEMBC(0x1f0)
1041 regmbc('j'); REGMBC(0x135) REGMBC(0x1f0)
1042 return;
1043 case 'k': CASEMBC(0x137) CASEMBC(0x1e9) CASEMBC(0x1e31)
1044 CASEMBC(0x1e35)
1045 regmbc('k'); REGMBC(0x137) REGMBC(0x1e9)
1046 REGMBC(0x1e31) REGMBC(0x1e35)
1047 return;
1048 case 'l': CASEMBC(0x13a) CASEMBC(0x13c) CASEMBC(0x13e)
1049 CASEMBC(0x140) CASEMBC(0x142) CASEMBC(0x1e3b)
1050 regmbc('l'); REGMBC(0x13a) REGMBC(0x13c)
1051 REGMBC(0x13e) REGMBC(0x140) REGMBC(0x142)
1052 REGMBC(0x1e3b)
1053 return;
1054 case 'm': CASEMBC(0x1e3f) CASEMBC(0x1e41)
1055 regmbc('m'); REGMBC(0x1e3f) REGMBC(0x1e41)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001056 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001057 case 'n': case 0xf1:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001058 CASEMBC(0x144) CASEMBC(0x146) CASEMBC(0x148) CASEMBC(0x149)
1059 CASEMBC(0x1e45) CASEMBC(0x1e49)
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001060 regmbc('n'); regmbc(0xf1);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001061 REGMBC(0x144) REGMBC(0x146) REGMBC(0x148)
1062 REGMBC(0x149) REGMBC(0x1e45) REGMBC(0x1e49)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001063 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001064 case 'o': case 0xf2: case 0xf3: case 0xf4: case 0xf5:
1065 case 0xf6: case 0xf8:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001066 CASEMBC(0x14d) CASEMBC(0x14f) CASEMBC(0x151) CASEMBC(0x1a1)
1067 CASEMBC(0x1d2) CASEMBC(0x1eb) CASEMBC(0x1ed) CASEMBC(0x1ecf)
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001068 regmbc('o'); regmbc(0xf2); regmbc(0xf3);
1069 regmbc(0xf4); regmbc(0xf5); regmbc(0xf6);
1070 regmbc(0xf8);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001071 REGMBC(0x14d) REGMBC(0x14f) REGMBC(0x151)
1072 REGMBC(0x1a1) REGMBC(0x1d2) REGMBC(0x1eb)
1073 REGMBC(0x1ed) REGMBC(0x1ecf)
1074 return;
1075 case 'p': CASEMBC(0x1e55) CASEMBC(0x1e57)
1076 regmbc('p'); REGMBC(0x1e55) REGMBC(0x1e57)
1077 return;
1078 case 'r': CASEMBC(0x155) CASEMBC(0x157) CASEMBC(0x159)
1079 CASEMBC(0x1e59) CASEMBC(0x1e5f)
1080 regmbc('r'); REGMBC(0x155) REGMBC(0x157) REGMBC(0x159)
1081 REGMBC(0x1e59) REGMBC(0x1e5f)
1082 return;
1083 case 's': CASEMBC(0x15b) CASEMBC(0x15d) CASEMBC(0x15f)
1084 CASEMBC(0x161) CASEMBC(0x1e61)
1085 regmbc('s'); REGMBC(0x15b) REGMBC(0x15d)
1086 REGMBC(0x15f) REGMBC(0x161) REGMBC(0x1e61)
1087 return;
1088 case 't': CASEMBC(0x163) CASEMBC(0x165) CASEMBC(0x167)
1089 CASEMBC(0x1e6b) CASEMBC(0x1e6f) CASEMBC(0x1e97)
1090 regmbc('t'); REGMBC(0x163) REGMBC(0x165) REGMBC(0x167)
1091 REGMBC(0x1e6b) REGMBC(0x1e6f) REGMBC(0x1e97)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001092 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001093 case 'u': case 0xf9: case 0xfa: case 0xfb: case 0xfc:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001094 CASEMBC(0x169) CASEMBC(0x16b) CASEMBC(0x16d) CASEMBC(0x16f)
1095 CASEMBC(0x171) CASEMBC(0x173) CASEMBC(0x1b0) CASEMBC(0x1d4)
1096 CASEMBC(0x1ee7)
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001097 regmbc('u'); regmbc(0xf9); regmbc(0xfa);
1098 regmbc(0xfb); regmbc(0xfc);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001099 REGMBC(0x169) REGMBC(0x16b) REGMBC(0x16d)
1100 REGMBC(0x16f) REGMBC(0x171) REGMBC(0x173)
1101 REGMBC(0x1b0) REGMBC(0x1d4) REGMBC(0x1ee7)
1102 return;
1103 case 'v': CASEMBC(0x1e7d)
1104 regmbc('v'); REGMBC(0x1e7d)
1105 return;
1106 case 'w': CASEMBC(0x175) CASEMBC(0x1e81) CASEMBC(0x1e83)
1107 CASEMBC(0x1e85) CASEMBC(0x1e87) CASEMBC(0x1e98)
1108 regmbc('w'); REGMBC(0x175) REGMBC(0x1e81)
1109 REGMBC(0x1e83) REGMBC(0x1e85) REGMBC(0x1e87)
1110 REGMBC(0x1e98)
1111 return;
1112 case 'x': CASEMBC(0x1e8b) CASEMBC(0x1e8d)
1113 regmbc('x'); REGMBC(0x1e8b) REGMBC(0x1e8d)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001114 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001115 case 'y': case 0xfd: case 0xff:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001116 CASEMBC(0x177) CASEMBC(0x1e8f) CASEMBC(0x1e99)
1117 CASEMBC(0x1ef3) CASEMBC(0x1ef7) CASEMBC(0x1ef9)
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001118 regmbc('y'); regmbc(0xfd); regmbc(0xff);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001119 REGMBC(0x177) REGMBC(0x1e8f) REGMBC(0x1e99)
1120 REGMBC(0x1ef3) REGMBC(0x1ef7) REGMBC(0x1ef9)
1121 return;
1122 case 'z': CASEMBC(0x17a) CASEMBC(0x17c) CASEMBC(0x17e)
1123 CASEMBC(0x1b6) CASEMBC(0x1e91) CASEMBC(0x1e95)
1124 regmbc('z'); REGMBC(0x17a) REGMBC(0x17c)
1125 REGMBC(0x17e) REGMBC(0x1b6) REGMBC(0x1e91)
1126 REGMBC(0x1e95)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001127 return;
1128 }
Bram Moolenaar2c704a72010-06-03 21:17:25 +02001129#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00001130 }
1131 regmbc(c);
1132}
1133
1134/*
1135 * Check for a collating element "[.a.]". "pp" points to the '['.
1136 * Returns a character. Zero means that no item was recognized. Otherwise
1137 * "pp" is advanced to after the item.
1138 * Currently only single characters are recognized!
1139 */
1140 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001141get_coll_element(char_u **pp)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001142{
1143 int c;
1144 int l = 1;
1145 char_u *p = *pp;
1146
Bram Moolenaarb878bbb2015-06-09 20:39:24 +02001147 if (p[0] != NUL && p[1] == '.')
Bram Moolenaardf177f62005-02-22 08:39:57 +00001148 {
1149#ifdef FEAT_MBYTE
1150 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001151 l = (*mb_ptr2len)(p + 2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00001152#endif
1153 if (p[l + 2] == '.' && p[l + 3] == ']')
1154 {
1155#ifdef FEAT_MBYTE
1156 if (has_mbyte)
1157 c = mb_ptr2char(p + 2);
1158 else
1159#endif
1160 c = p[2];
1161 *pp += l + 4;
1162 return c;
1163 }
1164 }
1165 return 0;
1166}
1167
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001168static void get_cpo_flags(void);
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001169static int reg_cpo_lit; /* 'cpoptions' contains 'l' flag */
1170static int reg_cpo_bsl; /* 'cpoptions' contains '\' flag */
1171
1172 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001173get_cpo_flags(void)
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001174{
1175 reg_cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL;
1176 reg_cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL;
1177}
Bram Moolenaardf177f62005-02-22 08:39:57 +00001178
1179/*
1180 * Skip over a "[]" range.
1181 * "p" must point to the character after the '['.
1182 * The returned pointer is on the matching ']', or the terminating NUL.
1183 */
1184 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001185skip_anyof(char_u *p)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001186{
Bram Moolenaardf177f62005-02-22 08:39:57 +00001187#ifdef FEAT_MBYTE
1188 int l;
1189#endif
1190
Bram Moolenaardf177f62005-02-22 08:39:57 +00001191 if (*p == '^') /* Complement of range. */
1192 ++p;
1193 if (*p == ']' || *p == '-')
1194 ++p;
1195 while (*p != NUL && *p != ']')
1196 {
1197#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001198 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001199 p += l;
1200 else
1201#endif
1202 if (*p == '-')
1203 {
1204 ++p;
1205 if (*p != ']' && *p != NUL)
1206 mb_ptr_adv(p);
1207 }
1208 else if (*p == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001209 && !reg_cpo_bsl
Bram Moolenaardf177f62005-02-22 08:39:57 +00001210 && (vim_strchr(REGEXP_INRANGE, p[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001211 || (!reg_cpo_lit && vim_strchr(REGEXP_ABBR, p[1]) != NULL)))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001212 p += 2;
1213 else if (*p == '[')
1214 {
1215 if (get_char_class(&p) == CLASS_NONE
1216 && get_equi_class(&p) == 0
Bram Moolenaarb878bbb2015-06-09 20:39:24 +02001217 && get_coll_element(&p) == 0
1218 && *p != NUL)
1219 ++p; /* it is not a class name and not NUL */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001220 }
1221 else
1222 ++p;
1223 }
1224
1225 return p;
1226}
1227
1228/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 * Skip past regular expression.
Bram Moolenaar748bf032005-02-02 23:04:36 +00001230 * Stop at end of "startp" or where "dirc" is found ('/', '?', etc).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 * Take care of characters with a backslash in front of it.
1232 * Skip strings inside [ and ].
1233 * When "newp" is not NULL and "dirc" is '?', make an allocated copy of the
1234 * expression and change "\?" to "?". If "*newp" is not NULL the expression
1235 * is changed in-place.
1236 */
1237 char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001238skip_regexp(
1239 char_u *startp,
1240 int dirc,
1241 int magic,
1242 char_u **newp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243{
1244 int mymagic;
1245 char_u *p = startp;
1246
1247 if (magic)
1248 mymagic = MAGIC_ON;
1249 else
1250 mymagic = MAGIC_OFF;
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001251 get_cpo_flags();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001253 for (; p[0] != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 {
1255 if (p[0] == dirc) /* found end of regexp */
1256 break;
1257 if ((p[0] == '[' && mymagic >= MAGIC_ON)
1258 || (p[0] == '\\' && p[1] == '[' && mymagic <= MAGIC_OFF))
1259 {
1260 p = skip_anyof(p + 1);
1261 if (p[0] == NUL)
1262 break;
1263 }
1264 else if (p[0] == '\\' && p[1] != NUL)
1265 {
1266 if (dirc == '?' && newp != NULL && p[1] == '?')
1267 {
1268 /* change "\?" to "?", make a copy first. */
1269 if (*newp == NULL)
1270 {
1271 *newp = vim_strsave(startp);
1272 if (*newp != NULL)
1273 p = *newp + (p - startp);
1274 }
1275 if (*newp != NULL)
Bram Moolenaar446cb832008-06-24 21:56:24 +00001276 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 else
1278 ++p;
1279 }
1280 else
1281 ++p; /* skip next character */
1282 if (*p == 'v')
1283 mymagic = MAGIC_ALL;
1284 else if (*p == 'V')
1285 mymagic = MAGIC_NONE;
1286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 }
1288 return p;
1289}
1290
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001291static regprog_T *bt_regcomp(char_u *expr, int re_flags);
1292static void bt_regfree(regprog_T *prog);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001293
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001295 * bt_regcomp() - compile a regular expression into internal code for the
1296 * traditional back track matcher.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001297 * Returns the program in allocated space. Returns NULL for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 *
1299 * We can't allocate space until we know how big the compiled form will be,
1300 * but we can't compile it (and thus know how big it is) until we've got a
1301 * place to put the code. So we cheat: we compile it twice, once with code
1302 * generation turned off and size counting turned on, and once "for real".
1303 * This also means that we don't allocate space until we are sure that the
1304 * thing really will compile successfully, and we never have to move the
1305 * code and thus invalidate pointers into it. (Note that it has to be in
1306 * one piece because vim_free() must be able to free it all.)
1307 *
1308 * Whether upper/lower case is to be ignored is decided when executing the
1309 * program, it does not matter here.
1310 *
1311 * Beware that the optimization-preparation code in here knows about some
1312 * of the structure of the compiled regexp.
1313 * "re_flags": RE_MAGIC and/or RE_STRING.
1314 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001315 static regprog_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001316bt_regcomp(char_u *expr, int re_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001318 bt_regprog_T *r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 char_u *scan;
1320 char_u *longest;
1321 int len;
1322 int flags;
1323
1324 if (expr == NULL)
1325 EMSG_RET_NULL(_(e_null));
1326
1327 init_class_tab();
1328
1329 /*
1330 * First pass: determine size, legality.
1331 */
1332 regcomp_start(expr, re_flags);
1333 regcode = JUST_CALC_SIZE;
1334 regc(REGMAGIC);
1335 if (reg(REG_NOPAREN, &flags) == NULL)
1336 return NULL;
1337
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 /* Allocate space. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001339 r = (bt_regprog_T *)lalloc(sizeof(bt_regprog_T) + regsize, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 if (r == NULL)
1341 return NULL;
1342
1343 /*
1344 * Second pass: emit code.
1345 */
1346 regcomp_start(expr, re_flags);
1347 regcode = r->program;
1348 regc(REGMAGIC);
Bram Moolenaard3005802009-11-25 17:21:32 +00001349 if (reg(REG_NOPAREN, &flags) == NULL || reg_toolong)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 {
1351 vim_free(r);
Bram Moolenaard3005802009-11-25 17:21:32 +00001352 if (reg_toolong)
1353 EMSG_RET_NULL(_("E339: Pattern too long"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 return NULL;
1355 }
1356
1357 /* Dig out information for optimizations. */
1358 r->regstart = NUL; /* Worst-case defaults. */
1359 r->reganch = 0;
1360 r->regmust = NULL;
1361 r->regmlen = 0;
1362 r->regflags = regflags;
1363 if (flags & HASNL)
1364 r->regflags |= RF_HASNL;
1365 if (flags & HASLOOKBH)
1366 r->regflags |= RF_LOOKBH;
1367#ifdef FEAT_SYN_HL
1368 /* Remember whether this pattern has any \z specials in it. */
1369 r->reghasz = re_has_z;
1370#endif
1371 scan = r->program + 1; /* First BRANCH. */
1372 if (OP(regnext(scan)) == END) /* Only one top-level choice. */
1373 {
1374 scan = OPERAND(scan);
1375
1376 /* Starting-point info. */
1377 if (OP(scan) == BOL || OP(scan) == RE_BOF)
1378 {
1379 r->reganch++;
1380 scan = regnext(scan);
1381 }
1382
1383 if (OP(scan) == EXACTLY)
1384 {
1385#ifdef FEAT_MBYTE
1386 if (has_mbyte)
1387 r->regstart = (*mb_ptr2char)(OPERAND(scan));
1388 else
1389#endif
1390 r->regstart = *OPERAND(scan);
1391 }
1392 else if ((OP(scan) == BOW
1393 || OP(scan) == EOW
1394 || OP(scan) == NOTHING
1395 || OP(scan) == MOPEN + 0 || OP(scan) == NOPEN
1396 || OP(scan) == MCLOSE + 0 || OP(scan) == NCLOSE)
1397 && OP(regnext(scan)) == EXACTLY)
1398 {
1399#ifdef FEAT_MBYTE
1400 if (has_mbyte)
1401 r->regstart = (*mb_ptr2char)(OPERAND(regnext(scan)));
1402 else
1403#endif
1404 r->regstart = *OPERAND(regnext(scan));
1405 }
1406
1407 /*
1408 * If there's something expensive in the r.e., find the longest
1409 * literal string that must appear and make it the regmust. Resolve
1410 * ties in favor of later strings, since the regstart check works
1411 * with the beginning of the r.e. and avoiding duplication
1412 * strengthens checking. Not a strong reason, but sufficient in the
1413 * absence of others.
1414 */
1415 /*
1416 * When the r.e. starts with BOW, it is faster to look for a regmust
1417 * first. Used a lot for "#" and "*" commands. (Added by mool).
1418 */
1419 if ((flags & SPSTART || OP(scan) == BOW || OP(scan) == EOW)
1420 && !(flags & HASNL))
1421 {
1422 longest = NULL;
1423 len = 0;
1424 for (; scan != NULL; scan = regnext(scan))
1425 if (OP(scan) == EXACTLY && STRLEN(OPERAND(scan)) >= (size_t)len)
1426 {
1427 longest = OPERAND(scan);
1428 len = (int)STRLEN(OPERAND(scan));
1429 }
1430 r->regmust = longest;
1431 r->regmlen = len;
1432 }
1433 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001434#ifdef BT_REGEXP_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 regdump(expr, r);
1436#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001437 r->engine = &bt_regengine;
1438 return (regprog_T *)r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439}
1440
1441/*
Bram Moolenaar473de612013-06-08 18:19:48 +02001442 * Free a compiled regexp program, returned by bt_regcomp().
1443 */
1444 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001445bt_regfree(regprog_T *prog)
Bram Moolenaar473de612013-06-08 18:19:48 +02001446{
1447 vim_free(prog);
1448}
1449
1450/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 * Setup to parse the regexp. Used once to get the length and once to do it.
1452 */
1453 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001454regcomp_start(
1455 char_u *expr,
1456 int re_flags) /* see vim_regcomp() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457{
1458 initchr(expr);
1459 if (re_flags & RE_MAGIC)
1460 reg_magic = MAGIC_ON;
1461 else
1462 reg_magic = MAGIC_OFF;
1463 reg_string = (re_flags & RE_STRING);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001464 reg_strict = (re_flags & RE_STRICT);
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001465 get_cpo_flags();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466
1467 num_complex_braces = 0;
1468 regnpar = 1;
1469 vim_memset(had_endbrace, 0, sizeof(had_endbrace));
1470#ifdef FEAT_SYN_HL
1471 regnzpar = 1;
1472 re_has_z = 0;
1473#endif
1474 regsize = 0L;
Bram Moolenaard3005802009-11-25 17:21:32 +00001475 reg_toolong = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 regflags = 0;
1477#if defined(FEAT_SYN_HL) || defined(PROTO)
1478 had_eol = FALSE;
1479#endif
1480}
1481
1482#if defined(FEAT_SYN_HL) || defined(PROTO)
1483/*
1484 * Check if during the previous call to vim_regcomp the EOL item "$" has been
1485 * found. This is messy, but it works fine.
1486 */
1487 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001488vim_regcomp_had_eol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489{
1490 return had_eol;
1491}
1492#endif
1493
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001494/* variables for parsing reginput */
1495static int at_start; /* True when on the first character */
1496static int prev_at_start; /* True when on the second character */
1497
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001499 * Parse regular expression, i.e. main body or parenthesized thing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 *
1501 * Caller must absorb opening parenthesis.
1502 *
1503 * Combining parenthesis handling with the base level of regular expression
1504 * is a trifle forced, but the need to tie the tails of the branches to what
1505 * follows makes it hard to avoid.
1506 */
1507 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001508reg(
1509 int paren, /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1510 int *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511{
1512 char_u *ret;
1513 char_u *br;
1514 char_u *ender;
1515 int parno = 0;
1516 int flags;
1517
1518 *flagp = HASWIDTH; /* Tentatively. */
1519
1520#ifdef FEAT_SYN_HL
1521 if (paren == REG_ZPAREN)
1522 {
1523 /* Make a ZOPEN node. */
1524 if (regnzpar >= NSUBEXP)
1525 EMSG_RET_NULL(_("E50: Too many \\z("));
1526 parno = regnzpar;
1527 regnzpar++;
1528 ret = regnode(ZOPEN + parno);
1529 }
1530 else
1531#endif
1532 if (paren == REG_PAREN)
1533 {
1534 /* Make a MOPEN node. */
1535 if (regnpar >= NSUBEXP)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001536 EMSG2_RET_NULL(_("E51: Too many %s("), reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 parno = regnpar;
1538 ++regnpar;
1539 ret = regnode(MOPEN + parno);
1540 }
1541 else if (paren == REG_NPAREN)
1542 {
1543 /* Make a NOPEN node. */
1544 ret = regnode(NOPEN);
1545 }
1546 else
1547 ret = NULL;
1548
1549 /* Pick up the branches, linking them together. */
1550 br = regbranch(&flags);
1551 if (br == NULL)
1552 return NULL;
1553 if (ret != NULL)
1554 regtail(ret, br); /* [MZ]OPEN -> first. */
1555 else
1556 ret = br;
1557 /* If one of the branches can be zero-width, the whole thing can.
1558 * If one of the branches has * at start or matches a line-break, the
1559 * whole thing can. */
1560 if (!(flags & HASWIDTH))
1561 *flagp &= ~HASWIDTH;
1562 *flagp |= flags & (SPSTART | HASNL | HASLOOKBH);
1563 while (peekchr() == Magic('|'))
1564 {
1565 skipchr();
1566 br = regbranch(&flags);
Bram Moolenaard3005802009-11-25 17:21:32 +00001567 if (br == NULL || reg_toolong)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 return NULL;
1569 regtail(ret, br); /* BRANCH -> BRANCH. */
1570 if (!(flags & HASWIDTH))
1571 *flagp &= ~HASWIDTH;
1572 *flagp |= flags & (SPSTART | HASNL | HASLOOKBH);
1573 }
1574
1575 /* Make a closing node, and hook it on the end. */
1576 ender = regnode(
1577#ifdef FEAT_SYN_HL
1578 paren == REG_ZPAREN ? ZCLOSE + parno :
1579#endif
1580 paren == REG_PAREN ? MCLOSE + parno :
1581 paren == REG_NPAREN ? NCLOSE : END);
1582 regtail(ret, ender);
1583
1584 /* Hook the tails of the branches to the closing node. */
1585 for (br = ret; br != NULL; br = regnext(br))
1586 regoptail(br, ender);
1587
1588 /* Check for proper termination. */
1589 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1590 {
1591#ifdef FEAT_SYN_HL
1592 if (paren == REG_ZPAREN)
Bram Moolenaar45eeb132005-06-06 21:59:07 +00001593 EMSG_RET_NULL(_("E52: Unmatched \\z("));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 else
1595#endif
1596 if (paren == REG_NPAREN)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001597 EMSG2_RET_NULL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001599 EMSG2_RET_NULL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 }
1601 else if (paren == REG_NOPAREN && peekchr() != NUL)
1602 {
1603 if (curchr == Magic(')'))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001604 EMSG2_RET_NULL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 else
Bram Moolenaar45eeb132005-06-06 21:59:07 +00001606 EMSG_RET_NULL(_(e_trailing)); /* "Can't happen". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 /* NOTREACHED */
1608 }
1609 /*
1610 * Here we set the flag allowing back references to this set of
1611 * parentheses.
1612 */
1613 if (paren == REG_PAREN)
1614 had_endbrace[parno] = TRUE; /* have seen the close paren */
1615 return ret;
1616}
1617
1618/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001619 * Parse one alternative of an | operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 * Implements the & operator.
1621 */
1622 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001623regbranch(int *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624{
1625 char_u *ret;
1626 char_u *chain = NULL;
1627 char_u *latest;
1628 int flags;
1629
1630 *flagp = WORST | HASNL; /* Tentatively. */
1631
1632 ret = regnode(BRANCH);
1633 for (;;)
1634 {
1635 latest = regconcat(&flags);
1636 if (latest == NULL)
1637 return NULL;
1638 /* If one of the branches has width, the whole thing has. If one of
1639 * the branches anchors at start-of-line, the whole thing does.
1640 * If one of the branches uses look-behind, the whole thing does. */
1641 *flagp |= flags & (HASWIDTH | SPSTART | HASLOOKBH);
1642 /* If one of the branches doesn't match a line-break, the whole thing
1643 * doesn't. */
1644 *flagp &= ~HASNL | (flags & HASNL);
1645 if (chain != NULL)
1646 regtail(chain, latest);
1647 if (peekchr() != Magic('&'))
1648 break;
1649 skipchr();
1650 regtail(latest, regnode(END)); /* operand ends */
Bram Moolenaard3005802009-11-25 17:21:32 +00001651 if (reg_toolong)
1652 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 reginsert(MATCH, latest);
1654 chain = latest;
1655 }
1656
1657 return ret;
1658}
1659
1660/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001661 * Parse one alternative of an | or & operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 * Implements the concatenation operator.
1663 */
1664 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001665regconcat(int *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666{
1667 char_u *first = NULL;
1668 char_u *chain = NULL;
1669 char_u *latest;
1670 int flags;
1671 int cont = TRUE;
1672
1673 *flagp = WORST; /* Tentatively. */
1674
1675 while (cont)
1676 {
1677 switch (peekchr())
1678 {
1679 case NUL:
1680 case Magic('|'):
1681 case Magic('&'):
1682 case Magic(')'):
1683 cont = FALSE;
1684 break;
1685 case Magic('Z'):
1686#ifdef FEAT_MBYTE
1687 regflags |= RF_ICOMBINE;
1688#endif
1689 skipchr_keepstart();
1690 break;
1691 case Magic('c'):
1692 regflags |= RF_ICASE;
1693 skipchr_keepstart();
1694 break;
1695 case Magic('C'):
1696 regflags |= RF_NOICASE;
1697 skipchr_keepstart();
1698 break;
1699 case Magic('v'):
1700 reg_magic = MAGIC_ALL;
1701 skipchr_keepstart();
1702 curchr = -1;
1703 break;
1704 case Magic('m'):
1705 reg_magic = MAGIC_ON;
1706 skipchr_keepstart();
1707 curchr = -1;
1708 break;
1709 case Magic('M'):
1710 reg_magic = MAGIC_OFF;
1711 skipchr_keepstart();
1712 curchr = -1;
1713 break;
1714 case Magic('V'):
1715 reg_magic = MAGIC_NONE;
1716 skipchr_keepstart();
1717 curchr = -1;
1718 break;
1719 default:
1720 latest = regpiece(&flags);
Bram Moolenaard3005802009-11-25 17:21:32 +00001721 if (latest == NULL || reg_toolong)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 return NULL;
1723 *flagp |= flags & (HASWIDTH | HASNL | HASLOOKBH);
1724 if (chain == NULL) /* First piece. */
1725 *flagp |= flags & SPSTART;
1726 else
1727 regtail(chain, latest);
1728 chain = latest;
1729 if (first == NULL)
1730 first = latest;
1731 break;
1732 }
1733 }
1734 if (first == NULL) /* Loop ran zero times. */
1735 first = regnode(NOTHING);
1736 return first;
1737}
1738
1739/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001740 * Parse something followed by possible [*+=].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 *
1742 * Note that the branching code sequences used for = and the general cases
1743 * of * and + are somewhat optimized: they use the same NOTHING node as
1744 * both the endmarker for their branch list and the body of the last branch.
1745 * It might seem that this node could be dispensed with entirely, but the
1746 * endmarker role is not redundant.
1747 */
1748 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001749regpiece(int *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750{
1751 char_u *ret;
1752 int op;
1753 char_u *next;
1754 int flags;
1755 long minval;
1756 long maxval;
1757
1758 ret = regatom(&flags);
1759 if (ret == NULL)
1760 return NULL;
1761
1762 op = peekchr();
1763 if (re_multi_type(op) == NOT_MULTI)
1764 {
1765 *flagp = flags;
1766 return ret;
1767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 /* default flags */
1769 *flagp = (WORST | SPSTART | (flags & (HASNL | HASLOOKBH)));
1770
1771 skipchr();
1772 switch (op)
1773 {
1774 case Magic('*'):
1775 if (flags & SIMPLE)
1776 reginsert(STAR, ret);
1777 else
1778 {
1779 /* Emit x* as (x&|), where & means "self". */
1780 reginsert(BRANCH, ret); /* Either x */
1781 regoptail(ret, regnode(BACK)); /* and loop */
1782 regoptail(ret, ret); /* back */
1783 regtail(ret, regnode(BRANCH)); /* or */
1784 regtail(ret, regnode(NOTHING)); /* null. */
1785 }
1786 break;
1787
1788 case Magic('+'):
1789 if (flags & SIMPLE)
1790 reginsert(PLUS, ret);
1791 else
1792 {
1793 /* Emit x+ as x(&|), where & means "self". */
1794 next = regnode(BRANCH); /* Either */
1795 regtail(ret, next);
Bram Moolenaar582fd852005-03-28 20:58:01 +00001796 regtail(regnode(BACK), ret); /* loop back */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 regtail(next, regnode(BRANCH)); /* or */
1798 regtail(ret, regnode(NOTHING)); /* null. */
1799 }
1800 *flagp = (WORST | HASWIDTH | (flags & (HASNL | HASLOOKBH)));
1801 break;
1802
1803 case Magic('@'):
1804 {
1805 int lop = END;
Bram Moolenaar75eb1612013-05-29 18:45:11 +02001806 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807
Bram Moolenaar75eb1612013-05-29 18:45:11 +02001808 nr = getdecchrs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 switch (no_Magic(getchr()))
1810 {
1811 case '=': lop = MATCH; break; /* \@= */
1812 case '!': lop = NOMATCH; break; /* \@! */
1813 case '>': lop = SUBPAT; break; /* \@> */
1814 case '<': switch (no_Magic(getchr()))
1815 {
1816 case '=': lop = BEHIND; break; /* \@<= */
1817 case '!': lop = NOBEHIND; break; /* \@<! */
1818 }
1819 }
1820 if (lop == END)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001821 EMSG2_RET_NULL(_("E59: invalid character after %s@"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 reg_magic == MAGIC_ALL);
1823 /* Look behind must match with behind_pos. */
1824 if (lop == BEHIND || lop == NOBEHIND)
1825 {
1826 regtail(ret, regnode(BHPOS));
1827 *flagp |= HASLOOKBH;
1828 }
1829 regtail(ret, regnode(END)); /* operand ends */
Bram Moolenaar75eb1612013-05-29 18:45:11 +02001830 if (lop == BEHIND || lop == NOBEHIND)
1831 {
1832 if (nr < 0)
1833 nr = 0; /* no limit is same as zero limit */
1834 reginsert_nr(lop, nr, ret);
1835 }
1836 else
1837 reginsert(lop, ret);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 break;
1839 }
1840
1841 case Magic('?'):
1842 case Magic('='):
1843 /* Emit x= as (x|) */
1844 reginsert(BRANCH, ret); /* Either x */
1845 regtail(ret, regnode(BRANCH)); /* or */
1846 next = regnode(NOTHING); /* null. */
1847 regtail(ret, next);
1848 regoptail(ret, next);
1849 break;
1850
1851 case Magic('{'):
1852 if (!read_limits(&minval, &maxval))
1853 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 if (flags & SIMPLE)
1855 {
1856 reginsert(BRACE_SIMPLE, ret);
1857 reginsert_limits(BRACE_LIMITS, minval, maxval, ret);
1858 }
1859 else
1860 {
1861 if (num_complex_braces >= 10)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001862 EMSG2_RET_NULL(_("E60: Too many complex %s{...}s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 reg_magic == MAGIC_ALL);
1864 reginsert(BRACE_COMPLEX + num_complex_braces, ret);
1865 regoptail(ret, regnode(BACK));
1866 regoptail(ret, ret);
1867 reginsert_limits(BRACE_LIMITS, minval, maxval, ret);
1868 ++num_complex_braces;
1869 }
1870 if (minval > 0 && maxval > 0)
1871 *flagp = (HASWIDTH | (flags & (HASNL | HASLOOKBH)));
1872 break;
1873 }
1874 if (re_multi_type(peekchr()) != NOT_MULTI)
1875 {
1876 /* Can't have a multi follow a multi. */
1877 if (peekchr() == Magic('*'))
1878 sprintf((char *)IObuff, _("E61: Nested %s*"),
1879 reg_magic >= MAGIC_ON ? "" : "\\");
1880 else
1881 sprintf((char *)IObuff, _("E62: Nested %s%c"),
1882 reg_magic == MAGIC_ALL ? "" : "\\", no_Magic(peekchr()));
1883 EMSG_RET_NULL(IObuff);
1884 }
1885
1886 return ret;
1887}
1888
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001889/* When making changes to classchars also change nfa_classcodes. */
1890static char_u *classchars = (char_u *)".iIkKfFpPsSdDxXoOwWhHaAlLuU";
1891static int classcodes[] = {
1892 ANY, IDENT, SIDENT, KWORD, SKWORD,
1893 FNAME, SFNAME, PRINT, SPRINT,
1894 WHITE, NWHITE, DIGIT, NDIGIT,
1895 HEX, NHEX, OCTAL, NOCTAL,
1896 WORD, NWORD, HEAD, NHEAD,
1897 ALPHA, NALPHA, LOWER, NLOWER,
1898 UPPER, NUPPER
1899};
1900
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001902 * Parse the lowest level.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 *
1904 * Optimization: gobbles an entire sequence of ordinary characters so that
1905 * it can turn them into a single node, which is smaller to store and
1906 * faster to run. Don't do this when one_exactly is set.
1907 */
1908 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001909regatom(int *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910{
1911 char_u *ret;
1912 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 char_u *p;
1915 int extra = 0;
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001916 int save_prev_at_start = prev_at_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917
1918 *flagp = WORST; /* Tentatively. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919
1920 c = getchr();
1921 switch (c)
1922 {
1923 case Magic('^'):
1924 ret = regnode(BOL);
1925 break;
1926
1927 case Magic('$'):
1928 ret = regnode(EOL);
1929#if defined(FEAT_SYN_HL) || defined(PROTO)
1930 had_eol = TRUE;
1931#endif
1932 break;
1933
1934 case Magic('<'):
1935 ret = regnode(BOW);
1936 break;
1937
1938 case Magic('>'):
1939 ret = regnode(EOW);
1940 break;
1941
1942 case Magic('_'):
1943 c = no_Magic(getchr());
1944 if (c == '^') /* "\_^" is start-of-line */
1945 {
1946 ret = regnode(BOL);
1947 break;
1948 }
1949 if (c == '$') /* "\_$" is end-of-line */
1950 {
1951 ret = regnode(EOL);
1952#if defined(FEAT_SYN_HL) || defined(PROTO)
1953 had_eol = TRUE;
1954#endif
1955 break;
1956 }
1957
1958 extra = ADD_NL;
1959 *flagp |= HASNL;
1960
1961 /* "\_[" is character range plus newline */
1962 if (c == '[')
1963 goto collection;
1964
1965 /* "\_x" is character class plus newline */
1966 /*FALLTHROUGH*/
1967
1968 /*
1969 * Character classes.
1970 */
1971 case Magic('.'):
1972 case Magic('i'):
1973 case Magic('I'):
1974 case Magic('k'):
1975 case Magic('K'):
1976 case Magic('f'):
1977 case Magic('F'):
1978 case Magic('p'):
1979 case Magic('P'):
1980 case Magic('s'):
1981 case Magic('S'):
1982 case Magic('d'):
1983 case Magic('D'):
1984 case Magic('x'):
1985 case Magic('X'):
1986 case Magic('o'):
1987 case Magic('O'):
1988 case Magic('w'):
1989 case Magic('W'):
1990 case Magic('h'):
1991 case Magic('H'):
1992 case Magic('a'):
1993 case Magic('A'):
1994 case Magic('l'):
1995 case Magic('L'):
1996 case Magic('u'):
1997 case Magic('U'):
1998 p = vim_strchr(classchars, no_Magic(c));
1999 if (p == NULL)
2000 EMSG_RET_NULL(_("E63: invalid use of \\_"));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002001#ifdef FEAT_MBYTE
2002 /* When '.' is followed by a composing char ignore the dot, so that
2003 * the composing char is matched here. */
2004 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
2005 {
2006 c = getchr();
2007 goto do_multibyte;
2008 }
2009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 ret = regnode(classcodes[p - classchars] + extra);
2011 *flagp |= HASWIDTH | SIMPLE;
2012 break;
2013
2014 case Magic('n'):
2015 if (reg_string)
2016 {
2017 /* In a string "\n" matches a newline character. */
2018 ret = regnode(EXACTLY);
2019 regc(NL);
2020 regc(NUL);
2021 *flagp |= HASWIDTH | SIMPLE;
2022 }
2023 else
2024 {
2025 /* In buffer text "\n" matches the end of a line. */
2026 ret = regnode(NEWL);
2027 *flagp |= HASWIDTH | HASNL;
2028 }
2029 break;
2030
2031 case Magic('('):
2032 if (one_exactly)
2033 EMSG_ONE_RET_NULL;
2034 ret = reg(REG_PAREN, &flags);
2035 if (ret == NULL)
2036 return NULL;
2037 *flagp |= flags & (HASWIDTH | SPSTART | HASNL | HASLOOKBH);
2038 break;
2039
2040 case NUL:
2041 case Magic('|'):
2042 case Magic('&'):
2043 case Magic(')'):
Bram Moolenaard4210772008-01-02 14:35:30 +00002044 if (one_exactly)
2045 EMSG_ONE_RET_NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 EMSG_RET_NULL(_(e_internal)); /* Supposed to be caught earlier. */
2047 /* NOTREACHED */
2048
2049 case Magic('='):
2050 case Magic('?'):
2051 case Magic('+'):
2052 case Magic('@'):
2053 case Magic('{'):
2054 case Magic('*'):
2055 c = no_Magic(c);
2056 sprintf((char *)IObuff, _("E64: %s%c follows nothing"),
2057 (c == '*' ? reg_magic >= MAGIC_ON : reg_magic == MAGIC_ALL)
2058 ? "" : "\\", c);
2059 EMSG_RET_NULL(IObuff);
2060 /* NOTREACHED */
2061
2062 case Magic('~'): /* previous substitute pattern */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002063 if (reg_prev_sub != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 {
2065 char_u *lp;
2066
2067 ret = regnode(EXACTLY);
2068 lp = reg_prev_sub;
2069 while (*lp != NUL)
2070 regc(*lp++);
2071 regc(NUL);
2072 if (*reg_prev_sub != NUL)
2073 {
2074 *flagp |= HASWIDTH;
2075 if ((lp - reg_prev_sub) == 1)
2076 *flagp |= SIMPLE;
2077 }
2078 }
2079 else
2080 EMSG_RET_NULL(_(e_nopresub));
2081 break;
2082
2083 case Magic('1'):
2084 case Magic('2'):
2085 case Magic('3'):
2086 case Magic('4'):
2087 case Magic('5'):
2088 case Magic('6'):
2089 case Magic('7'):
2090 case Magic('8'):
2091 case Magic('9'):
2092 {
2093 int refnum;
2094
2095 refnum = c - Magic('0');
2096 /*
2097 * Check if the back reference is legal. We must have seen the
2098 * close brace.
2099 * TODO: Should also check that we don't refer to something
2100 * that is repeated (+*=): what instance of the repetition
2101 * should we match?
2102 */
2103 if (!had_endbrace[refnum])
2104 {
2105 /* Trick: check if "@<=" or "@<!" follows, in which case
2106 * the \1 can appear before the referenced match. */
2107 for (p = regparse; *p != NUL; ++p)
2108 if (p[0] == '@' && p[1] == '<'
2109 && (p[2] == '!' || p[2] == '='))
2110 break;
2111 if (*p == NUL)
2112 EMSG_RET_NULL(_("E65: Illegal back reference"));
2113 }
2114 ret = regnode(BACKREF + refnum);
2115 }
2116 break;
2117
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 case Magic('z'):
2119 {
2120 c = no_Magic(getchr());
2121 switch (c)
2122 {
Bram Moolenaarc4956c82006-03-12 21:58:43 +00002123#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 case '(': if (reg_do_extmatch != REX_SET)
Bram Moolenaar5de820b2013-06-02 15:01:57 +02002125 EMSG_RET_NULL(_(e_z_not_allowed));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 if (one_exactly)
2127 EMSG_ONE_RET_NULL;
2128 ret = reg(REG_ZPAREN, &flags);
2129 if (ret == NULL)
2130 return NULL;
2131 *flagp |= flags & (HASWIDTH|SPSTART|HASNL|HASLOOKBH);
2132 re_has_z = REX_SET;
2133 break;
2134
2135 case '1':
2136 case '2':
2137 case '3':
2138 case '4':
2139 case '5':
2140 case '6':
2141 case '7':
2142 case '8':
2143 case '9': if (reg_do_extmatch != REX_USE)
Bram Moolenaar5de820b2013-06-02 15:01:57 +02002144 EMSG_RET_NULL(_(e_z1_not_allowed));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 ret = regnode(ZREF + c - '0');
2146 re_has_z = REX_USE;
2147 break;
Bram Moolenaarc4956c82006-03-12 21:58:43 +00002148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149
2150 case 's': ret = regnode(MOPEN + 0);
Bram Moolenaarfb031402014-09-09 17:18:49 +02002151 if (re_mult_next("\\zs") == FAIL)
2152 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 break;
2154
2155 case 'e': ret = regnode(MCLOSE + 0);
Bram Moolenaarfb031402014-09-09 17:18:49 +02002156 if (re_mult_next("\\ze") == FAIL)
2157 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 break;
2159
2160 default: EMSG_RET_NULL(_("E68: Invalid character after \\z"));
2161 }
2162 }
2163 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164
2165 case Magic('%'):
2166 {
2167 c = no_Magic(getchr());
2168 switch (c)
2169 {
2170 /* () without a back reference */
2171 case '(':
2172 if (one_exactly)
2173 EMSG_ONE_RET_NULL;
2174 ret = reg(REG_NPAREN, &flags);
2175 if (ret == NULL)
2176 return NULL;
2177 *flagp |= flags & (HASWIDTH | SPSTART | HASNL | HASLOOKBH);
2178 break;
2179
2180 /* Catch \%^ and \%$ regardless of where they appear in the
2181 * pattern -- regardless of whether or not it makes sense. */
2182 case '^':
2183 ret = regnode(RE_BOF);
2184 break;
2185
2186 case '$':
2187 ret = regnode(RE_EOF);
2188 break;
2189
2190 case '#':
2191 ret = regnode(CURSOR);
2192 break;
2193
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00002194 case 'V':
2195 ret = regnode(RE_VISUAL);
2196 break;
2197
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02002198 case 'C':
2199 ret = regnode(RE_COMPOSING);
2200 break;
2201
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 /* \%[abc]: Emit as a list of branches, all ending at the last
2203 * branch which matches nothing. */
2204 case '[':
2205 if (one_exactly) /* doesn't nest */
2206 EMSG_ONE_RET_NULL;
2207 {
2208 char_u *lastbranch;
2209 char_u *lastnode = NULL;
2210 char_u *br;
2211
2212 ret = NULL;
2213 while ((c = getchr()) != ']')
2214 {
2215 if (c == NUL)
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02002216 EMSG2_RET_NULL(_(e_missing_sb),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 reg_magic == MAGIC_ALL);
2218 br = regnode(BRANCH);
2219 if (ret == NULL)
2220 ret = br;
2221 else
2222 regtail(lastnode, br);
2223
2224 ungetchr();
2225 one_exactly = TRUE;
2226 lastnode = regatom(flagp);
2227 one_exactly = FALSE;
2228 if (lastnode == NULL)
2229 return NULL;
2230 }
2231 if (ret == NULL)
Bram Moolenaar2976c022013-06-05 21:30:37 +02002232 EMSG2_RET_NULL(_(e_empty_sb),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 reg_magic == MAGIC_ALL);
2234 lastbranch = regnode(BRANCH);
2235 br = regnode(NOTHING);
2236 if (ret != JUST_CALC_SIZE)
2237 {
2238 regtail(lastnode, br);
2239 regtail(lastbranch, br);
2240 /* connect all branches to the NOTHING
2241 * branch at the end */
2242 for (br = ret; br != lastnode; )
2243 {
2244 if (OP(br) == BRANCH)
2245 {
2246 regtail(br, lastbranch);
2247 br = OPERAND(br);
2248 }
2249 else
2250 br = regnext(br);
2251 }
2252 }
Bram Moolenaara6404a42008-08-08 11:45:39 +00002253 *flagp &= ~(HASWIDTH | SIMPLE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 break;
2255 }
2256
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002257 case 'd': /* %d123 decimal */
2258 case 'o': /* %o123 octal */
2259 case 'x': /* %xab hex 2 */
2260 case 'u': /* %uabcd hex 4 */
2261 case 'U': /* %U1234abcd hex 8 */
2262 {
2263 int i;
2264
2265 switch (c)
2266 {
2267 case 'd': i = getdecchrs(); break;
2268 case 'o': i = getoctchrs(); break;
2269 case 'x': i = gethexchrs(2); break;
2270 case 'u': i = gethexchrs(4); break;
2271 case 'U': i = gethexchrs(8); break;
2272 default: i = -1; break;
2273 }
2274
2275 if (i < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002276 EMSG2_RET_NULL(
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002277 _("E678: Invalid character after %s%%[dxouU]"),
2278 reg_magic == MAGIC_ALL);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002279#ifdef FEAT_MBYTE
2280 if (use_multibytecode(i))
2281 ret = regnode(MULTIBYTECODE);
2282 else
2283#endif
2284 ret = regnode(EXACTLY);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002285 if (i == 0)
2286 regc(0x0a);
2287 else
2288#ifdef FEAT_MBYTE
2289 regmbc(i);
2290#else
2291 regc(i);
2292#endif
2293 regc(NUL);
2294 *flagp |= HASWIDTH;
2295 break;
2296 }
2297
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 default:
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00002299 if (VIM_ISDIGIT(c) || c == '<' || c == '>'
2300 || c == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 {
2302 long_u n = 0;
2303 int cmp;
2304
2305 cmp = c;
2306 if (cmp == '<' || cmp == '>')
2307 c = getchr();
2308 while (VIM_ISDIGIT(c))
2309 {
2310 n = n * 10 + (c - '0');
2311 c = getchr();
2312 }
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00002313 if (c == '\'' && n == 0)
2314 {
2315 /* "\%'m", "\%<'m" and "\%>'m": Mark */
2316 c = getchr();
2317 ret = regnode(RE_MARK);
2318 if (ret == JUST_CALC_SIZE)
2319 regsize += 2;
2320 else
2321 {
2322 *regcode++ = c;
2323 *regcode++ = cmp;
2324 }
2325 break;
2326 }
2327 else if (c == 'l' || c == 'c' || c == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 {
2329 if (c == 'l')
Bram Moolenaar7c29f382016-02-12 19:08:15 +01002330 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 ret = regnode(RE_LNUM);
Bram Moolenaar7c29f382016-02-12 19:08:15 +01002332 if (save_prev_at_start)
2333 at_start = TRUE;
2334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 else if (c == 'c')
2336 ret = regnode(RE_COL);
2337 else
2338 ret = regnode(RE_VCOL);
2339 if (ret == JUST_CALC_SIZE)
2340 regsize += 5;
2341 else
2342 {
2343 /* put the number and the optional
2344 * comparator after the opcode */
2345 regcode = re_put_long(regcode, n);
2346 *regcode++ = cmp;
2347 }
2348 break;
2349 }
2350 }
2351
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002352 EMSG2_RET_NULL(_("E71: Invalid character after %s%%"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 reg_magic == MAGIC_ALL);
2354 }
2355 }
2356 break;
2357
2358 case Magic('['):
2359collection:
2360 {
2361 char_u *lp;
2362
2363 /*
2364 * If there is no matching ']', we assume the '[' is a normal
2365 * character. This makes 'incsearch' and ":help [" work.
2366 */
2367 lp = skip_anyof(regparse);
2368 if (*lp == ']') /* there is a matching ']' */
2369 {
2370 int startc = -1; /* > 0 when next '-' is a range */
2371 int endc;
2372
2373 /*
2374 * In a character class, different parsing rules apply.
2375 * Not even \ is special anymore, nothing is.
2376 */
2377 if (*regparse == '^') /* Complement of range. */
2378 {
2379 ret = regnode(ANYBUT + extra);
2380 regparse++;
2381 }
2382 else
2383 ret = regnode(ANYOF + extra);
2384
2385 /* At the start ']' and '-' mean the literal character. */
2386 if (*regparse == ']' || *regparse == '-')
Bram Moolenaardf177f62005-02-22 08:39:57 +00002387 {
2388 startc = *regparse;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 regc(*regparse++);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391
2392 while (*regparse != NUL && *regparse != ']')
2393 {
2394 if (*regparse == '-')
2395 {
2396 ++regparse;
2397 /* The '-' is not used for a range at the end and
2398 * after or before a '\n'. */
2399 if (*regparse == ']' || *regparse == NUL
2400 || startc == -1
2401 || (regparse[0] == '\\' && regparse[1] == 'n'))
2402 {
2403 regc('-');
2404 startc = '-'; /* [--x] is a range */
2405 }
2406 else
2407 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00002408 /* Also accept "a-[.z.]" */
2409 endc = 0;
2410 if (*regparse == '[')
2411 endc = get_coll_element(&regparse);
2412 if (endc == 0)
2413 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414#ifdef FEAT_MBYTE
Bram Moolenaardf177f62005-02-22 08:39:57 +00002415 if (has_mbyte)
2416 endc = mb_ptr2char_adv(&regparse);
2417 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00002419 endc = *regparse++;
2420 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002421
2422 /* Handle \o40, \x20 and \u20AC style sequences */
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02002423 if (endc == '\\' && !reg_cpo_lit && !reg_cpo_bsl)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002424 endc = coll_get_char();
2425
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 if (startc > endc)
2427 EMSG_RET_NULL(_(e_invrange));
2428#ifdef FEAT_MBYTE
2429 if (has_mbyte && ((*mb_char2len)(startc) > 1
2430 || (*mb_char2len)(endc) > 1))
2431 {
2432 /* Limit to a range of 256 chars */
2433 if (endc > startc + 256)
2434 EMSG_RET_NULL(_(e_invrange));
2435 while (++startc <= endc)
2436 regmbc(startc);
2437 }
2438 else
2439#endif
2440 {
2441#ifdef EBCDIC
2442 int alpha_only = FALSE;
2443
2444 /* for alphabetical range skip the gaps
2445 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
2446 if (isalpha(startc) && isalpha(endc))
2447 alpha_only = TRUE;
2448#endif
2449 while (++startc <= endc)
2450#ifdef EBCDIC
2451 if (!alpha_only || isalpha(startc))
2452#endif
2453 regc(startc);
2454 }
2455 startc = -1;
2456 }
2457 }
2458 /*
2459 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
2460 * accepts "\t", "\e", etc., but only when the 'l' flag in
2461 * 'cpoptions' is not included.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002462 * Posix doesn't recognize backslash at all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 */
2464 else if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02002465 && !reg_cpo_bsl
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02002467 || (!reg_cpo_lit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 && vim_strchr(REGEXP_ABBR,
2469 regparse[1]) != NULL)))
2470 {
2471 regparse++;
2472 if (*regparse == 'n')
2473 {
2474 /* '\n' in range: also match NL */
2475 if (ret != JUST_CALC_SIZE)
2476 {
Bram Moolenaare337e5f2013-01-30 18:21:51 +01002477 /* Using \n inside [^] does not change what
2478 * matches. "[^\n]" is the same as ".". */
2479 if (*ret == ANYOF)
2480 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 *ret = ANYOF + ADD_NL;
Bram Moolenaare337e5f2013-01-30 18:21:51 +01002482 *flagp |= HASNL;
2483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 /* else: must have had a \n already */
2485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 regparse++;
2487 startc = -1;
2488 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002489 else if (*regparse == 'd'
2490 || *regparse == 'o'
2491 || *regparse == 'x'
2492 || *regparse == 'u'
2493 || *regparse == 'U')
2494 {
2495 startc = coll_get_char();
2496 if (startc == 0)
2497 regc(0x0a);
2498 else
2499#ifdef FEAT_MBYTE
2500 regmbc(startc);
2501#else
2502 regc(startc);
2503#endif
2504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 else
2506 {
2507 startc = backslash_trans(*regparse++);
2508 regc(startc);
2509 }
2510 }
2511 else if (*regparse == '[')
2512 {
2513 int c_class;
2514 int cu;
2515
Bram Moolenaardf177f62005-02-22 08:39:57 +00002516 c_class = get_char_class(&regparse);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 startc = -1;
2518 /* Characters assumed to be 8 bits! */
2519 switch (c_class)
2520 {
2521 case CLASS_NONE:
Bram Moolenaardf177f62005-02-22 08:39:57 +00002522 c_class = get_equi_class(&regparse);
2523 if (c_class != 0)
2524 {
2525 /* produce equivalence class */
2526 reg_equi_class(c_class);
2527 }
2528 else if ((c_class =
2529 get_coll_element(&regparse)) != 0)
2530 {
2531 /* produce a collating element */
2532 regmbc(c_class);
2533 }
2534 else
2535 {
2536 /* literal '[', allow [[-x] as a range */
2537 startc = *regparse++;
2538 regc(startc);
2539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 break;
2541 case CLASS_ALNUM:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02002542 for (cu = 1; cu < 128; cu++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 if (isalnum(cu))
Bram Moolenaaraf98a492016-04-24 14:40:12 +02002544 regmbc(cu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 break;
2546 case CLASS_ALPHA:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02002547 for (cu = 1; cu < 128; cu++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 if (isalpha(cu))
Bram Moolenaaraf98a492016-04-24 14:40:12 +02002549 regmbc(cu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 break;
2551 case CLASS_BLANK:
2552 regc(' ');
2553 regc('\t');
2554 break;
2555 case CLASS_CNTRL:
2556 for (cu = 1; cu <= 255; cu++)
2557 if (iscntrl(cu))
Bram Moolenaaraf98a492016-04-24 14:40:12 +02002558 regmbc(cu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 break;
2560 case CLASS_DIGIT:
2561 for (cu = 1; cu <= 255; cu++)
2562 if (VIM_ISDIGIT(cu))
Bram Moolenaaraf98a492016-04-24 14:40:12 +02002563 regmbc(cu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 break;
2565 case CLASS_GRAPH:
2566 for (cu = 1; cu <= 255; cu++)
2567 if (isgraph(cu))
Bram Moolenaaraf98a492016-04-24 14:40:12 +02002568 regmbc(cu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 break;
2570 case CLASS_LOWER:
2571 for (cu = 1; cu <= 255; cu++)
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02002572 if (MB_ISLOWER(cu) && cu != 170
2573 && cu != 186)
Bram Moolenaaraf98a492016-04-24 14:40:12 +02002574 regmbc(cu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 break;
2576 case CLASS_PRINT:
2577 for (cu = 1; cu <= 255; cu++)
2578 if (vim_isprintc(cu))
Bram Moolenaaraf98a492016-04-24 14:40:12 +02002579 regmbc(cu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 break;
2581 case CLASS_PUNCT:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02002582 for (cu = 1; cu < 128; cu++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 if (ispunct(cu))
Bram Moolenaaraf98a492016-04-24 14:40:12 +02002584 regmbc(cu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 break;
2586 case CLASS_SPACE:
2587 for (cu = 9; cu <= 13; cu++)
2588 regc(cu);
2589 regc(' ');
2590 break;
2591 case CLASS_UPPER:
2592 for (cu = 1; cu <= 255; cu++)
Bram Moolenaara245a5b2007-08-11 11:58:23 +00002593 if (MB_ISUPPER(cu))
Bram Moolenaaraf98a492016-04-24 14:40:12 +02002594 regmbc(cu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 break;
2596 case CLASS_XDIGIT:
2597 for (cu = 1; cu <= 255; cu++)
2598 if (vim_isxdigit(cu))
Bram Moolenaaraf98a492016-04-24 14:40:12 +02002599 regmbc(cu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 break;
2601 case CLASS_TAB:
2602 regc('\t');
2603 break;
2604 case CLASS_RETURN:
2605 regc('\r');
2606 break;
2607 case CLASS_BACKSPACE:
2608 regc('\b');
2609 break;
2610 case CLASS_ESCAPE:
2611 regc('\033');
2612 break;
2613 }
2614 }
2615 else
2616 {
2617#ifdef FEAT_MBYTE
2618 if (has_mbyte)
2619 {
2620 int len;
2621
2622 /* produce a multibyte character, including any
2623 * following composing characters */
2624 startc = mb_ptr2char(regparse);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002625 len = (*mb_ptr2len)(regparse);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 if (enc_utf8 && utf_char2len(startc) != len)
2627 startc = -1; /* composing chars */
2628 while (--len >= 0)
2629 regc(*regparse++);
2630 }
2631 else
2632#endif
2633 {
2634 startc = *regparse++;
2635 regc(startc);
2636 }
2637 }
2638 }
2639 regc(NUL);
2640 prevchr_len = 1; /* last char was the ']' */
2641 if (*regparse != ']')
2642 EMSG_RET_NULL(_(e_toomsbra)); /* Cannot happen? */
2643 skipchr(); /* let's be friends with the lexer again */
2644 *flagp |= HASWIDTH | SIMPLE;
2645 break;
2646 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002647 else if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002648 EMSG2_RET_NULL(_(e_missingbracket), reg_magic > MAGIC_OFF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 }
2650 /* FALLTHROUGH */
2651
2652 default:
2653 {
2654 int len;
2655
2656#ifdef FEAT_MBYTE
2657 /* A multi-byte character is handled as a separate atom if it's
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002658 * before a multi and when it's a composing char. */
2659 if (use_multibytecode(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002661do_multibyte:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 ret = regnode(MULTIBYTECODE);
2663 regmbc(c);
2664 *flagp |= HASWIDTH | SIMPLE;
2665 break;
2666 }
2667#endif
2668
2669 ret = regnode(EXACTLY);
2670
2671 /*
2672 * Append characters as long as:
2673 * - there is no following multi, we then need the character in
2674 * front of it as a single character operand
2675 * - not running into a Magic character
2676 * - "one_exactly" is not set
2677 * But always emit at least one character. Might be a Multi,
2678 * e.g., a "[" without matching "]".
2679 */
2680 for (len = 0; c != NUL && (len == 0
2681 || (re_multi_type(peekchr()) == NOT_MULTI
2682 && !one_exactly
2683 && !is_Magic(c))); ++len)
2684 {
2685 c = no_Magic(c);
2686#ifdef FEAT_MBYTE
2687 if (has_mbyte)
2688 {
2689 regmbc(c);
2690 if (enc_utf8)
2691 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 int l;
2693
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002694 /* Need to get composing character too. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 for (;;)
2696 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002697 l = utf_ptr2len(regparse);
2698 if (!UTF_COMPOSINGLIKE(regparse, regparse + l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 break;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002700 regmbc(utf_ptr2char(regparse));
2701 skipchr();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 }
2704 }
2705 else
2706#endif
2707 regc(c);
2708 c = getchr();
2709 }
2710 ungetchr();
2711
2712 regc(NUL);
2713 *flagp |= HASWIDTH;
2714 if (len == 1)
2715 *flagp |= SIMPLE;
2716 }
2717 break;
2718 }
2719
2720 return ret;
2721}
2722
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002723#ifdef FEAT_MBYTE
2724/*
2725 * Return TRUE if MULTIBYTECODE should be used instead of EXACTLY for
2726 * character "c".
2727 */
2728 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002729use_multibytecode(int c)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002730{
2731 return has_mbyte && (*mb_char2len)(c) > 1
2732 && (re_multi_type(peekchr()) != NOT_MULTI
2733 || (enc_utf8 && utf_iscomposing(c)));
2734}
2735#endif
2736
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002738 * Emit a node.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 * Return pointer to generated code.
2740 */
2741 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002742regnode(int op)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743{
2744 char_u *ret;
2745
2746 ret = regcode;
2747 if (ret == JUST_CALC_SIZE)
2748 regsize += 3;
2749 else
2750 {
2751 *regcode++ = op;
2752 *regcode++ = NUL; /* Null "next" pointer. */
2753 *regcode++ = NUL;
2754 }
2755 return ret;
2756}
2757
2758/*
2759 * Emit (if appropriate) a byte of code
2760 */
2761 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002762regc(int b)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763{
2764 if (regcode == JUST_CALC_SIZE)
2765 regsize++;
2766 else
2767 *regcode++ = b;
2768}
2769
2770#ifdef FEAT_MBYTE
2771/*
2772 * Emit (if appropriate) a multi-byte character of code
2773 */
2774 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002775regmbc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776{
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02002777 if (!has_mbyte && c > 0xff)
2778 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 if (regcode == JUST_CALC_SIZE)
2780 regsize += (*mb_char2len)(c);
2781 else
2782 regcode += (*mb_char2bytes)(c, regcode);
2783}
2784#endif
2785
2786/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002787 * Insert an operator in front of already-emitted operand
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 *
2789 * Means relocating the operand.
2790 */
2791 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002792reginsert(int op, char_u *opnd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793{
2794 char_u *src;
2795 char_u *dst;
2796 char_u *place;
2797
2798 if (regcode == JUST_CALC_SIZE)
2799 {
2800 regsize += 3;
2801 return;
2802 }
2803 src = regcode;
2804 regcode += 3;
2805 dst = regcode;
2806 while (src > opnd)
2807 *--dst = *--src;
2808
2809 place = opnd; /* Op node, where operand used to be. */
2810 *place++ = op;
2811 *place++ = NUL;
2812 *place = NUL;
2813}
2814
2815/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002816 * Insert an operator in front of already-emitted operand.
Bram Moolenaar75eb1612013-05-29 18:45:11 +02002817 * Add a number to the operator.
2818 */
2819 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002820reginsert_nr(int op, long val, char_u *opnd)
Bram Moolenaar75eb1612013-05-29 18:45:11 +02002821{
2822 char_u *src;
2823 char_u *dst;
2824 char_u *place;
2825
2826 if (regcode == JUST_CALC_SIZE)
2827 {
2828 regsize += 7;
2829 return;
2830 }
2831 src = regcode;
2832 regcode += 7;
2833 dst = regcode;
2834 while (src > opnd)
2835 *--dst = *--src;
2836
2837 place = opnd; /* Op node, where operand used to be. */
2838 *place++ = op;
2839 *place++ = NUL;
2840 *place++ = NUL;
2841 place = re_put_long(place, (long_u)val);
2842}
2843
2844/*
2845 * Insert an operator in front of already-emitted operand.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 * The operator has the given limit values as operands. Also set next pointer.
2847 *
2848 * Means relocating the operand.
2849 */
2850 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002851reginsert_limits(
2852 int op,
2853 long minval,
2854 long maxval,
2855 char_u *opnd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856{
2857 char_u *src;
2858 char_u *dst;
2859 char_u *place;
2860
2861 if (regcode == JUST_CALC_SIZE)
2862 {
2863 regsize += 11;
2864 return;
2865 }
2866 src = regcode;
2867 regcode += 11;
2868 dst = regcode;
2869 while (src > opnd)
2870 *--dst = *--src;
2871
2872 place = opnd; /* Op node, where operand used to be. */
2873 *place++ = op;
2874 *place++ = NUL;
2875 *place++ = NUL;
2876 place = re_put_long(place, (long_u)minval);
2877 place = re_put_long(place, (long_u)maxval);
2878 regtail(opnd, place);
2879}
2880
2881/*
2882 * Write a long as four bytes at "p" and return pointer to the next char.
2883 */
2884 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002885re_put_long(char_u *p, long_u val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886{
2887 *p++ = (char_u) ((val >> 24) & 0377);
2888 *p++ = (char_u) ((val >> 16) & 0377);
2889 *p++ = (char_u) ((val >> 8) & 0377);
2890 *p++ = (char_u) (val & 0377);
2891 return p;
2892}
2893
2894/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002895 * Set the next-pointer at the end of a node chain.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 */
2897 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002898regtail(char_u *p, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899{
2900 char_u *scan;
2901 char_u *temp;
2902 int offset;
2903
2904 if (p == JUST_CALC_SIZE)
2905 return;
2906
2907 /* Find last node. */
2908 scan = p;
2909 for (;;)
2910 {
2911 temp = regnext(scan);
2912 if (temp == NULL)
2913 break;
2914 scan = temp;
2915 }
2916
Bram Moolenaar582fd852005-03-28 20:58:01 +00002917 if (OP(scan) == BACK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 offset = (int)(scan - val);
2919 else
2920 offset = (int)(val - scan);
Bram Moolenaard3005802009-11-25 17:21:32 +00002921 /* When the offset uses more than 16 bits it can no longer fit in the two
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02002922 * bytes available. Use a global flag to avoid having to check return
Bram Moolenaard3005802009-11-25 17:21:32 +00002923 * values in too many places. */
2924 if (offset > 0xffff)
2925 reg_toolong = TRUE;
2926 else
2927 {
2928 *(scan + 1) = (char_u) (((unsigned)offset >> 8) & 0377);
2929 *(scan + 2) = (char_u) (offset & 0377);
2930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931}
2932
2933/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002934 * Like regtail, on item after a BRANCH; nop if none.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 */
2936 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002937regoptail(char_u *p, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938{
2939 /* When op is neither BRANCH nor BRACE_COMPLEX0-9, it is "operandless" */
2940 if (p == NULL || p == JUST_CALC_SIZE
2941 || (OP(p) != BRANCH
2942 && (OP(p) < BRACE_COMPLEX || OP(p) > BRACE_COMPLEX + 9)))
2943 return;
2944 regtail(OPERAND(p), val);
2945}
2946
2947/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002948 * Functions for getting characters from the regexp input.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002950/*
2951 * Start parsing at "str".
2952 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002954initchr(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955{
2956 regparse = str;
2957 prevchr_len = 0;
2958 curchr = prevprevchr = prevchr = nextchr = -1;
2959 at_start = TRUE;
2960 prev_at_start = FALSE;
2961}
2962
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002963/*
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002964 * Save the current parse state, so that it can be restored and parsing
2965 * starts in the same state again.
2966 */
2967 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002968save_parse_state(parse_state_T *ps)
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002969{
2970 ps->regparse = regparse;
2971 ps->prevchr_len = prevchr_len;
2972 ps->curchr = curchr;
2973 ps->prevchr = prevchr;
2974 ps->prevprevchr = prevprevchr;
2975 ps->nextchr = nextchr;
2976 ps->at_start = at_start;
2977 ps->prev_at_start = prev_at_start;
2978 ps->regnpar = regnpar;
2979}
2980
2981/*
2982 * Restore a previously saved parse state.
2983 */
2984 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002985restore_parse_state(parse_state_T *ps)
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002986{
2987 regparse = ps->regparse;
2988 prevchr_len = ps->prevchr_len;
2989 curchr = ps->curchr;
2990 prevchr = ps->prevchr;
2991 prevprevchr = ps->prevprevchr;
2992 nextchr = ps->nextchr;
2993 at_start = ps->at_start;
2994 prev_at_start = ps->prev_at_start;
2995 regnpar = ps->regnpar;
2996}
2997
2998
2999/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003000 * Get the next character without advancing.
3001 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003003peekchr(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003005 static int after_slash = FALSE;
3006
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 if (curchr == -1)
3008 {
3009 switch (curchr = regparse[0])
3010 {
3011 case '.':
3012 case '[':
3013 case '~':
3014 /* magic when 'magic' is on */
3015 if (reg_magic >= MAGIC_ON)
3016 curchr = Magic(curchr);
3017 break;
3018 case '(':
3019 case ')':
3020 case '{':
3021 case '%':
3022 case '+':
3023 case '=':
3024 case '?':
3025 case '@':
3026 case '!':
3027 case '&':
3028 case '|':
3029 case '<':
3030 case '>':
3031 case '#': /* future ext. */
3032 case '"': /* future ext. */
3033 case '\'': /* future ext. */
3034 case ',': /* future ext. */
3035 case '-': /* future ext. */
3036 case ':': /* future ext. */
3037 case ';': /* future ext. */
3038 case '`': /* future ext. */
3039 case '/': /* Can't be used in / command */
3040 /* magic only after "\v" */
3041 if (reg_magic == MAGIC_ALL)
3042 curchr = Magic(curchr);
3043 break;
3044 case '*':
Bram Moolenaardf177f62005-02-22 08:39:57 +00003045 /* * is not magic as the very first character, eg "?*ptr", when
3046 * after '^', eg "/^*ptr" and when after "\(", "\|", "\&". But
3047 * "\(\*" is not magic, thus must be magic if "after_slash" */
3048 if (reg_magic >= MAGIC_ON
3049 && !at_start
3050 && !(prev_at_start && prevchr == Magic('^'))
3051 && (after_slash
3052 || (prevchr != Magic('(')
3053 && prevchr != Magic('&')
3054 && prevchr != Magic('|'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 curchr = Magic('*');
3056 break;
3057 case '^':
3058 /* '^' is only magic as the very first character and if it's after
3059 * "\(", "\|", "\&' or "\n" */
3060 if (reg_magic >= MAGIC_OFF
3061 && (at_start
3062 || reg_magic == MAGIC_ALL
3063 || prevchr == Magic('(')
3064 || prevchr == Magic('|')
3065 || prevchr == Magic('&')
3066 || prevchr == Magic('n')
3067 || (no_Magic(prevchr) == '('
3068 && prevprevchr == Magic('%'))))
3069 {
3070 curchr = Magic('^');
3071 at_start = TRUE;
3072 prev_at_start = FALSE;
3073 }
3074 break;
3075 case '$':
3076 /* '$' is only magic as the very last char and if it's in front of
3077 * either "\|", "\)", "\&", or "\n" */
3078 if (reg_magic >= MAGIC_OFF)
3079 {
3080 char_u *p = regparse + 1;
Bram Moolenaarff65ac82014-07-09 19:32:34 +02003081 int is_magic_all = (reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082
Bram Moolenaarff65ac82014-07-09 19:32:34 +02003083 /* ignore \c \C \m \M \v \V and \Z after '$' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 while (p[0] == '\\' && (p[1] == 'c' || p[1] == 'C'
Bram Moolenaarff65ac82014-07-09 19:32:34 +02003085 || p[1] == 'm' || p[1] == 'M'
3086 || p[1] == 'v' || p[1] == 'V' || p[1] == 'Z'))
3087 {
3088 if (p[1] == 'v')
3089 is_magic_all = TRUE;
3090 else if (p[1] == 'm' || p[1] == 'M' || p[1] == 'V')
3091 is_magic_all = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 p += 2;
Bram Moolenaarff65ac82014-07-09 19:32:34 +02003093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 if (p[0] == NUL
3095 || (p[0] == '\\'
3096 && (p[1] == '|' || p[1] == '&' || p[1] == ')'
3097 || p[1] == 'n'))
Bram Moolenaarff65ac82014-07-09 19:32:34 +02003098 || (is_magic_all
3099 && (p[0] == '|' || p[0] == '&' || p[0] == ')'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 || reg_magic == MAGIC_ALL)
3101 curchr = Magic('$');
3102 }
3103 break;
3104 case '\\':
3105 {
3106 int c = regparse[1];
3107
3108 if (c == NUL)
3109 curchr = '\\'; /* trailing '\' */
3110 else if (
3111#ifdef EBCDIC
3112 vim_strchr(META, c)
3113#else
3114 c <= '~' && META_flags[c]
3115#endif
3116 )
3117 {
3118 /*
3119 * META contains everything that may be magic sometimes,
3120 * except ^ and $ ("\^" and "\$" are only magic after
Bram Moolenaarb878bbb2015-06-09 20:39:24 +02003121 * "\V"). We now fetch the next character and toggle its
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 * magicness. Therefore, \ is so meta-magic that it is
3123 * not in META.
3124 */
3125 curchr = -1;
3126 prev_at_start = at_start;
3127 at_start = FALSE; /* be able to say "/\*ptr" */
3128 ++regparse;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003129 ++after_slash;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 peekchr();
3131 --regparse;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003132 --after_slash;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 curchr = toggle_Magic(curchr);
3134 }
3135 else if (vim_strchr(REGEXP_ABBR, c))
3136 {
3137 /*
3138 * Handle abbreviations, like "\t" for TAB -- webb
3139 */
3140 curchr = backslash_trans(c);
3141 }
3142 else if (reg_magic == MAGIC_NONE && (c == '$' || c == '^'))
3143 curchr = toggle_Magic(c);
3144 else
3145 {
3146 /*
3147 * Next character can never be (made) magic?
3148 * Then backslashing it won't do anything.
3149 */
3150#ifdef FEAT_MBYTE
3151 if (has_mbyte)
3152 curchr = (*mb_ptr2char)(regparse + 1);
3153 else
3154#endif
3155 curchr = c;
3156 }
3157 break;
3158 }
3159
3160#ifdef FEAT_MBYTE
3161 default:
3162 if (has_mbyte)
3163 curchr = (*mb_ptr2char)(regparse);
3164#endif
3165 }
3166 }
3167
3168 return curchr;
3169}
3170
3171/*
3172 * Eat one lexed character. Do this in a way that we can undo it.
3173 */
3174 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003175skipchr(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176{
3177 /* peekchr() eats a backslash, do the same here */
3178 if (*regparse == '\\')
3179 prevchr_len = 1;
3180 else
3181 prevchr_len = 0;
3182 if (regparse[prevchr_len] != NUL)
3183 {
3184#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003185 if (enc_utf8)
Bram Moolenaar8f5c5782007-11-29 20:27:21 +00003186 /* exclude composing chars that mb_ptr2len does include */
3187 prevchr_len += utf_ptr2len(regparse + prevchr_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003188 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003189 prevchr_len += (*mb_ptr2len)(regparse + prevchr_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 else
3191#endif
3192 ++prevchr_len;
3193 }
3194 regparse += prevchr_len;
3195 prev_at_start = at_start;
3196 at_start = FALSE;
3197 prevprevchr = prevchr;
3198 prevchr = curchr;
3199 curchr = nextchr; /* use previously unget char, or -1 */
3200 nextchr = -1;
3201}
3202
3203/*
3204 * Skip a character while keeping the value of prev_at_start for at_start.
3205 * prevchr and prevprevchr are also kept.
3206 */
3207 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003208skipchr_keepstart(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209{
3210 int as = prev_at_start;
3211 int pr = prevchr;
3212 int prpr = prevprevchr;
3213
3214 skipchr();
3215 at_start = as;
3216 prevchr = pr;
3217 prevprevchr = prpr;
3218}
3219
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003220/*
3221 * Get the next character from the pattern. We know about magic and such, so
3222 * therefore we need a lexical analyzer.
3223 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003225getchr(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226{
3227 int chr = peekchr();
3228
3229 skipchr();
3230 return chr;
3231}
3232
3233/*
3234 * put character back. Works only once!
3235 */
3236 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003237ungetchr(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238{
3239 nextchr = curchr;
3240 curchr = prevchr;
3241 prevchr = prevprevchr;
3242 at_start = prev_at_start;
3243 prev_at_start = FALSE;
3244
3245 /* Backup regparse, so that it's at the same position as before the
3246 * getchr(). */
3247 regparse -= prevchr_len;
3248}
3249
3250/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00003251 * Get and return the value of the hex string at the current position.
3252 * Return -1 if there is no valid hex number.
3253 * The position is updated:
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003254 * blahblah\%x20asdf
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003255 * before-^ ^-after
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003256 * The parameter controls the maximum number of input characters. This will be
3257 * 2 when reading a \%x20 sequence and 4 when reading a \%u20AC sequence.
3258 */
3259 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003260gethexchrs(int maxinputlen)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003261{
3262 int nr = 0;
3263 int c;
3264 int i;
3265
3266 for (i = 0; i < maxinputlen; ++i)
3267 {
3268 c = regparse[0];
3269 if (!vim_isxdigit(c))
3270 break;
3271 nr <<= 4;
3272 nr |= hex2nr(c);
3273 ++regparse;
3274 }
3275
3276 if (i == 0)
3277 return -1;
3278 return nr;
3279}
3280
3281/*
Bram Moolenaar75eb1612013-05-29 18:45:11 +02003282 * Get and return the value of the decimal string immediately after the
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003283 * current position. Return -1 for invalid. Consumes all digits.
3284 */
3285 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003286getdecchrs(void)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003287{
3288 int nr = 0;
3289 int c;
3290 int i;
3291
3292 for (i = 0; ; ++i)
3293 {
3294 c = regparse[0];
3295 if (c < '0' || c > '9')
3296 break;
3297 nr *= 10;
3298 nr += c - '0';
3299 ++regparse;
Bram Moolenaar75eb1612013-05-29 18:45:11 +02003300 curchr = -1; /* no longer valid */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003301 }
3302
3303 if (i == 0)
3304 return -1;
3305 return nr;
3306}
3307
3308/*
3309 * get and return the value of the octal string immediately after the current
3310 * position. Return -1 for invalid, or 0-255 for valid. Smart enough to handle
3311 * numbers > 377 correctly (for example, 400 is treated as 40) and doesn't
3312 * treat 8 or 9 as recognised characters. Position is updated:
3313 * blahblah\%o210asdf
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003314 * before-^ ^-after
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003315 */
3316 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003317getoctchrs(void)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003318{
3319 int nr = 0;
3320 int c;
3321 int i;
3322
3323 for (i = 0; i < 3 && nr < 040; ++i)
3324 {
3325 c = regparse[0];
3326 if (c < '0' || c > '7')
3327 break;
3328 nr <<= 3;
3329 nr |= hex2nr(c);
3330 ++regparse;
3331 }
3332
3333 if (i == 0)
3334 return -1;
3335 return nr;
3336}
3337
3338/*
3339 * Get a number after a backslash that is inside [].
3340 * When nothing is recognized return a backslash.
3341 */
3342 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003343coll_get_char(void)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003344{
3345 int nr = -1;
3346
3347 switch (*regparse++)
3348 {
3349 case 'd': nr = getdecchrs(); break;
3350 case 'o': nr = getoctchrs(); break;
3351 case 'x': nr = gethexchrs(2); break;
3352 case 'u': nr = gethexchrs(4); break;
3353 case 'U': nr = gethexchrs(8); break;
3354 }
3355 if (nr < 0)
3356 {
3357 /* If getting the number fails be backwards compatible: the character
3358 * is a backslash. */
3359 --regparse;
3360 nr = '\\';
3361 }
3362 return nr;
3363}
3364
3365/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 * read_limits - Read two integers to be taken as a minimum and maximum.
3367 * If the first character is '-', then the range is reversed.
3368 * Should end with 'end'. If minval is missing, zero is default, if maxval is
3369 * missing, a very big number is the default.
3370 */
3371 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003372read_limits(long *minval, long *maxval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373{
3374 int reverse = FALSE;
3375 char_u *first_char;
3376 long tmp;
3377
3378 if (*regparse == '-')
3379 {
3380 /* Starts with '-', so reverse the range later */
3381 regparse++;
3382 reverse = TRUE;
3383 }
3384 first_char = regparse;
3385 *minval = getdigits(&regparse);
3386 if (*regparse == ',') /* There is a comma */
3387 {
3388 if (vim_isdigit(*++regparse))
3389 *maxval = getdigits(&regparse);
3390 else
3391 *maxval = MAX_LIMIT;
3392 }
3393 else if (VIM_ISDIGIT(*first_char))
3394 *maxval = *minval; /* It was \{n} or \{-n} */
3395 else
3396 *maxval = MAX_LIMIT; /* It was \{} or \{-} */
3397 if (*regparse == '\\')
3398 regparse++; /* Allow either \{...} or \{...\} */
Bram Moolenaardf177f62005-02-22 08:39:57 +00003399 if (*regparse != '}')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 {
3401 sprintf((char *)IObuff, _("E554: Syntax error in %s{...}"),
3402 reg_magic == MAGIC_ALL ? "" : "\\");
3403 EMSG_RET_FAIL(IObuff);
3404 }
3405
3406 /*
3407 * Reverse the range if there was a '-', or make sure it is in the right
3408 * order otherwise.
3409 */
3410 if ((!reverse && *minval > *maxval) || (reverse && *minval < *maxval))
3411 {
3412 tmp = *minval;
3413 *minval = *maxval;
3414 *maxval = tmp;
3415 }
3416 skipchr(); /* let's be friends with the lexer again */
3417 return OK;
3418}
3419
3420/*
3421 * vim_regexec and friends
3422 */
3423
3424/*
3425 * Global work variables for vim_regexec().
3426 */
3427
3428/* The current match-position is remembered with these variables: */
3429static linenr_T reglnum; /* line number, relative to first line */
3430static char_u *regline; /* start of current line */
3431static char_u *reginput; /* current input, points into "regline" */
3432
3433static int need_clear_subexpr; /* subexpressions still need to be
3434 * cleared */
3435#ifdef FEAT_SYN_HL
3436static int need_clear_zsubexpr = FALSE; /* extmatch subexpressions
3437 * still need to be cleared */
3438#endif
3439
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440/*
3441 * Structure used to save the current input state, when it needs to be
3442 * restored after trying a match. Used by reg_save() and reg_restore().
Bram Moolenaar582fd852005-03-28 20:58:01 +00003443 * Also stores the length of "backpos".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 */
3445typedef struct
3446{
3447 union
3448 {
3449 char_u *ptr; /* reginput pointer, for single-line regexp */
3450 lpos_T pos; /* reginput pos, for multi-line regexp */
3451 } rs_u;
Bram Moolenaar582fd852005-03-28 20:58:01 +00003452 int rs_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453} regsave_T;
3454
3455/* struct to save start/end pointer/position in for \(\) */
3456typedef struct
3457{
3458 union
3459 {
3460 char_u *ptr;
3461 lpos_T pos;
3462 } se_u;
3463} save_se_T;
3464
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00003465/* used for BEHIND and NOBEHIND matching */
3466typedef struct regbehind_S
3467{
3468 regsave_T save_after;
3469 regsave_T save_behind;
Bram Moolenaarfde483c2008-06-15 12:21:50 +00003470 int save_need_clear_subexpr;
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00003471 save_se_T save_start[NSUBEXP];
3472 save_se_T save_end[NSUBEXP];
3473} regbehind_T;
3474
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003475static char_u *reg_getline(linenr_T lnum);
3476static long bt_regexec_both(char_u *line, colnr_T col, proftime_T *tm);
3477static long regtry(bt_regprog_T *prog, colnr_T col);
3478static void cleanup_subexpr(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003480static void cleanup_zsubexpr(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003482static void save_subexpr(regbehind_T *bp);
3483static void restore_subexpr(regbehind_T *bp);
3484static void reg_nextline(void);
3485static void reg_save(regsave_T *save, garray_T *gap);
3486static void reg_restore(regsave_T *save, garray_T *gap);
3487static int reg_save_equal(regsave_T *save);
3488static void save_se_multi(save_se_T *savep, lpos_T *posp);
3489static void save_se_one(save_se_T *savep, char_u **pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490
3491/* Save the sub-expressions before attempting a match. */
3492#define save_se(savep, posp, pp) \
3493 REG_MULTI ? save_se_multi((savep), (posp)) : save_se_one((savep), (pp))
3494
3495/* After a failed match restore the sub-expressions. */
3496#define restore_se(savep, posp, pp) { \
3497 if (REG_MULTI) \
3498 *(posp) = (savep)->se_u.pos; \
3499 else \
3500 *(pp) = (savep)->se_u.ptr; }
3501
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003502static int re_num_cmp(long_u val, char_u *scan);
3503static int match_with_backref(linenr_T start_lnum, colnr_T start_col, linenr_T end_lnum, colnr_T end_col, int *bytelen);
3504static int regmatch(char_u *prog);
3505static int regrepeat(char_u *p, long maxcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506
3507#ifdef DEBUG
3508int regnarrate = 0;
3509#endif
3510
3511/*
3512 * Internal copy of 'ignorecase'. It is set at each call to vim_regexec().
3513 * Normally it gets the value of "rm_ic" or "rmm_ic", but when the pattern
3514 * contains '\c' or '\C' the value is overruled.
3515 */
3516static int ireg_ic;
3517
3518#ifdef FEAT_MBYTE
3519/*
3520 * Similar to ireg_ic, but only for 'combining' characters. Set with \Z flag
3521 * in the regexp. Defaults to false, always.
3522 */
3523static int ireg_icombine;
3524#endif
3525
3526/*
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003527 * Copy of "rmm_maxcol": maximum column to search for a match. Zero when
3528 * there is no maximum.
3529 */
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003530static colnr_T ireg_maxcol;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003531
3532/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 * Sometimes need to save a copy of a line. Since alloc()/free() is very
3534 * slow, we keep one allocated piece of memory and only re-allocate it when
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003535 * it's too small. It's freed in bt_regexec_both() when finished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 */
Bram Moolenaard4210772008-01-02 14:35:30 +00003537static char_u *reg_tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538static unsigned reg_tofreelen;
3539
3540/*
3541 * These variables are set when executing a regexp to speed up the execution.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00003542 * Which ones are set depends on whether a single-line or multi-line match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 * done:
3544 * single-line multi-line
3545 * reg_match &regmatch_T NULL
3546 * reg_mmatch NULL &regmmatch_T
3547 * reg_startp reg_match->startp <invalid>
3548 * reg_endp reg_match->endp <invalid>
3549 * reg_startpos <invalid> reg_mmatch->startpos
3550 * reg_endpos <invalid> reg_mmatch->endpos
3551 * reg_win NULL window in which to search
Bram Moolenaar2f315ab2013-01-25 20:11:01 +01003552 * reg_buf curbuf buffer in which to search
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 * reg_firstlnum <invalid> first line in which to search
3554 * reg_maxline 0 last line nr
3555 * reg_line_lbr FALSE or TRUE FALSE
3556 */
3557static regmatch_T *reg_match;
3558static regmmatch_T *reg_mmatch;
3559static char_u **reg_startp = NULL;
3560static char_u **reg_endp = NULL;
3561static lpos_T *reg_startpos = NULL;
3562static lpos_T *reg_endpos = NULL;
3563static win_T *reg_win;
3564static buf_T *reg_buf;
3565static linenr_T reg_firstlnum;
3566static linenr_T reg_maxline;
3567static int reg_line_lbr; /* "\n" in string is line break */
3568
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003569/* Values for rs_state in regitem_T. */
3570typedef enum regstate_E
3571{
3572 RS_NOPEN = 0 /* NOPEN and NCLOSE */
3573 , RS_MOPEN /* MOPEN + [0-9] */
3574 , RS_MCLOSE /* MCLOSE + [0-9] */
3575#ifdef FEAT_SYN_HL
3576 , RS_ZOPEN /* ZOPEN + [0-9] */
3577 , RS_ZCLOSE /* ZCLOSE + [0-9] */
3578#endif
3579 , RS_BRANCH /* BRANCH */
3580 , RS_BRCPLX_MORE /* BRACE_COMPLEX and trying one more match */
3581 , RS_BRCPLX_LONG /* BRACE_COMPLEX and trying longest match */
3582 , RS_BRCPLX_SHORT /* BRACE_COMPLEX and trying shortest match */
3583 , RS_NOMATCH /* NOMATCH */
3584 , RS_BEHIND1 /* BEHIND / NOBEHIND matching rest */
3585 , RS_BEHIND2 /* BEHIND / NOBEHIND matching behind part */
3586 , RS_STAR_LONG /* STAR/PLUS/BRACE_SIMPLE longest match */
3587 , RS_STAR_SHORT /* STAR/PLUS/BRACE_SIMPLE shortest match */
3588} regstate_T;
3589
3590/*
3591 * When there are alternatives a regstate_T is put on the regstack to remember
3592 * what we are doing.
3593 * Before it may be another type of item, depending on rs_state, to remember
3594 * more things.
3595 */
3596typedef struct regitem_S
3597{
3598 regstate_T rs_state; /* what we are doing, one of RS_ above */
3599 char_u *rs_scan; /* current node in program */
3600 union
3601 {
3602 save_se_T sesave;
3603 regsave_T regsave;
3604 } rs_un; /* room for saving reginput */
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00003605 short rs_no; /* submatch nr or BEHIND/NOBEHIND */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003606} regitem_T;
3607
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003608static regitem_T *regstack_push(regstate_T state, char_u *scan);
3609static void regstack_pop(char_u **scan);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003610
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003611/* used for STAR, PLUS and BRACE_SIMPLE matching */
3612typedef struct regstar_S
3613{
3614 int nextb; /* next byte */
3615 int nextb_ic; /* next byte reverse case */
3616 long count;
3617 long minval;
3618 long maxval;
3619} regstar_T;
3620
3621/* used to store input position when a BACK was encountered, so that we now if
3622 * we made any progress since the last time. */
3623typedef struct backpos_S
3624{
3625 char_u *bp_scan; /* "scan" where BACK was encountered */
3626 regsave_T bp_pos; /* last input position */
3627} backpos_T;
3628
3629/*
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003630 * "regstack" and "backpos" are used by regmatch(). They are kept over calls
3631 * to avoid invoking malloc() and free() often.
3632 * "regstack" is a stack with regitem_T items, sometimes preceded by regstar_T
3633 * or regbehind_T.
3634 * "backpos_T" is a table with backpos_T for BACK
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003635 */
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003636static garray_T regstack = {0, 0, 0, 0, NULL};
3637static garray_T backpos = {0, 0, 0, 0, NULL};
3638
3639/*
3640 * Both for regstack and backpos tables we use the following strategy of
3641 * allocation (to reduce malloc/free calls):
3642 * - Initial size is fairly small.
3643 * - When needed, the tables are grown bigger (8 times at first, double after
3644 * that).
3645 * - After executing the match we free the memory only if the array has grown.
3646 * Thus the memory is kept allocated when it's at the initial size.
3647 * This makes it fast while not keeping a lot of memory allocated.
3648 * A three times speed increase was observed when using many simple patterns.
3649 */
3650#define REGSTACK_INITIAL 2048
3651#define BACKPOS_INITIAL 64
3652
3653#if defined(EXITFREE) || defined(PROTO)
3654 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003655free_regexp_stuff(void)
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003656{
3657 ga_clear(&regstack);
3658 ga_clear(&backpos);
3659 vim_free(reg_tofree);
3660 vim_free(reg_prev_sub);
3661}
3662#endif
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003663
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664/*
3665 * Get pointer to the line "lnum", which is relative to "reg_firstlnum".
3666 */
3667 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003668reg_getline(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669{
3670 /* when looking behind for a match/no-match lnum is negative. But we
3671 * can't go before line 1 */
3672 if (reg_firstlnum + lnum < 1)
3673 return NULL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00003674 if (lnum > reg_maxline)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003675 /* Must have matched the "\n" in the last line. */
3676 return (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 return ml_get_buf(reg_buf, reg_firstlnum + lnum, FALSE);
3678}
3679
3680static regsave_T behind_pos;
3681
3682#ifdef FEAT_SYN_HL
3683static char_u *reg_startzp[NSUBEXP]; /* Workspace to mark beginning */
3684static char_u *reg_endzp[NSUBEXP]; /* and end of \z(...\) matches */
3685static lpos_T reg_startzpos[NSUBEXP]; /* idem, beginning pos */
3686static lpos_T reg_endzpos[NSUBEXP]; /* idem, end pos */
3687#endif
3688
3689/* TRUE if using multi-line regexp. */
3690#define REG_MULTI (reg_match == NULL)
3691
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003692static int bt_regexec_nl(regmatch_T *rmp, char_u *line, colnr_T col, int line_lbr);
Bram Moolenaar2af78a12014-04-23 19:06:37 +02003693
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003694
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695/*
3696 * Match a regexp against a string.
3697 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
3698 * Uses curbuf for line count and 'iskeyword'.
Bram Moolenaar2af78a12014-04-23 19:06:37 +02003699 * if "line_lbr" is TRUE consider a "\n" in "line" to be a line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 *
Bram Moolenaar66a3e792014-11-20 23:07:05 +01003701 * Returns 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003703 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003704bt_regexec_nl(
3705 regmatch_T *rmp,
3706 char_u *line, /* string to match against */
3707 colnr_T col, /* column to start looking for match */
3708 int line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709{
3710 reg_match = rmp;
3711 reg_mmatch = NULL;
3712 reg_maxline = 0;
Bram Moolenaar2af78a12014-04-23 19:06:37 +02003713 reg_line_lbr = line_lbr;
Bram Moolenaar2f315ab2013-01-25 20:11:01 +01003714 reg_buf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 reg_win = NULL;
3716 ireg_ic = rmp->rm_ic;
3717#ifdef FEAT_MBYTE
3718 ireg_icombine = FALSE;
3719#endif
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003720 ireg_maxcol = 0;
Bram Moolenaar66a3e792014-11-20 23:07:05 +01003721
3722 return bt_regexec_both(line, col, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723}
3724
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003725static long bt_regexec_multi(regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003726
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727/*
3728 * Match a regexp against multiple lines.
3729 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
3730 * Uses curbuf for line count and 'iskeyword'.
3731 *
3732 * Return zero if there is no match. Return number of lines contained in the
3733 * match otherwise.
3734 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003735 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01003736bt_regexec_multi(
3737 regmmatch_T *rmp,
3738 win_T *win, /* window in which to search or NULL */
3739 buf_T *buf, /* buffer in which to search */
3740 linenr_T lnum, /* nr of line to start looking for match */
3741 colnr_T col, /* column to start looking for match */
3742 proftime_T *tm) /* timeout limit or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 reg_match = NULL;
3745 reg_mmatch = rmp;
3746 reg_buf = buf;
3747 reg_win = win;
3748 reg_firstlnum = lnum;
3749 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
3750 reg_line_lbr = FALSE;
3751 ireg_ic = rmp->rmm_ic;
3752#ifdef FEAT_MBYTE
3753 ireg_icombine = FALSE;
3754#endif
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003755 ireg_maxcol = rmp->rmm_maxcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756
Bram Moolenaar66a3e792014-11-20 23:07:05 +01003757 return bt_regexec_both(NULL, col, tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758}
3759
3760/*
3761 * Match a regexp against a string ("line" points to the string) or multiple
3762 * lines ("line" is NULL, use reg_getline()).
Bram Moolenaar66a3e792014-11-20 23:07:05 +01003763 * Returns 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01003766bt_regexec_both(
3767 char_u *line,
3768 colnr_T col, /* column to start looking for match */
3769 proftime_T *tm UNUSED) /* timeout limit or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770{
Bram Moolenaar66a3e792014-11-20 23:07:05 +01003771 bt_regprog_T *prog;
3772 char_u *s;
3773 long retval = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003775 /* Create "regstack" and "backpos" if they are not allocated yet.
3776 * We allocate *_INITIAL amount of bytes first and then set the grow size
3777 * to much bigger value to avoid many malloc calls in case of deep regular
3778 * expressions. */
3779 if (regstack.ga_data == NULL)
3780 {
3781 /* Use an item size of 1 byte, since we push different things
3782 * onto the regstack. */
3783 ga_init2(&regstack, 1, REGSTACK_INITIAL);
Bram Moolenaarcde88542015-08-11 19:14:00 +02003784 (void)ga_grow(&regstack, REGSTACK_INITIAL);
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003785 regstack.ga_growsize = REGSTACK_INITIAL * 8;
3786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003788 if (backpos.ga_data == NULL)
3789 {
3790 ga_init2(&backpos, sizeof(backpos_T), BACKPOS_INITIAL);
Bram Moolenaarcde88542015-08-11 19:14:00 +02003791 (void)ga_grow(&backpos, BACKPOS_INITIAL);
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003792 backpos.ga_growsize = BACKPOS_INITIAL * 8;
3793 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003794
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 if (REG_MULTI)
3796 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003797 prog = (bt_regprog_T *)reg_mmatch->regprog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 line = reg_getline((linenr_T)0);
3799 reg_startpos = reg_mmatch->startpos;
3800 reg_endpos = reg_mmatch->endpos;
3801 }
3802 else
3803 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003804 prog = (bt_regprog_T *)reg_match->regprog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 reg_startp = reg_match->startp;
3806 reg_endp = reg_match->endp;
3807 }
3808
3809 /* Be paranoid... */
3810 if (prog == NULL || line == NULL)
3811 {
3812 EMSG(_(e_null));
3813 goto theend;
3814 }
3815
3816 /* Check validity of program. */
3817 if (prog_magic_wrong())
3818 goto theend;
3819
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003820 /* If the start column is past the maximum column: no need to try. */
3821 if (ireg_maxcol > 0 && col >= ireg_maxcol)
3822 goto theend;
3823
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
3825 if (prog->regflags & RF_ICASE)
3826 ireg_ic = TRUE;
3827 else if (prog->regflags & RF_NOICASE)
3828 ireg_ic = FALSE;
3829
3830#ifdef FEAT_MBYTE
3831 /* If pattern contains "\Z" overrule value of ireg_icombine */
3832 if (prog->regflags & RF_ICOMBINE)
3833 ireg_icombine = TRUE;
3834#endif
3835
3836 /* If there is a "must appear" string, look for it. */
3837 if (prog->regmust != NULL)
3838 {
3839 int c;
3840
3841#ifdef FEAT_MBYTE
3842 if (has_mbyte)
3843 c = (*mb_ptr2char)(prog->regmust);
3844 else
3845#endif
3846 c = *prog->regmust;
3847 s = line + col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003848
3849 /*
3850 * This is used very often, esp. for ":global". Use three versions of
3851 * the loop to avoid overhead of conditions.
3852 */
3853 if (!ireg_ic
3854#ifdef FEAT_MBYTE
3855 && !has_mbyte
3856#endif
3857 )
3858 while ((s = vim_strbyte(s, c)) != NULL)
3859 {
3860 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3861 break; /* Found it. */
3862 ++s;
3863 }
3864#ifdef FEAT_MBYTE
3865 else if (!ireg_ic || (!enc_utf8 && mb_char2len(c) > 1))
3866 while ((s = vim_strchr(s, c)) != NULL)
3867 {
3868 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3869 break; /* Found it. */
3870 mb_ptr_adv(s);
3871 }
3872#endif
3873 else
3874 while ((s = cstrchr(s, c)) != NULL)
3875 {
3876 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3877 break; /* Found it. */
3878 mb_ptr_adv(s);
3879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 if (s == NULL) /* Not present. */
3881 goto theend;
3882 }
3883
3884 regline = line;
3885 reglnum = 0;
Bram Moolenaar73a92fe2010-09-14 10:55:47 +02003886 reg_toolong = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887
3888 /* Simplest case: Anchored match need be tried only once. */
3889 if (prog->reganch)
3890 {
3891 int c;
3892
3893#ifdef FEAT_MBYTE
3894 if (has_mbyte)
3895 c = (*mb_ptr2char)(regline + col);
3896 else
3897#endif
3898 c = regline[col];
3899 if (prog->regstart == NUL
3900 || prog->regstart == c
3901 || (ireg_ic && ((
3902#ifdef FEAT_MBYTE
3903 (enc_utf8 && utf_fold(prog->regstart) == utf_fold(c)))
3904 || (c < 255 && prog->regstart < 255 &&
3905#endif
Bram Moolenaara245a5b2007-08-11 11:58:23 +00003906 MB_TOLOWER(prog->regstart) == MB_TOLOWER(c)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 retval = regtry(prog, col);
3908 else
3909 retval = 0;
3910 }
3911 else
3912 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003913#ifdef FEAT_RELTIME
3914 int tm_count = 0;
3915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 /* Messy cases: unanchored match. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003917 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 {
3919 if (prog->regstart != NUL)
3920 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003921 /* Skip until the char we know it must start with.
3922 * Used often, do some work to avoid call overhead. */
3923 if (!ireg_ic
3924#ifdef FEAT_MBYTE
3925 && !has_mbyte
3926#endif
3927 )
3928 s = vim_strbyte(regline + col, prog->regstart);
3929 else
3930 s = cstrchr(regline + col, prog->regstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 if (s == NULL)
3932 {
3933 retval = 0;
3934 break;
3935 }
3936 col = (int)(s - regline);
3937 }
3938
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003939 /* Check for maximum column to try. */
3940 if (ireg_maxcol > 0 && col >= ireg_maxcol)
3941 {
3942 retval = 0;
3943 break;
3944 }
3945
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 retval = regtry(prog, col);
3947 if (retval > 0)
3948 break;
3949
3950 /* if not currently on the first line, get it again */
3951 if (reglnum != 0)
3952 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 reglnum = 0;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003954 regline = reg_getline((linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 }
3956 if (regline[col] == NUL)
3957 break;
3958#ifdef FEAT_MBYTE
3959 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003960 col += (*mb_ptr2len)(regline + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 else
3962#endif
3963 ++col;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003964#ifdef FEAT_RELTIME
3965 /* Check for timeout once in a twenty times to avoid overhead. */
3966 if (tm != NULL && ++tm_count == 20)
3967 {
3968 tm_count = 0;
3969 if (profile_passed_limit(tm))
3970 break;
3971 }
3972#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 }
3974 }
3975
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976theend:
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003977 /* Free "reg_tofree" when it's a bit big.
3978 * Free regstack and backpos if they are bigger than their initial size. */
3979 if (reg_tofreelen > 400)
3980 {
3981 vim_free(reg_tofree);
3982 reg_tofree = NULL;
3983 }
3984 if (regstack.ga_maxlen > REGSTACK_INITIAL)
3985 ga_clear(&regstack);
3986 if (backpos.ga_maxlen > BACKPOS_INITIAL)
3987 ga_clear(&backpos);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003988
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 return retval;
3990}
3991
3992#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003993static reg_extmatch_T *make_extmatch(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994
3995/*
3996 * Create a new extmatch and mark it as referenced once.
3997 */
3998 static reg_extmatch_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003999make_extmatch(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000{
4001 reg_extmatch_T *em;
4002
4003 em = (reg_extmatch_T *)alloc_clear((unsigned)sizeof(reg_extmatch_T));
4004 if (em != NULL)
4005 em->refcnt = 1;
4006 return em;
4007}
4008
4009/*
4010 * Add a reference to an extmatch.
4011 */
4012 reg_extmatch_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004013ref_extmatch(reg_extmatch_T *em)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014{
4015 if (em != NULL)
4016 em->refcnt++;
4017 return em;
4018}
4019
4020/*
4021 * Remove a reference to an extmatch. If there are no references left, free
4022 * the info.
4023 */
4024 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004025unref_extmatch(reg_extmatch_T *em)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026{
4027 int i;
4028
4029 if (em != NULL && --em->refcnt <= 0)
4030 {
4031 for (i = 0; i < NSUBEXP; ++i)
4032 vim_free(em->matches[i]);
4033 vim_free(em);
4034 }
4035}
4036#endif
4037
4038/*
4039 * regtry - try match of "prog" with at regline["col"].
4040 * Returns 0 for failure, number of lines contained in the match otherwise.
4041 */
4042 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01004043regtry(bt_regprog_T *prog, colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044{
4045 reginput = regline + col;
4046 need_clear_subexpr = TRUE;
4047#ifdef FEAT_SYN_HL
4048 /* Clear the external match subpointers if necessary. */
4049 if (prog->reghasz == REX_SET)
4050 need_clear_zsubexpr = TRUE;
4051#endif
4052
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004053 if (regmatch(prog->program + 1) == 0)
4054 return 0;
4055
4056 cleanup_subexpr();
4057 if (REG_MULTI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004059 if (reg_startpos[0].lnum < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004061 reg_startpos[0].lnum = 0;
4062 reg_startpos[0].col = col;
4063 }
4064 if (reg_endpos[0].lnum < 0)
4065 {
4066 reg_endpos[0].lnum = reglnum;
4067 reg_endpos[0].col = (int)(reginput - regline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 }
4069 else
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004070 /* Use line number of "\ze". */
4071 reglnum = reg_endpos[0].lnum;
4072 }
4073 else
4074 {
4075 if (reg_startp[0] == NULL)
4076 reg_startp[0] = regline + col;
4077 if (reg_endp[0] == NULL)
4078 reg_endp[0] = reginput;
4079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080#ifdef FEAT_SYN_HL
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004081 /* Package any found \z(...\) matches for export. Default is none. */
4082 unref_extmatch(re_extmatch_out);
4083 re_extmatch_out = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004085 if (prog->reghasz == REX_SET)
4086 {
4087 int i;
4088
4089 cleanup_zsubexpr();
4090 re_extmatch_out = make_extmatch();
4091 for (i = 0; i < NSUBEXP; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004093 if (REG_MULTI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004095 /* Only accept single line matches. */
4096 if (reg_startzpos[i].lnum >= 0
Bram Moolenaar5a4e1602014-04-06 21:34:04 +02004097 && reg_endzpos[i].lnum == reg_startzpos[i].lnum
4098 && reg_endzpos[i].col >= reg_startzpos[i].col)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004099 re_extmatch_out->matches[i] =
4100 vim_strnsave(reg_getline(reg_startzpos[i].lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 + reg_startzpos[i].col,
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004102 reg_endzpos[i].col - reg_startzpos[i].col);
4103 }
4104 else
4105 {
4106 if (reg_startzp[i] != NULL && reg_endzp[i] != NULL)
4107 re_extmatch_out->matches[i] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 vim_strnsave(reg_startzp[i],
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004109 (int)(reg_endzp[i] - reg_startzp[i]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 }
4111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 }
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004113#endif
4114 return 1 + reglnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115}
4116
4117#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004118static int reg_prev_class(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120/*
4121 * Get class of previous character.
4122 */
4123 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004124reg_prev_class(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125{
4126 if (reginput > regline)
Bram Moolenaarf813a182013-01-30 13:59:37 +01004127 return mb_get_class_buf(reginput - 1
4128 - (*mb_head_off)(regline, reginput - 1), reg_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 return -1;
4130}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004132
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004133static int reg_match_visual(void);
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004134
4135/*
4136 * Return TRUE if the current reginput position matches the Visual area.
4137 */
4138 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004139reg_match_visual(void)
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004140{
4141 pos_T top, bot;
4142 linenr_T lnum;
4143 colnr_T col;
4144 win_T *wp = reg_win == NULL ? curwin : reg_win;
4145 int mode;
4146 colnr_T start, end;
4147 colnr_T start2, end2;
4148 colnr_T cols;
4149
4150 /* Check if the buffer is the current buffer. */
4151 if (reg_buf != curbuf || VIsual.lnum == 0)
4152 return FALSE;
4153
4154 if (VIsual_active)
4155 {
4156 if (lt(VIsual, wp->w_cursor))
4157 {
4158 top = VIsual;
4159 bot = wp->w_cursor;
4160 }
4161 else
4162 {
4163 top = wp->w_cursor;
4164 bot = VIsual;
4165 }
4166 mode = VIsual_mode;
4167 }
4168 else
4169 {
4170 if (lt(curbuf->b_visual.vi_start, curbuf->b_visual.vi_end))
4171 {
4172 top = curbuf->b_visual.vi_start;
4173 bot = curbuf->b_visual.vi_end;
4174 }
4175 else
4176 {
4177 top = curbuf->b_visual.vi_end;
4178 bot = curbuf->b_visual.vi_start;
4179 }
4180 mode = curbuf->b_visual.vi_mode;
4181 }
4182 lnum = reglnum + reg_firstlnum;
4183 if (lnum < top.lnum || lnum > bot.lnum)
4184 return FALSE;
4185
4186 if (mode == 'v')
4187 {
4188 col = (colnr_T)(reginput - regline);
4189 if ((lnum == top.lnum && col < top.col)
4190 || (lnum == bot.lnum && col >= bot.col + (*p_sel != 'e')))
4191 return FALSE;
4192 }
4193 else if (mode == Ctrl_V)
4194 {
4195 getvvcol(wp, &top, &start, NULL, &end);
4196 getvvcol(wp, &bot, &start2, NULL, &end2);
4197 if (start2 < start)
4198 start = start2;
4199 if (end2 > end)
4200 end = end2;
4201 if (top.col == MAXCOL || bot.col == MAXCOL)
4202 end = MAXCOL;
4203 cols = win_linetabsize(wp, regline, (colnr_T)(reginput - regline));
4204 if (cols < start || cols > end - (*p_sel == 'e'))
4205 return FALSE;
4206 }
4207 return TRUE;
4208}
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004209
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004210#define ADVANCE_REGINPUT() mb_ptr_adv(reginput)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211
4212/*
4213 * The arguments from BRACE_LIMITS are stored here. They are actually local
4214 * to regmatch(), but they are here to reduce the amount of stack space used
4215 * (it can be called recursively many times).
4216 */
4217static long bl_minval;
4218static long bl_maxval;
4219
4220/*
4221 * regmatch - main matching routine
4222 *
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004223 * Conceptually the strategy is simple: Check to see whether the current node
4224 * matches, push an item onto the regstack and loop to see whether the rest
4225 * matches, and then act accordingly. In practice we make some effort to
4226 * avoid using the regstack, in particular by going through "ordinary" nodes
4227 * (that don't need to know whether the rest of the match failed) by a nested
4228 * loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 *
4230 * Returns TRUE when there is a match. Leaves reginput and reglnum just after
4231 * the last matched character.
4232 * Returns FALSE when there is no match. Leaves reginput and reglnum in an
4233 * undefined state!
4234 */
4235 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004236regmatch(
4237 char_u *scan) /* Current node. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004239 char_u *next; /* Next node. */
4240 int op;
4241 int c;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004242 regitem_T *rp;
4243 int no;
4244 int status; /* one of the RA_ values: */
4245#define RA_FAIL 1 /* something failed, abort */
4246#define RA_CONT 2 /* continue in inner loop */
4247#define RA_BREAK 3 /* break inner loop */
4248#define RA_MATCH 4 /* successful match */
4249#define RA_NOMATCH 5 /* didn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00004251 /* Make "regstack" and "backpos" empty. They are allocated and freed in
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004252 * bt_regexec_both() to reduce malloc()/free() calls. */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004253 regstack.ga_len = 0;
4254 backpos.ga_len = 0;
Bram Moolenaar582fd852005-03-28 20:58:01 +00004255
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004256 /*
Bram Moolenaar582fd852005-03-28 20:58:01 +00004257 * Repeat until "regstack" is empty.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004258 */
4259 for (;;)
4260 {
Bram Moolenaar41f12052013-08-25 17:01:42 +02004261 /* Some patterns may take a long time to match, e.g., "\([a-z]\+\)\+Q".
4262 * Allow interrupting them with CTRL-C. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 fast_breakcheck();
4264
4265#ifdef DEBUG
4266 if (scan != NULL && regnarrate)
4267 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004268 mch_errmsg((char *)regprop(scan));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 mch_errmsg("(\n");
4270 }
4271#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004272
4273 /*
Bram Moolenaar582fd852005-03-28 20:58:01 +00004274 * Repeat for items that can be matched sequentially, without using the
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004275 * regstack.
4276 */
4277 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004279 if (got_int || scan == NULL)
4280 {
4281 status = RA_FAIL;
4282 break;
4283 }
4284 status = RA_CONT;
4285
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286#ifdef DEBUG
4287 if (regnarrate)
4288 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004289 mch_errmsg((char *)regprop(scan));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 mch_errmsg("...\n");
4291# ifdef FEAT_SYN_HL
4292 if (re_extmatch_in != NULL)
4293 {
4294 int i;
4295
4296 mch_errmsg(_("External submatches:\n"));
4297 for (i = 0; i < NSUBEXP; i++)
4298 {
4299 mch_errmsg(" \"");
4300 if (re_extmatch_in->matches[i] != NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004301 mch_errmsg((char *)re_extmatch_in->matches[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 mch_errmsg("\"\n");
4303 }
4304 }
4305# endif
4306 }
4307#endif
4308 next = regnext(scan);
4309
4310 op = OP(scan);
4311 /* Check for character class with NL added. */
Bram Moolenaar640009d2006-10-17 16:48:26 +00004312 if (!reg_line_lbr && WITH_NL(op) && REG_MULTI
4313 && *reginput == NUL && reglnum <= reg_maxline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 {
4315 reg_nextline();
4316 }
4317 else if (reg_line_lbr && WITH_NL(op) && *reginput == '\n')
4318 {
4319 ADVANCE_REGINPUT();
4320 }
4321 else
4322 {
4323 if (WITH_NL(op))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004324 op -= ADD_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325#ifdef FEAT_MBYTE
4326 if (has_mbyte)
4327 c = (*mb_ptr2char)(reginput);
4328 else
4329#endif
4330 c = *reginput;
4331 switch (op)
4332 {
4333 case BOL:
4334 if (reginput != regline)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004335 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 break;
4337
4338 case EOL:
4339 if (c != NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004340 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 break;
4342
4343 case RE_BOF:
Bram Moolenaara7139332007-12-09 18:26:22 +00004344 /* We're not at the beginning of the file when below the first
4345 * line where we started, not at the start of the line or we
4346 * didn't start at the first line of the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 if (reglnum != 0 || reginput != regline
Bram Moolenaara7139332007-12-09 18:26:22 +00004348 || (REG_MULTI && reg_firstlnum > 1))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004349 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 break;
4351
4352 case RE_EOF:
4353 if (reglnum != reg_maxline || c != NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004354 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 break;
4356
4357 case CURSOR:
4358 /* Check if the buffer is in a window and compare the
4359 * reg_win->w_cursor position to the match position. */
4360 if (reg_win == NULL
4361 || (reglnum + reg_firstlnum != reg_win->w_cursor.lnum)
4362 || ((colnr_T)(reginput - regline) != reg_win->w_cursor.col))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004363 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 break;
4365
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004366 case RE_MARK:
Bram Moolenaar044aa292013-06-04 21:27:38 +02004367 /* Compare the mark position to the match position. */
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004368 {
4369 int mark = OPERAND(scan)[0];
4370 int cmp = OPERAND(scan)[1];
4371 pos_T *pos;
4372
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004373 pos = getmark_buf(reg_buf, mark, FALSE);
Bram Moolenaare9400a42007-05-06 13:04:32 +00004374 if (pos == NULL /* mark doesn't exist */
Bram Moolenaar044aa292013-06-04 21:27:38 +02004375 || pos->lnum <= 0 /* mark isn't set in reg_buf */
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004376 || (pos->lnum == reglnum + reg_firstlnum
4377 ? (pos->col == (colnr_T)(reginput - regline)
4378 ? (cmp == '<' || cmp == '>')
4379 : (pos->col < (colnr_T)(reginput - regline)
4380 ? cmp != '>'
4381 : cmp != '<'))
4382 : (pos->lnum < reglnum + reg_firstlnum
4383 ? cmp != '>'
4384 : cmp != '<')))
4385 status = RA_NOMATCH;
4386 }
4387 break;
4388
4389 case RE_VISUAL:
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004390 if (!reg_match_visual())
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004391 status = RA_NOMATCH;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004392 break;
4393
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 case RE_LNUM:
4395 if (!REG_MULTI || !re_num_cmp((long_u)(reglnum + reg_firstlnum),
4396 scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004397 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 break;
4399
4400 case RE_COL:
4401 if (!re_num_cmp((long_u)(reginput - regline) + 1, scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004402 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 break;
4404
4405 case RE_VCOL:
4406 if (!re_num_cmp((long_u)win_linetabsize(
4407 reg_win == NULL ? curwin : reg_win,
4408 regline, (colnr_T)(reginput - regline)) + 1, scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004409 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 break;
4411
4412 case BOW: /* \<word; reginput points to w */
4413 if (c == NUL) /* Can't match at end of line */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004414 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415#ifdef FEAT_MBYTE
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004416 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 {
4418 int this_class;
4419
4420 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf813a182013-01-30 13:59:37 +01004421 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 if (this_class <= 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004423 status = RA_NOMATCH; /* not on a word at all */
4424 else if (reg_prev_class() == this_class)
4425 status = RA_NOMATCH; /* previous char is in same word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 }
4427#endif
4428 else
4429 {
Bram Moolenaar2f315ab2013-01-25 20:11:01 +01004430 if (!vim_iswordc_buf(c, reg_buf) || (reginput > regline
4431 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004432 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 }
4434 break;
4435
4436 case EOW: /* word\>; reginput points after d */
4437 if (reginput == regline) /* Can't match at start of line */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004438 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439#ifdef FEAT_MBYTE
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004440 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 {
4442 int this_class, prev_class;
4443
4444 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf813a182013-01-30 13:59:37 +01004445 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 prev_class = reg_prev_class();
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004447 if (this_class == prev_class
4448 || prev_class == 0 || prev_class == 1)
4449 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004452 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004454 if (!vim_iswordc_buf(reginput[-1], reg_buf)
4455 || (reginput[0] != NUL && vim_iswordc_buf(c, reg_buf)))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004456 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 }
4458 break; /* Matched with EOW */
4459
4460 case ANY:
Bram Moolenaare337e5f2013-01-30 18:21:51 +01004461 /* ANY does not match new lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 if (c == NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004463 status = RA_NOMATCH;
4464 else
4465 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 break;
4467
4468 case IDENT:
4469 if (!vim_isIDc(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004470 status = RA_NOMATCH;
4471 else
4472 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 break;
4474
4475 case SIDENT:
4476 if (VIM_ISDIGIT(*reginput) || !vim_isIDc(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004477 status = RA_NOMATCH;
4478 else
4479 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 break;
4481
4482 case KWORD:
Bram Moolenaarf813a182013-01-30 13:59:37 +01004483 if (!vim_iswordp_buf(reginput, reg_buf))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004484 status = RA_NOMATCH;
4485 else
4486 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 break;
4488
4489 case SKWORD:
Bram Moolenaarf813a182013-01-30 13:59:37 +01004490 if (VIM_ISDIGIT(*reginput) || !vim_iswordp_buf(reginput, reg_buf))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004491 status = RA_NOMATCH;
4492 else
4493 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 break;
4495
4496 case FNAME:
4497 if (!vim_isfilec(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004498 status = RA_NOMATCH;
4499 else
4500 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 break;
4502
4503 case SFNAME:
4504 if (VIM_ISDIGIT(*reginput) || !vim_isfilec(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004505 status = RA_NOMATCH;
4506 else
4507 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 break;
4509
4510 case PRINT:
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02004511 if (!vim_isprintc(PTR2CHAR(reginput)))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004512 status = RA_NOMATCH;
4513 else
4514 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 break;
4516
4517 case SPRINT:
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02004518 if (VIM_ISDIGIT(*reginput) || !vim_isprintc(PTR2CHAR(reginput)))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004519 status = RA_NOMATCH;
4520 else
4521 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 break;
4523
4524 case WHITE:
4525 if (!vim_iswhite(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004526 status = RA_NOMATCH;
4527 else
4528 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 break;
4530
4531 case NWHITE:
4532 if (c == NUL || vim_iswhite(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004533 status = RA_NOMATCH;
4534 else
4535 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 break;
4537
4538 case DIGIT:
4539 if (!ri_digit(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004540 status = RA_NOMATCH;
4541 else
4542 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 break;
4544
4545 case NDIGIT:
4546 if (c == NUL || ri_digit(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004547 status = RA_NOMATCH;
4548 else
4549 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 break;
4551
4552 case HEX:
4553 if (!ri_hex(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004554 status = RA_NOMATCH;
4555 else
4556 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 break;
4558
4559 case NHEX:
4560 if (c == NUL || ri_hex(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004561 status = RA_NOMATCH;
4562 else
4563 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 break;
4565
4566 case OCTAL:
4567 if (!ri_octal(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004568 status = RA_NOMATCH;
4569 else
4570 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 break;
4572
4573 case NOCTAL:
4574 if (c == NUL || ri_octal(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004575 status = RA_NOMATCH;
4576 else
4577 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 break;
4579
4580 case WORD:
4581 if (!ri_word(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004582 status = RA_NOMATCH;
4583 else
4584 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 break;
4586
4587 case NWORD:
4588 if (c == NUL || ri_word(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004589 status = RA_NOMATCH;
4590 else
4591 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 break;
4593
4594 case HEAD:
4595 if (!ri_head(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004596 status = RA_NOMATCH;
4597 else
4598 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 break;
4600
4601 case NHEAD:
4602 if (c == NUL || ri_head(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004603 status = RA_NOMATCH;
4604 else
4605 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 break;
4607
4608 case ALPHA:
4609 if (!ri_alpha(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004610 status = RA_NOMATCH;
4611 else
4612 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 break;
4614
4615 case NALPHA:
4616 if (c == NUL || ri_alpha(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004617 status = RA_NOMATCH;
4618 else
4619 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 break;
4621
4622 case LOWER:
4623 if (!ri_lower(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004624 status = RA_NOMATCH;
4625 else
4626 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 break;
4628
4629 case NLOWER:
4630 if (c == NUL || ri_lower(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004631 status = RA_NOMATCH;
4632 else
4633 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 break;
4635
4636 case UPPER:
4637 if (!ri_upper(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004638 status = RA_NOMATCH;
4639 else
4640 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 break;
4642
4643 case NUPPER:
4644 if (c == NUL || ri_upper(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004645 status = RA_NOMATCH;
4646 else
4647 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 break;
4649
4650 case EXACTLY:
4651 {
4652 int len;
4653 char_u *opnd;
4654
4655 opnd = OPERAND(scan);
4656 /* Inline the first byte, for speed. */
4657 if (*opnd != *reginput
4658 && (!ireg_ic || (
4659#ifdef FEAT_MBYTE
4660 !enc_utf8 &&
4661#endif
Bram Moolenaara245a5b2007-08-11 11:58:23 +00004662 MB_TOLOWER(*opnd) != MB_TOLOWER(*reginput))))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004663 status = RA_NOMATCH;
4664 else if (*opnd == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 {
4666 /* match empty string always works; happens when "~" is
4667 * empty. */
4668 }
Bram Moolenaar6082bea2014-05-13 18:04:00 +02004669 else
4670 {
4671 if (opnd[1] == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672#ifdef FEAT_MBYTE
4673 && !(enc_utf8 && ireg_ic)
4674#endif
4675 )
Bram Moolenaar6082bea2014-05-13 18:04:00 +02004676 {
4677 len = 1; /* matched a single byte above */
4678 }
4679 else
4680 {
4681 /* Need to match first byte again for multi-byte. */
4682 len = (int)STRLEN(opnd);
4683 if (cstrncmp(opnd, reginput, &len) != 0)
4684 status = RA_NOMATCH;
4685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686#ifdef FEAT_MBYTE
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004687 /* Check for following composing character, unless %C
4688 * follows (skips over all composing chars). */
Bram Moolenaar6082bea2014-05-13 18:04:00 +02004689 if (status != RA_NOMATCH
4690 && enc_utf8
4691 && UTF_COMPOSINGLIKE(reginput, reginput + len)
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004692 && !ireg_icombine
4693 && OP(next) != RE_COMPOSING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 {
4695 /* raaron: This code makes a composing character get
4696 * ignored, which is the correct behavior (sometimes)
4697 * for voweled Hebrew texts. */
Bram Moolenaar6082bea2014-05-13 18:04:00 +02004698 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700#endif
Bram Moolenaar6082bea2014-05-13 18:04:00 +02004701 if (status != RA_NOMATCH)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004702 reginput += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 }
4704 }
4705 break;
4706
4707 case ANYOF:
4708 case ANYBUT:
4709 if (c == NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004710 status = RA_NOMATCH;
4711 else if ((cstrchr(OPERAND(scan), c) == NULL) == (op == ANYOF))
4712 status = RA_NOMATCH;
4713 else
4714 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 break;
4716
4717#ifdef FEAT_MBYTE
4718 case MULTIBYTECODE:
4719 if (has_mbyte)
4720 {
4721 int i, len;
4722 char_u *opnd;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004723 int opndc = 0, inpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724
4725 opnd = OPERAND(scan);
4726 /* Safety check (just in case 'encoding' was changed since
4727 * compiling the program). */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004728 if ((len = (*mb_ptr2len)(opnd)) < 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004729 {
4730 status = RA_NOMATCH;
4731 break;
4732 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004733 if (enc_utf8)
4734 opndc = mb_ptr2char(opnd);
4735 if (enc_utf8 && utf_iscomposing(opndc))
4736 {
4737 /* When only a composing char is given match at any
4738 * position where that composing char appears. */
4739 status = RA_NOMATCH;
Bram Moolenaar0e462412015-03-31 14:17:31 +02004740 for (i = 0; reginput[i] != NUL;
4741 i += utf_ptr2len(reginput + i))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004742 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004743 inpc = mb_ptr2char(reginput + i);
4744 if (!utf_iscomposing(inpc))
4745 {
4746 if (i > 0)
4747 break;
4748 }
4749 else if (opndc == inpc)
4750 {
4751 /* Include all following composing chars. */
4752 len = i + mb_ptr2len(reginput + i);
4753 status = RA_MATCH;
4754 break;
4755 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004756 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004757 }
4758 else
4759 for (i = 0; i < len; ++i)
4760 if (opnd[i] != reginput[i])
4761 {
4762 status = RA_NOMATCH;
4763 break;
4764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 reginput += len;
4766 }
4767 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004768 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 break;
4770#endif
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004771 case RE_COMPOSING:
4772#ifdef FEAT_MBYTE
4773 if (enc_utf8)
4774 {
4775 /* Skip composing characters. */
4776 while (utf_iscomposing(utf_ptr2char(reginput)))
4777 mb_cptr_adv(reginput);
4778 }
4779#endif
4780 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781
4782 case NOTHING:
4783 break;
4784
4785 case BACK:
Bram Moolenaar582fd852005-03-28 20:58:01 +00004786 {
4787 int i;
4788 backpos_T *bp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789
Bram Moolenaar582fd852005-03-28 20:58:01 +00004790 /*
4791 * When we run into BACK we need to check if we don't keep
4792 * looping without matching any input. The second and later
4793 * times a BACK is encountered it fails if the input is still
4794 * at the same position as the previous time.
4795 * The positions are stored in "backpos" and found by the
4796 * current value of "scan", the position in the RE program.
4797 */
4798 bp = (backpos_T *)backpos.ga_data;
4799 for (i = 0; i < backpos.ga_len; ++i)
4800 if (bp[i].bp_scan == scan)
4801 break;
4802 if (i == backpos.ga_len)
4803 {
4804 /* First time at this BACK, make room to store the pos. */
4805 if (ga_grow(&backpos, 1) == FAIL)
4806 status = RA_FAIL;
4807 else
4808 {
4809 /* get "ga_data" again, it may have changed */
4810 bp = (backpos_T *)backpos.ga_data;
4811 bp[i].bp_scan = scan;
4812 ++backpos.ga_len;
4813 }
4814 }
4815 else if (reg_save_equal(&bp[i].bp_pos))
4816 /* Still at same position as last time, fail. */
4817 status = RA_NOMATCH;
4818
4819 if (status != RA_FAIL && status != RA_NOMATCH)
4820 reg_save(&bp[i].bp_pos, &backpos);
4821 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004822 break;
4823
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 case MOPEN + 0: /* Match start: \zs */
4825 case MOPEN + 1: /* \( */
4826 case MOPEN + 2:
4827 case MOPEN + 3:
4828 case MOPEN + 4:
4829 case MOPEN + 5:
4830 case MOPEN + 6:
4831 case MOPEN + 7:
4832 case MOPEN + 8:
4833 case MOPEN + 9:
4834 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 no = op - MOPEN;
4836 cleanup_subexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004837 rp = regstack_push(RS_MOPEN, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004838 if (rp == NULL)
4839 status = RA_FAIL;
4840 else
4841 {
4842 rp->rs_no = no;
4843 save_se(&rp->rs_un.sesave, &reg_startpos[no],
4844 &reg_startp[no]);
4845 /* We simply continue and handle the result when done. */
4846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004848 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849
4850 case NOPEN: /* \%( */
4851 case NCLOSE: /* \) after \%( */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004852 if (regstack_push(RS_NOPEN, scan) == NULL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004853 status = RA_FAIL;
4854 /* We simply continue and handle the result when done. */
4855 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856
4857#ifdef FEAT_SYN_HL
4858 case ZOPEN + 1:
4859 case ZOPEN + 2:
4860 case ZOPEN + 3:
4861 case ZOPEN + 4:
4862 case ZOPEN + 5:
4863 case ZOPEN + 6:
4864 case ZOPEN + 7:
4865 case ZOPEN + 8:
4866 case ZOPEN + 9:
4867 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 no = op - ZOPEN;
4869 cleanup_zsubexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004870 rp = regstack_push(RS_ZOPEN, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004871 if (rp == NULL)
4872 status = RA_FAIL;
4873 else
4874 {
4875 rp->rs_no = no;
4876 save_se(&rp->rs_un.sesave, &reg_startzpos[no],
4877 &reg_startzp[no]);
4878 /* We simply continue and handle the result when done. */
4879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004881 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882#endif
4883
4884 case MCLOSE + 0: /* Match end: \ze */
4885 case MCLOSE + 1: /* \) */
4886 case MCLOSE + 2:
4887 case MCLOSE + 3:
4888 case MCLOSE + 4:
4889 case MCLOSE + 5:
4890 case MCLOSE + 6:
4891 case MCLOSE + 7:
4892 case MCLOSE + 8:
4893 case MCLOSE + 9:
4894 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 no = op - MCLOSE;
4896 cleanup_subexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004897 rp = regstack_push(RS_MCLOSE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004898 if (rp == NULL)
4899 status = RA_FAIL;
4900 else
4901 {
4902 rp->rs_no = no;
4903 save_se(&rp->rs_un.sesave, &reg_endpos[no], &reg_endp[no]);
4904 /* We simply continue and handle the result when done. */
4905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004907 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908
4909#ifdef FEAT_SYN_HL
4910 case ZCLOSE + 1: /* \) after \z( */
4911 case ZCLOSE + 2:
4912 case ZCLOSE + 3:
4913 case ZCLOSE + 4:
4914 case ZCLOSE + 5:
4915 case ZCLOSE + 6:
4916 case ZCLOSE + 7:
4917 case ZCLOSE + 8:
4918 case ZCLOSE + 9:
4919 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 no = op - ZCLOSE;
4921 cleanup_zsubexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004922 rp = regstack_push(RS_ZCLOSE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004923 if (rp == NULL)
4924 status = RA_FAIL;
4925 else
4926 {
4927 rp->rs_no = no;
4928 save_se(&rp->rs_un.sesave, &reg_endzpos[no],
4929 &reg_endzp[no]);
4930 /* We simply continue and handle the result when done. */
4931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004933 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934#endif
4935
4936 case BACKREF + 1:
4937 case BACKREF + 2:
4938 case BACKREF + 3:
4939 case BACKREF + 4:
4940 case BACKREF + 5:
4941 case BACKREF + 6:
4942 case BACKREF + 7:
4943 case BACKREF + 8:
4944 case BACKREF + 9:
4945 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947
4948 no = op - BACKREF;
4949 cleanup_subexpr();
4950 if (!REG_MULTI) /* Single-line regexp */
4951 {
Bram Moolenaar7670fa02009-02-21 21:04:20 +00004952 if (reg_startp[no] == NULL || reg_endp[no] == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 {
4954 /* Backref was not set: Match an empty string. */
4955 len = 0;
4956 }
4957 else
4958 {
4959 /* Compare current input with back-ref in the same
4960 * line. */
4961 len = (int)(reg_endp[no] - reg_startp[no]);
4962 if (cstrncmp(reg_startp[no], reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004963 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 }
4965 }
4966 else /* Multi-line regexp */
4967 {
Bram Moolenaar7670fa02009-02-21 21:04:20 +00004968 if (reg_startpos[no].lnum < 0 || reg_endpos[no].lnum < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 {
4970 /* Backref was not set: Match an empty string. */
4971 len = 0;
4972 }
4973 else
4974 {
4975 if (reg_startpos[no].lnum == reglnum
4976 && reg_endpos[no].lnum == reglnum)
4977 {
4978 /* Compare back-ref within the current line. */
4979 len = reg_endpos[no].col - reg_startpos[no].col;
4980 if (cstrncmp(regline + reg_startpos[no].col,
4981 reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004982 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 }
4984 else
4985 {
4986 /* Messy situation: Need to compare between two
4987 * lines. */
Bram Moolenaar141f6bb2013-06-15 15:09:50 +02004988 int r = match_with_backref(
Bram Moolenaar580abea2013-06-14 20:31:28 +02004989 reg_startpos[no].lnum,
4990 reg_startpos[no].col,
4991 reg_endpos[no].lnum,
4992 reg_endpos[no].col,
Bram Moolenaar4cff8fa2013-06-14 22:48:54 +02004993 &len);
Bram Moolenaar141f6bb2013-06-15 15:09:50 +02004994
4995 if (r != RA_MATCH)
4996 status = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 }
4998 }
4999 }
5000
5001 /* Matched the backref, skip over it. */
5002 reginput += len;
5003 }
5004 break;
5005
5006#ifdef FEAT_SYN_HL
5007 case ZREF + 1:
5008 case ZREF + 2:
5009 case ZREF + 3:
5010 case ZREF + 4:
5011 case ZREF + 5:
5012 case ZREF + 6:
5013 case ZREF + 7:
5014 case ZREF + 8:
5015 case ZREF + 9:
5016 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 int len;
5018
5019 cleanup_zsubexpr();
5020 no = op - ZREF;
5021 if (re_extmatch_in != NULL
5022 && re_extmatch_in->matches[no] != NULL)
5023 {
5024 len = (int)STRLEN(re_extmatch_in->matches[no]);
5025 if (cstrncmp(re_extmatch_in->matches[no],
5026 reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005027 status = RA_NOMATCH;
5028 else
5029 reginput += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 }
5031 else
5032 {
5033 /* Backref was not set: Match an empty string. */
5034 }
5035 }
5036 break;
5037#endif
5038
5039 case BRANCH:
5040 {
5041 if (OP(next) != BRANCH) /* No choice. */
5042 next = OPERAND(scan); /* Avoid recursion. */
5043 else
5044 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005045 rp = regstack_push(RS_BRANCH, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005046 if (rp == NULL)
5047 status = RA_FAIL;
5048 else
5049 status = RA_BREAK; /* rest is below */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 }
5051 }
5052 break;
5053
5054 case BRACE_LIMITS:
5055 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 if (OP(next) == BRACE_SIMPLE)
5057 {
5058 bl_minval = OPERAND_MIN(scan);
5059 bl_maxval = OPERAND_MAX(scan);
5060 }
5061 else if (OP(next) >= BRACE_COMPLEX
5062 && OP(next) < BRACE_COMPLEX + 10)
5063 {
5064 no = OP(next) - BRACE_COMPLEX;
5065 brace_min[no] = OPERAND_MIN(scan);
5066 brace_max[no] = OPERAND_MAX(scan);
5067 brace_count[no] = 0;
5068 }
5069 else
5070 {
5071 EMSG(_(e_internal)); /* Shouldn't happen */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005072 status = RA_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 }
5074 }
5075 break;
5076
5077 case BRACE_COMPLEX + 0:
5078 case BRACE_COMPLEX + 1:
5079 case BRACE_COMPLEX + 2:
5080 case BRACE_COMPLEX + 3:
5081 case BRACE_COMPLEX + 4:
5082 case BRACE_COMPLEX + 5:
5083 case BRACE_COMPLEX + 6:
5084 case BRACE_COMPLEX + 7:
5085 case BRACE_COMPLEX + 8:
5086 case BRACE_COMPLEX + 9:
5087 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 no = op - BRACE_COMPLEX;
5089 ++brace_count[no];
5090
5091 /* If not matched enough times yet, try one more */
5092 if (brace_count[no] <= (brace_min[no] <= brace_max[no]
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005093 ? brace_min[no] : brace_max[no]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005095 rp = regstack_push(RS_BRCPLX_MORE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005096 if (rp == NULL)
5097 status = RA_FAIL;
5098 else
5099 {
5100 rp->rs_no = no;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005101 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005102 next = OPERAND(scan);
5103 /* We continue and handle the result when done. */
5104 }
5105 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 }
5107
5108 /* If matched enough times, may try matching some more */
5109 if (brace_min[no] <= brace_max[no])
5110 {
5111 /* Range is the normal way around, use longest match */
5112 if (brace_count[no] <= brace_max[no])
5113 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005114 rp = regstack_push(RS_BRCPLX_LONG, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005115 if (rp == NULL)
5116 status = RA_FAIL;
5117 else
5118 {
5119 rp->rs_no = no;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005120 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005121 next = OPERAND(scan);
5122 /* We continue and handle the result when done. */
5123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 }
5125 }
5126 else
5127 {
5128 /* Range is backwards, use shortest match first */
5129 if (brace_count[no] <= brace_min[no])
5130 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005131 rp = regstack_push(RS_BRCPLX_SHORT, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005132 if (rp == NULL)
5133 status = RA_FAIL;
5134 else
5135 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005136 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005137 /* We continue and handle the result when done. */
5138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 }
5140 }
5141 }
5142 break;
5143
5144 case BRACE_SIMPLE:
5145 case STAR:
5146 case PLUS:
5147 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005148 regstar_T rst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149
5150 /*
5151 * Lookahead to avoid useless match attempts when we know
5152 * what character comes next.
5153 */
5154 if (OP(next) == EXACTLY)
5155 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005156 rst.nextb = *OPERAND(next);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 if (ireg_ic)
5158 {
Bram Moolenaara245a5b2007-08-11 11:58:23 +00005159 if (MB_ISUPPER(rst.nextb))
5160 rst.nextb_ic = MB_TOLOWER(rst.nextb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 else
Bram Moolenaara245a5b2007-08-11 11:58:23 +00005162 rst.nextb_ic = MB_TOUPPER(rst.nextb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 }
5164 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005165 rst.nextb_ic = rst.nextb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 }
5167 else
5168 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005169 rst.nextb = NUL;
5170 rst.nextb_ic = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 }
5172 if (op != BRACE_SIMPLE)
5173 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005174 rst.minval = (op == STAR) ? 0 : 1;
5175 rst.maxval = MAX_LIMIT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 }
5177 else
5178 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005179 rst.minval = bl_minval;
5180 rst.maxval = bl_maxval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 }
5182
5183 /*
5184 * When maxval > minval, try matching as much as possible, up
5185 * to maxval. When maxval < minval, try matching at least the
5186 * minimal number (since the range is backwards, that's also
5187 * maxval!).
5188 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005189 rst.count = regrepeat(OPERAND(scan), rst.maxval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005192 status = RA_FAIL;
5193 break;
5194 }
5195 if (rst.minval <= rst.maxval
5196 ? rst.count >= rst.minval : rst.count >= rst.maxval)
5197 {
5198 /* It could match. Prepare for trying to match what
5199 * follows. The code is below. Parameters are stored in
5200 * a regstar_T on the regstack. */
Bram Moolenaar916b7af2005-03-16 09:52:38 +00005201 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005202 {
5203 EMSG(_(e_maxmempat));
5204 status = RA_FAIL;
5205 }
5206 else if (ga_grow(&regstack, sizeof(regstar_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005207 status = RA_FAIL;
5208 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005210 regstack.ga_len += sizeof(regstar_T);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005211 rp = regstack_push(rst.minval <= rst.maxval
Bram Moolenaar582fd852005-03-28 20:58:01 +00005212 ? RS_STAR_LONG : RS_STAR_SHORT, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005213 if (rp == NULL)
5214 status = RA_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005216 {
5217 *(((regstar_T *)rp) - 1) = rst;
5218 status = RA_BREAK; /* skip the restore bits */
5219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 }
5221 }
5222 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005223 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 }
5226 break;
5227
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005228 case NOMATCH:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 case MATCH:
5230 case SUBPAT:
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005231 rp = regstack_push(RS_NOMATCH, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005232 if (rp == NULL)
5233 status = RA_FAIL;
5234 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005236 rp->rs_no = op;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005237 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005238 next = OPERAND(scan);
5239 /* We continue and handle the result when done. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 }
5241 break;
5242
5243 case BEHIND:
5244 case NOBEHIND:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005245 /* Need a bit of room to store extra positions. */
Bram Moolenaar916b7af2005-03-16 09:52:38 +00005246 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005247 {
5248 EMSG(_(e_maxmempat));
5249 status = RA_FAIL;
5250 }
5251 else if (ga_grow(&regstack, sizeof(regbehind_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005252 status = RA_FAIL;
5253 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005255 regstack.ga_len += sizeof(regbehind_T);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005256 rp = regstack_push(RS_BEHIND1, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005257 if (rp == NULL)
5258 status = RA_FAIL;
5259 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 {
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005261 /* Need to save the subexpr to be able to restore them
5262 * when there is a match but we don't use it. */
5263 save_subexpr(((regbehind_T *)rp) - 1);
5264
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005265 rp->rs_no = op;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005266 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005267 /* First try if what follows matches. If it does then we
5268 * check the behind match by looping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005271 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272
5273 case BHPOS:
5274 if (REG_MULTI)
5275 {
5276 if (behind_pos.rs_u.pos.col != (colnr_T)(reginput - regline)
5277 || behind_pos.rs_u.pos.lnum != reglnum)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005278 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 }
5280 else if (behind_pos.rs_u.ptr != reginput)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005281 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 break;
5283
5284 case NEWL:
Bram Moolenaar640009d2006-10-17 16:48:26 +00005285 if ((c != NUL || !REG_MULTI || reglnum > reg_maxline
5286 || reg_line_lbr) && (c != '\n' || !reg_line_lbr))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005287 status = RA_NOMATCH;
5288 else if (reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 ADVANCE_REGINPUT();
5290 else
5291 reg_nextline();
5292 break;
5293
5294 case END:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005295 status = RA_MATCH; /* Success! */
5296 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297
5298 default:
5299 EMSG(_(e_re_corr));
5300#ifdef DEBUG
5301 printf("Illegal op code %d\n", op);
5302#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005303 status = RA_FAIL;
5304 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 }
5306 }
5307
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005308 /* If we can't continue sequentially, break the inner loop. */
5309 if (status != RA_CONT)
5310 break;
5311
5312 /* Continue in inner loop, advance to next item. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 scan = next;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005314
5315 } /* end of inner loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316
5317 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005318 * If there is something on the regstack execute the code for the state.
Bram Moolenaar582fd852005-03-28 20:58:01 +00005319 * If the state is popped then loop and use the older state.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005321 while (regstack.ga_len > 0 && status != RA_FAIL)
5322 {
5323 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len) - 1;
5324 switch (rp->rs_state)
5325 {
5326 case RS_NOPEN:
5327 /* Result is passed on as-is, simply pop the state. */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005328 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005329 break;
5330
5331 case RS_MOPEN:
5332 /* Pop the state. Restore pointers when there is no match. */
5333 if (status == RA_NOMATCH)
5334 restore_se(&rp->rs_un.sesave, &reg_startpos[rp->rs_no],
5335 &reg_startp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005336 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005337 break;
5338
5339#ifdef FEAT_SYN_HL
5340 case RS_ZOPEN:
5341 /* Pop the state. Restore pointers when there is no match. */
5342 if (status == RA_NOMATCH)
5343 restore_se(&rp->rs_un.sesave, &reg_startzpos[rp->rs_no],
5344 &reg_startzp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005345 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005346 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005348
5349 case RS_MCLOSE:
5350 /* Pop the state. Restore pointers when there is no match. */
5351 if (status == RA_NOMATCH)
5352 restore_se(&rp->rs_un.sesave, &reg_endpos[rp->rs_no],
5353 &reg_endp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005354 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005355 break;
5356
5357#ifdef FEAT_SYN_HL
5358 case RS_ZCLOSE:
5359 /* Pop the state. Restore pointers when there is no match. */
5360 if (status == RA_NOMATCH)
5361 restore_se(&rp->rs_un.sesave, &reg_endzpos[rp->rs_no],
5362 &reg_endzp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005363 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005364 break;
5365#endif
5366
5367 case RS_BRANCH:
5368 if (status == RA_MATCH)
5369 /* this branch matched, use it */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005370 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005371 else
5372 {
5373 if (status != RA_BREAK)
5374 {
5375 /* After a non-matching branch: try next one. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005376 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005377 scan = rp->rs_scan;
5378 }
5379 if (scan == NULL || OP(scan) != BRANCH)
5380 {
5381 /* no more branches, didn't find a match */
5382 status = RA_NOMATCH;
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005383 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005384 }
5385 else
5386 {
5387 /* Prepare to try a branch. */
5388 rp->rs_scan = regnext(scan);
Bram Moolenaar582fd852005-03-28 20:58:01 +00005389 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005390 scan = OPERAND(scan);
5391 }
5392 }
5393 break;
5394
5395 case RS_BRCPLX_MORE:
5396 /* Pop the state. Restore pointers when there is no match. */
5397 if (status == RA_NOMATCH)
5398 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005399 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005400 --brace_count[rp->rs_no]; /* decrement match count */
5401 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005402 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005403 break;
5404
5405 case RS_BRCPLX_LONG:
5406 /* Pop the state. Restore pointers when there is no match. */
5407 if (status == RA_NOMATCH)
5408 {
5409 /* There was no match, but we did find enough matches. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005410 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005411 --brace_count[rp->rs_no];
5412 /* continue with the items after "\{}" */
5413 status = RA_CONT;
5414 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005415 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005416 if (status == RA_CONT)
5417 scan = regnext(scan);
5418 break;
5419
5420 case RS_BRCPLX_SHORT:
5421 /* Pop the state. Restore pointers when there is no match. */
5422 if (status == RA_NOMATCH)
5423 /* There was no match, try to match one more item. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005424 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005425 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005426 if (status == RA_NOMATCH)
5427 {
5428 scan = OPERAND(scan);
5429 status = RA_CONT;
5430 }
5431 break;
5432
5433 case RS_NOMATCH:
5434 /* Pop the state. If the operand matches for NOMATCH or
5435 * doesn't match for MATCH/SUBPAT, we fail. Otherwise backup,
5436 * except for SUBPAT, and continue with the next item. */
5437 if (status == (rp->rs_no == NOMATCH ? RA_MATCH : RA_NOMATCH))
5438 status = RA_NOMATCH;
5439 else
5440 {
5441 status = RA_CONT;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005442 if (rp->rs_no != SUBPAT) /* zero-width */
5443 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005444 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005445 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005446 if (status == RA_CONT)
5447 scan = regnext(scan);
5448 break;
5449
5450 case RS_BEHIND1:
5451 if (status == RA_NOMATCH)
5452 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005453 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005454 regstack.ga_len -= sizeof(regbehind_T);
5455 }
5456 else
5457 {
5458 /* The stuff after BEHIND/NOBEHIND matches. Now try if
5459 * the behind part does (not) match before the current
5460 * position in the input. This must be done at every
5461 * position in the input and checking if the match ends at
5462 * the current position. */
5463
5464 /* save the position after the found match for next */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005465 reg_save(&(((regbehind_T *)rp) - 1)->save_after, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005466
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005467 /* Start looking for a match with operand at the current
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00005468 * position. Go back one character until we find the
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005469 * result, hitting the start of the line or the previous
5470 * line (for multi-line matching).
5471 * Set behind_pos to where the match should end, BHPOS
5472 * will match it. Save the current value. */
5473 (((regbehind_T *)rp) - 1)->save_behind = behind_pos;
5474 behind_pos = rp->rs_un.regsave;
5475
5476 rp->rs_state = RS_BEHIND2;
5477
Bram Moolenaar582fd852005-03-28 20:58:01 +00005478 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005479 scan = OPERAND(rp->rs_scan) + 4;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005480 }
5481 break;
5482
5483 case RS_BEHIND2:
5484 /*
5485 * Looping for BEHIND / NOBEHIND match.
5486 */
5487 if (status == RA_MATCH && reg_save_equal(&behind_pos))
5488 {
5489 /* found a match that ends where "next" started */
5490 behind_pos = (((regbehind_T *)rp) - 1)->save_behind;
5491 if (rp->rs_no == BEHIND)
Bram Moolenaar582fd852005-03-28 20:58:01 +00005492 reg_restore(&(((regbehind_T *)rp) - 1)->save_after,
5493 &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005494 else
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005495 {
5496 /* But we didn't want a match. Need to restore the
5497 * subexpr, because what follows matched, so they have
5498 * been set. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005499 status = RA_NOMATCH;
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005500 restore_subexpr(((regbehind_T *)rp) - 1);
5501 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005502 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005503 regstack.ga_len -= sizeof(regbehind_T);
5504 }
5505 else
5506 {
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005507 long limit;
5508
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005509 /* No match or a match that doesn't end where we want it: Go
5510 * back one character. May go to previous line once. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005511 no = OK;
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005512 limit = OPERAND_MIN(rp->rs_scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005513 if (REG_MULTI)
5514 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02005515 if (limit > 0
5516 && ((rp->rs_un.regsave.rs_u.pos.lnum
5517 < behind_pos.rs_u.pos.lnum
5518 ? (colnr_T)STRLEN(regline)
5519 : behind_pos.rs_u.pos.col)
5520 - rp->rs_un.regsave.rs_u.pos.col >= limit))
5521 no = FAIL;
5522 else if (rp->rs_un.regsave.rs_u.pos.col == 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005523 {
5524 if (rp->rs_un.regsave.rs_u.pos.lnum
5525 < behind_pos.rs_u.pos.lnum
5526 || reg_getline(
5527 --rp->rs_un.regsave.rs_u.pos.lnum)
5528 == NULL)
5529 no = FAIL;
5530 else
5531 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005532 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005533 rp->rs_un.regsave.rs_u.pos.col =
5534 (colnr_T)STRLEN(regline);
5535 }
5536 }
5537 else
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005538 {
Bram Moolenaarf5e44a72013-02-26 18:46:01 +01005539#ifdef FEAT_MBYTE
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005540 if (has_mbyte)
5541 rp->rs_un.regsave.rs_u.pos.col -=
5542 (*mb_head_off)(regline, regline
Bram Moolenaarf5e44a72013-02-26 18:46:01 +01005543 + rp->rs_un.regsave.rs_u.pos.col - 1) + 1;
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005544 else
Bram Moolenaarf5e44a72013-02-26 18:46:01 +01005545#endif
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005546 --rp->rs_un.regsave.rs_u.pos.col;
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005547 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005548 }
5549 else
5550 {
5551 if (rp->rs_un.regsave.rs_u.ptr == regline)
5552 no = FAIL;
5553 else
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005554 {
5555 mb_ptr_back(regline, rp->rs_un.regsave.rs_u.ptr);
5556 if (limit > 0 && (long)(behind_pos.rs_u.ptr
5557 - rp->rs_un.regsave.rs_u.ptr) > limit)
5558 no = FAIL;
5559 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005560 }
5561 if (no == OK)
5562 {
5563 /* Advanced, prepare for finding match again. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005564 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005565 scan = OPERAND(rp->rs_scan) + 4;
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005566 if (status == RA_MATCH)
5567 {
5568 /* We did match, so subexpr may have been changed,
5569 * need to restore them for the next try. */
5570 status = RA_NOMATCH;
5571 restore_subexpr(((regbehind_T *)rp) - 1);
5572 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005573 }
5574 else
5575 {
5576 /* Can't advance. For NOBEHIND that's a match. */
5577 behind_pos = (((regbehind_T *)rp) - 1)->save_behind;
5578 if (rp->rs_no == NOBEHIND)
5579 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005580 reg_restore(&(((regbehind_T *)rp) - 1)->save_after,
5581 &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005582 status = RA_MATCH;
5583 }
5584 else
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005585 {
5586 /* We do want a proper match. Need to restore the
5587 * subexpr if we had a match, because they may have
5588 * been set. */
5589 if (status == RA_MATCH)
5590 {
5591 status = RA_NOMATCH;
5592 restore_subexpr(((regbehind_T *)rp) - 1);
5593 }
5594 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005595 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005596 regstack.ga_len -= sizeof(regbehind_T);
5597 }
5598 }
5599 break;
5600
5601 case RS_STAR_LONG:
5602 case RS_STAR_SHORT:
5603 {
5604 regstar_T *rst = ((regstar_T *)rp) - 1;
5605
5606 if (status == RA_MATCH)
5607 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005608 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005609 regstack.ga_len -= sizeof(regstar_T);
5610 break;
5611 }
5612
5613 /* Tried once already, restore input pointers. */
5614 if (status != RA_BREAK)
Bram Moolenaar582fd852005-03-28 20:58:01 +00005615 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005616
5617 /* Repeat until we found a position where it could match. */
5618 for (;;)
5619 {
5620 if (status != RA_BREAK)
5621 {
5622 /* Tried first position already, advance. */
5623 if (rp->rs_state == RS_STAR_LONG)
5624 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005625 /* Trying for longest match, but couldn't or
5626 * didn't match -- back up one char. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005627 if (--rst->count < rst->minval)
5628 break;
5629 if (reginput == regline)
5630 {
5631 /* backup to last char of previous line */
5632 --reglnum;
5633 regline = reg_getline(reglnum);
5634 /* Just in case regrepeat() didn't count
5635 * right. */
5636 if (regline == NULL)
5637 break;
5638 reginput = regline + STRLEN(regline);
5639 fast_breakcheck();
5640 }
5641 else
5642 mb_ptr_back(regline, reginput);
5643 }
5644 else
5645 {
5646 /* Range is backwards, use shortest match first.
5647 * Careful: maxval and minval are exchanged!
5648 * Couldn't or didn't match: try advancing one
5649 * char. */
5650 if (rst->count == rst->minval
5651 || regrepeat(OPERAND(rp->rs_scan), 1L) == 0)
5652 break;
5653 ++rst->count;
5654 }
5655 if (got_int)
5656 break;
5657 }
5658 else
5659 status = RA_NOMATCH;
5660
5661 /* If it could match, try it. */
5662 if (rst->nextb == NUL || *reginput == rst->nextb
5663 || *reginput == rst->nextb_ic)
5664 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005665 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005666 scan = regnext(rp->rs_scan);
5667 status = RA_CONT;
5668 break;
5669 }
5670 }
5671 if (status != RA_CONT)
5672 {
5673 /* Failed. */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005674 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005675 regstack.ga_len -= sizeof(regstar_T);
5676 status = RA_NOMATCH;
5677 }
5678 }
5679 break;
5680 }
5681
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005682 /* If we want to continue the inner loop or didn't pop a state
5683 * continue matching loop */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005684 if (status == RA_CONT || rp == (regitem_T *)
5685 ((char *)regstack.ga_data + regstack.ga_len) - 1)
5686 break;
5687 }
5688
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005689 /* May need to continue with the inner loop, starting at "scan". */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005690 if (status == RA_CONT)
5691 continue;
5692
5693 /*
5694 * If the regstack is empty or something failed we are done.
5695 */
5696 if (regstack.ga_len == 0 || status == RA_FAIL)
5697 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005698 if (scan == NULL)
5699 {
5700 /*
5701 * We get here only if there's trouble -- normally "case END" is
5702 * the terminating point.
5703 */
5704 EMSG(_(e_re_corr));
5705#ifdef DEBUG
5706 printf("Premature EOL\n");
5707#endif
5708 }
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005709 if (status == RA_FAIL)
5710 got_int = TRUE;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005711 return (status == RA_MATCH);
5712 }
5713
5714 } /* End of loop until the regstack is empty. */
5715
5716 /* NOTREACHED */
5717}
5718
5719/*
5720 * Push an item onto the regstack.
5721 * Returns pointer to new item. Returns NULL when out of memory.
5722 */
5723 static regitem_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01005724regstack_push(regstate_T state, char_u *scan)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005725{
5726 regitem_T *rp;
5727
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005728 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005729 {
5730 EMSG(_(e_maxmempat));
5731 return NULL;
5732 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005733 if (ga_grow(&regstack, sizeof(regitem_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005734 return NULL;
5735
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005736 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005737 rp->rs_state = state;
5738 rp->rs_scan = scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005739
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005740 regstack.ga_len += sizeof(regitem_T);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005741 return rp;
5742}
5743
5744/*
5745 * Pop an item from the regstack.
5746 */
5747 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005748regstack_pop(char_u **scan)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005749{
5750 regitem_T *rp;
5751
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005752 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len) - 1;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005753 *scan = rp->rs_scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005754
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005755 regstack.ga_len -= sizeof(regitem_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756}
5757
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758/*
5759 * regrepeat - repeatedly match something simple, return how many.
5760 * Advances reginput (and reglnum) to just after the matched chars.
5761 */
5762 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005763regrepeat(
5764 char_u *p,
5765 long maxcount) /* maximum number of matches allowed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766{
5767 long count = 0;
5768 char_u *scan;
5769 char_u *opnd;
5770 int mask;
5771 int testval = 0;
5772
5773 scan = reginput; /* Make local copy of reginput for speed. */
5774 opnd = OPERAND(p);
5775 switch (OP(p))
5776 {
5777 case ANY:
5778 case ANY + ADD_NL:
5779 while (count < maxcount)
5780 {
5781 /* Matching anything means we continue until end-of-line (or
5782 * end-of-file for ANY + ADD_NL), only limited by maxcount. */
5783 while (*scan != NUL && count < maxcount)
5784 {
5785 ++count;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005786 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 }
Bram Moolenaar640009d2006-10-17 16:48:26 +00005788 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline
5789 || reg_line_lbr || count == maxcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 break;
5791 ++count; /* count the line-break */
5792 reg_nextline();
5793 scan = reginput;
5794 if (got_int)
5795 break;
5796 }
5797 break;
5798
5799 case IDENT:
5800 case IDENT + ADD_NL:
5801 testval = TRUE;
5802 /*FALLTHROUGH*/
5803 case SIDENT:
5804 case SIDENT + ADD_NL:
5805 while (count < maxcount)
5806 {
Bram Moolenaar09ea9fc2013-05-21 00:03:02 +02005807 if (vim_isIDc(PTR2CHAR(scan)) && (testval || !VIM_ISDIGIT(*scan)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005809 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 }
5811 else if (*scan == NUL)
5812 {
Bram Moolenaar640009d2006-10-17 16:48:26 +00005813 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline
5814 || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 break;
5816 reg_nextline();
5817 scan = reginput;
5818 if (got_int)
5819 break;
5820 }
5821 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5822 ++scan;
5823 else
5824 break;
5825 ++count;
5826 }
5827 break;
5828
5829 case KWORD:
5830 case KWORD + ADD_NL:
5831 testval = TRUE;
5832 /*FALLTHROUGH*/
5833 case SKWORD:
5834 case SKWORD + ADD_NL:
5835 while (count < maxcount)
5836 {
Bram Moolenaarf813a182013-01-30 13:59:37 +01005837 if (vim_iswordp_buf(scan, reg_buf)
5838 && (testval || !VIM_ISDIGIT(*scan)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005840 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 }
5842 else if (*scan == NUL)
5843 {
Bram Moolenaar640009d2006-10-17 16:48:26 +00005844 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline
5845 || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 break;
5847 reg_nextline();
5848 scan = reginput;
5849 if (got_int)
5850 break;
5851 }
5852 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5853 ++scan;
5854 else
5855 break;
5856 ++count;
5857 }
5858 break;
5859
5860 case FNAME:
5861 case FNAME + ADD_NL:
5862 testval = TRUE;
5863 /*FALLTHROUGH*/
5864 case SFNAME:
5865 case SFNAME + ADD_NL:
5866 while (count < maxcount)
5867 {
Bram Moolenaar09ea9fc2013-05-21 00:03:02 +02005868 if (vim_isfilec(PTR2CHAR(scan)) && (testval || !VIM_ISDIGIT(*scan)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005870 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 }
5872 else if (*scan == NUL)
5873 {
Bram Moolenaar640009d2006-10-17 16:48:26 +00005874 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline
5875 || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 break;
5877 reg_nextline();
5878 scan = reginput;
5879 if (got_int)
5880 break;
5881 }
5882 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5883 ++scan;
5884 else
5885 break;
5886 ++count;
5887 }
5888 break;
5889
5890 case PRINT:
5891 case PRINT + ADD_NL:
5892 testval = TRUE;
5893 /*FALLTHROUGH*/
5894 case SPRINT:
5895 case SPRINT + ADD_NL:
5896 while (count < maxcount)
5897 {
5898 if (*scan == NUL)
5899 {
Bram Moolenaar640009d2006-10-17 16:48:26 +00005900 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline
5901 || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 break;
5903 reg_nextline();
5904 scan = reginput;
5905 if (got_int)
5906 break;
5907 }
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02005908 else if (vim_isprintc(PTR2CHAR(scan)) == 1
5909 && (testval || !VIM_ISDIGIT(*scan)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005911 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 }
5913 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5914 ++scan;
5915 else
5916 break;
5917 ++count;
5918 }
5919 break;
5920
5921 case WHITE:
5922 case WHITE + ADD_NL:
5923 testval = mask = RI_WHITE;
5924do_class:
5925 while (count < maxcount)
5926 {
5927#ifdef FEAT_MBYTE
5928 int l;
5929#endif
5930 if (*scan == NUL)
5931 {
Bram Moolenaar640009d2006-10-17 16:48:26 +00005932 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline
5933 || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 break;
5935 reg_nextline();
5936 scan = reginput;
5937 if (got_int)
5938 break;
5939 }
5940#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005941 else if (has_mbyte && (l = (*mb_ptr2len)(scan)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 {
5943 if (testval != 0)
5944 break;
5945 scan += l;
5946 }
5947#endif
5948 else if ((class_tab[*scan] & mask) == testval)
5949 ++scan;
5950 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5951 ++scan;
5952 else
5953 break;
5954 ++count;
5955 }
5956 break;
5957
5958 case NWHITE:
5959 case NWHITE + ADD_NL:
5960 mask = RI_WHITE;
5961 goto do_class;
5962 case DIGIT:
5963 case DIGIT + ADD_NL:
5964 testval = mask = RI_DIGIT;
5965 goto do_class;
5966 case NDIGIT:
5967 case NDIGIT + ADD_NL:
5968 mask = RI_DIGIT;
5969 goto do_class;
5970 case HEX:
5971 case HEX + ADD_NL:
5972 testval = mask = RI_HEX;
5973 goto do_class;
5974 case NHEX:
5975 case NHEX + ADD_NL:
5976 mask = RI_HEX;
5977 goto do_class;
5978 case OCTAL:
5979 case OCTAL + ADD_NL:
5980 testval = mask = RI_OCTAL;
5981 goto do_class;
5982 case NOCTAL:
5983 case NOCTAL + ADD_NL:
5984 mask = RI_OCTAL;
5985 goto do_class;
5986 case WORD:
5987 case WORD + ADD_NL:
5988 testval = mask = RI_WORD;
5989 goto do_class;
5990 case NWORD:
5991 case NWORD + ADD_NL:
5992 mask = RI_WORD;
5993 goto do_class;
5994 case HEAD:
5995 case HEAD + ADD_NL:
5996 testval = mask = RI_HEAD;
5997 goto do_class;
5998 case NHEAD:
5999 case NHEAD + ADD_NL:
6000 mask = RI_HEAD;
6001 goto do_class;
6002 case ALPHA:
6003 case ALPHA + ADD_NL:
6004 testval = mask = RI_ALPHA;
6005 goto do_class;
6006 case NALPHA:
6007 case NALPHA + ADD_NL:
6008 mask = RI_ALPHA;
6009 goto do_class;
6010 case LOWER:
6011 case LOWER + ADD_NL:
6012 testval = mask = RI_LOWER;
6013 goto do_class;
6014 case NLOWER:
6015 case NLOWER + ADD_NL:
6016 mask = RI_LOWER;
6017 goto do_class;
6018 case UPPER:
6019 case UPPER + ADD_NL:
6020 testval = mask = RI_UPPER;
6021 goto do_class;
6022 case NUPPER:
6023 case NUPPER + ADD_NL:
6024 mask = RI_UPPER;
6025 goto do_class;
6026
6027 case EXACTLY:
6028 {
6029 int cu, cl;
6030
6031 /* This doesn't do a multi-byte character, because a MULTIBYTECODE
Bram Moolenaara245a5b2007-08-11 11:58:23 +00006032 * would have been used for it. It does handle single-byte
6033 * characters, such as latin1. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 if (ireg_ic)
6035 {
Bram Moolenaara245a5b2007-08-11 11:58:23 +00006036 cu = MB_TOUPPER(*opnd);
6037 cl = MB_TOLOWER(*opnd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 while (count < maxcount && (*scan == cu || *scan == cl))
6039 {
6040 count++;
6041 scan++;
6042 }
6043 }
6044 else
6045 {
6046 cu = *opnd;
6047 while (count < maxcount && *scan == cu)
6048 {
6049 count++;
6050 scan++;
6051 }
6052 }
6053 break;
6054 }
6055
6056#ifdef FEAT_MBYTE
6057 case MULTIBYTECODE:
6058 {
6059 int i, len, cf = 0;
6060
6061 /* Safety check (just in case 'encoding' was changed since
6062 * compiling the program). */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006063 if ((len = (*mb_ptr2len)(opnd)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 {
6065 if (ireg_ic && enc_utf8)
6066 cf = utf_fold(utf_ptr2char(opnd));
Bram Moolenaar069dd082015-05-04 09:56:49 +02006067 while (count < maxcount && (*mb_ptr2len)(scan) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 {
6069 for (i = 0; i < len; ++i)
6070 if (opnd[i] != scan[i])
6071 break;
6072 if (i < len && (!ireg_ic || !enc_utf8
6073 || utf_fold(utf_ptr2char(scan)) != cf))
6074 break;
6075 scan += len;
6076 ++count;
6077 }
6078 }
6079 }
6080 break;
6081#endif
6082
6083 case ANYOF:
6084 case ANYOF + ADD_NL:
6085 testval = TRUE;
6086 /*FALLTHROUGH*/
6087
6088 case ANYBUT:
6089 case ANYBUT + ADD_NL:
6090 while (count < maxcount)
6091 {
6092#ifdef FEAT_MBYTE
6093 int len;
6094#endif
6095 if (*scan == NUL)
6096 {
Bram Moolenaar640009d2006-10-17 16:48:26 +00006097 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline
6098 || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 break;
6100 reg_nextline();
6101 scan = reginput;
6102 if (got_int)
6103 break;
6104 }
6105 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
6106 ++scan;
6107#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006108 else if (has_mbyte && (len = (*mb_ptr2len)(scan)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 {
6110 if ((cstrchr(opnd, (*mb_ptr2char)(scan)) == NULL) == testval)
6111 break;
6112 scan += len;
6113 }
6114#endif
6115 else
6116 {
6117 if ((cstrchr(opnd, *scan) == NULL) == testval)
6118 break;
6119 ++scan;
6120 }
6121 ++count;
6122 }
6123 break;
6124
6125 case NEWL:
6126 while (count < maxcount
Bram Moolenaar640009d2006-10-17 16:48:26 +00006127 && ((*scan == NUL && reglnum <= reg_maxline && !reg_line_lbr
6128 && REG_MULTI) || (*scan == '\n' && reg_line_lbr)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 {
6130 count++;
6131 if (reg_line_lbr)
6132 ADVANCE_REGINPUT();
6133 else
6134 reg_nextline();
6135 scan = reginput;
6136 if (got_int)
6137 break;
6138 }
6139 break;
6140
6141 default: /* Oh dear. Called inappropriately. */
6142 EMSG(_(e_re_corr));
6143#ifdef DEBUG
6144 printf("Called regrepeat with op code %d\n", OP(p));
6145#endif
6146 break;
6147 }
6148
6149 reginput = scan;
6150
6151 return (int)count;
6152}
6153
6154/*
6155 * regnext - dig the "next" pointer out of a node
Bram Moolenaard3005802009-11-25 17:21:32 +00006156 * Returns NULL when calculating size, when there is no next item and when
6157 * there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 */
6159 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01006160regnext(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161{
6162 int offset;
6163
Bram Moolenaard3005802009-11-25 17:21:32 +00006164 if (p == JUST_CALC_SIZE || reg_toolong)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 return NULL;
6166
6167 offset = NEXT(p);
6168 if (offset == 0)
6169 return NULL;
6170
Bram Moolenaar582fd852005-03-28 20:58:01 +00006171 if (OP(p) == BACK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 return p - offset;
6173 else
6174 return p + offset;
6175}
6176
6177/*
6178 * Check the regexp program for its magic number.
6179 * Return TRUE if it's wrong.
6180 */
6181 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006182prog_magic_wrong(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006184 regprog_T *prog;
6185
6186 prog = REG_MULTI ? reg_mmatch->regprog : reg_match->regprog;
6187 if (prog->engine == &nfa_regengine)
6188 /* For NFA matcher we don't check the magic */
6189 return FALSE;
6190
6191 if (UCHARAT(((bt_regprog_T *)prog)->program) != REGMAGIC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 {
6193 EMSG(_(e_re_corr));
6194 return TRUE;
6195 }
6196 return FALSE;
6197}
6198
6199/*
6200 * Cleanup the subexpressions, if this wasn't done yet.
6201 * This construction is used to clear the subexpressions only when they are
6202 * used (to increase speed).
6203 */
6204 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006205cleanup_subexpr(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206{
6207 if (need_clear_subexpr)
6208 {
6209 if (REG_MULTI)
6210 {
6211 /* Use 0xff to set lnum to -1 */
6212 vim_memset(reg_startpos, 0xff, sizeof(lpos_T) * NSUBEXP);
6213 vim_memset(reg_endpos, 0xff, sizeof(lpos_T) * NSUBEXP);
6214 }
6215 else
6216 {
6217 vim_memset(reg_startp, 0, sizeof(char_u *) * NSUBEXP);
6218 vim_memset(reg_endp, 0, sizeof(char_u *) * NSUBEXP);
6219 }
6220 need_clear_subexpr = FALSE;
6221 }
6222}
6223
6224#ifdef FEAT_SYN_HL
6225 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006226cleanup_zsubexpr(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227{
6228 if (need_clear_zsubexpr)
6229 {
6230 if (REG_MULTI)
6231 {
6232 /* Use 0xff to set lnum to -1 */
6233 vim_memset(reg_startzpos, 0xff, sizeof(lpos_T) * NSUBEXP);
6234 vim_memset(reg_endzpos, 0xff, sizeof(lpos_T) * NSUBEXP);
6235 }
6236 else
6237 {
6238 vim_memset(reg_startzp, 0, sizeof(char_u *) * NSUBEXP);
6239 vim_memset(reg_endzp, 0, sizeof(char_u *) * NSUBEXP);
6240 }
6241 need_clear_zsubexpr = FALSE;
6242 }
6243}
6244#endif
6245
6246/*
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006247 * Save the current subexpr to "bp", so that they can be restored
6248 * later by restore_subexpr().
6249 */
6250 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006251save_subexpr(regbehind_T *bp)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006252{
6253 int i;
6254
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006255 /* When "need_clear_subexpr" is set we don't need to save the values, only
6256 * remember that this flag needs to be set again when restoring. */
6257 bp->save_need_clear_subexpr = need_clear_subexpr;
6258 if (!need_clear_subexpr)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006259 {
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006260 for (i = 0; i < NSUBEXP; ++i)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006261 {
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006262 if (REG_MULTI)
6263 {
6264 bp->save_start[i].se_u.pos = reg_startpos[i];
6265 bp->save_end[i].se_u.pos = reg_endpos[i];
6266 }
6267 else
6268 {
6269 bp->save_start[i].se_u.ptr = reg_startp[i];
6270 bp->save_end[i].se_u.ptr = reg_endp[i];
6271 }
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006272 }
6273 }
6274}
6275
6276/*
6277 * Restore the subexpr from "bp".
6278 */
6279 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006280restore_subexpr(regbehind_T *bp)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006281{
6282 int i;
6283
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006284 /* Only need to restore saved values when they are not to be cleared. */
6285 need_clear_subexpr = bp->save_need_clear_subexpr;
6286 if (!need_clear_subexpr)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006287 {
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006288 for (i = 0; i < NSUBEXP; ++i)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006289 {
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006290 if (REG_MULTI)
6291 {
6292 reg_startpos[i] = bp->save_start[i].se_u.pos;
6293 reg_endpos[i] = bp->save_end[i].se_u.pos;
6294 }
6295 else
6296 {
6297 reg_startp[i] = bp->save_start[i].se_u.ptr;
6298 reg_endp[i] = bp->save_end[i].se_u.ptr;
6299 }
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006300 }
6301 }
6302}
6303
6304/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 * Advance reglnum, regline and reginput to the next line.
6306 */
6307 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006308reg_nextline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309{
6310 regline = reg_getline(++reglnum);
6311 reginput = regline;
6312 fast_breakcheck();
6313}
6314
6315/*
6316 * Save the input line and position in a regsave_T.
6317 */
6318 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006319reg_save(regsave_T *save, garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320{
6321 if (REG_MULTI)
6322 {
6323 save->rs_u.pos.col = (colnr_T)(reginput - regline);
6324 save->rs_u.pos.lnum = reglnum;
6325 }
6326 else
6327 save->rs_u.ptr = reginput;
Bram Moolenaar582fd852005-03-28 20:58:01 +00006328 save->rs_len = gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329}
6330
6331/*
6332 * Restore the input line and position from a regsave_T.
6333 */
6334 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006335reg_restore(regsave_T *save, garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336{
6337 if (REG_MULTI)
6338 {
6339 if (reglnum != save->rs_u.pos.lnum)
6340 {
6341 /* only call reg_getline() when the line number changed to save
6342 * a bit of time */
6343 reglnum = save->rs_u.pos.lnum;
6344 regline = reg_getline(reglnum);
6345 }
6346 reginput = regline + save->rs_u.pos.col;
6347 }
6348 else
6349 reginput = save->rs_u.ptr;
Bram Moolenaar582fd852005-03-28 20:58:01 +00006350 gap->ga_len = save->rs_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351}
6352
6353/*
6354 * Return TRUE if current position is equal to saved position.
6355 */
6356 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006357reg_save_equal(regsave_T *save)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358{
6359 if (REG_MULTI)
6360 return reglnum == save->rs_u.pos.lnum
6361 && reginput == regline + save->rs_u.pos.col;
6362 return reginput == save->rs_u.ptr;
6363}
6364
6365/*
6366 * Tentatively set the sub-expression start to the current position (after
6367 * calling regmatch() they will have changed). Need to save the existing
6368 * values for when there is no match.
6369 * Use se_save() to use pointer (save_se_multi()) or position (save_se_one()),
6370 * depending on REG_MULTI.
6371 */
6372 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006373save_se_multi(save_se_T *savep, lpos_T *posp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374{
6375 savep->se_u.pos = *posp;
6376 posp->lnum = reglnum;
6377 posp->col = (colnr_T)(reginput - regline);
6378}
6379
6380 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006381save_se_one(save_se_T *savep, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382{
6383 savep->se_u.ptr = *pp;
6384 *pp = reginput;
6385}
6386
6387/*
6388 * Compare a number with the operand of RE_LNUM, RE_COL or RE_VCOL.
6389 */
6390 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006391re_num_cmp(long_u val, char_u *scan)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392{
6393 long_u n = OPERAND_MIN(scan);
6394
6395 if (OPERAND_CMP(scan) == '>')
6396 return val > n;
6397 if (OPERAND_CMP(scan) == '<')
6398 return val < n;
6399 return val == n;
6400}
6401
Bram Moolenaar580abea2013-06-14 20:31:28 +02006402/*
6403 * Check whether a backreference matches.
6404 * Returns RA_FAIL, RA_NOMATCH or RA_MATCH.
Bram Moolenaar438ee5b2013-11-21 17:13:00 +01006405 * If "bytelen" is not NULL, it is set to the byte length of the match in the
6406 * last line.
Bram Moolenaar580abea2013-06-14 20:31:28 +02006407 */
6408 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006409match_with_backref(
6410 linenr_T start_lnum,
6411 colnr_T start_col,
6412 linenr_T end_lnum,
6413 colnr_T end_col,
6414 int *bytelen)
Bram Moolenaar580abea2013-06-14 20:31:28 +02006415{
6416 linenr_T clnum = start_lnum;
6417 colnr_T ccol = start_col;
6418 int len;
6419 char_u *p;
6420
6421 if (bytelen != NULL)
6422 *bytelen = 0;
6423 for (;;)
6424 {
6425 /* Since getting one line may invalidate the other, need to make copy.
6426 * Slow! */
6427 if (regline != reg_tofree)
6428 {
6429 len = (int)STRLEN(regline);
6430 if (reg_tofree == NULL || len >= (int)reg_tofreelen)
6431 {
6432 len += 50; /* get some extra */
6433 vim_free(reg_tofree);
6434 reg_tofree = alloc(len);
6435 if (reg_tofree == NULL)
6436 return RA_FAIL; /* out of memory!*/
6437 reg_tofreelen = len;
6438 }
6439 STRCPY(reg_tofree, regline);
6440 reginput = reg_tofree + (reginput - regline);
6441 regline = reg_tofree;
6442 }
6443
6444 /* Get the line to compare with. */
6445 p = reg_getline(clnum);
6446 if (clnum == end_lnum)
6447 len = end_col - ccol;
6448 else
6449 len = (int)STRLEN(p + ccol);
6450
6451 if (cstrncmp(p + ccol, reginput, &len) != 0)
6452 return RA_NOMATCH; /* doesn't match */
6453 if (bytelen != NULL)
6454 *bytelen += len;
6455 if (clnum == end_lnum)
6456 break; /* match and at end! */
6457 if (reglnum >= reg_maxline)
6458 return RA_NOMATCH; /* text too short */
6459
6460 /* Advance to next line. */
6461 reg_nextline();
Bram Moolenaar438ee5b2013-11-21 17:13:00 +01006462 if (bytelen != NULL)
6463 *bytelen = 0;
Bram Moolenaar580abea2013-06-14 20:31:28 +02006464 ++clnum;
6465 ccol = 0;
6466 if (got_int)
6467 return RA_FAIL;
6468 }
6469
6470 /* found a match! Note that regline may now point to a copy of the line,
6471 * that should not matter. */
6472 return RA_MATCH;
6473}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006475#ifdef BT_REGEXP_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476
6477/*
6478 * regdump - dump a regexp onto stdout in vaguely comprehensible form
6479 */
6480 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006481regdump(char_u *pattern, bt_regprog_T *r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482{
6483 char_u *s;
6484 int op = EXACTLY; /* Arbitrary non-END op. */
6485 char_u *next;
6486 char_u *end = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006487 FILE *f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006489#ifdef BT_REGEXP_LOG
6490 f = fopen("bt_regexp_log.log", "a");
6491#else
6492 f = stdout;
6493#endif
6494 if (f == NULL)
6495 return;
6496 fprintf(f, "-------------------------------------\n\r\nregcomp(%s):\r\n", pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497
6498 s = r->program + 1;
6499 /*
6500 * Loop until we find the END that isn't before a referred next (an END
6501 * can also appear in a NOMATCH operand).
6502 */
6503 while (op != END || s <= end)
6504 {
6505 op = OP(s);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006506 fprintf(f, "%2d%s", (int)(s - r->program), regprop(s)); /* Where, what. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 next = regnext(s);
6508 if (next == NULL) /* Next ptr. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006509 fprintf(f, "(0)");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006511 fprintf(f, "(%d)", (int)((s - r->program) + (next - s)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 if (end < next)
6513 end = next;
6514 if (op == BRACE_LIMITS)
6515 {
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02006516 /* Two ints */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006517 fprintf(f, " minval %ld, maxval %ld", OPERAND_MIN(s), OPERAND_MAX(s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 s += 8;
6519 }
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02006520 else if (op == BEHIND || op == NOBEHIND)
6521 {
6522 /* one int */
6523 fprintf(f, " count %ld", OPERAND_MIN(s));
6524 s += 4;
6525 }
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02006526 else if (op == RE_LNUM || op == RE_COL || op == RE_VCOL)
6527 {
6528 /* one int plus comperator */
6529 fprintf(f, " count %ld", OPERAND_MIN(s));
6530 s += 5;
6531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 s += 3;
6533 if (op == ANYOF || op == ANYOF + ADD_NL
6534 || op == ANYBUT || op == ANYBUT + ADD_NL
6535 || op == EXACTLY)
6536 {
6537 /* Literal string, where present. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006538 fprintf(f, "\nxxxxxxxxx\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 while (*s != NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006540 fprintf(f, "%c", *s++);
6541 fprintf(f, "\nxxxxxxxxx\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 s++;
6543 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006544 fprintf(f, "\r\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 }
6546
6547 /* Header fields of interest. */
6548 if (r->regstart != NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006549 fprintf(f, "start `%s' 0x%x; ", r->regstart < 256
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 ? (char *)transchar(r->regstart)
6551 : "multibyte", r->regstart);
6552 if (r->reganch)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006553 fprintf(f, "anchored; ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 if (r->regmust != NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006555 fprintf(f, "must have \"%s\"", r->regmust);
6556 fprintf(f, "\r\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006558#ifdef BT_REGEXP_LOG
6559 fclose(f);
6560#endif
6561}
6562#endif /* BT_REGEXP_DUMP */
6563
6564#ifdef DEBUG
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565/*
6566 * regprop - printable representation of opcode
6567 */
6568 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01006569regprop(char_u *op)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006571 char *p;
6572 static char buf[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006574 STRCPY(buf, ":");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006576 switch ((int) OP(op))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 {
6578 case BOL:
6579 p = "BOL";
6580 break;
6581 case EOL:
6582 p = "EOL";
6583 break;
6584 case RE_BOF:
6585 p = "BOF";
6586 break;
6587 case RE_EOF:
6588 p = "EOF";
6589 break;
6590 case CURSOR:
6591 p = "CURSOR";
6592 break;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00006593 case RE_VISUAL:
6594 p = "RE_VISUAL";
6595 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 case RE_LNUM:
6597 p = "RE_LNUM";
6598 break;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00006599 case RE_MARK:
6600 p = "RE_MARK";
6601 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 case RE_COL:
6603 p = "RE_COL";
6604 break;
6605 case RE_VCOL:
6606 p = "RE_VCOL";
6607 break;
6608 case BOW:
6609 p = "BOW";
6610 break;
6611 case EOW:
6612 p = "EOW";
6613 break;
6614 case ANY:
6615 p = "ANY";
6616 break;
6617 case ANY + ADD_NL:
6618 p = "ANY+NL";
6619 break;
6620 case ANYOF:
6621 p = "ANYOF";
6622 break;
6623 case ANYOF + ADD_NL:
6624 p = "ANYOF+NL";
6625 break;
6626 case ANYBUT:
6627 p = "ANYBUT";
6628 break;
6629 case ANYBUT + ADD_NL:
6630 p = "ANYBUT+NL";
6631 break;
6632 case IDENT:
6633 p = "IDENT";
6634 break;
6635 case IDENT + ADD_NL:
6636 p = "IDENT+NL";
6637 break;
6638 case SIDENT:
6639 p = "SIDENT";
6640 break;
6641 case SIDENT + ADD_NL:
6642 p = "SIDENT+NL";
6643 break;
6644 case KWORD:
6645 p = "KWORD";
6646 break;
6647 case KWORD + ADD_NL:
6648 p = "KWORD+NL";
6649 break;
6650 case SKWORD:
6651 p = "SKWORD";
6652 break;
6653 case SKWORD + ADD_NL:
6654 p = "SKWORD+NL";
6655 break;
6656 case FNAME:
6657 p = "FNAME";
6658 break;
6659 case FNAME + ADD_NL:
6660 p = "FNAME+NL";
6661 break;
6662 case SFNAME:
6663 p = "SFNAME";
6664 break;
6665 case SFNAME + ADD_NL:
6666 p = "SFNAME+NL";
6667 break;
6668 case PRINT:
6669 p = "PRINT";
6670 break;
6671 case PRINT + ADD_NL:
6672 p = "PRINT+NL";
6673 break;
6674 case SPRINT:
6675 p = "SPRINT";
6676 break;
6677 case SPRINT + ADD_NL:
6678 p = "SPRINT+NL";
6679 break;
6680 case WHITE:
6681 p = "WHITE";
6682 break;
6683 case WHITE + ADD_NL:
6684 p = "WHITE+NL";
6685 break;
6686 case NWHITE:
6687 p = "NWHITE";
6688 break;
6689 case NWHITE + ADD_NL:
6690 p = "NWHITE+NL";
6691 break;
6692 case DIGIT:
6693 p = "DIGIT";
6694 break;
6695 case DIGIT + ADD_NL:
6696 p = "DIGIT+NL";
6697 break;
6698 case NDIGIT:
6699 p = "NDIGIT";
6700 break;
6701 case NDIGIT + ADD_NL:
6702 p = "NDIGIT+NL";
6703 break;
6704 case HEX:
6705 p = "HEX";
6706 break;
6707 case HEX + ADD_NL:
6708 p = "HEX+NL";
6709 break;
6710 case NHEX:
6711 p = "NHEX";
6712 break;
6713 case NHEX + ADD_NL:
6714 p = "NHEX+NL";
6715 break;
6716 case OCTAL:
6717 p = "OCTAL";
6718 break;
6719 case OCTAL + ADD_NL:
6720 p = "OCTAL+NL";
6721 break;
6722 case NOCTAL:
6723 p = "NOCTAL";
6724 break;
6725 case NOCTAL + ADD_NL:
6726 p = "NOCTAL+NL";
6727 break;
6728 case WORD:
6729 p = "WORD";
6730 break;
6731 case WORD + ADD_NL:
6732 p = "WORD+NL";
6733 break;
6734 case NWORD:
6735 p = "NWORD";
6736 break;
6737 case NWORD + ADD_NL:
6738 p = "NWORD+NL";
6739 break;
6740 case HEAD:
6741 p = "HEAD";
6742 break;
6743 case HEAD + ADD_NL:
6744 p = "HEAD+NL";
6745 break;
6746 case NHEAD:
6747 p = "NHEAD";
6748 break;
6749 case NHEAD + ADD_NL:
6750 p = "NHEAD+NL";
6751 break;
6752 case ALPHA:
6753 p = "ALPHA";
6754 break;
6755 case ALPHA + ADD_NL:
6756 p = "ALPHA+NL";
6757 break;
6758 case NALPHA:
6759 p = "NALPHA";
6760 break;
6761 case NALPHA + ADD_NL:
6762 p = "NALPHA+NL";
6763 break;
6764 case LOWER:
6765 p = "LOWER";
6766 break;
6767 case LOWER + ADD_NL:
6768 p = "LOWER+NL";
6769 break;
6770 case NLOWER:
6771 p = "NLOWER";
6772 break;
6773 case NLOWER + ADD_NL:
6774 p = "NLOWER+NL";
6775 break;
6776 case UPPER:
6777 p = "UPPER";
6778 break;
6779 case UPPER + ADD_NL:
6780 p = "UPPER+NL";
6781 break;
6782 case NUPPER:
6783 p = "NUPPER";
6784 break;
6785 case NUPPER + ADD_NL:
6786 p = "NUPPER+NL";
6787 break;
6788 case BRANCH:
6789 p = "BRANCH";
6790 break;
6791 case EXACTLY:
6792 p = "EXACTLY";
6793 break;
6794 case NOTHING:
6795 p = "NOTHING";
6796 break;
6797 case BACK:
6798 p = "BACK";
6799 break;
6800 case END:
6801 p = "END";
6802 break;
6803 case MOPEN + 0:
6804 p = "MATCH START";
6805 break;
6806 case MOPEN + 1:
6807 case MOPEN + 2:
6808 case MOPEN + 3:
6809 case MOPEN + 4:
6810 case MOPEN + 5:
6811 case MOPEN + 6:
6812 case MOPEN + 7:
6813 case MOPEN + 8:
6814 case MOPEN + 9:
6815 sprintf(buf + STRLEN(buf), "MOPEN%d", OP(op) - MOPEN);
6816 p = NULL;
6817 break;
6818 case MCLOSE + 0:
6819 p = "MATCH END";
6820 break;
6821 case MCLOSE + 1:
6822 case MCLOSE + 2:
6823 case MCLOSE + 3:
6824 case MCLOSE + 4:
6825 case MCLOSE + 5:
6826 case MCLOSE + 6:
6827 case MCLOSE + 7:
6828 case MCLOSE + 8:
6829 case MCLOSE + 9:
6830 sprintf(buf + STRLEN(buf), "MCLOSE%d", OP(op) - MCLOSE);
6831 p = NULL;
6832 break;
6833 case BACKREF + 1:
6834 case BACKREF + 2:
6835 case BACKREF + 3:
6836 case BACKREF + 4:
6837 case BACKREF + 5:
6838 case BACKREF + 6:
6839 case BACKREF + 7:
6840 case BACKREF + 8:
6841 case BACKREF + 9:
6842 sprintf(buf + STRLEN(buf), "BACKREF%d", OP(op) - BACKREF);
6843 p = NULL;
6844 break;
6845 case NOPEN:
6846 p = "NOPEN";
6847 break;
6848 case NCLOSE:
6849 p = "NCLOSE";
6850 break;
6851#ifdef FEAT_SYN_HL
6852 case ZOPEN + 1:
6853 case ZOPEN + 2:
6854 case ZOPEN + 3:
6855 case ZOPEN + 4:
6856 case ZOPEN + 5:
6857 case ZOPEN + 6:
6858 case ZOPEN + 7:
6859 case ZOPEN + 8:
6860 case ZOPEN + 9:
6861 sprintf(buf + STRLEN(buf), "ZOPEN%d", OP(op) - ZOPEN);
6862 p = NULL;
6863 break;
6864 case ZCLOSE + 1:
6865 case ZCLOSE + 2:
6866 case ZCLOSE + 3:
6867 case ZCLOSE + 4:
6868 case ZCLOSE + 5:
6869 case ZCLOSE + 6:
6870 case ZCLOSE + 7:
6871 case ZCLOSE + 8:
6872 case ZCLOSE + 9:
6873 sprintf(buf + STRLEN(buf), "ZCLOSE%d", OP(op) - ZCLOSE);
6874 p = NULL;
6875 break;
6876 case ZREF + 1:
6877 case ZREF + 2:
6878 case ZREF + 3:
6879 case ZREF + 4:
6880 case ZREF + 5:
6881 case ZREF + 6:
6882 case ZREF + 7:
6883 case ZREF + 8:
6884 case ZREF + 9:
6885 sprintf(buf + STRLEN(buf), "ZREF%d", OP(op) - ZREF);
6886 p = NULL;
6887 break;
6888#endif
6889 case STAR:
6890 p = "STAR";
6891 break;
6892 case PLUS:
6893 p = "PLUS";
6894 break;
6895 case NOMATCH:
6896 p = "NOMATCH";
6897 break;
6898 case MATCH:
6899 p = "MATCH";
6900 break;
6901 case BEHIND:
6902 p = "BEHIND";
6903 break;
6904 case NOBEHIND:
6905 p = "NOBEHIND";
6906 break;
6907 case SUBPAT:
6908 p = "SUBPAT";
6909 break;
6910 case BRACE_LIMITS:
6911 p = "BRACE_LIMITS";
6912 break;
6913 case BRACE_SIMPLE:
6914 p = "BRACE_SIMPLE";
6915 break;
6916 case BRACE_COMPLEX + 0:
6917 case BRACE_COMPLEX + 1:
6918 case BRACE_COMPLEX + 2:
6919 case BRACE_COMPLEX + 3:
6920 case BRACE_COMPLEX + 4:
6921 case BRACE_COMPLEX + 5:
6922 case BRACE_COMPLEX + 6:
6923 case BRACE_COMPLEX + 7:
6924 case BRACE_COMPLEX + 8:
6925 case BRACE_COMPLEX + 9:
6926 sprintf(buf + STRLEN(buf), "BRACE_COMPLEX%d", OP(op) - BRACE_COMPLEX);
6927 p = NULL;
6928 break;
6929#ifdef FEAT_MBYTE
6930 case MULTIBYTECODE:
6931 p = "MULTIBYTECODE";
6932 break;
6933#endif
6934 case NEWL:
6935 p = "NEWL";
6936 break;
6937 default:
6938 sprintf(buf + STRLEN(buf), "corrupt %d", OP(op));
6939 p = NULL;
6940 break;
6941 }
6942 if (p != NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006943 STRCAT(buf, p);
6944 return (char_u *)buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006946#endif /* DEBUG */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947
Bram Moolenaarfb031402014-09-09 17:18:49 +02006948/*
6949 * Used in a place where no * or \+ can follow.
6950 */
6951 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006952re_mult_next(char *what)
Bram Moolenaarfb031402014-09-09 17:18:49 +02006953{
6954 if (re_multi_type(peekchr()) == MULTI_MULT)
6955 EMSG2_RET_FAIL(_("E888: (NFA regexp) cannot repeat %s"), what);
6956 return OK;
6957}
6958
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006960static void mb_decompose(int c, int *c1, int *c2, int *c3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961
6962typedef struct
6963{
6964 int a, b, c;
6965} decomp_T;
6966
6967
6968/* 0xfb20 - 0xfb4f */
Bram Moolenaard6f676d2005-06-01 21:51:55 +00006969static decomp_T decomp_table[0xfb4f-0xfb20+1] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970{
6971 {0x5e2,0,0}, /* 0xfb20 alt ayin */
6972 {0x5d0,0,0}, /* 0xfb21 alt alef */
6973 {0x5d3,0,0}, /* 0xfb22 alt dalet */
6974 {0x5d4,0,0}, /* 0xfb23 alt he */
6975 {0x5db,0,0}, /* 0xfb24 alt kaf */
6976 {0x5dc,0,0}, /* 0xfb25 alt lamed */
6977 {0x5dd,0,0}, /* 0xfb26 alt mem-sofit */
6978 {0x5e8,0,0}, /* 0xfb27 alt resh */
6979 {0x5ea,0,0}, /* 0xfb28 alt tav */
6980 {'+', 0, 0}, /* 0xfb29 alt plus */
6981 {0x5e9, 0x5c1, 0}, /* 0xfb2a shin+shin-dot */
6982 {0x5e9, 0x5c2, 0}, /* 0xfb2b shin+sin-dot */
6983 {0x5e9, 0x5c1, 0x5bc}, /* 0xfb2c shin+shin-dot+dagesh */
6984 {0x5e9, 0x5c2, 0x5bc}, /* 0xfb2d shin+sin-dot+dagesh */
6985 {0x5d0, 0x5b7, 0}, /* 0xfb2e alef+patah */
6986 {0x5d0, 0x5b8, 0}, /* 0xfb2f alef+qamats */
6987 {0x5d0, 0x5b4, 0}, /* 0xfb30 alef+hiriq */
6988 {0x5d1, 0x5bc, 0}, /* 0xfb31 bet+dagesh */
6989 {0x5d2, 0x5bc, 0}, /* 0xfb32 gimel+dagesh */
6990 {0x5d3, 0x5bc, 0}, /* 0xfb33 dalet+dagesh */
6991 {0x5d4, 0x5bc, 0}, /* 0xfb34 he+dagesh */
6992 {0x5d5, 0x5bc, 0}, /* 0xfb35 vav+dagesh */
6993 {0x5d6, 0x5bc, 0}, /* 0xfb36 zayin+dagesh */
6994 {0xfb37, 0, 0}, /* 0xfb37 -- UNUSED */
6995 {0x5d8, 0x5bc, 0}, /* 0xfb38 tet+dagesh */
6996 {0x5d9, 0x5bc, 0}, /* 0xfb39 yud+dagesh */
6997 {0x5da, 0x5bc, 0}, /* 0xfb3a kaf sofit+dagesh */
6998 {0x5db, 0x5bc, 0}, /* 0xfb3b kaf+dagesh */
6999 {0x5dc, 0x5bc, 0}, /* 0xfb3c lamed+dagesh */
7000 {0xfb3d, 0, 0}, /* 0xfb3d -- UNUSED */
7001 {0x5de, 0x5bc, 0}, /* 0xfb3e mem+dagesh */
7002 {0xfb3f, 0, 0}, /* 0xfb3f -- UNUSED */
7003 {0x5e0, 0x5bc, 0}, /* 0xfb40 nun+dagesh */
7004 {0x5e1, 0x5bc, 0}, /* 0xfb41 samech+dagesh */
7005 {0xfb42, 0, 0}, /* 0xfb42 -- UNUSED */
7006 {0x5e3, 0x5bc, 0}, /* 0xfb43 pe sofit+dagesh */
7007 {0x5e4, 0x5bc,0}, /* 0xfb44 pe+dagesh */
7008 {0xfb45, 0, 0}, /* 0xfb45 -- UNUSED */
7009 {0x5e6, 0x5bc, 0}, /* 0xfb46 tsadi+dagesh */
7010 {0x5e7, 0x5bc, 0}, /* 0xfb47 qof+dagesh */
7011 {0x5e8, 0x5bc, 0}, /* 0xfb48 resh+dagesh */
7012 {0x5e9, 0x5bc, 0}, /* 0xfb49 shin+dagesh */
7013 {0x5ea, 0x5bc, 0}, /* 0xfb4a tav+dagesh */
7014 {0x5d5, 0x5b9, 0}, /* 0xfb4b vav+holam */
7015 {0x5d1, 0x5bf, 0}, /* 0xfb4c bet+rafe */
7016 {0x5db, 0x5bf, 0}, /* 0xfb4d kaf+rafe */
7017 {0x5e4, 0x5bf, 0}, /* 0xfb4e pe+rafe */
7018 {0x5d0, 0x5dc, 0} /* 0xfb4f alef-lamed */
7019};
7020
7021 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007022mb_decompose(int c, int *c1, int *c2, int *c3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023{
7024 decomp_T d;
7025
Bram Moolenaar2eec59e2013-05-21 21:37:20 +02007026 if (c >= 0xfb20 && c <= 0xfb4f)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 {
7028 d = decomp_table[c - 0xfb20];
7029 *c1 = d.a;
7030 *c2 = d.b;
7031 *c3 = d.c;
7032 }
7033 else
7034 {
7035 *c1 = c;
7036 *c2 = *c3 = 0;
7037 }
7038}
7039#endif
7040
7041/*
7042 * Compare two strings, ignore case if ireg_ic set.
7043 * Return 0 if strings match, non-zero otherwise.
7044 * Correct the length "*n" when composing characters are ignored.
7045 */
7046 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007047cstrncmp(char_u *s1, char_u *s2, int *n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048{
7049 int result;
7050
7051 if (!ireg_ic)
7052 result = STRNCMP(s1, s2, *n);
7053 else
7054 result = MB_STRNICMP(s1, s2, *n);
7055
7056#ifdef FEAT_MBYTE
7057 /* if it failed and it's utf8 and we want to combineignore: */
7058 if (result != 0 && enc_utf8 && ireg_icombine)
7059 {
7060 char_u *str1, *str2;
7061 int c1, c2, c11, c12;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 int junk;
7063
7064 /* we have to handle the strcmp ourselves, since it is necessary to
7065 * deal with the composing characters by ignoring them: */
7066 str1 = s1;
7067 str2 = s2;
7068 c1 = c2 = 0;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007069 while ((int)(str1 - s1) < *n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 {
7071 c1 = mb_ptr2char_adv(&str1);
7072 c2 = mb_ptr2char_adv(&str2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073
7074 /* decompose the character if necessary, into 'base' characters
7075 * because I don't care about Arabic, I will hard-code the Hebrew
7076 * which I *do* care about! So sue me... */
7077 if (c1 != c2 && (!ireg_ic || utf_fold(c1) != utf_fold(c2)))
7078 {
7079 /* decomposition necessary? */
7080 mb_decompose(c1, &c11, &junk, &junk);
7081 mb_decompose(c2, &c12, &junk, &junk);
7082 c1 = c11;
7083 c2 = c12;
7084 if (c11 != c12 && (!ireg_ic || utf_fold(c11) != utf_fold(c12)))
7085 break;
7086 }
7087 }
7088 result = c2 - c1;
7089 if (result == 0)
7090 *n = (int)(str2 - s2);
7091 }
7092#endif
7093
7094 return result;
7095}
7096
7097/*
7098 * cstrchr: This function is used a lot for simple searches, keep it fast!
7099 */
7100 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01007101cstrchr(char_u *s, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102{
7103 char_u *p;
7104 int cc;
7105
7106 if (!ireg_ic
7107#ifdef FEAT_MBYTE
7108 || (!enc_utf8 && mb_char2len(c) > 1)
7109#endif
7110 )
7111 return vim_strchr(s, c);
7112
7113 /* tolower() and toupper() can be slow, comparing twice should be a lot
7114 * faster (esp. when using MS Visual C++!).
7115 * For UTF-8 need to use folded case. */
7116#ifdef FEAT_MBYTE
7117 if (enc_utf8 && c > 0x80)
7118 cc = utf_fold(c);
7119 else
7120#endif
Bram Moolenaara245a5b2007-08-11 11:58:23 +00007121 if (MB_ISUPPER(c))
7122 cc = MB_TOLOWER(c);
7123 else if (MB_ISLOWER(c))
7124 cc = MB_TOUPPER(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 else
7126 return vim_strchr(s, c);
7127
7128#ifdef FEAT_MBYTE
7129 if (has_mbyte)
7130 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007131 for (p = s; *p != NUL; p += (*mb_ptr2len)(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 {
7133 if (enc_utf8 && c > 0x80)
7134 {
7135 if (utf_fold(utf_ptr2char(p)) == cc)
7136 return p;
7137 }
7138 else if (*p == c || *p == cc)
7139 return p;
7140 }
7141 }
7142 else
7143#endif
7144 /* Faster version for when there are no multi-byte characters. */
7145 for (p = s; *p != NUL; ++p)
7146 if (*p == c || *p == cc)
7147 return p;
7148
7149 return NULL;
7150}
7151
7152/***************************************************************
7153 * regsub stuff *
7154 ***************************************************************/
7155
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156/*
7157 * We should define ftpr as a pointer to a function returning a pointer to
7158 * a function returning a pointer to a function ...
7159 * This is impossible, so we declare a pointer to a function returning a
7160 * pointer to a function returning void. This should work for all compilers.
7161 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007162typedef void (*(*fptr_T)(int *, int))();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007164static fptr_T do_upper(int *, int);
7165static fptr_T do_Upper(int *, int);
7166static fptr_T do_lower(int *, int);
7167static fptr_T do_Lower(int *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007169static int vim_regsub_both(char_u *source, typval_T *expr, char_u *dest, int copy, int magic, int backslash);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007171 static fptr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01007172do_upper(int *d, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173{
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007174 *d = MB_TOUPPER(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007176 return (fptr_T)NULL;
7177}
7178
7179 static fptr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01007180do_Upper(int *d, int c)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007181{
7182 *d = MB_TOUPPER(c);
7183
7184 return (fptr_T)do_Upper;
7185}
7186
7187 static fptr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01007188do_lower(int *d, int c)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007189{
7190 *d = MB_TOLOWER(c);
7191
7192 return (fptr_T)NULL;
7193}
7194
7195 static fptr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01007196do_Lower(int *d, int c)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007197{
7198 *d = MB_TOLOWER(c);
7199
7200 return (fptr_T)do_Lower;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201}
7202
7203/*
7204 * regtilde(): Replace tildes in the pattern by the old pattern.
7205 *
7206 * Short explanation of the tilde: It stands for the previous replacement
7207 * pattern. If that previous pattern also contains a ~ we should go back a
7208 * step further... But we insert the previous pattern into the current one
7209 * and remember that.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007210 * This still does not handle the case where "magic" changes. So require the
7211 * user to keep his hands off of "magic".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 *
7213 * The tildes are parsed once before the first call to vim_regsub().
7214 */
7215 char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01007216regtilde(char_u *source, int magic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217{
7218 char_u *newsub = source;
7219 char_u *tmpsub;
7220 char_u *p;
7221 int len;
7222 int prevlen;
7223
7224 for (p = newsub; *p; ++p)
7225 {
7226 if ((*p == '~' && magic) || (*p == '\\' && *(p + 1) == '~' && !magic))
7227 {
7228 if (reg_prev_sub != NULL)
7229 {
7230 /* length = len(newsub) - 1 + len(prev_sub) + 1 */
7231 prevlen = (int)STRLEN(reg_prev_sub);
7232 tmpsub = alloc((unsigned)(STRLEN(newsub) + prevlen));
7233 if (tmpsub != NULL)
7234 {
7235 /* copy prefix */
7236 len = (int)(p - newsub); /* not including ~ */
7237 mch_memmove(tmpsub, newsub, (size_t)len);
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00007238 /* interpret tilde */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 mch_memmove(tmpsub + len, reg_prev_sub, (size_t)prevlen);
7240 /* copy postfix */
7241 if (!magic)
7242 ++p; /* back off \ */
7243 STRCPY(tmpsub + len + prevlen, p + 1);
7244
7245 if (newsub != source) /* already allocated newsub */
7246 vim_free(newsub);
7247 newsub = tmpsub;
7248 p = newsub + len + prevlen;
7249 }
7250 }
7251 else if (magic)
Bram Moolenaar446cb832008-06-24 21:56:24 +00007252 STRMOVE(p, p + 1); /* remove '~' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 else
Bram Moolenaar446cb832008-06-24 21:56:24 +00007254 STRMOVE(p, p + 2); /* remove '\~' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 --p;
7256 }
7257 else
7258 {
7259 if (*p == '\\' && p[1]) /* skip escaped characters */
7260 ++p;
7261#ifdef FEAT_MBYTE
7262 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007263 p += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264#endif
7265 }
7266 }
7267
7268 vim_free(reg_prev_sub);
7269 if (newsub != source) /* newsub was allocated, just keep it */
7270 reg_prev_sub = newsub;
7271 else /* no ~ found, need to save newsub */
7272 reg_prev_sub = vim_strsave(newsub);
7273 return newsub;
7274}
7275
7276#ifdef FEAT_EVAL
7277static int can_f_submatch = FALSE; /* TRUE when submatch() can be used */
7278
7279/* These pointers are used instead of reg_match and reg_mmatch for
7280 * reg_submatch(). Needed for when the substitution string is an expression
7281 * that contains a call to substitute() and submatch(). */
7282static regmatch_T *submatch_match;
7283static regmmatch_T *submatch_mmatch;
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007284static linenr_T submatch_firstlnum;
7285static linenr_T submatch_maxline;
Bram Moolenaar978287b2011-06-19 04:32:15 +02007286static int submatch_line_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287#endif
7288
7289#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardf48fb42016-07-22 21:50:18 +02007290
7291/*
7292 * Put the submatches in "argv[0]" which is a list passed into call_func() by
7293 * vim_regsub_both().
7294 */
7295 static int
7296fill_submatch_list(int argc UNUSED, typval_T *argv, int argcount)
7297{
7298 listitem_T *li;
7299 int i;
7300 char_u *s;
7301
7302 if (argcount == 0)
7303 /* called function doesn't take an argument */
7304 return 0;
7305
7306 /* Relies on sl_list to be the first item in staticList10_T. */
7307 init_static_list((staticList10_T *)(argv->vval.v_list));
7308
7309 /* There are always 10 list items in staticList10_T. */
7310 li = argv->vval.v_list->lv_first;
7311 for (i = 0; i < 10; ++i)
7312 {
7313 s = submatch_match->startp[i];
7314 if (s == NULL || submatch_match->endp[i] == NULL)
7315 s = NULL;
7316 else
7317 s = vim_strnsave(s, (int)(submatch_match->endp[i] - s));
7318 li->li_tv.v_type = VAR_STRING;
7319 li->li_tv.vval.v_string = s;
7320 li = li->li_next;
7321 }
7322 return 1;
7323}
7324
7325 static void
7326clear_submatch_list(staticList10_T *sl)
7327{
7328 int i;
7329
7330 for (i = 0; i < 10; ++i)
7331 vim_free(sl->sl_items[i].li_tv.vval.v_string);
7332}
7333
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334/*
7335 * vim_regsub() - perform substitutions after a vim_regexec() or
7336 * vim_regexec_multi() match.
7337 *
7338 * If "copy" is TRUE really copy into "dest".
7339 * If "copy" is FALSE nothing is copied, this is just to find out the length
7340 * of the result.
7341 *
7342 * If "backslash" is TRUE, a backslash will be removed later, need to double
7343 * them to keep them, and insert a backslash before a CR to avoid it being
7344 * replaced with a line break later.
7345 *
7346 * Note: The matched text must not change between the call of
7347 * vim_regexec()/vim_regexec_multi() and vim_regsub()! It would make the back
7348 * references invalid!
7349 *
7350 * Returns the size of the replacement, including terminating NUL.
7351 */
7352 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007353vim_regsub(
7354 regmatch_T *rmp,
7355 char_u *source,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007356 typval_T *expr,
Bram Moolenaar05540972016-01-30 20:31:25 +01007357 char_u *dest,
7358 int copy,
7359 int magic,
7360 int backslash)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361{
7362 reg_match = rmp;
7363 reg_mmatch = NULL;
7364 reg_maxline = 0;
Bram Moolenaar2f315ab2013-01-25 20:11:01 +01007365 reg_buf = curbuf;
Bram Moolenaar93fc4812014-04-23 18:48:47 +02007366 reg_line_lbr = TRUE;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007367 return vim_regsub_both(source, expr, dest, copy, magic, backslash);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368}
7369#endif
7370
7371 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007372vim_regsub_multi(
7373 regmmatch_T *rmp,
7374 linenr_T lnum,
7375 char_u *source,
7376 char_u *dest,
7377 int copy,
7378 int magic,
7379 int backslash)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380{
7381 reg_match = NULL;
7382 reg_mmatch = rmp;
7383 reg_buf = curbuf; /* always works on the current buffer! */
7384 reg_firstlnum = lnum;
7385 reg_maxline = curbuf->b_ml.ml_line_count - lnum;
Bram Moolenaar93fc4812014-04-23 18:48:47 +02007386 reg_line_lbr = FALSE;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007387 return vim_regsub_both(source, NULL, dest, copy, magic, backslash);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388}
7389
7390 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007391vim_regsub_both(
7392 char_u *source,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007393 typval_T *expr,
Bram Moolenaar05540972016-01-30 20:31:25 +01007394 char_u *dest,
7395 int copy,
7396 int magic,
7397 int backslash)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398{
7399 char_u *src;
7400 char_u *dst;
7401 char_u *s;
7402 int c;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007403 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 int no = -1;
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007405 fptr_T func_all = (fptr_T)NULL;
7406 fptr_T func_one = (fptr_T)NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 linenr_T clnum = 0; /* init for GCC */
7408 int len = 0; /* init for GCC */
7409#ifdef FEAT_EVAL
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007410 static char_u *eval_result = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412
7413 /* Be paranoid... */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007414 if ((source == NULL && expr == NULL) || dest == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 {
7416 EMSG(_(e_null));
7417 return 0;
7418 }
7419 if (prog_magic_wrong())
7420 return 0;
7421 src = source;
7422 dst = dest;
7423
7424 /*
7425 * When the substitute part starts with "\=" evaluate it as an expression.
7426 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007427 if (expr != NULL || (source[0] == '\\' && source[1] == '='
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428#ifdef FEAT_EVAL
7429 && !can_f_submatch /* can't do this recursively */
7430#endif
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007431 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 {
7433#ifdef FEAT_EVAL
7434 /* To make sure that the length doesn't change between checking the
7435 * length and copying the string, and to speed up things, the
7436 * resulting string is saved from the call with "copy" == FALSE to the
7437 * call with "copy" == TRUE. */
7438 if (copy)
7439 {
7440 if (eval_result != NULL)
7441 {
7442 STRCPY(dest, eval_result);
7443 dst += STRLEN(eval_result);
7444 vim_free(eval_result);
7445 eval_result = NULL;
7446 }
7447 }
7448 else
7449 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 win_T *save_reg_win;
7451 int save_ireg_ic;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007452 int prev_can_f_submatch = can_f_submatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453
7454 vim_free(eval_result);
7455
7456 /* The expression may contain substitute(), which calls us
7457 * recursively. Make sure submatch() gets the text from the first
7458 * level. Don't need to save "reg_buf", because
7459 * vim_regexec_multi() can't be called recursively. */
7460 submatch_match = reg_match;
7461 submatch_mmatch = reg_mmatch;
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007462 submatch_firstlnum = reg_firstlnum;
7463 submatch_maxline = reg_maxline;
Bram Moolenaar978287b2011-06-19 04:32:15 +02007464 submatch_line_lbr = reg_line_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 save_reg_win = reg_win;
7466 save_ireg_ic = ireg_ic;
7467 can_f_submatch = TRUE;
7468
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007469 if (expr != NULL)
7470 {
Bram Moolenaardf48fb42016-07-22 21:50:18 +02007471 typval_T argv[2];
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007472 int dummy;
7473 char_u buf[NUMBUFLEN];
7474 typval_T rettv;
Bram Moolenaardf48fb42016-07-22 21:50:18 +02007475 staticList10_T matchList;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007476
7477 rettv.v_type = VAR_STRING;
7478 rettv.vval.v_string = NULL;
7479 if (prev_can_f_submatch)
7480 {
7481 /* can't do this recursively */
7482 }
Bram Moolenaardf48fb42016-07-22 21:50:18 +02007483 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007484 {
Bram Moolenaardf48fb42016-07-22 21:50:18 +02007485 argv[0].v_type = VAR_LIST;
7486 argv[0].vval.v_list = &matchList.sl_list;
7487 matchList.sl_list.lv_len = 0;
7488 if (expr->v_type == VAR_FUNC)
7489 {
7490 s = expr->vval.v_string;
7491 call_func(s, (int)STRLEN(s), &rettv,
7492 1, argv, fill_submatch_list,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007493 0L, 0L, &dummy, TRUE, NULL, NULL);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02007494 }
7495 else if (expr->v_type == VAR_PARTIAL)
7496 {
7497 partial_T *partial = expr->vval.v_partial;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007498
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007499 s = partial_name(partial);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02007500 call_func(s, (int)STRLEN(s), &rettv,
7501 1, argv, fill_submatch_list,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007502 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02007503 }
7504 if (matchList.sl_list.lv_len > 0)
7505 /* fill_submatch_list() was called */
7506 clear_submatch_list(&matchList);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007507 }
7508 eval_result = get_tv_string_buf_chk(&rettv, buf);
7509 if (eval_result != NULL)
7510 eval_result = vim_strsave(eval_result);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02007511 clear_tv(&rettv);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007512 }
7513 else
7514 eval_result = eval_to_string(source + 2, NULL, TRUE);
7515
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 if (eval_result != NULL)
7517 {
Bram Moolenaar06975a42010-03-23 16:27:22 +01007518 int had_backslash = FALSE;
7519
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00007520 for (s = eval_result; *s != NUL; mb_ptr_adv(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 {
Bram Moolenaar978287b2011-06-19 04:32:15 +02007522 /* Change NL to CR, so that it becomes a line break,
7523 * unless called from vim_regexec_nl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 * Skip over a backslashed character. */
Bram Moolenaar978287b2011-06-19 04:32:15 +02007525 if (*s == NL && !submatch_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 *s = CAR;
7527 else if (*s == '\\' && s[1] != NUL)
Bram Moolenaar06975a42010-03-23 16:27:22 +01007528 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 ++s;
Bram Moolenaar60190782010-05-21 13:08:58 +02007530 /* Change NL to CR here too, so that this works:
7531 * :s/abc\\\ndef/\="aaa\\\nbbb"/ on text:
7532 * abc\
7533 * def
Bram Moolenaar978287b2011-06-19 04:32:15 +02007534 * Not when called from vim_regexec_nl().
Bram Moolenaar60190782010-05-21 13:08:58 +02007535 */
Bram Moolenaar978287b2011-06-19 04:32:15 +02007536 if (*s == NL && !submatch_line_lbr)
Bram Moolenaar60190782010-05-21 13:08:58 +02007537 *s = CAR;
Bram Moolenaar06975a42010-03-23 16:27:22 +01007538 had_backslash = TRUE;
7539 }
7540 }
7541 if (had_backslash && backslash)
7542 {
7543 /* Backslashes will be consumed, need to double them. */
7544 s = vim_strsave_escaped(eval_result, (char_u *)"\\");
7545 if (s != NULL)
7546 {
7547 vim_free(eval_result);
7548 eval_result = s;
7549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550 }
7551
7552 dst += STRLEN(eval_result);
7553 }
7554
7555 reg_match = submatch_match;
7556 reg_mmatch = submatch_mmatch;
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007557 reg_firstlnum = submatch_firstlnum;
7558 reg_maxline = submatch_maxline;
Bram Moolenaar978287b2011-06-19 04:32:15 +02007559 reg_line_lbr = submatch_line_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 reg_win = save_reg_win;
7561 ireg_ic = save_ireg_ic;
7562 can_f_submatch = FALSE;
7563 }
7564#endif
7565 }
7566 else
7567 while ((c = *src++) != NUL)
7568 {
7569 if (c == '&' && magic)
7570 no = 0;
7571 else if (c == '\\' && *src != NUL)
7572 {
7573 if (*src == '&' && !magic)
7574 {
7575 ++src;
7576 no = 0;
7577 }
7578 else if ('0' <= *src && *src <= '9')
7579 {
7580 no = *src++ - '0';
7581 }
7582 else if (vim_strchr((char_u *)"uUlLeE", *src))
7583 {
7584 switch (*src++)
7585 {
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007586 case 'u': func_one = (fptr_T)do_upper;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 continue;
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007588 case 'U': func_all = (fptr_T)do_Upper;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 continue;
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007590 case 'l': func_one = (fptr_T)do_lower;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 continue;
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007592 case 'L': func_all = (fptr_T)do_Lower;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 continue;
7594 case 'e':
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007595 case 'E': func_one = func_all = (fptr_T)NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 continue;
7597 }
7598 }
7599 }
7600 if (no < 0) /* Ordinary character. */
7601 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00007602 if (c == K_SPECIAL && src[0] != NUL && src[1] != NUL)
7603 {
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00007604 /* Copy a special key as-is. */
Bram Moolenaardb552d602006-03-23 22:59:57 +00007605 if (copy)
7606 {
7607 *dst++ = c;
7608 *dst++ = *src++;
7609 *dst++ = *src++;
7610 }
7611 else
7612 {
7613 dst += 3;
7614 src += 2;
7615 }
7616 continue;
7617 }
7618
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 if (c == '\\' && *src != NUL)
7620 {
7621 /* Check for abbreviations -- webb */
7622 switch (*src)
7623 {
7624 case 'r': c = CAR; ++src; break;
7625 case 'n': c = NL; ++src; break;
7626 case 't': c = TAB; ++src; break;
7627 /* Oh no! \e already has meaning in subst pat :-( */
7628 /* case 'e': c = ESC; ++src; break; */
7629 case 'b': c = Ctrl_H; ++src; break;
7630
7631 /* If "backslash" is TRUE the backslash will be removed
7632 * later. Used to insert a literal CR. */
7633 default: if (backslash)
7634 {
7635 if (copy)
7636 *dst = '\\';
7637 ++dst;
7638 }
7639 c = *src++;
7640 }
7641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642#ifdef FEAT_MBYTE
Bram Moolenaardb552d602006-03-23 22:59:57 +00007643 else if (has_mbyte)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007644 c = mb_ptr2char(src - 1);
7645#endif
7646
Bram Moolenaardb552d602006-03-23 22:59:57 +00007647 /* Write to buffer, if copy is set. */
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007648 if (func_one != (fptr_T)NULL)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007649 /* Turbo C complains without the typecast */
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007650 func_one = (fptr_T)(func_one(&cc, c));
7651 else if (func_all != (fptr_T)NULL)
7652 /* Turbo C complains without the typecast */
7653 func_all = (fptr_T)(func_all(&cc, c));
7654 else /* just copy */
7655 cc = c;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007656
7657#ifdef FEAT_MBYTE
7658 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 {
Bram Moolenaar0c56c602010-07-12 22:42:33 +02007660 int totlen = mb_ptr2len(src - 1);
7661
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 if (copy)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007663 mb_char2bytes(cc, dst);
7664 dst += mb_char2len(cc) - 1;
Bram Moolenaar0c56c602010-07-12 22:42:33 +02007665 if (enc_utf8)
7666 {
7667 int clen = utf_ptr2len(src - 1);
7668
7669 /* If the character length is shorter than "totlen", there
7670 * are composing characters; copy them as-is. */
7671 if (clen < totlen)
7672 {
7673 if (copy)
7674 mch_memmove(dst + 1, src - 1 + clen,
7675 (size_t)(totlen - clen));
7676 dst += totlen - clen;
7677 }
7678 }
7679 src += totlen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 }
7681 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682#endif
7683 if (copy)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007684 *dst = cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 dst++;
7686 }
7687 else
7688 {
7689 if (REG_MULTI)
7690 {
7691 clnum = reg_mmatch->startpos[no].lnum;
7692 if (clnum < 0 || reg_mmatch->endpos[no].lnum < 0)
7693 s = NULL;
7694 else
7695 {
7696 s = reg_getline(clnum) + reg_mmatch->startpos[no].col;
7697 if (reg_mmatch->endpos[no].lnum == clnum)
7698 len = reg_mmatch->endpos[no].col
7699 - reg_mmatch->startpos[no].col;
7700 else
7701 len = (int)STRLEN(s);
7702 }
7703 }
7704 else
7705 {
7706 s = reg_match->startp[no];
7707 if (reg_match->endp[no] == NULL)
7708 s = NULL;
7709 else
7710 len = (int)(reg_match->endp[no] - s);
7711 }
7712 if (s != NULL)
7713 {
7714 for (;;)
7715 {
7716 if (len == 0)
7717 {
7718 if (REG_MULTI)
7719 {
7720 if (reg_mmatch->endpos[no].lnum == clnum)
7721 break;
7722 if (copy)
7723 *dst = CAR;
7724 ++dst;
7725 s = reg_getline(++clnum);
7726 if (reg_mmatch->endpos[no].lnum == clnum)
7727 len = reg_mmatch->endpos[no].col;
7728 else
7729 len = (int)STRLEN(s);
7730 }
7731 else
7732 break;
7733 }
7734 else if (*s == NUL) /* we hit NUL. */
7735 {
7736 if (copy)
7737 EMSG(_(e_re_damg));
7738 goto exit;
7739 }
7740 else
7741 {
7742 if (backslash && (*s == CAR || *s == '\\'))
7743 {
7744 /*
7745 * Insert a backslash in front of a CR, otherwise
7746 * it will be replaced by a line break.
7747 * Number of backslashes will be halved later,
7748 * double them here.
7749 */
7750 if (copy)
7751 {
7752 dst[0] = '\\';
7753 dst[1] = *s;
7754 }
7755 dst += 2;
7756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 else
7758 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007759#ifdef FEAT_MBYTE
7760 if (has_mbyte)
7761 c = mb_ptr2char(s);
7762 else
7763#endif
7764 c = *s;
7765
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007766 if (func_one != (fptr_T)NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 /* Turbo C complains without the typecast */
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007768 func_one = (fptr_T)(func_one(&cc, c));
7769 else if (func_all != (fptr_T)NULL)
7770 /* Turbo C complains without the typecast */
7771 func_all = (fptr_T)(func_all(&cc, c));
7772 else /* just copy */
7773 cc = c;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007774
7775#ifdef FEAT_MBYTE
7776 if (has_mbyte)
7777 {
Bram Moolenaar9225efb2007-07-30 20:32:53 +00007778 int l;
7779
7780 /* Copy composing characters separately, one
7781 * at a time. */
7782 if (enc_utf8)
7783 l = utf_ptr2len(s) - 1;
7784 else
7785 l = mb_ptr2len(s) - 1;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007786
7787 s += l;
7788 len -= l;
7789 if (copy)
7790 mb_char2bytes(cc, dst);
7791 dst += mb_char2len(cc) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007793 else
7794#endif
7795 if (copy)
7796 *dst = cc;
7797 dst++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007799
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 ++s;
7801 --len;
7802 }
7803 }
7804 }
7805 no = -1;
7806 }
7807 }
7808 if (copy)
7809 *dst = NUL;
7810
7811exit:
7812 return (int)((dst - dest) + 1);
7813}
7814
7815#ifdef FEAT_EVAL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007816static char_u *reg_getline_submatch(linenr_T lnum);
Bram Moolenaard32a3192009-11-26 19:40:49 +00007817
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818/*
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007819 * Call reg_getline() with the line numbers from the submatch. If a
7820 * substitute() was used the reg_maxline and other values have been
7821 * overwritten.
7822 */
7823 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01007824reg_getline_submatch(linenr_T lnum)
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007825{
7826 char_u *s;
7827 linenr_T save_first = reg_firstlnum;
7828 linenr_T save_max = reg_maxline;
7829
7830 reg_firstlnum = submatch_firstlnum;
7831 reg_maxline = submatch_maxline;
7832
7833 s = reg_getline(lnum);
7834
7835 reg_firstlnum = save_first;
7836 reg_maxline = save_max;
7837 return s;
7838}
7839
7840/*
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00007841 * Used for the submatch() function: get the string from the n'th submatch in
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 * allocated memory.
7843 * Returns NULL when not in a ":s" command and for a non-existing submatch.
7844 */
7845 char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01007846reg_submatch(int no)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847{
7848 char_u *retval = NULL;
7849 char_u *s;
7850 int len;
7851 int round;
7852 linenr_T lnum;
7853
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007854 if (!can_f_submatch || no < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 return NULL;
7856
7857 if (submatch_match == NULL)
7858 {
7859 /*
7860 * First round: compute the length and allocate memory.
7861 * Second round: copy the text.
7862 */
7863 for (round = 1; round <= 2; ++round)
7864 {
7865 lnum = submatch_mmatch->startpos[no].lnum;
7866 if (lnum < 0 || submatch_mmatch->endpos[no].lnum < 0)
7867 return NULL;
7868
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007869 s = reg_getline_submatch(lnum) + submatch_mmatch->startpos[no].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 if (s == NULL) /* anti-crash check, cannot happen? */
7871 break;
7872 if (submatch_mmatch->endpos[no].lnum == lnum)
7873 {
7874 /* Within one line: take form start to end col. */
7875 len = submatch_mmatch->endpos[no].col
7876 - submatch_mmatch->startpos[no].col;
7877 if (round == 2)
Bram Moolenaarbbebc852005-07-18 21:47:53 +00007878 vim_strncpy(retval, s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 ++len;
7880 }
7881 else
7882 {
7883 /* Multiple lines: take start line from start col, middle
7884 * lines completely and end line up to end col. */
7885 len = (int)STRLEN(s);
7886 if (round == 2)
7887 {
7888 STRCPY(retval, s);
7889 retval[len] = '\n';
7890 }
7891 ++len;
7892 ++lnum;
7893 while (lnum < submatch_mmatch->endpos[no].lnum)
7894 {
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007895 s = reg_getline_submatch(lnum++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 if (round == 2)
7897 STRCPY(retval + len, s);
7898 len += (int)STRLEN(s);
7899 if (round == 2)
7900 retval[len] = '\n';
7901 ++len;
7902 }
7903 if (round == 2)
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007904 STRNCPY(retval + len, reg_getline_submatch(lnum),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 submatch_mmatch->endpos[no].col);
7906 len += submatch_mmatch->endpos[no].col;
7907 if (round == 2)
7908 retval[len] = NUL;
7909 ++len;
7910 }
7911
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007912 if (retval == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 {
7914 retval = lalloc((long_u)len, TRUE);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007915 if (retval == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 return NULL;
7917 }
7918 }
7919 }
7920 else
7921 {
Bram Moolenaar7670fa02009-02-21 21:04:20 +00007922 s = submatch_match->startp[no];
7923 if (s == NULL || submatch_match->endp[no] == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 retval = NULL;
7925 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 retval = vim_strnsave(s, (int)(submatch_match->endp[no] - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 }
7928
7929 return retval;
7930}
Bram Moolenaar41571762014-04-02 19:00:58 +02007931
7932/*
7933 * Used for the submatch() function with the optional non-zero argument: get
7934 * the list of strings from the n'th submatch in allocated memory with NULs
7935 * represented in NLs.
7936 * Returns a list of allocated strings. Returns NULL when not in a ":s"
7937 * command, for a non-existing submatch and for any error.
7938 */
7939 list_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01007940reg_submatch_list(int no)
Bram Moolenaar41571762014-04-02 19:00:58 +02007941{
7942 char_u *s;
7943 linenr_T slnum;
7944 linenr_T elnum;
7945 colnr_T scol;
7946 colnr_T ecol;
7947 int i;
7948 list_T *list;
7949 int error = FALSE;
7950
7951 if (!can_f_submatch || no < 0)
7952 return NULL;
7953
7954 if (submatch_match == NULL)
7955 {
7956 slnum = submatch_mmatch->startpos[no].lnum;
7957 elnum = submatch_mmatch->endpos[no].lnum;
7958 if (slnum < 0 || elnum < 0)
7959 return NULL;
7960
7961 scol = submatch_mmatch->startpos[no].col;
7962 ecol = submatch_mmatch->endpos[no].col;
7963
7964 list = list_alloc();
7965 if (list == NULL)
7966 return NULL;
7967
7968 s = reg_getline_submatch(slnum) + scol;
7969 if (slnum == elnum)
7970 {
7971 if (list_append_string(list, s, ecol - scol) == FAIL)
7972 error = TRUE;
7973 }
7974 else
7975 {
7976 if (list_append_string(list, s, -1) == FAIL)
7977 error = TRUE;
7978 for (i = 1; i < elnum - slnum; i++)
7979 {
7980 s = reg_getline_submatch(slnum + i);
7981 if (list_append_string(list, s, -1) == FAIL)
7982 error = TRUE;
7983 }
7984 s = reg_getline_submatch(elnum);
7985 if (list_append_string(list, s, ecol) == FAIL)
7986 error = TRUE;
7987 }
7988 }
7989 else
7990 {
7991 s = submatch_match->startp[no];
7992 if (s == NULL || submatch_match->endp[no] == NULL)
7993 return NULL;
7994 list = list_alloc();
7995 if (list == NULL)
7996 return NULL;
7997 if (list_append_string(list, s,
7998 (int)(submatch_match->endp[no] - s)) == FAIL)
7999 error = TRUE;
8000 }
8001
8002 if (error)
8003 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02008004 list_free(list);
Bram Moolenaar41571762014-04-02 19:00:58 +02008005 return NULL;
8006 }
8007 return list;
8008}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008010
8011static regengine_T bt_regengine =
8012{
8013 bt_regcomp,
Bram Moolenaar473de612013-06-08 18:19:48 +02008014 bt_regfree,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008015 bt_regexec_nl,
Bram Moolenaarfda37292014-11-05 14:27:36 +01008016 bt_regexec_multi,
8017 (char_u *)""
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008018};
8019
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008020#include "regexp_nfa.c"
8021
8022static regengine_T nfa_regengine =
8023{
8024 nfa_regcomp,
Bram Moolenaar473de612013-06-08 18:19:48 +02008025 nfa_regfree,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008026 nfa_regexec_nl,
Bram Moolenaarfda37292014-11-05 14:27:36 +01008027 nfa_regexec_multi,
8028 (char_u *)""
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008029};
8030
8031/* Which regexp engine to use? Needed for vim_regcomp().
8032 * Must match with 'regexpengine'. */
8033static int regexp_engine = 0;
Bram Moolenaarfda37292014-11-05 14:27:36 +01008034
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008035#ifdef DEBUG
8036static char_u regname[][30] = {
8037 "AUTOMATIC Regexp Engine",
Bram Moolenaar75eb1612013-05-29 18:45:11 +02008038 "BACKTRACKING Regexp Engine",
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008039 "NFA Regexp Engine"
8040 };
8041#endif
8042
8043/*
8044 * Compile a regular expression into internal code.
Bram Moolenaar473de612013-06-08 18:19:48 +02008045 * Returns the program in allocated memory.
8046 * Use vim_regfree() to free the memory.
8047 * Returns NULL for an error.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008048 */
8049 regprog_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01008050vim_regcomp(char_u *expr_arg, int re_flags)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008051{
8052 regprog_T *prog = NULL;
8053 char_u *expr = expr_arg;
8054
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008055 regexp_engine = p_re;
8056
8057 /* Check for prefix "\%#=", that sets the regexp engine */
8058 if (STRNCMP(expr, "\\%#=", 4) == 0)
8059 {
8060 int newengine = expr[4] - '0';
8061
8062 if (newengine == AUTOMATIC_ENGINE
8063 || newengine == BACKTRACKING_ENGINE
8064 || newengine == NFA_ENGINE)
8065 {
8066 regexp_engine = expr[4] - '0';
8067 expr += 5;
8068#ifdef DEBUG
Bram Moolenaar6e132072014-05-13 16:46:32 +02008069 smsg((char_u *)"New regexp mode selected (%d): %s",
8070 regexp_engine, regname[newengine]);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008071#endif
8072 }
8073 else
8074 {
8075 EMSG(_("E864: \\%#= can only be followed by 0, 1, or 2. The automatic engine will be used "));
8076 regexp_engine = AUTOMATIC_ENGINE;
8077 }
8078 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008079 bt_regengine.expr = expr;
8080 nfa_regengine.expr = expr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008081
8082 /*
8083 * First try the NFA engine, unless backtracking was requested.
8084 */
8085 if (regexp_engine != BACKTRACKING_ENGINE)
Bram Moolenaare0ad3652015-01-27 12:59:55 +01008086 prog = nfa_regengine.regcomp(expr,
8087 re_flags + (regexp_engine == AUTOMATIC_ENGINE ? RE_AUTO : 0));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008088 else
8089 prog = bt_regengine.regcomp(expr, re_flags);
8090
Bram Moolenaarfda37292014-11-05 14:27:36 +01008091 /* Check for error compiling regexp with initial engine. */
8092 if (prog == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008093 {
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02008094#ifdef BT_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008095 if (regexp_engine != BACKTRACKING_ENGINE) /* debugging log for NFA */
8096 {
8097 FILE *f;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02008098 f = fopen(BT_REGEXP_DEBUG_LOG_NAME, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008099 if (f)
8100 {
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02008101 fprintf(f, "Syntax error in \"%s\"\n", expr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008102 fclose(f);
8103 }
8104 else
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02008105 EMSG2("(NFA) Could not open \"%s\" to write !!!",
8106 BT_REGEXP_DEBUG_LOG_NAME);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008107 }
8108#endif
8109 /*
Bram Moolenaarfda37292014-11-05 14:27:36 +01008110 * If the NFA engine failed, try the backtracking engine.
Bram Moolenaare0ad3652015-01-27 12:59:55 +01008111 * The NFA engine also fails for patterns that it can't handle well
8112 * but are still valid patterns, thus a retry should work.
8113 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008114 if (regexp_engine == AUTOMATIC_ENGINE)
Bram Moolenaarfda37292014-11-05 14:27:36 +01008115 {
Bram Moolenaare0ad3652015-01-27 12:59:55 +01008116 regexp_engine = BACKTRACKING_ENGINE;
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02008117 prog = bt_regengine.regcomp(expr, re_flags);
Bram Moolenaarfda37292014-11-05 14:27:36 +01008118 }
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02008119 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008120
Bram Moolenaarfda37292014-11-05 14:27:36 +01008121 if (prog != NULL)
8122 {
8123 /* Store the info needed to call regcomp() again when the engine turns
8124 * out to be very slow when executing it. */
8125 prog->re_engine = regexp_engine;
8126 prog->re_flags = re_flags;
8127 }
8128
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008129 return prog;
8130}
8131
8132/*
Bram Moolenaar473de612013-06-08 18:19:48 +02008133 * Free a compiled regexp program, returned by vim_regcomp().
8134 */
8135 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008136vim_regfree(regprog_T *prog)
Bram Moolenaar473de612013-06-08 18:19:48 +02008137{
8138 if (prog != NULL)
8139 prog->engine->regfree(prog);
8140}
8141
Bram Moolenaarfda37292014-11-05 14:27:36 +01008142#ifdef FEAT_EVAL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01008143static void report_re_switch(char_u *pat);
Bram Moolenaarfda37292014-11-05 14:27:36 +01008144
8145 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008146report_re_switch(char_u *pat)
Bram Moolenaarfda37292014-11-05 14:27:36 +01008147{
8148 if (p_verbose > 0)
8149 {
8150 verbose_enter();
8151 MSG_PUTS(_("Switching to backtracking RE engine for pattern: "));
8152 MSG_PUTS(pat);
8153 verbose_leave();
8154 }
8155}
8156#endif
8157
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01008158static int vim_regexec_both(regmatch_T *rmp, char_u *line, colnr_T col, int nl);
Bram Moolenaarfda37292014-11-05 14:27:36 +01008159
Bram Moolenaar473de612013-06-08 18:19:48 +02008160/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008161 * Match a regexp against a string.
8162 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
Bram Moolenaardffa5b82014-11-19 16:38:07 +01008163 * Note: "rmp->regprog" may be freed and changed.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008164 * Uses curbuf for line count and 'iskeyword'.
Bram Moolenaarfda37292014-11-05 14:27:36 +01008165 * When "nl" is TRUE consider a "\n" in "line" to be a line break.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008166 *
8167 * Return TRUE if there is a match, FALSE if not.
8168 */
Bram Moolenaarfda37292014-11-05 14:27:36 +01008169 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008170vim_regexec_both(
8171 regmatch_T *rmp,
8172 char_u *line, /* string to match against */
8173 colnr_T col, /* column to start looking for match */
8174 int nl)
Bram Moolenaarfda37292014-11-05 14:27:36 +01008175{
8176 int result = rmp->regprog->engine->regexec_nl(rmp, line, col, nl);
8177
8178 /* NFA engine aborted because it's very slow. */
8179 if (rmp->regprog->re_engine == AUTOMATIC_ENGINE
8180 && result == NFA_TOO_EXPENSIVE)
8181 {
8182 int save_p_re = p_re;
8183 int re_flags = rmp->regprog->re_flags;
8184 char_u *pat = vim_strsave(((nfa_regprog_T *)rmp->regprog)->pattern);
8185
8186 p_re = BACKTRACKING_ENGINE;
8187 vim_regfree(rmp->regprog);
8188 if (pat != NULL)
8189 {
8190#ifdef FEAT_EVAL
8191 report_re_switch(pat);
8192#endif
8193 rmp->regprog = vim_regcomp(pat, re_flags);
8194 if (rmp->regprog != NULL)
8195 result = rmp->regprog->engine->regexec_nl(rmp, line, col, nl);
8196 vim_free(pat);
8197 }
8198
8199 p_re = save_p_re;
8200 }
Bram Moolenaar66a3e792014-11-20 23:07:05 +01008201 return result > 0;
Bram Moolenaarfda37292014-11-05 14:27:36 +01008202}
8203
Bram Moolenaardffa5b82014-11-19 16:38:07 +01008204/*
8205 * Note: "*prog" may be freed and changed.
Bram Moolenaar66a3e792014-11-20 23:07:05 +01008206 * Return TRUE if there is a match, FALSE if not.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01008207 */
8208 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008209vim_regexec_prog(
8210 regprog_T **prog,
8211 int ignore_case,
8212 char_u *line,
8213 colnr_T col)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01008214{
8215 int r;
8216 regmatch_T regmatch;
8217
8218 regmatch.regprog = *prog;
8219 regmatch.rm_ic = ignore_case;
8220 r = vim_regexec_both(&regmatch, line, col, FALSE);
8221 *prog = regmatch.regprog;
8222 return r;
8223}
8224
8225/*
8226 * Note: "rmp->regprog" may be freed and changed.
Bram Moolenaar66a3e792014-11-20 23:07:05 +01008227 * Return TRUE if there is a match, FALSE if not.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01008228 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008229 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008230vim_regexec(regmatch_T *rmp, char_u *line, colnr_T col)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008231{
Bram Moolenaarfda37292014-11-05 14:27:36 +01008232 return vim_regexec_both(rmp, line, col, FALSE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008233}
8234
8235#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
8236 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
8237/*
8238 * Like vim_regexec(), but consider a "\n" in "line" to be a line break.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01008239 * Note: "rmp->regprog" may be freed and changed.
Bram Moolenaar66a3e792014-11-20 23:07:05 +01008240 * Return TRUE if there is a match, FALSE if not.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008241 */
8242 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008243vim_regexec_nl(regmatch_T *rmp, char_u *line, colnr_T col)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008244{
Bram Moolenaarfda37292014-11-05 14:27:36 +01008245 return vim_regexec_both(rmp, line, col, TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008246}
8247#endif
8248
8249/*
8250 * Match a regexp against multiple lines.
8251 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
Bram Moolenaardffa5b82014-11-19 16:38:07 +01008252 * Note: "rmp->regprog" may be freed and changed.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008253 * Uses curbuf for line count and 'iskeyword'.
8254 *
8255 * Return zero if there is no match. Return number of lines contained in the
8256 * match otherwise.
8257 */
8258 long
Bram Moolenaar05540972016-01-30 20:31:25 +01008259vim_regexec_multi(
8260 regmmatch_T *rmp,
8261 win_T *win, /* window in which to search or NULL */
8262 buf_T *buf, /* buffer in which to search */
8263 linenr_T lnum, /* nr of line to start looking for match */
8264 colnr_T col, /* column to start looking for match */
8265 proftime_T *tm) /* timeout limit or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008266{
Bram Moolenaarfda37292014-11-05 14:27:36 +01008267 int result = rmp->regprog->engine->regexec_multi(
8268 rmp, win, buf, lnum, col, tm);
8269
8270 /* NFA engine aborted because it's very slow. */
8271 if (rmp->regprog->re_engine == AUTOMATIC_ENGINE
8272 && result == NFA_TOO_EXPENSIVE)
8273 {
8274 int save_p_re = p_re;
8275 int re_flags = rmp->regprog->re_flags;
8276 char_u *pat = vim_strsave(((nfa_regprog_T *)rmp->regprog)->pattern);
8277
8278 p_re = BACKTRACKING_ENGINE;
8279 vim_regfree(rmp->regprog);
8280 if (pat != NULL)
8281 {
8282#ifdef FEAT_EVAL
8283 report_re_switch(pat);
8284#endif
8285 rmp->regprog = vim_regcomp(pat, re_flags);
8286 if (rmp->regprog != NULL)
8287 result = rmp->regprog->engine->regexec_multi(
8288 rmp, win, buf, lnum, col, tm);
8289 vim_free(pat);
8290 }
8291 p_re = save_p_re;
8292 }
8293
Bram Moolenaar66a3e792014-11-20 23:07:05 +01008294 return result <= 0 ? 0 : result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008295}