blob: d18198c243be68630a508480467bbf9f9eaa1744 [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
41#include "vim.h"
42
43#undef DEBUG
44
45/*
46 * The "internal use only" fields in regexp.h are present to pass info from
47 * compile to execute that permits the execute phase to run lots faster on
48 * simple cases. They are:
49 *
50 * regstart char that must begin a match; NUL if none obvious; Can be a
51 * multi-byte character.
52 * reganch is the match anchored (at beginning-of-line only)?
53 * regmust string (pointer into program) that match must include, or NULL
54 * regmlen length of regmust string
55 * regflags RF_ values or'ed together
56 *
57 * Regstart and reganch permit very fast decisions on suitable starting points
58 * for a match, cutting down the work a lot. Regmust permits fast rejection
59 * of lines that cannot possibly match. The regmust tests are costly enough
60 * that vim_regcomp() supplies a regmust only if the r.e. contains something
61 * potentially expensive (at present, the only such thing detected is * or +
62 * at the start of the r.e., which can involve a lot of backup). Regmlen is
63 * supplied because the test in vim_regexec() needs it and vim_regcomp() is
64 * computing it anyway.
65 */
66
67/*
68 * Structure for regexp "program". This is essentially a linear encoding
69 * of a nondeterministic finite-state machine (aka syntax charts or
70 * "railroad normal form" in parsing technology). Each node is an opcode
71 * plus a "next" pointer, possibly plus an operand. "Next" pointers of
72 * all nodes except BRANCH and BRACES_COMPLEX implement concatenation; a "next"
73 * pointer with a BRANCH on both ends of it is connecting two alternatives.
74 * (Here we have one of the subtle syntax dependencies: an individual BRANCH
75 * (as opposed to a collection of them) is never concatenated with anything
76 * because of operator precedence). The "next" pointer of a BRACES_COMPLEX
Bram Moolenaardf177f62005-02-22 08:39:57 +000077 * node points to the node after the stuff to be repeated.
78 * The operand of some types of node is a literal string; for others, it is a
79 * node leading into a sub-FSM. In particular, the operand of a BRANCH node
80 * is the first node of the branch.
81 * (NB this is *not* a tree structure: the tail of the branch connects to the
82 * thing following the set of BRANCHes.)
Bram Moolenaar071d4272004-06-13 20:20:40 +000083 *
84 * pattern is coded like:
85 *
86 * +-----------------+
87 * | V
88 * <aa>\|<bb> BRANCH <aa> BRANCH <bb> --> END
89 * | ^ | ^
90 * +------+ +----------+
91 *
92 *
93 * +------------------+
94 * V |
95 * <aa>* BRANCH BRANCH <aa> --> BACK BRANCH --> NOTHING --> END
96 * | | ^ ^
97 * | +---------------+ |
98 * +---------------------------------------------+
99 *
100 *
Bram Moolenaardf177f62005-02-22 08:39:57 +0000101 * +----------------------+
102 * V |
Bram Moolenaar582fd852005-03-28 20:58:01 +0000103 * <aa>\+ BRANCH <aa> --> BRANCH --> BACK BRANCH --> NOTHING --> END
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000104 * | | ^ ^
105 * | +-----------+ |
106 * +--------------------------------------------------+
Bram Moolenaardf177f62005-02-22 08:39:57 +0000107 *
108 *
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109 * +-------------------------+
110 * V |
111 * <aa>\{} BRANCH BRACE_LIMITS --> BRACE_COMPLEX <aa> --> BACK END
112 * | | ^
113 * | +----------------+
114 * +-----------------------------------------------+
115 *
116 *
117 * <aa>\@!<bb> BRANCH NOMATCH <aa> --> END <bb> --> END
118 * | | ^ ^
119 * | +----------------+ |
120 * +--------------------------------+
121 *
122 * +---------+
123 * | V
124 * \z[abc] BRANCH BRANCH a BRANCH b BRANCH c BRANCH NOTHING --> END
125 * | | | | ^ ^
126 * | | | +-----+ |
127 * | | +----------------+ |
128 * | +---------------------------+ |
129 * +------------------------------------------------------+
130 *
131 * They all start with a BRANCH for "\|" alternaties, even when there is only
132 * one alternative.
133 */
134
135/*
136 * The opcodes are:
137 */
138
139/* definition number opnd? meaning */
140#define END 0 /* End of program or NOMATCH operand. */
141#define BOL 1 /* Match "" at beginning of line. */
142#define EOL 2 /* Match "" at end of line. */
143#define BRANCH 3 /* node Match this alternative, or the
144 * next... */
145#define BACK 4 /* Match "", "next" ptr points backward. */
146#define EXACTLY 5 /* str Match this string. */
147#define NOTHING 6 /* Match empty string. */
148#define STAR 7 /* node Match this (simple) thing 0 or more
149 * times. */
150#define PLUS 8 /* node Match this (simple) thing 1 or more
151 * times. */
152#define MATCH 9 /* node match the operand zero-width */
153#define NOMATCH 10 /* node check for no match with operand */
154#define BEHIND 11 /* node look behind for a match with operand */
155#define NOBEHIND 12 /* node look behind for no match with operand */
156#define SUBPAT 13 /* node match the operand here */
157#define BRACE_SIMPLE 14 /* node Match this (simple) thing between m and
158 * n times (\{m,n\}). */
159#define BOW 15 /* Match "" after [^a-zA-Z0-9_] */
160#define EOW 16 /* Match "" at [^a-zA-Z0-9_] */
161#define BRACE_LIMITS 17 /* nr nr define the min & max for BRACE_SIMPLE
162 * and BRACE_COMPLEX. */
163#define NEWL 18 /* Match line-break */
164#define BHPOS 19 /* End position for BEHIND or NOBEHIND */
165
166
167/* character classes: 20-48 normal, 50-78 include a line-break */
168#define ADD_NL 30
169#define FIRST_NL ANY + ADD_NL
170#define ANY 20 /* Match any one character. */
171#define ANYOF 21 /* str Match any character in this string. */
172#define ANYBUT 22 /* str Match any character not in this
173 * string. */
174#define IDENT 23 /* Match identifier char */
175#define SIDENT 24 /* Match identifier char but no digit */
176#define KWORD 25 /* Match keyword char */
177#define SKWORD 26 /* Match word char but no digit */
178#define FNAME 27 /* Match file name char */
179#define SFNAME 28 /* Match file name char but no digit */
180#define PRINT 29 /* Match printable char */
181#define SPRINT 30 /* Match printable char but no digit */
182#define WHITE 31 /* Match whitespace char */
183#define NWHITE 32 /* Match non-whitespace char */
184#define DIGIT 33 /* Match digit char */
185#define NDIGIT 34 /* Match non-digit char */
186#define HEX 35 /* Match hex char */
187#define NHEX 36 /* Match non-hex char */
188#define OCTAL 37 /* Match octal char */
189#define NOCTAL 38 /* Match non-octal char */
190#define WORD 39 /* Match word char */
191#define NWORD 40 /* Match non-word char */
192#define HEAD 41 /* Match head char */
193#define NHEAD 42 /* Match non-head char */
194#define ALPHA 43 /* Match alpha char */
195#define NALPHA 44 /* Match non-alpha char */
196#define LOWER 45 /* Match lowercase char */
197#define NLOWER 46 /* Match non-lowercase char */
198#define UPPER 47 /* Match uppercase char */
199#define NUPPER 48 /* Match non-uppercase char */
200#define LAST_NL NUPPER + ADD_NL
201#define WITH_NL(op) ((op) >= FIRST_NL && (op) <= LAST_NL)
202
203#define MOPEN 80 /* -89 Mark this point in input as start of
204 * \( subexpr. MOPEN + 0 marks start of
205 * match. */
206#define MCLOSE 90 /* -99 Analogous to MOPEN. MCLOSE + 0 marks
207 * end of match. */
208#define BACKREF 100 /* -109 node Match same string again \1-\9 */
209
210#ifdef FEAT_SYN_HL
211# define ZOPEN 110 /* -119 Mark this point in input as start of
212 * \z( subexpr. */
213# define ZCLOSE 120 /* -129 Analogous to ZOPEN. */
214# define ZREF 130 /* -139 node Match external submatch \z1-\z9 */
215#endif
216
217#define BRACE_COMPLEX 140 /* -149 node Match nodes between m & n times */
218
219#define NOPEN 150 /* Mark this point in input as start of
220 \%( subexpr. */
221#define NCLOSE 151 /* Analogous to NOPEN. */
222
223#define MULTIBYTECODE 200 /* mbc Match one multi-byte character */
224#define RE_BOF 201 /* Match "" at beginning of file. */
225#define RE_EOF 202 /* Match "" at end of file. */
226#define CURSOR 203 /* Match location of cursor. */
227
228#define RE_LNUM 204 /* nr cmp Match line number */
229#define RE_COL 205 /* nr cmp Match column number */
230#define RE_VCOL 206 /* nr cmp Match virtual column number */
231
232/*
233 * Magic characters have a special meaning, they don't match literally.
234 * Magic characters are negative. This separates them from literal characters
235 * (possibly multi-byte). Only ASCII characters can be Magic.
236 */
237#define Magic(x) ((int)(x) - 256)
238#define un_Magic(x) ((x) + 256)
239#define is_Magic(x) ((x) < 0)
240
241static int no_Magic __ARGS((int x));
242static int toggle_Magic __ARGS((int x));
243
244 static int
245no_Magic(x)
246 int x;
247{
248 if (is_Magic(x))
249 return un_Magic(x);
250 return x;
251}
252
253 static int
254toggle_Magic(x)
255 int x;
256{
257 if (is_Magic(x))
258 return un_Magic(x);
259 return Magic(x);
260}
261
262/*
263 * The first byte of the regexp internal "program" is actually this magic
264 * number; the start node begins in the second byte. It's used to catch the
265 * most severe mutilation of the program by the caller.
266 */
267
268#define REGMAGIC 0234
269
270/*
271 * Opcode notes:
272 *
273 * BRANCH The set of branches constituting a single choice are hooked
274 * together with their "next" pointers, since precedence prevents
275 * anything being concatenated to any individual branch. The
276 * "next" pointer of the last BRANCH in a choice points to the
277 * thing following the whole choice. This is also where the
278 * final "next" pointer of each individual branch points; each
279 * branch starts with the operand node of a BRANCH node.
280 *
281 * BACK Normal "next" pointers all implicitly point forward; BACK
282 * exists to make loop structures possible.
283 *
284 * STAR,PLUS '=', and complex '*' and '+', are implemented as circular
285 * BRANCH structures using BACK. Simple cases (one character
286 * per match) are implemented with STAR and PLUS for speed
287 * and to minimize recursive plunges.
288 *
289 * BRACE_LIMITS This is always followed by a BRACE_SIMPLE or BRACE_COMPLEX
290 * node, and defines the min and max limits to be used for that
291 * node.
292 *
293 * MOPEN,MCLOSE ...are numbered at compile time.
294 * ZOPEN,ZCLOSE ...ditto
295 */
296
297/*
298 * A node is one char of opcode followed by two chars of "next" pointer.
299 * "Next" pointers are stored as two 8-bit bytes, high order first. The
300 * value is a positive offset from the opcode of the node containing it.
301 * An operand, if any, simply follows the node. (Note that much of the
302 * code generation knows about this implicit relationship.)
303 *
304 * Using two bytes for the "next" pointer is vast overkill for most things,
305 * but allows patterns to get big without disasters.
306 */
307#define OP(p) ((int)*(p))
308#define NEXT(p) (((*((p) + 1) & 0377) << 8) + (*((p) + 2) & 0377))
309#define OPERAND(p) ((p) + 3)
310/* Obtain an operand that was stored as four bytes, MSB first. */
311#define OPERAND_MIN(p) (((long)(p)[3] << 24) + ((long)(p)[4] << 16) \
312 + ((long)(p)[5] << 8) + (long)(p)[6])
313/* Obtain a second operand stored as four bytes. */
314#define OPERAND_MAX(p) OPERAND_MIN((p) + 4)
315/* Obtain a second single-byte operand stored after a four bytes operand. */
316#define OPERAND_CMP(p) (p)[7]
317
318/*
319 * Utility definitions.
320 */
321#define UCHARAT(p) ((int)*(char_u *)(p))
322
323/* Used for an error (down from) vim_regcomp(): give the error message, set
324 * rc_did_emsg and return NULL */
325#define EMSG_RET_NULL(m) { EMSG(m); rc_did_emsg = TRUE; return NULL; }
326#define EMSG_M_RET_NULL(m, c) { EMSG2(m, c ? "" : "\\"); rc_did_emsg = TRUE; return NULL; }
327#define EMSG_RET_FAIL(m) { EMSG(m); rc_did_emsg = TRUE; return FAIL; }
328#define EMSG_ONE_RET_NULL EMSG_M_RET_NULL(_("E369: invalid item in %s%%[]"), reg_magic == MAGIC_ALL)
329
330#define MAX_LIMIT (32767L << 16L)
331
332static int re_multi_type __ARGS((int));
333static int cstrncmp __ARGS((char_u *s1, char_u *s2, int *n));
334static char_u *cstrchr __ARGS((char_u *, int));
335
336#ifdef DEBUG
337static void regdump __ARGS((char_u *, regprog_T *));
338static char_u *regprop __ARGS((char_u *));
339#endif
340
341#define NOT_MULTI 0
342#define MULTI_ONE 1
343#define MULTI_MULT 2
344/*
345 * Return NOT_MULTI if c is not a "multi" operator.
346 * Return MULTI_ONE if c is a single "multi" operator.
347 * Return MULTI_MULT if c is a multi "multi" operator.
348 */
349 static int
350re_multi_type(c)
351 int c;
352{
353 if (c == Magic('@') || c == Magic('=') || c == Magic('?'))
354 return MULTI_ONE;
355 if (c == Magic('*') || c == Magic('+') || c == Magic('{'))
356 return MULTI_MULT;
357 return NOT_MULTI;
358}
359
360/*
361 * Flags to be passed up and down.
362 */
363#define HASWIDTH 0x1 /* Known never to match null string. */
364#define SIMPLE 0x2 /* Simple enough to be STAR/PLUS operand. */
365#define SPSTART 0x4 /* Starts with * or +. */
366#define HASNL 0x8 /* Contains some \n. */
367#define HASLOOKBH 0x10 /* Contains "\@<=" or "\@<!". */
368#define WORST 0 /* Worst case. */
369
370/*
371 * When regcode is set to this value, code is not emitted and size is computed
372 * instead.
373 */
374#define JUST_CALC_SIZE ((char_u *) -1)
375
376static char_u *reg_prev_sub;
377
378/*
379 * REGEXP_INRANGE contains all characters which are always special in a []
380 * range after '\'.
381 * REGEXP_ABBR contains all characters which act as abbreviations after '\'.
382 * These are:
383 * \n - New line (NL).
384 * \r - Carriage Return (CR).
385 * \t - Tab (TAB).
386 * \e - Escape (ESC).
387 * \b - Backspace (Ctrl_H).
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000388 * \d - Character code in decimal, eg \d123
389 * \o - Character code in octal, eg \o80
390 * \x - Character code in hex, eg \x4a
391 * \u - Multibyte character code, eg \u20ac
392 * \U - Long multibyte character code, eg \U12345678
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 */
394static char_u REGEXP_INRANGE[] = "]^-n\\";
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000395static char_u REGEXP_ABBR[] = "nrtebdoxuU";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396
397static int backslash_trans __ARGS((int c));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000398static int get_char_class __ARGS((char_u **pp));
399static int get_equi_class __ARGS((char_u **pp));
400static void reg_equi_class __ARGS((int c));
401static int get_coll_element __ARGS((char_u **pp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402static char_u *skip_anyof __ARGS((char_u *p));
403static void init_class_tab __ARGS((void));
404
405/*
406 * Translate '\x' to its control character, except "\n", which is Magic.
407 */
408 static int
409backslash_trans(c)
410 int c;
411{
412 switch (c)
413 {
414 case 'r': return CAR;
415 case 't': return TAB;
416 case 'e': return ESC;
417 case 'b': return BS;
418 }
419 return c;
420}
421
422/*
Bram Moolenaardf177f62005-02-22 08:39:57 +0000423 * Check for a character class name "[:name:]". "pp" points to the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 * Returns one of the CLASS_ items. CLASS_NONE means that no item was
425 * recognized. Otherwise "pp" is advanced to after the item.
426 */
427 static int
Bram Moolenaardf177f62005-02-22 08:39:57 +0000428get_char_class(pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429 char_u **pp;
430{
431 static const char *(class_names[]) =
432 {
433 "alnum:]",
434#define CLASS_ALNUM 0
435 "alpha:]",
436#define CLASS_ALPHA 1
437 "blank:]",
438#define CLASS_BLANK 2
439 "cntrl:]",
440#define CLASS_CNTRL 3
441 "digit:]",
442#define CLASS_DIGIT 4
443 "graph:]",
444#define CLASS_GRAPH 5
445 "lower:]",
446#define CLASS_LOWER 6
447 "print:]",
448#define CLASS_PRINT 7
449 "punct:]",
450#define CLASS_PUNCT 8
451 "space:]",
452#define CLASS_SPACE 9
453 "upper:]",
454#define CLASS_UPPER 10
455 "xdigit:]",
456#define CLASS_XDIGIT 11
457 "tab:]",
458#define CLASS_TAB 12
459 "return:]",
460#define CLASS_RETURN 13
461 "backspace:]",
462#define CLASS_BACKSPACE 14
463 "escape:]",
464#define CLASS_ESCAPE 15
465 };
466#define CLASS_NONE 99
467 int i;
468
469 if ((*pp)[1] == ':')
470 {
471 for (i = 0; i < sizeof(class_names) / sizeof(*class_names); ++i)
472 if (STRNCMP(*pp + 2, class_names[i], STRLEN(class_names[i])) == 0)
473 {
474 *pp += STRLEN(class_names[i]) + 2;
475 return i;
476 }
477 }
478 return CLASS_NONE;
479}
480
481/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 * Specific version of character class functions.
483 * Using a table to keep this fast.
484 */
485static short class_tab[256];
486
487#define RI_DIGIT 0x01
488#define RI_HEX 0x02
489#define RI_OCTAL 0x04
490#define RI_WORD 0x08
491#define RI_HEAD 0x10
492#define RI_ALPHA 0x20
493#define RI_LOWER 0x40
494#define RI_UPPER 0x80
495#define RI_WHITE 0x100
496
497 static void
498init_class_tab()
499{
500 int i;
501 static int done = FALSE;
502
503 if (done)
504 return;
505
506 for (i = 0; i < 256; ++i)
507 {
508 if (i >= '0' && i <= '7')
509 class_tab[i] = RI_DIGIT + RI_HEX + RI_OCTAL + RI_WORD;
510 else if (i >= '8' && i <= '9')
511 class_tab[i] = RI_DIGIT + RI_HEX + RI_WORD;
512 else if (i >= 'a' && i <= 'f')
513 class_tab[i] = RI_HEX + RI_WORD + RI_HEAD + RI_ALPHA + RI_LOWER;
514#ifdef EBCDIC
515 else if ((i >= 'g' && i <= 'i') || (i >= 'j' && i <= 'r')
516 || (i >= 's' && i <= 'z'))
517#else
518 else if (i >= 'g' && i <= 'z')
519#endif
520 class_tab[i] = RI_WORD + RI_HEAD + RI_ALPHA + RI_LOWER;
521 else if (i >= 'A' && i <= 'F')
522 class_tab[i] = RI_HEX + RI_WORD + RI_HEAD + RI_ALPHA + RI_UPPER;
523#ifdef EBCDIC
524 else if ((i >= 'G' && i <= 'I') || ( i >= 'J' && i <= 'R')
525 || (i >= 'S' && i <= 'Z'))
526#else
527 else if (i >= 'G' && i <= 'Z')
528#endif
529 class_tab[i] = RI_WORD + RI_HEAD + RI_ALPHA + RI_UPPER;
530 else if (i == '_')
531 class_tab[i] = RI_WORD + RI_HEAD;
532 else
533 class_tab[i] = 0;
534 }
535 class_tab[' '] |= RI_WHITE;
536 class_tab['\t'] |= RI_WHITE;
537 done = TRUE;
538}
539
540#ifdef FEAT_MBYTE
541# define ri_digit(c) (c < 0x100 && (class_tab[c] & RI_DIGIT))
542# define ri_hex(c) (c < 0x100 && (class_tab[c] & RI_HEX))
543# define ri_octal(c) (c < 0x100 && (class_tab[c] & RI_OCTAL))
544# define ri_word(c) (c < 0x100 && (class_tab[c] & RI_WORD))
545# define ri_head(c) (c < 0x100 && (class_tab[c] & RI_HEAD))
546# define ri_alpha(c) (c < 0x100 && (class_tab[c] & RI_ALPHA))
547# define ri_lower(c) (c < 0x100 && (class_tab[c] & RI_LOWER))
548# define ri_upper(c) (c < 0x100 && (class_tab[c] & RI_UPPER))
549# define ri_white(c) (c < 0x100 && (class_tab[c] & RI_WHITE))
550#else
551# define ri_digit(c) (class_tab[c] & RI_DIGIT)
552# define ri_hex(c) (class_tab[c] & RI_HEX)
553# define ri_octal(c) (class_tab[c] & RI_OCTAL)
554# define ri_word(c) (class_tab[c] & RI_WORD)
555# define ri_head(c) (class_tab[c] & RI_HEAD)
556# define ri_alpha(c) (class_tab[c] & RI_ALPHA)
557# define ri_lower(c) (class_tab[c] & RI_LOWER)
558# define ri_upper(c) (class_tab[c] & RI_UPPER)
559# define ri_white(c) (class_tab[c] & RI_WHITE)
560#endif
561
562/* flags for regflags */
563#define RF_ICASE 1 /* ignore case */
564#define RF_NOICASE 2 /* don't ignore case */
565#define RF_HASNL 4 /* can match a NL */
566#define RF_ICOMBINE 8 /* ignore combining characters */
567#define RF_LOOKBH 16 /* uses "\@<=" or "\@<!" */
568
569/*
570 * Global work variables for vim_regcomp().
571 */
572
573static char_u *regparse; /* Input-scan pointer. */
574static int prevchr_len; /* byte length of previous char */
575static int num_complex_braces; /* Complex \{...} count */
576static int regnpar; /* () count. */
577#ifdef FEAT_SYN_HL
578static int regnzpar; /* \z() count. */
579static int re_has_z; /* \z item detected */
580#endif
581static char_u *regcode; /* Code-emit pointer, or JUST_CALC_SIZE */
582static long regsize; /* Code size. */
583static char_u had_endbrace[NSUBEXP]; /* flags, TRUE if end of () found */
584static unsigned regflags; /* RF_ flags for prog */
585static long brace_min[10]; /* Minimums for complex brace repeats */
586static long brace_max[10]; /* Maximums for complex brace repeats */
587static int brace_count[10]; /* Current counts for complex brace repeats */
588#if defined(FEAT_SYN_HL) || defined(PROTO)
589static int had_eol; /* TRUE when EOL found by vim_regcomp() */
590#endif
591static int one_exactly = FALSE; /* only do one char for EXACTLY */
592
593static int reg_magic; /* magicness of the pattern: */
594#define MAGIC_NONE 1 /* "\V" very unmagic */
595#define MAGIC_OFF 2 /* "\M" or 'magic' off */
596#define MAGIC_ON 3 /* "\m" or 'magic' */
597#define MAGIC_ALL 4 /* "\v" very magic */
598
599static int reg_string; /* matching with a string instead of a buffer
600 line */
601
602/*
603 * META contains all characters that may be magic, except '^' and '$'.
604 */
605
606#ifdef EBCDIC
607static char_u META[] = "%&()*+.123456789<=>?@ACDFHIKLMOPSUVWX[_acdfhiklmnopsuvwxz{|~";
608#else
609/* META[] is used often enough to justify turning it into a table. */
610static char_u META_flags[] = {
611 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
612 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
613/* % & ( ) * + . */
614 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0,
615/* 1 2 3 4 5 6 7 8 9 < = > ? */
616 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1,
617/* @ A C D F H I K L M O */
618 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1,
619/* P S U V W X Z [ _ */
620 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1,
621/* a c d f h i k l m n o */
622 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1,
623/* p s u v w x z { | ~ */
624 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1
625};
626#endif
627
628static int curchr;
629
630/* arguments for reg() */
631#define REG_NOPAREN 0 /* toplevel reg() */
632#define REG_PAREN 1 /* \(\) */
633#define REG_ZPAREN 2 /* \z(\) */
634#define REG_NPAREN 3 /* \%(\) */
635
636/*
637 * Forward declarations for vim_regcomp()'s friends.
638 */
639static void initchr __ARGS((char_u *));
640static int getchr __ARGS((void));
641static void skipchr_keepstart __ARGS((void));
642static int peekchr __ARGS((void));
643static void skipchr __ARGS((void));
644static void ungetchr __ARGS((void));
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000645static int gethexchrs __ARGS((int maxinputlen));
646static int getoctchrs __ARGS((void));
647static int getdecchrs __ARGS((void));
648static int coll_get_char __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649static void regcomp_start __ARGS((char_u *expr, int flags));
650static char_u *reg __ARGS((int, int *));
651static char_u *regbranch __ARGS((int *flagp));
652static char_u *regconcat __ARGS((int *flagp));
653static char_u *regpiece __ARGS((int *));
654static char_u *regatom __ARGS((int *));
655static char_u *regnode __ARGS((int));
656static int prog_magic_wrong __ARGS((void));
657static char_u *regnext __ARGS((char_u *));
658static void regc __ARGS((int b));
659#ifdef FEAT_MBYTE
660static void regmbc __ARGS((int c));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000661#else
662# define regmbc(c) regc(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663#endif
664static void reginsert __ARGS((int, char_u *));
665static void reginsert_limits __ARGS((int, long, long, char_u *));
666static char_u *re_put_long __ARGS((char_u *pr, long_u val));
667static int read_limits __ARGS((long *, long *));
668static void regtail __ARGS((char_u *, char_u *));
669static void regoptail __ARGS((char_u *, char_u *));
670
671/*
672 * Return TRUE if compiled regular expression "prog" can match a line break.
673 */
674 int
675re_multiline(prog)
676 regprog_T *prog;
677{
678 return (prog->regflags & RF_HASNL);
679}
680
681/*
682 * Return TRUE if compiled regular expression "prog" looks before the start
683 * position (pattern contains "\@<=" or "\@<!").
684 */
685 int
686re_lookbehind(prog)
687 regprog_T *prog;
688{
689 return (prog->regflags & RF_LOOKBH);
690}
691
692/*
Bram Moolenaardf177f62005-02-22 08:39:57 +0000693 * Check for an equivalence class name "[=a=]". "pp" points to the '['.
694 * Returns a character representing the class. Zero means that no item was
695 * recognized. Otherwise "pp" is advanced to after the item.
696 */
697 static int
698get_equi_class(pp)
699 char_u **pp;
700{
701 int c;
702 int l = 1;
703 char_u *p = *pp;
704
705 if (p[1] == '=')
706 {
707#ifdef FEAT_MBYTE
708 if (has_mbyte)
709 l = mb_ptr2len_check(p + 2);
710#endif
711 if (p[l + 2] == '=' && p[l + 3] == ']')
712 {
713#ifdef FEAT_MBYTE
714 if (has_mbyte)
715 c = mb_ptr2char(p + 2);
716 else
717#endif
718 c = p[2];
719 *pp += l + 4;
720 return c;
721 }
722 }
723 return 0;
724}
725
726/*
727 * Produce the bytes for equivalence class "c".
728 * Currently only handles latin1, latin9 and utf-8.
729 */
730 static void
731reg_equi_class(c)
732 int c;
733{
734#ifdef FEAT_MBYTE
735 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
736 || STRCMP(p_enc, "latin9") == 0)
737#endif
738 {
739 switch (c)
740 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000741 case 'A': case '\300': case '\301': case '\302':
742 case '\303': case '\304': case '\305':
743 regmbc('A'); regmbc('\300'); regmbc('\301');
744 regmbc('\302'); regmbc('\303'); regmbc('\304');
745 regmbc('\305');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000746 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000747 case 'C': case '\307':
748 regmbc('C'); regmbc('\307');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000749 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000750 case 'E': case '\310': case '\311': case '\312': case '\313':
751 regmbc('E'); regmbc('\310'); regmbc('\311');
752 regmbc('\312'); regmbc('\313');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000753 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000754 case 'I': case '\314': case '\315': case '\316': case '\317':
755 regmbc('I'); regmbc('\314'); regmbc('\315');
756 regmbc('\316'); regmbc('\317');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000757 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000758 case 'N': case '\321':
759 regmbc('N'); regmbc('\321');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000760 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000761 case 'O': case '\322': case '\323': case '\324': case '\325':
762 case '\326':
763 regmbc('O'); regmbc('\322'); regmbc('\323');
764 regmbc('\324'); regmbc('\325'); regmbc('\326');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000765 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000766 case 'U': case '\331': case '\332': case '\333': case '\334':
767 regmbc('U'); regmbc('\331'); regmbc('\332');
768 regmbc('\333'); regmbc('\334');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000769 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000770 case 'Y': case '\335':
771 regmbc('Y'); regmbc('\335');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000772 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000773 case 'a': case '\340': case '\341': case '\342':
774 case '\343': case '\344': case '\345':
775 regmbc('a'); regmbc('\340'); regmbc('\341');
776 regmbc('\342'); regmbc('\343'); regmbc('\344');
777 regmbc('\345');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000778 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000779 case 'c': case '\347':
780 regmbc('c'); regmbc('\347');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000781 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000782 case 'e': case '\350': case '\351': case '\352': case '\353':
783 regmbc('e'); regmbc('\350'); regmbc('\351');
784 regmbc('\352'); regmbc('\353');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000785 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000786 case 'i': case '\354': case '\355': case '\356': case '\357':
787 regmbc('i'); regmbc('\354'); regmbc('\355');
788 regmbc('\356'); regmbc('\357');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000789 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000790 case 'n': case '\361':
791 regmbc('n'); regmbc('\361');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000792 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000793 case 'o': case '\362': case '\363': case '\364': case '\365':
794 case '\366':
795 regmbc('o'); regmbc('\362'); regmbc('\363');
796 regmbc('\364'); regmbc('\365'); regmbc('\366');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000797 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000798 case 'u': case '\371': case '\372': case '\373': case '\374':
799 regmbc('u'); regmbc('\371'); regmbc('\372');
800 regmbc('\373'); regmbc('\374');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000801 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000802 case 'y': case '\375': case '\377':
803 regmbc('y'); regmbc('\375'); regmbc('\377');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000804 return;
805 }
806 }
807 regmbc(c);
808}
809
810/*
811 * Check for a collating element "[.a.]". "pp" points to the '['.
812 * Returns a character. Zero means that no item was recognized. Otherwise
813 * "pp" is advanced to after the item.
814 * Currently only single characters are recognized!
815 */
816 static int
817get_coll_element(pp)
818 char_u **pp;
819{
820 int c;
821 int l = 1;
822 char_u *p = *pp;
823
824 if (p[1] == '.')
825 {
826#ifdef FEAT_MBYTE
827 if (has_mbyte)
828 l = mb_ptr2len_check(p + 2);
829#endif
830 if (p[l + 2] == '.' && p[l + 3] == ']')
831 {
832#ifdef FEAT_MBYTE
833 if (has_mbyte)
834 c = mb_ptr2char(p + 2);
835 else
836#endif
837 c = p[2];
838 *pp += l + 4;
839 return c;
840 }
841 }
842 return 0;
843}
844
845
846/*
847 * Skip over a "[]" range.
848 * "p" must point to the character after the '['.
849 * The returned pointer is on the matching ']', or the terminating NUL.
850 */
851 static char_u *
852skip_anyof(p)
853 char_u *p;
854{
855 int cpo_lit; /* 'cpoptions' contains 'l' flag */
856 int cpo_bsl; /* 'cpoptions' contains '\' flag */
857#ifdef FEAT_MBYTE
858 int l;
859#endif
860
861 cpo_lit = (!reg_syn && vim_strchr(p_cpo, CPO_LITERAL) != NULL);
862 cpo_bsl = (!reg_syn && vim_strchr(p_cpo, CPO_BACKSL) != NULL);
863
864 if (*p == '^') /* Complement of range. */
865 ++p;
866 if (*p == ']' || *p == '-')
867 ++p;
868 while (*p != NUL && *p != ']')
869 {
870#ifdef FEAT_MBYTE
871 if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
872 p += l;
873 else
874#endif
875 if (*p == '-')
876 {
877 ++p;
878 if (*p != ']' && *p != NUL)
879 mb_ptr_adv(p);
880 }
881 else if (*p == '\\'
882 && !cpo_bsl
883 && (vim_strchr(REGEXP_INRANGE, p[1]) != NULL
884 || (!cpo_lit && vim_strchr(REGEXP_ABBR, p[1]) != NULL)))
885 p += 2;
886 else if (*p == '[')
887 {
888 if (get_char_class(&p) == CLASS_NONE
889 && get_equi_class(&p) == 0
890 && get_coll_element(&p) == 0)
891 ++p; /* It was not a class name */
892 }
893 else
894 ++p;
895 }
896
897 return p;
898}
899
900/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 * Skip past regular expression.
Bram Moolenaar748bf032005-02-02 23:04:36 +0000902 * Stop at end of "startp" or where "dirc" is found ('/', '?', etc).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 * Take care of characters with a backslash in front of it.
904 * Skip strings inside [ and ].
905 * When "newp" is not NULL and "dirc" is '?', make an allocated copy of the
906 * expression and change "\?" to "?". If "*newp" is not NULL the expression
907 * is changed in-place.
908 */
909 char_u *
910skip_regexp(startp, dirc, magic, newp)
911 char_u *startp;
912 int dirc;
913 int magic;
914 char_u **newp;
915{
916 int mymagic;
917 char_u *p = startp;
918
919 if (magic)
920 mymagic = MAGIC_ON;
921 else
922 mymagic = MAGIC_OFF;
923
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000924 for (; p[0] != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 {
926 if (p[0] == dirc) /* found end of regexp */
927 break;
928 if ((p[0] == '[' && mymagic >= MAGIC_ON)
929 || (p[0] == '\\' && p[1] == '[' && mymagic <= MAGIC_OFF))
930 {
931 p = skip_anyof(p + 1);
932 if (p[0] == NUL)
933 break;
934 }
935 else if (p[0] == '\\' && p[1] != NUL)
936 {
937 if (dirc == '?' && newp != NULL && p[1] == '?')
938 {
939 /* change "\?" to "?", make a copy first. */
940 if (*newp == NULL)
941 {
942 *newp = vim_strsave(startp);
943 if (*newp != NULL)
944 p = *newp + (p - startp);
945 }
946 if (*newp != NULL)
947 mch_memmove(p, p + 1, STRLEN(p));
948 else
949 ++p;
950 }
951 else
952 ++p; /* skip next character */
953 if (*p == 'v')
954 mymagic = MAGIC_ALL;
955 else if (*p == 'V')
956 mymagic = MAGIC_NONE;
957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 }
959 return p;
960}
961
962/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000963 * vim_regcomp() - compile a regular expression into internal code
964 * Returns the program in allocated space. Returns NULL for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 *
966 * We can't allocate space until we know how big the compiled form will be,
967 * but we can't compile it (and thus know how big it is) until we've got a
968 * place to put the code. So we cheat: we compile it twice, once with code
969 * generation turned off and size counting turned on, and once "for real".
970 * This also means that we don't allocate space until we are sure that the
971 * thing really will compile successfully, and we never have to move the
972 * code and thus invalidate pointers into it. (Note that it has to be in
973 * one piece because vim_free() must be able to free it all.)
974 *
975 * Whether upper/lower case is to be ignored is decided when executing the
976 * program, it does not matter here.
977 *
978 * Beware that the optimization-preparation code in here knows about some
979 * of the structure of the compiled regexp.
980 * "re_flags": RE_MAGIC and/or RE_STRING.
981 */
982 regprog_T *
983vim_regcomp(expr, re_flags)
984 char_u *expr;
985 int re_flags;
986{
987 regprog_T *r;
988 char_u *scan;
989 char_u *longest;
990 int len;
991 int flags;
992
993 if (expr == NULL)
994 EMSG_RET_NULL(_(e_null));
995
996 init_class_tab();
997
998 /*
999 * First pass: determine size, legality.
1000 */
1001 regcomp_start(expr, re_flags);
1002 regcode = JUST_CALC_SIZE;
1003 regc(REGMAGIC);
1004 if (reg(REG_NOPAREN, &flags) == NULL)
1005 return NULL;
1006
1007 /* Small enough for pointer-storage convention? */
1008#ifdef SMALL_MALLOC /* 16 bit storage allocation */
1009 if (regsize >= 65536L - 256L)
1010 EMSG_RET_NULL(_("E339: Pattern too long"));
1011#endif
1012
1013 /* Allocate space. */
1014 r = (regprog_T *)lalloc(sizeof(regprog_T) + regsize, TRUE);
1015 if (r == NULL)
1016 return NULL;
1017
1018 /*
1019 * Second pass: emit code.
1020 */
1021 regcomp_start(expr, re_flags);
1022 regcode = r->program;
1023 regc(REGMAGIC);
1024 if (reg(REG_NOPAREN, &flags) == NULL)
1025 {
1026 vim_free(r);
1027 return NULL;
1028 }
1029
1030 /* Dig out information for optimizations. */
1031 r->regstart = NUL; /* Worst-case defaults. */
1032 r->reganch = 0;
1033 r->regmust = NULL;
1034 r->regmlen = 0;
1035 r->regflags = regflags;
1036 if (flags & HASNL)
1037 r->regflags |= RF_HASNL;
1038 if (flags & HASLOOKBH)
1039 r->regflags |= RF_LOOKBH;
1040#ifdef FEAT_SYN_HL
1041 /* Remember whether this pattern has any \z specials in it. */
1042 r->reghasz = re_has_z;
1043#endif
1044 scan = r->program + 1; /* First BRANCH. */
1045 if (OP(regnext(scan)) == END) /* Only one top-level choice. */
1046 {
1047 scan = OPERAND(scan);
1048
1049 /* Starting-point info. */
1050 if (OP(scan) == BOL || OP(scan) == RE_BOF)
1051 {
1052 r->reganch++;
1053 scan = regnext(scan);
1054 }
1055
1056 if (OP(scan) == EXACTLY)
1057 {
1058#ifdef FEAT_MBYTE
1059 if (has_mbyte)
1060 r->regstart = (*mb_ptr2char)(OPERAND(scan));
1061 else
1062#endif
1063 r->regstart = *OPERAND(scan);
1064 }
1065 else if ((OP(scan) == BOW
1066 || OP(scan) == EOW
1067 || OP(scan) == NOTHING
1068 || OP(scan) == MOPEN + 0 || OP(scan) == NOPEN
1069 || OP(scan) == MCLOSE + 0 || OP(scan) == NCLOSE)
1070 && OP(regnext(scan)) == EXACTLY)
1071 {
1072#ifdef FEAT_MBYTE
1073 if (has_mbyte)
1074 r->regstart = (*mb_ptr2char)(OPERAND(regnext(scan)));
1075 else
1076#endif
1077 r->regstart = *OPERAND(regnext(scan));
1078 }
1079
1080 /*
1081 * If there's something expensive in the r.e., find the longest
1082 * literal string that must appear and make it the regmust. Resolve
1083 * ties in favor of later strings, since the regstart check works
1084 * with the beginning of the r.e. and avoiding duplication
1085 * strengthens checking. Not a strong reason, but sufficient in the
1086 * absence of others.
1087 */
1088 /*
1089 * When the r.e. starts with BOW, it is faster to look for a regmust
1090 * first. Used a lot for "#" and "*" commands. (Added by mool).
1091 */
1092 if ((flags & SPSTART || OP(scan) == BOW || OP(scan) == EOW)
1093 && !(flags & HASNL))
1094 {
1095 longest = NULL;
1096 len = 0;
1097 for (; scan != NULL; scan = regnext(scan))
1098 if (OP(scan) == EXACTLY && STRLEN(OPERAND(scan)) >= (size_t)len)
1099 {
1100 longest = OPERAND(scan);
1101 len = (int)STRLEN(OPERAND(scan));
1102 }
1103 r->regmust = longest;
1104 r->regmlen = len;
1105 }
1106 }
1107#ifdef DEBUG
1108 regdump(expr, r);
1109#endif
1110 return r;
1111}
1112
1113/*
1114 * Setup to parse the regexp. Used once to get the length and once to do it.
1115 */
1116 static void
1117regcomp_start(expr, re_flags)
1118 char_u *expr;
1119 int re_flags; /* see vim_regcomp() */
1120{
1121 initchr(expr);
1122 if (re_flags & RE_MAGIC)
1123 reg_magic = MAGIC_ON;
1124 else
1125 reg_magic = MAGIC_OFF;
1126 reg_string = (re_flags & RE_STRING);
1127
1128 num_complex_braces = 0;
1129 regnpar = 1;
1130 vim_memset(had_endbrace, 0, sizeof(had_endbrace));
1131#ifdef FEAT_SYN_HL
1132 regnzpar = 1;
1133 re_has_z = 0;
1134#endif
1135 regsize = 0L;
1136 regflags = 0;
1137#if defined(FEAT_SYN_HL) || defined(PROTO)
1138 had_eol = FALSE;
1139#endif
1140}
1141
1142#if defined(FEAT_SYN_HL) || defined(PROTO)
1143/*
1144 * Check if during the previous call to vim_regcomp the EOL item "$" has been
1145 * found. This is messy, but it works fine.
1146 */
1147 int
1148vim_regcomp_had_eol()
1149{
1150 return had_eol;
1151}
1152#endif
1153
1154/*
1155 * reg - regular expression, i.e. main body or parenthesized thing
1156 *
1157 * Caller must absorb opening parenthesis.
1158 *
1159 * Combining parenthesis handling with the base level of regular expression
1160 * is a trifle forced, but the need to tie the tails of the branches to what
1161 * follows makes it hard to avoid.
1162 */
1163 static char_u *
1164reg(paren, flagp)
1165 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1166 int *flagp;
1167{
1168 char_u *ret;
1169 char_u *br;
1170 char_u *ender;
1171 int parno = 0;
1172 int flags;
1173
1174 *flagp = HASWIDTH; /* Tentatively. */
1175
1176#ifdef FEAT_SYN_HL
1177 if (paren == REG_ZPAREN)
1178 {
1179 /* Make a ZOPEN node. */
1180 if (regnzpar >= NSUBEXP)
1181 EMSG_RET_NULL(_("E50: Too many \\z("));
1182 parno = regnzpar;
1183 regnzpar++;
1184 ret = regnode(ZOPEN + parno);
1185 }
1186 else
1187#endif
1188 if (paren == REG_PAREN)
1189 {
1190 /* Make a MOPEN node. */
1191 if (regnpar >= NSUBEXP)
1192 EMSG_M_RET_NULL(_("E51: Too many %s("), reg_magic == MAGIC_ALL);
1193 parno = regnpar;
1194 ++regnpar;
1195 ret = regnode(MOPEN + parno);
1196 }
1197 else if (paren == REG_NPAREN)
1198 {
1199 /* Make a NOPEN node. */
1200 ret = regnode(NOPEN);
1201 }
1202 else
1203 ret = NULL;
1204
1205 /* Pick up the branches, linking them together. */
1206 br = regbranch(&flags);
1207 if (br == NULL)
1208 return NULL;
1209 if (ret != NULL)
1210 regtail(ret, br); /* [MZ]OPEN -> first. */
1211 else
1212 ret = br;
1213 /* If one of the branches can be zero-width, the whole thing can.
1214 * If one of the branches has * at start or matches a line-break, the
1215 * whole thing can. */
1216 if (!(flags & HASWIDTH))
1217 *flagp &= ~HASWIDTH;
1218 *flagp |= flags & (SPSTART | HASNL | HASLOOKBH);
1219 while (peekchr() == Magic('|'))
1220 {
1221 skipchr();
1222 br = regbranch(&flags);
1223 if (br == NULL)
1224 return NULL;
1225 regtail(ret, br); /* BRANCH -> BRANCH. */
1226 if (!(flags & HASWIDTH))
1227 *flagp &= ~HASWIDTH;
1228 *flagp |= flags & (SPSTART | HASNL | HASLOOKBH);
1229 }
1230
1231 /* Make a closing node, and hook it on the end. */
1232 ender = regnode(
1233#ifdef FEAT_SYN_HL
1234 paren == REG_ZPAREN ? ZCLOSE + parno :
1235#endif
1236 paren == REG_PAREN ? MCLOSE + parno :
1237 paren == REG_NPAREN ? NCLOSE : END);
1238 regtail(ret, ender);
1239
1240 /* Hook the tails of the branches to the closing node. */
1241 for (br = ret; br != NULL; br = regnext(br))
1242 regoptail(br, ender);
1243
1244 /* Check for proper termination. */
1245 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1246 {
1247#ifdef FEAT_SYN_HL
1248 if (paren == REG_ZPAREN)
1249 EMSG_RET_NULL(_("E52: Unmatched \\z("))
1250 else
1251#endif
1252 if (paren == REG_NPAREN)
1253 EMSG_M_RET_NULL(_("E53: Unmatched %s%%("), reg_magic == MAGIC_ALL)
1254 else
1255 EMSG_M_RET_NULL(_("E54: Unmatched %s("), reg_magic == MAGIC_ALL)
1256 }
1257 else if (paren == REG_NOPAREN && peekchr() != NUL)
1258 {
1259 if (curchr == Magic(')'))
1260 EMSG_M_RET_NULL(_("E55: Unmatched %s)"), reg_magic == MAGIC_ALL)
1261 else
1262 EMSG_RET_NULL(_(e_trailing)) /* "Can't happen". */
1263 /* NOTREACHED */
1264 }
1265 /*
1266 * Here we set the flag allowing back references to this set of
1267 * parentheses.
1268 */
1269 if (paren == REG_PAREN)
1270 had_endbrace[parno] = TRUE; /* have seen the close paren */
1271 return ret;
1272}
1273
1274/*
1275 * regbranch - one alternative of an | operator
1276 *
1277 * Implements the & operator.
1278 */
1279 static char_u *
1280regbranch(flagp)
1281 int *flagp;
1282{
1283 char_u *ret;
1284 char_u *chain = NULL;
1285 char_u *latest;
1286 int flags;
1287
1288 *flagp = WORST | HASNL; /* Tentatively. */
1289
1290 ret = regnode(BRANCH);
1291 for (;;)
1292 {
1293 latest = regconcat(&flags);
1294 if (latest == NULL)
1295 return NULL;
1296 /* If one of the branches has width, the whole thing has. If one of
1297 * the branches anchors at start-of-line, the whole thing does.
1298 * If one of the branches uses look-behind, the whole thing does. */
1299 *flagp |= flags & (HASWIDTH | SPSTART | HASLOOKBH);
1300 /* If one of the branches doesn't match a line-break, the whole thing
1301 * doesn't. */
1302 *flagp &= ~HASNL | (flags & HASNL);
1303 if (chain != NULL)
1304 regtail(chain, latest);
1305 if (peekchr() != Magic('&'))
1306 break;
1307 skipchr();
1308 regtail(latest, regnode(END)); /* operand ends */
1309 reginsert(MATCH, latest);
1310 chain = latest;
1311 }
1312
1313 return ret;
1314}
1315
1316/*
1317 * regbranch - one alternative of an | or & operator
1318 *
1319 * Implements the concatenation operator.
1320 */
1321 static char_u *
1322regconcat(flagp)
1323 int *flagp;
1324{
1325 char_u *first = NULL;
1326 char_u *chain = NULL;
1327 char_u *latest;
1328 int flags;
1329 int cont = TRUE;
1330
1331 *flagp = WORST; /* Tentatively. */
1332
1333 while (cont)
1334 {
1335 switch (peekchr())
1336 {
1337 case NUL:
1338 case Magic('|'):
1339 case Magic('&'):
1340 case Magic(')'):
1341 cont = FALSE;
1342 break;
1343 case Magic('Z'):
1344#ifdef FEAT_MBYTE
1345 regflags |= RF_ICOMBINE;
1346#endif
1347 skipchr_keepstart();
1348 break;
1349 case Magic('c'):
1350 regflags |= RF_ICASE;
1351 skipchr_keepstart();
1352 break;
1353 case Magic('C'):
1354 regflags |= RF_NOICASE;
1355 skipchr_keepstart();
1356 break;
1357 case Magic('v'):
1358 reg_magic = MAGIC_ALL;
1359 skipchr_keepstart();
1360 curchr = -1;
1361 break;
1362 case Magic('m'):
1363 reg_magic = MAGIC_ON;
1364 skipchr_keepstart();
1365 curchr = -1;
1366 break;
1367 case Magic('M'):
1368 reg_magic = MAGIC_OFF;
1369 skipchr_keepstart();
1370 curchr = -1;
1371 break;
1372 case Magic('V'):
1373 reg_magic = MAGIC_NONE;
1374 skipchr_keepstart();
1375 curchr = -1;
1376 break;
1377 default:
1378 latest = regpiece(&flags);
1379 if (latest == NULL)
1380 return NULL;
1381 *flagp |= flags & (HASWIDTH | HASNL | HASLOOKBH);
1382 if (chain == NULL) /* First piece. */
1383 *flagp |= flags & SPSTART;
1384 else
1385 regtail(chain, latest);
1386 chain = latest;
1387 if (first == NULL)
1388 first = latest;
1389 break;
1390 }
1391 }
1392 if (first == NULL) /* Loop ran zero times. */
1393 first = regnode(NOTHING);
1394 return first;
1395}
1396
1397/*
1398 * regpiece - something followed by possible [*+=]
1399 *
1400 * Note that the branching code sequences used for = and the general cases
1401 * of * and + are somewhat optimized: they use the same NOTHING node as
1402 * both the endmarker for their branch list and the body of the last branch.
1403 * It might seem that this node could be dispensed with entirely, but the
1404 * endmarker role is not redundant.
1405 */
1406 static char_u *
1407regpiece(flagp)
1408 int *flagp;
1409{
1410 char_u *ret;
1411 int op;
1412 char_u *next;
1413 int flags;
1414 long minval;
1415 long maxval;
1416
1417 ret = regatom(&flags);
1418 if (ret == NULL)
1419 return NULL;
1420
1421 op = peekchr();
1422 if (re_multi_type(op) == NOT_MULTI)
1423 {
1424 *flagp = flags;
1425 return ret;
1426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 /* default flags */
1428 *flagp = (WORST | SPSTART | (flags & (HASNL | HASLOOKBH)));
1429
1430 skipchr();
1431 switch (op)
1432 {
1433 case Magic('*'):
1434 if (flags & SIMPLE)
1435 reginsert(STAR, ret);
1436 else
1437 {
1438 /* Emit x* as (x&|), where & means "self". */
1439 reginsert(BRANCH, ret); /* Either x */
1440 regoptail(ret, regnode(BACK)); /* and loop */
1441 regoptail(ret, ret); /* back */
1442 regtail(ret, regnode(BRANCH)); /* or */
1443 regtail(ret, regnode(NOTHING)); /* null. */
1444 }
1445 break;
1446
1447 case Magic('+'):
1448 if (flags & SIMPLE)
1449 reginsert(PLUS, ret);
1450 else
1451 {
1452 /* Emit x+ as x(&|), where & means "self". */
1453 next = regnode(BRANCH); /* Either */
1454 regtail(ret, next);
Bram Moolenaar582fd852005-03-28 20:58:01 +00001455 regtail(regnode(BACK), ret); /* loop back */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 regtail(next, regnode(BRANCH)); /* or */
1457 regtail(ret, regnode(NOTHING)); /* null. */
1458 }
1459 *flagp = (WORST | HASWIDTH | (flags & (HASNL | HASLOOKBH)));
1460 break;
1461
1462 case Magic('@'):
1463 {
1464 int lop = END;
1465
1466 switch (no_Magic(getchr()))
1467 {
1468 case '=': lop = MATCH; break; /* \@= */
1469 case '!': lop = NOMATCH; break; /* \@! */
1470 case '>': lop = SUBPAT; break; /* \@> */
1471 case '<': switch (no_Magic(getchr()))
1472 {
1473 case '=': lop = BEHIND; break; /* \@<= */
1474 case '!': lop = NOBEHIND; break; /* \@<! */
1475 }
1476 }
1477 if (lop == END)
1478 EMSG_M_RET_NULL(_("E59: invalid character after %s@"),
1479 reg_magic == MAGIC_ALL);
1480 /* Look behind must match with behind_pos. */
1481 if (lop == BEHIND || lop == NOBEHIND)
1482 {
1483 regtail(ret, regnode(BHPOS));
1484 *flagp |= HASLOOKBH;
1485 }
1486 regtail(ret, regnode(END)); /* operand ends */
1487 reginsert(lop, ret);
1488 break;
1489 }
1490
1491 case Magic('?'):
1492 case Magic('='):
1493 /* Emit x= as (x|) */
1494 reginsert(BRANCH, ret); /* Either x */
1495 regtail(ret, regnode(BRANCH)); /* or */
1496 next = regnode(NOTHING); /* null. */
1497 regtail(ret, next);
1498 regoptail(ret, next);
1499 break;
1500
1501 case Magic('{'):
1502 if (!read_limits(&minval, &maxval))
1503 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 if (flags & SIMPLE)
1505 {
1506 reginsert(BRACE_SIMPLE, ret);
1507 reginsert_limits(BRACE_LIMITS, minval, maxval, ret);
1508 }
1509 else
1510 {
1511 if (num_complex_braces >= 10)
1512 EMSG_M_RET_NULL(_("E60: Too many complex %s{...}s"),
1513 reg_magic == MAGIC_ALL);
1514 reginsert(BRACE_COMPLEX + num_complex_braces, ret);
1515 regoptail(ret, regnode(BACK));
1516 regoptail(ret, ret);
1517 reginsert_limits(BRACE_LIMITS, minval, maxval, ret);
1518 ++num_complex_braces;
1519 }
1520 if (minval > 0 && maxval > 0)
1521 *flagp = (HASWIDTH | (flags & (HASNL | HASLOOKBH)));
1522 break;
1523 }
1524 if (re_multi_type(peekchr()) != NOT_MULTI)
1525 {
1526 /* Can't have a multi follow a multi. */
1527 if (peekchr() == Magic('*'))
1528 sprintf((char *)IObuff, _("E61: Nested %s*"),
1529 reg_magic >= MAGIC_ON ? "" : "\\");
1530 else
1531 sprintf((char *)IObuff, _("E62: Nested %s%c"),
1532 reg_magic == MAGIC_ALL ? "" : "\\", no_Magic(peekchr()));
1533 EMSG_RET_NULL(IObuff);
1534 }
1535
1536 return ret;
1537}
1538
1539/*
1540 * regatom - the lowest level
1541 *
1542 * Optimization: gobbles an entire sequence of ordinary characters so that
1543 * it can turn them into a single node, which is smaller to store and
1544 * faster to run. Don't do this when one_exactly is set.
1545 */
1546 static char_u *
1547regatom(flagp)
1548 int *flagp;
1549{
1550 char_u *ret;
1551 int flags;
1552 int cpo_lit; /* 'cpoptions' contains 'l' flag */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001553 int cpo_bsl; /* 'cpoptions' contains '\' flag */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 int c;
1555 static char_u *classchars = (char_u *)".iIkKfFpPsSdDxXoOwWhHaAlLuU";
1556 static int classcodes[] = {ANY, IDENT, SIDENT, KWORD, SKWORD,
1557 FNAME, SFNAME, PRINT, SPRINT,
1558 WHITE, NWHITE, DIGIT, NDIGIT,
1559 HEX, NHEX, OCTAL, NOCTAL,
1560 WORD, NWORD, HEAD, NHEAD,
1561 ALPHA, NALPHA, LOWER, NLOWER,
1562 UPPER, NUPPER
1563 };
1564 char_u *p;
1565 int extra = 0;
1566
1567 *flagp = WORST; /* Tentatively. */
1568 cpo_lit = (!reg_syn && vim_strchr(p_cpo, CPO_LITERAL) != NULL);
Bram Moolenaardf177f62005-02-22 08:39:57 +00001569 cpo_bsl = (!reg_syn && vim_strchr(p_cpo, CPO_BACKSL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570
1571 c = getchr();
1572 switch (c)
1573 {
1574 case Magic('^'):
1575 ret = regnode(BOL);
1576 break;
1577
1578 case Magic('$'):
1579 ret = regnode(EOL);
1580#if defined(FEAT_SYN_HL) || defined(PROTO)
1581 had_eol = TRUE;
1582#endif
1583 break;
1584
1585 case Magic('<'):
1586 ret = regnode(BOW);
1587 break;
1588
1589 case Magic('>'):
1590 ret = regnode(EOW);
1591 break;
1592
1593 case Magic('_'):
1594 c = no_Magic(getchr());
1595 if (c == '^') /* "\_^" is start-of-line */
1596 {
1597 ret = regnode(BOL);
1598 break;
1599 }
1600 if (c == '$') /* "\_$" is end-of-line */
1601 {
1602 ret = regnode(EOL);
1603#if defined(FEAT_SYN_HL) || defined(PROTO)
1604 had_eol = TRUE;
1605#endif
1606 break;
1607 }
1608
1609 extra = ADD_NL;
1610 *flagp |= HASNL;
1611
1612 /* "\_[" is character range plus newline */
1613 if (c == '[')
1614 goto collection;
1615
1616 /* "\_x" is character class plus newline */
1617 /*FALLTHROUGH*/
1618
1619 /*
1620 * Character classes.
1621 */
1622 case Magic('.'):
1623 case Magic('i'):
1624 case Magic('I'):
1625 case Magic('k'):
1626 case Magic('K'):
1627 case Magic('f'):
1628 case Magic('F'):
1629 case Magic('p'):
1630 case Magic('P'):
1631 case Magic('s'):
1632 case Magic('S'):
1633 case Magic('d'):
1634 case Magic('D'):
1635 case Magic('x'):
1636 case Magic('X'):
1637 case Magic('o'):
1638 case Magic('O'):
1639 case Magic('w'):
1640 case Magic('W'):
1641 case Magic('h'):
1642 case Magic('H'):
1643 case Magic('a'):
1644 case Magic('A'):
1645 case Magic('l'):
1646 case Magic('L'):
1647 case Magic('u'):
1648 case Magic('U'):
1649 p = vim_strchr(classchars, no_Magic(c));
1650 if (p == NULL)
1651 EMSG_RET_NULL(_("E63: invalid use of \\_"));
1652 ret = regnode(classcodes[p - classchars] + extra);
1653 *flagp |= HASWIDTH | SIMPLE;
1654 break;
1655
1656 case Magic('n'):
1657 if (reg_string)
1658 {
1659 /* In a string "\n" matches a newline character. */
1660 ret = regnode(EXACTLY);
1661 regc(NL);
1662 regc(NUL);
1663 *flagp |= HASWIDTH | SIMPLE;
1664 }
1665 else
1666 {
1667 /* In buffer text "\n" matches the end of a line. */
1668 ret = regnode(NEWL);
1669 *flagp |= HASWIDTH | HASNL;
1670 }
1671 break;
1672
1673 case Magic('('):
1674 if (one_exactly)
1675 EMSG_ONE_RET_NULL;
1676 ret = reg(REG_PAREN, &flags);
1677 if (ret == NULL)
1678 return NULL;
1679 *flagp |= flags & (HASWIDTH | SPSTART | HASNL | HASLOOKBH);
1680 break;
1681
1682 case NUL:
1683 case Magic('|'):
1684 case Magic('&'):
1685 case Magic(')'):
1686 EMSG_RET_NULL(_(e_internal)); /* Supposed to be caught earlier. */
1687 /* NOTREACHED */
1688
1689 case Magic('='):
1690 case Magic('?'):
1691 case Magic('+'):
1692 case Magic('@'):
1693 case Magic('{'):
1694 case Magic('*'):
1695 c = no_Magic(c);
1696 sprintf((char *)IObuff, _("E64: %s%c follows nothing"),
1697 (c == '*' ? reg_magic >= MAGIC_ON : reg_magic == MAGIC_ALL)
1698 ? "" : "\\", c);
1699 EMSG_RET_NULL(IObuff);
1700 /* NOTREACHED */
1701
1702 case Magic('~'): /* previous substitute pattern */
1703 if (reg_prev_sub)
1704 {
1705 char_u *lp;
1706
1707 ret = regnode(EXACTLY);
1708 lp = reg_prev_sub;
1709 while (*lp != NUL)
1710 regc(*lp++);
1711 regc(NUL);
1712 if (*reg_prev_sub != NUL)
1713 {
1714 *flagp |= HASWIDTH;
1715 if ((lp - reg_prev_sub) == 1)
1716 *flagp |= SIMPLE;
1717 }
1718 }
1719 else
1720 EMSG_RET_NULL(_(e_nopresub));
1721 break;
1722
1723 case Magic('1'):
1724 case Magic('2'):
1725 case Magic('3'):
1726 case Magic('4'):
1727 case Magic('5'):
1728 case Magic('6'):
1729 case Magic('7'):
1730 case Magic('8'):
1731 case Magic('9'):
1732 {
1733 int refnum;
1734
1735 refnum = c - Magic('0');
1736 /*
1737 * Check if the back reference is legal. We must have seen the
1738 * close brace.
1739 * TODO: Should also check that we don't refer to something
1740 * that is repeated (+*=): what instance of the repetition
1741 * should we match?
1742 */
1743 if (!had_endbrace[refnum])
1744 {
1745 /* Trick: check if "@<=" or "@<!" follows, in which case
1746 * the \1 can appear before the referenced match. */
1747 for (p = regparse; *p != NUL; ++p)
1748 if (p[0] == '@' && p[1] == '<'
1749 && (p[2] == '!' || p[2] == '='))
1750 break;
1751 if (*p == NUL)
1752 EMSG_RET_NULL(_("E65: Illegal back reference"));
1753 }
1754 ret = regnode(BACKREF + refnum);
1755 }
1756 break;
1757
1758#ifdef FEAT_SYN_HL
1759 case Magic('z'):
1760 {
1761 c = no_Magic(getchr());
1762 switch (c)
1763 {
1764 case '(': if (reg_do_extmatch != REX_SET)
1765 EMSG_RET_NULL(_("E66: \\z( not allowed here"));
1766 if (one_exactly)
1767 EMSG_ONE_RET_NULL;
1768 ret = reg(REG_ZPAREN, &flags);
1769 if (ret == NULL)
1770 return NULL;
1771 *flagp |= flags & (HASWIDTH|SPSTART|HASNL|HASLOOKBH);
1772 re_has_z = REX_SET;
1773 break;
1774
1775 case '1':
1776 case '2':
1777 case '3':
1778 case '4':
1779 case '5':
1780 case '6':
1781 case '7':
1782 case '8':
1783 case '9': if (reg_do_extmatch != REX_USE)
1784 EMSG_RET_NULL(_("E67: \\z1 et al. not allowed here"));
1785 ret = regnode(ZREF + c - '0');
1786 re_has_z = REX_USE;
1787 break;
1788
1789 case 's': ret = regnode(MOPEN + 0);
1790 break;
1791
1792 case 'e': ret = regnode(MCLOSE + 0);
1793 break;
1794
1795 default: EMSG_RET_NULL(_("E68: Invalid character after \\z"));
1796 }
1797 }
1798 break;
1799#endif
1800
1801 case Magic('%'):
1802 {
1803 c = no_Magic(getchr());
1804 switch (c)
1805 {
1806 /* () without a back reference */
1807 case '(':
1808 if (one_exactly)
1809 EMSG_ONE_RET_NULL;
1810 ret = reg(REG_NPAREN, &flags);
1811 if (ret == NULL)
1812 return NULL;
1813 *flagp |= flags & (HASWIDTH | SPSTART | HASNL | HASLOOKBH);
1814 break;
1815
1816 /* Catch \%^ and \%$ regardless of where they appear in the
1817 * pattern -- regardless of whether or not it makes sense. */
1818 case '^':
1819 ret = regnode(RE_BOF);
1820 break;
1821
1822 case '$':
1823 ret = regnode(RE_EOF);
1824 break;
1825
1826 case '#':
1827 ret = regnode(CURSOR);
1828 break;
1829
1830 /* \%[abc]: Emit as a list of branches, all ending at the last
1831 * branch which matches nothing. */
1832 case '[':
1833 if (one_exactly) /* doesn't nest */
1834 EMSG_ONE_RET_NULL;
1835 {
1836 char_u *lastbranch;
1837 char_u *lastnode = NULL;
1838 char_u *br;
1839
1840 ret = NULL;
1841 while ((c = getchr()) != ']')
1842 {
1843 if (c == NUL)
1844 EMSG_M_RET_NULL(_("E69: Missing ] after %s%%["),
1845 reg_magic == MAGIC_ALL);
1846 br = regnode(BRANCH);
1847 if (ret == NULL)
1848 ret = br;
1849 else
1850 regtail(lastnode, br);
1851
1852 ungetchr();
1853 one_exactly = TRUE;
1854 lastnode = regatom(flagp);
1855 one_exactly = FALSE;
1856 if (lastnode == NULL)
1857 return NULL;
1858 }
1859 if (ret == NULL)
1860 EMSG_M_RET_NULL(_("E70: Empty %s%%[]"),
1861 reg_magic == MAGIC_ALL);
1862 lastbranch = regnode(BRANCH);
1863 br = regnode(NOTHING);
1864 if (ret != JUST_CALC_SIZE)
1865 {
1866 regtail(lastnode, br);
1867 regtail(lastbranch, br);
1868 /* connect all branches to the NOTHING
1869 * branch at the end */
1870 for (br = ret; br != lastnode; )
1871 {
1872 if (OP(br) == BRANCH)
1873 {
1874 regtail(br, lastbranch);
1875 br = OPERAND(br);
1876 }
1877 else
1878 br = regnext(br);
1879 }
1880 }
1881 *flagp &= ~HASWIDTH;
1882 break;
1883 }
1884
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001885 case 'd': /* %d123 decimal */
1886 case 'o': /* %o123 octal */
1887 case 'x': /* %xab hex 2 */
1888 case 'u': /* %uabcd hex 4 */
1889 case 'U': /* %U1234abcd hex 8 */
1890 {
1891 int i;
1892
1893 switch (c)
1894 {
1895 case 'd': i = getdecchrs(); break;
1896 case 'o': i = getoctchrs(); break;
1897 case 'x': i = gethexchrs(2); break;
1898 case 'u': i = gethexchrs(4); break;
1899 case 'U': i = gethexchrs(8); break;
1900 default: i = -1; break;
1901 }
1902
1903 if (i < 0)
1904 EMSG_M_RET_NULL(
1905 _("E678: Invalid character after %s%%[dxouU]"),
1906 reg_magic == MAGIC_ALL);
1907 ret = regnode(EXACTLY);
1908 if (i == 0)
1909 regc(0x0a);
1910 else
1911#ifdef FEAT_MBYTE
1912 regmbc(i);
1913#else
1914 regc(i);
1915#endif
1916 regc(NUL);
1917 *flagp |= HASWIDTH;
1918 break;
1919 }
1920
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 default:
1922 if (VIM_ISDIGIT(c) || c == '<' || c == '>')
1923 {
1924 long_u n = 0;
1925 int cmp;
1926
1927 cmp = c;
1928 if (cmp == '<' || cmp == '>')
1929 c = getchr();
1930 while (VIM_ISDIGIT(c))
1931 {
1932 n = n * 10 + (c - '0');
1933 c = getchr();
1934 }
1935 if (c == 'l' || c == 'c' || c == 'v')
1936 {
1937 if (c == 'l')
1938 ret = regnode(RE_LNUM);
1939 else if (c == 'c')
1940 ret = regnode(RE_COL);
1941 else
1942 ret = regnode(RE_VCOL);
1943 if (ret == JUST_CALC_SIZE)
1944 regsize += 5;
1945 else
1946 {
1947 /* put the number and the optional
1948 * comparator after the opcode */
1949 regcode = re_put_long(regcode, n);
1950 *regcode++ = cmp;
1951 }
1952 break;
1953 }
1954 }
1955
1956 EMSG_M_RET_NULL(_("E71: Invalid character after %s%%"),
1957 reg_magic == MAGIC_ALL);
1958 }
1959 }
1960 break;
1961
1962 case Magic('['):
1963collection:
1964 {
1965 char_u *lp;
1966
1967 /*
1968 * If there is no matching ']', we assume the '[' is a normal
1969 * character. This makes 'incsearch' and ":help [" work.
1970 */
1971 lp = skip_anyof(regparse);
1972 if (*lp == ']') /* there is a matching ']' */
1973 {
1974 int startc = -1; /* > 0 when next '-' is a range */
1975 int endc;
1976
1977 /*
1978 * In a character class, different parsing rules apply.
1979 * Not even \ is special anymore, nothing is.
1980 */
1981 if (*regparse == '^') /* Complement of range. */
1982 {
1983 ret = regnode(ANYBUT + extra);
1984 regparse++;
1985 }
1986 else
1987 ret = regnode(ANYOF + extra);
1988
1989 /* At the start ']' and '-' mean the literal character. */
1990 if (*regparse == ']' || *regparse == '-')
Bram Moolenaardf177f62005-02-22 08:39:57 +00001991 {
1992 startc = *regparse;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 regc(*regparse++);
Bram Moolenaardf177f62005-02-22 08:39:57 +00001994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995
1996 while (*regparse != NUL && *regparse != ']')
1997 {
1998 if (*regparse == '-')
1999 {
2000 ++regparse;
2001 /* The '-' is not used for a range at the end and
2002 * after or before a '\n'. */
2003 if (*regparse == ']' || *regparse == NUL
2004 || startc == -1
2005 || (regparse[0] == '\\' && regparse[1] == 'n'))
2006 {
2007 regc('-');
2008 startc = '-'; /* [--x] is a range */
2009 }
2010 else
2011 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00002012 /* Also accept "a-[.z.]" */
2013 endc = 0;
2014 if (*regparse == '[')
2015 endc = get_coll_element(&regparse);
2016 if (endc == 0)
2017 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018#ifdef FEAT_MBYTE
Bram Moolenaardf177f62005-02-22 08:39:57 +00002019 if (has_mbyte)
2020 endc = mb_ptr2char_adv(&regparse);
2021 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00002023 endc = *regparse++;
2024 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002025
2026 /* Handle \o40, \x20 and \u20AC style sequences */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002027 if (endc == '\\' && !cpo_lit && !cpo_bsl)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002028 endc = coll_get_char();
2029
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 if (startc > endc)
2031 EMSG_RET_NULL(_(e_invrange));
2032#ifdef FEAT_MBYTE
2033 if (has_mbyte && ((*mb_char2len)(startc) > 1
2034 || (*mb_char2len)(endc) > 1))
2035 {
2036 /* Limit to a range of 256 chars */
2037 if (endc > startc + 256)
2038 EMSG_RET_NULL(_(e_invrange));
2039 while (++startc <= endc)
2040 regmbc(startc);
2041 }
2042 else
2043#endif
2044 {
2045#ifdef EBCDIC
2046 int alpha_only = FALSE;
2047
2048 /* for alphabetical range skip the gaps
2049 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
2050 if (isalpha(startc) && isalpha(endc))
2051 alpha_only = TRUE;
2052#endif
2053 while (++startc <= endc)
2054#ifdef EBCDIC
2055 if (!alpha_only || isalpha(startc))
2056#endif
2057 regc(startc);
2058 }
2059 startc = -1;
2060 }
2061 }
2062 /*
2063 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
2064 * accepts "\t", "\e", etc., but only when the 'l' flag in
2065 * 'cpoptions' is not included.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002066 * Posix doesn't recognize backslash at all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 */
2068 else if (*regparse == '\\'
Bram Moolenaardf177f62005-02-22 08:39:57 +00002069 && !cpo_bsl
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
2071 || (!cpo_lit
2072 && vim_strchr(REGEXP_ABBR,
2073 regparse[1]) != NULL)))
2074 {
2075 regparse++;
2076 if (*regparse == 'n')
2077 {
2078 /* '\n' in range: also match NL */
2079 if (ret != JUST_CALC_SIZE)
2080 {
2081 if (*ret == ANYBUT)
2082 *ret = ANYBUT + ADD_NL;
2083 else if (*ret == ANYOF)
2084 *ret = ANYOF + ADD_NL;
2085 /* else: must have had a \n already */
2086 }
2087 *flagp |= HASNL;
2088 regparse++;
2089 startc = -1;
2090 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002091 else if (*regparse == 'd'
2092 || *regparse == 'o'
2093 || *regparse == 'x'
2094 || *regparse == 'u'
2095 || *regparse == 'U')
2096 {
2097 startc = coll_get_char();
2098 if (startc == 0)
2099 regc(0x0a);
2100 else
2101#ifdef FEAT_MBYTE
2102 regmbc(startc);
2103#else
2104 regc(startc);
2105#endif
2106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 else
2108 {
2109 startc = backslash_trans(*regparse++);
2110 regc(startc);
2111 }
2112 }
2113 else if (*regparse == '[')
2114 {
2115 int c_class;
2116 int cu;
2117
Bram Moolenaardf177f62005-02-22 08:39:57 +00002118 c_class = get_char_class(&regparse);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 startc = -1;
2120 /* Characters assumed to be 8 bits! */
2121 switch (c_class)
2122 {
2123 case CLASS_NONE:
Bram Moolenaardf177f62005-02-22 08:39:57 +00002124 c_class = get_equi_class(&regparse);
2125 if (c_class != 0)
2126 {
2127 /* produce equivalence class */
2128 reg_equi_class(c_class);
2129 }
2130 else if ((c_class =
2131 get_coll_element(&regparse)) != 0)
2132 {
2133 /* produce a collating element */
2134 regmbc(c_class);
2135 }
2136 else
2137 {
2138 /* literal '[', allow [[-x] as a range */
2139 startc = *regparse++;
2140 regc(startc);
2141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 break;
2143 case CLASS_ALNUM:
2144 for (cu = 1; cu <= 255; cu++)
2145 if (isalnum(cu))
2146 regc(cu);
2147 break;
2148 case CLASS_ALPHA:
2149 for (cu = 1; cu <= 255; cu++)
2150 if (isalpha(cu))
2151 regc(cu);
2152 break;
2153 case CLASS_BLANK:
2154 regc(' ');
2155 regc('\t');
2156 break;
2157 case CLASS_CNTRL:
2158 for (cu = 1; cu <= 255; cu++)
2159 if (iscntrl(cu))
2160 regc(cu);
2161 break;
2162 case CLASS_DIGIT:
2163 for (cu = 1; cu <= 255; cu++)
2164 if (VIM_ISDIGIT(cu))
2165 regc(cu);
2166 break;
2167 case CLASS_GRAPH:
2168 for (cu = 1; cu <= 255; cu++)
2169 if (isgraph(cu))
2170 regc(cu);
2171 break;
2172 case CLASS_LOWER:
2173 for (cu = 1; cu <= 255; cu++)
2174 if (islower(cu))
2175 regc(cu);
2176 break;
2177 case CLASS_PRINT:
2178 for (cu = 1; cu <= 255; cu++)
2179 if (vim_isprintc(cu))
2180 regc(cu);
2181 break;
2182 case CLASS_PUNCT:
2183 for (cu = 1; cu <= 255; cu++)
2184 if (ispunct(cu))
2185 regc(cu);
2186 break;
2187 case CLASS_SPACE:
2188 for (cu = 9; cu <= 13; cu++)
2189 regc(cu);
2190 regc(' ');
2191 break;
2192 case CLASS_UPPER:
2193 for (cu = 1; cu <= 255; cu++)
2194 if (isupper(cu))
2195 regc(cu);
2196 break;
2197 case CLASS_XDIGIT:
2198 for (cu = 1; cu <= 255; cu++)
2199 if (vim_isxdigit(cu))
2200 regc(cu);
2201 break;
2202 case CLASS_TAB:
2203 regc('\t');
2204 break;
2205 case CLASS_RETURN:
2206 regc('\r');
2207 break;
2208 case CLASS_BACKSPACE:
2209 regc('\b');
2210 break;
2211 case CLASS_ESCAPE:
2212 regc('\033');
2213 break;
2214 }
2215 }
2216 else
2217 {
2218#ifdef FEAT_MBYTE
2219 if (has_mbyte)
2220 {
2221 int len;
2222
2223 /* produce a multibyte character, including any
2224 * following composing characters */
2225 startc = mb_ptr2char(regparse);
2226 len = (*mb_ptr2len_check)(regparse);
2227 if (enc_utf8 && utf_char2len(startc) != len)
2228 startc = -1; /* composing chars */
2229 while (--len >= 0)
2230 regc(*regparse++);
2231 }
2232 else
2233#endif
2234 {
2235 startc = *regparse++;
2236 regc(startc);
2237 }
2238 }
2239 }
2240 regc(NUL);
2241 prevchr_len = 1; /* last char was the ']' */
2242 if (*regparse != ']')
2243 EMSG_RET_NULL(_(e_toomsbra)); /* Cannot happen? */
2244 skipchr(); /* let's be friends with the lexer again */
2245 *flagp |= HASWIDTH | SIMPLE;
2246 break;
2247 }
2248 }
2249 /* FALLTHROUGH */
2250
2251 default:
2252 {
2253 int len;
2254
2255#ifdef FEAT_MBYTE
2256 /* A multi-byte character is handled as a separate atom if it's
2257 * before a multi. */
2258 if (has_mbyte && (*mb_char2len)(c) > 1
2259 && re_multi_type(peekchr()) != NOT_MULTI)
2260 {
2261 ret = regnode(MULTIBYTECODE);
2262 regmbc(c);
2263 *flagp |= HASWIDTH | SIMPLE;
2264 break;
2265 }
2266#endif
2267
2268 ret = regnode(EXACTLY);
2269
2270 /*
2271 * Append characters as long as:
2272 * - there is no following multi, we then need the character in
2273 * front of it as a single character operand
2274 * - not running into a Magic character
2275 * - "one_exactly" is not set
2276 * But always emit at least one character. Might be a Multi,
2277 * e.g., a "[" without matching "]".
2278 */
2279 for (len = 0; c != NUL && (len == 0
2280 || (re_multi_type(peekchr()) == NOT_MULTI
2281 && !one_exactly
2282 && !is_Magic(c))); ++len)
2283 {
2284 c = no_Magic(c);
2285#ifdef FEAT_MBYTE
2286 if (has_mbyte)
2287 {
2288 regmbc(c);
2289 if (enc_utf8)
2290 {
2291 int off;
2292 int l;
2293
2294 /* Need to get composing character too, directly
2295 * access regparse for that, because skipchr() skips
2296 * over composing chars. */
2297 ungetchr();
2298 if (*regparse == '\\' && regparse[1] != NUL)
2299 off = 1;
2300 else
2301 off = 0;
2302 for (;;)
2303 {
2304 l = utf_ptr2len_check(regparse + off);
2305 if (!UTF_COMPOSINGLIKE(regparse + off,
2306 regparse + off + l))
2307 break;
2308 off += l;
2309 regmbc(utf_ptr2char(regparse + off));
2310 }
2311 skipchr();
2312 }
2313 }
2314 else
2315#endif
2316 regc(c);
2317 c = getchr();
2318 }
2319 ungetchr();
2320
2321 regc(NUL);
2322 *flagp |= HASWIDTH;
2323 if (len == 1)
2324 *flagp |= SIMPLE;
2325 }
2326 break;
2327 }
2328
2329 return ret;
2330}
2331
2332/*
2333 * emit a node
2334 * Return pointer to generated code.
2335 */
2336 static char_u *
2337regnode(op)
2338 int op;
2339{
2340 char_u *ret;
2341
2342 ret = regcode;
2343 if (ret == JUST_CALC_SIZE)
2344 regsize += 3;
2345 else
2346 {
2347 *regcode++ = op;
2348 *regcode++ = NUL; /* Null "next" pointer. */
2349 *regcode++ = NUL;
2350 }
2351 return ret;
2352}
2353
2354/*
2355 * Emit (if appropriate) a byte of code
2356 */
2357 static void
2358regc(b)
2359 int b;
2360{
2361 if (regcode == JUST_CALC_SIZE)
2362 regsize++;
2363 else
2364 *regcode++ = b;
2365}
2366
2367#ifdef FEAT_MBYTE
2368/*
2369 * Emit (if appropriate) a multi-byte character of code
2370 */
2371 static void
2372regmbc(c)
2373 int c;
2374{
2375 if (regcode == JUST_CALC_SIZE)
2376 regsize += (*mb_char2len)(c);
2377 else
2378 regcode += (*mb_char2bytes)(c, regcode);
2379}
2380#endif
2381
2382/*
2383 * reginsert - insert an operator in front of already-emitted operand
2384 *
2385 * Means relocating the operand.
2386 */
2387 static void
2388reginsert(op, opnd)
2389 int op;
2390 char_u *opnd;
2391{
2392 char_u *src;
2393 char_u *dst;
2394 char_u *place;
2395
2396 if (regcode == JUST_CALC_SIZE)
2397 {
2398 regsize += 3;
2399 return;
2400 }
2401 src = regcode;
2402 regcode += 3;
2403 dst = regcode;
2404 while (src > opnd)
2405 *--dst = *--src;
2406
2407 place = opnd; /* Op node, where operand used to be. */
2408 *place++ = op;
2409 *place++ = NUL;
2410 *place = NUL;
2411}
2412
2413/*
2414 * reginsert_limits - insert an operator in front of already-emitted operand.
2415 * The operator has the given limit values as operands. Also set next pointer.
2416 *
2417 * Means relocating the operand.
2418 */
2419 static void
2420reginsert_limits(op, minval, maxval, opnd)
2421 int op;
2422 long minval;
2423 long maxval;
2424 char_u *opnd;
2425{
2426 char_u *src;
2427 char_u *dst;
2428 char_u *place;
2429
2430 if (regcode == JUST_CALC_SIZE)
2431 {
2432 regsize += 11;
2433 return;
2434 }
2435 src = regcode;
2436 regcode += 11;
2437 dst = regcode;
2438 while (src > opnd)
2439 *--dst = *--src;
2440
2441 place = opnd; /* Op node, where operand used to be. */
2442 *place++ = op;
2443 *place++ = NUL;
2444 *place++ = NUL;
2445 place = re_put_long(place, (long_u)minval);
2446 place = re_put_long(place, (long_u)maxval);
2447 regtail(opnd, place);
2448}
2449
2450/*
2451 * Write a long as four bytes at "p" and return pointer to the next char.
2452 */
2453 static char_u *
2454re_put_long(p, val)
2455 char_u *p;
2456 long_u val;
2457{
2458 *p++ = (char_u) ((val >> 24) & 0377);
2459 *p++ = (char_u) ((val >> 16) & 0377);
2460 *p++ = (char_u) ((val >> 8) & 0377);
2461 *p++ = (char_u) (val & 0377);
2462 return p;
2463}
2464
2465/*
2466 * regtail - set the next-pointer at the end of a node chain
2467 */
2468 static void
2469regtail(p, val)
2470 char_u *p;
2471 char_u *val;
2472{
2473 char_u *scan;
2474 char_u *temp;
2475 int offset;
2476
2477 if (p == JUST_CALC_SIZE)
2478 return;
2479
2480 /* Find last node. */
2481 scan = p;
2482 for (;;)
2483 {
2484 temp = regnext(scan);
2485 if (temp == NULL)
2486 break;
2487 scan = temp;
2488 }
2489
Bram Moolenaar582fd852005-03-28 20:58:01 +00002490 if (OP(scan) == BACK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 offset = (int)(scan - val);
2492 else
2493 offset = (int)(val - scan);
2494 *(scan + 1) = (char_u) (((unsigned)offset >> 8) & 0377);
2495 *(scan + 2) = (char_u) (offset & 0377);
2496}
2497
2498/*
2499 * regoptail - regtail on item after a BRANCH; nop if none
2500 */
2501 static void
2502regoptail(p, val)
2503 char_u *p;
2504 char_u *val;
2505{
2506 /* When op is neither BRANCH nor BRACE_COMPLEX0-9, it is "operandless" */
2507 if (p == NULL || p == JUST_CALC_SIZE
2508 || (OP(p) != BRANCH
2509 && (OP(p) < BRACE_COMPLEX || OP(p) > BRACE_COMPLEX + 9)))
2510 return;
2511 regtail(OPERAND(p), val);
2512}
2513
2514/*
2515 * getchr() - get the next character from the pattern. We know about
2516 * magic and such, so therefore we need a lexical analyzer.
2517 */
2518
2519/* static int curchr; */
2520static int prevprevchr;
2521static int prevchr;
2522static int nextchr; /* used for ungetchr() */
2523/*
2524 * Note: prevchr is sometimes -1 when we are not at the start,
2525 * eg in /[ ^I]^ the pattern was never found even if it existed, because ^ was
2526 * taken to be magic -- webb
2527 */
2528static int at_start; /* True when on the first character */
2529static int prev_at_start; /* True when on the second character */
2530
2531 static void
2532initchr(str)
2533 char_u *str;
2534{
2535 regparse = str;
2536 prevchr_len = 0;
2537 curchr = prevprevchr = prevchr = nextchr = -1;
2538 at_start = TRUE;
2539 prev_at_start = FALSE;
2540}
2541
2542 static int
2543peekchr()
2544{
Bram Moolenaardf177f62005-02-22 08:39:57 +00002545 static int after_slash = FALSE;
2546
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 if (curchr == -1)
2548 {
2549 switch (curchr = regparse[0])
2550 {
2551 case '.':
2552 case '[':
2553 case '~':
2554 /* magic when 'magic' is on */
2555 if (reg_magic >= MAGIC_ON)
2556 curchr = Magic(curchr);
2557 break;
2558 case '(':
2559 case ')':
2560 case '{':
2561 case '%':
2562 case '+':
2563 case '=':
2564 case '?':
2565 case '@':
2566 case '!':
2567 case '&':
2568 case '|':
2569 case '<':
2570 case '>':
2571 case '#': /* future ext. */
2572 case '"': /* future ext. */
2573 case '\'': /* future ext. */
2574 case ',': /* future ext. */
2575 case '-': /* future ext. */
2576 case ':': /* future ext. */
2577 case ';': /* future ext. */
2578 case '`': /* future ext. */
2579 case '/': /* Can't be used in / command */
2580 /* magic only after "\v" */
2581 if (reg_magic == MAGIC_ALL)
2582 curchr = Magic(curchr);
2583 break;
2584 case '*':
Bram Moolenaardf177f62005-02-22 08:39:57 +00002585 /* * is not magic as the very first character, eg "?*ptr", when
2586 * after '^', eg "/^*ptr" and when after "\(", "\|", "\&". But
2587 * "\(\*" is not magic, thus must be magic if "after_slash" */
2588 if (reg_magic >= MAGIC_ON
2589 && !at_start
2590 && !(prev_at_start && prevchr == Magic('^'))
2591 && (after_slash
2592 || (prevchr != Magic('(')
2593 && prevchr != Magic('&')
2594 && prevchr != Magic('|'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 curchr = Magic('*');
2596 break;
2597 case '^':
2598 /* '^' is only magic as the very first character and if it's after
2599 * "\(", "\|", "\&' or "\n" */
2600 if (reg_magic >= MAGIC_OFF
2601 && (at_start
2602 || reg_magic == MAGIC_ALL
2603 || prevchr == Magic('(')
2604 || prevchr == Magic('|')
2605 || prevchr == Magic('&')
2606 || prevchr == Magic('n')
2607 || (no_Magic(prevchr) == '('
2608 && prevprevchr == Magic('%'))))
2609 {
2610 curchr = Magic('^');
2611 at_start = TRUE;
2612 prev_at_start = FALSE;
2613 }
2614 break;
2615 case '$':
2616 /* '$' is only magic as the very last char and if it's in front of
2617 * either "\|", "\)", "\&", or "\n" */
2618 if (reg_magic >= MAGIC_OFF)
2619 {
2620 char_u *p = regparse + 1;
2621
2622 /* ignore \c \C \m and \M after '$' */
2623 while (p[0] == '\\' && (p[1] == 'c' || p[1] == 'C'
2624 || p[1] == 'm' || p[1] == 'M' || p[1] == 'Z'))
2625 p += 2;
2626 if (p[0] == NUL
2627 || (p[0] == '\\'
2628 && (p[1] == '|' || p[1] == '&' || p[1] == ')'
2629 || p[1] == 'n'))
2630 || reg_magic == MAGIC_ALL)
2631 curchr = Magic('$');
2632 }
2633 break;
2634 case '\\':
2635 {
2636 int c = regparse[1];
2637
2638 if (c == NUL)
2639 curchr = '\\'; /* trailing '\' */
2640 else if (
2641#ifdef EBCDIC
2642 vim_strchr(META, c)
2643#else
2644 c <= '~' && META_flags[c]
2645#endif
2646 )
2647 {
2648 /*
2649 * META contains everything that may be magic sometimes,
2650 * except ^ and $ ("\^" and "\$" are only magic after
2651 * "\v"). We now fetch the next character and toggle its
2652 * magicness. Therefore, \ is so meta-magic that it is
2653 * not in META.
2654 */
2655 curchr = -1;
2656 prev_at_start = at_start;
2657 at_start = FALSE; /* be able to say "/\*ptr" */
2658 ++regparse;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002659 ++after_slash;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 peekchr();
2661 --regparse;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002662 --after_slash;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 curchr = toggle_Magic(curchr);
2664 }
2665 else if (vim_strchr(REGEXP_ABBR, c))
2666 {
2667 /*
2668 * Handle abbreviations, like "\t" for TAB -- webb
2669 */
2670 curchr = backslash_trans(c);
2671 }
2672 else if (reg_magic == MAGIC_NONE && (c == '$' || c == '^'))
2673 curchr = toggle_Magic(c);
2674 else
2675 {
2676 /*
2677 * Next character can never be (made) magic?
2678 * Then backslashing it won't do anything.
2679 */
2680#ifdef FEAT_MBYTE
2681 if (has_mbyte)
2682 curchr = (*mb_ptr2char)(regparse + 1);
2683 else
2684#endif
2685 curchr = c;
2686 }
2687 break;
2688 }
2689
2690#ifdef FEAT_MBYTE
2691 default:
2692 if (has_mbyte)
2693 curchr = (*mb_ptr2char)(regparse);
2694#endif
2695 }
2696 }
2697
2698 return curchr;
2699}
2700
2701/*
2702 * Eat one lexed character. Do this in a way that we can undo it.
2703 */
2704 static void
2705skipchr()
2706{
2707 /* peekchr() eats a backslash, do the same here */
2708 if (*regparse == '\\')
2709 prevchr_len = 1;
2710 else
2711 prevchr_len = 0;
2712 if (regparse[prevchr_len] != NUL)
2713 {
2714#ifdef FEAT_MBYTE
2715 if (has_mbyte)
2716 prevchr_len += (*mb_ptr2len_check)(regparse + prevchr_len);
2717 else
2718#endif
2719 ++prevchr_len;
2720 }
2721 regparse += prevchr_len;
2722 prev_at_start = at_start;
2723 at_start = FALSE;
2724 prevprevchr = prevchr;
2725 prevchr = curchr;
2726 curchr = nextchr; /* use previously unget char, or -1 */
2727 nextchr = -1;
2728}
2729
2730/*
2731 * Skip a character while keeping the value of prev_at_start for at_start.
2732 * prevchr and prevprevchr are also kept.
2733 */
2734 static void
2735skipchr_keepstart()
2736{
2737 int as = prev_at_start;
2738 int pr = prevchr;
2739 int prpr = prevprevchr;
2740
2741 skipchr();
2742 at_start = as;
2743 prevchr = pr;
2744 prevprevchr = prpr;
2745}
2746
2747 static int
2748getchr()
2749{
2750 int chr = peekchr();
2751
2752 skipchr();
2753 return chr;
2754}
2755
2756/*
2757 * put character back. Works only once!
2758 */
2759 static void
2760ungetchr()
2761{
2762 nextchr = curchr;
2763 curchr = prevchr;
2764 prevchr = prevprevchr;
2765 at_start = prev_at_start;
2766 prev_at_start = FALSE;
2767
2768 /* Backup regparse, so that it's at the same position as before the
2769 * getchr(). */
2770 regparse -= prevchr_len;
2771}
2772
2773/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002774 * Get and return the value of the hex string at the current position.
2775 * Return -1 if there is no valid hex number.
2776 * The position is updated:
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002777 * blahblah\%x20asdf
2778 * before-^ ^-after
2779 * The parameter controls the maximum number of input characters. This will be
2780 * 2 when reading a \%x20 sequence and 4 when reading a \%u20AC sequence.
2781 */
2782 static int
2783gethexchrs(maxinputlen)
2784 int maxinputlen;
2785{
2786 int nr = 0;
2787 int c;
2788 int i;
2789
2790 for (i = 0; i < maxinputlen; ++i)
2791 {
2792 c = regparse[0];
2793 if (!vim_isxdigit(c))
2794 break;
2795 nr <<= 4;
2796 nr |= hex2nr(c);
2797 ++regparse;
2798 }
2799
2800 if (i == 0)
2801 return -1;
2802 return nr;
2803}
2804
2805/*
2806 * get and return the value of the decimal string immediately after the
2807 * current position. Return -1 for invalid. Consumes all digits.
2808 */
2809 static int
2810getdecchrs()
2811{
2812 int nr = 0;
2813 int c;
2814 int i;
2815
2816 for (i = 0; ; ++i)
2817 {
2818 c = regparse[0];
2819 if (c < '0' || c > '9')
2820 break;
2821 nr *= 10;
2822 nr += c - '0';
2823 ++regparse;
2824 }
2825
2826 if (i == 0)
2827 return -1;
2828 return nr;
2829}
2830
2831/*
2832 * get and return the value of the octal string immediately after the current
2833 * position. Return -1 for invalid, or 0-255 for valid. Smart enough to handle
2834 * numbers > 377 correctly (for example, 400 is treated as 40) and doesn't
2835 * treat 8 or 9 as recognised characters. Position is updated:
2836 * blahblah\%o210asdf
2837 * before-^ ^-after
2838 */
2839 static int
2840getoctchrs()
2841{
2842 int nr = 0;
2843 int c;
2844 int i;
2845
2846 for (i = 0; i < 3 && nr < 040; ++i)
2847 {
2848 c = regparse[0];
2849 if (c < '0' || c > '7')
2850 break;
2851 nr <<= 3;
2852 nr |= hex2nr(c);
2853 ++regparse;
2854 }
2855
2856 if (i == 0)
2857 return -1;
2858 return nr;
2859}
2860
2861/*
2862 * Get a number after a backslash that is inside [].
2863 * When nothing is recognized return a backslash.
2864 */
2865 static int
2866coll_get_char()
2867{
2868 int nr = -1;
2869
2870 switch (*regparse++)
2871 {
2872 case 'd': nr = getdecchrs(); break;
2873 case 'o': nr = getoctchrs(); break;
2874 case 'x': nr = gethexchrs(2); break;
2875 case 'u': nr = gethexchrs(4); break;
2876 case 'U': nr = gethexchrs(8); break;
2877 }
2878 if (nr < 0)
2879 {
2880 /* If getting the number fails be backwards compatible: the character
2881 * is a backslash. */
2882 --regparse;
2883 nr = '\\';
2884 }
2885 return nr;
2886}
2887
2888/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 * read_limits - Read two integers to be taken as a minimum and maximum.
2890 * If the first character is '-', then the range is reversed.
2891 * Should end with 'end'. If minval is missing, zero is default, if maxval is
2892 * missing, a very big number is the default.
2893 */
2894 static int
2895read_limits(minval, maxval)
2896 long *minval;
2897 long *maxval;
2898{
2899 int reverse = FALSE;
2900 char_u *first_char;
2901 long tmp;
2902
2903 if (*regparse == '-')
2904 {
2905 /* Starts with '-', so reverse the range later */
2906 regparse++;
2907 reverse = TRUE;
2908 }
2909 first_char = regparse;
2910 *minval = getdigits(&regparse);
2911 if (*regparse == ',') /* There is a comma */
2912 {
2913 if (vim_isdigit(*++regparse))
2914 *maxval = getdigits(&regparse);
2915 else
2916 *maxval = MAX_LIMIT;
2917 }
2918 else if (VIM_ISDIGIT(*first_char))
2919 *maxval = *minval; /* It was \{n} or \{-n} */
2920 else
2921 *maxval = MAX_LIMIT; /* It was \{} or \{-} */
2922 if (*regparse == '\\')
2923 regparse++; /* Allow either \{...} or \{...\} */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002924 if (*regparse != '}')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 {
2926 sprintf((char *)IObuff, _("E554: Syntax error in %s{...}"),
2927 reg_magic == MAGIC_ALL ? "" : "\\");
2928 EMSG_RET_FAIL(IObuff);
2929 }
2930
2931 /*
2932 * Reverse the range if there was a '-', or make sure it is in the right
2933 * order otherwise.
2934 */
2935 if ((!reverse && *minval > *maxval) || (reverse && *minval < *maxval))
2936 {
2937 tmp = *minval;
2938 *minval = *maxval;
2939 *maxval = tmp;
2940 }
2941 skipchr(); /* let's be friends with the lexer again */
2942 return OK;
2943}
2944
2945/*
2946 * vim_regexec and friends
2947 */
2948
2949/*
2950 * Global work variables for vim_regexec().
2951 */
2952
2953/* The current match-position is remembered with these variables: */
2954static linenr_T reglnum; /* line number, relative to first line */
2955static char_u *regline; /* start of current line */
2956static char_u *reginput; /* current input, points into "regline" */
2957
2958static int need_clear_subexpr; /* subexpressions still need to be
2959 * cleared */
2960#ifdef FEAT_SYN_HL
2961static int need_clear_zsubexpr = FALSE; /* extmatch subexpressions
2962 * still need to be cleared */
2963#endif
2964
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965/*
2966 * Structure used to save the current input state, when it needs to be
2967 * restored after trying a match. Used by reg_save() and reg_restore().
Bram Moolenaar582fd852005-03-28 20:58:01 +00002968 * Also stores the length of "backpos".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 */
2970typedef struct
2971{
2972 union
2973 {
2974 char_u *ptr; /* reginput pointer, for single-line regexp */
2975 lpos_T pos; /* reginput pos, for multi-line regexp */
2976 } rs_u;
Bram Moolenaar582fd852005-03-28 20:58:01 +00002977 int rs_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978} regsave_T;
2979
2980/* struct to save start/end pointer/position in for \(\) */
2981typedef struct
2982{
2983 union
2984 {
2985 char_u *ptr;
2986 lpos_T pos;
2987 } se_u;
Bram Moolenaar582fd852005-03-28 20:58:01 +00002988 int se_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989} save_se_T;
2990
2991static char_u *reg_getline __ARGS((linenr_T lnum));
2992static long vim_regexec_both __ARGS((char_u *line, colnr_T col));
2993static long regtry __ARGS((regprog_T *prog, colnr_T col));
2994static void cleanup_subexpr __ARGS((void));
2995#ifdef FEAT_SYN_HL
2996static void cleanup_zsubexpr __ARGS((void));
2997#endif
2998static void reg_nextline __ARGS((void));
Bram Moolenaar582fd852005-03-28 20:58:01 +00002999static void reg_save __ARGS((regsave_T *save, garray_T *gap));
3000static void reg_restore __ARGS((regsave_T *save, garray_T *gap));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001static int reg_save_equal __ARGS((regsave_T *save));
3002static void save_se_multi __ARGS((save_se_T *savep, lpos_T *posp));
3003static void save_se_one __ARGS((save_se_T *savep, char_u **pp));
3004
3005/* Save the sub-expressions before attempting a match. */
3006#define save_se(savep, posp, pp) \
3007 REG_MULTI ? save_se_multi((savep), (posp)) : save_se_one((savep), (pp))
3008
3009/* After a failed match restore the sub-expressions. */
3010#define restore_se(savep, posp, pp) { \
3011 if (REG_MULTI) \
3012 *(posp) = (savep)->se_u.pos; \
3013 else \
3014 *(pp) = (savep)->se_u.ptr; }
3015
3016static int re_num_cmp __ARGS((long_u val, char_u *scan));
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003017static int regmatch __ARGS((char_u *prog));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018static int regrepeat __ARGS((char_u *p, long maxcount));
3019
3020#ifdef DEBUG
3021int regnarrate = 0;
3022#endif
3023
3024/*
3025 * Internal copy of 'ignorecase'. It is set at each call to vim_regexec().
3026 * Normally it gets the value of "rm_ic" or "rmm_ic", but when the pattern
3027 * contains '\c' or '\C' the value is overruled.
3028 */
3029static int ireg_ic;
3030
3031#ifdef FEAT_MBYTE
3032/*
3033 * Similar to ireg_ic, but only for 'combining' characters. Set with \Z flag
3034 * in the regexp. Defaults to false, always.
3035 */
3036static int ireg_icombine;
3037#endif
3038
3039/*
3040 * Sometimes need to save a copy of a line. Since alloc()/free() is very
3041 * slow, we keep one allocated piece of memory and only re-allocate it when
3042 * it's too small. It's freed in vim_regexec_both() when finished.
3043 */
3044static char_u *reg_tofree;
3045static unsigned reg_tofreelen;
3046
3047/*
3048 * These variables are set when executing a regexp to speed up the execution.
3049 * Which ones are set depends on whethere a single-line or multi-line match is
3050 * done:
3051 * single-line multi-line
3052 * reg_match &regmatch_T NULL
3053 * reg_mmatch NULL &regmmatch_T
3054 * reg_startp reg_match->startp <invalid>
3055 * reg_endp reg_match->endp <invalid>
3056 * reg_startpos <invalid> reg_mmatch->startpos
3057 * reg_endpos <invalid> reg_mmatch->endpos
3058 * reg_win NULL window in which to search
3059 * reg_buf <invalid> buffer in which to search
3060 * reg_firstlnum <invalid> first line in which to search
3061 * reg_maxline 0 last line nr
3062 * reg_line_lbr FALSE or TRUE FALSE
3063 */
3064static regmatch_T *reg_match;
3065static regmmatch_T *reg_mmatch;
3066static char_u **reg_startp = NULL;
3067static char_u **reg_endp = NULL;
3068static lpos_T *reg_startpos = NULL;
3069static lpos_T *reg_endpos = NULL;
3070static win_T *reg_win;
3071static buf_T *reg_buf;
3072static linenr_T reg_firstlnum;
3073static linenr_T reg_maxline;
3074static int reg_line_lbr; /* "\n" in string is line break */
3075
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003076/* Values for rs_state in regitem_T. */
3077typedef enum regstate_E
3078{
3079 RS_NOPEN = 0 /* NOPEN and NCLOSE */
3080 , RS_MOPEN /* MOPEN + [0-9] */
3081 , RS_MCLOSE /* MCLOSE + [0-9] */
3082#ifdef FEAT_SYN_HL
3083 , RS_ZOPEN /* ZOPEN + [0-9] */
3084 , RS_ZCLOSE /* ZCLOSE + [0-9] */
3085#endif
3086 , RS_BRANCH /* BRANCH */
3087 , RS_BRCPLX_MORE /* BRACE_COMPLEX and trying one more match */
3088 , RS_BRCPLX_LONG /* BRACE_COMPLEX and trying longest match */
3089 , RS_BRCPLX_SHORT /* BRACE_COMPLEX and trying shortest match */
3090 , RS_NOMATCH /* NOMATCH */
3091 , RS_BEHIND1 /* BEHIND / NOBEHIND matching rest */
3092 , RS_BEHIND2 /* BEHIND / NOBEHIND matching behind part */
3093 , RS_STAR_LONG /* STAR/PLUS/BRACE_SIMPLE longest match */
3094 , RS_STAR_SHORT /* STAR/PLUS/BRACE_SIMPLE shortest match */
3095} regstate_T;
3096
3097/*
3098 * When there are alternatives a regstate_T is put on the regstack to remember
3099 * what we are doing.
3100 * Before it may be another type of item, depending on rs_state, to remember
3101 * more things.
3102 */
3103typedef struct regitem_S
3104{
3105 regstate_T rs_state; /* what we are doing, one of RS_ above */
3106 char_u *rs_scan; /* current node in program */
3107 union
3108 {
3109 save_se_T sesave;
3110 regsave_T regsave;
3111 } rs_un; /* room for saving reginput */
3112 short rs_no; /* submatch nr */
3113} regitem_T;
3114
3115static regitem_T *regstack_push __ARGS((regstate_T state, char_u *scan));
3116static void regstack_pop __ARGS((char_u **scan));
3117
3118/* used for BEHIND and NOBEHIND matching */
3119typedef struct regbehind_S
3120{
3121 regsave_T save_after;
3122 regsave_T save_behind;
3123} regbehind_T;
3124
3125/* used for STAR, PLUS and BRACE_SIMPLE matching */
3126typedef struct regstar_S
3127{
3128 int nextb; /* next byte */
3129 int nextb_ic; /* next byte reverse case */
3130 long count;
3131 long minval;
3132 long maxval;
3133} regstar_T;
3134
3135/* used to store input position when a BACK was encountered, so that we now if
3136 * we made any progress since the last time. */
3137typedef struct backpos_S
3138{
3139 char_u *bp_scan; /* "scan" where BACK was encountered */
3140 regsave_T bp_pos; /* last input position */
3141} backpos_T;
3142
3143/*
3144 * regstack and backpos are used by regmatch(). They are kept over calls to
3145 * avoid invoking malloc() and free() often.
3146 */
3147static garray_T regstack; /* stack with regitem_T items, sometimes
3148 preceded by regstar_T or regbehind_T. */
3149static garray_T backpos; /* table with backpos_T for BACK */
3150
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151/*
3152 * Get pointer to the line "lnum", which is relative to "reg_firstlnum".
3153 */
3154 static char_u *
3155reg_getline(lnum)
3156 linenr_T lnum;
3157{
3158 /* when looking behind for a match/no-match lnum is negative. But we
3159 * can't go before line 1 */
3160 if (reg_firstlnum + lnum < 1)
3161 return NULL;
3162 return ml_get_buf(reg_buf, reg_firstlnum + lnum, FALSE);
3163}
3164
3165static regsave_T behind_pos;
3166
3167#ifdef FEAT_SYN_HL
3168static char_u *reg_startzp[NSUBEXP]; /* Workspace to mark beginning */
3169static char_u *reg_endzp[NSUBEXP]; /* and end of \z(...\) matches */
3170static lpos_T reg_startzpos[NSUBEXP]; /* idem, beginning pos */
3171static lpos_T reg_endzpos[NSUBEXP]; /* idem, end pos */
3172#endif
3173
3174/* TRUE if using multi-line regexp. */
3175#define REG_MULTI (reg_match == NULL)
3176
3177/*
3178 * Match a regexp against a string.
3179 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
3180 * Uses curbuf for line count and 'iskeyword'.
3181 *
3182 * Return TRUE if there is a match, FALSE if not.
3183 */
3184 int
3185vim_regexec(rmp, line, col)
3186 regmatch_T *rmp;
3187 char_u *line; /* string to match against */
3188 colnr_T col; /* column to start looking for match */
3189{
3190 reg_match = rmp;
3191 reg_mmatch = NULL;
3192 reg_maxline = 0;
3193 reg_line_lbr = FALSE;
3194 reg_win = NULL;
3195 ireg_ic = rmp->rm_ic;
3196#ifdef FEAT_MBYTE
3197 ireg_icombine = FALSE;
3198#endif
3199 return (vim_regexec_both(line, col) != 0);
3200}
3201
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003202#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
3203 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204/*
3205 * Like vim_regexec(), but consider a "\n" in "line" to be a line break.
3206 */
3207 int
3208vim_regexec_nl(rmp, line, col)
3209 regmatch_T *rmp;
3210 char_u *line; /* string to match against */
3211 colnr_T col; /* column to start looking for match */
3212{
3213 reg_match = rmp;
3214 reg_mmatch = NULL;
3215 reg_maxline = 0;
3216 reg_line_lbr = TRUE;
3217 reg_win = NULL;
3218 ireg_ic = rmp->rm_ic;
3219#ifdef FEAT_MBYTE
3220 ireg_icombine = FALSE;
3221#endif
3222 return (vim_regexec_both(line, col) != 0);
3223}
3224#endif
3225
3226/*
3227 * Match a regexp against multiple lines.
3228 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
3229 * Uses curbuf for line count and 'iskeyword'.
3230 *
3231 * Return zero if there is no match. Return number of lines contained in the
3232 * match otherwise.
3233 */
3234 long
3235vim_regexec_multi(rmp, win, buf, lnum, col)
3236 regmmatch_T *rmp;
3237 win_T *win; /* window in which to search or NULL */
3238 buf_T *buf; /* buffer in which to search */
3239 linenr_T lnum; /* nr of line to start looking for match */
3240 colnr_T col; /* column to start looking for match */
3241{
3242 long r;
3243 buf_T *save_curbuf = curbuf;
3244
3245 reg_match = NULL;
3246 reg_mmatch = rmp;
3247 reg_buf = buf;
3248 reg_win = win;
3249 reg_firstlnum = lnum;
3250 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
3251 reg_line_lbr = FALSE;
3252 ireg_ic = rmp->rmm_ic;
3253#ifdef FEAT_MBYTE
3254 ireg_icombine = FALSE;
3255#endif
3256
3257 /* Need to switch to buffer "buf" to make vim_iswordc() work. */
3258 curbuf = buf;
3259 r = vim_regexec_both(NULL, col);
3260 curbuf = save_curbuf;
3261
3262 return r;
3263}
3264
3265/*
3266 * Match a regexp against a string ("line" points to the string) or multiple
3267 * lines ("line" is NULL, use reg_getline()).
3268 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 static long
3270vim_regexec_both(line, col)
3271 char_u *line;
3272 colnr_T col; /* column to start looking for match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273{
3274 regprog_T *prog;
3275 char_u *s;
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003276 long retval = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277
3278 reg_tofree = NULL;
3279
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003280 /* Init the regstack empty. Use an item size of 1 byte, since we push
3281 * different things onto it. Use a large grow size to avoid reallocating
3282 * it too often. */
3283 ga_init2(&regstack, 1, 10000);
3284
3285 /* Init the backpos table empty. */
3286 ga_init2(&backpos, sizeof(backpos_T), 10);
3287
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 if (REG_MULTI)
3289 {
3290 prog = reg_mmatch->regprog;
3291 line = reg_getline((linenr_T)0);
3292 reg_startpos = reg_mmatch->startpos;
3293 reg_endpos = reg_mmatch->endpos;
3294 }
3295 else
3296 {
3297 prog = reg_match->regprog;
3298 reg_startp = reg_match->startp;
3299 reg_endp = reg_match->endp;
3300 }
3301
3302 /* Be paranoid... */
3303 if (prog == NULL || line == NULL)
3304 {
3305 EMSG(_(e_null));
3306 goto theend;
3307 }
3308
3309 /* Check validity of program. */
3310 if (prog_magic_wrong())
3311 goto theend;
3312
3313 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
3314 if (prog->regflags & RF_ICASE)
3315 ireg_ic = TRUE;
3316 else if (prog->regflags & RF_NOICASE)
3317 ireg_ic = FALSE;
3318
3319#ifdef FEAT_MBYTE
3320 /* If pattern contains "\Z" overrule value of ireg_icombine */
3321 if (prog->regflags & RF_ICOMBINE)
3322 ireg_icombine = TRUE;
3323#endif
3324
3325 /* If there is a "must appear" string, look for it. */
3326 if (prog->regmust != NULL)
3327 {
3328 int c;
3329
3330#ifdef FEAT_MBYTE
3331 if (has_mbyte)
3332 c = (*mb_ptr2char)(prog->regmust);
3333 else
3334#endif
3335 c = *prog->regmust;
3336 s = line + col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003337
3338 /*
3339 * This is used very often, esp. for ":global". Use three versions of
3340 * the loop to avoid overhead of conditions.
3341 */
3342 if (!ireg_ic
3343#ifdef FEAT_MBYTE
3344 && !has_mbyte
3345#endif
3346 )
3347 while ((s = vim_strbyte(s, c)) != NULL)
3348 {
3349 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3350 break; /* Found it. */
3351 ++s;
3352 }
3353#ifdef FEAT_MBYTE
3354 else if (!ireg_ic || (!enc_utf8 && mb_char2len(c) > 1))
3355 while ((s = vim_strchr(s, c)) != NULL)
3356 {
3357 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3358 break; /* Found it. */
3359 mb_ptr_adv(s);
3360 }
3361#endif
3362 else
3363 while ((s = cstrchr(s, c)) != NULL)
3364 {
3365 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3366 break; /* Found it. */
3367 mb_ptr_adv(s);
3368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 if (s == NULL) /* Not present. */
3370 goto theend;
3371 }
3372
3373 regline = line;
3374 reglnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375
3376 /* Simplest case: Anchored match need be tried only once. */
3377 if (prog->reganch)
3378 {
3379 int c;
3380
3381#ifdef FEAT_MBYTE
3382 if (has_mbyte)
3383 c = (*mb_ptr2char)(regline + col);
3384 else
3385#endif
3386 c = regline[col];
3387 if (prog->regstart == NUL
3388 || prog->regstart == c
3389 || (ireg_ic && ((
3390#ifdef FEAT_MBYTE
3391 (enc_utf8 && utf_fold(prog->regstart) == utf_fold(c)))
3392 || (c < 255 && prog->regstart < 255 &&
3393#endif
3394 TOLOWER_LOC(prog->regstart) == TOLOWER_LOC(c)))))
3395 retval = regtry(prog, col);
3396 else
3397 retval = 0;
3398 }
3399 else
3400 {
3401 /* Messy cases: unanchored match. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003402 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 {
3404 if (prog->regstart != NUL)
3405 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003406 /* Skip until the char we know it must start with.
3407 * Used often, do some work to avoid call overhead. */
3408 if (!ireg_ic
3409#ifdef FEAT_MBYTE
3410 && !has_mbyte
3411#endif
3412 )
3413 s = vim_strbyte(regline + col, prog->regstart);
3414 else
3415 s = cstrchr(regline + col, prog->regstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 if (s == NULL)
3417 {
3418 retval = 0;
3419 break;
3420 }
3421 col = (int)(s - regline);
3422 }
3423
3424 retval = regtry(prog, col);
3425 if (retval > 0)
3426 break;
3427
3428 /* if not currently on the first line, get it again */
3429 if (reglnum != 0)
3430 {
3431 regline = reg_getline((linenr_T)0);
3432 reglnum = 0;
3433 }
3434 if (regline[col] == NUL)
3435 break;
3436#ifdef FEAT_MBYTE
3437 if (has_mbyte)
3438 col += (*mb_ptr2len_check)(regline + col);
3439 else
3440#endif
3441 ++col;
3442 }
3443 }
3444
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 vim_free(reg_tofree);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003447 ga_clear(&regstack);
3448 ga_clear(&backpos);
3449
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 return retval;
3451}
3452
3453#ifdef FEAT_SYN_HL
3454static reg_extmatch_T *make_extmatch __ARGS((void));
3455
3456/*
3457 * Create a new extmatch and mark it as referenced once.
3458 */
3459 static reg_extmatch_T *
3460make_extmatch()
3461{
3462 reg_extmatch_T *em;
3463
3464 em = (reg_extmatch_T *)alloc_clear((unsigned)sizeof(reg_extmatch_T));
3465 if (em != NULL)
3466 em->refcnt = 1;
3467 return em;
3468}
3469
3470/*
3471 * Add a reference to an extmatch.
3472 */
3473 reg_extmatch_T *
3474ref_extmatch(em)
3475 reg_extmatch_T *em;
3476{
3477 if (em != NULL)
3478 em->refcnt++;
3479 return em;
3480}
3481
3482/*
3483 * Remove a reference to an extmatch. If there are no references left, free
3484 * the info.
3485 */
3486 void
3487unref_extmatch(em)
3488 reg_extmatch_T *em;
3489{
3490 int i;
3491
3492 if (em != NULL && --em->refcnt <= 0)
3493 {
3494 for (i = 0; i < NSUBEXP; ++i)
3495 vim_free(em->matches[i]);
3496 vim_free(em);
3497 }
3498}
3499#endif
3500
3501/*
3502 * regtry - try match of "prog" with at regline["col"].
3503 * Returns 0 for failure, number of lines contained in the match otherwise.
3504 */
3505 static long
3506regtry(prog, col)
3507 regprog_T *prog;
3508 colnr_T col;
3509{
3510 reginput = regline + col;
3511 need_clear_subexpr = TRUE;
3512#ifdef FEAT_SYN_HL
3513 /* Clear the external match subpointers if necessary. */
3514 if (prog->reghasz == REX_SET)
3515 need_clear_zsubexpr = TRUE;
3516#endif
3517
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003518 if (regmatch(prog->program + 1) == 0)
3519 return 0;
3520
3521 cleanup_subexpr();
3522 if (REG_MULTI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003524 if (reg_startpos[0].lnum < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003526 reg_startpos[0].lnum = 0;
3527 reg_startpos[0].col = col;
3528 }
3529 if (reg_endpos[0].lnum < 0)
3530 {
3531 reg_endpos[0].lnum = reglnum;
3532 reg_endpos[0].col = (int)(reginput - regline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 }
3534 else
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003535 /* Use line number of "\ze". */
3536 reglnum = reg_endpos[0].lnum;
3537 }
3538 else
3539 {
3540 if (reg_startp[0] == NULL)
3541 reg_startp[0] = regline + col;
3542 if (reg_endp[0] == NULL)
3543 reg_endp[0] = reginput;
3544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545#ifdef FEAT_SYN_HL
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003546 /* Package any found \z(...\) matches for export. Default is none. */
3547 unref_extmatch(re_extmatch_out);
3548 re_extmatch_out = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003550 if (prog->reghasz == REX_SET)
3551 {
3552 int i;
3553
3554 cleanup_zsubexpr();
3555 re_extmatch_out = make_extmatch();
3556 for (i = 0; i < NSUBEXP; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003558 if (REG_MULTI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003560 /* Only accept single line matches. */
3561 if (reg_startzpos[i].lnum >= 0
3562 && reg_endzpos[i].lnum == reg_startzpos[i].lnum)
3563 re_extmatch_out->matches[i] =
3564 vim_strnsave(reg_getline(reg_startzpos[i].lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 + reg_startzpos[i].col,
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003566 reg_endzpos[i].col - reg_startzpos[i].col);
3567 }
3568 else
3569 {
3570 if (reg_startzp[i] != NULL && reg_endzp[i] != NULL)
3571 re_extmatch_out->matches[i] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 vim_strnsave(reg_startzp[i],
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003573 (int)(reg_endzp[i] - reg_startzp[i]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 }
3575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 }
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003577#endif
3578 return 1 + reglnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579}
3580
3581#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582static int reg_prev_class __ARGS((void));
3583
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584/*
3585 * Get class of previous character.
3586 */
3587 static int
3588reg_prev_class()
3589{
3590 if (reginput > regline)
3591 return mb_get_class(reginput - 1
3592 - (*mb_head_off)(regline, reginput - 1));
3593 return -1;
3594}
3595
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003597#define ADVANCE_REGINPUT() mb_ptr_adv(reginput)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598
3599/*
3600 * The arguments from BRACE_LIMITS are stored here. They are actually local
3601 * to regmatch(), but they are here to reduce the amount of stack space used
3602 * (it can be called recursively many times).
3603 */
3604static long bl_minval;
3605static long bl_maxval;
3606
3607/*
3608 * regmatch - main matching routine
3609 *
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003610 * Conceptually the strategy is simple: Check to see whether the current node
3611 * matches, push an item onto the regstack and loop to see whether the rest
3612 * matches, and then act accordingly. In practice we make some effort to
3613 * avoid using the regstack, in particular by going through "ordinary" nodes
3614 * (that don't need to know whether the rest of the match failed) by a nested
3615 * loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 *
3617 * Returns TRUE when there is a match. Leaves reginput and reglnum just after
3618 * the last matched character.
3619 * Returns FALSE when there is no match. Leaves reginput and reglnum in an
3620 * undefined state!
3621 */
3622 static int
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003623regmatch(scan)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 char_u *scan; /* Current node. */
3625{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003626 char_u *next; /* Next node. */
3627 int op;
3628 int c;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003629 regitem_T *rp;
3630 int no;
3631 int status; /* one of the RA_ values: */
3632#define RA_FAIL 1 /* something failed, abort */
3633#define RA_CONT 2 /* continue in inner loop */
3634#define RA_BREAK 3 /* break inner loop */
3635#define RA_MATCH 4 /* successful match */
3636#define RA_NOMATCH 5 /* didn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003638 /* Init the regstack and backpos table empty. They are initialized and
3639 * freed in vim_regexec_both() to reduce malloc()/free() calls. */
3640 regstack.ga_len = 0;
3641 backpos.ga_len = 0;
Bram Moolenaar582fd852005-03-28 20:58:01 +00003642
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003643 /*
Bram Moolenaar582fd852005-03-28 20:58:01 +00003644 * Repeat until "regstack" is empty.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003645 */
3646 for (;;)
3647 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 /* Some patterns my cause a long time to match, even though they are not
3649 * illegal. E.g., "\([a-z]\+\)\+Q". Allow breaking them with CTRL-C. */
3650 fast_breakcheck();
3651
3652#ifdef DEBUG
3653 if (scan != NULL && regnarrate)
3654 {
3655 mch_errmsg(regprop(scan));
3656 mch_errmsg("(\n");
3657 }
3658#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003659
3660 /*
Bram Moolenaar582fd852005-03-28 20:58:01 +00003661 * Repeat for items that can be matched sequentially, without using the
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003662 * regstack.
3663 */
3664 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003666 if (got_int || scan == NULL)
3667 {
3668 status = RA_FAIL;
3669 break;
3670 }
3671 status = RA_CONT;
3672
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673#ifdef DEBUG
3674 if (regnarrate)
3675 {
3676 mch_errmsg(regprop(scan));
3677 mch_errmsg("...\n");
3678# ifdef FEAT_SYN_HL
3679 if (re_extmatch_in != NULL)
3680 {
3681 int i;
3682
3683 mch_errmsg(_("External submatches:\n"));
3684 for (i = 0; i < NSUBEXP; i++)
3685 {
3686 mch_errmsg(" \"");
3687 if (re_extmatch_in->matches[i] != NULL)
3688 mch_errmsg(re_extmatch_in->matches[i]);
3689 mch_errmsg("\"\n");
3690 }
3691 }
3692# endif
3693 }
3694#endif
3695 next = regnext(scan);
3696
3697 op = OP(scan);
3698 /* Check for character class with NL added. */
3699 if (WITH_NL(op) && *reginput == NUL && reglnum < reg_maxline)
3700 {
3701 reg_nextline();
3702 }
3703 else if (reg_line_lbr && WITH_NL(op) && *reginput == '\n')
3704 {
3705 ADVANCE_REGINPUT();
3706 }
3707 else
3708 {
3709 if (WITH_NL(op))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003710 op -= ADD_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711#ifdef FEAT_MBYTE
3712 if (has_mbyte)
3713 c = (*mb_ptr2char)(reginput);
3714 else
3715#endif
3716 c = *reginput;
3717 switch (op)
3718 {
3719 case BOL:
3720 if (reginput != regline)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003721 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 break;
3723
3724 case EOL:
3725 if (c != NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003726 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 break;
3728
3729 case RE_BOF:
3730 /* Passing -1 to the getline() function provided for the search
3731 * should always return NULL if the current line is the first
3732 * line of the file. */
3733 if (reglnum != 0 || reginput != regline
3734 || (REG_MULTI && reg_getline((linenr_T)-1) != NULL))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003735 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 break;
3737
3738 case RE_EOF:
3739 if (reglnum != reg_maxline || c != NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003740 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 break;
3742
3743 case CURSOR:
3744 /* Check if the buffer is in a window and compare the
3745 * reg_win->w_cursor position to the match position. */
3746 if (reg_win == NULL
3747 || (reglnum + reg_firstlnum != reg_win->w_cursor.lnum)
3748 || ((colnr_T)(reginput - regline) != reg_win->w_cursor.col))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003749 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 break;
3751
3752 case RE_LNUM:
3753 if (!REG_MULTI || !re_num_cmp((long_u)(reglnum + reg_firstlnum),
3754 scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003755 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 break;
3757
3758 case RE_COL:
3759 if (!re_num_cmp((long_u)(reginput - regline) + 1, scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003760 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 break;
3762
3763 case RE_VCOL:
3764 if (!re_num_cmp((long_u)win_linetabsize(
3765 reg_win == NULL ? curwin : reg_win,
3766 regline, (colnr_T)(reginput - regline)) + 1, scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003767 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 break;
3769
3770 case BOW: /* \<word; reginput points to w */
3771 if (c == NUL) /* Can't match at end of line */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003772 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773#ifdef FEAT_MBYTE
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003774 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 {
3776 int this_class;
3777
3778 /* Get class of current and previous char (if it exists). */
3779 this_class = mb_get_class(reginput);
3780 if (this_class <= 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003781 status = RA_NOMATCH; /* not on a word at all */
3782 else if (reg_prev_class() == this_class)
3783 status = RA_NOMATCH; /* previous char is in same word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 }
3785#endif
3786 else
3787 {
3788 if (!vim_iswordc(c)
3789 || (reginput > regline && vim_iswordc(reginput[-1])))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003790 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 }
3792 break;
3793
3794 case EOW: /* word\>; reginput points after d */
3795 if (reginput == regline) /* Can't match at start of line */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003796 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797#ifdef FEAT_MBYTE
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003798 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 {
3800 int this_class, prev_class;
3801
3802 /* Get class of current and previous char (if it exists). */
3803 this_class = mb_get_class(reginput);
3804 prev_class = reg_prev_class();
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003805 if (this_class == prev_class
3806 || prev_class == 0 || prev_class == 1)
3807 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003810 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003812 if (!vim_iswordc(reginput[-1])
3813 || (reginput[0] != NUL && vim_iswordc(c)))
3814 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 }
3816 break; /* Matched with EOW */
3817
3818 case ANY:
3819 if (c == NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003820 status = RA_NOMATCH;
3821 else
3822 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 break;
3824
3825 case IDENT:
3826 if (!vim_isIDc(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003827 status = RA_NOMATCH;
3828 else
3829 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 break;
3831
3832 case SIDENT:
3833 if (VIM_ISDIGIT(*reginput) || !vim_isIDc(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003834 status = RA_NOMATCH;
3835 else
3836 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 break;
3838
3839 case KWORD:
3840 if (!vim_iswordp(reginput))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003841 status = RA_NOMATCH;
3842 else
3843 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 break;
3845
3846 case SKWORD:
3847 if (VIM_ISDIGIT(*reginput) || !vim_iswordp(reginput))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003848 status = RA_NOMATCH;
3849 else
3850 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 break;
3852
3853 case FNAME:
3854 if (!vim_isfilec(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003855 status = RA_NOMATCH;
3856 else
3857 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 break;
3859
3860 case SFNAME:
3861 if (VIM_ISDIGIT(*reginput) || !vim_isfilec(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003862 status = RA_NOMATCH;
3863 else
3864 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 break;
3866
3867 case PRINT:
3868 if (ptr2cells(reginput) != 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003869 status = RA_NOMATCH;
3870 else
3871 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 break;
3873
3874 case SPRINT:
3875 if (VIM_ISDIGIT(*reginput) || ptr2cells(reginput) != 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003876 status = RA_NOMATCH;
3877 else
3878 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 break;
3880
3881 case WHITE:
3882 if (!vim_iswhite(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003883 status = RA_NOMATCH;
3884 else
3885 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 break;
3887
3888 case NWHITE:
3889 if (c == NUL || vim_iswhite(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003890 status = RA_NOMATCH;
3891 else
3892 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 break;
3894
3895 case DIGIT:
3896 if (!ri_digit(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003897 status = RA_NOMATCH;
3898 else
3899 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 break;
3901
3902 case NDIGIT:
3903 if (c == NUL || ri_digit(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003904 status = RA_NOMATCH;
3905 else
3906 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 break;
3908
3909 case HEX:
3910 if (!ri_hex(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003911 status = RA_NOMATCH;
3912 else
3913 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 break;
3915
3916 case NHEX:
3917 if (c == NUL || ri_hex(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003918 status = RA_NOMATCH;
3919 else
3920 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 break;
3922
3923 case OCTAL:
3924 if (!ri_octal(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003925 status = RA_NOMATCH;
3926 else
3927 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 break;
3929
3930 case NOCTAL:
3931 if (c == NUL || ri_octal(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003932 status = RA_NOMATCH;
3933 else
3934 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 break;
3936
3937 case WORD:
3938 if (!ri_word(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003939 status = RA_NOMATCH;
3940 else
3941 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 break;
3943
3944 case NWORD:
3945 if (c == NUL || ri_word(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003946 status = RA_NOMATCH;
3947 else
3948 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 break;
3950
3951 case HEAD:
3952 if (!ri_head(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003953 status = RA_NOMATCH;
3954 else
3955 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 break;
3957
3958 case NHEAD:
3959 if (c == NUL || ri_head(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003960 status = RA_NOMATCH;
3961 else
3962 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 break;
3964
3965 case ALPHA:
3966 if (!ri_alpha(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003967 status = RA_NOMATCH;
3968 else
3969 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 break;
3971
3972 case NALPHA:
3973 if (c == NUL || ri_alpha(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003974 status = RA_NOMATCH;
3975 else
3976 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 break;
3978
3979 case LOWER:
3980 if (!ri_lower(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003981 status = RA_NOMATCH;
3982 else
3983 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 break;
3985
3986 case NLOWER:
3987 if (c == NUL || ri_lower(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003988 status = RA_NOMATCH;
3989 else
3990 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 break;
3992
3993 case UPPER:
3994 if (!ri_upper(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003995 status = RA_NOMATCH;
3996 else
3997 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 break;
3999
4000 case NUPPER:
4001 if (c == NUL || ri_upper(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004002 status = RA_NOMATCH;
4003 else
4004 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 break;
4006
4007 case EXACTLY:
4008 {
4009 int len;
4010 char_u *opnd;
4011
4012 opnd = OPERAND(scan);
4013 /* Inline the first byte, for speed. */
4014 if (*opnd != *reginput
4015 && (!ireg_ic || (
4016#ifdef FEAT_MBYTE
4017 !enc_utf8 &&
4018#endif
4019 TOLOWER_LOC(*opnd) != TOLOWER_LOC(*reginput))))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004020 status = RA_NOMATCH;
4021 else if (*opnd == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 {
4023 /* match empty string always works; happens when "~" is
4024 * empty. */
4025 }
4026 else if (opnd[1] == NUL
4027#ifdef FEAT_MBYTE
4028 && !(enc_utf8 && ireg_ic)
4029#endif
4030 )
4031 ++reginput; /* matched a single char */
4032 else
4033 {
4034 len = (int)STRLEN(opnd);
4035 /* Need to match first byte again for multi-byte. */
4036 if (cstrncmp(opnd, reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004037 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038#ifdef FEAT_MBYTE
4039 /* Check for following composing character. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004040 else if (enc_utf8
4041 && UTF_COMPOSINGLIKE(reginput, reginput + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 {
4043 /* raaron: This code makes a composing character get
4044 * ignored, which is the correct behavior (sometimes)
4045 * for voweled Hebrew texts. */
4046 if (!ireg_icombine)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004047 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004050 else
4051 reginput += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 }
4053 }
4054 break;
4055
4056 case ANYOF:
4057 case ANYBUT:
4058 if (c == NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004059 status = RA_NOMATCH;
4060 else if ((cstrchr(OPERAND(scan), c) == NULL) == (op == ANYOF))
4061 status = RA_NOMATCH;
4062 else
4063 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 break;
4065
4066#ifdef FEAT_MBYTE
4067 case MULTIBYTECODE:
4068 if (has_mbyte)
4069 {
4070 int i, len;
4071 char_u *opnd;
4072
4073 opnd = OPERAND(scan);
4074 /* Safety check (just in case 'encoding' was changed since
4075 * compiling the program). */
4076 if ((len = (*mb_ptr2len_check)(opnd)) < 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004077 {
4078 status = RA_NOMATCH;
4079 break;
4080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 for (i = 0; i < len; ++i)
4082 if (opnd[i] != reginput[i])
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004083 {
4084 status = RA_NOMATCH;
4085 break;
4086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 reginput += len;
4088 }
4089 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004090 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 break;
4092#endif
4093
4094 case NOTHING:
4095 break;
4096
4097 case BACK:
Bram Moolenaar582fd852005-03-28 20:58:01 +00004098 {
4099 int i;
4100 backpos_T *bp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101
Bram Moolenaar582fd852005-03-28 20:58:01 +00004102 /*
4103 * When we run into BACK we need to check if we don't keep
4104 * looping without matching any input. The second and later
4105 * times a BACK is encountered it fails if the input is still
4106 * at the same position as the previous time.
4107 * The positions are stored in "backpos" and found by the
4108 * current value of "scan", the position in the RE program.
4109 */
4110 bp = (backpos_T *)backpos.ga_data;
4111 for (i = 0; i < backpos.ga_len; ++i)
4112 if (bp[i].bp_scan == scan)
4113 break;
4114 if (i == backpos.ga_len)
4115 {
4116 /* First time at this BACK, make room to store the pos. */
4117 if (ga_grow(&backpos, 1) == FAIL)
4118 status = RA_FAIL;
4119 else
4120 {
4121 /* get "ga_data" again, it may have changed */
4122 bp = (backpos_T *)backpos.ga_data;
4123 bp[i].bp_scan = scan;
4124 ++backpos.ga_len;
4125 }
4126 }
4127 else if (reg_save_equal(&bp[i].bp_pos))
4128 /* Still at same position as last time, fail. */
4129 status = RA_NOMATCH;
4130
4131 if (status != RA_FAIL && status != RA_NOMATCH)
4132 reg_save(&bp[i].bp_pos, &backpos);
4133 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004134 break;
4135
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 case MOPEN + 0: /* Match start: \zs */
4137 case MOPEN + 1: /* \( */
4138 case MOPEN + 2:
4139 case MOPEN + 3:
4140 case MOPEN + 4:
4141 case MOPEN + 5:
4142 case MOPEN + 6:
4143 case MOPEN + 7:
4144 case MOPEN + 8:
4145 case MOPEN + 9:
4146 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 no = op - MOPEN;
4148 cleanup_subexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004149 rp = regstack_push(RS_MOPEN, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004150 if (rp == NULL)
4151 status = RA_FAIL;
4152 else
4153 {
4154 rp->rs_no = no;
4155 save_se(&rp->rs_un.sesave, &reg_startpos[no],
4156 &reg_startp[no]);
4157 /* We simply continue and handle the result when done. */
4158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004160 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161
4162 case NOPEN: /* \%( */
4163 case NCLOSE: /* \) after \%( */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004164 if (regstack_push(RS_NOPEN, scan) == NULL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004165 status = RA_FAIL;
4166 /* We simply continue and handle the result when done. */
4167 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168
4169#ifdef FEAT_SYN_HL
4170 case ZOPEN + 1:
4171 case ZOPEN + 2:
4172 case ZOPEN + 3:
4173 case ZOPEN + 4:
4174 case ZOPEN + 5:
4175 case ZOPEN + 6:
4176 case ZOPEN + 7:
4177 case ZOPEN + 8:
4178 case ZOPEN + 9:
4179 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 no = op - ZOPEN;
4181 cleanup_zsubexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004182 rp = regstack_push(RS_ZOPEN, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004183 if (rp == NULL)
4184 status = RA_FAIL;
4185 else
4186 {
4187 rp->rs_no = no;
4188 save_se(&rp->rs_un.sesave, &reg_startzpos[no],
4189 &reg_startzp[no]);
4190 /* We simply continue and handle the result when done. */
4191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004193 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194#endif
4195
4196 case MCLOSE + 0: /* Match end: \ze */
4197 case MCLOSE + 1: /* \) */
4198 case MCLOSE + 2:
4199 case MCLOSE + 3:
4200 case MCLOSE + 4:
4201 case MCLOSE + 5:
4202 case MCLOSE + 6:
4203 case MCLOSE + 7:
4204 case MCLOSE + 8:
4205 case MCLOSE + 9:
4206 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 no = op - MCLOSE;
4208 cleanup_subexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004209 rp = regstack_push(RS_MCLOSE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004210 if (rp == NULL)
4211 status = RA_FAIL;
4212 else
4213 {
4214 rp->rs_no = no;
4215 save_se(&rp->rs_un.sesave, &reg_endpos[no], &reg_endp[no]);
4216 /* We simply continue and handle the result when done. */
4217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004219 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220
4221#ifdef FEAT_SYN_HL
4222 case ZCLOSE + 1: /* \) after \z( */
4223 case ZCLOSE + 2:
4224 case ZCLOSE + 3:
4225 case ZCLOSE + 4:
4226 case ZCLOSE + 5:
4227 case ZCLOSE + 6:
4228 case ZCLOSE + 7:
4229 case ZCLOSE + 8:
4230 case ZCLOSE + 9:
4231 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 no = op - ZCLOSE;
4233 cleanup_zsubexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004234 rp = regstack_push(RS_ZCLOSE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004235 if (rp == NULL)
4236 status = RA_FAIL;
4237 else
4238 {
4239 rp->rs_no = no;
4240 save_se(&rp->rs_un.sesave, &reg_endzpos[no],
4241 &reg_endzp[no]);
4242 /* We simply continue and handle the result when done. */
4243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004245 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246#endif
4247
4248 case BACKREF + 1:
4249 case BACKREF + 2:
4250 case BACKREF + 3:
4251 case BACKREF + 4:
4252 case BACKREF + 5:
4253 case BACKREF + 6:
4254 case BACKREF + 7:
4255 case BACKREF + 8:
4256 case BACKREF + 9:
4257 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 int len;
4259 linenr_T clnum;
4260 colnr_T ccol;
4261 char_u *p;
4262
4263 no = op - BACKREF;
4264 cleanup_subexpr();
4265 if (!REG_MULTI) /* Single-line regexp */
4266 {
4267 if (reg_endp[no] == NULL)
4268 {
4269 /* Backref was not set: Match an empty string. */
4270 len = 0;
4271 }
4272 else
4273 {
4274 /* Compare current input with back-ref in the same
4275 * line. */
4276 len = (int)(reg_endp[no] - reg_startp[no]);
4277 if (cstrncmp(reg_startp[no], reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004278 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 }
4280 }
4281 else /* Multi-line regexp */
4282 {
4283 if (reg_endpos[no].lnum < 0)
4284 {
4285 /* Backref was not set: Match an empty string. */
4286 len = 0;
4287 }
4288 else
4289 {
4290 if (reg_startpos[no].lnum == reglnum
4291 && reg_endpos[no].lnum == reglnum)
4292 {
4293 /* Compare back-ref within the current line. */
4294 len = reg_endpos[no].col - reg_startpos[no].col;
4295 if (cstrncmp(regline + reg_startpos[no].col,
4296 reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004297 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 }
4299 else
4300 {
4301 /* Messy situation: Need to compare between two
4302 * lines. */
4303 ccol = reg_startpos[no].col;
4304 clnum = reg_startpos[no].lnum;
4305 for (;;)
4306 {
4307 /* Since getting one line may invalidate
4308 * the other, need to make copy. Slow! */
4309 if (regline != reg_tofree)
4310 {
4311 len = (int)STRLEN(regline);
4312 if (reg_tofree == NULL
4313 || len >= (int)reg_tofreelen)
4314 {
4315 len += 50; /* get some extra */
4316 vim_free(reg_tofree);
4317 reg_tofree = alloc(len);
4318 if (reg_tofree == NULL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004319 {
4320 status = RA_FAIL; /* outof memory!*/
4321 break;
4322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 reg_tofreelen = len;
4324 }
4325 STRCPY(reg_tofree, regline);
4326 reginput = reg_tofree
4327 + (reginput - regline);
4328 regline = reg_tofree;
4329 }
4330
4331 /* Get the line to compare with. */
4332 p = reg_getline(clnum);
4333 if (clnum == reg_endpos[no].lnum)
4334 len = reg_endpos[no].col - ccol;
4335 else
4336 len = (int)STRLEN(p + ccol);
4337
4338 if (cstrncmp(p + ccol, reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004339 {
4340 status = RA_NOMATCH; /* doesn't match */
4341 break;
4342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 if (clnum == reg_endpos[no].lnum)
4344 break; /* match and at end! */
4345 if (reglnum == reg_maxline)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004346 {
4347 status = RA_NOMATCH; /* text too short */
4348 break;
4349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350
4351 /* Advance to next line. */
4352 reg_nextline();
4353 ++clnum;
4354 ccol = 0;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004355 if (got_int)
4356 {
4357 status = RA_FAIL;
4358 break;
4359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 }
4361
4362 /* found a match! Note that regline may now point
4363 * to a copy of the line, that should not matter. */
4364 }
4365 }
4366 }
4367
4368 /* Matched the backref, skip over it. */
4369 reginput += len;
4370 }
4371 break;
4372
4373#ifdef FEAT_SYN_HL
4374 case ZREF + 1:
4375 case ZREF + 2:
4376 case ZREF + 3:
4377 case ZREF + 4:
4378 case ZREF + 5:
4379 case ZREF + 6:
4380 case ZREF + 7:
4381 case ZREF + 8:
4382 case ZREF + 9:
4383 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 int len;
4385
4386 cleanup_zsubexpr();
4387 no = op - ZREF;
4388 if (re_extmatch_in != NULL
4389 && re_extmatch_in->matches[no] != NULL)
4390 {
4391 len = (int)STRLEN(re_extmatch_in->matches[no]);
4392 if (cstrncmp(re_extmatch_in->matches[no],
4393 reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004394 status = RA_NOMATCH;
4395 else
4396 reginput += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 }
4398 else
4399 {
4400 /* Backref was not set: Match an empty string. */
4401 }
4402 }
4403 break;
4404#endif
4405
4406 case BRANCH:
4407 {
4408 if (OP(next) != BRANCH) /* No choice. */
4409 next = OPERAND(scan); /* Avoid recursion. */
4410 else
4411 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004412 rp = regstack_push(RS_BRANCH, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004413 if (rp == NULL)
4414 status = RA_FAIL;
4415 else
4416 status = RA_BREAK; /* rest is below */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 }
4418 }
4419 break;
4420
4421 case BRACE_LIMITS:
4422 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 if (OP(next) == BRACE_SIMPLE)
4424 {
4425 bl_minval = OPERAND_MIN(scan);
4426 bl_maxval = OPERAND_MAX(scan);
4427 }
4428 else if (OP(next) >= BRACE_COMPLEX
4429 && OP(next) < BRACE_COMPLEX + 10)
4430 {
4431 no = OP(next) - BRACE_COMPLEX;
4432 brace_min[no] = OPERAND_MIN(scan);
4433 brace_max[no] = OPERAND_MAX(scan);
4434 brace_count[no] = 0;
4435 }
4436 else
4437 {
4438 EMSG(_(e_internal)); /* Shouldn't happen */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004439 status = RA_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 }
4441 }
4442 break;
4443
4444 case BRACE_COMPLEX + 0:
4445 case BRACE_COMPLEX + 1:
4446 case BRACE_COMPLEX + 2:
4447 case BRACE_COMPLEX + 3:
4448 case BRACE_COMPLEX + 4:
4449 case BRACE_COMPLEX + 5:
4450 case BRACE_COMPLEX + 6:
4451 case BRACE_COMPLEX + 7:
4452 case BRACE_COMPLEX + 8:
4453 case BRACE_COMPLEX + 9:
4454 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 no = op - BRACE_COMPLEX;
4456 ++brace_count[no];
4457
4458 /* If not matched enough times yet, try one more */
4459 if (brace_count[no] <= (brace_min[no] <= brace_max[no]
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004460 ? brace_min[no] : brace_max[no]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004462 rp = regstack_push(RS_BRCPLX_MORE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004463 if (rp == NULL)
4464 status = RA_FAIL;
4465 else
4466 {
4467 rp->rs_no = no;
Bram Moolenaar582fd852005-03-28 20:58:01 +00004468 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004469 next = OPERAND(scan);
4470 /* We continue and handle the result when done. */
4471 }
4472 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 }
4474
4475 /* If matched enough times, may try matching some more */
4476 if (brace_min[no] <= brace_max[no])
4477 {
4478 /* Range is the normal way around, use longest match */
4479 if (brace_count[no] <= brace_max[no])
4480 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004481 rp = regstack_push(RS_BRCPLX_LONG, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004482 if (rp == NULL)
4483 status = RA_FAIL;
4484 else
4485 {
4486 rp->rs_no = no;
Bram Moolenaar582fd852005-03-28 20:58:01 +00004487 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004488 next = OPERAND(scan);
4489 /* We continue and handle the result when done. */
4490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 }
4492 }
4493 else
4494 {
4495 /* Range is backwards, use shortest match first */
4496 if (brace_count[no] <= brace_min[no])
4497 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004498 rp = regstack_push(RS_BRCPLX_SHORT, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004499 if (rp == NULL)
4500 status = RA_FAIL;
4501 else
4502 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00004503 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004504 /* We continue and handle the result when done. */
4505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 }
4507 }
4508 }
4509 break;
4510
4511 case BRACE_SIMPLE:
4512 case STAR:
4513 case PLUS:
4514 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004515 regstar_T rst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516
4517 /*
4518 * Lookahead to avoid useless match attempts when we know
4519 * what character comes next.
4520 */
4521 if (OP(next) == EXACTLY)
4522 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004523 rst.nextb = *OPERAND(next);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 if (ireg_ic)
4525 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004526 if (isupper(rst.nextb))
4527 rst.nextb_ic = TOLOWER_LOC(rst.nextb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004529 rst.nextb_ic = TOUPPER_LOC(rst.nextb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 }
4531 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004532 rst.nextb_ic = rst.nextb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 }
4534 else
4535 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004536 rst.nextb = NUL;
4537 rst.nextb_ic = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 }
4539 if (op != BRACE_SIMPLE)
4540 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004541 rst.minval = (op == STAR) ? 0 : 1;
4542 rst.maxval = MAX_LIMIT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 }
4544 else
4545 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004546 rst.minval = bl_minval;
4547 rst.maxval = bl_maxval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 }
4549
4550 /*
4551 * When maxval > minval, try matching as much as possible, up
4552 * to maxval. When maxval < minval, try matching at least the
4553 * minimal number (since the range is backwards, that's also
4554 * maxval!).
4555 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004556 rst.count = regrepeat(OPERAND(scan), rst.maxval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004559 status = RA_FAIL;
4560 break;
4561 }
4562 if (rst.minval <= rst.maxval
4563 ? rst.count >= rst.minval : rst.count >= rst.maxval)
4564 {
4565 /* It could match. Prepare for trying to match what
4566 * follows. The code is below. Parameters are stored in
4567 * a regstar_T on the regstack. */
Bram Moolenaar916b7af2005-03-16 09:52:38 +00004568 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004569 {
4570 EMSG(_(e_maxmempat));
4571 status = RA_FAIL;
4572 }
4573 else if (ga_grow(&regstack, sizeof(regstar_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004574 status = RA_FAIL;
4575 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004577 regstack.ga_len += sizeof(regstar_T);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004578 rp = regstack_push(rst.minval <= rst.maxval
Bram Moolenaar582fd852005-03-28 20:58:01 +00004579 ? RS_STAR_LONG : RS_STAR_SHORT, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004580 if (rp == NULL)
4581 status = RA_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004583 {
4584 *(((regstar_T *)rp) - 1) = rst;
4585 status = RA_BREAK; /* skip the restore bits */
4586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 }
4588 }
4589 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004590 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 }
4593 break;
4594
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004595 case NOMATCH:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 case MATCH:
4597 case SUBPAT:
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004598 rp = regstack_push(RS_NOMATCH, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004599 if (rp == NULL)
4600 status = RA_FAIL;
4601 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004603 rp->rs_no = op;
Bram Moolenaar582fd852005-03-28 20:58:01 +00004604 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004605 next = OPERAND(scan);
4606 /* We continue and handle the result when done. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 }
4608 break;
4609
4610 case BEHIND:
4611 case NOBEHIND:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004612 /* Need a bit of room to store extra positions. */
Bram Moolenaar916b7af2005-03-16 09:52:38 +00004613 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004614 {
4615 EMSG(_(e_maxmempat));
4616 status = RA_FAIL;
4617 }
4618 else if (ga_grow(&regstack, sizeof(regbehind_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004619 status = RA_FAIL;
4620 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004622 regstack.ga_len += sizeof(regbehind_T);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004623 rp = regstack_push(RS_BEHIND1, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004624 if (rp == NULL)
4625 status = RA_FAIL;
4626 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004628 rp->rs_no = op;
Bram Moolenaar582fd852005-03-28 20:58:01 +00004629 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004630 /* First try if what follows matches. If it does then we
4631 * check the behind match by looping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004634 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635
4636 case BHPOS:
4637 if (REG_MULTI)
4638 {
4639 if (behind_pos.rs_u.pos.col != (colnr_T)(reginput - regline)
4640 || behind_pos.rs_u.pos.lnum != reglnum)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004641 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 }
4643 else if (behind_pos.rs_u.ptr != reginput)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004644 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 break;
4646
4647 case NEWL:
4648 if ((c != NUL || reglnum == reg_maxline)
4649 && (c != '\n' || !reg_line_lbr))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004650 status = RA_NOMATCH;
4651 else if (reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 ADVANCE_REGINPUT();
4653 else
4654 reg_nextline();
4655 break;
4656
4657 case END:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004658 status = RA_MATCH; /* Success! */
4659 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660
4661 default:
4662 EMSG(_(e_re_corr));
4663#ifdef DEBUG
4664 printf("Illegal op code %d\n", op);
4665#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004666 status = RA_FAIL;
4667 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 }
4669 }
4670
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004671 /* If we can't continue sequentially, break the inner loop. */
4672 if (status != RA_CONT)
4673 break;
4674
4675 /* Continue in inner loop, advance to next item. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 scan = next;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004677
4678 } /* end of inner loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679
4680 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004681 * If there is something on the regstack execute the code for the state.
Bram Moolenaar582fd852005-03-28 20:58:01 +00004682 * If the state is popped then loop and use the older state.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004684 while (regstack.ga_len > 0 && status != RA_FAIL)
4685 {
4686 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len) - 1;
4687 switch (rp->rs_state)
4688 {
4689 case RS_NOPEN:
4690 /* Result is passed on as-is, simply pop the state. */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004691 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004692 break;
4693
4694 case RS_MOPEN:
4695 /* Pop the state. Restore pointers when there is no match. */
4696 if (status == RA_NOMATCH)
4697 restore_se(&rp->rs_un.sesave, &reg_startpos[rp->rs_no],
4698 &reg_startp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004699 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004700 break;
4701
4702#ifdef FEAT_SYN_HL
4703 case RS_ZOPEN:
4704 /* Pop the state. Restore pointers when there is no match. */
4705 if (status == RA_NOMATCH)
4706 restore_se(&rp->rs_un.sesave, &reg_startzpos[rp->rs_no],
4707 &reg_startzp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004708 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004709 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004711
4712 case RS_MCLOSE:
4713 /* Pop the state. Restore pointers when there is no match. */
4714 if (status == RA_NOMATCH)
4715 restore_se(&rp->rs_un.sesave, &reg_endpos[rp->rs_no],
4716 &reg_endp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004717 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004718 break;
4719
4720#ifdef FEAT_SYN_HL
4721 case RS_ZCLOSE:
4722 /* Pop the state. Restore pointers when there is no match. */
4723 if (status == RA_NOMATCH)
4724 restore_se(&rp->rs_un.sesave, &reg_endzpos[rp->rs_no],
4725 &reg_endzp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004726 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004727 break;
4728#endif
4729
4730 case RS_BRANCH:
4731 if (status == RA_MATCH)
4732 /* this branch matched, use it */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004733 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004734 else
4735 {
4736 if (status != RA_BREAK)
4737 {
4738 /* After a non-matching branch: try next one. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00004739 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004740 scan = rp->rs_scan;
4741 }
4742 if (scan == NULL || OP(scan) != BRANCH)
4743 {
4744 /* no more branches, didn't find a match */
4745 status = RA_NOMATCH;
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004746 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004747 }
4748 else
4749 {
4750 /* Prepare to try a branch. */
4751 rp->rs_scan = regnext(scan);
Bram Moolenaar582fd852005-03-28 20:58:01 +00004752 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004753 scan = OPERAND(scan);
4754 }
4755 }
4756 break;
4757
4758 case RS_BRCPLX_MORE:
4759 /* Pop the state. Restore pointers when there is no match. */
4760 if (status == RA_NOMATCH)
4761 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00004762 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004763 --brace_count[rp->rs_no]; /* decrement match count */
4764 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004765 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004766 break;
4767
4768 case RS_BRCPLX_LONG:
4769 /* Pop the state. Restore pointers when there is no match. */
4770 if (status == RA_NOMATCH)
4771 {
4772 /* There was no match, but we did find enough matches. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00004773 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004774 --brace_count[rp->rs_no];
4775 /* continue with the items after "\{}" */
4776 status = RA_CONT;
4777 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004778 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004779 if (status == RA_CONT)
4780 scan = regnext(scan);
4781 break;
4782
4783 case RS_BRCPLX_SHORT:
4784 /* Pop the state. Restore pointers when there is no match. */
4785 if (status == RA_NOMATCH)
4786 /* There was no match, try to match one more item. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00004787 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004788 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004789 if (status == RA_NOMATCH)
4790 {
4791 scan = OPERAND(scan);
4792 status = RA_CONT;
4793 }
4794 break;
4795
4796 case RS_NOMATCH:
4797 /* Pop the state. If the operand matches for NOMATCH or
4798 * doesn't match for MATCH/SUBPAT, we fail. Otherwise backup,
4799 * except for SUBPAT, and continue with the next item. */
4800 if (status == (rp->rs_no == NOMATCH ? RA_MATCH : RA_NOMATCH))
4801 status = RA_NOMATCH;
4802 else
4803 {
4804 status = RA_CONT;
Bram Moolenaar582fd852005-03-28 20:58:01 +00004805 if (rp->rs_no != SUBPAT) /* zero-width */
4806 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004807 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004808 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004809 if (status == RA_CONT)
4810 scan = regnext(scan);
4811 break;
4812
4813 case RS_BEHIND1:
4814 if (status == RA_NOMATCH)
4815 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004816 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004817 regstack.ga_len -= sizeof(regbehind_T);
4818 }
4819 else
4820 {
4821 /* The stuff after BEHIND/NOBEHIND matches. Now try if
4822 * the behind part does (not) match before the current
4823 * position in the input. This must be done at every
4824 * position in the input and checking if the match ends at
4825 * the current position. */
4826
4827 /* save the position after the found match for next */
Bram Moolenaar582fd852005-03-28 20:58:01 +00004828 reg_save(&(((regbehind_T *)rp) - 1)->save_after, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004829
4830 /* start looking for a match with operand at the current
4831 * postion. Go back one character until we find the
4832 * result, hitting the start of the line or the previous
4833 * line (for multi-line matching).
4834 * Set behind_pos to where the match should end, BHPOS
4835 * will match it. Save the current value. */
4836 (((regbehind_T *)rp) - 1)->save_behind = behind_pos;
4837 behind_pos = rp->rs_un.regsave;
4838
4839 rp->rs_state = RS_BEHIND2;
4840
Bram Moolenaar582fd852005-03-28 20:58:01 +00004841 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004842 scan = OPERAND(rp->rs_scan);
4843 }
4844 break;
4845
4846 case RS_BEHIND2:
4847 /*
4848 * Looping for BEHIND / NOBEHIND match.
4849 */
4850 if (status == RA_MATCH && reg_save_equal(&behind_pos))
4851 {
4852 /* found a match that ends where "next" started */
4853 behind_pos = (((regbehind_T *)rp) - 1)->save_behind;
4854 if (rp->rs_no == BEHIND)
Bram Moolenaar582fd852005-03-28 20:58:01 +00004855 reg_restore(&(((regbehind_T *)rp) - 1)->save_after,
4856 &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004857 else
4858 /* But we didn't want a match. */
4859 status = RA_NOMATCH;
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004860 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004861 regstack.ga_len -= sizeof(regbehind_T);
4862 }
4863 else
4864 {
4865 /* No match: Go back one character. May go to previous
4866 * line once. */
4867 no = OK;
4868 if (REG_MULTI)
4869 {
4870 if (rp->rs_un.regsave.rs_u.pos.col == 0)
4871 {
4872 if (rp->rs_un.regsave.rs_u.pos.lnum
4873 < behind_pos.rs_u.pos.lnum
4874 || reg_getline(
4875 --rp->rs_un.regsave.rs_u.pos.lnum)
4876 == NULL)
4877 no = FAIL;
4878 else
4879 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00004880 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004881 rp->rs_un.regsave.rs_u.pos.col =
4882 (colnr_T)STRLEN(regline);
4883 }
4884 }
4885 else
4886 --rp->rs_un.regsave.rs_u.pos.col;
4887 }
4888 else
4889 {
4890 if (rp->rs_un.regsave.rs_u.ptr == regline)
4891 no = FAIL;
4892 else
4893 --rp->rs_un.regsave.rs_u.ptr;
4894 }
4895 if (no == OK)
4896 {
4897 /* Advanced, prepare for finding match again. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00004898 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004899 scan = OPERAND(rp->rs_scan);
4900 }
4901 else
4902 {
4903 /* Can't advance. For NOBEHIND that's a match. */
4904 behind_pos = (((regbehind_T *)rp) - 1)->save_behind;
4905 if (rp->rs_no == NOBEHIND)
4906 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00004907 reg_restore(&(((regbehind_T *)rp) - 1)->save_after,
4908 &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004909 status = RA_MATCH;
4910 }
4911 else
4912 status = RA_NOMATCH;
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004913 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004914 regstack.ga_len -= sizeof(regbehind_T);
4915 }
4916 }
4917 break;
4918
4919 case RS_STAR_LONG:
4920 case RS_STAR_SHORT:
4921 {
4922 regstar_T *rst = ((regstar_T *)rp) - 1;
4923
4924 if (status == RA_MATCH)
4925 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004926 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004927 regstack.ga_len -= sizeof(regstar_T);
4928 break;
4929 }
4930
4931 /* Tried once already, restore input pointers. */
4932 if (status != RA_BREAK)
Bram Moolenaar582fd852005-03-28 20:58:01 +00004933 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004934
4935 /* Repeat until we found a position where it could match. */
4936 for (;;)
4937 {
4938 if (status != RA_BREAK)
4939 {
4940 /* Tried first position already, advance. */
4941 if (rp->rs_state == RS_STAR_LONG)
4942 {
4943 /* Trying for longest matc, but couldn't or didn't
4944 * match -- back up one char. */
4945 if (--rst->count < rst->minval)
4946 break;
4947 if (reginput == regline)
4948 {
4949 /* backup to last char of previous line */
4950 --reglnum;
4951 regline = reg_getline(reglnum);
4952 /* Just in case regrepeat() didn't count
4953 * right. */
4954 if (regline == NULL)
4955 break;
4956 reginput = regline + STRLEN(regline);
4957 fast_breakcheck();
4958 }
4959 else
4960 mb_ptr_back(regline, reginput);
4961 }
4962 else
4963 {
4964 /* Range is backwards, use shortest match first.
4965 * Careful: maxval and minval are exchanged!
4966 * Couldn't or didn't match: try advancing one
4967 * char. */
4968 if (rst->count == rst->minval
4969 || regrepeat(OPERAND(rp->rs_scan), 1L) == 0)
4970 break;
4971 ++rst->count;
4972 }
4973 if (got_int)
4974 break;
4975 }
4976 else
4977 status = RA_NOMATCH;
4978
4979 /* If it could match, try it. */
4980 if (rst->nextb == NUL || *reginput == rst->nextb
4981 || *reginput == rst->nextb_ic)
4982 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00004983 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004984 scan = regnext(rp->rs_scan);
4985 status = RA_CONT;
4986 break;
4987 }
4988 }
4989 if (status != RA_CONT)
4990 {
4991 /* Failed. */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004992 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004993 regstack.ga_len -= sizeof(regstar_T);
4994 status = RA_NOMATCH;
4995 }
4996 }
4997 break;
4998 }
4999
5000 /* If we want to continue the inner loop or didn't pop a state contine
5001 * matching loop */
5002 if (status == RA_CONT || rp == (regitem_T *)
5003 ((char *)regstack.ga_data + regstack.ga_len) - 1)
5004 break;
5005 }
5006
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005007 /* May need to continue with the inner loop, starting at "scan". */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005008 if (status == RA_CONT)
5009 continue;
5010
5011 /*
5012 * If the regstack is empty or something failed we are done.
5013 */
5014 if (regstack.ga_len == 0 || status == RA_FAIL)
5015 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005016 if (scan == NULL)
5017 {
5018 /*
5019 * We get here only if there's trouble -- normally "case END" is
5020 * the terminating point.
5021 */
5022 EMSG(_(e_re_corr));
5023#ifdef DEBUG
5024 printf("Premature EOL\n");
5025#endif
5026 }
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005027 if (status == RA_FAIL)
5028 got_int = TRUE;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005029 return (status == RA_MATCH);
5030 }
5031
5032 } /* End of loop until the regstack is empty. */
5033
5034 /* NOTREACHED */
5035}
5036
5037/*
5038 * Push an item onto the regstack.
5039 * Returns pointer to new item. Returns NULL when out of memory.
5040 */
5041 static regitem_T *
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005042regstack_push(state, scan)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005043 regstate_T state;
5044 char_u *scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005045{
5046 regitem_T *rp;
5047
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005048 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005049 {
5050 EMSG(_(e_maxmempat));
5051 return NULL;
5052 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005053 if (ga_grow(&regstack, sizeof(regitem_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005054 return NULL;
5055
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005056 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005057 rp->rs_state = state;
5058 rp->rs_scan = scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005059
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005060 regstack.ga_len += sizeof(regitem_T);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005061 return rp;
5062}
5063
5064/*
5065 * Pop an item from the regstack.
5066 */
5067 static void
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005068regstack_pop(scan)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005069 char_u **scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005070{
5071 regitem_T *rp;
5072
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005073 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len) - 1;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005074 *scan = rp->rs_scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005075
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005076 regstack.ga_len -= sizeof(regitem_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077}
5078
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079/*
5080 * regrepeat - repeatedly match something simple, return how many.
5081 * Advances reginput (and reglnum) to just after the matched chars.
5082 */
5083 static int
5084regrepeat(p, maxcount)
5085 char_u *p;
5086 long maxcount; /* maximum number of matches allowed */
5087{
5088 long count = 0;
5089 char_u *scan;
5090 char_u *opnd;
5091 int mask;
5092 int testval = 0;
5093
5094 scan = reginput; /* Make local copy of reginput for speed. */
5095 opnd = OPERAND(p);
5096 switch (OP(p))
5097 {
5098 case ANY:
5099 case ANY + ADD_NL:
5100 while (count < maxcount)
5101 {
5102 /* Matching anything means we continue until end-of-line (or
5103 * end-of-file for ANY + ADD_NL), only limited by maxcount. */
5104 while (*scan != NUL && count < maxcount)
5105 {
5106 ++count;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005107 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 }
5109 if (!WITH_NL(OP(p)) || reglnum == reg_maxline || count == maxcount)
5110 break;
5111 ++count; /* count the line-break */
5112 reg_nextline();
5113 scan = reginput;
5114 if (got_int)
5115 break;
5116 }
5117 break;
5118
5119 case IDENT:
5120 case IDENT + ADD_NL:
5121 testval = TRUE;
5122 /*FALLTHROUGH*/
5123 case SIDENT:
5124 case SIDENT + ADD_NL:
5125 while (count < maxcount)
5126 {
5127 if (vim_isIDc(*scan) && (testval || !VIM_ISDIGIT(*scan)))
5128 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005129 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 }
5131 else if (*scan == NUL)
5132 {
5133 if (!WITH_NL(OP(p)) || reglnum == reg_maxline)
5134 break;
5135 reg_nextline();
5136 scan = reginput;
5137 if (got_int)
5138 break;
5139 }
5140 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5141 ++scan;
5142 else
5143 break;
5144 ++count;
5145 }
5146 break;
5147
5148 case KWORD:
5149 case KWORD + ADD_NL:
5150 testval = TRUE;
5151 /*FALLTHROUGH*/
5152 case SKWORD:
5153 case SKWORD + ADD_NL:
5154 while (count < maxcount)
5155 {
5156 if (vim_iswordp(scan) && (testval || !VIM_ISDIGIT(*scan)))
5157 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005158 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 }
5160 else if (*scan == NUL)
5161 {
5162 if (!WITH_NL(OP(p)) || reglnum == reg_maxline)
5163 break;
5164 reg_nextline();
5165 scan = reginput;
5166 if (got_int)
5167 break;
5168 }
5169 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5170 ++scan;
5171 else
5172 break;
5173 ++count;
5174 }
5175 break;
5176
5177 case FNAME:
5178 case FNAME + ADD_NL:
5179 testval = TRUE;
5180 /*FALLTHROUGH*/
5181 case SFNAME:
5182 case SFNAME + ADD_NL:
5183 while (count < maxcount)
5184 {
5185 if (vim_isfilec(*scan) && (testval || !VIM_ISDIGIT(*scan)))
5186 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005187 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 }
5189 else if (*scan == NUL)
5190 {
5191 if (!WITH_NL(OP(p)) || reglnum == reg_maxline)
5192 break;
5193 reg_nextline();
5194 scan = reginput;
5195 if (got_int)
5196 break;
5197 }
5198 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5199 ++scan;
5200 else
5201 break;
5202 ++count;
5203 }
5204 break;
5205
5206 case PRINT:
5207 case PRINT + ADD_NL:
5208 testval = TRUE;
5209 /*FALLTHROUGH*/
5210 case SPRINT:
5211 case SPRINT + ADD_NL:
5212 while (count < maxcount)
5213 {
5214 if (*scan == NUL)
5215 {
5216 if (!WITH_NL(OP(p)) || reglnum == reg_maxline)
5217 break;
5218 reg_nextline();
5219 scan = reginput;
5220 if (got_int)
5221 break;
5222 }
5223 else if (ptr2cells(scan) == 1 && (testval || !VIM_ISDIGIT(*scan)))
5224 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005225 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 }
5227 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5228 ++scan;
5229 else
5230 break;
5231 ++count;
5232 }
5233 break;
5234
5235 case WHITE:
5236 case WHITE + ADD_NL:
5237 testval = mask = RI_WHITE;
5238do_class:
5239 while (count < maxcount)
5240 {
5241#ifdef FEAT_MBYTE
5242 int l;
5243#endif
5244 if (*scan == NUL)
5245 {
5246 if (!WITH_NL(OP(p)) || reglnum == reg_maxline)
5247 break;
5248 reg_nextline();
5249 scan = reginput;
5250 if (got_int)
5251 break;
5252 }
5253#ifdef FEAT_MBYTE
5254 else if (has_mbyte && (l = (*mb_ptr2len_check)(scan)) > 1)
5255 {
5256 if (testval != 0)
5257 break;
5258 scan += l;
5259 }
5260#endif
5261 else if ((class_tab[*scan] & mask) == testval)
5262 ++scan;
5263 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5264 ++scan;
5265 else
5266 break;
5267 ++count;
5268 }
5269 break;
5270
5271 case NWHITE:
5272 case NWHITE + ADD_NL:
5273 mask = RI_WHITE;
5274 goto do_class;
5275 case DIGIT:
5276 case DIGIT + ADD_NL:
5277 testval = mask = RI_DIGIT;
5278 goto do_class;
5279 case NDIGIT:
5280 case NDIGIT + ADD_NL:
5281 mask = RI_DIGIT;
5282 goto do_class;
5283 case HEX:
5284 case HEX + ADD_NL:
5285 testval = mask = RI_HEX;
5286 goto do_class;
5287 case NHEX:
5288 case NHEX + ADD_NL:
5289 mask = RI_HEX;
5290 goto do_class;
5291 case OCTAL:
5292 case OCTAL + ADD_NL:
5293 testval = mask = RI_OCTAL;
5294 goto do_class;
5295 case NOCTAL:
5296 case NOCTAL + ADD_NL:
5297 mask = RI_OCTAL;
5298 goto do_class;
5299 case WORD:
5300 case WORD + ADD_NL:
5301 testval = mask = RI_WORD;
5302 goto do_class;
5303 case NWORD:
5304 case NWORD + ADD_NL:
5305 mask = RI_WORD;
5306 goto do_class;
5307 case HEAD:
5308 case HEAD + ADD_NL:
5309 testval = mask = RI_HEAD;
5310 goto do_class;
5311 case NHEAD:
5312 case NHEAD + ADD_NL:
5313 mask = RI_HEAD;
5314 goto do_class;
5315 case ALPHA:
5316 case ALPHA + ADD_NL:
5317 testval = mask = RI_ALPHA;
5318 goto do_class;
5319 case NALPHA:
5320 case NALPHA + ADD_NL:
5321 mask = RI_ALPHA;
5322 goto do_class;
5323 case LOWER:
5324 case LOWER + ADD_NL:
5325 testval = mask = RI_LOWER;
5326 goto do_class;
5327 case NLOWER:
5328 case NLOWER + ADD_NL:
5329 mask = RI_LOWER;
5330 goto do_class;
5331 case UPPER:
5332 case UPPER + ADD_NL:
5333 testval = mask = RI_UPPER;
5334 goto do_class;
5335 case NUPPER:
5336 case NUPPER + ADD_NL:
5337 mask = RI_UPPER;
5338 goto do_class;
5339
5340 case EXACTLY:
5341 {
5342 int cu, cl;
5343
5344 /* This doesn't do a multi-byte character, because a MULTIBYTECODE
5345 * would have been used for it. */
5346 if (ireg_ic)
5347 {
5348 cu = TOUPPER_LOC(*opnd);
5349 cl = TOLOWER_LOC(*opnd);
5350 while (count < maxcount && (*scan == cu || *scan == cl))
5351 {
5352 count++;
5353 scan++;
5354 }
5355 }
5356 else
5357 {
5358 cu = *opnd;
5359 while (count < maxcount && *scan == cu)
5360 {
5361 count++;
5362 scan++;
5363 }
5364 }
5365 break;
5366 }
5367
5368#ifdef FEAT_MBYTE
5369 case MULTIBYTECODE:
5370 {
5371 int i, len, cf = 0;
5372
5373 /* Safety check (just in case 'encoding' was changed since
5374 * compiling the program). */
5375 if ((len = (*mb_ptr2len_check)(opnd)) > 1)
5376 {
5377 if (ireg_ic && enc_utf8)
5378 cf = utf_fold(utf_ptr2char(opnd));
5379 while (count < maxcount)
5380 {
5381 for (i = 0; i < len; ++i)
5382 if (opnd[i] != scan[i])
5383 break;
5384 if (i < len && (!ireg_ic || !enc_utf8
5385 || utf_fold(utf_ptr2char(scan)) != cf))
5386 break;
5387 scan += len;
5388 ++count;
5389 }
5390 }
5391 }
5392 break;
5393#endif
5394
5395 case ANYOF:
5396 case ANYOF + ADD_NL:
5397 testval = TRUE;
5398 /*FALLTHROUGH*/
5399
5400 case ANYBUT:
5401 case ANYBUT + ADD_NL:
5402 while (count < maxcount)
5403 {
5404#ifdef FEAT_MBYTE
5405 int len;
5406#endif
5407 if (*scan == NUL)
5408 {
5409 if (!WITH_NL(OP(p)) || reglnum == reg_maxline)
5410 break;
5411 reg_nextline();
5412 scan = reginput;
5413 if (got_int)
5414 break;
5415 }
5416 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5417 ++scan;
5418#ifdef FEAT_MBYTE
5419 else if (has_mbyte && (len = (*mb_ptr2len_check)(scan)) > 1)
5420 {
5421 if ((cstrchr(opnd, (*mb_ptr2char)(scan)) == NULL) == testval)
5422 break;
5423 scan += len;
5424 }
5425#endif
5426 else
5427 {
5428 if ((cstrchr(opnd, *scan) == NULL) == testval)
5429 break;
5430 ++scan;
5431 }
5432 ++count;
5433 }
5434 break;
5435
5436 case NEWL:
5437 while (count < maxcount
5438 && ((*scan == NUL && reglnum < reg_maxline)
5439 || (*scan == '\n' && reg_line_lbr)))
5440 {
5441 count++;
5442 if (reg_line_lbr)
5443 ADVANCE_REGINPUT();
5444 else
5445 reg_nextline();
5446 scan = reginput;
5447 if (got_int)
5448 break;
5449 }
5450 break;
5451
5452 default: /* Oh dear. Called inappropriately. */
5453 EMSG(_(e_re_corr));
5454#ifdef DEBUG
5455 printf("Called regrepeat with op code %d\n", OP(p));
5456#endif
5457 break;
5458 }
5459
5460 reginput = scan;
5461
5462 return (int)count;
5463}
5464
5465/*
5466 * regnext - dig the "next" pointer out of a node
5467 */
5468 static char_u *
5469regnext(p)
5470 char_u *p;
5471{
5472 int offset;
5473
5474 if (p == JUST_CALC_SIZE)
5475 return NULL;
5476
5477 offset = NEXT(p);
5478 if (offset == 0)
5479 return NULL;
5480
Bram Moolenaar582fd852005-03-28 20:58:01 +00005481 if (OP(p) == BACK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 return p - offset;
5483 else
5484 return p + offset;
5485}
5486
5487/*
5488 * Check the regexp program for its magic number.
5489 * Return TRUE if it's wrong.
5490 */
5491 static int
5492prog_magic_wrong()
5493{
5494 if (UCHARAT(REG_MULTI
5495 ? reg_mmatch->regprog->program
5496 : reg_match->regprog->program) != REGMAGIC)
5497 {
5498 EMSG(_(e_re_corr));
5499 return TRUE;
5500 }
5501 return FALSE;
5502}
5503
5504/*
5505 * Cleanup the subexpressions, if this wasn't done yet.
5506 * This construction is used to clear the subexpressions only when they are
5507 * used (to increase speed).
5508 */
5509 static void
5510cleanup_subexpr()
5511{
5512 if (need_clear_subexpr)
5513 {
5514 if (REG_MULTI)
5515 {
5516 /* Use 0xff to set lnum to -1 */
5517 vim_memset(reg_startpos, 0xff, sizeof(lpos_T) * NSUBEXP);
5518 vim_memset(reg_endpos, 0xff, sizeof(lpos_T) * NSUBEXP);
5519 }
5520 else
5521 {
5522 vim_memset(reg_startp, 0, sizeof(char_u *) * NSUBEXP);
5523 vim_memset(reg_endp, 0, sizeof(char_u *) * NSUBEXP);
5524 }
5525 need_clear_subexpr = FALSE;
5526 }
5527}
5528
5529#ifdef FEAT_SYN_HL
5530 static void
5531cleanup_zsubexpr()
5532{
5533 if (need_clear_zsubexpr)
5534 {
5535 if (REG_MULTI)
5536 {
5537 /* Use 0xff to set lnum to -1 */
5538 vim_memset(reg_startzpos, 0xff, sizeof(lpos_T) * NSUBEXP);
5539 vim_memset(reg_endzpos, 0xff, sizeof(lpos_T) * NSUBEXP);
5540 }
5541 else
5542 {
5543 vim_memset(reg_startzp, 0, sizeof(char_u *) * NSUBEXP);
5544 vim_memset(reg_endzp, 0, sizeof(char_u *) * NSUBEXP);
5545 }
5546 need_clear_zsubexpr = FALSE;
5547 }
5548}
5549#endif
5550
5551/*
5552 * Advance reglnum, regline and reginput to the next line.
5553 */
5554 static void
5555reg_nextline()
5556{
5557 regline = reg_getline(++reglnum);
5558 reginput = regline;
5559 fast_breakcheck();
5560}
5561
5562/*
5563 * Save the input line and position in a regsave_T.
5564 */
5565 static void
Bram Moolenaar582fd852005-03-28 20:58:01 +00005566reg_save(save, gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 regsave_T *save;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005568 garray_T *gap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569{
5570 if (REG_MULTI)
5571 {
5572 save->rs_u.pos.col = (colnr_T)(reginput - regline);
5573 save->rs_u.pos.lnum = reglnum;
5574 }
5575 else
5576 save->rs_u.ptr = reginput;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005577 save->rs_len = gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578}
5579
5580/*
5581 * Restore the input line and position from a regsave_T.
5582 */
5583 static void
Bram Moolenaar582fd852005-03-28 20:58:01 +00005584reg_restore(save, gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 regsave_T *save;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005586 garray_T *gap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587{
5588 if (REG_MULTI)
5589 {
5590 if (reglnum != save->rs_u.pos.lnum)
5591 {
5592 /* only call reg_getline() when the line number changed to save
5593 * a bit of time */
5594 reglnum = save->rs_u.pos.lnum;
5595 regline = reg_getline(reglnum);
5596 }
5597 reginput = regline + save->rs_u.pos.col;
5598 }
5599 else
5600 reginput = save->rs_u.ptr;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005601 gap->ga_len = save->rs_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602}
5603
5604/*
5605 * Return TRUE if current position is equal to saved position.
5606 */
5607 static int
5608reg_save_equal(save)
5609 regsave_T *save;
5610{
5611 if (REG_MULTI)
5612 return reglnum == save->rs_u.pos.lnum
5613 && reginput == regline + save->rs_u.pos.col;
5614 return reginput == save->rs_u.ptr;
5615}
5616
5617/*
5618 * Tentatively set the sub-expression start to the current position (after
5619 * calling regmatch() they will have changed). Need to save the existing
5620 * values for when there is no match.
5621 * Use se_save() to use pointer (save_se_multi()) or position (save_se_one()),
5622 * depending on REG_MULTI.
5623 */
5624 static void
5625save_se_multi(savep, posp)
5626 save_se_T *savep;
5627 lpos_T *posp;
5628{
5629 savep->se_u.pos = *posp;
5630 posp->lnum = reglnum;
5631 posp->col = (colnr_T)(reginput - regline);
5632}
5633
5634 static void
5635save_se_one(savep, pp)
5636 save_se_T *savep;
5637 char_u **pp;
5638{
5639 savep->se_u.ptr = *pp;
5640 *pp = reginput;
5641}
5642
5643/*
5644 * Compare a number with the operand of RE_LNUM, RE_COL or RE_VCOL.
5645 */
5646 static int
5647re_num_cmp(val, scan)
5648 long_u val;
5649 char_u *scan;
5650{
5651 long_u n = OPERAND_MIN(scan);
5652
5653 if (OPERAND_CMP(scan) == '>')
5654 return val > n;
5655 if (OPERAND_CMP(scan) == '<')
5656 return val < n;
5657 return val == n;
5658}
5659
5660
5661#ifdef DEBUG
5662
5663/*
5664 * regdump - dump a regexp onto stdout in vaguely comprehensible form
5665 */
5666 static void
5667regdump(pattern, r)
5668 char_u *pattern;
5669 regprog_T *r;
5670{
5671 char_u *s;
5672 int op = EXACTLY; /* Arbitrary non-END op. */
5673 char_u *next;
5674 char_u *end = NULL;
5675
5676 printf("\r\nregcomp(%s):\r\n", pattern);
5677
5678 s = r->program + 1;
5679 /*
5680 * Loop until we find the END that isn't before a referred next (an END
5681 * can also appear in a NOMATCH operand).
5682 */
5683 while (op != END || s <= end)
5684 {
5685 op = OP(s);
5686 printf("%2d%s", (int)(s - r->program), regprop(s)); /* Where, what. */
5687 next = regnext(s);
5688 if (next == NULL) /* Next ptr. */
5689 printf("(0)");
5690 else
5691 printf("(%d)", (int)((s - r->program) + (next - s)));
5692 if (end < next)
5693 end = next;
5694 if (op == BRACE_LIMITS)
5695 {
5696 /* Two short ints */
5697 printf(" minval %ld, maxval %ld", OPERAND_MIN(s), OPERAND_MAX(s));
5698 s += 8;
5699 }
5700 s += 3;
5701 if (op == ANYOF || op == ANYOF + ADD_NL
5702 || op == ANYBUT || op == ANYBUT + ADD_NL
5703 || op == EXACTLY)
5704 {
5705 /* Literal string, where present. */
5706 while (*s != NUL)
5707 printf("%c", *s++);
5708 s++;
5709 }
5710 printf("\r\n");
5711 }
5712
5713 /* Header fields of interest. */
5714 if (r->regstart != NUL)
5715 printf("start `%s' 0x%x; ", r->regstart < 256
5716 ? (char *)transchar(r->regstart)
5717 : "multibyte", r->regstart);
5718 if (r->reganch)
5719 printf("anchored; ");
5720 if (r->regmust != NULL)
5721 printf("must have \"%s\"", r->regmust);
5722 printf("\r\n");
5723}
5724
5725/*
5726 * regprop - printable representation of opcode
5727 */
5728 static char_u *
5729regprop(op)
5730 char_u *op;
5731{
5732 char_u *p;
5733 static char_u buf[50];
5734
5735 (void) strcpy(buf, ":");
5736
5737 switch (OP(op))
5738 {
5739 case BOL:
5740 p = "BOL";
5741 break;
5742 case EOL:
5743 p = "EOL";
5744 break;
5745 case RE_BOF:
5746 p = "BOF";
5747 break;
5748 case RE_EOF:
5749 p = "EOF";
5750 break;
5751 case CURSOR:
5752 p = "CURSOR";
5753 break;
5754 case RE_LNUM:
5755 p = "RE_LNUM";
5756 break;
5757 case RE_COL:
5758 p = "RE_COL";
5759 break;
5760 case RE_VCOL:
5761 p = "RE_VCOL";
5762 break;
5763 case BOW:
5764 p = "BOW";
5765 break;
5766 case EOW:
5767 p = "EOW";
5768 break;
5769 case ANY:
5770 p = "ANY";
5771 break;
5772 case ANY + ADD_NL:
5773 p = "ANY+NL";
5774 break;
5775 case ANYOF:
5776 p = "ANYOF";
5777 break;
5778 case ANYOF + ADD_NL:
5779 p = "ANYOF+NL";
5780 break;
5781 case ANYBUT:
5782 p = "ANYBUT";
5783 break;
5784 case ANYBUT + ADD_NL:
5785 p = "ANYBUT+NL";
5786 break;
5787 case IDENT:
5788 p = "IDENT";
5789 break;
5790 case IDENT + ADD_NL:
5791 p = "IDENT+NL";
5792 break;
5793 case SIDENT:
5794 p = "SIDENT";
5795 break;
5796 case SIDENT + ADD_NL:
5797 p = "SIDENT+NL";
5798 break;
5799 case KWORD:
5800 p = "KWORD";
5801 break;
5802 case KWORD + ADD_NL:
5803 p = "KWORD+NL";
5804 break;
5805 case SKWORD:
5806 p = "SKWORD";
5807 break;
5808 case SKWORD + ADD_NL:
5809 p = "SKWORD+NL";
5810 break;
5811 case FNAME:
5812 p = "FNAME";
5813 break;
5814 case FNAME + ADD_NL:
5815 p = "FNAME+NL";
5816 break;
5817 case SFNAME:
5818 p = "SFNAME";
5819 break;
5820 case SFNAME + ADD_NL:
5821 p = "SFNAME+NL";
5822 break;
5823 case PRINT:
5824 p = "PRINT";
5825 break;
5826 case PRINT + ADD_NL:
5827 p = "PRINT+NL";
5828 break;
5829 case SPRINT:
5830 p = "SPRINT";
5831 break;
5832 case SPRINT + ADD_NL:
5833 p = "SPRINT+NL";
5834 break;
5835 case WHITE:
5836 p = "WHITE";
5837 break;
5838 case WHITE + ADD_NL:
5839 p = "WHITE+NL";
5840 break;
5841 case NWHITE:
5842 p = "NWHITE";
5843 break;
5844 case NWHITE + ADD_NL:
5845 p = "NWHITE+NL";
5846 break;
5847 case DIGIT:
5848 p = "DIGIT";
5849 break;
5850 case DIGIT + ADD_NL:
5851 p = "DIGIT+NL";
5852 break;
5853 case NDIGIT:
5854 p = "NDIGIT";
5855 break;
5856 case NDIGIT + ADD_NL:
5857 p = "NDIGIT+NL";
5858 break;
5859 case HEX:
5860 p = "HEX";
5861 break;
5862 case HEX + ADD_NL:
5863 p = "HEX+NL";
5864 break;
5865 case NHEX:
5866 p = "NHEX";
5867 break;
5868 case NHEX + ADD_NL:
5869 p = "NHEX+NL";
5870 break;
5871 case OCTAL:
5872 p = "OCTAL";
5873 break;
5874 case OCTAL + ADD_NL:
5875 p = "OCTAL+NL";
5876 break;
5877 case NOCTAL:
5878 p = "NOCTAL";
5879 break;
5880 case NOCTAL + ADD_NL:
5881 p = "NOCTAL+NL";
5882 break;
5883 case WORD:
5884 p = "WORD";
5885 break;
5886 case WORD + ADD_NL:
5887 p = "WORD+NL";
5888 break;
5889 case NWORD:
5890 p = "NWORD";
5891 break;
5892 case NWORD + ADD_NL:
5893 p = "NWORD+NL";
5894 break;
5895 case HEAD:
5896 p = "HEAD";
5897 break;
5898 case HEAD + ADD_NL:
5899 p = "HEAD+NL";
5900 break;
5901 case NHEAD:
5902 p = "NHEAD";
5903 break;
5904 case NHEAD + ADD_NL:
5905 p = "NHEAD+NL";
5906 break;
5907 case ALPHA:
5908 p = "ALPHA";
5909 break;
5910 case ALPHA + ADD_NL:
5911 p = "ALPHA+NL";
5912 break;
5913 case NALPHA:
5914 p = "NALPHA";
5915 break;
5916 case NALPHA + ADD_NL:
5917 p = "NALPHA+NL";
5918 break;
5919 case LOWER:
5920 p = "LOWER";
5921 break;
5922 case LOWER + ADD_NL:
5923 p = "LOWER+NL";
5924 break;
5925 case NLOWER:
5926 p = "NLOWER";
5927 break;
5928 case NLOWER + ADD_NL:
5929 p = "NLOWER+NL";
5930 break;
5931 case UPPER:
5932 p = "UPPER";
5933 break;
5934 case UPPER + ADD_NL:
5935 p = "UPPER+NL";
5936 break;
5937 case NUPPER:
5938 p = "NUPPER";
5939 break;
5940 case NUPPER + ADD_NL:
5941 p = "NUPPER+NL";
5942 break;
5943 case BRANCH:
5944 p = "BRANCH";
5945 break;
5946 case EXACTLY:
5947 p = "EXACTLY";
5948 break;
5949 case NOTHING:
5950 p = "NOTHING";
5951 break;
5952 case BACK:
5953 p = "BACK";
5954 break;
5955 case END:
5956 p = "END";
5957 break;
5958 case MOPEN + 0:
5959 p = "MATCH START";
5960 break;
5961 case MOPEN + 1:
5962 case MOPEN + 2:
5963 case MOPEN + 3:
5964 case MOPEN + 4:
5965 case MOPEN + 5:
5966 case MOPEN + 6:
5967 case MOPEN + 7:
5968 case MOPEN + 8:
5969 case MOPEN + 9:
5970 sprintf(buf + STRLEN(buf), "MOPEN%d", OP(op) - MOPEN);
5971 p = NULL;
5972 break;
5973 case MCLOSE + 0:
5974 p = "MATCH END";
5975 break;
5976 case MCLOSE + 1:
5977 case MCLOSE + 2:
5978 case MCLOSE + 3:
5979 case MCLOSE + 4:
5980 case MCLOSE + 5:
5981 case MCLOSE + 6:
5982 case MCLOSE + 7:
5983 case MCLOSE + 8:
5984 case MCLOSE + 9:
5985 sprintf(buf + STRLEN(buf), "MCLOSE%d", OP(op) - MCLOSE);
5986 p = NULL;
5987 break;
5988 case BACKREF + 1:
5989 case BACKREF + 2:
5990 case BACKREF + 3:
5991 case BACKREF + 4:
5992 case BACKREF + 5:
5993 case BACKREF + 6:
5994 case BACKREF + 7:
5995 case BACKREF + 8:
5996 case BACKREF + 9:
5997 sprintf(buf + STRLEN(buf), "BACKREF%d", OP(op) - BACKREF);
5998 p = NULL;
5999 break;
6000 case NOPEN:
6001 p = "NOPEN";
6002 break;
6003 case NCLOSE:
6004 p = "NCLOSE";
6005 break;
6006#ifdef FEAT_SYN_HL
6007 case ZOPEN + 1:
6008 case ZOPEN + 2:
6009 case ZOPEN + 3:
6010 case ZOPEN + 4:
6011 case ZOPEN + 5:
6012 case ZOPEN + 6:
6013 case ZOPEN + 7:
6014 case ZOPEN + 8:
6015 case ZOPEN + 9:
6016 sprintf(buf + STRLEN(buf), "ZOPEN%d", OP(op) - ZOPEN);
6017 p = NULL;
6018 break;
6019 case ZCLOSE + 1:
6020 case ZCLOSE + 2:
6021 case ZCLOSE + 3:
6022 case ZCLOSE + 4:
6023 case ZCLOSE + 5:
6024 case ZCLOSE + 6:
6025 case ZCLOSE + 7:
6026 case ZCLOSE + 8:
6027 case ZCLOSE + 9:
6028 sprintf(buf + STRLEN(buf), "ZCLOSE%d", OP(op) - ZCLOSE);
6029 p = NULL;
6030 break;
6031 case ZREF + 1:
6032 case ZREF + 2:
6033 case ZREF + 3:
6034 case ZREF + 4:
6035 case ZREF + 5:
6036 case ZREF + 6:
6037 case ZREF + 7:
6038 case ZREF + 8:
6039 case ZREF + 9:
6040 sprintf(buf + STRLEN(buf), "ZREF%d", OP(op) - ZREF);
6041 p = NULL;
6042 break;
6043#endif
6044 case STAR:
6045 p = "STAR";
6046 break;
6047 case PLUS:
6048 p = "PLUS";
6049 break;
6050 case NOMATCH:
6051 p = "NOMATCH";
6052 break;
6053 case MATCH:
6054 p = "MATCH";
6055 break;
6056 case BEHIND:
6057 p = "BEHIND";
6058 break;
6059 case NOBEHIND:
6060 p = "NOBEHIND";
6061 break;
6062 case SUBPAT:
6063 p = "SUBPAT";
6064 break;
6065 case BRACE_LIMITS:
6066 p = "BRACE_LIMITS";
6067 break;
6068 case BRACE_SIMPLE:
6069 p = "BRACE_SIMPLE";
6070 break;
6071 case BRACE_COMPLEX + 0:
6072 case BRACE_COMPLEX + 1:
6073 case BRACE_COMPLEX + 2:
6074 case BRACE_COMPLEX + 3:
6075 case BRACE_COMPLEX + 4:
6076 case BRACE_COMPLEX + 5:
6077 case BRACE_COMPLEX + 6:
6078 case BRACE_COMPLEX + 7:
6079 case BRACE_COMPLEX + 8:
6080 case BRACE_COMPLEX + 9:
6081 sprintf(buf + STRLEN(buf), "BRACE_COMPLEX%d", OP(op) - BRACE_COMPLEX);
6082 p = NULL;
6083 break;
6084#ifdef FEAT_MBYTE
6085 case MULTIBYTECODE:
6086 p = "MULTIBYTECODE";
6087 break;
6088#endif
6089 case NEWL:
6090 p = "NEWL";
6091 break;
6092 default:
6093 sprintf(buf + STRLEN(buf), "corrupt %d", OP(op));
6094 p = NULL;
6095 break;
6096 }
6097 if (p != NULL)
6098 (void) strcat(buf, p);
6099 return buf;
6100}
6101#endif
6102
6103#ifdef FEAT_MBYTE
6104static void mb_decompose __ARGS((int c, int *c1, int *c2, int *c3));
6105
6106typedef struct
6107{
6108 int a, b, c;
6109} decomp_T;
6110
6111
6112/* 0xfb20 - 0xfb4f */
6113decomp_T decomp_table[0xfb4f-0xfb20+1] =
6114{
6115 {0x5e2,0,0}, /* 0xfb20 alt ayin */
6116 {0x5d0,0,0}, /* 0xfb21 alt alef */
6117 {0x5d3,0,0}, /* 0xfb22 alt dalet */
6118 {0x5d4,0,0}, /* 0xfb23 alt he */
6119 {0x5db,0,0}, /* 0xfb24 alt kaf */
6120 {0x5dc,0,0}, /* 0xfb25 alt lamed */
6121 {0x5dd,0,0}, /* 0xfb26 alt mem-sofit */
6122 {0x5e8,0,0}, /* 0xfb27 alt resh */
6123 {0x5ea,0,0}, /* 0xfb28 alt tav */
6124 {'+', 0, 0}, /* 0xfb29 alt plus */
6125 {0x5e9, 0x5c1, 0}, /* 0xfb2a shin+shin-dot */
6126 {0x5e9, 0x5c2, 0}, /* 0xfb2b shin+sin-dot */
6127 {0x5e9, 0x5c1, 0x5bc}, /* 0xfb2c shin+shin-dot+dagesh */
6128 {0x5e9, 0x5c2, 0x5bc}, /* 0xfb2d shin+sin-dot+dagesh */
6129 {0x5d0, 0x5b7, 0}, /* 0xfb2e alef+patah */
6130 {0x5d0, 0x5b8, 0}, /* 0xfb2f alef+qamats */
6131 {0x5d0, 0x5b4, 0}, /* 0xfb30 alef+hiriq */
6132 {0x5d1, 0x5bc, 0}, /* 0xfb31 bet+dagesh */
6133 {0x5d2, 0x5bc, 0}, /* 0xfb32 gimel+dagesh */
6134 {0x5d3, 0x5bc, 0}, /* 0xfb33 dalet+dagesh */
6135 {0x5d4, 0x5bc, 0}, /* 0xfb34 he+dagesh */
6136 {0x5d5, 0x5bc, 0}, /* 0xfb35 vav+dagesh */
6137 {0x5d6, 0x5bc, 0}, /* 0xfb36 zayin+dagesh */
6138 {0xfb37, 0, 0}, /* 0xfb37 -- UNUSED */
6139 {0x5d8, 0x5bc, 0}, /* 0xfb38 tet+dagesh */
6140 {0x5d9, 0x5bc, 0}, /* 0xfb39 yud+dagesh */
6141 {0x5da, 0x5bc, 0}, /* 0xfb3a kaf sofit+dagesh */
6142 {0x5db, 0x5bc, 0}, /* 0xfb3b kaf+dagesh */
6143 {0x5dc, 0x5bc, 0}, /* 0xfb3c lamed+dagesh */
6144 {0xfb3d, 0, 0}, /* 0xfb3d -- UNUSED */
6145 {0x5de, 0x5bc, 0}, /* 0xfb3e mem+dagesh */
6146 {0xfb3f, 0, 0}, /* 0xfb3f -- UNUSED */
6147 {0x5e0, 0x5bc, 0}, /* 0xfb40 nun+dagesh */
6148 {0x5e1, 0x5bc, 0}, /* 0xfb41 samech+dagesh */
6149 {0xfb42, 0, 0}, /* 0xfb42 -- UNUSED */
6150 {0x5e3, 0x5bc, 0}, /* 0xfb43 pe sofit+dagesh */
6151 {0x5e4, 0x5bc,0}, /* 0xfb44 pe+dagesh */
6152 {0xfb45, 0, 0}, /* 0xfb45 -- UNUSED */
6153 {0x5e6, 0x5bc, 0}, /* 0xfb46 tsadi+dagesh */
6154 {0x5e7, 0x5bc, 0}, /* 0xfb47 qof+dagesh */
6155 {0x5e8, 0x5bc, 0}, /* 0xfb48 resh+dagesh */
6156 {0x5e9, 0x5bc, 0}, /* 0xfb49 shin+dagesh */
6157 {0x5ea, 0x5bc, 0}, /* 0xfb4a tav+dagesh */
6158 {0x5d5, 0x5b9, 0}, /* 0xfb4b vav+holam */
6159 {0x5d1, 0x5bf, 0}, /* 0xfb4c bet+rafe */
6160 {0x5db, 0x5bf, 0}, /* 0xfb4d kaf+rafe */
6161 {0x5e4, 0x5bf, 0}, /* 0xfb4e pe+rafe */
6162 {0x5d0, 0x5dc, 0} /* 0xfb4f alef-lamed */
6163};
6164
6165 static void
6166mb_decompose(c, c1, c2, c3)
6167 int c, *c1, *c2, *c3;
6168{
6169 decomp_T d;
6170
6171 if (c >= 0x4b20 && c <= 0xfb4f)
6172 {
6173 d = decomp_table[c - 0xfb20];
6174 *c1 = d.a;
6175 *c2 = d.b;
6176 *c3 = d.c;
6177 }
6178 else
6179 {
6180 *c1 = c;
6181 *c2 = *c3 = 0;
6182 }
6183}
6184#endif
6185
6186/*
6187 * Compare two strings, ignore case if ireg_ic set.
6188 * Return 0 if strings match, non-zero otherwise.
6189 * Correct the length "*n" when composing characters are ignored.
6190 */
6191 static int
6192cstrncmp(s1, s2, n)
6193 char_u *s1, *s2;
6194 int *n;
6195{
6196 int result;
6197
6198 if (!ireg_ic)
6199 result = STRNCMP(s1, s2, *n);
6200 else
6201 result = MB_STRNICMP(s1, s2, *n);
6202
6203#ifdef FEAT_MBYTE
6204 /* if it failed and it's utf8 and we want to combineignore: */
6205 if (result != 0 && enc_utf8 && ireg_icombine)
6206 {
6207 char_u *str1, *str2;
6208 int c1, c2, c11, c12;
6209 int ix;
6210 int junk;
6211
6212 /* we have to handle the strcmp ourselves, since it is necessary to
6213 * deal with the composing characters by ignoring them: */
6214 str1 = s1;
6215 str2 = s2;
6216 c1 = c2 = 0;
6217 for (ix = 0; ix < *n; )
6218 {
6219 c1 = mb_ptr2char_adv(&str1);
6220 c2 = mb_ptr2char_adv(&str2);
6221 ix += utf_char2len(c1);
6222
6223 /* decompose the character if necessary, into 'base' characters
6224 * because I don't care about Arabic, I will hard-code the Hebrew
6225 * which I *do* care about! So sue me... */
6226 if (c1 != c2 && (!ireg_ic || utf_fold(c1) != utf_fold(c2)))
6227 {
6228 /* decomposition necessary? */
6229 mb_decompose(c1, &c11, &junk, &junk);
6230 mb_decompose(c2, &c12, &junk, &junk);
6231 c1 = c11;
6232 c2 = c12;
6233 if (c11 != c12 && (!ireg_ic || utf_fold(c11) != utf_fold(c12)))
6234 break;
6235 }
6236 }
6237 result = c2 - c1;
6238 if (result == 0)
6239 *n = (int)(str2 - s2);
6240 }
6241#endif
6242
6243 return result;
6244}
6245
6246/*
6247 * cstrchr: This function is used a lot for simple searches, keep it fast!
6248 */
6249 static char_u *
6250cstrchr(s, c)
6251 char_u *s;
6252 int c;
6253{
6254 char_u *p;
6255 int cc;
6256
6257 if (!ireg_ic
6258#ifdef FEAT_MBYTE
6259 || (!enc_utf8 && mb_char2len(c) > 1)
6260#endif
6261 )
6262 return vim_strchr(s, c);
6263
6264 /* tolower() and toupper() can be slow, comparing twice should be a lot
6265 * faster (esp. when using MS Visual C++!).
6266 * For UTF-8 need to use folded case. */
6267#ifdef FEAT_MBYTE
6268 if (enc_utf8 && c > 0x80)
6269 cc = utf_fold(c);
6270 else
6271#endif
6272 if (isupper(c))
6273 cc = TOLOWER_LOC(c);
6274 else if (islower(c))
6275 cc = TOUPPER_LOC(c);
6276 else
6277 return vim_strchr(s, c);
6278
6279#ifdef FEAT_MBYTE
6280 if (has_mbyte)
6281 {
6282 for (p = s; *p != NUL; p += (*mb_ptr2len_check)(p))
6283 {
6284 if (enc_utf8 && c > 0x80)
6285 {
6286 if (utf_fold(utf_ptr2char(p)) == cc)
6287 return p;
6288 }
6289 else if (*p == c || *p == cc)
6290 return p;
6291 }
6292 }
6293 else
6294#endif
6295 /* Faster version for when there are no multi-byte characters. */
6296 for (p = s; *p != NUL; ++p)
6297 if (*p == c || *p == cc)
6298 return p;
6299
6300 return NULL;
6301}
6302
6303/***************************************************************
6304 * regsub stuff *
6305 ***************************************************************/
6306
6307/* This stuff below really confuses cc on an SGI -- webb */
6308#ifdef __sgi
6309# undef __ARGS
6310# define __ARGS(x) ()
6311#endif
6312
6313/*
6314 * We should define ftpr as a pointer to a function returning a pointer to
6315 * a function returning a pointer to a function ...
6316 * This is impossible, so we declare a pointer to a function returning a
6317 * pointer to a function returning void. This should work for all compilers.
6318 */
6319typedef void (*(*fptr) __ARGS((char_u *, int)))();
6320
6321static fptr do_upper __ARGS((char_u *, int));
6322static fptr do_Upper __ARGS((char_u *, int));
6323static fptr do_lower __ARGS((char_u *, int));
6324static fptr do_Lower __ARGS((char_u *, int));
6325
6326static int vim_regsub_both __ARGS((char_u *source, char_u *dest, int copy, int magic, int backslash));
6327
6328 static fptr
6329do_upper(d, c)
6330 char_u *d;
6331 int c;
6332{
6333 *d = TOUPPER_LOC(c);
6334
6335 return (fptr)NULL;
6336}
6337
6338 static fptr
6339do_Upper(d, c)
6340 char_u *d;
6341 int c;
6342{
6343 *d = TOUPPER_LOC(c);
6344
6345 return (fptr)do_Upper;
6346}
6347
6348 static fptr
6349do_lower(d, c)
6350 char_u *d;
6351 int c;
6352{
6353 *d = TOLOWER_LOC(c);
6354
6355 return (fptr)NULL;
6356}
6357
6358 static fptr
6359do_Lower(d, c)
6360 char_u *d;
6361 int c;
6362{
6363 *d = TOLOWER_LOC(c);
6364
6365 return (fptr)do_Lower;
6366}
6367
6368/*
6369 * regtilde(): Replace tildes in the pattern by the old pattern.
6370 *
6371 * Short explanation of the tilde: It stands for the previous replacement
6372 * pattern. If that previous pattern also contains a ~ we should go back a
6373 * step further... But we insert the previous pattern into the current one
6374 * and remember that.
6375 * This still does not handle the case where "magic" changes. TODO?
6376 *
6377 * The tildes are parsed once before the first call to vim_regsub().
6378 */
6379 char_u *
6380regtilde(source, magic)
6381 char_u *source;
6382 int magic;
6383{
6384 char_u *newsub = source;
6385 char_u *tmpsub;
6386 char_u *p;
6387 int len;
6388 int prevlen;
6389
6390 for (p = newsub; *p; ++p)
6391 {
6392 if ((*p == '~' && magic) || (*p == '\\' && *(p + 1) == '~' && !magic))
6393 {
6394 if (reg_prev_sub != NULL)
6395 {
6396 /* length = len(newsub) - 1 + len(prev_sub) + 1 */
6397 prevlen = (int)STRLEN(reg_prev_sub);
6398 tmpsub = alloc((unsigned)(STRLEN(newsub) + prevlen));
6399 if (tmpsub != NULL)
6400 {
6401 /* copy prefix */
6402 len = (int)(p - newsub); /* not including ~ */
6403 mch_memmove(tmpsub, newsub, (size_t)len);
6404 /* interpretate tilde */
6405 mch_memmove(tmpsub + len, reg_prev_sub, (size_t)prevlen);
6406 /* copy postfix */
6407 if (!magic)
6408 ++p; /* back off \ */
6409 STRCPY(tmpsub + len + prevlen, p + 1);
6410
6411 if (newsub != source) /* already allocated newsub */
6412 vim_free(newsub);
6413 newsub = tmpsub;
6414 p = newsub + len + prevlen;
6415 }
6416 }
6417 else if (magic)
6418 STRCPY(p, p + 1); /* remove '~' */
6419 else
6420 STRCPY(p, p + 2); /* remove '\~' */
6421 --p;
6422 }
6423 else
6424 {
6425 if (*p == '\\' && p[1]) /* skip escaped characters */
6426 ++p;
6427#ifdef FEAT_MBYTE
6428 if (has_mbyte)
6429 p += (*mb_ptr2len_check)(p) - 1;
6430#endif
6431 }
6432 }
6433
6434 vim_free(reg_prev_sub);
6435 if (newsub != source) /* newsub was allocated, just keep it */
6436 reg_prev_sub = newsub;
6437 else /* no ~ found, need to save newsub */
6438 reg_prev_sub = vim_strsave(newsub);
6439 return newsub;
6440}
6441
6442#ifdef FEAT_EVAL
6443static int can_f_submatch = FALSE; /* TRUE when submatch() can be used */
6444
6445/* These pointers are used instead of reg_match and reg_mmatch for
6446 * reg_submatch(). Needed for when the substitution string is an expression
6447 * that contains a call to substitute() and submatch(). */
6448static regmatch_T *submatch_match;
6449static regmmatch_T *submatch_mmatch;
6450#endif
6451
6452#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
6453/*
6454 * vim_regsub() - perform substitutions after a vim_regexec() or
6455 * vim_regexec_multi() match.
6456 *
6457 * If "copy" is TRUE really copy into "dest".
6458 * If "copy" is FALSE nothing is copied, this is just to find out the length
6459 * of the result.
6460 *
6461 * If "backslash" is TRUE, a backslash will be removed later, need to double
6462 * them to keep them, and insert a backslash before a CR to avoid it being
6463 * replaced with a line break later.
6464 *
6465 * Note: The matched text must not change between the call of
6466 * vim_regexec()/vim_regexec_multi() and vim_regsub()! It would make the back
6467 * references invalid!
6468 *
6469 * Returns the size of the replacement, including terminating NUL.
6470 */
6471 int
6472vim_regsub(rmp, source, dest, copy, magic, backslash)
6473 regmatch_T *rmp;
6474 char_u *source;
6475 char_u *dest;
6476 int copy;
6477 int magic;
6478 int backslash;
6479{
6480 reg_match = rmp;
6481 reg_mmatch = NULL;
6482 reg_maxline = 0;
6483 return vim_regsub_both(source, dest, copy, magic, backslash);
6484}
6485#endif
6486
6487 int
6488vim_regsub_multi(rmp, lnum, source, dest, copy, magic, backslash)
6489 regmmatch_T *rmp;
6490 linenr_T lnum;
6491 char_u *source;
6492 char_u *dest;
6493 int copy;
6494 int magic;
6495 int backslash;
6496{
6497 reg_match = NULL;
6498 reg_mmatch = rmp;
6499 reg_buf = curbuf; /* always works on the current buffer! */
6500 reg_firstlnum = lnum;
6501 reg_maxline = curbuf->b_ml.ml_line_count - lnum;
6502 return vim_regsub_both(source, dest, copy, magic, backslash);
6503}
6504
6505 static int
6506vim_regsub_both(source, dest, copy, magic, backslash)
6507 char_u *source;
6508 char_u *dest;
6509 int copy;
6510 int magic;
6511 int backslash;
6512{
6513 char_u *src;
6514 char_u *dst;
6515 char_u *s;
6516 int c;
6517 int no = -1;
6518 fptr func = (fptr)NULL;
6519 linenr_T clnum = 0; /* init for GCC */
6520 int len = 0; /* init for GCC */
6521#ifdef FEAT_EVAL
6522 static char_u *eval_result = NULL;
6523#endif
6524#ifdef FEAT_MBYTE
6525 int l;
6526#endif
6527
6528
6529 /* Be paranoid... */
6530 if (source == NULL || dest == NULL)
6531 {
6532 EMSG(_(e_null));
6533 return 0;
6534 }
6535 if (prog_magic_wrong())
6536 return 0;
6537 src = source;
6538 dst = dest;
6539
6540 /*
6541 * When the substitute part starts with "\=" evaluate it as an expression.
6542 */
6543 if (source[0] == '\\' && source[1] == '='
6544#ifdef FEAT_EVAL
6545 && !can_f_submatch /* can't do this recursively */
6546#endif
6547 )
6548 {
6549#ifdef FEAT_EVAL
6550 /* To make sure that the length doesn't change between checking the
6551 * length and copying the string, and to speed up things, the
6552 * resulting string is saved from the call with "copy" == FALSE to the
6553 * call with "copy" == TRUE. */
6554 if (copy)
6555 {
6556 if (eval_result != NULL)
6557 {
6558 STRCPY(dest, eval_result);
6559 dst += STRLEN(eval_result);
6560 vim_free(eval_result);
6561 eval_result = NULL;
6562 }
6563 }
6564 else
6565 {
6566 linenr_T save_reg_maxline;
6567 win_T *save_reg_win;
6568 int save_ireg_ic;
6569
6570 vim_free(eval_result);
6571
6572 /* The expression may contain substitute(), which calls us
6573 * recursively. Make sure submatch() gets the text from the first
6574 * level. Don't need to save "reg_buf", because
6575 * vim_regexec_multi() can't be called recursively. */
6576 submatch_match = reg_match;
6577 submatch_mmatch = reg_mmatch;
6578 save_reg_maxline = reg_maxline;
6579 save_reg_win = reg_win;
6580 save_ireg_ic = ireg_ic;
6581 can_f_submatch = TRUE;
6582
6583 eval_result = eval_to_string(source + 2, NULL);
6584 if (eval_result != NULL)
6585 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006586 for (s = eval_result; *s != NUL; mb_ptr_adv(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 {
6588 /* Change NL to CR, so that it becomes a line break.
6589 * Skip over a backslashed character. */
6590 if (*s == NL)
6591 *s = CAR;
6592 else if (*s == '\\' && s[1] != NUL)
6593 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 }
6595
6596 dst += STRLEN(eval_result);
6597 }
6598
6599 reg_match = submatch_match;
6600 reg_mmatch = submatch_mmatch;
6601 reg_maxline = save_reg_maxline;
6602 reg_win = save_reg_win;
6603 ireg_ic = save_ireg_ic;
6604 can_f_submatch = FALSE;
6605 }
6606#endif
6607 }
6608 else
6609 while ((c = *src++) != NUL)
6610 {
6611 if (c == '&' && magic)
6612 no = 0;
6613 else if (c == '\\' && *src != NUL)
6614 {
6615 if (*src == '&' && !magic)
6616 {
6617 ++src;
6618 no = 0;
6619 }
6620 else if ('0' <= *src && *src <= '9')
6621 {
6622 no = *src++ - '0';
6623 }
6624 else if (vim_strchr((char_u *)"uUlLeE", *src))
6625 {
6626 switch (*src++)
6627 {
6628 case 'u': func = (fptr)do_upper;
6629 continue;
6630 case 'U': func = (fptr)do_Upper;
6631 continue;
6632 case 'l': func = (fptr)do_lower;
6633 continue;
6634 case 'L': func = (fptr)do_Lower;
6635 continue;
6636 case 'e':
6637 case 'E': func = (fptr)NULL;
6638 continue;
6639 }
6640 }
6641 }
6642 if (no < 0) /* Ordinary character. */
6643 {
6644 if (c == '\\' && *src != NUL)
6645 {
6646 /* Check for abbreviations -- webb */
6647 switch (*src)
6648 {
6649 case 'r': c = CAR; ++src; break;
6650 case 'n': c = NL; ++src; break;
6651 case 't': c = TAB; ++src; break;
6652 /* Oh no! \e already has meaning in subst pat :-( */
6653 /* case 'e': c = ESC; ++src; break; */
6654 case 'b': c = Ctrl_H; ++src; break;
6655
6656 /* If "backslash" is TRUE the backslash will be removed
6657 * later. Used to insert a literal CR. */
6658 default: if (backslash)
6659 {
6660 if (copy)
6661 *dst = '\\';
6662 ++dst;
6663 }
6664 c = *src++;
6665 }
6666 }
6667
6668 /* Write to buffer, if copy is set. */
6669#ifdef FEAT_MBYTE
6670 if (has_mbyte && (l = (*mb_ptr2len_check)(src - 1)) > 1)
6671 {
6672 /* TODO: should use "func" here. */
6673 if (copy)
6674 mch_memmove(dst, src - 1, l);
6675 dst += l - 1;
6676 src += l - 1;
6677 }
6678 else
6679 {
6680#endif
6681 if (copy)
6682 {
6683 if (func == (fptr)NULL) /* just copy */
6684 *dst = c;
6685 else /* change case */
6686 func = (fptr)(func(dst, c));
6687 /* Turbo C complains without the typecast */
6688 }
6689#ifdef FEAT_MBYTE
6690 }
6691#endif
6692 dst++;
6693 }
6694 else
6695 {
6696 if (REG_MULTI)
6697 {
6698 clnum = reg_mmatch->startpos[no].lnum;
6699 if (clnum < 0 || reg_mmatch->endpos[no].lnum < 0)
6700 s = NULL;
6701 else
6702 {
6703 s = reg_getline(clnum) + reg_mmatch->startpos[no].col;
6704 if (reg_mmatch->endpos[no].lnum == clnum)
6705 len = reg_mmatch->endpos[no].col
6706 - reg_mmatch->startpos[no].col;
6707 else
6708 len = (int)STRLEN(s);
6709 }
6710 }
6711 else
6712 {
6713 s = reg_match->startp[no];
6714 if (reg_match->endp[no] == NULL)
6715 s = NULL;
6716 else
6717 len = (int)(reg_match->endp[no] - s);
6718 }
6719 if (s != NULL)
6720 {
6721 for (;;)
6722 {
6723 if (len == 0)
6724 {
6725 if (REG_MULTI)
6726 {
6727 if (reg_mmatch->endpos[no].lnum == clnum)
6728 break;
6729 if (copy)
6730 *dst = CAR;
6731 ++dst;
6732 s = reg_getline(++clnum);
6733 if (reg_mmatch->endpos[no].lnum == clnum)
6734 len = reg_mmatch->endpos[no].col;
6735 else
6736 len = (int)STRLEN(s);
6737 }
6738 else
6739 break;
6740 }
6741 else if (*s == NUL) /* we hit NUL. */
6742 {
6743 if (copy)
6744 EMSG(_(e_re_damg));
6745 goto exit;
6746 }
6747 else
6748 {
6749 if (backslash && (*s == CAR || *s == '\\'))
6750 {
6751 /*
6752 * Insert a backslash in front of a CR, otherwise
6753 * it will be replaced by a line break.
6754 * Number of backslashes will be halved later,
6755 * double them here.
6756 */
6757 if (copy)
6758 {
6759 dst[0] = '\\';
6760 dst[1] = *s;
6761 }
6762 dst += 2;
6763 }
6764#ifdef FEAT_MBYTE
6765 else if (has_mbyte && (l = (*mb_ptr2len_check)(s)) > 1)
6766 {
6767 /* TODO: should use "func" here. */
6768 if (copy)
6769 mch_memmove(dst, s, l);
6770 dst += l;
6771 s += l - 1;
6772 len -= l - 1;
6773 }
6774#endif
6775 else
6776 {
6777 if (copy)
6778 {
6779 if (func == (fptr)NULL) /* just copy */
6780 *dst = *s;
6781 else /* change case */
6782 func = (fptr)(func(dst, *s));
6783 /* Turbo C complains without the typecast */
6784 }
6785 ++dst;
6786 }
6787 ++s;
6788 --len;
6789 }
6790 }
6791 }
6792 no = -1;
6793 }
6794 }
6795 if (copy)
6796 *dst = NUL;
6797
6798exit:
6799 return (int)((dst - dest) + 1);
6800}
6801
6802#ifdef FEAT_EVAL
6803/*
6804 * Used for the submatch() function: get the string from tne n'th submatch in
6805 * allocated memory.
6806 * Returns NULL when not in a ":s" command and for a non-existing submatch.
6807 */
6808 char_u *
6809reg_submatch(no)
6810 int no;
6811{
6812 char_u *retval = NULL;
6813 char_u *s;
6814 int len;
6815 int round;
6816 linenr_T lnum;
6817
6818 if (!can_f_submatch)
6819 return NULL;
6820
6821 if (submatch_match == NULL)
6822 {
6823 /*
6824 * First round: compute the length and allocate memory.
6825 * Second round: copy the text.
6826 */
6827 for (round = 1; round <= 2; ++round)
6828 {
6829 lnum = submatch_mmatch->startpos[no].lnum;
6830 if (lnum < 0 || submatch_mmatch->endpos[no].lnum < 0)
6831 return NULL;
6832
6833 s = reg_getline(lnum) + submatch_mmatch->startpos[no].col;
6834 if (s == NULL) /* anti-crash check, cannot happen? */
6835 break;
6836 if (submatch_mmatch->endpos[no].lnum == lnum)
6837 {
6838 /* Within one line: take form start to end col. */
6839 len = submatch_mmatch->endpos[no].col
6840 - submatch_mmatch->startpos[no].col;
6841 if (round == 2)
6842 {
6843 STRNCPY(retval, s, len);
6844 retval[len] = NUL;
6845 }
6846 ++len;
6847 }
6848 else
6849 {
6850 /* Multiple lines: take start line from start col, middle
6851 * lines completely and end line up to end col. */
6852 len = (int)STRLEN(s);
6853 if (round == 2)
6854 {
6855 STRCPY(retval, s);
6856 retval[len] = '\n';
6857 }
6858 ++len;
6859 ++lnum;
6860 while (lnum < submatch_mmatch->endpos[no].lnum)
6861 {
6862 s = reg_getline(lnum++);
6863 if (round == 2)
6864 STRCPY(retval + len, s);
6865 len += (int)STRLEN(s);
6866 if (round == 2)
6867 retval[len] = '\n';
6868 ++len;
6869 }
6870 if (round == 2)
6871 STRNCPY(retval + len, reg_getline(lnum),
6872 submatch_mmatch->endpos[no].col);
6873 len += submatch_mmatch->endpos[no].col;
6874 if (round == 2)
6875 retval[len] = NUL;
6876 ++len;
6877 }
6878
6879 if (round == 1)
6880 {
6881 retval = lalloc((long_u)len, TRUE);
6882 if (s == NULL)
6883 return NULL;
6884 }
6885 }
6886 }
6887 else
6888 {
6889 if (submatch_match->endp[no] == NULL)
6890 retval = NULL;
6891 else
6892 {
6893 s = submatch_match->startp[no];
6894 retval = vim_strnsave(s, (int)(submatch_match->endp[no] - s));
6895 }
6896 }
6897
6898 return retval;
6899}
6900#endif