blob: 8a2643dcce903dbe045a734d5951000ad07cd117 [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
3076/*
3077 * Get pointer to the line "lnum", which is relative to "reg_firstlnum".
3078 */
3079 static char_u *
3080reg_getline(lnum)
3081 linenr_T lnum;
3082{
3083 /* when looking behind for a match/no-match lnum is negative. But we
3084 * can't go before line 1 */
3085 if (reg_firstlnum + lnum < 1)
3086 return NULL;
3087 return ml_get_buf(reg_buf, reg_firstlnum + lnum, FALSE);
3088}
3089
3090static regsave_T behind_pos;
3091
3092#ifdef FEAT_SYN_HL
3093static char_u *reg_startzp[NSUBEXP]; /* Workspace to mark beginning */
3094static char_u *reg_endzp[NSUBEXP]; /* and end of \z(...\) matches */
3095static lpos_T reg_startzpos[NSUBEXP]; /* idem, beginning pos */
3096static lpos_T reg_endzpos[NSUBEXP]; /* idem, end pos */
3097#endif
3098
3099/* TRUE if using multi-line regexp. */
3100#define REG_MULTI (reg_match == NULL)
3101
3102/*
3103 * Match a regexp against a string.
3104 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
3105 * Uses curbuf for line count and 'iskeyword'.
3106 *
3107 * Return TRUE if there is a match, FALSE if not.
3108 */
3109 int
3110vim_regexec(rmp, line, col)
3111 regmatch_T *rmp;
3112 char_u *line; /* string to match against */
3113 colnr_T col; /* column to start looking for match */
3114{
3115 reg_match = rmp;
3116 reg_mmatch = NULL;
3117 reg_maxline = 0;
3118 reg_line_lbr = FALSE;
3119 reg_win = NULL;
3120 ireg_ic = rmp->rm_ic;
3121#ifdef FEAT_MBYTE
3122 ireg_icombine = FALSE;
3123#endif
3124 return (vim_regexec_both(line, col) != 0);
3125}
3126
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003127#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
3128 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129/*
3130 * Like vim_regexec(), but consider a "\n" in "line" to be a line break.
3131 */
3132 int
3133vim_regexec_nl(rmp, line, col)
3134 regmatch_T *rmp;
3135 char_u *line; /* string to match against */
3136 colnr_T col; /* column to start looking for match */
3137{
3138 reg_match = rmp;
3139 reg_mmatch = NULL;
3140 reg_maxline = 0;
3141 reg_line_lbr = TRUE;
3142 reg_win = NULL;
3143 ireg_ic = rmp->rm_ic;
3144#ifdef FEAT_MBYTE
3145 ireg_icombine = FALSE;
3146#endif
3147 return (vim_regexec_both(line, col) != 0);
3148}
3149#endif
3150
3151/*
3152 * Match a regexp against multiple lines.
3153 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
3154 * Uses curbuf for line count and 'iskeyword'.
3155 *
3156 * Return zero if there is no match. Return number of lines contained in the
3157 * match otherwise.
3158 */
3159 long
3160vim_regexec_multi(rmp, win, buf, lnum, col)
3161 regmmatch_T *rmp;
3162 win_T *win; /* window in which to search or NULL */
3163 buf_T *buf; /* buffer in which to search */
3164 linenr_T lnum; /* nr of line to start looking for match */
3165 colnr_T col; /* column to start looking for match */
3166{
3167 long r;
3168 buf_T *save_curbuf = curbuf;
3169
3170 reg_match = NULL;
3171 reg_mmatch = rmp;
3172 reg_buf = buf;
3173 reg_win = win;
3174 reg_firstlnum = lnum;
3175 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
3176 reg_line_lbr = FALSE;
3177 ireg_ic = rmp->rmm_ic;
3178#ifdef FEAT_MBYTE
3179 ireg_icombine = FALSE;
3180#endif
3181
3182 /* Need to switch to buffer "buf" to make vim_iswordc() work. */
3183 curbuf = buf;
3184 r = vim_regexec_both(NULL, col);
3185 curbuf = save_curbuf;
3186
3187 return r;
3188}
3189
3190/*
3191 * Match a regexp against a string ("line" points to the string) or multiple
3192 * lines ("line" is NULL, use reg_getline()).
3193 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 static long
3195vim_regexec_both(line, col)
3196 char_u *line;
3197 colnr_T col; /* column to start looking for match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198{
3199 regprog_T *prog;
3200 char_u *s;
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003201 long retval = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202
3203 reg_tofree = NULL;
3204
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 if (REG_MULTI)
3206 {
3207 prog = reg_mmatch->regprog;
3208 line = reg_getline((linenr_T)0);
3209 reg_startpos = reg_mmatch->startpos;
3210 reg_endpos = reg_mmatch->endpos;
3211 }
3212 else
3213 {
3214 prog = reg_match->regprog;
3215 reg_startp = reg_match->startp;
3216 reg_endp = reg_match->endp;
3217 }
3218
3219 /* Be paranoid... */
3220 if (prog == NULL || line == NULL)
3221 {
3222 EMSG(_(e_null));
3223 goto theend;
3224 }
3225
3226 /* Check validity of program. */
3227 if (prog_magic_wrong())
3228 goto theend;
3229
3230 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
3231 if (prog->regflags & RF_ICASE)
3232 ireg_ic = TRUE;
3233 else if (prog->regflags & RF_NOICASE)
3234 ireg_ic = FALSE;
3235
3236#ifdef FEAT_MBYTE
3237 /* If pattern contains "\Z" overrule value of ireg_icombine */
3238 if (prog->regflags & RF_ICOMBINE)
3239 ireg_icombine = TRUE;
3240#endif
3241
3242 /* If there is a "must appear" string, look for it. */
3243 if (prog->regmust != NULL)
3244 {
3245 int c;
3246
3247#ifdef FEAT_MBYTE
3248 if (has_mbyte)
3249 c = (*mb_ptr2char)(prog->regmust);
3250 else
3251#endif
3252 c = *prog->regmust;
3253 s = line + col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003254
3255 /*
3256 * This is used very often, esp. for ":global". Use three versions of
3257 * the loop to avoid overhead of conditions.
3258 */
3259 if (!ireg_ic
3260#ifdef FEAT_MBYTE
3261 && !has_mbyte
3262#endif
3263 )
3264 while ((s = vim_strbyte(s, c)) != NULL)
3265 {
3266 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3267 break; /* Found it. */
3268 ++s;
3269 }
3270#ifdef FEAT_MBYTE
3271 else if (!ireg_ic || (!enc_utf8 && mb_char2len(c) > 1))
3272 while ((s = vim_strchr(s, c)) != NULL)
3273 {
3274 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3275 break; /* Found it. */
3276 mb_ptr_adv(s);
3277 }
3278#endif
3279 else
3280 while ((s = cstrchr(s, c)) != NULL)
3281 {
3282 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3283 break; /* Found it. */
3284 mb_ptr_adv(s);
3285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 if (s == NULL) /* Not present. */
3287 goto theend;
3288 }
3289
3290 regline = line;
3291 reglnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292
3293 /* Simplest case: Anchored match need be tried only once. */
3294 if (prog->reganch)
3295 {
3296 int c;
3297
3298#ifdef FEAT_MBYTE
3299 if (has_mbyte)
3300 c = (*mb_ptr2char)(regline + col);
3301 else
3302#endif
3303 c = regline[col];
3304 if (prog->regstart == NUL
3305 || prog->regstart == c
3306 || (ireg_ic && ((
3307#ifdef FEAT_MBYTE
3308 (enc_utf8 && utf_fold(prog->regstart) == utf_fold(c)))
3309 || (c < 255 && prog->regstart < 255 &&
3310#endif
3311 TOLOWER_LOC(prog->regstart) == TOLOWER_LOC(c)))))
3312 retval = regtry(prog, col);
3313 else
3314 retval = 0;
3315 }
3316 else
3317 {
3318 /* Messy cases: unanchored match. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003319 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 {
3321 if (prog->regstart != NUL)
3322 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003323 /* Skip until the char we know it must start with.
3324 * Used often, do some work to avoid call overhead. */
3325 if (!ireg_ic
3326#ifdef FEAT_MBYTE
3327 && !has_mbyte
3328#endif
3329 )
3330 s = vim_strbyte(regline + col, prog->regstart);
3331 else
3332 s = cstrchr(regline + col, prog->regstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 if (s == NULL)
3334 {
3335 retval = 0;
3336 break;
3337 }
3338 col = (int)(s - regline);
3339 }
3340
3341 retval = regtry(prog, col);
3342 if (retval > 0)
3343 break;
3344
3345 /* if not currently on the first line, get it again */
3346 if (reglnum != 0)
3347 {
3348 regline = reg_getline((linenr_T)0);
3349 reglnum = 0;
3350 }
3351 if (regline[col] == NUL)
3352 break;
3353#ifdef FEAT_MBYTE
3354 if (has_mbyte)
3355 col += (*mb_ptr2len_check)(regline + col);
3356 else
3357#endif
3358 ++col;
3359 }
3360 }
3361
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362theend:
3363 /* Didn't find a match. */
3364 vim_free(reg_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 return retval;
3366}
3367
3368#ifdef FEAT_SYN_HL
3369static reg_extmatch_T *make_extmatch __ARGS((void));
3370
3371/*
3372 * Create a new extmatch and mark it as referenced once.
3373 */
3374 static reg_extmatch_T *
3375make_extmatch()
3376{
3377 reg_extmatch_T *em;
3378
3379 em = (reg_extmatch_T *)alloc_clear((unsigned)sizeof(reg_extmatch_T));
3380 if (em != NULL)
3381 em->refcnt = 1;
3382 return em;
3383}
3384
3385/*
3386 * Add a reference to an extmatch.
3387 */
3388 reg_extmatch_T *
3389ref_extmatch(em)
3390 reg_extmatch_T *em;
3391{
3392 if (em != NULL)
3393 em->refcnt++;
3394 return em;
3395}
3396
3397/*
3398 * Remove a reference to an extmatch. If there are no references left, free
3399 * the info.
3400 */
3401 void
3402unref_extmatch(em)
3403 reg_extmatch_T *em;
3404{
3405 int i;
3406
3407 if (em != NULL && --em->refcnt <= 0)
3408 {
3409 for (i = 0; i < NSUBEXP; ++i)
3410 vim_free(em->matches[i]);
3411 vim_free(em);
3412 }
3413}
3414#endif
3415
3416/*
3417 * regtry - try match of "prog" with at regline["col"].
3418 * Returns 0 for failure, number of lines contained in the match otherwise.
3419 */
3420 static long
3421regtry(prog, col)
3422 regprog_T *prog;
3423 colnr_T col;
3424{
3425 reginput = regline + col;
3426 need_clear_subexpr = TRUE;
3427#ifdef FEAT_SYN_HL
3428 /* Clear the external match subpointers if necessary. */
3429 if (prog->reghasz == REX_SET)
3430 need_clear_zsubexpr = TRUE;
3431#endif
3432
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003433 if (regmatch(prog->program + 1) == 0)
3434 return 0;
3435
3436 cleanup_subexpr();
3437 if (REG_MULTI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003439 if (reg_startpos[0].lnum < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003441 reg_startpos[0].lnum = 0;
3442 reg_startpos[0].col = col;
3443 }
3444 if (reg_endpos[0].lnum < 0)
3445 {
3446 reg_endpos[0].lnum = reglnum;
3447 reg_endpos[0].col = (int)(reginput - regline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 }
3449 else
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003450 /* Use line number of "\ze". */
3451 reglnum = reg_endpos[0].lnum;
3452 }
3453 else
3454 {
3455 if (reg_startp[0] == NULL)
3456 reg_startp[0] = regline + col;
3457 if (reg_endp[0] == NULL)
3458 reg_endp[0] = reginput;
3459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460#ifdef FEAT_SYN_HL
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003461 /* Package any found \z(...\) matches for export. Default is none. */
3462 unref_extmatch(re_extmatch_out);
3463 re_extmatch_out = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003465 if (prog->reghasz == REX_SET)
3466 {
3467 int i;
3468
3469 cleanup_zsubexpr();
3470 re_extmatch_out = make_extmatch();
3471 for (i = 0; i < NSUBEXP; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003473 if (REG_MULTI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003475 /* Only accept single line matches. */
3476 if (reg_startzpos[i].lnum >= 0
3477 && reg_endzpos[i].lnum == reg_startzpos[i].lnum)
3478 re_extmatch_out->matches[i] =
3479 vim_strnsave(reg_getline(reg_startzpos[i].lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 + reg_startzpos[i].col,
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003481 reg_endzpos[i].col - reg_startzpos[i].col);
3482 }
3483 else
3484 {
3485 if (reg_startzp[i] != NULL && reg_endzp[i] != NULL)
3486 re_extmatch_out->matches[i] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 vim_strnsave(reg_startzp[i],
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003488 (int)(reg_endzp[i] - reg_startzp[i]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 }
3490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 }
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003492#endif
3493 return 1 + reglnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494}
3495
3496#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497static int reg_prev_class __ARGS((void));
3498
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499/*
3500 * Get class of previous character.
3501 */
3502 static int
3503reg_prev_class()
3504{
3505 if (reginput > regline)
3506 return mb_get_class(reginput - 1
3507 - (*mb_head_off)(regline, reginput - 1));
3508 return -1;
3509}
3510
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003512#define ADVANCE_REGINPUT() mb_ptr_adv(reginput)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513
3514/*
3515 * The arguments from BRACE_LIMITS are stored here. They are actually local
3516 * to regmatch(), but they are here to reduce the amount of stack space used
3517 * (it can be called recursively many times).
3518 */
3519static long bl_minval;
3520static long bl_maxval;
3521
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00003522/* Values for rs_state in regitem_T. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003523typedef enum regstate_E
3524{
3525 RS_NOPEN = 0 /* NOPEN and NCLOSE */
3526 , RS_MOPEN /* MOPEN + [0-9] */
3527 , RS_MCLOSE /* MCLOSE + [0-9] */
3528#ifdef FEAT_SYN_HL
3529 , RS_ZOPEN /* ZOPEN + [0-9] */
3530 , RS_ZCLOSE /* ZCLOSE + [0-9] */
3531#endif
3532 , RS_BRANCH /* BRANCH */
3533 , RS_BRCPLX_MORE /* BRACE_COMPLEX and trying one more match */
3534 , RS_BRCPLX_LONG /* BRACE_COMPLEX and trying longest match */
3535 , RS_BRCPLX_SHORT /* BRACE_COMPLEX and trying shortest match */
3536 , RS_NOMATCH /* NOMATCH */
3537 , RS_BEHIND1 /* BEHIND / NOBEHIND matching rest */
3538 , RS_BEHIND2 /* BEHIND / NOBEHIND matching behind part */
3539 , RS_STAR_LONG /* STAR/PLUS/BRACE_SIMPLE longest match */
3540 , RS_STAR_SHORT /* STAR/PLUS/BRACE_SIMPLE shortest match */
3541} regstate_T;
3542
3543/*
3544 * When there are alternatives a regstate_T is put on the regstack to remember
3545 * what we are doing.
3546 * Before it may be another type of item, depending on rs_state, to remember
3547 * more things.
3548 */
3549typedef struct regitem_S
3550{
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00003551 regstate_T rs_state; /* what we are doing, one of RS_ above */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003552 char_u *rs_scan; /* current node in program */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003553 union
3554 {
3555 save_se_T sesave;
3556 regsave_T regsave;
3557 } rs_un; /* room for saving reginput */
3558 short rs_no; /* submatch nr */
3559} regitem_T;
3560
Bram Moolenaar582fd852005-03-28 20:58:01 +00003561static regitem_T *regstack_push __ARGS((garray_T *regstack, regstate_T state, char_u *scan));
3562static void regstack_pop __ARGS((garray_T *regstack, char_u **scan));
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003563
3564/* used for BEHIND and NOBEHIND matching */
3565typedef struct regbehind_S
3566{
Bram Moolenaar582fd852005-03-28 20:58:01 +00003567 regsave_T save_after;
3568 regsave_T save_behind;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003569} regbehind_T;
3570
3571/* used for STAR, PLUS and BRACE_SIMPLE matching */
3572typedef struct regstar_S
3573{
3574 int nextb; /* next byte */
3575 int nextb_ic; /* next byte reverse case */
3576 long count;
3577 long minval;
3578 long maxval;
3579} regstar_T;
3580
Bram Moolenaar582fd852005-03-28 20:58:01 +00003581/* used to store input position when a BACK was encountered, so that we now if
3582 * we made any progress since the last time. */
3583typedef struct backpos_S
3584{
3585 char_u *bp_scan; /* "scan" where BACK was encountered */
3586 regsave_T bp_pos; /* last input position */
3587} backpos_T;
3588
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589/*
3590 * regmatch - main matching routine
3591 *
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003592 * Conceptually the strategy is simple: Check to see whether the current node
3593 * matches, push an item onto the regstack and loop to see whether the rest
3594 * matches, and then act accordingly. In practice we make some effort to
3595 * avoid using the regstack, in particular by going through "ordinary" nodes
3596 * (that don't need to know whether the rest of the match failed) by a nested
3597 * loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 *
3599 * Returns TRUE when there is a match. Leaves reginput and reglnum just after
3600 * the last matched character.
3601 * Returns FALSE when there is no match. Leaves reginput and reglnum in an
3602 * undefined state!
3603 */
3604 static int
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003605regmatch(scan)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 char_u *scan; /* Current node. */
3607{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003608 char_u *next; /* Next node. */
3609 int op;
3610 int c;
Bram Moolenaar582fd852005-03-28 20:58:01 +00003611 garray_T regstack; /* stack with regitem_T items, sometimes
3612 preceded by regstar_T or regbehind_T. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003613 regitem_T *rp;
3614 int no;
3615 int status; /* one of the RA_ values: */
3616#define RA_FAIL 1 /* something failed, abort */
3617#define RA_CONT 2 /* continue in inner loop */
3618#define RA_BREAK 3 /* break inner loop */
3619#define RA_MATCH 4 /* successful match */
3620#define RA_NOMATCH 5 /* didn't match */
Bram Moolenaar582fd852005-03-28 20:58:01 +00003621 garray_T backpos; /* table with backpos_T for BACK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003623 /* Init the regstack empty. Use an item size of 1 byte, since we push
3624 * different things onto it. Use a large grow size to avoid reallocating
3625 * it too often. */
3626 ga_init2(&regstack, 1, 10000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627
Bram Moolenaar582fd852005-03-28 20:58:01 +00003628 ga_init2(&backpos, sizeof(backpos_T), 10);
3629
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003630 /*
Bram Moolenaar582fd852005-03-28 20:58:01 +00003631 * Repeat until "regstack" is empty.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003632 */
3633 for (;;)
3634 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 /* Some patterns my cause a long time to match, even though they are not
3636 * illegal. E.g., "\([a-z]\+\)\+Q". Allow breaking them with CTRL-C. */
3637 fast_breakcheck();
3638
3639#ifdef DEBUG
3640 if (scan != NULL && regnarrate)
3641 {
3642 mch_errmsg(regprop(scan));
3643 mch_errmsg("(\n");
3644 }
3645#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003646
3647 /*
Bram Moolenaar582fd852005-03-28 20:58:01 +00003648 * Repeat for items that can be matched sequentially, without using the
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003649 * regstack.
3650 */
3651 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003653 if (got_int || scan == NULL)
3654 {
3655 status = RA_FAIL;
3656 break;
3657 }
3658 status = RA_CONT;
3659
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660#ifdef DEBUG
3661 if (regnarrate)
3662 {
3663 mch_errmsg(regprop(scan));
3664 mch_errmsg("...\n");
3665# ifdef FEAT_SYN_HL
3666 if (re_extmatch_in != NULL)
3667 {
3668 int i;
3669
3670 mch_errmsg(_("External submatches:\n"));
3671 for (i = 0; i < NSUBEXP; i++)
3672 {
3673 mch_errmsg(" \"");
3674 if (re_extmatch_in->matches[i] != NULL)
3675 mch_errmsg(re_extmatch_in->matches[i]);
3676 mch_errmsg("\"\n");
3677 }
3678 }
3679# endif
3680 }
3681#endif
3682 next = regnext(scan);
3683
3684 op = OP(scan);
3685 /* Check for character class with NL added. */
3686 if (WITH_NL(op) && *reginput == NUL && reglnum < reg_maxline)
3687 {
3688 reg_nextline();
3689 }
3690 else if (reg_line_lbr && WITH_NL(op) && *reginput == '\n')
3691 {
3692 ADVANCE_REGINPUT();
3693 }
3694 else
3695 {
3696 if (WITH_NL(op))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003697 op -= ADD_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698#ifdef FEAT_MBYTE
3699 if (has_mbyte)
3700 c = (*mb_ptr2char)(reginput);
3701 else
3702#endif
3703 c = *reginput;
3704 switch (op)
3705 {
3706 case BOL:
3707 if (reginput != regline)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003708 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 break;
3710
3711 case EOL:
3712 if (c != NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003713 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 break;
3715
3716 case RE_BOF:
3717 /* Passing -1 to the getline() function provided for the search
3718 * should always return NULL if the current line is the first
3719 * line of the file. */
3720 if (reglnum != 0 || reginput != regline
3721 || (REG_MULTI && reg_getline((linenr_T)-1) != NULL))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003722 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 break;
3724
3725 case RE_EOF:
3726 if (reglnum != reg_maxline || c != NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003727 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 break;
3729
3730 case CURSOR:
3731 /* Check if the buffer is in a window and compare the
3732 * reg_win->w_cursor position to the match position. */
3733 if (reg_win == NULL
3734 || (reglnum + reg_firstlnum != reg_win->w_cursor.lnum)
3735 || ((colnr_T)(reginput - regline) != reg_win->w_cursor.col))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003736 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 break;
3738
3739 case RE_LNUM:
3740 if (!REG_MULTI || !re_num_cmp((long_u)(reglnum + reg_firstlnum),
3741 scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003742 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 break;
3744
3745 case RE_COL:
3746 if (!re_num_cmp((long_u)(reginput - regline) + 1, scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003747 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 break;
3749
3750 case RE_VCOL:
3751 if (!re_num_cmp((long_u)win_linetabsize(
3752 reg_win == NULL ? curwin : reg_win,
3753 regline, (colnr_T)(reginput - regline)) + 1, scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003754 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 break;
3756
3757 case BOW: /* \<word; reginput points to w */
3758 if (c == NUL) /* Can't match at end of line */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003759 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760#ifdef FEAT_MBYTE
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003761 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 {
3763 int this_class;
3764
3765 /* Get class of current and previous char (if it exists). */
3766 this_class = mb_get_class(reginput);
3767 if (this_class <= 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003768 status = RA_NOMATCH; /* not on a word at all */
3769 else if (reg_prev_class() == this_class)
3770 status = RA_NOMATCH; /* previous char is in same word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 }
3772#endif
3773 else
3774 {
3775 if (!vim_iswordc(c)
3776 || (reginput > regline && vim_iswordc(reginput[-1])))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003777 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 }
3779 break;
3780
3781 case EOW: /* word\>; reginput points after d */
3782 if (reginput == regline) /* Can't match at start of line */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003783 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784#ifdef FEAT_MBYTE
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003785 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 {
3787 int this_class, prev_class;
3788
3789 /* Get class of current and previous char (if it exists). */
3790 this_class = mb_get_class(reginput);
3791 prev_class = reg_prev_class();
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003792 if (this_class == prev_class
3793 || prev_class == 0 || prev_class == 1)
3794 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003797 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003799 if (!vim_iswordc(reginput[-1])
3800 || (reginput[0] != NUL && vim_iswordc(c)))
3801 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 }
3803 break; /* Matched with EOW */
3804
3805 case ANY:
3806 if (c == NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003807 status = RA_NOMATCH;
3808 else
3809 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 break;
3811
3812 case IDENT:
3813 if (!vim_isIDc(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003814 status = RA_NOMATCH;
3815 else
3816 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 break;
3818
3819 case SIDENT:
3820 if (VIM_ISDIGIT(*reginput) || !vim_isIDc(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003821 status = RA_NOMATCH;
3822 else
3823 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 break;
3825
3826 case KWORD:
3827 if (!vim_iswordp(reginput))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003828 status = RA_NOMATCH;
3829 else
3830 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 break;
3832
3833 case SKWORD:
3834 if (VIM_ISDIGIT(*reginput) || !vim_iswordp(reginput))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003835 status = RA_NOMATCH;
3836 else
3837 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 break;
3839
3840 case FNAME:
3841 if (!vim_isfilec(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003842 status = RA_NOMATCH;
3843 else
3844 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 break;
3846
3847 case SFNAME:
3848 if (VIM_ISDIGIT(*reginput) || !vim_isfilec(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003849 status = RA_NOMATCH;
3850 else
3851 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 break;
3853
3854 case PRINT:
3855 if (ptr2cells(reginput) != 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003856 status = RA_NOMATCH;
3857 else
3858 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 break;
3860
3861 case SPRINT:
3862 if (VIM_ISDIGIT(*reginput) || ptr2cells(reginput) != 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003863 status = RA_NOMATCH;
3864 else
3865 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 break;
3867
3868 case WHITE:
3869 if (!vim_iswhite(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003870 status = RA_NOMATCH;
3871 else
3872 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 break;
3874
3875 case NWHITE:
3876 if (c == NUL || vim_iswhite(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003877 status = RA_NOMATCH;
3878 else
3879 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 break;
3881
3882 case DIGIT:
3883 if (!ri_digit(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003884 status = RA_NOMATCH;
3885 else
3886 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 break;
3888
3889 case NDIGIT:
3890 if (c == NUL || ri_digit(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003891 status = RA_NOMATCH;
3892 else
3893 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 break;
3895
3896 case HEX:
3897 if (!ri_hex(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003898 status = RA_NOMATCH;
3899 else
3900 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 break;
3902
3903 case NHEX:
3904 if (c == NUL || ri_hex(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003905 status = RA_NOMATCH;
3906 else
3907 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 break;
3909
3910 case OCTAL:
3911 if (!ri_octal(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003912 status = RA_NOMATCH;
3913 else
3914 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 break;
3916
3917 case NOCTAL:
3918 if (c == NUL || ri_octal(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003919 status = RA_NOMATCH;
3920 else
3921 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 break;
3923
3924 case WORD:
3925 if (!ri_word(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003926 status = RA_NOMATCH;
3927 else
3928 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 break;
3930
3931 case NWORD:
3932 if (c == NUL || ri_word(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003933 status = RA_NOMATCH;
3934 else
3935 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 break;
3937
3938 case HEAD:
3939 if (!ri_head(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003940 status = RA_NOMATCH;
3941 else
3942 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 break;
3944
3945 case NHEAD:
3946 if (c == NUL || ri_head(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003947 status = RA_NOMATCH;
3948 else
3949 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 break;
3951
3952 case ALPHA:
3953 if (!ri_alpha(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003954 status = RA_NOMATCH;
3955 else
3956 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 break;
3958
3959 case NALPHA:
3960 if (c == NUL || ri_alpha(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003961 status = RA_NOMATCH;
3962 else
3963 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 break;
3965
3966 case LOWER:
3967 if (!ri_lower(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003968 status = RA_NOMATCH;
3969 else
3970 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 break;
3972
3973 case NLOWER:
3974 if (c == NUL || ri_lower(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003975 status = RA_NOMATCH;
3976 else
3977 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 break;
3979
3980 case UPPER:
3981 if (!ri_upper(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003982 status = RA_NOMATCH;
3983 else
3984 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 break;
3986
3987 case NUPPER:
3988 if (c == NUL || ri_upper(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003989 status = RA_NOMATCH;
3990 else
3991 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 break;
3993
3994 case EXACTLY:
3995 {
3996 int len;
3997 char_u *opnd;
3998
3999 opnd = OPERAND(scan);
4000 /* Inline the first byte, for speed. */
4001 if (*opnd != *reginput
4002 && (!ireg_ic || (
4003#ifdef FEAT_MBYTE
4004 !enc_utf8 &&
4005#endif
4006 TOLOWER_LOC(*opnd) != TOLOWER_LOC(*reginput))))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004007 status = RA_NOMATCH;
4008 else if (*opnd == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 {
4010 /* match empty string always works; happens when "~" is
4011 * empty. */
4012 }
4013 else if (opnd[1] == NUL
4014#ifdef FEAT_MBYTE
4015 && !(enc_utf8 && ireg_ic)
4016#endif
4017 )
4018 ++reginput; /* matched a single char */
4019 else
4020 {
4021 len = (int)STRLEN(opnd);
4022 /* Need to match first byte again for multi-byte. */
4023 if (cstrncmp(opnd, reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004024 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025#ifdef FEAT_MBYTE
4026 /* Check for following composing character. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004027 else if (enc_utf8
4028 && UTF_COMPOSINGLIKE(reginput, reginput + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 {
4030 /* raaron: This code makes a composing character get
4031 * ignored, which is the correct behavior (sometimes)
4032 * for voweled Hebrew texts. */
4033 if (!ireg_icombine)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004034 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004037 else
4038 reginput += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 }
4040 }
4041 break;
4042
4043 case ANYOF:
4044 case ANYBUT:
4045 if (c == NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004046 status = RA_NOMATCH;
4047 else if ((cstrchr(OPERAND(scan), c) == NULL) == (op == ANYOF))
4048 status = RA_NOMATCH;
4049 else
4050 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 break;
4052
4053#ifdef FEAT_MBYTE
4054 case MULTIBYTECODE:
4055 if (has_mbyte)
4056 {
4057 int i, len;
4058 char_u *opnd;
4059
4060 opnd = OPERAND(scan);
4061 /* Safety check (just in case 'encoding' was changed since
4062 * compiling the program). */
4063 if ((len = (*mb_ptr2len_check)(opnd)) < 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004064 {
4065 status = RA_NOMATCH;
4066 break;
4067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 for (i = 0; i < len; ++i)
4069 if (opnd[i] != reginput[i])
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004070 {
4071 status = RA_NOMATCH;
4072 break;
4073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 reginput += len;
4075 }
4076 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004077 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 break;
4079#endif
4080
4081 case NOTHING:
4082 break;
4083
4084 case BACK:
Bram Moolenaar582fd852005-03-28 20:58:01 +00004085 {
4086 int i;
4087 backpos_T *bp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088
Bram Moolenaar582fd852005-03-28 20:58:01 +00004089 /*
4090 * When we run into BACK we need to check if we don't keep
4091 * looping without matching any input. The second and later
4092 * times a BACK is encountered it fails if the input is still
4093 * at the same position as the previous time.
4094 * The positions are stored in "backpos" and found by the
4095 * current value of "scan", the position in the RE program.
4096 */
4097 bp = (backpos_T *)backpos.ga_data;
4098 for (i = 0; i < backpos.ga_len; ++i)
4099 if (bp[i].bp_scan == scan)
4100 break;
4101 if (i == backpos.ga_len)
4102 {
4103 /* First time at this BACK, make room to store the pos. */
4104 if (ga_grow(&backpos, 1) == FAIL)
4105 status = RA_FAIL;
4106 else
4107 {
4108 /* get "ga_data" again, it may have changed */
4109 bp = (backpos_T *)backpos.ga_data;
4110 bp[i].bp_scan = scan;
4111 ++backpos.ga_len;
4112 }
4113 }
4114 else if (reg_save_equal(&bp[i].bp_pos))
4115 /* Still at same position as last time, fail. */
4116 status = RA_NOMATCH;
4117
4118 if (status != RA_FAIL && status != RA_NOMATCH)
4119 reg_save(&bp[i].bp_pos, &backpos);
4120 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004121 break;
4122
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 case MOPEN + 0: /* Match start: \zs */
4124 case MOPEN + 1: /* \( */
4125 case MOPEN + 2:
4126 case MOPEN + 3:
4127 case MOPEN + 4:
4128 case MOPEN + 5:
4129 case MOPEN + 6:
4130 case MOPEN + 7:
4131 case MOPEN + 8:
4132 case MOPEN + 9:
4133 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 no = op - MOPEN;
4135 cleanup_subexpr();
Bram Moolenaar582fd852005-03-28 20:58:01 +00004136 rp = regstack_push(&regstack, RS_MOPEN, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004137 if (rp == NULL)
4138 status = RA_FAIL;
4139 else
4140 {
4141 rp->rs_no = no;
4142 save_se(&rp->rs_un.sesave, &reg_startpos[no],
4143 &reg_startp[no]);
4144 /* We simply continue and handle the result when done. */
4145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004147 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148
4149 case NOPEN: /* \%( */
4150 case NCLOSE: /* \) after \%( */
Bram Moolenaar582fd852005-03-28 20:58:01 +00004151 if (regstack_push(&regstack, RS_NOPEN, scan) == NULL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004152 status = RA_FAIL;
4153 /* We simply continue and handle the result when done. */
4154 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155
4156#ifdef FEAT_SYN_HL
4157 case ZOPEN + 1:
4158 case ZOPEN + 2:
4159 case ZOPEN + 3:
4160 case ZOPEN + 4:
4161 case ZOPEN + 5:
4162 case ZOPEN + 6:
4163 case ZOPEN + 7:
4164 case ZOPEN + 8:
4165 case ZOPEN + 9:
4166 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 no = op - ZOPEN;
4168 cleanup_zsubexpr();
Bram Moolenaar582fd852005-03-28 20:58:01 +00004169 rp = regstack_push(&regstack, RS_ZOPEN, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004170 if (rp == NULL)
4171 status = RA_FAIL;
4172 else
4173 {
4174 rp->rs_no = no;
4175 save_se(&rp->rs_un.sesave, &reg_startzpos[no],
4176 &reg_startzp[no]);
4177 /* We simply continue and handle the result when done. */
4178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004180 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181#endif
4182
4183 case MCLOSE + 0: /* Match end: \ze */
4184 case MCLOSE + 1: /* \) */
4185 case MCLOSE + 2:
4186 case MCLOSE + 3:
4187 case MCLOSE + 4:
4188 case MCLOSE + 5:
4189 case MCLOSE + 6:
4190 case MCLOSE + 7:
4191 case MCLOSE + 8:
4192 case MCLOSE + 9:
4193 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 no = op - MCLOSE;
4195 cleanup_subexpr();
Bram Moolenaar582fd852005-03-28 20:58:01 +00004196 rp = regstack_push(&regstack, RS_MCLOSE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004197 if (rp == NULL)
4198 status = RA_FAIL;
4199 else
4200 {
4201 rp->rs_no = no;
4202 save_se(&rp->rs_un.sesave, &reg_endpos[no], &reg_endp[no]);
4203 /* We simply continue and handle the result when done. */
4204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004206 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207
4208#ifdef FEAT_SYN_HL
4209 case ZCLOSE + 1: /* \) after \z( */
4210 case ZCLOSE + 2:
4211 case ZCLOSE + 3:
4212 case ZCLOSE + 4:
4213 case ZCLOSE + 5:
4214 case ZCLOSE + 6:
4215 case ZCLOSE + 7:
4216 case ZCLOSE + 8:
4217 case ZCLOSE + 9:
4218 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 no = op - ZCLOSE;
4220 cleanup_zsubexpr();
Bram Moolenaar582fd852005-03-28 20:58:01 +00004221 rp = regstack_push(&regstack, RS_ZCLOSE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004222 if (rp == NULL)
4223 status = RA_FAIL;
4224 else
4225 {
4226 rp->rs_no = no;
4227 save_se(&rp->rs_un.sesave, &reg_endzpos[no],
4228 &reg_endzp[no]);
4229 /* We simply continue and handle the result when done. */
4230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004232 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233#endif
4234
4235 case BACKREF + 1:
4236 case BACKREF + 2:
4237 case BACKREF + 3:
4238 case BACKREF + 4:
4239 case BACKREF + 5:
4240 case BACKREF + 6:
4241 case BACKREF + 7:
4242 case BACKREF + 8:
4243 case BACKREF + 9:
4244 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 int len;
4246 linenr_T clnum;
4247 colnr_T ccol;
4248 char_u *p;
4249
4250 no = op - BACKREF;
4251 cleanup_subexpr();
4252 if (!REG_MULTI) /* Single-line regexp */
4253 {
4254 if (reg_endp[no] == NULL)
4255 {
4256 /* Backref was not set: Match an empty string. */
4257 len = 0;
4258 }
4259 else
4260 {
4261 /* Compare current input with back-ref in the same
4262 * line. */
4263 len = (int)(reg_endp[no] - reg_startp[no]);
4264 if (cstrncmp(reg_startp[no], reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004265 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 }
4267 }
4268 else /* Multi-line regexp */
4269 {
4270 if (reg_endpos[no].lnum < 0)
4271 {
4272 /* Backref was not set: Match an empty string. */
4273 len = 0;
4274 }
4275 else
4276 {
4277 if (reg_startpos[no].lnum == reglnum
4278 && reg_endpos[no].lnum == reglnum)
4279 {
4280 /* Compare back-ref within the current line. */
4281 len = reg_endpos[no].col - reg_startpos[no].col;
4282 if (cstrncmp(regline + reg_startpos[no].col,
4283 reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004284 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 }
4286 else
4287 {
4288 /* Messy situation: Need to compare between two
4289 * lines. */
4290 ccol = reg_startpos[no].col;
4291 clnum = reg_startpos[no].lnum;
4292 for (;;)
4293 {
4294 /* Since getting one line may invalidate
4295 * the other, need to make copy. Slow! */
4296 if (regline != reg_tofree)
4297 {
4298 len = (int)STRLEN(regline);
4299 if (reg_tofree == NULL
4300 || len >= (int)reg_tofreelen)
4301 {
4302 len += 50; /* get some extra */
4303 vim_free(reg_tofree);
4304 reg_tofree = alloc(len);
4305 if (reg_tofree == NULL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004306 {
4307 status = RA_FAIL; /* outof memory!*/
4308 break;
4309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 reg_tofreelen = len;
4311 }
4312 STRCPY(reg_tofree, regline);
4313 reginput = reg_tofree
4314 + (reginput - regline);
4315 regline = reg_tofree;
4316 }
4317
4318 /* Get the line to compare with. */
4319 p = reg_getline(clnum);
4320 if (clnum == reg_endpos[no].lnum)
4321 len = reg_endpos[no].col - ccol;
4322 else
4323 len = (int)STRLEN(p + ccol);
4324
4325 if (cstrncmp(p + ccol, reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004326 {
4327 status = RA_NOMATCH; /* doesn't match */
4328 break;
4329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 if (clnum == reg_endpos[no].lnum)
4331 break; /* match and at end! */
4332 if (reglnum == reg_maxline)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004333 {
4334 status = RA_NOMATCH; /* text too short */
4335 break;
4336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337
4338 /* Advance to next line. */
4339 reg_nextline();
4340 ++clnum;
4341 ccol = 0;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004342 if (got_int)
4343 {
4344 status = RA_FAIL;
4345 break;
4346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 }
4348
4349 /* found a match! Note that regline may now point
4350 * to a copy of the line, that should not matter. */
4351 }
4352 }
4353 }
4354
4355 /* Matched the backref, skip over it. */
4356 reginput += len;
4357 }
4358 break;
4359
4360#ifdef FEAT_SYN_HL
4361 case ZREF + 1:
4362 case ZREF + 2:
4363 case ZREF + 3:
4364 case ZREF + 4:
4365 case ZREF + 5:
4366 case ZREF + 6:
4367 case ZREF + 7:
4368 case ZREF + 8:
4369 case ZREF + 9:
4370 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 int len;
4372
4373 cleanup_zsubexpr();
4374 no = op - ZREF;
4375 if (re_extmatch_in != NULL
4376 && re_extmatch_in->matches[no] != NULL)
4377 {
4378 len = (int)STRLEN(re_extmatch_in->matches[no]);
4379 if (cstrncmp(re_extmatch_in->matches[no],
4380 reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004381 status = RA_NOMATCH;
4382 else
4383 reginput += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 }
4385 else
4386 {
4387 /* Backref was not set: Match an empty string. */
4388 }
4389 }
4390 break;
4391#endif
4392
4393 case BRANCH:
4394 {
4395 if (OP(next) != BRANCH) /* No choice. */
4396 next = OPERAND(scan); /* Avoid recursion. */
4397 else
4398 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00004399 rp = regstack_push(&regstack, RS_BRANCH, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004400 if (rp == NULL)
4401 status = RA_FAIL;
4402 else
4403 status = RA_BREAK; /* rest is below */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 }
4405 }
4406 break;
4407
4408 case BRACE_LIMITS:
4409 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 if (OP(next) == BRACE_SIMPLE)
4411 {
4412 bl_minval = OPERAND_MIN(scan);
4413 bl_maxval = OPERAND_MAX(scan);
4414 }
4415 else if (OP(next) >= BRACE_COMPLEX
4416 && OP(next) < BRACE_COMPLEX + 10)
4417 {
4418 no = OP(next) - BRACE_COMPLEX;
4419 brace_min[no] = OPERAND_MIN(scan);
4420 brace_max[no] = OPERAND_MAX(scan);
4421 brace_count[no] = 0;
4422 }
4423 else
4424 {
4425 EMSG(_(e_internal)); /* Shouldn't happen */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004426 status = RA_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 }
4428 }
4429 break;
4430
4431 case BRACE_COMPLEX + 0:
4432 case BRACE_COMPLEX + 1:
4433 case BRACE_COMPLEX + 2:
4434 case BRACE_COMPLEX + 3:
4435 case BRACE_COMPLEX + 4:
4436 case BRACE_COMPLEX + 5:
4437 case BRACE_COMPLEX + 6:
4438 case BRACE_COMPLEX + 7:
4439 case BRACE_COMPLEX + 8:
4440 case BRACE_COMPLEX + 9:
4441 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 no = op - BRACE_COMPLEX;
4443 ++brace_count[no];
4444
4445 /* If not matched enough times yet, try one more */
4446 if (brace_count[no] <= (brace_min[no] <= brace_max[no]
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004447 ? brace_min[no] : brace_max[no]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00004449 rp = regstack_push(&regstack, RS_BRCPLX_MORE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004450 if (rp == NULL)
4451 status = RA_FAIL;
4452 else
4453 {
4454 rp->rs_no = no;
Bram Moolenaar582fd852005-03-28 20:58:01 +00004455 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004456 next = OPERAND(scan);
4457 /* We continue and handle the result when done. */
4458 }
4459 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 }
4461
4462 /* If matched enough times, may try matching some more */
4463 if (brace_min[no] <= brace_max[no])
4464 {
4465 /* Range is the normal way around, use longest match */
4466 if (brace_count[no] <= brace_max[no])
4467 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00004468 rp = regstack_push(&regstack, RS_BRCPLX_LONG, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004469 if (rp == NULL)
4470 status = RA_FAIL;
4471 else
4472 {
4473 rp->rs_no = no;
Bram Moolenaar582fd852005-03-28 20:58:01 +00004474 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004475 next = OPERAND(scan);
4476 /* We continue and handle the result when done. */
4477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 }
4479 }
4480 else
4481 {
4482 /* Range is backwards, use shortest match first */
4483 if (brace_count[no] <= brace_min[no])
4484 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00004485 rp = regstack_push(&regstack, RS_BRCPLX_SHORT, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004486 if (rp == NULL)
4487 status = RA_FAIL;
4488 else
4489 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00004490 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004491 /* We continue and handle the result when done. */
4492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 }
4494 }
4495 }
4496 break;
4497
4498 case BRACE_SIMPLE:
4499 case STAR:
4500 case PLUS:
4501 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004502 regstar_T rst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503
4504 /*
4505 * Lookahead to avoid useless match attempts when we know
4506 * what character comes next.
4507 */
4508 if (OP(next) == EXACTLY)
4509 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004510 rst.nextb = *OPERAND(next);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 if (ireg_ic)
4512 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004513 if (isupper(rst.nextb))
4514 rst.nextb_ic = TOLOWER_LOC(rst.nextb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004516 rst.nextb_ic = TOUPPER_LOC(rst.nextb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 }
4518 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004519 rst.nextb_ic = rst.nextb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 }
4521 else
4522 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004523 rst.nextb = NUL;
4524 rst.nextb_ic = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 }
4526 if (op != BRACE_SIMPLE)
4527 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004528 rst.minval = (op == STAR) ? 0 : 1;
4529 rst.maxval = MAX_LIMIT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 }
4531 else
4532 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004533 rst.minval = bl_minval;
4534 rst.maxval = bl_maxval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 }
4536
4537 /*
4538 * When maxval > minval, try matching as much as possible, up
4539 * to maxval. When maxval < minval, try matching at least the
4540 * minimal number (since the range is backwards, that's also
4541 * maxval!).
4542 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004543 rst.count = regrepeat(OPERAND(scan), rst.maxval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004546 status = RA_FAIL;
4547 break;
4548 }
4549 if (rst.minval <= rst.maxval
4550 ? rst.count >= rst.minval : rst.count >= rst.maxval)
4551 {
4552 /* It could match. Prepare for trying to match what
4553 * follows. The code is below. Parameters are stored in
4554 * a regstar_T on the regstack. */
Bram Moolenaar916b7af2005-03-16 09:52:38 +00004555 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004556 {
4557 EMSG(_(e_maxmempat));
4558 status = RA_FAIL;
4559 }
4560 else if (ga_grow(&regstack, sizeof(regstar_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004561 status = RA_FAIL;
4562 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004564 regstack.ga_len += sizeof(regstar_T);
4565 rp = regstack_push(&regstack, rst.minval <= rst.maxval
Bram Moolenaar582fd852005-03-28 20:58:01 +00004566 ? RS_STAR_LONG : RS_STAR_SHORT, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004567 if (rp == NULL)
4568 status = RA_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004570 {
4571 *(((regstar_T *)rp) - 1) = rst;
4572 status = RA_BREAK; /* skip the restore bits */
4573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 }
4575 }
4576 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004577 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 }
4580 break;
4581
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004582 case NOMATCH:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 case MATCH:
4584 case SUBPAT:
Bram Moolenaar582fd852005-03-28 20:58:01 +00004585 rp = regstack_push(&regstack, RS_NOMATCH, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004586 if (rp == NULL)
4587 status = RA_FAIL;
4588 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004590 rp->rs_no = op;
Bram Moolenaar582fd852005-03-28 20:58:01 +00004591 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004592 next = OPERAND(scan);
4593 /* We continue and handle the result when done. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 }
4595 break;
4596
4597 case BEHIND:
4598 case NOBEHIND:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004599 /* Need a bit of room to store extra positions. */
Bram Moolenaar916b7af2005-03-16 09:52:38 +00004600 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004601 {
4602 EMSG(_(e_maxmempat));
4603 status = RA_FAIL;
4604 }
4605 else if (ga_grow(&regstack, sizeof(regbehind_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004606 status = RA_FAIL;
4607 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004609 regstack.ga_len += sizeof(regbehind_T);
Bram Moolenaar582fd852005-03-28 20:58:01 +00004610 rp = regstack_push(&regstack, RS_BEHIND1, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004611 if (rp == NULL)
4612 status = RA_FAIL;
4613 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004615 rp->rs_no = op;
Bram Moolenaar582fd852005-03-28 20:58:01 +00004616 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004617 /* First try if what follows matches. If it does then we
4618 * check the behind match by looping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004621 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622
4623 case BHPOS:
4624 if (REG_MULTI)
4625 {
4626 if (behind_pos.rs_u.pos.col != (colnr_T)(reginput - regline)
4627 || behind_pos.rs_u.pos.lnum != reglnum)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004628 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 }
4630 else if (behind_pos.rs_u.ptr != reginput)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004631 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 break;
4633
4634 case NEWL:
4635 if ((c != NUL || reglnum == reg_maxline)
4636 && (c != '\n' || !reg_line_lbr))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004637 status = RA_NOMATCH;
4638 else if (reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 ADVANCE_REGINPUT();
4640 else
4641 reg_nextline();
4642 break;
4643
4644 case END:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004645 status = RA_MATCH; /* Success! */
4646 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647
4648 default:
4649 EMSG(_(e_re_corr));
4650#ifdef DEBUG
4651 printf("Illegal op code %d\n", op);
4652#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004653 status = RA_FAIL;
4654 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 }
4656 }
4657
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004658 /* If we can't continue sequentially, break the inner loop. */
4659 if (status != RA_CONT)
4660 break;
4661
4662 /* Continue in inner loop, advance to next item. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 scan = next;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004664
4665 } /* end of inner loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666
4667 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004668 * If there is something on the regstack execute the code for the state.
Bram Moolenaar582fd852005-03-28 20:58:01 +00004669 * If the state is popped then loop and use the older state.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004671 while (regstack.ga_len > 0 && status != RA_FAIL)
4672 {
4673 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len) - 1;
4674 switch (rp->rs_state)
4675 {
4676 case RS_NOPEN:
4677 /* Result is passed on as-is, simply pop the state. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00004678 regstack_pop(&regstack, &scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004679 break;
4680
4681 case RS_MOPEN:
4682 /* Pop the state. Restore pointers when there is no match. */
4683 if (status == RA_NOMATCH)
4684 restore_se(&rp->rs_un.sesave, &reg_startpos[rp->rs_no],
4685 &reg_startp[rp->rs_no]);
Bram Moolenaar582fd852005-03-28 20:58:01 +00004686 regstack_pop(&regstack, &scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004687 break;
4688
4689#ifdef FEAT_SYN_HL
4690 case RS_ZOPEN:
4691 /* Pop the state. Restore pointers when there is no match. */
4692 if (status == RA_NOMATCH)
4693 restore_se(&rp->rs_un.sesave, &reg_startzpos[rp->rs_no],
4694 &reg_startzp[rp->rs_no]);
Bram Moolenaar582fd852005-03-28 20:58:01 +00004695 regstack_pop(&regstack, &scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004696 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004698
4699 case RS_MCLOSE:
4700 /* Pop the state. Restore pointers when there is no match. */
4701 if (status == RA_NOMATCH)
4702 restore_se(&rp->rs_un.sesave, &reg_endpos[rp->rs_no],
4703 &reg_endp[rp->rs_no]);
Bram Moolenaar582fd852005-03-28 20:58:01 +00004704 regstack_pop(&regstack, &scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004705 break;
4706
4707#ifdef FEAT_SYN_HL
4708 case RS_ZCLOSE:
4709 /* Pop the state. Restore pointers when there is no match. */
4710 if (status == RA_NOMATCH)
4711 restore_se(&rp->rs_un.sesave, &reg_endzpos[rp->rs_no],
4712 &reg_endzp[rp->rs_no]);
Bram Moolenaar582fd852005-03-28 20:58:01 +00004713 regstack_pop(&regstack, &scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004714 break;
4715#endif
4716
4717 case RS_BRANCH:
4718 if (status == RA_MATCH)
4719 /* this branch matched, use it */
Bram Moolenaar582fd852005-03-28 20:58:01 +00004720 regstack_pop(&regstack, &scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004721 else
4722 {
4723 if (status != RA_BREAK)
4724 {
4725 /* After a non-matching branch: try next one. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00004726 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004727 scan = rp->rs_scan;
4728 }
4729 if (scan == NULL || OP(scan) != BRANCH)
4730 {
4731 /* no more branches, didn't find a match */
4732 status = RA_NOMATCH;
Bram Moolenaar582fd852005-03-28 20:58:01 +00004733 regstack_pop(&regstack, &scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004734 }
4735 else
4736 {
4737 /* Prepare to try a branch. */
4738 rp->rs_scan = regnext(scan);
Bram Moolenaar582fd852005-03-28 20:58:01 +00004739 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004740 scan = OPERAND(scan);
4741 }
4742 }
4743 break;
4744
4745 case RS_BRCPLX_MORE:
4746 /* Pop the state. Restore pointers when there is no match. */
4747 if (status == RA_NOMATCH)
4748 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00004749 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004750 --brace_count[rp->rs_no]; /* decrement match count */
4751 }
Bram Moolenaar582fd852005-03-28 20:58:01 +00004752 regstack_pop(&regstack, &scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004753 break;
4754
4755 case RS_BRCPLX_LONG:
4756 /* Pop the state. Restore pointers when there is no match. */
4757 if (status == RA_NOMATCH)
4758 {
4759 /* There was no match, but we did find enough matches. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00004760 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004761 --brace_count[rp->rs_no];
4762 /* continue with the items after "\{}" */
4763 status = RA_CONT;
4764 }
Bram Moolenaar582fd852005-03-28 20:58:01 +00004765 regstack_pop(&regstack, &scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004766 if (status == RA_CONT)
4767 scan = regnext(scan);
4768 break;
4769
4770 case RS_BRCPLX_SHORT:
4771 /* Pop the state. Restore pointers when there is no match. */
4772 if (status == RA_NOMATCH)
4773 /* There was no match, try to match one more item. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00004774 reg_restore(&rp->rs_un.regsave, &backpos);
4775 regstack_pop(&regstack, &scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004776 if (status == RA_NOMATCH)
4777 {
4778 scan = OPERAND(scan);
4779 status = RA_CONT;
4780 }
4781 break;
4782
4783 case RS_NOMATCH:
4784 /* Pop the state. If the operand matches for NOMATCH or
4785 * doesn't match for MATCH/SUBPAT, we fail. Otherwise backup,
4786 * except for SUBPAT, and continue with the next item. */
4787 if (status == (rp->rs_no == NOMATCH ? RA_MATCH : RA_NOMATCH))
4788 status = RA_NOMATCH;
4789 else
4790 {
4791 status = RA_CONT;
Bram Moolenaar582fd852005-03-28 20:58:01 +00004792 if (rp->rs_no != SUBPAT) /* zero-width */
4793 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004794 }
Bram Moolenaar582fd852005-03-28 20:58:01 +00004795 regstack_pop(&regstack, &scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004796 if (status == RA_CONT)
4797 scan = regnext(scan);
4798 break;
4799
4800 case RS_BEHIND1:
4801 if (status == RA_NOMATCH)
4802 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00004803 regstack_pop(&regstack, &scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004804 regstack.ga_len -= sizeof(regbehind_T);
4805 }
4806 else
4807 {
4808 /* The stuff after BEHIND/NOBEHIND matches. Now try if
4809 * the behind part does (not) match before the current
4810 * position in the input. This must be done at every
4811 * position in the input and checking if the match ends at
4812 * the current position. */
4813
4814 /* save the position after the found match for next */
Bram Moolenaar582fd852005-03-28 20:58:01 +00004815 reg_save(&(((regbehind_T *)rp) - 1)->save_after, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004816
4817 /* start looking for a match with operand at the current
4818 * postion. Go back one character until we find the
4819 * result, hitting the start of the line or the previous
4820 * line (for multi-line matching).
4821 * Set behind_pos to where the match should end, BHPOS
4822 * will match it. Save the current value. */
4823 (((regbehind_T *)rp) - 1)->save_behind = behind_pos;
4824 behind_pos = rp->rs_un.regsave;
4825
4826 rp->rs_state = RS_BEHIND2;
4827
Bram Moolenaar582fd852005-03-28 20:58:01 +00004828 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004829 scan = OPERAND(rp->rs_scan);
4830 }
4831 break;
4832
4833 case RS_BEHIND2:
4834 /*
4835 * Looping for BEHIND / NOBEHIND match.
4836 */
4837 if (status == RA_MATCH && reg_save_equal(&behind_pos))
4838 {
4839 /* found a match that ends where "next" started */
4840 behind_pos = (((regbehind_T *)rp) - 1)->save_behind;
4841 if (rp->rs_no == BEHIND)
Bram Moolenaar582fd852005-03-28 20:58:01 +00004842 reg_restore(&(((regbehind_T *)rp) - 1)->save_after,
4843 &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004844 else
4845 /* But we didn't want a match. */
4846 status = RA_NOMATCH;
Bram Moolenaar582fd852005-03-28 20:58:01 +00004847 regstack_pop(&regstack, &scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004848 regstack.ga_len -= sizeof(regbehind_T);
4849 }
4850 else
4851 {
4852 /* No match: Go back one character. May go to previous
4853 * line once. */
4854 no = OK;
4855 if (REG_MULTI)
4856 {
4857 if (rp->rs_un.regsave.rs_u.pos.col == 0)
4858 {
4859 if (rp->rs_un.regsave.rs_u.pos.lnum
4860 < behind_pos.rs_u.pos.lnum
4861 || reg_getline(
4862 --rp->rs_un.regsave.rs_u.pos.lnum)
4863 == NULL)
4864 no = FAIL;
4865 else
4866 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00004867 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004868 rp->rs_un.regsave.rs_u.pos.col =
4869 (colnr_T)STRLEN(regline);
4870 }
4871 }
4872 else
4873 --rp->rs_un.regsave.rs_u.pos.col;
4874 }
4875 else
4876 {
4877 if (rp->rs_un.regsave.rs_u.ptr == regline)
4878 no = FAIL;
4879 else
4880 --rp->rs_un.regsave.rs_u.ptr;
4881 }
4882 if (no == OK)
4883 {
4884 /* Advanced, prepare for finding match again. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00004885 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004886 scan = OPERAND(rp->rs_scan);
4887 }
4888 else
4889 {
4890 /* Can't advance. For NOBEHIND that's a match. */
4891 behind_pos = (((regbehind_T *)rp) - 1)->save_behind;
4892 if (rp->rs_no == NOBEHIND)
4893 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00004894 reg_restore(&(((regbehind_T *)rp) - 1)->save_after,
4895 &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004896 status = RA_MATCH;
4897 }
4898 else
4899 status = RA_NOMATCH;
Bram Moolenaar582fd852005-03-28 20:58:01 +00004900 regstack_pop(&regstack, &scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004901 regstack.ga_len -= sizeof(regbehind_T);
4902 }
4903 }
4904 break;
4905
4906 case RS_STAR_LONG:
4907 case RS_STAR_SHORT:
4908 {
4909 regstar_T *rst = ((regstar_T *)rp) - 1;
4910
4911 if (status == RA_MATCH)
4912 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00004913 regstack_pop(&regstack, &scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004914 regstack.ga_len -= sizeof(regstar_T);
4915 break;
4916 }
4917
4918 /* Tried once already, restore input pointers. */
4919 if (status != RA_BREAK)
Bram Moolenaar582fd852005-03-28 20:58:01 +00004920 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004921
4922 /* Repeat until we found a position where it could match. */
4923 for (;;)
4924 {
4925 if (status != RA_BREAK)
4926 {
4927 /* Tried first position already, advance. */
4928 if (rp->rs_state == RS_STAR_LONG)
4929 {
4930 /* Trying for longest matc, but couldn't or didn't
4931 * match -- back up one char. */
4932 if (--rst->count < rst->minval)
4933 break;
4934 if (reginput == regline)
4935 {
4936 /* backup to last char of previous line */
4937 --reglnum;
4938 regline = reg_getline(reglnum);
4939 /* Just in case regrepeat() didn't count
4940 * right. */
4941 if (regline == NULL)
4942 break;
4943 reginput = regline + STRLEN(regline);
4944 fast_breakcheck();
4945 }
4946 else
4947 mb_ptr_back(regline, reginput);
4948 }
4949 else
4950 {
4951 /* Range is backwards, use shortest match first.
4952 * Careful: maxval and minval are exchanged!
4953 * Couldn't or didn't match: try advancing one
4954 * char. */
4955 if (rst->count == rst->minval
4956 || regrepeat(OPERAND(rp->rs_scan), 1L) == 0)
4957 break;
4958 ++rst->count;
4959 }
4960 if (got_int)
4961 break;
4962 }
4963 else
4964 status = RA_NOMATCH;
4965
4966 /* If it could match, try it. */
4967 if (rst->nextb == NUL || *reginput == rst->nextb
4968 || *reginput == rst->nextb_ic)
4969 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00004970 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004971 scan = regnext(rp->rs_scan);
4972 status = RA_CONT;
4973 break;
4974 }
4975 }
4976 if (status != RA_CONT)
4977 {
4978 /* Failed. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00004979 regstack_pop(&regstack, &scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004980 regstack.ga_len -= sizeof(regstar_T);
4981 status = RA_NOMATCH;
4982 }
4983 }
4984 break;
4985 }
4986
4987 /* If we want to continue the inner loop or didn't pop a state contine
4988 * matching loop */
4989 if (status == RA_CONT || rp == (regitem_T *)
4990 ((char *)regstack.ga_data + regstack.ga_len) - 1)
4991 break;
4992 }
4993
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004994 /* May need to continue with the inner loop, starting at "scan". */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004995 if (status == RA_CONT)
4996 continue;
4997
4998 /*
4999 * If the regstack is empty or something failed we are done.
5000 */
5001 if (regstack.ga_len == 0 || status == RA_FAIL)
5002 {
5003 ga_clear(&regstack);
5004 if (scan == NULL)
5005 {
5006 /*
5007 * We get here only if there's trouble -- normally "case END" is
5008 * the terminating point.
5009 */
5010 EMSG(_(e_re_corr));
5011#ifdef DEBUG
5012 printf("Premature EOL\n");
5013#endif
5014 }
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005015 if (status == RA_FAIL)
5016 got_int = TRUE;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005017 return (status == RA_MATCH);
5018 }
5019
5020 } /* End of loop until the regstack is empty. */
5021
5022 /* NOTREACHED */
5023}
5024
5025/*
5026 * Push an item onto the regstack.
5027 * Returns pointer to new item. Returns NULL when out of memory.
5028 */
5029 static regitem_T *
Bram Moolenaar582fd852005-03-28 20:58:01 +00005030regstack_push(regstack, state, scan)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005031 garray_T *regstack;
5032 regstate_T state;
5033 char_u *scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005034{
5035 regitem_T *rp;
5036
Bram Moolenaar916b7af2005-03-16 09:52:38 +00005037 if ((long)((unsigned)regstack->ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005038 {
5039 EMSG(_(e_maxmempat));
5040 return NULL;
5041 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005042 if (ga_grow(regstack, sizeof(regitem_T)) == FAIL)
5043 return NULL;
5044
5045 rp = (regitem_T *)((char *)regstack->ga_data + regstack->ga_len);
5046 rp->rs_state = state;
5047 rp->rs_scan = scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005048
5049 regstack->ga_len += sizeof(regitem_T);
5050 return rp;
5051}
5052
5053/*
5054 * Pop an item from the regstack.
5055 */
5056 static void
Bram Moolenaar582fd852005-03-28 20:58:01 +00005057regstack_pop(regstack, scan)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005058 garray_T *regstack;
5059 char_u **scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005060{
5061 regitem_T *rp;
5062
5063 rp = (regitem_T *)((char *)regstack->ga_data + regstack->ga_len) - 1;
5064 *scan = rp->rs_scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005065
5066 regstack->ga_len -= sizeof(regitem_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067}
5068
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069/*
5070 * regrepeat - repeatedly match something simple, return how many.
5071 * Advances reginput (and reglnum) to just after the matched chars.
5072 */
5073 static int
5074regrepeat(p, maxcount)
5075 char_u *p;
5076 long maxcount; /* maximum number of matches allowed */
5077{
5078 long count = 0;
5079 char_u *scan;
5080 char_u *opnd;
5081 int mask;
5082 int testval = 0;
5083
5084 scan = reginput; /* Make local copy of reginput for speed. */
5085 opnd = OPERAND(p);
5086 switch (OP(p))
5087 {
5088 case ANY:
5089 case ANY + ADD_NL:
5090 while (count < maxcount)
5091 {
5092 /* Matching anything means we continue until end-of-line (or
5093 * end-of-file for ANY + ADD_NL), only limited by maxcount. */
5094 while (*scan != NUL && count < maxcount)
5095 {
5096 ++count;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005097 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 }
5099 if (!WITH_NL(OP(p)) || reglnum == reg_maxline || count == maxcount)
5100 break;
5101 ++count; /* count the line-break */
5102 reg_nextline();
5103 scan = reginput;
5104 if (got_int)
5105 break;
5106 }
5107 break;
5108
5109 case IDENT:
5110 case IDENT + ADD_NL:
5111 testval = TRUE;
5112 /*FALLTHROUGH*/
5113 case SIDENT:
5114 case SIDENT + ADD_NL:
5115 while (count < maxcount)
5116 {
5117 if (vim_isIDc(*scan) && (testval || !VIM_ISDIGIT(*scan)))
5118 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005119 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 }
5121 else if (*scan == NUL)
5122 {
5123 if (!WITH_NL(OP(p)) || reglnum == reg_maxline)
5124 break;
5125 reg_nextline();
5126 scan = reginput;
5127 if (got_int)
5128 break;
5129 }
5130 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5131 ++scan;
5132 else
5133 break;
5134 ++count;
5135 }
5136 break;
5137
5138 case KWORD:
5139 case KWORD + ADD_NL:
5140 testval = TRUE;
5141 /*FALLTHROUGH*/
5142 case SKWORD:
5143 case SKWORD + ADD_NL:
5144 while (count < maxcount)
5145 {
5146 if (vim_iswordp(scan) && (testval || !VIM_ISDIGIT(*scan)))
5147 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005148 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 }
5150 else if (*scan == NUL)
5151 {
5152 if (!WITH_NL(OP(p)) || reglnum == reg_maxline)
5153 break;
5154 reg_nextline();
5155 scan = reginput;
5156 if (got_int)
5157 break;
5158 }
5159 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5160 ++scan;
5161 else
5162 break;
5163 ++count;
5164 }
5165 break;
5166
5167 case FNAME:
5168 case FNAME + ADD_NL:
5169 testval = TRUE;
5170 /*FALLTHROUGH*/
5171 case SFNAME:
5172 case SFNAME + ADD_NL:
5173 while (count < maxcount)
5174 {
5175 if (vim_isfilec(*scan) && (testval || !VIM_ISDIGIT(*scan)))
5176 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005177 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 }
5179 else if (*scan == NUL)
5180 {
5181 if (!WITH_NL(OP(p)) || reglnum == reg_maxline)
5182 break;
5183 reg_nextline();
5184 scan = reginput;
5185 if (got_int)
5186 break;
5187 }
5188 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5189 ++scan;
5190 else
5191 break;
5192 ++count;
5193 }
5194 break;
5195
5196 case PRINT:
5197 case PRINT + ADD_NL:
5198 testval = TRUE;
5199 /*FALLTHROUGH*/
5200 case SPRINT:
5201 case SPRINT + ADD_NL:
5202 while (count < maxcount)
5203 {
5204 if (*scan == NUL)
5205 {
5206 if (!WITH_NL(OP(p)) || reglnum == reg_maxline)
5207 break;
5208 reg_nextline();
5209 scan = reginput;
5210 if (got_int)
5211 break;
5212 }
5213 else if (ptr2cells(scan) == 1 && (testval || !VIM_ISDIGIT(*scan)))
5214 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005215 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 }
5217 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5218 ++scan;
5219 else
5220 break;
5221 ++count;
5222 }
5223 break;
5224
5225 case WHITE:
5226 case WHITE + ADD_NL:
5227 testval = mask = RI_WHITE;
5228do_class:
5229 while (count < maxcount)
5230 {
5231#ifdef FEAT_MBYTE
5232 int l;
5233#endif
5234 if (*scan == NUL)
5235 {
5236 if (!WITH_NL(OP(p)) || reglnum == reg_maxline)
5237 break;
5238 reg_nextline();
5239 scan = reginput;
5240 if (got_int)
5241 break;
5242 }
5243#ifdef FEAT_MBYTE
5244 else if (has_mbyte && (l = (*mb_ptr2len_check)(scan)) > 1)
5245 {
5246 if (testval != 0)
5247 break;
5248 scan += l;
5249 }
5250#endif
5251 else if ((class_tab[*scan] & mask) == testval)
5252 ++scan;
5253 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5254 ++scan;
5255 else
5256 break;
5257 ++count;
5258 }
5259 break;
5260
5261 case NWHITE:
5262 case NWHITE + ADD_NL:
5263 mask = RI_WHITE;
5264 goto do_class;
5265 case DIGIT:
5266 case DIGIT + ADD_NL:
5267 testval = mask = RI_DIGIT;
5268 goto do_class;
5269 case NDIGIT:
5270 case NDIGIT + ADD_NL:
5271 mask = RI_DIGIT;
5272 goto do_class;
5273 case HEX:
5274 case HEX + ADD_NL:
5275 testval = mask = RI_HEX;
5276 goto do_class;
5277 case NHEX:
5278 case NHEX + ADD_NL:
5279 mask = RI_HEX;
5280 goto do_class;
5281 case OCTAL:
5282 case OCTAL + ADD_NL:
5283 testval = mask = RI_OCTAL;
5284 goto do_class;
5285 case NOCTAL:
5286 case NOCTAL + ADD_NL:
5287 mask = RI_OCTAL;
5288 goto do_class;
5289 case WORD:
5290 case WORD + ADD_NL:
5291 testval = mask = RI_WORD;
5292 goto do_class;
5293 case NWORD:
5294 case NWORD + ADD_NL:
5295 mask = RI_WORD;
5296 goto do_class;
5297 case HEAD:
5298 case HEAD + ADD_NL:
5299 testval = mask = RI_HEAD;
5300 goto do_class;
5301 case NHEAD:
5302 case NHEAD + ADD_NL:
5303 mask = RI_HEAD;
5304 goto do_class;
5305 case ALPHA:
5306 case ALPHA + ADD_NL:
5307 testval = mask = RI_ALPHA;
5308 goto do_class;
5309 case NALPHA:
5310 case NALPHA + ADD_NL:
5311 mask = RI_ALPHA;
5312 goto do_class;
5313 case LOWER:
5314 case LOWER + ADD_NL:
5315 testval = mask = RI_LOWER;
5316 goto do_class;
5317 case NLOWER:
5318 case NLOWER + ADD_NL:
5319 mask = RI_LOWER;
5320 goto do_class;
5321 case UPPER:
5322 case UPPER + ADD_NL:
5323 testval = mask = RI_UPPER;
5324 goto do_class;
5325 case NUPPER:
5326 case NUPPER + ADD_NL:
5327 mask = RI_UPPER;
5328 goto do_class;
5329
5330 case EXACTLY:
5331 {
5332 int cu, cl;
5333
5334 /* This doesn't do a multi-byte character, because a MULTIBYTECODE
5335 * would have been used for it. */
5336 if (ireg_ic)
5337 {
5338 cu = TOUPPER_LOC(*opnd);
5339 cl = TOLOWER_LOC(*opnd);
5340 while (count < maxcount && (*scan == cu || *scan == cl))
5341 {
5342 count++;
5343 scan++;
5344 }
5345 }
5346 else
5347 {
5348 cu = *opnd;
5349 while (count < maxcount && *scan == cu)
5350 {
5351 count++;
5352 scan++;
5353 }
5354 }
5355 break;
5356 }
5357
5358#ifdef FEAT_MBYTE
5359 case MULTIBYTECODE:
5360 {
5361 int i, len, cf = 0;
5362
5363 /* Safety check (just in case 'encoding' was changed since
5364 * compiling the program). */
5365 if ((len = (*mb_ptr2len_check)(opnd)) > 1)
5366 {
5367 if (ireg_ic && enc_utf8)
5368 cf = utf_fold(utf_ptr2char(opnd));
5369 while (count < maxcount)
5370 {
5371 for (i = 0; i < len; ++i)
5372 if (opnd[i] != scan[i])
5373 break;
5374 if (i < len && (!ireg_ic || !enc_utf8
5375 || utf_fold(utf_ptr2char(scan)) != cf))
5376 break;
5377 scan += len;
5378 ++count;
5379 }
5380 }
5381 }
5382 break;
5383#endif
5384
5385 case ANYOF:
5386 case ANYOF + ADD_NL:
5387 testval = TRUE;
5388 /*FALLTHROUGH*/
5389
5390 case ANYBUT:
5391 case ANYBUT + ADD_NL:
5392 while (count < maxcount)
5393 {
5394#ifdef FEAT_MBYTE
5395 int len;
5396#endif
5397 if (*scan == NUL)
5398 {
5399 if (!WITH_NL(OP(p)) || reglnum == reg_maxline)
5400 break;
5401 reg_nextline();
5402 scan = reginput;
5403 if (got_int)
5404 break;
5405 }
5406 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5407 ++scan;
5408#ifdef FEAT_MBYTE
5409 else if (has_mbyte && (len = (*mb_ptr2len_check)(scan)) > 1)
5410 {
5411 if ((cstrchr(opnd, (*mb_ptr2char)(scan)) == NULL) == testval)
5412 break;
5413 scan += len;
5414 }
5415#endif
5416 else
5417 {
5418 if ((cstrchr(opnd, *scan) == NULL) == testval)
5419 break;
5420 ++scan;
5421 }
5422 ++count;
5423 }
5424 break;
5425
5426 case NEWL:
5427 while (count < maxcount
5428 && ((*scan == NUL && reglnum < reg_maxline)
5429 || (*scan == '\n' && reg_line_lbr)))
5430 {
5431 count++;
5432 if (reg_line_lbr)
5433 ADVANCE_REGINPUT();
5434 else
5435 reg_nextline();
5436 scan = reginput;
5437 if (got_int)
5438 break;
5439 }
5440 break;
5441
5442 default: /* Oh dear. Called inappropriately. */
5443 EMSG(_(e_re_corr));
5444#ifdef DEBUG
5445 printf("Called regrepeat with op code %d\n", OP(p));
5446#endif
5447 break;
5448 }
5449
5450 reginput = scan;
5451
5452 return (int)count;
5453}
5454
5455/*
5456 * regnext - dig the "next" pointer out of a node
5457 */
5458 static char_u *
5459regnext(p)
5460 char_u *p;
5461{
5462 int offset;
5463
5464 if (p == JUST_CALC_SIZE)
5465 return NULL;
5466
5467 offset = NEXT(p);
5468 if (offset == 0)
5469 return NULL;
5470
Bram Moolenaar582fd852005-03-28 20:58:01 +00005471 if (OP(p) == BACK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 return p - offset;
5473 else
5474 return p + offset;
5475}
5476
5477/*
5478 * Check the regexp program for its magic number.
5479 * Return TRUE if it's wrong.
5480 */
5481 static int
5482prog_magic_wrong()
5483{
5484 if (UCHARAT(REG_MULTI
5485 ? reg_mmatch->regprog->program
5486 : reg_match->regprog->program) != REGMAGIC)
5487 {
5488 EMSG(_(e_re_corr));
5489 return TRUE;
5490 }
5491 return FALSE;
5492}
5493
5494/*
5495 * Cleanup the subexpressions, if this wasn't done yet.
5496 * This construction is used to clear the subexpressions only when they are
5497 * used (to increase speed).
5498 */
5499 static void
5500cleanup_subexpr()
5501{
5502 if (need_clear_subexpr)
5503 {
5504 if (REG_MULTI)
5505 {
5506 /* Use 0xff to set lnum to -1 */
5507 vim_memset(reg_startpos, 0xff, sizeof(lpos_T) * NSUBEXP);
5508 vim_memset(reg_endpos, 0xff, sizeof(lpos_T) * NSUBEXP);
5509 }
5510 else
5511 {
5512 vim_memset(reg_startp, 0, sizeof(char_u *) * NSUBEXP);
5513 vim_memset(reg_endp, 0, sizeof(char_u *) * NSUBEXP);
5514 }
5515 need_clear_subexpr = FALSE;
5516 }
5517}
5518
5519#ifdef FEAT_SYN_HL
5520 static void
5521cleanup_zsubexpr()
5522{
5523 if (need_clear_zsubexpr)
5524 {
5525 if (REG_MULTI)
5526 {
5527 /* Use 0xff to set lnum to -1 */
5528 vim_memset(reg_startzpos, 0xff, sizeof(lpos_T) * NSUBEXP);
5529 vim_memset(reg_endzpos, 0xff, sizeof(lpos_T) * NSUBEXP);
5530 }
5531 else
5532 {
5533 vim_memset(reg_startzp, 0, sizeof(char_u *) * NSUBEXP);
5534 vim_memset(reg_endzp, 0, sizeof(char_u *) * NSUBEXP);
5535 }
5536 need_clear_zsubexpr = FALSE;
5537 }
5538}
5539#endif
5540
5541/*
5542 * Advance reglnum, regline and reginput to the next line.
5543 */
5544 static void
5545reg_nextline()
5546{
5547 regline = reg_getline(++reglnum);
5548 reginput = regline;
5549 fast_breakcheck();
5550}
5551
5552/*
5553 * Save the input line and position in a regsave_T.
5554 */
5555 static void
Bram Moolenaar582fd852005-03-28 20:58:01 +00005556reg_save(save, gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 regsave_T *save;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005558 garray_T *gap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559{
5560 if (REG_MULTI)
5561 {
5562 save->rs_u.pos.col = (colnr_T)(reginput - regline);
5563 save->rs_u.pos.lnum = reglnum;
5564 }
5565 else
5566 save->rs_u.ptr = reginput;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005567 save->rs_len = gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568}
5569
5570/*
5571 * Restore the input line and position from a regsave_T.
5572 */
5573 static void
Bram Moolenaar582fd852005-03-28 20:58:01 +00005574reg_restore(save, gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 regsave_T *save;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005576 garray_T *gap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577{
5578 if (REG_MULTI)
5579 {
5580 if (reglnum != save->rs_u.pos.lnum)
5581 {
5582 /* only call reg_getline() when the line number changed to save
5583 * a bit of time */
5584 reglnum = save->rs_u.pos.lnum;
5585 regline = reg_getline(reglnum);
5586 }
5587 reginput = regline + save->rs_u.pos.col;
5588 }
5589 else
5590 reginput = save->rs_u.ptr;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005591 gap->ga_len = save->rs_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592}
5593
5594/*
5595 * Return TRUE if current position is equal to saved position.
5596 */
5597 static int
5598reg_save_equal(save)
5599 regsave_T *save;
5600{
5601 if (REG_MULTI)
5602 return reglnum == save->rs_u.pos.lnum
5603 && reginput == regline + save->rs_u.pos.col;
5604 return reginput == save->rs_u.ptr;
5605}
5606
5607/*
5608 * Tentatively set the sub-expression start to the current position (after
5609 * calling regmatch() they will have changed). Need to save the existing
5610 * values for when there is no match.
5611 * Use se_save() to use pointer (save_se_multi()) or position (save_se_one()),
5612 * depending on REG_MULTI.
5613 */
5614 static void
5615save_se_multi(savep, posp)
5616 save_se_T *savep;
5617 lpos_T *posp;
5618{
5619 savep->se_u.pos = *posp;
5620 posp->lnum = reglnum;
5621 posp->col = (colnr_T)(reginput - regline);
5622}
5623
5624 static void
5625save_se_one(savep, pp)
5626 save_se_T *savep;
5627 char_u **pp;
5628{
5629 savep->se_u.ptr = *pp;
5630 *pp = reginput;
5631}
5632
5633/*
5634 * Compare a number with the operand of RE_LNUM, RE_COL or RE_VCOL.
5635 */
5636 static int
5637re_num_cmp(val, scan)
5638 long_u val;
5639 char_u *scan;
5640{
5641 long_u n = OPERAND_MIN(scan);
5642
5643 if (OPERAND_CMP(scan) == '>')
5644 return val > n;
5645 if (OPERAND_CMP(scan) == '<')
5646 return val < n;
5647 return val == n;
5648}
5649
5650
5651#ifdef DEBUG
5652
5653/*
5654 * regdump - dump a regexp onto stdout in vaguely comprehensible form
5655 */
5656 static void
5657regdump(pattern, r)
5658 char_u *pattern;
5659 regprog_T *r;
5660{
5661 char_u *s;
5662 int op = EXACTLY; /* Arbitrary non-END op. */
5663 char_u *next;
5664 char_u *end = NULL;
5665
5666 printf("\r\nregcomp(%s):\r\n", pattern);
5667
5668 s = r->program + 1;
5669 /*
5670 * Loop until we find the END that isn't before a referred next (an END
5671 * can also appear in a NOMATCH operand).
5672 */
5673 while (op != END || s <= end)
5674 {
5675 op = OP(s);
5676 printf("%2d%s", (int)(s - r->program), regprop(s)); /* Where, what. */
5677 next = regnext(s);
5678 if (next == NULL) /* Next ptr. */
5679 printf("(0)");
5680 else
5681 printf("(%d)", (int)((s - r->program) + (next - s)));
5682 if (end < next)
5683 end = next;
5684 if (op == BRACE_LIMITS)
5685 {
5686 /* Two short ints */
5687 printf(" minval %ld, maxval %ld", OPERAND_MIN(s), OPERAND_MAX(s));
5688 s += 8;
5689 }
5690 s += 3;
5691 if (op == ANYOF || op == ANYOF + ADD_NL
5692 || op == ANYBUT || op == ANYBUT + ADD_NL
5693 || op == EXACTLY)
5694 {
5695 /* Literal string, where present. */
5696 while (*s != NUL)
5697 printf("%c", *s++);
5698 s++;
5699 }
5700 printf("\r\n");
5701 }
5702
5703 /* Header fields of interest. */
5704 if (r->regstart != NUL)
5705 printf("start `%s' 0x%x; ", r->regstart < 256
5706 ? (char *)transchar(r->regstart)
5707 : "multibyte", r->regstart);
5708 if (r->reganch)
5709 printf("anchored; ");
5710 if (r->regmust != NULL)
5711 printf("must have \"%s\"", r->regmust);
5712 printf("\r\n");
5713}
5714
5715/*
5716 * regprop - printable representation of opcode
5717 */
5718 static char_u *
5719regprop(op)
5720 char_u *op;
5721{
5722 char_u *p;
5723 static char_u buf[50];
5724
5725 (void) strcpy(buf, ":");
5726
5727 switch (OP(op))
5728 {
5729 case BOL:
5730 p = "BOL";
5731 break;
5732 case EOL:
5733 p = "EOL";
5734 break;
5735 case RE_BOF:
5736 p = "BOF";
5737 break;
5738 case RE_EOF:
5739 p = "EOF";
5740 break;
5741 case CURSOR:
5742 p = "CURSOR";
5743 break;
5744 case RE_LNUM:
5745 p = "RE_LNUM";
5746 break;
5747 case RE_COL:
5748 p = "RE_COL";
5749 break;
5750 case RE_VCOL:
5751 p = "RE_VCOL";
5752 break;
5753 case BOW:
5754 p = "BOW";
5755 break;
5756 case EOW:
5757 p = "EOW";
5758 break;
5759 case ANY:
5760 p = "ANY";
5761 break;
5762 case ANY + ADD_NL:
5763 p = "ANY+NL";
5764 break;
5765 case ANYOF:
5766 p = "ANYOF";
5767 break;
5768 case ANYOF + ADD_NL:
5769 p = "ANYOF+NL";
5770 break;
5771 case ANYBUT:
5772 p = "ANYBUT";
5773 break;
5774 case ANYBUT + ADD_NL:
5775 p = "ANYBUT+NL";
5776 break;
5777 case IDENT:
5778 p = "IDENT";
5779 break;
5780 case IDENT + ADD_NL:
5781 p = "IDENT+NL";
5782 break;
5783 case SIDENT:
5784 p = "SIDENT";
5785 break;
5786 case SIDENT + ADD_NL:
5787 p = "SIDENT+NL";
5788 break;
5789 case KWORD:
5790 p = "KWORD";
5791 break;
5792 case KWORD + ADD_NL:
5793 p = "KWORD+NL";
5794 break;
5795 case SKWORD:
5796 p = "SKWORD";
5797 break;
5798 case SKWORD + ADD_NL:
5799 p = "SKWORD+NL";
5800 break;
5801 case FNAME:
5802 p = "FNAME";
5803 break;
5804 case FNAME + ADD_NL:
5805 p = "FNAME+NL";
5806 break;
5807 case SFNAME:
5808 p = "SFNAME";
5809 break;
5810 case SFNAME + ADD_NL:
5811 p = "SFNAME+NL";
5812 break;
5813 case PRINT:
5814 p = "PRINT";
5815 break;
5816 case PRINT + ADD_NL:
5817 p = "PRINT+NL";
5818 break;
5819 case SPRINT:
5820 p = "SPRINT";
5821 break;
5822 case SPRINT + ADD_NL:
5823 p = "SPRINT+NL";
5824 break;
5825 case WHITE:
5826 p = "WHITE";
5827 break;
5828 case WHITE + ADD_NL:
5829 p = "WHITE+NL";
5830 break;
5831 case NWHITE:
5832 p = "NWHITE";
5833 break;
5834 case NWHITE + ADD_NL:
5835 p = "NWHITE+NL";
5836 break;
5837 case DIGIT:
5838 p = "DIGIT";
5839 break;
5840 case DIGIT + ADD_NL:
5841 p = "DIGIT+NL";
5842 break;
5843 case NDIGIT:
5844 p = "NDIGIT";
5845 break;
5846 case NDIGIT + ADD_NL:
5847 p = "NDIGIT+NL";
5848 break;
5849 case HEX:
5850 p = "HEX";
5851 break;
5852 case HEX + ADD_NL:
5853 p = "HEX+NL";
5854 break;
5855 case NHEX:
5856 p = "NHEX";
5857 break;
5858 case NHEX + ADD_NL:
5859 p = "NHEX+NL";
5860 break;
5861 case OCTAL:
5862 p = "OCTAL";
5863 break;
5864 case OCTAL + ADD_NL:
5865 p = "OCTAL+NL";
5866 break;
5867 case NOCTAL:
5868 p = "NOCTAL";
5869 break;
5870 case NOCTAL + ADD_NL:
5871 p = "NOCTAL+NL";
5872 break;
5873 case WORD:
5874 p = "WORD";
5875 break;
5876 case WORD + ADD_NL:
5877 p = "WORD+NL";
5878 break;
5879 case NWORD:
5880 p = "NWORD";
5881 break;
5882 case NWORD + ADD_NL:
5883 p = "NWORD+NL";
5884 break;
5885 case HEAD:
5886 p = "HEAD";
5887 break;
5888 case HEAD + ADD_NL:
5889 p = "HEAD+NL";
5890 break;
5891 case NHEAD:
5892 p = "NHEAD";
5893 break;
5894 case NHEAD + ADD_NL:
5895 p = "NHEAD+NL";
5896 break;
5897 case ALPHA:
5898 p = "ALPHA";
5899 break;
5900 case ALPHA + ADD_NL:
5901 p = "ALPHA+NL";
5902 break;
5903 case NALPHA:
5904 p = "NALPHA";
5905 break;
5906 case NALPHA + ADD_NL:
5907 p = "NALPHA+NL";
5908 break;
5909 case LOWER:
5910 p = "LOWER";
5911 break;
5912 case LOWER + ADD_NL:
5913 p = "LOWER+NL";
5914 break;
5915 case NLOWER:
5916 p = "NLOWER";
5917 break;
5918 case NLOWER + ADD_NL:
5919 p = "NLOWER+NL";
5920 break;
5921 case UPPER:
5922 p = "UPPER";
5923 break;
5924 case UPPER + ADD_NL:
5925 p = "UPPER+NL";
5926 break;
5927 case NUPPER:
5928 p = "NUPPER";
5929 break;
5930 case NUPPER + ADD_NL:
5931 p = "NUPPER+NL";
5932 break;
5933 case BRANCH:
5934 p = "BRANCH";
5935 break;
5936 case EXACTLY:
5937 p = "EXACTLY";
5938 break;
5939 case NOTHING:
5940 p = "NOTHING";
5941 break;
5942 case BACK:
5943 p = "BACK";
5944 break;
5945 case END:
5946 p = "END";
5947 break;
5948 case MOPEN + 0:
5949 p = "MATCH START";
5950 break;
5951 case MOPEN + 1:
5952 case MOPEN + 2:
5953 case MOPEN + 3:
5954 case MOPEN + 4:
5955 case MOPEN + 5:
5956 case MOPEN + 6:
5957 case MOPEN + 7:
5958 case MOPEN + 8:
5959 case MOPEN + 9:
5960 sprintf(buf + STRLEN(buf), "MOPEN%d", OP(op) - MOPEN);
5961 p = NULL;
5962 break;
5963 case MCLOSE + 0:
5964 p = "MATCH END";
5965 break;
5966 case MCLOSE + 1:
5967 case MCLOSE + 2:
5968 case MCLOSE + 3:
5969 case MCLOSE + 4:
5970 case MCLOSE + 5:
5971 case MCLOSE + 6:
5972 case MCLOSE + 7:
5973 case MCLOSE + 8:
5974 case MCLOSE + 9:
5975 sprintf(buf + STRLEN(buf), "MCLOSE%d", OP(op) - MCLOSE);
5976 p = NULL;
5977 break;
5978 case BACKREF + 1:
5979 case BACKREF + 2:
5980 case BACKREF + 3:
5981 case BACKREF + 4:
5982 case BACKREF + 5:
5983 case BACKREF + 6:
5984 case BACKREF + 7:
5985 case BACKREF + 8:
5986 case BACKREF + 9:
5987 sprintf(buf + STRLEN(buf), "BACKREF%d", OP(op) - BACKREF);
5988 p = NULL;
5989 break;
5990 case NOPEN:
5991 p = "NOPEN";
5992 break;
5993 case NCLOSE:
5994 p = "NCLOSE";
5995 break;
5996#ifdef FEAT_SYN_HL
5997 case ZOPEN + 1:
5998 case ZOPEN + 2:
5999 case ZOPEN + 3:
6000 case ZOPEN + 4:
6001 case ZOPEN + 5:
6002 case ZOPEN + 6:
6003 case ZOPEN + 7:
6004 case ZOPEN + 8:
6005 case ZOPEN + 9:
6006 sprintf(buf + STRLEN(buf), "ZOPEN%d", OP(op) - ZOPEN);
6007 p = NULL;
6008 break;
6009 case ZCLOSE + 1:
6010 case ZCLOSE + 2:
6011 case ZCLOSE + 3:
6012 case ZCLOSE + 4:
6013 case ZCLOSE + 5:
6014 case ZCLOSE + 6:
6015 case ZCLOSE + 7:
6016 case ZCLOSE + 8:
6017 case ZCLOSE + 9:
6018 sprintf(buf + STRLEN(buf), "ZCLOSE%d", OP(op) - ZCLOSE);
6019 p = NULL;
6020 break;
6021 case ZREF + 1:
6022 case ZREF + 2:
6023 case ZREF + 3:
6024 case ZREF + 4:
6025 case ZREF + 5:
6026 case ZREF + 6:
6027 case ZREF + 7:
6028 case ZREF + 8:
6029 case ZREF + 9:
6030 sprintf(buf + STRLEN(buf), "ZREF%d", OP(op) - ZREF);
6031 p = NULL;
6032 break;
6033#endif
6034 case STAR:
6035 p = "STAR";
6036 break;
6037 case PLUS:
6038 p = "PLUS";
6039 break;
6040 case NOMATCH:
6041 p = "NOMATCH";
6042 break;
6043 case MATCH:
6044 p = "MATCH";
6045 break;
6046 case BEHIND:
6047 p = "BEHIND";
6048 break;
6049 case NOBEHIND:
6050 p = "NOBEHIND";
6051 break;
6052 case SUBPAT:
6053 p = "SUBPAT";
6054 break;
6055 case BRACE_LIMITS:
6056 p = "BRACE_LIMITS";
6057 break;
6058 case BRACE_SIMPLE:
6059 p = "BRACE_SIMPLE";
6060 break;
6061 case BRACE_COMPLEX + 0:
6062 case BRACE_COMPLEX + 1:
6063 case BRACE_COMPLEX + 2:
6064 case BRACE_COMPLEX + 3:
6065 case BRACE_COMPLEX + 4:
6066 case BRACE_COMPLEX + 5:
6067 case BRACE_COMPLEX + 6:
6068 case BRACE_COMPLEX + 7:
6069 case BRACE_COMPLEX + 8:
6070 case BRACE_COMPLEX + 9:
6071 sprintf(buf + STRLEN(buf), "BRACE_COMPLEX%d", OP(op) - BRACE_COMPLEX);
6072 p = NULL;
6073 break;
6074#ifdef FEAT_MBYTE
6075 case MULTIBYTECODE:
6076 p = "MULTIBYTECODE";
6077 break;
6078#endif
6079 case NEWL:
6080 p = "NEWL";
6081 break;
6082 default:
6083 sprintf(buf + STRLEN(buf), "corrupt %d", OP(op));
6084 p = NULL;
6085 break;
6086 }
6087 if (p != NULL)
6088 (void) strcat(buf, p);
6089 return buf;
6090}
6091#endif
6092
6093#ifdef FEAT_MBYTE
6094static void mb_decompose __ARGS((int c, int *c1, int *c2, int *c3));
6095
6096typedef struct
6097{
6098 int a, b, c;
6099} decomp_T;
6100
6101
6102/* 0xfb20 - 0xfb4f */
6103decomp_T decomp_table[0xfb4f-0xfb20+1] =
6104{
6105 {0x5e2,0,0}, /* 0xfb20 alt ayin */
6106 {0x5d0,0,0}, /* 0xfb21 alt alef */
6107 {0x5d3,0,0}, /* 0xfb22 alt dalet */
6108 {0x5d4,0,0}, /* 0xfb23 alt he */
6109 {0x5db,0,0}, /* 0xfb24 alt kaf */
6110 {0x5dc,0,0}, /* 0xfb25 alt lamed */
6111 {0x5dd,0,0}, /* 0xfb26 alt mem-sofit */
6112 {0x5e8,0,0}, /* 0xfb27 alt resh */
6113 {0x5ea,0,0}, /* 0xfb28 alt tav */
6114 {'+', 0, 0}, /* 0xfb29 alt plus */
6115 {0x5e9, 0x5c1, 0}, /* 0xfb2a shin+shin-dot */
6116 {0x5e9, 0x5c2, 0}, /* 0xfb2b shin+sin-dot */
6117 {0x5e9, 0x5c1, 0x5bc}, /* 0xfb2c shin+shin-dot+dagesh */
6118 {0x5e9, 0x5c2, 0x5bc}, /* 0xfb2d shin+sin-dot+dagesh */
6119 {0x5d0, 0x5b7, 0}, /* 0xfb2e alef+patah */
6120 {0x5d0, 0x5b8, 0}, /* 0xfb2f alef+qamats */
6121 {0x5d0, 0x5b4, 0}, /* 0xfb30 alef+hiriq */
6122 {0x5d1, 0x5bc, 0}, /* 0xfb31 bet+dagesh */
6123 {0x5d2, 0x5bc, 0}, /* 0xfb32 gimel+dagesh */
6124 {0x5d3, 0x5bc, 0}, /* 0xfb33 dalet+dagesh */
6125 {0x5d4, 0x5bc, 0}, /* 0xfb34 he+dagesh */
6126 {0x5d5, 0x5bc, 0}, /* 0xfb35 vav+dagesh */
6127 {0x5d6, 0x5bc, 0}, /* 0xfb36 zayin+dagesh */
6128 {0xfb37, 0, 0}, /* 0xfb37 -- UNUSED */
6129 {0x5d8, 0x5bc, 0}, /* 0xfb38 tet+dagesh */
6130 {0x5d9, 0x5bc, 0}, /* 0xfb39 yud+dagesh */
6131 {0x5da, 0x5bc, 0}, /* 0xfb3a kaf sofit+dagesh */
6132 {0x5db, 0x5bc, 0}, /* 0xfb3b kaf+dagesh */
6133 {0x5dc, 0x5bc, 0}, /* 0xfb3c lamed+dagesh */
6134 {0xfb3d, 0, 0}, /* 0xfb3d -- UNUSED */
6135 {0x5de, 0x5bc, 0}, /* 0xfb3e mem+dagesh */
6136 {0xfb3f, 0, 0}, /* 0xfb3f -- UNUSED */
6137 {0x5e0, 0x5bc, 0}, /* 0xfb40 nun+dagesh */
6138 {0x5e1, 0x5bc, 0}, /* 0xfb41 samech+dagesh */
6139 {0xfb42, 0, 0}, /* 0xfb42 -- UNUSED */
6140 {0x5e3, 0x5bc, 0}, /* 0xfb43 pe sofit+dagesh */
6141 {0x5e4, 0x5bc,0}, /* 0xfb44 pe+dagesh */
6142 {0xfb45, 0, 0}, /* 0xfb45 -- UNUSED */
6143 {0x5e6, 0x5bc, 0}, /* 0xfb46 tsadi+dagesh */
6144 {0x5e7, 0x5bc, 0}, /* 0xfb47 qof+dagesh */
6145 {0x5e8, 0x5bc, 0}, /* 0xfb48 resh+dagesh */
6146 {0x5e9, 0x5bc, 0}, /* 0xfb49 shin+dagesh */
6147 {0x5ea, 0x5bc, 0}, /* 0xfb4a tav+dagesh */
6148 {0x5d5, 0x5b9, 0}, /* 0xfb4b vav+holam */
6149 {0x5d1, 0x5bf, 0}, /* 0xfb4c bet+rafe */
6150 {0x5db, 0x5bf, 0}, /* 0xfb4d kaf+rafe */
6151 {0x5e4, 0x5bf, 0}, /* 0xfb4e pe+rafe */
6152 {0x5d0, 0x5dc, 0} /* 0xfb4f alef-lamed */
6153};
6154
6155 static void
6156mb_decompose(c, c1, c2, c3)
6157 int c, *c1, *c2, *c3;
6158{
6159 decomp_T d;
6160
6161 if (c >= 0x4b20 && c <= 0xfb4f)
6162 {
6163 d = decomp_table[c - 0xfb20];
6164 *c1 = d.a;
6165 *c2 = d.b;
6166 *c3 = d.c;
6167 }
6168 else
6169 {
6170 *c1 = c;
6171 *c2 = *c3 = 0;
6172 }
6173}
6174#endif
6175
6176/*
6177 * Compare two strings, ignore case if ireg_ic set.
6178 * Return 0 if strings match, non-zero otherwise.
6179 * Correct the length "*n" when composing characters are ignored.
6180 */
6181 static int
6182cstrncmp(s1, s2, n)
6183 char_u *s1, *s2;
6184 int *n;
6185{
6186 int result;
6187
6188 if (!ireg_ic)
6189 result = STRNCMP(s1, s2, *n);
6190 else
6191 result = MB_STRNICMP(s1, s2, *n);
6192
6193#ifdef FEAT_MBYTE
6194 /* if it failed and it's utf8 and we want to combineignore: */
6195 if (result != 0 && enc_utf8 && ireg_icombine)
6196 {
6197 char_u *str1, *str2;
6198 int c1, c2, c11, c12;
6199 int ix;
6200 int junk;
6201
6202 /* we have to handle the strcmp ourselves, since it is necessary to
6203 * deal with the composing characters by ignoring them: */
6204 str1 = s1;
6205 str2 = s2;
6206 c1 = c2 = 0;
6207 for (ix = 0; ix < *n; )
6208 {
6209 c1 = mb_ptr2char_adv(&str1);
6210 c2 = mb_ptr2char_adv(&str2);
6211 ix += utf_char2len(c1);
6212
6213 /* decompose the character if necessary, into 'base' characters
6214 * because I don't care about Arabic, I will hard-code the Hebrew
6215 * which I *do* care about! So sue me... */
6216 if (c1 != c2 && (!ireg_ic || utf_fold(c1) != utf_fold(c2)))
6217 {
6218 /* decomposition necessary? */
6219 mb_decompose(c1, &c11, &junk, &junk);
6220 mb_decompose(c2, &c12, &junk, &junk);
6221 c1 = c11;
6222 c2 = c12;
6223 if (c11 != c12 && (!ireg_ic || utf_fold(c11) != utf_fold(c12)))
6224 break;
6225 }
6226 }
6227 result = c2 - c1;
6228 if (result == 0)
6229 *n = (int)(str2 - s2);
6230 }
6231#endif
6232
6233 return result;
6234}
6235
6236/*
6237 * cstrchr: This function is used a lot for simple searches, keep it fast!
6238 */
6239 static char_u *
6240cstrchr(s, c)
6241 char_u *s;
6242 int c;
6243{
6244 char_u *p;
6245 int cc;
6246
6247 if (!ireg_ic
6248#ifdef FEAT_MBYTE
6249 || (!enc_utf8 && mb_char2len(c) > 1)
6250#endif
6251 )
6252 return vim_strchr(s, c);
6253
6254 /* tolower() and toupper() can be slow, comparing twice should be a lot
6255 * faster (esp. when using MS Visual C++!).
6256 * For UTF-8 need to use folded case. */
6257#ifdef FEAT_MBYTE
6258 if (enc_utf8 && c > 0x80)
6259 cc = utf_fold(c);
6260 else
6261#endif
6262 if (isupper(c))
6263 cc = TOLOWER_LOC(c);
6264 else if (islower(c))
6265 cc = TOUPPER_LOC(c);
6266 else
6267 return vim_strchr(s, c);
6268
6269#ifdef FEAT_MBYTE
6270 if (has_mbyte)
6271 {
6272 for (p = s; *p != NUL; p += (*mb_ptr2len_check)(p))
6273 {
6274 if (enc_utf8 && c > 0x80)
6275 {
6276 if (utf_fold(utf_ptr2char(p)) == cc)
6277 return p;
6278 }
6279 else if (*p == c || *p == cc)
6280 return p;
6281 }
6282 }
6283 else
6284#endif
6285 /* Faster version for when there are no multi-byte characters. */
6286 for (p = s; *p != NUL; ++p)
6287 if (*p == c || *p == cc)
6288 return p;
6289
6290 return NULL;
6291}
6292
6293/***************************************************************
6294 * regsub stuff *
6295 ***************************************************************/
6296
6297/* This stuff below really confuses cc on an SGI -- webb */
6298#ifdef __sgi
6299# undef __ARGS
6300# define __ARGS(x) ()
6301#endif
6302
6303/*
6304 * We should define ftpr as a pointer to a function returning a pointer to
6305 * a function returning a pointer to a function ...
6306 * This is impossible, so we declare a pointer to a function returning a
6307 * pointer to a function returning void. This should work for all compilers.
6308 */
6309typedef void (*(*fptr) __ARGS((char_u *, int)))();
6310
6311static fptr do_upper __ARGS((char_u *, int));
6312static fptr do_Upper __ARGS((char_u *, int));
6313static fptr do_lower __ARGS((char_u *, int));
6314static fptr do_Lower __ARGS((char_u *, int));
6315
6316static int vim_regsub_both __ARGS((char_u *source, char_u *dest, int copy, int magic, int backslash));
6317
6318 static fptr
6319do_upper(d, c)
6320 char_u *d;
6321 int c;
6322{
6323 *d = TOUPPER_LOC(c);
6324
6325 return (fptr)NULL;
6326}
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)do_Upper;
6336}
6337
6338 static fptr
6339do_lower(d, c)
6340 char_u *d;
6341 int c;
6342{
6343 *d = TOLOWER_LOC(c);
6344
6345 return (fptr)NULL;
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)do_Lower;
6356}
6357
6358/*
6359 * regtilde(): Replace tildes in the pattern by the old pattern.
6360 *
6361 * Short explanation of the tilde: It stands for the previous replacement
6362 * pattern. If that previous pattern also contains a ~ we should go back a
6363 * step further... But we insert the previous pattern into the current one
6364 * and remember that.
6365 * This still does not handle the case where "magic" changes. TODO?
6366 *
6367 * The tildes are parsed once before the first call to vim_regsub().
6368 */
6369 char_u *
6370regtilde(source, magic)
6371 char_u *source;
6372 int magic;
6373{
6374 char_u *newsub = source;
6375 char_u *tmpsub;
6376 char_u *p;
6377 int len;
6378 int prevlen;
6379
6380 for (p = newsub; *p; ++p)
6381 {
6382 if ((*p == '~' && magic) || (*p == '\\' && *(p + 1) == '~' && !magic))
6383 {
6384 if (reg_prev_sub != NULL)
6385 {
6386 /* length = len(newsub) - 1 + len(prev_sub) + 1 */
6387 prevlen = (int)STRLEN(reg_prev_sub);
6388 tmpsub = alloc((unsigned)(STRLEN(newsub) + prevlen));
6389 if (tmpsub != NULL)
6390 {
6391 /* copy prefix */
6392 len = (int)(p - newsub); /* not including ~ */
6393 mch_memmove(tmpsub, newsub, (size_t)len);
6394 /* interpretate tilde */
6395 mch_memmove(tmpsub + len, reg_prev_sub, (size_t)prevlen);
6396 /* copy postfix */
6397 if (!magic)
6398 ++p; /* back off \ */
6399 STRCPY(tmpsub + len + prevlen, p + 1);
6400
6401 if (newsub != source) /* already allocated newsub */
6402 vim_free(newsub);
6403 newsub = tmpsub;
6404 p = newsub + len + prevlen;
6405 }
6406 }
6407 else if (magic)
6408 STRCPY(p, p + 1); /* remove '~' */
6409 else
6410 STRCPY(p, p + 2); /* remove '\~' */
6411 --p;
6412 }
6413 else
6414 {
6415 if (*p == '\\' && p[1]) /* skip escaped characters */
6416 ++p;
6417#ifdef FEAT_MBYTE
6418 if (has_mbyte)
6419 p += (*mb_ptr2len_check)(p) - 1;
6420#endif
6421 }
6422 }
6423
6424 vim_free(reg_prev_sub);
6425 if (newsub != source) /* newsub was allocated, just keep it */
6426 reg_prev_sub = newsub;
6427 else /* no ~ found, need to save newsub */
6428 reg_prev_sub = vim_strsave(newsub);
6429 return newsub;
6430}
6431
6432#ifdef FEAT_EVAL
6433static int can_f_submatch = FALSE; /* TRUE when submatch() can be used */
6434
6435/* These pointers are used instead of reg_match and reg_mmatch for
6436 * reg_submatch(). Needed for when the substitution string is an expression
6437 * that contains a call to substitute() and submatch(). */
6438static regmatch_T *submatch_match;
6439static regmmatch_T *submatch_mmatch;
6440#endif
6441
6442#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
6443/*
6444 * vim_regsub() - perform substitutions after a vim_regexec() or
6445 * vim_regexec_multi() match.
6446 *
6447 * If "copy" is TRUE really copy into "dest".
6448 * If "copy" is FALSE nothing is copied, this is just to find out the length
6449 * of the result.
6450 *
6451 * If "backslash" is TRUE, a backslash will be removed later, need to double
6452 * them to keep them, and insert a backslash before a CR to avoid it being
6453 * replaced with a line break later.
6454 *
6455 * Note: The matched text must not change between the call of
6456 * vim_regexec()/vim_regexec_multi() and vim_regsub()! It would make the back
6457 * references invalid!
6458 *
6459 * Returns the size of the replacement, including terminating NUL.
6460 */
6461 int
6462vim_regsub(rmp, source, dest, copy, magic, backslash)
6463 regmatch_T *rmp;
6464 char_u *source;
6465 char_u *dest;
6466 int copy;
6467 int magic;
6468 int backslash;
6469{
6470 reg_match = rmp;
6471 reg_mmatch = NULL;
6472 reg_maxline = 0;
6473 return vim_regsub_both(source, dest, copy, magic, backslash);
6474}
6475#endif
6476
6477 int
6478vim_regsub_multi(rmp, lnum, source, dest, copy, magic, backslash)
6479 regmmatch_T *rmp;
6480 linenr_T lnum;
6481 char_u *source;
6482 char_u *dest;
6483 int copy;
6484 int magic;
6485 int backslash;
6486{
6487 reg_match = NULL;
6488 reg_mmatch = rmp;
6489 reg_buf = curbuf; /* always works on the current buffer! */
6490 reg_firstlnum = lnum;
6491 reg_maxline = curbuf->b_ml.ml_line_count - lnum;
6492 return vim_regsub_both(source, dest, copy, magic, backslash);
6493}
6494
6495 static int
6496vim_regsub_both(source, dest, copy, magic, backslash)
6497 char_u *source;
6498 char_u *dest;
6499 int copy;
6500 int magic;
6501 int backslash;
6502{
6503 char_u *src;
6504 char_u *dst;
6505 char_u *s;
6506 int c;
6507 int no = -1;
6508 fptr func = (fptr)NULL;
6509 linenr_T clnum = 0; /* init for GCC */
6510 int len = 0; /* init for GCC */
6511#ifdef FEAT_EVAL
6512 static char_u *eval_result = NULL;
6513#endif
6514#ifdef FEAT_MBYTE
6515 int l;
6516#endif
6517
6518
6519 /* Be paranoid... */
6520 if (source == NULL || dest == NULL)
6521 {
6522 EMSG(_(e_null));
6523 return 0;
6524 }
6525 if (prog_magic_wrong())
6526 return 0;
6527 src = source;
6528 dst = dest;
6529
6530 /*
6531 * When the substitute part starts with "\=" evaluate it as an expression.
6532 */
6533 if (source[0] == '\\' && source[1] == '='
6534#ifdef FEAT_EVAL
6535 && !can_f_submatch /* can't do this recursively */
6536#endif
6537 )
6538 {
6539#ifdef FEAT_EVAL
6540 /* To make sure that the length doesn't change between checking the
6541 * length and copying the string, and to speed up things, the
6542 * resulting string is saved from the call with "copy" == FALSE to the
6543 * call with "copy" == TRUE. */
6544 if (copy)
6545 {
6546 if (eval_result != NULL)
6547 {
6548 STRCPY(dest, eval_result);
6549 dst += STRLEN(eval_result);
6550 vim_free(eval_result);
6551 eval_result = NULL;
6552 }
6553 }
6554 else
6555 {
6556 linenr_T save_reg_maxline;
6557 win_T *save_reg_win;
6558 int save_ireg_ic;
6559
6560 vim_free(eval_result);
6561
6562 /* The expression may contain substitute(), which calls us
6563 * recursively. Make sure submatch() gets the text from the first
6564 * level. Don't need to save "reg_buf", because
6565 * vim_regexec_multi() can't be called recursively. */
6566 submatch_match = reg_match;
6567 submatch_mmatch = reg_mmatch;
6568 save_reg_maxline = reg_maxline;
6569 save_reg_win = reg_win;
6570 save_ireg_ic = ireg_ic;
6571 can_f_submatch = TRUE;
6572
6573 eval_result = eval_to_string(source + 2, NULL);
6574 if (eval_result != NULL)
6575 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006576 for (s = eval_result; *s != NUL; mb_ptr_adv(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 {
6578 /* Change NL to CR, so that it becomes a line break.
6579 * Skip over a backslashed character. */
6580 if (*s == NL)
6581 *s = CAR;
6582 else if (*s == '\\' && s[1] != NUL)
6583 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 }
6585
6586 dst += STRLEN(eval_result);
6587 }
6588
6589 reg_match = submatch_match;
6590 reg_mmatch = submatch_mmatch;
6591 reg_maxline = save_reg_maxline;
6592 reg_win = save_reg_win;
6593 ireg_ic = save_ireg_ic;
6594 can_f_submatch = FALSE;
6595 }
6596#endif
6597 }
6598 else
6599 while ((c = *src++) != NUL)
6600 {
6601 if (c == '&' && magic)
6602 no = 0;
6603 else if (c == '\\' && *src != NUL)
6604 {
6605 if (*src == '&' && !magic)
6606 {
6607 ++src;
6608 no = 0;
6609 }
6610 else if ('0' <= *src && *src <= '9')
6611 {
6612 no = *src++ - '0';
6613 }
6614 else if (vim_strchr((char_u *)"uUlLeE", *src))
6615 {
6616 switch (*src++)
6617 {
6618 case 'u': func = (fptr)do_upper;
6619 continue;
6620 case 'U': func = (fptr)do_Upper;
6621 continue;
6622 case 'l': func = (fptr)do_lower;
6623 continue;
6624 case 'L': func = (fptr)do_Lower;
6625 continue;
6626 case 'e':
6627 case 'E': func = (fptr)NULL;
6628 continue;
6629 }
6630 }
6631 }
6632 if (no < 0) /* Ordinary character. */
6633 {
6634 if (c == '\\' && *src != NUL)
6635 {
6636 /* Check for abbreviations -- webb */
6637 switch (*src)
6638 {
6639 case 'r': c = CAR; ++src; break;
6640 case 'n': c = NL; ++src; break;
6641 case 't': c = TAB; ++src; break;
6642 /* Oh no! \e already has meaning in subst pat :-( */
6643 /* case 'e': c = ESC; ++src; break; */
6644 case 'b': c = Ctrl_H; ++src; break;
6645
6646 /* If "backslash" is TRUE the backslash will be removed
6647 * later. Used to insert a literal CR. */
6648 default: if (backslash)
6649 {
6650 if (copy)
6651 *dst = '\\';
6652 ++dst;
6653 }
6654 c = *src++;
6655 }
6656 }
6657
6658 /* Write to buffer, if copy is set. */
6659#ifdef FEAT_MBYTE
6660 if (has_mbyte && (l = (*mb_ptr2len_check)(src - 1)) > 1)
6661 {
6662 /* TODO: should use "func" here. */
6663 if (copy)
6664 mch_memmove(dst, src - 1, l);
6665 dst += l - 1;
6666 src += l - 1;
6667 }
6668 else
6669 {
6670#endif
6671 if (copy)
6672 {
6673 if (func == (fptr)NULL) /* just copy */
6674 *dst = c;
6675 else /* change case */
6676 func = (fptr)(func(dst, c));
6677 /* Turbo C complains without the typecast */
6678 }
6679#ifdef FEAT_MBYTE
6680 }
6681#endif
6682 dst++;
6683 }
6684 else
6685 {
6686 if (REG_MULTI)
6687 {
6688 clnum = reg_mmatch->startpos[no].lnum;
6689 if (clnum < 0 || reg_mmatch->endpos[no].lnum < 0)
6690 s = NULL;
6691 else
6692 {
6693 s = reg_getline(clnum) + reg_mmatch->startpos[no].col;
6694 if (reg_mmatch->endpos[no].lnum == clnum)
6695 len = reg_mmatch->endpos[no].col
6696 - reg_mmatch->startpos[no].col;
6697 else
6698 len = (int)STRLEN(s);
6699 }
6700 }
6701 else
6702 {
6703 s = reg_match->startp[no];
6704 if (reg_match->endp[no] == NULL)
6705 s = NULL;
6706 else
6707 len = (int)(reg_match->endp[no] - s);
6708 }
6709 if (s != NULL)
6710 {
6711 for (;;)
6712 {
6713 if (len == 0)
6714 {
6715 if (REG_MULTI)
6716 {
6717 if (reg_mmatch->endpos[no].lnum == clnum)
6718 break;
6719 if (copy)
6720 *dst = CAR;
6721 ++dst;
6722 s = reg_getline(++clnum);
6723 if (reg_mmatch->endpos[no].lnum == clnum)
6724 len = reg_mmatch->endpos[no].col;
6725 else
6726 len = (int)STRLEN(s);
6727 }
6728 else
6729 break;
6730 }
6731 else if (*s == NUL) /* we hit NUL. */
6732 {
6733 if (copy)
6734 EMSG(_(e_re_damg));
6735 goto exit;
6736 }
6737 else
6738 {
6739 if (backslash && (*s == CAR || *s == '\\'))
6740 {
6741 /*
6742 * Insert a backslash in front of a CR, otherwise
6743 * it will be replaced by a line break.
6744 * Number of backslashes will be halved later,
6745 * double them here.
6746 */
6747 if (copy)
6748 {
6749 dst[0] = '\\';
6750 dst[1] = *s;
6751 }
6752 dst += 2;
6753 }
6754#ifdef FEAT_MBYTE
6755 else if (has_mbyte && (l = (*mb_ptr2len_check)(s)) > 1)
6756 {
6757 /* TODO: should use "func" here. */
6758 if (copy)
6759 mch_memmove(dst, s, l);
6760 dst += l;
6761 s += l - 1;
6762 len -= l - 1;
6763 }
6764#endif
6765 else
6766 {
6767 if (copy)
6768 {
6769 if (func == (fptr)NULL) /* just copy */
6770 *dst = *s;
6771 else /* change case */
6772 func = (fptr)(func(dst, *s));
6773 /* Turbo C complains without the typecast */
6774 }
6775 ++dst;
6776 }
6777 ++s;
6778 --len;
6779 }
6780 }
6781 }
6782 no = -1;
6783 }
6784 }
6785 if (copy)
6786 *dst = NUL;
6787
6788exit:
6789 return (int)((dst - dest) + 1);
6790}
6791
6792#ifdef FEAT_EVAL
6793/*
6794 * Used for the submatch() function: get the string from tne n'th submatch in
6795 * allocated memory.
6796 * Returns NULL when not in a ":s" command and for a non-existing submatch.
6797 */
6798 char_u *
6799reg_submatch(no)
6800 int no;
6801{
6802 char_u *retval = NULL;
6803 char_u *s;
6804 int len;
6805 int round;
6806 linenr_T lnum;
6807
6808 if (!can_f_submatch)
6809 return NULL;
6810
6811 if (submatch_match == NULL)
6812 {
6813 /*
6814 * First round: compute the length and allocate memory.
6815 * Second round: copy the text.
6816 */
6817 for (round = 1; round <= 2; ++round)
6818 {
6819 lnum = submatch_mmatch->startpos[no].lnum;
6820 if (lnum < 0 || submatch_mmatch->endpos[no].lnum < 0)
6821 return NULL;
6822
6823 s = reg_getline(lnum) + submatch_mmatch->startpos[no].col;
6824 if (s == NULL) /* anti-crash check, cannot happen? */
6825 break;
6826 if (submatch_mmatch->endpos[no].lnum == lnum)
6827 {
6828 /* Within one line: take form start to end col. */
6829 len = submatch_mmatch->endpos[no].col
6830 - submatch_mmatch->startpos[no].col;
6831 if (round == 2)
6832 {
6833 STRNCPY(retval, s, len);
6834 retval[len] = NUL;
6835 }
6836 ++len;
6837 }
6838 else
6839 {
6840 /* Multiple lines: take start line from start col, middle
6841 * lines completely and end line up to end col. */
6842 len = (int)STRLEN(s);
6843 if (round == 2)
6844 {
6845 STRCPY(retval, s);
6846 retval[len] = '\n';
6847 }
6848 ++len;
6849 ++lnum;
6850 while (lnum < submatch_mmatch->endpos[no].lnum)
6851 {
6852 s = reg_getline(lnum++);
6853 if (round == 2)
6854 STRCPY(retval + len, s);
6855 len += (int)STRLEN(s);
6856 if (round == 2)
6857 retval[len] = '\n';
6858 ++len;
6859 }
6860 if (round == 2)
6861 STRNCPY(retval + len, reg_getline(lnum),
6862 submatch_mmatch->endpos[no].col);
6863 len += submatch_mmatch->endpos[no].col;
6864 if (round == 2)
6865 retval[len] = NUL;
6866 ++len;
6867 }
6868
6869 if (round == 1)
6870 {
6871 retval = lalloc((long_u)len, TRUE);
6872 if (s == NULL)
6873 return NULL;
6874 }
6875 }
6876 }
6877 else
6878 {
6879 if (submatch_match->endp[no] == NULL)
6880 retval = NULL;
6881 else
6882 {
6883 s = submatch_match->startp[no];
6884 retval = vim_strnsave(s, (int)(submatch_match->endp[no] - s));
6885 }
6886 }
6887
6888 return retval;
6889}
6890#endif