blob: 2804f8062cc5c1b0d3f98a4686ee27a7eff89937 [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 Moolenaar5de820b2013-06-02 15:01:57 +0200364static char_u e_z_not_allowed[] = N_("E66: \\z( not allowed here");
365static char_u e_z1_not_allowed[] = N_("E67: \\z1 et al. not allowed here");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200366
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367#define NOT_MULTI 0
368#define MULTI_ONE 1
369#define MULTI_MULT 2
370/*
371 * Return NOT_MULTI if c is not a "multi" operator.
372 * Return MULTI_ONE if c is a single "multi" operator.
373 * Return MULTI_MULT if c is a multi "multi" operator.
374 */
375 static int
376re_multi_type(c)
377 int c;
378{
379 if (c == Magic('@') || c == Magic('=') || c == Magic('?'))
380 return MULTI_ONE;
381 if (c == Magic('*') || c == Magic('+') || c == Magic('{'))
382 return MULTI_MULT;
383 return NOT_MULTI;
384}
385
386/*
387 * Flags to be passed up and down.
388 */
389#define HASWIDTH 0x1 /* Known never to match null string. */
390#define SIMPLE 0x2 /* Simple enough to be STAR/PLUS operand. */
391#define SPSTART 0x4 /* Starts with * or +. */
392#define HASNL 0x8 /* Contains some \n. */
393#define HASLOOKBH 0x10 /* Contains "\@<=" or "\@<!". */
394#define WORST 0 /* Worst case. */
395
396/*
397 * When regcode is set to this value, code is not emitted and size is computed
398 * instead.
399 */
400#define JUST_CALC_SIZE ((char_u *) -1)
401
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000402static char_u *reg_prev_sub = NULL;
403
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404/*
405 * REGEXP_INRANGE contains all characters which are always special in a []
406 * range after '\'.
407 * REGEXP_ABBR contains all characters which act as abbreviations after '\'.
408 * These are:
409 * \n - New line (NL).
410 * \r - Carriage Return (CR).
411 * \t - Tab (TAB).
412 * \e - Escape (ESC).
413 * \b - Backspace (Ctrl_H).
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000414 * \d - Character code in decimal, eg \d123
415 * \o - Character code in octal, eg \o80
416 * \x - Character code in hex, eg \x4a
417 * \u - Multibyte character code, eg \u20ac
418 * \U - Long multibyte character code, eg \U12345678
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 */
420static char_u REGEXP_INRANGE[] = "]^-n\\";
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000421static char_u REGEXP_ABBR[] = "nrtebdoxuU";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422
423static int backslash_trans __ARGS((int c));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000424static int get_char_class __ARGS((char_u **pp));
425static int get_equi_class __ARGS((char_u **pp));
426static void reg_equi_class __ARGS((int c));
427static int get_coll_element __ARGS((char_u **pp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428static char_u *skip_anyof __ARGS((char_u *p));
429static void init_class_tab __ARGS((void));
430
431/*
432 * Translate '\x' to its control character, except "\n", which is Magic.
433 */
434 static int
435backslash_trans(c)
436 int c;
437{
438 switch (c)
439 {
440 case 'r': return CAR;
441 case 't': return TAB;
442 case 'e': return ESC;
443 case 'b': return BS;
444 }
445 return c;
446}
447
448/*
Bram Moolenaardf177f62005-02-22 08:39:57 +0000449 * Check for a character class name "[:name:]". "pp" points to the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 * Returns one of the CLASS_ items. CLASS_NONE means that no item was
451 * recognized. Otherwise "pp" is advanced to after the item.
452 */
453 static int
Bram Moolenaardf177f62005-02-22 08:39:57 +0000454get_char_class(pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 char_u **pp;
456{
457 static const char *(class_names[]) =
458 {
459 "alnum:]",
460#define CLASS_ALNUM 0
461 "alpha:]",
462#define CLASS_ALPHA 1
463 "blank:]",
464#define CLASS_BLANK 2
465 "cntrl:]",
466#define CLASS_CNTRL 3
467 "digit:]",
468#define CLASS_DIGIT 4
469 "graph:]",
470#define CLASS_GRAPH 5
471 "lower:]",
472#define CLASS_LOWER 6
473 "print:]",
474#define CLASS_PRINT 7
475 "punct:]",
476#define CLASS_PUNCT 8
477 "space:]",
478#define CLASS_SPACE 9
479 "upper:]",
480#define CLASS_UPPER 10
481 "xdigit:]",
482#define CLASS_XDIGIT 11
483 "tab:]",
484#define CLASS_TAB 12
485 "return:]",
486#define CLASS_RETURN 13
487 "backspace:]",
488#define CLASS_BACKSPACE 14
489 "escape:]",
490#define CLASS_ESCAPE 15
491 };
492#define CLASS_NONE 99
493 int i;
494
495 if ((*pp)[1] == ':')
496 {
Bram Moolenaar78a15312009-05-15 19:33:18 +0000497 for (i = 0; i < (int)(sizeof(class_names) / sizeof(*class_names)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 if (STRNCMP(*pp + 2, class_names[i], STRLEN(class_names[i])) == 0)
499 {
500 *pp += STRLEN(class_names[i]) + 2;
501 return i;
502 }
503 }
504 return CLASS_NONE;
505}
506
507/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 * Specific version of character class functions.
509 * Using a table to keep this fast.
510 */
511static short class_tab[256];
512
513#define RI_DIGIT 0x01
514#define RI_HEX 0x02
515#define RI_OCTAL 0x04
516#define RI_WORD 0x08
517#define RI_HEAD 0x10
518#define RI_ALPHA 0x20
519#define RI_LOWER 0x40
520#define RI_UPPER 0x80
521#define RI_WHITE 0x100
522
523 static void
524init_class_tab()
525{
526 int i;
527 static int done = FALSE;
528
529 if (done)
530 return;
531
532 for (i = 0; i < 256; ++i)
533 {
534 if (i >= '0' && i <= '7')
535 class_tab[i] = RI_DIGIT + RI_HEX + RI_OCTAL + RI_WORD;
536 else if (i >= '8' && i <= '9')
537 class_tab[i] = RI_DIGIT + RI_HEX + RI_WORD;
538 else if (i >= 'a' && i <= 'f')
539 class_tab[i] = RI_HEX + RI_WORD + RI_HEAD + RI_ALPHA + RI_LOWER;
540#ifdef EBCDIC
541 else if ((i >= 'g' && i <= 'i') || (i >= 'j' && i <= 'r')
542 || (i >= 's' && i <= 'z'))
543#else
544 else if (i >= 'g' && i <= 'z')
545#endif
546 class_tab[i] = RI_WORD + RI_HEAD + RI_ALPHA + RI_LOWER;
547 else if (i >= 'A' && i <= 'F')
548 class_tab[i] = RI_HEX + RI_WORD + RI_HEAD + RI_ALPHA + RI_UPPER;
549#ifdef EBCDIC
550 else if ((i >= 'G' && i <= 'I') || ( i >= 'J' && i <= 'R')
551 || (i >= 'S' && i <= 'Z'))
552#else
553 else if (i >= 'G' && i <= 'Z')
554#endif
555 class_tab[i] = RI_WORD + RI_HEAD + RI_ALPHA + RI_UPPER;
556 else if (i == '_')
557 class_tab[i] = RI_WORD + RI_HEAD;
558 else
559 class_tab[i] = 0;
560 }
561 class_tab[' '] |= RI_WHITE;
562 class_tab['\t'] |= RI_WHITE;
563 done = TRUE;
564}
565
566#ifdef FEAT_MBYTE
567# define ri_digit(c) (c < 0x100 && (class_tab[c] & RI_DIGIT))
568# define ri_hex(c) (c < 0x100 && (class_tab[c] & RI_HEX))
569# define ri_octal(c) (c < 0x100 && (class_tab[c] & RI_OCTAL))
570# define ri_word(c) (c < 0x100 && (class_tab[c] & RI_WORD))
571# define ri_head(c) (c < 0x100 && (class_tab[c] & RI_HEAD))
572# define ri_alpha(c) (c < 0x100 && (class_tab[c] & RI_ALPHA))
573# define ri_lower(c) (c < 0x100 && (class_tab[c] & RI_LOWER))
574# define ri_upper(c) (c < 0x100 && (class_tab[c] & RI_UPPER))
575# define ri_white(c) (c < 0x100 && (class_tab[c] & RI_WHITE))
576#else
577# define ri_digit(c) (class_tab[c] & RI_DIGIT)
578# define ri_hex(c) (class_tab[c] & RI_HEX)
579# define ri_octal(c) (class_tab[c] & RI_OCTAL)
580# define ri_word(c) (class_tab[c] & RI_WORD)
581# define ri_head(c) (class_tab[c] & RI_HEAD)
582# define ri_alpha(c) (class_tab[c] & RI_ALPHA)
583# define ri_lower(c) (class_tab[c] & RI_LOWER)
584# define ri_upper(c) (class_tab[c] & RI_UPPER)
585# define ri_white(c) (class_tab[c] & RI_WHITE)
586#endif
587
588/* flags for regflags */
589#define RF_ICASE 1 /* ignore case */
590#define RF_NOICASE 2 /* don't ignore case */
591#define RF_HASNL 4 /* can match a NL */
592#define RF_ICOMBINE 8 /* ignore combining characters */
593#define RF_LOOKBH 16 /* uses "\@<=" or "\@<!" */
594
595/*
596 * Global work variables for vim_regcomp().
597 */
598
599static char_u *regparse; /* Input-scan pointer. */
600static int prevchr_len; /* byte length of previous char */
601static int num_complex_braces; /* Complex \{...} count */
602static int regnpar; /* () count. */
603#ifdef FEAT_SYN_HL
604static int regnzpar; /* \z() count. */
605static int re_has_z; /* \z item detected */
606#endif
607static char_u *regcode; /* Code-emit pointer, or JUST_CALC_SIZE */
608static long regsize; /* Code size. */
Bram Moolenaard3005802009-11-25 17:21:32 +0000609static int reg_toolong; /* TRUE when offset out of range */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610static char_u had_endbrace[NSUBEXP]; /* flags, TRUE if end of () found */
611static unsigned regflags; /* RF_ flags for prog */
612static long brace_min[10]; /* Minimums for complex brace repeats */
613static long brace_max[10]; /* Maximums for complex brace repeats */
614static int brace_count[10]; /* Current counts for complex brace repeats */
615#if defined(FEAT_SYN_HL) || defined(PROTO)
616static int had_eol; /* TRUE when EOL found by vim_regcomp() */
617#endif
618static int one_exactly = FALSE; /* only do one char for EXACTLY */
619
620static int reg_magic; /* magicness of the pattern: */
621#define MAGIC_NONE 1 /* "\V" very unmagic */
622#define MAGIC_OFF 2 /* "\M" or 'magic' off */
623#define MAGIC_ON 3 /* "\m" or 'magic' */
624#define MAGIC_ALL 4 /* "\v" very magic */
625
626static int reg_string; /* matching with a string instead of a buffer
627 line */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000628static int reg_strict; /* "[abc" is illegal */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629
630/*
631 * META contains all characters that may be magic, except '^' and '$'.
632 */
633
634#ifdef EBCDIC
635static char_u META[] = "%&()*+.123456789<=>?@ACDFHIKLMOPSUVWX[_acdfhiklmnopsuvwxz{|~";
636#else
637/* META[] is used often enough to justify turning it into a table. */
638static char_u META_flags[] = {
639 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
640 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
641/* % & ( ) * + . */
642 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0,
643/* 1 2 3 4 5 6 7 8 9 < = > ? */
644 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1,
645/* @ A C D F H I K L M O */
646 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1,
647/* P S U V W X Z [ _ */
648 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1,
649/* a c d f h i k l m n o */
650 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1,
651/* p s u v w x z { | ~ */
652 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1
653};
654#endif
655
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200656static int curchr; /* currently parsed character */
657/* Previous character. Note: prevchr is sometimes -1 when we are not at the
658 * start, eg in /[ ^I]^ the pattern was never found even if it existed,
659 * because ^ was taken to be magic -- webb */
660static int prevchr;
661static int prevprevchr; /* previous-previous character */
662static int nextchr; /* used for ungetchr() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663
664/* arguments for reg() */
665#define REG_NOPAREN 0 /* toplevel reg() */
666#define REG_PAREN 1 /* \(\) */
667#define REG_ZPAREN 2 /* \z(\) */
668#define REG_NPAREN 3 /* \%(\) */
669
Bram Moolenaar3737fc12013-06-01 14:42:56 +0200670typedef struct
671{
672 char_u *regparse;
673 int prevchr_len;
674 int curchr;
675 int prevchr;
676 int prevprevchr;
677 int nextchr;
678 int at_start;
679 int prev_at_start;
680 int regnpar;
681} parse_state_T;
682
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683/*
684 * Forward declarations for vim_regcomp()'s friends.
685 */
686static void initchr __ARGS((char_u *));
Bram Moolenaar3737fc12013-06-01 14:42:56 +0200687static void save_parse_state __ARGS((parse_state_T *ps));
688static void restore_parse_state __ARGS((parse_state_T *ps));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689static int getchr __ARGS((void));
690static void skipchr_keepstart __ARGS((void));
691static int peekchr __ARGS((void));
692static void skipchr __ARGS((void));
693static void ungetchr __ARGS((void));
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000694static int gethexchrs __ARGS((int maxinputlen));
695static int getoctchrs __ARGS((void));
696static int getdecchrs __ARGS((void));
697static int coll_get_char __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698static void regcomp_start __ARGS((char_u *expr, int flags));
699static char_u *reg __ARGS((int, int *));
700static char_u *regbranch __ARGS((int *flagp));
701static char_u *regconcat __ARGS((int *flagp));
702static char_u *regpiece __ARGS((int *));
703static char_u *regatom __ARGS((int *));
704static char_u *regnode __ARGS((int));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000705#ifdef FEAT_MBYTE
706static int use_multibytecode __ARGS((int c));
707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708static int prog_magic_wrong __ARGS((void));
709static char_u *regnext __ARGS((char_u *));
710static void regc __ARGS((int b));
711#ifdef FEAT_MBYTE
712static void regmbc __ARGS((int c));
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200713# define REGMBC(x) regmbc(x);
714# define CASEMBC(x) case x:
Bram Moolenaardf177f62005-02-22 08:39:57 +0000715#else
716# define regmbc(c) regc(c)
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200717# define REGMBC(x)
718# define CASEMBC(x)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719#endif
720static void reginsert __ARGS((int, char_u *));
Bram Moolenaar75eb1612013-05-29 18:45:11 +0200721static void reginsert_nr __ARGS((int op, long val, char_u *opnd));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722static void reginsert_limits __ARGS((int, long, long, char_u *));
723static char_u *re_put_long __ARGS((char_u *pr, long_u val));
724static int read_limits __ARGS((long *, long *));
725static void regtail __ARGS((char_u *, char_u *));
726static void regoptail __ARGS((char_u *, char_u *));
727
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200728static regengine_T bt_regengine;
729static regengine_T nfa_regengine;
730
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731/*
732 * Return TRUE if compiled regular expression "prog" can match a line break.
733 */
734 int
735re_multiline(prog)
736 regprog_T *prog;
737{
738 return (prog->regflags & RF_HASNL);
739}
740
741/*
742 * Return TRUE if compiled regular expression "prog" looks before the start
743 * position (pattern contains "\@<=" or "\@<!").
744 */
745 int
746re_lookbehind(prog)
747 regprog_T *prog;
748{
749 return (prog->regflags & RF_LOOKBH);
750}
751
752/*
Bram Moolenaardf177f62005-02-22 08:39:57 +0000753 * Check for an equivalence class name "[=a=]". "pp" points to the '['.
754 * Returns a character representing the class. Zero means that no item was
755 * recognized. Otherwise "pp" is advanced to after the item.
756 */
757 static int
758get_equi_class(pp)
759 char_u **pp;
760{
761 int c;
762 int l = 1;
763 char_u *p = *pp;
764
765 if (p[1] == '=')
766 {
767#ifdef FEAT_MBYTE
768 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000769 l = (*mb_ptr2len)(p + 2);
Bram Moolenaardf177f62005-02-22 08:39:57 +0000770#endif
771 if (p[l + 2] == '=' && p[l + 3] == ']')
772 {
773#ifdef FEAT_MBYTE
774 if (has_mbyte)
775 c = mb_ptr2char(p + 2);
776 else
777#endif
778 c = p[2];
779 *pp += l + 4;
780 return c;
781 }
782 }
783 return 0;
784}
785
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200786#ifdef EBCDIC
787/*
788 * Table for equivalence class "c". (IBM-1047)
789 */
790char *EQUIVAL_CLASS_C[16] = {
791 "A\x62\x63\x64\x65\x66\x67",
792 "C\x68",
793 "E\x71\x72\x73\x74",
794 "I\x75\x76\x77\x78",
795 "N\x69",
796 "O\xEB\xEC\xED\xEE\xEF",
797 "U\xFB\xFC\xFD\xFE",
798 "Y\xBA",
799 "a\x42\x43\x44\x45\x46\x47",
800 "c\x48",
801 "e\x51\x52\x53\x54",
802 "i\x55\x56\x57\x58",
803 "n\x49",
804 "o\xCB\xCC\xCD\xCE\xCF",
805 "u\xDB\xDC\xDD\xDE",
806 "y\x8D\xDF",
807};
808#endif
809
Bram Moolenaardf177f62005-02-22 08:39:57 +0000810/*
811 * Produce the bytes for equivalence class "c".
812 * Currently only handles latin1, latin9 and utf-8.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200813 * NOTE: When changing this function, also change nfa_emit_equi_class()
Bram Moolenaardf177f62005-02-22 08:39:57 +0000814 */
815 static void
816reg_equi_class(c)
817 int c;
818{
819#ifdef FEAT_MBYTE
820 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
Bram Moolenaar78622822005-08-23 21:00:13 +0000821 || STRCMP(p_enc, "iso-8859-15") == 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000822#endif
823 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200824#ifdef EBCDIC
825 int i;
826
827 /* This might be slower than switch/case below. */
828 for (i = 0; i < 16; i++)
829 {
830 if (vim_strchr(EQUIVAL_CLASS_C[i], c) != NULL)
831 {
832 char *p = EQUIVAL_CLASS_C[i];
833
834 while (*p != 0)
835 regmbc(*p++);
836 return;
837 }
838 }
839#else
Bram Moolenaardf177f62005-02-22 08:39:57 +0000840 switch (c)
841 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000842 case 'A': case '\300': case '\301': case '\302':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200843 CASEMBC(0x100) CASEMBC(0x102) CASEMBC(0x104) CASEMBC(0x1cd)
844 CASEMBC(0x1de) CASEMBC(0x1e0) CASEMBC(0x1ea2)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000845 case '\303': case '\304': case '\305':
846 regmbc('A'); regmbc('\300'); regmbc('\301');
847 regmbc('\302'); regmbc('\303'); regmbc('\304');
848 regmbc('\305');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200849 REGMBC(0x100) REGMBC(0x102) REGMBC(0x104)
850 REGMBC(0x1cd) REGMBC(0x1de) REGMBC(0x1e0)
851 REGMBC(0x1ea2)
852 return;
853 case 'B': CASEMBC(0x1e02) CASEMBC(0x1e06)
854 regmbc('B'); REGMBC(0x1e02) REGMBC(0x1e06)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000855 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000856 case 'C': case '\307':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200857 CASEMBC(0x106) CASEMBC(0x108) CASEMBC(0x10a) CASEMBC(0x10c)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000858 regmbc('C'); regmbc('\307');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200859 REGMBC(0x106) REGMBC(0x108) REGMBC(0x10a)
860 REGMBC(0x10c)
861 return;
862 case 'D': CASEMBC(0x10e) CASEMBC(0x110) CASEMBC(0x1e0a)
863 CASEMBC(0x1e0e) CASEMBC(0x1e10)
864 regmbc('D'); REGMBC(0x10e) REGMBC(0x110)
865 REGMBC(0x1e0a) REGMBC(0x1e0e) REGMBC(0x1e10)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000866 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000867 case 'E': case '\310': case '\311': case '\312': case '\313':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200868 CASEMBC(0x112) CASEMBC(0x114) CASEMBC(0x116) CASEMBC(0x118)
869 CASEMBC(0x11a) CASEMBC(0x1eba) CASEMBC(0x1ebc)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000870 regmbc('E'); regmbc('\310'); regmbc('\311');
871 regmbc('\312'); regmbc('\313');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200872 REGMBC(0x112) REGMBC(0x114) REGMBC(0x116)
873 REGMBC(0x118) REGMBC(0x11a) REGMBC(0x1eba)
874 REGMBC(0x1ebc)
875 return;
876 case 'F': CASEMBC(0x1e1e)
877 regmbc('F'); REGMBC(0x1e1e)
878 return;
879 case 'G': CASEMBC(0x11c) CASEMBC(0x11e) CASEMBC(0x120)
880 CASEMBC(0x122) CASEMBC(0x1e4) CASEMBC(0x1e6) CASEMBC(0x1f4)
881 CASEMBC(0x1e20)
882 regmbc('G'); REGMBC(0x11c) REGMBC(0x11e)
883 REGMBC(0x120) REGMBC(0x122) REGMBC(0x1e4)
884 REGMBC(0x1e6) REGMBC(0x1f4) REGMBC(0x1e20)
885 return;
886 case 'H': CASEMBC(0x124) CASEMBC(0x126) CASEMBC(0x1e22)
887 CASEMBC(0x1e26) CASEMBC(0x1e28)
888 regmbc('H'); REGMBC(0x124) REGMBC(0x126)
889 REGMBC(0x1e22) REGMBC(0x1e26) REGMBC(0x1e28)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000890 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000891 case 'I': case '\314': case '\315': case '\316': case '\317':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200892 CASEMBC(0x128) CASEMBC(0x12a) CASEMBC(0x12c) CASEMBC(0x12e)
893 CASEMBC(0x130) CASEMBC(0x1cf) CASEMBC(0x1ec8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000894 regmbc('I'); regmbc('\314'); regmbc('\315');
895 regmbc('\316'); regmbc('\317');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200896 REGMBC(0x128) REGMBC(0x12a) REGMBC(0x12c)
897 REGMBC(0x12e) REGMBC(0x130) REGMBC(0x1cf)
898 REGMBC(0x1ec8)
899 return;
900 case 'J': CASEMBC(0x134)
901 regmbc('J'); REGMBC(0x134)
902 return;
903 case 'K': CASEMBC(0x136) CASEMBC(0x1e8) CASEMBC(0x1e30)
904 CASEMBC(0x1e34)
905 regmbc('K'); REGMBC(0x136) REGMBC(0x1e8)
906 REGMBC(0x1e30) REGMBC(0x1e34)
907 return;
908 case 'L': CASEMBC(0x139) CASEMBC(0x13b) CASEMBC(0x13d)
909 CASEMBC(0x13f) CASEMBC(0x141) CASEMBC(0x1e3a)
910 regmbc('L'); REGMBC(0x139) REGMBC(0x13b)
911 REGMBC(0x13d) REGMBC(0x13f) REGMBC(0x141)
912 REGMBC(0x1e3a)
913 return;
914 case 'M': CASEMBC(0x1e3e) CASEMBC(0x1e40)
915 regmbc('M'); REGMBC(0x1e3e) REGMBC(0x1e40)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000916 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000917 case 'N': case '\321':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200918 CASEMBC(0x143) CASEMBC(0x145) CASEMBC(0x147) CASEMBC(0x1e44)
919 CASEMBC(0x1e48)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000920 regmbc('N'); regmbc('\321');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200921 REGMBC(0x143) REGMBC(0x145) REGMBC(0x147)
922 REGMBC(0x1e44) REGMBC(0x1e48)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000923 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000924 case 'O': case '\322': case '\323': case '\324': case '\325':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200925 case '\326': case '\330':
926 CASEMBC(0x14c) CASEMBC(0x14e) CASEMBC(0x150) CASEMBC(0x1a0)
927 CASEMBC(0x1d1) CASEMBC(0x1ea) CASEMBC(0x1ec) CASEMBC(0x1ece)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000928 regmbc('O'); regmbc('\322'); regmbc('\323');
929 regmbc('\324'); regmbc('\325'); regmbc('\326');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200930 regmbc('\330');
931 REGMBC(0x14c) REGMBC(0x14e) REGMBC(0x150)
932 REGMBC(0x1a0) REGMBC(0x1d1) REGMBC(0x1ea)
933 REGMBC(0x1ec) REGMBC(0x1ece)
934 return;
935 case 'P': case 0x1e54: case 0x1e56:
936 regmbc('P'); REGMBC(0x1e54) REGMBC(0x1e56)
937 return;
938 case 'R': CASEMBC(0x154) CASEMBC(0x156) CASEMBC(0x158)
939 CASEMBC(0x1e58) CASEMBC(0x1e5e)
940 regmbc('R'); REGMBC(0x154) REGMBC(0x156) REGMBC(0x158)
941 REGMBC(0x1e58) REGMBC(0x1e5e)
942 return;
943 case 'S': CASEMBC(0x15a) CASEMBC(0x15c) CASEMBC(0x15e)
944 CASEMBC(0x160) CASEMBC(0x1e60)
945 regmbc('S'); REGMBC(0x15a) REGMBC(0x15c)
946 REGMBC(0x15e) REGMBC(0x160) REGMBC(0x1e60)
947 return;
948 case 'T': CASEMBC(0x162) CASEMBC(0x164) CASEMBC(0x166)
949 CASEMBC(0x1e6a) CASEMBC(0x1e6e)
950 regmbc('T'); REGMBC(0x162) REGMBC(0x164)
951 REGMBC(0x166) REGMBC(0x1e6a) REGMBC(0x1e6e)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000952 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000953 case 'U': case '\331': case '\332': case '\333': case '\334':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200954 CASEMBC(0x168) CASEMBC(0x16a) CASEMBC(0x16c) CASEMBC(0x16e)
955 CASEMBC(0x170) CASEMBC(0x172) CASEMBC(0x1af) CASEMBC(0x1d3)
956 CASEMBC(0x1ee6)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000957 regmbc('U'); regmbc('\331'); regmbc('\332');
958 regmbc('\333'); regmbc('\334');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200959 REGMBC(0x168) REGMBC(0x16a) REGMBC(0x16c)
960 REGMBC(0x16e) REGMBC(0x170) REGMBC(0x172)
961 REGMBC(0x1af) REGMBC(0x1d3) REGMBC(0x1ee6)
962 return;
963 case 'V': CASEMBC(0x1e7c)
964 regmbc('V'); REGMBC(0x1e7c)
965 return;
966 case 'W': CASEMBC(0x174) CASEMBC(0x1e80) CASEMBC(0x1e82)
967 CASEMBC(0x1e84) CASEMBC(0x1e86)
968 regmbc('W'); REGMBC(0x174) REGMBC(0x1e80)
969 REGMBC(0x1e82) REGMBC(0x1e84) REGMBC(0x1e86)
970 return;
971 case 'X': CASEMBC(0x1e8a) CASEMBC(0x1e8c)
972 regmbc('X'); REGMBC(0x1e8a) REGMBC(0x1e8c)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000973 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000974 case 'Y': case '\335':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200975 CASEMBC(0x176) CASEMBC(0x178) CASEMBC(0x1e8e) CASEMBC(0x1ef2)
976 CASEMBC(0x1ef6) CASEMBC(0x1ef8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000977 regmbc('Y'); regmbc('\335');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200978 REGMBC(0x176) REGMBC(0x178) REGMBC(0x1e8e)
979 REGMBC(0x1ef2) REGMBC(0x1ef6) REGMBC(0x1ef8)
980 return;
981 case 'Z': CASEMBC(0x179) CASEMBC(0x17b) CASEMBC(0x17d)
982 CASEMBC(0x1b5) CASEMBC(0x1e90) CASEMBC(0x1e94)
983 regmbc('Z'); REGMBC(0x179) REGMBC(0x17b)
984 REGMBC(0x17d) REGMBC(0x1b5) REGMBC(0x1e90)
985 REGMBC(0x1e94)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000986 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000987 case 'a': case '\340': case '\341': case '\342':
988 case '\343': case '\344': case '\345':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200989 CASEMBC(0x101) CASEMBC(0x103) CASEMBC(0x105) CASEMBC(0x1ce)
990 CASEMBC(0x1df) CASEMBC(0x1e1) CASEMBC(0x1ea3)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000991 regmbc('a'); regmbc('\340'); regmbc('\341');
992 regmbc('\342'); regmbc('\343'); regmbc('\344');
993 regmbc('\345');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200994 REGMBC(0x101) REGMBC(0x103) REGMBC(0x105)
995 REGMBC(0x1ce) REGMBC(0x1df) REGMBC(0x1e1)
996 REGMBC(0x1ea3)
997 return;
998 case 'b': CASEMBC(0x1e03) CASEMBC(0x1e07)
999 regmbc('b'); REGMBC(0x1e03) REGMBC(0x1e07)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001000 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001001 case 'c': case '\347':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001002 CASEMBC(0x107) CASEMBC(0x109) CASEMBC(0x10b) CASEMBC(0x10d)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001003 regmbc('c'); regmbc('\347');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001004 REGMBC(0x107) REGMBC(0x109) REGMBC(0x10b)
1005 REGMBC(0x10d)
1006 return;
1007 case 'd': CASEMBC(0x10f) CASEMBC(0x111) CASEMBC(0x1d0b)
1008 CASEMBC(0x1e11)
1009 regmbc('d'); REGMBC(0x10f) REGMBC(0x111)
1010 REGMBC(0x1e0b) REGMBC(0x01e0f) REGMBC(0x1e11)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001011 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001012 case 'e': case '\350': case '\351': case '\352': case '\353':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001013 CASEMBC(0x113) CASEMBC(0x115) CASEMBC(0x117) CASEMBC(0x119)
1014 CASEMBC(0x11b) CASEMBC(0x1ebb) CASEMBC(0x1ebd)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001015 regmbc('e'); regmbc('\350'); regmbc('\351');
1016 regmbc('\352'); regmbc('\353');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001017 REGMBC(0x113) REGMBC(0x115) REGMBC(0x117)
1018 REGMBC(0x119) REGMBC(0x11b) REGMBC(0x1ebb)
1019 REGMBC(0x1ebd)
1020 return;
1021 case 'f': CASEMBC(0x1e1f)
1022 regmbc('f'); REGMBC(0x1e1f)
1023 return;
1024 case 'g': CASEMBC(0x11d) CASEMBC(0x11f) CASEMBC(0x121)
1025 CASEMBC(0x123) CASEMBC(0x1e5) CASEMBC(0x1e7) CASEMBC(0x1f5)
1026 CASEMBC(0x1e21)
1027 regmbc('g'); REGMBC(0x11d) REGMBC(0x11f)
1028 REGMBC(0x121) REGMBC(0x123) REGMBC(0x1e5)
1029 REGMBC(0x1e7) REGMBC(0x1f5) REGMBC(0x1e21)
1030 return;
1031 case 'h': CASEMBC(0x125) CASEMBC(0x127) CASEMBC(0x1e23)
1032 CASEMBC(0x1e27) CASEMBC(0x1e29) CASEMBC(0x1e96)
1033 regmbc('h'); REGMBC(0x125) REGMBC(0x127)
1034 REGMBC(0x1e23) REGMBC(0x1e27) REGMBC(0x1e29)
1035 REGMBC(0x1e96)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001036 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001037 case 'i': case '\354': case '\355': case '\356': case '\357':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001038 CASEMBC(0x129) CASEMBC(0x12b) CASEMBC(0x12d) CASEMBC(0x12f)
1039 CASEMBC(0x1d0) CASEMBC(0x1ec9)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001040 regmbc('i'); regmbc('\354'); regmbc('\355');
1041 regmbc('\356'); regmbc('\357');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001042 REGMBC(0x129) REGMBC(0x12b) REGMBC(0x12d)
1043 REGMBC(0x12f) REGMBC(0x1d0) REGMBC(0x1ec9)
1044 return;
1045 case 'j': CASEMBC(0x135) CASEMBC(0x1f0)
1046 regmbc('j'); REGMBC(0x135) REGMBC(0x1f0)
1047 return;
1048 case 'k': CASEMBC(0x137) CASEMBC(0x1e9) CASEMBC(0x1e31)
1049 CASEMBC(0x1e35)
1050 regmbc('k'); REGMBC(0x137) REGMBC(0x1e9)
1051 REGMBC(0x1e31) REGMBC(0x1e35)
1052 return;
1053 case 'l': CASEMBC(0x13a) CASEMBC(0x13c) CASEMBC(0x13e)
1054 CASEMBC(0x140) CASEMBC(0x142) CASEMBC(0x1e3b)
1055 regmbc('l'); REGMBC(0x13a) REGMBC(0x13c)
1056 REGMBC(0x13e) REGMBC(0x140) REGMBC(0x142)
1057 REGMBC(0x1e3b)
1058 return;
1059 case 'm': CASEMBC(0x1e3f) CASEMBC(0x1e41)
1060 regmbc('m'); REGMBC(0x1e3f) REGMBC(0x1e41)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001061 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001062 case 'n': case '\361':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001063 CASEMBC(0x144) CASEMBC(0x146) CASEMBC(0x148) CASEMBC(0x149)
1064 CASEMBC(0x1e45) CASEMBC(0x1e49)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001065 regmbc('n'); regmbc('\361');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001066 REGMBC(0x144) REGMBC(0x146) REGMBC(0x148)
1067 REGMBC(0x149) REGMBC(0x1e45) REGMBC(0x1e49)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001068 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001069 case 'o': case '\362': case '\363': case '\364': case '\365':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001070 case '\366': case '\370':
1071 CASEMBC(0x14d) CASEMBC(0x14f) CASEMBC(0x151) CASEMBC(0x1a1)
1072 CASEMBC(0x1d2) CASEMBC(0x1eb) CASEMBC(0x1ed) CASEMBC(0x1ecf)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001073 regmbc('o'); regmbc('\362'); regmbc('\363');
1074 regmbc('\364'); regmbc('\365'); regmbc('\366');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001075 regmbc('\370');
1076 REGMBC(0x14d) REGMBC(0x14f) REGMBC(0x151)
1077 REGMBC(0x1a1) REGMBC(0x1d2) REGMBC(0x1eb)
1078 REGMBC(0x1ed) REGMBC(0x1ecf)
1079 return;
1080 case 'p': CASEMBC(0x1e55) CASEMBC(0x1e57)
1081 regmbc('p'); REGMBC(0x1e55) REGMBC(0x1e57)
1082 return;
1083 case 'r': CASEMBC(0x155) CASEMBC(0x157) CASEMBC(0x159)
1084 CASEMBC(0x1e59) CASEMBC(0x1e5f)
1085 regmbc('r'); REGMBC(0x155) REGMBC(0x157) REGMBC(0x159)
1086 REGMBC(0x1e59) REGMBC(0x1e5f)
1087 return;
1088 case 's': CASEMBC(0x15b) CASEMBC(0x15d) CASEMBC(0x15f)
1089 CASEMBC(0x161) CASEMBC(0x1e61)
1090 regmbc('s'); REGMBC(0x15b) REGMBC(0x15d)
1091 REGMBC(0x15f) REGMBC(0x161) REGMBC(0x1e61)
1092 return;
1093 case 't': CASEMBC(0x163) CASEMBC(0x165) CASEMBC(0x167)
1094 CASEMBC(0x1e6b) CASEMBC(0x1e6f) CASEMBC(0x1e97)
1095 regmbc('t'); REGMBC(0x163) REGMBC(0x165) REGMBC(0x167)
1096 REGMBC(0x1e6b) REGMBC(0x1e6f) REGMBC(0x1e97)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001097 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001098 case 'u': case '\371': case '\372': case '\373': case '\374':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001099 CASEMBC(0x169) CASEMBC(0x16b) CASEMBC(0x16d) CASEMBC(0x16f)
1100 CASEMBC(0x171) CASEMBC(0x173) CASEMBC(0x1b0) CASEMBC(0x1d4)
1101 CASEMBC(0x1ee7)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001102 regmbc('u'); regmbc('\371'); regmbc('\372');
1103 regmbc('\373'); regmbc('\374');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001104 REGMBC(0x169) REGMBC(0x16b) REGMBC(0x16d)
1105 REGMBC(0x16f) REGMBC(0x171) REGMBC(0x173)
1106 REGMBC(0x1b0) REGMBC(0x1d4) REGMBC(0x1ee7)
1107 return;
1108 case 'v': CASEMBC(0x1e7d)
1109 regmbc('v'); REGMBC(0x1e7d)
1110 return;
1111 case 'w': CASEMBC(0x175) CASEMBC(0x1e81) CASEMBC(0x1e83)
1112 CASEMBC(0x1e85) CASEMBC(0x1e87) CASEMBC(0x1e98)
1113 regmbc('w'); REGMBC(0x175) REGMBC(0x1e81)
1114 REGMBC(0x1e83) REGMBC(0x1e85) REGMBC(0x1e87)
1115 REGMBC(0x1e98)
1116 return;
1117 case 'x': CASEMBC(0x1e8b) CASEMBC(0x1e8d)
1118 regmbc('x'); REGMBC(0x1e8b) REGMBC(0x1e8d)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001119 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001120 case 'y': case '\375': case '\377':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001121 CASEMBC(0x177) CASEMBC(0x1e8f) CASEMBC(0x1e99)
1122 CASEMBC(0x1ef3) CASEMBC(0x1ef7) CASEMBC(0x1ef9)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001123 regmbc('y'); regmbc('\375'); regmbc('\377');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001124 REGMBC(0x177) REGMBC(0x1e8f) REGMBC(0x1e99)
1125 REGMBC(0x1ef3) REGMBC(0x1ef7) REGMBC(0x1ef9)
1126 return;
1127 case 'z': CASEMBC(0x17a) CASEMBC(0x17c) CASEMBC(0x17e)
1128 CASEMBC(0x1b6) CASEMBC(0x1e91) CASEMBC(0x1e95)
1129 regmbc('z'); REGMBC(0x17a) REGMBC(0x17c)
1130 REGMBC(0x17e) REGMBC(0x1b6) REGMBC(0x1e91)
1131 REGMBC(0x1e95)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001132 return;
1133 }
Bram Moolenaar2c704a72010-06-03 21:17:25 +02001134#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00001135 }
1136 regmbc(c);
1137}
1138
1139/*
1140 * Check for a collating element "[.a.]". "pp" points to the '['.
1141 * Returns a character. Zero means that no item was recognized. Otherwise
1142 * "pp" is advanced to after the item.
1143 * Currently only single characters are recognized!
1144 */
1145 static int
1146get_coll_element(pp)
1147 char_u **pp;
1148{
1149 int c;
1150 int l = 1;
1151 char_u *p = *pp;
1152
1153 if (p[1] == '.')
1154 {
1155#ifdef FEAT_MBYTE
1156 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001157 l = (*mb_ptr2len)(p + 2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00001158#endif
1159 if (p[l + 2] == '.' && p[l + 3] == ']')
1160 {
1161#ifdef FEAT_MBYTE
1162 if (has_mbyte)
1163 c = mb_ptr2char(p + 2);
1164 else
1165#endif
1166 c = p[2];
1167 *pp += l + 4;
1168 return c;
1169 }
1170 }
1171 return 0;
1172}
1173
1174
1175/*
1176 * Skip over a "[]" range.
1177 * "p" must point to the character after the '['.
1178 * The returned pointer is on the matching ']', or the terminating NUL.
1179 */
1180 static char_u *
1181skip_anyof(p)
1182 char_u *p;
1183{
1184 int cpo_lit; /* 'cpoptions' contains 'l' flag */
1185 int cpo_bsl; /* 'cpoptions' contains '\' flag */
1186#ifdef FEAT_MBYTE
1187 int l;
1188#endif
1189
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00001190 cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL;
1191 cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001192
1193 if (*p == '^') /* Complement of range. */
1194 ++p;
1195 if (*p == ']' || *p == '-')
1196 ++p;
1197 while (*p != NUL && *p != ']')
1198 {
1199#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001200 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001201 p += l;
1202 else
1203#endif
1204 if (*p == '-')
1205 {
1206 ++p;
1207 if (*p != ']' && *p != NUL)
1208 mb_ptr_adv(p);
1209 }
1210 else if (*p == '\\'
1211 && !cpo_bsl
1212 && (vim_strchr(REGEXP_INRANGE, p[1]) != NULL
1213 || (!cpo_lit && vim_strchr(REGEXP_ABBR, p[1]) != NULL)))
1214 p += 2;
1215 else if (*p == '[')
1216 {
1217 if (get_char_class(&p) == CLASS_NONE
1218 && get_equi_class(&p) == 0
1219 && get_coll_element(&p) == 0)
1220 ++p; /* It was not a class name */
1221 }
1222 else
1223 ++p;
1224 }
1225
1226 return p;
1227}
1228
1229/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 * Skip past regular expression.
Bram Moolenaar748bf032005-02-02 23:04:36 +00001231 * Stop at end of "startp" or where "dirc" is found ('/', '?', etc).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 * Take care of characters with a backslash in front of it.
1233 * Skip strings inside [ and ].
1234 * When "newp" is not NULL and "dirc" is '?', make an allocated copy of the
1235 * expression and change "\?" to "?". If "*newp" is not NULL the expression
1236 * is changed in-place.
1237 */
1238 char_u *
1239skip_regexp(startp, dirc, magic, newp)
1240 char_u *startp;
1241 int dirc;
1242 int magic;
1243 char_u **newp;
1244{
1245 int mymagic;
1246 char_u *p = startp;
1247
1248 if (magic)
1249 mymagic = MAGIC_ON;
1250 else
1251 mymagic = MAGIC_OFF;
1252
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001253 for (; p[0] != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 {
1255 if (p[0] == dirc) /* found end of regexp */
1256 break;
1257 if ((p[0] == '[' && mymagic >= MAGIC_ON)
1258 || (p[0] == '\\' && p[1] == '[' && mymagic <= MAGIC_OFF))
1259 {
1260 p = skip_anyof(p + 1);
1261 if (p[0] == NUL)
1262 break;
1263 }
1264 else if (p[0] == '\\' && p[1] != NUL)
1265 {
1266 if (dirc == '?' && newp != NULL && p[1] == '?')
1267 {
1268 /* change "\?" to "?", make a copy first. */
1269 if (*newp == NULL)
1270 {
1271 *newp = vim_strsave(startp);
1272 if (*newp != NULL)
1273 p = *newp + (p - startp);
1274 }
1275 if (*newp != NULL)
Bram Moolenaar446cb832008-06-24 21:56:24 +00001276 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 else
1278 ++p;
1279 }
1280 else
1281 ++p; /* skip next character */
1282 if (*p == 'v')
1283 mymagic = MAGIC_ALL;
1284 else if (*p == 'V')
1285 mymagic = MAGIC_NONE;
1286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 }
1288 return p;
1289}
1290
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001291static regprog_T *bt_regcomp __ARGS((char_u *expr, int re_flags));
1292
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001294 * bt_regcomp() - compile a regular expression into internal code for the
1295 * traditional back track matcher.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001296 * Returns the program in allocated space. Returns NULL for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 *
1298 * We can't allocate space until we know how big the compiled form will be,
1299 * but we can't compile it (and thus know how big it is) until we've got a
1300 * place to put the code. So we cheat: we compile it twice, once with code
1301 * generation turned off and size counting turned on, and once "for real".
1302 * This also means that we don't allocate space until we are sure that the
1303 * thing really will compile successfully, and we never have to move the
1304 * code and thus invalidate pointers into it. (Note that it has to be in
1305 * one piece because vim_free() must be able to free it all.)
1306 *
1307 * Whether upper/lower case is to be ignored is decided when executing the
1308 * program, it does not matter here.
1309 *
1310 * Beware that the optimization-preparation code in here knows about some
1311 * of the structure of the compiled regexp.
1312 * "re_flags": RE_MAGIC and/or RE_STRING.
1313 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001314 static regprog_T *
1315bt_regcomp(expr, re_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 char_u *expr;
1317 int re_flags;
1318{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001319 bt_regprog_T *r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 char_u *scan;
1321 char_u *longest;
1322 int len;
1323 int flags;
1324
1325 if (expr == NULL)
1326 EMSG_RET_NULL(_(e_null));
1327
1328 init_class_tab();
1329
1330 /*
1331 * First pass: determine size, legality.
1332 */
1333 regcomp_start(expr, re_flags);
1334 regcode = JUST_CALC_SIZE;
1335 regc(REGMAGIC);
1336 if (reg(REG_NOPAREN, &flags) == NULL)
1337 return NULL;
1338
1339 /* Small enough for pointer-storage convention? */
1340#ifdef SMALL_MALLOC /* 16 bit storage allocation */
1341 if (regsize >= 65536L - 256L)
1342 EMSG_RET_NULL(_("E339: Pattern too long"));
1343#endif
1344
1345 /* Allocate space. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001346 r = (bt_regprog_T *)lalloc(sizeof(bt_regprog_T) + regsize, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 if (r == NULL)
1348 return NULL;
1349
1350 /*
1351 * Second pass: emit code.
1352 */
1353 regcomp_start(expr, re_flags);
1354 regcode = r->program;
1355 regc(REGMAGIC);
Bram Moolenaard3005802009-11-25 17:21:32 +00001356 if (reg(REG_NOPAREN, &flags) == NULL || reg_toolong)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 {
1358 vim_free(r);
Bram Moolenaard3005802009-11-25 17:21:32 +00001359 if (reg_toolong)
1360 EMSG_RET_NULL(_("E339: Pattern too long"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 return NULL;
1362 }
1363
1364 /* Dig out information for optimizations. */
1365 r->regstart = NUL; /* Worst-case defaults. */
1366 r->reganch = 0;
1367 r->regmust = NULL;
1368 r->regmlen = 0;
1369 r->regflags = regflags;
1370 if (flags & HASNL)
1371 r->regflags |= RF_HASNL;
1372 if (flags & HASLOOKBH)
1373 r->regflags |= RF_LOOKBH;
1374#ifdef FEAT_SYN_HL
1375 /* Remember whether this pattern has any \z specials in it. */
1376 r->reghasz = re_has_z;
1377#endif
1378 scan = r->program + 1; /* First BRANCH. */
1379 if (OP(regnext(scan)) == END) /* Only one top-level choice. */
1380 {
1381 scan = OPERAND(scan);
1382
1383 /* Starting-point info. */
1384 if (OP(scan) == BOL || OP(scan) == RE_BOF)
1385 {
1386 r->reganch++;
1387 scan = regnext(scan);
1388 }
1389
1390 if (OP(scan) == EXACTLY)
1391 {
1392#ifdef FEAT_MBYTE
1393 if (has_mbyte)
1394 r->regstart = (*mb_ptr2char)(OPERAND(scan));
1395 else
1396#endif
1397 r->regstart = *OPERAND(scan);
1398 }
1399 else if ((OP(scan) == BOW
1400 || OP(scan) == EOW
1401 || OP(scan) == NOTHING
1402 || OP(scan) == MOPEN + 0 || OP(scan) == NOPEN
1403 || OP(scan) == MCLOSE + 0 || OP(scan) == NCLOSE)
1404 && OP(regnext(scan)) == EXACTLY)
1405 {
1406#ifdef FEAT_MBYTE
1407 if (has_mbyte)
1408 r->regstart = (*mb_ptr2char)(OPERAND(regnext(scan)));
1409 else
1410#endif
1411 r->regstart = *OPERAND(regnext(scan));
1412 }
1413
1414 /*
1415 * If there's something expensive in the r.e., find the longest
1416 * literal string that must appear and make it the regmust. Resolve
1417 * ties in favor of later strings, since the regstart check works
1418 * with the beginning of the r.e. and avoiding duplication
1419 * strengthens checking. Not a strong reason, but sufficient in the
1420 * absence of others.
1421 */
1422 /*
1423 * When the r.e. starts with BOW, it is faster to look for a regmust
1424 * first. Used a lot for "#" and "*" commands. (Added by mool).
1425 */
1426 if ((flags & SPSTART || OP(scan) == BOW || OP(scan) == EOW)
1427 && !(flags & HASNL))
1428 {
1429 longest = NULL;
1430 len = 0;
1431 for (; scan != NULL; scan = regnext(scan))
1432 if (OP(scan) == EXACTLY && STRLEN(OPERAND(scan)) >= (size_t)len)
1433 {
1434 longest = OPERAND(scan);
1435 len = (int)STRLEN(OPERAND(scan));
1436 }
1437 r->regmust = longest;
1438 r->regmlen = len;
1439 }
1440 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001441#ifdef BT_REGEXP_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 regdump(expr, r);
1443#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001444 r->engine = &bt_regengine;
1445 return (regprog_T *)r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446}
1447
1448/*
1449 * Setup to parse the regexp. Used once to get the length and once to do it.
1450 */
1451 static void
1452regcomp_start(expr, re_flags)
1453 char_u *expr;
1454 int re_flags; /* see vim_regcomp() */
1455{
1456 initchr(expr);
1457 if (re_flags & RE_MAGIC)
1458 reg_magic = MAGIC_ON;
1459 else
1460 reg_magic = MAGIC_OFF;
1461 reg_string = (re_flags & RE_STRING);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001462 reg_strict = (re_flags & RE_STRICT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463
1464 num_complex_braces = 0;
1465 regnpar = 1;
1466 vim_memset(had_endbrace, 0, sizeof(had_endbrace));
1467#ifdef FEAT_SYN_HL
1468 regnzpar = 1;
1469 re_has_z = 0;
1470#endif
1471 regsize = 0L;
Bram Moolenaard3005802009-11-25 17:21:32 +00001472 reg_toolong = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 regflags = 0;
1474#if defined(FEAT_SYN_HL) || defined(PROTO)
1475 had_eol = FALSE;
1476#endif
1477}
1478
1479#if defined(FEAT_SYN_HL) || defined(PROTO)
1480/*
1481 * Check if during the previous call to vim_regcomp the EOL item "$" has been
1482 * found. This is messy, but it works fine.
1483 */
1484 int
1485vim_regcomp_had_eol()
1486{
1487 return had_eol;
1488}
1489#endif
1490
1491/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001492 * Parse regular expression, i.e. main body or parenthesized thing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 *
1494 * Caller must absorb opening parenthesis.
1495 *
1496 * Combining parenthesis handling with the base level of regular expression
1497 * is a trifle forced, but the need to tie the tails of the branches to what
1498 * follows makes it hard to avoid.
1499 */
1500 static char_u *
1501reg(paren, flagp)
1502 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1503 int *flagp;
1504{
1505 char_u *ret;
1506 char_u *br;
1507 char_u *ender;
1508 int parno = 0;
1509 int flags;
1510
1511 *flagp = HASWIDTH; /* Tentatively. */
1512
1513#ifdef FEAT_SYN_HL
1514 if (paren == REG_ZPAREN)
1515 {
1516 /* Make a ZOPEN node. */
1517 if (regnzpar >= NSUBEXP)
1518 EMSG_RET_NULL(_("E50: Too many \\z("));
1519 parno = regnzpar;
1520 regnzpar++;
1521 ret = regnode(ZOPEN + parno);
1522 }
1523 else
1524#endif
1525 if (paren == REG_PAREN)
1526 {
1527 /* Make a MOPEN node. */
1528 if (regnpar >= NSUBEXP)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001529 EMSG2_RET_NULL(_("E51: Too many %s("), reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 parno = regnpar;
1531 ++regnpar;
1532 ret = regnode(MOPEN + parno);
1533 }
1534 else if (paren == REG_NPAREN)
1535 {
1536 /* Make a NOPEN node. */
1537 ret = regnode(NOPEN);
1538 }
1539 else
1540 ret = NULL;
1541
1542 /* Pick up the branches, linking them together. */
1543 br = regbranch(&flags);
1544 if (br == NULL)
1545 return NULL;
1546 if (ret != NULL)
1547 regtail(ret, br); /* [MZ]OPEN -> first. */
1548 else
1549 ret = br;
1550 /* If one of the branches can be zero-width, the whole thing can.
1551 * If one of the branches has * at start or matches a line-break, the
1552 * whole thing can. */
1553 if (!(flags & HASWIDTH))
1554 *flagp &= ~HASWIDTH;
1555 *flagp |= flags & (SPSTART | HASNL | HASLOOKBH);
1556 while (peekchr() == Magic('|'))
1557 {
1558 skipchr();
1559 br = regbranch(&flags);
Bram Moolenaard3005802009-11-25 17:21:32 +00001560 if (br == NULL || reg_toolong)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 return NULL;
1562 regtail(ret, br); /* BRANCH -> BRANCH. */
1563 if (!(flags & HASWIDTH))
1564 *flagp &= ~HASWIDTH;
1565 *flagp |= flags & (SPSTART | HASNL | HASLOOKBH);
1566 }
1567
1568 /* Make a closing node, and hook it on the end. */
1569 ender = regnode(
1570#ifdef FEAT_SYN_HL
1571 paren == REG_ZPAREN ? ZCLOSE + parno :
1572#endif
1573 paren == REG_PAREN ? MCLOSE + parno :
1574 paren == REG_NPAREN ? NCLOSE : END);
1575 regtail(ret, ender);
1576
1577 /* Hook the tails of the branches to the closing node. */
1578 for (br = ret; br != NULL; br = regnext(br))
1579 regoptail(br, ender);
1580
1581 /* Check for proper termination. */
1582 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1583 {
1584#ifdef FEAT_SYN_HL
1585 if (paren == REG_ZPAREN)
Bram Moolenaar45eeb132005-06-06 21:59:07 +00001586 EMSG_RET_NULL(_("E52: Unmatched \\z("));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 else
1588#endif
1589 if (paren == REG_NPAREN)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001590 EMSG2_RET_NULL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001592 EMSG2_RET_NULL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 }
1594 else if (paren == REG_NOPAREN && peekchr() != NUL)
1595 {
1596 if (curchr == Magic(')'))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001597 EMSG2_RET_NULL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 else
Bram Moolenaar45eeb132005-06-06 21:59:07 +00001599 EMSG_RET_NULL(_(e_trailing)); /* "Can't happen". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 /* NOTREACHED */
1601 }
1602 /*
1603 * Here we set the flag allowing back references to this set of
1604 * parentheses.
1605 */
1606 if (paren == REG_PAREN)
1607 had_endbrace[parno] = TRUE; /* have seen the close paren */
1608 return ret;
1609}
1610
1611/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001612 * Parse one alternative of an | operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 * Implements the & operator.
1614 */
1615 static char_u *
1616regbranch(flagp)
1617 int *flagp;
1618{
1619 char_u *ret;
1620 char_u *chain = NULL;
1621 char_u *latest;
1622 int flags;
1623
1624 *flagp = WORST | HASNL; /* Tentatively. */
1625
1626 ret = regnode(BRANCH);
1627 for (;;)
1628 {
1629 latest = regconcat(&flags);
1630 if (latest == NULL)
1631 return NULL;
1632 /* If one of the branches has width, the whole thing has. If one of
1633 * the branches anchors at start-of-line, the whole thing does.
1634 * If one of the branches uses look-behind, the whole thing does. */
1635 *flagp |= flags & (HASWIDTH | SPSTART | HASLOOKBH);
1636 /* If one of the branches doesn't match a line-break, the whole thing
1637 * doesn't. */
1638 *flagp &= ~HASNL | (flags & HASNL);
1639 if (chain != NULL)
1640 regtail(chain, latest);
1641 if (peekchr() != Magic('&'))
1642 break;
1643 skipchr();
1644 regtail(latest, regnode(END)); /* operand ends */
Bram Moolenaard3005802009-11-25 17:21:32 +00001645 if (reg_toolong)
1646 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 reginsert(MATCH, latest);
1648 chain = latest;
1649 }
1650
1651 return ret;
1652}
1653
1654/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001655 * Parse one alternative of an | or & operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 * Implements the concatenation operator.
1657 */
1658 static char_u *
1659regconcat(flagp)
1660 int *flagp;
1661{
1662 char_u *first = NULL;
1663 char_u *chain = NULL;
1664 char_u *latest;
1665 int flags;
1666 int cont = TRUE;
1667
1668 *flagp = WORST; /* Tentatively. */
1669
1670 while (cont)
1671 {
1672 switch (peekchr())
1673 {
1674 case NUL:
1675 case Magic('|'):
1676 case Magic('&'):
1677 case Magic(')'):
1678 cont = FALSE;
1679 break;
1680 case Magic('Z'):
1681#ifdef FEAT_MBYTE
1682 regflags |= RF_ICOMBINE;
1683#endif
1684 skipchr_keepstart();
1685 break;
1686 case Magic('c'):
1687 regflags |= RF_ICASE;
1688 skipchr_keepstart();
1689 break;
1690 case Magic('C'):
1691 regflags |= RF_NOICASE;
1692 skipchr_keepstart();
1693 break;
1694 case Magic('v'):
1695 reg_magic = MAGIC_ALL;
1696 skipchr_keepstart();
1697 curchr = -1;
1698 break;
1699 case Magic('m'):
1700 reg_magic = MAGIC_ON;
1701 skipchr_keepstart();
1702 curchr = -1;
1703 break;
1704 case Magic('M'):
1705 reg_magic = MAGIC_OFF;
1706 skipchr_keepstart();
1707 curchr = -1;
1708 break;
1709 case Magic('V'):
1710 reg_magic = MAGIC_NONE;
1711 skipchr_keepstart();
1712 curchr = -1;
1713 break;
1714 default:
1715 latest = regpiece(&flags);
Bram Moolenaard3005802009-11-25 17:21:32 +00001716 if (latest == NULL || reg_toolong)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 return NULL;
1718 *flagp |= flags & (HASWIDTH | HASNL | HASLOOKBH);
1719 if (chain == NULL) /* First piece. */
1720 *flagp |= flags & SPSTART;
1721 else
1722 regtail(chain, latest);
1723 chain = latest;
1724 if (first == NULL)
1725 first = latest;
1726 break;
1727 }
1728 }
1729 if (first == NULL) /* Loop ran zero times. */
1730 first = regnode(NOTHING);
1731 return first;
1732}
1733
1734/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001735 * Parse something followed by possible [*+=].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 *
1737 * Note that the branching code sequences used for = and the general cases
1738 * of * and + are somewhat optimized: they use the same NOTHING node as
1739 * both the endmarker for their branch list and the body of the last branch.
1740 * It might seem that this node could be dispensed with entirely, but the
1741 * endmarker role is not redundant.
1742 */
1743 static char_u *
1744regpiece(flagp)
1745 int *flagp;
1746{
1747 char_u *ret;
1748 int op;
1749 char_u *next;
1750 int flags;
1751 long minval;
1752 long maxval;
1753
1754 ret = regatom(&flags);
1755 if (ret == NULL)
1756 return NULL;
1757
1758 op = peekchr();
1759 if (re_multi_type(op) == NOT_MULTI)
1760 {
1761 *flagp = flags;
1762 return ret;
1763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 /* default flags */
1765 *flagp = (WORST | SPSTART | (flags & (HASNL | HASLOOKBH)));
1766
1767 skipchr();
1768 switch (op)
1769 {
1770 case Magic('*'):
1771 if (flags & SIMPLE)
1772 reginsert(STAR, ret);
1773 else
1774 {
1775 /* Emit x* as (x&|), where & means "self". */
1776 reginsert(BRANCH, ret); /* Either x */
1777 regoptail(ret, regnode(BACK)); /* and loop */
1778 regoptail(ret, ret); /* back */
1779 regtail(ret, regnode(BRANCH)); /* or */
1780 regtail(ret, regnode(NOTHING)); /* null. */
1781 }
1782 break;
1783
1784 case Magic('+'):
1785 if (flags & SIMPLE)
1786 reginsert(PLUS, ret);
1787 else
1788 {
1789 /* Emit x+ as x(&|), where & means "self". */
1790 next = regnode(BRANCH); /* Either */
1791 regtail(ret, next);
Bram Moolenaar582fd852005-03-28 20:58:01 +00001792 regtail(regnode(BACK), ret); /* loop back */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 regtail(next, regnode(BRANCH)); /* or */
1794 regtail(ret, regnode(NOTHING)); /* null. */
1795 }
1796 *flagp = (WORST | HASWIDTH | (flags & (HASNL | HASLOOKBH)));
1797 break;
1798
1799 case Magic('@'):
1800 {
1801 int lop = END;
Bram Moolenaar75eb1612013-05-29 18:45:11 +02001802 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803
Bram Moolenaar75eb1612013-05-29 18:45:11 +02001804 nr = getdecchrs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 switch (no_Magic(getchr()))
1806 {
1807 case '=': lop = MATCH; break; /* \@= */
1808 case '!': lop = NOMATCH; break; /* \@! */
1809 case '>': lop = SUBPAT; break; /* \@> */
1810 case '<': switch (no_Magic(getchr()))
1811 {
1812 case '=': lop = BEHIND; break; /* \@<= */
1813 case '!': lop = NOBEHIND; break; /* \@<! */
1814 }
1815 }
1816 if (lop == END)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001817 EMSG2_RET_NULL(_("E59: invalid character after %s@"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 reg_magic == MAGIC_ALL);
1819 /* Look behind must match with behind_pos. */
1820 if (lop == BEHIND || lop == NOBEHIND)
1821 {
1822 regtail(ret, regnode(BHPOS));
1823 *flagp |= HASLOOKBH;
1824 }
1825 regtail(ret, regnode(END)); /* operand ends */
Bram Moolenaar75eb1612013-05-29 18:45:11 +02001826 if (lop == BEHIND || lop == NOBEHIND)
1827 {
1828 if (nr < 0)
1829 nr = 0; /* no limit is same as zero limit */
1830 reginsert_nr(lop, nr, ret);
1831 }
1832 else
1833 reginsert(lop, ret);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 break;
1835 }
1836
1837 case Magic('?'):
1838 case Magic('='):
1839 /* Emit x= as (x|) */
1840 reginsert(BRANCH, ret); /* Either x */
1841 regtail(ret, regnode(BRANCH)); /* or */
1842 next = regnode(NOTHING); /* null. */
1843 regtail(ret, next);
1844 regoptail(ret, next);
1845 break;
1846
1847 case Magic('{'):
1848 if (!read_limits(&minval, &maxval))
1849 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 if (flags & SIMPLE)
1851 {
1852 reginsert(BRACE_SIMPLE, ret);
1853 reginsert_limits(BRACE_LIMITS, minval, maxval, ret);
1854 }
1855 else
1856 {
1857 if (num_complex_braces >= 10)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001858 EMSG2_RET_NULL(_("E60: Too many complex %s{...}s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 reg_magic == MAGIC_ALL);
1860 reginsert(BRACE_COMPLEX + num_complex_braces, ret);
1861 regoptail(ret, regnode(BACK));
1862 regoptail(ret, ret);
1863 reginsert_limits(BRACE_LIMITS, minval, maxval, ret);
1864 ++num_complex_braces;
1865 }
1866 if (minval > 0 && maxval > 0)
1867 *flagp = (HASWIDTH | (flags & (HASNL | HASLOOKBH)));
1868 break;
1869 }
1870 if (re_multi_type(peekchr()) != NOT_MULTI)
1871 {
1872 /* Can't have a multi follow a multi. */
1873 if (peekchr() == Magic('*'))
1874 sprintf((char *)IObuff, _("E61: Nested %s*"),
1875 reg_magic >= MAGIC_ON ? "" : "\\");
1876 else
1877 sprintf((char *)IObuff, _("E62: Nested %s%c"),
1878 reg_magic == MAGIC_ALL ? "" : "\\", no_Magic(peekchr()));
1879 EMSG_RET_NULL(IObuff);
1880 }
1881
1882 return ret;
1883}
1884
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001885/* When making changes to classchars also change nfa_classcodes. */
1886static char_u *classchars = (char_u *)".iIkKfFpPsSdDxXoOwWhHaAlLuU";
1887static int classcodes[] = {
1888 ANY, IDENT, SIDENT, KWORD, SKWORD,
1889 FNAME, SFNAME, PRINT, SPRINT,
1890 WHITE, NWHITE, DIGIT, NDIGIT,
1891 HEX, NHEX, OCTAL, NOCTAL,
1892 WORD, NWORD, HEAD, NHEAD,
1893 ALPHA, NALPHA, LOWER, NLOWER,
1894 UPPER, NUPPER
1895};
1896
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001898 * Parse the lowest level.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 *
1900 * Optimization: gobbles an entire sequence of ordinary characters so that
1901 * it can turn them into a single node, which is smaller to store and
1902 * faster to run. Don't do this when one_exactly is set.
1903 */
1904 static char_u *
1905regatom(flagp)
1906 int *flagp;
1907{
1908 char_u *ret;
1909 int flags;
1910 int cpo_lit; /* 'cpoptions' contains 'l' flag */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001911 int cpo_bsl; /* 'cpoptions' contains '\' flag */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 char_u *p;
1914 int extra = 0;
1915
1916 *flagp = WORST; /* Tentatively. */
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00001917 cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL;
1918 cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919
1920 c = getchr();
1921 switch (c)
1922 {
1923 case Magic('^'):
1924 ret = regnode(BOL);
1925 break;
1926
1927 case Magic('$'):
1928 ret = regnode(EOL);
1929#if defined(FEAT_SYN_HL) || defined(PROTO)
1930 had_eol = TRUE;
1931#endif
1932 break;
1933
1934 case Magic('<'):
1935 ret = regnode(BOW);
1936 break;
1937
1938 case Magic('>'):
1939 ret = regnode(EOW);
1940 break;
1941
1942 case Magic('_'):
1943 c = no_Magic(getchr());
1944 if (c == '^') /* "\_^" is start-of-line */
1945 {
1946 ret = regnode(BOL);
1947 break;
1948 }
1949 if (c == '$') /* "\_$" is end-of-line */
1950 {
1951 ret = regnode(EOL);
1952#if defined(FEAT_SYN_HL) || defined(PROTO)
1953 had_eol = TRUE;
1954#endif
1955 break;
1956 }
1957
1958 extra = ADD_NL;
1959 *flagp |= HASNL;
1960
1961 /* "\_[" is character range plus newline */
1962 if (c == '[')
1963 goto collection;
1964
1965 /* "\_x" is character class plus newline */
1966 /*FALLTHROUGH*/
1967
1968 /*
1969 * Character classes.
1970 */
1971 case Magic('.'):
1972 case Magic('i'):
1973 case Magic('I'):
1974 case Magic('k'):
1975 case Magic('K'):
1976 case Magic('f'):
1977 case Magic('F'):
1978 case Magic('p'):
1979 case Magic('P'):
1980 case Magic('s'):
1981 case Magic('S'):
1982 case Magic('d'):
1983 case Magic('D'):
1984 case Magic('x'):
1985 case Magic('X'):
1986 case Magic('o'):
1987 case Magic('O'):
1988 case Magic('w'):
1989 case Magic('W'):
1990 case Magic('h'):
1991 case Magic('H'):
1992 case Magic('a'):
1993 case Magic('A'):
1994 case Magic('l'):
1995 case Magic('L'):
1996 case Magic('u'):
1997 case Magic('U'):
1998 p = vim_strchr(classchars, no_Magic(c));
1999 if (p == NULL)
2000 EMSG_RET_NULL(_("E63: invalid use of \\_"));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002001#ifdef FEAT_MBYTE
2002 /* When '.' is followed by a composing char ignore the dot, so that
2003 * the composing char is matched here. */
2004 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
2005 {
2006 c = getchr();
2007 goto do_multibyte;
2008 }
2009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 ret = regnode(classcodes[p - classchars] + extra);
2011 *flagp |= HASWIDTH | SIMPLE;
2012 break;
2013
2014 case Magic('n'):
2015 if (reg_string)
2016 {
2017 /* In a string "\n" matches a newline character. */
2018 ret = regnode(EXACTLY);
2019 regc(NL);
2020 regc(NUL);
2021 *flagp |= HASWIDTH | SIMPLE;
2022 }
2023 else
2024 {
2025 /* In buffer text "\n" matches the end of a line. */
2026 ret = regnode(NEWL);
2027 *flagp |= HASWIDTH | HASNL;
2028 }
2029 break;
2030
2031 case Magic('('):
2032 if (one_exactly)
2033 EMSG_ONE_RET_NULL;
2034 ret = reg(REG_PAREN, &flags);
2035 if (ret == NULL)
2036 return NULL;
2037 *flagp |= flags & (HASWIDTH | SPSTART | HASNL | HASLOOKBH);
2038 break;
2039
2040 case NUL:
2041 case Magic('|'):
2042 case Magic('&'):
2043 case Magic(')'):
Bram Moolenaard4210772008-01-02 14:35:30 +00002044 if (one_exactly)
2045 EMSG_ONE_RET_NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 EMSG_RET_NULL(_(e_internal)); /* Supposed to be caught earlier. */
2047 /* NOTREACHED */
2048
2049 case Magic('='):
2050 case Magic('?'):
2051 case Magic('+'):
2052 case Magic('@'):
2053 case Magic('{'):
2054 case Magic('*'):
2055 c = no_Magic(c);
2056 sprintf((char *)IObuff, _("E64: %s%c follows nothing"),
2057 (c == '*' ? reg_magic >= MAGIC_ON : reg_magic == MAGIC_ALL)
2058 ? "" : "\\", c);
2059 EMSG_RET_NULL(IObuff);
2060 /* NOTREACHED */
2061
2062 case Magic('~'): /* previous substitute pattern */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002063 if (reg_prev_sub != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 {
2065 char_u *lp;
2066
2067 ret = regnode(EXACTLY);
2068 lp = reg_prev_sub;
2069 while (*lp != NUL)
2070 regc(*lp++);
2071 regc(NUL);
2072 if (*reg_prev_sub != NUL)
2073 {
2074 *flagp |= HASWIDTH;
2075 if ((lp - reg_prev_sub) == 1)
2076 *flagp |= SIMPLE;
2077 }
2078 }
2079 else
2080 EMSG_RET_NULL(_(e_nopresub));
2081 break;
2082
2083 case Magic('1'):
2084 case Magic('2'):
2085 case Magic('3'):
2086 case Magic('4'):
2087 case Magic('5'):
2088 case Magic('6'):
2089 case Magic('7'):
2090 case Magic('8'):
2091 case Magic('9'):
2092 {
2093 int refnum;
2094
2095 refnum = c - Magic('0');
2096 /*
2097 * Check if the back reference is legal. We must have seen the
2098 * close brace.
2099 * TODO: Should also check that we don't refer to something
2100 * that is repeated (+*=): what instance of the repetition
2101 * should we match?
2102 */
2103 if (!had_endbrace[refnum])
2104 {
2105 /* Trick: check if "@<=" or "@<!" follows, in which case
2106 * the \1 can appear before the referenced match. */
2107 for (p = regparse; *p != NUL; ++p)
2108 if (p[0] == '@' && p[1] == '<'
2109 && (p[2] == '!' || p[2] == '='))
2110 break;
2111 if (*p == NUL)
2112 EMSG_RET_NULL(_("E65: Illegal back reference"));
2113 }
2114 ret = regnode(BACKREF + refnum);
2115 }
2116 break;
2117
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 case Magic('z'):
2119 {
2120 c = no_Magic(getchr());
2121 switch (c)
2122 {
Bram Moolenaarc4956c82006-03-12 21:58:43 +00002123#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 case '(': if (reg_do_extmatch != REX_SET)
Bram Moolenaar5de820b2013-06-02 15:01:57 +02002125 EMSG_RET_NULL(_(e_z_not_allowed));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 if (one_exactly)
2127 EMSG_ONE_RET_NULL;
2128 ret = reg(REG_ZPAREN, &flags);
2129 if (ret == NULL)
2130 return NULL;
2131 *flagp |= flags & (HASWIDTH|SPSTART|HASNL|HASLOOKBH);
2132 re_has_z = REX_SET;
2133 break;
2134
2135 case '1':
2136 case '2':
2137 case '3':
2138 case '4':
2139 case '5':
2140 case '6':
2141 case '7':
2142 case '8':
2143 case '9': if (reg_do_extmatch != REX_USE)
Bram Moolenaar5de820b2013-06-02 15:01:57 +02002144 EMSG_RET_NULL(_(e_z1_not_allowed));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 ret = regnode(ZREF + c - '0');
2146 re_has_z = REX_USE;
2147 break;
Bram Moolenaarc4956c82006-03-12 21:58:43 +00002148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149
2150 case 's': ret = regnode(MOPEN + 0);
2151 break;
2152
2153 case 'e': ret = regnode(MCLOSE + 0);
2154 break;
2155
2156 default: EMSG_RET_NULL(_("E68: Invalid character after \\z"));
2157 }
2158 }
2159 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160
2161 case Magic('%'):
2162 {
2163 c = no_Magic(getchr());
2164 switch (c)
2165 {
2166 /* () without a back reference */
2167 case '(':
2168 if (one_exactly)
2169 EMSG_ONE_RET_NULL;
2170 ret = reg(REG_NPAREN, &flags);
2171 if (ret == NULL)
2172 return NULL;
2173 *flagp |= flags & (HASWIDTH | SPSTART | HASNL | HASLOOKBH);
2174 break;
2175
2176 /* Catch \%^ and \%$ regardless of where they appear in the
2177 * pattern -- regardless of whether or not it makes sense. */
2178 case '^':
2179 ret = regnode(RE_BOF);
2180 break;
2181
2182 case '$':
2183 ret = regnode(RE_EOF);
2184 break;
2185
2186 case '#':
2187 ret = regnode(CURSOR);
2188 break;
2189
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00002190 case 'V':
2191 ret = regnode(RE_VISUAL);
2192 break;
2193
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 /* \%[abc]: Emit as a list of branches, all ending at the last
2195 * branch which matches nothing. */
2196 case '[':
2197 if (one_exactly) /* doesn't nest */
2198 EMSG_ONE_RET_NULL;
2199 {
2200 char_u *lastbranch;
2201 char_u *lastnode = NULL;
2202 char_u *br;
2203
2204 ret = NULL;
2205 while ((c = getchr()) != ']')
2206 {
2207 if (c == NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002208 EMSG2_RET_NULL(_("E69: Missing ] after %s%%["),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 reg_magic == MAGIC_ALL);
2210 br = regnode(BRANCH);
2211 if (ret == NULL)
2212 ret = br;
2213 else
2214 regtail(lastnode, br);
2215
2216 ungetchr();
2217 one_exactly = TRUE;
2218 lastnode = regatom(flagp);
2219 one_exactly = FALSE;
2220 if (lastnode == NULL)
2221 return NULL;
2222 }
2223 if (ret == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002224 EMSG2_RET_NULL(_("E70: Empty %s%%[]"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 reg_magic == MAGIC_ALL);
2226 lastbranch = regnode(BRANCH);
2227 br = regnode(NOTHING);
2228 if (ret != JUST_CALC_SIZE)
2229 {
2230 regtail(lastnode, br);
2231 regtail(lastbranch, br);
2232 /* connect all branches to the NOTHING
2233 * branch at the end */
2234 for (br = ret; br != lastnode; )
2235 {
2236 if (OP(br) == BRANCH)
2237 {
2238 regtail(br, lastbranch);
2239 br = OPERAND(br);
2240 }
2241 else
2242 br = regnext(br);
2243 }
2244 }
Bram Moolenaara6404a42008-08-08 11:45:39 +00002245 *flagp &= ~(HASWIDTH | SIMPLE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 break;
2247 }
2248
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002249 case 'd': /* %d123 decimal */
2250 case 'o': /* %o123 octal */
2251 case 'x': /* %xab hex 2 */
2252 case 'u': /* %uabcd hex 4 */
2253 case 'U': /* %U1234abcd hex 8 */
2254 {
2255 int i;
2256
2257 switch (c)
2258 {
2259 case 'd': i = getdecchrs(); break;
2260 case 'o': i = getoctchrs(); break;
2261 case 'x': i = gethexchrs(2); break;
2262 case 'u': i = gethexchrs(4); break;
2263 case 'U': i = gethexchrs(8); break;
2264 default: i = -1; break;
2265 }
2266
2267 if (i < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002268 EMSG2_RET_NULL(
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002269 _("E678: Invalid character after %s%%[dxouU]"),
2270 reg_magic == MAGIC_ALL);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002271#ifdef FEAT_MBYTE
2272 if (use_multibytecode(i))
2273 ret = regnode(MULTIBYTECODE);
2274 else
2275#endif
2276 ret = regnode(EXACTLY);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002277 if (i == 0)
2278 regc(0x0a);
2279 else
2280#ifdef FEAT_MBYTE
2281 regmbc(i);
2282#else
2283 regc(i);
2284#endif
2285 regc(NUL);
2286 *flagp |= HASWIDTH;
2287 break;
2288 }
2289
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 default:
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00002291 if (VIM_ISDIGIT(c) || c == '<' || c == '>'
2292 || c == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 {
2294 long_u n = 0;
2295 int cmp;
2296
2297 cmp = c;
2298 if (cmp == '<' || cmp == '>')
2299 c = getchr();
2300 while (VIM_ISDIGIT(c))
2301 {
2302 n = n * 10 + (c - '0');
2303 c = getchr();
2304 }
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00002305 if (c == '\'' && n == 0)
2306 {
2307 /* "\%'m", "\%<'m" and "\%>'m": Mark */
2308 c = getchr();
2309 ret = regnode(RE_MARK);
2310 if (ret == JUST_CALC_SIZE)
2311 regsize += 2;
2312 else
2313 {
2314 *regcode++ = c;
2315 *regcode++ = cmp;
2316 }
2317 break;
2318 }
2319 else if (c == 'l' || c == 'c' || c == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 {
2321 if (c == 'l')
2322 ret = regnode(RE_LNUM);
2323 else if (c == 'c')
2324 ret = regnode(RE_COL);
2325 else
2326 ret = regnode(RE_VCOL);
2327 if (ret == JUST_CALC_SIZE)
2328 regsize += 5;
2329 else
2330 {
2331 /* put the number and the optional
2332 * comparator after the opcode */
2333 regcode = re_put_long(regcode, n);
2334 *regcode++ = cmp;
2335 }
2336 break;
2337 }
2338 }
2339
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002340 EMSG2_RET_NULL(_("E71: Invalid character after %s%%"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 reg_magic == MAGIC_ALL);
2342 }
2343 }
2344 break;
2345
2346 case Magic('['):
2347collection:
2348 {
2349 char_u *lp;
2350
2351 /*
2352 * If there is no matching ']', we assume the '[' is a normal
2353 * character. This makes 'incsearch' and ":help [" work.
2354 */
2355 lp = skip_anyof(regparse);
2356 if (*lp == ']') /* there is a matching ']' */
2357 {
2358 int startc = -1; /* > 0 when next '-' is a range */
2359 int endc;
2360
2361 /*
2362 * In a character class, different parsing rules apply.
2363 * Not even \ is special anymore, nothing is.
2364 */
2365 if (*regparse == '^') /* Complement of range. */
2366 {
2367 ret = regnode(ANYBUT + extra);
2368 regparse++;
2369 }
2370 else
2371 ret = regnode(ANYOF + extra);
2372
2373 /* At the start ']' and '-' mean the literal character. */
2374 if (*regparse == ']' || *regparse == '-')
Bram Moolenaardf177f62005-02-22 08:39:57 +00002375 {
2376 startc = *regparse;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 regc(*regparse++);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379
2380 while (*regparse != NUL && *regparse != ']')
2381 {
2382 if (*regparse == '-')
2383 {
2384 ++regparse;
2385 /* The '-' is not used for a range at the end and
2386 * after or before a '\n'. */
2387 if (*regparse == ']' || *regparse == NUL
2388 || startc == -1
2389 || (regparse[0] == '\\' && regparse[1] == 'n'))
2390 {
2391 regc('-');
2392 startc = '-'; /* [--x] is a range */
2393 }
2394 else
2395 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00002396 /* Also accept "a-[.z.]" */
2397 endc = 0;
2398 if (*regparse == '[')
2399 endc = get_coll_element(&regparse);
2400 if (endc == 0)
2401 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402#ifdef FEAT_MBYTE
Bram Moolenaardf177f62005-02-22 08:39:57 +00002403 if (has_mbyte)
2404 endc = mb_ptr2char_adv(&regparse);
2405 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00002407 endc = *regparse++;
2408 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002409
2410 /* Handle \o40, \x20 and \u20AC style sequences */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002411 if (endc == '\\' && !cpo_lit && !cpo_bsl)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002412 endc = coll_get_char();
2413
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 if (startc > endc)
2415 EMSG_RET_NULL(_(e_invrange));
2416#ifdef FEAT_MBYTE
2417 if (has_mbyte && ((*mb_char2len)(startc) > 1
2418 || (*mb_char2len)(endc) > 1))
2419 {
2420 /* Limit to a range of 256 chars */
2421 if (endc > startc + 256)
2422 EMSG_RET_NULL(_(e_invrange));
2423 while (++startc <= endc)
2424 regmbc(startc);
2425 }
2426 else
2427#endif
2428 {
2429#ifdef EBCDIC
2430 int alpha_only = FALSE;
2431
2432 /* for alphabetical range skip the gaps
2433 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
2434 if (isalpha(startc) && isalpha(endc))
2435 alpha_only = TRUE;
2436#endif
2437 while (++startc <= endc)
2438#ifdef EBCDIC
2439 if (!alpha_only || isalpha(startc))
2440#endif
2441 regc(startc);
2442 }
2443 startc = -1;
2444 }
2445 }
2446 /*
2447 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
2448 * accepts "\t", "\e", etc., but only when the 'l' flag in
2449 * 'cpoptions' is not included.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002450 * Posix doesn't recognize backslash at all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 */
2452 else if (*regparse == '\\'
Bram Moolenaardf177f62005-02-22 08:39:57 +00002453 && !cpo_bsl
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
2455 || (!cpo_lit
2456 && vim_strchr(REGEXP_ABBR,
2457 regparse[1]) != NULL)))
2458 {
2459 regparse++;
2460 if (*regparse == 'n')
2461 {
2462 /* '\n' in range: also match NL */
2463 if (ret != JUST_CALC_SIZE)
2464 {
Bram Moolenaare337e5f2013-01-30 18:21:51 +01002465 /* Using \n inside [^] does not change what
2466 * matches. "[^\n]" is the same as ".". */
2467 if (*ret == ANYOF)
2468 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 *ret = ANYOF + ADD_NL;
Bram Moolenaare337e5f2013-01-30 18:21:51 +01002470 *flagp |= HASNL;
2471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 /* else: must have had a \n already */
2473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 regparse++;
2475 startc = -1;
2476 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002477 else if (*regparse == 'd'
2478 || *regparse == 'o'
2479 || *regparse == 'x'
2480 || *regparse == 'u'
2481 || *regparse == 'U')
2482 {
2483 startc = coll_get_char();
2484 if (startc == 0)
2485 regc(0x0a);
2486 else
2487#ifdef FEAT_MBYTE
2488 regmbc(startc);
2489#else
2490 regc(startc);
2491#endif
2492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 else
2494 {
2495 startc = backslash_trans(*regparse++);
2496 regc(startc);
2497 }
2498 }
2499 else if (*regparse == '[')
2500 {
2501 int c_class;
2502 int cu;
2503
Bram Moolenaardf177f62005-02-22 08:39:57 +00002504 c_class = get_char_class(&regparse);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 startc = -1;
2506 /* Characters assumed to be 8 bits! */
2507 switch (c_class)
2508 {
2509 case CLASS_NONE:
Bram Moolenaardf177f62005-02-22 08:39:57 +00002510 c_class = get_equi_class(&regparse);
2511 if (c_class != 0)
2512 {
2513 /* produce equivalence class */
2514 reg_equi_class(c_class);
2515 }
2516 else if ((c_class =
2517 get_coll_element(&regparse)) != 0)
2518 {
2519 /* produce a collating element */
2520 regmbc(c_class);
2521 }
2522 else
2523 {
2524 /* literal '[', allow [[-x] as a range */
2525 startc = *regparse++;
2526 regc(startc);
2527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 break;
2529 case CLASS_ALNUM:
2530 for (cu = 1; cu <= 255; cu++)
2531 if (isalnum(cu))
2532 regc(cu);
2533 break;
2534 case CLASS_ALPHA:
2535 for (cu = 1; cu <= 255; cu++)
2536 if (isalpha(cu))
2537 regc(cu);
2538 break;
2539 case CLASS_BLANK:
2540 regc(' ');
2541 regc('\t');
2542 break;
2543 case CLASS_CNTRL:
2544 for (cu = 1; cu <= 255; cu++)
2545 if (iscntrl(cu))
2546 regc(cu);
2547 break;
2548 case CLASS_DIGIT:
2549 for (cu = 1; cu <= 255; cu++)
2550 if (VIM_ISDIGIT(cu))
2551 regc(cu);
2552 break;
2553 case CLASS_GRAPH:
2554 for (cu = 1; cu <= 255; cu++)
2555 if (isgraph(cu))
2556 regc(cu);
2557 break;
2558 case CLASS_LOWER:
2559 for (cu = 1; cu <= 255; cu++)
Bram Moolenaara245a5b2007-08-11 11:58:23 +00002560 if (MB_ISLOWER(cu))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 regc(cu);
2562 break;
2563 case CLASS_PRINT:
2564 for (cu = 1; cu <= 255; cu++)
2565 if (vim_isprintc(cu))
2566 regc(cu);
2567 break;
2568 case CLASS_PUNCT:
2569 for (cu = 1; cu <= 255; cu++)
2570 if (ispunct(cu))
2571 regc(cu);
2572 break;
2573 case CLASS_SPACE:
2574 for (cu = 9; cu <= 13; cu++)
2575 regc(cu);
2576 regc(' ');
2577 break;
2578 case CLASS_UPPER:
2579 for (cu = 1; cu <= 255; cu++)
Bram Moolenaara245a5b2007-08-11 11:58:23 +00002580 if (MB_ISUPPER(cu))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 regc(cu);
2582 break;
2583 case CLASS_XDIGIT:
2584 for (cu = 1; cu <= 255; cu++)
2585 if (vim_isxdigit(cu))
2586 regc(cu);
2587 break;
2588 case CLASS_TAB:
2589 regc('\t');
2590 break;
2591 case CLASS_RETURN:
2592 regc('\r');
2593 break;
2594 case CLASS_BACKSPACE:
2595 regc('\b');
2596 break;
2597 case CLASS_ESCAPE:
2598 regc('\033');
2599 break;
2600 }
2601 }
2602 else
2603 {
2604#ifdef FEAT_MBYTE
2605 if (has_mbyte)
2606 {
2607 int len;
2608
2609 /* produce a multibyte character, including any
2610 * following composing characters */
2611 startc = mb_ptr2char(regparse);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002612 len = (*mb_ptr2len)(regparse);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 if (enc_utf8 && utf_char2len(startc) != len)
2614 startc = -1; /* composing chars */
2615 while (--len >= 0)
2616 regc(*regparse++);
2617 }
2618 else
2619#endif
2620 {
2621 startc = *regparse++;
2622 regc(startc);
2623 }
2624 }
2625 }
2626 regc(NUL);
2627 prevchr_len = 1; /* last char was the ']' */
2628 if (*regparse != ']')
2629 EMSG_RET_NULL(_(e_toomsbra)); /* Cannot happen? */
2630 skipchr(); /* let's be friends with the lexer again */
2631 *flagp |= HASWIDTH | SIMPLE;
2632 break;
2633 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002634 else if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002635 EMSG2_RET_NULL(_(e_missingbracket), reg_magic > MAGIC_OFF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 }
2637 /* FALLTHROUGH */
2638
2639 default:
2640 {
2641 int len;
2642
2643#ifdef FEAT_MBYTE
2644 /* A multi-byte character is handled as a separate atom if it's
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002645 * before a multi and when it's a composing char. */
2646 if (use_multibytecode(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002648do_multibyte:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 ret = regnode(MULTIBYTECODE);
2650 regmbc(c);
2651 *flagp |= HASWIDTH | SIMPLE;
2652 break;
2653 }
2654#endif
2655
2656 ret = regnode(EXACTLY);
2657
2658 /*
2659 * Append characters as long as:
2660 * - there is no following multi, we then need the character in
2661 * front of it as a single character operand
2662 * - not running into a Magic character
2663 * - "one_exactly" is not set
2664 * But always emit at least one character. Might be a Multi,
2665 * e.g., a "[" without matching "]".
2666 */
2667 for (len = 0; c != NUL && (len == 0
2668 || (re_multi_type(peekchr()) == NOT_MULTI
2669 && !one_exactly
2670 && !is_Magic(c))); ++len)
2671 {
2672 c = no_Magic(c);
2673#ifdef FEAT_MBYTE
2674 if (has_mbyte)
2675 {
2676 regmbc(c);
2677 if (enc_utf8)
2678 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 int l;
2680
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002681 /* Need to get composing character too. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 for (;;)
2683 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002684 l = utf_ptr2len(regparse);
2685 if (!UTF_COMPOSINGLIKE(regparse, regparse + l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 break;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002687 regmbc(utf_ptr2char(regparse));
2688 skipchr();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 }
2691 }
2692 else
2693#endif
2694 regc(c);
2695 c = getchr();
2696 }
2697 ungetchr();
2698
2699 regc(NUL);
2700 *flagp |= HASWIDTH;
2701 if (len == 1)
2702 *flagp |= SIMPLE;
2703 }
2704 break;
2705 }
2706
2707 return ret;
2708}
2709
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002710#ifdef FEAT_MBYTE
2711/*
2712 * Return TRUE if MULTIBYTECODE should be used instead of EXACTLY for
2713 * character "c".
2714 */
2715 static int
2716use_multibytecode(c)
2717 int c;
2718{
2719 return has_mbyte && (*mb_char2len)(c) > 1
2720 && (re_multi_type(peekchr()) != NOT_MULTI
2721 || (enc_utf8 && utf_iscomposing(c)));
2722}
2723#endif
2724
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002726 * Emit a node.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 * Return pointer to generated code.
2728 */
2729 static char_u *
2730regnode(op)
2731 int op;
2732{
2733 char_u *ret;
2734
2735 ret = regcode;
2736 if (ret == JUST_CALC_SIZE)
2737 regsize += 3;
2738 else
2739 {
2740 *regcode++ = op;
2741 *regcode++ = NUL; /* Null "next" pointer. */
2742 *regcode++ = NUL;
2743 }
2744 return ret;
2745}
2746
2747/*
2748 * Emit (if appropriate) a byte of code
2749 */
2750 static void
2751regc(b)
2752 int b;
2753{
2754 if (regcode == JUST_CALC_SIZE)
2755 regsize++;
2756 else
2757 *regcode++ = b;
2758}
2759
2760#ifdef FEAT_MBYTE
2761/*
2762 * Emit (if appropriate) a multi-byte character of code
2763 */
2764 static void
2765regmbc(c)
2766 int c;
2767{
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02002768 if (!has_mbyte && c > 0xff)
2769 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 if (regcode == JUST_CALC_SIZE)
2771 regsize += (*mb_char2len)(c);
2772 else
2773 regcode += (*mb_char2bytes)(c, regcode);
2774}
2775#endif
2776
2777/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002778 * Insert an operator in front of already-emitted operand
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 *
2780 * Means relocating the operand.
2781 */
2782 static void
2783reginsert(op, opnd)
2784 int op;
2785 char_u *opnd;
2786{
2787 char_u *src;
2788 char_u *dst;
2789 char_u *place;
2790
2791 if (regcode == JUST_CALC_SIZE)
2792 {
2793 regsize += 3;
2794 return;
2795 }
2796 src = regcode;
2797 regcode += 3;
2798 dst = regcode;
2799 while (src > opnd)
2800 *--dst = *--src;
2801
2802 place = opnd; /* Op node, where operand used to be. */
2803 *place++ = op;
2804 *place++ = NUL;
2805 *place = NUL;
2806}
2807
2808/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002809 * Insert an operator in front of already-emitted operand.
Bram Moolenaar75eb1612013-05-29 18:45:11 +02002810 * Add a number to the operator.
2811 */
2812 static void
2813reginsert_nr(op, val, opnd)
2814 int op;
2815 long val;
2816 char_u *opnd;
2817{
2818 char_u *src;
2819 char_u *dst;
2820 char_u *place;
2821
2822 if (regcode == JUST_CALC_SIZE)
2823 {
2824 regsize += 7;
2825 return;
2826 }
2827 src = regcode;
2828 regcode += 7;
2829 dst = regcode;
2830 while (src > opnd)
2831 *--dst = *--src;
2832
2833 place = opnd; /* Op node, where operand used to be. */
2834 *place++ = op;
2835 *place++ = NUL;
2836 *place++ = NUL;
2837 place = re_put_long(place, (long_u)val);
2838}
2839
2840/*
2841 * Insert an operator in front of already-emitted operand.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 * The operator has the given limit values as operands. Also set next pointer.
2843 *
2844 * Means relocating the operand.
2845 */
2846 static void
2847reginsert_limits(op, minval, maxval, opnd)
2848 int op;
2849 long minval;
2850 long maxval;
2851 char_u *opnd;
2852{
2853 char_u *src;
2854 char_u *dst;
2855 char_u *place;
2856
2857 if (regcode == JUST_CALC_SIZE)
2858 {
2859 regsize += 11;
2860 return;
2861 }
2862 src = regcode;
2863 regcode += 11;
2864 dst = regcode;
2865 while (src > opnd)
2866 *--dst = *--src;
2867
2868 place = opnd; /* Op node, where operand used to be. */
2869 *place++ = op;
2870 *place++ = NUL;
2871 *place++ = NUL;
2872 place = re_put_long(place, (long_u)minval);
2873 place = re_put_long(place, (long_u)maxval);
2874 regtail(opnd, place);
2875}
2876
2877/*
2878 * Write a long as four bytes at "p" and return pointer to the next char.
2879 */
2880 static char_u *
2881re_put_long(p, val)
2882 char_u *p;
2883 long_u val;
2884{
2885 *p++ = (char_u) ((val >> 24) & 0377);
2886 *p++ = (char_u) ((val >> 16) & 0377);
2887 *p++ = (char_u) ((val >> 8) & 0377);
2888 *p++ = (char_u) (val & 0377);
2889 return p;
2890}
2891
2892/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002893 * Set the next-pointer at the end of a node chain.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 */
2895 static void
2896regtail(p, val)
2897 char_u *p;
2898 char_u *val;
2899{
2900 char_u *scan;
2901 char_u *temp;
2902 int offset;
2903
2904 if (p == JUST_CALC_SIZE)
2905 return;
2906
2907 /* Find last node. */
2908 scan = p;
2909 for (;;)
2910 {
2911 temp = regnext(scan);
2912 if (temp == NULL)
2913 break;
2914 scan = temp;
2915 }
2916
Bram Moolenaar582fd852005-03-28 20:58:01 +00002917 if (OP(scan) == BACK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 offset = (int)(scan - val);
2919 else
2920 offset = (int)(val - scan);
Bram Moolenaard3005802009-11-25 17:21:32 +00002921 /* When the offset uses more than 16 bits it can no longer fit in the two
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02002922 * bytes available. Use a global flag to avoid having to check return
Bram Moolenaard3005802009-11-25 17:21:32 +00002923 * values in too many places. */
2924 if (offset > 0xffff)
2925 reg_toolong = TRUE;
2926 else
2927 {
2928 *(scan + 1) = (char_u) (((unsigned)offset >> 8) & 0377);
2929 *(scan + 2) = (char_u) (offset & 0377);
2930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931}
2932
2933/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002934 * Like regtail, on item after a BRANCH; nop if none.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 */
2936 static void
2937regoptail(p, val)
2938 char_u *p;
2939 char_u *val;
2940{
2941 /* When op is neither BRANCH nor BRACE_COMPLEX0-9, it is "operandless" */
2942 if (p == NULL || p == JUST_CALC_SIZE
2943 || (OP(p) != BRANCH
2944 && (OP(p) < BRACE_COMPLEX || OP(p) > BRACE_COMPLEX + 9)))
2945 return;
2946 regtail(OPERAND(p), val);
2947}
2948
2949/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002950 * Functions for getting characters from the regexp input.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 */
2952
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953static int at_start; /* True when on the first character */
2954static int prev_at_start; /* True when on the second character */
2955
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002956/*
2957 * Start parsing at "str".
2958 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 static void
2960initchr(str)
2961 char_u *str;
2962{
2963 regparse = str;
2964 prevchr_len = 0;
2965 curchr = prevprevchr = prevchr = nextchr = -1;
2966 at_start = TRUE;
2967 prev_at_start = FALSE;
2968}
2969
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002970/*
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002971 * Save the current parse state, so that it can be restored and parsing
2972 * starts in the same state again.
2973 */
2974 static void
2975save_parse_state(ps)
2976 parse_state_T *ps;
2977{
2978 ps->regparse = regparse;
2979 ps->prevchr_len = prevchr_len;
2980 ps->curchr = curchr;
2981 ps->prevchr = prevchr;
2982 ps->prevprevchr = prevprevchr;
2983 ps->nextchr = nextchr;
2984 ps->at_start = at_start;
2985 ps->prev_at_start = prev_at_start;
2986 ps->regnpar = regnpar;
2987}
2988
2989/*
2990 * Restore a previously saved parse state.
2991 */
2992 static void
2993restore_parse_state(ps)
2994 parse_state_T *ps;
2995{
2996 regparse = ps->regparse;
2997 prevchr_len = ps->prevchr_len;
2998 curchr = ps->curchr;
2999 prevchr = ps->prevchr;
3000 prevprevchr = ps->prevprevchr;
3001 nextchr = ps->nextchr;
3002 at_start = ps->at_start;
3003 prev_at_start = ps->prev_at_start;
3004 regnpar = ps->regnpar;
3005}
3006
3007
3008/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003009 * Get the next character without advancing.
3010 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 static int
3012peekchr()
3013{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003014 static int after_slash = FALSE;
3015
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 if (curchr == -1)
3017 {
3018 switch (curchr = regparse[0])
3019 {
3020 case '.':
3021 case '[':
3022 case '~':
3023 /* magic when 'magic' is on */
3024 if (reg_magic >= MAGIC_ON)
3025 curchr = Magic(curchr);
3026 break;
3027 case '(':
3028 case ')':
3029 case '{':
3030 case '%':
3031 case '+':
3032 case '=':
3033 case '?':
3034 case '@':
3035 case '!':
3036 case '&':
3037 case '|':
3038 case '<':
3039 case '>':
3040 case '#': /* future ext. */
3041 case '"': /* future ext. */
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 '/': /* Can't be used in / command */
3049 /* magic only after "\v" */
3050 if (reg_magic == MAGIC_ALL)
3051 curchr = Magic(curchr);
3052 break;
3053 case '*':
Bram Moolenaardf177f62005-02-22 08:39:57 +00003054 /* * is not magic as the very first character, eg "?*ptr", when
3055 * after '^', eg "/^*ptr" and when after "\(", "\|", "\&". But
3056 * "\(\*" is not magic, thus must be magic if "after_slash" */
3057 if (reg_magic >= MAGIC_ON
3058 && !at_start
3059 && !(prev_at_start && prevchr == Magic('^'))
3060 && (after_slash
3061 || (prevchr != Magic('(')
3062 && prevchr != Magic('&')
3063 && prevchr != Magic('|'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 curchr = Magic('*');
3065 break;
3066 case '^':
3067 /* '^' is only magic as the very first character and if it's after
3068 * "\(", "\|", "\&' or "\n" */
3069 if (reg_magic >= MAGIC_OFF
3070 && (at_start
3071 || reg_magic == MAGIC_ALL
3072 || prevchr == Magic('(')
3073 || prevchr == Magic('|')
3074 || prevchr == Magic('&')
3075 || prevchr == Magic('n')
3076 || (no_Magic(prevchr) == '('
3077 && prevprevchr == Magic('%'))))
3078 {
3079 curchr = Magic('^');
3080 at_start = TRUE;
3081 prev_at_start = FALSE;
3082 }
3083 break;
3084 case '$':
3085 /* '$' is only magic as the very last char and if it's in front of
3086 * either "\|", "\)", "\&", or "\n" */
3087 if (reg_magic >= MAGIC_OFF)
3088 {
3089 char_u *p = regparse + 1;
3090
3091 /* ignore \c \C \m and \M after '$' */
3092 while (p[0] == '\\' && (p[1] == 'c' || p[1] == 'C'
3093 || p[1] == 'm' || p[1] == 'M' || p[1] == 'Z'))
3094 p += 2;
3095 if (p[0] == NUL
3096 || (p[0] == '\\'
3097 && (p[1] == '|' || p[1] == '&' || p[1] == ')'
3098 || p[1] == 'n'))
3099 || reg_magic == MAGIC_ALL)
3100 curchr = Magic('$');
3101 }
3102 break;
3103 case '\\':
3104 {
3105 int c = regparse[1];
3106
3107 if (c == NUL)
3108 curchr = '\\'; /* trailing '\' */
3109 else if (
3110#ifdef EBCDIC
3111 vim_strchr(META, c)
3112#else
3113 c <= '~' && META_flags[c]
3114#endif
3115 )
3116 {
3117 /*
3118 * META contains everything that may be magic sometimes,
3119 * except ^ and $ ("\^" and "\$" are only magic after
3120 * "\v"). We now fetch the next character and toggle its
3121 * magicness. Therefore, \ is so meta-magic that it is
3122 * not in META.
3123 */
3124 curchr = -1;
3125 prev_at_start = at_start;
3126 at_start = FALSE; /* be able to say "/\*ptr" */
3127 ++regparse;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003128 ++after_slash;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 peekchr();
3130 --regparse;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003131 --after_slash;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 curchr = toggle_Magic(curchr);
3133 }
3134 else if (vim_strchr(REGEXP_ABBR, c))
3135 {
3136 /*
3137 * Handle abbreviations, like "\t" for TAB -- webb
3138 */
3139 curchr = backslash_trans(c);
3140 }
3141 else if (reg_magic == MAGIC_NONE && (c == '$' || c == '^'))
3142 curchr = toggle_Magic(c);
3143 else
3144 {
3145 /*
3146 * Next character can never be (made) magic?
3147 * Then backslashing it won't do anything.
3148 */
3149#ifdef FEAT_MBYTE
3150 if (has_mbyte)
3151 curchr = (*mb_ptr2char)(regparse + 1);
3152 else
3153#endif
3154 curchr = c;
3155 }
3156 break;
3157 }
3158
3159#ifdef FEAT_MBYTE
3160 default:
3161 if (has_mbyte)
3162 curchr = (*mb_ptr2char)(regparse);
3163#endif
3164 }
3165 }
3166
3167 return curchr;
3168}
3169
3170/*
3171 * Eat one lexed character. Do this in a way that we can undo it.
3172 */
3173 static void
3174skipchr()
3175{
3176 /* peekchr() eats a backslash, do the same here */
3177 if (*regparse == '\\')
3178 prevchr_len = 1;
3179 else
3180 prevchr_len = 0;
3181 if (regparse[prevchr_len] != NUL)
3182 {
3183#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003184 if (enc_utf8)
Bram Moolenaar8f5c5782007-11-29 20:27:21 +00003185 /* exclude composing chars that mb_ptr2len does include */
3186 prevchr_len += utf_ptr2len(regparse + prevchr_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003187 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003188 prevchr_len += (*mb_ptr2len)(regparse + prevchr_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 else
3190#endif
3191 ++prevchr_len;
3192 }
3193 regparse += prevchr_len;
3194 prev_at_start = at_start;
3195 at_start = FALSE;
3196 prevprevchr = prevchr;
3197 prevchr = curchr;
3198 curchr = nextchr; /* use previously unget char, or -1 */
3199 nextchr = -1;
3200}
3201
3202/*
3203 * Skip a character while keeping the value of prev_at_start for at_start.
3204 * prevchr and prevprevchr are also kept.
3205 */
3206 static void
3207skipchr_keepstart()
3208{
3209 int as = prev_at_start;
3210 int pr = prevchr;
3211 int prpr = prevprevchr;
3212
3213 skipchr();
3214 at_start = as;
3215 prevchr = pr;
3216 prevprevchr = prpr;
3217}
3218
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003219/*
3220 * Get the next character from the pattern. We know about magic and such, so
3221 * therefore we need a lexical analyzer.
3222 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 static int
3224getchr()
3225{
3226 int chr = peekchr();
3227
3228 skipchr();
3229 return chr;
3230}
3231
3232/*
3233 * put character back. Works only once!
3234 */
3235 static void
3236ungetchr()
3237{
3238 nextchr = curchr;
3239 curchr = prevchr;
3240 prevchr = prevprevchr;
3241 at_start = prev_at_start;
3242 prev_at_start = FALSE;
3243
3244 /* Backup regparse, so that it's at the same position as before the
3245 * getchr(). */
3246 regparse -= prevchr_len;
3247}
3248
3249/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00003250 * Get and return the value of the hex string at the current position.
3251 * Return -1 if there is no valid hex number.
3252 * The position is updated:
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003253 * blahblah\%x20asdf
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003254 * before-^ ^-after
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003255 * The parameter controls the maximum number of input characters. This will be
3256 * 2 when reading a \%x20 sequence and 4 when reading a \%u20AC sequence.
3257 */
3258 static int
3259gethexchrs(maxinputlen)
3260 int maxinputlen;
3261{
3262 int nr = 0;
3263 int c;
3264 int i;
3265
3266 for (i = 0; i < maxinputlen; ++i)
3267 {
3268 c = regparse[0];
3269 if (!vim_isxdigit(c))
3270 break;
3271 nr <<= 4;
3272 nr |= hex2nr(c);
3273 ++regparse;
3274 }
3275
3276 if (i == 0)
3277 return -1;
3278 return nr;
3279}
3280
3281/*
Bram Moolenaar75eb1612013-05-29 18:45:11 +02003282 * Get and return the value of the decimal string immediately after the
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003283 * current position. Return -1 for invalid. Consumes all digits.
3284 */
3285 static int
3286getdecchrs()
3287{
3288 int nr = 0;
3289 int c;
3290 int i;
3291
3292 for (i = 0; ; ++i)
3293 {
3294 c = regparse[0];
3295 if (c < '0' || c > '9')
3296 break;
3297 nr *= 10;
3298 nr += c - '0';
3299 ++regparse;
Bram Moolenaar75eb1612013-05-29 18:45:11 +02003300 curchr = -1; /* no longer valid */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003301 }
3302
3303 if (i == 0)
3304 return -1;
3305 return nr;
3306}
3307
3308/*
3309 * get and return the value of the octal string immediately after the current
3310 * position. Return -1 for invalid, or 0-255 for valid. Smart enough to handle
3311 * numbers > 377 correctly (for example, 400 is treated as 40) and doesn't
3312 * treat 8 or 9 as recognised characters. Position is updated:
3313 * blahblah\%o210asdf
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003314 * before-^ ^-after
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003315 */
3316 static int
3317getoctchrs()
3318{
3319 int nr = 0;
3320 int c;
3321 int i;
3322
3323 for (i = 0; i < 3 && nr < 040; ++i)
3324 {
3325 c = regparse[0];
3326 if (c < '0' || c > '7')
3327 break;
3328 nr <<= 3;
3329 nr |= hex2nr(c);
3330 ++regparse;
3331 }
3332
3333 if (i == 0)
3334 return -1;
3335 return nr;
3336}
3337
3338/*
3339 * Get a number after a backslash that is inside [].
3340 * When nothing is recognized return a backslash.
3341 */
3342 static int
3343coll_get_char()
3344{
3345 int nr = -1;
3346
3347 switch (*regparse++)
3348 {
3349 case 'd': nr = getdecchrs(); break;
3350 case 'o': nr = getoctchrs(); break;
3351 case 'x': nr = gethexchrs(2); break;
3352 case 'u': nr = gethexchrs(4); break;
3353 case 'U': nr = gethexchrs(8); break;
3354 }
3355 if (nr < 0)
3356 {
3357 /* If getting the number fails be backwards compatible: the character
3358 * is a backslash. */
3359 --regparse;
3360 nr = '\\';
3361 }
3362 return nr;
3363}
3364
3365/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 * read_limits - Read two integers to be taken as a minimum and maximum.
3367 * If the first character is '-', then the range is reversed.
3368 * Should end with 'end'. If minval is missing, zero is default, if maxval is
3369 * missing, a very big number is the default.
3370 */
3371 static int
3372read_limits(minval, maxval)
3373 long *minval;
3374 long *maxval;
3375{
3376 int reverse = FALSE;
3377 char_u *first_char;
3378 long tmp;
3379
3380 if (*regparse == '-')
3381 {
3382 /* Starts with '-', so reverse the range later */
3383 regparse++;
3384 reverse = TRUE;
3385 }
3386 first_char = regparse;
3387 *minval = getdigits(&regparse);
3388 if (*regparse == ',') /* There is a comma */
3389 {
3390 if (vim_isdigit(*++regparse))
3391 *maxval = getdigits(&regparse);
3392 else
3393 *maxval = MAX_LIMIT;
3394 }
3395 else if (VIM_ISDIGIT(*first_char))
3396 *maxval = *minval; /* It was \{n} or \{-n} */
3397 else
3398 *maxval = MAX_LIMIT; /* It was \{} or \{-} */
3399 if (*regparse == '\\')
3400 regparse++; /* Allow either \{...} or \{...\} */
Bram Moolenaardf177f62005-02-22 08:39:57 +00003401 if (*regparse != '}')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 {
3403 sprintf((char *)IObuff, _("E554: Syntax error in %s{...}"),
3404 reg_magic == MAGIC_ALL ? "" : "\\");
3405 EMSG_RET_FAIL(IObuff);
3406 }
3407
3408 /*
3409 * Reverse the range if there was a '-', or make sure it is in the right
3410 * order otherwise.
3411 */
3412 if ((!reverse && *minval > *maxval) || (reverse && *minval < *maxval))
3413 {
3414 tmp = *minval;
3415 *minval = *maxval;
3416 *maxval = tmp;
3417 }
3418 skipchr(); /* let's be friends with the lexer again */
3419 return OK;
3420}
3421
3422/*
3423 * vim_regexec and friends
3424 */
3425
3426/*
3427 * Global work variables for vim_regexec().
3428 */
3429
3430/* The current match-position is remembered with these variables: */
3431static linenr_T reglnum; /* line number, relative to first line */
3432static char_u *regline; /* start of current line */
3433static char_u *reginput; /* current input, points into "regline" */
3434
3435static int need_clear_subexpr; /* subexpressions still need to be
3436 * cleared */
3437#ifdef FEAT_SYN_HL
3438static int need_clear_zsubexpr = FALSE; /* extmatch subexpressions
3439 * still need to be cleared */
3440#endif
3441
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442/*
3443 * Structure used to save the current input state, when it needs to be
3444 * restored after trying a match. Used by reg_save() and reg_restore().
Bram Moolenaar582fd852005-03-28 20:58:01 +00003445 * Also stores the length of "backpos".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 */
3447typedef struct
3448{
3449 union
3450 {
3451 char_u *ptr; /* reginput pointer, for single-line regexp */
3452 lpos_T pos; /* reginput pos, for multi-line regexp */
3453 } rs_u;
Bram Moolenaar582fd852005-03-28 20:58:01 +00003454 int rs_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455} regsave_T;
3456
3457/* struct to save start/end pointer/position in for \(\) */
3458typedef struct
3459{
3460 union
3461 {
3462 char_u *ptr;
3463 lpos_T pos;
3464 } se_u;
3465} save_se_T;
3466
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00003467/* used for BEHIND and NOBEHIND matching */
3468typedef struct regbehind_S
3469{
3470 regsave_T save_after;
3471 regsave_T save_behind;
Bram Moolenaarfde483c2008-06-15 12:21:50 +00003472 int save_need_clear_subexpr;
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00003473 save_se_T save_start[NSUBEXP];
3474 save_se_T save_end[NSUBEXP];
3475} regbehind_T;
3476
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477static char_u *reg_getline __ARGS((linenr_T lnum));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003478static long bt_regexec_both __ARGS((char_u *line, colnr_T col, proftime_T *tm));
3479static long regtry __ARGS((bt_regprog_T *prog, colnr_T col));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480static void cleanup_subexpr __ARGS((void));
3481#ifdef FEAT_SYN_HL
3482static void cleanup_zsubexpr __ARGS((void));
3483#endif
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00003484static void save_subexpr __ARGS((regbehind_T *bp));
3485static void restore_subexpr __ARGS((regbehind_T *bp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486static void reg_nextline __ARGS((void));
Bram Moolenaar582fd852005-03-28 20:58:01 +00003487static void reg_save __ARGS((regsave_T *save, garray_T *gap));
3488static void reg_restore __ARGS((regsave_T *save, garray_T *gap));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489static int reg_save_equal __ARGS((regsave_T *save));
3490static void save_se_multi __ARGS((save_se_T *savep, lpos_T *posp));
3491static void save_se_one __ARGS((save_se_T *savep, char_u **pp));
3492
3493/* Save the sub-expressions before attempting a match. */
3494#define save_se(savep, posp, pp) \
3495 REG_MULTI ? save_se_multi((savep), (posp)) : save_se_one((savep), (pp))
3496
3497/* After a failed match restore the sub-expressions. */
3498#define restore_se(savep, posp, pp) { \
3499 if (REG_MULTI) \
3500 *(posp) = (savep)->se_u.pos; \
3501 else \
3502 *(pp) = (savep)->se_u.ptr; }
3503
3504static int re_num_cmp __ARGS((long_u val, char_u *scan));
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003505static int regmatch __ARGS((char_u *prog));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506static int regrepeat __ARGS((char_u *p, long maxcount));
3507
3508#ifdef DEBUG
3509int regnarrate = 0;
3510#endif
3511
3512/*
3513 * Internal copy of 'ignorecase'. It is set at each call to vim_regexec().
3514 * Normally it gets the value of "rm_ic" or "rmm_ic", but when the pattern
3515 * contains '\c' or '\C' the value is overruled.
3516 */
3517static int ireg_ic;
3518
3519#ifdef FEAT_MBYTE
3520/*
3521 * Similar to ireg_ic, but only for 'combining' characters. Set with \Z flag
3522 * in the regexp. Defaults to false, always.
3523 */
3524static int ireg_icombine;
3525#endif
3526
3527/*
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003528 * Copy of "rmm_maxcol": maximum column to search for a match. Zero when
3529 * there is no maximum.
3530 */
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003531static colnr_T ireg_maxcol;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003532
3533/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 * Sometimes need to save a copy of a line. Since alloc()/free() is very
3535 * slow, we keep one allocated piece of memory and only re-allocate it when
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003536 * it's too small. It's freed in bt_regexec_both() when finished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 */
Bram Moolenaard4210772008-01-02 14:35:30 +00003538static char_u *reg_tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539static unsigned reg_tofreelen;
3540
3541/*
3542 * These variables are set when executing a regexp to speed up the execution.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00003543 * Which ones are set depends on whether a single-line or multi-line match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 * done:
3545 * single-line multi-line
3546 * reg_match &regmatch_T NULL
3547 * reg_mmatch NULL &regmmatch_T
3548 * reg_startp reg_match->startp <invalid>
3549 * reg_endp reg_match->endp <invalid>
3550 * reg_startpos <invalid> reg_mmatch->startpos
3551 * reg_endpos <invalid> reg_mmatch->endpos
3552 * reg_win NULL window in which to search
Bram Moolenaar2f315ab2013-01-25 20:11:01 +01003553 * reg_buf curbuf buffer in which to search
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 * reg_firstlnum <invalid> first line in which to search
3555 * reg_maxline 0 last line nr
3556 * reg_line_lbr FALSE or TRUE FALSE
3557 */
3558static regmatch_T *reg_match;
3559static regmmatch_T *reg_mmatch;
3560static char_u **reg_startp = NULL;
3561static char_u **reg_endp = NULL;
3562static lpos_T *reg_startpos = NULL;
3563static lpos_T *reg_endpos = NULL;
3564static win_T *reg_win;
3565static buf_T *reg_buf;
3566static linenr_T reg_firstlnum;
3567static linenr_T reg_maxline;
3568static int reg_line_lbr; /* "\n" in string is line break */
3569
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003570/* Values for rs_state in regitem_T. */
3571typedef enum regstate_E
3572{
3573 RS_NOPEN = 0 /* NOPEN and NCLOSE */
3574 , RS_MOPEN /* MOPEN + [0-9] */
3575 , RS_MCLOSE /* MCLOSE + [0-9] */
3576#ifdef FEAT_SYN_HL
3577 , RS_ZOPEN /* ZOPEN + [0-9] */
3578 , RS_ZCLOSE /* ZCLOSE + [0-9] */
3579#endif
3580 , RS_BRANCH /* BRANCH */
3581 , RS_BRCPLX_MORE /* BRACE_COMPLEX and trying one more match */
3582 , RS_BRCPLX_LONG /* BRACE_COMPLEX and trying longest match */
3583 , RS_BRCPLX_SHORT /* BRACE_COMPLEX and trying shortest match */
3584 , RS_NOMATCH /* NOMATCH */
3585 , RS_BEHIND1 /* BEHIND / NOBEHIND matching rest */
3586 , RS_BEHIND2 /* BEHIND / NOBEHIND matching behind part */
3587 , RS_STAR_LONG /* STAR/PLUS/BRACE_SIMPLE longest match */
3588 , RS_STAR_SHORT /* STAR/PLUS/BRACE_SIMPLE shortest match */
3589} regstate_T;
3590
3591/*
3592 * When there are alternatives a regstate_T is put on the regstack to remember
3593 * what we are doing.
3594 * Before it may be another type of item, depending on rs_state, to remember
3595 * more things.
3596 */
3597typedef struct regitem_S
3598{
3599 regstate_T rs_state; /* what we are doing, one of RS_ above */
3600 char_u *rs_scan; /* current node in program */
3601 union
3602 {
3603 save_se_T sesave;
3604 regsave_T regsave;
3605 } rs_un; /* room for saving reginput */
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00003606 short rs_no; /* submatch nr or BEHIND/NOBEHIND */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003607} regitem_T;
3608
3609static regitem_T *regstack_push __ARGS((regstate_T state, char_u *scan));
3610static void regstack_pop __ARGS((char_u **scan));
3611
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003612/* used for STAR, PLUS and BRACE_SIMPLE matching */
3613typedef struct regstar_S
3614{
3615 int nextb; /* next byte */
3616 int nextb_ic; /* next byte reverse case */
3617 long count;
3618 long minval;
3619 long maxval;
3620} regstar_T;
3621
3622/* used to store input position when a BACK was encountered, so that we now if
3623 * we made any progress since the last time. */
3624typedef struct backpos_S
3625{
3626 char_u *bp_scan; /* "scan" where BACK was encountered */
3627 regsave_T bp_pos; /* last input position */
3628} backpos_T;
3629
3630/*
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003631 * "regstack" and "backpos" are used by regmatch(). They are kept over calls
3632 * to avoid invoking malloc() and free() often.
3633 * "regstack" is a stack with regitem_T items, sometimes preceded by regstar_T
3634 * or regbehind_T.
3635 * "backpos_T" is a table with backpos_T for BACK
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003636 */
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003637static garray_T regstack = {0, 0, 0, 0, NULL};
3638static garray_T backpos = {0, 0, 0, 0, NULL};
3639
3640/*
3641 * Both for regstack and backpos tables we use the following strategy of
3642 * allocation (to reduce malloc/free calls):
3643 * - Initial size is fairly small.
3644 * - When needed, the tables are grown bigger (8 times at first, double after
3645 * that).
3646 * - After executing the match we free the memory only if the array has grown.
3647 * Thus the memory is kept allocated when it's at the initial size.
3648 * This makes it fast while not keeping a lot of memory allocated.
3649 * A three times speed increase was observed when using many simple patterns.
3650 */
3651#define REGSTACK_INITIAL 2048
3652#define BACKPOS_INITIAL 64
3653
3654#if defined(EXITFREE) || defined(PROTO)
3655 void
3656free_regexp_stuff()
3657{
3658 ga_clear(&regstack);
3659 ga_clear(&backpos);
3660 vim_free(reg_tofree);
3661 vim_free(reg_prev_sub);
3662}
3663#endif
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003664
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665/*
3666 * Get pointer to the line "lnum", which is relative to "reg_firstlnum".
3667 */
3668 static char_u *
3669reg_getline(lnum)
3670 linenr_T lnum;
3671{
3672 /* when looking behind for a match/no-match lnum is negative. But we
3673 * can't go before line 1 */
3674 if (reg_firstlnum + lnum < 1)
3675 return NULL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00003676 if (lnum > reg_maxline)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003677 /* Must have matched the "\n" in the last line. */
3678 return (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 return ml_get_buf(reg_buf, reg_firstlnum + lnum, FALSE);
3680}
3681
3682static regsave_T behind_pos;
3683
3684#ifdef FEAT_SYN_HL
3685static char_u *reg_startzp[NSUBEXP]; /* Workspace to mark beginning */
3686static char_u *reg_endzp[NSUBEXP]; /* and end of \z(...\) matches */
3687static lpos_T reg_startzpos[NSUBEXP]; /* idem, beginning pos */
3688static lpos_T reg_endzpos[NSUBEXP]; /* idem, end pos */
3689#endif
3690
3691/* TRUE if using multi-line regexp. */
3692#define REG_MULTI (reg_match == NULL)
3693
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003694static int bt_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
3695
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696/*
3697 * Match a regexp against a string.
3698 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
3699 * Uses curbuf for line count and 'iskeyword'.
3700 *
3701 * Return TRUE if there is a match, FALSE if not.
3702 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003703 static int
3704bt_regexec(rmp, line, col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 regmatch_T *rmp;
3706 char_u *line; /* string to match against */
3707 colnr_T col; /* column to start looking for match */
3708{
3709 reg_match = rmp;
3710 reg_mmatch = NULL;
3711 reg_maxline = 0;
3712 reg_line_lbr = FALSE;
Bram Moolenaar2f315ab2013-01-25 20:11:01 +01003713 reg_buf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 reg_win = NULL;
3715 ireg_ic = rmp->rm_ic;
3716#ifdef FEAT_MBYTE
3717 ireg_icombine = FALSE;
3718#endif
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003719 ireg_maxcol = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003720 return (bt_regexec_both(line, col, NULL) != 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721}
3722
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003723#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
3724 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003725
3726static int bt_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
3727
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728/*
3729 * Like vim_regexec(), but consider a "\n" in "line" to be a line break.
3730 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003731 static int
3732bt_regexec_nl(rmp, line, col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 regmatch_T *rmp;
3734 char_u *line; /* string to match against */
3735 colnr_T col; /* column to start looking for match */
3736{
3737 reg_match = rmp;
3738 reg_mmatch = NULL;
3739 reg_maxline = 0;
3740 reg_line_lbr = TRUE;
Bram Moolenaar2f315ab2013-01-25 20:11:01 +01003741 reg_buf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 reg_win = NULL;
3743 ireg_ic = rmp->rm_ic;
3744#ifdef FEAT_MBYTE
3745 ireg_icombine = FALSE;
3746#endif
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003747 ireg_maxcol = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003748 return (bt_regexec_both(line, col, NULL) != 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749}
3750#endif
3751
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003752static long bt_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm));
3753
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754/*
3755 * Match a regexp against multiple lines.
3756 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
3757 * Uses curbuf for line count and 'iskeyword'.
3758 *
3759 * Return zero if there is no match. Return number of lines contained in the
3760 * match otherwise.
3761 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003762 static long
3763bt_regexec_multi(rmp, win, buf, lnum, col, tm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 regmmatch_T *rmp;
3765 win_T *win; /* window in which to search or NULL */
3766 buf_T *buf; /* buffer in which to search */
3767 linenr_T lnum; /* nr of line to start looking for match */
3768 colnr_T col; /* column to start looking for match */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003769 proftime_T *tm; /* timeout limit or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770{
3771 long r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772
3773 reg_match = NULL;
3774 reg_mmatch = rmp;
3775 reg_buf = buf;
3776 reg_win = win;
3777 reg_firstlnum = lnum;
3778 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
3779 reg_line_lbr = FALSE;
3780 ireg_ic = rmp->rmm_ic;
3781#ifdef FEAT_MBYTE
3782 ireg_icombine = FALSE;
3783#endif
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003784 ireg_maxcol = rmp->rmm_maxcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003786 r = bt_regexec_both(NULL, col, tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787
3788 return r;
3789}
3790
3791/*
3792 * Match a regexp against a string ("line" points to the string) or multiple
3793 * lines ("line" is NULL, use reg_getline()).
3794 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 static long
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003796bt_regexec_both(line, col, tm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 char_u *line;
3798 colnr_T col; /* column to start looking for match */
Bram Moolenaar78a15312009-05-15 19:33:18 +00003799 proftime_T *tm UNUSED; /* timeout limit or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003801 bt_regprog_T *prog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 char_u *s;
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003803 long retval = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003805 /* Create "regstack" and "backpos" if they are not allocated yet.
3806 * We allocate *_INITIAL amount of bytes first and then set the grow size
3807 * to much bigger value to avoid many malloc calls in case of deep regular
3808 * expressions. */
3809 if (regstack.ga_data == NULL)
3810 {
3811 /* Use an item size of 1 byte, since we push different things
3812 * onto the regstack. */
3813 ga_init2(&regstack, 1, REGSTACK_INITIAL);
3814 ga_grow(&regstack, REGSTACK_INITIAL);
3815 regstack.ga_growsize = REGSTACK_INITIAL * 8;
3816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003818 if (backpos.ga_data == NULL)
3819 {
3820 ga_init2(&backpos, sizeof(backpos_T), BACKPOS_INITIAL);
3821 ga_grow(&backpos, BACKPOS_INITIAL);
3822 backpos.ga_growsize = BACKPOS_INITIAL * 8;
3823 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003824
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 if (REG_MULTI)
3826 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003827 prog = (bt_regprog_T *)reg_mmatch->regprog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 line = reg_getline((linenr_T)0);
3829 reg_startpos = reg_mmatch->startpos;
3830 reg_endpos = reg_mmatch->endpos;
3831 }
3832 else
3833 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003834 prog = (bt_regprog_T *)reg_match->regprog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 reg_startp = reg_match->startp;
3836 reg_endp = reg_match->endp;
3837 }
3838
3839 /* Be paranoid... */
3840 if (prog == NULL || line == NULL)
3841 {
3842 EMSG(_(e_null));
3843 goto theend;
3844 }
3845
3846 /* Check validity of program. */
3847 if (prog_magic_wrong())
3848 goto theend;
3849
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003850 /* If the start column is past the maximum column: no need to try. */
3851 if (ireg_maxcol > 0 && col >= ireg_maxcol)
3852 goto theend;
3853
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
3855 if (prog->regflags & RF_ICASE)
3856 ireg_ic = TRUE;
3857 else if (prog->regflags & RF_NOICASE)
3858 ireg_ic = FALSE;
3859
3860#ifdef FEAT_MBYTE
3861 /* If pattern contains "\Z" overrule value of ireg_icombine */
3862 if (prog->regflags & RF_ICOMBINE)
3863 ireg_icombine = TRUE;
3864#endif
3865
3866 /* If there is a "must appear" string, look for it. */
3867 if (prog->regmust != NULL)
3868 {
3869 int c;
3870
3871#ifdef FEAT_MBYTE
3872 if (has_mbyte)
3873 c = (*mb_ptr2char)(prog->regmust);
3874 else
3875#endif
3876 c = *prog->regmust;
3877 s = line + col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003878
3879 /*
3880 * This is used very often, esp. for ":global". Use three versions of
3881 * the loop to avoid overhead of conditions.
3882 */
3883 if (!ireg_ic
3884#ifdef FEAT_MBYTE
3885 && !has_mbyte
3886#endif
3887 )
3888 while ((s = vim_strbyte(s, c)) != NULL)
3889 {
3890 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3891 break; /* Found it. */
3892 ++s;
3893 }
3894#ifdef FEAT_MBYTE
3895 else if (!ireg_ic || (!enc_utf8 && mb_char2len(c) > 1))
3896 while ((s = vim_strchr(s, c)) != NULL)
3897 {
3898 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3899 break; /* Found it. */
3900 mb_ptr_adv(s);
3901 }
3902#endif
3903 else
3904 while ((s = cstrchr(s, c)) != NULL)
3905 {
3906 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3907 break; /* Found it. */
3908 mb_ptr_adv(s);
3909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 if (s == NULL) /* Not present. */
3911 goto theend;
3912 }
3913
3914 regline = line;
3915 reglnum = 0;
Bram Moolenaar73a92fe2010-09-14 10:55:47 +02003916 reg_toolong = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917
3918 /* Simplest case: Anchored match need be tried only once. */
3919 if (prog->reganch)
3920 {
3921 int c;
3922
3923#ifdef FEAT_MBYTE
3924 if (has_mbyte)
3925 c = (*mb_ptr2char)(regline + col);
3926 else
3927#endif
3928 c = regline[col];
3929 if (prog->regstart == NUL
3930 || prog->regstart == c
3931 || (ireg_ic && ((
3932#ifdef FEAT_MBYTE
3933 (enc_utf8 && utf_fold(prog->regstart) == utf_fold(c)))
3934 || (c < 255 && prog->regstart < 255 &&
3935#endif
Bram Moolenaara245a5b2007-08-11 11:58:23 +00003936 MB_TOLOWER(prog->regstart) == MB_TOLOWER(c)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 retval = regtry(prog, col);
3938 else
3939 retval = 0;
3940 }
3941 else
3942 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003943#ifdef FEAT_RELTIME
3944 int tm_count = 0;
3945#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 /* Messy cases: unanchored match. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003947 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 {
3949 if (prog->regstart != NUL)
3950 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003951 /* Skip until the char we know it must start with.
3952 * Used often, do some work to avoid call overhead. */
3953 if (!ireg_ic
3954#ifdef FEAT_MBYTE
3955 && !has_mbyte
3956#endif
3957 )
3958 s = vim_strbyte(regline + col, prog->regstart);
3959 else
3960 s = cstrchr(regline + col, prog->regstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 if (s == NULL)
3962 {
3963 retval = 0;
3964 break;
3965 }
3966 col = (int)(s - regline);
3967 }
3968
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003969 /* Check for maximum column to try. */
3970 if (ireg_maxcol > 0 && col >= ireg_maxcol)
3971 {
3972 retval = 0;
3973 break;
3974 }
3975
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 retval = regtry(prog, col);
3977 if (retval > 0)
3978 break;
3979
3980 /* if not currently on the first line, get it again */
3981 if (reglnum != 0)
3982 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 reglnum = 0;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003984 regline = reg_getline((linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 }
3986 if (regline[col] == NUL)
3987 break;
3988#ifdef FEAT_MBYTE
3989 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003990 col += (*mb_ptr2len)(regline + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 else
3992#endif
3993 ++col;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003994#ifdef FEAT_RELTIME
3995 /* Check for timeout once in a twenty times to avoid overhead. */
3996 if (tm != NULL && ++tm_count == 20)
3997 {
3998 tm_count = 0;
3999 if (profile_passed_limit(tm))
4000 break;
4001 }
4002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 }
4004 }
4005
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006theend:
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00004007 /* Free "reg_tofree" when it's a bit big.
4008 * Free regstack and backpos if they are bigger than their initial size. */
4009 if (reg_tofreelen > 400)
4010 {
4011 vim_free(reg_tofree);
4012 reg_tofree = NULL;
4013 }
4014 if (regstack.ga_maxlen > REGSTACK_INITIAL)
4015 ga_clear(&regstack);
4016 if (backpos.ga_maxlen > BACKPOS_INITIAL)
4017 ga_clear(&backpos);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004018
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 return retval;
4020}
4021
4022#ifdef FEAT_SYN_HL
4023static reg_extmatch_T *make_extmatch __ARGS((void));
4024
4025/*
4026 * Create a new extmatch and mark it as referenced once.
4027 */
4028 static reg_extmatch_T *
4029make_extmatch()
4030{
4031 reg_extmatch_T *em;
4032
4033 em = (reg_extmatch_T *)alloc_clear((unsigned)sizeof(reg_extmatch_T));
4034 if (em != NULL)
4035 em->refcnt = 1;
4036 return em;
4037}
4038
4039/*
4040 * Add a reference to an extmatch.
4041 */
4042 reg_extmatch_T *
4043ref_extmatch(em)
4044 reg_extmatch_T *em;
4045{
4046 if (em != NULL)
4047 em->refcnt++;
4048 return em;
4049}
4050
4051/*
4052 * Remove a reference to an extmatch. If there are no references left, free
4053 * the info.
4054 */
4055 void
4056unref_extmatch(em)
4057 reg_extmatch_T *em;
4058{
4059 int i;
4060
4061 if (em != NULL && --em->refcnt <= 0)
4062 {
4063 for (i = 0; i < NSUBEXP; ++i)
4064 vim_free(em->matches[i]);
4065 vim_free(em);
4066 }
4067}
4068#endif
4069
4070/*
4071 * regtry - try match of "prog" with at regline["col"].
4072 * Returns 0 for failure, number of lines contained in the match otherwise.
4073 */
4074 static long
4075regtry(prog, col)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004076 bt_regprog_T *prog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 colnr_T col;
4078{
4079 reginput = regline + col;
4080 need_clear_subexpr = TRUE;
4081#ifdef FEAT_SYN_HL
4082 /* Clear the external match subpointers if necessary. */
4083 if (prog->reghasz == REX_SET)
4084 need_clear_zsubexpr = TRUE;
4085#endif
4086
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004087 if (regmatch(prog->program + 1) == 0)
4088 return 0;
4089
4090 cleanup_subexpr();
4091 if (REG_MULTI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004093 if (reg_startpos[0].lnum < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004095 reg_startpos[0].lnum = 0;
4096 reg_startpos[0].col = col;
4097 }
4098 if (reg_endpos[0].lnum < 0)
4099 {
4100 reg_endpos[0].lnum = reglnum;
4101 reg_endpos[0].col = (int)(reginput - regline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 }
4103 else
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004104 /* Use line number of "\ze". */
4105 reglnum = reg_endpos[0].lnum;
4106 }
4107 else
4108 {
4109 if (reg_startp[0] == NULL)
4110 reg_startp[0] = regline + col;
4111 if (reg_endp[0] == NULL)
4112 reg_endp[0] = reginput;
4113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114#ifdef FEAT_SYN_HL
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004115 /* Package any found \z(...\) matches for export. Default is none. */
4116 unref_extmatch(re_extmatch_out);
4117 re_extmatch_out = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004119 if (prog->reghasz == REX_SET)
4120 {
4121 int i;
4122
4123 cleanup_zsubexpr();
4124 re_extmatch_out = make_extmatch();
4125 for (i = 0; i < NSUBEXP; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004127 if (REG_MULTI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004129 /* Only accept single line matches. */
4130 if (reg_startzpos[i].lnum >= 0
4131 && reg_endzpos[i].lnum == reg_startzpos[i].lnum)
4132 re_extmatch_out->matches[i] =
4133 vim_strnsave(reg_getline(reg_startzpos[i].lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 + reg_startzpos[i].col,
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004135 reg_endzpos[i].col - reg_startzpos[i].col);
4136 }
4137 else
4138 {
4139 if (reg_startzp[i] != NULL && reg_endzp[i] != NULL)
4140 re_extmatch_out->matches[i] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 vim_strnsave(reg_startzp[i],
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004142 (int)(reg_endzp[i] - reg_startzp[i]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 }
4144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 }
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004146#endif
4147 return 1 + reglnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148}
4149
4150#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151static int reg_prev_class __ARGS((void));
4152
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153/*
4154 * Get class of previous character.
4155 */
4156 static int
4157reg_prev_class()
4158{
4159 if (reginput > regline)
Bram Moolenaarf813a182013-01-30 13:59:37 +01004160 return mb_get_class_buf(reginput - 1
4161 - (*mb_head_off)(regline, reginput - 1), reg_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 return -1;
4163}
4164
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004166#define ADVANCE_REGINPUT() mb_ptr_adv(reginput)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167
4168/*
4169 * The arguments from BRACE_LIMITS are stored here. They are actually local
4170 * to regmatch(), but they are here to reduce the amount of stack space used
4171 * (it can be called recursively many times).
4172 */
4173static long bl_minval;
4174static long bl_maxval;
4175
4176/*
4177 * regmatch - main matching routine
4178 *
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004179 * Conceptually the strategy is simple: Check to see whether the current node
4180 * matches, push an item onto the regstack and loop to see whether the rest
4181 * matches, and then act accordingly. In practice we make some effort to
4182 * avoid using the regstack, in particular by going through "ordinary" nodes
4183 * (that don't need to know whether the rest of the match failed) by a nested
4184 * loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 *
4186 * Returns TRUE when there is a match. Leaves reginput and reglnum just after
4187 * the last matched character.
4188 * Returns FALSE when there is no match. Leaves reginput and reglnum in an
4189 * undefined state!
4190 */
4191 static int
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004192regmatch(scan)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 char_u *scan; /* Current node. */
4194{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004195 char_u *next; /* Next node. */
4196 int op;
4197 int c;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004198 regitem_T *rp;
4199 int no;
4200 int status; /* one of the RA_ values: */
4201#define RA_FAIL 1 /* something failed, abort */
4202#define RA_CONT 2 /* continue in inner loop */
4203#define RA_BREAK 3 /* break inner loop */
4204#define RA_MATCH 4 /* successful match */
4205#define RA_NOMATCH 5 /* didn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00004207 /* Make "regstack" and "backpos" empty. They are allocated and freed in
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004208 * bt_regexec_both() to reduce malloc()/free() calls. */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004209 regstack.ga_len = 0;
4210 backpos.ga_len = 0;
Bram Moolenaar582fd852005-03-28 20:58:01 +00004211
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004212 /*
Bram Moolenaar582fd852005-03-28 20:58:01 +00004213 * Repeat until "regstack" is empty.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004214 */
4215 for (;;)
4216 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004217 /* Some patterns may cause a long time to match, even though they are not
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 * illegal. E.g., "\([a-z]\+\)\+Q". Allow breaking them with CTRL-C. */
4219 fast_breakcheck();
4220
4221#ifdef DEBUG
4222 if (scan != NULL && regnarrate)
4223 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004224 mch_errmsg((char *)regprop(scan));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 mch_errmsg("(\n");
4226 }
4227#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004228
4229 /*
Bram Moolenaar582fd852005-03-28 20:58:01 +00004230 * Repeat for items that can be matched sequentially, without using the
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004231 * regstack.
4232 */
4233 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004235 if (got_int || scan == NULL)
4236 {
4237 status = RA_FAIL;
4238 break;
4239 }
4240 status = RA_CONT;
4241
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242#ifdef DEBUG
4243 if (regnarrate)
4244 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004245 mch_errmsg((char *)regprop(scan));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 mch_errmsg("...\n");
4247# ifdef FEAT_SYN_HL
4248 if (re_extmatch_in != NULL)
4249 {
4250 int i;
4251
4252 mch_errmsg(_("External submatches:\n"));
4253 for (i = 0; i < NSUBEXP; i++)
4254 {
4255 mch_errmsg(" \"");
4256 if (re_extmatch_in->matches[i] != NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004257 mch_errmsg((char *)re_extmatch_in->matches[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 mch_errmsg("\"\n");
4259 }
4260 }
4261# endif
4262 }
4263#endif
4264 next = regnext(scan);
4265
4266 op = OP(scan);
4267 /* Check for character class with NL added. */
Bram Moolenaar640009d2006-10-17 16:48:26 +00004268 if (!reg_line_lbr && WITH_NL(op) && REG_MULTI
4269 && *reginput == NUL && reglnum <= reg_maxline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 {
4271 reg_nextline();
4272 }
4273 else if (reg_line_lbr && WITH_NL(op) && *reginput == '\n')
4274 {
4275 ADVANCE_REGINPUT();
4276 }
4277 else
4278 {
4279 if (WITH_NL(op))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004280 op -= ADD_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281#ifdef FEAT_MBYTE
4282 if (has_mbyte)
4283 c = (*mb_ptr2char)(reginput);
4284 else
4285#endif
4286 c = *reginput;
4287 switch (op)
4288 {
4289 case BOL:
4290 if (reginput != regline)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004291 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 break;
4293
4294 case EOL:
4295 if (c != NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004296 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 break;
4298
4299 case RE_BOF:
Bram Moolenaara7139332007-12-09 18:26:22 +00004300 /* We're not at the beginning of the file when below the first
4301 * line where we started, not at the start of the line or we
4302 * didn't start at the first line of the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 if (reglnum != 0 || reginput != regline
Bram Moolenaara7139332007-12-09 18:26:22 +00004304 || (REG_MULTI && reg_firstlnum > 1))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004305 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 break;
4307
4308 case RE_EOF:
4309 if (reglnum != reg_maxline || c != NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004310 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 break;
4312
4313 case CURSOR:
4314 /* Check if the buffer is in a window and compare the
4315 * reg_win->w_cursor position to the match position. */
4316 if (reg_win == NULL
4317 || (reglnum + reg_firstlnum != reg_win->w_cursor.lnum)
4318 || ((colnr_T)(reginput - regline) != reg_win->w_cursor.col))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004319 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 break;
4321
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004322 case RE_MARK:
4323 /* Compare the mark position to the match position. NOTE: Always
4324 * uses the current buffer. */
4325 {
4326 int mark = OPERAND(scan)[0];
4327 int cmp = OPERAND(scan)[1];
4328 pos_T *pos;
4329
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004330 pos = getmark_buf(reg_buf, mark, FALSE);
Bram Moolenaare9400a42007-05-06 13:04:32 +00004331 if (pos == NULL /* mark doesn't exist */
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004332 || pos->lnum <= 0 /* mark isn't set (in curbuf) */
4333 || (pos->lnum == reglnum + reg_firstlnum
4334 ? (pos->col == (colnr_T)(reginput - regline)
4335 ? (cmp == '<' || cmp == '>')
4336 : (pos->col < (colnr_T)(reginput - regline)
4337 ? cmp != '>'
4338 : cmp != '<'))
4339 : (pos->lnum < reglnum + reg_firstlnum
4340 ? cmp != '>'
4341 : cmp != '<')))
4342 status = RA_NOMATCH;
4343 }
4344 break;
4345
4346 case RE_VISUAL:
4347#ifdef FEAT_VISUAL
4348 /* Check if the buffer is the current buffer. and whether the
4349 * position is inside the Visual area. */
4350 if (reg_buf != curbuf || VIsual.lnum == 0)
4351 status = RA_NOMATCH;
4352 else
4353 {
4354 pos_T top, bot;
4355 linenr_T lnum;
4356 colnr_T col;
4357 win_T *wp = reg_win == NULL ? curwin : reg_win;
4358 int mode;
4359
4360 if (VIsual_active)
4361 {
4362 if (lt(VIsual, wp->w_cursor))
4363 {
4364 top = VIsual;
4365 bot = wp->w_cursor;
4366 }
4367 else
4368 {
4369 top = wp->w_cursor;
4370 bot = VIsual;
4371 }
4372 mode = VIsual_mode;
4373 }
4374 else
4375 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004376 if (lt(curbuf->b_visual.vi_start, curbuf->b_visual.vi_end))
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004377 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004378 top = curbuf->b_visual.vi_start;
4379 bot = curbuf->b_visual.vi_end;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004380 }
4381 else
4382 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004383 top = curbuf->b_visual.vi_end;
4384 bot = curbuf->b_visual.vi_start;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004385 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004386 mode = curbuf->b_visual.vi_mode;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004387 }
4388 lnum = reglnum + reg_firstlnum;
4389 col = (colnr_T)(reginput - regline);
4390 if (lnum < top.lnum || lnum > bot.lnum)
4391 status = RA_NOMATCH;
4392 else if (mode == 'v')
4393 {
4394 if ((lnum == top.lnum && col < top.col)
4395 || (lnum == bot.lnum
4396 && col >= bot.col + (*p_sel != 'e')))
4397 status = RA_NOMATCH;
4398 }
4399 else if (mode == Ctrl_V)
4400 {
4401 colnr_T start, end;
4402 colnr_T start2, end2;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004403 colnr_T cols;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004404
4405 getvvcol(wp, &top, &start, NULL, &end);
4406 getvvcol(wp, &bot, &start2, NULL, &end2);
4407 if (start2 < start)
4408 start = start2;
4409 if (end2 > end)
4410 end = end2;
4411 if (top.col == MAXCOL || bot.col == MAXCOL)
4412 end = MAXCOL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004413 cols = win_linetabsize(wp,
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004414 regline, (colnr_T)(reginput - regline));
Bram Moolenaar89d40322006-08-29 15:30:07 +00004415 if (cols < start || cols > end - (*p_sel == 'e'))
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004416 status = RA_NOMATCH;
4417 }
4418 }
4419#else
4420 status = RA_NOMATCH;
4421#endif
4422 break;
4423
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 case RE_LNUM:
4425 if (!REG_MULTI || !re_num_cmp((long_u)(reglnum + reg_firstlnum),
4426 scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004427 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 break;
4429
4430 case RE_COL:
4431 if (!re_num_cmp((long_u)(reginput - regline) + 1, scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004432 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 break;
4434
4435 case RE_VCOL:
4436 if (!re_num_cmp((long_u)win_linetabsize(
4437 reg_win == NULL ? curwin : reg_win,
4438 regline, (colnr_T)(reginput - regline)) + 1, scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004439 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 break;
4441
4442 case BOW: /* \<word; reginput points to w */
4443 if (c == NUL) /* Can't match at end of line */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004444 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445#ifdef FEAT_MBYTE
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004446 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 {
4448 int this_class;
4449
4450 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf813a182013-01-30 13:59:37 +01004451 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 if (this_class <= 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004453 status = RA_NOMATCH; /* not on a word at all */
4454 else if (reg_prev_class() == this_class)
4455 status = RA_NOMATCH; /* previous char is in same word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 }
4457#endif
4458 else
4459 {
Bram Moolenaar2f315ab2013-01-25 20:11:01 +01004460 if (!vim_iswordc_buf(c, reg_buf) || (reginput > regline
4461 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004462 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 }
4464 break;
4465
4466 case EOW: /* word\>; reginput points after d */
4467 if (reginput == regline) /* Can't match at start of line */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004468 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469#ifdef FEAT_MBYTE
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004470 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 {
4472 int this_class, prev_class;
4473
4474 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf813a182013-01-30 13:59:37 +01004475 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 prev_class = reg_prev_class();
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004477 if (this_class == prev_class
4478 || prev_class == 0 || prev_class == 1)
4479 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004482 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004484 if (!vim_iswordc_buf(reginput[-1], reg_buf)
4485 || (reginput[0] != NUL && vim_iswordc_buf(c, reg_buf)))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004486 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 }
4488 break; /* Matched with EOW */
4489
4490 case ANY:
Bram Moolenaare337e5f2013-01-30 18:21:51 +01004491 /* ANY does not match new lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 if (c == NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004493 status = RA_NOMATCH;
4494 else
4495 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 break;
4497
4498 case IDENT:
4499 if (!vim_isIDc(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004500 status = RA_NOMATCH;
4501 else
4502 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 break;
4504
4505 case SIDENT:
4506 if (VIM_ISDIGIT(*reginput) || !vim_isIDc(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004507 status = RA_NOMATCH;
4508 else
4509 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 break;
4511
4512 case KWORD:
Bram Moolenaarf813a182013-01-30 13:59:37 +01004513 if (!vim_iswordp_buf(reginput, reg_buf))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004514 status = RA_NOMATCH;
4515 else
4516 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 break;
4518
4519 case SKWORD:
Bram Moolenaarf813a182013-01-30 13:59:37 +01004520 if (VIM_ISDIGIT(*reginput) || !vim_iswordp_buf(reginput, reg_buf))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004521 status = RA_NOMATCH;
4522 else
4523 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 break;
4525
4526 case FNAME:
4527 if (!vim_isfilec(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004528 status = RA_NOMATCH;
4529 else
4530 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 break;
4532
4533 case SFNAME:
4534 if (VIM_ISDIGIT(*reginput) || !vim_isfilec(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004535 status = RA_NOMATCH;
4536 else
4537 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 break;
4539
4540 case PRINT:
4541 if (ptr2cells(reginput) != 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004542 status = RA_NOMATCH;
4543 else
4544 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 break;
4546
4547 case SPRINT:
4548 if (VIM_ISDIGIT(*reginput) || ptr2cells(reginput) != 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004549 status = RA_NOMATCH;
4550 else
4551 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 break;
4553
4554 case WHITE:
4555 if (!vim_iswhite(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004556 status = RA_NOMATCH;
4557 else
4558 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 break;
4560
4561 case NWHITE:
4562 if (c == NUL || vim_iswhite(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004563 status = RA_NOMATCH;
4564 else
4565 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 break;
4567
4568 case DIGIT:
4569 if (!ri_digit(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004570 status = RA_NOMATCH;
4571 else
4572 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 break;
4574
4575 case NDIGIT:
4576 if (c == NUL || ri_digit(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004577 status = RA_NOMATCH;
4578 else
4579 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 break;
4581
4582 case HEX:
4583 if (!ri_hex(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004584 status = RA_NOMATCH;
4585 else
4586 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 break;
4588
4589 case NHEX:
4590 if (c == NUL || ri_hex(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004591 status = RA_NOMATCH;
4592 else
4593 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 break;
4595
4596 case OCTAL:
4597 if (!ri_octal(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004598 status = RA_NOMATCH;
4599 else
4600 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 break;
4602
4603 case NOCTAL:
4604 if (c == NUL || ri_octal(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004605 status = RA_NOMATCH;
4606 else
4607 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 break;
4609
4610 case WORD:
4611 if (!ri_word(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004612 status = RA_NOMATCH;
4613 else
4614 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 break;
4616
4617 case NWORD:
4618 if (c == NUL || ri_word(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004619 status = RA_NOMATCH;
4620 else
4621 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 break;
4623
4624 case HEAD:
4625 if (!ri_head(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004626 status = RA_NOMATCH;
4627 else
4628 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 break;
4630
4631 case NHEAD:
4632 if (c == NUL || ri_head(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004633 status = RA_NOMATCH;
4634 else
4635 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 break;
4637
4638 case ALPHA:
4639 if (!ri_alpha(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004640 status = RA_NOMATCH;
4641 else
4642 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 break;
4644
4645 case NALPHA:
4646 if (c == NUL || ri_alpha(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004647 status = RA_NOMATCH;
4648 else
4649 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 break;
4651
4652 case LOWER:
4653 if (!ri_lower(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004654 status = RA_NOMATCH;
4655 else
4656 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 break;
4658
4659 case NLOWER:
4660 if (c == NUL || ri_lower(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004661 status = RA_NOMATCH;
4662 else
4663 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 break;
4665
4666 case UPPER:
4667 if (!ri_upper(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004668 status = RA_NOMATCH;
4669 else
4670 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 break;
4672
4673 case NUPPER:
4674 if (c == NUL || ri_upper(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004675 status = RA_NOMATCH;
4676 else
4677 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 break;
4679
4680 case EXACTLY:
4681 {
4682 int len;
4683 char_u *opnd;
4684
4685 opnd = OPERAND(scan);
4686 /* Inline the first byte, for speed. */
4687 if (*opnd != *reginput
4688 && (!ireg_ic || (
4689#ifdef FEAT_MBYTE
4690 !enc_utf8 &&
4691#endif
Bram Moolenaara245a5b2007-08-11 11:58:23 +00004692 MB_TOLOWER(*opnd) != MB_TOLOWER(*reginput))))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004693 status = RA_NOMATCH;
4694 else if (*opnd == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 {
4696 /* match empty string always works; happens when "~" is
4697 * empty. */
4698 }
4699 else if (opnd[1] == NUL
4700#ifdef FEAT_MBYTE
4701 && !(enc_utf8 && ireg_ic)
4702#endif
4703 )
4704 ++reginput; /* matched a single char */
4705 else
4706 {
4707 len = (int)STRLEN(opnd);
4708 /* Need to match first byte again for multi-byte. */
4709 if (cstrncmp(opnd, reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004710 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711#ifdef FEAT_MBYTE
4712 /* Check for following composing character. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004713 else if (enc_utf8
4714 && UTF_COMPOSINGLIKE(reginput, reginput + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 {
4716 /* raaron: This code makes a composing character get
4717 * ignored, which is the correct behavior (sometimes)
4718 * for voweled Hebrew texts. */
4719 if (!ireg_icombine)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004720 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004723 else
4724 reginput += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 }
4726 }
4727 break;
4728
4729 case ANYOF:
4730 case ANYBUT:
4731 if (c == NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004732 status = RA_NOMATCH;
4733 else if ((cstrchr(OPERAND(scan), c) == NULL) == (op == ANYOF))
4734 status = RA_NOMATCH;
4735 else
4736 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 break;
4738
4739#ifdef FEAT_MBYTE
4740 case MULTIBYTECODE:
4741 if (has_mbyte)
4742 {
4743 int i, len;
4744 char_u *opnd;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004745 int opndc = 0, inpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746
4747 opnd = OPERAND(scan);
4748 /* Safety check (just in case 'encoding' was changed since
4749 * compiling the program). */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004750 if ((len = (*mb_ptr2len)(opnd)) < 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004751 {
4752 status = RA_NOMATCH;
4753 break;
4754 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004755 if (enc_utf8)
4756 opndc = mb_ptr2char(opnd);
4757 if (enc_utf8 && utf_iscomposing(opndc))
4758 {
4759 /* When only a composing char is given match at any
4760 * position where that composing char appears. */
4761 status = RA_NOMATCH;
4762 for (i = 0; reginput[i] != NUL; i += utf_char2len(inpc))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004763 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004764 inpc = mb_ptr2char(reginput + i);
4765 if (!utf_iscomposing(inpc))
4766 {
4767 if (i > 0)
4768 break;
4769 }
4770 else if (opndc == inpc)
4771 {
4772 /* Include all following composing chars. */
4773 len = i + mb_ptr2len(reginput + i);
4774 status = RA_MATCH;
4775 break;
4776 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004777 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004778 }
4779 else
4780 for (i = 0; i < len; ++i)
4781 if (opnd[i] != reginput[i])
4782 {
4783 status = RA_NOMATCH;
4784 break;
4785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 reginput += len;
4787 }
4788 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004789 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 break;
4791#endif
4792
4793 case NOTHING:
4794 break;
4795
4796 case BACK:
Bram Moolenaar582fd852005-03-28 20:58:01 +00004797 {
4798 int i;
4799 backpos_T *bp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800
Bram Moolenaar582fd852005-03-28 20:58:01 +00004801 /*
4802 * When we run into BACK we need to check if we don't keep
4803 * looping without matching any input. The second and later
4804 * times a BACK is encountered it fails if the input is still
4805 * at the same position as the previous time.
4806 * The positions are stored in "backpos" and found by the
4807 * current value of "scan", the position in the RE program.
4808 */
4809 bp = (backpos_T *)backpos.ga_data;
4810 for (i = 0; i < backpos.ga_len; ++i)
4811 if (bp[i].bp_scan == scan)
4812 break;
4813 if (i == backpos.ga_len)
4814 {
4815 /* First time at this BACK, make room to store the pos. */
4816 if (ga_grow(&backpos, 1) == FAIL)
4817 status = RA_FAIL;
4818 else
4819 {
4820 /* get "ga_data" again, it may have changed */
4821 bp = (backpos_T *)backpos.ga_data;
4822 bp[i].bp_scan = scan;
4823 ++backpos.ga_len;
4824 }
4825 }
4826 else if (reg_save_equal(&bp[i].bp_pos))
4827 /* Still at same position as last time, fail. */
4828 status = RA_NOMATCH;
4829
4830 if (status != RA_FAIL && status != RA_NOMATCH)
4831 reg_save(&bp[i].bp_pos, &backpos);
4832 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004833 break;
4834
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 case MOPEN + 0: /* Match start: \zs */
4836 case MOPEN + 1: /* \( */
4837 case MOPEN + 2:
4838 case MOPEN + 3:
4839 case MOPEN + 4:
4840 case MOPEN + 5:
4841 case MOPEN + 6:
4842 case MOPEN + 7:
4843 case MOPEN + 8:
4844 case MOPEN + 9:
4845 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 no = op - MOPEN;
4847 cleanup_subexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004848 rp = regstack_push(RS_MOPEN, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004849 if (rp == NULL)
4850 status = RA_FAIL;
4851 else
4852 {
4853 rp->rs_no = no;
4854 save_se(&rp->rs_un.sesave, &reg_startpos[no],
4855 &reg_startp[no]);
4856 /* We simply continue and handle the result when done. */
4857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004859 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860
4861 case NOPEN: /* \%( */
4862 case NCLOSE: /* \) after \%( */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004863 if (regstack_push(RS_NOPEN, scan) == NULL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004864 status = RA_FAIL;
4865 /* We simply continue and handle the result when done. */
4866 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867
4868#ifdef FEAT_SYN_HL
4869 case ZOPEN + 1:
4870 case ZOPEN + 2:
4871 case ZOPEN + 3:
4872 case ZOPEN + 4:
4873 case ZOPEN + 5:
4874 case ZOPEN + 6:
4875 case ZOPEN + 7:
4876 case ZOPEN + 8:
4877 case ZOPEN + 9:
4878 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 no = op - ZOPEN;
4880 cleanup_zsubexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004881 rp = regstack_push(RS_ZOPEN, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004882 if (rp == NULL)
4883 status = RA_FAIL;
4884 else
4885 {
4886 rp->rs_no = no;
4887 save_se(&rp->rs_un.sesave, &reg_startzpos[no],
4888 &reg_startzp[no]);
4889 /* We simply continue and handle the result when done. */
4890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004892 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893#endif
4894
4895 case MCLOSE + 0: /* Match end: \ze */
4896 case MCLOSE + 1: /* \) */
4897 case MCLOSE + 2:
4898 case MCLOSE + 3:
4899 case MCLOSE + 4:
4900 case MCLOSE + 5:
4901 case MCLOSE + 6:
4902 case MCLOSE + 7:
4903 case MCLOSE + 8:
4904 case MCLOSE + 9:
4905 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 no = op - MCLOSE;
4907 cleanup_subexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004908 rp = regstack_push(RS_MCLOSE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004909 if (rp == NULL)
4910 status = RA_FAIL;
4911 else
4912 {
4913 rp->rs_no = no;
4914 save_se(&rp->rs_un.sesave, &reg_endpos[no], &reg_endp[no]);
4915 /* We simply continue and handle the result when done. */
4916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004918 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919
4920#ifdef FEAT_SYN_HL
4921 case ZCLOSE + 1: /* \) after \z( */
4922 case ZCLOSE + 2:
4923 case ZCLOSE + 3:
4924 case ZCLOSE + 4:
4925 case ZCLOSE + 5:
4926 case ZCLOSE + 6:
4927 case ZCLOSE + 7:
4928 case ZCLOSE + 8:
4929 case ZCLOSE + 9:
4930 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 no = op - ZCLOSE;
4932 cleanup_zsubexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004933 rp = regstack_push(RS_ZCLOSE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004934 if (rp == NULL)
4935 status = RA_FAIL;
4936 else
4937 {
4938 rp->rs_no = no;
4939 save_se(&rp->rs_un.sesave, &reg_endzpos[no],
4940 &reg_endzp[no]);
4941 /* We simply continue and handle the result when done. */
4942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004944 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945#endif
4946
4947 case BACKREF + 1:
4948 case BACKREF + 2:
4949 case BACKREF + 3:
4950 case BACKREF + 4:
4951 case BACKREF + 5:
4952 case BACKREF + 6:
4953 case BACKREF + 7:
4954 case BACKREF + 8:
4955 case BACKREF + 9:
4956 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 int len;
4958 linenr_T clnum;
4959 colnr_T ccol;
4960 char_u *p;
4961
4962 no = op - BACKREF;
4963 cleanup_subexpr();
4964 if (!REG_MULTI) /* Single-line regexp */
4965 {
Bram Moolenaar7670fa02009-02-21 21:04:20 +00004966 if (reg_startp[no] == NULL || reg_endp[no] == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 {
4968 /* Backref was not set: Match an empty string. */
4969 len = 0;
4970 }
4971 else
4972 {
4973 /* Compare current input with back-ref in the same
4974 * line. */
4975 len = (int)(reg_endp[no] - reg_startp[no]);
4976 if (cstrncmp(reg_startp[no], reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004977 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 }
4979 }
4980 else /* Multi-line regexp */
4981 {
Bram Moolenaar7670fa02009-02-21 21:04:20 +00004982 if (reg_startpos[no].lnum < 0 || reg_endpos[no].lnum < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 {
4984 /* Backref was not set: Match an empty string. */
4985 len = 0;
4986 }
4987 else
4988 {
4989 if (reg_startpos[no].lnum == reglnum
4990 && reg_endpos[no].lnum == reglnum)
4991 {
4992 /* Compare back-ref within the current line. */
4993 len = reg_endpos[no].col - reg_startpos[no].col;
4994 if (cstrncmp(regline + reg_startpos[no].col,
4995 reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004996 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 }
4998 else
4999 {
5000 /* Messy situation: Need to compare between two
5001 * lines. */
5002 ccol = reg_startpos[no].col;
5003 clnum = reg_startpos[no].lnum;
5004 for (;;)
5005 {
5006 /* Since getting one line may invalidate
5007 * the other, need to make copy. Slow! */
5008 if (regline != reg_tofree)
5009 {
5010 len = (int)STRLEN(regline);
5011 if (reg_tofree == NULL
5012 || len >= (int)reg_tofreelen)
5013 {
5014 len += 50; /* get some extra */
5015 vim_free(reg_tofree);
5016 reg_tofree = alloc(len);
5017 if (reg_tofree == NULL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005018 {
5019 status = RA_FAIL; /* outof memory!*/
5020 break;
5021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 reg_tofreelen = len;
5023 }
5024 STRCPY(reg_tofree, regline);
5025 reginput = reg_tofree
5026 + (reginput - regline);
5027 regline = reg_tofree;
5028 }
5029
5030 /* Get the line to compare with. */
5031 p = reg_getline(clnum);
5032 if (clnum == reg_endpos[no].lnum)
5033 len = reg_endpos[no].col - ccol;
5034 else
5035 len = (int)STRLEN(p + ccol);
5036
5037 if (cstrncmp(p + ccol, reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005038 {
5039 status = RA_NOMATCH; /* doesn't match */
5040 break;
5041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 if (clnum == reg_endpos[no].lnum)
5043 break; /* match and at end! */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005044 if (reglnum >= reg_maxline)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005045 {
5046 status = RA_NOMATCH; /* text too short */
5047 break;
5048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049
5050 /* Advance to next line. */
5051 reg_nextline();
5052 ++clnum;
5053 ccol = 0;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005054 if (got_int)
5055 {
5056 status = RA_FAIL;
5057 break;
5058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 }
5060
5061 /* found a match! Note that regline may now point
5062 * to a copy of the line, that should not matter. */
5063 }
5064 }
5065 }
5066
5067 /* Matched the backref, skip over it. */
5068 reginput += len;
5069 }
5070 break;
5071
5072#ifdef FEAT_SYN_HL
5073 case ZREF + 1:
5074 case ZREF + 2:
5075 case ZREF + 3:
5076 case ZREF + 4:
5077 case ZREF + 5:
5078 case ZREF + 6:
5079 case ZREF + 7:
5080 case ZREF + 8:
5081 case ZREF + 9:
5082 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 int len;
5084
5085 cleanup_zsubexpr();
5086 no = op - ZREF;
5087 if (re_extmatch_in != NULL
5088 && re_extmatch_in->matches[no] != NULL)
5089 {
5090 len = (int)STRLEN(re_extmatch_in->matches[no]);
5091 if (cstrncmp(re_extmatch_in->matches[no],
5092 reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005093 status = RA_NOMATCH;
5094 else
5095 reginput += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 }
5097 else
5098 {
5099 /* Backref was not set: Match an empty string. */
5100 }
5101 }
5102 break;
5103#endif
5104
5105 case BRANCH:
5106 {
5107 if (OP(next) != BRANCH) /* No choice. */
5108 next = OPERAND(scan); /* Avoid recursion. */
5109 else
5110 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005111 rp = regstack_push(RS_BRANCH, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005112 if (rp == NULL)
5113 status = RA_FAIL;
5114 else
5115 status = RA_BREAK; /* rest is below */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 }
5117 }
5118 break;
5119
5120 case BRACE_LIMITS:
5121 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 if (OP(next) == BRACE_SIMPLE)
5123 {
5124 bl_minval = OPERAND_MIN(scan);
5125 bl_maxval = OPERAND_MAX(scan);
5126 }
5127 else if (OP(next) >= BRACE_COMPLEX
5128 && OP(next) < BRACE_COMPLEX + 10)
5129 {
5130 no = OP(next) - BRACE_COMPLEX;
5131 brace_min[no] = OPERAND_MIN(scan);
5132 brace_max[no] = OPERAND_MAX(scan);
5133 brace_count[no] = 0;
5134 }
5135 else
5136 {
5137 EMSG(_(e_internal)); /* Shouldn't happen */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005138 status = RA_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 }
5140 }
5141 break;
5142
5143 case BRACE_COMPLEX + 0:
5144 case BRACE_COMPLEX + 1:
5145 case BRACE_COMPLEX + 2:
5146 case BRACE_COMPLEX + 3:
5147 case BRACE_COMPLEX + 4:
5148 case BRACE_COMPLEX + 5:
5149 case BRACE_COMPLEX + 6:
5150 case BRACE_COMPLEX + 7:
5151 case BRACE_COMPLEX + 8:
5152 case BRACE_COMPLEX + 9:
5153 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 no = op - BRACE_COMPLEX;
5155 ++brace_count[no];
5156
5157 /* If not matched enough times yet, try one more */
5158 if (brace_count[no] <= (brace_min[no] <= brace_max[no]
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005159 ? brace_min[no] : brace_max[no]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005161 rp = regstack_push(RS_BRCPLX_MORE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005162 if (rp == NULL)
5163 status = RA_FAIL;
5164 else
5165 {
5166 rp->rs_no = no;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005167 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005168 next = OPERAND(scan);
5169 /* We continue and handle the result when done. */
5170 }
5171 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 }
5173
5174 /* If matched enough times, may try matching some more */
5175 if (brace_min[no] <= brace_max[no])
5176 {
5177 /* Range is the normal way around, use longest match */
5178 if (brace_count[no] <= brace_max[no])
5179 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005180 rp = regstack_push(RS_BRCPLX_LONG, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005181 if (rp == NULL)
5182 status = RA_FAIL;
5183 else
5184 {
5185 rp->rs_no = no;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005186 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005187 next = OPERAND(scan);
5188 /* We continue and handle the result when done. */
5189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 }
5191 }
5192 else
5193 {
5194 /* Range is backwards, use shortest match first */
5195 if (brace_count[no] <= brace_min[no])
5196 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005197 rp = regstack_push(RS_BRCPLX_SHORT, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005198 if (rp == NULL)
5199 status = RA_FAIL;
5200 else
5201 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005202 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005203 /* We continue and handle the result when done. */
5204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 }
5206 }
5207 }
5208 break;
5209
5210 case BRACE_SIMPLE:
5211 case STAR:
5212 case PLUS:
5213 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005214 regstar_T rst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215
5216 /*
5217 * Lookahead to avoid useless match attempts when we know
5218 * what character comes next.
5219 */
5220 if (OP(next) == EXACTLY)
5221 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005222 rst.nextb = *OPERAND(next);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 if (ireg_ic)
5224 {
Bram Moolenaara245a5b2007-08-11 11:58:23 +00005225 if (MB_ISUPPER(rst.nextb))
5226 rst.nextb_ic = MB_TOLOWER(rst.nextb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 else
Bram Moolenaara245a5b2007-08-11 11:58:23 +00005228 rst.nextb_ic = MB_TOUPPER(rst.nextb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 }
5230 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005231 rst.nextb_ic = rst.nextb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 }
5233 else
5234 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005235 rst.nextb = NUL;
5236 rst.nextb_ic = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 }
5238 if (op != BRACE_SIMPLE)
5239 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005240 rst.minval = (op == STAR) ? 0 : 1;
5241 rst.maxval = MAX_LIMIT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 }
5243 else
5244 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005245 rst.minval = bl_minval;
5246 rst.maxval = bl_maxval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 }
5248
5249 /*
5250 * When maxval > minval, try matching as much as possible, up
5251 * to maxval. When maxval < minval, try matching at least the
5252 * minimal number (since the range is backwards, that's also
5253 * maxval!).
5254 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005255 rst.count = regrepeat(OPERAND(scan), rst.maxval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005258 status = RA_FAIL;
5259 break;
5260 }
5261 if (rst.minval <= rst.maxval
5262 ? rst.count >= rst.minval : rst.count >= rst.maxval)
5263 {
5264 /* It could match. Prepare for trying to match what
5265 * follows. The code is below. Parameters are stored in
5266 * a regstar_T on the regstack. */
Bram Moolenaar916b7af2005-03-16 09:52:38 +00005267 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005268 {
5269 EMSG(_(e_maxmempat));
5270 status = RA_FAIL;
5271 }
5272 else if (ga_grow(&regstack, sizeof(regstar_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005273 status = RA_FAIL;
5274 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005276 regstack.ga_len += sizeof(regstar_T);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005277 rp = regstack_push(rst.minval <= rst.maxval
Bram Moolenaar582fd852005-03-28 20:58:01 +00005278 ? RS_STAR_LONG : RS_STAR_SHORT, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005279 if (rp == NULL)
5280 status = RA_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005282 {
5283 *(((regstar_T *)rp) - 1) = rst;
5284 status = RA_BREAK; /* skip the restore bits */
5285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 }
5287 }
5288 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005289 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 }
5292 break;
5293
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005294 case NOMATCH:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 case MATCH:
5296 case SUBPAT:
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005297 rp = regstack_push(RS_NOMATCH, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005298 if (rp == NULL)
5299 status = RA_FAIL;
5300 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005302 rp->rs_no = op;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005303 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005304 next = OPERAND(scan);
5305 /* We continue and handle the result when done. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 }
5307 break;
5308
5309 case BEHIND:
5310 case NOBEHIND:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005311 /* Need a bit of room to store extra positions. */
Bram Moolenaar916b7af2005-03-16 09:52:38 +00005312 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005313 {
5314 EMSG(_(e_maxmempat));
5315 status = RA_FAIL;
5316 }
5317 else if (ga_grow(&regstack, sizeof(regbehind_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005318 status = RA_FAIL;
5319 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005321 regstack.ga_len += sizeof(regbehind_T);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005322 rp = regstack_push(RS_BEHIND1, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005323 if (rp == NULL)
5324 status = RA_FAIL;
5325 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 {
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005327 /* Need to save the subexpr to be able to restore them
5328 * when there is a match but we don't use it. */
5329 save_subexpr(((regbehind_T *)rp) - 1);
5330
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005331 rp->rs_no = op;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005332 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005333 /* First try if what follows matches. If it does then we
5334 * check the behind match by looping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005337 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338
5339 case BHPOS:
5340 if (REG_MULTI)
5341 {
5342 if (behind_pos.rs_u.pos.col != (colnr_T)(reginput - regline)
5343 || behind_pos.rs_u.pos.lnum != reglnum)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005344 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 }
5346 else if (behind_pos.rs_u.ptr != reginput)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005347 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 break;
5349
5350 case NEWL:
Bram Moolenaar640009d2006-10-17 16:48:26 +00005351 if ((c != NUL || !REG_MULTI || reglnum > reg_maxline
5352 || reg_line_lbr) && (c != '\n' || !reg_line_lbr))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005353 status = RA_NOMATCH;
5354 else if (reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 ADVANCE_REGINPUT();
5356 else
5357 reg_nextline();
5358 break;
5359
5360 case END:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005361 status = RA_MATCH; /* Success! */
5362 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363
5364 default:
5365 EMSG(_(e_re_corr));
5366#ifdef DEBUG
5367 printf("Illegal op code %d\n", op);
5368#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005369 status = RA_FAIL;
5370 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 }
5372 }
5373
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005374 /* If we can't continue sequentially, break the inner loop. */
5375 if (status != RA_CONT)
5376 break;
5377
5378 /* Continue in inner loop, advance to next item. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 scan = next;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005380
5381 } /* end of inner loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382
5383 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005384 * If there is something on the regstack execute the code for the state.
Bram Moolenaar582fd852005-03-28 20:58:01 +00005385 * If the state is popped then loop and use the older state.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005387 while (regstack.ga_len > 0 && status != RA_FAIL)
5388 {
5389 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len) - 1;
5390 switch (rp->rs_state)
5391 {
5392 case RS_NOPEN:
5393 /* Result is passed on as-is, simply pop the state. */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005394 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005395 break;
5396
5397 case RS_MOPEN:
5398 /* Pop the state. Restore pointers when there is no match. */
5399 if (status == RA_NOMATCH)
5400 restore_se(&rp->rs_un.sesave, &reg_startpos[rp->rs_no],
5401 &reg_startp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005402 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005403 break;
5404
5405#ifdef FEAT_SYN_HL
5406 case RS_ZOPEN:
5407 /* Pop the state. Restore pointers when there is no match. */
5408 if (status == RA_NOMATCH)
5409 restore_se(&rp->rs_un.sesave, &reg_startzpos[rp->rs_no],
5410 &reg_startzp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005411 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005412 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005414
5415 case RS_MCLOSE:
5416 /* Pop the state. Restore pointers when there is no match. */
5417 if (status == RA_NOMATCH)
5418 restore_se(&rp->rs_un.sesave, &reg_endpos[rp->rs_no],
5419 &reg_endp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005420 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005421 break;
5422
5423#ifdef FEAT_SYN_HL
5424 case RS_ZCLOSE:
5425 /* Pop the state. Restore pointers when there is no match. */
5426 if (status == RA_NOMATCH)
5427 restore_se(&rp->rs_un.sesave, &reg_endzpos[rp->rs_no],
5428 &reg_endzp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005429 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005430 break;
5431#endif
5432
5433 case RS_BRANCH:
5434 if (status == RA_MATCH)
5435 /* this branch matched, use it */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005436 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005437 else
5438 {
5439 if (status != RA_BREAK)
5440 {
5441 /* After a non-matching branch: try next one. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005442 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005443 scan = rp->rs_scan;
5444 }
5445 if (scan == NULL || OP(scan) != BRANCH)
5446 {
5447 /* no more branches, didn't find a match */
5448 status = RA_NOMATCH;
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005449 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005450 }
5451 else
5452 {
5453 /* Prepare to try a branch. */
5454 rp->rs_scan = regnext(scan);
Bram Moolenaar582fd852005-03-28 20:58:01 +00005455 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005456 scan = OPERAND(scan);
5457 }
5458 }
5459 break;
5460
5461 case RS_BRCPLX_MORE:
5462 /* Pop the state. Restore pointers when there is no match. */
5463 if (status == RA_NOMATCH)
5464 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005465 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005466 --brace_count[rp->rs_no]; /* decrement match count */
5467 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005468 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005469 break;
5470
5471 case RS_BRCPLX_LONG:
5472 /* Pop the state. Restore pointers when there is no match. */
5473 if (status == RA_NOMATCH)
5474 {
5475 /* There was no match, but we did find enough matches. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005476 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005477 --brace_count[rp->rs_no];
5478 /* continue with the items after "\{}" */
5479 status = RA_CONT;
5480 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005481 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005482 if (status == RA_CONT)
5483 scan = regnext(scan);
5484 break;
5485
5486 case RS_BRCPLX_SHORT:
5487 /* Pop the state. Restore pointers when there is no match. */
5488 if (status == RA_NOMATCH)
5489 /* There was no match, try to match one more item. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005490 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005491 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005492 if (status == RA_NOMATCH)
5493 {
5494 scan = OPERAND(scan);
5495 status = RA_CONT;
5496 }
5497 break;
5498
5499 case RS_NOMATCH:
5500 /* Pop the state. If the operand matches for NOMATCH or
5501 * doesn't match for MATCH/SUBPAT, we fail. Otherwise backup,
5502 * except for SUBPAT, and continue with the next item. */
5503 if (status == (rp->rs_no == NOMATCH ? RA_MATCH : RA_NOMATCH))
5504 status = RA_NOMATCH;
5505 else
5506 {
5507 status = RA_CONT;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005508 if (rp->rs_no != SUBPAT) /* zero-width */
5509 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005510 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005511 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005512 if (status == RA_CONT)
5513 scan = regnext(scan);
5514 break;
5515
5516 case RS_BEHIND1:
5517 if (status == RA_NOMATCH)
5518 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005519 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005520 regstack.ga_len -= sizeof(regbehind_T);
5521 }
5522 else
5523 {
5524 /* The stuff after BEHIND/NOBEHIND matches. Now try if
5525 * the behind part does (not) match before the current
5526 * position in the input. This must be done at every
5527 * position in the input and checking if the match ends at
5528 * the current position. */
5529
5530 /* save the position after the found match for next */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005531 reg_save(&(((regbehind_T *)rp) - 1)->save_after, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005532
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005533 /* Start looking for a match with operand at the current
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00005534 * position. Go back one character until we find the
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005535 * result, hitting the start of the line or the previous
5536 * line (for multi-line matching).
5537 * Set behind_pos to where the match should end, BHPOS
5538 * will match it. Save the current value. */
5539 (((regbehind_T *)rp) - 1)->save_behind = behind_pos;
5540 behind_pos = rp->rs_un.regsave;
5541
5542 rp->rs_state = RS_BEHIND2;
5543
Bram Moolenaar582fd852005-03-28 20:58:01 +00005544 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005545 scan = OPERAND(rp->rs_scan) + 4;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005546 }
5547 break;
5548
5549 case RS_BEHIND2:
5550 /*
5551 * Looping for BEHIND / NOBEHIND match.
5552 */
5553 if (status == RA_MATCH && reg_save_equal(&behind_pos))
5554 {
5555 /* found a match that ends where "next" started */
5556 behind_pos = (((regbehind_T *)rp) - 1)->save_behind;
5557 if (rp->rs_no == BEHIND)
Bram Moolenaar582fd852005-03-28 20:58:01 +00005558 reg_restore(&(((regbehind_T *)rp) - 1)->save_after,
5559 &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005560 else
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005561 {
5562 /* But we didn't want a match. Need to restore the
5563 * subexpr, because what follows matched, so they have
5564 * been set. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005565 status = RA_NOMATCH;
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005566 restore_subexpr(((regbehind_T *)rp) - 1);
5567 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005568 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005569 regstack.ga_len -= sizeof(regbehind_T);
5570 }
5571 else
5572 {
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005573 long limit;
5574
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005575 /* No match or a match that doesn't end where we want it: Go
5576 * back one character. May go to previous line once. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005577 no = OK;
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005578 limit = OPERAND_MIN(rp->rs_scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005579 if (REG_MULTI)
5580 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02005581 if (limit > 0
5582 && ((rp->rs_un.regsave.rs_u.pos.lnum
5583 < behind_pos.rs_u.pos.lnum
5584 ? (colnr_T)STRLEN(regline)
5585 : behind_pos.rs_u.pos.col)
5586 - rp->rs_un.regsave.rs_u.pos.col >= limit))
5587 no = FAIL;
5588 else if (rp->rs_un.regsave.rs_u.pos.col == 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005589 {
5590 if (rp->rs_un.regsave.rs_u.pos.lnum
5591 < behind_pos.rs_u.pos.lnum
5592 || reg_getline(
5593 --rp->rs_un.regsave.rs_u.pos.lnum)
5594 == NULL)
5595 no = FAIL;
5596 else
5597 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005598 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005599 rp->rs_un.regsave.rs_u.pos.col =
5600 (colnr_T)STRLEN(regline);
5601 }
5602 }
5603 else
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005604 {
Bram Moolenaarf5e44a72013-02-26 18:46:01 +01005605#ifdef FEAT_MBYTE
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005606 if (has_mbyte)
5607 rp->rs_un.regsave.rs_u.pos.col -=
5608 (*mb_head_off)(regline, regline
Bram Moolenaarf5e44a72013-02-26 18:46:01 +01005609 + rp->rs_un.regsave.rs_u.pos.col - 1) + 1;
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005610 else
Bram Moolenaarf5e44a72013-02-26 18:46:01 +01005611#endif
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005612 --rp->rs_un.regsave.rs_u.pos.col;
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005613 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005614 }
5615 else
5616 {
5617 if (rp->rs_un.regsave.rs_u.ptr == regline)
5618 no = FAIL;
5619 else
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005620 {
5621 mb_ptr_back(regline, rp->rs_un.regsave.rs_u.ptr);
5622 if (limit > 0 && (long)(behind_pos.rs_u.ptr
5623 - rp->rs_un.regsave.rs_u.ptr) > limit)
5624 no = FAIL;
5625 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005626 }
5627 if (no == OK)
5628 {
5629 /* Advanced, prepare for finding match again. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005630 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005631 scan = OPERAND(rp->rs_scan) + 4;
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005632 if (status == RA_MATCH)
5633 {
5634 /* We did match, so subexpr may have been changed,
5635 * need to restore them for the next try. */
5636 status = RA_NOMATCH;
5637 restore_subexpr(((regbehind_T *)rp) - 1);
5638 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005639 }
5640 else
5641 {
5642 /* Can't advance. For NOBEHIND that's a match. */
5643 behind_pos = (((regbehind_T *)rp) - 1)->save_behind;
5644 if (rp->rs_no == NOBEHIND)
5645 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005646 reg_restore(&(((regbehind_T *)rp) - 1)->save_after,
5647 &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005648 status = RA_MATCH;
5649 }
5650 else
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005651 {
5652 /* We do want a proper match. Need to restore the
5653 * subexpr if we had a match, because they may have
5654 * been set. */
5655 if (status == RA_MATCH)
5656 {
5657 status = RA_NOMATCH;
5658 restore_subexpr(((regbehind_T *)rp) - 1);
5659 }
5660 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005661 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005662 regstack.ga_len -= sizeof(regbehind_T);
5663 }
5664 }
5665 break;
5666
5667 case RS_STAR_LONG:
5668 case RS_STAR_SHORT:
5669 {
5670 regstar_T *rst = ((regstar_T *)rp) - 1;
5671
5672 if (status == RA_MATCH)
5673 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005674 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005675 regstack.ga_len -= sizeof(regstar_T);
5676 break;
5677 }
5678
5679 /* Tried once already, restore input pointers. */
5680 if (status != RA_BREAK)
Bram Moolenaar582fd852005-03-28 20:58:01 +00005681 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005682
5683 /* Repeat until we found a position where it could match. */
5684 for (;;)
5685 {
5686 if (status != RA_BREAK)
5687 {
5688 /* Tried first position already, advance. */
5689 if (rp->rs_state == RS_STAR_LONG)
5690 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005691 /* Trying for longest match, but couldn't or
5692 * didn't match -- back up one char. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005693 if (--rst->count < rst->minval)
5694 break;
5695 if (reginput == regline)
5696 {
5697 /* backup to last char of previous line */
5698 --reglnum;
5699 regline = reg_getline(reglnum);
5700 /* Just in case regrepeat() didn't count
5701 * right. */
5702 if (regline == NULL)
5703 break;
5704 reginput = regline + STRLEN(regline);
5705 fast_breakcheck();
5706 }
5707 else
5708 mb_ptr_back(regline, reginput);
5709 }
5710 else
5711 {
5712 /* Range is backwards, use shortest match first.
5713 * Careful: maxval and minval are exchanged!
5714 * Couldn't or didn't match: try advancing one
5715 * char. */
5716 if (rst->count == rst->minval
5717 || regrepeat(OPERAND(rp->rs_scan), 1L) == 0)
5718 break;
5719 ++rst->count;
5720 }
5721 if (got_int)
5722 break;
5723 }
5724 else
5725 status = RA_NOMATCH;
5726
5727 /* If it could match, try it. */
5728 if (rst->nextb == NUL || *reginput == rst->nextb
5729 || *reginput == rst->nextb_ic)
5730 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005731 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005732 scan = regnext(rp->rs_scan);
5733 status = RA_CONT;
5734 break;
5735 }
5736 }
5737 if (status != RA_CONT)
5738 {
5739 /* Failed. */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005740 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005741 regstack.ga_len -= sizeof(regstar_T);
5742 status = RA_NOMATCH;
5743 }
5744 }
5745 break;
5746 }
5747
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005748 /* If we want to continue the inner loop or didn't pop a state
5749 * continue matching loop */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005750 if (status == RA_CONT || rp == (regitem_T *)
5751 ((char *)regstack.ga_data + regstack.ga_len) - 1)
5752 break;
5753 }
5754
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005755 /* May need to continue with the inner loop, starting at "scan". */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005756 if (status == RA_CONT)
5757 continue;
5758
5759 /*
5760 * If the regstack is empty or something failed we are done.
5761 */
5762 if (regstack.ga_len == 0 || status == RA_FAIL)
5763 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005764 if (scan == NULL)
5765 {
5766 /*
5767 * We get here only if there's trouble -- normally "case END" is
5768 * the terminating point.
5769 */
5770 EMSG(_(e_re_corr));
5771#ifdef DEBUG
5772 printf("Premature EOL\n");
5773#endif
5774 }
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005775 if (status == RA_FAIL)
5776 got_int = TRUE;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005777 return (status == RA_MATCH);
5778 }
5779
5780 } /* End of loop until the regstack is empty. */
5781
5782 /* NOTREACHED */
5783}
5784
5785/*
5786 * Push an item onto the regstack.
5787 * Returns pointer to new item. Returns NULL when out of memory.
5788 */
5789 static regitem_T *
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005790regstack_push(state, scan)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005791 regstate_T state;
5792 char_u *scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005793{
5794 regitem_T *rp;
5795
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005796 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005797 {
5798 EMSG(_(e_maxmempat));
5799 return NULL;
5800 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005801 if (ga_grow(&regstack, sizeof(regitem_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005802 return NULL;
5803
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005804 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005805 rp->rs_state = state;
5806 rp->rs_scan = scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005807
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005808 regstack.ga_len += sizeof(regitem_T);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005809 return rp;
5810}
5811
5812/*
5813 * Pop an item from the regstack.
5814 */
5815 static void
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005816regstack_pop(scan)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005817 char_u **scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005818{
5819 regitem_T *rp;
5820
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005821 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len) - 1;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005822 *scan = rp->rs_scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005823
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005824 regstack.ga_len -= sizeof(regitem_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825}
5826
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827/*
5828 * regrepeat - repeatedly match something simple, return how many.
5829 * Advances reginput (and reglnum) to just after the matched chars.
5830 */
5831 static int
5832regrepeat(p, maxcount)
5833 char_u *p;
5834 long maxcount; /* maximum number of matches allowed */
5835{
5836 long count = 0;
5837 char_u *scan;
5838 char_u *opnd;
5839 int mask;
5840 int testval = 0;
5841
5842 scan = reginput; /* Make local copy of reginput for speed. */
5843 opnd = OPERAND(p);
5844 switch (OP(p))
5845 {
5846 case ANY:
5847 case ANY + ADD_NL:
5848 while (count < maxcount)
5849 {
5850 /* Matching anything means we continue until end-of-line (or
5851 * end-of-file for ANY + ADD_NL), only limited by maxcount. */
5852 while (*scan != NUL && count < maxcount)
5853 {
5854 ++count;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005855 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 }
Bram Moolenaar640009d2006-10-17 16:48:26 +00005857 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline
5858 || reg_line_lbr || count == maxcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 break;
5860 ++count; /* count the line-break */
5861 reg_nextline();
5862 scan = reginput;
5863 if (got_int)
5864 break;
5865 }
5866 break;
5867
5868 case IDENT:
5869 case IDENT + ADD_NL:
5870 testval = TRUE;
5871 /*FALLTHROUGH*/
5872 case SIDENT:
5873 case SIDENT + ADD_NL:
5874 while (count < maxcount)
5875 {
Bram Moolenaar09ea9fc2013-05-21 00:03:02 +02005876 if (vim_isIDc(PTR2CHAR(scan)) && (testval || !VIM_ISDIGIT(*scan)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005878 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 }
5880 else if (*scan == NUL)
5881 {
Bram Moolenaar640009d2006-10-17 16:48:26 +00005882 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline
5883 || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 break;
5885 reg_nextline();
5886 scan = reginput;
5887 if (got_int)
5888 break;
5889 }
5890 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5891 ++scan;
5892 else
5893 break;
5894 ++count;
5895 }
5896 break;
5897
5898 case KWORD:
5899 case KWORD + ADD_NL:
5900 testval = TRUE;
5901 /*FALLTHROUGH*/
5902 case SKWORD:
5903 case SKWORD + ADD_NL:
5904 while (count < maxcount)
5905 {
Bram Moolenaarf813a182013-01-30 13:59:37 +01005906 if (vim_iswordp_buf(scan, reg_buf)
5907 && (testval || !VIM_ISDIGIT(*scan)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005909 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 }
5911 else if (*scan == NUL)
5912 {
Bram Moolenaar640009d2006-10-17 16:48:26 +00005913 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline
5914 || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 break;
5916 reg_nextline();
5917 scan = reginput;
5918 if (got_int)
5919 break;
5920 }
5921 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5922 ++scan;
5923 else
5924 break;
5925 ++count;
5926 }
5927 break;
5928
5929 case FNAME:
5930 case FNAME + ADD_NL:
5931 testval = TRUE;
5932 /*FALLTHROUGH*/
5933 case SFNAME:
5934 case SFNAME + ADD_NL:
5935 while (count < maxcount)
5936 {
Bram Moolenaar09ea9fc2013-05-21 00:03:02 +02005937 if (vim_isfilec(PTR2CHAR(scan)) && (testval || !VIM_ISDIGIT(*scan)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005939 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 }
5941 else if (*scan == NUL)
5942 {
Bram Moolenaar640009d2006-10-17 16:48:26 +00005943 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline
5944 || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 break;
5946 reg_nextline();
5947 scan = reginput;
5948 if (got_int)
5949 break;
5950 }
5951 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5952 ++scan;
5953 else
5954 break;
5955 ++count;
5956 }
5957 break;
5958
5959 case PRINT:
5960 case PRINT + ADD_NL:
5961 testval = TRUE;
5962 /*FALLTHROUGH*/
5963 case SPRINT:
5964 case SPRINT + ADD_NL:
5965 while (count < maxcount)
5966 {
5967 if (*scan == NUL)
5968 {
Bram Moolenaar640009d2006-10-17 16:48:26 +00005969 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline
5970 || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971 break;
5972 reg_nextline();
5973 scan = reginput;
5974 if (got_int)
5975 break;
5976 }
5977 else if (ptr2cells(scan) == 1 && (testval || !VIM_ISDIGIT(*scan)))
5978 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005979 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 }
5981 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5982 ++scan;
5983 else
5984 break;
5985 ++count;
5986 }
5987 break;
5988
5989 case WHITE:
5990 case WHITE + ADD_NL:
5991 testval = mask = RI_WHITE;
5992do_class:
5993 while (count < maxcount)
5994 {
5995#ifdef FEAT_MBYTE
5996 int l;
5997#endif
5998 if (*scan == NUL)
5999 {
Bram Moolenaar640009d2006-10-17 16:48:26 +00006000 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline
6001 || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 break;
6003 reg_nextline();
6004 scan = reginput;
6005 if (got_int)
6006 break;
6007 }
6008#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006009 else if (has_mbyte && (l = (*mb_ptr2len)(scan)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 {
6011 if (testval != 0)
6012 break;
6013 scan += l;
6014 }
6015#endif
6016 else if ((class_tab[*scan] & mask) == testval)
6017 ++scan;
6018 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
6019 ++scan;
6020 else
6021 break;
6022 ++count;
6023 }
6024 break;
6025
6026 case NWHITE:
6027 case NWHITE + ADD_NL:
6028 mask = RI_WHITE;
6029 goto do_class;
6030 case DIGIT:
6031 case DIGIT + ADD_NL:
6032 testval = mask = RI_DIGIT;
6033 goto do_class;
6034 case NDIGIT:
6035 case NDIGIT + ADD_NL:
6036 mask = RI_DIGIT;
6037 goto do_class;
6038 case HEX:
6039 case HEX + ADD_NL:
6040 testval = mask = RI_HEX;
6041 goto do_class;
6042 case NHEX:
6043 case NHEX + ADD_NL:
6044 mask = RI_HEX;
6045 goto do_class;
6046 case OCTAL:
6047 case OCTAL + ADD_NL:
6048 testval = mask = RI_OCTAL;
6049 goto do_class;
6050 case NOCTAL:
6051 case NOCTAL + ADD_NL:
6052 mask = RI_OCTAL;
6053 goto do_class;
6054 case WORD:
6055 case WORD + ADD_NL:
6056 testval = mask = RI_WORD;
6057 goto do_class;
6058 case NWORD:
6059 case NWORD + ADD_NL:
6060 mask = RI_WORD;
6061 goto do_class;
6062 case HEAD:
6063 case HEAD + ADD_NL:
6064 testval = mask = RI_HEAD;
6065 goto do_class;
6066 case NHEAD:
6067 case NHEAD + ADD_NL:
6068 mask = RI_HEAD;
6069 goto do_class;
6070 case ALPHA:
6071 case ALPHA + ADD_NL:
6072 testval = mask = RI_ALPHA;
6073 goto do_class;
6074 case NALPHA:
6075 case NALPHA + ADD_NL:
6076 mask = RI_ALPHA;
6077 goto do_class;
6078 case LOWER:
6079 case LOWER + ADD_NL:
6080 testval = mask = RI_LOWER;
6081 goto do_class;
6082 case NLOWER:
6083 case NLOWER + ADD_NL:
6084 mask = RI_LOWER;
6085 goto do_class;
6086 case UPPER:
6087 case UPPER + ADD_NL:
6088 testval = mask = RI_UPPER;
6089 goto do_class;
6090 case NUPPER:
6091 case NUPPER + ADD_NL:
6092 mask = RI_UPPER;
6093 goto do_class;
6094
6095 case EXACTLY:
6096 {
6097 int cu, cl;
6098
6099 /* This doesn't do a multi-byte character, because a MULTIBYTECODE
Bram Moolenaara245a5b2007-08-11 11:58:23 +00006100 * would have been used for it. It does handle single-byte
6101 * characters, such as latin1. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 if (ireg_ic)
6103 {
Bram Moolenaara245a5b2007-08-11 11:58:23 +00006104 cu = MB_TOUPPER(*opnd);
6105 cl = MB_TOLOWER(*opnd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 while (count < maxcount && (*scan == cu || *scan == cl))
6107 {
6108 count++;
6109 scan++;
6110 }
6111 }
6112 else
6113 {
6114 cu = *opnd;
6115 while (count < maxcount && *scan == cu)
6116 {
6117 count++;
6118 scan++;
6119 }
6120 }
6121 break;
6122 }
6123
6124#ifdef FEAT_MBYTE
6125 case MULTIBYTECODE:
6126 {
6127 int i, len, cf = 0;
6128
6129 /* Safety check (just in case 'encoding' was changed since
6130 * compiling the program). */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006131 if ((len = (*mb_ptr2len)(opnd)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 {
6133 if (ireg_ic && enc_utf8)
6134 cf = utf_fold(utf_ptr2char(opnd));
6135 while (count < maxcount)
6136 {
6137 for (i = 0; i < len; ++i)
6138 if (opnd[i] != scan[i])
6139 break;
6140 if (i < len && (!ireg_ic || !enc_utf8
6141 || utf_fold(utf_ptr2char(scan)) != cf))
6142 break;
6143 scan += len;
6144 ++count;
6145 }
6146 }
6147 }
6148 break;
6149#endif
6150
6151 case ANYOF:
6152 case ANYOF + ADD_NL:
6153 testval = TRUE;
6154 /*FALLTHROUGH*/
6155
6156 case ANYBUT:
6157 case ANYBUT + ADD_NL:
6158 while (count < maxcount)
6159 {
6160#ifdef FEAT_MBYTE
6161 int len;
6162#endif
6163 if (*scan == NUL)
6164 {
Bram Moolenaar640009d2006-10-17 16:48:26 +00006165 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline
6166 || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 break;
6168 reg_nextline();
6169 scan = reginput;
6170 if (got_int)
6171 break;
6172 }
6173 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
6174 ++scan;
6175#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006176 else if (has_mbyte && (len = (*mb_ptr2len)(scan)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 {
6178 if ((cstrchr(opnd, (*mb_ptr2char)(scan)) == NULL) == testval)
6179 break;
6180 scan += len;
6181 }
6182#endif
6183 else
6184 {
6185 if ((cstrchr(opnd, *scan) == NULL) == testval)
6186 break;
6187 ++scan;
6188 }
6189 ++count;
6190 }
6191 break;
6192
6193 case NEWL:
6194 while (count < maxcount
Bram Moolenaar640009d2006-10-17 16:48:26 +00006195 && ((*scan == NUL && reglnum <= reg_maxline && !reg_line_lbr
6196 && REG_MULTI) || (*scan == '\n' && reg_line_lbr)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 {
6198 count++;
6199 if (reg_line_lbr)
6200 ADVANCE_REGINPUT();
6201 else
6202 reg_nextline();
6203 scan = reginput;
6204 if (got_int)
6205 break;
6206 }
6207 break;
6208
6209 default: /* Oh dear. Called inappropriately. */
6210 EMSG(_(e_re_corr));
6211#ifdef DEBUG
6212 printf("Called regrepeat with op code %d\n", OP(p));
6213#endif
6214 break;
6215 }
6216
6217 reginput = scan;
6218
6219 return (int)count;
6220}
6221
6222/*
6223 * regnext - dig the "next" pointer out of a node
Bram Moolenaard3005802009-11-25 17:21:32 +00006224 * Returns NULL when calculating size, when there is no next item and when
6225 * there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 */
6227 static char_u *
6228regnext(p)
6229 char_u *p;
6230{
6231 int offset;
6232
Bram Moolenaard3005802009-11-25 17:21:32 +00006233 if (p == JUST_CALC_SIZE || reg_toolong)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 return NULL;
6235
6236 offset = NEXT(p);
6237 if (offset == 0)
6238 return NULL;
6239
Bram Moolenaar582fd852005-03-28 20:58:01 +00006240 if (OP(p) == BACK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 return p - offset;
6242 else
6243 return p + offset;
6244}
6245
6246/*
6247 * Check the regexp program for its magic number.
6248 * Return TRUE if it's wrong.
6249 */
6250 static int
6251prog_magic_wrong()
6252{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006253 regprog_T *prog;
6254
6255 prog = REG_MULTI ? reg_mmatch->regprog : reg_match->regprog;
6256 if (prog->engine == &nfa_regengine)
6257 /* For NFA matcher we don't check the magic */
6258 return FALSE;
6259
6260 if (UCHARAT(((bt_regprog_T *)prog)->program) != REGMAGIC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 {
6262 EMSG(_(e_re_corr));
6263 return TRUE;
6264 }
6265 return FALSE;
6266}
6267
6268/*
6269 * Cleanup the subexpressions, if this wasn't done yet.
6270 * This construction is used to clear the subexpressions only when they are
6271 * used (to increase speed).
6272 */
6273 static void
6274cleanup_subexpr()
6275{
6276 if (need_clear_subexpr)
6277 {
6278 if (REG_MULTI)
6279 {
6280 /* Use 0xff to set lnum to -1 */
6281 vim_memset(reg_startpos, 0xff, sizeof(lpos_T) * NSUBEXP);
6282 vim_memset(reg_endpos, 0xff, sizeof(lpos_T) * NSUBEXP);
6283 }
6284 else
6285 {
6286 vim_memset(reg_startp, 0, sizeof(char_u *) * NSUBEXP);
6287 vim_memset(reg_endp, 0, sizeof(char_u *) * NSUBEXP);
6288 }
6289 need_clear_subexpr = FALSE;
6290 }
6291}
6292
6293#ifdef FEAT_SYN_HL
6294 static void
6295cleanup_zsubexpr()
6296{
6297 if (need_clear_zsubexpr)
6298 {
6299 if (REG_MULTI)
6300 {
6301 /* Use 0xff to set lnum to -1 */
6302 vim_memset(reg_startzpos, 0xff, sizeof(lpos_T) * NSUBEXP);
6303 vim_memset(reg_endzpos, 0xff, sizeof(lpos_T) * NSUBEXP);
6304 }
6305 else
6306 {
6307 vim_memset(reg_startzp, 0, sizeof(char_u *) * NSUBEXP);
6308 vim_memset(reg_endzp, 0, sizeof(char_u *) * NSUBEXP);
6309 }
6310 need_clear_zsubexpr = FALSE;
6311 }
6312}
6313#endif
6314
6315/*
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006316 * Save the current subexpr to "bp", so that they can be restored
6317 * later by restore_subexpr().
6318 */
6319 static void
6320save_subexpr(bp)
6321 regbehind_T *bp;
6322{
6323 int i;
6324
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006325 /* When "need_clear_subexpr" is set we don't need to save the values, only
6326 * remember that this flag needs to be set again when restoring. */
6327 bp->save_need_clear_subexpr = need_clear_subexpr;
6328 if (!need_clear_subexpr)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006329 {
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006330 for (i = 0; i < NSUBEXP; ++i)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006331 {
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006332 if (REG_MULTI)
6333 {
6334 bp->save_start[i].se_u.pos = reg_startpos[i];
6335 bp->save_end[i].se_u.pos = reg_endpos[i];
6336 }
6337 else
6338 {
6339 bp->save_start[i].se_u.ptr = reg_startp[i];
6340 bp->save_end[i].se_u.ptr = reg_endp[i];
6341 }
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006342 }
6343 }
6344}
6345
6346/*
6347 * Restore the subexpr from "bp".
6348 */
6349 static void
6350restore_subexpr(bp)
6351 regbehind_T *bp;
6352{
6353 int i;
6354
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006355 /* Only need to restore saved values when they are not to be cleared. */
6356 need_clear_subexpr = bp->save_need_clear_subexpr;
6357 if (!need_clear_subexpr)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006358 {
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006359 for (i = 0; i < NSUBEXP; ++i)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006360 {
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006361 if (REG_MULTI)
6362 {
6363 reg_startpos[i] = bp->save_start[i].se_u.pos;
6364 reg_endpos[i] = bp->save_end[i].se_u.pos;
6365 }
6366 else
6367 {
6368 reg_startp[i] = bp->save_start[i].se_u.ptr;
6369 reg_endp[i] = bp->save_end[i].se_u.ptr;
6370 }
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006371 }
6372 }
6373}
6374
6375/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 * Advance reglnum, regline and reginput to the next line.
6377 */
6378 static void
6379reg_nextline()
6380{
6381 regline = reg_getline(++reglnum);
6382 reginput = regline;
6383 fast_breakcheck();
6384}
6385
6386/*
6387 * Save the input line and position in a regsave_T.
6388 */
6389 static void
Bram Moolenaar582fd852005-03-28 20:58:01 +00006390reg_save(save, gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 regsave_T *save;
Bram Moolenaar582fd852005-03-28 20:58:01 +00006392 garray_T *gap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393{
6394 if (REG_MULTI)
6395 {
6396 save->rs_u.pos.col = (colnr_T)(reginput - regline);
6397 save->rs_u.pos.lnum = reglnum;
6398 }
6399 else
6400 save->rs_u.ptr = reginput;
Bram Moolenaar582fd852005-03-28 20:58:01 +00006401 save->rs_len = gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402}
6403
6404/*
6405 * Restore the input line and position from a regsave_T.
6406 */
6407 static void
Bram Moolenaar582fd852005-03-28 20:58:01 +00006408reg_restore(save, gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 regsave_T *save;
Bram Moolenaar582fd852005-03-28 20:58:01 +00006410 garray_T *gap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411{
6412 if (REG_MULTI)
6413 {
6414 if (reglnum != save->rs_u.pos.lnum)
6415 {
6416 /* only call reg_getline() when the line number changed to save
6417 * a bit of time */
6418 reglnum = save->rs_u.pos.lnum;
6419 regline = reg_getline(reglnum);
6420 }
6421 reginput = regline + save->rs_u.pos.col;
6422 }
6423 else
6424 reginput = save->rs_u.ptr;
Bram Moolenaar582fd852005-03-28 20:58:01 +00006425 gap->ga_len = save->rs_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426}
6427
6428/*
6429 * Return TRUE if current position is equal to saved position.
6430 */
6431 static int
6432reg_save_equal(save)
6433 regsave_T *save;
6434{
6435 if (REG_MULTI)
6436 return reglnum == save->rs_u.pos.lnum
6437 && reginput == regline + save->rs_u.pos.col;
6438 return reginput == save->rs_u.ptr;
6439}
6440
6441/*
6442 * Tentatively set the sub-expression start to the current position (after
6443 * calling regmatch() they will have changed). Need to save the existing
6444 * values for when there is no match.
6445 * Use se_save() to use pointer (save_se_multi()) or position (save_se_one()),
6446 * depending on REG_MULTI.
6447 */
6448 static void
6449save_se_multi(savep, posp)
6450 save_se_T *savep;
6451 lpos_T *posp;
6452{
6453 savep->se_u.pos = *posp;
6454 posp->lnum = reglnum;
6455 posp->col = (colnr_T)(reginput - regline);
6456}
6457
6458 static void
6459save_se_one(savep, pp)
6460 save_se_T *savep;
6461 char_u **pp;
6462{
6463 savep->se_u.ptr = *pp;
6464 *pp = reginput;
6465}
6466
6467/*
6468 * Compare a number with the operand of RE_LNUM, RE_COL or RE_VCOL.
6469 */
6470 static int
6471re_num_cmp(val, scan)
6472 long_u val;
6473 char_u *scan;
6474{
6475 long_u n = OPERAND_MIN(scan);
6476
6477 if (OPERAND_CMP(scan) == '>')
6478 return val > n;
6479 if (OPERAND_CMP(scan) == '<')
6480 return val < n;
6481 return val == n;
6482}
6483
6484
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006485#ifdef BT_REGEXP_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486
6487/*
6488 * regdump - dump a regexp onto stdout in vaguely comprehensible form
6489 */
6490 static void
6491regdump(pattern, r)
6492 char_u *pattern;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006493 bt_regprog_T *r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494{
6495 char_u *s;
6496 int op = EXACTLY; /* Arbitrary non-END op. */
6497 char_u *next;
6498 char_u *end = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006499 FILE *f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006501#ifdef BT_REGEXP_LOG
6502 f = fopen("bt_regexp_log.log", "a");
6503#else
6504 f = stdout;
6505#endif
6506 if (f == NULL)
6507 return;
6508 fprintf(f, "-------------------------------------\n\r\nregcomp(%s):\r\n", pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509
6510 s = r->program + 1;
6511 /*
6512 * Loop until we find the END that isn't before a referred next (an END
6513 * can also appear in a NOMATCH operand).
6514 */
6515 while (op != END || s <= end)
6516 {
6517 op = OP(s);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006518 fprintf(f, "%2d%s", (int)(s - r->program), regprop(s)); /* Where, what. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 next = regnext(s);
6520 if (next == NULL) /* Next ptr. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006521 fprintf(f, "(0)");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006523 fprintf(f, "(%d)", (int)((s - r->program) + (next - s)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 if (end < next)
6525 end = next;
6526 if (op == BRACE_LIMITS)
6527 {
6528 /* Two short ints */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006529 fprintf(f, " minval %ld, maxval %ld", OPERAND_MIN(s), OPERAND_MAX(s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 s += 8;
6531 }
6532 s += 3;
6533 if (op == ANYOF || op == ANYOF + ADD_NL
6534 || op == ANYBUT || op == ANYBUT + ADD_NL
6535 || op == EXACTLY)
6536 {
6537 /* Literal string, where present. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006538 fprintf(f, "\nxxxxxxxxx\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 while (*s != NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006540 fprintf(f, "%c", *s++);
6541 fprintf(f, "\nxxxxxxxxx\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 s++;
6543 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006544 fprintf(f, "\r\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 }
6546
6547 /* Header fields of interest. */
6548 if (r->regstart != NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006549 fprintf(f, "start `%s' 0x%x; ", r->regstart < 256
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 ? (char *)transchar(r->regstart)
6551 : "multibyte", r->regstart);
6552 if (r->reganch)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006553 fprintf(f, "anchored; ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 if (r->regmust != NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006555 fprintf(f, "must have \"%s\"", r->regmust);
6556 fprintf(f, "\r\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006558#ifdef BT_REGEXP_LOG
6559 fclose(f);
6560#endif
6561}
6562#endif /* BT_REGEXP_DUMP */
6563
6564#ifdef DEBUG
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565/*
6566 * regprop - printable representation of opcode
6567 */
6568 static char_u *
6569regprop(op)
6570 char_u *op;
6571{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006572 char *p;
6573 static char buf[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006575 STRCPY(buf, ":");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006577 switch ((int) OP(op))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 {
6579 case BOL:
6580 p = "BOL";
6581 break;
6582 case EOL:
6583 p = "EOL";
6584 break;
6585 case RE_BOF:
6586 p = "BOF";
6587 break;
6588 case RE_EOF:
6589 p = "EOF";
6590 break;
6591 case CURSOR:
6592 p = "CURSOR";
6593 break;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00006594 case RE_VISUAL:
6595 p = "RE_VISUAL";
6596 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 case RE_LNUM:
6598 p = "RE_LNUM";
6599 break;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00006600 case RE_MARK:
6601 p = "RE_MARK";
6602 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 case RE_COL:
6604 p = "RE_COL";
6605 break;
6606 case RE_VCOL:
6607 p = "RE_VCOL";
6608 break;
6609 case BOW:
6610 p = "BOW";
6611 break;
6612 case EOW:
6613 p = "EOW";
6614 break;
6615 case ANY:
6616 p = "ANY";
6617 break;
6618 case ANY + ADD_NL:
6619 p = "ANY+NL";
6620 break;
6621 case ANYOF:
6622 p = "ANYOF";
6623 break;
6624 case ANYOF + ADD_NL:
6625 p = "ANYOF+NL";
6626 break;
6627 case ANYBUT:
6628 p = "ANYBUT";
6629 break;
6630 case ANYBUT + ADD_NL:
6631 p = "ANYBUT+NL";
6632 break;
6633 case IDENT:
6634 p = "IDENT";
6635 break;
6636 case IDENT + ADD_NL:
6637 p = "IDENT+NL";
6638 break;
6639 case SIDENT:
6640 p = "SIDENT";
6641 break;
6642 case SIDENT + ADD_NL:
6643 p = "SIDENT+NL";
6644 break;
6645 case KWORD:
6646 p = "KWORD";
6647 break;
6648 case KWORD + ADD_NL:
6649 p = "KWORD+NL";
6650 break;
6651 case SKWORD:
6652 p = "SKWORD";
6653 break;
6654 case SKWORD + ADD_NL:
6655 p = "SKWORD+NL";
6656 break;
6657 case FNAME:
6658 p = "FNAME";
6659 break;
6660 case FNAME + ADD_NL:
6661 p = "FNAME+NL";
6662 break;
6663 case SFNAME:
6664 p = "SFNAME";
6665 break;
6666 case SFNAME + ADD_NL:
6667 p = "SFNAME+NL";
6668 break;
6669 case PRINT:
6670 p = "PRINT";
6671 break;
6672 case PRINT + ADD_NL:
6673 p = "PRINT+NL";
6674 break;
6675 case SPRINT:
6676 p = "SPRINT";
6677 break;
6678 case SPRINT + ADD_NL:
6679 p = "SPRINT+NL";
6680 break;
6681 case WHITE:
6682 p = "WHITE";
6683 break;
6684 case WHITE + ADD_NL:
6685 p = "WHITE+NL";
6686 break;
6687 case NWHITE:
6688 p = "NWHITE";
6689 break;
6690 case NWHITE + ADD_NL:
6691 p = "NWHITE+NL";
6692 break;
6693 case DIGIT:
6694 p = "DIGIT";
6695 break;
6696 case DIGIT + ADD_NL:
6697 p = "DIGIT+NL";
6698 break;
6699 case NDIGIT:
6700 p = "NDIGIT";
6701 break;
6702 case NDIGIT + ADD_NL:
6703 p = "NDIGIT+NL";
6704 break;
6705 case HEX:
6706 p = "HEX";
6707 break;
6708 case HEX + ADD_NL:
6709 p = "HEX+NL";
6710 break;
6711 case NHEX:
6712 p = "NHEX";
6713 break;
6714 case NHEX + ADD_NL:
6715 p = "NHEX+NL";
6716 break;
6717 case OCTAL:
6718 p = "OCTAL";
6719 break;
6720 case OCTAL + ADD_NL:
6721 p = "OCTAL+NL";
6722 break;
6723 case NOCTAL:
6724 p = "NOCTAL";
6725 break;
6726 case NOCTAL + ADD_NL:
6727 p = "NOCTAL+NL";
6728 break;
6729 case WORD:
6730 p = "WORD";
6731 break;
6732 case WORD + ADD_NL:
6733 p = "WORD+NL";
6734 break;
6735 case NWORD:
6736 p = "NWORD";
6737 break;
6738 case NWORD + ADD_NL:
6739 p = "NWORD+NL";
6740 break;
6741 case HEAD:
6742 p = "HEAD";
6743 break;
6744 case HEAD + ADD_NL:
6745 p = "HEAD+NL";
6746 break;
6747 case NHEAD:
6748 p = "NHEAD";
6749 break;
6750 case NHEAD + ADD_NL:
6751 p = "NHEAD+NL";
6752 break;
6753 case ALPHA:
6754 p = "ALPHA";
6755 break;
6756 case ALPHA + ADD_NL:
6757 p = "ALPHA+NL";
6758 break;
6759 case NALPHA:
6760 p = "NALPHA";
6761 break;
6762 case NALPHA + ADD_NL:
6763 p = "NALPHA+NL";
6764 break;
6765 case LOWER:
6766 p = "LOWER";
6767 break;
6768 case LOWER + ADD_NL:
6769 p = "LOWER+NL";
6770 break;
6771 case NLOWER:
6772 p = "NLOWER";
6773 break;
6774 case NLOWER + ADD_NL:
6775 p = "NLOWER+NL";
6776 break;
6777 case UPPER:
6778 p = "UPPER";
6779 break;
6780 case UPPER + ADD_NL:
6781 p = "UPPER+NL";
6782 break;
6783 case NUPPER:
6784 p = "NUPPER";
6785 break;
6786 case NUPPER + ADD_NL:
6787 p = "NUPPER+NL";
6788 break;
6789 case BRANCH:
6790 p = "BRANCH";
6791 break;
6792 case EXACTLY:
6793 p = "EXACTLY";
6794 break;
6795 case NOTHING:
6796 p = "NOTHING";
6797 break;
6798 case BACK:
6799 p = "BACK";
6800 break;
6801 case END:
6802 p = "END";
6803 break;
6804 case MOPEN + 0:
6805 p = "MATCH START";
6806 break;
6807 case MOPEN + 1:
6808 case MOPEN + 2:
6809 case MOPEN + 3:
6810 case MOPEN + 4:
6811 case MOPEN + 5:
6812 case MOPEN + 6:
6813 case MOPEN + 7:
6814 case MOPEN + 8:
6815 case MOPEN + 9:
6816 sprintf(buf + STRLEN(buf), "MOPEN%d", OP(op) - MOPEN);
6817 p = NULL;
6818 break;
6819 case MCLOSE + 0:
6820 p = "MATCH END";
6821 break;
6822 case MCLOSE + 1:
6823 case MCLOSE + 2:
6824 case MCLOSE + 3:
6825 case MCLOSE + 4:
6826 case MCLOSE + 5:
6827 case MCLOSE + 6:
6828 case MCLOSE + 7:
6829 case MCLOSE + 8:
6830 case MCLOSE + 9:
6831 sprintf(buf + STRLEN(buf), "MCLOSE%d", OP(op) - MCLOSE);
6832 p = NULL;
6833 break;
6834 case BACKREF + 1:
6835 case BACKREF + 2:
6836 case BACKREF + 3:
6837 case BACKREF + 4:
6838 case BACKREF + 5:
6839 case BACKREF + 6:
6840 case BACKREF + 7:
6841 case BACKREF + 8:
6842 case BACKREF + 9:
6843 sprintf(buf + STRLEN(buf), "BACKREF%d", OP(op) - BACKREF);
6844 p = NULL;
6845 break;
6846 case NOPEN:
6847 p = "NOPEN";
6848 break;
6849 case NCLOSE:
6850 p = "NCLOSE";
6851 break;
6852#ifdef FEAT_SYN_HL
6853 case ZOPEN + 1:
6854 case ZOPEN + 2:
6855 case ZOPEN + 3:
6856 case ZOPEN + 4:
6857 case ZOPEN + 5:
6858 case ZOPEN + 6:
6859 case ZOPEN + 7:
6860 case ZOPEN + 8:
6861 case ZOPEN + 9:
6862 sprintf(buf + STRLEN(buf), "ZOPEN%d", OP(op) - ZOPEN);
6863 p = NULL;
6864 break;
6865 case ZCLOSE + 1:
6866 case ZCLOSE + 2:
6867 case ZCLOSE + 3:
6868 case ZCLOSE + 4:
6869 case ZCLOSE + 5:
6870 case ZCLOSE + 6:
6871 case ZCLOSE + 7:
6872 case ZCLOSE + 8:
6873 case ZCLOSE + 9:
6874 sprintf(buf + STRLEN(buf), "ZCLOSE%d", OP(op) - ZCLOSE);
6875 p = NULL;
6876 break;
6877 case ZREF + 1:
6878 case ZREF + 2:
6879 case ZREF + 3:
6880 case ZREF + 4:
6881 case ZREF + 5:
6882 case ZREF + 6:
6883 case ZREF + 7:
6884 case ZREF + 8:
6885 case ZREF + 9:
6886 sprintf(buf + STRLEN(buf), "ZREF%d", OP(op) - ZREF);
6887 p = NULL;
6888 break;
6889#endif
6890 case STAR:
6891 p = "STAR";
6892 break;
6893 case PLUS:
6894 p = "PLUS";
6895 break;
6896 case NOMATCH:
6897 p = "NOMATCH";
6898 break;
6899 case MATCH:
6900 p = "MATCH";
6901 break;
6902 case BEHIND:
6903 p = "BEHIND";
6904 break;
6905 case NOBEHIND:
6906 p = "NOBEHIND";
6907 break;
6908 case SUBPAT:
6909 p = "SUBPAT";
6910 break;
6911 case BRACE_LIMITS:
6912 p = "BRACE_LIMITS";
6913 break;
6914 case BRACE_SIMPLE:
6915 p = "BRACE_SIMPLE";
6916 break;
6917 case BRACE_COMPLEX + 0:
6918 case BRACE_COMPLEX + 1:
6919 case BRACE_COMPLEX + 2:
6920 case BRACE_COMPLEX + 3:
6921 case BRACE_COMPLEX + 4:
6922 case BRACE_COMPLEX + 5:
6923 case BRACE_COMPLEX + 6:
6924 case BRACE_COMPLEX + 7:
6925 case BRACE_COMPLEX + 8:
6926 case BRACE_COMPLEX + 9:
6927 sprintf(buf + STRLEN(buf), "BRACE_COMPLEX%d", OP(op) - BRACE_COMPLEX);
6928 p = NULL;
6929 break;
6930#ifdef FEAT_MBYTE
6931 case MULTIBYTECODE:
6932 p = "MULTIBYTECODE";
6933 break;
6934#endif
6935 case NEWL:
6936 p = "NEWL";
6937 break;
6938 default:
6939 sprintf(buf + STRLEN(buf), "corrupt %d", OP(op));
6940 p = NULL;
6941 break;
6942 }
6943 if (p != NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006944 STRCAT(buf, p);
6945 return (char_u *)buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006947#endif /* DEBUG */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948
6949#ifdef FEAT_MBYTE
6950static void mb_decompose __ARGS((int c, int *c1, int *c2, int *c3));
6951
6952typedef struct
6953{
6954 int a, b, c;
6955} decomp_T;
6956
6957
6958/* 0xfb20 - 0xfb4f */
Bram Moolenaard6f676d2005-06-01 21:51:55 +00006959static decomp_T decomp_table[0xfb4f-0xfb20+1] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960{
6961 {0x5e2,0,0}, /* 0xfb20 alt ayin */
6962 {0x5d0,0,0}, /* 0xfb21 alt alef */
6963 {0x5d3,0,0}, /* 0xfb22 alt dalet */
6964 {0x5d4,0,0}, /* 0xfb23 alt he */
6965 {0x5db,0,0}, /* 0xfb24 alt kaf */
6966 {0x5dc,0,0}, /* 0xfb25 alt lamed */
6967 {0x5dd,0,0}, /* 0xfb26 alt mem-sofit */
6968 {0x5e8,0,0}, /* 0xfb27 alt resh */
6969 {0x5ea,0,0}, /* 0xfb28 alt tav */
6970 {'+', 0, 0}, /* 0xfb29 alt plus */
6971 {0x5e9, 0x5c1, 0}, /* 0xfb2a shin+shin-dot */
6972 {0x5e9, 0x5c2, 0}, /* 0xfb2b shin+sin-dot */
6973 {0x5e9, 0x5c1, 0x5bc}, /* 0xfb2c shin+shin-dot+dagesh */
6974 {0x5e9, 0x5c2, 0x5bc}, /* 0xfb2d shin+sin-dot+dagesh */
6975 {0x5d0, 0x5b7, 0}, /* 0xfb2e alef+patah */
6976 {0x5d0, 0x5b8, 0}, /* 0xfb2f alef+qamats */
6977 {0x5d0, 0x5b4, 0}, /* 0xfb30 alef+hiriq */
6978 {0x5d1, 0x5bc, 0}, /* 0xfb31 bet+dagesh */
6979 {0x5d2, 0x5bc, 0}, /* 0xfb32 gimel+dagesh */
6980 {0x5d3, 0x5bc, 0}, /* 0xfb33 dalet+dagesh */
6981 {0x5d4, 0x5bc, 0}, /* 0xfb34 he+dagesh */
6982 {0x5d5, 0x5bc, 0}, /* 0xfb35 vav+dagesh */
6983 {0x5d6, 0x5bc, 0}, /* 0xfb36 zayin+dagesh */
6984 {0xfb37, 0, 0}, /* 0xfb37 -- UNUSED */
6985 {0x5d8, 0x5bc, 0}, /* 0xfb38 tet+dagesh */
6986 {0x5d9, 0x5bc, 0}, /* 0xfb39 yud+dagesh */
6987 {0x5da, 0x5bc, 0}, /* 0xfb3a kaf sofit+dagesh */
6988 {0x5db, 0x5bc, 0}, /* 0xfb3b kaf+dagesh */
6989 {0x5dc, 0x5bc, 0}, /* 0xfb3c lamed+dagesh */
6990 {0xfb3d, 0, 0}, /* 0xfb3d -- UNUSED */
6991 {0x5de, 0x5bc, 0}, /* 0xfb3e mem+dagesh */
6992 {0xfb3f, 0, 0}, /* 0xfb3f -- UNUSED */
6993 {0x5e0, 0x5bc, 0}, /* 0xfb40 nun+dagesh */
6994 {0x5e1, 0x5bc, 0}, /* 0xfb41 samech+dagesh */
6995 {0xfb42, 0, 0}, /* 0xfb42 -- UNUSED */
6996 {0x5e3, 0x5bc, 0}, /* 0xfb43 pe sofit+dagesh */
6997 {0x5e4, 0x5bc,0}, /* 0xfb44 pe+dagesh */
6998 {0xfb45, 0, 0}, /* 0xfb45 -- UNUSED */
6999 {0x5e6, 0x5bc, 0}, /* 0xfb46 tsadi+dagesh */
7000 {0x5e7, 0x5bc, 0}, /* 0xfb47 qof+dagesh */
7001 {0x5e8, 0x5bc, 0}, /* 0xfb48 resh+dagesh */
7002 {0x5e9, 0x5bc, 0}, /* 0xfb49 shin+dagesh */
7003 {0x5ea, 0x5bc, 0}, /* 0xfb4a tav+dagesh */
7004 {0x5d5, 0x5b9, 0}, /* 0xfb4b vav+holam */
7005 {0x5d1, 0x5bf, 0}, /* 0xfb4c bet+rafe */
7006 {0x5db, 0x5bf, 0}, /* 0xfb4d kaf+rafe */
7007 {0x5e4, 0x5bf, 0}, /* 0xfb4e pe+rafe */
7008 {0x5d0, 0x5dc, 0} /* 0xfb4f alef-lamed */
7009};
7010
7011 static void
7012mb_decompose(c, c1, c2, c3)
7013 int c, *c1, *c2, *c3;
7014{
7015 decomp_T d;
7016
Bram Moolenaar2eec59e2013-05-21 21:37:20 +02007017 if (c >= 0xfb20 && c <= 0xfb4f)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 {
7019 d = decomp_table[c - 0xfb20];
7020 *c1 = d.a;
7021 *c2 = d.b;
7022 *c3 = d.c;
7023 }
7024 else
7025 {
7026 *c1 = c;
7027 *c2 = *c3 = 0;
7028 }
7029}
7030#endif
7031
7032/*
7033 * Compare two strings, ignore case if ireg_ic set.
7034 * Return 0 if strings match, non-zero otherwise.
7035 * Correct the length "*n" when composing characters are ignored.
7036 */
7037 static int
7038cstrncmp(s1, s2, n)
7039 char_u *s1, *s2;
7040 int *n;
7041{
7042 int result;
7043
7044 if (!ireg_ic)
7045 result = STRNCMP(s1, s2, *n);
7046 else
7047 result = MB_STRNICMP(s1, s2, *n);
7048
7049#ifdef FEAT_MBYTE
7050 /* if it failed and it's utf8 and we want to combineignore: */
7051 if (result != 0 && enc_utf8 && ireg_icombine)
7052 {
7053 char_u *str1, *str2;
7054 int c1, c2, c11, c12;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 int junk;
7056
7057 /* we have to handle the strcmp ourselves, since it is necessary to
7058 * deal with the composing characters by ignoring them: */
7059 str1 = s1;
7060 str2 = s2;
7061 c1 = c2 = 0;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007062 while ((int)(str1 - s1) < *n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 {
7064 c1 = mb_ptr2char_adv(&str1);
7065 c2 = mb_ptr2char_adv(&str2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066
7067 /* decompose the character if necessary, into 'base' characters
7068 * because I don't care about Arabic, I will hard-code the Hebrew
7069 * which I *do* care about! So sue me... */
7070 if (c1 != c2 && (!ireg_ic || utf_fold(c1) != utf_fold(c2)))
7071 {
7072 /* decomposition necessary? */
7073 mb_decompose(c1, &c11, &junk, &junk);
7074 mb_decompose(c2, &c12, &junk, &junk);
7075 c1 = c11;
7076 c2 = c12;
7077 if (c11 != c12 && (!ireg_ic || utf_fold(c11) != utf_fold(c12)))
7078 break;
7079 }
7080 }
7081 result = c2 - c1;
7082 if (result == 0)
7083 *n = (int)(str2 - s2);
7084 }
7085#endif
7086
7087 return result;
7088}
7089
7090/*
7091 * cstrchr: This function is used a lot for simple searches, keep it fast!
7092 */
7093 static char_u *
7094cstrchr(s, c)
7095 char_u *s;
7096 int c;
7097{
7098 char_u *p;
7099 int cc;
7100
7101 if (!ireg_ic
7102#ifdef FEAT_MBYTE
7103 || (!enc_utf8 && mb_char2len(c) > 1)
7104#endif
7105 )
7106 return vim_strchr(s, c);
7107
7108 /* tolower() and toupper() can be slow, comparing twice should be a lot
7109 * faster (esp. when using MS Visual C++!).
7110 * For UTF-8 need to use folded case. */
7111#ifdef FEAT_MBYTE
7112 if (enc_utf8 && c > 0x80)
7113 cc = utf_fold(c);
7114 else
7115#endif
Bram Moolenaara245a5b2007-08-11 11:58:23 +00007116 if (MB_ISUPPER(c))
7117 cc = MB_TOLOWER(c);
7118 else if (MB_ISLOWER(c))
7119 cc = MB_TOUPPER(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 else
7121 return vim_strchr(s, c);
7122
7123#ifdef FEAT_MBYTE
7124 if (has_mbyte)
7125 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007126 for (p = s; *p != NUL; p += (*mb_ptr2len)(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 {
7128 if (enc_utf8 && c > 0x80)
7129 {
7130 if (utf_fold(utf_ptr2char(p)) == cc)
7131 return p;
7132 }
7133 else if (*p == c || *p == cc)
7134 return p;
7135 }
7136 }
7137 else
7138#endif
7139 /* Faster version for when there are no multi-byte characters. */
7140 for (p = s; *p != NUL; ++p)
7141 if (*p == c || *p == cc)
7142 return p;
7143
7144 return NULL;
7145}
7146
7147/***************************************************************
7148 * regsub stuff *
7149 ***************************************************************/
7150
7151/* This stuff below really confuses cc on an SGI -- webb */
7152#ifdef __sgi
7153# undef __ARGS
7154# define __ARGS(x) ()
7155#endif
7156
7157/*
7158 * We should define ftpr as a pointer to a function returning a pointer to
7159 * a function returning a pointer to a function ...
7160 * This is impossible, so we declare a pointer to a function returning a
7161 * pointer to a function returning void. This should work for all compilers.
7162 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007163typedef void (*(*fptr_T) __ARGS((int *, int)))();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007165static fptr_T do_upper __ARGS((int *, int));
7166static fptr_T do_Upper __ARGS((int *, int));
7167static fptr_T do_lower __ARGS((int *, int));
7168static fptr_T do_Lower __ARGS((int *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169
7170static int vim_regsub_both __ARGS((char_u *source, char_u *dest, int copy, int magic, int backslash));
7171
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007172 static fptr_T
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173do_upper(d, c)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007174 int *d;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 int c;
7176{
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007177 *d = MB_TOUPPER(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007179 return (fptr_T)NULL;
7180}
7181
7182 static fptr_T
7183do_Upper(d, c)
7184 int *d;
7185 int c;
7186{
7187 *d = MB_TOUPPER(c);
7188
7189 return (fptr_T)do_Upper;
7190}
7191
7192 static fptr_T
7193do_lower(d, c)
7194 int *d;
7195 int c;
7196{
7197 *d = MB_TOLOWER(c);
7198
7199 return (fptr_T)NULL;
7200}
7201
7202 static fptr_T
7203do_Lower(d, c)
7204 int *d;
7205 int c;
7206{
7207 *d = MB_TOLOWER(c);
7208
7209 return (fptr_T)do_Lower;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210}
7211
7212/*
7213 * regtilde(): Replace tildes in the pattern by the old pattern.
7214 *
7215 * Short explanation of the tilde: It stands for the previous replacement
7216 * pattern. If that previous pattern also contains a ~ we should go back a
7217 * step further... But we insert the previous pattern into the current one
7218 * and remember that.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007219 * This still does not handle the case where "magic" changes. So require the
7220 * user to keep his hands off of "magic".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 *
7222 * The tildes are parsed once before the first call to vim_regsub().
7223 */
7224 char_u *
7225regtilde(source, magic)
7226 char_u *source;
7227 int magic;
7228{
7229 char_u *newsub = source;
7230 char_u *tmpsub;
7231 char_u *p;
7232 int len;
7233 int prevlen;
7234
7235 for (p = newsub; *p; ++p)
7236 {
7237 if ((*p == '~' && magic) || (*p == '\\' && *(p + 1) == '~' && !magic))
7238 {
7239 if (reg_prev_sub != NULL)
7240 {
7241 /* length = len(newsub) - 1 + len(prev_sub) + 1 */
7242 prevlen = (int)STRLEN(reg_prev_sub);
7243 tmpsub = alloc((unsigned)(STRLEN(newsub) + prevlen));
7244 if (tmpsub != NULL)
7245 {
7246 /* copy prefix */
7247 len = (int)(p - newsub); /* not including ~ */
7248 mch_memmove(tmpsub, newsub, (size_t)len);
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00007249 /* interpret tilde */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 mch_memmove(tmpsub + len, reg_prev_sub, (size_t)prevlen);
7251 /* copy postfix */
7252 if (!magic)
7253 ++p; /* back off \ */
7254 STRCPY(tmpsub + len + prevlen, p + 1);
7255
7256 if (newsub != source) /* already allocated newsub */
7257 vim_free(newsub);
7258 newsub = tmpsub;
7259 p = newsub + len + prevlen;
7260 }
7261 }
7262 else if (magic)
Bram Moolenaar446cb832008-06-24 21:56:24 +00007263 STRMOVE(p, p + 1); /* remove '~' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 else
Bram Moolenaar446cb832008-06-24 21:56:24 +00007265 STRMOVE(p, p + 2); /* remove '\~' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 --p;
7267 }
7268 else
7269 {
7270 if (*p == '\\' && p[1]) /* skip escaped characters */
7271 ++p;
7272#ifdef FEAT_MBYTE
7273 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007274 p += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275#endif
7276 }
7277 }
7278
7279 vim_free(reg_prev_sub);
7280 if (newsub != source) /* newsub was allocated, just keep it */
7281 reg_prev_sub = newsub;
7282 else /* no ~ found, need to save newsub */
7283 reg_prev_sub = vim_strsave(newsub);
7284 return newsub;
7285}
7286
7287#ifdef FEAT_EVAL
7288static int can_f_submatch = FALSE; /* TRUE when submatch() can be used */
7289
7290/* These pointers are used instead of reg_match and reg_mmatch for
7291 * reg_submatch(). Needed for when the substitution string is an expression
7292 * that contains a call to substitute() and submatch(). */
7293static regmatch_T *submatch_match;
7294static regmmatch_T *submatch_mmatch;
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007295static linenr_T submatch_firstlnum;
7296static linenr_T submatch_maxline;
Bram Moolenaar978287b2011-06-19 04:32:15 +02007297static int submatch_line_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298#endif
7299
7300#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
7301/*
7302 * vim_regsub() - perform substitutions after a vim_regexec() or
7303 * vim_regexec_multi() match.
7304 *
7305 * If "copy" is TRUE really copy into "dest".
7306 * If "copy" is FALSE nothing is copied, this is just to find out the length
7307 * of the result.
7308 *
7309 * If "backslash" is TRUE, a backslash will be removed later, need to double
7310 * them to keep them, and insert a backslash before a CR to avoid it being
7311 * replaced with a line break later.
7312 *
7313 * Note: The matched text must not change between the call of
7314 * vim_regexec()/vim_regexec_multi() and vim_regsub()! It would make the back
7315 * references invalid!
7316 *
7317 * Returns the size of the replacement, including terminating NUL.
7318 */
7319 int
7320vim_regsub(rmp, source, dest, copy, magic, backslash)
7321 regmatch_T *rmp;
7322 char_u *source;
7323 char_u *dest;
7324 int copy;
7325 int magic;
7326 int backslash;
7327{
7328 reg_match = rmp;
7329 reg_mmatch = NULL;
7330 reg_maxline = 0;
Bram Moolenaar2f315ab2013-01-25 20:11:01 +01007331 reg_buf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 return vim_regsub_both(source, dest, copy, magic, backslash);
7333}
7334#endif
7335
7336 int
7337vim_regsub_multi(rmp, lnum, source, dest, copy, magic, backslash)
7338 regmmatch_T *rmp;
7339 linenr_T lnum;
7340 char_u *source;
7341 char_u *dest;
7342 int copy;
7343 int magic;
7344 int backslash;
7345{
7346 reg_match = NULL;
7347 reg_mmatch = rmp;
7348 reg_buf = curbuf; /* always works on the current buffer! */
7349 reg_firstlnum = lnum;
7350 reg_maxline = curbuf->b_ml.ml_line_count - lnum;
7351 return vim_regsub_both(source, dest, copy, magic, backslash);
7352}
7353
7354 static int
7355vim_regsub_both(source, dest, copy, magic, backslash)
7356 char_u *source;
7357 char_u *dest;
7358 int copy;
7359 int magic;
7360 int backslash;
7361{
7362 char_u *src;
7363 char_u *dst;
7364 char_u *s;
7365 int c;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007366 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 int no = -1;
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007368 fptr_T func_all = (fptr_T)NULL;
7369 fptr_T func_one = (fptr_T)NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 linenr_T clnum = 0; /* init for GCC */
7371 int len = 0; /* init for GCC */
7372#ifdef FEAT_EVAL
7373 static char_u *eval_result = NULL;
7374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375
7376 /* Be paranoid... */
7377 if (source == NULL || dest == NULL)
7378 {
7379 EMSG(_(e_null));
7380 return 0;
7381 }
7382 if (prog_magic_wrong())
7383 return 0;
7384 src = source;
7385 dst = dest;
7386
7387 /*
7388 * When the substitute part starts with "\=" evaluate it as an expression.
7389 */
7390 if (source[0] == '\\' && source[1] == '='
7391#ifdef FEAT_EVAL
7392 && !can_f_submatch /* can't do this recursively */
7393#endif
7394 )
7395 {
7396#ifdef FEAT_EVAL
7397 /* To make sure that the length doesn't change between checking the
7398 * length and copying the string, and to speed up things, the
7399 * resulting string is saved from the call with "copy" == FALSE to the
7400 * call with "copy" == TRUE. */
7401 if (copy)
7402 {
7403 if (eval_result != NULL)
7404 {
7405 STRCPY(dest, eval_result);
7406 dst += STRLEN(eval_result);
7407 vim_free(eval_result);
7408 eval_result = NULL;
7409 }
7410 }
7411 else
7412 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 win_T *save_reg_win;
7414 int save_ireg_ic;
7415
7416 vim_free(eval_result);
7417
7418 /* The expression may contain substitute(), which calls us
7419 * recursively. Make sure submatch() gets the text from the first
7420 * level. Don't need to save "reg_buf", because
7421 * vim_regexec_multi() can't be called recursively. */
7422 submatch_match = reg_match;
7423 submatch_mmatch = reg_mmatch;
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007424 submatch_firstlnum = reg_firstlnum;
7425 submatch_maxline = reg_maxline;
Bram Moolenaar978287b2011-06-19 04:32:15 +02007426 submatch_line_lbr = reg_line_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 save_reg_win = reg_win;
7428 save_ireg_ic = ireg_ic;
7429 can_f_submatch = TRUE;
7430
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007431 eval_result = eval_to_string(source + 2, NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 if (eval_result != NULL)
7433 {
Bram Moolenaar06975a42010-03-23 16:27:22 +01007434 int had_backslash = FALSE;
7435
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00007436 for (s = eval_result; *s != NUL; mb_ptr_adv(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 {
Bram Moolenaar978287b2011-06-19 04:32:15 +02007438 /* Change NL to CR, so that it becomes a line break,
7439 * unless called from vim_regexec_nl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 * Skip over a backslashed character. */
Bram Moolenaar978287b2011-06-19 04:32:15 +02007441 if (*s == NL && !submatch_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 *s = CAR;
7443 else if (*s == '\\' && s[1] != NUL)
Bram Moolenaar06975a42010-03-23 16:27:22 +01007444 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 ++s;
Bram Moolenaar60190782010-05-21 13:08:58 +02007446 /* Change NL to CR here too, so that this works:
7447 * :s/abc\\\ndef/\="aaa\\\nbbb"/ on text:
7448 * abc\
7449 * def
Bram Moolenaar978287b2011-06-19 04:32:15 +02007450 * Not when called from vim_regexec_nl().
Bram Moolenaar60190782010-05-21 13:08:58 +02007451 */
Bram Moolenaar978287b2011-06-19 04:32:15 +02007452 if (*s == NL && !submatch_line_lbr)
Bram Moolenaar60190782010-05-21 13:08:58 +02007453 *s = CAR;
Bram Moolenaar06975a42010-03-23 16:27:22 +01007454 had_backslash = TRUE;
7455 }
7456 }
7457 if (had_backslash && backslash)
7458 {
7459 /* Backslashes will be consumed, need to double them. */
7460 s = vim_strsave_escaped(eval_result, (char_u *)"\\");
7461 if (s != NULL)
7462 {
7463 vim_free(eval_result);
7464 eval_result = s;
7465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 }
7467
7468 dst += STRLEN(eval_result);
7469 }
7470
7471 reg_match = submatch_match;
7472 reg_mmatch = submatch_mmatch;
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007473 reg_firstlnum = submatch_firstlnum;
7474 reg_maxline = submatch_maxline;
Bram Moolenaar978287b2011-06-19 04:32:15 +02007475 reg_line_lbr = submatch_line_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 reg_win = save_reg_win;
7477 ireg_ic = save_ireg_ic;
7478 can_f_submatch = FALSE;
7479 }
7480#endif
7481 }
7482 else
7483 while ((c = *src++) != NUL)
7484 {
7485 if (c == '&' && magic)
7486 no = 0;
7487 else if (c == '\\' && *src != NUL)
7488 {
7489 if (*src == '&' && !magic)
7490 {
7491 ++src;
7492 no = 0;
7493 }
7494 else if ('0' <= *src && *src <= '9')
7495 {
7496 no = *src++ - '0';
7497 }
7498 else if (vim_strchr((char_u *)"uUlLeE", *src))
7499 {
7500 switch (*src++)
7501 {
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007502 case 'u': func_one = (fptr_T)do_upper;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 continue;
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007504 case 'U': func_all = (fptr_T)do_Upper;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505 continue;
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007506 case 'l': func_one = (fptr_T)do_lower;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 continue;
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007508 case 'L': func_all = (fptr_T)do_Lower;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 continue;
7510 case 'e':
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007511 case 'E': func_one = func_all = (fptr_T)NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 continue;
7513 }
7514 }
7515 }
7516 if (no < 0) /* Ordinary character. */
7517 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00007518 if (c == K_SPECIAL && src[0] != NUL && src[1] != NUL)
7519 {
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00007520 /* Copy a special key as-is. */
Bram Moolenaardb552d602006-03-23 22:59:57 +00007521 if (copy)
7522 {
7523 *dst++ = c;
7524 *dst++ = *src++;
7525 *dst++ = *src++;
7526 }
7527 else
7528 {
7529 dst += 3;
7530 src += 2;
7531 }
7532 continue;
7533 }
7534
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 if (c == '\\' && *src != NUL)
7536 {
7537 /* Check for abbreviations -- webb */
7538 switch (*src)
7539 {
7540 case 'r': c = CAR; ++src; break;
7541 case 'n': c = NL; ++src; break;
7542 case 't': c = TAB; ++src; break;
7543 /* Oh no! \e already has meaning in subst pat :-( */
7544 /* case 'e': c = ESC; ++src; break; */
7545 case 'b': c = Ctrl_H; ++src; break;
7546
7547 /* If "backslash" is TRUE the backslash will be removed
7548 * later. Used to insert a literal CR. */
7549 default: if (backslash)
7550 {
7551 if (copy)
7552 *dst = '\\';
7553 ++dst;
7554 }
7555 c = *src++;
7556 }
7557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558#ifdef FEAT_MBYTE
Bram Moolenaardb552d602006-03-23 22:59:57 +00007559 else if (has_mbyte)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007560 c = mb_ptr2char(src - 1);
7561#endif
7562
Bram Moolenaardb552d602006-03-23 22:59:57 +00007563 /* Write to buffer, if copy is set. */
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007564 if (func_one != (fptr_T)NULL)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007565 /* Turbo C complains without the typecast */
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007566 func_one = (fptr_T)(func_one(&cc, c));
7567 else if (func_all != (fptr_T)NULL)
7568 /* Turbo C complains without the typecast */
7569 func_all = (fptr_T)(func_all(&cc, c));
7570 else /* just copy */
7571 cc = c;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007572
7573#ifdef FEAT_MBYTE
7574 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 {
Bram Moolenaar0c56c602010-07-12 22:42:33 +02007576 int totlen = mb_ptr2len(src - 1);
7577
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 if (copy)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007579 mb_char2bytes(cc, dst);
7580 dst += mb_char2len(cc) - 1;
Bram Moolenaar0c56c602010-07-12 22:42:33 +02007581 if (enc_utf8)
7582 {
7583 int clen = utf_ptr2len(src - 1);
7584
7585 /* If the character length is shorter than "totlen", there
7586 * are composing characters; copy them as-is. */
7587 if (clen < totlen)
7588 {
7589 if (copy)
7590 mch_memmove(dst + 1, src - 1 + clen,
7591 (size_t)(totlen - clen));
7592 dst += totlen - clen;
7593 }
7594 }
7595 src += totlen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 }
7597 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598#endif
7599 if (copy)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007600 *dst = cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 dst++;
7602 }
7603 else
7604 {
7605 if (REG_MULTI)
7606 {
7607 clnum = reg_mmatch->startpos[no].lnum;
7608 if (clnum < 0 || reg_mmatch->endpos[no].lnum < 0)
7609 s = NULL;
7610 else
7611 {
7612 s = reg_getline(clnum) + reg_mmatch->startpos[no].col;
7613 if (reg_mmatch->endpos[no].lnum == clnum)
7614 len = reg_mmatch->endpos[no].col
7615 - reg_mmatch->startpos[no].col;
7616 else
7617 len = (int)STRLEN(s);
7618 }
7619 }
7620 else
7621 {
7622 s = reg_match->startp[no];
7623 if (reg_match->endp[no] == NULL)
7624 s = NULL;
7625 else
7626 len = (int)(reg_match->endp[no] - s);
7627 }
7628 if (s != NULL)
7629 {
7630 for (;;)
7631 {
7632 if (len == 0)
7633 {
7634 if (REG_MULTI)
7635 {
7636 if (reg_mmatch->endpos[no].lnum == clnum)
7637 break;
7638 if (copy)
7639 *dst = CAR;
7640 ++dst;
7641 s = reg_getline(++clnum);
7642 if (reg_mmatch->endpos[no].lnum == clnum)
7643 len = reg_mmatch->endpos[no].col;
7644 else
7645 len = (int)STRLEN(s);
7646 }
7647 else
7648 break;
7649 }
7650 else if (*s == NUL) /* we hit NUL. */
7651 {
7652 if (copy)
7653 EMSG(_(e_re_damg));
7654 goto exit;
7655 }
7656 else
7657 {
7658 if (backslash && (*s == CAR || *s == '\\'))
7659 {
7660 /*
7661 * Insert a backslash in front of a CR, otherwise
7662 * it will be replaced by a line break.
7663 * Number of backslashes will be halved later,
7664 * double them here.
7665 */
7666 if (copy)
7667 {
7668 dst[0] = '\\';
7669 dst[1] = *s;
7670 }
7671 dst += 2;
7672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 else
7674 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007675#ifdef FEAT_MBYTE
7676 if (has_mbyte)
7677 c = mb_ptr2char(s);
7678 else
7679#endif
7680 c = *s;
7681
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007682 if (func_one != (fptr_T)NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 /* Turbo C complains without the typecast */
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007684 func_one = (fptr_T)(func_one(&cc, c));
7685 else if (func_all != (fptr_T)NULL)
7686 /* Turbo C complains without the typecast */
7687 func_all = (fptr_T)(func_all(&cc, c));
7688 else /* just copy */
7689 cc = c;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007690
7691#ifdef FEAT_MBYTE
7692 if (has_mbyte)
7693 {
Bram Moolenaar9225efb2007-07-30 20:32:53 +00007694 int l;
7695
7696 /* Copy composing characters separately, one
7697 * at a time. */
7698 if (enc_utf8)
7699 l = utf_ptr2len(s) - 1;
7700 else
7701 l = mb_ptr2len(s) - 1;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007702
7703 s += l;
7704 len -= l;
7705 if (copy)
7706 mb_char2bytes(cc, dst);
7707 dst += mb_char2len(cc) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007709 else
7710#endif
7711 if (copy)
7712 *dst = cc;
7713 dst++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007715
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 ++s;
7717 --len;
7718 }
7719 }
7720 }
7721 no = -1;
7722 }
7723 }
7724 if (copy)
7725 *dst = NUL;
7726
7727exit:
7728 return (int)((dst - dest) + 1);
7729}
7730
7731#ifdef FEAT_EVAL
Bram Moolenaard32a3192009-11-26 19:40:49 +00007732static char_u *reg_getline_submatch __ARGS((linenr_T lnum));
7733
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734/*
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007735 * Call reg_getline() with the line numbers from the submatch. If a
7736 * substitute() was used the reg_maxline and other values have been
7737 * overwritten.
7738 */
7739 static char_u *
7740reg_getline_submatch(lnum)
7741 linenr_T lnum;
7742{
7743 char_u *s;
7744 linenr_T save_first = reg_firstlnum;
7745 linenr_T save_max = reg_maxline;
7746
7747 reg_firstlnum = submatch_firstlnum;
7748 reg_maxline = submatch_maxline;
7749
7750 s = reg_getline(lnum);
7751
7752 reg_firstlnum = save_first;
7753 reg_maxline = save_max;
7754 return s;
7755}
7756
7757/*
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00007758 * Used for the submatch() function: get the string from the n'th submatch in
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 * allocated memory.
7760 * Returns NULL when not in a ":s" command and for a non-existing submatch.
7761 */
7762 char_u *
7763reg_submatch(no)
7764 int no;
7765{
7766 char_u *retval = NULL;
7767 char_u *s;
7768 int len;
7769 int round;
7770 linenr_T lnum;
7771
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007772 if (!can_f_submatch || no < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 return NULL;
7774
7775 if (submatch_match == NULL)
7776 {
7777 /*
7778 * First round: compute the length and allocate memory.
7779 * Second round: copy the text.
7780 */
7781 for (round = 1; round <= 2; ++round)
7782 {
7783 lnum = submatch_mmatch->startpos[no].lnum;
7784 if (lnum < 0 || submatch_mmatch->endpos[no].lnum < 0)
7785 return NULL;
7786
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007787 s = reg_getline_submatch(lnum) + submatch_mmatch->startpos[no].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 if (s == NULL) /* anti-crash check, cannot happen? */
7789 break;
7790 if (submatch_mmatch->endpos[no].lnum == lnum)
7791 {
7792 /* Within one line: take form start to end col. */
7793 len = submatch_mmatch->endpos[no].col
7794 - submatch_mmatch->startpos[no].col;
7795 if (round == 2)
Bram Moolenaarbbebc852005-07-18 21:47:53 +00007796 vim_strncpy(retval, s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 ++len;
7798 }
7799 else
7800 {
7801 /* Multiple lines: take start line from start col, middle
7802 * lines completely and end line up to end col. */
7803 len = (int)STRLEN(s);
7804 if (round == 2)
7805 {
7806 STRCPY(retval, s);
7807 retval[len] = '\n';
7808 }
7809 ++len;
7810 ++lnum;
7811 while (lnum < submatch_mmatch->endpos[no].lnum)
7812 {
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007813 s = reg_getline_submatch(lnum++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 if (round == 2)
7815 STRCPY(retval + len, s);
7816 len += (int)STRLEN(s);
7817 if (round == 2)
7818 retval[len] = '\n';
7819 ++len;
7820 }
7821 if (round == 2)
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007822 STRNCPY(retval + len, reg_getline_submatch(lnum),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 submatch_mmatch->endpos[no].col);
7824 len += submatch_mmatch->endpos[no].col;
7825 if (round == 2)
7826 retval[len] = NUL;
7827 ++len;
7828 }
7829
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007830 if (retval == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 {
7832 retval = lalloc((long_u)len, TRUE);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007833 if (retval == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 return NULL;
7835 }
7836 }
7837 }
7838 else
7839 {
Bram Moolenaar7670fa02009-02-21 21:04:20 +00007840 s = submatch_match->startp[no];
7841 if (s == NULL || submatch_match->endp[no] == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 retval = NULL;
7843 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 retval = vim_strnsave(s, (int)(submatch_match->endp[no] - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 }
7846
7847 return retval;
7848}
7849#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007850
7851static regengine_T bt_regengine =
7852{
7853 bt_regcomp,
7854 bt_regexec,
7855#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
7856 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
7857 bt_regexec_nl,
7858#endif
7859 bt_regexec_multi
7860#ifdef DEBUG
7861 ,(char_u *)""
7862#endif
7863};
7864
7865
7866#include "regexp_nfa.c"
7867
7868static regengine_T nfa_regengine =
7869{
7870 nfa_regcomp,
7871 nfa_regexec,
7872#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
7873 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
7874 nfa_regexec_nl,
7875#endif
7876 nfa_regexec_multi
7877#ifdef DEBUG
7878 ,(char_u *)""
7879#endif
7880};
7881
7882/* Which regexp engine to use? Needed for vim_regcomp().
7883 * Must match with 'regexpengine'. */
7884static int regexp_engine = 0;
7885#define AUTOMATIC_ENGINE 0
7886#define BACKTRACKING_ENGINE 1
7887#define NFA_ENGINE 2
7888#ifdef DEBUG
7889static char_u regname[][30] = {
7890 "AUTOMATIC Regexp Engine",
Bram Moolenaar75eb1612013-05-29 18:45:11 +02007891 "BACKTRACKING Regexp Engine",
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007892 "NFA Regexp Engine"
7893 };
7894#endif
7895
7896/*
7897 * Compile a regular expression into internal code.
7898 * Returns the program in allocated memory. Returns NULL for an error.
7899 */
7900 regprog_T *
7901vim_regcomp(expr_arg, re_flags)
7902 char_u *expr_arg;
7903 int re_flags;
7904{
7905 regprog_T *prog = NULL;
7906 char_u *expr = expr_arg;
7907
7908 syntax_error = FALSE;
7909 regexp_engine = p_re;
7910
7911 /* Check for prefix "\%#=", that sets the regexp engine */
7912 if (STRNCMP(expr, "\\%#=", 4) == 0)
7913 {
7914 int newengine = expr[4] - '0';
7915
7916 if (newengine == AUTOMATIC_ENGINE
7917 || newengine == BACKTRACKING_ENGINE
7918 || newengine == NFA_ENGINE)
7919 {
7920 regexp_engine = expr[4] - '0';
7921 expr += 5;
7922#ifdef DEBUG
7923 EMSG3("New regexp mode selected (%d): %s", regexp_engine,
7924 regname[newengine]);
7925#endif
7926 }
7927 else
7928 {
7929 EMSG(_("E864: \\%#= can only be followed by 0, 1, or 2. The automatic engine will be used "));
7930 regexp_engine = AUTOMATIC_ENGINE;
7931 }
7932 }
7933#ifdef DEBUG
7934 bt_regengine.expr = expr;
7935 nfa_regengine.expr = expr;
7936#endif
7937
7938 /*
7939 * First try the NFA engine, unless backtracking was requested.
7940 */
7941 if (regexp_engine != BACKTRACKING_ENGINE)
7942 prog = nfa_regengine.regcomp(expr, re_flags);
7943 else
7944 prog = bt_regengine.regcomp(expr, re_flags);
7945
7946 if (prog == NULL) /* error compiling regexp with initial engine */
7947 {
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02007948#ifdef BT_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007949 if (regexp_engine != BACKTRACKING_ENGINE) /* debugging log for NFA */
7950 {
7951 FILE *f;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02007952 f = fopen(BT_REGEXP_DEBUG_LOG_NAME, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007953 if (f)
7954 {
7955 if (!syntax_error)
7956 fprintf(f, "NFA engine could not handle \"%s\"\n", expr);
7957 else
7958 fprintf(f, "Syntax error in \"%s\"\n", expr);
7959 fclose(f);
7960 }
7961 else
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02007962 EMSG2("(NFA) Could not open \"%s\" to write !!!",
7963 BT_REGEXP_DEBUG_LOG_NAME);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007964 /*
7965 if (syntax_error)
7966 EMSG("NFA Regexp: Syntax Error !");
7967 */
7968 }
7969#endif
7970 /*
7971 * If NFA engine failed, then revert to the backtracking engine.
7972 * Except when there was a syntax error, which was properly handled by
7973 * NFA engine.
7974 */
7975 if (regexp_engine == AUTOMATIC_ENGINE)
7976 if (!syntax_error)
7977 prog = bt_regengine.regcomp(expr, re_flags);
7978
7979 } /* endif prog==NULL */
7980
7981
7982 return prog;
7983}
7984
7985/*
7986 * Match a regexp against a string.
7987 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
7988 * Uses curbuf for line count and 'iskeyword'.
7989 *
7990 * Return TRUE if there is a match, FALSE if not.
7991 */
7992 int
7993vim_regexec(rmp, line, col)
7994 regmatch_T *rmp;
7995 char_u *line; /* string to match against */
7996 colnr_T col; /* column to start looking for match */
7997{
7998 return rmp->regprog->engine->regexec(rmp, line, col);
7999}
8000
8001#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
8002 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
8003/*
8004 * Like vim_regexec(), but consider a "\n" in "line" to be a line break.
8005 */
8006 int
8007vim_regexec_nl(rmp, line, col)
8008 regmatch_T *rmp;
8009 char_u *line;
8010 colnr_T col;
8011{
8012 return rmp->regprog->engine->regexec_nl(rmp, line, col);
8013}
8014#endif
8015
8016/*
8017 * Match a regexp against multiple lines.
8018 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
8019 * Uses curbuf for line count and 'iskeyword'.
8020 *
8021 * Return zero if there is no match. Return number of lines contained in the
8022 * match otherwise.
8023 */
8024 long
8025vim_regexec_multi(rmp, win, buf, lnum, col, tm)
8026 regmmatch_T *rmp;
8027 win_T *win; /* window in which to search or NULL */
8028 buf_T *buf; /* buffer in which to search */
8029 linenr_T lnum; /* nr of line to start looking for match */
8030 colnr_T col; /* column to start looking for match */
8031 proftime_T *tm; /* timeout limit or NULL */
8032{
8033 return rmp->regprog->engine->regexec_multi(rmp, win, buf, lnum, col, tm);
8034}