blob: 29b40813f4438ecaa7c9a46cab03948063cddec3 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
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 */
247
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248/*
249 * Magic characters have a special meaning, they don't match literally.
250 * Magic characters are negative. This separates them from literal characters
251 * (possibly multi-byte). Only ASCII characters can be Magic.
252 */
253#define Magic(x) ((int)(x) - 256)
254#define un_Magic(x) ((x) + 256)
255#define is_Magic(x) ((x) < 0)
256
257static int no_Magic __ARGS((int x));
258static int toggle_Magic __ARGS((int x));
259
260 static int
261no_Magic(x)
262 int x;
263{
264 if (is_Magic(x))
265 return un_Magic(x);
266 return x;
267}
268
269 static int
270toggle_Magic(x)
271 int x;
272{
273 if (is_Magic(x))
274 return un_Magic(x);
275 return Magic(x);
276}
277
278/*
279 * The first byte of the regexp internal "program" is actually this magic
280 * number; the start node begins in the second byte. It's used to catch the
281 * most severe mutilation of the program by the caller.
282 */
283
284#define REGMAGIC 0234
285
286/*
287 * Opcode notes:
288 *
289 * BRANCH The set of branches constituting a single choice are hooked
290 * together with their "next" pointers, since precedence prevents
291 * anything being concatenated to any individual branch. The
292 * "next" pointer of the last BRANCH in a choice points to the
293 * thing following the whole choice. This is also where the
294 * final "next" pointer of each individual branch points; each
295 * branch starts with the operand node of a BRANCH node.
296 *
297 * BACK Normal "next" pointers all implicitly point forward; BACK
298 * exists to make loop structures possible.
299 *
300 * STAR,PLUS '=', and complex '*' and '+', are implemented as circular
301 * BRANCH structures using BACK. Simple cases (one character
302 * per match) are implemented with STAR and PLUS for speed
303 * and to minimize recursive plunges.
304 *
305 * BRACE_LIMITS This is always followed by a BRACE_SIMPLE or BRACE_COMPLEX
306 * node, and defines the min and max limits to be used for that
307 * node.
308 *
309 * MOPEN,MCLOSE ...are numbered at compile time.
310 * ZOPEN,ZCLOSE ...ditto
311 */
312
313/*
314 * A node is one char of opcode followed by two chars of "next" pointer.
315 * "Next" pointers are stored as two 8-bit bytes, high order first. The
316 * value is a positive offset from the opcode of the node containing it.
317 * An operand, if any, simply follows the node. (Note that much of the
318 * code generation knows about this implicit relationship.)
319 *
320 * Using two bytes for the "next" pointer is vast overkill for most things,
321 * but allows patterns to get big without disasters.
322 */
323#define OP(p) ((int)*(p))
324#define NEXT(p) (((*((p) + 1) & 0377) << 8) + (*((p) + 2) & 0377))
325#define OPERAND(p) ((p) + 3)
326/* Obtain an operand that was stored as four bytes, MSB first. */
327#define OPERAND_MIN(p) (((long)(p)[3] << 24) + ((long)(p)[4] << 16) \
328 + ((long)(p)[5] << 8) + (long)(p)[6])
329/* Obtain a second operand stored as four bytes. */
330#define OPERAND_MAX(p) OPERAND_MIN((p) + 4)
331/* Obtain a second single-byte operand stored after a four bytes operand. */
332#define OPERAND_CMP(p) (p)[7]
333
334/*
335 * Utility definitions.
336 */
337#define UCHARAT(p) ((int)*(char_u *)(p))
338
339/* Used for an error (down from) vim_regcomp(): give the error message, set
340 * rc_did_emsg and return NULL */
Bram Moolenaar98692072006-02-04 00:57:42 +0000341#define EMSG_RET_NULL(m) return (EMSG(m), rc_did_emsg = TRUE, (void *)NULL)
Bram Moolenaar45eeb132005-06-06 21:59:07 +0000342#define EMSG_RET_FAIL(m) return (EMSG(m), rc_did_emsg = TRUE, FAIL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200343#define EMSG2_RET_NULL(m, c) return (EMSG2((m), (c) ? "" : "\\"), rc_did_emsg = TRUE, (void *)NULL)
344#define EMSG2_RET_FAIL(m, c) return (EMSG2((m), (c) ? "" : "\\"), rc_did_emsg = TRUE, FAIL)
345#define EMSG_ONE_RET_NULL EMSG2_RET_NULL(_("E369: invalid item in %s%%[]"), reg_magic == MAGIC_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346
347#define MAX_LIMIT (32767L << 16L)
348
349static int re_multi_type __ARGS((int));
350static int cstrncmp __ARGS((char_u *s1, char_u *s2, int *n));
351static char_u *cstrchr __ARGS((char_u *, int));
352
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200353#ifdef BT_REGEXP_DUMP
354static void regdump __ARGS((char_u *, bt_regprog_T *));
355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356#ifdef DEBUG
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357static char_u *regprop __ARGS((char_u *));
358#endif
359
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200360static char_u e_missingbracket[] = N_("E769: Missing ] after %s[");
361static char_u e_unmatchedpp[] = N_("E53: Unmatched %s%%(");
362static char_u e_unmatchedp[] = N_("E54: Unmatched %s(");
363static char_u e_unmatchedpar[] = N_("E55: Unmatched %s)");
364
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365#define NOT_MULTI 0
366#define MULTI_ONE 1
367#define MULTI_MULT 2
368/*
369 * Return NOT_MULTI if c is not a "multi" operator.
370 * Return MULTI_ONE if c is a single "multi" operator.
371 * Return MULTI_MULT if c is a multi "multi" operator.
372 */
373 static int
374re_multi_type(c)
375 int c;
376{
377 if (c == Magic('@') || c == Magic('=') || c == Magic('?'))
378 return MULTI_ONE;
379 if (c == Magic('*') || c == Magic('+') || c == Magic('{'))
380 return MULTI_MULT;
381 return NOT_MULTI;
382}
383
384/*
385 * Flags to be passed up and down.
386 */
387#define HASWIDTH 0x1 /* Known never to match null string. */
388#define SIMPLE 0x2 /* Simple enough to be STAR/PLUS operand. */
389#define SPSTART 0x4 /* Starts with * or +. */
390#define HASNL 0x8 /* Contains some \n. */
391#define HASLOOKBH 0x10 /* Contains "\@<=" or "\@<!". */
392#define WORST 0 /* Worst case. */
393
394/*
395 * When regcode is set to this value, code is not emitted and size is computed
396 * instead.
397 */
398#define JUST_CALC_SIZE ((char_u *) -1)
399
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000400static char_u *reg_prev_sub = NULL;
401
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402/*
403 * REGEXP_INRANGE contains all characters which are always special in a []
404 * range after '\'.
405 * REGEXP_ABBR contains all characters which act as abbreviations after '\'.
406 * These are:
407 * \n - New line (NL).
408 * \r - Carriage Return (CR).
409 * \t - Tab (TAB).
410 * \e - Escape (ESC).
411 * \b - Backspace (Ctrl_H).
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000412 * \d - Character code in decimal, eg \d123
413 * \o - Character code in octal, eg \o80
414 * \x - Character code in hex, eg \x4a
415 * \u - Multibyte character code, eg \u20ac
416 * \U - Long multibyte character code, eg \U12345678
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417 */
418static char_u REGEXP_INRANGE[] = "]^-n\\";
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000419static char_u REGEXP_ABBR[] = "nrtebdoxuU";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420
421static int backslash_trans __ARGS((int c));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000422static int get_char_class __ARGS((char_u **pp));
423static int get_equi_class __ARGS((char_u **pp));
424static void reg_equi_class __ARGS((int c));
425static int get_coll_element __ARGS((char_u **pp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426static char_u *skip_anyof __ARGS((char_u *p));
427static void init_class_tab __ARGS((void));
428
429/*
430 * Translate '\x' to its control character, except "\n", which is Magic.
431 */
432 static int
433backslash_trans(c)
434 int c;
435{
436 switch (c)
437 {
438 case 'r': return CAR;
439 case 't': return TAB;
440 case 'e': return ESC;
441 case 'b': return BS;
442 }
443 return c;
444}
445
446/*
Bram Moolenaardf177f62005-02-22 08:39:57 +0000447 * Check for a character class name "[:name:]". "pp" points to the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 * Returns one of the CLASS_ items. CLASS_NONE means that no item was
449 * recognized. Otherwise "pp" is advanced to after the item.
450 */
451 static int
Bram Moolenaardf177f62005-02-22 08:39:57 +0000452get_char_class(pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 char_u **pp;
454{
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
522init_class_tab()
523{
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
668/*
669 * Forward declarations for vim_regcomp()'s friends.
670 */
671static void initchr __ARGS((char_u *));
672static int getchr __ARGS((void));
673static void skipchr_keepstart __ARGS((void));
674static int peekchr __ARGS((void));
675static void skipchr __ARGS((void));
676static void ungetchr __ARGS((void));
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000677static int gethexchrs __ARGS((int maxinputlen));
678static int getoctchrs __ARGS((void));
679static int getdecchrs __ARGS((void));
680static int coll_get_char __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681static void regcomp_start __ARGS((char_u *expr, int flags));
682static char_u *reg __ARGS((int, int *));
683static char_u *regbranch __ARGS((int *flagp));
684static char_u *regconcat __ARGS((int *flagp));
685static char_u *regpiece __ARGS((int *));
686static char_u *regatom __ARGS((int *));
687static char_u *regnode __ARGS((int));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000688#ifdef FEAT_MBYTE
689static int use_multibytecode __ARGS((int c));
690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691static int prog_magic_wrong __ARGS((void));
692static char_u *regnext __ARGS((char_u *));
693static void regc __ARGS((int b));
694#ifdef FEAT_MBYTE
695static void regmbc __ARGS((int c));
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200696# define REGMBC(x) regmbc(x);
697# define CASEMBC(x) case x:
Bram Moolenaardf177f62005-02-22 08:39:57 +0000698#else
699# define regmbc(c) regc(c)
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200700# define REGMBC(x)
701# define CASEMBC(x)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702#endif
703static void reginsert __ARGS((int, char_u *));
704static void reginsert_limits __ARGS((int, long, long, char_u *));
705static char_u *re_put_long __ARGS((char_u *pr, long_u val));
706static int read_limits __ARGS((long *, long *));
707static void regtail __ARGS((char_u *, char_u *));
708static void regoptail __ARGS((char_u *, char_u *));
709
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200710static regengine_T bt_regengine;
711static regengine_T nfa_regengine;
712
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713/*
714 * Return TRUE if compiled regular expression "prog" can match a line break.
715 */
716 int
717re_multiline(prog)
718 regprog_T *prog;
719{
720 return (prog->regflags & RF_HASNL);
721}
722
723/*
724 * Return TRUE if compiled regular expression "prog" looks before the start
725 * position (pattern contains "\@<=" or "\@<!").
726 */
727 int
728re_lookbehind(prog)
729 regprog_T *prog;
730{
731 return (prog->regflags & RF_LOOKBH);
732}
733
734/*
Bram Moolenaardf177f62005-02-22 08:39:57 +0000735 * Check for an equivalence class name "[=a=]". "pp" points to the '['.
736 * Returns a character representing the class. Zero means that no item was
737 * recognized. Otherwise "pp" is advanced to after the item.
738 */
739 static int
740get_equi_class(pp)
741 char_u **pp;
742{
743 int c;
744 int l = 1;
745 char_u *p = *pp;
746
747 if (p[1] == '=')
748 {
749#ifdef FEAT_MBYTE
750 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000751 l = (*mb_ptr2len)(p + 2);
Bram Moolenaardf177f62005-02-22 08:39:57 +0000752#endif
753 if (p[l + 2] == '=' && p[l + 3] == ']')
754 {
755#ifdef FEAT_MBYTE
756 if (has_mbyte)
757 c = mb_ptr2char(p + 2);
758 else
759#endif
760 c = p[2];
761 *pp += l + 4;
762 return c;
763 }
764 }
765 return 0;
766}
767
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200768#ifdef EBCDIC
769/*
770 * Table for equivalence class "c". (IBM-1047)
771 */
772char *EQUIVAL_CLASS_C[16] = {
773 "A\x62\x63\x64\x65\x66\x67",
774 "C\x68",
775 "E\x71\x72\x73\x74",
776 "I\x75\x76\x77\x78",
777 "N\x69",
778 "O\xEB\xEC\xED\xEE\xEF",
779 "U\xFB\xFC\xFD\xFE",
780 "Y\xBA",
781 "a\x42\x43\x44\x45\x46\x47",
782 "c\x48",
783 "e\x51\x52\x53\x54",
784 "i\x55\x56\x57\x58",
785 "n\x49",
786 "o\xCB\xCC\xCD\xCE\xCF",
787 "u\xDB\xDC\xDD\xDE",
788 "y\x8D\xDF",
789};
790#endif
791
Bram Moolenaardf177f62005-02-22 08:39:57 +0000792/*
793 * Produce the bytes for equivalence class "c".
794 * Currently only handles latin1, latin9 and utf-8.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200795 * NOTE: When changing this function, also change nfa_emit_equi_class()
Bram Moolenaardf177f62005-02-22 08:39:57 +0000796 */
797 static void
798reg_equi_class(c)
799 int c;
800{
801#ifdef FEAT_MBYTE
802 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
Bram Moolenaar78622822005-08-23 21:00:13 +0000803 || STRCMP(p_enc, "iso-8859-15") == 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000804#endif
805 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200806#ifdef EBCDIC
807 int i;
808
809 /* This might be slower than switch/case below. */
810 for (i = 0; i < 16; i++)
811 {
812 if (vim_strchr(EQUIVAL_CLASS_C[i], c) != NULL)
813 {
814 char *p = EQUIVAL_CLASS_C[i];
815
816 while (*p != 0)
817 regmbc(*p++);
818 return;
819 }
820 }
821#else
Bram Moolenaardf177f62005-02-22 08:39:57 +0000822 switch (c)
823 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000824 case 'A': case '\300': case '\301': case '\302':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200825 CASEMBC(0x100) CASEMBC(0x102) CASEMBC(0x104) CASEMBC(0x1cd)
826 CASEMBC(0x1de) CASEMBC(0x1e0) CASEMBC(0x1ea2)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000827 case '\303': case '\304': case '\305':
828 regmbc('A'); regmbc('\300'); regmbc('\301');
829 regmbc('\302'); regmbc('\303'); regmbc('\304');
830 regmbc('\305');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200831 REGMBC(0x100) REGMBC(0x102) REGMBC(0x104)
832 REGMBC(0x1cd) REGMBC(0x1de) REGMBC(0x1e0)
833 REGMBC(0x1ea2)
834 return;
835 case 'B': CASEMBC(0x1e02) CASEMBC(0x1e06)
836 regmbc('B'); REGMBC(0x1e02) REGMBC(0x1e06)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000837 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000838 case 'C': case '\307':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200839 CASEMBC(0x106) CASEMBC(0x108) CASEMBC(0x10a) CASEMBC(0x10c)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000840 regmbc('C'); regmbc('\307');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200841 REGMBC(0x106) REGMBC(0x108) REGMBC(0x10a)
842 REGMBC(0x10c)
843 return;
844 case 'D': CASEMBC(0x10e) CASEMBC(0x110) CASEMBC(0x1e0a)
845 CASEMBC(0x1e0e) CASEMBC(0x1e10)
846 regmbc('D'); REGMBC(0x10e) REGMBC(0x110)
847 REGMBC(0x1e0a) REGMBC(0x1e0e) REGMBC(0x1e10)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000848 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000849 case 'E': case '\310': case '\311': case '\312': case '\313':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200850 CASEMBC(0x112) CASEMBC(0x114) CASEMBC(0x116) CASEMBC(0x118)
851 CASEMBC(0x11a) CASEMBC(0x1eba) CASEMBC(0x1ebc)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000852 regmbc('E'); regmbc('\310'); regmbc('\311');
853 regmbc('\312'); regmbc('\313');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200854 REGMBC(0x112) REGMBC(0x114) REGMBC(0x116)
855 REGMBC(0x118) REGMBC(0x11a) REGMBC(0x1eba)
856 REGMBC(0x1ebc)
857 return;
858 case 'F': CASEMBC(0x1e1e)
859 regmbc('F'); REGMBC(0x1e1e)
860 return;
861 case 'G': CASEMBC(0x11c) CASEMBC(0x11e) CASEMBC(0x120)
862 CASEMBC(0x122) CASEMBC(0x1e4) CASEMBC(0x1e6) CASEMBC(0x1f4)
863 CASEMBC(0x1e20)
864 regmbc('G'); REGMBC(0x11c) REGMBC(0x11e)
865 REGMBC(0x120) REGMBC(0x122) REGMBC(0x1e4)
866 REGMBC(0x1e6) REGMBC(0x1f4) REGMBC(0x1e20)
867 return;
868 case 'H': CASEMBC(0x124) CASEMBC(0x126) CASEMBC(0x1e22)
869 CASEMBC(0x1e26) CASEMBC(0x1e28)
870 regmbc('H'); REGMBC(0x124) REGMBC(0x126)
871 REGMBC(0x1e22) REGMBC(0x1e26) REGMBC(0x1e28)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000872 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000873 case 'I': case '\314': case '\315': case '\316': case '\317':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200874 CASEMBC(0x128) CASEMBC(0x12a) CASEMBC(0x12c) CASEMBC(0x12e)
875 CASEMBC(0x130) CASEMBC(0x1cf) CASEMBC(0x1ec8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000876 regmbc('I'); regmbc('\314'); regmbc('\315');
877 regmbc('\316'); regmbc('\317');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200878 REGMBC(0x128) REGMBC(0x12a) REGMBC(0x12c)
879 REGMBC(0x12e) REGMBC(0x130) REGMBC(0x1cf)
880 REGMBC(0x1ec8)
881 return;
882 case 'J': CASEMBC(0x134)
883 regmbc('J'); REGMBC(0x134)
884 return;
885 case 'K': CASEMBC(0x136) CASEMBC(0x1e8) CASEMBC(0x1e30)
886 CASEMBC(0x1e34)
887 regmbc('K'); REGMBC(0x136) REGMBC(0x1e8)
888 REGMBC(0x1e30) REGMBC(0x1e34)
889 return;
890 case 'L': CASEMBC(0x139) CASEMBC(0x13b) CASEMBC(0x13d)
891 CASEMBC(0x13f) CASEMBC(0x141) CASEMBC(0x1e3a)
892 regmbc('L'); REGMBC(0x139) REGMBC(0x13b)
893 REGMBC(0x13d) REGMBC(0x13f) REGMBC(0x141)
894 REGMBC(0x1e3a)
895 return;
896 case 'M': CASEMBC(0x1e3e) CASEMBC(0x1e40)
897 regmbc('M'); REGMBC(0x1e3e) REGMBC(0x1e40)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000898 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000899 case 'N': case '\321':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200900 CASEMBC(0x143) CASEMBC(0x145) CASEMBC(0x147) CASEMBC(0x1e44)
901 CASEMBC(0x1e48)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000902 regmbc('N'); regmbc('\321');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200903 REGMBC(0x143) REGMBC(0x145) REGMBC(0x147)
904 REGMBC(0x1e44) REGMBC(0x1e48)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000905 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000906 case 'O': case '\322': case '\323': case '\324': case '\325':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200907 case '\326': case '\330':
908 CASEMBC(0x14c) CASEMBC(0x14e) CASEMBC(0x150) CASEMBC(0x1a0)
909 CASEMBC(0x1d1) CASEMBC(0x1ea) CASEMBC(0x1ec) CASEMBC(0x1ece)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000910 regmbc('O'); regmbc('\322'); regmbc('\323');
911 regmbc('\324'); regmbc('\325'); regmbc('\326');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200912 regmbc('\330');
913 REGMBC(0x14c) REGMBC(0x14e) REGMBC(0x150)
914 REGMBC(0x1a0) REGMBC(0x1d1) REGMBC(0x1ea)
915 REGMBC(0x1ec) REGMBC(0x1ece)
916 return;
917 case 'P': case 0x1e54: case 0x1e56:
918 regmbc('P'); REGMBC(0x1e54) REGMBC(0x1e56)
919 return;
920 case 'R': CASEMBC(0x154) CASEMBC(0x156) CASEMBC(0x158)
921 CASEMBC(0x1e58) CASEMBC(0x1e5e)
922 regmbc('R'); REGMBC(0x154) REGMBC(0x156) REGMBC(0x158)
923 REGMBC(0x1e58) REGMBC(0x1e5e)
924 return;
925 case 'S': CASEMBC(0x15a) CASEMBC(0x15c) CASEMBC(0x15e)
926 CASEMBC(0x160) CASEMBC(0x1e60)
927 regmbc('S'); REGMBC(0x15a) REGMBC(0x15c)
928 REGMBC(0x15e) REGMBC(0x160) REGMBC(0x1e60)
929 return;
930 case 'T': CASEMBC(0x162) CASEMBC(0x164) CASEMBC(0x166)
931 CASEMBC(0x1e6a) CASEMBC(0x1e6e)
932 regmbc('T'); REGMBC(0x162) REGMBC(0x164)
933 REGMBC(0x166) REGMBC(0x1e6a) REGMBC(0x1e6e)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000934 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000935 case 'U': case '\331': case '\332': case '\333': case '\334':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200936 CASEMBC(0x168) CASEMBC(0x16a) CASEMBC(0x16c) CASEMBC(0x16e)
937 CASEMBC(0x170) CASEMBC(0x172) CASEMBC(0x1af) CASEMBC(0x1d3)
938 CASEMBC(0x1ee6)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000939 regmbc('U'); regmbc('\331'); regmbc('\332');
940 regmbc('\333'); regmbc('\334');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200941 REGMBC(0x168) REGMBC(0x16a) REGMBC(0x16c)
942 REGMBC(0x16e) REGMBC(0x170) REGMBC(0x172)
943 REGMBC(0x1af) REGMBC(0x1d3) REGMBC(0x1ee6)
944 return;
945 case 'V': CASEMBC(0x1e7c)
946 regmbc('V'); REGMBC(0x1e7c)
947 return;
948 case 'W': CASEMBC(0x174) CASEMBC(0x1e80) CASEMBC(0x1e82)
949 CASEMBC(0x1e84) CASEMBC(0x1e86)
950 regmbc('W'); REGMBC(0x174) REGMBC(0x1e80)
951 REGMBC(0x1e82) REGMBC(0x1e84) REGMBC(0x1e86)
952 return;
953 case 'X': CASEMBC(0x1e8a) CASEMBC(0x1e8c)
954 regmbc('X'); REGMBC(0x1e8a) REGMBC(0x1e8c)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000955 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000956 case 'Y': case '\335':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200957 CASEMBC(0x176) CASEMBC(0x178) CASEMBC(0x1e8e) CASEMBC(0x1ef2)
958 CASEMBC(0x1ef6) CASEMBC(0x1ef8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000959 regmbc('Y'); regmbc('\335');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200960 REGMBC(0x176) REGMBC(0x178) REGMBC(0x1e8e)
961 REGMBC(0x1ef2) REGMBC(0x1ef6) REGMBC(0x1ef8)
962 return;
963 case 'Z': CASEMBC(0x179) CASEMBC(0x17b) CASEMBC(0x17d)
964 CASEMBC(0x1b5) CASEMBC(0x1e90) CASEMBC(0x1e94)
965 regmbc('Z'); REGMBC(0x179) REGMBC(0x17b)
966 REGMBC(0x17d) REGMBC(0x1b5) REGMBC(0x1e90)
967 REGMBC(0x1e94)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000968 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000969 case 'a': case '\340': case '\341': case '\342':
970 case '\343': case '\344': case '\345':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200971 CASEMBC(0x101) CASEMBC(0x103) CASEMBC(0x105) CASEMBC(0x1ce)
972 CASEMBC(0x1df) CASEMBC(0x1e1) CASEMBC(0x1ea3)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000973 regmbc('a'); regmbc('\340'); regmbc('\341');
974 regmbc('\342'); regmbc('\343'); regmbc('\344');
975 regmbc('\345');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200976 REGMBC(0x101) REGMBC(0x103) REGMBC(0x105)
977 REGMBC(0x1ce) REGMBC(0x1df) REGMBC(0x1e1)
978 REGMBC(0x1ea3)
979 return;
980 case 'b': CASEMBC(0x1e03) CASEMBC(0x1e07)
981 regmbc('b'); REGMBC(0x1e03) REGMBC(0x1e07)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000982 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000983 case 'c': case '\347':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200984 CASEMBC(0x107) CASEMBC(0x109) CASEMBC(0x10b) CASEMBC(0x10d)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000985 regmbc('c'); regmbc('\347');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200986 REGMBC(0x107) REGMBC(0x109) REGMBC(0x10b)
987 REGMBC(0x10d)
988 return;
989 case 'd': CASEMBC(0x10f) CASEMBC(0x111) CASEMBC(0x1d0b)
990 CASEMBC(0x1e11)
991 regmbc('d'); REGMBC(0x10f) REGMBC(0x111)
992 REGMBC(0x1e0b) REGMBC(0x01e0f) REGMBC(0x1e11)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000993 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000994 case 'e': case '\350': case '\351': case '\352': case '\353':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200995 CASEMBC(0x113) CASEMBC(0x115) CASEMBC(0x117) CASEMBC(0x119)
996 CASEMBC(0x11b) CASEMBC(0x1ebb) CASEMBC(0x1ebd)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000997 regmbc('e'); regmbc('\350'); regmbc('\351');
998 regmbc('\352'); regmbc('\353');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200999 REGMBC(0x113) REGMBC(0x115) REGMBC(0x117)
1000 REGMBC(0x119) REGMBC(0x11b) REGMBC(0x1ebb)
1001 REGMBC(0x1ebd)
1002 return;
1003 case 'f': CASEMBC(0x1e1f)
1004 regmbc('f'); REGMBC(0x1e1f)
1005 return;
1006 case 'g': CASEMBC(0x11d) CASEMBC(0x11f) CASEMBC(0x121)
1007 CASEMBC(0x123) CASEMBC(0x1e5) CASEMBC(0x1e7) CASEMBC(0x1f5)
1008 CASEMBC(0x1e21)
1009 regmbc('g'); REGMBC(0x11d) REGMBC(0x11f)
1010 REGMBC(0x121) REGMBC(0x123) REGMBC(0x1e5)
1011 REGMBC(0x1e7) REGMBC(0x1f5) REGMBC(0x1e21)
1012 return;
1013 case 'h': CASEMBC(0x125) CASEMBC(0x127) CASEMBC(0x1e23)
1014 CASEMBC(0x1e27) CASEMBC(0x1e29) CASEMBC(0x1e96)
1015 regmbc('h'); REGMBC(0x125) REGMBC(0x127)
1016 REGMBC(0x1e23) REGMBC(0x1e27) REGMBC(0x1e29)
1017 REGMBC(0x1e96)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001018 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001019 case 'i': case '\354': case '\355': case '\356': case '\357':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001020 CASEMBC(0x129) CASEMBC(0x12b) CASEMBC(0x12d) CASEMBC(0x12f)
1021 CASEMBC(0x1d0) CASEMBC(0x1ec9)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001022 regmbc('i'); regmbc('\354'); regmbc('\355');
1023 regmbc('\356'); regmbc('\357');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001024 REGMBC(0x129) REGMBC(0x12b) REGMBC(0x12d)
1025 REGMBC(0x12f) REGMBC(0x1d0) REGMBC(0x1ec9)
1026 return;
1027 case 'j': CASEMBC(0x135) CASEMBC(0x1f0)
1028 regmbc('j'); REGMBC(0x135) REGMBC(0x1f0)
1029 return;
1030 case 'k': CASEMBC(0x137) CASEMBC(0x1e9) CASEMBC(0x1e31)
1031 CASEMBC(0x1e35)
1032 regmbc('k'); REGMBC(0x137) REGMBC(0x1e9)
1033 REGMBC(0x1e31) REGMBC(0x1e35)
1034 return;
1035 case 'l': CASEMBC(0x13a) CASEMBC(0x13c) CASEMBC(0x13e)
1036 CASEMBC(0x140) CASEMBC(0x142) CASEMBC(0x1e3b)
1037 regmbc('l'); REGMBC(0x13a) REGMBC(0x13c)
1038 REGMBC(0x13e) REGMBC(0x140) REGMBC(0x142)
1039 REGMBC(0x1e3b)
1040 return;
1041 case 'm': CASEMBC(0x1e3f) CASEMBC(0x1e41)
1042 regmbc('m'); REGMBC(0x1e3f) REGMBC(0x1e41)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001043 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001044 case 'n': case '\361':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001045 CASEMBC(0x144) CASEMBC(0x146) CASEMBC(0x148) CASEMBC(0x149)
1046 CASEMBC(0x1e45) CASEMBC(0x1e49)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001047 regmbc('n'); regmbc('\361');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001048 REGMBC(0x144) REGMBC(0x146) REGMBC(0x148)
1049 REGMBC(0x149) REGMBC(0x1e45) REGMBC(0x1e49)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001050 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001051 case 'o': case '\362': case '\363': case '\364': case '\365':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001052 case '\366': case '\370':
1053 CASEMBC(0x14d) CASEMBC(0x14f) CASEMBC(0x151) CASEMBC(0x1a1)
1054 CASEMBC(0x1d2) CASEMBC(0x1eb) CASEMBC(0x1ed) CASEMBC(0x1ecf)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001055 regmbc('o'); regmbc('\362'); regmbc('\363');
1056 regmbc('\364'); regmbc('\365'); regmbc('\366');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001057 regmbc('\370');
1058 REGMBC(0x14d) REGMBC(0x14f) REGMBC(0x151)
1059 REGMBC(0x1a1) REGMBC(0x1d2) REGMBC(0x1eb)
1060 REGMBC(0x1ed) REGMBC(0x1ecf)
1061 return;
1062 case 'p': CASEMBC(0x1e55) CASEMBC(0x1e57)
1063 regmbc('p'); REGMBC(0x1e55) REGMBC(0x1e57)
1064 return;
1065 case 'r': CASEMBC(0x155) CASEMBC(0x157) CASEMBC(0x159)
1066 CASEMBC(0x1e59) CASEMBC(0x1e5f)
1067 regmbc('r'); REGMBC(0x155) REGMBC(0x157) REGMBC(0x159)
1068 REGMBC(0x1e59) REGMBC(0x1e5f)
1069 return;
1070 case 's': CASEMBC(0x15b) CASEMBC(0x15d) CASEMBC(0x15f)
1071 CASEMBC(0x161) CASEMBC(0x1e61)
1072 regmbc('s'); REGMBC(0x15b) REGMBC(0x15d)
1073 REGMBC(0x15f) REGMBC(0x161) REGMBC(0x1e61)
1074 return;
1075 case 't': CASEMBC(0x163) CASEMBC(0x165) CASEMBC(0x167)
1076 CASEMBC(0x1e6b) CASEMBC(0x1e6f) CASEMBC(0x1e97)
1077 regmbc('t'); REGMBC(0x163) REGMBC(0x165) REGMBC(0x167)
1078 REGMBC(0x1e6b) REGMBC(0x1e6f) REGMBC(0x1e97)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001079 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001080 case 'u': case '\371': case '\372': case '\373': case '\374':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001081 CASEMBC(0x169) CASEMBC(0x16b) CASEMBC(0x16d) CASEMBC(0x16f)
1082 CASEMBC(0x171) CASEMBC(0x173) CASEMBC(0x1b0) CASEMBC(0x1d4)
1083 CASEMBC(0x1ee7)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001084 regmbc('u'); regmbc('\371'); regmbc('\372');
1085 regmbc('\373'); regmbc('\374');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001086 REGMBC(0x169) REGMBC(0x16b) REGMBC(0x16d)
1087 REGMBC(0x16f) REGMBC(0x171) REGMBC(0x173)
1088 REGMBC(0x1b0) REGMBC(0x1d4) REGMBC(0x1ee7)
1089 return;
1090 case 'v': CASEMBC(0x1e7d)
1091 regmbc('v'); REGMBC(0x1e7d)
1092 return;
1093 case 'w': CASEMBC(0x175) CASEMBC(0x1e81) CASEMBC(0x1e83)
1094 CASEMBC(0x1e85) CASEMBC(0x1e87) CASEMBC(0x1e98)
1095 regmbc('w'); REGMBC(0x175) REGMBC(0x1e81)
1096 REGMBC(0x1e83) REGMBC(0x1e85) REGMBC(0x1e87)
1097 REGMBC(0x1e98)
1098 return;
1099 case 'x': CASEMBC(0x1e8b) CASEMBC(0x1e8d)
1100 regmbc('x'); REGMBC(0x1e8b) REGMBC(0x1e8d)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001101 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001102 case 'y': case '\375': case '\377':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001103 CASEMBC(0x177) CASEMBC(0x1e8f) CASEMBC(0x1e99)
1104 CASEMBC(0x1ef3) CASEMBC(0x1ef7) CASEMBC(0x1ef9)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001105 regmbc('y'); regmbc('\375'); regmbc('\377');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001106 REGMBC(0x177) REGMBC(0x1e8f) REGMBC(0x1e99)
1107 REGMBC(0x1ef3) REGMBC(0x1ef7) REGMBC(0x1ef9)
1108 return;
1109 case 'z': CASEMBC(0x17a) CASEMBC(0x17c) CASEMBC(0x17e)
1110 CASEMBC(0x1b6) CASEMBC(0x1e91) CASEMBC(0x1e95)
1111 regmbc('z'); REGMBC(0x17a) REGMBC(0x17c)
1112 REGMBC(0x17e) REGMBC(0x1b6) REGMBC(0x1e91)
1113 REGMBC(0x1e95)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001114 return;
1115 }
Bram Moolenaar2c704a72010-06-03 21:17:25 +02001116#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00001117 }
1118 regmbc(c);
1119}
1120
1121/*
1122 * Check for a collating element "[.a.]". "pp" points to the '['.
1123 * Returns a character. Zero means that no item was recognized. Otherwise
1124 * "pp" is advanced to after the item.
1125 * Currently only single characters are recognized!
1126 */
1127 static int
1128get_coll_element(pp)
1129 char_u **pp;
1130{
1131 int c;
1132 int l = 1;
1133 char_u *p = *pp;
1134
1135 if (p[1] == '.')
1136 {
1137#ifdef FEAT_MBYTE
1138 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001139 l = (*mb_ptr2len)(p + 2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00001140#endif
1141 if (p[l + 2] == '.' && p[l + 3] == ']')
1142 {
1143#ifdef FEAT_MBYTE
1144 if (has_mbyte)
1145 c = mb_ptr2char(p + 2);
1146 else
1147#endif
1148 c = p[2];
1149 *pp += l + 4;
1150 return c;
1151 }
1152 }
1153 return 0;
1154}
1155
1156
1157/*
1158 * Skip over a "[]" range.
1159 * "p" must point to the character after the '['.
1160 * The returned pointer is on the matching ']', or the terminating NUL.
1161 */
1162 static char_u *
1163skip_anyof(p)
1164 char_u *p;
1165{
1166 int cpo_lit; /* 'cpoptions' contains 'l' flag */
1167 int cpo_bsl; /* 'cpoptions' contains '\' flag */
1168#ifdef FEAT_MBYTE
1169 int l;
1170#endif
1171
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00001172 cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL;
1173 cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001174
1175 if (*p == '^') /* Complement of range. */
1176 ++p;
1177 if (*p == ']' || *p == '-')
1178 ++p;
1179 while (*p != NUL && *p != ']')
1180 {
1181#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001182 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001183 p += l;
1184 else
1185#endif
1186 if (*p == '-')
1187 {
1188 ++p;
1189 if (*p != ']' && *p != NUL)
1190 mb_ptr_adv(p);
1191 }
1192 else if (*p == '\\'
1193 && !cpo_bsl
1194 && (vim_strchr(REGEXP_INRANGE, p[1]) != NULL
1195 || (!cpo_lit && vim_strchr(REGEXP_ABBR, p[1]) != NULL)))
1196 p += 2;
1197 else if (*p == '[')
1198 {
1199 if (get_char_class(&p) == CLASS_NONE
1200 && get_equi_class(&p) == 0
1201 && get_coll_element(&p) == 0)
1202 ++p; /* It was not a class name */
1203 }
1204 else
1205 ++p;
1206 }
1207
1208 return p;
1209}
1210
1211/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 * Skip past regular expression.
Bram Moolenaar748bf032005-02-02 23:04:36 +00001213 * Stop at end of "startp" or where "dirc" is found ('/', '?', etc).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 * Take care of characters with a backslash in front of it.
1215 * Skip strings inside [ and ].
1216 * When "newp" is not NULL and "dirc" is '?', make an allocated copy of the
1217 * expression and change "\?" to "?". If "*newp" is not NULL the expression
1218 * is changed in-place.
1219 */
1220 char_u *
1221skip_regexp(startp, dirc, magic, newp)
1222 char_u *startp;
1223 int dirc;
1224 int magic;
1225 char_u **newp;
1226{
1227 int mymagic;
1228 char_u *p = startp;
1229
1230 if (magic)
1231 mymagic = MAGIC_ON;
1232 else
1233 mymagic = MAGIC_OFF;
1234
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001235 for (; p[0] != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 {
1237 if (p[0] == dirc) /* found end of regexp */
1238 break;
1239 if ((p[0] == '[' && mymagic >= MAGIC_ON)
1240 || (p[0] == '\\' && p[1] == '[' && mymagic <= MAGIC_OFF))
1241 {
1242 p = skip_anyof(p + 1);
1243 if (p[0] == NUL)
1244 break;
1245 }
1246 else if (p[0] == '\\' && p[1] != NUL)
1247 {
1248 if (dirc == '?' && newp != NULL && p[1] == '?')
1249 {
1250 /* change "\?" to "?", make a copy first. */
1251 if (*newp == NULL)
1252 {
1253 *newp = vim_strsave(startp);
1254 if (*newp != NULL)
1255 p = *newp + (p - startp);
1256 }
1257 if (*newp != NULL)
Bram Moolenaar446cb832008-06-24 21:56:24 +00001258 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 else
1260 ++p;
1261 }
1262 else
1263 ++p; /* skip next character */
1264 if (*p == 'v')
1265 mymagic = MAGIC_ALL;
1266 else if (*p == 'V')
1267 mymagic = MAGIC_NONE;
1268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 }
1270 return p;
1271}
1272
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001273static regprog_T *bt_regcomp __ARGS((char_u *expr, int re_flags));
1274
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001276 * bt_regcomp() - compile a regular expression into internal code for the
1277 * traditional back track matcher.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001278 * Returns the program in allocated space. Returns NULL for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 *
1280 * We can't allocate space until we know how big the compiled form will be,
1281 * but we can't compile it (and thus know how big it is) until we've got a
1282 * place to put the code. So we cheat: we compile it twice, once with code
1283 * generation turned off and size counting turned on, and once "for real".
1284 * This also means that we don't allocate space until we are sure that the
1285 * thing really will compile successfully, and we never have to move the
1286 * code and thus invalidate pointers into it. (Note that it has to be in
1287 * one piece because vim_free() must be able to free it all.)
1288 *
1289 * Whether upper/lower case is to be ignored is decided when executing the
1290 * program, it does not matter here.
1291 *
1292 * Beware that the optimization-preparation code in here knows about some
1293 * of the structure of the compiled regexp.
1294 * "re_flags": RE_MAGIC and/or RE_STRING.
1295 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001296 static regprog_T *
1297bt_regcomp(expr, re_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 char_u *expr;
1299 int re_flags;
1300{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001301 bt_regprog_T *r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 char_u *scan;
1303 char_u *longest;
1304 int len;
1305 int flags;
1306
1307 if (expr == NULL)
1308 EMSG_RET_NULL(_(e_null));
1309
1310 init_class_tab();
1311
1312 /*
1313 * First pass: determine size, legality.
1314 */
1315 regcomp_start(expr, re_flags);
1316 regcode = JUST_CALC_SIZE;
1317 regc(REGMAGIC);
1318 if (reg(REG_NOPAREN, &flags) == NULL)
1319 return NULL;
1320
1321 /* Small enough for pointer-storage convention? */
1322#ifdef SMALL_MALLOC /* 16 bit storage allocation */
1323 if (regsize >= 65536L - 256L)
1324 EMSG_RET_NULL(_("E339: Pattern too long"));
1325#endif
1326
1327 /* Allocate space. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001328 r = (bt_regprog_T *)lalloc(sizeof(bt_regprog_T) + regsize, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 if (r == NULL)
1330 return NULL;
1331
1332 /*
1333 * Second pass: emit code.
1334 */
1335 regcomp_start(expr, re_flags);
1336 regcode = r->program;
1337 regc(REGMAGIC);
Bram Moolenaard3005802009-11-25 17:21:32 +00001338 if (reg(REG_NOPAREN, &flags) == NULL || reg_toolong)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 {
1340 vim_free(r);
Bram Moolenaard3005802009-11-25 17:21:32 +00001341 if (reg_toolong)
1342 EMSG_RET_NULL(_("E339: Pattern too long"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 return NULL;
1344 }
1345
1346 /* Dig out information for optimizations. */
1347 r->regstart = NUL; /* Worst-case defaults. */
1348 r->reganch = 0;
1349 r->regmust = NULL;
1350 r->regmlen = 0;
1351 r->regflags = regflags;
1352 if (flags & HASNL)
1353 r->regflags |= RF_HASNL;
1354 if (flags & HASLOOKBH)
1355 r->regflags |= RF_LOOKBH;
1356#ifdef FEAT_SYN_HL
1357 /* Remember whether this pattern has any \z specials in it. */
1358 r->reghasz = re_has_z;
1359#endif
1360 scan = r->program + 1; /* First BRANCH. */
1361 if (OP(regnext(scan)) == END) /* Only one top-level choice. */
1362 {
1363 scan = OPERAND(scan);
1364
1365 /* Starting-point info. */
1366 if (OP(scan) == BOL || OP(scan) == RE_BOF)
1367 {
1368 r->reganch++;
1369 scan = regnext(scan);
1370 }
1371
1372 if (OP(scan) == EXACTLY)
1373 {
1374#ifdef FEAT_MBYTE
1375 if (has_mbyte)
1376 r->regstart = (*mb_ptr2char)(OPERAND(scan));
1377 else
1378#endif
1379 r->regstart = *OPERAND(scan);
1380 }
1381 else if ((OP(scan) == BOW
1382 || OP(scan) == EOW
1383 || OP(scan) == NOTHING
1384 || OP(scan) == MOPEN + 0 || OP(scan) == NOPEN
1385 || OP(scan) == MCLOSE + 0 || OP(scan) == NCLOSE)
1386 && OP(regnext(scan)) == EXACTLY)
1387 {
1388#ifdef FEAT_MBYTE
1389 if (has_mbyte)
1390 r->regstart = (*mb_ptr2char)(OPERAND(regnext(scan)));
1391 else
1392#endif
1393 r->regstart = *OPERAND(regnext(scan));
1394 }
1395
1396 /*
1397 * If there's something expensive in the r.e., find the longest
1398 * literal string that must appear and make it the regmust. Resolve
1399 * ties in favor of later strings, since the regstart check works
1400 * with the beginning of the r.e. and avoiding duplication
1401 * strengthens checking. Not a strong reason, but sufficient in the
1402 * absence of others.
1403 */
1404 /*
1405 * When the r.e. starts with BOW, it is faster to look for a regmust
1406 * first. Used a lot for "#" and "*" commands. (Added by mool).
1407 */
1408 if ((flags & SPSTART || OP(scan) == BOW || OP(scan) == EOW)
1409 && !(flags & HASNL))
1410 {
1411 longest = NULL;
1412 len = 0;
1413 for (; scan != NULL; scan = regnext(scan))
1414 if (OP(scan) == EXACTLY && STRLEN(OPERAND(scan)) >= (size_t)len)
1415 {
1416 longest = OPERAND(scan);
1417 len = (int)STRLEN(OPERAND(scan));
1418 }
1419 r->regmust = longest;
1420 r->regmlen = len;
1421 }
1422 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001423#ifdef BT_REGEXP_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 regdump(expr, r);
1425#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001426 r->engine = &bt_regengine;
1427 return (regprog_T *)r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428}
1429
1430/*
1431 * Setup to parse the regexp. Used once to get the length and once to do it.
1432 */
1433 static void
1434regcomp_start(expr, re_flags)
1435 char_u *expr;
1436 int re_flags; /* see vim_regcomp() */
1437{
1438 initchr(expr);
1439 if (re_flags & RE_MAGIC)
1440 reg_magic = MAGIC_ON;
1441 else
1442 reg_magic = MAGIC_OFF;
1443 reg_string = (re_flags & RE_STRING);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001444 reg_strict = (re_flags & RE_STRICT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445
1446 num_complex_braces = 0;
1447 regnpar = 1;
1448 vim_memset(had_endbrace, 0, sizeof(had_endbrace));
1449#ifdef FEAT_SYN_HL
1450 regnzpar = 1;
1451 re_has_z = 0;
1452#endif
1453 regsize = 0L;
Bram Moolenaard3005802009-11-25 17:21:32 +00001454 reg_toolong = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 regflags = 0;
1456#if defined(FEAT_SYN_HL) || defined(PROTO)
1457 had_eol = FALSE;
1458#endif
1459}
1460
1461#if defined(FEAT_SYN_HL) || defined(PROTO)
1462/*
1463 * Check if during the previous call to vim_regcomp the EOL item "$" has been
1464 * found. This is messy, but it works fine.
1465 */
1466 int
1467vim_regcomp_had_eol()
1468{
1469 return had_eol;
1470}
1471#endif
1472
1473/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001474 * Parse regular expression, i.e. main body or parenthesized thing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 *
1476 * Caller must absorb opening parenthesis.
1477 *
1478 * Combining parenthesis handling with the base level of regular expression
1479 * is a trifle forced, but the need to tie the tails of the branches to what
1480 * follows makes it hard to avoid.
1481 */
1482 static char_u *
1483reg(paren, flagp)
1484 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1485 int *flagp;
1486{
1487 char_u *ret;
1488 char_u *br;
1489 char_u *ender;
1490 int parno = 0;
1491 int flags;
1492
1493 *flagp = HASWIDTH; /* Tentatively. */
1494
1495#ifdef FEAT_SYN_HL
1496 if (paren == REG_ZPAREN)
1497 {
1498 /* Make a ZOPEN node. */
1499 if (regnzpar >= NSUBEXP)
1500 EMSG_RET_NULL(_("E50: Too many \\z("));
1501 parno = regnzpar;
1502 regnzpar++;
1503 ret = regnode(ZOPEN + parno);
1504 }
1505 else
1506#endif
1507 if (paren == REG_PAREN)
1508 {
1509 /* Make a MOPEN node. */
1510 if (regnpar >= NSUBEXP)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001511 EMSG2_RET_NULL(_("E51: Too many %s("), reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 parno = regnpar;
1513 ++regnpar;
1514 ret = regnode(MOPEN + parno);
1515 }
1516 else if (paren == REG_NPAREN)
1517 {
1518 /* Make a NOPEN node. */
1519 ret = regnode(NOPEN);
1520 }
1521 else
1522 ret = NULL;
1523
1524 /* Pick up the branches, linking them together. */
1525 br = regbranch(&flags);
1526 if (br == NULL)
1527 return NULL;
1528 if (ret != NULL)
1529 regtail(ret, br); /* [MZ]OPEN -> first. */
1530 else
1531 ret = br;
1532 /* If one of the branches can be zero-width, the whole thing can.
1533 * If one of the branches has * at start or matches a line-break, the
1534 * whole thing can. */
1535 if (!(flags & HASWIDTH))
1536 *flagp &= ~HASWIDTH;
1537 *flagp |= flags & (SPSTART | HASNL | HASLOOKBH);
1538 while (peekchr() == Magic('|'))
1539 {
1540 skipchr();
1541 br = regbranch(&flags);
Bram Moolenaard3005802009-11-25 17:21:32 +00001542 if (br == NULL || reg_toolong)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 return NULL;
1544 regtail(ret, br); /* BRANCH -> BRANCH. */
1545 if (!(flags & HASWIDTH))
1546 *flagp &= ~HASWIDTH;
1547 *flagp |= flags & (SPSTART | HASNL | HASLOOKBH);
1548 }
1549
1550 /* Make a closing node, and hook it on the end. */
1551 ender = regnode(
1552#ifdef FEAT_SYN_HL
1553 paren == REG_ZPAREN ? ZCLOSE + parno :
1554#endif
1555 paren == REG_PAREN ? MCLOSE + parno :
1556 paren == REG_NPAREN ? NCLOSE : END);
1557 regtail(ret, ender);
1558
1559 /* Hook the tails of the branches to the closing node. */
1560 for (br = ret; br != NULL; br = regnext(br))
1561 regoptail(br, ender);
1562
1563 /* Check for proper termination. */
1564 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1565 {
1566#ifdef FEAT_SYN_HL
1567 if (paren == REG_ZPAREN)
Bram Moolenaar45eeb132005-06-06 21:59:07 +00001568 EMSG_RET_NULL(_("E52: Unmatched \\z("));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 else
1570#endif
1571 if (paren == REG_NPAREN)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001572 EMSG2_RET_NULL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001574 EMSG2_RET_NULL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 }
1576 else if (paren == REG_NOPAREN && peekchr() != NUL)
1577 {
1578 if (curchr == Magic(')'))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001579 EMSG2_RET_NULL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 else
Bram Moolenaar45eeb132005-06-06 21:59:07 +00001581 EMSG_RET_NULL(_(e_trailing)); /* "Can't happen". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 /* NOTREACHED */
1583 }
1584 /*
1585 * Here we set the flag allowing back references to this set of
1586 * parentheses.
1587 */
1588 if (paren == REG_PAREN)
1589 had_endbrace[parno] = TRUE; /* have seen the close paren */
1590 return ret;
1591}
1592
1593/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001594 * Parse one alternative of an | operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 * Implements the & operator.
1596 */
1597 static char_u *
1598regbranch(flagp)
1599 int *flagp;
1600{
1601 char_u *ret;
1602 char_u *chain = NULL;
1603 char_u *latest;
1604 int flags;
1605
1606 *flagp = WORST | HASNL; /* Tentatively. */
1607
1608 ret = regnode(BRANCH);
1609 for (;;)
1610 {
1611 latest = regconcat(&flags);
1612 if (latest == NULL)
1613 return NULL;
1614 /* If one of the branches has width, the whole thing has. If one of
1615 * the branches anchors at start-of-line, the whole thing does.
1616 * If one of the branches uses look-behind, the whole thing does. */
1617 *flagp |= flags & (HASWIDTH | SPSTART | HASLOOKBH);
1618 /* If one of the branches doesn't match a line-break, the whole thing
1619 * doesn't. */
1620 *flagp &= ~HASNL | (flags & HASNL);
1621 if (chain != NULL)
1622 regtail(chain, latest);
1623 if (peekchr() != Magic('&'))
1624 break;
1625 skipchr();
1626 regtail(latest, regnode(END)); /* operand ends */
Bram Moolenaard3005802009-11-25 17:21:32 +00001627 if (reg_toolong)
1628 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 reginsert(MATCH, latest);
1630 chain = latest;
1631 }
1632
1633 return ret;
1634}
1635
1636/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001637 * Parse one alternative of an | or & operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 * Implements the concatenation operator.
1639 */
1640 static char_u *
1641regconcat(flagp)
1642 int *flagp;
1643{
1644 char_u *first = NULL;
1645 char_u *chain = NULL;
1646 char_u *latest;
1647 int flags;
1648 int cont = TRUE;
1649
1650 *flagp = WORST; /* Tentatively. */
1651
1652 while (cont)
1653 {
1654 switch (peekchr())
1655 {
1656 case NUL:
1657 case Magic('|'):
1658 case Magic('&'):
1659 case Magic(')'):
1660 cont = FALSE;
1661 break;
1662 case Magic('Z'):
1663#ifdef FEAT_MBYTE
1664 regflags |= RF_ICOMBINE;
1665#endif
1666 skipchr_keepstart();
1667 break;
1668 case Magic('c'):
1669 regflags |= RF_ICASE;
1670 skipchr_keepstart();
1671 break;
1672 case Magic('C'):
1673 regflags |= RF_NOICASE;
1674 skipchr_keepstart();
1675 break;
1676 case Magic('v'):
1677 reg_magic = MAGIC_ALL;
1678 skipchr_keepstart();
1679 curchr = -1;
1680 break;
1681 case Magic('m'):
1682 reg_magic = MAGIC_ON;
1683 skipchr_keepstart();
1684 curchr = -1;
1685 break;
1686 case Magic('M'):
1687 reg_magic = MAGIC_OFF;
1688 skipchr_keepstart();
1689 curchr = -1;
1690 break;
1691 case Magic('V'):
1692 reg_magic = MAGIC_NONE;
1693 skipchr_keepstart();
1694 curchr = -1;
1695 break;
1696 default:
1697 latest = regpiece(&flags);
Bram Moolenaard3005802009-11-25 17:21:32 +00001698 if (latest == NULL || reg_toolong)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 return NULL;
1700 *flagp |= flags & (HASWIDTH | HASNL | HASLOOKBH);
1701 if (chain == NULL) /* First piece. */
1702 *flagp |= flags & SPSTART;
1703 else
1704 regtail(chain, latest);
1705 chain = latest;
1706 if (first == NULL)
1707 first = latest;
1708 break;
1709 }
1710 }
1711 if (first == NULL) /* Loop ran zero times. */
1712 first = regnode(NOTHING);
1713 return first;
1714}
1715
1716/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001717 * Parse something followed by possible [*+=].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 *
1719 * Note that the branching code sequences used for = and the general cases
1720 * of * and + are somewhat optimized: they use the same NOTHING node as
1721 * both the endmarker for their branch list and the body of the last branch.
1722 * It might seem that this node could be dispensed with entirely, but the
1723 * endmarker role is not redundant.
1724 */
1725 static char_u *
1726regpiece(flagp)
1727 int *flagp;
1728{
1729 char_u *ret;
1730 int op;
1731 char_u *next;
1732 int flags;
1733 long minval;
1734 long maxval;
1735
1736 ret = regatom(&flags);
1737 if (ret == NULL)
1738 return NULL;
1739
1740 op = peekchr();
1741 if (re_multi_type(op) == NOT_MULTI)
1742 {
1743 *flagp = flags;
1744 return ret;
1745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 /* default flags */
1747 *flagp = (WORST | SPSTART | (flags & (HASNL | HASLOOKBH)));
1748
1749 skipchr();
1750 switch (op)
1751 {
1752 case Magic('*'):
1753 if (flags & SIMPLE)
1754 reginsert(STAR, ret);
1755 else
1756 {
1757 /* Emit x* as (x&|), where & means "self". */
1758 reginsert(BRANCH, ret); /* Either x */
1759 regoptail(ret, regnode(BACK)); /* and loop */
1760 regoptail(ret, ret); /* back */
1761 regtail(ret, regnode(BRANCH)); /* or */
1762 regtail(ret, regnode(NOTHING)); /* null. */
1763 }
1764 break;
1765
1766 case Magic('+'):
1767 if (flags & SIMPLE)
1768 reginsert(PLUS, ret);
1769 else
1770 {
1771 /* Emit x+ as x(&|), where & means "self". */
1772 next = regnode(BRANCH); /* Either */
1773 regtail(ret, next);
Bram Moolenaar582fd852005-03-28 20:58:01 +00001774 regtail(regnode(BACK), ret); /* loop back */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 regtail(next, regnode(BRANCH)); /* or */
1776 regtail(ret, regnode(NOTHING)); /* null. */
1777 }
1778 *flagp = (WORST | HASWIDTH | (flags & (HASNL | HASLOOKBH)));
1779 break;
1780
1781 case Magic('@'):
1782 {
1783 int lop = END;
1784
1785 switch (no_Magic(getchr()))
1786 {
1787 case '=': lop = MATCH; break; /* \@= */
1788 case '!': lop = NOMATCH; break; /* \@! */
1789 case '>': lop = SUBPAT; break; /* \@> */
1790 case '<': switch (no_Magic(getchr()))
1791 {
1792 case '=': lop = BEHIND; break; /* \@<= */
1793 case '!': lop = NOBEHIND; break; /* \@<! */
1794 }
1795 }
1796 if (lop == END)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001797 EMSG2_RET_NULL(_("E59: invalid character after %s@"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 reg_magic == MAGIC_ALL);
1799 /* Look behind must match with behind_pos. */
1800 if (lop == BEHIND || lop == NOBEHIND)
1801 {
1802 regtail(ret, regnode(BHPOS));
1803 *flagp |= HASLOOKBH;
1804 }
1805 regtail(ret, regnode(END)); /* operand ends */
1806 reginsert(lop, ret);
1807 break;
1808 }
1809
1810 case Magic('?'):
1811 case Magic('='):
1812 /* Emit x= as (x|) */
1813 reginsert(BRANCH, ret); /* Either x */
1814 regtail(ret, regnode(BRANCH)); /* or */
1815 next = regnode(NOTHING); /* null. */
1816 regtail(ret, next);
1817 regoptail(ret, next);
1818 break;
1819
1820 case Magic('{'):
1821 if (!read_limits(&minval, &maxval))
1822 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 if (flags & SIMPLE)
1824 {
1825 reginsert(BRACE_SIMPLE, ret);
1826 reginsert_limits(BRACE_LIMITS, minval, maxval, ret);
1827 }
1828 else
1829 {
1830 if (num_complex_braces >= 10)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001831 EMSG2_RET_NULL(_("E60: Too many complex %s{...}s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 reg_magic == MAGIC_ALL);
1833 reginsert(BRACE_COMPLEX + num_complex_braces, ret);
1834 regoptail(ret, regnode(BACK));
1835 regoptail(ret, ret);
1836 reginsert_limits(BRACE_LIMITS, minval, maxval, ret);
1837 ++num_complex_braces;
1838 }
1839 if (minval > 0 && maxval > 0)
1840 *flagp = (HASWIDTH | (flags & (HASNL | HASLOOKBH)));
1841 break;
1842 }
1843 if (re_multi_type(peekchr()) != NOT_MULTI)
1844 {
1845 /* Can't have a multi follow a multi. */
1846 if (peekchr() == Magic('*'))
1847 sprintf((char *)IObuff, _("E61: Nested %s*"),
1848 reg_magic >= MAGIC_ON ? "" : "\\");
1849 else
1850 sprintf((char *)IObuff, _("E62: Nested %s%c"),
1851 reg_magic == MAGIC_ALL ? "" : "\\", no_Magic(peekchr()));
1852 EMSG_RET_NULL(IObuff);
1853 }
1854
1855 return ret;
1856}
1857
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001858/* When making changes to classchars also change nfa_classcodes. */
1859static char_u *classchars = (char_u *)".iIkKfFpPsSdDxXoOwWhHaAlLuU";
1860static int classcodes[] = {
1861 ANY, IDENT, SIDENT, KWORD, SKWORD,
1862 FNAME, SFNAME, PRINT, SPRINT,
1863 WHITE, NWHITE, DIGIT, NDIGIT,
1864 HEX, NHEX, OCTAL, NOCTAL,
1865 WORD, NWORD, HEAD, NHEAD,
1866 ALPHA, NALPHA, LOWER, NLOWER,
1867 UPPER, NUPPER
1868};
1869
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001871 * Parse the lowest level.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 *
1873 * Optimization: gobbles an entire sequence of ordinary characters so that
1874 * it can turn them into a single node, which is smaller to store and
1875 * faster to run. Don't do this when one_exactly is set.
1876 */
1877 static char_u *
1878regatom(flagp)
1879 int *flagp;
1880{
1881 char_u *ret;
1882 int flags;
1883 int cpo_lit; /* 'cpoptions' contains 'l' flag */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001884 int cpo_bsl; /* 'cpoptions' contains '\' flag */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 char_u *p;
1887 int extra = 0;
1888
1889 *flagp = WORST; /* Tentatively. */
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00001890 cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL;
1891 cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892
1893 c = getchr();
1894 switch (c)
1895 {
1896 case Magic('^'):
1897 ret = regnode(BOL);
1898 break;
1899
1900 case Magic('$'):
1901 ret = regnode(EOL);
1902#if defined(FEAT_SYN_HL) || defined(PROTO)
1903 had_eol = TRUE;
1904#endif
1905 break;
1906
1907 case Magic('<'):
1908 ret = regnode(BOW);
1909 break;
1910
1911 case Magic('>'):
1912 ret = regnode(EOW);
1913 break;
1914
1915 case Magic('_'):
1916 c = no_Magic(getchr());
1917 if (c == '^') /* "\_^" is start-of-line */
1918 {
1919 ret = regnode(BOL);
1920 break;
1921 }
1922 if (c == '$') /* "\_$" is end-of-line */
1923 {
1924 ret = regnode(EOL);
1925#if defined(FEAT_SYN_HL) || defined(PROTO)
1926 had_eol = TRUE;
1927#endif
1928 break;
1929 }
1930
1931 extra = ADD_NL;
1932 *flagp |= HASNL;
1933
1934 /* "\_[" is character range plus newline */
1935 if (c == '[')
1936 goto collection;
1937
1938 /* "\_x" is character class plus newline */
1939 /*FALLTHROUGH*/
1940
1941 /*
1942 * Character classes.
1943 */
1944 case Magic('.'):
1945 case Magic('i'):
1946 case Magic('I'):
1947 case Magic('k'):
1948 case Magic('K'):
1949 case Magic('f'):
1950 case Magic('F'):
1951 case Magic('p'):
1952 case Magic('P'):
1953 case Magic('s'):
1954 case Magic('S'):
1955 case Magic('d'):
1956 case Magic('D'):
1957 case Magic('x'):
1958 case Magic('X'):
1959 case Magic('o'):
1960 case Magic('O'):
1961 case Magic('w'):
1962 case Magic('W'):
1963 case Magic('h'):
1964 case Magic('H'):
1965 case Magic('a'):
1966 case Magic('A'):
1967 case Magic('l'):
1968 case Magic('L'):
1969 case Magic('u'):
1970 case Magic('U'):
1971 p = vim_strchr(classchars, no_Magic(c));
1972 if (p == NULL)
1973 EMSG_RET_NULL(_("E63: invalid use of \\_"));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001974#ifdef FEAT_MBYTE
1975 /* When '.' is followed by a composing char ignore the dot, so that
1976 * the composing char is matched here. */
1977 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
1978 {
1979 c = getchr();
1980 goto do_multibyte;
1981 }
1982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 ret = regnode(classcodes[p - classchars] + extra);
1984 *flagp |= HASWIDTH | SIMPLE;
1985 break;
1986
1987 case Magic('n'):
1988 if (reg_string)
1989 {
1990 /* In a string "\n" matches a newline character. */
1991 ret = regnode(EXACTLY);
1992 regc(NL);
1993 regc(NUL);
1994 *flagp |= HASWIDTH | SIMPLE;
1995 }
1996 else
1997 {
1998 /* In buffer text "\n" matches the end of a line. */
1999 ret = regnode(NEWL);
2000 *flagp |= HASWIDTH | HASNL;
2001 }
2002 break;
2003
2004 case Magic('('):
2005 if (one_exactly)
2006 EMSG_ONE_RET_NULL;
2007 ret = reg(REG_PAREN, &flags);
2008 if (ret == NULL)
2009 return NULL;
2010 *flagp |= flags & (HASWIDTH | SPSTART | HASNL | HASLOOKBH);
2011 break;
2012
2013 case NUL:
2014 case Magic('|'):
2015 case Magic('&'):
2016 case Magic(')'):
Bram Moolenaard4210772008-01-02 14:35:30 +00002017 if (one_exactly)
2018 EMSG_ONE_RET_NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 EMSG_RET_NULL(_(e_internal)); /* Supposed to be caught earlier. */
2020 /* NOTREACHED */
2021
2022 case Magic('='):
2023 case Magic('?'):
2024 case Magic('+'):
2025 case Magic('@'):
2026 case Magic('{'):
2027 case Magic('*'):
2028 c = no_Magic(c);
2029 sprintf((char *)IObuff, _("E64: %s%c follows nothing"),
2030 (c == '*' ? reg_magic >= MAGIC_ON : reg_magic == MAGIC_ALL)
2031 ? "" : "\\", c);
2032 EMSG_RET_NULL(IObuff);
2033 /* NOTREACHED */
2034
2035 case Magic('~'): /* previous substitute pattern */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002036 if (reg_prev_sub != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 {
2038 char_u *lp;
2039
2040 ret = regnode(EXACTLY);
2041 lp = reg_prev_sub;
2042 while (*lp != NUL)
2043 regc(*lp++);
2044 regc(NUL);
2045 if (*reg_prev_sub != NUL)
2046 {
2047 *flagp |= HASWIDTH;
2048 if ((lp - reg_prev_sub) == 1)
2049 *flagp |= SIMPLE;
2050 }
2051 }
2052 else
2053 EMSG_RET_NULL(_(e_nopresub));
2054 break;
2055
2056 case Magic('1'):
2057 case Magic('2'):
2058 case Magic('3'):
2059 case Magic('4'):
2060 case Magic('5'):
2061 case Magic('6'):
2062 case Magic('7'):
2063 case Magic('8'):
2064 case Magic('9'):
2065 {
2066 int refnum;
2067
2068 refnum = c - Magic('0');
2069 /*
2070 * Check if the back reference is legal. We must have seen the
2071 * close brace.
2072 * TODO: Should also check that we don't refer to something
2073 * that is repeated (+*=): what instance of the repetition
2074 * should we match?
2075 */
2076 if (!had_endbrace[refnum])
2077 {
2078 /* Trick: check if "@<=" or "@<!" follows, in which case
2079 * the \1 can appear before the referenced match. */
2080 for (p = regparse; *p != NUL; ++p)
2081 if (p[0] == '@' && p[1] == '<'
2082 && (p[2] == '!' || p[2] == '='))
2083 break;
2084 if (*p == NUL)
2085 EMSG_RET_NULL(_("E65: Illegal back reference"));
2086 }
2087 ret = regnode(BACKREF + refnum);
2088 }
2089 break;
2090
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 case Magic('z'):
2092 {
2093 c = no_Magic(getchr());
2094 switch (c)
2095 {
Bram Moolenaarc4956c82006-03-12 21:58:43 +00002096#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 case '(': if (reg_do_extmatch != REX_SET)
2098 EMSG_RET_NULL(_("E66: \\z( not allowed here"));
2099 if (one_exactly)
2100 EMSG_ONE_RET_NULL;
2101 ret = reg(REG_ZPAREN, &flags);
2102 if (ret == NULL)
2103 return NULL;
2104 *flagp |= flags & (HASWIDTH|SPSTART|HASNL|HASLOOKBH);
2105 re_has_z = REX_SET;
2106 break;
2107
2108 case '1':
2109 case '2':
2110 case '3':
2111 case '4':
2112 case '5':
2113 case '6':
2114 case '7':
2115 case '8':
2116 case '9': if (reg_do_extmatch != REX_USE)
2117 EMSG_RET_NULL(_("E67: \\z1 et al. not allowed here"));
2118 ret = regnode(ZREF + c - '0');
2119 re_has_z = REX_USE;
2120 break;
Bram Moolenaarc4956c82006-03-12 21:58:43 +00002121#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122
2123 case 's': ret = regnode(MOPEN + 0);
2124 break;
2125
2126 case 'e': ret = regnode(MCLOSE + 0);
2127 break;
2128
2129 default: EMSG_RET_NULL(_("E68: Invalid character after \\z"));
2130 }
2131 }
2132 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133
2134 case Magic('%'):
2135 {
2136 c = no_Magic(getchr());
2137 switch (c)
2138 {
2139 /* () without a back reference */
2140 case '(':
2141 if (one_exactly)
2142 EMSG_ONE_RET_NULL;
2143 ret = reg(REG_NPAREN, &flags);
2144 if (ret == NULL)
2145 return NULL;
2146 *flagp |= flags & (HASWIDTH | SPSTART | HASNL | HASLOOKBH);
2147 break;
2148
2149 /* Catch \%^ and \%$ regardless of where they appear in the
2150 * pattern -- regardless of whether or not it makes sense. */
2151 case '^':
2152 ret = regnode(RE_BOF);
2153 break;
2154
2155 case '$':
2156 ret = regnode(RE_EOF);
2157 break;
2158
2159 case '#':
2160 ret = regnode(CURSOR);
2161 break;
2162
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00002163 case 'V':
2164 ret = regnode(RE_VISUAL);
2165 break;
2166
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 /* \%[abc]: Emit as a list of branches, all ending at the last
2168 * branch which matches nothing. */
2169 case '[':
2170 if (one_exactly) /* doesn't nest */
2171 EMSG_ONE_RET_NULL;
2172 {
2173 char_u *lastbranch;
2174 char_u *lastnode = NULL;
2175 char_u *br;
2176
2177 ret = NULL;
2178 while ((c = getchr()) != ']')
2179 {
2180 if (c == NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002181 EMSG2_RET_NULL(_("E69: Missing ] after %s%%["),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 reg_magic == MAGIC_ALL);
2183 br = regnode(BRANCH);
2184 if (ret == NULL)
2185 ret = br;
2186 else
2187 regtail(lastnode, br);
2188
2189 ungetchr();
2190 one_exactly = TRUE;
2191 lastnode = regatom(flagp);
2192 one_exactly = FALSE;
2193 if (lastnode == NULL)
2194 return NULL;
2195 }
2196 if (ret == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002197 EMSG2_RET_NULL(_("E70: Empty %s%%[]"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 reg_magic == MAGIC_ALL);
2199 lastbranch = regnode(BRANCH);
2200 br = regnode(NOTHING);
2201 if (ret != JUST_CALC_SIZE)
2202 {
2203 regtail(lastnode, br);
2204 regtail(lastbranch, br);
2205 /* connect all branches to the NOTHING
2206 * branch at the end */
2207 for (br = ret; br != lastnode; )
2208 {
2209 if (OP(br) == BRANCH)
2210 {
2211 regtail(br, lastbranch);
2212 br = OPERAND(br);
2213 }
2214 else
2215 br = regnext(br);
2216 }
2217 }
Bram Moolenaara6404a42008-08-08 11:45:39 +00002218 *flagp &= ~(HASWIDTH | SIMPLE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 break;
2220 }
2221
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002222 case 'd': /* %d123 decimal */
2223 case 'o': /* %o123 octal */
2224 case 'x': /* %xab hex 2 */
2225 case 'u': /* %uabcd hex 4 */
2226 case 'U': /* %U1234abcd hex 8 */
2227 {
2228 int i;
2229
2230 switch (c)
2231 {
2232 case 'd': i = getdecchrs(); break;
2233 case 'o': i = getoctchrs(); break;
2234 case 'x': i = gethexchrs(2); break;
2235 case 'u': i = gethexchrs(4); break;
2236 case 'U': i = gethexchrs(8); break;
2237 default: i = -1; break;
2238 }
2239
2240 if (i < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002241 EMSG2_RET_NULL(
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002242 _("E678: Invalid character after %s%%[dxouU]"),
2243 reg_magic == MAGIC_ALL);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002244#ifdef FEAT_MBYTE
2245 if (use_multibytecode(i))
2246 ret = regnode(MULTIBYTECODE);
2247 else
2248#endif
2249 ret = regnode(EXACTLY);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002250 if (i == 0)
2251 regc(0x0a);
2252 else
2253#ifdef FEAT_MBYTE
2254 regmbc(i);
2255#else
2256 regc(i);
2257#endif
2258 regc(NUL);
2259 *flagp |= HASWIDTH;
2260 break;
2261 }
2262
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 default:
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00002264 if (VIM_ISDIGIT(c) || c == '<' || c == '>'
2265 || c == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 {
2267 long_u n = 0;
2268 int cmp;
2269
2270 cmp = c;
2271 if (cmp == '<' || cmp == '>')
2272 c = getchr();
2273 while (VIM_ISDIGIT(c))
2274 {
2275 n = n * 10 + (c - '0');
2276 c = getchr();
2277 }
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00002278 if (c == '\'' && n == 0)
2279 {
2280 /* "\%'m", "\%<'m" and "\%>'m": Mark */
2281 c = getchr();
2282 ret = regnode(RE_MARK);
2283 if (ret == JUST_CALC_SIZE)
2284 regsize += 2;
2285 else
2286 {
2287 *regcode++ = c;
2288 *regcode++ = cmp;
2289 }
2290 break;
2291 }
2292 else if (c == 'l' || c == 'c' || c == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 {
2294 if (c == 'l')
2295 ret = regnode(RE_LNUM);
2296 else if (c == 'c')
2297 ret = regnode(RE_COL);
2298 else
2299 ret = regnode(RE_VCOL);
2300 if (ret == JUST_CALC_SIZE)
2301 regsize += 5;
2302 else
2303 {
2304 /* put the number and the optional
2305 * comparator after the opcode */
2306 regcode = re_put_long(regcode, n);
2307 *regcode++ = cmp;
2308 }
2309 break;
2310 }
2311 }
2312
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002313 EMSG2_RET_NULL(_("E71: Invalid character after %s%%"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 reg_magic == MAGIC_ALL);
2315 }
2316 }
2317 break;
2318
2319 case Magic('['):
2320collection:
2321 {
2322 char_u *lp;
2323
2324 /*
2325 * If there is no matching ']', we assume the '[' is a normal
2326 * character. This makes 'incsearch' and ":help [" work.
2327 */
2328 lp = skip_anyof(regparse);
2329 if (*lp == ']') /* there is a matching ']' */
2330 {
2331 int startc = -1; /* > 0 when next '-' is a range */
2332 int endc;
2333
2334 /*
2335 * In a character class, different parsing rules apply.
2336 * Not even \ is special anymore, nothing is.
2337 */
2338 if (*regparse == '^') /* Complement of range. */
2339 {
2340 ret = regnode(ANYBUT + extra);
2341 regparse++;
2342 }
2343 else
2344 ret = regnode(ANYOF + extra);
2345
2346 /* At the start ']' and '-' mean the literal character. */
2347 if (*regparse == ']' || *regparse == '-')
Bram Moolenaardf177f62005-02-22 08:39:57 +00002348 {
2349 startc = *regparse;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 regc(*regparse++);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352
2353 while (*regparse != NUL && *regparse != ']')
2354 {
2355 if (*regparse == '-')
2356 {
2357 ++regparse;
2358 /* The '-' is not used for a range at the end and
2359 * after or before a '\n'. */
2360 if (*regparse == ']' || *regparse == NUL
2361 || startc == -1
2362 || (regparse[0] == '\\' && regparse[1] == 'n'))
2363 {
2364 regc('-');
2365 startc = '-'; /* [--x] is a range */
2366 }
2367 else
2368 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00002369 /* Also accept "a-[.z.]" */
2370 endc = 0;
2371 if (*regparse == '[')
2372 endc = get_coll_element(&regparse);
2373 if (endc == 0)
2374 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375#ifdef FEAT_MBYTE
Bram Moolenaardf177f62005-02-22 08:39:57 +00002376 if (has_mbyte)
2377 endc = mb_ptr2char_adv(&regparse);
2378 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00002380 endc = *regparse++;
2381 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002382
2383 /* Handle \o40, \x20 and \u20AC style sequences */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002384 if (endc == '\\' && !cpo_lit && !cpo_bsl)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002385 endc = coll_get_char();
2386
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 if (startc > endc)
2388 EMSG_RET_NULL(_(e_invrange));
2389#ifdef FEAT_MBYTE
2390 if (has_mbyte && ((*mb_char2len)(startc) > 1
2391 || (*mb_char2len)(endc) > 1))
2392 {
2393 /* Limit to a range of 256 chars */
2394 if (endc > startc + 256)
2395 EMSG_RET_NULL(_(e_invrange));
2396 while (++startc <= endc)
2397 regmbc(startc);
2398 }
2399 else
2400#endif
2401 {
2402#ifdef EBCDIC
2403 int alpha_only = FALSE;
2404
2405 /* for alphabetical range skip the gaps
2406 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
2407 if (isalpha(startc) && isalpha(endc))
2408 alpha_only = TRUE;
2409#endif
2410 while (++startc <= endc)
2411#ifdef EBCDIC
2412 if (!alpha_only || isalpha(startc))
2413#endif
2414 regc(startc);
2415 }
2416 startc = -1;
2417 }
2418 }
2419 /*
2420 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
2421 * accepts "\t", "\e", etc., but only when the 'l' flag in
2422 * 'cpoptions' is not included.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002423 * Posix doesn't recognize backslash at all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 */
2425 else if (*regparse == '\\'
Bram Moolenaardf177f62005-02-22 08:39:57 +00002426 && !cpo_bsl
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
2428 || (!cpo_lit
2429 && vim_strchr(REGEXP_ABBR,
2430 regparse[1]) != NULL)))
2431 {
2432 regparse++;
2433 if (*regparse == 'n')
2434 {
2435 /* '\n' in range: also match NL */
2436 if (ret != JUST_CALC_SIZE)
2437 {
Bram Moolenaare337e5f2013-01-30 18:21:51 +01002438 /* Using \n inside [^] does not change what
2439 * matches. "[^\n]" is the same as ".". */
2440 if (*ret == ANYOF)
2441 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 *ret = ANYOF + ADD_NL;
Bram Moolenaare337e5f2013-01-30 18:21:51 +01002443 *flagp |= HASNL;
2444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 /* else: must have had a \n already */
2446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 regparse++;
2448 startc = -1;
2449 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002450 else if (*regparse == 'd'
2451 || *regparse == 'o'
2452 || *regparse == 'x'
2453 || *regparse == 'u'
2454 || *regparse == 'U')
2455 {
2456 startc = coll_get_char();
2457 if (startc == 0)
2458 regc(0x0a);
2459 else
2460#ifdef FEAT_MBYTE
2461 regmbc(startc);
2462#else
2463 regc(startc);
2464#endif
2465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 else
2467 {
2468 startc = backslash_trans(*regparse++);
2469 regc(startc);
2470 }
2471 }
2472 else if (*regparse == '[')
2473 {
2474 int c_class;
2475 int cu;
2476
Bram Moolenaardf177f62005-02-22 08:39:57 +00002477 c_class = get_char_class(&regparse);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 startc = -1;
2479 /* Characters assumed to be 8 bits! */
2480 switch (c_class)
2481 {
2482 case CLASS_NONE:
Bram Moolenaardf177f62005-02-22 08:39:57 +00002483 c_class = get_equi_class(&regparse);
2484 if (c_class != 0)
2485 {
2486 /* produce equivalence class */
2487 reg_equi_class(c_class);
2488 }
2489 else if ((c_class =
2490 get_coll_element(&regparse)) != 0)
2491 {
2492 /* produce a collating element */
2493 regmbc(c_class);
2494 }
2495 else
2496 {
2497 /* literal '[', allow [[-x] as a range */
2498 startc = *regparse++;
2499 regc(startc);
2500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 break;
2502 case CLASS_ALNUM:
2503 for (cu = 1; cu <= 255; cu++)
2504 if (isalnum(cu))
2505 regc(cu);
2506 break;
2507 case CLASS_ALPHA:
2508 for (cu = 1; cu <= 255; cu++)
2509 if (isalpha(cu))
2510 regc(cu);
2511 break;
2512 case CLASS_BLANK:
2513 regc(' ');
2514 regc('\t');
2515 break;
2516 case CLASS_CNTRL:
2517 for (cu = 1; cu <= 255; cu++)
2518 if (iscntrl(cu))
2519 regc(cu);
2520 break;
2521 case CLASS_DIGIT:
2522 for (cu = 1; cu <= 255; cu++)
2523 if (VIM_ISDIGIT(cu))
2524 regc(cu);
2525 break;
2526 case CLASS_GRAPH:
2527 for (cu = 1; cu <= 255; cu++)
2528 if (isgraph(cu))
2529 regc(cu);
2530 break;
2531 case CLASS_LOWER:
2532 for (cu = 1; cu <= 255; cu++)
Bram Moolenaara245a5b2007-08-11 11:58:23 +00002533 if (MB_ISLOWER(cu))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 regc(cu);
2535 break;
2536 case CLASS_PRINT:
2537 for (cu = 1; cu <= 255; cu++)
2538 if (vim_isprintc(cu))
2539 regc(cu);
2540 break;
2541 case CLASS_PUNCT:
2542 for (cu = 1; cu <= 255; cu++)
2543 if (ispunct(cu))
2544 regc(cu);
2545 break;
2546 case CLASS_SPACE:
2547 for (cu = 9; cu <= 13; cu++)
2548 regc(cu);
2549 regc(' ');
2550 break;
2551 case CLASS_UPPER:
2552 for (cu = 1; cu <= 255; cu++)
Bram Moolenaara245a5b2007-08-11 11:58:23 +00002553 if (MB_ISUPPER(cu))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 regc(cu);
2555 break;
2556 case CLASS_XDIGIT:
2557 for (cu = 1; cu <= 255; cu++)
2558 if (vim_isxdigit(cu))
2559 regc(cu);
2560 break;
2561 case CLASS_TAB:
2562 regc('\t');
2563 break;
2564 case CLASS_RETURN:
2565 regc('\r');
2566 break;
2567 case CLASS_BACKSPACE:
2568 regc('\b');
2569 break;
2570 case CLASS_ESCAPE:
2571 regc('\033');
2572 break;
2573 }
2574 }
2575 else
2576 {
2577#ifdef FEAT_MBYTE
2578 if (has_mbyte)
2579 {
2580 int len;
2581
2582 /* produce a multibyte character, including any
2583 * following composing characters */
2584 startc = mb_ptr2char(regparse);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002585 len = (*mb_ptr2len)(regparse);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 if (enc_utf8 && utf_char2len(startc) != len)
2587 startc = -1; /* composing chars */
2588 while (--len >= 0)
2589 regc(*regparse++);
2590 }
2591 else
2592#endif
2593 {
2594 startc = *regparse++;
2595 regc(startc);
2596 }
2597 }
2598 }
2599 regc(NUL);
2600 prevchr_len = 1; /* last char was the ']' */
2601 if (*regparse != ']')
2602 EMSG_RET_NULL(_(e_toomsbra)); /* Cannot happen? */
2603 skipchr(); /* let's be friends with the lexer again */
2604 *flagp |= HASWIDTH | SIMPLE;
2605 break;
2606 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002607 else if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002608 EMSG2_RET_NULL(_(e_missingbracket), reg_magic > MAGIC_OFF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 }
2610 /* FALLTHROUGH */
2611
2612 default:
2613 {
2614 int len;
2615
2616#ifdef FEAT_MBYTE
2617 /* A multi-byte character is handled as a separate atom if it's
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002618 * before a multi and when it's a composing char. */
2619 if (use_multibytecode(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002621do_multibyte:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 ret = regnode(MULTIBYTECODE);
2623 regmbc(c);
2624 *flagp |= HASWIDTH | SIMPLE;
2625 break;
2626 }
2627#endif
2628
2629 ret = regnode(EXACTLY);
2630
2631 /*
2632 * Append characters as long as:
2633 * - there is no following multi, we then need the character in
2634 * front of it as a single character operand
2635 * - not running into a Magic character
2636 * - "one_exactly" is not set
2637 * But always emit at least one character. Might be a Multi,
2638 * e.g., a "[" without matching "]".
2639 */
2640 for (len = 0; c != NUL && (len == 0
2641 || (re_multi_type(peekchr()) == NOT_MULTI
2642 && !one_exactly
2643 && !is_Magic(c))); ++len)
2644 {
2645 c = no_Magic(c);
2646#ifdef FEAT_MBYTE
2647 if (has_mbyte)
2648 {
2649 regmbc(c);
2650 if (enc_utf8)
2651 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 int l;
2653
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002654 /* Need to get composing character too. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 for (;;)
2656 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002657 l = utf_ptr2len(regparse);
2658 if (!UTF_COMPOSINGLIKE(regparse, regparse + l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 break;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002660 regmbc(utf_ptr2char(regparse));
2661 skipchr();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 }
2664 }
2665 else
2666#endif
2667 regc(c);
2668 c = getchr();
2669 }
2670 ungetchr();
2671
2672 regc(NUL);
2673 *flagp |= HASWIDTH;
2674 if (len == 1)
2675 *flagp |= SIMPLE;
2676 }
2677 break;
2678 }
2679
2680 return ret;
2681}
2682
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002683#ifdef FEAT_MBYTE
2684/*
2685 * Return TRUE if MULTIBYTECODE should be used instead of EXACTLY for
2686 * character "c".
2687 */
2688 static int
2689use_multibytecode(c)
2690 int c;
2691{
2692 return has_mbyte && (*mb_char2len)(c) > 1
2693 && (re_multi_type(peekchr()) != NOT_MULTI
2694 || (enc_utf8 && utf_iscomposing(c)));
2695}
2696#endif
2697
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002699 * Emit a node.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 * Return pointer to generated code.
2701 */
2702 static char_u *
2703regnode(op)
2704 int op;
2705{
2706 char_u *ret;
2707
2708 ret = regcode;
2709 if (ret == JUST_CALC_SIZE)
2710 regsize += 3;
2711 else
2712 {
2713 *regcode++ = op;
2714 *regcode++ = NUL; /* Null "next" pointer. */
2715 *regcode++ = NUL;
2716 }
2717 return ret;
2718}
2719
2720/*
2721 * Emit (if appropriate) a byte of code
2722 */
2723 static void
2724regc(b)
2725 int b;
2726{
2727 if (regcode == JUST_CALC_SIZE)
2728 regsize++;
2729 else
2730 *regcode++ = b;
2731}
2732
2733#ifdef FEAT_MBYTE
2734/*
2735 * Emit (if appropriate) a multi-byte character of code
2736 */
2737 static void
2738regmbc(c)
2739 int c;
2740{
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02002741 if (!has_mbyte && c > 0xff)
2742 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 if (regcode == JUST_CALC_SIZE)
2744 regsize += (*mb_char2len)(c);
2745 else
2746 regcode += (*mb_char2bytes)(c, regcode);
2747}
2748#endif
2749
2750/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002751 * Insert an operator in front of already-emitted operand
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 *
2753 * Means relocating the operand.
2754 */
2755 static void
2756reginsert(op, opnd)
2757 int op;
2758 char_u *opnd;
2759{
2760 char_u *src;
2761 char_u *dst;
2762 char_u *place;
2763
2764 if (regcode == JUST_CALC_SIZE)
2765 {
2766 regsize += 3;
2767 return;
2768 }
2769 src = regcode;
2770 regcode += 3;
2771 dst = regcode;
2772 while (src > opnd)
2773 *--dst = *--src;
2774
2775 place = opnd; /* Op node, where operand used to be. */
2776 *place++ = op;
2777 *place++ = NUL;
2778 *place = NUL;
2779}
2780
2781/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002782 * Insert an operator in front of already-emitted operand.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 * The operator has the given limit values as operands. Also set next pointer.
2784 *
2785 * Means relocating the operand.
2786 */
2787 static void
2788reginsert_limits(op, minval, maxval, opnd)
2789 int op;
2790 long minval;
2791 long maxval;
2792 char_u *opnd;
2793{
2794 char_u *src;
2795 char_u *dst;
2796 char_u *place;
2797
2798 if (regcode == JUST_CALC_SIZE)
2799 {
2800 regsize += 11;
2801 return;
2802 }
2803 src = regcode;
2804 regcode += 11;
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 place = re_put_long(place, (long_u)minval);
2814 place = re_put_long(place, (long_u)maxval);
2815 regtail(opnd, place);
2816}
2817
2818/*
2819 * Write a long as four bytes at "p" and return pointer to the next char.
2820 */
2821 static char_u *
2822re_put_long(p, val)
2823 char_u *p;
2824 long_u val;
2825{
2826 *p++ = (char_u) ((val >> 24) & 0377);
2827 *p++ = (char_u) ((val >> 16) & 0377);
2828 *p++ = (char_u) ((val >> 8) & 0377);
2829 *p++ = (char_u) (val & 0377);
2830 return p;
2831}
2832
2833/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002834 * Set the next-pointer at the end of a node chain.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 */
2836 static void
2837regtail(p, val)
2838 char_u *p;
2839 char_u *val;
2840{
2841 char_u *scan;
2842 char_u *temp;
2843 int offset;
2844
2845 if (p == JUST_CALC_SIZE)
2846 return;
2847
2848 /* Find last node. */
2849 scan = p;
2850 for (;;)
2851 {
2852 temp = regnext(scan);
2853 if (temp == NULL)
2854 break;
2855 scan = temp;
2856 }
2857
Bram Moolenaar582fd852005-03-28 20:58:01 +00002858 if (OP(scan) == BACK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 offset = (int)(scan - val);
2860 else
2861 offset = (int)(val - scan);
Bram Moolenaard3005802009-11-25 17:21:32 +00002862 /* When the offset uses more than 16 bits it can no longer fit in the two
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02002863 * bytes available. Use a global flag to avoid having to check return
Bram Moolenaard3005802009-11-25 17:21:32 +00002864 * values in too many places. */
2865 if (offset > 0xffff)
2866 reg_toolong = TRUE;
2867 else
2868 {
2869 *(scan + 1) = (char_u) (((unsigned)offset >> 8) & 0377);
2870 *(scan + 2) = (char_u) (offset & 0377);
2871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872}
2873
2874/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002875 * Like regtail, on item after a BRANCH; nop if none.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 */
2877 static void
2878regoptail(p, val)
2879 char_u *p;
2880 char_u *val;
2881{
2882 /* When op is neither BRANCH nor BRACE_COMPLEX0-9, it is "operandless" */
2883 if (p == NULL || p == JUST_CALC_SIZE
2884 || (OP(p) != BRANCH
2885 && (OP(p) < BRACE_COMPLEX || OP(p) > BRACE_COMPLEX + 9)))
2886 return;
2887 regtail(OPERAND(p), val);
2888}
2889
2890/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002891 * Functions for getting characters from the regexp input.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 */
2893
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894static int at_start; /* True when on the first character */
2895static int prev_at_start; /* True when on the second character */
2896
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002897/*
2898 * Start parsing at "str".
2899 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 static void
2901initchr(str)
2902 char_u *str;
2903{
2904 regparse = str;
2905 prevchr_len = 0;
2906 curchr = prevprevchr = prevchr = nextchr = -1;
2907 at_start = TRUE;
2908 prev_at_start = FALSE;
2909}
2910
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002911/*
2912 * Get the next character without advancing.
2913 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 static int
2915peekchr()
2916{
Bram Moolenaardf177f62005-02-22 08:39:57 +00002917 static int after_slash = FALSE;
2918
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 if (curchr == -1)
2920 {
2921 switch (curchr = regparse[0])
2922 {
2923 case '.':
2924 case '[':
2925 case '~':
2926 /* magic when 'magic' is on */
2927 if (reg_magic >= MAGIC_ON)
2928 curchr = Magic(curchr);
2929 break;
2930 case '(':
2931 case ')':
2932 case '{':
2933 case '%':
2934 case '+':
2935 case '=':
2936 case '?':
2937 case '@':
2938 case '!':
2939 case '&':
2940 case '|':
2941 case '<':
2942 case '>':
2943 case '#': /* future ext. */
2944 case '"': /* future ext. */
2945 case '\'': /* future ext. */
2946 case ',': /* future ext. */
2947 case '-': /* future ext. */
2948 case ':': /* future ext. */
2949 case ';': /* future ext. */
2950 case '`': /* future ext. */
2951 case '/': /* Can't be used in / command */
2952 /* magic only after "\v" */
2953 if (reg_magic == MAGIC_ALL)
2954 curchr = Magic(curchr);
2955 break;
2956 case '*':
Bram Moolenaardf177f62005-02-22 08:39:57 +00002957 /* * is not magic as the very first character, eg "?*ptr", when
2958 * after '^', eg "/^*ptr" and when after "\(", "\|", "\&". But
2959 * "\(\*" is not magic, thus must be magic if "after_slash" */
2960 if (reg_magic >= MAGIC_ON
2961 && !at_start
2962 && !(prev_at_start && prevchr == Magic('^'))
2963 && (after_slash
2964 || (prevchr != Magic('(')
2965 && prevchr != Magic('&')
2966 && prevchr != Magic('|'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 curchr = Magic('*');
2968 break;
2969 case '^':
2970 /* '^' is only magic as the very first character and if it's after
2971 * "\(", "\|", "\&' or "\n" */
2972 if (reg_magic >= MAGIC_OFF
2973 && (at_start
2974 || reg_magic == MAGIC_ALL
2975 || prevchr == Magic('(')
2976 || prevchr == Magic('|')
2977 || prevchr == Magic('&')
2978 || prevchr == Magic('n')
2979 || (no_Magic(prevchr) == '('
2980 && prevprevchr == Magic('%'))))
2981 {
2982 curchr = Magic('^');
2983 at_start = TRUE;
2984 prev_at_start = FALSE;
2985 }
2986 break;
2987 case '$':
2988 /* '$' is only magic as the very last char and if it's in front of
2989 * either "\|", "\)", "\&", or "\n" */
2990 if (reg_magic >= MAGIC_OFF)
2991 {
2992 char_u *p = regparse + 1;
2993
2994 /* ignore \c \C \m and \M after '$' */
2995 while (p[0] == '\\' && (p[1] == 'c' || p[1] == 'C'
2996 || p[1] == 'm' || p[1] == 'M' || p[1] == 'Z'))
2997 p += 2;
2998 if (p[0] == NUL
2999 || (p[0] == '\\'
3000 && (p[1] == '|' || p[1] == '&' || p[1] == ')'
3001 || p[1] == 'n'))
3002 || reg_magic == MAGIC_ALL)
3003 curchr = Magic('$');
3004 }
3005 break;
3006 case '\\':
3007 {
3008 int c = regparse[1];
3009
3010 if (c == NUL)
3011 curchr = '\\'; /* trailing '\' */
3012 else if (
3013#ifdef EBCDIC
3014 vim_strchr(META, c)
3015#else
3016 c <= '~' && META_flags[c]
3017#endif
3018 )
3019 {
3020 /*
3021 * META contains everything that may be magic sometimes,
3022 * except ^ and $ ("\^" and "\$" are only magic after
3023 * "\v"). We now fetch the next character and toggle its
3024 * magicness. Therefore, \ is so meta-magic that it is
3025 * not in META.
3026 */
3027 curchr = -1;
3028 prev_at_start = at_start;
3029 at_start = FALSE; /* be able to say "/\*ptr" */
3030 ++regparse;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003031 ++after_slash;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 peekchr();
3033 --regparse;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003034 --after_slash;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 curchr = toggle_Magic(curchr);
3036 }
3037 else if (vim_strchr(REGEXP_ABBR, c))
3038 {
3039 /*
3040 * Handle abbreviations, like "\t" for TAB -- webb
3041 */
3042 curchr = backslash_trans(c);
3043 }
3044 else if (reg_magic == MAGIC_NONE && (c == '$' || c == '^'))
3045 curchr = toggle_Magic(c);
3046 else
3047 {
3048 /*
3049 * Next character can never be (made) magic?
3050 * Then backslashing it won't do anything.
3051 */
3052#ifdef FEAT_MBYTE
3053 if (has_mbyte)
3054 curchr = (*mb_ptr2char)(regparse + 1);
3055 else
3056#endif
3057 curchr = c;
3058 }
3059 break;
3060 }
3061
3062#ifdef FEAT_MBYTE
3063 default:
3064 if (has_mbyte)
3065 curchr = (*mb_ptr2char)(regparse);
3066#endif
3067 }
3068 }
3069
3070 return curchr;
3071}
3072
3073/*
3074 * Eat one lexed character. Do this in a way that we can undo it.
3075 */
3076 static void
3077skipchr()
3078{
3079 /* peekchr() eats a backslash, do the same here */
3080 if (*regparse == '\\')
3081 prevchr_len = 1;
3082 else
3083 prevchr_len = 0;
3084 if (regparse[prevchr_len] != NUL)
3085 {
3086#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003087 if (enc_utf8)
Bram Moolenaar8f5c5782007-11-29 20:27:21 +00003088 /* exclude composing chars that mb_ptr2len does include */
3089 prevchr_len += utf_ptr2len(regparse + prevchr_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003090 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003091 prevchr_len += (*mb_ptr2len)(regparse + prevchr_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 else
3093#endif
3094 ++prevchr_len;
3095 }
3096 regparse += prevchr_len;
3097 prev_at_start = at_start;
3098 at_start = FALSE;
3099 prevprevchr = prevchr;
3100 prevchr = curchr;
3101 curchr = nextchr; /* use previously unget char, or -1 */
3102 nextchr = -1;
3103}
3104
3105/*
3106 * Skip a character while keeping the value of prev_at_start for at_start.
3107 * prevchr and prevprevchr are also kept.
3108 */
3109 static void
3110skipchr_keepstart()
3111{
3112 int as = prev_at_start;
3113 int pr = prevchr;
3114 int prpr = prevprevchr;
3115
3116 skipchr();
3117 at_start = as;
3118 prevchr = pr;
3119 prevprevchr = prpr;
3120}
3121
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003122/*
3123 * Get the next character from the pattern. We know about magic and such, so
3124 * therefore we need a lexical analyzer.
3125 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 static int
3127getchr()
3128{
3129 int chr = peekchr();
3130
3131 skipchr();
3132 return chr;
3133}
3134
3135/*
3136 * put character back. Works only once!
3137 */
3138 static void
3139ungetchr()
3140{
3141 nextchr = curchr;
3142 curchr = prevchr;
3143 prevchr = prevprevchr;
3144 at_start = prev_at_start;
3145 prev_at_start = FALSE;
3146
3147 /* Backup regparse, so that it's at the same position as before the
3148 * getchr(). */
3149 regparse -= prevchr_len;
3150}
3151
3152/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00003153 * Get and return the value of the hex string at the current position.
3154 * Return -1 if there is no valid hex number.
3155 * The position is updated:
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003156 * blahblah\%x20asdf
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003157 * before-^ ^-after
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003158 * The parameter controls the maximum number of input characters. This will be
3159 * 2 when reading a \%x20 sequence and 4 when reading a \%u20AC sequence.
3160 */
3161 static int
3162gethexchrs(maxinputlen)
3163 int maxinputlen;
3164{
3165 int nr = 0;
3166 int c;
3167 int i;
3168
3169 for (i = 0; i < maxinputlen; ++i)
3170 {
3171 c = regparse[0];
3172 if (!vim_isxdigit(c))
3173 break;
3174 nr <<= 4;
3175 nr |= hex2nr(c);
3176 ++regparse;
3177 }
3178
3179 if (i == 0)
3180 return -1;
3181 return nr;
3182}
3183
3184/*
3185 * get and return the value of the decimal string immediately after the
3186 * current position. Return -1 for invalid. Consumes all digits.
3187 */
3188 static int
3189getdecchrs()
3190{
3191 int nr = 0;
3192 int c;
3193 int i;
3194
3195 for (i = 0; ; ++i)
3196 {
3197 c = regparse[0];
3198 if (c < '0' || c > '9')
3199 break;
3200 nr *= 10;
3201 nr += c - '0';
3202 ++regparse;
3203 }
3204
3205 if (i == 0)
3206 return -1;
3207 return nr;
3208}
3209
3210/*
3211 * get and return the value of the octal string immediately after the current
3212 * position. Return -1 for invalid, or 0-255 for valid. Smart enough to handle
3213 * numbers > 377 correctly (for example, 400 is treated as 40) and doesn't
3214 * treat 8 or 9 as recognised characters. Position is updated:
3215 * blahblah\%o210asdf
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003216 * before-^ ^-after
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003217 */
3218 static int
3219getoctchrs()
3220{
3221 int nr = 0;
3222 int c;
3223 int i;
3224
3225 for (i = 0; i < 3 && nr < 040; ++i)
3226 {
3227 c = regparse[0];
3228 if (c < '0' || c > '7')
3229 break;
3230 nr <<= 3;
3231 nr |= hex2nr(c);
3232 ++regparse;
3233 }
3234
3235 if (i == 0)
3236 return -1;
3237 return nr;
3238}
3239
3240/*
3241 * Get a number after a backslash that is inside [].
3242 * When nothing is recognized return a backslash.
3243 */
3244 static int
3245coll_get_char()
3246{
3247 int nr = -1;
3248
3249 switch (*regparse++)
3250 {
3251 case 'd': nr = getdecchrs(); break;
3252 case 'o': nr = getoctchrs(); break;
3253 case 'x': nr = gethexchrs(2); break;
3254 case 'u': nr = gethexchrs(4); break;
3255 case 'U': nr = gethexchrs(8); break;
3256 }
3257 if (nr < 0)
3258 {
3259 /* If getting the number fails be backwards compatible: the character
3260 * is a backslash. */
3261 --regparse;
3262 nr = '\\';
3263 }
3264 return nr;
3265}
3266
3267/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 * read_limits - Read two integers to be taken as a minimum and maximum.
3269 * If the first character is '-', then the range is reversed.
3270 * Should end with 'end'. If minval is missing, zero is default, if maxval is
3271 * missing, a very big number is the default.
3272 */
3273 static int
3274read_limits(minval, maxval)
3275 long *minval;
3276 long *maxval;
3277{
3278 int reverse = FALSE;
3279 char_u *first_char;
3280 long tmp;
3281
3282 if (*regparse == '-')
3283 {
3284 /* Starts with '-', so reverse the range later */
3285 regparse++;
3286 reverse = TRUE;
3287 }
3288 first_char = regparse;
3289 *minval = getdigits(&regparse);
3290 if (*regparse == ',') /* There is a comma */
3291 {
3292 if (vim_isdigit(*++regparse))
3293 *maxval = getdigits(&regparse);
3294 else
3295 *maxval = MAX_LIMIT;
3296 }
3297 else if (VIM_ISDIGIT(*first_char))
3298 *maxval = *minval; /* It was \{n} or \{-n} */
3299 else
3300 *maxval = MAX_LIMIT; /* It was \{} or \{-} */
3301 if (*regparse == '\\')
3302 regparse++; /* Allow either \{...} or \{...\} */
Bram Moolenaardf177f62005-02-22 08:39:57 +00003303 if (*regparse != '}')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 {
3305 sprintf((char *)IObuff, _("E554: Syntax error in %s{...}"),
3306 reg_magic == MAGIC_ALL ? "" : "\\");
3307 EMSG_RET_FAIL(IObuff);
3308 }
3309
3310 /*
3311 * Reverse the range if there was a '-', or make sure it is in the right
3312 * order otherwise.
3313 */
3314 if ((!reverse && *minval > *maxval) || (reverse && *minval < *maxval))
3315 {
3316 tmp = *minval;
3317 *minval = *maxval;
3318 *maxval = tmp;
3319 }
3320 skipchr(); /* let's be friends with the lexer again */
3321 return OK;
3322}
3323
3324/*
3325 * vim_regexec and friends
3326 */
3327
3328/*
3329 * Global work variables for vim_regexec().
3330 */
3331
3332/* The current match-position is remembered with these variables: */
3333static linenr_T reglnum; /* line number, relative to first line */
3334static char_u *regline; /* start of current line */
3335static char_u *reginput; /* current input, points into "regline" */
3336
3337static int need_clear_subexpr; /* subexpressions still need to be
3338 * cleared */
3339#ifdef FEAT_SYN_HL
3340static int need_clear_zsubexpr = FALSE; /* extmatch subexpressions
3341 * still need to be cleared */
3342#endif
3343
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344/*
3345 * Structure used to save the current input state, when it needs to be
3346 * restored after trying a match. Used by reg_save() and reg_restore().
Bram Moolenaar582fd852005-03-28 20:58:01 +00003347 * Also stores the length of "backpos".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 */
3349typedef struct
3350{
3351 union
3352 {
3353 char_u *ptr; /* reginput pointer, for single-line regexp */
3354 lpos_T pos; /* reginput pos, for multi-line regexp */
3355 } rs_u;
Bram Moolenaar582fd852005-03-28 20:58:01 +00003356 int rs_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357} regsave_T;
3358
3359/* struct to save start/end pointer/position in for \(\) */
3360typedef struct
3361{
3362 union
3363 {
3364 char_u *ptr;
3365 lpos_T pos;
3366 } se_u;
3367} save_se_T;
3368
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00003369/* used for BEHIND and NOBEHIND matching */
3370typedef struct regbehind_S
3371{
3372 regsave_T save_after;
3373 regsave_T save_behind;
Bram Moolenaarfde483c2008-06-15 12:21:50 +00003374 int save_need_clear_subexpr;
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00003375 save_se_T save_start[NSUBEXP];
3376 save_se_T save_end[NSUBEXP];
3377} regbehind_T;
3378
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379static char_u *reg_getline __ARGS((linenr_T lnum));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003380static long bt_regexec_both __ARGS((char_u *line, colnr_T col, proftime_T *tm));
3381static long regtry __ARGS((bt_regprog_T *prog, colnr_T col));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382static void cleanup_subexpr __ARGS((void));
3383#ifdef FEAT_SYN_HL
3384static void cleanup_zsubexpr __ARGS((void));
3385#endif
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00003386static void save_subexpr __ARGS((regbehind_T *bp));
3387static void restore_subexpr __ARGS((regbehind_T *bp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388static void reg_nextline __ARGS((void));
Bram Moolenaar582fd852005-03-28 20:58:01 +00003389static void reg_save __ARGS((regsave_T *save, garray_T *gap));
3390static void reg_restore __ARGS((regsave_T *save, garray_T *gap));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391static int reg_save_equal __ARGS((regsave_T *save));
3392static void save_se_multi __ARGS((save_se_T *savep, lpos_T *posp));
3393static void save_se_one __ARGS((save_se_T *savep, char_u **pp));
3394
3395/* Save the sub-expressions before attempting a match. */
3396#define save_se(savep, posp, pp) \
3397 REG_MULTI ? save_se_multi((savep), (posp)) : save_se_one((savep), (pp))
3398
3399/* After a failed match restore the sub-expressions. */
3400#define restore_se(savep, posp, pp) { \
3401 if (REG_MULTI) \
3402 *(posp) = (savep)->se_u.pos; \
3403 else \
3404 *(pp) = (savep)->se_u.ptr; }
3405
3406static int re_num_cmp __ARGS((long_u val, char_u *scan));
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003407static int regmatch __ARGS((char_u *prog));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408static int regrepeat __ARGS((char_u *p, long maxcount));
3409
3410#ifdef DEBUG
3411int regnarrate = 0;
3412#endif
3413
3414/*
3415 * Internal copy of 'ignorecase'. It is set at each call to vim_regexec().
3416 * Normally it gets the value of "rm_ic" or "rmm_ic", but when the pattern
3417 * contains '\c' or '\C' the value is overruled.
3418 */
3419static int ireg_ic;
3420
3421#ifdef FEAT_MBYTE
3422/*
3423 * Similar to ireg_ic, but only for 'combining' characters. Set with \Z flag
3424 * in the regexp. Defaults to false, always.
3425 */
3426static int ireg_icombine;
3427#endif
3428
3429/*
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003430 * Copy of "rmm_maxcol": maximum column to search for a match. Zero when
3431 * there is no maximum.
3432 */
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003433static colnr_T ireg_maxcol;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003434
3435/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 * Sometimes need to save a copy of a line. Since alloc()/free() is very
3437 * slow, we keep one allocated piece of memory and only re-allocate it when
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003438 * it's too small. It's freed in bt_regexec_both() when finished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 */
Bram Moolenaard4210772008-01-02 14:35:30 +00003440static char_u *reg_tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441static unsigned reg_tofreelen;
3442
3443/*
3444 * These variables are set when executing a regexp to speed up the execution.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00003445 * Which ones are set depends on whether a single-line or multi-line match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 * done:
3447 * single-line multi-line
3448 * reg_match &regmatch_T NULL
3449 * reg_mmatch NULL &regmmatch_T
3450 * reg_startp reg_match->startp <invalid>
3451 * reg_endp reg_match->endp <invalid>
3452 * reg_startpos <invalid> reg_mmatch->startpos
3453 * reg_endpos <invalid> reg_mmatch->endpos
3454 * reg_win NULL window in which to search
Bram Moolenaar2f315ab2013-01-25 20:11:01 +01003455 * reg_buf curbuf buffer in which to search
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 * reg_firstlnum <invalid> first line in which to search
3457 * reg_maxline 0 last line nr
3458 * reg_line_lbr FALSE or TRUE FALSE
3459 */
3460static regmatch_T *reg_match;
3461static regmmatch_T *reg_mmatch;
3462static char_u **reg_startp = NULL;
3463static char_u **reg_endp = NULL;
3464static lpos_T *reg_startpos = NULL;
3465static lpos_T *reg_endpos = NULL;
3466static win_T *reg_win;
3467static buf_T *reg_buf;
3468static linenr_T reg_firstlnum;
3469static linenr_T reg_maxline;
3470static int reg_line_lbr; /* "\n" in string is line break */
3471
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003472/* Values for rs_state in regitem_T. */
3473typedef enum regstate_E
3474{
3475 RS_NOPEN = 0 /* NOPEN and NCLOSE */
3476 , RS_MOPEN /* MOPEN + [0-9] */
3477 , RS_MCLOSE /* MCLOSE + [0-9] */
3478#ifdef FEAT_SYN_HL
3479 , RS_ZOPEN /* ZOPEN + [0-9] */
3480 , RS_ZCLOSE /* ZCLOSE + [0-9] */
3481#endif
3482 , RS_BRANCH /* BRANCH */
3483 , RS_BRCPLX_MORE /* BRACE_COMPLEX and trying one more match */
3484 , RS_BRCPLX_LONG /* BRACE_COMPLEX and trying longest match */
3485 , RS_BRCPLX_SHORT /* BRACE_COMPLEX and trying shortest match */
3486 , RS_NOMATCH /* NOMATCH */
3487 , RS_BEHIND1 /* BEHIND / NOBEHIND matching rest */
3488 , RS_BEHIND2 /* BEHIND / NOBEHIND matching behind part */
3489 , RS_STAR_LONG /* STAR/PLUS/BRACE_SIMPLE longest match */
3490 , RS_STAR_SHORT /* STAR/PLUS/BRACE_SIMPLE shortest match */
3491} regstate_T;
3492
3493/*
3494 * When there are alternatives a regstate_T is put on the regstack to remember
3495 * what we are doing.
3496 * Before it may be another type of item, depending on rs_state, to remember
3497 * more things.
3498 */
3499typedef struct regitem_S
3500{
3501 regstate_T rs_state; /* what we are doing, one of RS_ above */
3502 char_u *rs_scan; /* current node in program */
3503 union
3504 {
3505 save_se_T sesave;
3506 regsave_T regsave;
3507 } rs_un; /* room for saving reginput */
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00003508 short rs_no; /* submatch nr or BEHIND/NOBEHIND */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003509} regitem_T;
3510
3511static regitem_T *regstack_push __ARGS((regstate_T state, char_u *scan));
3512static void regstack_pop __ARGS((char_u **scan));
3513
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003514/* used for STAR, PLUS and BRACE_SIMPLE matching */
3515typedef struct regstar_S
3516{
3517 int nextb; /* next byte */
3518 int nextb_ic; /* next byte reverse case */
3519 long count;
3520 long minval;
3521 long maxval;
3522} regstar_T;
3523
3524/* used to store input position when a BACK was encountered, so that we now if
3525 * we made any progress since the last time. */
3526typedef struct backpos_S
3527{
3528 char_u *bp_scan; /* "scan" where BACK was encountered */
3529 regsave_T bp_pos; /* last input position */
3530} backpos_T;
3531
3532/*
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003533 * "regstack" and "backpos" are used by regmatch(). They are kept over calls
3534 * to avoid invoking malloc() and free() often.
3535 * "regstack" is a stack with regitem_T items, sometimes preceded by regstar_T
3536 * or regbehind_T.
3537 * "backpos_T" is a table with backpos_T for BACK
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003538 */
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003539static garray_T regstack = {0, 0, 0, 0, NULL};
3540static garray_T backpos = {0, 0, 0, 0, NULL};
3541
3542/*
3543 * Both for regstack and backpos tables we use the following strategy of
3544 * allocation (to reduce malloc/free calls):
3545 * - Initial size is fairly small.
3546 * - When needed, the tables are grown bigger (8 times at first, double after
3547 * that).
3548 * - After executing the match we free the memory only if the array has grown.
3549 * Thus the memory is kept allocated when it's at the initial size.
3550 * This makes it fast while not keeping a lot of memory allocated.
3551 * A three times speed increase was observed when using many simple patterns.
3552 */
3553#define REGSTACK_INITIAL 2048
3554#define BACKPOS_INITIAL 64
3555
3556#if defined(EXITFREE) || defined(PROTO)
3557 void
3558free_regexp_stuff()
3559{
3560 ga_clear(&regstack);
3561 ga_clear(&backpos);
3562 vim_free(reg_tofree);
3563 vim_free(reg_prev_sub);
3564}
3565#endif
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003566
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567/*
3568 * Get pointer to the line "lnum", which is relative to "reg_firstlnum".
3569 */
3570 static char_u *
3571reg_getline(lnum)
3572 linenr_T lnum;
3573{
3574 /* when looking behind for a match/no-match lnum is negative. But we
3575 * can't go before line 1 */
3576 if (reg_firstlnum + lnum < 1)
3577 return NULL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00003578 if (lnum > reg_maxline)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003579 /* Must have matched the "\n" in the last line. */
3580 return (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 return ml_get_buf(reg_buf, reg_firstlnum + lnum, FALSE);
3582}
3583
3584static regsave_T behind_pos;
3585
3586#ifdef FEAT_SYN_HL
3587static char_u *reg_startzp[NSUBEXP]; /* Workspace to mark beginning */
3588static char_u *reg_endzp[NSUBEXP]; /* and end of \z(...\) matches */
3589static lpos_T reg_startzpos[NSUBEXP]; /* idem, beginning pos */
3590static lpos_T reg_endzpos[NSUBEXP]; /* idem, end pos */
3591#endif
3592
3593/* TRUE if using multi-line regexp. */
3594#define REG_MULTI (reg_match == NULL)
3595
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003596static int bt_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
3597
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598/*
3599 * Match a regexp against a string.
3600 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
3601 * Uses curbuf for line count and 'iskeyword'.
3602 *
3603 * Return TRUE if there is a match, FALSE if not.
3604 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003605 static int
3606bt_regexec(rmp, line, col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 regmatch_T *rmp;
3608 char_u *line; /* string to match against */
3609 colnr_T col; /* column to start looking for match */
3610{
3611 reg_match = rmp;
3612 reg_mmatch = NULL;
3613 reg_maxline = 0;
3614 reg_line_lbr = FALSE;
Bram Moolenaar2f315ab2013-01-25 20:11:01 +01003615 reg_buf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 reg_win = NULL;
3617 ireg_ic = rmp->rm_ic;
3618#ifdef FEAT_MBYTE
3619 ireg_icombine = FALSE;
3620#endif
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003621 ireg_maxcol = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003622 return (bt_regexec_both(line, col, NULL) != 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623}
3624
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003625#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
3626 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003627
3628static int bt_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
3629
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630/*
3631 * Like vim_regexec(), but consider a "\n" in "line" to be a line break.
3632 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003633 static int
3634bt_regexec_nl(rmp, line, col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 regmatch_T *rmp;
3636 char_u *line; /* string to match against */
3637 colnr_T col; /* column to start looking for match */
3638{
3639 reg_match = rmp;
3640 reg_mmatch = NULL;
3641 reg_maxline = 0;
3642 reg_line_lbr = TRUE;
Bram Moolenaar2f315ab2013-01-25 20:11:01 +01003643 reg_buf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 reg_win = NULL;
3645 ireg_ic = rmp->rm_ic;
3646#ifdef FEAT_MBYTE
3647 ireg_icombine = FALSE;
3648#endif
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003649 ireg_maxcol = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003650 return (bt_regexec_both(line, col, NULL) != 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651}
3652#endif
3653
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003654static long bt_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm));
3655
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656/*
3657 * Match a regexp against multiple lines.
3658 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
3659 * Uses curbuf for line count and 'iskeyword'.
3660 *
3661 * Return zero if there is no match. Return number of lines contained in the
3662 * match otherwise.
3663 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003664 static long
3665bt_regexec_multi(rmp, win, buf, lnum, col, tm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 regmmatch_T *rmp;
3667 win_T *win; /* window in which to search or NULL */
3668 buf_T *buf; /* buffer in which to search */
3669 linenr_T lnum; /* nr of line to start looking for match */
3670 colnr_T col; /* column to start looking for match */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003671 proftime_T *tm; /* timeout limit or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672{
3673 long r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674
3675 reg_match = NULL;
3676 reg_mmatch = rmp;
3677 reg_buf = buf;
3678 reg_win = win;
3679 reg_firstlnum = lnum;
3680 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
3681 reg_line_lbr = FALSE;
3682 ireg_ic = rmp->rmm_ic;
3683#ifdef FEAT_MBYTE
3684 ireg_icombine = FALSE;
3685#endif
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003686 ireg_maxcol = rmp->rmm_maxcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003688 r = bt_regexec_both(NULL, col, tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689
3690 return r;
3691}
3692
3693/*
3694 * Match a regexp against a string ("line" points to the string) or multiple
3695 * lines ("line" is NULL, use reg_getline()).
3696 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 static long
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003698bt_regexec_both(line, col, tm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 char_u *line;
3700 colnr_T col; /* column to start looking for match */
Bram Moolenaar78a15312009-05-15 19:33:18 +00003701 proftime_T *tm UNUSED; /* timeout limit or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003703 bt_regprog_T *prog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 char_u *s;
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003705 long retval = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003707 /* Create "regstack" and "backpos" if they are not allocated yet.
3708 * We allocate *_INITIAL amount of bytes first and then set the grow size
3709 * to much bigger value to avoid many malloc calls in case of deep regular
3710 * expressions. */
3711 if (regstack.ga_data == NULL)
3712 {
3713 /* Use an item size of 1 byte, since we push different things
3714 * onto the regstack. */
3715 ga_init2(&regstack, 1, REGSTACK_INITIAL);
3716 ga_grow(&regstack, REGSTACK_INITIAL);
3717 regstack.ga_growsize = REGSTACK_INITIAL * 8;
3718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003720 if (backpos.ga_data == NULL)
3721 {
3722 ga_init2(&backpos, sizeof(backpos_T), BACKPOS_INITIAL);
3723 ga_grow(&backpos, BACKPOS_INITIAL);
3724 backpos.ga_growsize = BACKPOS_INITIAL * 8;
3725 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003726
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 if (REG_MULTI)
3728 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003729 prog = (bt_regprog_T *)reg_mmatch->regprog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 line = reg_getline((linenr_T)0);
3731 reg_startpos = reg_mmatch->startpos;
3732 reg_endpos = reg_mmatch->endpos;
3733 }
3734 else
3735 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003736 prog = (bt_regprog_T *)reg_match->regprog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 reg_startp = reg_match->startp;
3738 reg_endp = reg_match->endp;
3739 }
3740
3741 /* Be paranoid... */
3742 if (prog == NULL || line == NULL)
3743 {
3744 EMSG(_(e_null));
3745 goto theend;
3746 }
3747
3748 /* Check validity of program. */
3749 if (prog_magic_wrong())
3750 goto theend;
3751
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003752 /* If the start column is past the maximum column: no need to try. */
3753 if (ireg_maxcol > 0 && col >= ireg_maxcol)
3754 goto theend;
3755
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
3757 if (prog->regflags & RF_ICASE)
3758 ireg_ic = TRUE;
3759 else if (prog->regflags & RF_NOICASE)
3760 ireg_ic = FALSE;
3761
3762#ifdef FEAT_MBYTE
3763 /* If pattern contains "\Z" overrule value of ireg_icombine */
3764 if (prog->regflags & RF_ICOMBINE)
3765 ireg_icombine = TRUE;
3766#endif
3767
3768 /* If there is a "must appear" string, look for it. */
3769 if (prog->regmust != NULL)
3770 {
3771 int c;
3772
3773#ifdef FEAT_MBYTE
3774 if (has_mbyte)
3775 c = (*mb_ptr2char)(prog->regmust);
3776 else
3777#endif
3778 c = *prog->regmust;
3779 s = line + col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003780
3781 /*
3782 * This is used very often, esp. for ":global". Use three versions of
3783 * the loop to avoid overhead of conditions.
3784 */
3785 if (!ireg_ic
3786#ifdef FEAT_MBYTE
3787 && !has_mbyte
3788#endif
3789 )
3790 while ((s = vim_strbyte(s, c)) != NULL)
3791 {
3792 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3793 break; /* Found it. */
3794 ++s;
3795 }
3796#ifdef FEAT_MBYTE
3797 else if (!ireg_ic || (!enc_utf8 && mb_char2len(c) > 1))
3798 while ((s = vim_strchr(s, c)) != NULL)
3799 {
3800 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3801 break; /* Found it. */
3802 mb_ptr_adv(s);
3803 }
3804#endif
3805 else
3806 while ((s = cstrchr(s, c)) != NULL)
3807 {
3808 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3809 break; /* Found it. */
3810 mb_ptr_adv(s);
3811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 if (s == NULL) /* Not present. */
3813 goto theend;
3814 }
3815
3816 regline = line;
3817 reglnum = 0;
Bram Moolenaar73a92fe2010-09-14 10:55:47 +02003818 reg_toolong = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819
3820 /* Simplest case: Anchored match need be tried only once. */
3821 if (prog->reganch)
3822 {
3823 int c;
3824
3825#ifdef FEAT_MBYTE
3826 if (has_mbyte)
3827 c = (*mb_ptr2char)(regline + col);
3828 else
3829#endif
3830 c = regline[col];
3831 if (prog->regstart == NUL
3832 || prog->regstart == c
3833 || (ireg_ic && ((
3834#ifdef FEAT_MBYTE
3835 (enc_utf8 && utf_fold(prog->regstart) == utf_fold(c)))
3836 || (c < 255 && prog->regstart < 255 &&
3837#endif
Bram Moolenaara245a5b2007-08-11 11:58:23 +00003838 MB_TOLOWER(prog->regstart) == MB_TOLOWER(c)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 retval = regtry(prog, col);
3840 else
3841 retval = 0;
3842 }
3843 else
3844 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003845#ifdef FEAT_RELTIME
3846 int tm_count = 0;
3847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 /* Messy cases: unanchored match. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003849 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 {
3851 if (prog->regstart != NUL)
3852 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003853 /* Skip until the char we know it must start with.
3854 * Used often, do some work to avoid call overhead. */
3855 if (!ireg_ic
3856#ifdef FEAT_MBYTE
3857 && !has_mbyte
3858#endif
3859 )
3860 s = vim_strbyte(regline + col, prog->regstart);
3861 else
3862 s = cstrchr(regline + col, prog->regstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 if (s == NULL)
3864 {
3865 retval = 0;
3866 break;
3867 }
3868 col = (int)(s - regline);
3869 }
3870
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003871 /* Check for maximum column to try. */
3872 if (ireg_maxcol > 0 && col >= ireg_maxcol)
3873 {
3874 retval = 0;
3875 break;
3876 }
3877
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 retval = regtry(prog, col);
3879 if (retval > 0)
3880 break;
3881
3882 /* if not currently on the first line, get it again */
3883 if (reglnum != 0)
3884 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 reglnum = 0;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003886 regline = reg_getline((linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 }
3888 if (regline[col] == NUL)
3889 break;
3890#ifdef FEAT_MBYTE
3891 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003892 col += (*mb_ptr2len)(regline + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 else
3894#endif
3895 ++col;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003896#ifdef FEAT_RELTIME
3897 /* Check for timeout once in a twenty times to avoid overhead. */
3898 if (tm != NULL && ++tm_count == 20)
3899 {
3900 tm_count = 0;
3901 if (profile_passed_limit(tm))
3902 break;
3903 }
3904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 }
3906 }
3907
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908theend:
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003909 /* Free "reg_tofree" when it's a bit big.
3910 * Free regstack and backpos if they are bigger than their initial size. */
3911 if (reg_tofreelen > 400)
3912 {
3913 vim_free(reg_tofree);
3914 reg_tofree = NULL;
3915 }
3916 if (regstack.ga_maxlen > REGSTACK_INITIAL)
3917 ga_clear(&regstack);
3918 if (backpos.ga_maxlen > BACKPOS_INITIAL)
3919 ga_clear(&backpos);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003920
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 return retval;
3922}
3923
3924#ifdef FEAT_SYN_HL
3925static reg_extmatch_T *make_extmatch __ARGS((void));
3926
3927/*
3928 * Create a new extmatch and mark it as referenced once.
3929 */
3930 static reg_extmatch_T *
3931make_extmatch()
3932{
3933 reg_extmatch_T *em;
3934
3935 em = (reg_extmatch_T *)alloc_clear((unsigned)sizeof(reg_extmatch_T));
3936 if (em != NULL)
3937 em->refcnt = 1;
3938 return em;
3939}
3940
3941/*
3942 * Add a reference to an extmatch.
3943 */
3944 reg_extmatch_T *
3945ref_extmatch(em)
3946 reg_extmatch_T *em;
3947{
3948 if (em != NULL)
3949 em->refcnt++;
3950 return em;
3951}
3952
3953/*
3954 * Remove a reference to an extmatch. If there are no references left, free
3955 * the info.
3956 */
3957 void
3958unref_extmatch(em)
3959 reg_extmatch_T *em;
3960{
3961 int i;
3962
3963 if (em != NULL && --em->refcnt <= 0)
3964 {
3965 for (i = 0; i < NSUBEXP; ++i)
3966 vim_free(em->matches[i]);
3967 vim_free(em);
3968 }
3969}
3970#endif
3971
3972/*
3973 * regtry - try match of "prog" with at regline["col"].
3974 * Returns 0 for failure, number of lines contained in the match otherwise.
3975 */
3976 static long
3977regtry(prog, col)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003978 bt_regprog_T *prog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 colnr_T col;
3980{
3981 reginput = regline + col;
3982 need_clear_subexpr = TRUE;
3983#ifdef FEAT_SYN_HL
3984 /* Clear the external match subpointers if necessary. */
3985 if (prog->reghasz == REX_SET)
3986 need_clear_zsubexpr = TRUE;
3987#endif
3988
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003989 if (regmatch(prog->program + 1) == 0)
3990 return 0;
3991
3992 cleanup_subexpr();
3993 if (REG_MULTI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003995 if (reg_startpos[0].lnum < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003997 reg_startpos[0].lnum = 0;
3998 reg_startpos[0].col = col;
3999 }
4000 if (reg_endpos[0].lnum < 0)
4001 {
4002 reg_endpos[0].lnum = reglnum;
4003 reg_endpos[0].col = (int)(reginput - regline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 }
4005 else
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004006 /* Use line number of "\ze". */
4007 reglnum = reg_endpos[0].lnum;
4008 }
4009 else
4010 {
4011 if (reg_startp[0] == NULL)
4012 reg_startp[0] = regline + col;
4013 if (reg_endp[0] == NULL)
4014 reg_endp[0] = reginput;
4015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016#ifdef FEAT_SYN_HL
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004017 /* Package any found \z(...\) matches for export. Default is none. */
4018 unref_extmatch(re_extmatch_out);
4019 re_extmatch_out = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004021 if (prog->reghasz == REX_SET)
4022 {
4023 int i;
4024
4025 cleanup_zsubexpr();
4026 re_extmatch_out = make_extmatch();
4027 for (i = 0; i < NSUBEXP; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004029 if (REG_MULTI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004031 /* Only accept single line matches. */
4032 if (reg_startzpos[i].lnum >= 0
4033 && reg_endzpos[i].lnum == reg_startzpos[i].lnum)
4034 re_extmatch_out->matches[i] =
4035 vim_strnsave(reg_getline(reg_startzpos[i].lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 + reg_startzpos[i].col,
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004037 reg_endzpos[i].col - reg_startzpos[i].col);
4038 }
4039 else
4040 {
4041 if (reg_startzp[i] != NULL && reg_endzp[i] != NULL)
4042 re_extmatch_out->matches[i] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 vim_strnsave(reg_startzp[i],
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004044 (int)(reg_endzp[i] - reg_startzp[i]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 }
4046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 }
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004048#endif
4049 return 1 + reglnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050}
4051
4052#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053static int reg_prev_class __ARGS((void));
4054
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055/*
4056 * Get class of previous character.
4057 */
4058 static int
4059reg_prev_class()
4060{
4061 if (reginput > regline)
Bram Moolenaarf813a182013-01-30 13:59:37 +01004062 return mb_get_class_buf(reginput - 1
4063 - (*mb_head_off)(regline, reginput - 1), reg_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 return -1;
4065}
4066
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004068#define ADVANCE_REGINPUT() mb_ptr_adv(reginput)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069
4070/*
4071 * The arguments from BRACE_LIMITS are stored here. They are actually local
4072 * to regmatch(), but they are here to reduce the amount of stack space used
4073 * (it can be called recursively many times).
4074 */
4075static long bl_minval;
4076static long bl_maxval;
4077
4078/*
4079 * regmatch - main matching routine
4080 *
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004081 * Conceptually the strategy is simple: Check to see whether the current node
4082 * matches, push an item onto the regstack and loop to see whether the rest
4083 * matches, and then act accordingly. In practice we make some effort to
4084 * avoid using the regstack, in particular by going through "ordinary" nodes
4085 * (that don't need to know whether the rest of the match failed) by a nested
4086 * loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 *
4088 * Returns TRUE when there is a match. Leaves reginput and reglnum just after
4089 * the last matched character.
4090 * Returns FALSE when there is no match. Leaves reginput and reglnum in an
4091 * undefined state!
4092 */
4093 static int
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004094regmatch(scan)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 char_u *scan; /* Current node. */
4096{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004097 char_u *next; /* Next node. */
4098 int op;
4099 int c;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004100 regitem_T *rp;
4101 int no;
4102 int status; /* one of the RA_ values: */
4103#define RA_FAIL 1 /* something failed, abort */
4104#define RA_CONT 2 /* continue in inner loop */
4105#define RA_BREAK 3 /* break inner loop */
4106#define RA_MATCH 4 /* successful match */
4107#define RA_NOMATCH 5 /* didn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00004109 /* Make "regstack" and "backpos" empty. They are allocated and freed in
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004110 * bt_regexec_both() to reduce malloc()/free() calls. */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004111 regstack.ga_len = 0;
4112 backpos.ga_len = 0;
Bram Moolenaar582fd852005-03-28 20:58:01 +00004113
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004114 /*
Bram Moolenaar582fd852005-03-28 20:58:01 +00004115 * Repeat until "regstack" is empty.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004116 */
4117 for (;;)
4118 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004119 /* Some patterns may cause a long time to match, even though they are not
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 * illegal. E.g., "\([a-z]\+\)\+Q". Allow breaking them with CTRL-C. */
4121 fast_breakcheck();
4122
4123#ifdef DEBUG
4124 if (scan != NULL && regnarrate)
4125 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004126 mch_errmsg((char *)regprop(scan));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 mch_errmsg("(\n");
4128 }
4129#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004130
4131 /*
Bram Moolenaar582fd852005-03-28 20:58:01 +00004132 * Repeat for items that can be matched sequentially, without using the
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004133 * regstack.
4134 */
4135 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004137 if (got_int || scan == NULL)
4138 {
4139 status = RA_FAIL;
4140 break;
4141 }
4142 status = RA_CONT;
4143
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144#ifdef DEBUG
4145 if (regnarrate)
4146 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004147 mch_errmsg((char *)regprop(scan));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 mch_errmsg("...\n");
4149# ifdef FEAT_SYN_HL
4150 if (re_extmatch_in != NULL)
4151 {
4152 int i;
4153
4154 mch_errmsg(_("External submatches:\n"));
4155 for (i = 0; i < NSUBEXP; i++)
4156 {
4157 mch_errmsg(" \"");
4158 if (re_extmatch_in->matches[i] != NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004159 mch_errmsg((char *)re_extmatch_in->matches[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 mch_errmsg("\"\n");
4161 }
4162 }
4163# endif
4164 }
4165#endif
4166 next = regnext(scan);
4167
4168 op = OP(scan);
4169 /* Check for character class with NL added. */
Bram Moolenaar640009d2006-10-17 16:48:26 +00004170 if (!reg_line_lbr && WITH_NL(op) && REG_MULTI
4171 && *reginput == NUL && reglnum <= reg_maxline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 {
4173 reg_nextline();
4174 }
4175 else if (reg_line_lbr && WITH_NL(op) && *reginput == '\n')
4176 {
4177 ADVANCE_REGINPUT();
4178 }
4179 else
4180 {
4181 if (WITH_NL(op))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004182 op -= ADD_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183#ifdef FEAT_MBYTE
4184 if (has_mbyte)
4185 c = (*mb_ptr2char)(reginput);
4186 else
4187#endif
4188 c = *reginput;
4189 switch (op)
4190 {
4191 case BOL:
4192 if (reginput != regline)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004193 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 break;
4195
4196 case EOL:
4197 if (c != NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004198 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 break;
4200
4201 case RE_BOF:
Bram Moolenaara7139332007-12-09 18:26:22 +00004202 /* We're not at the beginning of the file when below the first
4203 * line where we started, not at the start of the line or we
4204 * didn't start at the first line of the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 if (reglnum != 0 || reginput != regline
Bram Moolenaara7139332007-12-09 18:26:22 +00004206 || (REG_MULTI && reg_firstlnum > 1))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004207 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 break;
4209
4210 case RE_EOF:
4211 if (reglnum != reg_maxline || c != NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004212 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 break;
4214
4215 case CURSOR:
4216 /* Check if the buffer is in a window and compare the
4217 * reg_win->w_cursor position to the match position. */
4218 if (reg_win == NULL
4219 || (reglnum + reg_firstlnum != reg_win->w_cursor.lnum)
4220 || ((colnr_T)(reginput - regline) != reg_win->w_cursor.col))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004221 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 break;
4223
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004224 case RE_MARK:
4225 /* Compare the mark position to the match position. NOTE: Always
4226 * uses the current buffer. */
4227 {
4228 int mark = OPERAND(scan)[0];
4229 int cmp = OPERAND(scan)[1];
4230 pos_T *pos;
4231
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004232 pos = getmark_buf(reg_buf, mark, FALSE);
Bram Moolenaare9400a42007-05-06 13:04:32 +00004233 if (pos == NULL /* mark doesn't exist */
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004234 || pos->lnum <= 0 /* mark isn't set (in curbuf) */
4235 || (pos->lnum == reglnum + reg_firstlnum
4236 ? (pos->col == (colnr_T)(reginput - regline)
4237 ? (cmp == '<' || cmp == '>')
4238 : (pos->col < (colnr_T)(reginput - regline)
4239 ? cmp != '>'
4240 : cmp != '<'))
4241 : (pos->lnum < reglnum + reg_firstlnum
4242 ? cmp != '>'
4243 : cmp != '<')))
4244 status = RA_NOMATCH;
4245 }
4246 break;
4247
4248 case RE_VISUAL:
4249#ifdef FEAT_VISUAL
4250 /* Check if the buffer is the current buffer. and whether the
4251 * position is inside the Visual area. */
4252 if (reg_buf != curbuf || VIsual.lnum == 0)
4253 status = RA_NOMATCH;
4254 else
4255 {
4256 pos_T top, bot;
4257 linenr_T lnum;
4258 colnr_T col;
4259 win_T *wp = reg_win == NULL ? curwin : reg_win;
4260 int mode;
4261
4262 if (VIsual_active)
4263 {
4264 if (lt(VIsual, wp->w_cursor))
4265 {
4266 top = VIsual;
4267 bot = wp->w_cursor;
4268 }
4269 else
4270 {
4271 top = wp->w_cursor;
4272 bot = VIsual;
4273 }
4274 mode = VIsual_mode;
4275 }
4276 else
4277 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004278 if (lt(curbuf->b_visual.vi_start, curbuf->b_visual.vi_end))
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004279 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004280 top = curbuf->b_visual.vi_start;
4281 bot = curbuf->b_visual.vi_end;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004282 }
4283 else
4284 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004285 top = curbuf->b_visual.vi_end;
4286 bot = curbuf->b_visual.vi_start;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004287 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004288 mode = curbuf->b_visual.vi_mode;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004289 }
4290 lnum = reglnum + reg_firstlnum;
4291 col = (colnr_T)(reginput - regline);
4292 if (lnum < top.lnum || lnum > bot.lnum)
4293 status = RA_NOMATCH;
4294 else if (mode == 'v')
4295 {
4296 if ((lnum == top.lnum && col < top.col)
4297 || (lnum == bot.lnum
4298 && col >= bot.col + (*p_sel != 'e')))
4299 status = RA_NOMATCH;
4300 }
4301 else if (mode == Ctrl_V)
4302 {
4303 colnr_T start, end;
4304 colnr_T start2, end2;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004305 colnr_T cols;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004306
4307 getvvcol(wp, &top, &start, NULL, &end);
4308 getvvcol(wp, &bot, &start2, NULL, &end2);
4309 if (start2 < start)
4310 start = start2;
4311 if (end2 > end)
4312 end = end2;
4313 if (top.col == MAXCOL || bot.col == MAXCOL)
4314 end = MAXCOL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004315 cols = win_linetabsize(wp,
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004316 regline, (colnr_T)(reginput - regline));
Bram Moolenaar89d40322006-08-29 15:30:07 +00004317 if (cols < start || cols > end - (*p_sel == 'e'))
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004318 status = RA_NOMATCH;
4319 }
4320 }
4321#else
4322 status = RA_NOMATCH;
4323#endif
4324 break;
4325
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 case RE_LNUM:
4327 if (!REG_MULTI || !re_num_cmp((long_u)(reglnum + reg_firstlnum),
4328 scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004329 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 break;
4331
4332 case RE_COL:
4333 if (!re_num_cmp((long_u)(reginput - regline) + 1, scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004334 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 break;
4336
4337 case RE_VCOL:
4338 if (!re_num_cmp((long_u)win_linetabsize(
4339 reg_win == NULL ? curwin : reg_win,
4340 regline, (colnr_T)(reginput - regline)) + 1, scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004341 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 break;
4343
4344 case BOW: /* \<word; reginput points to w */
4345 if (c == NUL) /* Can't match at end of line */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004346 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347#ifdef FEAT_MBYTE
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004348 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 {
4350 int this_class;
4351
4352 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf813a182013-01-30 13:59:37 +01004353 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 if (this_class <= 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004355 status = RA_NOMATCH; /* not on a word at all */
4356 else if (reg_prev_class() == this_class)
4357 status = RA_NOMATCH; /* previous char is in same word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 }
4359#endif
4360 else
4361 {
Bram Moolenaar2f315ab2013-01-25 20:11:01 +01004362 if (!vim_iswordc_buf(c, reg_buf) || (reginput > regline
4363 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004364 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 }
4366 break;
4367
4368 case EOW: /* word\>; reginput points after d */
4369 if (reginput == regline) /* Can't match at start of line */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004370 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371#ifdef FEAT_MBYTE
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004372 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 {
4374 int this_class, prev_class;
4375
4376 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf813a182013-01-30 13:59:37 +01004377 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 prev_class = reg_prev_class();
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004379 if (this_class == prev_class
4380 || prev_class == 0 || prev_class == 1)
4381 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004384 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004386 if (!vim_iswordc_buf(reginput[-1], reg_buf)
4387 || (reginput[0] != NUL && vim_iswordc_buf(c, reg_buf)))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004388 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 }
4390 break; /* Matched with EOW */
4391
4392 case ANY:
Bram Moolenaare337e5f2013-01-30 18:21:51 +01004393 /* ANY does not match new lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 if (c == NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004395 status = RA_NOMATCH;
4396 else
4397 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 break;
4399
4400 case IDENT:
4401 if (!vim_isIDc(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004402 status = RA_NOMATCH;
4403 else
4404 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 break;
4406
4407 case SIDENT:
4408 if (VIM_ISDIGIT(*reginput) || !vim_isIDc(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004409 status = RA_NOMATCH;
4410 else
4411 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 break;
4413
4414 case KWORD:
Bram Moolenaarf813a182013-01-30 13:59:37 +01004415 if (!vim_iswordp_buf(reginput, reg_buf))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004416 status = RA_NOMATCH;
4417 else
4418 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 break;
4420
4421 case SKWORD:
Bram Moolenaarf813a182013-01-30 13:59:37 +01004422 if (VIM_ISDIGIT(*reginput) || !vim_iswordp_buf(reginput, reg_buf))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004423 status = RA_NOMATCH;
4424 else
4425 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 break;
4427
4428 case FNAME:
4429 if (!vim_isfilec(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004430 status = RA_NOMATCH;
4431 else
4432 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 break;
4434
4435 case SFNAME:
4436 if (VIM_ISDIGIT(*reginput) || !vim_isfilec(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004437 status = RA_NOMATCH;
4438 else
4439 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 break;
4441
4442 case PRINT:
4443 if (ptr2cells(reginput) != 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004444 status = RA_NOMATCH;
4445 else
4446 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 break;
4448
4449 case SPRINT:
4450 if (VIM_ISDIGIT(*reginput) || ptr2cells(reginput) != 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004451 status = RA_NOMATCH;
4452 else
4453 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 break;
4455
4456 case WHITE:
4457 if (!vim_iswhite(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004458 status = RA_NOMATCH;
4459 else
4460 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 break;
4462
4463 case NWHITE:
4464 if (c == NUL || vim_iswhite(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004465 status = RA_NOMATCH;
4466 else
4467 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 break;
4469
4470 case DIGIT:
4471 if (!ri_digit(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004472 status = RA_NOMATCH;
4473 else
4474 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 break;
4476
4477 case NDIGIT:
4478 if (c == NUL || ri_digit(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004479 status = RA_NOMATCH;
4480 else
4481 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 break;
4483
4484 case HEX:
4485 if (!ri_hex(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004486 status = RA_NOMATCH;
4487 else
4488 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 break;
4490
4491 case NHEX:
4492 if (c == NUL || ri_hex(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004493 status = RA_NOMATCH;
4494 else
4495 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 break;
4497
4498 case OCTAL:
4499 if (!ri_octal(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004500 status = RA_NOMATCH;
4501 else
4502 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 break;
4504
4505 case NOCTAL:
4506 if (c == NUL || ri_octal(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004507 status = RA_NOMATCH;
4508 else
4509 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 break;
4511
4512 case WORD:
4513 if (!ri_word(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004514 status = RA_NOMATCH;
4515 else
4516 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 break;
4518
4519 case NWORD:
4520 if (c == NUL || ri_word(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004521 status = RA_NOMATCH;
4522 else
4523 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 break;
4525
4526 case HEAD:
4527 if (!ri_head(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004528 status = RA_NOMATCH;
4529 else
4530 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 break;
4532
4533 case NHEAD:
4534 if (c == NUL || ri_head(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004535 status = RA_NOMATCH;
4536 else
4537 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 break;
4539
4540 case ALPHA:
4541 if (!ri_alpha(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004542 status = RA_NOMATCH;
4543 else
4544 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 break;
4546
4547 case NALPHA:
4548 if (c == NUL || ri_alpha(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004549 status = RA_NOMATCH;
4550 else
4551 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 break;
4553
4554 case LOWER:
4555 if (!ri_lower(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004556 status = RA_NOMATCH;
4557 else
4558 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 break;
4560
4561 case NLOWER:
4562 if (c == NUL || ri_lower(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004563 status = RA_NOMATCH;
4564 else
4565 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 break;
4567
4568 case UPPER:
4569 if (!ri_upper(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004570 status = RA_NOMATCH;
4571 else
4572 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 break;
4574
4575 case NUPPER:
4576 if (c == NUL || ri_upper(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004577 status = RA_NOMATCH;
4578 else
4579 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 break;
4581
4582 case EXACTLY:
4583 {
4584 int len;
4585 char_u *opnd;
4586
4587 opnd = OPERAND(scan);
4588 /* Inline the first byte, for speed. */
4589 if (*opnd != *reginput
4590 && (!ireg_ic || (
4591#ifdef FEAT_MBYTE
4592 !enc_utf8 &&
4593#endif
Bram Moolenaara245a5b2007-08-11 11:58:23 +00004594 MB_TOLOWER(*opnd) != MB_TOLOWER(*reginput))))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004595 status = RA_NOMATCH;
4596 else if (*opnd == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 {
4598 /* match empty string always works; happens when "~" is
4599 * empty. */
4600 }
4601 else if (opnd[1] == NUL
4602#ifdef FEAT_MBYTE
4603 && !(enc_utf8 && ireg_ic)
4604#endif
4605 )
4606 ++reginput; /* matched a single char */
4607 else
4608 {
4609 len = (int)STRLEN(opnd);
4610 /* Need to match first byte again for multi-byte. */
4611 if (cstrncmp(opnd, reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004612 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613#ifdef FEAT_MBYTE
4614 /* Check for following composing character. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004615 else if (enc_utf8
4616 && UTF_COMPOSINGLIKE(reginput, reginput + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 {
4618 /* raaron: This code makes a composing character get
4619 * ignored, which is the correct behavior (sometimes)
4620 * for voweled Hebrew texts. */
4621 if (!ireg_icombine)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004622 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004625 else
4626 reginput += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 }
4628 }
4629 break;
4630
4631 case ANYOF:
4632 case ANYBUT:
4633 if (c == NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004634 status = RA_NOMATCH;
4635 else if ((cstrchr(OPERAND(scan), c) == NULL) == (op == ANYOF))
4636 status = RA_NOMATCH;
4637 else
4638 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 break;
4640
4641#ifdef FEAT_MBYTE
4642 case MULTIBYTECODE:
4643 if (has_mbyte)
4644 {
4645 int i, len;
4646 char_u *opnd;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004647 int opndc = 0, inpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648
4649 opnd = OPERAND(scan);
4650 /* Safety check (just in case 'encoding' was changed since
4651 * compiling the program). */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004652 if ((len = (*mb_ptr2len)(opnd)) < 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004653 {
4654 status = RA_NOMATCH;
4655 break;
4656 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004657 if (enc_utf8)
4658 opndc = mb_ptr2char(opnd);
4659 if (enc_utf8 && utf_iscomposing(opndc))
4660 {
4661 /* When only a composing char is given match at any
4662 * position where that composing char appears. */
4663 status = RA_NOMATCH;
4664 for (i = 0; reginput[i] != NUL; i += utf_char2len(inpc))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004665 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004666 inpc = mb_ptr2char(reginput + i);
4667 if (!utf_iscomposing(inpc))
4668 {
4669 if (i > 0)
4670 break;
4671 }
4672 else if (opndc == inpc)
4673 {
4674 /* Include all following composing chars. */
4675 len = i + mb_ptr2len(reginput + i);
4676 status = RA_MATCH;
4677 break;
4678 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004679 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004680 }
4681 else
4682 for (i = 0; i < len; ++i)
4683 if (opnd[i] != reginput[i])
4684 {
4685 status = RA_NOMATCH;
4686 break;
4687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 reginput += len;
4689 }
4690 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004691 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 break;
4693#endif
4694
4695 case NOTHING:
4696 break;
4697
4698 case BACK:
Bram Moolenaar582fd852005-03-28 20:58:01 +00004699 {
4700 int i;
4701 backpos_T *bp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702
Bram Moolenaar582fd852005-03-28 20:58:01 +00004703 /*
4704 * When we run into BACK we need to check if we don't keep
4705 * looping without matching any input. The second and later
4706 * times a BACK is encountered it fails if the input is still
4707 * at the same position as the previous time.
4708 * The positions are stored in "backpos" and found by the
4709 * current value of "scan", the position in the RE program.
4710 */
4711 bp = (backpos_T *)backpos.ga_data;
4712 for (i = 0; i < backpos.ga_len; ++i)
4713 if (bp[i].bp_scan == scan)
4714 break;
4715 if (i == backpos.ga_len)
4716 {
4717 /* First time at this BACK, make room to store the pos. */
4718 if (ga_grow(&backpos, 1) == FAIL)
4719 status = RA_FAIL;
4720 else
4721 {
4722 /* get "ga_data" again, it may have changed */
4723 bp = (backpos_T *)backpos.ga_data;
4724 bp[i].bp_scan = scan;
4725 ++backpos.ga_len;
4726 }
4727 }
4728 else if (reg_save_equal(&bp[i].bp_pos))
4729 /* Still at same position as last time, fail. */
4730 status = RA_NOMATCH;
4731
4732 if (status != RA_FAIL && status != RA_NOMATCH)
4733 reg_save(&bp[i].bp_pos, &backpos);
4734 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004735 break;
4736
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 case MOPEN + 0: /* Match start: \zs */
4738 case MOPEN + 1: /* \( */
4739 case MOPEN + 2:
4740 case MOPEN + 3:
4741 case MOPEN + 4:
4742 case MOPEN + 5:
4743 case MOPEN + 6:
4744 case MOPEN + 7:
4745 case MOPEN + 8:
4746 case MOPEN + 9:
4747 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 no = op - MOPEN;
4749 cleanup_subexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004750 rp = regstack_push(RS_MOPEN, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004751 if (rp == NULL)
4752 status = RA_FAIL;
4753 else
4754 {
4755 rp->rs_no = no;
4756 save_se(&rp->rs_un.sesave, &reg_startpos[no],
4757 &reg_startp[no]);
4758 /* We simply continue and handle the result when done. */
4759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004761 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762
4763 case NOPEN: /* \%( */
4764 case NCLOSE: /* \) after \%( */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004765 if (regstack_push(RS_NOPEN, scan) == NULL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004766 status = RA_FAIL;
4767 /* We simply continue and handle the result when done. */
4768 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769
4770#ifdef FEAT_SYN_HL
4771 case ZOPEN + 1:
4772 case ZOPEN + 2:
4773 case ZOPEN + 3:
4774 case ZOPEN + 4:
4775 case ZOPEN + 5:
4776 case ZOPEN + 6:
4777 case ZOPEN + 7:
4778 case ZOPEN + 8:
4779 case ZOPEN + 9:
4780 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 no = op - ZOPEN;
4782 cleanup_zsubexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004783 rp = regstack_push(RS_ZOPEN, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004784 if (rp == NULL)
4785 status = RA_FAIL;
4786 else
4787 {
4788 rp->rs_no = no;
4789 save_se(&rp->rs_un.sesave, &reg_startzpos[no],
4790 &reg_startzp[no]);
4791 /* We simply continue and handle the result when done. */
4792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004794 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795#endif
4796
4797 case MCLOSE + 0: /* Match end: \ze */
4798 case MCLOSE + 1: /* \) */
4799 case MCLOSE + 2:
4800 case MCLOSE + 3:
4801 case MCLOSE + 4:
4802 case MCLOSE + 5:
4803 case MCLOSE + 6:
4804 case MCLOSE + 7:
4805 case MCLOSE + 8:
4806 case MCLOSE + 9:
4807 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 no = op - MCLOSE;
4809 cleanup_subexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004810 rp = regstack_push(RS_MCLOSE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004811 if (rp == NULL)
4812 status = RA_FAIL;
4813 else
4814 {
4815 rp->rs_no = no;
4816 save_se(&rp->rs_un.sesave, &reg_endpos[no], &reg_endp[no]);
4817 /* We simply continue and handle the result when done. */
4818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004820 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821
4822#ifdef FEAT_SYN_HL
4823 case ZCLOSE + 1: /* \) after \z( */
4824 case ZCLOSE + 2:
4825 case ZCLOSE + 3:
4826 case ZCLOSE + 4:
4827 case ZCLOSE + 5:
4828 case ZCLOSE + 6:
4829 case ZCLOSE + 7:
4830 case ZCLOSE + 8:
4831 case ZCLOSE + 9:
4832 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 no = op - ZCLOSE;
4834 cleanup_zsubexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004835 rp = regstack_push(RS_ZCLOSE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004836 if (rp == NULL)
4837 status = RA_FAIL;
4838 else
4839 {
4840 rp->rs_no = no;
4841 save_se(&rp->rs_un.sesave, &reg_endzpos[no],
4842 &reg_endzp[no]);
4843 /* We simply continue and handle the result when done. */
4844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004846 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847#endif
4848
4849 case BACKREF + 1:
4850 case BACKREF + 2:
4851 case BACKREF + 3:
4852 case BACKREF + 4:
4853 case BACKREF + 5:
4854 case BACKREF + 6:
4855 case BACKREF + 7:
4856 case BACKREF + 8:
4857 case BACKREF + 9:
4858 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 int len;
4860 linenr_T clnum;
4861 colnr_T ccol;
4862 char_u *p;
4863
4864 no = op - BACKREF;
4865 cleanup_subexpr();
4866 if (!REG_MULTI) /* Single-line regexp */
4867 {
Bram Moolenaar7670fa02009-02-21 21:04:20 +00004868 if (reg_startp[no] == NULL || reg_endp[no] == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 {
4870 /* Backref was not set: Match an empty string. */
4871 len = 0;
4872 }
4873 else
4874 {
4875 /* Compare current input with back-ref in the same
4876 * line. */
4877 len = (int)(reg_endp[no] - reg_startp[no]);
4878 if (cstrncmp(reg_startp[no], reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004879 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 }
4881 }
4882 else /* Multi-line regexp */
4883 {
Bram Moolenaar7670fa02009-02-21 21:04:20 +00004884 if (reg_startpos[no].lnum < 0 || reg_endpos[no].lnum < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 {
4886 /* Backref was not set: Match an empty string. */
4887 len = 0;
4888 }
4889 else
4890 {
4891 if (reg_startpos[no].lnum == reglnum
4892 && reg_endpos[no].lnum == reglnum)
4893 {
4894 /* Compare back-ref within the current line. */
4895 len = reg_endpos[no].col - reg_startpos[no].col;
4896 if (cstrncmp(regline + reg_startpos[no].col,
4897 reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004898 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 }
4900 else
4901 {
4902 /* Messy situation: Need to compare between two
4903 * lines. */
4904 ccol = reg_startpos[no].col;
4905 clnum = reg_startpos[no].lnum;
4906 for (;;)
4907 {
4908 /* Since getting one line may invalidate
4909 * the other, need to make copy. Slow! */
4910 if (regline != reg_tofree)
4911 {
4912 len = (int)STRLEN(regline);
4913 if (reg_tofree == NULL
4914 || len >= (int)reg_tofreelen)
4915 {
4916 len += 50; /* get some extra */
4917 vim_free(reg_tofree);
4918 reg_tofree = alloc(len);
4919 if (reg_tofree == NULL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004920 {
4921 status = RA_FAIL; /* outof memory!*/
4922 break;
4923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 reg_tofreelen = len;
4925 }
4926 STRCPY(reg_tofree, regline);
4927 reginput = reg_tofree
4928 + (reginput - regline);
4929 regline = reg_tofree;
4930 }
4931
4932 /* Get the line to compare with. */
4933 p = reg_getline(clnum);
4934 if (clnum == reg_endpos[no].lnum)
4935 len = reg_endpos[no].col - ccol;
4936 else
4937 len = (int)STRLEN(p + ccol);
4938
4939 if (cstrncmp(p + ccol, reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004940 {
4941 status = RA_NOMATCH; /* doesn't match */
4942 break;
4943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 if (clnum == reg_endpos[no].lnum)
4945 break; /* match and at end! */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004946 if (reglnum >= reg_maxline)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004947 {
4948 status = RA_NOMATCH; /* text too short */
4949 break;
4950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951
4952 /* Advance to next line. */
4953 reg_nextline();
4954 ++clnum;
4955 ccol = 0;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004956 if (got_int)
4957 {
4958 status = RA_FAIL;
4959 break;
4960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 }
4962
4963 /* found a match! Note that regline may now point
4964 * to a copy of the line, that should not matter. */
4965 }
4966 }
4967 }
4968
4969 /* Matched the backref, skip over it. */
4970 reginput += len;
4971 }
4972 break;
4973
4974#ifdef FEAT_SYN_HL
4975 case ZREF + 1:
4976 case ZREF + 2:
4977 case ZREF + 3:
4978 case ZREF + 4:
4979 case ZREF + 5:
4980 case ZREF + 6:
4981 case ZREF + 7:
4982 case ZREF + 8:
4983 case ZREF + 9:
4984 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 int len;
4986
4987 cleanup_zsubexpr();
4988 no = op - ZREF;
4989 if (re_extmatch_in != NULL
4990 && re_extmatch_in->matches[no] != NULL)
4991 {
4992 len = (int)STRLEN(re_extmatch_in->matches[no]);
4993 if (cstrncmp(re_extmatch_in->matches[no],
4994 reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004995 status = RA_NOMATCH;
4996 else
4997 reginput += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 }
4999 else
5000 {
5001 /* Backref was not set: Match an empty string. */
5002 }
5003 }
5004 break;
5005#endif
5006
5007 case BRANCH:
5008 {
5009 if (OP(next) != BRANCH) /* No choice. */
5010 next = OPERAND(scan); /* Avoid recursion. */
5011 else
5012 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005013 rp = regstack_push(RS_BRANCH, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005014 if (rp == NULL)
5015 status = RA_FAIL;
5016 else
5017 status = RA_BREAK; /* rest is below */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 }
5019 }
5020 break;
5021
5022 case BRACE_LIMITS:
5023 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 if (OP(next) == BRACE_SIMPLE)
5025 {
5026 bl_minval = OPERAND_MIN(scan);
5027 bl_maxval = OPERAND_MAX(scan);
5028 }
5029 else if (OP(next) >= BRACE_COMPLEX
5030 && OP(next) < BRACE_COMPLEX + 10)
5031 {
5032 no = OP(next) - BRACE_COMPLEX;
5033 brace_min[no] = OPERAND_MIN(scan);
5034 brace_max[no] = OPERAND_MAX(scan);
5035 brace_count[no] = 0;
5036 }
5037 else
5038 {
5039 EMSG(_(e_internal)); /* Shouldn't happen */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005040 status = RA_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 }
5042 }
5043 break;
5044
5045 case BRACE_COMPLEX + 0:
5046 case BRACE_COMPLEX + 1:
5047 case BRACE_COMPLEX + 2:
5048 case BRACE_COMPLEX + 3:
5049 case BRACE_COMPLEX + 4:
5050 case BRACE_COMPLEX + 5:
5051 case BRACE_COMPLEX + 6:
5052 case BRACE_COMPLEX + 7:
5053 case BRACE_COMPLEX + 8:
5054 case BRACE_COMPLEX + 9:
5055 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 no = op - BRACE_COMPLEX;
5057 ++brace_count[no];
5058
5059 /* If not matched enough times yet, try one more */
5060 if (brace_count[no] <= (brace_min[no] <= brace_max[no]
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005061 ? brace_min[no] : brace_max[no]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005063 rp = regstack_push(RS_BRCPLX_MORE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005064 if (rp == NULL)
5065 status = RA_FAIL;
5066 else
5067 {
5068 rp->rs_no = no;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005069 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005070 next = OPERAND(scan);
5071 /* We continue and handle the result when done. */
5072 }
5073 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 }
5075
5076 /* If matched enough times, may try matching some more */
5077 if (brace_min[no] <= brace_max[no])
5078 {
5079 /* Range is the normal way around, use longest match */
5080 if (brace_count[no] <= brace_max[no])
5081 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005082 rp = regstack_push(RS_BRCPLX_LONG, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005083 if (rp == NULL)
5084 status = RA_FAIL;
5085 else
5086 {
5087 rp->rs_no = no;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005088 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005089 next = OPERAND(scan);
5090 /* We continue and handle the result when done. */
5091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 }
5093 }
5094 else
5095 {
5096 /* Range is backwards, use shortest match first */
5097 if (brace_count[no] <= brace_min[no])
5098 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005099 rp = regstack_push(RS_BRCPLX_SHORT, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005100 if (rp == NULL)
5101 status = RA_FAIL;
5102 else
5103 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005104 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005105 /* We continue and handle the result when done. */
5106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 }
5108 }
5109 }
5110 break;
5111
5112 case BRACE_SIMPLE:
5113 case STAR:
5114 case PLUS:
5115 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005116 regstar_T rst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117
5118 /*
5119 * Lookahead to avoid useless match attempts when we know
5120 * what character comes next.
5121 */
5122 if (OP(next) == EXACTLY)
5123 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005124 rst.nextb = *OPERAND(next);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 if (ireg_ic)
5126 {
Bram Moolenaara245a5b2007-08-11 11:58:23 +00005127 if (MB_ISUPPER(rst.nextb))
5128 rst.nextb_ic = MB_TOLOWER(rst.nextb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 else
Bram Moolenaara245a5b2007-08-11 11:58:23 +00005130 rst.nextb_ic = MB_TOUPPER(rst.nextb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 }
5132 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005133 rst.nextb_ic = rst.nextb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 }
5135 else
5136 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005137 rst.nextb = NUL;
5138 rst.nextb_ic = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 }
5140 if (op != BRACE_SIMPLE)
5141 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005142 rst.minval = (op == STAR) ? 0 : 1;
5143 rst.maxval = MAX_LIMIT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 }
5145 else
5146 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005147 rst.minval = bl_minval;
5148 rst.maxval = bl_maxval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 }
5150
5151 /*
5152 * When maxval > minval, try matching as much as possible, up
5153 * to maxval. When maxval < minval, try matching at least the
5154 * minimal number (since the range is backwards, that's also
5155 * maxval!).
5156 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005157 rst.count = regrepeat(OPERAND(scan), rst.maxval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005160 status = RA_FAIL;
5161 break;
5162 }
5163 if (rst.minval <= rst.maxval
5164 ? rst.count >= rst.minval : rst.count >= rst.maxval)
5165 {
5166 /* It could match. Prepare for trying to match what
5167 * follows. The code is below. Parameters are stored in
5168 * a regstar_T on the regstack. */
Bram Moolenaar916b7af2005-03-16 09:52:38 +00005169 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005170 {
5171 EMSG(_(e_maxmempat));
5172 status = RA_FAIL;
5173 }
5174 else if (ga_grow(&regstack, sizeof(regstar_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005175 status = RA_FAIL;
5176 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005178 regstack.ga_len += sizeof(regstar_T);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005179 rp = regstack_push(rst.minval <= rst.maxval
Bram Moolenaar582fd852005-03-28 20:58:01 +00005180 ? RS_STAR_LONG : RS_STAR_SHORT, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005181 if (rp == NULL)
5182 status = RA_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005184 {
5185 *(((regstar_T *)rp) - 1) = rst;
5186 status = RA_BREAK; /* skip the restore bits */
5187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 }
5189 }
5190 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005191 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 }
5194 break;
5195
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005196 case NOMATCH:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 case MATCH:
5198 case SUBPAT:
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005199 rp = regstack_push(RS_NOMATCH, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005200 if (rp == NULL)
5201 status = RA_FAIL;
5202 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005204 rp->rs_no = op;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005205 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005206 next = OPERAND(scan);
5207 /* We continue and handle the result when done. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 }
5209 break;
5210
5211 case BEHIND:
5212 case NOBEHIND:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005213 /* Need a bit of room to store extra positions. */
Bram Moolenaar916b7af2005-03-16 09:52:38 +00005214 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005215 {
5216 EMSG(_(e_maxmempat));
5217 status = RA_FAIL;
5218 }
5219 else if (ga_grow(&regstack, sizeof(regbehind_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005220 status = RA_FAIL;
5221 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005223 regstack.ga_len += sizeof(regbehind_T);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005224 rp = regstack_push(RS_BEHIND1, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005225 if (rp == NULL)
5226 status = RA_FAIL;
5227 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 {
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005229 /* Need to save the subexpr to be able to restore them
5230 * when there is a match but we don't use it. */
5231 save_subexpr(((regbehind_T *)rp) - 1);
5232
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005233 rp->rs_no = op;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005234 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005235 /* First try if what follows matches. If it does then we
5236 * check the behind match by looping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005239 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240
5241 case BHPOS:
5242 if (REG_MULTI)
5243 {
5244 if (behind_pos.rs_u.pos.col != (colnr_T)(reginput - regline)
5245 || behind_pos.rs_u.pos.lnum != reglnum)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005246 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 }
5248 else if (behind_pos.rs_u.ptr != reginput)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005249 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 break;
5251
5252 case NEWL:
Bram Moolenaar640009d2006-10-17 16:48:26 +00005253 if ((c != NUL || !REG_MULTI || reglnum > reg_maxline
5254 || reg_line_lbr) && (c != '\n' || !reg_line_lbr))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005255 status = RA_NOMATCH;
5256 else if (reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 ADVANCE_REGINPUT();
5258 else
5259 reg_nextline();
5260 break;
5261
5262 case END:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005263 status = RA_MATCH; /* Success! */
5264 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265
5266 default:
5267 EMSG(_(e_re_corr));
5268#ifdef DEBUG
5269 printf("Illegal op code %d\n", op);
5270#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005271 status = RA_FAIL;
5272 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 }
5274 }
5275
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005276 /* If we can't continue sequentially, break the inner loop. */
5277 if (status != RA_CONT)
5278 break;
5279
5280 /* Continue in inner loop, advance to next item. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 scan = next;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005282
5283 } /* end of inner loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284
5285 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005286 * If there is something on the regstack execute the code for the state.
Bram Moolenaar582fd852005-03-28 20:58:01 +00005287 * If the state is popped then loop and use the older state.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005289 while (regstack.ga_len > 0 && status != RA_FAIL)
5290 {
5291 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len) - 1;
5292 switch (rp->rs_state)
5293 {
5294 case RS_NOPEN:
5295 /* Result is passed on as-is, simply pop the state. */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005296 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005297 break;
5298
5299 case RS_MOPEN:
5300 /* Pop the state. Restore pointers when there is no match. */
5301 if (status == RA_NOMATCH)
5302 restore_se(&rp->rs_un.sesave, &reg_startpos[rp->rs_no],
5303 &reg_startp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005304 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005305 break;
5306
5307#ifdef FEAT_SYN_HL
5308 case RS_ZOPEN:
5309 /* Pop the state. Restore pointers when there is no match. */
5310 if (status == RA_NOMATCH)
5311 restore_se(&rp->rs_un.sesave, &reg_startzpos[rp->rs_no],
5312 &reg_startzp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005313 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005314 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005316
5317 case RS_MCLOSE:
5318 /* Pop the state. Restore pointers when there is no match. */
5319 if (status == RA_NOMATCH)
5320 restore_se(&rp->rs_un.sesave, &reg_endpos[rp->rs_no],
5321 &reg_endp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005322 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005323 break;
5324
5325#ifdef FEAT_SYN_HL
5326 case RS_ZCLOSE:
5327 /* Pop the state. Restore pointers when there is no match. */
5328 if (status == RA_NOMATCH)
5329 restore_se(&rp->rs_un.sesave, &reg_endzpos[rp->rs_no],
5330 &reg_endzp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005331 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005332 break;
5333#endif
5334
5335 case RS_BRANCH:
5336 if (status == RA_MATCH)
5337 /* this branch matched, use it */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005338 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005339 else
5340 {
5341 if (status != RA_BREAK)
5342 {
5343 /* After a non-matching branch: try next one. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005344 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005345 scan = rp->rs_scan;
5346 }
5347 if (scan == NULL || OP(scan) != BRANCH)
5348 {
5349 /* no more branches, didn't find a match */
5350 status = RA_NOMATCH;
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005351 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005352 }
5353 else
5354 {
5355 /* Prepare to try a branch. */
5356 rp->rs_scan = regnext(scan);
Bram Moolenaar582fd852005-03-28 20:58:01 +00005357 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005358 scan = OPERAND(scan);
5359 }
5360 }
5361 break;
5362
5363 case RS_BRCPLX_MORE:
5364 /* Pop the state. Restore pointers when there is no match. */
5365 if (status == RA_NOMATCH)
5366 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005367 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005368 --brace_count[rp->rs_no]; /* decrement match count */
5369 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005370 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005371 break;
5372
5373 case RS_BRCPLX_LONG:
5374 /* Pop the state. Restore pointers when there is no match. */
5375 if (status == RA_NOMATCH)
5376 {
5377 /* There was no match, but we did find enough matches. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005378 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005379 --brace_count[rp->rs_no];
5380 /* continue with the items after "\{}" */
5381 status = RA_CONT;
5382 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005383 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005384 if (status == RA_CONT)
5385 scan = regnext(scan);
5386 break;
5387
5388 case RS_BRCPLX_SHORT:
5389 /* Pop the state. Restore pointers when there is no match. */
5390 if (status == RA_NOMATCH)
5391 /* There was no match, try to match one more item. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005392 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005393 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005394 if (status == RA_NOMATCH)
5395 {
5396 scan = OPERAND(scan);
5397 status = RA_CONT;
5398 }
5399 break;
5400
5401 case RS_NOMATCH:
5402 /* Pop the state. If the operand matches for NOMATCH or
5403 * doesn't match for MATCH/SUBPAT, we fail. Otherwise backup,
5404 * except for SUBPAT, and continue with the next item. */
5405 if (status == (rp->rs_no == NOMATCH ? RA_MATCH : RA_NOMATCH))
5406 status = RA_NOMATCH;
5407 else
5408 {
5409 status = RA_CONT;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005410 if (rp->rs_no != SUBPAT) /* zero-width */
5411 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005412 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005413 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005414 if (status == RA_CONT)
5415 scan = regnext(scan);
5416 break;
5417
5418 case RS_BEHIND1:
5419 if (status == RA_NOMATCH)
5420 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005421 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005422 regstack.ga_len -= sizeof(regbehind_T);
5423 }
5424 else
5425 {
5426 /* The stuff after BEHIND/NOBEHIND matches. Now try if
5427 * the behind part does (not) match before the current
5428 * position in the input. This must be done at every
5429 * position in the input and checking if the match ends at
5430 * the current position. */
5431
5432 /* save the position after the found match for next */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005433 reg_save(&(((regbehind_T *)rp) - 1)->save_after, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005434
5435 /* start looking for a match with operand at the current
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00005436 * position. Go back one character until we find the
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005437 * result, hitting the start of the line or the previous
5438 * line (for multi-line matching).
5439 * Set behind_pos to where the match should end, BHPOS
5440 * will match it. Save the current value. */
5441 (((regbehind_T *)rp) - 1)->save_behind = behind_pos;
5442 behind_pos = rp->rs_un.regsave;
5443
5444 rp->rs_state = RS_BEHIND2;
5445
Bram Moolenaar582fd852005-03-28 20:58:01 +00005446 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005447 scan = OPERAND(rp->rs_scan);
5448 }
5449 break;
5450
5451 case RS_BEHIND2:
5452 /*
5453 * Looping for BEHIND / NOBEHIND match.
5454 */
5455 if (status == RA_MATCH && reg_save_equal(&behind_pos))
5456 {
5457 /* found a match that ends where "next" started */
5458 behind_pos = (((regbehind_T *)rp) - 1)->save_behind;
5459 if (rp->rs_no == BEHIND)
Bram Moolenaar582fd852005-03-28 20:58:01 +00005460 reg_restore(&(((regbehind_T *)rp) - 1)->save_after,
5461 &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005462 else
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005463 {
5464 /* But we didn't want a match. Need to restore the
5465 * subexpr, because what follows matched, so they have
5466 * been set. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005467 status = RA_NOMATCH;
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005468 restore_subexpr(((regbehind_T *)rp) - 1);
5469 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005470 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005471 regstack.ga_len -= sizeof(regbehind_T);
5472 }
5473 else
5474 {
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005475 /* No match or a match that doesn't end where we want it: Go
5476 * back one character. May go to previous line once. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005477 no = OK;
5478 if (REG_MULTI)
5479 {
5480 if (rp->rs_un.regsave.rs_u.pos.col == 0)
5481 {
5482 if (rp->rs_un.regsave.rs_u.pos.lnum
5483 < behind_pos.rs_u.pos.lnum
5484 || reg_getline(
5485 --rp->rs_un.regsave.rs_u.pos.lnum)
5486 == NULL)
5487 no = FAIL;
5488 else
5489 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005490 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005491 rp->rs_un.regsave.rs_u.pos.col =
5492 (colnr_T)STRLEN(regline);
5493 }
5494 }
5495 else
Bram Moolenaarf5e44a72013-02-26 18:46:01 +01005496#ifdef FEAT_MBYTE
5497 if (has_mbyte)
5498 rp->rs_un.regsave.rs_u.pos.col -=
5499 (*mb_head_off)(regline, regline
5500 + rp->rs_un.regsave.rs_u.pos.col - 1) + 1;
5501 else
5502#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005503 --rp->rs_un.regsave.rs_u.pos.col;
5504 }
5505 else
5506 {
5507 if (rp->rs_un.regsave.rs_u.ptr == regline)
5508 no = FAIL;
5509 else
5510 --rp->rs_un.regsave.rs_u.ptr;
5511 }
5512 if (no == OK)
5513 {
5514 /* Advanced, prepare for finding match again. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005515 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005516 scan = OPERAND(rp->rs_scan);
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005517 if (status == RA_MATCH)
5518 {
5519 /* We did match, so subexpr may have been changed,
5520 * need to restore them for the next try. */
5521 status = RA_NOMATCH;
5522 restore_subexpr(((regbehind_T *)rp) - 1);
5523 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005524 }
5525 else
5526 {
5527 /* Can't advance. For NOBEHIND that's a match. */
5528 behind_pos = (((regbehind_T *)rp) - 1)->save_behind;
5529 if (rp->rs_no == NOBEHIND)
5530 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005531 reg_restore(&(((regbehind_T *)rp) - 1)->save_after,
5532 &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005533 status = RA_MATCH;
5534 }
5535 else
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005536 {
5537 /* We do want a proper match. Need to restore the
5538 * subexpr if we had a match, because they may have
5539 * been set. */
5540 if (status == RA_MATCH)
5541 {
5542 status = RA_NOMATCH;
5543 restore_subexpr(((regbehind_T *)rp) - 1);
5544 }
5545 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005546 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005547 regstack.ga_len -= sizeof(regbehind_T);
5548 }
5549 }
5550 break;
5551
5552 case RS_STAR_LONG:
5553 case RS_STAR_SHORT:
5554 {
5555 regstar_T *rst = ((regstar_T *)rp) - 1;
5556
5557 if (status == RA_MATCH)
5558 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005559 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005560 regstack.ga_len -= sizeof(regstar_T);
5561 break;
5562 }
5563
5564 /* Tried once already, restore input pointers. */
5565 if (status != RA_BREAK)
Bram Moolenaar582fd852005-03-28 20:58:01 +00005566 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005567
5568 /* Repeat until we found a position where it could match. */
5569 for (;;)
5570 {
5571 if (status != RA_BREAK)
5572 {
5573 /* Tried first position already, advance. */
5574 if (rp->rs_state == RS_STAR_LONG)
5575 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005576 /* Trying for longest match, but couldn't or
5577 * didn't match -- back up one char. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005578 if (--rst->count < rst->minval)
5579 break;
5580 if (reginput == regline)
5581 {
5582 /* backup to last char of previous line */
5583 --reglnum;
5584 regline = reg_getline(reglnum);
5585 /* Just in case regrepeat() didn't count
5586 * right. */
5587 if (regline == NULL)
5588 break;
5589 reginput = regline + STRLEN(regline);
5590 fast_breakcheck();
5591 }
5592 else
5593 mb_ptr_back(regline, reginput);
5594 }
5595 else
5596 {
5597 /* Range is backwards, use shortest match first.
5598 * Careful: maxval and minval are exchanged!
5599 * Couldn't or didn't match: try advancing one
5600 * char. */
5601 if (rst->count == rst->minval
5602 || regrepeat(OPERAND(rp->rs_scan), 1L) == 0)
5603 break;
5604 ++rst->count;
5605 }
5606 if (got_int)
5607 break;
5608 }
5609 else
5610 status = RA_NOMATCH;
5611
5612 /* If it could match, try it. */
5613 if (rst->nextb == NUL || *reginput == rst->nextb
5614 || *reginput == rst->nextb_ic)
5615 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005616 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005617 scan = regnext(rp->rs_scan);
5618 status = RA_CONT;
5619 break;
5620 }
5621 }
5622 if (status != RA_CONT)
5623 {
5624 /* Failed. */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005625 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005626 regstack.ga_len -= sizeof(regstar_T);
5627 status = RA_NOMATCH;
5628 }
5629 }
5630 break;
5631 }
5632
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005633 /* If we want to continue the inner loop or didn't pop a state
5634 * continue matching loop */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005635 if (status == RA_CONT || rp == (regitem_T *)
5636 ((char *)regstack.ga_data + regstack.ga_len) - 1)
5637 break;
5638 }
5639
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005640 /* May need to continue with the inner loop, starting at "scan". */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005641 if (status == RA_CONT)
5642 continue;
5643
5644 /*
5645 * If the regstack is empty or something failed we are done.
5646 */
5647 if (regstack.ga_len == 0 || status == RA_FAIL)
5648 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005649 if (scan == NULL)
5650 {
5651 /*
5652 * We get here only if there's trouble -- normally "case END" is
5653 * the terminating point.
5654 */
5655 EMSG(_(e_re_corr));
5656#ifdef DEBUG
5657 printf("Premature EOL\n");
5658#endif
5659 }
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005660 if (status == RA_FAIL)
5661 got_int = TRUE;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005662 return (status == RA_MATCH);
5663 }
5664
5665 } /* End of loop until the regstack is empty. */
5666
5667 /* NOTREACHED */
5668}
5669
5670/*
5671 * Push an item onto the regstack.
5672 * Returns pointer to new item. Returns NULL when out of memory.
5673 */
5674 static regitem_T *
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005675regstack_push(state, scan)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005676 regstate_T state;
5677 char_u *scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005678{
5679 regitem_T *rp;
5680
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005681 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005682 {
5683 EMSG(_(e_maxmempat));
5684 return NULL;
5685 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005686 if (ga_grow(&regstack, sizeof(regitem_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005687 return NULL;
5688
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005689 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005690 rp->rs_state = state;
5691 rp->rs_scan = scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005692
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005693 regstack.ga_len += sizeof(regitem_T);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005694 return rp;
5695}
5696
5697/*
5698 * Pop an item from the regstack.
5699 */
5700 static void
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005701regstack_pop(scan)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005702 char_u **scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005703{
5704 regitem_T *rp;
5705
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005706 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len) - 1;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005707 *scan = rp->rs_scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005708
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005709 regstack.ga_len -= sizeof(regitem_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710}
5711
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712/*
5713 * regrepeat - repeatedly match something simple, return how many.
5714 * Advances reginput (and reglnum) to just after the matched chars.
5715 */
5716 static int
5717regrepeat(p, maxcount)
5718 char_u *p;
5719 long maxcount; /* maximum number of matches allowed */
5720{
5721 long count = 0;
5722 char_u *scan;
5723 char_u *opnd;
5724 int mask;
5725 int testval = 0;
5726
5727 scan = reginput; /* Make local copy of reginput for speed. */
5728 opnd = OPERAND(p);
5729 switch (OP(p))
5730 {
5731 case ANY:
5732 case ANY + ADD_NL:
5733 while (count < maxcount)
5734 {
5735 /* Matching anything means we continue until end-of-line (or
5736 * end-of-file for ANY + ADD_NL), only limited by maxcount. */
5737 while (*scan != NUL && count < maxcount)
5738 {
5739 ++count;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005740 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 }
Bram Moolenaar640009d2006-10-17 16:48:26 +00005742 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline
5743 || reg_line_lbr || count == maxcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 break;
5745 ++count; /* count the line-break */
5746 reg_nextline();
5747 scan = reginput;
5748 if (got_int)
5749 break;
5750 }
5751 break;
5752
5753 case IDENT:
5754 case IDENT + ADD_NL:
5755 testval = TRUE;
5756 /*FALLTHROUGH*/
5757 case SIDENT:
5758 case SIDENT + ADD_NL:
5759 while (count < maxcount)
5760 {
Bram Moolenaar09ea9fc2013-05-21 00:03:02 +02005761 if (vim_isIDc(PTR2CHAR(scan)) && (testval || !VIM_ISDIGIT(*scan)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005763 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 }
5765 else if (*scan == NUL)
5766 {
Bram Moolenaar640009d2006-10-17 16:48:26 +00005767 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline
5768 || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 break;
5770 reg_nextline();
5771 scan = reginput;
5772 if (got_int)
5773 break;
5774 }
5775 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5776 ++scan;
5777 else
5778 break;
5779 ++count;
5780 }
5781 break;
5782
5783 case KWORD:
5784 case KWORD + ADD_NL:
5785 testval = TRUE;
5786 /*FALLTHROUGH*/
5787 case SKWORD:
5788 case SKWORD + ADD_NL:
5789 while (count < maxcount)
5790 {
Bram Moolenaarf813a182013-01-30 13:59:37 +01005791 if (vim_iswordp_buf(scan, reg_buf)
5792 && (testval || !VIM_ISDIGIT(*scan)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005794 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 }
5796 else if (*scan == NUL)
5797 {
Bram Moolenaar640009d2006-10-17 16:48:26 +00005798 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline
5799 || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 break;
5801 reg_nextline();
5802 scan = reginput;
5803 if (got_int)
5804 break;
5805 }
5806 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5807 ++scan;
5808 else
5809 break;
5810 ++count;
5811 }
5812 break;
5813
5814 case FNAME:
5815 case FNAME + ADD_NL:
5816 testval = TRUE;
5817 /*FALLTHROUGH*/
5818 case SFNAME:
5819 case SFNAME + ADD_NL:
5820 while (count < maxcount)
5821 {
Bram Moolenaar09ea9fc2013-05-21 00:03:02 +02005822 if (vim_isfilec(PTR2CHAR(scan)) && (testval || !VIM_ISDIGIT(*scan)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005824 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 }
5826 else if (*scan == NUL)
5827 {
Bram Moolenaar640009d2006-10-17 16:48:26 +00005828 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline
5829 || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 break;
5831 reg_nextline();
5832 scan = reginput;
5833 if (got_int)
5834 break;
5835 }
5836 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5837 ++scan;
5838 else
5839 break;
5840 ++count;
5841 }
5842 break;
5843
5844 case PRINT:
5845 case PRINT + ADD_NL:
5846 testval = TRUE;
5847 /*FALLTHROUGH*/
5848 case SPRINT:
5849 case SPRINT + ADD_NL:
5850 while (count < maxcount)
5851 {
5852 if (*scan == NUL)
5853 {
Bram Moolenaar640009d2006-10-17 16:48:26 +00005854 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline
5855 || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 break;
5857 reg_nextline();
5858 scan = reginput;
5859 if (got_int)
5860 break;
5861 }
5862 else if (ptr2cells(scan) == 1 && (testval || !VIM_ISDIGIT(*scan)))
5863 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005864 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 }
5866 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5867 ++scan;
5868 else
5869 break;
5870 ++count;
5871 }
5872 break;
5873
5874 case WHITE:
5875 case WHITE + ADD_NL:
5876 testval = mask = RI_WHITE;
5877do_class:
5878 while (count < maxcount)
5879 {
5880#ifdef FEAT_MBYTE
5881 int l;
5882#endif
5883 if (*scan == NUL)
5884 {
Bram Moolenaar640009d2006-10-17 16:48:26 +00005885 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline
5886 || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 break;
5888 reg_nextline();
5889 scan = reginput;
5890 if (got_int)
5891 break;
5892 }
5893#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005894 else if (has_mbyte && (l = (*mb_ptr2len)(scan)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 {
5896 if (testval != 0)
5897 break;
5898 scan += l;
5899 }
5900#endif
5901 else if ((class_tab[*scan] & mask) == testval)
5902 ++scan;
5903 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5904 ++scan;
5905 else
5906 break;
5907 ++count;
5908 }
5909 break;
5910
5911 case NWHITE:
5912 case NWHITE + ADD_NL:
5913 mask = RI_WHITE;
5914 goto do_class;
5915 case DIGIT:
5916 case DIGIT + ADD_NL:
5917 testval = mask = RI_DIGIT;
5918 goto do_class;
5919 case NDIGIT:
5920 case NDIGIT + ADD_NL:
5921 mask = RI_DIGIT;
5922 goto do_class;
5923 case HEX:
5924 case HEX + ADD_NL:
5925 testval = mask = RI_HEX;
5926 goto do_class;
5927 case NHEX:
5928 case NHEX + ADD_NL:
5929 mask = RI_HEX;
5930 goto do_class;
5931 case OCTAL:
5932 case OCTAL + ADD_NL:
5933 testval = mask = RI_OCTAL;
5934 goto do_class;
5935 case NOCTAL:
5936 case NOCTAL + ADD_NL:
5937 mask = RI_OCTAL;
5938 goto do_class;
5939 case WORD:
5940 case WORD + ADD_NL:
5941 testval = mask = RI_WORD;
5942 goto do_class;
5943 case NWORD:
5944 case NWORD + ADD_NL:
5945 mask = RI_WORD;
5946 goto do_class;
5947 case HEAD:
5948 case HEAD + ADD_NL:
5949 testval = mask = RI_HEAD;
5950 goto do_class;
5951 case NHEAD:
5952 case NHEAD + ADD_NL:
5953 mask = RI_HEAD;
5954 goto do_class;
5955 case ALPHA:
5956 case ALPHA + ADD_NL:
5957 testval = mask = RI_ALPHA;
5958 goto do_class;
5959 case NALPHA:
5960 case NALPHA + ADD_NL:
5961 mask = RI_ALPHA;
5962 goto do_class;
5963 case LOWER:
5964 case LOWER + ADD_NL:
5965 testval = mask = RI_LOWER;
5966 goto do_class;
5967 case NLOWER:
5968 case NLOWER + ADD_NL:
5969 mask = RI_LOWER;
5970 goto do_class;
5971 case UPPER:
5972 case UPPER + ADD_NL:
5973 testval = mask = RI_UPPER;
5974 goto do_class;
5975 case NUPPER:
5976 case NUPPER + ADD_NL:
5977 mask = RI_UPPER;
5978 goto do_class;
5979
5980 case EXACTLY:
5981 {
5982 int cu, cl;
5983
5984 /* This doesn't do a multi-byte character, because a MULTIBYTECODE
Bram Moolenaara245a5b2007-08-11 11:58:23 +00005985 * would have been used for it. It does handle single-byte
5986 * characters, such as latin1. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 if (ireg_ic)
5988 {
Bram Moolenaara245a5b2007-08-11 11:58:23 +00005989 cu = MB_TOUPPER(*opnd);
5990 cl = MB_TOLOWER(*opnd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 while (count < maxcount && (*scan == cu || *scan == cl))
5992 {
5993 count++;
5994 scan++;
5995 }
5996 }
5997 else
5998 {
5999 cu = *opnd;
6000 while (count < maxcount && *scan == cu)
6001 {
6002 count++;
6003 scan++;
6004 }
6005 }
6006 break;
6007 }
6008
6009#ifdef FEAT_MBYTE
6010 case MULTIBYTECODE:
6011 {
6012 int i, len, cf = 0;
6013
6014 /* Safety check (just in case 'encoding' was changed since
6015 * compiling the program). */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006016 if ((len = (*mb_ptr2len)(opnd)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 {
6018 if (ireg_ic && enc_utf8)
6019 cf = utf_fold(utf_ptr2char(opnd));
6020 while (count < maxcount)
6021 {
6022 for (i = 0; i < len; ++i)
6023 if (opnd[i] != scan[i])
6024 break;
6025 if (i < len && (!ireg_ic || !enc_utf8
6026 || utf_fold(utf_ptr2char(scan)) != cf))
6027 break;
6028 scan += len;
6029 ++count;
6030 }
6031 }
6032 }
6033 break;
6034#endif
6035
6036 case ANYOF:
6037 case ANYOF + ADD_NL:
6038 testval = TRUE;
6039 /*FALLTHROUGH*/
6040
6041 case ANYBUT:
6042 case ANYBUT + ADD_NL:
6043 while (count < maxcount)
6044 {
6045#ifdef FEAT_MBYTE
6046 int len;
6047#endif
6048 if (*scan == NUL)
6049 {
Bram Moolenaar640009d2006-10-17 16:48:26 +00006050 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline
6051 || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 break;
6053 reg_nextline();
6054 scan = reginput;
6055 if (got_int)
6056 break;
6057 }
6058 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
6059 ++scan;
6060#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006061 else if (has_mbyte && (len = (*mb_ptr2len)(scan)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 {
6063 if ((cstrchr(opnd, (*mb_ptr2char)(scan)) == NULL) == testval)
6064 break;
6065 scan += len;
6066 }
6067#endif
6068 else
6069 {
6070 if ((cstrchr(opnd, *scan) == NULL) == testval)
6071 break;
6072 ++scan;
6073 }
6074 ++count;
6075 }
6076 break;
6077
6078 case NEWL:
6079 while (count < maxcount
Bram Moolenaar640009d2006-10-17 16:48:26 +00006080 && ((*scan == NUL && reglnum <= reg_maxline && !reg_line_lbr
6081 && REG_MULTI) || (*scan == '\n' && reg_line_lbr)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 {
6083 count++;
6084 if (reg_line_lbr)
6085 ADVANCE_REGINPUT();
6086 else
6087 reg_nextline();
6088 scan = reginput;
6089 if (got_int)
6090 break;
6091 }
6092 break;
6093
6094 default: /* Oh dear. Called inappropriately. */
6095 EMSG(_(e_re_corr));
6096#ifdef DEBUG
6097 printf("Called regrepeat with op code %d\n", OP(p));
6098#endif
6099 break;
6100 }
6101
6102 reginput = scan;
6103
6104 return (int)count;
6105}
6106
6107/*
6108 * regnext - dig the "next" pointer out of a node
Bram Moolenaard3005802009-11-25 17:21:32 +00006109 * Returns NULL when calculating size, when there is no next item and when
6110 * there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 */
6112 static char_u *
6113regnext(p)
6114 char_u *p;
6115{
6116 int offset;
6117
Bram Moolenaard3005802009-11-25 17:21:32 +00006118 if (p == JUST_CALC_SIZE || reg_toolong)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 return NULL;
6120
6121 offset = NEXT(p);
6122 if (offset == 0)
6123 return NULL;
6124
Bram Moolenaar582fd852005-03-28 20:58:01 +00006125 if (OP(p) == BACK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 return p - offset;
6127 else
6128 return p + offset;
6129}
6130
6131/*
6132 * Check the regexp program for its magic number.
6133 * Return TRUE if it's wrong.
6134 */
6135 static int
6136prog_magic_wrong()
6137{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006138 regprog_T *prog;
6139
6140 prog = REG_MULTI ? reg_mmatch->regprog : reg_match->regprog;
6141 if (prog->engine == &nfa_regengine)
6142 /* For NFA matcher we don't check the magic */
6143 return FALSE;
6144
6145 if (UCHARAT(((bt_regprog_T *)prog)->program) != REGMAGIC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 {
6147 EMSG(_(e_re_corr));
6148 return TRUE;
6149 }
6150 return FALSE;
6151}
6152
6153/*
6154 * Cleanup the subexpressions, if this wasn't done yet.
6155 * This construction is used to clear the subexpressions only when they are
6156 * used (to increase speed).
6157 */
6158 static void
6159cleanup_subexpr()
6160{
6161 if (need_clear_subexpr)
6162 {
6163 if (REG_MULTI)
6164 {
6165 /* Use 0xff to set lnum to -1 */
6166 vim_memset(reg_startpos, 0xff, sizeof(lpos_T) * NSUBEXP);
6167 vim_memset(reg_endpos, 0xff, sizeof(lpos_T) * NSUBEXP);
6168 }
6169 else
6170 {
6171 vim_memset(reg_startp, 0, sizeof(char_u *) * NSUBEXP);
6172 vim_memset(reg_endp, 0, sizeof(char_u *) * NSUBEXP);
6173 }
6174 need_clear_subexpr = FALSE;
6175 }
6176}
6177
6178#ifdef FEAT_SYN_HL
6179 static void
6180cleanup_zsubexpr()
6181{
6182 if (need_clear_zsubexpr)
6183 {
6184 if (REG_MULTI)
6185 {
6186 /* Use 0xff to set lnum to -1 */
6187 vim_memset(reg_startzpos, 0xff, sizeof(lpos_T) * NSUBEXP);
6188 vim_memset(reg_endzpos, 0xff, sizeof(lpos_T) * NSUBEXP);
6189 }
6190 else
6191 {
6192 vim_memset(reg_startzp, 0, sizeof(char_u *) * NSUBEXP);
6193 vim_memset(reg_endzp, 0, sizeof(char_u *) * NSUBEXP);
6194 }
6195 need_clear_zsubexpr = FALSE;
6196 }
6197}
6198#endif
6199
6200/*
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006201 * Save the current subexpr to "bp", so that they can be restored
6202 * later by restore_subexpr().
6203 */
6204 static void
6205save_subexpr(bp)
6206 regbehind_T *bp;
6207{
6208 int i;
6209
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006210 /* When "need_clear_subexpr" is set we don't need to save the values, only
6211 * remember that this flag needs to be set again when restoring. */
6212 bp->save_need_clear_subexpr = need_clear_subexpr;
6213 if (!need_clear_subexpr)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006214 {
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006215 for (i = 0; i < NSUBEXP; ++i)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006216 {
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006217 if (REG_MULTI)
6218 {
6219 bp->save_start[i].se_u.pos = reg_startpos[i];
6220 bp->save_end[i].se_u.pos = reg_endpos[i];
6221 }
6222 else
6223 {
6224 bp->save_start[i].se_u.ptr = reg_startp[i];
6225 bp->save_end[i].se_u.ptr = reg_endp[i];
6226 }
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006227 }
6228 }
6229}
6230
6231/*
6232 * Restore the subexpr from "bp".
6233 */
6234 static void
6235restore_subexpr(bp)
6236 regbehind_T *bp;
6237{
6238 int i;
6239
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006240 /* Only need to restore saved values when they are not to be cleared. */
6241 need_clear_subexpr = bp->save_need_clear_subexpr;
6242 if (!need_clear_subexpr)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006243 {
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006244 for (i = 0; i < NSUBEXP; ++i)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006245 {
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006246 if (REG_MULTI)
6247 {
6248 reg_startpos[i] = bp->save_start[i].se_u.pos;
6249 reg_endpos[i] = bp->save_end[i].se_u.pos;
6250 }
6251 else
6252 {
6253 reg_startp[i] = bp->save_start[i].se_u.ptr;
6254 reg_endp[i] = bp->save_end[i].se_u.ptr;
6255 }
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006256 }
6257 }
6258}
6259
6260/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 * Advance reglnum, regline and reginput to the next line.
6262 */
6263 static void
6264reg_nextline()
6265{
6266 regline = reg_getline(++reglnum);
6267 reginput = regline;
6268 fast_breakcheck();
6269}
6270
6271/*
6272 * Save the input line and position in a regsave_T.
6273 */
6274 static void
Bram Moolenaar582fd852005-03-28 20:58:01 +00006275reg_save(save, gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 regsave_T *save;
Bram Moolenaar582fd852005-03-28 20:58:01 +00006277 garray_T *gap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278{
6279 if (REG_MULTI)
6280 {
6281 save->rs_u.pos.col = (colnr_T)(reginput - regline);
6282 save->rs_u.pos.lnum = reglnum;
6283 }
6284 else
6285 save->rs_u.ptr = reginput;
Bram Moolenaar582fd852005-03-28 20:58:01 +00006286 save->rs_len = gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287}
6288
6289/*
6290 * Restore the input line and position from a regsave_T.
6291 */
6292 static void
Bram Moolenaar582fd852005-03-28 20:58:01 +00006293reg_restore(save, gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 regsave_T *save;
Bram Moolenaar582fd852005-03-28 20:58:01 +00006295 garray_T *gap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296{
6297 if (REG_MULTI)
6298 {
6299 if (reglnum != save->rs_u.pos.lnum)
6300 {
6301 /* only call reg_getline() when the line number changed to save
6302 * a bit of time */
6303 reglnum = save->rs_u.pos.lnum;
6304 regline = reg_getline(reglnum);
6305 }
6306 reginput = regline + save->rs_u.pos.col;
6307 }
6308 else
6309 reginput = save->rs_u.ptr;
Bram Moolenaar582fd852005-03-28 20:58:01 +00006310 gap->ga_len = save->rs_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311}
6312
6313/*
6314 * Return TRUE if current position is equal to saved position.
6315 */
6316 static int
6317reg_save_equal(save)
6318 regsave_T *save;
6319{
6320 if (REG_MULTI)
6321 return reglnum == save->rs_u.pos.lnum
6322 && reginput == regline + save->rs_u.pos.col;
6323 return reginput == save->rs_u.ptr;
6324}
6325
6326/*
6327 * Tentatively set the sub-expression start to the current position (after
6328 * calling regmatch() they will have changed). Need to save the existing
6329 * values for when there is no match.
6330 * Use se_save() to use pointer (save_se_multi()) or position (save_se_one()),
6331 * depending on REG_MULTI.
6332 */
6333 static void
6334save_se_multi(savep, posp)
6335 save_se_T *savep;
6336 lpos_T *posp;
6337{
6338 savep->se_u.pos = *posp;
6339 posp->lnum = reglnum;
6340 posp->col = (colnr_T)(reginput - regline);
6341}
6342
6343 static void
6344save_se_one(savep, pp)
6345 save_se_T *savep;
6346 char_u **pp;
6347{
6348 savep->se_u.ptr = *pp;
6349 *pp = reginput;
6350}
6351
6352/*
6353 * Compare a number with the operand of RE_LNUM, RE_COL or RE_VCOL.
6354 */
6355 static int
6356re_num_cmp(val, scan)
6357 long_u val;
6358 char_u *scan;
6359{
6360 long_u n = OPERAND_MIN(scan);
6361
6362 if (OPERAND_CMP(scan) == '>')
6363 return val > n;
6364 if (OPERAND_CMP(scan) == '<')
6365 return val < n;
6366 return val == n;
6367}
6368
6369
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006370#ifdef BT_REGEXP_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371
6372/*
6373 * regdump - dump a regexp onto stdout in vaguely comprehensible form
6374 */
6375 static void
6376regdump(pattern, r)
6377 char_u *pattern;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006378 bt_regprog_T *r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379{
6380 char_u *s;
6381 int op = EXACTLY; /* Arbitrary non-END op. */
6382 char_u *next;
6383 char_u *end = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006384 FILE *f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006386#ifdef BT_REGEXP_LOG
6387 f = fopen("bt_regexp_log.log", "a");
6388#else
6389 f = stdout;
6390#endif
6391 if (f == NULL)
6392 return;
6393 fprintf(f, "-------------------------------------\n\r\nregcomp(%s):\r\n", pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394
6395 s = r->program + 1;
6396 /*
6397 * Loop until we find the END that isn't before a referred next (an END
6398 * can also appear in a NOMATCH operand).
6399 */
6400 while (op != END || s <= end)
6401 {
6402 op = OP(s);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006403 fprintf(f, "%2d%s", (int)(s - r->program), regprop(s)); /* Where, what. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 next = regnext(s);
6405 if (next == NULL) /* Next ptr. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006406 fprintf(f, "(0)");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006408 fprintf(f, "(%d)", (int)((s - r->program) + (next - s)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 if (end < next)
6410 end = next;
6411 if (op == BRACE_LIMITS)
6412 {
6413 /* Two short ints */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006414 fprintf(f, " minval %ld, maxval %ld", OPERAND_MIN(s), OPERAND_MAX(s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 s += 8;
6416 }
6417 s += 3;
6418 if (op == ANYOF || op == ANYOF + ADD_NL
6419 || op == ANYBUT || op == ANYBUT + ADD_NL
6420 || op == EXACTLY)
6421 {
6422 /* Literal string, where present. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006423 fprintf(f, "\nxxxxxxxxx\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 while (*s != NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006425 fprintf(f, "%c", *s++);
6426 fprintf(f, "\nxxxxxxxxx\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 s++;
6428 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006429 fprintf(f, "\r\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 }
6431
6432 /* Header fields of interest. */
6433 if (r->regstart != NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006434 fprintf(f, "start `%s' 0x%x; ", r->regstart < 256
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 ? (char *)transchar(r->regstart)
6436 : "multibyte", r->regstart);
6437 if (r->reganch)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006438 fprintf(f, "anchored; ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 if (r->regmust != NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006440 fprintf(f, "must have \"%s\"", r->regmust);
6441 fprintf(f, "\r\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006443#ifdef BT_REGEXP_LOG
6444 fclose(f);
6445#endif
6446}
6447#endif /* BT_REGEXP_DUMP */
6448
6449#ifdef DEBUG
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450/*
6451 * regprop - printable representation of opcode
6452 */
6453 static char_u *
6454regprop(op)
6455 char_u *op;
6456{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006457 char *p;
6458 static char buf[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006460 STRCPY(buf, ":");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006462 switch ((int) OP(op))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 {
6464 case BOL:
6465 p = "BOL";
6466 break;
6467 case EOL:
6468 p = "EOL";
6469 break;
6470 case RE_BOF:
6471 p = "BOF";
6472 break;
6473 case RE_EOF:
6474 p = "EOF";
6475 break;
6476 case CURSOR:
6477 p = "CURSOR";
6478 break;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00006479 case RE_VISUAL:
6480 p = "RE_VISUAL";
6481 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 case RE_LNUM:
6483 p = "RE_LNUM";
6484 break;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00006485 case RE_MARK:
6486 p = "RE_MARK";
6487 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 case RE_COL:
6489 p = "RE_COL";
6490 break;
6491 case RE_VCOL:
6492 p = "RE_VCOL";
6493 break;
6494 case BOW:
6495 p = "BOW";
6496 break;
6497 case EOW:
6498 p = "EOW";
6499 break;
6500 case ANY:
6501 p = "ANY";
6502 break;
6503 case ANY + ADD_NL:
6504 p = "ANY+NL";
6505 break;
6506 case ANYOF:
6507 p = "ANYOF";
6508 break;
6509 case ANYOF + ADD_NL:
6510 p = "ANYOF+NL";
6511 break;
6512 case ANYBUT:
6513 p = "ANYBUT";
6514 break;
6515 case ANYBUT + ADD_NL:
6516 p = "ANYBUT+NL";
6517 break;
6518 case IDENT:
6519 p = "IDENT";
6520 break;
6521 case IDENT + ADD_NL:
6522 p = "IDENT+NL";
6523 break;
6524 case SIDENT:
6525 p = "SIDENT";
6526 break;
6527 case SIDENT + ADD_NL:
6528 p = "SIDENT+NL";
6529 break;
6530 case KWORD:
6531 p = "KWORD";
6532 break;
6533 case KWORD + ADD_NL:
6534 p = "KWORD+NL";
6535 break;
6536 case SKWORD:
6537 p = "SKWORD";
6538 break;
6539 case SKWORD + ADD_NL:
6540 p = "SKWORD+NL";
6541 break;
6542 case FNAME:
6543 p = "FNAME";
6544 break;
6545 case FNAME + ADD_NL:
6546 p = "FNAME+NL";
6547 break;
6548 case SFNAME:
6549 p = "SFNAME";
6550 break;
6551 case SFNAME + ADD_NL:
6552 p = "SFNAME+NL";
6553 break;
6554 case PRINT:
6555 p = "PRINT";
6556 break;
6557 case PRINT + ADD_NL:
6558 p = "PRINT+NL";
6559 break;
6560 case SPRINT:
6561 p = "SPRINT";
6562 break;
6563 case SPRINT + ADD_NL:
6564 p = "SPRINT+NL";
6565 break;
6566 case WHITE:
6567 p = "WHITE";
6568 break;
6569 case WHITE + ADD_NL:
6570 p = "WHITE+NL";
6571 break;
6572 case NWHITE:
6573 p = "NWHITE";
6574 break;
6575 case NWHITE + ADD_NL:
6576 p = "NWHITE+NL";
6577 break;
6578 case DIGIT:
6579 p = "DIGIT";
6580 break;
6581 case DIGIT + ADD_NL:
6582 p = "DIGIT+NL";
6583 break;
6584 case NDIGIT:
6585 p = "NDIGIT";
6586 break;
6587 case NDIGIT + ADD_NL:
6588 p = "NDIGIT+NL";
6589 break;
6590 case HEX:
6591 p = "HEX";
6592 break;
6593 case HEX + ADD_NL:
6594 p = "HEX+NL";
6595 break;
6596 case NHEX:
6597 p = "NHEX";
6598 break;
6599 case NHEX + ADD_NL:
6600 p = "NHEX+NL";
6601 break;
6602 case OCTAL:
6603 p = "OCTAL";
6604 break;
6605 case OCTAL + ADD_NL:
6606 p = "OCTAL+NL";
6607 break;
6608 case NOCTAL:
6609 p = "NOCTAL";
6610 break;
6611 case NOCTAL + ADD_NL:
6612 p = "NOCTAL+NL";
6613 break;
6614 case WORD:
6615 p = "WORD";
6616 break;
6617 case WORD + ADD_NL:
6618 p = "WORD+NL";
6619 break;
6620 case NWORD:
6621 p = "NWORD";
6622 break;
6623 case NWORD + ADD_NL:
6624 p = "NWORD+NL";
6625 break;
6626 case HEAD:
6627 p = "HEAD";
6628 break;
6629 case HEAD + ADD_NL:
6630 p = "HEAD+NL";
6631 break;
6632 case NHEAD:
6633 p = "NHEAD";
6634 break;
6635 case NHEAD + ADD_NL:
6636 p = "NHEAD+NL";
6637 break;
6638 case ALPHA:
6639 p = "ALPHA";
6640 break;
6641 case ALPHA + ADD_NL:
6642 p = "ALPHA+NL";
6643 break;
6644 case NALPHA:
6645 p = "NALPHA";
6646 break;
6647 case NALPHA + ADD_NL:
6648 p = "NALPHA+NL";
6649 break;
6650 case LOWER:
6651 p = "LOWER";
6652 break;
6653 case LOWER + ADD_NL:
6654 p = "LOWER+NL";
6655 break;
6656 case NLOWER:
6657 p = "NLOWER";
6658 break;
6659 case NLOWER + ADD_NL:
6660 p = "NLOWER+NL";
6661 break;
6662 case UPPER:
6663 p = "UPPER";
6664 break;
6665 case UPPER + ADD_NL:
6666 p = "UPPER+NL";
6667 break;
6668 case NUPPER:
6669 p = "NUPPER";
6670 break;
6671 case NUPPER + ADD_NL:
6672 p = "NUPPER+NL";
6673 break;
6674 case BRANCH:
6675 p = "BRANCH";
6676 break;
6677 case EXACTLY:
6678 p = "EXACTLY";
6679 break;
6680 case NOTHING:
6681 p = "NOTHING";
6682 break;
6683 case BACK:
6684 p = "BACK";
6685 break;
6686 case END:
6687 p = "END";
6688 break;
6689 case MOPEN + 0:
6690 p = "MATCH START";
6691 break;
6692 case MOPEN + 1:
6693 case MOPEN + 2:
6694 case MOPEN + 3:
6695 case MOPEN + 4:
6696 case MOPEN + 5:
6697 case MOPEN + 6:
6698 case MOPEN + 7:
6699 case MOPEN + 8:
6700 case MOPEN + 9:
6701 sprintf(buf + STRLEN(buf), "MOPEN%d", OP(op) - MOPEN);
6702 p = NULL;
6703 break;
6704 case MCLOSE + 0:
6705 p = "MATCH END";
6706 break;
6707 case MCLOSE + 1:
6708 case MCLOSE + 2:
6709 case MCLOSE + 3:
6710 case MCLOSE + 4:
6711 case MCLOSE + 5:
6712 case MCLOSE + 6:
6713 case MCLOSE + 7:
6714 case MCLOSE + 8:
6715 case MCLOSE + 9:
6716 sprintf(buf + STRLEN(buf), "MCLOSE%d", OP(op) - MCLOSE);
6717 p = NULL;
6718 break;
6719 case BACKREF + 1:
6720 case BACKREF + 2:
6721 case BACKREF + 3:
6722 case BACKREF + 4:
6723 case BACKREF + 5:
6724 case BACKREF + 6:
6725 case BACKREF + 7:
6726 case BACKREF + 8:
6727 case BACKREF + 9:
6728 sprintf(buf + STRLEN(buf), "BACKREF%d", OP(op) - BACKREF);
6729 p = NULL;
6730 break;
6731 case NOPEN:
6732 p = "NOPEN";
6733 break;
6734 case NCLOSE:
6735 p = "NCLOSE";
6736 break;
6737#ifdef FEAT_SYN_HL
6738 case ZOPEN + 1:
6739 case ZOPEN + 2:
6740 case ZOPEN + 3:
6741 case ZOPEN + 4:
6742 case ZOPEN + 5:
6743 case ZOPEN + 6:
6744 case ZOPEN + 7:
6745 case ZOPEN + 8:
6746 case ZOPEN + 9:
6747 sprintf(buf + STRLEN(buf), "ZOPEN%d", OP(op) - ZOPEN);
6748 p = NULL;
6749 break;
6750 case ZCLOSE + 1:
6751 case ZCLOSE + 2:
6752 case ZCLOSE + 3:
6753 case ZCLOSE + 4:
6754 case ZCLOSE + 5:
6755 case ZCLOSE + 6:
6756 case ZCLOSE + 7:
6757 case ZCLOSE + 8:
6758 case ZCLOSE + 9:
6759 sprintf(buf + STRLEN(buf), "ZCLOSE%d", OP(op) - ZCLOSE);
6760 p = NULL;
6761 break;
6762 case ZREF + 1:
6763 case ZREF + 2:
6764 case ZREF + 3:
6765 case ZREF + 4:
6766 case ZREF + 5:
6767 case ZREF + 6:
6768 case ZREF + 7:
6769 case ZREF + 8:
6770 case ZREF + 9:
6771 sprintf(buf + STRLEN(buf), "ZREF%d", OP(op) - ZREF);
6772 p = NULL;
6773 break;
6774#endif
6775 case STAR:
6776 p = "STAR";
6777 break;
6778 case PLUS:
6779 p = "PLUS";
6780 break;
6781 case NOMATCH:
6782 p = "NOMATCH";
6783 break;
6784 case MATCH:
6785 p = "MATCH";
6786 break;
6787 case BEHIND:
6788 p = "BEHIND";
6789 break;
6790 case NOBEHIND:
6791 p = "NOBEHIND";
6792 break;
6793 case SUBPAT:
6794 p = "SUBPAT";
6795 break;
6796 case BRACE_LIMITS:
6797 p = "BRACE_LIMITS";
6798 break;
6799 case BRACE_SIMPLE:
6800 p = "BRACE_SIMPLE";
6801 break;
6802 case BRACE_COMPLEX + 0:
6803 case BRACE_COMPLEX + 1:
6804 case BRACE_COMPLEX + 2:
6805 case BRACE_COMPLEX + 3:
6806 case BRACE_COMPLEX + 4:
6807 case BRACE_COMPLEX + 5:
6808 case BRACE_COMPLEX + 6:
6809 case BRACE_COMPLEX + 7:
6810 case BRACE_COMPLEX + 8:
6811 case BRACE_COMPLEX + 9:
6812 sprintf(buf + STRLEN(buf), "BRACE_COMPLEX%d", OP(op) - BRACE_COMPLEX);
6813 p = NULL;
6814 break;
6815#ifdef FEAT_MBYTE
6816 case MULTIBYTECODE:
6817 p = "MULTIBYTECODE";
6818 break;
6819#endif
6820 case NEWL:
6821 p = "NEWL";
6822 break;
6823 default:
6824 sprintf(buf + STRLEN(buf), "corrupt %d", OP(op));
6825 p = NULL;
6826 break;
6827 }
6828 if (p != NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006829 STRCAT(buf, p);
6830 return (char_u *)buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006832#endif /* DEBUG */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833
6834#ifdef FEAT_MBYTE
6835static void mb_decompose __ARGS((int c, int *c1, int *c2, int *c3));
6836
6837typedef struct
6838{
6839 int a, b, c;
6840} decomp_T;
6841
6842
6843/* 0xfb20 - 0xfb4f */
Bram Moolenaard6f676d2005-06-01 21:51:55 +00006844static decomp_T decomp_table[0xfb4f-0xfb20+1] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845{
6846 {0x5e2,0,0}, /* 0xfb20 alt ayin */
6847 {0x5d0,0,0}, /* 0xfb21 alt alef */
6848 {0x5d3,0,0}, /* 0xfb22 alt dalet */
6849 {0x5d4,0,0}, /* 0xfb23 alt he */
6850 {0x5db,0,0}, /* 0xfb24 alt kaf */
6851 {0x5dc,0,0}, /* 0xfb25 alt lamed */
6852 {0x5dd,0,0}, /* 0xfb26 alt mem-sofit */
6853 {0x5e8,0,0}, /* 0xfb27 alt resh */
6854 {0x5ea,0,0}, /* 0xfb28 alt tav */
6855 {'+', 0, 0}, /* 0xfb29 alt plus */
6856 {0x5e9, 0x5c1, 0}, /* 0xfb2a shin+shin-dot */
6857 {0x5e9, 0x5c2, 0}, /* 0xfb2b shin+sin-dot */
6858 {0x5e9, 0x5c1, 0x5bc}, /* 0xfb2c shin+shin-dot+dagesh */
6859 {0x5e9, 0x5c2, 0x5bc}, /* 0xfb2d shin+sin-dot+dagesh */
6860 {0x5d0, 0x5b7, 0}, /* 0xfb2e alef+patah */
6861 {0x5d0, 0x5b8, 0}, /* 0xfb2f alef+qamats */
6862 {0x5d0, 0x5b4, 0}, /* 0xfb30 alef+hiriq */
6863 {0x5d1, 0x5bc, 0}, /* 0xfb31 bet+dagesh */
6864 {0x5d2, 0x5bc, 0}, /* 0xfb32 gimel+dagesh */
6865 {0x5d3, 0x5bc, 0}, /* 0xfb33 dalet+dagesh */
6866 {0x5d4, 0x5bc, 0}, /* 0xfb34 he+dagesh */
6867 {0x5d5, 0x5bc, 0}, /* 0xfb35 vav+dagesh */
6868 {0x5d6, 0x5bc, 0}, /* 0xfb36 zayin+dagesh */
6869 {0xfb37, 0, 0}, /* 0xfb37 -- UNUSED */
6870 {0x5d8, 0x5bc, 0}, /* 0xfb38 tet+dagesh */
6871 {0x5d9, 0x5bc, 0}, /* 0xfb39 yud+dagesh */
6872 {0x5da, 0x5bc, 0}, /* 0xfb3a kaf sofit+dagesh */
6873 {0x5db, 0x5bc, 0}, /* 0xfb3b kaf+dagesh */
6874 {0x5dc, 0x5bc, 0}, /* 0xfb3c lamed+dagesh */
6875 {0xfb3d, 0, 0}, /* 0xfb3d -- UNUSED */
6876 {0x5de, 0x5bc, 0}, /* 0xfb3e mem+dagesh */
6877 {0xfb3f, 0, 0}, /* 0xfb3f -- UNUSED */
6878 {0x5e0, 0x5bc, 0}, /* 0xfb40 nun+dagesh */
6879 {0x5e1, 0x5bc, 0}, /* 0xfb41 samech+dagesh */
6880 {0xfb42, 0, 0}, /* 0xfb42 -- UNUSED */
6881 {0x5e3, 0x5bc, 0}, /* 0xfb43 pe sofit+dagesh */
6882 {0x5e4, 0x5bc,0}, /* 0xfb44 pe+dagesh */
6883 {0xfb45, 0, 0}, /* 0xfb45 -- UNUSED */
6884 {0x5e6, 0x5bc, 0}, /* 0xfb46 tsadi+dagesh */
6885 {0x5e7, 0x5bc, 0}, /* 0xfb47 qof+dagesh */
6886 {0x5e8, 0x5bc, 0}, /* 0xfb48 resh+dagesh */
6887 {0x5e9, 0x5bc, 0}, /* 0xfb49 shin+dagesh */
6888 {0x5ea, 0x5bc, 0}, /* 0xfb4a tav+dagesh */
6889 {0x5d5, 0x5b9, 0}, /* 0xfb4b vav+holam */
6890 {0x5d1, 0x5bf, 0}, /* 0xfb4c bet+rafe */
6891 {0x5db, 0x5bf, 0}, /* 0xfb4d kaf+rafe */
6892 {0x5e4, 0x5bf, 0}, /* 0xfb4e pe+rafe */
6893 {0x5d0, 0x5dc, 0} /* 0xfb4f alef-lamed */
6894};
6895
6896 static void
6897mb_decompose(c, c1, c2, c3)
6898 int c, *c1, *c2, *c3;
6899{
6900 decomp_T d;
6901
6902 if (c >= 0x4b20 && c <= 0xfb4f)
6903 {
6904 d = decomp_table[c - 0xfb20];
6905 *c1 = d.a;
6906 *c2 = d.b;
6907 *c3 = d.c;
6908 }
6909 else
6910 {
6911 *c1 = c;
6912 *c2 = *c3 = 0;
6913 }
6914}
6915#endif
6916
6917/*
6918 * Compare two strings, ignore case if ireg_ic set.
6919 * Return 0 if strings match, non-zero otherwise.
6920 * Correct the length "*n" when composing characters are ignored.
6921 */
6922 static int
6923cstrncmp(s1, s2, n)
6924 char_u *s1, *s2;
6925 int *n;
6926{
6927 int result;
6928
6929 if (!ireg_ic)
6930 result = STRNCMP(s1, s2, *n);
6931 else
6932 result = MB_STRNICMP(s1, s2, *n);
6933
6934#ifdef FEAT_MBYTE
6935 /* if it failed and it's utf8 and we want to combineignore: */
6936 if (result != 0 && enc_utf8 && ireg_icombine)
6937 {
6938 char_u *str1, *str2;
6939 int c1, c2, c11, c12;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 int junk;
6941
6942 /* we have to handle the strcmp ourselves, since it is necessary to
6943 * deal with the composing characters by ignoring them: */
6944 str1 = s1;
6945 str2 = s2;
6946 c1 = c2 = 0;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006947 while ((int)(str1 - s1) < *n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 {
6949 c1 = mb_ptr2char_adv(&str1);
6950 c2 = mb_ptr2char_adv(&str2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951
6952 /* decompose the character if necessary, into 'base' characters
6953 * because I don't care about Arabic, I will hard-code the Hebrew
6954 * which I *do* care about! So sue me... */
6955 if (c1 != c2 && (!ireg_ic || utf_fold(c1) != utf_fold(c2)))
6956 {
6957 /* decomposition necessary? */
6958 mb_decompose(c1, &c11, &junk, &junk);
6959 mb_decompose(c2, &c12, &junk, &junk);
6960 c1 = c11;
6961 c2 = c12;
6962 if (c11 != c12 && (!ireg_ic || utf_fold(c11) != utf_fold(c12)))
6963 break;
6964 }
6965 }
6966 result = c2 - c1;
6967 if (result == 0)
6968 *n = (int)(str2 - s2);
6969 }
6970#endif
6971
6972 return result;
6973}
6974
6975/*
6976 * cstrchr: This function is used a lot for simple searches, keep it fast!
6977 */
6978 static char_u *
6979cstrchr(s, c)
6980 char_u *s;
6981 int c;
6982{
6983 char_u *p;
6984 int cc;
6985
6986 if (!ireg_ic
6987#ifdef FEAT_MBYTE
6988 || (!enc_utf8 && mb_char2len(c) > 1)
6989#endif
6990 )
6991 return vim_strchr(s, c);
6992
6993 /* tolower() and toupper() can be slow, comparing twice should be a lot
6994 * faster (esp. when using MS Visual C++!).
6995 * For UTF-8 need to use folded case. */
6996#ifdef FEAT_MBYTE
6997 if (enc_utf8 && c > 0x80)
6998 cc = utf_fold(c);
6999 else
7000#endif
Bram Moolenaara245a5b2007-08-11 11:58:23 +00007001 if (MB_ISUPPER(c))
7002 cc = MB_TOLOWER(c);
7003 else if (MB_ISLOWER(c))
7004 cc = MB_TOUPPER(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 else
7006 return vim_strchr(s, c);
7007
7008#ifdef FEAT_MBYTE
7009 if (has_mbyte)
7010 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007011 for (p = s; *p != NUL; p += (*mb_ptr2len)(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 {
7013 if (enc_utf8 && c > 0x80)
7014 {
7015 if (utf_fold(utf_ptr2char(p)) == cc)
7016 return p;
7017 }
7018 else if (*p == c || *p == cc)
7019 return p;
7020 }
7021 }
7022 else
7023#endif
7024 /* Faster version for when there are no multi-byte characters. */
7025 for (p = s; *p != NUL; ++p)
7026 if (*p == c || *p == cc)
7027 return p;
7028
7029 return NULL;
7030}
7031
7032/***************************************************************
7033 * regsub stuff *
7034 ***************************************************************/
7035
7036/* This stuff below really confuses cc on an SGI -- webb */
7037#ifdef __sgi
7038# undef __ARGS
7039# define __ARGS(x) ()
7040#endif
7041
7042/*
7043 * We should define ftpr as a pointer to a function returning a pointer to
7044 * a function returning a pointer to a function ...
7045 * This is impossible, so we declare a pointer to a function returning a
7046 * pointer to a function returning void. This should work for all compilers.
7047 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007048typedef void (*(*fptr_T) __ARGS((int *, int)))();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007050static fptr_T do_upper __ARGS((int *, int));
7051static fptr_T do_Upper __ARGS((int *, int));
7052static fptr_T do_lower __ARGS((int *, int));
7053static fptr_T do_Lower __ARGS((int *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054
7055static int vim_regsub_both __ARGS((char_u *source, char_u *dest, int copy, int magic, int backslash));
7056
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007057 static fptr_T
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058do_upper(d, c)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007059 int *d;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 int c;
7061{
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007062 *d = MB_TOUPPER(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007064 return (fptr_T)NULL;
7065}
7066
7067 static fptr_T
7068do_Upper(d, c)
7069 int *d;
7070 int c;
7071{
7072 *d = MB_TOUPPER(c);
7073
7074 return (fptr_T)do_Upper;
7075}
7076
7077 static fptr_T
7078do_lower(d, c)
7079 int *d;
7080 int c;
7081{
7082 *d = MB_TOLOWER(c);
7083
7084 return (fptr_T)NULL;
7085}
7086
7087 static fptr_T
7088do_Lower(d, c)
7089 int *d;
7090 int c;
7091{
7092 *d = MB_TOLOWER(c);
7093
7094 return (fptr_T)do_Lower;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095}
7096
7097/*
7098 * regtilde(): Replace tildes in the pattern by the old pattern.
7099 *
7100 * Short explanation of the tilde: It stands for the previous replacement
7101 * pattern. If that previous pattern also contains a ~ we should go back a
7102 * step further... But we insert the previous pattern into the current one
7103 * and remember that.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007104 * This still does not handle the case where "magic" changes. So require the
7105 * user to keep his hands off of "magic".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 *
7107 * The tildes are parsed once before the first call to vim_regsub().
7108 */
7109 char_u *
7110regtilde(source, magic)
7111 char_u *source;
7112 int magic;
7113{
7114 char_u *newsub = source;
7115 char_u *tmpsub;
7116 char_u *p;
7117 int len;
7118 int prevlen;
7119
7120 for (p = newsub; *p; ++p)
7121 {
7122 if ((*p == '~' && magic) || (*p == '\\' && *(p + 1) == '~' && !magic))
7123 {
7124 if (reg_prev_sub != NULL)
7125 {
7126 /* length = len(newsub) - 1 + len(prev_sub) + 1 */
7127 prevlen = (int)STRLEN(reg_prev_sub);
7128 tmpsub = alloc((unsigned)(STRLEN(newsub) + prevlen));
7129 if (tmpsub != NULL)
7130 {
7131 /* copy prefix */
7132 len = (int)(p - newsub); /* not including ~ */
7133 mch_memmove(tmpsub, newsub, (size_t)len);
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00007134 /* interpret tilde */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 mch_memmove(tmpsub + len, reg_prev_sub, (size_t)prevlen);
7136 /* copy postfix */
7137 if (!magic)
7138 ++p; /* back off \ */
7139 STRCPY(tmpsub + len + prevlen, p + 1);
7140
7141 if (newsub != source) /* already allocated newsub */
7142 vim_free(newsub);
7143 newsub = tmpsub;
7144 p = newsub + len + prevlen;
7145 }
7146 }
7147 else if (magic)
Bram Moolenaar446cb832008-06-24 21:56:24 +00007148 STRMOVE(p, p + 1); /* remove '~' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 else
Bram Moolenaar446cb832008-06-24 21:56:24 +00007150 STRMOVE(p, p + 2); /* remove '\~' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 --p;
7152 }
7153 else
7154 {
7155 if (*p == '\\' && p[1]) /* skip escaped characters */
7156 ++p;
7157#ifdef FEAT_MBYTE
7158 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007159 p += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160#endif
7161 }
7162 }
7163
7164 vim_free(reg_prev_sub);
7165 if (newsub != source) /* newsub was allocated, just keep it */
7166 reg_prev_sub = newsub;
7167 else /* no ~ found, need to save newsub */
7168 reg_prev_sub = vim_strsave(newsub);
7169 return newsub;
7170}
7171
7172#ifdef FEAT_EVAL
7173static int can_f_submatch = FALSE; /* TRUE when submatch() can be used */
7174
7175/* These pointers are used instead of reg_match and reg_mmatch for
7176 * reg_submatch(). Needed for when the substitution string is an expression
7177 * that contains a call to substitute() and submatch(). */
7178static regmatch_T *submatch_match;
7179static regmmatch_T *submatch_mmatch;
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007180static linenr_T submatch_firstlnum;
7181static linenr_T submatch_maxline;
Bram Moolenaar978287b2011-06-19 04:32:15 +02007182static int submatch_line_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183#endif
7184
7185#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
7186/*
7187 * vim_regsub() - perform substitutions after a vim_regexec() or
7188 * vim_regexec_multi() match.
7189 *
7190 * If "copy" is TRUE really copy into "dest".
7191 * If "copy" is FALSE nothing is copied, this is just to find out the length
7192 * of the result.
7193 *
7194 * If "backslash" is TRUE, a backslash will be removed later, need to double
7195 * them to keep them, and insert a backslash before a CR to avoid it being
7196 * replaced with a line break later.
7197 *
7198 * Note: The matched text must not change between the call of
7199 * vim_regexec()/vim_regexec_multi() and vim_regsub()! It would make the back
7200 * references invalid!
7201 *
7202 * Returns the size of the replacement, including terminating NUL.
7203 */
7204 int
7205vim_regsub(rmp, source, dest, copy, magic, backslash)
7206 regmatch_T *rmp;
7207 char_u *source;
7208 char_u *dest;
7209 int copy;
7210 int magic;
7211 int backslash;
7212{
7213 reg_match = rmp;
7214 reg_mmatch = NULL;
7215 reg_maxline = 0;
Bram Moolenaar2f315ab2013-01-25 20:11:01 +01007216 reg_buf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 return vim_regsub_both(source, dest, copy, magic, backslash);
7218}
7219#endif
7220
7221 int
7222vim_regsub_multi(rmp, lnum, source, dest, copy, magic, backslash)
7223 regmmatch_T *rmp;
7224 linenr_T lnum;
7225 char_u *source;
7226 char_u *dest;
7227 int copy;
7228 int magic;
7229 int backslash;
7230{
7231 reg_match = NULL;
7232 reg_mmatch = rmp;
7233 reg_buf = curbuf; /* always works on the current buffer! */
7234 reg_firstlnum = lnum;
7235 reg_maxline = curbuf->b_ml.ml_line_count - lnum;
7236 return vim_regsub_both(source, dest, copy, magic, backslash);
7237}
7238
7239 static int
7240vim_regsub_both(source, dest, copy, magic, backslash)
7241 char_u *source;
7242 char_u *dest;
7243 int copy;
7244 int magic;
7245 int backslash;
7246{
7247 char_u *src;
7248 char_u *dst;
7249 char_u *s;
7250 int c;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007251 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 int no = -1;
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007253 fptr_T func_all = (fptr_T)NULL;
7254 fptr_T func_one = (fptr_T)NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 linenr_T clnum = 0; /* init for GCC */
7256 int len = 0; /* init for GCC */
7257#ifdef FEAT_EVAL
7258 static char_u *eval_result = NULL;
7259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260
7261 /* Be paranoid... */
7262 if (source == NULL || dest == NULL)
7263 {
7264 EMSG(_(e_null));
7265 return 0;
7266 }
7267 if (prog_magic_wrong())
7268 return 0;
7269 src = source;
7270 dst = dest;
7271
7272 /*
7273 * When the substitute part starts with "\=" evaluate it as an expression.
7274 */
7275 if (source[0] == '\\' && source[1] == '='
7276#ifdef FEAT_EVAL
7277 && !can_f_submatch /* can't do this recursively */
7278#endif
7279 )
7280 {
7281#ifdef FEAT_EVAL
7282 /* To make sure that the length doesn't change between checking the
7283 * length and copying the string, and to speed up things, the
7284 * resulting string is saved from the call with "copy" == FALSE to the
7285 * call with "copy" == TRUE. */
7286 if (copy)
7287 {
7288 if (eval_result != NULL)
7289 {
7290 STRCPY(dest, eval_result);
7291 dst += STRLEN(eval_result);
7292 vim_free(eval_result);
7293 eval_result = NULL;
7294 }
7295 }
7296 else
7297 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 win_T *save_reg_win;
7299 int save_ireg_ic;
7300
7301 vim_free(eval_result);
7302
7303 /* The expression may contain substitute(), which calls us
7304 * recursively. Make sure submatch() gets the text from the first
7305 * level. Don't need to save "reg_buf", because
7306 * vim_regexec_multi() can't be called recursively. */
7307 submatch_match = reg_match;
7308 submatch_mmatch = reg_mmatch;
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007309 submatch_firstlnum = reg_firstlnum;
7310 submatch_maxline = reg_maxline;
Bram Moolenaar978287b2011-06-19 04:32:15 +02007311 submatch_line_lbr = reg_line_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 save_reg_win = reg_win;
7313 save_ireg_ic = ireg_ic;
7314 can_f_submatch = TRUE;
7315
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007316 eval_result = eval_to_string(source + 2, NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 if (eval_result != NULL)
7318 {
Bram Moolenaar06975a42010-03-23 16:27:22 +01007319 int had_backslash = FALSE;
7320
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00007321 for (s = eval_result; *s != NUL; mb_ptr_adv(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 {
Bram Moolenaar978287b2011-06-19 04:32:15 +02007323 /* Change NL to CR, so that it becomes a line break,
7324 * unless called from vim_regexec_nl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 * Skip over a backslashed character. */
Bram Moolenaar978287b2011-06-19 04:32:15 +02007326 if (*s == NL && !submatch_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 *s = CAR;
7328 else if (*s == '\\' && s[1] != NUL)
Bram Moolenaar06975a42010-03-23 16:27:22 +01007329 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 ++s;
Bram Moolenaar60190782010-05-21 13:08:58 +02007331 /* Change NL to CR here too, so that this works:
7332 * :s/abc\\\ndef/\="aaa\\\nbbb"/ on text:
7333 * abc\
7334 * def
Bram Moolenaar978287b2011-06-19 04:32:15 +02007335 * Not when called from vim_regexec_nl().
Bram Moolenaar60190782010-05-21 13:08:58 +02007336 */
Bram Moolenaar978287b2011-06-19 04:32:15 +02007337 if (*s == NL && !submatch_line_lbr)
Bram Moolenaar60190782010-05-21 13:08:58 +02007338 *s = CAR;
Bram Moolenaar06975a42010-03-23 16:27:22 +01007339 had_backslash = TRUE;
7340 }
7341 }
7342 if (had_backslash && backslash)
7343 {
7344 /* Backslashes will be consumed, need to double them. */
7345 s = vim_strsave_escaped(eval_result, (char_u *)"\\");
7346 if (s != NULL)
7347 {
7348 vim_free(eval_result);
7349 eval_result = s;
7350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 }
7352
7353 dst += STRLEN(eval_result);
7354 }
7355
7356 reg_match = submatch_match;
7357 reg_mmatch = submatch_mmatch;
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007358 reg_firstlnum = submatch_firstlnum;
7359 reg_maxline = submatch_maxline;
Bram Moolenaar978287b2011-06-19 04:32:15 +02007360 reg_line_lbr = submatch_line_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 reg_win = save_reg_win;
7362 ireg_ic = save_ireg_ic;
7363 can_f_submatch = FALSE;
7364 }
7365#endif
7366 }
7367 else
7368 while ((c = *src++) != NUL)
7369 {
7370 if (c == '&' && magic)
7371 no = 0;
7372 else if (c == '\\' && *src != NUL)
7373 {
7374 if (*src == '&' && !magic)
7375 {
7376 ++src;
7377 no = 0;
7378 }
7379 else if ('0' <= *src && *src <= '9')
7380 {
7381 no = *src++ - '0';
7382 }
7383 else if (vim_strchr((char_u *)"uUlLeE", *src))
7384 {
7385 switch (*src++)
7386 {
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007387 case 'u': func_one = (fptr_T)do_upper;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 continue;
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007389 case 'U': func_all = (fptr_T)do_Upper;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 continue;
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007391 case 'l': func_one = (fptr_T)do_lower;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 continue;
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007393 case 'L': func_all = (fptr_T)do_Lower;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 continue;
7395 case 'e':
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007396 case 'E': func_one = func_all = (fptr_T)NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 continue;
7398 }
7399 }
7400 }
7401 if (no < 0) /* Ordinary character. */
7402 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00007403 if (c == K_SPECIAL && src[0] != NUL && src[1] != NUL)
7404 {
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00007405 /* Copy a special key as-is. */
Bram Moolenaardb552d602006-03-23 22:59:57 +00007406 if (copy)
7407 {
7408 *dst++ = c;
7409 *dst++ = *src++;
7410 *dst++ = *src++;
7411 }
7412 else
7413 {
7414 dst += 3;
7415 src += 2;
7416 }
7417 continue;
7418 }
7419
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 if (c == '\\' && *src != NUL)
7421 {
7422 /* Check for abbreviations -- webb */
7423 switch (*src)
7424 {
7425 case 'r': c = CAR; ++src; break;
7426 case 'n': c = NL; ++src; break;
7427 case 't': c = TAB; ++src; break;
7428 /* Oh no! \e already has meaning in subst pat :-( */
7429 /* case 'e': c = ESC; ++src; break; */
7430 case 'b': c = Ctrl_H; ++src; break;
7431
7432 /* If "backslash" is TRUE the backslash will be removed
7433 * later. Used to insert a literal CR. */
7434 default: if (backslash)
7435 {
7436 if (copy)
7437 *dst = '\\';
7438 ++dst;
7439 }
7440 c = *src++;
7441 }
7442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443#ifdef FEAT_MBYTE
Bram Moolenaardb552d602006-03-23 22:59:57 +00007444 else if (has_mbyte)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007445 c = mb_ptr2char(src - 1);
7446#endif
7447
Bram Moolenaardb552d602006-03-23 22:59:57 +00007448 /* Write to buffer, if copy is set. */
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007449 if (func_one != (fptr_T)NULL)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007450 /* Turbo C complains without the typecast */
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007451 func_one = (fptr_T)(func_one(&cc, c));
7452 else if (func_all != (fptr_T)NULL)
7453 /* Turbo C complains without the typecast */
7454 func_all = (fptr_T)(func_all(&cc, c));
7455 else /* just copy */
7456 cc = c;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007457
7458#ifdef FEAT_MBYTE
7459 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 {
Bram Moolenaar0c56c602010-07-12 22:42:33 +02007461 int totlen = mb_ptr2len(src - 1);
7462
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 if (copy)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007464 mb_char2bytes(cc, dst);
7465 dst += mb_char2len(cc) - 1;
Bram Moolenaar0c56c602010-07-12 22:42:33 +02007466 if (enc_utf8)
7467 {
7468 int clen = utf_ptr2len(src - 1);
7469
7470 /* If the character length is shorter than "totlen", there
7471 * are composing characters; copy them as-is. */
7472 if (clen < totlen)
7473 {
7474 if (copy)
7475 mch_memmove(dst + 1, src - 1 + clen,
7476 (size_t)(totlen - clen));
7477 dst += totlen - clen;
7478 }
7479 }
7480 src += totlen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 }
7482 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483#endif
7484 if (copy)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007485 *dst = cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 dst++;
7487 }
7488 else
7489 {
7490 if (REG_MULTI)
7491 {
7492 clnum = reg_mmatch->startpos[no].lnum;
7493 if (clnum < 0 || reg_mmatch->endpos[no].lnum < 0)
7494 s = NULL;
7495 else
7496 {
7497 s = reg_getline(clnum) + reg_mmatch->startpos[no].col;
7498 if (reg_mmatch->endpos[no].lnum == clnum)
7499 len = reg_mmatch->endpos[no].col
7500 - reg_mmatch->startpos[no].col;
7501 else
7502 len = (int)STRLEN(s);
7503 }
7504 }
7505 else
7506 {
7507 s = reg_match->startp[no];
7508 if (reg_match->endp[no] == NULL)
7509 s = NULL;
7510 else
7511 len = (int)(reg_match->endp[no] - s);
7512 }
7513 if (s != NULL)
7514 {
7515 for (;;)
7516 {
7517 if (len == 0)
7518 {
7519 if (REG_MULTI)
7520 {
7521 if (reg_mmatch->endpos[no].lnum == clnum)
7522 break;
7523 if (copy)
7524 *dst = CAR;
7525 ++dst;
7526 s = reg_getline(++clnum);
7527 if (reg_mmatch->endpos[no].lnum == clnum)
7528 len = reg_mmatch->endpos[no].col;
7529 else
7530 len = (int)STRLEN(s);
7531 }
7532 else
7533 break;
7534 }
7535 else if (*s == NUL) /* we hit NUL. */
7536 {
7537 if (copy)
7538 EMSG(_(e_re_damg));
7539 goto exit;
7540 }
7541 else
7542 {
7543 if (backslash && (*s == CAR || *s == '\\'))
7544 {
7545 /*
7546 * Insert a backslash in front of a CR, otherwise
7547 * it will be replaced by a line break.
7548 * Number of backslashes will be halved later,
7549 * double them here.
7550 */
7551 if (copy)
7552 {
7553 dst[0] = '\\';
7554 dst[1] = *s;
7555 }
7556 dst += 2;
7557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 else
7559 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007560#ifdef FEAT_MBYTE
7561 if (has_mbyte)
7562 c = mb_ptr2char(s);
7563 else
7564#endif
7565 c = *s;
7566
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007567 if (func_one != (fptr_T)NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 /* Turbo C complains without the typecast */
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007569 func_one = (fptr_T)(func_one(&cc, c));
7570 else if (func_all != (fptr_T)NULL)
7571 /* Turbo C complains without the typecast */
7572 func_all = (fptr_T)(func_all(&cc, c));
7573 else /* just copy */
7574 cc = c;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007575
7576#ifdef FEAT_MBYTE
7577 if (has_mbyte)
7578 {
Bram Moolenaar9225efb2007-07-30 20:32:53 +00007579 int l;
7580
7581 /* Copy composing characters separately, one
7582 * at a time. */
7583 if (enc_utf8)
7584 l = utf_ptr2len(s) - 1;
7585 else
7586 l = mb_ptr2len(s) - 1;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007587
7588 s += l;
7589 len -= l;
7590 if (copy)
7591 mb_char2bytes(cc, dst);
7592 dst += mb_char2len(cc) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007594 else
7595#endif
7596 if (copy)
7597 *dst = cc;
7598 dst++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007600
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 ++s;
7602 --len;
7603 }
7604 }
7605 }
7606 no = -1;
7607 }
7608 }
7609 if (copy)
7610 *dst = NUL;
7611
7612exit:
7613 return (int)((dst - dest) + 1);
7614}
7615
7616#ifdef FEAT_EVAL
Bram Moolenaard32a3192009-11-26 19:40:49 +00007617static char_u *reg_getline_submatch __ARGS((linenr_T lnum));
7618
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619/*
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007620 * Call reg_getline() with the line numbers from the submatch. If a
7621 * substitute() was used the reg_maxline and other values have been
7622 * overwritten.
7623 */
7624 static char_u *
7625reg_getline_submatch(lnum)
7626 linenr_T lnum;
7627{
7628 char_u *s;
7629 linenr_T save_first = reg_firstlnum;
7630 linenr_T save_max = reg_maxline;
7631
7632 reg_firstlnum = submatch_firstlnum;
7633 reg_maxline = submatch_maxline;
7634
7635 s = reg_getline(lnum);
7636
7637 reg_firstlnum = save_first;
7638 reg_maxline = save_max;
7639 return s;
7640}
7641
7642/*
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00007643 * Used for the submatch() function: get the string from the n'th submatch in
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 * allocated memory.
7645 * Returns NULL when not in a ":s" command and for a non-existing submatch.
7646 */
7647 char_u *
7648reg_submatch(no)
7649 int no;
7650{
7651 char_u *retval = NULL;
7652 char_u *s;
7653 int len;
7654 int round;
7655 linenr_T lnum;
7656
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007657 if (!can_f_submatch || no < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 return NULL;
7659
7660 if (submatch_match == NULL)
7661 {
7662 /*
7663 * First round: compute the length and allocate memory.
7664 * Second round: copy the text.
7665 */
7666 for (round = 1; round <= 2; ++round)
7667 {
7668 lnum = submatch_mmatch->startpos[no].lnum;
7669 if (lnum < 0 || submatch_mmatch->endpos[no].lnum < 0)
7670 return NULL;
7671
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007672 s = reg_getline_submatch(lnum) + submatch_mmatch->startpos[no].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 if (s == NULL) /* anti-crash check, cannot happen? */
7674 break;
7675 if (submatch_mmatch->endpos[no].lnum == lnum)
7676 {
7677 /* Within one line: take form start to end col. */
7678 len = submatch_mmatch->endpos[no].col
7679 - submatch_mmatch->startpos[no].col;
7680 if (round == 2)
Bram Moolenaarbbebc852005-07-18 21:47:53 +00007681 vim_strncpy(retval, s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 ++len;
7683 }
7684 else
7685 {
7686 /* Multiple lines: take start line from start col, middle
7687 * lines completely and end line up to end col. */
7688 len = (int)STRLEN(s);
7689 if (round == 2)
7690 {
7691 STRCPY(retval, s);
7692 retval[len] = '\n';
7693 }
7694 ++len;
7695 ++lnum;
7696 while (lnum < submatch_mmatch->endpos[no].lnum)
7697 {
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007698 s = reg_getline_submatch(lnum++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 if (round == 2)
7700 STRCPY(retval + len, s);
7701 len += (int)STRLEN(s);
7702 if (round == 2)
7703 retval[len] = '\n';
7704 ++len;
7705 }
7706 if (round == 2)
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007707 STRNCPY(retval + len, reg_getline_submatch(lnum),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 submatch_mmatch->endpos[no].col);
7709 len += submatch_mmatch->endpos[no].col;
7710 if (round == 2)
7711 retval[len] = NUL;
7712 ++len;
7713 }
7714
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007715 if (retval == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 {
7717 retval = lalloc((long_u)len, TRUE);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007718 if (retval == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 return NULL;
7720 }
7721 }
7722 }
7723 else
7724 {
Bram Moolenaar7670fa02009-02-21 21:04:20 +00007725 s = submatch_match->startp[no];
7726 if (s == NULL || submatch_match->endp[no] == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 retval = NULL;
7728 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 retval = vim_strnsave(s, (int)(submatch_match->endp[no] - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 }
7731
7732 return retval;
7733}
7734#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007735
7736static regengine_T bt_regengine =
7737{
7738 bt_regcomp,
7739 bt_regexec,
7740#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
7741 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
7742 bt_regexec_nl,
7743#endif
7744 bt_regexec_multi
7745#ifdef DEBUG
7746 ,(char_u *)""
7747#endif
7748};
7749
7750
7751#include "regexp_nfa.c"
7752
7753static regengine_T nfa_regengine =
7754{
7755 nfa_regcomp,
7756 nfa_regexec,
7757#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
7758 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
7759 nfa_regexec_nl,
7760#endif
7761 nfa_regexec_multi
7762#ifdef DEBUG
7763 ,(char_u *)""
7764#endif
7765};
7766
7767/* Which regexp engine to use? Needed for vim_regcomp().
7768 * Must match with 'regexpengine'. */
7769static int regexp_engine = 0;
7770#define AUTOMATIC_ENGINE 0
7771#define BACKTRACKING_ENGINE 1
7772#define NFA_ENGINE 2
7773#ifdef DEBUG
7774static char_u regname[][30] = {
7775 "AUTOMATIC Regexp Engine",
7776 "BACKTACKING Regexp Engine",
7777 "NFA Regexp Engine"
7778 };
7779#endif
7780
7781/*
7782 * Compile a regular expression into internal code.
7783 * Returns the program in allocated memory. Returns NULL for an error.
7784 */
7785 regprog_T *
7786vim_regcomp(expr_arg, re_flags)
7787 char_u *expr_arg;
7788 int re_flags;
7789{
7790 regprog_T *prog = NULL;
7791 char_u *expr = expr_arg;
7792
7793 syntax_error = FALSE;
7794 regexp_engine = p_re;
7795
7796 /* Check for prefix "\%#=", that sets the regexp engine */
7797 if (STRNCMP(expr, "\\%#=", 4) == 0)
7798 {
7799 int newengine = expr[4] - '0';
7800
7801 if (newengine == AUTOMATIC_ENGINE
7802 || newengine == BACKTRACKING_ENGINE
7803 || newengine == NFA_ENGINE)
7804 {
7805 regexp_engine = expr[4] - '0';
7806 expr += 5;
7807#ifdef DEBUG
7808 EMSG3("New regexp mode selected (%d): %s", regexp_engine,
7809 regname[newengine]);
7810#endif
7811 }
7812 else
7813 {
7814 EMSG(_("E864: \\%#= can only be followed by 0, 1, or 2. The automatic engine will be used "));
7815 regexp_engine = AUTOMATIC_ENGINE;
7816 }
7817 }
7818#ifdef DEBUG
7819 bt_regengine.expr = expr;
7820 nfa_regengine.expr = expr;
7821#endif
7822
7823 /*
7824 * First try the NFA engine, unless backtracking was requested.
7825 */
7826 if (regexp_engine != BACKTRACKING_ENGINE)
7827 prog = nfa_regengine.regcomp(expr, re_flags);
7828 else
7829 prog = bt_regengine.regcomp(expr, re_flags);
7830
7831 if (prog == NULL) /* error compiling regexp with initial engine */
7832 {
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02007833#ifdef BT_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007834 if (regexp_engine != BACKTRACKING_ENGINE) /* debugging log for NFA */
7835 {
7836 FILE *f;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02007837 f = fopen(BT_REGEXP_DEBUG_LOG_NAME, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007838 if (f)
7839 {
7840 if (!syntax_error)
7841 fprintf(f, "NFA engine could not handle \"%s\"\n", expr);
7842 else
7843 fprintf(f, "Syntax error in \"%s\"\n", expr);
7844 fclose(f);
7845 }
7846 else
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02007847 EMSG2("(NFA) Could not open \"%s\" to write !!!",
7848 BT_REGEXP_DEBUG_LOG_NAME);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007849 /*
7850 if (syntax_error)
7851 EMSG("NFA Regexp: Syntax Error !");
7852 */
7853 }
7854#endif
7855 /*
7856 * If NFA engine failed, then revert to the backtracking engine.
7857 * Except when there was a syntax error, which was properly handled by
7858 * NFA engine.
7859 */
7860 if (regexp_engine == AUTOMATIC_ENGINE)
7861 if (!syntax_error)
7862 prog = bt_regengine.regcomp(expr, re_flags);
7863
7864 } /* endif prog==NULL */
7865
7866
7867 return prog;
7868}
7869
7870/*
7871 * Match a regexp against a string.
7872 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
7873 * Uses curbuf for line count and 'iskeyword'.
7874 *
7875 * Return TRUE if there is a match, FALSE if not.
7876 */
7877 int
7878vim_regexec(rmp, line, col)
7879 regmatch_T *rmp;
7880 char_u *line; /* string to match against */
7881 colnr_T col; /* column to start looking for match */
7882{
7883 return rmp->regprog->engine->regexec(rmp, line, col);
7884}
7885
7886#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
7887 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
7888/*
7889 * Like vim_regexec(), but consider a "\n" in "line" to be a line break.
7890 */
7891 int
7892vim_regexec_nl(rmp, line, col)
7893 regmatch_T *rmp;
7894 char_u *line;
7895 colnr_T col;
7896{
7897 return rmp->regprog->engine->regexec_nl(rmp, line, col);
7898}
7899#endif
7900
7901/*
7902 * Match a regexp against multiple lines.
7903 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
7904 * Uses curbuf for line count and 'iskeyword'.
7905 *
7906 * Return zero if there is no match. Return number of lines contained in the
7907 * match otherwise.
7908 */
7909 long
7910vim_regexec_multi(rmp, win, buf, lnum, col, tm)
7911 regmmatch_T *rmp;
7912 win_T *win; /* window in which to search or NULL */
7913 buf_T *buf; /* buffer in which to search */
7914 linenr_T lnum; /* nr of line to start looking for match */
7915 colnr_T col; /* column to start looking for match */
7916 proftime_T *tm; /* timeout limit or NULL */
7917{
7918 return rmp->regprog->engine->regexec_multi(rmp, win, buf, lnum, col, tm);
7919}