blob: 3450f3aa90288c98ca3bc127ff63cff6ee2532a4 [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)");
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200364#ifdef FEAT_SYN_HL
Bram Moolenaar5de820b2013-06-02 15:01:57 +0200365static char_u e_z_not_allowed[] = N_("E66: \\z( not allowed here");
366static char_u e_z1_not_allowed[] = N_("E67: \\z1 et al. not allowed here");
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200367#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200368
Bram Moolenaar071d4272004-06-13 20:20:40 +0000369#define NOT_MULTI 0
370#define MULTI_ONE 1
371#define MULTI_MULT 2
372/*
373 * Return NOT_MULTI if c is not a "multi" operator.
374 * Return MULTI_ONE if c is a single "multi" operator.
375 * Return MULTI_MULT if c is a multi "multi" operator.
376 */
377 static int
378re_multi_type(c)
379 int c;
380{
381 if (c == Magic('@') || c == Magic('=') || c == Magic('?'))
382 return MULTI_ONE;
383 if (c == Magic('*') || c == Magic('+') || c == Magic('{'))
384 return MULTI_MULT;
385 return NOT_MULTI;
386}
387
388/*
389 * Flags to be passed up and down.
390 */
391#define HASWIDTH 0x1 /* Known never to match null string. */
392#define SIMPLE 0x2 /* Simple enough to be STAR/PLUS operand. */
393#define SPSTART 0x4 /* Starts with * or +. */
394#define HASNL 0x8 /* Contains some \n. */
395#define HASLOOKBH 0x10 /* Contains "\@<=" or "\@<!". */
396#define WORST 0 /* Worst case. */
397
398/*
399 * When regcode is set to this value, code is not emitted and size is computed
400 * instead.
401 */
402#define JUST_CALC_SIZE ((char_u *) -1)
403
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000404static char_u *reg_prev_sub = NULL;
405
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406/*
407 * REGEXP_INRANGE contains all characters which are always special in a []
408 * range after '\'.
409 * REGEXP_ABBR contains all characters which act as abbreviations after '\'.
410 * These are:
411 * \n - New line (NL).
412 * \r - Carriage Return (CR).
413 * \t - Tab (TAB).
414 * \e - Escape (ESC).
415 * \b - Backspace (Ctrl_H).
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000416 * \d - Character code in decimal, eg \d123
417 * \o - Character code in octal, eg \o80
418 * \x - Character code in hex, eg \x4a
419 * \u - Multibyte character code, eg \u20ac
420 * \U - Long multibyte character code, eg \U12345678
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 */
422static char_u REGEXP_INRANGE[] = "]^-n\\";
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000423static char_u REGEXP_ABBR[] = "nrtebdoxuU";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424
425static int backslash_trans __ARGS((int c));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000426static int get_char_class __ARGS((char_u **pp));
427static int get_equi_class __ARGS((char_u **pp));
428static void reg_equi_class __ARGS((int c));
429static int get_coll_element __ARGS((char_u **pp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430static char_u *skip_anyof __ARGS((char_u *p));
431static void init_class_tab __ARGS((void));
432
433/*
434 * Translate '\x' to its control character, except "\n", which is Magic.
435 */
436 static int
437backslash_trans(c)
438 int c;
439{
440 switch (c)
441 {
442 case 'r': return CAR;
443 case 't': return TAB;
444 case 'e': return ESC;
445 case 'b': return BS;
446 }
447 return c;
448}
449
450/*
Bram Moolenaardf177f62005-02-22 08:39:57 +0000451 * Check for a character class name "[:name:]". "pp" points to the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 * Returns one of the CLASS_ items. CLASS_NONE means that no item was
453 * recognized. Otherwise "pp" is advanced to after the item.
454 */
455 static int
Bram Moolenaardf177f62005-02-22 08:39:57 +0000456get_char_class(pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 char_u **pp;
458{
459 static const char *(class_names[]) =
460 {
461 "alnum:]",
462#define CLASS_ALNUM 0
463 "alpha:]",
464#define CLASS_ALPHA 1
465 "blank:]",
466#define CLASS_BLANK 2
467 "cntrl:]",
468#define CLASS_CNTRL 3
469 "digit:]",
470#define CLASS_DIGIT 4
471 "graph:]",
472#define CLASS_GRAPH 5
473 "lower:]",
474#define CLASS_LOWER 6
475 "print:]",
476#define CLASS_PRINT 7
477 "punct:]",
478#define CLASS_PUNCT 8
479 "space:]",
480#define CLASS_SPACE 9
481 "upper:]",
482#define CLASS_UPPER 10
483 "xdigit:]",
484#define CLASS_XDIGIT 11
485 "tab:]",
486#define CLASS_TAB 12
487 "return:]",
488#define CLASS_RETURN 13
489 "backspace:]",
490#define CLASS_BACKSPACE 14
491 "escape:]",
492#define CLASS_ESCAPE 15
493 };
494#define CLASS_NONE 99
495 int i;
496
497 if ((*pp)[1] == ':')
498 {
Bram Moolenaar78a15312009-05-15 19:33:18 +0000499 for (i = 0; i < (int)(sizeof(class_names) / sizeof(*class_names)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 if (STRNCMP(*pp + 2, class_names[i], STRLEN(class_names[i])) == 0)
501 {
502 *pp += STRLEN(class_names[i]) + 2;
503 return i;
504 }
505 }
506 return CLASS_NONE;
507}
508
509/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 * Specific version of character class functions.
511 * Using a table to keep this fast.
512 */
513static short class_tab[256];
514
515#define RI_DIGIT 0x01
516#define RI_HEX 0x02
517#define RI_OCTAL 0x04
518#define RI_WORD 0x08
519#define RI_HEAD 0x10
520#define RI_ALPHA 0x20
521#define RI_LOWER 0x40
522#define RI_UPPER 0x80
523#define RI_WHITE 0x100
524
525 static void
526init_class_tab()
527{
528 int i;
529 static int done = FALSE;
530
531 if (done)
532 return;
533
534 for (i = 0; i < 256; ++i)
535 {
536 if (i >= '0' && i <= '7')
537 class_tab[i] = RI_DIGIT + RI_HEX + RI_OCTAL + RI_WORD;
538 else if (i >= '8' && i <= '9')
539 class_tab[i] = RI_DIGIT + RI_HEX + RI_WORD;
540 else if (i >= 'a' && i <= 'f')
541 class_tab[i] = RI_HEX + RI_WORD + RI_HEAD + RI_ALPHA + RI_LOWER;
542#ifdef EBCDIC
543 else if ((i >= 'g' && i <= 'i') || (i >= 'j' && i <= 'r')
544 || (i >= 's' && i <= 'z'))
545#else
546 else if (i >= 'g' && i <= 'z')
547#endif
548 class_tab[i] = RI_WORD + RI_HEAD + RI_ALPHA + RI_LOWER;
549 else if (i >= 'A' && i <= 'F')
550 class_tab[i] = RI_HEX + RI_WORD + RI_HEAD + RI_ALPHA + RI_UPPER;
551#ifdef EBCDIC
552 else if ((i >= 'G' && i <= 'I') || ( i >= 'J' && i <= 'R')
553 || (i >= 'S' && i <= 'Z'))
554#else
555 else if (i >= 'G' && i <= 'Z')
556#endif
557 class_tab[i] = RI_WORD + RI_HEAD + RI_ALPHA + RI_UPPER;
558 else if (i == '_')
559 class_tab[i] = RI_WORD + RI_HEAD;
560 else
561 class_tab[i] = 0;
562 }
563 class_tab[' '] |= RI_WHITE;
564 class_tab['\t'] |= RI_WHITE;
565 done = TRUE;
566}
567
568#ifdef FEAT_MBYTE
569# define ri_digit(c) (c < 0x100 && (class_tab[c] & RI_DIGIT))
570# define ri_hex(c) (c < 0x100 && (class_tab[c] & RI_HEX))
571# define ri_octal(c) (c < 0x100 && (class_tab[c] & RI_OCTAL))
572# define ri_word(c) (c < 0x100 && (class_tab[c] & RI_WORD))
573# define ri_head(c) (c < 0x100 && (class_tab[c] & RI_HEAD))
574# define ri_alpha(c) (c < 0x100 && (class_tab[c] & RI_ALPHA))
575# define ri_lower(c) (c < 0x100 && (class_tab[c] & RI_LOWER))
576# define ri_upper(c) (c < 0x100 && (class_tab[c] & RI_UPPER))
577# define ri_white(c) (c < 0x100 && (class_tab[c] & RI_WHITE))
578#else
579# define ri_digit(c) (class_tab[c] & RI_DIGIT)
580# define ri_hex(c) (class_tab[c] & RI_HEX)
581# define ri_octal(c) (class_tab[c] & RI_OCTAL)
582# define ri_word(c) (class_tab[c] & RI_WORD)
583# define ri_head(c) (class_tab[c] & RI_HEAD)
584# define ri_alpha(c) (class_tab[c] & RI_ALPHA)
585# define ri_lower(c) (class_tab[c] & RI_LOWER)
586# define ri_upper(c) (class_tab[c] & RI_UPPER)
587# define ri_white(c) (class_tab[c] & RI_WHITE)
588#endif
589
590/* flags for regflags */
591#define RF_ICASE 1 /* ignore case */
592#define RF_NOICASE 2 /* don't ignore case */
593#define RF_HASNL 4 /* can match a NL */
594#define RF_ICOMBINE 8 /* ignore combining characters */
595#define RF_LOOKBH 16 /* uses "\@<=" or "\@<!" */
596
597/*
598 * Global work variables for vim_regcomp().
599 */
600
601static char_u *regparse; /* Input-scan pointer. */
602static int prevchr_len; /* byte length of previous char */
603static int num_complex_braces; /* Complex \{...} count */
604static int regnpar; /* () count. */
605#ifdef FEAT_SYN_HL
606static int regnzpar; /* \z() count. */
607static int re_has_z; /* \z item detected */
608#endif
609static char_u *regcode; /* Code-emit pointer, or JUST_CALC_SIZE */
610static long regsize; /* Code size. */
Bram Moolenaard3005802009-11-25 17:21:32 +0000611static int reg_toolong; /* TRUE when offset out of range */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612static char_u had_endbrace[NSUBEXP]; /* flags, TRUE if end of () found */
613static unsigned regflags; /* RF_ flags for prog */
614static long brace_min[10]; /* Minimums for complex brace repeats */
615static long brace_max[10]; /* Maximums for complex brace repeats */
616static int brace_count[10]; /* Current counts for complex brace repeats */
617#if defined(FEAT_SYN_HL) || defined(PROTO)
618static int had_eol; /* TRUE when EOL found by vim_regcomp() */
619#endif
620static int one_exactly = FALSE; /* only do one char for EXACTLY */
621
622static int reg_magic; /* magicness of the pattern: */
623#define MAGIC_NONE 1 /* "\V" very unmagic */
624#define MAGIC_OFF 2 /* "\M" or 'magic' off */
625#define MAGIC_ON 3 /* "\m" or 'magic' */
626#define MAGIC_ALL 4 /* "\v" very magic */
627
628static int reg_string; /* matching with a string instead of a buffer
629 line */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000630static int reg_strict; /* "[abc" is illegal */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631
632/*
633 * META contains all characters that may be magic, except '^' and '$'.
634 */
635
636#ifdef EBCDIC
637static char_u META[] = "%&()*+.123456789<=>?@ACDFHIKLMOPSUVWX[_acdfhiklmnopsuvwxz{|~";
638#else
639/* META[] is used often enough to justify turning it into a table. */
640static char_u META_flags[] = {
641 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
642 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
643/* % & ( ) * + . */
644 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0,
645/* 1 2 3 4 5 6 7 8 9 < = > ? */
646 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1,
647/* @ A C D F H I K L M O */
648 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1,
649/* P S U V W X Z [ _ */
650 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1,
651/* a c d f h i k l m n o */
652 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1,
653/* p s u v w x z { | ~ */
654 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1
655};
656#endif
657
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200658static int curchr; /* currently parsed character */
659/* Previous character. Note: prevchr is sometimes -1 when we are not at the
660 * start, eg in /[ ^I]^ the pattern was never found even if it existed,
661 * because ^ was taken to be magic -- webb */
662static int prevchr;
663static int prevprevchr; /* previous-previous character */
664static int nextchr; /* used for ungetchr() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665
666/* arguments for reg() */
667#define REG_NOPAREN 0 /* toplevel reg() */
668#define REG_PAREN 1 /* \(\) */
669#define REG_ZPAREN 2 /* \z(\) */
670#define REG_NPAREN 3 /* \%(\) */
671
Bram Moolenaar3737fc12013-06-01 14:42:56 +0200672typedef struct
673{
674 char_u *regparse;
675 int prevchr_len;
676 int curchr;
677 int prevchr;
678 int prevprevchr;
679 int nextchr;
680 int at_start;
681 int prev_at_start;
682 int regnpar;
683} parse_state_T;
684
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685/*
686 * Forward declarations for vim_regcomp()'s friends.
687 */
688static void initchr __ARGS((char_u *));
Bram Moolenaar3737fc12013-06-01 14:42:56 +0200689static void save_parse_state __ARGS((parse_state_T *ps));
690static void restore_parse_state __ARGS((parse_state_T *ps));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691static int getchr __ARGS((void));
692static void skipchr_keepstart __ARGS((void));
693static int peekchr __ARGS((void));
694static void skipchr __ARGS((void));
695static void ungetchr __ARGS((void));
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000696static int gethexchrs __ARGS((int maxinputlen));
697static int getoctchrs __ARGS((void));
698static int getdecchrs __ARGS((void));
699static int coll_get_char __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700static void regcomp_start __ARGS((char_u *expr, int flags));
701static char_u *reg __ARGS((int, int *));
702static char_u *regbranch __ARGS((int *flagp));
703static char_u *regconcat __ARGS((int *flagp));
704static char_u *regpiece __ARGS((int *));
705static char_u *regatom __ARGS((int *));
706static char_u *regnode __ARGS((int));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000707#ifdef FEAT_MBYTE
708static int use_multibytecode __ARGS((int c));
709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710static int prog_magic_wrong __ARGS((void));
711static char_u *regnext __ARGS((char_u *));
712static void regc __ARGS((int b));
713#ifdef FEAT_MBYTE
714static void regmbc __ARGS((int c));
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200715# define REGMBC(x) regmbc(x);
716# define CASEMBC(x) case x:
Bram Moolenaardf177f62005-02-22 08:39:57 +0000717#else
718# define regmbc(c) regc(c)
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200719# define REGMBC(x)
720# define CASEMBC(x)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721#endif
722static void reginsert __ARGS((int, char_u *));
Bram Moolenaar75eb1612013-05-29 18:45:11 +0200723static void reginsert_nr __ARGS((int op, long val, char_u *opnd));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724static void reginsert_limits __ARGS((int, long, long, char_u *));
725static char_u *re_put_long __ARGS((char_u *pr, long_u val));
726static int read_limits __ARGS((long *, long *));
727static void regtail __ARGS((char_u *, char_u *));
728static void regoptail __ARGS((char_u *, char_u *));
729
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200730static regengine_T bt_regengine;
731static regengine_T nfa_regengine;
732
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733/*
734 * Return TRUE if compiled regular expression "prog" can match a line break.
735 */
736 int
737re_multiline(prog)
738 regprog_T *prog;
739{
740 return (prog->regflags & RF_HASNL);
741}
742
743/*
744 * Return TRUE if compiled regular expression "prog" looks before the start
745 * position (pattern contains "\@<=" or "\@<!").
746 */
747 int
748re_lookbehind(prog)
749 regprog_T *prog;
750{
751 return (prog->regflags & RF_LOOKBH);
752}
753
754/*
Bram Moolenaardf177f62005-02-22 08:39:57 +0000755 * Check for an equivalence class name "[=a=]". "pp" points to the '['.
756 * Returns a character representing the class. Zero means that no item was
757 * recognized. Otherwise "pp" is advanced to after the item.
758 */
759 static int
760get_equi_class(pp)
761 char_u **pp;
762{
763 int c;
764 int l = 1;
765 char_u *p = *pp;
766
767 if (p[1] == '=')
768 {
769#ifdef FEAT_MBYTE
770 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000771 l = (*mb_ptr2len)(p + 2);
Bram Moolenaardf177f62005-02-22 08:39:57 +0000772#endif
773 if (p[l + 2] == '=' && p[l + 3] == ']')
774 {
775#ifdef FEAT_MBYTE
776 if (has_mbyte)
777 c = mb_ptr2char(p + 2);
778 else
779#endif
780 c = p[2];
781 *pp += l + 4;
782 return c;
783 }
784 }
785 return 0;
786}
787
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200788#ifdef EBCDIC
789/*
790 * Table for equivalence class "c". (IBM-1047)
791 */
792char *EQUIVAL_CLASS_C[16] = {
793 "A\x62\x63\x64\x65\x66\x67",
794 "C\x68",
795 "E\x71\x72\x73\x74",
796 "I\x75\x76\x77\x78",
797 "N\x69",
798 "O\xEB\xEC\xED\xEE\xEF",
799 "U\xFB\xFC\xFD\xFE",
800 "Y\xBA",
801 "a\x42\x43\x44\x45\x46\x47",
802 "c\x48",
803 "e\x51\x52\x53\x54",
804 "i\x55\x56\x57\x58",
805 "n\x49",
806 "o\xCB\xCC\xCD\xCE\xCF",
807 "u\xDB\xDC\xDD\xDE",
808 "y\x8D\xDF",
809};
810#endif
811
Bram Moolenaardf177f62005-02-22 08:39:57 +0000812/*
813 * Produce the bytes for equivalence class "c".
814 * Currently only handles latin1, latin9 and utf-8.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200815 * NOTE: When changing this function, also change nfa_emit_equi_class()
Bram Moolenaardf177f62005-02-22 08:39:57 +0000816 */
817 static void
818reg_equi_class(c)
819 int c;
820{
821#ifdef FEAT_MBYTE
822 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
Bram Moolenaar78622822005-08-23 21:00:13 +0000823 || STRCMP(p_enc, "iso-8859-15") == 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000824#endif
825 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200826#ifdef EBCDIC
827 int i;
828
829 /* This might be slower than switch/case below. */
830 for (i = 0; i < 16; i++)
831 {
832 if (vim_strchr(EQUIVAL_CLASS_C[i], c) != NULL)
833 {
834 char *p = EQUIVAL_CLASS_C[i];
835
836 while (*p != 0)
837 regmbc(*p++);
838 return;
839 }
840 }
841#else
Bram Moolenaardf177f62005-02-22 08:39:57 +0000842 switch (c)
843 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000844 case 'A': case '\300': case '\301': case '\302':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200845 CASEMBC(0x100) CASEMBC(0x102) CASEMBC(0x104) CASEMBC(0x1cd)
846 CASEMBC(0x1de) CASEMBC(0x1e0) CASEMBC(0x1ea2)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000847 case '\303': case '\304': case '\305':
848 regmbc('A'); regmbc('\300'); regmbc('\301');
849 regmbc('\302'); regmbc('\303'); regmbc('\304');
850 regmbc('\305');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200851 REGMBC(0x100) REGMBC(0x102) REGMBC(0x104)
852 REGMBC(0x1cd) REGMBC(0x1de) REGMBC(0x1e0)
853 REGMBC(0x1ea2)
854 return;
855 case 'B': CASEMBC(0x1e02) CASEMBC(0x1e06)
856 regmbc('B'); REGMBC(0x1e02) REGMBC(0x1e06)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000857 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000858 case 'C': case '\307':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200859 CASEMBC(0x106) CASEMBC(0x108) CASEMBC(0x10a) CASEMBC(0x10c)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000860 regmbc('C'); regmbc('\307');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200861 REGMBC(0x106) REGMBC(0x108) REGMBC(0x10a)
862 REGMBC(0x10c)
863 return;
864 case 'D': CASEMBC(0x10e) CASEMBC(0x110) CASEMBC(0x1e0a)
865 CASEMBC(0x1e0e) CASEMBC(0x1e10)
866 regmbc('D'); REGMBC(0x10e) REGMBC(0x110)
867 REGMBC(0x1e0a) REGMBC(0x1e0e) REGMBC(0x1e10)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000868 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000869 case 'E': case '\310': case '\311': case '\312': case '\313':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200870 CASEMBC(0x112) CASEMBC(0x114) CASEMBC(0x116) CASEMBC(0x118)
871 CASEMBC(0x11a) CASEMBC(0x1eba) CASEMBC(0x1ebc)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000872 regmbc('E'); regmbc('\310'); regmbc('\311');
873 regmbc('\312'); regmbc('\313');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200874 REGMBC(0x112) REGMBC(0x114) REGMBC(0x116)
875 REGMBC(0x118) REGMBC(0x11a) REGMBC(0x1eba)
876 REGMBC(0x1ebc)
877 return;
878 case 'F': CASEMBC(0x1e1e)
879 regmbc('F'); REGMBC(0x1e1e)
880 return;
881 case 'G': CASEMBC(0x11c) CASEMBC(0x11e) CASEMBC(0x120)
882 CASEMBC(0x122) CASEMBC(0x1e4) CASEMBC(0x1e6) CASEMBC(0x1f4)
883 CASEMBC(0x1e20)
884 regmbc('G'); REGMBC(0x11c) REGMBC(0x11e)
885 REGMBC(0x120) REGMBC(0x122) REGMBC(0x1e4)
886 REGMBC(0x1e6) REGMBC(0x1f4) REGMBC(0x1e20)
887 return;
888 case 'H': CASEMBC(0x124) CASEMBC(0x126) CASEMBC(0x1e22)
889 CASEMBC(0x1e26) CASEMBC(0x1e28)
890 regmbc('H'); REGMBC(0x124) REGMBC(0x126)
891 REGMBC(0x1e22) REGMBC(0x1e26) REGMBC(0x1e28)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000892 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000893 case 'I': case '\314': case '\315': case '\316': case '\317':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200894 CASEMBC(0x128) CASEMBC(0x12a) CASEMBC(0x12c) CASEMBC(0x12e)
895 CASEMBC(0x130) CASEMBC(0x1cf) CASEMBC(0x1ec8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000896 regmbc('I'); regmbc('\314'); regmbc('\315');
897 regmbc('\316'); regmbc('\317');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200898 REGMBC(0x128) REGMBC(0x12a) REGMBC(0x12c)
899 REGMBC(0x12e) REGMBC(0x130) REGMBC(0x1cf)
900 REGMBC(0x1ec8)
901 return;
902 case 'J': CASEMBC(0x134)
903 regmbc('J'); REGMBC(0x134)
904 return;
905 case 'K': CASEMBC(0x136) CASEMBC(0x1e8) CASEMBC(0x1e30)
906 CASEMBC(0x1e34)
907 regmbc('K'); REGMBC(0x136) REGMBC(0x1e8)
908 REGMBC(0x1e30) REGMBC(0x1e34)
909 return;
910 case 'L': CASEMBC(0x139) CASEMBC(0x13b) CASEMBC(0x13d)
911 CASEMBC(0x13f) CASEMBC(0x141) CASEMBC(0x1e3a)
912 regmbc('L'); REGMBC(0x139) REGMBC(0x13b)
913 REGMBC(0x13d) REGMBC(0x13f) REGMBC(0x141)
914 REGMBC(0x1e3a)
915 return;
916 case 'M': CASEMBC(0x1e3e) CASEMBC(0x1e40)
917 regmbc('M'); REGMBC(0x1e3e) REGMBC(0x1e40)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000918 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000919 case 'N': case '\321':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200920 CASEMBC(0x143) CASEMBC(0x145) CASEMBC(0x147) CASEMBC(0x1e44)
921 CASEMBC(0x1e48)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000922 regmbc('N'); regmbc('\321');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200923 REGMBC(0x143) REGMBC(0x145) REGMBC(0x147)
924 REGMBC(0x1e44) REGMBC(0x1e48)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000925 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000926 case 'O': case '\322': case '\323': case '\324': case '\325':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200927 case '\326': case '\330':
928 CASEMBC(0x14c) CASEMBC(0x14e) CASEMBC(0x150) CASEMBC(0x1a0)
929 CASEMBC(0x1d1) CASEMBC(0x1ea) CASEMBC(0x1ec) CASEMBC(0x1ece)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000930 regmbc('O'); regmbc('\322'); regmbc('\323');
931 regmbc('\324'); regmbc('\325'); regmbc('\326');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200932 regmbc('\330');
933 REGMBC(0x14c) REGMBC(0x14e) REGMBC(0x150)
934 REGMBC(0x1a0) REGMBC(0x1d1) REGMBC(0x1ea)
935 REGMBC(0x1ec) REGMBC(0x1ece)
936 return;
937 case 'P': case 0x1e54: case 0x1e56:
938 regmbc('P'); REGMBC(0x1e54) REGMBC(0x1e56)
939 return;
940 case 'R': CASEMBC(0x154) CASEMBC(0x156) CASEMBC(0x158)
941 CASEMBC(0x1e58) CASEMBC(0x1e5e)
942 regmbc('R'); REGMBC(0x154) REGMBC(0x156) REGMBC(0x158)
943 REGMBC(0x1e58) REGMBC(0x1e5e)
944 return;
945 case 'S': CASEMBC(0x15a) CASEMBC(0x15c) CASEMBC(0x15e)
946 CASEMBC(0x160) CASEMBC(0x1e60)
947 regmbc('S'); REGMBC(0x15a) REGMBC(0x15c)
948 REGMBC(0x15e) REGMBC(0x160) REGMBC(0x1e60)
949 return;
950 case 'T': CASEMBC(0x162) CASEMBC(0x164) CASEMBC(0x166)
951 CASEMBC(0x1e6a) CASEMBC(0x1e6e)
952 regmbc('T'); REGMBC(0x162) REGMBC(0x164)
953 REGMBC(0x166) REGMBC(0x1e6a) REGMBC(0x1e6e)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000954 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000955 case 'U': case '\331': case '\332': case '\333': case '\334':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200956 CASEMBC(0x168) CASEMBC(0x16a) CASEMBC(0x16c) CASEMBC(0x16e)
957 CASEMBC(0x170) CASEMBC(0x172) CASEMBC(0x1af) CASEMBC(0x1d3)
958 CASEMBC(0x1ee6)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000959 regmbc('U'); regmbc('\331'); regmbc('\332');
960 regmbc('\333'); regmbc('\334');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200961 REGMBC(0x168) REGMBC(0x16a) REGMBC(0x16c)
962 REGMBC(0x16e) REGMBC(0x170) REGMBC(0x172)
963 REGMBC(0x1af) REGMBC(0x1d3) REGMBC(0x1ee6)
964 return;
965 case 'V': CASEMBC(0x1e7c)
966 regmbc('V'); REGMBC(0x1e7c)
967 return;
968 case 'W': CASEMBC(0x174) CASEMBC(0x1e80) CASEMBC(0x1e82)
969 CASEMBC(0x1e84) CASEMBC(0x1e86)
970 regmbc('W'); REGMBC(0x174) REGMBC(0x1e80)
971 REGMBC(0x1e82) REGMBC(0x1e84) REGMBC(0x1e86)
972 return;
973 case 'X': CASEMBC(0x1e8a) CASEMBC(0x1e8c)
974 regmbc('X'); REGMBC(0x1e8a) REGMBC(0x1e8c)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000975 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000976 case 'Y': case '\335':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200977 CASEMBC(0x176) CASEMBC(0x178) CASEMBC(0x1e8e) CASEMBC(0x1ef2)
978 CASEMBC(0x1ef6) CASEMBC(0x1ef8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000979 regmbc('Y'); regmbc('\335');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200980 REGMBC(0x176) REGMBC(0x178) REGMBC(0x1e8e)
981 REGMBC(0x1ef2) REGMBC(0x1ef6) REGMBC(0x1ef8)
982 return;
983 case 'Z': CASEMBC(0x179) CASEMBC(0x17b) CASEMBC(0x17d)
984 CASEMBC(0x1b5) CASEMBC(0x1e90) CASEMBC(0x1e94)
985 regmbc('Z'); REGMBC(0x179) REGMBC(0x17b)
986 REGMBC(0x17d) REGMBC(0x1b5) REGMBC(0x1e90)
987 REGMBC(0x1e94)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000988 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000989 case 'a': case '\340': case '\341': case '\342':
990 case '\343': case '\344': case '\345':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200991 CASEMBC(0x101) CASEMBC(0x103) CASEMBC(0x105) CASEMBC(0x1ce)
992 CASEMBC(0x1df) CASEMBC(0x1e1) CASEMBC(0x1ea3)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000993 regmbc('a'); regmbc('\340'); regmbc('\341');
994 regmbc('\342'); regmbc('\343'); regmbc('\344');
995 regmbc('\345');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200996 REGMBC(0x101) REGMBC(0x103) REGMBC(0x105)
997 REGMBC(0x1ce) REGMBC(0x1df) REGMBC(0x1e1)
998 REGMBC(0x1ea3)
999 return;
1000 case 'b': CASEMBC(0x1e03) CASEMBC(0x1e07)
1001 regmbc('b'); REGMBC(0x1e03) REGMBC(0x1e07)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001002 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001003 case 'c': case '\347':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001004 CASEMBC(0x107) CASEMBC(0x109) CASEMBC(0x10b) CASEMBC(0x10d)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001005 regmbc('c'); regmbc('\347');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001006 REGMBC(0x107) REGMBC(0x109) REGMBC(0x10b)
1007 REGMBC(0x10d)
1008 return;
1009 case 'd': CASEMBC(0x10f) CASEMBC(0x111) CASEMBC(0x1d0b)
1010 CASEMBC(0x1e11)
1011 regmbc('d'); REGMBC(0x10f) REGMBC(0x111)
1012 REGMBC(0x1e0b) REGMBC(0x01e0f) REGMBC(0x1e11)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001013 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001014 case 'e': case '\350': case '\351': case '\352': case '\353':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001015 CASEMBC(0x113) CASEMBC(0x115) CASEMBC(0x117) CASEMBC(0x119)
1016 CASEMBC(0x11b) CASEMBC(0x1ebb) CASEMBC(0x1ebd)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001017 regmbc('e'); regmbc('\350'); regmbc('\351');
1018 regmbc('\352'); regmbc('\353');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001019 REGMBC(0x113) REGMBC(0x115) REGMBC(0x117)
1020 REGMBC(0x119) REGMBC(0x11b) REGMBC(0x1ebb)
1021 REGMBC(0x1ebd)
1022 return;
1023 case 'f': CASEMBC(0x1e1f)
1024 regmbc('f'); REGMBC(0x1e1f)
1025 return;
1026 case 'g': CASEMBC(0x11d) CASEMBC(0x11f) CASEMBC(0x121)
1027 CASEMBC(0x123) CASEMBC(0x1e5) CASEMBC(0x1e7) CASEMBC(0x1f5)
1028 CASEMBC(0x1e21)
1029 regmbc('g'); REGMBC(0x11d) REGMBC(0x11f)
1030 REGMBC(0x121) REGMBC(0x123) REGMBC(0x1e5)
1031 REGMBC(0x1e7) REGMBC(0x1f5) REGMBC(0x1e21)
1032 return;
1033 case 'h': CASEMBC(0x125) CASEMBC(0x127) CASEMBC(0x1e23)
1034 CASEMBC(0x1e27) CASEMBC(0x1e29) CASEMBC(0x1e96)
1035 regmbc('h'); REGMBC(0x125) REGMBC(0x127)
1036 REGMBC(0x1e23) REGMBC(0x1e27) REGMBC(0x1e29)
1037 REGMBC(0x1e96)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001038 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001039 case 'i': case '\354': case '\355': case '\356': case '\357':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001040 CASEMBC(0x129) CASEMBC(0x12b) CASEMBC(0x12d) CASEMBC(0x12f)
1041 CASEMBC(0x1d0) CASEMBC(0x1ec9)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001042 regmbc('i'); regmbc('\354'); regmbc('\355');
1043 regmbc('\356'); regmbc('\357');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001044 REGMBC(0x129) REGMBC(0x12b) REGMBC(0x12d)
1045 REGMBC(0x12f) REGMBC(0x1d0) REGMBC(0x1ec9)
1046 return;
1047 case 'j': CASEMBC(0x135) CASEMBC(0x1f0)
1048 regmbc('j'); REGMBC(0x135) REGMBC(0x1f0)
1049 return;
1050 case 'k': CASEMBC(0x137) CASEMBC(0x1e9) CASEMBC(0x1e31)
1051 CASEMBC(0x1e35)
1052 regmbc('k'); REGMBC(0x137) REGMBC(0x1e9)
1053 REGMBC(0x1e31) REGMBC(0x1e35)
1054 return;
1055 case 'l': CASEMBC(0x13a) CASEMBC(0x13c) CASEMBC(0x13e)
1056 CASEMBC(0x140) CASEMBC(0x142) CASEMBC(0x1e3b)
1057 regmbc('l'); REGMBC(0x13a) REGMBC(0x13c)
1058 REGMBC(0x13e) REGMBC(0x140) REGMBC(0x142)
1059 REGMBC(0x1e3b)
1060 return;
1061 case 'm': CASEMBC(0x1e3f) CASEMBC(0x1e41)
1062 regmbc('m'); REGMBC(0x1e3f) REGMBC(0x1e41)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001063 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001064 case 'n': case '\361':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001065 CASEMBC(0x144) CASEMBC(0x146) CASEMBC(0x148) CASEMBC(0x149)
1066 CASEMBC(0x1e45) CASEMBC(0x1e49)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001067 regmbc('n'); regmbc('\361');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001068 REGMBC(0x144) REGMBC(0x146) REGMBC(0x148)
1069 REGMBC(0x149) REGMBC(0x1e45) REGMBC(0x1e49)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001070 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001071 case 'o': case '\362': case '\363': case '\364': case '\365':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001072 case '\366': case '\370':
1073 CASEMBC(0x14d) CASEMBC(0x14f) CASEMBC(0x151) CASEMBC(0x1a1)
1074 CASEMBC(0x1d2) CASEMBC(0x1eb) CASEMBC(0x1ed) CASEMBC(0x1ecf)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001075 regmbc('o'); regmbc('\362'); regmbc('\363');
1076 regmbc('\364'); regmbc('\365'); regmbc('\366');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001077 regmbc('\370');
1078 REGMBC(0x14d) REGMBC(0x14f) REGMBC(0x151)
1079 REGMBC(0x1a1) REGMBC(0x1d2) REGMBC(0x1eb)
1080 REGMBC(0x1ed) REGMBC(0x1ecf)
1081 return;
1082 case 'p': CASEMBC(0x1e55) CASEMBC(0x1e57)
1083 regmbc('p'); REGMBC(0x1e55) REGMBC(0x1e57)
1084 return;
1085 case 'r': CASEMBC(0x155) CASEMBC(0x157) CASEMBC(0x159)
1086 CASEMBC(0x1e59) CASEMBC(0x1e5f)
1087 regmbc('r'); REGMBC(0x155) REGMBC(0x157) REGMBC(0x159)
1088 REGMBC(0x1e59) REGMBC(0x1e5f)
1089 return;
1090 case 's': CASEMBC(0x15b) CASEMBC(0x15d) CASEMBC(0x15f)
1091 CASEMBC(0x161) CASEMBC(0x1e61)
1092 regmbc('s'); REGMBC(0x15b) REGMBC(0x15d)
1093 REGMBC(0x15f) REGMBC(0x161) REGMBC(0x1e61)
1094 return;
1095 case 't': CASEMBC(0x163) CASEMBC(0x165) CASEMBC(0x167)
1096 CASEMBC(0x1e6b) CASEMBC(0x1e6f) CASEMBC(0x1e97)
1097 regmbc('t'); REGMBC(0x163) REGMBC(0x165) REGMBC(0x167)
1098 REGMBC(0x1e6b) REGMBC(0x1e6f) REGMBC(0x1e97)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001099 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001100 case 'u': case '\371': case '\372': case '\373': case '\374':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001101 CASEMBC(0x169) CASEMBC(0x16b) CASEMBC(0x16d) CASEMBC(0x16f)
1102 CASEMBC(0x171) CASEMBC(0x173) CASEMBC(0x1b0) CASEMBC(0x1d4)
1103 CASEMBC(0x1ee7)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001104 regmbc('u'); regmbc('\371'); regmbc('\372');
1105 regmbc('\373'); regmbc('\374');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001106 REGMBC(0x169) REGMBC(0x16b) REGMBC(0x16d)
1107 REGMBC(0x16f) REGMBC(0x171) REGMBC(0x173)
1108 REGMBC(0x1b0) REGMBC(0x1d4) REGMBC(0x1ee7)
1109 return;
1110 case 'v': CASEMBC(0x1e7d)
1111 regmbc('v'); REGMBC(0x1e7d)
1112 return;
1113 case 'w': CASEMBC(0x175) CASEMBC(0x1e81) CASEMBC(0x1e83)
1114 CASEMBC(0x1e85) CASEMBC(0x1e87) CASEMBC(0x1e98)
1115 regmbc('w'); REGMBC(0x175) REGMBC(0x1e81)
1116 REGMBC(0x1e83) REGMBC(0x1e85) REGMBC(0x1e87)
1117 REGMBC(0x1e98)
1118 return;
1119 case 'x': CASEMBC(0x1e8b) CASEMBC(0x1e8d)
1120 regmbc('x'); REGMBC(0x1e8b) REGMBC(0x1e8d)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001121 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001122 case 'y': case '\375': case '\377':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001123 CASEMBC(0x177) CASEMBC(0x1e8f) CASEMBC(0x1e99)
1124 CASEMBC(0x1ef3) CASEMBC(0x1ef7) CASEMBC(0x1ef9)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001125 regmbc('y'); regmbc('\375'); regmbc('\377');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001126 REGMBC(0x177) REGMBC(0x1e8f) REGMBC(0x1e99)
1127 REGMBC(0x1ef3) REGMBC(0x1ef7) REGMBC(0x1ef9)
1128 return;
1129 case 'z': CASEMBC(0x17a) CASEMBC(0x17c) CASEMBC(0x17e)
1130 CASEMBC(0x1b6) CASEMBC(0x1e91) CASEMBC(0x1e95)
1131 regmbc('z'); REGMBC(0x17a) REGMBC(0x17c)
1132 REGMBC(0x17e) REGMBC(0x1b6) REGMBC(0x1e91)
1133 REGMBC(0x1e95)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001134 return;
1135 }
Bram Moolenaar2c704a72010-06-03 21:17:25 +02001136#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00001137 }
1138 regmbc(c);
1139}
1140
1141/*
1142 * Check for a collating element "[.a.]". "pp" points to the '['.
1143 * Returns a character. Zero means that no item was recognized. Otherwise
1144 * "pp" is advanced to after the item.
1145 * Currently only single characters are recognized!
1146 */
1147 static int
1148get_coll_element(pp)
1149 char_u **pp;
1150{
1151 int c;
1152 int l = 1;
1153 char_u *p = *pp;
1154
1155 if (p[1] == '.')
1156 {
1157#ifdef FEAT_MBYTE
1158 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001159 l = (*mb_ptr2len)(p + 2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00001160#endif
1161 if (p[l + 2] == '.' && p[l + 3] == ']')
1162 {
1163#ifdef FEAT_MBYTE
1164 if (has_mbyte)
1165 c = mb_ptr2char(p + 2);
1166 else
1167#endif
1168 c = p[2];
1169 *pp += l + 4;
1170 return c;
1171 }
1172 }
1173 return 0;
1174}
1175
1176
1177/*
1178 * Skip over a "[]" range.
1179 * "p" must point to the character after the '['.
1180 * The returned pointer is on the matching ']', or the terminating NUL.
1181 */
1182 static char_u *
1183skip_anyof(p)
1184 char_u *p;
1185{
1186 int cpo_lit; /* 'cpoptions' contains 'l' flag */
1187 int cpo_bsl; /* 'cpoptions' contains '\' flag */
1188#ifdef FEAT_MBYTE
1189 int l;
1190#endif
1191
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00001192 cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL;
1193 cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001194
1195 if (*p == '^') /* Complement of range. */
1196 ++p;
1197 if (*p == ']' || *p == '-')
1198 ++p;
1199 while (*p != NUL && *p != ']')
1200 {
1201#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001202 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001203 p += l;
1204 else
1205#endif
1206 if (*p == '-')
1207 {
1208 ++p;
1209 if (*p != ']' && *p != NUL)
1210 mb_ptr_adv(p);
1211 }
1212 else if (*p == '\\'
1213 && !cpo_bsl
1214 && (vim_strchr(REGEXP_INRANGE, p[1]) != NULL
1215 || (!cpo_lit && vim_strchr(REGEXP_ABBR, p[1]) != NULL)))
1216 p += 2;
1217 else if (*p == '[')
1218 {
1219 if (get_char_class(&p) == CLASS_NONE
1220 && get_equi_class(&p) == 0
1221 && get_coll_element(&p) == 0)
1222 ++p; /* It was not a class name */
1223 }
1224 else
1225 ++p;
1226 }
1227
1228 return p;
1229}
1230
1231/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 * Skip past regular expression.
Bram Moolenaar748bf032005-02-02 23:04:36 +00001233 * Stop at end of "startp" or where "dirc" is found ('/', '?', etc).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 * Take care of characters with a backslash in front of it.
1235 * Skip strings inside [ and ].
1236 * When "newp" is not NULL and "dirc" is '?', make an allocated copy of the
1237 * expression and change "\?" to "?". If "*newp" is not NULL the expression
1238 * is changed in-place.
1239 */
1240 char_u *
1241skip_regexp(startp, dirc, magic, newp)
1242 char_u *startp;
1243 int dirc;
1244 int magic;
1245 char_u **newp;
1246{
1247 int mymagic;
1248 char_u *p = startp;
1249
1250 if (magic)
1251 mymagic = MAGIC_ON;
1252 else
1253 mymagic = MAGIC_OFF;
1254
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001255 for (; p[0] != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 {
1257 if (p[0] == dirc) /* found end of regexp */
1258 break;
1259 if ((p[0] == '[' && mymagic >= MAGIC_ON)
1260 || (p[0] == '\\' && p[1] == '[' && mymagic <= MAGIC_OFF))
1261 {
1262 p = skip_anyof(p + 1);
1263 if (p[0] == NUL)
1264 break;
1265 }
1266 else if (p[0] == '\\' && p[1] != NUL)
1267 {
1268 if (dirc == '?' && newp != NULL && p[1] == '?')
1269 {
1270 /* change "\?" to "?", make a copy first. */
1271 if (*newp == NULL)
1272 {
1273 *newp = vim_strsave(startp);
1274 if (*newp != NULL)
1275 p = *newp + (p - startp);
1276 }
1277 if (*newp != NULL)
Bram Moolenaar446cb832008-06-24 21:56:24 +00001278 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 else
1280 ++p;
1281 }
1282 else
1283 ++p; /* skip next character */
1284 if (*p == 'v')
1285 mymagic = MAGIC_ALL;
1286 else if (*p == 'V')
1287 mymagic = MAGIC_NONE;
1288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 }
1290 return p;
1291}
1292
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001293static regprog_T *bt_regcomp __ARGS((char_u *expr, int re_flags));
1294
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001296 * bt_regcomp() - compile a regular expression into internal code for the
1297 * traditional back track matcher.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001298 * Returns the program in allocated space. Returns NULL for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 *
1300 * We can't allocate space until we know how big the compiled form will be,
1301 * but we can't compile it (and thus know how big it is) until we've got a
1302 * place to put the code. So we cheat: we compile it twice, once with code
1303 * generation turned off and size counting turned on, and once "for real".
1304 * This also means that we don't allocate space until we are sure that the
1305 * thing really will compile successfully, and we never have to move the
1306 * code and thus invalidate pointers into it. (Note that it has to be in
1307 * one piece because vim_free() must be able to free it all.)
1308 *
1309 * Whether upper/lower case is to be ignored is decided when executing the
1310 * program, it does not matter here.
1311 *
1312 * Beware that the optimization-preparation code in here knows about some
1313 * of the structure of the compiled regexp.
1314 * "re_flags": RE_MAGIC and/or RE_STRING.
1315 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001316 static regprog_T *
1317bt_regcomp(expr, re_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 char_u *expr;
1319 int re_flags;
1320{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001321 bt_regprog_T *r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 char_u *scan;
1323 char_u *longest;
1324 int len;
1325 int flags;
1326
1327 if (expr == NULL)
1328 EMSG_RET_NULL(_(e_null));
1329
1330 init_class_tab();
1331
1332 /*
1333 * First pass: determine size, legality.
1334 */
1335 regcomp_start(expr, re_flags);
1336 regcode = JUST_CALC_SIZE;
1337 regc(REGMAGIC);
1338 if (reg(REG_NOPAREN, &flags) == NULL)
1339 return NULL;
1340
1341 /* Small enough for pointer-storage convention? */
1342#ifdef SMALL_MALLOC /* 16 bit storage allocation */
1343 if (regsize >= 65536L - 256L)
1344 EMSG_RET_NULL(_("E339: Pattern too long"));
1345#endif
1346
1347 /* Allocate space. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001348 r = (bt_regprog_T *)lalloc(sizeof(bt_regprog_T) + regsize, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 if (r == NULL)
1350 return NULL;
1351
1352 /*
1353 * Second pass: emit code.
1354 */
1355 regcomp_start(expr, re_flags);
1356 regcode = r->program;
1357 regc(REGMAGIC);
Bram Moolenaard3005802009-11-25 17:21:32 +00001358 if (reg(REG_NOPAREN, &flags) == NULL || reg_toolong)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 {
1360 vim_free(r);
Bram Moolenaard3005802009-11-25 17:21:32 +00001361 if (reg_toolong)
1362 EMSG_RET_NULL(_("E339: Pattern too long"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 return NULL;
1364 }
1365
1366 /* Dig out information for optimizations. */
1367 r->regstart = NUL; /* Worst-case defaults. */
1368 r->reganch = 0;
1369 r->regmust = NULL;
1370 r->regmlen = 0;
1371 r->regflags = regflags;
1372 if (flags & HASNL)
1373 r->regflags |= RF_HASNL;
1374 if (flags & HASLOOKBH)
1375 r->regflags |= RF_LOOKBH;
1376#ifdef FEAT_SYN_HL
1377 /* Remember whether this pattern has any \z specials in it. */
1378 r->reghasz = re_has_z;
1379#endif
1380 scan = r->program + 1; /* First BRANCH. */
1381 if (OP(regnext(scan)) == END) /* Only one top-level choice. */
1382 {
1383 scan = OPERAND(scan);
1384
1385 /* Starting-point info. */
1386 if (OP(scan) == BOL || OP(scan) == RE_BOF)
1387 {
1388 r->reganch++;
1389 scan = regnext(scan);
1390 }
1391
1392 if (OP(scan) == EXACTLY)
1393 {
1394#ifdef FEAT_MBYTE
1395 if (has_mbyte)
1396 r->regstart = (*mb_ptr2char)(OPERAND(scan));
1397 else
1398#endif
1399 r->regstart = *OPERAND(scan);
1400 }
1401 else if ((OP(scan) == BOW
1402 || OP(scan) == EOW
1403 || OP(scan) == NOTHING
1404 || OP(scan) == MOPEN + 0 || OP(scan) == NOPEN
1405 || OP(scan) == MCLOSE + 0 || OP(scan) == NCLOSE)
1406 && OP(regnext(scan)) == EXACTLY)
1407 {
1408#ifdef FEAT_MBYTE
1409 if (has_mbyte)
1410 r->regstart = (*mb_ptr2char)(OPERAND(regnext(scan)));
1411 else
1412#endif
1413 r->regstart = *OPERAND(regnext(scan));
1414 }
1415
1416 /*
1417 * If there's something expensive in the r.e., find the longest
1418 * literal string that must appear and make it the regmust. Resolve
1419 * ties in favor of later strings, since the regstart check works
1420 * with the beginning of the r.e. and avoiding duplication
1421 * strengthens checking. Not a strong reason, but sufficient in the
1422 * absence of others.
1423 */
1424 /*
1425 * When the r.e. starts with BOW, it is faster to look for a regmust
1426 * first. Used a lot for "#" and "*" commands. (Added by mool).
1427 */
1428 if ((flags & SPSTART || OP(scan) == BOW || OP(scan) == EOW)
1429 && !(flags & HASNL))
1430 {
1431 longest = NULL;
1432 len = 0;
1433 for (; scan != NULL; scan = regnext(scan))
1434 if (OP(scan) == EXACTLY && STRLEN(OPERAND(scan)) >= (size_t)len)
1435 {
1436 longest = OPERAND(scan);
1437 len = (int)STRLEN(OPERAND(scan));
1438 }
1439 r->regmust = longest;
1440 r->regmlen = len;
1441 }
1442 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001443#ifdef BT_REGEXP_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 regdump(expr, r);
1445#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001446 r->engine = &bt_regengine;
1447 return (regprog_T *)r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448}
1449
1450/*
1451 * Setup to parse the regexp. Used once to get the length and once to do it.
1452 */
1453 static void
1454regcomp_start(expr, re_flags)
1455 char_u *expr;
1456 int re_flags; /* see vim_regcomp() */
1457{
1458 initchr(expr);
1459 if (re_flags & RE_MAGIC)
1460 reg_magic = MAGIC_ON;
1461 else
1462 reg_magic = MAGIC_OFF;
1463 reg_string = (re_flags & RE_STRING);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001464 reg_strict = (re_flags & RE_STRICT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465
1466 num_complex_braces = 0;
1467 regnpar = 1;
1468 vim_memset(had_endbrace, 0, sizeof(had_endbrace));
1469#ifdef FEAT_SYN_HL
1470 regnzpar = 1;
1471 re_has_z = 0;
1472#endif
1473 regsize = 0L;
Bram Moolenaard3005802009-11-25 17:21:32 +00001474 reg_toolong = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 regflags = 0;
1476#if defined(FEAT_SYN_HL) || defined(PROTO)
1477 had_eol = FALSE;
1478#endif
1479}
1480
1481#if defined(FEAT_SYN_HL) || defined(PROTO)
1482/*
1483 * Check if during the previous call to vim_regcomp the EOL item "$" has been
1484 * found. This is messy, but it works fine.
1485 */
1486 int
1487vim_regcomp_had_eol()
1488{
1489 return had_eol;
1490}
1491#endif
1492
1493/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001494 * Parse regular expression, i.e. main body or parenthesized thing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 *
1496 * Caller must absorb opening parenthesis.
1497 *
1498 * Combining parenthesis handling with the base level of regular expression
1499 * is a trifle forced, but the need to tie the tails of the branches to what
1500 * follows makes it hard to avoid.
1501 */
1502 static char_u *
1503reg(paren, flagp)
1504 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1505 int *flagp;
1506{
1507 char_u *ret;
1508 char_u *br;
1509 char_u *ender;
1510 int parno = 0;
1511 int flags;
1512
1513 *flagp = HASWIDTH; /* Tentatively. */
1514
1515#ifdef FEAT_SYN_HL
1516 if (paren == REG_ZPAREN)
1517 {
1518 /* Make a ZOPEN node. */
1519 if (regnzpar >= NSUBEXP)
1520 EMSG_RET_NULL(_("E50: Too many \\z("));
1521 parno = regnzpar;
1522 regnzpar++;
1523 ret = regnode(ZOPEN + parno);
1524 }
1525 else
1526#endif
1527 if (paren == REG_PAREN)
1528 {
1529 /* Make a MOPEN node. */
1530 if (regnpar >= NSUBEXP)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001531 EMSG2_RET_NULL(_("E51: Too many %s("), reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 parno = regnpar;
1533 ++regnpar;
1534 ret = regnode(MOPEN + parno);
1535 }
1536 else if (paren == REG_NPAREN)
1537 {
1538 /* Make a NOPEN node. */
1539 ret = regnode(NOPEN);
1540 }
1541 else
1542 ret = NULL;
1543
1544 /* Pick up the branches, linking them together. */
1545 br = regbranch(&flags);
1546 if (br == NULL)
1547 return NULL;
1548 if (ret != NULL)
1549 regtail(ret, br); /* [MZ]OPEN -> first. */
1550 else
1551 ret = br;
1552 /* If one of the branches can be zero-width, the whole thing can.
1553 * If one of the branches has * at start or matches a line-break, the
1554 * whole thing can. */
1555 if (!(flags & HASWIDTH))
1556 *flagp &= ~HASWIDTH;
1557 *flagp |= flags & (SPSTART | HASNL | HASLOOKBH);
1558 while (peekchr() == Magic('|'))
1559 {
1560 skipchr();
1561 br = regbranch(&flags);
Bram Moolenaard3005802009-11-25 17:21:32 +00001562 if (br == NULL || reg_toolong)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 return NULL;
1564 regtail(ret, br); /* BRANCH -> BRANCH. */
1565 if (!(flags & HASWIDTH))
1566 *flagp &= ~HASWIDTH;
1567 *flagp |= flags & (SPSTART | HASNL | HASLOOKBH);
1568 }
1569
1570 /* Make a closing node, and hook it on the end. */
1571 ender = regnode(
1572#ifdef FEAT_SYN_HL
1573 paren == REG_ZPAREN ? ZCLOSE + parno :
1574#endif
1575 paren == REG_PAREN ? MCLOSE + parno :
1576 paren == REG_NPAREN ? NCLOSE : END);
1577 regtail(ret, ender);
1578
1579 /* Hook the tails of the branches to the closing node. */
1580 for (br = ret; br != NULL; br = regnext(br))
1581 regoptail(br, ender);
1582
1583 /* Check for proper termination. */
1584 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1585 {
1586#ifdef FEAT_SYN_HL
1587 if (paren == REG_ZPAREN)
Bram Moolenaar45eeb132005-06-06 21:59:07 +00001588 EMSG_RET_NULL(_("E52: Unmatched \\z("));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 else
1590#endif
1591 if (paren == REG_NPAREN)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001592 EMSG2_RET_NULL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001594 EMSG2_RET_NULL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 }
1596 else if (paren == REG_NOPAREN && peekchr() != NUL)
1597 {
1598 if (curchr == Magic(')'))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001599 EMSG2_RET_NULL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 else
Bram Moolenaar45eeb132005-06-06 21:59:07 +00001601 EMSG_RET_NULL(_(e_trailing)); /* "Can't happen". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 /* NOTREACHED */
1603 }
1604 /*
1605 * Here we set the flag allowing back references to this set of
1606 * parentheses.
1607 */
1608 if (paren == REG_PAREN)
1609 had_endbrace[parno] = TRUE; /* have seen the close paren */
1610 return ret;
1611}
1612
1613/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001614 * Parse one alternative of an | operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 * Implements the & operator.
1616 */
1617 static char_u *
1618regbranch(flagp)
1619 int *flagp;
1620{
1621 char_u *ret;
1622 char_u *chain = NULL;
1623 char_u *latest;
1624 int flags;
1625
1626 *flagp = WORST | HASNL; /* Tentatively. */
1627
1628 ret = regnode(BRANCH);
1629 for (;;)
1630 {
1631 latest = regconcat(&flags);
1632 if (latest == NULL)
1633 return NULL;
1634 /* If one of the branches has width, the whole thing has. If one of
1635 * the branches anchors at start-of-line, the whole thing does.
1636 * If one of the branches uses look-behind, the whole thing does. */
1637 *flagp |= flags & (HASWIDTH | SPSTART | HASLOOKBH);
1638 /* If one of the branches doesn't match a line-break, the whole thing
1639 * doesn't. */
1640 *flagp &= ~HASNL | (flags & HASNL);
1641 if (chain != NULL)
1642 regtail(chain, latest);
1643 if (peekchr() != Magic('&'))
1644 break;
1645 skipchr();
1646 regtail(latest, regnode(END)); /* operand ends */
Bram Moolenaard3005802009-11-25 17:21:32 +00001647 if (reg_toolong)
1648 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 reginsert(MATCH, latest);
1650 chain = latest;
1651 }
1652
1653 return ret;
1654}
1655
1656/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001657 * Parse one alternative of an | or & operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 * Implements the concatenation operator.
1659 */
1660 static char_u *
1661regconcat(flagp)
1662 int *flagp;
1663{
1664 char_u *first = NULL;
1665 char_u *chain = NULL;
1666 char_u *latest;
1667 int flags;
1668 int cont = TRUE;
1669
1670 *flagp = WORST; /* Tentatively. */
1671
1672 while (cont)
1673 {
1674 switch (peekchr())
1675 {
1676 case NUL:
1677 case Magic('|'):
1678 case Magic('&'):
1679 case Magic(')'):
1680 cont = FALSE;
1681 break;
1682 case Magic('Z'):
1683#ifdef FEAT_MBYTE
1684 regflags |= RF_ICOMBINE;
1685#endif
1686 skipchr_keepstart();
1687 break;
1688 case Magic('c'):
1689 regflags |= RF_ICASE;
1690 skipchr_keepstart();
1691 break;
1692 case Magic('C'):
1693 regflags |= RF_NOICASE;
1694 skipchr_keepstart();
1695 break;
1696 case Magic('v'):
1697 reg_magic = MAGIC_ALL;
1698 skipchr_keepstart();
1699 curchr = -1;
1700 break;
1701 case Magic('m'):
1702 reg_magic = MAGIC_ON;
1703 skipchr_keepstart();
1704 curchr = -1;
1705 break;
1706 case Magic('M'):
1707 reg_magic = MAGIC_OFF;
1708 skipchr_keepstart();
1709 curchr = -1;
1710 break;
1711 case Magic('V'):
1712 reg_magic = MAGIC_NONE;
1713 skipchr_keepstart();
1714 curchr = -1;
1715 break;
1716 default:
1717 latest = regpiece(&flags);
Bram Moolenaard3005802009-11-25 17:21:32 +00001718 if (latest == NULL || reg_toolong)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 return NULL;
1720 *flagp |= flags & (HASWIDTH | HASNL | HASLOOKBH);
1721 if (chain == NULL) /* First piece. */
1722 *flagp |= flags & SPSTART;
1723 else
1724 regtail(chain, latest);
1725 chain = latest;
1726 if (first == NULL)
1727 first = latest;
1728 break;
1729 }
1730 }
1731 if (first == NULL) /* Loop ran zero times. */
1732 first = regnode(NOTHING);
1733 return first;
1734}
1735
1736/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001737 * Parse something followed by possible [*+=].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 *
1739 * Note that the branching code sequences used for = and the general cases
1740 * of * and + are somewhat optimized: they use the same NOTHING node as
1741 * both the endmarker for their branch list and the body of the last branch.
1742 * It might seem that this node could be dispensed with entirely, but the
1743 * endmarker role is not redundant.
1744 */
1745 static char_u *
1746regpiece(flagp)
1747 int *flagp;
1748{
1749 char_u *ret;
1750 int op;
1751 char_u *next;
1752 int flags;
1753 long minval;
1754 long maxval;
1755
1756 ret = regatom(&flags);
1757 if (ret == NULL)
1758 return NULL;
1759
1760 op = peekchr();
1761 if (re_multi_type(op) == NOT_MULTI)
1762 {
1763 *flagp = flags;
1764 return ret;
1765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 /* default flags */
1767 *flagp = (WORST | SPSTART | (flags & (HASNL | HASLOOKBH)));
1768
1769 skipchr();
1770 switch (op)
1771 {
1772 case Magic('*'):
1773 if (flags & SIMPLE)
1774 reginsert(STAR, ret);
1775 else
1776 {
1777 /* Emit x* as (x&|), where & means "self". */
1778 reginsert(BRANCH, ret); /* Either x */
1779 regoptail(ret, regnode(BACK)); /* and loop */
1780 regoptail(ret, ret); /* back */
1781 regtail(ret, regnode(BRANCH)); /* or */
1782 regtail(ret, regnode(NOTHING)); /* null. */
1783 }
1784 break;
1785
1786 case Magic('+'):
1787 if (flags & SIMPLE)
1788 reginsert(PLUS, ret);
1789 else
1790 {
1791 /* Emit x+ as x(&|), where & means "self". */
1792 next = regnode(BRANCH); /* Either */
1793 regtail(ret, next);
Bram Moolenaar582fd852005-03-28 20:58:01 +00001794 regtail(regnode(BACK), ret); /* loop back */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 regtail(next, regnode(BRANCH)); /* or */
1796 regtail(ret, regnode(NOTHING)); /* null. */
1797 }
1798 *flagp = (WORST | HASWIDTH | (flags & (HASNL | HASLOOKBH)));
1799 break;
1800
1801 case Magic('@'):
1802 {
1803 int lop = END;
Bram Moolenaar75eb1612013-05-29 18:45:11 +02001804 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805
Bram Moolenaar75eb1612013-05-29 18:45:11 +02001806 nr = getdecchrs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 switch (no_Magic(getchr()))
1808 {
1809 case '=': lop = MATCH; break; /* \@= */
1810 case '!': lop = NOMATCH; break; /* \@! */
1811 case '>': lop = SUBPAT; break; /* \@> */
1812 case '<': switch (no_Magic(getchr()))
1813 {
1814 case '=': lop = BEHIND; break; /* \@<= */
1815 case '!': lop = NOBEHIND; break; /* \@<! */
1816 }
1817 }
1818 if (lop == END)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001819 EMSG2_RET_NULL(_("E59: invalid character after %s@"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 reg_magic == MAGIC_ALL);
1821 /* Look behind must match with behind_pos. */
1822 if (lop == BEHIND || lop == NOBEHIND)
1823 {
1824 regtail(ret, regnode(BHPOS));
1825 *flagp |= HASLOOKBH;
1826 }
1827 regtail(ret, regnode(END)); /* operand ends */
Bram Moolenaar75eb1612013-05-29 18:45:11 +02001828 if (lop == BEHIND || lop == NOBEHIND)
1829 {
1830 if (nr < 0)
1831 nr = 0; /* no limit is same as zero limit */
1832 reginsert_nr(lop, nr, ret);
1833 }
1834 else
1835 reginsert(lop, ret);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 break;
1837 }
1838
1839 case Magic('?'):
1840 case Magic('='):
1841 /* Emit x= as (x|) */
1842 reginsert(BRANCH, ret); /* Either x */
1843 regtail(ret, regnode(BRANCH)); /* or */
1844 next = regnode(NOTHING); /* null. */
1845 regtail(ret, next);
1846 regoptail(ret, next);
1847 break;
1848
1849 case Magic('{'):
1850 if (!read_limits(&minval, &maxval))
1851 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 if (flags & SIMPLE)
1853 {
1854 reginsert(BRACE_SIMPLE, ret);
1855 reginsert_limits(BRACE_LIMITS, minval, maxval, ret);
1856 }
1857 else
1858 {
1859 if (num_complex_braces >= 10)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001860 EMSG2_RET_NULL(_("E60: Too many complex %s{...}s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 reg_magic == MAGIC_ALL);
1862 reginsert(BRACE_COMPLEX + num_complex_braces, ret);
1863 regoptail(ret, regnode(BACK));
1864 regoptail(ret, ret);
1865 reginsert_limits(BRACE_LIMITS, minval, maxval, ret);
1866 ++num_complex_braces;
1867 }
1868 if (minval > 0 && maxval > 0)
1869 *flagp = (HASWIDTH | (flags & (HASNL | HASLOOKBH)));
1870 break;
1871 }
1872 if (re_multi_type(peekchr()) != NOT_MULTI)
1873 {
1874 /* Can't have a multi follow a multi. */
1875 if (peekchr() == Magic('*'))
1876 sprintf((char *)IObuff, _("E61: Nested %s*"),
1877 reg_magic >= MAGIC_ON ? "" : "\\");
1878 else
1879 sprintf((char *)IObuff, _("E62: Nested %s%c"),
1880 reg_magic == MAGIC_ALL ? "" : "\\", no_Magic(peekchr()));
1881 EMSG_RET_NULL(IObuff);
1882 }
1883
1884 return ret;
1885}
1886
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001887/* When making changes to classchars also change nfa_classcodes. */
1888static char_u *classchars = (char_u *)".iIkKfFpPsSdDxXoOwWhHaAlLuU";
1889static int classcodes[] = {
1890 ANY, IDENT, SIDENT, KWORD, SKWORD,
1891 FNAME, SFNAME, PRINT, SPRINT,
1892 WHITE, NWHITE, DIGIT, NDIGIT,
1893 HEX, NHEX, OCTAL, NOCTAL,
1894 WORD, NWORD, HEAD, NHEAD,
1895 ALPHA, NALPHA, LOWER, NLOWER,
1896 UPPER, NUPPER
1897};
1898
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001900 * Parse the lowest level.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 *
1902 * Optimization: gobbles an entire sequence of ordinary characters so that
1903 * it can turn them into a single node, which is smaller to store and
1904 * faster to run. Don't do this when one_exactly is set.
1905 */
1906 static char_u *
1907regatom(flagp)
1908 int *flagp;
1909{
1910 char_u *ret;
1911 int flags;
1912 int cpo_lit; /* 'cpoptions' contains 'l' flag */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001913 int cpo_bsl; /* 'cpoptions' contains '\' flag */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 char_u *p;
1916 int extra = 0;
1917
1918 *flagp = WORST; /* Tentatively. */
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00001919 cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL;
1920 cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921
1922 c = getchr();
1923 switch (c)
1924 {
1925 case Magic('^'):
1926 ret = regnode(BOL);
1927 break;
1928
1929 case Magic('$'):
1930 ret = regnode(EOL);
1931#if defined(FEAT_SYN_HL) || defined(PROTO)
1932 had_eol = TRUE;
1933#endif
1934 break;
1935
1936 case Magic('<'):
1937 ret = regnode(BOW);
1938 break;
1939
1940 case Magic('>'):
1941 ret = regnode(EOW);
1942 break;
1943
1944 case Magic('_'):
1945 c = no_Magic(getchr());
1946 if (c == '^') /* "\_^" is start-of-line */
1947 {
1948 ret = regnode(BOL);
1949 break;
1950 }
1951 if (c == '$') /* "\_$" is end-of-line */
1952 {
1953 ret = regnode(EOL);
1954#if defined(FEAT_SYN_HL) || defined(PROTO)
1955 had_eol = TRUE;
1956#endif
1957 break;
1958 }
1959
1960 extra = ADD_NL;
1961 *flagp |= HASNL;
1962
1963 /* "\_[" is character range plus newline */
1964 if (c == '[')
1965 goto collection;
1966
1967 /* "\_x" is character class plus newline */
1968 /*FALLTHROUGH*/
1969
1970 /*
1971 * Character classes.
1972 */
1973 case Magic('.'):
1974 case Magic('i'):
1975 case Magic('I'):
1976 case Magic('k'):
1977 case Magic('K'):
1978 case Magic('f'):
1979 case Magic('F'):
1980 case Magic('p'):
1981 case Magic('P'):
1982 case Magic('s'):
1983 case Magic('S'):
1984 case Magic('d'):
1985 case Magic('D'):
1986 case Magic('x'):
1987 case Magic('X'):
1988 case Magic('o'):
1989 case Magic('O'):
1990 case Magic('w'):
1991 case Magic('W'):
1992 case Magic('h'):
1993 case Magic('H'):
1994 case Magic('a'):
1995 case Magic('A'):
1996 case Magic('l'):
1997 case Magic('L'):
1998 case Magic('u'):
1999 case Magic('U'):
2000 p = vim_strchr(classchars, no_Magic(c));
2001 if (p == NULL)
2002 EMSG_RET_NULL(_("E63: invalid use of \\_"));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002003#ifdef FEAT_MBYTE
2004 /* When '.' is followed by a composing char ignore the dot, so that
2005 * the composing char is matched here. */
2006 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
2007 {
2008 c = getchr();
2009 goto do_multibyte;
2010 }
2011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 ret = regnode(classcodes[p - classchars] + extra);
2013 *flagp |= HASWIDTH | SIMPLE;
2014 break;
2015
2016 case Magic('n'):
2017 if (reg_string)
2018 {
2019 /* In a string "\n" matches a newline character. */
2020 ret = regnode(EXACTLY);
2021 regc(NL);
2022 regc(NUL);
2023 *flagp |= HASWIDTH | SIMPLE;
2024 }
2025 else
2026 {
2027 /* In buffer text "\n" matches the end of a line. */
2028 ret = regnode(NEWL);
2029 *flagp |= HASWIDTH | HASNL;
2030 }
2031 break;
2032
2033 case Magic('('):
2034 if (one_exactly)
2035 EMSG_ONE_RET_NULL;
2036 ret = reg(REG_PAREN, &flags);
2037 if (ret == NULL)
2038 return NULL;
2039 *flagp |= flags & (HASWIDTH | SPSTART | HASNL | HASLOOKBH);
2040 break;
2041
2042 case NUL:
2043 case Magic('|'):
2044 case Magic('&'):
2045 case Magic(')'):
Bram Moolenaard4210772008-01-02 14:35:30 +00002046 if (one_exactly)
2047 EMSG_ONE_RET_NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 EMSG_RET_NULL(_(e_internal)); /* Supposed to be caught earlier. */
2049 /* NOTREACHED */
2050
2051 case Magic('='):
2052 case Magic('?'):
2053 case Magic('+'):
2054 case Magic('@'):
2055 case Magic('{'):
2056 case Magic('*'):
2057 c = no_Magic(c);
2058 sprintf((char *)IObuff, _("E64: %s%c follows nothing"),
2059 (c == '*' ? reg_magic >= MAGIC_ON : reg_magic == MAGIC_ALL)
2060 ? "" : "\\", c);
2061 EMSG_RET_NULL(IObuff);
2062 /* NOTREACHED */
2063
2064 case Magic('~'): /* previous substitute pattern */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002065 if (reg_prev_sub != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 {
2067 char_u *lp;
2068
2069 ret = regnode(EXACTLY);
2070 lp = reg_prev_sub;
2071 while (*lp != NUL)
2072 regc(*lp++);
2073 regc(NUL);
2074 if (*reg_prev_sub != NUL)
2075 {
2076 *flagp |= HASWIDTH;
2077 if ((lp - reg_prev_sub) == 1)
2078 *flagp |= SIMPLE;
2079 }
2080 }
2081 else
2082 EMSG_RET_NULL(_(e_nopresub));
2083 break;
2084
2085 case Magic('1'):
2086 case Magic('2'):
2087 case Magic('3'):
2088 case Magic('4'):
2089 case Magic('5'):
2090 case Magic('6'):
2091 case Magic('7'):
2092 case Magic('8'):
2093 case Magic('9'):
2094 {
2095 int refnum;
2096
2097 refnum = c - Magic('0');
2098 /*
2099 * Check if the back reference is legal. We must have seen the
2100 * close brace.
2101 * TODO: Should also check that we don't refer to something
2102 * that is repeated (+*=): what instance of the repetition
2103 * should we match?
2104 */
2105 if (!had_endbrace[refnum])
2106 {
2107 /* Trick: check if "@<=" or "@<!" follows, in which case
2108 * the \1 can appear before the referenced match. */
2109 for (p = regparse; *p != NUL; ++p)
2110 if (p[0] == '@' && p[1] == '<'
2111 && (p[2] == '!' || p[2] == '='))
2112 break;
2113 if (*p == NUL)
2114 EMSG_RET_NULL(_("E65: Illegal back reference"));
2115 }
2116 ret = regnode(BACKREF + refnum);
2117 }
2118 break;
2119
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 case Magic('z'):
2121 {
2122 c = no_Magic(getchr());
2123 switch (c)
2124 {
Bram Moolenaarc4956c82006-03-12 21:58:43 +00002125#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 case '(': if (reg_do_extmatch != REX_SET)
Bram Moolenaar5de820b2013-06-02 15:01:57 +02002127 EMSG_RET_NULL(_(e_z_not_allowed));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 if (one_exactly)
2129 EMSG_ONE_RET_NULL;
2130 ret = reg(REG_ZPAREN, &flags);
2131 if (ret == NULL)
2132 return NULL;
2133 *flagp |= flags & (HASWIDTH|SPSTART|HASNL|HASLOOKBH);
2134 re_has_z = REX_SET;
2135 break;
2136
2137 case '1':
2138 case '2':
2139 case '3':
2140 case '4':
2141 case '5':
2142 case '6':
2143 case '7':
2144 case '8':
2145 case '9': if (reg_do_extmatch != REX_USE)
Bram Moolenaar5de820b2013-06-02 15:01:57 +02002146 EMSG_RET_NULL(_(e_z1_not_allowed));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 ret = regnode(ZREF + c - '0');
2148 re_has_z = REX_USE;
2149 break;
Bram Moolenaarc4956c82006-03-12 21:58:43 +00002150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151
2152 case 's': ret = regnode(MOPEN + 0);
2153 break;
2154
2155 case 'e': ret = regnode(MCLOSE + 0);
2156 break;
2157
2158 default: EMSG_RET_NULL(_("E68: Invalid character after \\z"));
2159 }
2160 }
2161 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162
2163 case Magic('%'):
2164 {
2165 c = no_Magic(getchr());
2166 switch (c)
2167 {
2168 /* () without a back reference */
2169 case '(':
2170 if (one_exactly)
2171 EMSG_ONE_RET_NULL;
2172 ret = reg(REG_NPAREN, &flags);
2173 if (ret == NULL)
2174 return NULL;
2175 *flagp |= flags & (HASWIDTH | SPSTART | HASNL | HASLOOKBH);
2176 break;
2177
2178 /* Catch \%^ and \%$ regardless of where they appear in the
2179 * pattern -- regardless of whether or not it makes sense. */
2180 case '^':
2181 ret = regnode(RE_BOF);
2182 break;
2183
2184 case '$':
2185 ret = regnode(RE_EOF);
2186 break;
2187
2188 case '#':
2189 ret = regnode(CURSOR);
2190 break;
2191
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00002192 case 'V':
2193 ret = regnode(RE_VISUAL);
2194 break;
2195
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 /* \%[abc]: Emit as a list of branches, all ending at the last
2197 * branch which matches nothing. */
2198 case '[':
2199 if (one_exactly) /* doesn't nest */
2200 EMSG_ONE_RET_NULL;
2201 {
2202 char_u *lastbranch;
2203 char_u *lastnode = NULL;
2204 char_u *br;
2205
2206 ret = NULL;
2207 while ((c = getchr()) != ']')
2208 {
2209 if (c == NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002210 EMSG2_RET_NULL(_("E69: Missing ] after %s%%["),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 reg_magic == MAGIC_ALL);
2212 br = regnode(BRANCH);
2213 if (ret == NULL)
2214 ret = br;
2215 else
2216 regtail(lastnode, br);
2217
2218 ungetchr();
2219 one_exactly = TRUE;
2220 lastnode = regatom(flagp);
2221 one_exactly = FALSE;
2222 if (lastnode == NULL)
2223 return NULL;
2224 }
2225 if (ret == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002226 EMSG2_RET_NULL(_("E70: Empty %s%%[]"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 reg_magic == MAGIC_ALL);
2228 lastbranch = regnode(BRANCH);
2229 br = regnode(NOTHING);
2230 if (ret != JUST_CALC_SIZE)
2231 {
2232 regtail(lastnode, br);
2233 regtail(lastbranch, br);
2234 /* connect all branches to the NOTHING
2235 * branch at the end */
2236 for (br = ret; br != lastnode; )
2237 {
2238 if (OP(br) == BRANCH)
2239 {
2240 regtail(br, lastbranch);
2241 br = OPERAND(br);
2242 }
2243 else
2244 br = regnext(br);
2245 }
2246 }
Bram Moolenaara6404a42008-08-08 11:45:39 +00002247 *flagp &= ~(HASWIDTH | SIMPLE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 break;
2249 }
2250
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002251 case 'd': /* %d123 decimal */
2252 case 'o': /* %o123 octal */
2253 case 'x': /* %xab hex 2 */
2254 case 'u': /* %uabcd hex 4 */
2255 case 'U': /* %U1234abcd hex 8 */
2256 {
2257 int i;
2258
2259 switch (c)
2260 {
2261 case 'd': i = getdecchrs(); break;
2262 case 'o': i = getoctchrs(); break;
2263 case 'x': i = gethexchrs(2); break;
2264 case 'u': i = gethexchrs(4); break;
2265 case 'U': i = gethexchrs(8); break;
2266 default: i = -1; break;
2267 }
2268
2269 if (i < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002270 EMSG2_RET_NULL(
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002271 _("E678: Invalid character after %s%%[dxouU]"),
2272 reg_magic == MAGIC_ALL);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002273#ifdef FEAT_MBYTE
2274 if (use_multibytecode(i))
2275 ret = regnode(MULTIBYTECODE);
2276 else
2277#endif
2278 ret = regnode(EXACTLY);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002279 if (i == 0)
2280 regc(0x0a);
2281 else
2282#ifdef FEAT_MBYTE
2283 regmbc(i);
2284#else
2285 regc(i);
2286#endif
2287 regc(NUL);
2288 *flagp |= HASWIDTH;
2289 break;
2290 }
2291
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 default:
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00002293 if (VIM_ISDIGIT(c) || c == '<' || c == '>'
2294 || c == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 {
2296 long_u n = 0;
2297 int cmp;
2298
2299 cmp = c;
2300 if (cmp == '<' || cmp == '>')
2301 c = getchr();
2302 while (VIM_ISDIGIT(c))
2303 {
2304 n = n * 10 + (c - '0');
2305 c = getchr();
2306 }
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00002307 if (c == '\'' && n == 0)
2308 {
2309 /* "\%'m", "\%<'m" and "\%>'m": Mark */
2310 c = getchr();
2311 ret = regnode(RE_MARK);
2312 if (ret == JUST_CALC_SIZE)
2313 regsize += 2;
2314 else
2315 {
2316 *regcode++ = c;
2317 *regcode++ = cmp;
2318 }
2319 break;
2320 }
2321 else if (c == 'l' || c == 'c' || c == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 {
2323 if (c == 'l')
2324 ret = regnode(RE_LNUM);
2325 else if (c == 'c')
2326 ret = regnode(RE_COL);
2327 else
2328 ret = regnode(RE_VCOL);
2329 if (ret == JUST_CALC_SIZE)
2330 regsize += 5;
2331 else
2332 {
2333 /* put the number and the optional
2334 * comparator after the opcode */
2335 regcode = re_put_long(regcode, n);
2336 *regcode++ = cmp;
2337 }
2338 break;
2339 }
2340 }
2341
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002342 EMSG2_RET_NULL(_("E71: Invalid character after %s%%"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 reg_magic == MAGIC_ALL);
2344 }
2345 }
2346 break;
2347
2348 case Magic('['):
2349collection:
2350 {
2351 char_u *lp;
2352
2353 /*
2354 * If there is no matching ']', we assume the '[' is a normal
2355 * character. This makes 'incsearch' and ":help [" work.
2356 */
2357 lp = skip_anyof(regparse);
2358 if (*lp == ']') /* there is a matching ']' */
2359 {
2360 int startc = -1; /* > 0 when next '-' is a range */
2361 int endc;
2362
2363 /*
2364 * In a character class, different parsing rules apply.
2365 * Not even \ is special anymore, nothing is.
2366 */
2367 if (*regparse == '^') /* Complement of range. */
2368 {
2369 ret = regnode(ANYBUT + extra);
2370 regparse++;
2371 }
2372 else
2373 ret = regnode(ANYOF + extra);
2374
2375 /* At the start ']' and '-' mean the literal character. */
2376 if (*regparse == ']' || *regparse == '-')
Bram Moolenaardf177f62005-02-22 08:39:57 +00002377 {
2378 startc = *regparse;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 regc(*regparse++);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381
2382 while (*regparse != NUL && *regparse != ']')
2383 {
2384 if (*regparse == '-')
2385 {
2386 ++regparse;
2387 /* The '-' is not used for a range at the end and
2388 * after or before a '\n'. */
2389 if (*regparse == ']' || *regparse == NUL
2390 || startc == -1
2391 || (regparse[0] == '\\' && regparse[1] == 'n'))
2392 {
2393 regc('-');
2394 startc = '-'; /* [--x] is a range */
2395 }
2396 else
2397 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00002398 /* Also accept "a-[.z.]" */
2399 endc = 0;
2400 if (*regparse == '[')
2401 endc = get_coll_element(&regparse);
2402 if (endc == 0)
2403 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404#ifdef FEAT_MBYTE
Bram Moolenaardf177f62005-02-22 08:39:57 +00002405 if (has_mbyte)
2406 endc = mb_ptr2char_adv(&regparse);
2407 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00002409 endc = *regparse++;
2410 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002411
2412 /* Handle \o40, \x20 and \u20AC style sequences */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002413 if (endc == '\\' && !cpo_lit && !cpo_bsl)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002414 endc = coll_get_char();
2415
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 if (startc > endc)
2417 EMSG_RET_NULL(_(e_invrange));
2418#ifdef FEAT_MBYTE
2419 if (has_mbyte && ((*mb_char2len)(startc) > 1
2420 || (*mb_char2len)(endc) > 1))
2421 {
2422 /* Limit to a range of 256 chars */
2423 if (endc > startc + 256)
2424 EMSG_RET_NULL(_(e_invrange));
2425 while (++startc <= endc)
2426 regmbc(startc);
2427 }
2428 else
2429#endif
2430 {
2431#ifdef EBCDIC
2432 int alpha_only = FALSE;
2433
2434 /* for alphabetical range skip the gaps
2435 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
2436 if (isalpha(startc) && isalpha(endc))
2437 alpha_only = TRUE;
2438#endif
2439 while (++startc <= endc)
2440#ifdef EBCDIC
2441 if (!alpha_only || isalpha(startc))
2442#endif
2443 regc(startc);
2444 }
2445 startc = -1;
2446 }
2447 }
2448 /*
2449 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
2450 * accepts "\t", "\e", etc., but only when the 'l' flag in
2451 * 'cpoptions' is not included.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002452 * Posix doesn't recognize backslash at all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 */
2454 else if (*regparse == '\\'
Bram Moolenaardf177f62005-02-22 08:39:57 +00002455 && !cpo_bsl
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
2457 || (!cpo_lit
2458 && vim_strchr(REGEXP_ABBR,
2459 regparse[1]) != NULL)))
2460 {
2461 regparse++;
2462 if (*regparse == 'n')
2463 {
2464 /* '\n' in range: also match NL */
2465 if (ret != JUST_CALC_SIZE)
2466 {
Bram Moolenaare337e5f2013-01-30 18:21:51 +01002467 /* Using \n inside [^] does not change what
2468 * matches. "[^\n]" is the same as ".". */
2469 if (*ret == ANYOF)
2470 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 *ret = ANYOF + ADD_NL;
Bram Moolenaare337e5f2013-01-30 18:21:51 +01002472 *flagp |= HASNL;
2473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 /* else: must have had a \n already */
2475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 regparse++;
2477 startc = -1;
2478 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002479 else if (*regparse == 'd'
2480 || *regparse == 'o'
2481 || *regparse == 'x'
2482 || *regparse == 'u'
2483 || *regparse == 'U')
2484 {
2485 startc = coll_get_char();
2486 if (startc == 0)
2487 regc(0x0a);
2488 else
2489#ifdef FEAT_MBYTE
2490 regmbc(startc);
2491#else
2492 regc(startc);
2493#endif
2494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 else
2496 {
2497 startc = backslash_trans(*regparse++);
2498 regc(startc);
2499 }
2500 }
2501 else if (*regparse == '[')
2502 {
2503 int c_class;
2504 int cu;
2505
Bram Moolenaardf177f62005-02-22 08:39:57 +00002506 c_class = get_char_class(&regparse);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 startc = -1;
2508 /* Characters assumed to be 8 bits! */
2509 switch (c_class)
2510 {
2511 case CLASS_NONE:
Bram Moolenaardf177f62005-02-22 08:39:57 +00002512 c_class = get_equi_class(&regparse);
2513 if (c_class != 0)
2514 {
2515 /* produce equivalence class */
2516 reg_equi_class(c_class);
2517 }
2518 else if ((c_class =
2519 get_coll_element(&regparse)) != 0)
2520 {
2521 /* produce a collating element */
2522 regmbc(c_class);
2523 }
2524 else
2525 {
2526 /* literal '[', allow [[-x] as a range */
2527 startc = *regparse++;
2528 regc(startc);
2529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 break;
2531 case CLASS_ALNUM:
2532 for (cu = 1; cu <= 255; cu++)
2533 if (isalnum(cu))
2534 regc(cu);
2535 break;
2536 case CLASS_ALPHA:
2537 for (cu = 1; cu <= 255; cu++)
2538 if (isalpha(cu))
2539 regc(cu);
2540 break;
2541 case CLASS_BLANK:
2542 regc(' ');
2543 regc('\t');
2544 break;
2545 case CLASS_CNTRL:
2546 for (cu = 1; cu <= 255; cu++)
2547 if (iscntrl(cu))
2548 regc(cu);
2549 break;
2550 case CLASS_DIGIT:
2551 for (cu = 1; cu <= 255; cu++)
2552 if (VIM_ISDIGIT(cu))
2553 regc(cu);
2554 break;
2555 case CLASS_GRAPH:
2556 for (cu = 1; cu <= 255; cu++)
2557 if (isgraph(cu))
2558 regc(cu);
2559 break;
2560 case CLASS_LOWER:
2561 for (cu = 1; cu <= 255; cu++)
Bram Moolenaara245a5b2007-08-11 11:58:23 +00002562 if (MB_ISLOWER(cu))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 regc(cu);
2564 break;
2565 case CLASS_PRINT:
2566 for (cu = 1; cu <= 255; cu++)
2567 if (vim_isprintc(cu))
2568 regc(cu);
2569 break;
2570 case CLASS_PUNCT:
2571 for (cu = 1; cu <= 255; cu++)
2572 if (ispunct(cu))
2573 regc(cu);
2574 break;
2575 case CLASS_SPACE:
2576 for (cu = 9; cu <= 13; cu++)
2577 regc(cu);
2578 regc(' ');
2579 break;
2580 case CLASS_UPPER:
2581 for (cu = 1; cu <= 255; cu++)
Bram Moolenaara245a5b2007-08-11 11:58:23 +00002582 if (MB_ISUPPER(cu))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 regc(cu);
2584 break;
2585 case CLASS_XDIGIT:
2586 for (cu = 1; cu <= 255; cu++)
2587 if (vim_isxdigit(cu))
2588 regc(cu);
2589 break;
2590 case CLASS_TAB:
2591 regc('\t');
2592 break;
2593 case CLASS_RETURN:
2594 regc('\r');
2595 break;
2596 case CLASS_BACKSPACE:
2597 regc('\b');
2598 break;
2599 case CLASS_ESCAPE:
2600 regc('\033');
2601 break;
2602 }
2603 }
2604 else
2605 {
2606#ifdef FEAT_MBYTE
2607 if (has_mbyte)
2608 {
2609 int len;
2610
2611 /* produce a multibyte character, including any
2612 * following composing characters */
2613 startc = mb_ptr2char(regparse);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002614 len = (*mb_ptr2len)(regparse);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 if (enc_utf8 && utf_char2len(startc) != len)
2616 startc = -1; /* composing chars */
2617 while (--len >= 0)
2618 regc(*regparse++);
2619 }
2620 else
2621#endif
2622 {
2623 startc = *regparse++;
2624 regc(startc);
2625 }
2626 }
2627 }
2628 regc(NUL);
2629 prevchr_len = 1; /* last char was the ']' */
2630 if (*regparse != ']')
2631 EMSG_RET_NULL(_(e_toomsbra)); /* Cannot happen? */
2632 skipchr(); /* let's be friends with the lexer again */
2633 *flagp |= HASWIDTH | SIMPLE;
2634 break;
2635 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002636 else if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002637 EMSG2_RET_NULL(_(e_missingbracket), reg_magic > MAGIC_OFF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 }
2639 /* FALLTHROUGH */
2640
2641 default:
2642 {
2643 int len;
2644
2645#ifdef FEAT_MBYTE
2646 /* A multi-byte character is handled as a separate atom if it's
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002647 * before a multi and when it's a composing char. */
2648 if (use_multibytecode(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002650do_multibyte:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 ret = regnode(MULTIBYTECODE);
2652 regmbc(c);
2653 *flagp |= HASWIDTH | SIMPLE;
2654 break;
2655 }
2656#endif
2657
2658 ret = regnode(EXACTLY);
2659
2660 /*
2661 * Append characters as long as:
2662 * - there is no following multi, we then need the character in
2663 * front of it as a single character operand
2664 * - not running into a Magic character
2665 * - "one_exactly" is not set
2666 * But always emit at least one character. Might be a Multi,
2667 * e.g., a "[" without matching "]".
2668 */
2669 for (len = 0; c != NUL && (len == 0
2670 || (re_multi_type(peekchr()) == NOT_MULTI
2671 && !one_exactly
2672 && !is_Magic(c))); ++len)
2673 {
2674 c = no_Magic(c);
2675#ifdef FEAT_MBYTE
2676 if (has_mbyte)
2677 {
2678 regmbc(c);
2679 if (enc_utf8)
2680 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 int l;
2682
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002683 /* Need to get composing character too. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 for (;;)
2685 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002686 l = utf_ptr2len(regparse);
2687 if (!UTF_COMPOSINGLIKE(regparse, regparse + l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 break;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002689 regmbc(utf_ptr2char(regparse));
2690 skipchr();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 }
2693 }
2694 else
2695#endif
2696 regc(c);
2697 c = getchr();
2698 }
2699 ungetchr();
2700
2701 regc(NUL);
2702 *flagp |= HASWIDTH;
2703 if (len == 1)
2704 *flagp |= SIMPLE;
2705 }
2706 break;
2707 }
2708
2709 return ret;
2710}
2711
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002712#ifdef FEAT_MBYTE
2713/*
2714 * Return TRUE if MULTIBYTECODE should be used instead of EXACTLY for
2715 * character "c".
2716 */
2717 static int
2718use_multibytecode(c)
2719 int c;
2720{
2721 return has_mbyte && (*mb_char2len)(c) > 1
2722 && (re_multi_type(peekchr()) != NOT_MULTI
2723 || (enc_utf8 && utf_iscomposing(c)));
2724}
2725#endif
2726
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002728 * Emit a node.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 * Return pointer to generated code.
2730 */
2731 static char_u *
2732regnode(op)
2733 int op;
2734{
2735 char_u *ret;
2736
2737 ret = regcode;
2738 if (ret == JUST_CALC_SIZE)
2739 regsize += 3;
2740 else
2741 {
2742 *regcode++ = op;
2743 *regcode++ = NUL; /* Null "next" pointer. */
2744 *regcode++ = NUL;
2745 }
2746 return ret;
2747}
2748
2749/*
2750 * Emit (if appropriate) a byte of code
2751 */
2752 static void
2753regc(b)
2754 int b;
2755{
2756 if (regcode == JUST_CALC_SIZE)
2757 regsize++;
2758 else
2759 *regcode++ = b;
2760}
2761
2762#ifdef FEAT_MBYTE
2763/*
2764 * Emit (if appropriate) a multi-byte character of code
2765 */
2766 static void
2767regmbc(c)
2768 int c;
2769{
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02002770 if (!has_mbyte && c > 0xff)
2771 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 if (regcode == JUST_CALC_SIZE)
2773 regsize += (*mb_char2len)(c);
2774 else
2775 regcode += (*mb_char2bytes)(c, regcode);
2776}
2777#endif
2778
2779/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002780 * Insert an operator in front of already-emitted operand
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 *
2782 * Means relocating the operand.
2783 */
2784 static void
2785reginsert(op, opnd)
2786 int op;
2787 char_u *opnd;
2788{
2789 char_u *src;
2790 char_u *dst;
2791 char_u *place;
2792
2793 if (regcode == JUST_CALC_SIZE)
2794 {
2795 regsize += 3;
2796 return;
2797 }
2798 src = regcode;
2799 regcode += 3;
2800 dst = regcode;
2801 while (src > opnd)
2802 *--dst = *--src;
2803
2804 place = opnd; /* Op node, where operand used to be. */
2805 *place++ = op;
2806 *place++ = NUL;
2807 *place = NUL;
2808}
2809
2810/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002811 * Insert an operator in front of already-emitted operand.
Bram Moolenaar75eb1612013-05-29 18:45:11 +02002812 * Add a number to the operator.
2813 */
2814 static void
2815reginsert_nr(op, val, opnd)
2816 int op;
2817 long val;
2818 char_u *opnd;
2819{
2820 char_u *src;
2821 char_u *dst;
2822 char_u *place;
2823
2824 if (regcode == JUST_CALC_SIZE)
2825 {
2826 regsize += 7;
2827 return;
2828 }
2829 src = regcode;
2830 regcode += 7;
2831 dst = regcode;
2832 while (src > opnd)
2833 *--dst = *--src;
2834
2835 place = opnd; /* Op node, where operand used to be. */
2836 *place++ = op;
2837 *place++ = NUL;
2838 *place++ = NUL;
2839 place = re_put_long(place, (long_u)val);
2840}
2841
2842/*
2843 * Insert an operator in front of already-emitted operand.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 * The operator has the given limit values as operands. Also set next pointer.
2845 *
2846 * Means relocating the operand.
2847 */
2848 static void
2849reginsert_limits(op, minval, maxval, opnd)
2850 int op;
2851 long minval;
2852 long maxval;
2853 char_u *opnd;
2854{
2855 char_u *src;
2856 char_u *dst;
2857 char_u *place;
2858
2859 if (regcode == JUST_CALC_SIZE)
2860 {
2861 regsize += 11;
2862 return;
2863 }
2864 src = regcode;
2865 regcode += 11;
2866 dst = regcode;
2867 while (src > opnd)
2868 *--dst = *--src;
2869
2870 place = opnd; /* Op node, where operand used to be. */
2871 *place++ = op;
2872 *place++ = NUL;
2873 *place++ = NUL;
2874 place = re_put_long(place, (long_u)minval);
2875 place = re_put_long(place, (long_u)maxval);
2876 regtail(opnd, place);
2877}
2878
2879/*
2880 * Write a long as four bytes at "p" and return pointer to the next char.
2881 */
2882 static char_u *
2883re_put_long(p, val)
2884 char_u *p;
2885 long_u val;
2886{
2887 *p++ = (char_u) ((val >> 24) & 0377);
2888 *p++ = (char_u) ((val >> 16) & 0377);
2889 *p++ = (char_u) ((val >> 8) & 0377);
2890 *p++ = (char_u) (val & 0377);
2891 return p;
2892}
2893
2894/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002895 * Set the next-pointer at the end of a node chain.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 */
2897 static void
2898regtail(p, val)
2899 char_u *p;
2900 char_u *val;
2901{
2902 char_u *scan;
2903 char_u *temp;
2904 int offset;
2905
2906 if (p == JUST_CALC_SIZE)
2907 return;
2908
2909 /* Find last node. */
2910 scan = p;
2911 for (;;)
2912 {
2913 temp = regnext(scan);
2914 if (temp == NULL)
2915 break;
2916 scan = temp;
2917 }
2918
Bram Moolenaar582fd852005-03-28 20:58:01 +00002919 if (OP(scan) == BACK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 offset = (int)(scan - val);
2921 else
2922 offset = (int)(val - scan);
Bram Moolenaard3005802009-11-25 17:21:32 +00002923 /* When the offset uses more than 16 bits it can no longer fit in the two
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02002924 * bytes available. Use a global flag to avoid having to check return
Bram Moolenaard3005802009-11-25 17:21:32 +00002925 * values in too many places. */
2926 if (offset > 0xffff)
2927 reg_toolong = TRUE;
2928 else
2929 {
2930 *(scan + 1) = (char_u) (((unsigned)offset >> 8) & 0377);
2931 *(scan + 2) = (char_u) (offset & 0377);
2932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933}
2934
2935/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002936 * Like regtail, on item after a BRANCH; nop if none.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 */
2938 static void
2939regoptail(p, val)
2940 char_u *p;
2941 char_u *val;
2942{
2943 /* When op is neither BRANCH nor BRACE_COMPLEX0-9, it is "operandless" */
2944 if (p == NULL || p == JUST_CALC_SIZE
2945 || (OP(p) != BRANCH
2946 && (OP(p) < BRACE_COMPLEX || OP(p) > BRACE_COMPLEX + 9)))
2947 return;
2948 regtail(OPERAND(p), val);
2949}
2950
2951/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002952 * Functions for getting characters from the regexp input.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 */
2954
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955static int at_start; /* True when on the first character */
2956static int prev_at_start; /* True when on the second character */
2957
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002958/*
2959 * Start parsing at "str".
2960 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 static void
2962initchr(str)
2963 char_u *str;
2964{
2965 regparse = str;
2966 prevchr_len = 0;
2967 curchr = prevprevchr = prevchr = nextchr = -1;
2968 at_start = TRUE;
2969 prev_at_start = FALSE;
2970}
2971
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002972/*
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002973 * Save the current parse state, so that it can be restored and parsing
2974 * starts in the same state again.
2975 */
2976 static void
2977save_parse_state(ps)
2978 parse_state_T *ps;
2979{
2980 ps->regparse = regparse;
2981 ps->prevchr_len = prevchr_len;
2982 ps->curchr = curchr;
2983 ps->prevchr = prevchr;
2984 ps->prevprevchr = prevprevchr;
2985 ps->nextchr = nextchr;
2986 ps->at_start = at_start;
2987 ps->prev_at_start = prev_at_start;
2988 ps->regnpar = regnpar;
2989}
2990
2991/*
2992 * Restore a previously saved parse state.
2993 */
2994 static void
2995restore_parse_state(ps)
2996 parse_state_T *ps;
2997{
2998 regparse = ps->regparse;
2999 prevchr_len = ps->prevchr_len;
3000 curchr = ps->curchr;
3001 prevchr = ps->prevchr;
3002 prevprevchr = ps->prevprevchr;
3003 nextchr = ps->nextchr;
3004 at_start = ps->at_start;
3005 prev_at_start = ps->prev_at_start;
3006 regnpar = ps->regnpar;
3007}
3008
3009
3010/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003011 * Get the next character without advancing.
3012 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 static int
3014peekchr()
3015{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003016 static int after_slash = FALSE;
3017
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 if (curchr == -1)
3019 {
3020 switch (curchr = regparse[0])
3021 {
3022 case '.':
3023 case '[':
3024 case '~':
3025 /* magic when 'magic' is on */
3026 if (reg_magic >= MAGIC_ON)
3027 curchr = Magic(curchr);
3028 break;
3029 case '(':
3030 case ')':
3031 case '{':
3032 case '%':
3033 case '+':
3034 case '=':
3035 case '?':
3036 case '@':
3037 case '!':
3038 case '&':
3039 case '|':
3040 case '<':
3041 case '>':
3042 case '#': /* future ext. */
3043 case '"': /* future ext. */
3044 case '\'': /* future ext. */
3045 case ',': /* future ext. */
3046 case '-': /* future ext. */
3047 case ':': /* future ext. */
3048 case ';': /* future ext. */
3049 case '`': /* future ext. */
3050 case '/': /* Can't be used in / command */
3051 /* magic only after "\v" */
3052 if (reg_magic == MAGIC_ALL)
3053 curchr = Magic(curchr);
3054 break;
3055 case '*':
Bram Moolenaardf177f62005-02-22 08:39:57 +00003056 /* * is not magic as the very first character, eg "?*ptr", when
3057 * after '^', eg "/^*ptr" and when after "\(", "\|", "\&". But
3058 * "\(\*" is not magic, thus must be magic if "after_slash" */
3059 if (reg_magic >= MAGIC_ON
3060 && !at_start
3061 && !(prev_at_start && prevchr == Magic('^'))
3062 && (after_slash
3063 || (prevchr != Magic('(')
3064 && prevchr != Magic('&')
3065 && prevchr != Magic('|'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 curchr = Magic('*');
3067 break;
3068 case '^':
3069 /* '^' is only magic as the very first character and if it's after
3070 * "\(", "\|", "\&' or "\n" */
3071 if (reg_magic >= MAGIC_OFF
3072 && (at_start
3073 || reg_magic == MAGIC_ALL
3074 || prevchr == Magic('(')
3075 || prevchr == Magic('|')
3076 || prevchr == Magic('&')
3077 || prevchr == Magic('n')
3078 || (no_Magic(prevchr) == '('
3079 && prevprevchr == Magic('%'))))
3080 {
3081 curchr = Magic('^');
3082 at_start = TRUE;
3083 prev_at_start = FALSE;
3084 }
3085 break;
3086 case '$':
3087 /* '$' is only magic as the very last char and if it's in front of
3088 * either "\|", "\)", "\&", or "\n" */
3089 if (reg_magic >= MAGIC_OFF)
3090 {
3091 char_u *p = regparse + 1;
3092
3093 /* ignore \c \C \m and \M after '$' */
3094 while (p[0] == '\\' && (p[1] == 'c' || p[1] == 'C'
3095 || p[1] == 'm' || p[1] == 'M' || p[1] == 'Z'))
3096 p += 2;
3097 if (p[0] == NUL
3098 || (p[0] == '\\'
3099 && (p[1] == '|' || p[1] == '&' || p[1] == ')'
3100 || p[1] == 'n'))
3101 || reg_magic == MAGIC_ALL)
3102 curchr = Magic('$');
3103 }
3104 break;
3105 case '\\':
3106 {
3107 int c = regparse[1];
3108
3109 if (c == NUL)
3110 curchr = '\\'; /* trailing '\' */
3111 else if (
3112#ifdef EBCDIC
3113 vim_strchr(META, c)
3114#else
3115 c <= '~' && META_flags[c]
3116#endif
3117 )
3118 {
3119 /*
3120 * META contains everything that may be magic sometimes,
3121 * except ^ and $ ("\^" and "\$" are only magic after
3122 * "\v"). We now fetch the next character and toggle its
3123 * magicness. Therefore, \ is so meta-magic that it is
3124 * not in META.
3125 */
3126 curchr = -1;
3127 prev_at_start = at_start;
3128 at_start = FALSE; /* be able to say "/\*ptr" */
3129 ++regparse;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003130 ++after_slash;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 peekchr();
3132 --regparse;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003133 --after_slash;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 curchr = toggle_Magic(curchr);
3135 }
3136 else if (vim_strchr(REGEXP_ABBR, c))
3137 {
3138 /*
3139 * Handle abbreviations, like "\t" for TAB -- webb
3140 */
3141 curchr = backslash_trans(c);
3142 }
3143 else if (reg_magic == MAGIC_NONE && (c == '$' || c == '^'))
3144 curchr = toggle_Magic(c);
3145 else
3146 {
3147 /*
3148 * Next character can never be (made) magic?
3149 * Then backslashing it won't do anything.
3150 */
3151#ifdef FEAT_MBYTE
3152 if (has_mbyte)
3153 curchr = (*mb_ptr2char)(regparse + 1);
3154 else
3155#endif
3156 curchr = c;
3157 }
3158 break;
3159 }
3160
3161#ifdef FEAT_MBYTE
3162 default:
3163 if (has_mbyte)
3164 curchr = (*mb_ptr2char)(regparse);
3165#endif
3166 }
3167 }
3168
3169 return curchr;
3170}
3171
3172/*
3173 * Eat one lexed character. Do this in a way that we can undo it.
3174 */
3175 static void
3176skipchr()
3177{
3178 /* peekchr() eats a backslash, do the same here */
3179 if (*regparse == '\\')
3180 prevchr_len = 1;
3181 else
3182 prevchr_len = 0;
3183 if (regparse[prevchr_len] != NUL)
3184 {
3185#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003186 if (enc_utf8)
Bram Moolenaar8f5c5782007-11-29 20:27:21 +00003187 /* exclude composing chars that mb_ptr2len does include */
3188 prevchr_len += utf_ptr2len(regparse + prevchr_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003189 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003190 prevchr_len += (*mb_ptr2len)(regparse + prevchr_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 else
3192#endif
3193 ++prevchr_len;
3194 }
3195 regparse += prevchr_len;
3196 prev_at_start = at_start;
3197 at_start = FALSE;
3198 prevprevchr = prevchr;
3199 prevchr = curchr;
3200 curchr = nextchr; /* use previously unget char, or -1 */
3201 nextchr = -1;
3202}
3203
3204/*
3205 * Skip a character while keeping the value of prev_at_start for at_start.
3206 * prevchr and prevprevchr are also kept.
3207 */
3208 static void
3209skipchr_keepstart()
3210{
3211 int as = prev_at_start;
3212 int pr = prevchr;
3213 int prpr = prevprevchr;
3214
3215 skipchr();
3216 at_start = as;
3217 prevchr = pr;
3218 prevprevchr = prpr;
3219}
3220
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003221/*
3222 * Get the next character from the pattern. We know about magic and such, so
3223 * therefore we need a lexical analyzer.
3224 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 static int
3226getchr()
3227{
3228 int chr = peekchr();
3229
3230 skipchr();
3231 return chr;
3232}
3233
3234/*
3235 * put character back. Works only once!
3236 */
3237 static void
3238ungetchr()
3239{
3240 nextchr = curchr;
3241 curchr = prevchr;
3242 prevchr = prevprevchr;
3243 at_start = prev_at_start;
3244 prev_at_start = FALSE;
3245
3246 /* Backup regparse, so that it's at the same position as before the
3247 * getchr(). */
3248 regparse -= prevchr_len;
3249}
3250
3251/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00003252 * Get and return the value of the hex string at the current position.
3253 * Return -1 if there is no valid hex number.
3254 * The position is updated:
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003255 * blahblah\%x20asdf
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003256 * before-^ ^-after
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003257 * The parameter controls the maximum number of input characters. This will be
3258 * 2 when reading a \%x20 sequence and 4 when reading a \%u20AC sequence.
3259 */
3260 static int
3261gethexchrs(maxinputlen)
3262 int maxinputlen;
3263{
3264 int nr = 0;
3265 int c;
3266 int i;
3267
3268 for (i = 0; i < maxinputlen; ++i)
3269 {
3270 c = regparse[0];
3271 if (!vim_isxdigit(c))
3272 break;
3273 nr <<= 4;
3274 nr |= hex2nr(c);
3275 ++regparse;
3276 }
3277
3278 if (i == 0)
3279 return -1;
3280 return nr;
3281}
3282
3283/*
Bram Moolenaar75eb1612013-05-29 18:45:11 +02003284 * Get and return the value of the decimal string immediately after the
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003285 * current position. Return -1 for invalid. Consumes all digits.
3286 */
3287 static int
3288getdecchrs()
3289{
3290 int nr = 0;
3291 int c;
3292 int i;
3293
3294 for (i = 0; ; ++i)
3295 {
3296 c = regparse[0];
3297 if (c < '0' || c > '9')
3298 break;
3299 nr *= 10;
3300 nr += c - '0';
3301 ++regparse;
Bram Moolenaar75eb1612013-05-29 18:45:11 +02003302 curchr = -1; /* no longer valid */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003303 }
3304
3305 if (i == 0)
3306 return -1;
3307 return nr;
3308}
3309
3310/*
3311 * get and return the value of the octal string immediately after the current
3312 * position. Return -1 for invalid, or 0-255 for valid. Smart enough to handle
3313 * numbers > 377 correctly (for example, 400 is treated as 40) and doesn't
3314 * treat 8 or 9 as recognised characters. Position is updated:
3315 * blahblah\%o210asdf
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003316 * before-^ ^-after
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003317 */
3318 static int
3319getoctchrs()
3320{
3321 int nr = 0;
3322 int c;
3323 int i;
3324
3325 for (i = 0; i < 3 && nr < 040; ++i)
3326 {
3327 c = regparse[0];
3328 if (c < '0' || c > '7')
3329 break;
3330 nr <<= 3;
3331 nr |= hex2nr(c);
3332 ++regparse;
3333 }
3334
3335 if (i == 0)
3336 return -1;
3337 return nr;
3338}
3339
3340/*
3341 * Get a number after a backslash that is inside [].
3342 * When nothing is recognized return a backslash.
3343 */
3344 static int
3345coll_get_char()
3346{
3347 int nr = -1;
3348
3349 switch (*regparse++)
3350 {
3351 case 'd': nr = getdecchrs(); break;
3352 case 'o': nr = getoctchrs(); break;
3353 case 'x': nr = gethexchrs(2); break;
3354 case 'u': nr = gethexchrs(4); break;
3355 case 'U': nr = gethexchrs(8); break;
3356 }
3357 if (nr < 0)
3358 {
3359 /* If getting the number fails be backwards compatible: the character
3360 * is a backslash. */
3361 --regparse;
3362 nr = '\\';
3363 }
3364 return nr;
3365}
3366
3367/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 * read_limits - Read two integers to be taken as a minimum and maximum.
3369 * If the first character is '-', then the range is reversed.
3370 * Should end with 'end'. If minval is missing, zero is default, if maxval is
3371 * missing, a very big number is the default.
3372 */
3373 static int
3374read_limits(minval, maxval)
3375 long *minval;
3376 long *maxval;
3377{
3378 int reverse = FALSE;
3379 char_u *first_char;
3380 long tmp;
3381
3382 if (*regparse == '-')
3383 {
3384 /* Starts with '-', so reverse the range later */
3385 regparse++;
3386 reverse = TRUE;
3387 }
3388 first_char = regparse;
3389 *minval = getdigits(&regparse);
3390 if (*regparse == ',') /* There is a comma */
3391 {
3392 if (vim_isdigit(*++regparse))
3393 *maxval = getdigits(&regparse);
3394 else
3395 *maxval = MAX_LIMIT;
3396 }
3397 else if (VIM_ISDIGIT(*first_char))
3398 *maxval = *minval; /* It was \{n} or \{-n} */
3399 else
3400 *maxval = MAX_LIMIT; /* It was \{} or \{-} */
3401 if (*regparse == '\\')
3402 regparse++; /* Allow either \{...} or \{...\} */
Bram Moolenaardf177f62005-02-22 08:39:57 +00003403 if (*regparse != '}')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 {
3405 sprintf((char *)IObuff, _("E554: Syntax error in %s{...}"),
3406 reg_magic == MAGIC_ALL ? "" : "\\");
3407 EMSG_RET_FAIL(IObuff);
3408 }
3409
3410 /*
3411 * Reverse the range if there was a '-', or make sure it is in the right
3412 * order otherwise.
3413 */
3414 if ((!reverse && *minval > *maxval) || (reverse && *minval < *maxval))
3415 {
3416 tmp = *minval;
3417 *minval = *maxval;
3418 *maxval = tmp;
3419 }
3420 skipchr(); /* let's be friends with the lexer again */
3421 return OK;
3422}
3423
3424/*
3425 * vim_regexec and friends
3426 */
3427
3428/*
3429 * Global work variables for vim_regexec().
3430 */
3431
3432/* The current match-position is remembered with these variables: */
3433static linenr_T reglnum; /* line number, relative to first line */
3434static char_u *regline; /* start of current line */
3435static char_u *reginput; /* current input, points into "regline" */
3436
3437static int need_clear_subexpr; /* subexpressions still need to be
3438 * cleared */
3439#ifdef FEAT_SYN_HL
3440static int need_clear_zsubexpr = FALSE; /* extmatch subexpressions
3441 * still need to be cleared */
3442#endif
3443
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444/*
3445 * Structure used to save the current input state, when it needs to be
3446 * restored after trying a match. Used by reg_save() and reg_restore().
Bram Moolenaar582fd852005-03-28 20:58:01 +00003447 * Also stores the length of "backpos".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 */
3449typedef struct
3450{
3451 union
3452 {
3453 char_u *ptr; /* reginput pointer, for single-line regexp */
3454 lpos_T pos; /* reginput pos, for multi-line regexp */
3455 } rs_u;
Bram Moolenaar582fd852005-03-28 20:58:01 +00003456 int rs_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457} regsave_T;
3458
3459/* struct to save start/end pointer/position in for \(\) */
3460typedef struct
3461{
3462 union
3463 {
3464 char_u *ptr;
3465 lpos_T pos;
3466 } se_u;
3467} save_se_T;
3468
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00003469/* used for BEHIND and NOBEHIND matching */
3470typedef struct regbehind_S
3471{
3472 regsave_T save_after;
3473 regsave_T save_behind;
Bram Moolenaarfde483c2008-06-15 12:21:50 +00003474 int save_need_clear_subexpr;
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00003475 save_se_T save_start[NSUBEXP];
3476 save_se_T save_end[NSUBEXP];
3477} regbehind_T;
3478
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479static char_u *reg_getline __ARGS((linenr_T lnum));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003480static long bt_regexec_both __ARGS((char_u *line, colnr_T col, proftime_T *tm));
3481static long regtry __ARGS((bt_regprog_T *prog, colnr_T col));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482static void cleanup_subexpr __ARGS((void));
3483#ifdef FEAT_SYN_HL
3484static void cleanup_zsubexpr __ARGS((void));
3485#endif
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00003486static void save_subexpr __ARGS((regbehind_T *bp));
3487static void restore_subexpr __ARGS((regbehind_T *bp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488static void reg_nextline __ARGS((void));
Bram Moolenaar582fd852005-03-28 20:58:01 +00003489static void reg_save __ARGS((regsave_T *save, garray_T *gap));
3490static void reg_restore __ARGS((regsave_T *save, garray_T *gap));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491static int reg_save_equal __ARGS((regsave_T *save));
3492static void save_se_multi __ARGS((save_se_T *savep, lpos_T *posp));
3493static void save_se_one __ARGS((save_se_T *savep, char_u **pp));
3494
3495/* Save the sub-expressions before attempting a match. */
3496#define save_se(savep, posp, pp) \
3497 REG_MULTI ? save_se_multi((savep), (posp)) : save_se_one((savep), (pp))
3498
3499/* After a failed match restore the sub-expressions. */
3500#define restore_se(savep, posp, pp) { \
3501 if (REG_MULTI) \
3502 *(posp) = (savep)->se_u.pos; \
3503 else \
3504 *(pp) = (savep)->se_u.ptr; }
3505
3506static int re_num_cmp __ARGS((long_u val, char_u *scan));
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003507static int regmatch __ARGS((char_u *prog));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508static int regrepeat __ARGS((char_u *p, long maxcount));
3509
3510#ifdef DEBUG
3511int regnarrate = 0;
3512#endif
3513
3514/*
3515 * Internal copy of 'ignorecase'. It is set at each call to vim_regexec().
3516 * Normally it gets the value of "rm_ic" or "rmm_ic", but when the pattern
3517 * contains '\c' or '\C' the value is overruled.
3518 */
3519static int ireg_ic;
3520
3521#ifdef FEAT_MBYTE
3522/*
3523 * Similar to ireg_ic, but only for 'combining' characters. Set with \Z flag
3524 * in the regexp. Defaults to false, always.
3525 */
3526static int ireg_icombine;
3527#endif
3528
3529/*
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003530 * Copy of "rmm_maxcol": maximum column to search for a match. Zero when
3531 * there is no maximum.
3532 */
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003533static colnr_T ireg_maxcol;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003534
3535/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 * Sometimes need to save a copy of a line. Since alloc()/free() is very
3537 * slow, we keep one allocated piece of memory and only re-allocate it when
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003538 * it's too small. It's freed in bt_regexec_both() when finished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 */
Bram Moolenaard4210772008-01-02 14:35:30 +00003540static char_u *reg_tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541static unsigned reg_tofreelen;
3542
3543/*
3544 * These variables are set when executing a regexp to speed up the execution.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00003545 * Which ones are set depends on whether a single-line or multi-line match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 * done:
3547 * single-line multi-line
3548 * reg_match &regmatch_T NULL
3549 * reg_mmatch NULL &regmmatch_T
3550 * reg_startp reg_match->startp <invalid>
3551 * reg_endp reg_match->endp <invalid>
3552 * reg_startpos <invalid> reg_mmatch->startpos
3553 * reg_endpos <invalid> reg_mmatch->endpos
3554 * reg_win NULL window in which to search
Bram Moolenaar2f315ab2013-01-25 20:11:01 +01003555 * reg_buf curbuf buffer in which to search
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 * reg_firstlnum <invalid> first line in which to search
3557 * reg_maxline 0 last line nr
3558 * reg_line_lbr FALSE or TRUE FALSE
3559 */
3560static regmatch_T *reg_match;
3561static regmmatch_T *reg_mmatch;
3562static char_u **reg_startp = NULL;
3563static char_u **reg_endp = NULL;
3564static lpos_T *reg_startpos = NULL;
3565static lpos_T *reg_endpos = NULL;
3566static win_T *reg_win;
3567static buf_T *reg_buf;
3568static linenr_T reg_firstlnum;
3569static linenr_T reg_maxline;
3570static int reg_line_lbr; /* "\n" in string is line break */
3571
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003572/* Values for rs_state in regitem_T. */
3573typedef enum regstate_E
3574{
3575 RS_NOPEN = 0 /* NOPEN and NCLOSE */
3576 , RS_MOPEN /* MOPEN + [0-9] */
3577 , RS_MCLOSE /* MCLOSE + [0-9] */
3578#ifdef FEAT_SYN_HL
3579 , RS_ZOPEN /* ZOPEN + [0-9] */
3580 , RS_ZCLOSE /* ZCLOSE + [0-9] */
3581#endif
3582 , RS_BRANCH /* BRANCH */
3583 , RS_BRCPLX_MORE /* BRACE_COMPLEX and trying one more match */
3584 , RS_BRCPLX_LONG /* BRACE_COMPLEX and trying longest match */
3585 , RS_BRCPLX_SHORT /* BRACE_COMPLEX and trying shortest match */
3586 , RS_NOMATCH /* NOMATCH */
3587 , RS_BEHIND1 /* BEHIND / NOBEHIND matching rest */
3588 , RS_BEHIND2 /* BEHIND / NOBEHIND matching behind part */
3589 , RS_STAR_LONG /* STAR/PLUS/BRACE_SIMPLE longest match */
3590 , RS_STAR_SHORT /* STAR/PLUS/BRACE_SIMPLE shortest match */
3591} regstate_T;
3592
3593/*
3594 * When there are alternatives a regstate_T is put on the regstack to remember
3595 * what we are doing.
3596 * Before it may be another type of item, depending on rs_state, to remember
3597 * more things.
3598 */
3599typedef struct regitem_S
3600{
3601 regstate_T rs_state; /* what we are doing, one of RS_ above */
3602 char_u *rs_scan; /* current node in program */
3603 union
3604 {
3605 save_se_T sesave;
3606 regsave_T regsave;
3607 } rs_un; /* room for saving reginput */
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00003608 short rs_no; /* submatch nr or BEHIND/NOBEHIND */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003609} regitem_T;
3610
3611static regitem_T *regstack_push __ARGS((regstate_T state, char_u *scan));
3612static void regstack_pop __ARGS((char_u **scan));
3613
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003614/* used for STAR, PLUS and BRACE_SIMPLE matching */
3615typedef struct regstar_S
3616{
3617 int nextb; /* next byte */
3618 int nextb_ic; /* next byte reverse case */
3619 long count;
3620 long minval;
3621 long maxval;
3622} regstar_T;
3623
3624/* used to store input position when a BACK was encountered, so that we now if
3625 * we made any progress since the last time. */
3626typedef struct backpos_S
3627{
3628 char_u *bp_scan; /* "scan" where BACK was encountered */
3629 regsave_T bp_pos; /* last input position */
3630} backpos_T;
3631
3632/*
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003633 * "regstack" and "backpos" are used by regmatch(). They are kept over calls
3634 * to avoid invoking malloc() and free() often.
3635 * "regstack" is a stack with regitem_T items, sometimes preceded by regstar_T
3636 * or regbehind_T.
3637 * "backpos_T" is a table with backpos_T for BACK
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003638 */
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003639static garray_T regstack = {0, 0, 0, 0, NULL};
3640static garray_T backpos = {0, 0, 0, 0, NULL};
3641
3642/*
3643 * Both for regstack and backpos tables we use the following strategy of
3644 * allocation (to reduce malloc/free calls):
3645 * - Initial size is fairly small.
3646 * - When needed, the tables are grown bigger (8 times at first, double after
3647 * that).
3648 * - After executing the match we free the memory only if the array has grown.
3649 * Thus the memory is kept allocated when it's at the initial size.
3650 * This makes it fast while not keeping a lot of memory allocated.
3651 * A three times speed increase was observed when using many simple patterns.
3652 */
3653#define REGSTACK_INITIAL 2048
3654#define BACKPOS_INITIAL 64
3655
3656#if defined(EXITFREE) || defined(PROTO)
3657 void
3658free_regexp_stuff()
3659{
3660 ga_clear(&regstack);
3661 ga_clear(&backpos);
3662 vim_free(reg_tofree);
3663 vim_free(reg_prev_sub);
3664}
3665#endif
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003666
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667/*
3668 * Get pointer to the line "lnum", which is relative to "reg_firstlnum".
3669 */
3670 static char_u *
3671reg_getline(lnum)
3672 linenr_T lnum;
3673{
3674 /* when looking behind for a match/no-match lnum is negative. But we
3675 * can't go before line 1 */
3676 if (reg_firstlnum + lnum < 1)
3677 return NULL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00003678 if (lnum > reg_maxline)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003679 /* Must have matched the "\n" in the last line. */
3680 return (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 return ml_get_buf(reg_buf, reg_firstlnum + lnum, FALSE);
3682}
3683
3684static regsave_T behind_pos;
3685
3686#ifdef FEAT_SYN_HL
3687static char_u *reg_startzp[NSUBEXP]; /* Workspace to mark beginning */
3688static char_u *reg_endzp[NSUBEXP]; /* and end of \z(...\) matches */
3689static lpos_T reg_startzpos[NSUBEXP]; /* idem, beginning pos */
3690static lpos_T reg_endzpos[NSUBEXP]; /* idem, end pos */
3691#endif
3692
3693/* TRUE if using multi-line regexp. */
3694#define REG_MULTI (reg_match == NULL)
3695
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003696static int bt_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
3697
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698/*
3699 * Match a regexp against a string.
3700 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
3701 * Uses curbuf for line count and 'iskeyword'.
3702 *
3703 * Return TRUE if there is a match, FALSE if not.
3704 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003705 static int
3706bt_regexec(rmp, line, col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 regmatch_T *rmp;
3708 char_u *line; /* string to match against */
3709 colnr_T col; /* column to start looking for match */
3710{
3711 reg_match = rmp;
3712 reg_mmatch = NULL;
3713 reg_maxline = 0;
3714 reg_line_lbr = FALSE;
Bram Moolenaar2f315ab2013-01-25 20:11:01 +01003715 reg_buf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 reg_win = NULL;
3717 ireg_ic = rmp->rm_ic;
3718#ifdef FEAT_MBYTE
3719 ireg_icombine = FALSE;
3720#endif
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003721 ireg_maxcol = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003722 return (bt_regexec_both(line, col, NULL) != 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723}
3724
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003725#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
3726 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003727
3728static int bt_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
3729
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730/*
3731 * Like vim_regexec(), but consider a "\n" in "line" to be a line break.
3732 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003733 static int
3734bt_regexec_nl(rmp, line, col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 regmatch_T *rmp;
3736 char_u *line; /* string to match against */
3737 colnr_T col; /* column to start looking for match */
3738{
3739 reg_match = rmp;
3740 reg_mmatch = NULL;
3741 reg_maxline = 0;
3742 reg_line_lbr = TRUE;
Bram Moolenaar2f315ab2013-01-25 20:11:01 +01003743 reg_buf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 reg_win = NULL;
3745 ireg_ic = rmp->rm_ic;
3746#ifdef FEAT_MBYTE
3747 ireg_icombine = FALSE;
3748#endif
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003749 ireg_maxcol = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003750 return (bt_regexec_both(line, col, NULL) != 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751}
3752#endif
3753
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003754static long bt_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm));
3755
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756/*
3757 * Match a regexp against multiple lines.
3758 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
3759 * Uses curbuf for line count and 'iskeyword'.
3760 *
3761 * Return zero if there is no match. Return number of lines contained in the
3762 * match otherwise.
3763 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003764 static long
3765bt_regexec_multi(rmp, win, buf, lnum, col, tm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 regmmatch_T *rmp;
3767 win_T *win; /* window in which to search or NULL */
3768 buf_T *buf; /* buffer in which to search */
3769 linenr_T lnum; /* nr of line to start looking for match */
3770 colnr_T col; /* column to start looking for match */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003771 proftime_T *tm; /* timeout limit or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772{
3773 long r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774
3775 reg_match = NULL;
3776 reg_mmatch = rmp;
3777 reg_buf = buf;
3778 reg_win = win;
3779 reg_firstlnum = lnum;
3780 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
3781 reg_line_lbr = FALSE;
3782 ireg_ic = rmp->rmm_ic;
3783#ifdef FEAT_MBYTE
3784 ireg_icombine = FALSE;
3785#endif
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003786 ireg_maxcol = rmp->rmm_maxcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003788 r = bt_regexec_both(NULL, col, tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789
3790 return r;
3791}
3792
3793/*
3794 * Match a regexp against a string ("line" points to the string) or multiple
3795 * lines ("line" is NULL, use reg_getline()).
3796 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 static long
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003798bt_regexec_both(line, col, tm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 char_u *line;
3800 colnr_T col; /* column to start looking for match */
Bram Moolenaar78a15312009-05-15 19:33:18 +00003801 proftime_T *tm UNUSED; /* timeout limit or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003803 bt_regprog_T *prog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 char_u *s;
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003805 long retval = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003807 /* Create "regstack" and "backpos" if they are not allocated yet.
3808 * We allocate *_INITIAL amount of bytes first and then set the grow size
3809 * to much bigger value to avoid many malloc calls in case of deep regular
3810 * expressions. */
3811 if (regstack.ga_data == NULL)
3812 {
3813 /* Use an item size of 1 byte, since we push different things
3814 * onto the regstack. */
3815 ga_init2(&regstack, 1, REGSTACK_INITIAL);
3816 ga_grow(&regstack, REGSTACK_INITIAL);
3817 regstack.ga_growsize = REGSTACK_INITIAL * 8;
3818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003820 if (backpos.ga_data == NULL)
3821 {
3822 ga_init2(&backpos, sizeof(backpos_T), BACKPOS_INITIAL);
3823 ga_grow(&backpos, BACKPOS_INITIAL);
3824 backpos.ga_growsize = BACKPOS_INITIAL * 8;
3825 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003826
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 if (REG_MULTI)
3828 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003829 prog = (bt_regprog_T *)reg_mmatch->regprog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 line = reg_getline((linenr_T)0);
3831 reg_startpos = reg_mmatch->startpos;
3832 reg_endpos = reg_mmatch->endpos;
3833 }
3834 else
3835 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003836 prog = (bt_regprog_T *)reg_match->regprog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 reg_startp = reg_match->startp;
3838 reg_endp = reg_match->endp;
3839 }
3840
3841 /* Be paranoid... */
3842 if (prog == NULL || line == NULL)
3843 {
3844 EMSG(_(e_null));
3845 goto theend;
3846 }
3847
3848 /* Check validity of program. */
3849 if (prog_magic_wrong())
3850 goto theend;
3851
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003852 /* If the start column is past the maximum column: no need to try. */
3853 if (ireg_maxcol > 0 && col >= ireg_maxcol)
3854 goto theend;
3855
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
3857 if (prog->regflags & RF_ICASE)
3858 ireg_ic = TRUE;
3859 else if (prog->regflags & RF_NOICASE)
3860 ireg_ic = FALSE;
3861
3862#ifdef FEAT_MBYTE
3863 /* If pattern contains "\Z" overrule value of ireg_icombine */
3864 if (prog->regflags & RF_ICOMBINE)
3865 ireg_icombine = TRUE;
3866#endif
3867
3868 /* If there is a "must appear" string, look for it. */
3869 if (prog->regmust != NULL)
3870 {
3871 int c;
3872
3873#ifdef FEAT_MBYTE
3874 if (has_mbyte)
3875 c = (*mb_ptr2char)(prog->regmust);
3876 else
3877#endif
3878 c = *prog->regmust;
3879 s = line + col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003880
3881 /*
3882 * This is used very often, esp. for ":global". Use three versions of
3883 * the loop to avoid overhead of conditions.
3884 */
3885 if (!ireg_ic
3886#ifdef FEAT_MBYTE
3887 && !has_mbyte
3888#endif
3889 )
3890 while ((s = vim_strbyte(s, c)) != NULL)
3891 {
3892 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3893 break; /* Found it. */
3894 ++s;
3895 }
3896#ifdef FEAT_MBYTE
3897 else if (!ireg_ic || (!enc_utf8 && mb_char2len(c) > 1))
3898 while ((s = vim_strchr(s, c)) != NULL)
3899 {
3900 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3901 break; /* Found it. */
3902 mb_ptr_adv(s);
3903 }
3904#endif
3905 else
3906 while ((s = cstrchr(s, c)) != NULL)
3907 {
3908 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3909 break; /* Found it. */
3910 mb_ptr_adv(s);
3911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 if (s == NULL) /* Not present. */
3913 goto theend;
3914 }
3915
3916 regline = line;
3917 reglnum = 0;
Bram Moolenaar73a92fe2010-09-14 10:55:47 +02003918 reg_toolong = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919
3920 /* Simplest case: Anchored match need be tried only once. */
3921 if (prog->reganch)
3922 {
3923 int c;
3924
3925#ifdef FEAT_MBYTE
3926 if (has_mbyte)
3927 c = (*mb_ptr2char)(regline + col);
3928 else
3929#endif
3930 c = regline[col];
3931 if (prog->regstart == NUL
3932 || prog->regstart == c
3933 || (ireg_ic && ((
3934#ifdef FEAT_MBYTE
3935 (enc_utf8 && utf_fold(prog->regstart) == utf_fold(c)))
3936 || (c < 255 && prog->regstart < 255 &&
3937#endif
Bram Moolenaara245a5b2007-08-11 11:58:23 +00003938 MB_TOLOWER(prog->regstart) == MB_TOLOWER(c)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 retval = regtry(prog, col);
3940 else
3941 retval = 0;
3942 }
3943 else
3944 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003945#ifdef FEAT_RELTIME
3946 int tm_count = 0;
3947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 /* Messy cases: unanchored match. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003949 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 {
3951 if (prog->regstart != NUL)
3952 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003953 /* Skip until the char we know it must start with.
3954 * Used often, do some work to avoid call overhead. */
3955 if (!ireg_ic
3956#ifdef FEAT_MBYTE
3957 && !has_mbyte
3958#endif
3959 )
3960 s = vim_strbyte(regline + col, prog->regstart);
3961 else
3962 s = cstrchr(regline + col, prog->regstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 if (s == NULL)
3964 {
3965 retval = 0;
3966 break;
3967 }
3968 col = (int)(s - regline);
3969 }
3970
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003971 /* Check for maximum column to try. */
3972 if (ireg_maxcol > 0 && col >= ireg_maxcol)
3973 {
3974 retval = 0;
3975 break;
3976 }
3977
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 retval = regtry(prog, col);
3979 if (retval > 0)
3980 break;
3981
3982 /* if not currently on the first line, get it again */
3983 if (reglnum != 0)
3984 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 reglnum = 0;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003986 regline = reg_getline((linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 }
3988 if (regline[col] == NUL)
3989 break;
3990#ifdef FEAT_MBYTE
3991 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003992 col += (*mb_ptr2len)(regline + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 else
3994#endif
3995 ++col;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003996#ifdef FEAT_RELTIME
3997 /* Check for timeout once in a twenty times to avoid overhead. */
3998 if (tm != NULL && ++tm_count == 20)
3999 {
4000 tm_count = 0;
4001 if (profile_passed_limit(tm))
4002 break;
4003 }
4004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 }
4006 }
4007
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008theend:
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00004009 /* Free "reg_tofree" when it's a bit big.
4010 * Free regstack and backpos if they are bigger than their initial size. */
4011 if (reg_tofreelen > 400)
4012 {
4013 vim_free(reg_tofree);
4014 reg_tofree = NULL;
4015 }
4016 if (regstack.ga_maxlen > REGSTACK_INITIAL)
4017 ga_clear(&regstack);
4018 if (backpos.ga_maxlen > BACKPOS_INITIAL)
4019 ga_clear(&backpos);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004020
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 return retval;
4022}
4023
4024#ifdef FEAT_SYN_HL
4025static reg_extmatch_T *make_extmatch __ARGS((void));
4026
4027/*
4028 * Create a new extmatch and mark it as referenced once.
4029 */
4030 static reg_extmatch_T *
4031make_extmatch()
4032{
4033 reg_extmatch_T *em;
4034
4035 em = (reg_extmatch_T *)alloc_clear((unsigned)sizeof(reg_extmatch_T));
4036 if (em != NULL)
4037 em->refcnt = 1;
4038 return em;
4039}
4040
4041/*
4042 * Add a reference to an extmatch.
4043 */
4044 reg_extmatch_T *
4045ref_extmatch(em)
4046 reg_extmatch_T *em;
4047{
4048 if (em != NULL)
4049 em->refcnt++;
4050 return em;
4051}
4052
4053/*
4054 * Remove a reference to an extmatch. If there are no references left, free
4055 * the info.
4056 */
4057 void
4058unref_extmatch(em)
4059 reg_extmatch_T *em;
4060{
4061 int i;
4062
4063 if (em != NULL && --em->refcnt <= 0)
4064 {
4065 for (i = 0; i < NSUBEXP; ++i)
4066 vim_free(em->matches[i]);
4067 vim_free(em);
4068 }
4069}
4070#endif
4071
4072/*
4073 * regtry - try match of "prog" with at regline["col"].
4074 * Returns 0 for failure, number of lines contained in the match otherwise.
4075 */
4076 static long
4077regtry(prog, col)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004078 bt_regprog_T *prog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 colnr_T col;
4080{
4081 reginput = regline + col;
4082 need_clear_subexpr = TRUE;
4083#ifdef FEAT_SYN_HL
4084 /* Clear the external match subpointers if necessary. */
4085 if (prog->reghasz == REX_SET)
4086 need_clear_zsubexpr = TRUE;
4087#endif
4088
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004089 if (regmatch(prog->program + 1) == 0)
4090 return 0;
4091
4092 cleanup_subexpr();
4093 if (REG_MULTI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004095 if (reg_startpos[0].lnum < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004097 reg_startpos[0].lnum = 0;
4098 reg_startpos[0].col = col;
4099 }
4100 if (reg_endpos[0].lnum < 0)
4101 {
4102 reg_endpos[0].lnum = reglnum;
4103 reg_endpos[0].col = (int)(reginput - regline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 }
4105 else
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004106 /* Use line number of "\ze". */
4107 reglnum = reg_endpos[0].lnum;
4108 }
4109 else
4110 {
4111 if (reg_startp[0] == NULL)
4112 reg_startp[0] = regline + col;
4113 if (reg_endp[0] == NULL)
4114 reg_endp[0] = reginput;
4115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116#ifdef FEAT_SYN_HL
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004117 /* Package any found \z(...\) matches for export. Default is none. */
4118 unref_extmatch(re_extmatch_out);
4119 re_extmatch_out = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004121 if (prog->reghasz == REX_SET)
4122 {
4123 int i;
4124
4125 cleanup_zsubexpr();
4126 re_extmatch_out = make_extmatch();
4127 for (i = 0; i < NSUBEXP; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004129 if (REG_MULTI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004131 /* Only accept single line matches. */
4132 if (reg_startzpos[i].lnum >= 0
4133 && reg_endzpos[i].lnum == reg_startzpos[i].lnum)
4134 re_extmatch_out->matches[i] =
4135 vim_strnsave(reg_getline(reg_startzpos[i].lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 + reg_startzpos[i].col,
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004137 reg_endzpos[i].col - reg_startzpos[i].col);
4138 }
4139 else
4140 {
4141 if (reg_startzp[i] != NULL && reg_endzp[i] != NULL)
4142 re_extmatch_out->matches[i] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 vim_strnsave(reg_startzp[i],
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004144 (int)(reg_endzp[i] - reg_startzp[i]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 }
4146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 }
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004148#endif
4149 return 1 + reglnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150}
4151
4152#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153static int reg_prev_class __ARGS((void));
4154
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155/*
4156 * Get class of previous character.
4157 */
4158 static int
4159reg_prev_class()
4160{
4161 if (reginput > regline)
Bram Moolenaarf813a182013-01-30 13:59:37 +01004162 return mb_get_class_buf(reginput - 1
4163 - (*mb_head_off)(regline, reginput - 1), reg_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 return -1;
4165}
4166
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167#endif
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004168#ifdef FEAT_VISUAL
4169static int reg_match_visual __ARGS((void));
4170
4171/*
4172 * Return TRUE if the current reginput position matches the Visual area.
4173 */
4174 static int
4175reg_match_visual()
4176{
4177 pos_T top, bot;
4178 linenr_T lnum;
4179 colnr_T col;
4180 win_T *wp = reg_win == NULL ? curwin : reg_win;
4181 int mode;
4182 colnr_T start, end;
4183 colnr_T start2, end2;
4184 colnr_T cols;
4185
4186 /* Check if the buffer is the current buffer. */
4187 if (reg_buf != curbuf || VIsual.lnum == 0)
4188 return FALSE;
4189
4190 if (VIsual_active)
4191 {
4192 if (lt(VIsual, wp->w_cursor))
4193 {
4194 top = VIsual;
4195 bot = wp->w_cursor;
4196 }
4197 else
4198 {
4199 top = wp->w_cursor;
4200 bot = VIsual;
4201 }
4202 mode = VIsual_mode;
4203 }
4204 else
4205 {
4206 if (lt(curbuf->b_visual.vi_start, curbuf->b_visual.vi_end))
4207 {
4208 top = curbuf->b_visual.vi_start;
4209 bot = curbuf->b_visual.vi_end;
4210 }
4211 else
4212 {
4213 top = curbuf->b_visual.vi_end;
4214 bot = curbuf->b_visual.vi_start;
4215 }
4216 mode = curbuf->b_visual.vi_mode;
4217 }
4218 lnum = reglnum + reg_firstlnum;
4219 if (lnum < top.lnum || lnum > bot.lnum)
4220 return FALSE;
4221
4222 if (mode == 'v')
4223 {
4224 col = (colnr_T)(reginput - regline);
4225 if ((lnum == top.lnum && col < top.col)
4226 || (lnum == bot.lnum && col >= bot.col + (*p_sel != 'e')))
4227 return FALSE;
4228 }
4229 else if (mode == Ctrl_V)
4230 {
4231 getvvcol(wp, &top, &start, NULL, &end);
4232 getvvcol(wp, &bot, &start2, NULL, &end2);
4233 if (start2 < start)
4234 start = start2;
4235 if (end2 > end)
4236 end = end2;
4237 if (top.col == MAXCOL || bot.col == MAXCOL)
4238 end = MAXCOL;
4239 cols = win_linetabsize(wp, regline, (colnr_T)(reginput - regline));
4240 if (cols < start || cols > end - (*p_sel == 'e'))
4241 return FALSE;
4242 }
4243 return TRUE;
4244}
4245#endif
4246
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004247#define ADVANCE_REGINPUT() mb_ptr_adv(reginput)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248
4249/*
4250 * The arguments from BRACE_LIMITS are stored here. They are actually local
4251 * to regmatch(), but they are here to reduce the amount of stack space used
4252 * (it can be called recursively many times).
4253 */
4254static long bl_minval;
4255static long bl_maxval;
4256
4257/*
4258 * regmatch - main matching routine
4259 *
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004260 * Conceptually the strategy is simple: Check to see whether the current node
4261 * matches, push an item onto the regstack and loop to see whether the rest
4262 * matches, and then act accordingly. In practice we make some effort to
4263 * avoid using the regstack, in particular by going through "ordinary" nodes
4264 * (that don't need to know whether the rest of the match failed) by a nested
4265 * loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 *
4267 * Returns TRUE when there is a match. Leaves reginput and reglnum just after
4268 * the last matched character.
4269 * Returns FALSE when there is no match. Leaves reginput and reglnum in an
4270 * undefined state!
4271 */
4272 static int
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004273regmatch(scan)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 char_u *scan; /* Current node. */
4275{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004276 char_u *next; /* Next node. */
4277 int op;
4278 int c;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004279 regitem_T *rp;
4280 int no;
4281 int status; /* one of the RA_ values: */
4282#define RA_FAIL 1 /* something failed, abort */
4283#define RA_CONT 2 /* continue in inner loop */
4284#define RA_BREAK 3 /* break inner loop */
4285#define RA_MATCH 4 /* successful match */
4286#define RA_NOMATCH 5 /* didn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00004288 /* Make "regstack" and "backpos" empty. They are allocated and freed in
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004289 * bt_regexec_both() to reduce malloc()/free() calls. */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004290 regstack.ga_len = 0;
4291 backpos.ga_len = 0;
Bram Moolenaar582fd852005-03-28 20:58:01 +00004292
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004293 /*
Bram Moolenaar582fd852005-03-28 20:58:01 +00004294 * Repeat until "regstack" is empty.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004295 */
4296 for (;;)
4297 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004298 /* Some patterns may cause a long time to match, even though they are not
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 * illegal. E.g., "\([a-z]\+\)\+Q". Allow breaking them with CTRL-C. */
4300 fast_breakcheck();
4301
4302#ifdef DEBUG
4303 if (scan != NULL && regnarrate)
4304 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004305 mch_errmsg((char *)regprop(scan));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 mch_errmsg("(\n");
4307 }
4308#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004309
4310 /*
Bram Moolenaar582fd852005-03-28 20:58:01 +00004311 * Repeat for items that can be matched sequentially, without using the
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004312 * regstack.
4313 */
4314 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004316 if (got_int || scan == NULL)
4317 {
4318 status = RA_FAIL;
4319 break;
4320 }
4321 status = RA_CONT;
4322
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323#ifdef DEBUG
4324 if (regnarrate)
4325 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004326 mch_errmsg((char *)regprop(scan));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 mch_errmsg("...\n");
4328# ifdef FEAT_SYN_HL
4329 if (re_extmatch_in != NULL)
4330 {
4331 int i;
4332
4333 mch_errmsg(_("External submatches:\n"));
4334 for (i = 0; i < NSUBEXP; i++)
4335 {
4336 mch_errmsg(" \"");
4337 if (re_extmatch_in->matches[i] != NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004338 mch_errmsg((char *)re_extmatch_in->matches[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 mch_errmsg("\"\n");
4340 }
4341 }
4342# endif
4343 }
4344#endif
4345 next = regnext(scan);
4346
4347 op = OP(scan);
4348 /* Check for character class with NL added. */
Bram Moolenaar640009d2006-10-17 16:48:26 +00004349 if (!reg_line_lbr && WITH_NL(op) && REG_MULTI
4350 && *reginput == NUL && reglnum <= reg_maxline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 {
4352 reg_nextline();
4353 }
4354 else if (reg_line_lbr && WITH_NL(op) && *reginput == '\n')
4355 {
4356 ADVANCE_REGINPUT();
4357 }
4358 else
4359 {
4360 if (WITH_NL(op))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004361 op -= ADD_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362#ifdef FEAT_MBYTE
4363 if (has_mbyte)
4364 c = (*mb_ptr2char)(reginput);
4365 else
4366#endif
4367 c = *reginput;
4368 switch (op)
4369 {
4370 case BOL:
4371 if (reginput != regline)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004372 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 break;
4374
4375 case EOL:
4376 if (c != NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004377 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 break;
4379
4380 case RE_BOF:
Bram Moolenaara7139332007-12-09 18:26:22 +00004381 /* We're not at the beginning of the file when below the first
4382 * line where we started, not at the start of the line or we
4383 * didn't start at the first line of the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 if (reglnum != 0 || reginput != regline
Bram Moolenaara7139332007-12-09 18:26:22 +00004385 || (REG_MULTI && reg_firstlnum > 1))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004386 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 break;
4388
4389 case RE_EOF:
4390 if (reglnum != reg_maxline || c != NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004391 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 break;
4393
4394 case CURSOR:
4395 /* Check if the buffer is in a window and compare the
4396 * reg_win->w_cursor position to the match position. */
4397 if (reg_win == NULL
4398 || (reglnum + reg_firstlnum != reg_win->w_cursor.lnum)
4399 || ((colnr_T)(reginput - regline) != reg_win->w_cursor.col))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004400 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 break;
4402
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004403 case RE_MARK:
Bram Moolenaar044aa292013-06-04 21:27:38 +02004404 /* Compare the mark position to the match position. */
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004405 {
4406 int mark = OPERAND(scan)[0];
4407 int cmp = OPERAND(scan)[1];
4408 pos_T *pos;
4409
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004410 pos = getmark_buf(reg_buf, mark, FALSE);
Bram Moolenaare9400a42007-05-06 13:04:32 +00004411 if (pos == NULL /* mark doesn't exist */
Bram Moolenaar044aa292013-06-04 21:27:38 +02004412 || pos->lnum <= 0 /* mark isn't set in reg_buf */
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004413 || (pos->lnum == reglnum + reg_firstlnum
4414 ? (pos->col == (colnr_T)(reginput - regline)
4415 ? (cmp == '<' || cmp == '>')
4416 : (pos->col < (colnr_T)(reginput - regline)
4417 ? cmp != '>'
4418 : cmp != '<'))
4419 : (pos->lnum < reglnum + reg_firstlnum
4420 ? cmp != '>'
4421 : cmp != '<')))
4422 status = RA_NOMATCH;
4423 }
4424 break;
4425
4426 case RE_VISUAL:
4427#ifdef FEAT_VISUAL
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004428 if (!reg_match_visual())
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004429#endif
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004430 status = RA_NOMATCH;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004431 break;
4432
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 case RE_LNUM:
4434 if (!REG_MULTI || !re_num_cmp((long_u)(reglnum + reg_firstlnum),
4435 scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004436 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 break;
4438
4439 case RE_COL:
4440 if (!re_num_cmp((long_u)(reginput - regline) + 1, scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004441 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 break;
4443
4444 case RE_VCOL:
4445 if (!re_num_cmp((long_u)win_linetabsize(
4446 reg_win == NULL ? curwin : reg_win,
4447 regline, (colnr_T)(reginput - regline)) + 1, scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004448 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 break;
4450
4451 case BOW: /* \<word; reginput points to w */
4452 if (c == NUL) /* Can't match at end of line */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004453 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454#ifdef FEAT_MBYTE
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004455 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 {
4457 int this_class;
4458
4459 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf813a182013-01-30 13:59:37 +01004460 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 if (this_class <= 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004462 status = RA_NOMATCH; /* not on a word at all */
4463 else if (reg_prev_class() == this_class)
4464 status = RA_NOMATCH; /* previous char is in same word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 }
4466#endif
4467 else
4468 {
Bram Moolenaar2f315ab2013-01-25 20:11:01 +01004469 if (!vim_iswordc_buf(c, reg_buf) || (reginput > regline
4470 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004471 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 }
4473 break;
4474
4475 case EOW: /* word\>; reginput points after d */
4476 if (reginput == regline) /* Can't match at start of line */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004477 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478#ifdef FEAT_MBYTE
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004479 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 {
4481 int this_class, prev_class;
4482
4483 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf813a182013-01-30 13:59:37 +01004484 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 prev_class = reg_prev_class();
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004486 if (this_class == prev_class
4487 || prev_class == 0 || prev_class == 1)
4488 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004491 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004493 if (!vim_iswordc_buf(reginput[-1], reg_buf)
4494 || (reginput[0] != NUL && vim_iswordc_buf(c, reg_buf)))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004495 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 }
4497 break; /* Matched with EOW */
4498
4499 case ANY:
Bram Moolenaare337e5f2013-01-30 18:21:51 +01004500 /* ANY does not match new lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 if (c == NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004502 status = RA_NOMATCH;
4503 else
4504 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 break;
4506
4507 case IDENT:
4508 if (!vim_isIDc(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004509 status = RA_NOMATCH;
4510 else
4511 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 break;
4513
4514 case SIDENT:
4515 if (VIM_ISDIGIT(*reginput) || !vim_isIDc(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004516 status = RA_NOMATCH;
4517 else
4518 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 break;
4520
4521 case KWORD:
Bram Moolenaarf813a182013-01-30 13:59:37 +01004522 if (!vim_iswordp_buf(reginput, reg_buf))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004523 status = RA_NOMATCH;
4524 else
4525 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 break;
4527
4528 case SKWORD:
Bram Moolenaarf813a182013-01-30 13:59:37 +01004529 if (VIM_ISDIGIT(*reginput) || !vim_iswordp_buf(reginput, reg_buf))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004530 status = RA_NOMATCH;
4531 else
4532 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 break;
4534
4535 case FNAME:
4536 if (!vim_isfilec(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004537 status = RA_NOMATCH;
4538 else
4539 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 break;
4541
4542 case SFNAME:
4543 if (VIM_ISDIGIT(*reginput) || !vim_isfilec(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004544 status = RA_NOMATCH;
4545 else
4546 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 break;
4548
4549 case PRINT:
4550 if (ptr2cells(reginput) != 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004551 status = RA_NOMATCH;
4552 else
4553 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 break;
4555
4556 case SPRINT:
4557 if (VIM_ISDIGIT(*reginput) || ptr2cells(reginput) != 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004558 status = RA_NOMATCH;
4559 else
4560 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 break;
4562
4563 case WHITE:
4564 if (!vim_iswhite(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004565 status = RA_NOMATCH;
4566 else
4567 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 break;
4569
4570 case NWHITE:
4571 if (c == NUL || vim_iswhite(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004572 status = RA_NOMATCH;
4573 else
4574 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 break;
4576
4577 case DIGIT:
4578 if (!ri_digit(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004579 status = RA_NOMATCH;
4580 else
4581 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 break;
4583
4584 case NDIGIT:
4585 if (c == NUL || ri_digit(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004586 status = RA_NOMATCH;
4587 else
4588 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 break;
4590
4591 case HEX:
4592 if (!ri_hex(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004593 status = RA_NOMATCH;
4594 else
4595 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 break;
4597
4598 case NHEX:
4599 if (c == NUL || ri_hex(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004600 status = RA_NOMATCH;
4601 else
4602 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 break;
4604
4605 case OCTAL:
4606 if (!ri_octal(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004607 status = RA_NOMATCH;
4608 else
4609 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 break;
4611
4612 case NOCTAL:
4613 if (c == NUL || ri_octal(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004614 status = RA_NOMATCH;
4615 else
4616 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 break;
4618
4619 case WORD:
4620 if (!ri_word(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004621 status = RA_NOMATCH;
4622 else
4623 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 break;
4625
4626 case NWORD:
4627 if (c == NUL || ri_word(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004628 status = RA_NOMATCH;
4629 else
4630 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 break;
4632
4633 case HEAD:
4634 if (!ri_head(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004635 status = RA_NOMATCH;
4636 else
4637 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 break;
4639
4640 case NHEAD:
4641 if (c == NUL || ri_head(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004642 status = RA_NOMATCH;
4643 else
4644 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 break;
4646
4647 case ALPHA:
4648 if (!ri_alpha(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004649 status = RA_NOMATCH;
4650 else
4651 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 break;
4653
4654 case NALPHA:
4655 if (c == NUL || ri_alpha(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004656 status = RA_NOMATCH;
4657 else
4658 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 break;
4660
4661 case LOWER:
4662 if (!ri_lower(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004663 status = RA_NOMATCH;
4664 else
4665 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 break;
4667
4668 case NLOWER:
4669 if (c == NUL || ri_lower(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004670 status = RA_NOMATCH;
4671 else
4672 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 break;
4674
4675 case UPPER:
4676 if (!ri_upper(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004677 status = RA_NOMATCH;
4678 else
4679 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 break;
4681
4682 case NUPPER:
4683 if (c == NUL || ri_upper(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004684 status = RA_NOMATCH;
4685 else
4686 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 break;
4688
4689 case EXACTLY:
4690 {
4691 int len;
4692 char_u *opnd;
4693
4694 opnd = OPERAND(scan);
4695 /* Inline the first byte, for speed. */
4696 if (*opnd != *reginput
4697 && (!ireg_ic || (
4698#ifdef FEAT_MBYTE
4699 !enc_utf8 &&
4700#endif
Bram Moolenaara245a5b2007-08-11 11:58:23 +00004701 MB_TOLOWER(*opnd) != MB_TOLOWER(*reginput))))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004702 status = RA_NOMATCH;
4703 else if (*opnd == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 {
4705 /* match empty string always works; happens when "~" is
4706 * empty. */
4707 }
4708 else if (opnd[1] == NUL
4709#ifdef FEAT_MBYTE
4710 && !(enc_utf8 && ireg_ic)
4711#endif
4712 )
4713 ++reginput; /* matched a single char */
4714 else
4715 {
4716 len = (int)STRLEN(opnd);
4717 /* Need to match first byte again for multi-byte. */
4718 if (cstrncmp(opnd, reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004719 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720#ifdef FEAT_MBYTE
4721 /* Check for following composing character. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004722 else if (enc_utf8
4723 && UTF_COMPOSINGLIKE(reginput, reginput + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 {
4725 /* raaron: This code makes a composing character get
4726 * ignored, which is the correct behavior (sometimes)
4727 * for voweled Hebrew texts. */
4728 if (!ireg_icombine)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004729 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004732 else
4733 reginput += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 }
4735 }
4736 break;
4737
4738 case ANYOF:
4739 case ANYBUT:
4740 if (c == NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004741 status = RA_NOMATCH;
4742 else if ((cstrchr(OPERAND(scan), c) == NULL) == (op == ANYOF))
4743 status = RA_NOMATCH;
4744 else
4745 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 break;
4747
4748#ifdef FEAT_MBYTE
4749 case MULTIBYTECODE:
4750 if (has_mbyte)
4751 {
4752 int i, len;
4753 char_u *opnd;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004754 int opndc = 0, inpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755
4756 opnd = OPERAND(scan);
4757 /* Safety check (just in case 'encoding' was changed since
4758 * compiling the program). */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004759 if ((len = (*mb_ptr2len)(opnd)) < 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004760 {
4761 status = RA_NOMATCH;
4762 break;
4763 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004764 if (enc_utf8)
4765 opndc = mb_ptr2char(opnd);
4766 if (enc_utf8 && utf_iscomposing(opndc))
4767 {
4768 /* When only a composing char is given match at any
4769 * position where that composing char appears. */
4770 status = RA_NOMATCH;
4771 for (i = 0; reginput[i] != NUL; i += utf_char2len(inpc))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004772 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004773 inpc = mb_ptr2char(reginput + i);
4774 if (!utf_iscomposing(inpc))
4775 {
4776 if (i > 0)
4777 break;
4778 }
4779 else if (opndc == inpc)
4780 {
4781 /* Include all following composing chars. */
4782 len = i + mb_ptr2len(reginput + i);
4783 status = RA_MATCH;
4784 break;
4785 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004786 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004787 }
4788 else
4789 for (i = 0; i < len; ++i)
4790 if (opnd[i] != reginput[i])
4791 {
4792 status = RA_NOMATCH;
4793 break;
4794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 reginput += len;
4796 }
4797 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004798 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 break;
4800#endif
4801
4802 case NOTHING:
4803 break;
4804
4805 case BACK:
Bram Moolenaar582fd852005-03-28 20:58:01 +00004806 {
4807 int i;
4808 backpos_T *bp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809
Bram Moolenaar582fd852005-03-28 20:58:01 +00004810 /*
4811 * When we run into BACK we need to check if we don't keep
4812 * looping without matching any input. The second and later
4813 * times a BACK is encountered it fails if the input is still
4814 * at the same position as the previous time.
4815 * The positions are stored in "backpos" and found by the
4816 * current value of "scan", the position in the RE program.
4817 */
4818 bp = (backpos_T *)backpos.ga_data;
4819 for (i = 0; i < backpos.ga_len; ++i)
4820 if (bp[i].bp_scan == scan)
4821 break;
4822 if (i == backpos.ga_len)
4823 {
4824 /* First time at this BACK, make room to store the pos. */
4825 if (ga_grow(&backpos, 1) == FAIL)
4826 status = RA_FAIL;
4827 else
4828 {
4829 /* get "ga_data" again, it may have changed */
4830 bp = (backpos_T *)backpos.ga_data;
4831 bp[i].bp_scan = scan;
4832 ++backpos.ga_len;
4833 }
4834 }
4835 else if (reg_save_equal(&bp[i].bp_pos))
4836 /* Still at same position as last time, fail. */
4837 status = RA_NOMATCH;
4838
4839 if (status != RA_FAIL && status != RA_NOMATCH)
4840 reg_save(&bp[i].bp_pos, &backpos);
4841 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004842 break;
4843
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 case MOPEN + 0: /* Match start: \zs */
4845 case MOPEN + 1: /* \( */
4846 case MOPEN + 2:
4847 case MOPEN + 3:
4848 case MOPEN + 4:
4849 case MOPEN + 5:
4850 case MOPEN + 6:
4851 case MOPEN + 7:
4852 case MOPEN + 8:
4853 case MOPEN + 9:
4854 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 no = op - MOPEN;
4856 cleanup_subexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004857 rp = regstack_push(RS_MOPEN, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004858 if (rp == NULL)
4859 status = RA_FAIL;
4860 else
4861 {
4862 rp->rs_no = no;
4863 save_se(&rp->rs_un.sesave, &reg_startpos[no],
4864 &reg_startp[no]);
4865 /* We simply continue and handle the result when done. */
4866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004868 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869
4870 case NOPEN: /* \%( */
4871 case NCLOSE: /* \) after \%( */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004872 if (regstack_push(RS_NOPEN, scan) == NULL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004873 status = RA_FAIL;
4874 /* We simply continue and handle the result when done. */
4875 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876
4877#ifdef FEAT_SYN_HL
4878 case ZOPEN + 1:
4879 case ZOPEN + 2:
4880 case ZOPEN + 3:
4881 case ZOPEN + 4:
4882 case ZOPEN + 5:
4883 case ZOPEN + 6:
4884 case ZOPEN + 7:
4885 case ZOPEN + 8:
4886 case ZOPEN + 9:
4887 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 no = op - ZOPEN;
4889 cleanup_zsubexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004890 rp = regstack_push(RS_ZOPEN, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004891 if (rp == NULL)
4892 status = RA_FAIL;
4893 else
4894 {
4895 rp->rs_no = no;
4896 save_se(&rp->rs_un.sesave, &reg_startzpos[no],
4897 &reg_startzp[no]);
4898 /* We simply continue and handle the result when done. */
4899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004901 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902#endif
4903
4904 case MCLOSE + 0: /* Match end: \ze */
4905 case MCLOSE + 1: /* \) */
4906 case MCLOSE + 2:
4907 case MCLOSE + 3:
4908 case MCLOSE + 4:
4909 case MCLOSE + 5:
4910 case MCLOSE + 6:
4911 case MCLOSE + 7:
4912 case MCLOSE + 8:
4913 case MCLOSE + 9:
4914 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 no = op - MCLOSE;
4916 cleanup_subexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004917 rp = regstack_push(RS_MCLOSE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004918 if (rp == NULL)
4919 status = RA_FAIL;
4920 else
4921 {
4922 rp->rs_no = no;
4923 save_se(&rp->rs_un.sesave, &reg_endpos[no], &reg_endp[no]);
4924 /* We simply continue and handle the result when done. */
4925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004927 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928
4929#ifdef FEAT_SYN_HL
4930 case ZCLOSE + 1: /* \) after \z( */
4931 case ZCLOSE + 2:
4932 case ZCLOSE + 3:
4933 case ZCLOSE + 4:
4934 case ZCLOSE + 5:
4935 case ZCLOSE + 6:
4936 case ZCLOSE + 7:
4937 case ZCLOSE + 8:
4938 case ZCLOSE + 9:
4939 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 no = op - ZCLOSE;
4941 cleanup_zsubexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004942 rp = regstack_push(RS_ZCLOSE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004943 if (rp == NULL)
4944 status = RA_FAIL;
4945 else
4946 {
4947 rp->rs_no = no;
4948 save_se(&rp->rs_un.sesave, &reg_endzpos[no],
4949 &reg_endzp[no]);
4950 /* We simply continue and handle the result when done. */
4951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004953 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954#endif
4955
4956 case BACKREF + 1:
4957 case BACKREF + 2:
4958 case BACKREF + 3:
4959 case BACKREF + 4:
4960 case BACKREF + 5:
4961 case BACKREF + 6:
4962 case BACKREF + 7:
4963 case BACKREF + 8:
4964 case BACKREF + 9:
4965 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 int len;
4967 linenr_T clnum;
4968 colnr_T ccol;
4969 char_u *p;
4970
4971 no = op - BACKREF;
4972 cleanup_subexpr();
4973 if (!REG_MULTI) /* Single-line regexp */
4974 {
Bram Moolenaar7670fa02009-02-21 21:04:20 +00004975 if (reg_startp[no] == NULL || reg_endp[no] == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 {
4977 /* Backref was not set: Match an empty string. */
4978 len = 0;
4979 }
4980 else
4981 {
4982 /* Compare current input with back-ref in the same
4983 * line. */
4984 len = (int)(reg_endp[no] - reg_startp[no]);
4985 if (cstrncmp(reg_startp[no], reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004986 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 }
4988 }
4989 else /* Multi-line regexp */
4990 {
Bram Moolenaar7670fa02009-02-21 21:04:20 +00004991 if (reg_startpos[no].lnum < 0 || reg_endpos[no].lnum < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 {
4993 /* Backref was not set: Match an empty string. */
4994 len = 0;
4995 }
4996 else
4997 {
4998 if (reg_startpos[no].lnum == reglnum
4999 && reg_endpos[no].lnum == reglnum)
5000 {
5001 /* Compare back-ref within the current line. */
5002 len = reg_endpos[no].col - reg_startpos[no].col;
5003 if (cstrncmp(regline + reg_startpos[no].col,
5004 reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005005 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 }
5007 else
5008 {
5009 /* Messy situation: Need to compare between two
5010 * lines. */
5011 ccol = reg_startpos[no].col;
5012 clnum = reg_startpos[no].lnum;
5013 for (;;)
5014 {
5015 /* Since getting one line may invalidate
5016 * the other, need to make copy. Slow! */
5017 if (regline != reg_tofree)
5018 {
5019 len = (int)STRLEN(regline);
5020 if (reg_tofree == NULL
5021 || len >= (int)reg_tofreelen)
5022 {
5023 len += 50; /* get some extra */
5024 vim_free(reg_tofree);
5025 reg_tofree = alloc(len);
5026 if (reg_tofree == NULL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005027 {
5028 status = RA_FAIL; /* outof memory!*/
5029 break;
5030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 reg_tofreelen = len;
5032 }
5033 STRCPY(reg_tofree, regline);
5034 reginput = reg_tofree
5035 + (reginput - regline);
5036 regline = reg_tofree;
5037 }
5038
5039 /* Get the line to compare with. */
5040 p = reg_getline(clnum);
5041 if (clnum == reg_endpos[no].lnum)
5042 len = reg_endpos[no].col - ccol;
5043 else
5044 len = (int)STRLEN(p + ccol);
5045
5046 if (cstrncmp(p + ccol, reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005047 {
5048 status = RA_NOMATCH; /* doesn't match */
5049 break;
5050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 if (clnum == reg_endpos[no].lnum)
5052 break; /* match and at end! */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005053 if (reglnum >= reg_maxline)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005054 {
5055 status = RA_NOMATCH; /* text too short */
5056 break;
5057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058
5059 /* Advance to next line. */
5060 reg_nextline();
5061 ++clnum;
5062 ccol = 0;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005063 if (got_int)
5064 {
5065 status = RA_FAIL;
5066 break;
5067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 }
5069
5070 /* found a match! Note that regline may now point
5071 * to a copy of the line, that should not matter. */
5072 }
5073 }
5074 }
5075
5076 /* Matched the backref, skip over it. */
5077 reginput += len;
5078 }
5079 break;
5080
5081#ifdef FEAT_SYN_HL
5082 case ZREF + 1:
5083 case ZREF + 2:
5084 case ZREF + 3:
5085 case ZREF + 4:
5086 case ZREF + 5:
5087 case ZREF + 6:
5088 case ZREF + 7:
5089 case ZREF + 8:
5090 case ZREF + 9:
5091 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 int len;
5093
5094 cleanup_zsubexpr();
5095 no = op - ZREF;
5096 if (re_extmatch_in != NULL
5097 && re_extmatch_in->matches[no] != NULL)
5098 {
5099 len = (int)STRLEN(re_extmatch_in->matches[no]);
5100 if (cstrncmp(re_extmatch_in->matches[no],
5101 reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005102 status = RA_NOMATCH;
5103 else
5104 reginput += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 }
5106 else
5107 {
5108 /* Backref was not set: Match an empty string. */
5109 }
5110 }
5111 break;
5112#endif
5113
5114 case BRANCH:
5115 {
5116 if (OP(next) != BRANCH) /* No choice. */
5117 next = OPERAND(scan); /* Avoid recursion. */
5118 else
5119 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005120 rp = regstack_push(RS_BRANCH, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005121 if (rp == NULL)
5122 status = RA_FAIL;
5123 else
5124 status = RA_BREAK; /* rest is below */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 }
5126 }
5127 break;
5128
5129 case BRACE_LIMITS:
5130 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 if (OP(next) == BRACE_SIMPLE)
5132 {
5133 bl_minval = OPERAND_MIN(scan);
5134 bl_maxval = OPERAND_MAX(scan);
5135 }
5136 else if (OP(next) >= BRACE_COMPLEX
5137 && OP(next) < BRACE_COMPLEX + 10)
5138 {
5139 no = OP(next) - BRACE_COMPLEX;
5140 brace_min[no] = OPERAND_MIN(scan);
5141 brace_max[no] = OPERAND_MAX(scan);
5142 brace_count[no] = 0;
5143 }
5144 else
5145 {
5146 EMSG(_(e_internal)); /* Shouldn't happen */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005147 status = RA_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 }
5149 }
5150 break;
5151
5152 case BRACE_COMPLEX + 0:
5153 case BRACE_COMPLEX + 1:
5154 case BRACE_COMPLEX + 2:
5155 case BRACE_COMPLEX + 3:
5156 case BRACE_COMPLEX + 4:
5157 case BRACE_COMPLEX + 5:
5158 case BRACE_COMPLEX + 6:
5159 case BRACE_COMPLEX + 7:
5160 case BRACE_COMPLEX + 8:
5161 case BRACE_COMPLEX + 9:
5162 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 no = op - BRACE_COMPLEX;
5164 ++brace_count[no];
5165
5166 /* If not matched enough times yet, try one more */
5167 if (brace_count[no] <= (brace_min[no] <= brace_max[no]
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005168 ? brace_min[no] : brace_max[no]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005170 rp = regstack_push(RS_BRCPLX_MORE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005171 if (rp == NULL)
5172 status = RA_FAIL;
5173 else
5174 {
5175 rp->rs_no = no;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005176 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005177 next = OPERAND(scan);
5178 /* We continue and handle the result when done. */
5179 }
5180 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 }
5182
5183 /* If matched enough times, may try matching some more */
5184 if (brace_min[no] <= brace_max[no])
5185 {
5186 /* Range is the normal way around, use longest match */
5187 if (brace_count[no] <= brace_max[no])
5188 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005189 rp = regstack_push(RS_BRCPLX_LONG, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005190 if (rp == NULL)
5191 status = RA_FAIL;
5192 else
5193 {
5194 rp->rs_no = no;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005195 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005196 next = OPERAND(scan);
5197 /* We continue and handle the result when done. */
5198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 }
5200 }
5201 else
5202 {
5203 /* Range is backwards, use shortest match first */
5204 if (brace_count[no] <= brace_min[no])
5205 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005206 rp = regstack_push(RS_BRCPLX_SHORT, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005207 if (rp == NULL)
5208 status = RA_FAIL;
5209 else
5210 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005211 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005212 /* We continue and handle the result when done. */
5213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 }
5215 }
5216 }
5217 break;
5218
5219 case BRACE_SIMPLE:
5220 case STAR:
5221 case PLUS:
5222 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005223 regstar_T rst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224
5225 /*
5226 * Lookahead to avoid useless match attempts when we know
5227 * what character comes next.
5228 */
5229 if (OP(next) == EXACTLY)
5230 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005231 rst.nextb = *OPERAND(next);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 if (ireg_ic)
5233 {
Bram Moolenaara245a5b2007-08-11 11:58:23 +00005234 if (MB_ISUPPER(rst.nextb))
5235 rst.nextb_ic = MB_TOLOWER(rst.nextb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 else
Bram Moolenaara245a5b2007-08-11 11:58:23 +00005237 rst.nextb_ic = MB_TOUPPER(rst.nextb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 }
5239 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005240 rst.nextb_ic = rst.nextb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 }
5242 else
5243 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005244 rst.nextb = NUL;
5245 rst.nextb_ic = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 }
5247 if (op != BRACE_SIMPLE)
5248 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005249 rst.minval = (op == STAR) ? 0 : 1;
5250 rst.maxval = MAX_LIMIT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 }
5252 else
5253 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005254 rst.minval = bl_minval;
5255 rst.maxval = bl_maxval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 }
5257
5258 /*
5259 * When maxval > minval, try matching as much as possible, up
5260 * to maxval. When maxval < minval, try matching at least the
5261 * minimal number (since the range is backwards, that's also
5262 * maxval!).
5263 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005264 rst.count = regrepeat(OPERAND(scan), rst.maxval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005267 status = RA_FAIL;
5268 break;
5269 }
5270 if (rst.minval <= rst.maxval
5271 ? rst.count >= rst.minval : rst.count >= rst.maxval)
5272 {
5273 /* It could match. Prepare for trying to match what
5274 * follows. The code is below. Parameters are stored in
5275 * a regstar_T on the regstack. */
Bram Moolenaar916b7af2005-03-16 09:52:38 +00005276 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005277 {
5278 EMSG(_(e_maxmempat));
5279 status = RA_FAIL;
5280 }
5281 else if (ga_grow(&regstack, sizeof(regstar_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005282 status = RA_FAIL;
5283 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005285 regstack.ga_len += sizeof(regstar_T);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005286 rp = regstack_push(rst.minval <= rst.maxval
Bram Moolenaar582fd852005-03-28 20:58:01 +00005287 ? RS_STAR_LONG : RS_STAR_SHORT, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005288 if (rp == NULL)
5289 status = RA_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005291 {
5292 *(((regstar_T *)rp) - 1) = rst;
5293 status = RA_BREAK; /* skip the restore bits */
5294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 }
5296 }
5297 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005298 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 }
5301 break;
5302
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005303 case NOMATCH:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 case MATCH:
5305 case SUBPAT:
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005306 rp = regstack_push(RS_NOMATCH, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005307 if (rp == NULL)
5308 status = RA_FAIL;
5309 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005311 rp->rs_no = op;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005312 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005313 next = OPERAND(scan);
5314 /* We continue and handle the result when done. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 }
5316 break;
5317
5318 case BEHIND:
5319 case NOBEHIND:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005320 /* Need a bit of room to store extra positions. */
Bram Moolenaar916b7af2005-03-16 09:52:38 +00005321 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005322 {
5323 EMSG(_(e_maxmempat));
5324 status = RA_FAIL;
5325 }
5326 else if (ga_grow(&regstack, sizeof(regbehind_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005327 status = RA_FAIL;
5328 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005330 regstack.ga_len += sizeof(regbehind_T);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005331 rp = regstack_push(RS_BEHIND1, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005332 if (rp == NULL)
5333 status = RA_FAIL;
5334 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 {
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005336 /* Need to save the subexpr to be able to restore them
5337 * when there is a match but we don't use it. */
5338 save_subexpr(((regbehind_T *)rp) - 1);
5339
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005340 rp->rs_no = op;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005341 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005342 /* First try if what follows matches. If it does then we
5343 * check the behind match by looping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005346 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347
5348 case BHPOS:
5349 if (REG_MULTI)
5350 {
5351 if (behind_pos.rs_u.pos.col != (colnr_T)(reginput - regline)
5352 || behind_pos.rs_u.pos.lnum != reglnum)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005353 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 }
5355 else if (behind_pos.rs_u.ptr != reginput)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005356 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 break;
5358
5359 case NEWL:
Bram Moolenaar640009d2006-10-17 16:48:26 +00005360 if ((c != NUL || !REG_MULTI || reglnum > reg_maxline
5361 || reg_line_lbr) && (c != '\n' || !reg_line_lbr))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005362 status = RA_NOMATCH;
5363 else if (reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 ADVANCE_REGINPUT();
5365 else
5366 reg_nextline();
5367 break;
5368
5369 case END:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005370 status = RA_MATCH; /* Success! */
5371 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372
5373 default:
5374 EMSG(_(e_re_corr));
5375#ifdef DEBUG
5376 printf("Illegal op code %d\n", op);
5377#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005378 status = RA_FAIL;
5379 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 }
5381 }
5382
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005383 /* If we can't continue sequentially, break the inner loop. */
5384 if (status != RA_CONT)
5385 break;
5386
5387 /* Continue in inner loop, advance to next item. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 scan = next;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005389
5390 } /* end of inner loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391
5392 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005393 * If there is something on the regstack execute the code for the state.
Bram Moolenaar582fd852005-03-28 20:58:01 +00005394 * If the state is popped then loop and use the older state.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005396 while (regstack.ga_len > 0 && status != RA_FAIL)
5397 {
5398 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len) - 1;
5399 switch (rp->rs_state)
5400 {
5401 case RS_NOPEN:
5402 /* Result is passed on as-is, simply pop the state. */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005403 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005404 break;
5405
5406 case RS_MOPEN:
5407 /* Pop the state. Restore pointers when there is no match. */
5408 if (status == RA_NOMATCH)
5409 restore_se(&rp->rs_un.sesave, &reg_startpos[rp->rs_no],
5410 &reg_startp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005411 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005412 break;
5413
5414#ifdef FEAT_SYN_HL
5415 case RS_ZOPEN:
5416 /* Pop the state. Restore pointers when there is no match. */
5417 if (status == RA_NOMATCH)
5418 restore_se(&rp->rs_un.sesave, &reg_startzpos[rp->rs_no],
5419 &reg_startzp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005420 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005421 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005423
5424 case RS_MCLOSE:
5425 /* Pop the state. Restore pointers when there is no match. */
5426 if (status == RA_NOMATCH)
5427 restore_se(&rp->rs_un.sesave, &reg_endpos[rp->rs_no],
5428 &reg_endp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005429 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005430 break;
5431
5432#ifdef FEAT_SYN_HL
5433 case RS_ZCLOSE:
5434 /* Pop the state. Restore pointers when there is no match. */
5435 if (status == RA_NOMATCH)
5436 restore_se(&rp->rs_un.sesave, &reg_endzpos[rp->rs_no],
5437 &reg_endzp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005438 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005439 break;
5440#endif
5441
5442 case RS_BRANCH:
5443 if (status == RA_MATCH)
5444 /* this branch matched, use it */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005445 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005446 else
5447 {
5448 if (status != RA_BREAK)
5449 {
5450 /* After a non-matching branch: try next one. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005451 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005452 scan = rp->rs_scan;
5453 }
5454 if (scan == NULL || OP(scan) != BRANCH)
5455 {
5456 /* no more branches, didn't find a match */
5457 status = RA_NOMATCH;
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005458 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005459 }
5460 else
5461 {
5462 /* Prepare to try a branch. */
5463 rp->rs_scan = regnext(scan);
Bram Moolenaar582fd852005-03-28 20:58:01 +00005464 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005465 scan = OPERAND(scan);
5466 }
5467 }
5468 break;
5469
5470 case RS_BRCPLX_MORE:
5471 /* Pop the state. Restore pointers when there is no match. */
5472 if (status == RA_NOMATCH)
5473 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005474 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005475 --brace_count[rp->rs_no]; /* decrement match count */
5476 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005477 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005478 break;
5479
5480 case RS_BRCPLX_LONG:
5481 /* Pop the state. Restore pointers when there is no match. */
5482 if (status == RA_NOMATCH)
5483 {
5484 /* There was no match, but we did find enough matches. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005485 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005486 --brace_count[rp->rs_no];
5487 /* continue with the items after "\{}" */
5488 status = RA_CONT;
5489 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005490 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005491 if (status == RA_CONT)
5492 scan = regnext(scan);
5493 break;
5494
5495 case RS_BRCPLX_SHORT:
5496 /* Pop the state. Restore pointers when there is no match. */
5497 if (status == RA_NOMATCH)
5498 /* There was no match, try to match one more item. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005499 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005500 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005501 if (status == RA_NOMATCH)
5502 {
5503 scan = OPERAND(scan);
5504 status = RA_CONT;
5505 }
5506 break;
5507
5508 case RS_NOMATCH:
5509 /* Pop the state. If the operand matches for NOMATCH or
5510 * doesn't match for MATCH/SUBPAT, we fail. Otherwise backup,
5511 * except for SUBPAT, and continue with the next item. */
5512 if (status == (rp->rs_no == NOMATCH ? RA_MATCH : RA_NOMATCH))
5513 status = RA_NOMATCH;
5514 else
5515 {
5516 status = RA_CONT;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005517 if (rp->rs_no != SUBPAT) /* zero-width */
5518 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005519 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005520 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005521 if (status == RA_CONT)
5522 scan = regnext(scan);
5523 break;
5524
5525 case RS_BEHIND1:
5526 if (status == RA_NOMATCH)
5527 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005528 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005529 regstack.ga_len -= sizeof(regbehind_T);
5530 }
5531 else
5532 {
5533 /* The stuff after BEHIND/NOBEHIND matches. Now try if
5534 * the behind part does (not) match before the current
5535 * position in the input. This must be done at every
5536 * position in the input and checking if the match ends at
5537 * the current position. */
5538
5539 /* save the position after the found match for next */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005540 reg_save(&(((regbehind_T *)rp) - 1)->save_after, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005541
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005542 /* Start looking for a match with operand at the current
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00005543 * position. Go back one character until we find the
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005544 * result, hitting the start of the line or the previous
5545 * line (for multi-line matching).
5546 * Set behind_pos to where the match should end, BHPOS
5547 * will match it. Save the current value. */
5548 (((regbehind_T *)rp) - 1)->save_behind = behind_pos;
5549 behind_pos = rp->rs_un.regsave;
5550
5551 rp->rs_state = RS_BEHIND2;
5552
Bram Moolenaar582fd852005-03-28 20:58:01 +00005553 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005554 scan = OPERAND(rp->rs_scan) + 4;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005555 }
5556 break;
5557
5558 case RS_BEHIND2:
5559 /*
5560 * Looping for BEHIND / NOBEHIND match.
5561 */
5562 if (status == RA_MATCH && reg_save_equal(&behind_pos))
5563 {
5564 /* found a match that ends where "next" started */
5565 behind_pos = (((regbehind_T *)rp) - 1)->save_behind;
5566 if (rp->rs_no == BEHIND)
Bram Moolenaar582fd852005-03-28 20:58:01 +00005567 reg_restore(&(((regbehind_T *)rp) - 1)->save_after,
5568 &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005569 else
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005570 {
5571 /* But we didn't want a match. Need to restore the
5572 * subexpr, because what follows matched, so they have
5573 * been set. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005574 status = RA_NOMATCH;
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005575 restore_subexpr(((regbehind_T *)rp) - 1);
5576 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005577 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005578 regstack.ga_len -= sizeof(regbehind_T);
5579 }
5580 else
5581 {
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005582 long limit;
5583
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005584 /* No match or a match that doesn't end where we want it: Go
5585 * back one character. May go to previous line once. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005586 no = OK;
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005587 limit = OPERAND_MIN(rp->rs_scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005588 if (REG_MULTI)
5589 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02005590 if (limit > 0
5591 && ((rp->rs_un.regsave.rs_u.pos.lnum
5592 < behind_pos.rs_u.pos.lnum
5593 ? (colnr_T)STRLEN(regline)
5594 : behind_pos.rs_u.pos.col)
5595 - rp->rs_un.regsave.rs_u.pos.col >= limit))
5596 no = FAIL;
5597 else if (rp->rs_un.regsave.rs_u.pos.col == 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005598 {
5599 if (rp->rs_un.regsave.rs_u.pos.lnum
5600 < behind_pos.rs_u.pos.lnum
5601 || reg_getline(
5602 --rp->rs_un.regsave.rs_u.pos.lnum)
5603 == NULL)
5604 no = FAIL;
5605 else
5606 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005607 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005608 rp->rs_un.regsave.rs_u.pos.col =
5609 (colnr_T)STRLEN(regline);
5610 }
5611 }
5612 else
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005613 {
Bram Moolenaarf5e44a72013-02-26 18:46:01 +01005614#ifdef FEAT_MBYTE
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005615 if (has_mbyte)
5616 rp->rs_un.regsave.rs_u.pos.col -=
5617 (*mb_head_off)(regline, regline
Bram Moolenaarf5e44a72013-02-26 18:46:01 +01005618 + rp->rs_un.regsave.rs_u.pos.col - 1) + 1;
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005619 else
Bram Moolenaarf5e44a72013-02-26 18:46:01 +01005620#endif
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005621 --rp->rs_un.regsave.rs_u.pos.col;
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005622 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005623 }
5624 else
5625 {
5626 if (rp->rs_un.regsave.rs_u.ptr == regline)
5627 no = FAIL;
5628 else
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005629 {
5630 mb_ptr_back(regline, rp->rs_un.regsave.rs_u.ptr);
5631 if (limit > 0 && (long)(behind_pos.rs_u.ptr
5632 - rp->rs_un.regsave.rs_u.ptr) > limit)
5633 no = FAIL;
5634 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005635 }
5636 if (no == OK)
5637 {
5638 /* Advanced, prepare for finding match again. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005639 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005640 scan = OPERAND(rp->rs_scan) + 4;
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005641 if (status == RA_MATCH)
5642 {
5643 /* We did match, so subexpr may have been changed,
5644 * need to restore them for the next try. */
5645 status = RA_NOMATCH;
5646 restore_subexpr(((regbehind_T *)rp) - 1);
5647 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005648 }
5649 else
5650 {
5651 /* Can't advance. For NOBEHIND that's a match. */
5652 behind_pos = (((regbehind_T *)rp) - 1)->save_behind;
5653 if (rp->rs_no == NOBEHIND)
5654 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005655 reg_restore(&(((regbehind_T *)rp) - 1)->save_after,
5656 &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005657 status = RA_MATCH;
5658 }
5659 else
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005660 {
5661 /* We do want a proper match. Need to restore the
5662 * subexpr if we had a match, because they may have
5663 * been set. */
5664 if (status == RA_MATCH)
5665 {
5666 status = RA_NOMATCH;
5667 restore_subexpr(((regbehind_T *)rp) - 1);
5668 }
5669 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005670 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005671 regstack.ga_len -= sizeof(regbehind_T);
5672 }
5673 }
5674 break;
5675
5676 case RS_STAR_LONG:
5677 case RS_STAR_SHORT:
5678 {
5679 regstar_T *rst = ((regstar_T *)rp) - 1;
5680
5681 if (status == RA_MATCH)
5682 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005683 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005684 regstack.ga_len -= sizeof(regstar_T);
5685 break;
5686 }
5687
5688 /* Tried once already, restore input pointers. */
5689 if (status != RA_BREAK)
Bram Moolenaar582fd852005-03-28 20:58:01 +00005690 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005691
5692 /* Repeat until we found a position where it could match. */
5693 for (;;)
5694 {
5695 if (status != RA_BREAK)
5696 {
5697 /* Tried first position already, advance. */
5698 if (rp->rs_state == RS_STAR_LONG)
5699 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005700 /* Trying for longest match, but couldn't or
5701 * didn't match -- back up one char. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005702 if (--rst->count < rst->minval)
5703 break;
5704 if (reginput == regline)
5705 {
5706 /* backup to last char of previous line */
5707 --reglnum;
5708 regline = reg_getline(reglnum);
5709 /* Just in case regrepeat() didn't count
5710 * right. */
5711 if (regline == NULL)
5712 break;
5713 reginput = regline + STRLEN(regline);
5714 fast_breakcheck();
5715 }
5716 else
5717 mb_ptr_back(regline, reginput);
5718 }
5719 else
5720 {
5721 /* Range is backwards, use shortest match first.
5722 * Careful: maxval and minval are exchanged!
5723 * Couldn't or didn't match: try advancing one
5724 * char. */
5725 if (rst->count == rst->minval
5726 || regrepeat(OPERAND(rp->rs_scan), 1L) == 0)
5727 break;
5728 ++rst->count;
5729 }
5730 if (got_int)
5731 break;
5732 }
5733 else
5734 status = RA_NOMATCH;
5735
5736 /* If it could match, try it. */
5737 if (rst->nextb == NUL || *reginput == rst->nextb
5738 || *reginput == rst->nextb_ic)
5739 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005740 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005741 scan = regnext(rp->rs_scan);
5742 status = RA_CONT;
5743 break;
5744 }
5745 }
5746 if (status != RA_CONT)
5747 {
5748 /* Failed. */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005749 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005750 regstack.ga_len -= sizeof(regstar_T);
5751 status = RA_NOMATCH;
5752 }
5753 }
5754 break;
5755 }
5756
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005757 /* If we want to continue the inner loop or didn't pop a state
5758 * continue matching loop */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005759 if (status == RA_CONT || rp == (regitem_T *)
5760 ((char *)regstack.ga_data + regstack.ga_len) - 1)
5761 break;
5762 }
5763
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005764 /* May need to continue with the inner loop, starting at "scan". */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005765 if (status == RA_CONT)
5766 continue;
5767
5768 /*
5769 * If the regstack is empty or something failed we are done.
5770 */
5771 if (regstack.ga_len == 0 || status == RA_FAIL)
5772 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005773 if (scan == NULL)
5774 {
5775 /*
5776 * We get here only if there's trouble -- normally "case END" is
5777 * the terminating point.
5778 */
5779 EMSG(_(e_re_corr));
5780#ifdef DEBUG
5781 printf("Premature EOL\n");
5782#endif
5783 }
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005784 if (status == RA_FAIL)
5785 got_int = TRUE;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005786 return (status == RA_MATCH);
5787 }
5788
5789 } /* End of loop until the regstack is empty. */
5790
5791 /* NOTREACHED */
5792}
5793
5794/*
5795 * Push an item onto the regstack.
5796 * Returns pointer to new item. Returns NULL when out of memory.
5797 */
5798 static regitem_T *
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005799regstack_push(state, scan)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005800 regstate_T state;
5801 char_u *scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005802{
5803 regitem_T *rp;
5804
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005805 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005806 {
5807 EMSG(_(e_maxmempat));
5808 return NULL;
5809 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005810 if (ga_grow(&regstack, sizeof(regitem_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005811 return NULL;
5812
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005813 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005814 rp->rs_state = state;
5815 rp->rs_scan = scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005816
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005817 regstack.ga_len += sizeof(regitem_T);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005818 return rp;
5819}
5820
5821/*
5822 * Pop an item from the regstack.
5823 */
5824 static void
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005825regstack_pop(scan)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005826 char_u **scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005827{
5828 regitem_T *rp;
5829
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005830 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len) - 1;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005831 *scan = rp->rs_scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005832
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005833 regstack.ga_len -= sizeof(regitem_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834}
5835
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836/*
5837 * regrepeat - repeatedly match something simple, return how many.
5838 * Advances reginput (and reglnum) to just after the matched chars.
5839 */
5840 static int
5841regrepeat(p, maxcount)
5842 char_u *p;
5843 long maxcount; /* maximum number of matches allowed */
5844{
5845 long count = 0;
5846 char_u *scan;
5847 char_u *opnd;
5848 int mask;
5849 int testval = 0;
5850
5851 scan = reginput; /* Make local copy of reginput for speed. */
5852 opnd = OPERAND(p);
5853 switch (OP(p))
5854 {
5855 case ANY:
5856 case ANY + ADD_NL:
5857 while (count < maxcount)
5858 {
5859 /* Matching anything means we continue until end-of-line (or
5860 * end-of-file for ANY + ADD_NL), only limited by maxcount. */
5861 while (*scan != NUL && count < maxcount)
5862 {
5863 ++count;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005864 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 }
Bram Moolenaar640009d2006-10-17 16:48:26 +00005866 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline
5867 || reg_line_lbr || count == maxcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 break;
5869 ++count; /* count the line-break */
5870 reg_nextline();
5871 scan = reginput;
5872 if (got_int)
5873 break;
5874 }
5875 break;
5876
5877 case IDENT:
5878 case IDENT + ADD_NL:
5879 testval = TRUE;
5880 /*FALLTHROUGH*/
5881 case SIDENT:
5882 case SIDENT + ADD_NL:
5883 while (count < maxcount)
5884 {
Bram Moolenaar09ea9fc2013-05-21 00:03:02 +02005885 if (vim_isIDc(PTR2CHAR(scan)) && (testval || !VIM_ISDIGIT(*scan)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005887 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 }
5889 else if (*scan == NUL)
5890 {
Bram Moolenaar640009d2006-10-17 16:48:26 +00005891 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline
5892 || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 break;
5894 reg_nextline();
5895 scan = reginput;
5896 if (got_int)
5897 break;
5898 }
5899 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5900 ++scan;
5901 else
5902 break;
5903 ++count;
5904 }
5905 break;
5906
5907 case KWORD:
5908 case KWORD + ADD_NL:
5909 testval = TRUE;
5910 /*FALLTHROUGH*/
5911 case SKWORD:
5912 case SKWORD + ADD_NL:
5913 while (count < maxcount)
5914 {
Bram Moolenaarf813a182013-01-30 13:59:37 +01005915 if (vim_iswordp_buf(scan, reg_buf)
5916 && (testval || !VIM_ISDIGIT(*scan)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005918 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 }
5920 else if (*scan == NUL)
5921 {
Bram Moolenaar640009d2006-10-17 16:48:26 +00005922 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline
5923 || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 break;
5925 reg_nextline();
5926 scan = reginput;
5927 if (got_int)
5928 break;
5929 }
5930 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5931 ++scan;
5932 else
5933 break;
5934 ++count;
5935 }
5936 break;
5937
5938 case FNAME:
5939 case FNAME + ADD_NL:
5940 testval = TRUE;
5941 /*FALLTHROUGH*/
5942 case SFNAME:
5943 case SFNAME + ADD_NL:
5944 while (count < maxcount)
5945 {
Bram Moolenaar09ea9fc2013-05-21 00:03:02 +02005946 if (vim_isfilec(PTR2CHAR(scan)) && (testval || !VIM_ISDIGIT(*scan)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005948 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 }
5950 else if (*scan == NUL)
5951 {
Bram Moolenaar640009d2006-10-17 16:48:26 +00005952 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline
5953 || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 break;
5955 reg_nextline();
5956 scan = reginput;
5957 if (got_int)
5958 break;
5959 }
5960 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5961 ++scan;
5962 else
5963 break;
5964 ++count;
5965 }
5966 break;
5967
5968 case PRINT:
5969 case PRINT + ADD_NL:
5970 testval = TRUE;
5971 /*FALLTHROUGH*/
5972 case SPRINT:
5973 case SPRINT + ADD_NL:
5974 while (count < maxcount)
5975 {
5976 if (*scan == NUL)
5977 {
Bram Moolenaar640009d2006-10-17 16:48:26 +00005978 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline
5979 || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 break;
5981 reg_nextline();
5982 scan = reginput;
5983 if (got_int)
5984 break;
5985 }
5986 else if (ptr2cells(scan) == 1 && (testval || !VIM_ISDIGIT(*scan)))
5987 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005988 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 }
5990 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5991 ++scan;
5992 else
5993 break;
5994 ++count;
5995 }
5996 break;
5997
5998 case WHITE:
5999 case WHITE + ADD_NL:
6000 testval = mask = RI_WHITE;
6001do_class:
6002 while (count < maxcount)
6003 {
6004#ifdef FEAT_MBYTE
6005 int l;
6006#endif
6007 if (*scan == NUL)
6008 {
Bram Moolenaar640009d2006-10-17 16:48:26 +00006009 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline
6010 || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 break;
6012 reg_nextline();
6013 scan = reginput;
6014 if (got_int)
6015 break;
6016 }
6017#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006018 else if (has_mbyte && (l = (*mb_ptr2len)(scan)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 {
6020 if (testval != 0)
6021 break;
6022 scan += l;
6023 }
6024#endif
6025 else if ((class_tab[*scan] & mask) == testval)
6026 ++scan;
6027 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
6028 ++scan;
6029 else
6030 break;
6031 ++count;
6032 }
6033 break;
6034
6035 case NWHITE:
6036 case NWHITE + ADD_NL:
6037 mask = RI_WHITE;
6038 goto do_class;
6039 case DIGIT:
6040 case DIGIT + ADD_NL:
6041 testval = mask = RI_DIGIT;
6042 goto do_class;
6043 case NDIGIT:
6044 case NDIGIT + ADD_NL:
6045 mask = RI_DIGIT;
6046 goto do_class;
6047 case HEX:
6048 case HEX + ADD_NL:
6049 testval = mask = RI_HEX;
6050 goto do_class;
6051 case NHEX:
6052 case NHEX + ADD_NL:
6053 mask = RI_HEX;
6054 goto do_class;
6055 case OCTAL:
6056 case OCTAL + ADD_NL:
6057 testval = mask = RI_OCTAL;
6058 goto do_class;
6059 case NOCTAL:
6060 case NOCTAL + ADD_NL:
6061 mask = RI_OCTAL;
6062 goto do_class;
6063 case WORD:
6064 case WORD + ADD_NL:
6065 testval = mask = RI_WORD;
6066 goto do_class;
6067 case NWORD:
6068 case NWORD + ADD_NL:
6069 mask = RI_WORD;
6070 goto do_class;
6071 case HEAD:
6072 case HEAD + ADD_NL:
6073 testval = mask = RI_HEAD;
6074 goto do_class;
6075 case NHEAD:
6076 case NHEAD + ADD_NL:
6077 mask = RI_HEAD;
6078 goto do_class;
6079 case ALPHA:
6080 case ALPHA + ADD_NL:
6081 testval = mask = RI_ALPHA;
6082 goto do_class;
6083 case NALPHA:
6084 case NALPHA + ADD_NL:
6085 mask = RI_ALPHA;
6086 goto do_class;
6087 case LOWER:
6088 case LOWER + ADD_NL:
6089 testval = mask = RI_LOWER;
6090 goto do_class;
6091 case NLOWER:
6092 case NLOWER + ADD_NL:
6093 mask = RI_LOWER;
6094 goto do_class;
6095 case UPPER:
6096 case UPPER + ADD_NL:
6097 testval = mask = RI_UPPER;
6098 goto do_class;
6099 case NUPPER:
6100 case NUPPER + ADD_NL:
6101 mask = RI_UPPER;
6102 goto do_class;
6103
6104 case EXACTLY:
6105 {
6106 int cu, cl;
6107
6108 /* This doesn't do a multi-byte character, because a MULTIBYTECODE
Bram Moolenaara245a5b2007-08-11 11:58:23 +00006109 * would have been used for it. It does handle single-byte
6110 * characters, such as latin1. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 if (ireg_ic)
6112 {
Bram Moolenaara245a5b2007-08-11 11:58:23 +00006113 cu = MB_TOUPPER(*opnd);
6114 cl = MB_TOLOWER(*opnd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 while (count < maxcount && (*scan == cu || *scan == cl))
6116 {
6117 count++;
6118 scan++;
6119 }
6120 }
6121 else
6122 {
6123 cu = *opnd;
6124 while (count < maxcount && *scan == cu)
6125 {
6126 count++;
6127 scan++;
6128 }
6129 }
6130 break;
6131 }
6132
6133#ifdef FEAT_MBYTE
6134 case MULTIBYTECODE:
6135 {
6136 int i, len, cf = 0;
6137
6138 /* Safety check (just in case 'encoding' was changed since
6139 * compiling the program). */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006140 if ((len = (*mb_ptr2len)(opnd)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 {
6142 if (ireg_ic && enc_utf8)
6143 cf = utf_fold(utf_ptr2char(opnd));
6144 while (count < maxcount)
6145 {
6146 for (i = 0; i < len; ++i)
6147 if (opnd[i] != scan[i])
6148 break;
6149 if (i < len && (!ireg_ic || !enc_utf8
6150 || utf_fold(utf_ptr2char(scan)) != cf))
6151 break;
6152 scan += len;
6153 ++count;
6154 }
6155 }
6156 }
6157 break;
6158#endif
6159
6160 case ANYOF:
6161 case ANYOF + ADD_NL:
6162 testval = TRUE;
6163 /*FALLTHROUGH*/
6164
6165 case ANYBUT:
6166 case ANYBUT + ADD_NL:
6167 while (count < maxcount)
6168 {
6169#ifdef FEAT_MBYTE
6170 int len;
6171#endif
6172 if (*scan == NUL)
6173 {
Bram Moolenaar640009d2006-10-17 16:48:26 +00006174 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline
6175 || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 break;
6177 reg_nextline();
6178 scan = reginput;
6179 if (got_int)
6180 break;
6181 }
6182 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
6183 ++scan;
6184#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006185 else if (has_mbyte && (len = (*mb_ptr2len)(scan)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 {
6187 if ((cstrchr(opnd, (*mb_ptr2char)(scan)) == NULL) == testval)
6188 break;
6189 scan += len;
6190 }
6191#endif
6192 else
6193 {
6194 if ((cstrchr(opnd, *scan) == NULL) == testval)
6195 break;
6196 ++scan;
6197 }
6198 ++count;
6199 }
6200 break;
6201
6202 case NEWL:
6203 while (count < maxcount
Bram Moolenaar640009d2006-10-17 16:48:26 +00006204 && ((*scan == NUL && reglnum <= reg_maxline && !reg_line_lbr
6205 && REG_MULTI) || (*scan == '\n' && reg_line_lbr)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 {
6207 count++;
6208 if (reg_line_lbr)
6209 ADVANCE_REGINPUT();
6210 else
6211 reg_nextline();
6212 scan = reginput;
6213 if (got_int)
6214 break;
6215 }
6216 break;
6217
6218 default: /* Oh dear. Called inappropriately. */
6219 EMSG(_(e_re_corr));
6220#ifdef DEBUG
6221 printf("Called regrepeat with op code %d\n", OP(p));
6222#endif
6223 break;
6224 }
6225
6226 reginput = scan;
6227
6228 return (int)count;
6229}
6230
6231/*
6232 * regnext - dig the "next" pointer out of a node
Bram Moolenaard3005802009-11-25 17:21:32 +00006233 * Returns NULL when calculating size, when there is no next item and when
6234 * there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 */
6236 static char_u *
6237regnext(p)
6238 char_u *p;
6239{
6240 int offset;
6241
Bram Moolenaard3005802009-11-25 17:21:32 +00006242 if (p == JUST_CALC_SIZE || reg_toolong)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 return NULL;
6244
6245 offset = NEXT(p);
6246 if (offset == 0)
6247 return NULL;
6248
Bram Moolenaar582fd852005-03-28 20:58:01 +00006249 if (OP(p) == BACK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 return p - offset;
6251 else
6252 return p + offset;
6253}
6254
6255/*
6256 * Check the regexp program for its magic number.
6257 * Return TRUE if it's wrong.
6258 */
6259 static int
6260prog_magic_wrong()
6261{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006262 regprog_T *prog;
6263
6264 prog = REG_MULTI ? reg_mmatch->regprog : reg_match->regprog;
6265 if (prog->engine == &nfa_regengine)
6266 /* For NFA matcher we don't check the magic */
6267 return FALSE;
6268
6269 if (UCHARAT(((bt_regprog_T *)prog)->program) != REGMAGIC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 {
6271 EMSG(_(e_re_corr));
6272 return TRUE;
6273 }
6274 return FALSE;
6275}
6276
6277/*
6278 * Cleanup the subexpressions, if this wasn't done yet.
6279 * This construction is used to clear the subexpressions only when they are
6280 * used (to increase speed).
6281 */
6282 static void
6283cleanup_subexpr()
6284{
6285 if (need_clear_subexpr)
6286 {
6287 if (REG_MULTI)
6288 {
6289 /* Use 0xff to set lnum to -1 */
6290 vim_memset(reg_startpos, 0xff, sizeof(lpos_T) * NSUBEXP);
6291 vim_memset(reg_endpos, 0xff, sizeof(lpos_T) * NSUBEXP);
6292 }
6293 else
6294 {
6295 vim_memset(reg_startp, 0, sizeof(char_u *) * NSUBEXP);
6296 vim_memset(reg_endp, 0, sizeof(char_u *) * NSUBEXP);
6297 }
6298 need_clear_subexpr = FALSE;
6299 }
6300}
6301
6302#ifdef FEAT_SYN_HL
6303 static void
6304cleanup_zsubexpr()
6305{
6306 if (need_clear_zsubexpr)
6307 {
6308 if (REG_MULTI)
6309 {
6310 /* Use 0xff to set lnum to -1 */
6311 vim_memset(reg_startzpos, 0xff, sizeof(lpos_T) * NSUBEXP);
6312 vim_memset(reg_endzpos, 0xff, sizeof(lpos_T) * NSUBEXP);
6313 }
6314 else
6315 {
6316 vim_memset(reg_startzp, 0, sizeof(char_u *) * NSUBEXP);
6317 vim_memset(reg_endzp, 0, sizeof(char_u *) * NSUBEXP);
6318 }
6319 need_clear_zsubexpr = FALSE;
6320 }
6321}
6322#endif
6323
6324/*
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006325 * Save the current subexpr to "bp", so that they can be restored
6326 * later by restore_subexpr().
6327 */
6328 static void
6329save_subexpr(bp)
6330 regbehind_T *bp;
6331{
6332 int i;
6333
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006334 /* When "need_clear_subexpr" is set we don't need to save the values, only
6335 * remember that this flag needs to be set again when restoring. */
6336 bp->save_need_clear_subexpr = need_clear_subexpr;
6337 if (!need_clear_subexpr)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006338 {
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006339 for (i = 0; i < NSUBEXP; ++i)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006340 {
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006341 if (REG_MULTI)
6342 {
6343 bp->save_start[i].se_u.pos = reg_startpos[i];
6344 bp->save_end[i].se_u.pos = reg_endpos[i];
6345 }
6346 else
6347 {
6348 bp->save_start[i].se_u.ptr = reg_startp[i];
6349 bp->save_end[i].se_u.ptr = reg_endp[i];
6350 }
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006351 }
6352 }
6353}
6354
6355/*
6356 * Restore the subexpr from "bp".
6357 */
6358 static void
6359restore_subexpr(bp)
6360 regbehind_T *bp;
6361{
6362 int i;
6363
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006364 /* Only need to restore saved values when they are not to be cleared. */
6365 need_clear_subexpr = bp->save_need_clear_subexpr;
6366 if (!need_clear_subexpr)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006367 {
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006368 for (i = 0; i < NSUBEXP; ++i)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006369 {
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006370 if (REG_MULTI)
6371 {
6372 reg_startpos[i] = bp->save_start[i].se_u.pos;
6373 reg_endpos[i] = bp->save_end[i].se_u.pos;
6374 }
6375 else
6376 {
6377 reg_startp[i] = bp->save_start[i].se_u.ptr;
6378 reg_endp[i] = bp->save_end[i].se_u.ptr;
6379 }
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006380 }
6381 }
6382}
6383
6384/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 * Advance reglnum, regline and reginput to the next line.
6386 */
6387 static void
6388reg_nextline()
6389{
6390 regline = reg_getline(++reglnum);
6391 reginput = regline;
6392 fast_breakcheck();
6393}
6394
6395/*
6396 * Save the input line and position in a regsave_T.
6397 */
6398 static void
Bram Moolenaar582fd852005-03-28 20:58:01 +00006399reg_save(save, gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 regsave_T *save;
Bram Moolenaar582fd852005-03-28 20:58:01 +00006401 garray_T *gap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402{
6403 if (REG_MULTI)
6404 {
6405 save->rs_u.pos.col = (colnr_T)(reginput - regline);
6406 save->rs_u.pos.lnum = reglnum;
6407 }
6408 else
6409 save->rs_u.ptr = reginput;
Bram Moolenaar582fd852005-03-28 20:58:01 +00006410 save->rs_len = gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411}
6412
6413/*
6414 * Restore the input line and position from a regsave_T.
6415 */
6416 static void
Bram Moolenaar582fd852005-03-28 20:58:01 +00006417reg_restore(save, gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 regsave_T *save;
Bram Moolenaar582fd852005-03-28 20:58:01 +00006419 garray_T *gap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420{
6421 if (REG_MULTI)
6422 {
6423 if (reglnum != save->rs_u.pos.lnum)
6424 {
6425 /* only call reg_getline() when the line number changed to save
6426 * a bit of time */
6427 reglnum = save->rs_u.pos.lnum;
6428 regline = reg_getline(reglnum);
6429 }
6430 reginput = regline + save->rs_u.pos.col;
6431 }
6432 else
6433 reginput = save->rs_u.ptr;
Bram Moolenaar582fd852005-03-28 20:58:01 +00006434 gap->ga_len = save->rs_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435}
6436
6437/*
6438 * Return TRUE if current position is equal to saved position.
6439 */
6440 static int
6441reg_save_equal(save)
6442 regsave_T *save;
6443{
6444 if (REG_MULTI)
6445 return reglnum == save->rs_u.pos.lnum
6446 && reginput == regline + save->rs_u.pos.col;
6447 return reginput == save->rs_u.ptr;
6448}
6449
6450/*
6451 * Tentatively set the sub-expression start to the current position (after
6452 * calling regmatch() they will have changed). Need to save the existing
6453 * values for when there is no match.
6454 * Use se_save() to use pointer (save_se_multi()) or position (save_se_one()),
6455 * depending on REG_MULTI.
6456 */
6457 static void
6458save_se_multi(savep, posp)
6459 save_se_T *savep;
6460 lpos_T *posp;
6461{
6462 savep->se_u.pos = *posp;
6463 posp->lnum = reglnum;
6464 posp->col = (colnr_T)(reginput - regline);
6465}
6466
6467 static void
6468save_se_one(savep, pp)
6469 save_se_T *savep;
6470 char_u **pp;
6471{
6472 savep->se_u.ptr = *pp;
6473 *pp = reginput;
6474}
6475
6476/*
6477 * Compare a number with the operand of RE_LNUM, RE_COL or RE_VCOL.
6478 */
6479 static int
6480re_num_cmp(val, scan)
6481 long_u val;
6482 char_u *scan;
6483{
6484 long_u n = OPERAND_MIN(scan);
6485
6486 if (OPERAND_CMP(scan) == '>')
6487 return val > n;
6488 if (OPERAND_CMP(scan) == '<')
6489 return val < n;
6490 return val == n;
6491}
6492
6493
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006494#ifdef BT_REGEXP_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495
6496/*
6497 * regdump - dump a regexp onto stdout in vaguely comprehensible form
6498 */
6499 static void
6500regdump(pattern, r)
6501 char_u *pattern;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006502 bt_regprog_T *r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503{
6504 char_u *s;
6505 int op = EXACTLY; /* Arbitrary non-END op. */
6506 char_u *next;
6507 char_u *end = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006508 FILE *f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006510#ifdef BT_REGEXP_LOG
6511 f = fopen("bt_regexp_log.log", "a");
6512#else
6513 f = stdout;
6514#endif
6515 if (f == NULL)
6516 return;
6517 fprintf(f, "-------------------------------------\n\r\nregcomp(%s):\r\n", pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518
6519 s = r->program + 1;
6520 /*
6521 * Loop until we find the END that isn't before a referred next (an END
6522 * can also appear in a NOMATCH operand).
6523 */
6524 while (op != END || s <= end)
6525 {
6526 op = OP(s);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006527 fprintf(f, "%2d%s", (int)(s - r->program), regprop(s)); /* Where, what. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 next = regnext(s);
6529 if (next == NULL) /* Next ptr. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006530 fprintf(f, "(0)");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006532 fprintf(f, "(%d)", (int)((s - r->program) + (next - s)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 if (end < next)
6534 end = next;
6535 if (op == BRACE_LIMITS)
6536 {
6537 /* Two short ints */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006538 fprintf(f, " minval %ld, maxval %ld", OPERAND_MIN(s), OPERAND_MAX(s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 s += 8;
6540 }
6541 s += 3;
6542 if (op == ANYOF || op == ANYOF + ADD_NL
6543 || op == ANYBUT || op == ANYBUT + ADD_NL
6544 || op == EXACTLY)
6545 {
6546 /* Literal string, where present. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006547 fprintf(f, "\nxxxxxxxxx\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 while (*s != NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006549 fprintf(f, "%c", *s++);
6550 fprintf(f, "\nxxxxxxxxx\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 s++;
6552 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006553 fprintf(f, "\r\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 }
6555
6556 /* Header fields of interest. */
6557 if (r->regstart != NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006558 fprintf(f, "start `%s' 0x%x; ", r->regstart < 256
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 ? (char *)transchar(r->regstart)
6560 : "multibyte", r->regstart);
6561 if (r->reganch)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006562 fprintf(f, "anchored; ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 if (r->regmust != NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006564 fprintf(f, "must have \"%s\"", r->regmust);
6565 fprintf(f, "\r\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006567#ifdef BT_REGEXP_LOG
6568 fclose(f);
6569#endif
6570}
6571#endif /* BT_REGEXP_DUMP */
6572
6573#ifdef DEBUG
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574/*
6575 * regprop - printable representation of opcode
6576 */
6577 static char_u *
6578regprop(op)
6579 char_u *op;
6580{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006581 char *p;
6582 static char buf[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006584 STRCPY(buf, ":");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006586 switch ((int) OP(op))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 {
6588 case BOL:
6589 p = "BOL";
6590 break;
6591 case EOL:
6592 p = "EOL";
6593 break;
6594 case RE_BOF:
6595 p = "BOF";
6596 break;
6597 case RE_EOF:
6598 p = "EOF";
6599 break;
6600 case CURSOR:
6601 p = "CURSOR";
6602 break;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00006603 case RE_VISUAL:
6604 p = "RE_VISUAL";
6605 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 case RE_LNUM:
6607 p = "RE_LNUM";
6608 break;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00006609 case RE_MARK:
6610 p = "RE_MARK";
6611 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 case RE_COL:
6613 p = "RE_COL";
6614 break;
6615 case RE_VCOL:
6616 p = "RE_VCOL";
6617 break;
6618 case BOW:
6619 p = "BOW";
6620 break;
6621 case EOW:
6622 p = "EOW";
6623 break;
6624 case ANY:
6625 p = "ANY";
6626 break;
6627 case ANY + ADD_NL:
6628 p = "ANY+NL";
6629 break;
6630 case ANYOF:
6631 p = "ANYOF";
6632 break;
6633 case ANYOF + ADD_NL:
6634 p = "ANYOF+NL";
6635 break;
6636 case ANYBUT:
6637 p = "ANYBUT";
6638 break;
6639 case ANYBUT + ADD_NL:
6640 p = "ANYBUT+NL";
6641 break;
6642 case IDENT:
6643 p = "IDENT";
6644 break;
6645 case IDENT + ADD_NL:
6646 p = "IDENT+NL";
6647 break;
6648 case SIDENT:
6649 p = "SIDENT";
6650 break;
6651 case SIDENT + ADD_NL:
6652 p = "SIDENT+NL";
6653 break;
6654 case KWORD:
6655 p = "KWORD";
6656 break;
6657 case KWORD + ADD_NL:
6658 p = "KWORD+NL";
6659 break;
6660 case SKWORD:
6661 p = "SKWORD";
6662 break;
6663 case SKWORD + ADD_NL:
6664 p = "SKWORD+NL";
6665 break;
6666 case FNAME:
6667 p = "FNAME";
6668 break;
6669 case FNAME + ADD_NL:
6670 p = "FNAME+NL";
6671 break;
6672 case SFNAME:
6673 p = "SFNAME";
6674 break;
6675 case SFNAME + ADD_NL:
6676 p = "SFNAME+NL";
6677 break;
6678 case PRINT:
6679 p = "PRINT";
6680 break;
6681 case PRINT + ADD_NL:
6682 p = "PRINT+NL";
6683 break;
6684 case SPRINT:
6685 p = "SPRINT";
6686 break;
6687 case SPRINT + ADD_NL:
6688 p = "SPRINT+NL";
6689 break;
6690 case WHITE:
6691 p = "WHITE";
6692 break;
6693 case WHITE + ADD_NL:
6694 p = "WHITE+NL";
6695 break;
6696 case NWHITE:
6697 p = "NWHITE";
6698 break;
6699 case NWHITE + ADD_NL:
6700 p = "NWHITE+NL";
6701 break;
6702 case DIGIT:
6703 p = "DIGIT";
6704 break;
6705 case DIGIT + ADD_NL:
6706 p = "DIGIT+NL";
6707 break;
6708 case NDIGIT:
6709 p = "NDIGIT";
6710 break;
6711 case NDIGIT + ADD_NL:
6712 p = "NDIGIT+NL";
6713 break;
6714 case HEX:
6715 p = "HEX";
6716 break;
6717 case HEX + ADD_NL:
6718 p = "HEX+NL";
6719 break;
6720 case NHEX:
6721 p = "NHEX";
6722 break;
6723 case NHEX + ADD_NL:
6724 p = "NHEX+NL";
6725 break;
6726 case OCTAL:
6727 p = "OCTAL";
6728 break;
6729 case OCTAL + ADD_NL:
6730 p = "OCTAL+NL";
6731 break;
6732 case NOCTAL:
6733 p = "NOCTAL";
6734 break;
6735 case NOCTAL + ADD_NL:
6736 p = "NOCTAL+NL";
6737 break;
6738 case WORD:
6739 p = "WORD";
6740 break;
6741 case WORD + ADD_NL:
6742 p = "WORD+NL";
6743 break;
6744 case NWORD:
6745 p = "NWORD";
6746 break;
6747 case NWORD + ADD_NL:
6748 p = "NWORD+NL";
6749 break;
6750 case HEAD:
6751 p = "HEAD";
6752 break;
6753 case HEAD + ADD_NL:
6754 p = "HEAD+NL";
6755 break;
6756 case NHEAD:
6757 p = "NHEAD";
6758 break;
6759 case NHEAD + ADD_NL:
6760 p = "NHEAD+NL";
6761 break;
6762 case ALPHA:
6763 p = "ALPHA";
6764 break;
6765 case ALPHA + ADD_NL:
6766 p = "ALPHA+NL";
6767 break;
6768 case NALPHA:
6769 p = "NALPHA";
6770 break;
6771 case NALPHA + ADD_NL:
6772 p = "NALPHA+NL";
6773 break;
6774 case LOWER:
6775 p = "LOWER";
6776 break;
6777 case LOWER + ADD_NL:
6778 p = "LOWER+NL";
6779 break;
6780 case NLOWER:
6781 p = "NLOWER";
6782 break;
6783 case NLOWER + ADD_NL:
6784 p = "NLOWER+NL";
6785 break;
6786 case UPPER:
6787 p = "UPPER";
6788 break;
6789 case UPPER + ADD_NL:
6790 p = "UPPER+NL";
6791 break;
6792 case NUPPER:
6793 p = "NUPPER";
6794 break;
6795 case NUPPER + ADD_NL:
6796 p = "NUPPER+NL";
6797 break;
6798 case BRANCH:
6799 p = "BRANCH";
6800 break;
6801 case EXACTLY:
6802 p = "EXACTLY";
6803 break;
6804 case NOTHING:
6805 p = "NOTHING";
6806 break;
6807 case BACK:
6808 p = "BACK";
6809 break;
6810 case END:
6811 p = "END";
6812 break;
6813 case MOPEN + 0:
6814 p = "MATCH START";
6815 break;
6816 case MOPEN + 1:
6817 case MOPEN + 2:
6818 case MOPEN + 3:
6819 case MOPEN + 4:
6820 case MOPEN + 5:
6821 case MOPEN + 6:
6822 case MOPEN + 7:
6823 case MOPEN + 8:
6824 case MOPEN + 9:
6825 sprintf(buf + STRLEN(buf), "MOPEN%d", OP(op) - MOPEN);
6826 p = NULL;
6827 break;
6828 case MCLOSE + 0:
6829 p = "MATCH END";
6830 break;
6831 case MCLOSE + 1:
6832 case MCLOSE + 2:
6833 case MCLOSE + 3:
6834 case MCLOSE + 4:
6835 case MCLOSE + 5:
6836 case MCLOSE + 6:
6837 case MCLOSE + 7:
6838 case MCLOSE + 8:
6839 case MCLOSE + 9:
6840 sprintf(buf + STRLEN(buf), "MCLOSE%d", OP(op) - MCLOSE);
6841 p = NULL;
6842 break;
6843 case BACKREF + 1:
6844 case BACKREF + 2:
6845 case BACKREF + 3:
6846 case BACKREF + 4:
6847 case BACKREF + 5:
6848 case BACKREF + 6:
6849 case BACKREF + 7:
6850 case BACKREF + 8:
6851 case BACKREF + 9:
6852 sprintf(buf + STRLEN(buf), "BACKREF%d", OP(op) - BACKREF);
6853 p = NULL;
6854 break;
6855 case NOPEN:
6856 p = "NOPEN";
6857 break;
6858 case NCLOSE:
6859 p = "NCLOSE";
6860 break;
6861#ifdef FEAT_SYN_HL
6862 case ZOPEN + 1:
6863 case ZOPEN + 2:
6864 case ZOPEN + 3:
6865 case ZOPEN + 4:
6866 case ZOPEN + 5:
6867 case ZOPEN + 6:
6868 case ZOPEN + 7:
6869 case ZOPEN + 8:
6870 case ZOPEN + 9:
6871 sprintf(buf + STRLEN(buf), "ZOPEN%d", OP(op) - ZOPEN);
6872 p = NULL;
6873 break;
6874 case ZCLOSE + 1:
6875 case ZCLOSE + 2:
6876 case ZCLOSE + 3:
6877 case ZCLOSE + 4:
6878 case ZCLOSE + 5:
6879 case ZCLOSE + 6:
6880 case ZCLOSE + 7:
6881 case ZCLOSE + 8:
6882 case ZCLOSE + 9:
6883 sprintf(buf + STRLEN(buf), "ZCLOSE%d", OP(op) - ZCLOSE);
6884 p = NULL;
6885 break;
6886 case ZREF + 1:
6887 case ZREF + 2:
6888 case ZREF + 3:
6889 case ZREF + 4:
6890 case ZREF + 5:
6891 case ZREF + 6:
6892 case ZREF + 7:
6893 case ZREF + 8:
6894 case ZREF + 9:
6895 sprintf(buf + STRLEN(buf), "ZREF%d", OP(op) - ZREF);
6896 p = NULL;
6897 break;
6898#endif
6899 case STAR:
6900 p = "STAR";
6901 break;
6902 case PLUS:
6903 p = "PLUS";
6904 break;
6905 case NOMATCH:
6906 p = "NOMATCH";
6907 break;
6908 case MATCH:
6909 p = "MATCH";
6910 break;
6911 case BEHIND:
6912 p = "BEHIND";
6913 break;
6914 case NOBEHIND:
6915 p = "NOBEHIND";
6916 break;
6917 case SUBPAT:
6918 p = "SUBPAT";
6919 break;
6920 case BRACE_LIMITS:
6921 p = "BRACE_LIMITS";
6922 break;
6923 case BRACE_SIMPLE:
6924 p = "BRACE_SIMPLE";
6925 break;
6926 case BRACE_COMPLEX + 0:
6927 case BRACE_COMPLEX + 1:
6928 case BRACE_COMPLEX + 2:
6929 case BRACE_COMPLEX + 3:
6930 case BRACE_COMPLEX + 4:
6931 case BRACE_COMPLEX + 5:
6932 case BRACE_COMPLEX + 6:
6933 case BRACE_COMPLEX + 7:
6934 case BRACE_COMPLEX + 8:
6935 case BRACE_COMPLEX + 9:
6936 sprintf(buf + STRLEN(buf), "BRACE_COMPLEX%d", OP(op) - BRACE_COMPLEX);
6937 p = NULL;
6938 break;
6939#ifdef FEAT_MBYTE
6940 case MULTIBYTECODE:
6941 p = "MULTIBYTECODE";
6942 break;
6943#endif
6944 case NEWL:
6945 p = "NEWL";
6946 break;
6947 default:
6948 sprintf(buf + STRLEN(buf), "corrupt %d", OP(op));
6949 p = NULL;
6950 break;
6951 }
6952 if (p != NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006953 STRCAT(buf, p);
6954 return (char_u *)buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006956#endif /* DEBUG */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957
6958#ifdef FEAT_MBYTE
6959static void mb_decompose __ARGS((int c, int *c1, int *c2, int *c3));
6960
6961typedef struct
6962{
6963 int a, b, c;
6964} decomp_T;
6965
6966
6967/* 0xfb20 - 0xfb4f */
Bram Moolenaard6f676d2005-06-01 21:51:55 +00006968static decomp_T decomp_table[0xfb4f-0xfb20+1] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969{
6970 {0x5e2,0,0}, /* 0xfb20 alt ayin */
6971 {0x5d0,0,0}, /* 0xfb21 alt alef */
6972 {0x5d3,0,0}, /* 0xfb22 alt dalet */
6973 {0x5d4,0,0}, /* 0xfb23 alt he */
6974 {0x5db,0,0}, /* 0xfb24 alt kaf */
6975 {0x5dc,0,0}, /* 0xfb25 alt lamed */
6976 {0x5dd,0,0}, /* 0xfb26 alt mem-sofit */
6977 {0x5e8,0,0}, /* 0xfb27 alt resh */
6978 {0x5ea,0,0}, /* 0xfb28 alt tav */
6979 {'+', 0, 0}, /* 0xfb29 alt plus */
6980 {0x5e9, 0x5c1, 0}, /* 0xfb2a shin+shin-dot */
6981 {0x5e9, 0x5c2, 0}, /* 0xfb2b shin+sin-dot */
6982 {0x5e9, 0x5c1, 0x5bc}, /* 0xfb2c shin+shin-dot+dagesh */
6983 {0x5e9, 0x5c2, 0x5bc}, /* 0xfb2d shin+sin-dot+dagesh */
6984 {0x5d0, 0x5b7, 0}, /* 0xfb2e alef+patah */
6985 {0x5d0, 0x5b8, 0}, /* 0xfb2f alef+qamats */
6986 {0x5d0, 0x5b4, 0}, /* 0xfb30 alef+hiriq */
6987 {0x5d1, 0x5bc, 0}, /* 0xfb31 bet+dagesh */
6988 {0x5d2, 0x5bc, 0}, /* 0xfb32 gimel+dagesh */
6989 {0x5d3, 0x5bc, 0}, /* 0xfb33 dalet+dagesh */
6990 {0x5d4, 0x5bc, 0}, /* 0xfb34 he+dagesh */
6991 {0x5d5, 0x5bc, 0}, /* 0xfb35 vav+dagesh */
6992 {0x5d6, 0x5bc, 0}, /* 0xfb36 zayin+dagesh */
6993 {0xfb37, 0, 0}, /* 0xfb37 -- UNUSED */
6994 {0x5d8, 0x5bc, 0}, /* 0xfb38 tet+dagesh */
6995 {0x5d9, 0x5bc, 0}, /* 0xfb39 yud+dagesh */
6996 {0x5da, 0x5bc, 0}, /* 0xfb3a kaf sofit+dagesh */
6997 {0x5db, 0x5bc, 0}, /* 0xfb3b kaf+dagesh */
6998 {0x5dc, 0x5bc, 0}, /* 0xfb3c lamed+dagesh */
6999 {0xfb3d, 0, 0}, /* 0xfb3d -- UNUSED */
7000 {0x5de, 0x5bc, 0}, /* 0xfb3e mem+dagesh */
7001 {0xfb3f, 0, 0}, /* 0xfb3f -- UNUSED */
7002 {0x5e0, 0x5bc, 0}, /* 0xfb40 nun+dagesh */
7003 {0x5e1, 0x5bc, 0}, /* 0xfb41 samech+dagesh */
7004 {0xfb42, 0, 0}, /* 0xfb42 -- UNUSED */
7005 {0x5e3, 0x5bc, 0}, /* 0xfb43 pe sofit+dagesh */
7006 {0x5e4, 0x5bc,0}, /* 0xfb44 pe+dagesh */
7007 {0xfb45, 0, 0}, /* 0xfb45 -- UNUSED */
7008 {0x5e6, 0x5bc, 0}, /* 0xfb46 tsadi+dagesh */
7009 {0x5e7, 0x5bc, 0}, /* 0xfb47 qof+dagesh */
7010 {0x5e8, 0x5bc, 0}, /* 0xfb48 resh+dagesh */
7011 {0x5e9, 0x5bc, 0}, /* 0xfb49 shin+dagesh */
7012 {0x5ea, 0x5bc, 0}, /* 0xfb4a tav+dagesh */
7013 {0x5d5, 0x5b9, 0}, /* 0xfb4b vav+holam */
7014 {0x5d1, 0x5bf, 0}, /* 0xfb4c bet+rafe */
7015 {0x5db, 0x5bf, 0}, /* 0xfb4d kaf+rafe */
7016 {0x5e4, 0x5bf, 0}, /* 0xfb4e pe+rafe */
7017 {0x5d0, 0x5dc, 0} /* 0xfb4f alef-lamed */
7018};
7019
7020 static void
7021mb_decompose(c, c1, c2, c3)
7022 int c, *c1, *c2, *c3;
7023{
7024 decomp_T d;
7025
Bram Moolenaar2eec59e2013-05-21 21:37:20 +02007026 if (c >= 0xfb20 && c <= 0xfb4f)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 {
7028 d = decomp_table[c - 0xfb20];
7029 *c1 = d.a;
7030 *c2 = d.b;
7031 *c3 = d.c;
7032 }
7033 else
7034 {
7035 *c1 = c;
7036 *c2 = *c3 = 0;
7037 }
7038}
7039#endif
7040
7041/*
7042 * Compare two strings, ignore case if ireg_ic set.
7043 * Return 0 if strings match, non-zero otherwise.
7044 * Correct the length "*n" when composing characters are ignored.
7045 */
7046 static int
7047cstrncmp(s1, s2, n)
7048 char_u *s1, *s2;
7049 int *n;
7050{
7051 int result;
7052
7053 if (!ireg_ic)
7054 result = STRNCMP(s1, s2, *n);
7055 else
7056 result = MB_STRNICMP(s1, s2, *n);
7057
7058#ifdef FEAT_MBYTE
7059 /* if it failed and it's utf8 and we want to combineignore: */
7060 if (result != 0 && enc_utf8 && ireg_icombine)
7061 {
7062 char_u *str1, *str2;
7063 int c1, c2, c11, c12;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 int junk;
7065
7066 /* we have to handle the strcmp ourselves, since it is necessary to
7067 * deal with the composing characters by ignoring them: */
7068 str1 = s1;
7069 str2 = s2;
7070 c1 = c2 = 0;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007071 while ((int)(str1 - s1) < *n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 {
7073 c1 = mb_ptr2char_adv(&str1);
7074 c2 = mb_ptr2char_adv(&str2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075
7076 /* decompose the character if necessary, into 'base' characters
7077 * because I don't care about Arabic, I will hard-code the Hebrew
7078 * which I *do* care about! So sue me... */
7079 if (c1 != c2 && (!ireg_ic || utf_fold(c1) != utf_fold(c2)))
7080 {
7081 /* decomposition necessary? */
7082 mb_decompose(c1, &c11, &junk, &junk);
7083 mb_decompose(c2, &c12, &junk, &junk);
7084 c1 = c11;
7085 c2 = c12;
7086 if (c11 != c12 && (!ireg_ic || utf_fold(c11) != utf_fold(c12)))
7087 break;
7088 }
7089 }
7090 result = c2 - c1;
7091 if (result == 0)
7092 *n = (int)(str2 - s2);
7093 }
7094#endif
7095
7096 return result;
7097}
7098
7099/*
7100 * cstrchr: This function is used a lot for simple searches, keep it fast!
7101 */
7102 static char_u *
7103cstrchr(s, c)
7104 char_u *s;
7105 int c;
7106{
7107 char_u *p;
7108 int cc;
7109
7110 if (!ireg_ic
7111#ifdef FEAT_MBYTE
7112 || (!enc_utf8 && mb_char2len(c) > 1)
7113#endif
7114 )
7115 return vim_strchr(s, c);
7116
7117 /* tolower() and toupper() can be slow, comparing twice should be a lot
7118 * faster (esp. when using MS Visual C++!).
7119 * For UTF-8 need to use folded case. */
7120#ifdef FEAT_MBYTE
7121 if (enc_utf8 && c > 0x80)
7122 cc = utf_fold(c);
7123 else
7124#endif
Bram Moolenaara245a5b2007-08-11 11:58:23 +00007125 if (MB_ISUPPER(c))
7126 cc = MB_TOLOWER(c);
7127 else if (MB_ISLOWER(c))
7128 cc = MB_TOUPPER(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 else
7130 return vim_strchr(s, c);
7131
7132#ifdef FEAT_MBYTE
7133 if (has_mbyte)
7134 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007135 for (p = s; *p != NUL; p += (*mb_ptr2len)(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 {
7137 if (enc_utf8 && c > 0x80)
7138 {
7139 if (utf_fold(utf_ptr2char(p)) == cc)
7140 return p;
7141 }
7142 else if (*p == c || *p == cc)
7143 return p;
7144 }
7145 }
7146 else
7147#endif
7148 /* Faster version for when there are no multi-byte characters. */
7149 for (p = s; *p != NUL; ++p)
7150 if (*p == c || *p == cc)
7151 return p;
7152
7153 return NULL;
7154}
7155
7156/***************************************************************
7157 * regsub stuff *
7158 ***************************************************************/
7159
7160/* This stuff below really confuses cc on an SGI -- webb */
7161#ifdef __sgi
7162# undef __ARGS
7163# define __ARGS(x) ()
7164#endif
7165
7166/*
7167 * We should define ftpr as a pointer to a function returning a pointer to
7168 * a function returning a pointer to a function ...
7169 * This is impossible, so we declare a pointer to a function returning a
7170 * pointer to a function returning void. This should work for all compilers.
7171 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007172typedef void (*(*fptr_T) __ARGS((int *, int)))();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007174static fptr_T do_upper __ARGS((int *, int));
7175static fptr_T do_Upper __ARGS((int *, int));
7176static fptr_T do_lower __ARGS((int *, int));
7177static fptr_T do_Lower __ARGS((int *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178
7179static int vim_regsub_both __ARGS((char_u *source, char_u *dest, int copy, int magic, int backslash));
7180
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007181 static fptr_T
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182do_upper(d, c)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007183 int *d;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 int c;
7185{
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007186 *d = MB_TOUPPER(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007188 return (fptr_T)NULL;
7189}
7190
7191 static fptr_T
7192do_Upper(d, c)
7193 int *d;
7194 int c;
7195{
7196 *d = MB_TOUPPER(c);
7197
7198 return (fptr_T)do_Upper;
7199}
7200
7201 static fptr_T
7202do_lower(d, c)
7203 int *d;
7204 int c;
7205{
7206 *d = MB_TOLOWER(c);
7207
7208 return (fptr_T)NULL;
7209}
7210
7211 static fptr_T
7212do_Lower(d, c)
7213 int *d;
7214 int c;
7215{
7216 *d = MB_TOLOWER(c);
7217
7218 return (fptr_T)do_Lower;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219}
7220
7221/*
7222 * regtilde(): Replace tildes in the pattern by the old pattern.
7223 *
7224 * Short explanation of the tilde: It stands for the previous replacement
7225 * pattern. If that previous pattern also contains a ~ we should go back a
7226 * step further... But we insert the previous pattern into the current one
7227 * and remember that.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007228 * This still does not handle the case where "magic" changes. So require the
7229 * user to keep his hands off of "magic".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 *
7231 * The tildes are parsed once before the first call to vim_regsub().
7232 */
7233 char_u *
7234regtilde(source, magic)
7235 char_u *source;
7236 int magic;
7237{
7238 char_u *newsub = source;
7239 char_u *tmpsub;
7240 char_u *p;
7241 int len;
7242 int prevlen;
7243
7244 for (p = newsub; *p; ++p)
7245 {
7246 if ((*p == '~' && magic) || (*p == '\\' && *(p + 1) == '~' && !magic))
7247 {
7248 if (reg_prev_sub != NULL)
7249 {
7250 /* length = len(newsub) - 1 + len(prev_sub) + 1 */
7251 prevlen = (int)STRLEN(reg_prev_sub);
7252 tmpsub = alloc((unsigned)(STRLEN(newsub) + prevlen));
7253 if (tmpsub != NULL)
7254 {
7255 /* copy prefix */
7256 len = (int)(p - newsub); /* not including ~ */
7257 mch_memmove(tmpsub, newsub, (size_t)len);
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00007258 /* interpret tilde */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 mch_memmove(tmpsub + len, reg_prev_sub, (size_t)prevlen);
7260 /* copy postfix */
7261 if (!magic)
7262 ++p; /* back off \ */
7263 STRCPY(tmpsub + len + prevlen, p + 1);
7264
7265 if (newsub != source) /* already allocated newsub */
7266 vim_free(newsub);
7267 newsub = tmpsub;
7268 p = newsub + len + prevlen;
7269 }
7270 }
7271 else if (magic)
Bram Moolenaar446cb832008-06-24 21:56:24 +00007272 STRMOVE(p, p + 1); /* remove '~' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 else
Bram Moolenaar446cb832008-06-24 21:56:24 +00007274 STRMOVE(p, p + 2); /* remove '\~' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 --p;
7276 }
7277 else
7278 {
7279 if (*p == '\\' && p[1]) /* skip escaped characters */
7280 ++p;
7281#ifdef FEAT_MBYTE
7282 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007283 p += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284#endif
7285 }
7286 }
7287
7288 vim_free(reg_prev_sub);
7289 if (newsub != source) /* newsub was allocated, just keep it */
7290 reg_prev_sub = newsub;
7291 else /* no ~ found, need to save newsub */
7292 reg_prev_sub = vim_strsave(newsub);
7293 return newsub;
7294}
7295
7296#ifdef FEAT_EVAL
7297static int can_f_submatch = FALSE; /* TRUE when submatch() can be used */
7298
7299/* These pointers are used instead of reg_match and reg_mmatch for
7300 * reg_submatch(). Needed for when the substitution string is an expression
7301 * that contains a call to substitute() and submatch(). */
7302static regmatch_T *submatch_match;
7303static regmmatch_T *submatch_mmatch;
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007304static linenr_T submatch_firstlnum;
7305static linenr_T submatch_maxline;
Bram Moolenaar978287b2011-06-19 04:32:15 +02007306static int submatch_line_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307#endif
7308
7309#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
7310/*
7311 * vim_regsub() - perform substitutions after a vim_regexec() or
7312 * vim_regexec_multi() match.
7313 *
7314 * If "copy" is TRUE really copy into "dest".
7315 * If "copy" is FALSE nothing is copied, this is just to find out the length
7316 * of the result.
7317 *
7318 * If "backslash" is TRUE, a backslash will be removed later, need to double
7319 * them to keep them, and insert a backslash before a CR to avoid it being
7320 * replaced with a line break later.
7321 *
7322 * Note: The matched text must not change between the call of
7323 * vim_regexec()/vim_regexec_multi() and vim_regsub()! It would make the back
7324 * references invalid!
7325 *
7326 * Returns the size of the replacement, including terminating NUL.
7327 */
7328 int
7329vim_regsub(rmp, source, dest, copy, magic, backslash)
7330 regmatch_T *rmp;
7331 char_u *source;
7332 char_u *dest;
7333 int copy;
7334 int magic;
7335 int backslash;
7336{
7337 reg_match = rmp;
7338 reg_mmatch = NULL;
7339 reg_maxline = 0;
Bram Moolenaar2f315ab2013-01-25 20:11:01 +01007340 reg_buf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 return vim_regsub_both(source, dest, copy, magic, backslash);
7342}
7343#endif
7344
7345 int
7346vim_regsub_multi(rmp, lnum, source, dest, copy, magic, backslash)
7347 regmmatch_T *rmp;
7348 linenr_T lnum;
7349 char_u *source;
7350 char_u *dest;
7351 int copy;
7352 int magic;
7353 int backslash;
7354{
7355 reg_match = NULL;
7356 reg_mmatch = rmp;
7357 reg_buf = curbuf; /* always works on the current buffer! */
7358 reg_firstlnum = lnum;
7359 reg_maxline = curbuf->b_ml.ml_line_count - lnum;
7360 return vim_regsub_both(source, dest, copy, magic, backslash);
7361}
7362
7363 static int
7364vim_regsub_both(source, dest, copy, magic, backslash)
7365 char_u *source;
7366 char_u *dest;
7367 int copy;
7368 int magic;
7369 int backslash;
7370{
7371 char_u *src;
7372 char_u *dst;
7373 char_u *s;
7374 int c;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007375 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 int no = -1;
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007377 fptr_T func_all = (fptr_T)NULL;
7378 fptr_T func_one = (fptr_T)NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 linenr_T clnum = 0; /* init for GCC */
7380 int len = 0; /* init for GCC */
7381#ifdef FEAT_EVAL
7382 static char_u *eval_result = NULL;
7383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384
7385 /* Be paranoid... */
7386 if (source == NULL || dest == NULL)
7387 {
7388 EMSG(_(e_null));
7389 return 0;
7390 }
7391 if (prog_magic_wrong())
7392 return 0;
7393 src = source;
7394 dst = dest;
7395
7396 /*
7397 * When the substitute part starts with "\=" evaluate it as an expression.
7398 */
7399 if (source[0] == '\\' && source[1] == '='
7400#ifdef FEAT_EVAL
7401 && !can_f_submatch /* can't do this recursively */
7402#endif
7403 )
7404 {
7405#ifdef FEAT_EVAL
7406 /* To make sure that the length doesn't change between checking the
7407 * length and copying the string, and to speed up things, the
7408 * resulting string is saved from the call with "copy" == FALSE to the
7409 * call with "copy" == TRUE. */
7410 if (copy)
7411 {
7412 if (eval_result != NULL)
7413 {
7414 STRCPY(dest, eval_result);
7415 dst += STRLEN(eval_result);
7416 vim_free(eval_result);
7417 eval_result = NULL;
7418 }
7419 }
7420 else
7421 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 win_T *save_reg_win;
7423 int save_ireg_ic;
7424
7425 vim_free(eval_result);
7426
7427 /* The expression may contain substitute(), which calls us
7428 * recursively. Make sure submatch() gets the text from the first
7429 * level. Don't need to save "reg_buf", because
7430 * vim_regexec_multi() can't be called recursively. */
7431 submatch_match = reg_match;
7432 submatch_mmatch = reg_mmatch;
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007433 submatch_firstlnum = reg_firstlnum;
7434 submatch_maxline = reg_maxline;
Bram Moolenaar978287b2011-06-19 04:32:15 +02007435 submatch_line_lbr = reg_line_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 save_reg_win = reg_win;
7437 save_ireg_ic = ireg_ic;
7438 can_f_submatch = TRUE;
7439
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007440 eval_result = eval_to_string(source + 2, NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 if (eval_result != NULL)
7442 {
Bram Moolenaar06975a42010-03-23 16:27:22 +01007443 int had_backslash = FALSE;
7444
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00007445 for (s = eval_result; *s != NUL; mb_ptr_adv(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 {
Bram Moolenaar978287b2011-06-19 04:32:15 +02007447 /* Change NL to CR, so that it becomes a line break,
7448 * unless called from vim_regexec_nl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 * Skip over a backslashed character. */
Bram Moolenaar978287b2011-06-19 04:32:15 +02007450 if (*s == NL && !submatch_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 *s = CAR;
7452 else if (*s == '\\' && s[1] != NUL)
Bram Moolenaar06975a42010-03-23 16:27:22 +01007453 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 ++s;
Bram Moolenaar60190782010-05-21 13:08:58 +02007455 /* Change NL to CR here too, so that this works:
7456 * :s/abc\\\ndef/\="aaa\\\nbbb"/ on text:
7457 * abc\
7458 * def
Bram Moolenaar978287b2011-06-19 04:32:15 +02007459 * Not when called from vim_regexec_nl().
Bram Moolenaar60190782010-05-21 13:08:58 +02007460 */
Bram Moolenaar978287b2011-06-19 04:32:15 +02007461 if (*s == NL && !submatch_line_lbr)
Bram Moolenaar60190782010-05-21 13:08:58 +02007462 *s = CAR;
Bram Moolenaar06975a42010-03-23 16:27:22 +01007463 had_backslash = TRUE;
7464 }
7465 }
7466 if (had_backslash && backslash)
7467 {
7468 /* Backslashes will be consumed, need to double them. */
7469 s = vim_strsave_escaped(eval_result, (char_u *)"\\");
7470 if (s != NULL)
7471 {
7472 vim_free(eval_result);
7473 eval_result = s;
7474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 }
7476
7477 dst += STRLEN(eval_result);
7478 }
7479
7480 reg_match = submatch_match;
7481 reg_mmatch = submatch_mmatch;
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007482 reg_firstlnum = submatch_firstlnum;
7483 reg_maxline = submatch_maxline;
Bram Moolenaar978287b2011-06-19 04:32:15 +02007484 reg_line_lbr = submatch_line_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 reg_win = save_reg_win;
7486 ireg_ic = save_ireg_ic;
7487 can_f_submatch = FALSE;
7488 }
7489#endif
7490 }
7491 else
7492 while ((c = *src++) != NUL)
7493 {
7494 if (c == '&' && magic)
7495 no = 0;
7496 else if (c == '\\' && *src != NUL)
7497 {
7498 if (*src == '&' && !magic)
7499 {
7500 ++src;
7501 no = 0;
7502 }
7503 else if ('0' <= *src && *src <= '9')
7504 {
7505 no = *src++ - '0';
7506 }
7507 else if (vim_strchr((char_u *)"uUlLeE", *src))
7508 {
7509 switch (*src++)
7510 {
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007511 case 'u': func_one = (fptr_T)do_upper;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 continue;
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007513 case 'U': func_all = (fptr_T)do_Upper;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514 continue;
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007515 case 'l': func_one = (fptr_T)do_lower;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 continue;
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007517 case 'L': func_all = (fptr_T)do_Lower;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 continue;
7519 case 'e':
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007520 case 'E': func_one = func_all = (fptr_T)NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 continue;
7522 }
7523 }
7524 }
7525 if (no < 0) /* Ordinary character. */
7526 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00007527 if (c == K_SPECIAL && src[0] != NUL && src[1] != NUL)
7528 {
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00007529 /* Copy a special key as-is. */
Bram Moolenaardb552d602006-03-23 22:59:57 +00007530 if (copy)
7531 {
7532 *dst++ = c;
7533 *dst++ = *src++;
7534 *dst++ = *src++;
7535 }
7536 else
7537 {
7538 dst += 3;
7539 src += 2;
7540 }
7541 continue;
7542 }
7543
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 if (c == '\\' && *src != NUL)
7545 {
7546 /* Check for abbreviations -- webb */
7547 switch (*src)
7548 {
7549 case 'r': c = CAR; ++src; break;
7550 case 'n': c = NL; ++src; break;
7551 case 't': c = TAB; ++src; break;
7552 /* Oh no! \e already has meaning in subst pat :-( */
7553 /* case 'e': c = ESC; ++src; break; */
7554 case 'b': c = Ctrl_H; ++src; break;
7555
7556 /* If "backslash" is TRUE the backslash will be removed
7557 * later. Used to insert a literal CR. */
7558 default: if (backslash)
7559 {
7560 if (copy)
7561 *dst = '\\';
7562 ++dst;
7563 }
7564 c = *src++;
7565 }
7566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567#ifdef FEAT_MBYTE
Bram Moolenaardb552d602006-03-23 22:59:57 +00007568 else if (has_mbyte)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007569 c = mb_ptr2char(src - 1);
7570#endif
7571
Bram Moolenaardb552d602006-03-23 22:59:57 +00007572 /* Write to buffer, if copy is set. */
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007573 if (func_one != (fptr_T)NULL)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007574 /* Turbo C complains without the typecast */
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007575 func_one = (fptr_T)(func_one(&cc, c));
7576 else if (func_all != (fptr_T)NULL)
7577 /* Turbo C complains without the typecast */
7578 func_all = (fptr_T)(func_all(&cc, c));
7579 else /* just copy */
7580 cc = c;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007581
7582#ifdef FEAT_MBYTE
7583 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 {
Bram Moolenaar0c56c602010-07-12 22:42:33 +02007585 int totlen = mb_ptr2len(src - 1);
7586
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 if (copy)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007588 mb_char2bytes(cc, dst);
7589 dst += mb_char2len(cc) - 1;
Bram Moolenaar0c56c602010-07-12 22:42:33 +02007590 if (enc_utf8)
7591 {
7592 int clen = utf_ptr2len(src - 1);
7593
7594 /* If the character length is shorter than "totlen", there
7595 * are composing characters; copy them as-is. */
7596 if (clen < totlen)
7597 {
7598 if (copy)
7599 mch_memmove(dst + 1, src - 1 + clen,
7600 (size_t)(totlen - clen));
7601 dst += totlen - clen;
7602 }
7603 }
7604 src += totlen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 }
7606 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607#endif
7608 if (copy)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007609 *dst = cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 dst++;
7611 }
7612 else
7613 {
7614 if (REG_MULTI)
7615 {
7616 clnum = reg_mmatch->startpos[no].lnum;
7617 if (clnum < 0 || reg_mmatch->endpos[no].lnum < 0)
7618 s = NULL;
7619 else
7620 {
7621 s = reg_getline(clnum) + reg_mmatch->startpos[no].col;
7622 if (reg_mmatch->endpos[no].lnum == clnum)
7623 len = reg_mmatch->endpos[no].col
7624 - reg_mmatch->startpos[no].col;
7625 else
7626 len = (int)STRLEN(s);
7627 }
7628 }
7629 else
7630 {
7631 s = reg_match->startp[no];
7632 if (reg_match->endp[no] == NULL)
7633 s = NULL;
7634 else
7635 len = (int)(reg_match->endp[no] - s);
7636 }
7637 if (s != NULL)
7638 {
7639 for (;;)
7640 {
7641 if (len == 0)
7642 {
7643 if (REG_MULTI)
7644 {
7645 if (reg_mmatch->endpos[no].lnum == clnum)
7646 break;
7647 if (copy)
7648 *dst = CAR;
7649 ++dst;
7650 s = reg_getline(++clnum);
7651 if (reg_mmatch->endpos[no].lnum == clnum)
7652 len = reg_mmatch->endpos[no].col;
7653 else
7654 len = (int)STRLEN(s);
7655 }
7656 else
7657 break;
7658 }
7659 else if (*s == NUL) /* we hit NUL. */
7660 {
7661 if (copy)
7662 EMSG(_(e_re_damg));
7663 goto exit;
7664 }
7665 else
7666 {
7667 if (backslash && (*s == CAR || *s == '\\'))
7668 {
7669 /*
7670 * Insert a backslash in front of a CR, otherwise
7671 * it will be replaced by a line break.
7672 * Number of backslashes will be halved later,
7673 * double them here.
7674 */
7675 if (copy)
7676 {
7677 dst[0] = '\\';
7678 dst[1] = *s;
7679 }
7680 dst += 2;
7681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 else
7683 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007684#ifdef FEAT_MBYTE
7685 if (has_mbyte)
7686 c = mb_ptr2char(s);
7687 else
7688#endif
7689 c = *s;
7690
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007691 if (func_one != (fptr_T)NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 /* Turbo C complains without the typecast */
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007693 func_one = (fptr_T)(func_one(&cc, c));
7694 else if (func_all != (fptr_T)NULL)
7695 /* Turbo C complains without the typecast */
7696 func_all = (fptr_T)(func_all(&cc, c));
7697 else /* just copy */
7698 cc = c;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007699
7700#ifdef FEAT_MBYTE
7701 if (has_mbyte)
7702 {
Bram Moolenaar9225efb2007-07-30 20:32:53 +00007703 int l;
7704
7705 /* Copy composing characters separately, one
7706 * at a time. */
7707 if (enc_utf8)
7708 l = utf_ptr2len(s) - 1;
7709 else
7710 l = mb_ptr2len(s) - 1;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007711
7712 s += l;
7713 len -= l;
7714 if (copy)
7715 mb_char2bytes(cc, dst);
7716 dst += mb_char2len(cc) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007718 else
7719#endif
7720 if (copy)
7721 *dst = cc;
7722 dst++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007724
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 ++s;
7726 --len;
7727 }
7728 }
7729 }
7730 no = -1;
7731 }
7732 }
7733 if (copy)
7734 *dst = NUL;
7735
7736exit:
7737 return (int)((dst - dest) + 1);
7738}
7739
7740#ifdef FEAT_EVAL
Bram Moolenaard32a3192009-11-26 19:40:49 +00007741static char_u *reg_getline_submatch __ARGS((linenr_T lnum));
7742
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743/*
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007744 * Call reg_getline() with the line numbers from the submatch. If a
7745 * substitute() was used the reg_maxline and other values have been
7746 * overwritten.
7747 */
7748 static char_u *
7749reg_getline_submatch(lnum)
7750 linenr_T lnum;
7751{
7752 char_u *s;
7753 linenr_T save_first = reg_firstlnum;
7754 linenr_T save_max = reg_maxline;
7755
7756 reg_firstlnum = submatch_firstlnum;
7757 reg_maxline = submatch_maxline;
7758
7759 s = reg_getline(lnum);
7760
7761 reg_firstlnum = save_first;
7762 reg_maxline = save_max;
7763 return s;
7764}
7765
7766/*
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00007767 * Used for the submatch() function: get the string from the n'th submatch in
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 * allocated memory.
7769 * Returns NULL when not in a ":s" command and for a non-existing submatch.
7770 */
7771 char_u *
7772reg_submatch(no)
7773 int no;
7774{
7775 char_u *retval = NULL;
7776 char_u *s;
7777 int len;
7778 int round;
7779 linenr_T lnum;
7780
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007781 if (!can_f_submatch || no < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 return NULL;
7783
7784 if (submatch_match == NULL)
7785 {
7786 /*
7787 * First round: compute the length and allocate memory.
7788 * Second round: copy the text.
7789 */
7790 for (round = 1; round <= 2; ++round)
7791 {
7792 lnum = submatch_mmatch->startpos[no].lnum;
7793 if (lnum < 0 || submatch_mmatch->endpos[no].lnum < 0)
7794 return NULL;
7795
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007796 s = reg_getline_submatch(lnum) + submatch_mmatch->startpos[no].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 if (s == NULL) /* anti-crash check, cannot happen? */
7798 break;
7799 if (submatch_mmatch->endpos[no].lnum == lnum)
7800 {
7801 /* Within one line: take form start to end col. */
7802 len = submatch_mmatch->endpos[no].col
7803 - submatch_mmatch->startpos[no].col;
7804 if (round == 2)
Bram Moolenaarbbebc852005-07-18 21:47:53 +00007805 vim_strncpy(retval, s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 ++len;
7807 }
7808 else
7809 {
7810 /* Multiple lines: take start line from start col, middle
7811 * lines completely and end line up to end col. */
7812 len = (int)STRLEN(s);
7813 if (round == 2)
7814 {
7815 STRCPY(retval, s);
7816 retval[len] = '\n';
7817 }
7818 ++len;
7819 ++lnum;
7820 while (lnum < submatch_mmatch->endpos[no].lnum)
7821 {
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007822 s = reg_getline_submatch(lnum++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 if (round == 2)
7824 STRCPY(retval + len, s);
7825 len += (int)STRLEN(s);
7826 if (round == 2)
7827 retval[len] = '\n';
7828 ++len;
7829 }
7830 if (round == 2)
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007831 STRNCPY(retval + len, reg_getline_submatch(lnum),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 submatch_mmatch->endpos[no].col);
7833 len += submatch_mmatch->endpos[no].col;
7834 if (round == 2)
7835 retval[len] = NUL;
7836 ++len;
7837 }
7838
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007839 if (retval == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 {
7841 retval = lalloc((long_u)len, TRUE);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007842 if (retval == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 return NULL;
7844 }
7845 }
7846 }
7847 else
7848 {
Bram Moolenaar7670fa02009-02-21 21:04:20 +00007849 s = submatch_match->startp[no];
7850 if (s == NULL || submatch_match->endp[no] == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 retval = NULL;
7852 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 retval = vim_strnsave(s, (int)(submatch_match->endp[no] - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 }
7855
7856 return retval;
7857}
7858#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007859
7860static regengine_T bt_regengine =
7861{
7862 bt_regcomp,
7863 bt_regexec,
7864#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
7865 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
7866 bt_regexec_nl,
7867#endif
7868 bt_regexec_multi
7869#ifdef DEBUG
7870 ,(char_u *)""
7871#endif
7872};
7873
7874
7875#include "regexp_nfa.c"
7876
7877static regengine_T nfa_regengine =
7878{
7879 nfa_regcomp,
7880 nfa_regexec,
7881#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
7882 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
7883 nfa_regexec_nl,
7884#endif
7885 nfa_regexec_multi
7886#ifdef DEBUG
7887 ,(char_u *)""
7888#endif
7889};
7890
7891/* Which regexp engine to use? Needed for vim_regcomp().
7892 * Must match with 'regexpengine'. */
7893static int regexp_engine = 0;
7894#define AUTOMATIC_ENGINE 0
7895#define BACKTRACKING_ENGINE 1
7896#define NFA_ENGINE 2
7897#ifdef DEBUG
7898static char_u regname[][30] = {
7899 "AUTOMATIC Regexp Engine",
Bram Moolenaar75eb1612013-05-29 18:45:11 +02007900 "BACKTRACKING Regexp Engine",
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007901 "NFA Regexp Engine"
7902 };
7903#endif
7904
7905/*
7906 * Compile a regular expression into internal code.
7907 * Returns the program in allocated memory. Returns NULL for an error.
7908 */
7909 regprog_T *
7910vim_regcomp(expr_arg, re_flags)
7911 char_u *expr_arg;
7912 int re_flags;
7913{
7914 regprog_T *prog = NULL;
7915 char_u *expr = expr_arg;
7916
7917 syntax_error = FALSE;
7918 regexp_engine = p_re;
7919
7920 /* Check for prefix "\%#=", that sets the regexp engine */
7921 if (STRNCMP(expr, "\\%#=", 4) == 0)
7922 {
7923 int newengine = expr[4] - '0';
7924
7925 if (newengine == AUTOMATIC_ENGINE
7926 || newengine == BACKTRACKING_ENGINE
7927 || newengine == NFA_ENGINE)
7928 {
7929 regexp_engine = expr[4] - '0';
7930 expr += 5;
7931#ifdef DEBUG
7932 EMSG3("New regexp mode selected (%d): %s", regexp_engine,
7933 regname[newengine]);
7934#endif
7935 }
7936 else
7937 {
7938 EMSG(_("E864: \\%#= can only be followed by 0, 1, or 2. The automatic engine will be used "));
7939 regexp_engine = AUTOMATIC_ENGINE;
7940 }
7941 }
7942#ifdef DEBUG
7943 bt_regengine.expr = expr;
7944 nfa_regengine.expr = expr;
7945#endif
7946
7947 /*
7948 * First try the NFA engine, unless backtracking was requested.
7949 */
7950 if (regexp_engine != BACKTRACKING_ENGINE)
7951 prog = nfa_regengine.regcomp(expr, re_flags);
7952 else
7953 prog = bt_regengine.regcomp(expr, re_flags);
7954
7955 if (prog == NULL) /* error compiling regexp with initial engine */
7956 {
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02007957#ifdef BT_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007958 if (regexp_engine != BACKTRACKING_ENGINE) /* debugging log for NFA */
7959 {
7960 FILE *f;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02007961 f = fopen(BT_REGEXP_DEBUG_LOG_NAME, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007962 if (f)
7963 {
7964 if (!syntax_error)
7965 fprintf(f, "NFA engine could not handle \"%s\"\n", expr);
7966 else
7967 fprintf(f, "Syntax error in \"%s\"\n", expr);
7968 fclose(f);
7969 }
7970 else
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02007971 EMSG2("(NFA) Could not open \"%s\" to write !!!",
7972 BT_REGEXP_DEBUG_LOG_NAME);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007973 /*
7974 if (syntax_error)
7975 EMSG("NFA Regexp: Syntax Error !");
7976 */
7977 }
7978#endif
7979 /*
7980 * If NFA engine failed, then revert to the backtracking engine.
7981 * Except when there was a syntax error, which was properly handled by
7982 * NFA engine.
7983 */
7984 if (regexp_engine == AUTOMATIC_ENGINE)
7985 if (!syntax_error)
7986 prog = bt_regengine.regcomp(expr, re_flags);
7987
7988 } /* endif prog==NULL */
7989
7990
7991 return prog;
7992}
7993
7994/*
7995 * Match a regexp against a string.
7996 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
7997 * Uses curbuf for line count and 'iskeyword'.
7998 *
7999 * Return TRUE if there is a match, FALSE if not.
8000 */
8001 int
8002vim_regexec(rmp, line, col)
8003 regmatch_T *rmp;
8004 char_u *line; /* string to match against */
8005 colnr_T col; /* column to start looking for match */
8006{
8007 return rmp->regprog->engine->regexec(rmp, line, col);
8008}
8009
8010#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
8011 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
8012/*
8013 * Like vim_regexec(), but consider a "\n" in "line" to be a line break.
8014 */
8015 int
8016vim_regexec_nl(rmp, line, col)
8017 regmatch_T *rmp;
8018 char_u *line;
8019 colnr_T col;
8020{
8021 return rmp->regprog->engine->regexec_nl(rmp, line, col);
8022}
8023#endif
8024
8025/*
8026 * Match a regexp against multiple lines.
8027 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
8028 * Uses curbuf for line count and 'iskeyword'.
8029 *
8030 * Return zero if there is no match. Return number of lines contained in the
8031 * match otherwise.
8032 */
8033 long
8034vim_regexec_multi(rmp, win, buf, lnum, col, tm)
8035 regmmatch_T *rmp;
8036 win_T *win; /* window in which to search or NULL */
8037 buf_T *buf; /* buffer in which to search */
8038 linenr_T lnum; /* nr of line to start looking for match */
8039 colnr_T col; /* column to start looking for match */
8040 proftime_T *tm; /* timeout limit or NULL */
8041{
8042 return rmp->regprog->engine->regexec_multi(rmp, win, buf, lnum, col, tm);
8043}