blob: 79d3e2a573bf76731a1c6c627ece2206bb44fbe6 [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 Moolenaar19a09a12005-03-04 23:39:37 +0000103 * <aa>\+ BRANCH <aa> --> BRANCH --> BACKP BRANCH --> NOTHING --> END
104 * | | ^ ^
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
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000232#define BACKP 207 /* Like BACK but for \+ */
233
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234/*
235 * Magic characters have a special meaning, they don't match literally.
236 * Magic characters are negative. This separates them from literal characters
237 * (possibly multi-byte). Only ASCII characters can be Magic.
238 */
239#define Magic(x) ((int)(x) - 256)
240#define un_Magic(x) ((x) + 256)
241#define is_Magic(x) ((x) < 0)
242
243static int no_Magic __ARGS((int x));
244static int toggle_Magic __ARGS((int x));
245
246 static int
247no_Magic(x)
248 int x;
249{
250 if (is_Magic(x))
251 return un_Magic(x);
252 return x;
253}
254
255 static int
256toggle_Magic(x)
257 int x;
258{
259 if (is_Magic(x))
260 return un_Magic(x);
261 return Magic(x);
262}
263
264/*
265 * The first byte of the regexp internal "program" is actually this magic
266 * number; the start node begins in the second byte. It's used to catch the
267 * most severe mutilation of the program by the caller.
268 */
269
270#define REGMAGIC 0234
271
272/*
273 * Opcode notes:
274 *
275 * BRANCH The set of branches constituting a single choice are hooked
276 * together with their "next" pointers, since precedence prevents
277 * anything being concatenated to any individual branch. The
278 * "next" pointer of the last BRANCH in a choice points to the
279 * thing following the whole choice. This is also where the
280 * final "next" pointer of each individual branch points; each
281 * branch starts with the operand node of a BRANCH node.
282 *
283 * BACK Normal "next" pointers all implicitly point forward; BACK
284 * exists to make loop structures possible.
285 *
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000286 * BACKP Like BACK, but used for \+. Doesn't check for an empty match.
287 *
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 * STAR,PLUS '=', and complex '*' and '+', are implemented as circular
289 * BRANCH structures using BACK. Simple cases (one character
290 * per match) are implemented with STAR and PLUS for speed
291 * and to minimize recursive plunges.
292 *
293 * BRACE_LIMITS This is always followed by a BRACE_SIMPLE or BRACE_COMPLEX
294 * node, and defines the min and max limits to be used for that
295 * node.
296 *
297 * MOPEN,MCLOSE ...are numbered at compile time.
298 * ZOPEN,ZCLOSE ...ditto
299 */
300
301/*
302 * A node is one char of opcode followed by two chars of "next" pointer.
303 * "Next" pointers are stored as two 8-bit bytes, high order first. The
304 * value is a positive offset from the opcode of the node containing it.
305 * An operand, if any, simply follows the node. (Note that much of the
306 * code generation knows about this implicit relationship.)
307 *
308 * Using two bytes for the "next" pointer is vast overkill for most things,
309 * but allows patterns to get big without disasters.
310 */
311#define OP(p) ((int)*(p))
312#define NEXT(p) (((*((p) + 1) & 0377) << 8) + (*((p) + 2) & 0377))
313#define OPERAND(p) ((p) + 3)
314/* Obtain an operand that was stored as four bytes, MSB first. */
315#define OPERAND_MIN(p) (((long)(p)[3] << 24) + ((long)(p)[4] << 16) \
316 + ((long)(p)[5] << 8) + (long)(p)[6])
317/* Obtain a second operand stored as four bytes. */
318#define OPERAND_MAX(p) OPERAND_MIN((p) + 4)
319/* Obtain a second single-byte operand stored after a four bytes operand. */
320#define OPERAND_CMP(p) (p)[7]
321
322/*
323 * Utility definitions.
324 */
325#define UCHARAT(p) ((int)*(char_u *)(p))
326
327/* Used for an error (down from) vim_regcomp(): give the error message, set
328 * rc_did_emsg and return NULL */
329#define EMSG_RET_NULL(m) { EMSG(m); rc_did_emsg = TRUE; return NULL; }
330#define EMSG_M_RET_NULL(m, c) { EMSG2(m, c ? "" : "\\"); rc_did_emsg = TRUE; return NULL; }
331#define EMSG_RET_FAIL(m) { EMSG(m); rc_did_emsg = TRUE; return FAIL; }
332#define EMSG_ONE_RET_NULL EMSG_M_RET_NULL(_("E369: invalid item in %s%%[]"), reg_magic == MAGIC_ALL)
333
334#define MAX_LIMIT (32767L << 16L)
335
336static int re_multi_type __ARGS((int));
337static int cstrncmp __ARGS((char_u *s1, char_u *s2, int *n));
338static char_u *cstrchr __ARGS((char_u *, int));
339
340#ifdef DEBUG
341static void regdump __ARGS((char_u *, regprog_T *));
342static char_u *regprop __ARGS((char_u *));
343#endif
344
345#define NOT_MULTI 0
346#define MULTI_ONE 1
347#define MULTI_MULT 2
348/*
349 * Return NOT_MULTI if c is not a "multi" operator.
350 * Return MULTI_ONE if c is a single "multi" operator.
351 * Return MULTI_MULT if c is a multi "multi" operator.
352 */
353 static int
354re_multi_type(c)
355 int c;
356{
357 if (c == Magic('@') || c == Magic('=') || c == Magic('?'))
358 return MULTI_ONE;
359 if (c == Magic('*') || c == Magic('+') || c == Magic('{'))
360 return MULTI_MULT;
361 return NOT_MULTI;
362}
363
364/*
365 * Flags to be passed up and down.
366 */
367#define HASWIDTH 0x1 /* Known never to match null string. */
368#define SIMPLE 0x2 /* Simple enough to be STAR/PLUS operand. */
369#define SPSTART 0x4 /* Starts with * or +. */
370#define HASNL 0x8 /* Contains some \n. */
371#define HASLOOKBH 0x10 /* Contains "\@<=" or "\@<!". */
372#define WORST 0 /* Worst case. */
373
374/*
375 * When regcode is set to this value, code is not emitted and size is computed
376 * instead.
377 */
378#define JUST_CALC_SIZE ((char_u *) -1)
379
380static char_u *reg_prev_sub;
381
382/*
383 * REGEXP_INRANGE contains all characters which are always special in a []
384 * range after '\'.
385 * REGEXP_ABBR contains all characters which act as abbreviations after '\'.
386 * These are:
387 * \n - New line (NL).
388 * \r - Carriage Return (CR).
389 * \t - Tab (TAB).
390 * \e - Escape (ESC).
391 * \b - Backspace (Ctrl_H).
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000392 * \d - Character code in decimal, eg \d123
393 * \o - Character code in octal, eg \o80
394 * \x - Character code in hex, eg \x4a
395 * \u - Multibyte character code, eg \u20ac
396 * \U - Long multibyte character code, eg \U12345678
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 */
398static char_u REGEXP_INRANGE[] = "]^-n\\";
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000399static char_u REGEXP_ABBR[] = "nrtebdoxuU";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400
401static int backslash_trans __ARGS((int c));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000402static int get_char_class __ARGS((char_u **pp));
403static int get_equi_class __ARGS((char_u **pp));
404static void reg_equi_class __ARGS((int c));
405static int get_coll_element __ARGS((char_u **pp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406static char_u *skip_anyof __ARGS((char_u *p));
407static void init_class_tab __ARGS((void));
408
409/*
410 * Translate '\x' to its control character, except "\n", which is Magic.
411 */
412 static int
413backslash_trans(c)
414 int c;
415{
416 switch (c)
417 {
418 case 'r': return CAR;
419 case 't': return TAB;
420 case 'e': return ESC;
421 case 'b': return BS;
422 }
423 return c;
424}
425
426/*
Bram Moolenaardf177f62005-02-22 08:39:57 +0000427 * Check for a character class name "[:name:]". "pp" points to the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 * Returns one of the CLASS_ items. CLASS_NONE means that no item was
429 * recognized. Otherwise "pp" is advanced to after the item.
430 */
431 static int
Bram Moolenaardf177f62005-02-22 08:39:57 +0000432get_char_class(pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 char_u **pp;
434{
435 static const char *(class_names[]) =
436 {
437 "alnum:]",
438#define CLASS_ALNUM 0
439 "alpha:]",
440#define CLASS_ALPHA 1
441 "blank:]",
442#define CLASS_BLANK 2
443 "cntrl:]",
444#define CLASS_CNTRL 3
445 "digit:]",
446#define CLASS_DIGIT 4
447 "graph:]",
448#define CLASS_GRAPH 5
449 "lower:]",
450#define CLASS_LOWER 6
451 "print:]",
452#define CLASS_PRINT 7
453 "punct:]",
454#define CLASS_PUNCT 8
455 "space:]",
456#define CLASS_SPACE 9
457 "upper:]",
458#define CLASS_UPPER 10
459 "xdigit:]",
460#define CLASS_XDIGIT 11
461 "tab:]",
462#define CLASS_TAB 12
463 "return:]",
464#define CLASS_RETURN 13
465 "backspace:]",
466#define CLASS_BACKSPACE 14
467 "escape:]",
468#define CLASS_ESCAPE 15
469 };
470#define CLASS_NONE 99
471 int i;
472
473 if ((*pp)[1] == ':')
474 {
475 for (i = 0; i < sizeof(class_names) / sizeof(*class_names); ++i)
476 if (STRNCMP(*pp + 2, class_names[i], STRLEN(class_names[i])) == 0)
477 {
478 *pp += STRLEN(class_names[i]) + 2;
479 return i;
480 }
481 }
482 return CLASS_NONE;
483}
484
485/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 * Specific version of character class functions.
487 * Using a table to keep this fast.
488 */
489static short class_tab[256];
490
491#define RI_DIGIT 0x01
492#define RI_HEX 0x02
493#define RI_OCTAL 0x04
494#define RI_WORD 0x08
495#define RI_HEAD 0x10
496#define RI_ALPHA 0x20
497#define RI_LOWER 0x40
498#define RI_UPPER 0x80
499#define RI_WHITE 0x100
500
501 static void
502init_class_tab()
503{
504 int i;
505 static int done = FALSE;
506
507 if (done)
508 return;
509
510 for (i = 0; i < 256; ++i)
511 {
512 if (i >= '0' && i <= '7')
513 class_tab[i] = RI_DIGIT + RI_HEX + RI_OCTAL + RI_WORD;
514 else if (i >= '8' && i <= '9')
515 class_tab[i] = RI_DIGIT + RI_HEX + RI_WORD;
516 else if (i >= 'a' && i <= 'f')
517 class_tab[i] = RI_HEX + RI_WORD + RI_HEAD + RI_ALPHA + RI_LOWER;
518#ifdef EBCDIC
519 else if ((i >= 'g' && i <= 'i') || (i >= 'j' && i <= 'r')
520 || (i >= 's' && i <= 'z'))
521#else
522 else if (i >= 'g' && i <= 'z')
523#endif
524 class_tab[i] = RI_WORD + RI_HEAD + RI_ALPHA + RI_LOWER;
525 else if (i >= 'A' && i <= 'F')
526 class_tab[i] = RI_HEX + RI_WORD + RI_HEAD + RI_ALPHA + RI_UPPER;
527#ifdef EBCDIC
528 else if ((i >= 'G' && i <= 'I') || ( i >= 'J' && i <= 'R')
529 || (i >= 'S' && i <= 'Z'))
530#else
531 else if (i >= 'G' && i <= 'Z')
532#endif
533 class_tab[i] = RI_WORD + RI_HEAD + RI_ALPHA + RI_UPPER;
534 else if (i == '_')
535 class_tab[i] = RI_WORD + RI_HEAD;
536 else
537 class_tab[i] = 0;
538 }
539 class_tab[' '] |= RI_WHITE;
540 class_tab['\t'] |= RI_WHITE;
541 done = TRUE;
542}
543
544#ifdef FEAT_MBYTE
545# define ri_digit(c) (c < 0x100 && (class_tab[c] & RI_DIGIT))
546# define ri_hex(c) (c < 0x100 && (class_tab[c] & RI_HEX))
547# define ri_octal(c) (c < 0x100 && (class_tab[c] & RI_OCTAL))
548# define ri_word(c) (c < 0x100 && (class_tab[c] & RI_WORD))
549# define ri_head(c) (c < 0x100 && (class_tab[c] & RI_HEAD))
550# define ri_alpha(c) (c < 0x100 && (class_tab[c] & RI_ALPHA))
551# define ri_lower(c) (c < 0x100 && (class_tab[c] & RI_LOWER))
552# define ri_upper(c) (c < 0x100 && (class_tab[c] & RI_UPPER))
553# define ri_white(c) (c < 0x100 && (class_tab[c] & RI_WHITE))
554#else
555# define ri_digit(c) (class_tab[c] & RI_DIGIT)
556# define ri_hex(c) (class_tab[c] & RI_HEX)
557# define ri_octal(c) (class_tab[c] & RI_OCTAL)
558# define ri_word(c) (class_tab[c] & RI_WORD)
559# define ri_head(c) (class_tab[c] & RI_HEAD)
560# define ri_alpha(c) (class_tab[c] & RI_ALPHA)
561# define ri_lower(c) (class_tab[c] & RI_LOWER)
562# define ri_upper(c) (class_tab[c] & RI_UPPER)
563# define ri_white(c) (class_tab[c] & RI_WHITE)
564#endif
565
566/* flags for regflags */
567#define RF_ICASE 1 /* ignore case */
568#define RF_NOICASE 2 /* don't ignore case */
569#define RF_HASNL 4 /* can match a NL */
570#define RF_ICOMBINE 8 /* ignore combining characters */
571#define RF_LOOKBH 16 /* uses "\@<=" or "\@<!" */
572
573/*
574 * Global work variables for vim_regcomp().
575 */
576
577static char_u *regparse; /* Input-scan pointer. */
578static int prevchr_len; /* byte length of previous char */
579static int num_complex_braces; /* Complex \{...} count */
580static int regnpar; /* () count. */
581#ifdef FEAT_SYN_HL
582static int regnzpar; /* \z() count. */
583static int re_has_z; /* \z item detected */
584#endif
585static char_u *regcode; /* Code-emit pointer, or JUST_CALC_SIZE */
586static long regsize; /* Code size. */
587static char_u had_endbrace[NSUBEXP]; /* flags, TRUE if end of () found */
588static unsigned regflags; /* RF_ flags for prog */
589static long brace_min[10]; /* Minimums for complex brace repeats */
590static long brace_max[10]; /* Maximums for complex brace repeats */
591static int brace_count[10]; /* Current counts for complex brace repeats */
592#if defined(FEAT_SYN_HL) || defined(PROTO)
593static int had_eol; /* TRUE when EOL found by vim_regcomp() */
594#endif
595static int one_exactly = FALSE; /* only do one char for EXACTLY */
596
597static int reg_magic; /* magicness of the pattern: */
598#define MAGIC_NONE 1 /* "\V" very unmagic */
599#define MAGIC_OFF 2 /* "\M" or 'magic' off */
600#define MAGIC_ON 3 /* "\m" or 'magic' */
601#define MAGIC_ALL 4 /* "\v" very magic */
602
603static int reg_string; /* matching with a string instead of a buffer
604 line */
605
606/*
607 * META contains all characters that may be magic, except '^' and '$'.
608 */
609
610#ifdef EBCDIC
611static char_u META[] = "%&()*+.123456789<=>?@ACDFHIKLMOPSUVWX[_acdfhiklmnopsuvwxz{|~";
612#else
613/* META[] is used often enough to justify turning it into a table. */
614static char_u META_flags[] = {
615 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
616 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
617/* % & ( ) * + . */
618 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0,
619/* 1 2 3 4 5 6 7 8 9 < = > ? */
620 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1,
621/* @ A C D F H I K L M O */
622 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1,
623/* P S U V W X Z [ _ */
624 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1,
625/* a c d f h i k l m n o */
626 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1,
627/* p s u v w x z { | ~ */
628 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1
629};
630#endif
631
632static int curchr;
633
634/* arguments for reg() */
635#define REG_NOPAREN 0 /* toplevel reg() */
636#define REG_PAREN 1 /* \(\) */
637#define REG_ZPAREN 2 /* \z(\) */
638#define REG_NPAREN 3 /* \%(\) */
639
640/*
641 * Forward declarations for vim_regcomp()'s friends.
642 */
643static void initchr __ARGS((char_u *));
644static int getchr __ARGS((void));
645static void skipchr_keepstart __ARGS((void));
646static int peekchr __ARGS((void));
647static void skipchr __ARGS((void));
648static void ungetchr __ARGS((void));
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000649static int gethexchrs __ARGS((int maxinputlen));
650static int getoctchrs __ARGS((void));
651static int getdecchrs __ARGS((void));
652static int coll_get_char __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653static void regcomp_start __ARGS((char_u *expr, int flags));
654static char_u *reg __ARGS((int, int *));
655static char_u *regbranch __ARGS((int *flagp));
656static char_u *regconcat __ARGS((int *flagp));
657static char_u *regpiece __ARGS((int *));
658static char_u *regatom __ARGS((int *));
659static char_u *regnode __ARGS((int));
660static int prog_magic_wrong __ARGS((void));
661static char_u *regnext __ARGS((char_u *));
662static void regc __ARGS((int b));
663#ifdef FEAT_MBYTE
664static void regmbc __ARGS((int c));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000665#else
666# define regmbc(c) regc(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667#endif
668static void reginsert __ARGS((int, char_u *));
669static void reginsert_limits __ARGS((int, long, long, char_u *));
670static char_u *re_put_long __ARGS((char_u *pr, long_u val));
671static int read_limits __ARGS((long *, long *));
672static void regtail __ARGS((char_u *, char_u *));
673static void regoptail __ARGS((char_u *, char_u *));
674
675/*
676 * Return TRUE if compiled regular expression "prog" can match a line break.
677 */
678 int
679re_multiline(prog)
680 regprog_T *prog;
681{
682 return (prog->regflags & RF_HASNL);
683}
684
685/*
686 * Return TRUE if compiled regular expression "prog" looks before the start
687 * position (pattern contains "\@<=" or "\@<!").
688 */
689 int
690re_lookbehind(prog)
691 regprog_T *prog;
692{
693 return (prog->regflags & RF_LOOKBH);
694}
695
696/*
Bram Moolenaardf177f62005-02-22 08:39:57 +0000697 * Check for an equivalence class name "[=a=]". "pp" points to the '['.
698 * Returns a character representing the class. Zero means that no item was
699 * recognized. Otherwise "pp" is advanced to after the item.
700 */
701 static int
702get_equi_class(pp)
703 char_u **pp;
704{
705 int c;
706 int l = 1;
707 char_u *p = *pp;
708
709 if (p[1] == '=')
710 {
711#ifdef FEAT_MBYTE
712 if (has_mbyte)
713 l = mb_ptr2len_check(p + 2);
714#endif
715 if (p[l + 2] == '=' && p[l + 3] == ']')
716 {
717#ifdef FEAT_MBYTE
718 if (has_mbyte)
719 c = mb_ptr2char(p + 2);
720 else
721#endif
722 c = p[2];
723 *pp += l + 4;
724 return c;
725 }
726 }
727 return 0;
728}
729
730/*
731 * Produce the bytes for equivalence class "c".
732 * Currently only handles latin1, latin9 and utf-8.
733 */
734 static void
735reg_equi_class(c)
736 int c;
737{
738#ifdef FEAT_MBYTE
739 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
740 || STRCMP(p_enc, "latin9") == 0)
741#endif
742 {
743 switch (c)
744 {
745 case 'A': case 'À': case 'Á': case 'Â':
746 case 'Ã': case 'Ä': case 'Å':
747 regmbc('A'); regmbc('À'); regmbc('Á'); regmbc('Â');
748 regmbc('Ã'); regmbc('Ä'); regmbc('Å');
749 return;
750 case 'C': case 'Ç':
751 regmbc('C'); regmbc('Ç');
752 return;
753 case 'E': case 'È': case 'É': case 'Ê': case 'Ë':
754 regmbc('E'); regmbc('È'); regmbc('É'); regmbc('Ê');
755 regmbc('Ë');
756 return;
757 case 'I': case 'Ì': case 'Í': case 'Î': case 'Ï':
758 regmbc('I'); regmbc('Ì'); regmbc('Í'); regmbc('Î');
759 regmbc('Ï');
760 return;
761 case 'N': case 'Ñ':
762 regmbc('N'); regmbc('Ñ');
763 return;
764 case 'O': case 'Ò': case 'Ó': case 'Ô': case 'Õ': case 'Ö':
765 regmbc('O'); regmbc('Ò'); regmbc('Ó'); regmbc('Ô');
766 regmbc('Õ'); regmbc('Ö');
767 return;
768 case 'U': case 'Ù': case 'Ú': case 'Û': case 'Ü':
769 regmbc('U'); regmbc('Ù'); regmbc('Ú'); regmbc('Û');
770 regmbc('Ü');
771 return;
772 case 'Y': case 'Ý':
773 regmbc('Y'); regmbc('Ý');
774 return;
775 case 'a': case 'à': case 'á': case 'â':
776 case 'ã': case 'ä': case 'å':
777 regmbc('a'); regmbc('à'); regmbc('á'); regmbc('â');
778 regmbc('ã'); regmbc('ä'); regmbc('å');
779 return;
780 case 'c': case 'ç':
781 regmbc('c'); regmbc('ç');
782 return;
783 case 'e': case 'è': case 'é': case 'ê': case 'ë':
784 regmbc('e'); regmbc('è'); regmbc('é'); regmbc('ê');
785 regmbc('ë');
786 return;
787 case 'i': case 'ì': case 'í': case 'î': case 'ï':
788 regmbc('i'); regmbc('ì'); regmbc('í'); regmbc('î');
789 regmbc('ï');
790 return;
791 case 'n': case 'ñ':
792 regmbc('n'); regmbc('ñ');
793 return;
794 case 'o': case 'ò': case 'ó': case 'ô': case 'õ': case 'ö':
795 regmbc('o'); regmbc('ò'); regmbc('ó'); regmbc('ô');
796 regmbc('õ'); regmbc('ö');
797 return;
798 case 'u': case 'ù': case 'ú': case 'û': case 'ü':
799 regmbc('u'); regmbc('ù'); regmbc('ú'); regmbc('û');
800 regmbc('ü');
801 return;
802 case 'y': case 'ý': case 'ÿ':
803 regmbc('y'); regmbc('ý'); regmbc('ÿ');
804 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 Moolenaar19a09a12005-03-04 23:39:37 +00001455 regtail(regnode(BACKP), 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 Moolenaar19a09a12005-03-04 23:39:37 +00002490 if (OP(scan) == BACK || OP(scan) == BACKP)
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().
2968 */
2969typedef struct
2970{
2971 union
2972 {
2973 char_u *ptr; /* reginput pointer, for single-line regexp */
2974 lpos_T pos; /* reginput pos, for multi-line regexp */
2975 } rs_u;
2976} regsave_T;
2977
2978/* struct to save start/end pointer/position in for \(\) */
2979typedef struct
2980{
2981 union
2982 {
2983 char_u *ptr;
2984 lpos_T pos;
2985 } se_u;
2986} save_se_T;
2987
2988static char_u *reg_getline __ARGS((linenr_T lnum));
2989static long vim_regexec_both __ARGS((char_u *line, colnr_T col));
2990static long regtry __ARGS((regprog_T *prog, colnr_T col));
2991static void cleanup_subexpr __ARGS((void));
2992#ifdef FEAT_SYN_HL
2993static void cleanup_zsubexpr __ARGS((void));
2994#endif
2995static void reg_nextline __ARGS((void));
2996static void reg_save __ARGS((regsave_T *save));
2997static void reg_restore __ARGS((regsave_T *save));
2998static int reg_save_equal __ARGS((regsave_T *save));
2999static void save_se_multi __ARGS((save_se_T *savep, lpos_T *posp));
3000static void save_se_one __ARGS((save_se_T *savep, char_u **pp));
3001
3002/* Save the sub-expressions before attempting a match. */
3003#define save_se(savep, posp, pp) \
3004 REG_MULTI ? save_se_multi((savep), (posp)) : save_se_one((savep), (pp))
3005
3006/* After a failed match restore the sub-expressions. */
3007#define restore_se(savep, posp, pp) { \
3008 if (REG_MULTI) \
3009 *(posp) = (savep)->se_u.pos; \
3010 else \
3011 *(pp) = (savep)->se_u.ptr; }
3012
3013static int re_num_cmp __ARGS((long_u val, char_u *scan));
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003014static int regmatch __ARGS((char_u *prog));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015static int regrepeat __ARGS((char_u *p, long maxcount));
3016
3017#ifdef DEBUG
3018int regnarrate = 0;
3019#endif
3020
3021/*
3022 * Internal copy of 'ignorecase'. It is set at each call to vim_regexec().
3023 * Normally it gets the value of "rm_ic" or "rmm_ic", but when the pattern
3024 * contains '\c' or '\C' the value is overruled.
3025 */
3026static int ireg_ic;
3027
3028#ifdef FEAT_MBYTE
3029/*
3030 * Similar to ireg_ic, but only for 'combining' characters. Set with \Z flag
3031 * in the regexp. Defaults to false, always.
3032 */
3033static int ireg_icombine;
3034#endif
3035
3036/*
3037 * Sometimes need to save a copy of a line. Since alloc()/free() is very
3038 * slow, we keep one allocated piece of memory and only re-allocate it when
3039 * it's too small. It's freed in vim_regexec_both() when finished.
3040 */
3041static char_u *reg_tofree;
3042static unsigned reg_tofreelen;
3043
3044/*
3045 * These variables are set when executing a regexp to speed up the execution.
3046 * Which ones are set depends on whethere a single-line or multi-line match is
3047 * done:
3048 * single-line multi-line
3049 * reg_match &regmatch_T NULL
3050 * reg_mmatch NULL &regmmatch_T
3051 * reg_startp reg_match->startp <invalid>
3052 * reg_endp reg_match->endp <invalid>
3053 * reg_startpos <invalid> reg_mmatch->startpos
3054 * reg_endpos <invalid> reg_mmatch->endpos
3055 * reg_win NULL window in which to search
3056 * reg_buf <invalid> buffer in which to search
3057 * reg_firstlnum <invalid> first line in which to search
3058 * reg_maxline 0 last line nr
3059 * reg_line_lbr FALSE or TRUE FALSE
3060 */
3061static regmatch_T *reg_match;
3062static regmmatch_T *reg_mmatch;
3063static char_u **reg_startp = NULL;
3064static char_u **reg_endp = NULL;
3065static lpos_T *reg_startpos = NULL;
3066static lpos_T *reg_endpos = NULL;
3067static win_T *reg_win;
3068static buf_T *reg_buf;
3069static linenr_T reg_firstlnum;
3070static linenr_T reg_maxline;
3071static int reg_line_lbr; /* "\n" in string is line break */
3072
3073/*
3074 * Get pointer to the line "lnum", which is relative to "reg_firstlnum".
3075 */
3076 static char_u *
3077reg_getline(lnum)
3078 linenr_T lnum;
3079{
3080 /* when looking behind for a match/no-match lnum is negative. But we
3081 * can't go before line 1 */
3082 if (reg_firstlnum + lnum < 1)
3083 return NULL;
3084 return ml_get_buf(reg_buf, reg_firstlnum + lnum, FALSE);
3085}
3086
3087static regsave_T behind_pos;
3088
3089#ifdef FEAT_SYN_HL
3090static char_u *reg_startzp[NSUBEXP]; /* Workspace to mark beginning */
3091static char_u *reg_endzp[NSUBEXP]; /* and end of \z(...\) matches */
3092static lpos_T reg_startzpos[NSUBEXP]; /* idem, beginning pos */
3093static lpos_T reg_endzpos[NSUBEXP]; /* idem, end pos */
3094#endif
3095
3096/* TRUE if using multi-line regexp. */
3097#define REG_MULTI (reg_match == NULL)
3098
3099/*
3100 * Match a regexp against a string.
3101 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
3102 * Uses curbuf for line count and 'iskeyword'.
3103 *
3104 * Return TRUE if there is a match, FALSE if not.
3105 */
3106 int
3107vim_regexec(rmp, line, col)
3108 regmatch_T *rmp;
3109 char_u *line; /* string to match against */
3110 colnr_T col; /* column to start looking for match */
3111{
3112 reg_match = rmp;
3113 reg_mmatch = NULL;
3114 reg_maxline = 0;
3115 reg_line_lbr = FALSE;
3116 reg_win = NULL;
3117 ireg_ic = rmp->rm_ic;
3118#ifdef FEAT_MBYTE
3119 ireg_icombine = FALSE;
3120#endif
3121 return (vim_regexec_both(line, col) != 0);
3122}
3123
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003124#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
3125 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126/*
3127 * Like vim_regexec(), but consider a "\n" in "line" to be a line break.
3128 */
3129 int
3130vim_regexec_nl(rmp, line, col)
3131 regmatch_T *rmp;
3132 char_u *line; /* string to match against */
3133 colnr_T col; /* column to start looking for match */
3134{
3135 reg_match = rmp;
3136 reg_mmatch = NULL;
3137 reg_maxline = 0;
3138 reg_line_lbr = TRUE;
3139 reg_win = NULL;
3140 ireg_ic = rmp->rm_ic;
3141#ifdef FEAT_MBYTE
3142 ireg_icombine = FALSE;
3143#endif
3144 return (vim_regexec_both(line, col) != 0);
3145}
3146#endif
3147
3148/*
3149 * Match a regexp against multiple lines.
3150 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
3151 * Uses curbuf for line count and 'iskeyword'.
3152 *
3153 * Return zero if there is no match. Return number of lines contained in the
3154 * match otherwise.
3155 */
3156 long
3157vim_regexec_multi(rmp, win, buf, lnum, col)
3158 regmmatch_T *rmp;
3159 win_T *win; /* window in which to search or NULL */
3160 buf_T *buf; /* buffer in which to search */
3161 linenr_T lnum; /* nr of line to start looking for match */
3162 colnr_T col; /* column to start looking for match */
3163{
3164 long r;
3165 buf_T *save_curbuf = curbuf;
3166
3167 reg_match = NULL;
3168 reg_mmatch = rmp;
3169 reg_buf = buf;
3170 reg_win = win;
3171 reg_firstlnum = lnum;
3172 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
3173 reg_line_lbr = FALSE;
3174 ireg_ic = rmp->rmm_ic;
3175#ifdef FEAT_MBYTE
3176 ireg_icombine = FALSE;
3177#endif
3178
3179 /* Need to switch to buffer "buf" to make vim_iswordc() work. */
3180 curbuf = buf;
3181 r = vim_regexec_both(NULL, col);
3182 curbuf = save_curbuf;
3183
3184 return r;
3185}
3186
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003187#if 0 /* disabled, no longer needed now that regmatch() is not recursive */
3188# ifdef HAVE_SETJMP_H
3189# define USE_SETJMP
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00003190# endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003191# ifdef HAVE_TRY_EXCEPT
3192# define USE_TRY_EXCEPT
3193# endif
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00003194#endif
3195
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196/*
3197 * Match a regexp against a string ("line" points to the string) or multiple
3198 * lines ("line" is NULL, use reg_getline()).
3199 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003200#ifdef USE_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 static long
3202vim_regexec_both(line_arg, col_arg)
3203 char_u *line_arg;
3204 colnr_T col_arg; /* column to start looking for match */
3205#else
3206 static long
3207vim_regexec_both(line, col)
3208 char_u *line;
3209 colnr_T col; /* column to start looking for match */
3210#endif
3211{
3212 regprog_T *prog;
3213 char_u *s;
3214 long retval;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003215#ifdef USE_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 char_u *line;
3217 colnr_T col;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003218 int did_mch_startjmp = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219#endif
3220
3221 reg_tofree = NULL;
3222
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003223#ifdef USE_SETJMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 /* Trick to avoid "might be clobbered by `longjmp'" warning from gcc. */
3225 line = line_arg;
3226 col = col_arg;
3227#endif
3228 retval = 0L;
3229
3230 if (REG_MULTI)
3231 {
3232 prog = reg_mmatch->regprog;
3233 line = reg_getline((linenr_T)0);
3234 reg_startpos = reg_mmatch->startpos;
3235 reg_endpos = reg_mmatch->endpos;
3236 }
3237 else
3238 {
3239 prog = reg_match->regprog;
3240 reg_startp = reg_match->startp;
3241 reg_endp = reg_match->endp;
3242 }
3243
3244 /* Be paranoid... */
3245 if (prog == NULL || line == NULL)
3246 {
3247 EMSG(_(e_null));
3248 goto theend;
3249 }
3250
3251 /* Check validity of program. */
3252 if (prog_magic_wrong())
3253 goto theend;
3254
3255 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
3256 if (prog->regflags & RF_ICASE)
3257 ireg_ic = TRUE;
3258 else if (prog->regflags & RF_NOICASE)
3259 ireg_ic = FALSE;
3260
3261#ifdef FEAT_MBYTE
3262 /* If pattern contains "\Z" overrule value of ireg_icombine */
3263 if (prog->regflags & RF_ICOMBINE)
3264 ireg_icombine = TRUE;
3265#endif
3266
3267 /* If there is a "must appear" string, look for it. */
3268 if (prog->regmust != NULL)
3269 {
3270 int c;
3271
3272#ifdef FEAT_MBYTE
3273 if (has_mbyte)
3274 c = (*mb_ptr2char)(prog->regmust);
3275 else
3276#endif
3277 c = *prog->regmust;
3278 s = line + col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003279
3280 /*
3281 * This is used very often, esp. for ":global". Use three versions of
3282 * the loop to avoid overhead of conditions.
3283 */
3284 if (!ireg_ic
3285#ifdef FEAT_MBYTE
3286 && !has_mbyte
3287#endif
3288 )
3289 while ((s = vim_strbyte(s, c)) != NULL)
3290 {
3291 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3292 break; /* Found it. */
3293 ++s;
3294 }
3295#ifdef FEAT_MBYTE
3296 else if (!ireg_ic || (!enc_utf8 && mb_char2len(c) > 1))
3297 while ((s = vim_strchr(s, c)) != NULL)
3298 {
3299 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3300 break; /* Found it. */
3301 mb_ptr_adv(s);
3302 }
3303#endif
3304 else
3305 while ((s = cstrchr(s, c)) != NULL)
3306 {
3307 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3308 break; /* Found it. */
3309 mb_ptr_adv(s);
3310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 if (s == NULL) /* Not present. */
3312 goto theend;
3313 }
3314
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003315#ifdef USE_TRY_EXCEPT
Bram Moolenaar748bf032005-02-02 23:04:36 +00003316 __try
3317 {
3318#endif
3319
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003320#ifdef USE_SETJMP
Bram Moolenaar748bf032005-02-02 23:04:36 +00003321 /*
3322 * Matching with a regexp may cause a very deep recursive call of
3323 * regmatch(). Vim will crash when running out of stack space. Catch
3324 * this here if the system supports it.
3325 * It's a bit slow, do it after the check for "regmust".
3326 * Don't do it if the caller already set it up.
3327 */
3328 if (!lc_active)
3329 {
3330 did_mch_startjmp = TRUE;
3331 mch_startjmp();
3332 if (SETJMP(lc_jump_env) != 0)
3333 {
3334 mch_didjmp();
3335# ifdef SIGHASARG
3336 if (lc_signal != SIGINT)
3337# endif
3338 EMSG(_(e_complex));
3339 retval = 0L;
3340 goto inner_end;
3341 }
3342 }
3343#endif
3344
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 regline = line;
3346 reglnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347
3348 /* Simplest case: Anchored match need be tried only once. */
3349 if (prog->reganch)
3350 {
3351 int c;
3352
3353#ifdef FEAT_MBYTE
3354 if (has_mbyte)
3355 c = (*mb_ptr2char)(regline + col);
3356 else
3357#endif
3358 c = regline[col];
3359 if (prog->regstart == NUL
3360 || prog->regstart == c
3361 || (ireg_ic && ((
3362#ifdef FEAT_MBYTE
3363 (enc_utf8 && utf_fold(prog->regstart) == utf_fold(c)))
3364 || (c < 255 && prog->regstart < 255 &&
3365#endif
3366 TOLOWER_LOC(prog->regstart) == TOLOWER_LOC(c)))))
3367 retval = regtry(prog, col);
3368 else
3369 retval = 0;
3370 }
3371 else
3372 {
3373 /* Messy cases: unanchored match. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003374 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 {
3376 if (prog->regstart != NUL)
3377 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003378 /* Skip until the char we know it must start with.
3379 * Used often, do some work to avoid call overhead. */
3380 if (!ireg_ic
3381#ifdef FEAT_MBYTE
3382 && !has_mbyte
3383#endif
3384 )
3385 s = vim_strbyte(regline + col, prog->regstart);
3386 else
3387 s = cstrchr(regline + col, prog->regstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 if (s == NULL)
3389 {
3390 retval = 0;
3391 break;
3392 }
3393 col = (int)(s - regline);
3394 }
3395
3396 retval = regtry(prog, col);
3397 if (retval > 0)
3398 break;
3399
3400 /* if not currently on the first line, get it again */
3401 if (reglnum != 0)
3402 {
3403 regline = reg_getline((linenr_T)0);
3404 reglnum = 0;
3405 }
3406 if (regline[col] == NUL)
3407 break;
3408#ifdef FEAT_MBYTE
3409 if (has_mbyte)
3410 col += (*mb_ptr2len_check)(regline + col);
3411 else
3412#endif
3413 ++col;
3414 }
3415 }
3416
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003417#ifdef USE_SETJMP
Bram Moolenaar748bf032005-02-02 23:04:36 +00003418inner_end:
Bram Moolenaar05159a02005-02-26 23:04:13 +00003419 if (did_mch_startjmp)
3420 mch_endjmp();
Bram Moolenaar748bf032005-02-02 23:04:36 +00003421#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003422#ifdef USE_TRY_EXCEPT
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 }
3424 __except(EXCEPTION_EXECUTE_HANDLER)
3425 {
3426 if (GetExceptionCode() == EXCEPTION_STACK_OVERFLOW)
3427 {
3428 RESETSTKOFLW();
Bram Moolenaar748bf032005-02-02 23:04:36 +00003429 EMSG(_(e_outofstack));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 }
3431 else
Bram Moolenaar748bf032005-02-02 23:04:36 +00003432 EMSG(_(e_complex));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 retval = 0L;
3434 }
3435#endif
3436
3437theend:
3438 /* Didn't find a match. */
3439 vim_free(reg_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 return retval;
3441}
3442
3443#ifdef FEAT_SYN_HL
3444static reg_extmatch_T *make_extmatch __ARGS((void));
3445
3446/*
3447 * Create a new extmatch and mark it as referenced once.
3448 */
3449 static reg_extmatch_T *
3450make_extmatch()
3451{
3452 reg_extmatch_T *em;
3453
3454 em = (reg_extmatch_T *)alloc_clear((unsigned)sizeof(reg_extmatch_T));
3455 if (em != NULL)
3456 em->refcnt = 1;
3457 return em;
3458}
3459
3460/*
3461 * Add a reference to an extmatch.
3462 */
3463 reg_extmatch_T *
3464ref_extmatch(em)
3465 reg_extmatch_T *em;
3466{
3467 if (em != NULL)
3468 em->refcnt++;
3469 return em;
3470}
3471
3472/*
3473 * Remove a reference to an extmatch. If there are no references left, free
3474 * the info.
3475 */
3476 void
3477unref_extmatch(em)
3478 reg_extmatch_T *em;
3479{
3480 int i;
3481
3482 if (em != NULL && --em->refcnt <= 0)
3483 {
3484 for (i = 0; i < NSUBEXP; ++i)
3485 vim_free(em->matches[i]);
3486 vim_free(em);
3487 }
3488}
3489#endif
3490
3491/*
3492 * regtry - try match of "prog" with at regline["col"].
3493 * Returns 0 for failure, number of lines contained in the match otherwise.
3494 */
3495 static long
3496regtry(prog, col)
3497 regprog_T *prog;
3498 colnr_T col;
3499{
3500 reginput = regline + col;
3501 need_clear_subexpr = TRUE;
3502#ifdef FEAT_SYN_HL
3503 /* Clear the external match subpointers if necessary. */
3504 if (prog->reghasz == REX_SET)
3505 need_clear_zsubexpr = TRUE;
3506#endif
3507
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003508 if (regmatch(prog->program + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 {
3510 cleanup_subexpr();
3511 if (REG_MULTI)
3512 {
3513 if (reg_startpos[0].lnum < 0)
3514 {
3515 reg_startpos[0].lnum = 0;
3516 reg_startpos[0].col = col;
3517 }
3518 if (reg_endpos[0].lnum < 0)
3519 {
3520 reg_endpos[0].lnum = reglnum;
3521 reg_endpos[0].col = (int)(reginput - regline);
3522 }
3523 else
3524 /* Use line number of "\ze". */
3525 reglnum = reg_endpos[0].lnum;
3526 }
3527 else
3528 {
3529 if (reg_startp[0] == NULL)
3530 reg_startp[0] = regline + col;
3531 if (reg_endp[0] == NULL)
3532 reg_endp[0] = reginput;
3533 }
3534#ifdef FEAT_SYN_HL
3535 /* Package any found \z(...\) matches for export. Default is none. */
3536 unref_extmatch(re_extmatch_out);
3537 re_extmatch_out = NULL;
3538
3539 if (prog->reghasz == REX_SET)
3540 {
3541 int i;
3542
3543 cleanup_zsubexpr();
3544 re_extmatch_out = make_extmatch();
3545 for (i = 0; i < NSUBEXP; i++)
3546 {
3547 if (REG_MULTI)
3548 {
3549 /* Only accept single line matches. */
3550 if (reg_startzpos[i].lnum >= 0
3551 && reg_endzpos[i].lnum == reg_startzpos[i].lnum)
3552 re_extmatch_out->matches[i] =
3553 vim_strnsave(reg_getline(reg_startzpos[i].lnum)
3554 + reg_startzpos[i].col,
3555 reg_endzpos[i].col - reg_startzpos[i].col);
3556 }
3557 else
3558 {
3559 if (reg_startzp[i] != NULL && reg_endzp[i] != NULL)
3560 re_extmatch_out->matches[i] =
3561 vim_strnsave(reg_startzp[i],
3562 (int)(reg_endzp[i] - reg_startzp[i]));
3563 }
3564 }
3565 }
3566#endif
3567 return 1 + reglnum;
3568 }
3569 return 0;
3570}
3571
3572#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573static int reg_prev_class __ARGS((void));
3574
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575/*
3576 * Get class of previous character.
3577 */
3578 static int
3579reg_prev_class()
3580{
3581 if (reginput > regline)
3582 return mb_get_class(reginput - 1
3583 - (*mb_head_off)(regline, reginput - 1));
3584 return -1;
3585}
3586
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003588#define ADVANCE_REGINPUT() mb_ptr_adv(reginput)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589
3590/*
3591 * The arguments from BRACE_LIMITS are stored here. They are actually local
3592 * to regmatch(), but they are here to reduce the amount of stack space used
3593 * (it can be called recursively many times).
3594 */
3595static long bl_minval;
3596static long bl_maxval;
3597
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003598/* Values for rs_state. */
3599typedef enum regstate_E
3600{
3601 RS_NOPEN = 0 /* NOPEN and NCLOSE */
3602 , RS_MOPEN /* MOPEN + [0-9] */
3603 , RS_MCLOSE /* MCLOSE + [0-9] */
3604#ifdef FEAT_SYN_HL
3605 , RS_ZOPEN /* ZOPEN + [0-9] */
3606 , RS_ZCLOSE /* ZCLOSE + [0-9] */
3607#endif
3608 , RS_BRANCH /* BRANCH */
3609 , RS_BRCPLX_MORE /* BRACE_COMPLEX and trying one more match */
3610 , RS_BRCPLX_LONG /* BRACE_COMPLEX and trying longest match */
3611 , RS_BRCPLX_SHORT /* BRACE_COMPLEX and trying shortest match */
3612 , RS_NOMATCH /* NOMATCH */
3613 , RS_BEHIND1 /* BEHIND / NOBEHIND matching rest */
3614 , RS_BEHIND2 /* BEHIND / NOBEHIND matching behind part */
3615 , RS_STAR_LONG /* STAR/PLUS/BRACE_SIMPLE longest match */
3616 , RS_STAR_SHORT /* STAR/PLUS/BRACE_SIMPLE shortest match */
3617} regstate_T;
3618
3619/*
3620 * When there are alternatives a regstate_T is put on the regstack to remember
3621 * what we are doing.
3622 * Before it may be another type of item, depending on rs_state, to remember
3623 * more things.
3624 */
3625typedef struct regitem_S
3626{
3627 regstate_T rs_state; /* what we are doing, on of RS_ below */
3628 char_u *rs_scan; /* current node in program */
3629 long rs_startp; /* start position for BACK (offset) */
3630 union
3631 {
3632 save_se_T sesave;
3633 regsave_T regsave;
3634 } rs_un; /* room for saving reginput */
3635 short rs_no; /* submatch nr */
3636} regitem_T;
3637
3638static regitem_T *regstack_push __ARGS((garray_T *regstack, regstate_T state, char_u *scan, long startp));
3639static void regstack_pop __ARGS((garray_T *regstack, char_u **scan, long *startp));
3640
3641/* used for BEHIND and NOBEHIND matching */
3642typedef struct regbehind_S
3643{
3644 regsave_T save_after;
3645 regsave_T save_behind;
3646} regbehind_T;
3647
3648/* used for STAR, PLUS and BRACE_SIMPLE matching */
3649typedef struct regstar_S
3650{
3651 int nextb; /* next byte */
3652 int nextb_ic; /* next byte reverse case */
3653 long count;
3654 long minval;
3655 long maxval;
3656} regstar_T;
3657
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658/*
3659 * regmatch - main matching routine
3660 *
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003661 * Conceptually the strategy is simple: Check to see whether the current node
3662 * matches, push an item onto the regstack and loop to see whether the rest
3663 * matches, and then act accordingly. In practice we make some effort to
3664 * avoid using the regstack, in particular by going through "ordinary" nodes
3665 * (that don't need to know whether the rest of the match failed) by a nested
3666 * loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 *
3668 * Returns TRUE when there is a match. Leaves reginput and reglnum just after
3669 * the last matched character.
3670 * Returns FALSE when there is no match. Leaves reginput and reglnum in an
3671 * undefined state!
3672 */
3673 static int
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003674regmatch(scan)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 char_u *scan; /* Current node. */
3676{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003677 char_u *next; /* Next node. */
3678 int op;
3679 int c;
3680 garray_T regstack;
3681 regitem_T *rp;
3682 int no;
3683 int status; /* one of the RA_ values: */
3684#define RA_FAIL 1 /* something failed, abort */
3685#define RA_CONT 2 /* continue in inner loop */
3686#define RA_BREAK 3 /* break inner loop */
3687#define RA_MATCH 4 /* successful match */
3688#define RA_NOMATCH 5 /* didn't match */
3689 long startp = 0; /* start position for BACK, offset to
3690 regstack.ga_data */
3691#define STARTP2REGS(startp) (regsave_T *)(((char *)regstack.ga_data) + startp)
3692#define REGS2STARTP(p) (long)((char *)p - (char *)regstack.ga_data)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003694 /* Init the regstack empty. Use an item size of 1 byte, since we push
3695 * different things onto it. Use a large grow size to avoid reallocating
3696 * it too often. */
3697 ga_init2(&regstack, 1, 10000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003699 /*
3700 * Repeat until the stack is empty.
3701 */
3702 for (;;)
3703 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 /* Some patterns my cause a long time to match, even though they are not
3705 * illegal. E.g., "\([a-z]\+\)\+Q". Allow breaking them with CTRL-C. */
3706 fast_breakcheck();
3707
3708#ifdef DEBUG
3709 if (scan != NULL && regnarrate)
3710 {
3711 mch_errmsg(regprop(scan));
3712 mch_errmsg("(\n");
3713 }
3714#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003715
3716 /*
3717 * Repeat for items that can be matched sequential, without using the
3718 * regstack.
3719 */
3720 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003722 if (got_int || scan == NULL)
3723 {
3724 status = RA_FAIL;
3725 break;
3726 }
3727 status = RA_CONT;
3728
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729#ifdef DEBUG
3730 if (regnarrate)
3731 {
3732 mch_errmsg(regprop(scan));
3733 mch_errmsg("...\n");
3734# ifdef FEAT_SYN_HL
3735 if (re_extmatch_in != NULL)
3736 {
3737 int i;
3738
3739 mch_errmsg(_("External submatches:\n"));
3740 for (i = 0; i < NSUBEXP; i++)
3741 {
3742 mch_errmsg(" \"");
3743 if (re_extmatch_in->matches[i] != NULL)
3744 mch_errmsg(re_extmatch_in->matches[i]);
3745 mch_errmsg("\"\n");
3746 }
3747 }
3748# endif
3749 }
3750#endif
3751 next = regnext(scan);
3752
3753 op = OP(scan);
3754 /* Check for character class with NL added. */
3755 if (WITH_NL(op) && *reginput == NUL && reglnum < reg_maxline)
3756 {
3757 reg_nextline();
3758 }
3759 else if (reg_line_lbr && WITH_NL(op) && *reginput == '\n')
3760 {
3761 ADVANCE_REGINPUT();
3762 }
3763 else
3764 {
3765 if (WITH_NL(op))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003766 op -= ADD_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767#ifdef FEAT_MBYTE
3768 if (has_mbyte)
3769 c = (*mb_ptr2char)(reginput);
3770 else
3771#endif
3772 c = *reginput;
3773 switch (op)
3774 {
3775 case BOL:
3776 if (reginput != regline)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003777 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 break;
3779
3780 case EOL:
3781 if (c != NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003782 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 break;
3784
3785 case RE_BOF:
3786 /* Passing -1 to the getline() function provided for the search
3787 * should always return NULL if the current line is the first
3788 * line of the file. */
3789 if (reglnum != 0 || reginput != regline
3790 || (REG_MULTI && reg_getline((linenr_T)-1) != NULL))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003791 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 break;
3793
3794 case RE_EOF:
3795 if (reglnum != reg_maxline || c != NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003796 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 break;
3798
3799 case CURSOR:
3800 /* Check if the buffer is in a window and compare the
3801 * reg_win->w_cursor position to the match position. */
3802 if (reg_win == NULL
3803 || (reglnum + reg_firstlnum != reg_win->w_cursor.lnum)
3804 || ((colnr_T)(reginput - regline) != reg_win->w_cursor.col))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003805 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 break;
3807
3808 case RE_LNUM:
3809 if (!REG_MULTI || !re_num_cmp((long_u)(reglnum + reg_firstlnum),
3810 scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003811 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 break;
3813
3814 case RE_COL:
3815 if (!re_num_cmp((long_u)(reginput - regline) + 1, scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003816 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 break;
3818
3819 case RE_VCOL:
3820 if (!re_num_cmp((long_u)win_linetabsize(
3821 reg_win == NULL ? curwin : reg_win,
3822 regline, (colnr_T)(reginput - regline)) + 1, scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003823 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 break;
3825
3826 case BOW: /* \<word; reginput points to w */
3827 if (c == NUL) /* Can't match at end of line */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003828 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829#ifdef FEAT_MBYTE
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003830 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 {
3832 int this_class;
3833
3834 /* Get class of current and previous char (if it exists). */
3835 this_class = mb_get_class(reginput);
3836 if (this_class <= 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003837 status = RA_NOMATCH; /* not on a word at all */
3838 else if (reg_prev_class() == this_class)
3839 status = RA_NOMATCH; /* previous char is in same word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 }
3841#endif
3842 else
3843 {
3844 if (!vim_iswordc(c)
3845 || (reginput > regline && vim_iswordc(reginput[-1])))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003846 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 }
3848 break;
3849
3850 case EOW: /* word\>; reginput points after d */
3851 if (reginput == regline) /* Can't match at start of line */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003852 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853#ifdef FEAT_MBYTE
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003854 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 {
3856 int this_class, prev_class;
3857
3858 /* Get class of current and previous char (if it exists). */
3859 this_class = mb_get_class(reginput);
3860 prev_class = reg_prev_class();
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003861 if (this_class == prev_class
3862 || prev_class == 0 || prev_class == 1)
3863 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003866 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003868 if (!vim_iswordc(reginput[-1])
3869 || (reginput[0] != NUL && vim_iswordc(c)))
3870 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 }
3872 break; /* Matched with EOW */
3873
3874 case ANY:
3875 if (c == NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003876 status = RA_NOMATCH;
3877 else
3878 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 break;
3880
3881 case IDENT:
3882 if (!vim_isIDc(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003883 status = RA_NOMATCH;
3884 else
3885 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 break;
3887
3888 case SIDENT:
3889 if (VIM_ISDIGIT(*reginput) || !vim_isIDc(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003890 status = RA_NOMATCH;
3891 else
3892 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 break;
3894
3895 case KWORD:
3896 if (!vim_iswordp(reginput))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003897 status = RA_NOMATCH;
3898 else
3899 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 break;
3901
3902 case SKWORD:
3903 if (VIM_ISDIGIT(*reginput) || !vim_iswordp(reginput))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003904 status = RA_NOMATCH;
3905 else
3906 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 break;
3908
3909 case FNAME:
3910 if (!vim_isfilec(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003911 status = RA_NOMATCH;
3912 else
3913 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 break;
3915
3916 case SFNAME:
3917 if (VIM_ISDIGIT(*reginput) || !vim_isfilec(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003918 status = RA_NOMATCH;
3919 else
3920 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 break;
3922
3923 case PRINT:
3924 if (ptr2cells(reginput) != 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003925 status = RA_NOMATCH;
3926 else
3927 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 break;
3929
3930 case SPRINT:
3931 if (VIM_ISDIGIT(*reginput) || ptr2cells(reginput) != 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003932 status = RA_NOMATCH;
3933 else
3934 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 break;
3936
3937 case WHITE:
3938 if (!vim_iswhite(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003939 status = RA_NOMATCH;
3940 else
3941 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 break;
3943
3944 case NWHITE:
3945 if (c == NUL || vim_iswhite(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003946 status = RA_NOMATCH;
3947 else
3948 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 break;
3950
3951 case DIGIT:
3952 if (!ri_digit(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003953 status = RA_NOMATCH;
3954 else
3955 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 break;
3957
3958 case NDIGIT:
3959 if (c == NUL || ri_digit(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003960 status = RA_NOMATCH;
3961 else
3962 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 break;
3964
3965 case HEX:
3966 if (!ri_hex(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003967 status = RA_NOMATCH;
3968 else
3969 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 break;
3971
3972 case NHEX:
3973 if (c == NUL || ri_hex(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003974 status = RA_NOMATCH;
3975 else
3976 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 break;
3978
3979 case OCTAL:
3980 if (!ri_octal(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003981 status = RA_NOMATCH;
3982 else
3983 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 break;
3985
3986 case NOCTAL:
3987 if (c == NUL || ri_octal(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003988 status = RA_NOMATCH;
3989 else
3990 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 break;
3992
3993 case WORD:
3994 if (!ri_word(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003995 status = RA_NOMATCH;
3996 else
3997 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 break;
3999
4000 case NWORD:
4001 if (c == NUL || ri_word(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004002 status = RA_NOMATCH;
4003 else
4004 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 break;
4006
4007 case HEAD:
4008 if (!ri_head(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004009 status = RA_NOMATCH;
4010 else
4011 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 break;
4013
4014 case NHEAD:
4015 if (c == NUL || ri_head(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004016 status = RA_NOMATCH;
4017 else
4018 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 break;
4020
4021 case ALPHA:
4022 if (!ri_alpha(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004023 status = RA_NOMATCH;
4024 else
4025 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 break;
4027
4028 case NALPHA:
4029 if (c == NUL || ri_alpha(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004030 status = RA_NOMATCH;
4031 else
4032 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 break;
4034
4035 case LOWER:
4036 if (!ri_lower(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004037 status = RA_NOMATCH;
4038 else
4039 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 break;
4041
4042 case NLOWER:
4043 if (c == NUL || ri_lower(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004044 status = RA_NOMATCH;
4045 else
4046 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 break;
4048
4049 case UPPER:
4050 if (!ri_upper(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004051 status = RA_NOMATCH;
4052 else
4053 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 break;
4055
4056 case NUPPER:
4057 if (c == NUL || ri_upper(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004058 status = RA_NOMATCH;
4059 else
4060 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 break;
4062
4063 case EXACTLY:
4064 {
4065 int len;
4066 char_u *opnd;
4067
4068 opnd = OPERAND(scan);
4069 /* Inline the first byte, for speed. */
4070 if (*opnd != *reginput
4071 && (!ireg_ic || (
4072#ifdef FEAT_MBYTE
4073 !enc_utf8 &&
4074#endif
4075 TOLOWER_LOC(*opnd) != TOLOWER_LOC(*reginput))))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004076 status = RA_NOMATCH;
4077 else if (*opnd == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 {
4079 /* match empty string always works; happens when "~" is
4080 * empty. */
4081 }
4082 else if (opnd[1] == NUL
4083#ifdef FEAT_MBYTE
4084 && !(enc_utf8 && ireg_ic)
4085#endif
4086 )
4087 ++reginput; /* matched a single char */
4088 else
4089 {
4090 len = (int)STRLEN(opnd);
4091 /* Need to match first byte again for multi-byte. */
4092 if (cstrncmp(opnd, reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004093 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094#ifdef FEAT_MBYTE
4095 /* Check for following composing character. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004096 else if (enc_utf8
4097 && UTF_COMPOSINGLIKE(reginput, reginput + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 {
4099 /* raaron: This code makes a composing character get
4100 * ignored, which is the correct behavior (sometimes)
4101 * for voweled Hebrew texts. */
4102 if (!ireg_icombine)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004103 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004106 else
4107 reginput += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 }
4109 }
4110 break;
4111
4112 case ANYOF:
4113 case ANYBUT:
4114 if (c == NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004115 status = RA_NOMATCH;
4116 else if ((cstrchr(OPERAND(scan), c) == NULL) == (op == ANYOF))
4117 status = RA_NOMATCH;
4118 else
4119 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 break;
4121
4122#ifdef FEAT_MBYTE
4123 case MULTIBYTECODE:
4124 if (has_mbyte)
4125 {
4126 int i, len;
4127 char_u *opnd;
4128
4129 opnd = OPERAND(scan);
4130 /* Safety check (just in case 'encoding' was changed since
4131 * compiling the program). */
4132 if ((len = (*mb_ptr2len_check)(opnd)) < 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004133 {
4134 status = RA_NOMATCH;
4135 break;
4136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 for (i = 0; i < len; ++i)
4138 if (opnd[i] != reginput[i])
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004139 {
4140 status = RA_NOMATCH;
4141 break;
4142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 reginput += len;
4144 }
4145 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004146 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 break;
4148#endif
4149
4150 case NOTHING:
4151 break;
4152
4153 case BACK:
Bram Moolenaardf177f62005-02-22 08:39:57 +00004154 /* When we run into BACK without matching something non-empty, we
4155 * fail. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004156 if (startp != 0 && reg_save_equal(STARTP2REGS(startp)))
4157 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 break;
4159
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004160 case BACKP:
4161 break;
4162
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 case MOPEN + 0: /* Match start: \zs */
4164 case MOPEN + 1: /* \( */
4165 case MOPEN + 2:
4166 case MOPEN + 3:
4167 case MOPEN + 4:
4168 case MOPEN + 5:
4169 case MOPEN + 6:
4170 case MOPEN + 7:
4171 case MOPEN + 8:
4172 case MOPEN + 9:
4173 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 no = op - MOPEN;
4175 cleanup_subexpr();
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004176 rp = regstack_push(&regstack, RS_MOPEN, scan, startp);
4177 if (rp == NULL)
4178 status = RA_FAIL;
4179 else
4180 {
4181 rp->rs_no = no;
4182 save_se(&rp->rs_un.sesave, &reg_startpos[no],
4183 &reg_startp[no]);
4184 /* We simply continue and handle the result when done. */
4185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004187 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188
4189 case NOPEN: /* \%( */
4190 case NCLOSE: /* \) after \%( */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004191 if (regstack_push(&regstack, RS_NOPEN, scan, startp) == NULL)
4192 status = RA_FAIL;
4193 /* We simply continue and handle the result when done. */
4194 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195
4196#ifdef FEAT_SYN_HL
4197 case ZOPEN + 1:
4198 case ZOPEN + 2:
4199 case ZOPEN + 3:
4200 case ZOPEN + 4:
4201 case ZOPEN + 5:
4202 case ZOPEN + 6:
4203 case ZOPEN + 7:
4204 case ZOPEN + 8:
4205 case ZOPEN + 9:
4206 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 no = op - ZOPEN;
4208 cleanup_zsubexpr();
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004209 rp = regstack_push(&regstack, RS_ZOPEN, scan, startp);
4210 if (rp == NULL)
4211 status = RA_FAIL;
4212 else
4213 {
4214 rp->rs_no = no;
4215 save_se(&rp->rs_un.sesave, &reg_startzpos[no],
4216 &reg_startzp[no]);
4217 /* We simply continue and handle the result when done. */
4218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004220 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221#endif
4222
4223 case MCLOSE + 0: /* Match end: \ze */
4224 case MCLOSE + 1: /* \) */
4225 case MCLOSE + 2:
4226 case MCLOSE + 3:
4227 case MCLOSE + 4:
4228 case MCLOSE + 5:
4229 case MCLOSE + 6:
4230 case MCLOSE + 7:
4231 case MCLOSE + 8:
4232 case MCLOSE + 9:
4233 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 no = op - MCLOSE;
4235 cleanup_subexpr();
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004236 rp = regstack_push(&regstack, RS_MCLOSE, scan, startp);
4237 if (rp == NULL)
4238 status = RA_FAIL;
4239 else
4240 {
4241 rp->rs_no = no;
4242 save_se(&rp->rs_un.sesave, &reg_endpos[no], &reg_endp[no]);
4243 /* We simply continue and handle the result when done. */
4244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004246 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247
4248#ifdef FEAT_SYN_HL
4249 case ZCLOSE + 1: /* \) after \z( */
4250 case ZCLOSE + 2:
4251 case ZCLOSE + 3:
4252 case ZCLOSE + 4:
4253 case ZCLOSE + 5:
4254 case ZCLOSE + 6:
4255 case ZCLOSE + 7:
4256 case ZCLOSE + 8:
4257 case ZCLOSE + 9:
4258 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 no = op - ZCLOSE;
4260 cleanup_zsubexpr();
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004261 rp = regstack_push(&regstack, RS_ZCLOSE, scan, startp);
4262 if (rp == NULL)
4263 status = RA_FAIL;
4264 else
4265 {
4266 rp->rs_no = no;
4267 save_se(&rp->rs_un.sesave, &reg_endzpos[no],
4268 &reg_endzp[no]);
4269 /* We simply continue and handle the result when done. */
4270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004272 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273#endif
4274
4275 case BACKREF + 1:
4276 case BACKREF + 2:
4277 case BACKREF + 3:
4278 case BACKREF + 4:
4279 case BACKREF + 5:
4280 case BACKREF + 6:
4281 case BACKREF + 7:
4282 case BACKREF + 8:
4283 case BACKREF + 9:
4284 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 int len;
4286 linenr_T clnum;
4287 colnr_T ccol;
4288 char_u *p;
4289
4290 no = op - BACKREF;
4291 cleanup_subexpr();
4292 if (!REG_MULTI) /* Single-line regexp */
4293 {
4294 if (reg_endp[no] == NULL)
4295 {
4296 /* Backref was not set: Match an empty string. */
4297 len = 0;
4298 }
4299 else
4300 {
4301 /* Compare current input with back-ref in the same
4302 * line. */
4303 len = (int)(reg_endp[no] - reg_startp[no]);
4304 if (cstrncmp(reg_startp[no], reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004305 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 }
4307 }
4308 else /* Multi-line regexp */
4309 {
4310 if (reg_endpos[no].lnum < 0)
4311 {
4312 /* Backref was not set: Match an empty string. */
4313 len = 0;
4314 }
4315 else
4316 {
4317 if (reg_startpos[no].lnum == reglnum
4318 && reg_endpos[no].lnum == reglnum)
4319 {
4320 /* Compare back-ref within the current line. */
4321 len = reg_endpos[no].col - reg_startpos[no].col;
4322 if (cstrncmp(regline + reg_startpos[no].col,
4323 reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004324 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 }
4326 else
4327 {
4328 /* Messy situation: Need to compare between two
4329 * lines. */
4330 ccol = reg_startpos[no].col;
4331 clnum = reg_startpos[no].lnum;
4332 for (;;)
4333 {
4334 /* Since getting one line may invalidate
4335 * the other, need to make copy. Slow! */
4336 if (regline != reg_tofree)
4337 {
4338 len = (int)STRLEN(regline);
4339 if (reg_tofree == NULL
4340 || len >= (int)reg_tofreelen)
4341 {
4342 len += 50; /* get some extra */
4343 vim_free(reg_tofree);
4344 reg_tofree = alloc(len);
4345 if (reg_tofree == NULL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004346 {
4347 status = RA_FAIL; /* outof memory!*/
4348 break;
4349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 reg_tofreelen = len;
4351 }
4352 STRCPY(reg_tofree, regline);
4353 reginput = reg_tofree
4354 + (reginput - regline);
4355 regline = reg_tofree;
4356 }
4357
4358 /* Get the line to compare with. */
4359 p = reg_getline(clnum);
4360 if (clnum == reg_endpos[no].lnum)
4361 len = reg_endpos[no].col - ccol;
4362 else
4363 len = (int)STRLEN(p + ccol);
4364
4365 if (cstrncmp(p + ccol, reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004366 {
4367 status = RA_NOMATCH; /* doesn't match */
4368 break;
4369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 if (clnum == reg_endpos[no].lnum)
4371 break; /* match and at end! */
4372 if (reglnum == reg_maxline)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004373 {
4374 status = RA_NOMATCH; /* text too short */
4375 break;
4376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377
4378 /* Advance to next line. */
4379 reg_nextline();
4380 ++clnum;
4381 ccol = 0;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004382 if (got_int)
4383 {
4384 status = RA_FAIL;
4385 break;
4386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 }
4388
4389 /* found a match! Note that regline may now point
4390 * to a copy of the line, that should not matter. */
4391 }
4392 }
4393 }
4394
4395 /* Matched the backref, skip over it. */
4396 reginput += len;
4397 }
4398 break;
4399
4400#ifdef FEAT_SYN_HL
4401 case ZREF + 1:
4402 case ZREF + 2:
4403 case ZREF + 3:
4404 case ZREF + 4:
4405 case ZREF + 5:
4406 case ZREF + 6:
4407 case ZREF + 7:
4408 case ZREF + 8:
4409 case ZREF + 9:
4410 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 int len;
4412
4413 cleanup_zsubexpr();
4414 no = op - ZREF;
4415 if (re_extmatch_in != NULL
4416 && re_extmatch_in->matches[no] != NULL)
4417 {
4418 len = (int)STRLEN(re_extmatch_in->matches[no]);
4419 if (cstrncmp(re_extmatch_in->matches[no],
4420 reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004421 status = RA_NOMATCH;
4422 else
4423 reginput += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 }
4425 else
4426 {
4427 /* Backref was not set: Match an empty string. */
4428 }
4429 }
4430 break;
4431#endif
4432
4433 case BRANCH:
4434 {
4435 if (OP(next) != BRANCH) /* No choice. */
4436 next = OPERAND(scan); /* Avoid recursion. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004437 else if (startp != 0 && OP(OPERAND(scan)) == BACKP
4438 && reg_save_equal(STARTP2REGS(startp)))
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004439 /* \+ with something empty before it */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004440 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 else
4442 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004443 rp = regstack_push(&regstack, RS_BRANCH, scan, startp);
4444 if (rp == NULL)
4445 status = RA_FAIL;
4446 else
4447 status = RA_BREAK; /* rest is below */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 }
4449 }
4450 break;
4451
4452 case BRACE_LIMITS:
4453 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 if (OP(next) == BRACE_SIMPLE)
4455 {
4456 bl_minval = OPERAND_MIN(scan);
4457 bl_maxval = OPERAND_MAX(scan);
4458 }
4459 else if (OP(next) >= BRACE_COMPLEX
4460 && OP(next) < BRACE_COMPLEX + 10)
4461 {
4462 no = OP(next) - BRACE_COMPLEX;
4463 brace_min[no] = OPERAND_MIN(scan);
4464 brace_max[no] = OPERAND_MAX(scan);
4465 brace_count[no] = 0;
4466 }
4467 else
4468 {
4469 EMSG(_(e_internal)); /* Shouldn't happen */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004470 status = RA_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 }
4472 }
4473 break;
4474
4475 case BRACE_COMPLEX + 0:
4476 case BRACE_COMPLEX + 1:
4477 case BRACE_COMPLEX + 2:
4478 case BRACE_COMPLEX + 3:
4479 case BRACE_COMPLEX + 4:
4480 case BRACE_COMPLEX + 5:
4481 case BRACE_COMPLEX + 6:
4482 case BRACE_COMPLEX + 7:
4483 case BRACE_COMPLEX + 8:
4484 case BRACE_COMPLEX + 9:
4485 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 no = op - BRACE_COMPLEX;
4487 ++brace_count[no];
4488
4489 /* If not matched enough times yet, try one more */
4490 if (brace_count[no] <= (brace_min[no] <= brace_max[no]
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004491 ? brace_min[no] : brace_max[no]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004493 rp = regstack_push(&regstack, RS_BRCPLX_MORE, scan, startp);
4494 if (rp == NULL)
4495 status = RA_FAIL;
4496 else
4497 {
4498 rp->rs_no = no;
4499 reg_save(&rp->rs_un.regsave);
4500 startp = REGS2STARTP(&rp->rs_un.regsave);
4501 next = OPERAND(scan);
4502 /* We continue and handle the result when done. */
4503 }
4504 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 }
4506
4507 /* If matched enough times, may try matching some more */
4508 if (brace_min[no] <= brace_max[no])
4509 {
4510 /* Range is the normal way around, use longest match */
4511 if (brace_count[no] <= brace_max[no])
4512 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004513 rp = regstack_push(&regstack, RS_BRCPLX_LONG,
4514 scan, startp);
4515 if (rp == NULL)
4516 status = RA_FAIL;
4517 else
4518 {
4519 rp->rs_no = no;
4520 reg_save(&rp->rs_un.regsave);
4521 startp = REGS2STARTP(&rp->rs_un.regsave);
4522 next = OPERAND(scan);
4523 /* We continue and handle the result when done. */
4524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 }
4526 }
4527 else
4528 {
4529 /* Range is backwards, use shortest match first */
4530 if (brace_count[no] <= brace_min[no])
4531 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004532 rp = regstack_push(&regstack, RS_BRCPLX_SHORT,
4533 scan, startp);
4534 if (rp == NULL)
4535 status = RA_FAIL;
4536 else
4537 {
4538 reg_save(&rp->rs_un.regsave);
4539 startp = REGS2STARTP(&rp->rs_un.regsave);
4540 /* We continue and handle the result when done. */
4541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 }
4543 }
4544 }
4545 break;
4546
4547 case BRACE_SIMPLE:
4548 case STAR:
4549 case PLUS:
4550 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004551 regstar_T rst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552
4553 /*
4554 * Lookahead to avoid useless match attempts when we know
4555 * what character comes next.
4556 */
4557 if (OP(next) == EXACTLY)
4558 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004559 rst.nextb = *OPERAND(next);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 if (ireg_ic)
4561 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004562 if (isupper(rst.nextb))
4563 rst.nextb_ic = TOLOWER_LOC(rst.nextb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004565 rst.nextb_ic = TOUPPER_LOC(rst.nextb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 }
4567 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004568 rst.nextb_ic = rst.nextb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 }
4570 else
4571 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004572 rst.nextb = NUL;
4573 rst.nextb_ic = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 }
4575 if (op != BRACE_SIMPLE)
4576 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004577 rst.minval = (op == STAR) ? 0 : 1;
4578 rst.maxval = MAX_LIMIT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 }
4580 else
4581 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004582 rst.minval = bl_minval;
4583 rst.maxval = bl_maxval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 }
4585
4586 /*
4587 * When maxval > minval, try matching as much as possible, up
4588 * to maxval. When maxval < minval, try matching at least the
4589 * minimal number (since the range is backwards, that's also
4590 * maxval!).
4591 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004592 rst.count = regrepeat(OPERAND(scan), rst.maxval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004595 status = RA_FAIL;
4596 break;
4597 }
4598 if (rst.minval <= rst.maxval
4599 ? rst.count >= rst.minval : rst.count >= rst.maxval)
4600 {
4601 /* It could match. Prepare for trying to match what
4602 * follows. The code is below. Parameters are stored in
4603 * a regstar_T on the regstack. */
4604 if (ga_grow(&regstack, sizeof(regstar_T)) == FAIL)
4605 status = RA_FAIL;
4606 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004608 regstack.ga_len += sizeof(regstar_T);
4609 rp = regstack_push(&regstack, rst.minval <= rst.maxval
4610 ? RS_STAR_LONG : RS_STAR_SHORT, scan, startp);
4611 if (rp == NULL)
4612 status = RA_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004614 {
4615 *(((regstar_T *)rp) - 1) = rst;
4616 status = RA_BREAK; /* skip the restore bits */
4617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 }
4619 }
4620 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004621 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 }
4624 break;
4625
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004626 case NOMATCH:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 case MATCH:
4628 case SUBPAT:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004629 rp = regstack_push(&regstack, RS_NOMATCH, scan, startp);
4630 if (rp == NULL)
4631 status = RA_FAIL;
4632 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004634 rp->rs_no = op;
4635 reg_save(&rp->rs_un.regsave);
4636 next = OPERAND(scan);
4637 /* We continue and handle the result when done. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 }
4639 break;
4640
4641 case BEHIND:
4642 case NOBEHIND:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004643 /* Need a bit of room to store extra positions. */
4644 if (ga_grow(&regstack, sizeof(regbehind_T)) == FAIL)
4645 status = RA_FAIL;
4646 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004648 regstack.ga_len += sizeof(regbehind_T);
4649 rp = regstack_push(&regstack, RS_BEHIND1, scan, startp);
4650 if (rp == NULL)
4651 status = RA_FAIL;
4652 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004654 rp->rs_no = op;
4655 reg_save(&rp->rs_un.regsave);
4656 /* First try if what follows matches. If it does then we
4657 * check the behind match by looping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004660 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661
4662 case BHPOS:
4663 if (REG_MULTI)
4664 {
4665 if (behind_pos.rs_u.pos.col != (colnr_T)(reginput - regline)
4666 || behind_pos.rs_u.pos.lnum != reglnum)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004667 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 }
4669 else if (behind_pos.rs_u.ptr != reginput)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004670 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 break;
4672
4673 case NEWL:
4674 if ((c != NUL || reglnum == reg_maxline)
4675 && (c != '\n' || !reg_line_lbr))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004676 status = RA_NOMATCH;
4677 else if (reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 ADVANCE_REGINPUT();
4679 else
4680 reg_nextline();
4681 break;
4682
4683 case END:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004684 status = RA_MATCH; /* Success! */
4685 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686
4687 default:
4688 EMSG(_(e_re_corr));
4689#ifdef DEBUG
4690 printf("Illegal op code %d\n", op);
4691#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004692 status = RA_FAIL;
4693 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 }
4695 }
4696
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004697 /* If we can't continue sequentially, break the inner loop. */
4698 if (status != RA_CONT)
4699 break;
4700
4701 /* Continue in inner loop, advance to next item. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 scan = next;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004703
4704 } /* end of inner loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705
4706 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004707 * If there is something on the regstack execute the code for the state.
4708 * If the state is popped then loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004710 while (regstack.ga_len > 0 && status != RA_FAIL)
4711 {
4712 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len) - 1;
4713 switch (rp->rs_state)
4714 {
4715 case RS_NOPEN:
4716 /* Result is passed on as-is, simply pop the state. */
4717 regstack_pop(&regstack, &scan, &startp);
4718 break;
4719
4720 case RS_MOPEN:
4721 /* Pop the state. Restore pointers when there is no match. */
4722 if (status == RA_NOMATCH)
4723 restore_se(&rp->rs_un.sesave, &reg_startpos[rp->rs_no],
4724 &reg_startp[rp->rs_no]);
4725 regstack_pop(&regstack, &scan, &startp);
4726 break;
4727
4728#ifdef FEAT_SYN_HL
4729 case RS_ZOPEN:
4730 /* Pop the state. Restore pointers when there is no match. */
4731 if (status == RA_NOMATCH)
4732 restore_se(&rp->rs_un.sesave, &reg_startzpos[rp->rs_no],
4733 &reg_startzp[rp->rs_no]);
4734 regstack_pop(&regstack, &scan, &startp);
4735 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004737
4738 case RS_MCLOSE:
4739 /* Pop the state. Restore pointers when there is no match. */
4740 if (status == RA_NOMATCH)
4741 restore_se(&rp->rs_un.sesave, &reg_endpos[rp->rs_no],
4742 &reg_endp[rp->rs_no]);
4743 regstack_pop(&regstack, &scan, &startp);
4744 break;
4745
4746#ifdef FEAT_SYN_HL
4747 case RS_ZCLOSE:
4748 /* Pop the state. Restore pointers when there is no match. */
4749 if (status == RA_NOMATCH)
4750 restore_se(&rp->rs_un.sesave, &reg_endzpos[rp->rs_no],
4751 &reg_endzp[rp->rs_no]);
4752 regstack_pop(&regstack, &scan, &startp);
4753 break;
4754#endif
4755
4756 case RS_BRANCH:
4757 if (status == RA_MATCH)
4758 /* this branch matched, use it */
4759 regstack_pop(&regstack, &scan, &startp);
4760 else
4761 {
4762 if (status != RA_BREAK)
4763 {
4764 /* After a non-matching branch: try next one. */
4765 reg_restore(&rp->rs_un.regsave);
4766 scan = rp->rs_scan;
4767 }
4768 if (scan == NULL || OP(scan) != BRANCH)
4769 {
4770 /* no more branches, didn't find a match */
4771 status = RA_NOMATCH;
4772 regstack_pop(&regstack, &scan, &startp);
4773 }
4774 else
4775 {
4776 /* Prepare to try a branch. */
4777 rp->rs_scan = regnext(scan);
4778 reg_save(&rp->rs_un.regsave);
4779 startp = REGS2STARTP(&rp->rs_un.regsave);
4780 scan = OPERAND(scan);
4781 }
4782 }
4783 break;
4784
4785 case RS_BRCPLX_MORE:
4786 /* Pop the state. Restore pointers when there is no match. */
4787 if (status == RA_NOMATCH)
4788 {
4789 reg_restore(&rp->rs_un.regsave);
4790 --brace_count[rp->rs_no]; /* decrement match count */
4791 }
4792 regstack_pop(&regstack, &scan, &startp);
4793 break;
4794
4795 case RS_BRCPLX_LONG:
4796 /* Pop the state. Restore pointers when there is no match. */
4797 if (status == RA_NOMATCH)
4798 {
4799 /* There was no match, but we did find enough matches. */
4800 reg_restore(&rp->rs_un.regsave);
4801 --brace_count[rp->rs_no];
4802 /* continue with the items after "\{}" */
4803 status = RA_CONT;
4804 }
4805 regstack_pop(&regstack, &scan, &startp);
4806 if (status == RA_CONT)
4807 scan = regnext(scan);
4808 break;
4809
4810 case RS_BRCPLX_SHORT:
4811 /* Pop the state. Restore pointers when there is no match. */
4812 if (status == RA_NOMATCH)
4813 /* There was no match, try to match one more item. */
4814 reg_restore(&rp->rs_un.regsave);
4815 regstack_pop(&regstack, &scan, &startp);
4816 if (status == RA_NOMATCH)
4817 {
4818 scan = OPERAND(scan);
4819 status = RA_CONT;
4820 }
4821 break;
4822
4823 case RS_NOMATCH:
4824 /* Pop the state. If the operand matches for NOMATCH or
4825 * doesn't match for MATCH/SUBPAT, we fail. Otherwise backup,
4826 * except for SUBPAT, and continue with the next item. */
4827 if (status == (rp->rs_no == NOMATCH ? RA_MATCH : RA_NOMATCH))
4828 status = RA_NOMATCH;
4829 else
4830 {
4831 status = RA_CONT;
4832 if (rp->rs_no != SUBPAT)
4833 reg_restore(&rp->rs_un.regsave); /* zero-width */
4834 }
4835 regstack_pop(&regstack, &scan, &startp);
4836 if (status == RA_CONT)
4837 scan = regnext(scan);
4838 break;
4839
4840 case RS_BEHIND1:
4841 if (status == RA_NOMATCH)
4842 {
4843 regstack_pop(&regstack, &scan, &startp);
4844 regstack.ga_len -= sizeof(regbehind_T);
4845 }
4846 else
4847 {
4848 /* The stuff after BEHIND/NOBEHIND matches. Now try if
4849 * the behind part does (not) match before the current
4850 * position in the input. This must be done at every
4851 * position in the input and checking if the match ends at
4852 * the current position. */
4853
4854 /* save the position after the found match for next */
4855 reg_save(&(((regbehind_T *)rp) - 1)->save_after);
4856
4857 /* start looking for a match with operand at the current
4858 * postion. Go back one character until we find the
4859 * result, hitting the start of the line or the previous
4860 * line (for multi-line matching).
4861 * Set behind_pos to where the match should end, BHPOS
4862 * will match it. Save the current value. */
4863 (((regbehind_T *)rp) - 1)->save_behind = behind_pos;
4864 behind_pos = rp->rs_un.regsave;
4865
4866 rp->rs_state = RS_BEHIND2;
4867
4868 reg_restore(&rp->rs_un.regsave);
4869 scan = OPERAND(rp->rs_scan);
4870 }
4871 break;
4872
4873 case RS_BEHIND2:
4874 /*
4875 * Looping for BEHIND / NOBEHIND match.
4876 */
4877 if (status == RA_MATCH && reg_save_equal(&behind_pos))
4878 {
4879 /* found a match that ends where "next" started */
4880 behind_pos = (((regbehind_T *)rp) - 1)->save_behind;
4881 if (rp->rs_no == BEHIND)
4882 reg_restore(&(((regbehind_T *)rp) - 1)->save_after);
4883 else
4884 /* But we didn't want a match. */
4885 status = RA_NOMATCH;
4886 regstack_pop(&regstack, &scan, &startp);
4887 regstack.ga_len -= sizeof(regbehind_T);
4888 }
4889 else
4890 {
4891 /* No match: Go back one character. May go to previous
4892 * line once. */
4893 no = OK;
4894 if (REG_MULTI)
4895 {
4896 if (rp->rs_un.regsave.rs_u.pos.col == 0)
4897 {
4898 if (rp->rs_un.regsave.rs_u.pos.lnum
4899 < behind_pos.rs_u.pos.lnum
4900 || reg_getline(
4901 --rp->rs_un.regsave.rs_u.pos.lnum)
4902 == NULL)
4903 no = FAIL;
4904 else
4905 {
4906 reg_restore(&rp->rs_un.regsave);
4907 rp->rs_un.regsave.rs_u.pos.col =
4908 (colnr_T)STRLEN(regline);
4909 }
4910 }
4911 else
4912 --rp->rs_un.regsave.rs_u.pos.col;
4913 }
4914 else
4915 {
4916 if (rp->rs_un.regsave.rs_u.ptr == regline)
4917 no = FAIL;
4918 else
4919 --rp->rs_un.regsave.rs_u.ptr;
4920 }
4921 if (no == OK)
4922 {
4923 /* Advanced, prepare for finding match again. */
4924 reg_restore(&rp->rs_un.regsave);
4925 scan = OPERAND(rp->rs_scan);
4926 }
4927 else
4928 {
4929 /* Can't advance. For NOBEHIND that's a match. */
4930 behind_pos = (((regbehind_T *)rp) - 1)->save_behind;
4931 if (rp->rs_no == NOBEHIND)
4932 {
4933 reg_restore(&(((regbehind_T *)rp) - 1)->save_after);
4934 status = RA_MATCH;
4935 }
4936 else
4937 status = RA_NOMATCH;
4938 regstack_pop(&regstack, &scan, &startp);
4939 regstack.ga_len -= sizeof(regbehind_T);
4940 }
4941 }
4942 break;
4943
4944 case RS_STAR_LONG:
4945 case RS_STAR_SHORT:
4946 {
4947 regstar_T *rst = ((regstar_T *)rp) - 1;
4948
4949 if (status == RA_MATCH)
4950 {
4951 regstack_pop(&regstack, &scan, &startp);
4952 regstack.ga_len -= sizeof(regstar_T);
4953 break;
4954 }
4955
4956 /* Tried once already, restore input pointers. */
4957 if (status != RA_BREAK)
4958 reg_restore(&rp->rs_un.regsave);
4959
4960 /* Repeat until we found a position where it could match. */
4961 for (;;)
4962 {
4963 if (status != RA_BREAK)
4964 {
4965 /* Tried first position already, advance. */
4966 if (rp->rs_state == RS_STAR_LONG)
4967 {
4968 /* Trying for longest matc, but couldn't or didn't
4969 * match -- back up one char. */
4970 if (--rst->count < rst->minval)
4971 break;
4972 if (reginput == regline)
4973 {
4974 /* backup to last char of previous line */
4975 --reglnum;
4976 regline = reg_getline(reglnum);
4977 /* Just in case regrepeat() didn't count
4978 * right. */
4979 if (regline == NULL)
4980 break;
4981 reginput = regline + STRLEN(regline);
4982 fast_breakcheck();
4983 }
4984 else
4985 mb_ptr_back(regline, reginput);
4986 }
4987 else
4988 {
4989 /* Range is backwards, use shortest match first.
4990 * Careful: maxval and minval are exchanged!
4991 * Couldn't or didn't match: try advancing one
4992 * char. */
4993 if (rst->count == rst->minval
4994 || regrepeat(OPERAND(rp->rs_scan), 1L) == 0)
4995 break;
4996 ++rst->count;
4997 }
4998 if (got_int)
4999 break;
5000 }
5001 else
5002 status = RA_NOMATCH;
5003
5004 /* If it could match, try it. */
5005 if (rst->nextb == NUL || *reginput == rst->nextb
5006 || *reginput == rst->nextb_ic)
5007 {
5008 reg_save(&rp->rs_un.regsave);
5009 scan = regnext(rp->rs_scan);
5010 status = RA_CONT;
5011 break;
5012 }
5013 }
5014 if (status != RA_CONT)
5015 {
5016 /* Failed. */
5017 regstack_pop(&regstack, &scan, &startp);
5018 regstack.ga_len -= sizeof(regstar_T);
5019 status = RA_NOMATCH;
5020 }
5021 }
5022 break;
5023 }
5024
5025 /* If we want to continue the inner loop or didn't pop a state contine
5026 * matching loop */
5027 if (status == RA_CONT || rp == (regitem_T *)
5028 ((char *)regstack.ga_data + regstack.ga_len) - 1)
5029 break;
5030 }
5031
5032 /* May want to continue with the inner loop. */
5033 if (status == RA_CONT)
5034 continue;
5035
5036 /*
5037 * If the regstack is empty or something failed we are done.
5038 */
5039 if (regstack.ga_len == 0 || status == RA_FAIL)
5040 {
5041 ga_clear(&regstack);
5042 if (scan == NULL)
5043 {
5044 /*
5045 * We get here only if there's trouble -- normally "case END" is
5046 * the terminating point.
5047 */
5048 EMSG(_(e_re_corr));
5049#ifdef DEBUG
5050 printf("Premature EOL\n");
5051#endif
5052 }
5053 return (status == RA_MATCH);
5054 }
5055
5056 } /* End of loop until the regstack is empty. */
5057
5058 /* NOTREACHED */
5059}
5060
5061/*
5062 * Push an item onto the regstack.
5063 * Returns pointer to new item. Returns NULL when out of memory.
5064 */
5065 static regitem_T *
5066regstack_push(regstack, state, scan, startp)
5067 garray_T *regstack;
5068 regstate_T state;
5069 char_u *scan;
5070 long startp;
5071{
5072 regitem_T *rp;
5073
5074 if (ga_grow(regstack, sizeof(regitem_T)) == FAIL)
5075 return NULL;
5076
5077 rp = (regitem_T *)((char *)regstack->ga_data + regstack->ga_len);
5078 rp->rs_state = state;
5079 rp->rs_scan = scan;
5080 rp->rs_startp = startp;
5081
5082 regstack->ga_len += sizeof(regitem_T);
5083 return rp;
5084}
5085
5086/*
5087 * Pop an item from the regstack.
5088 */
5089 static void
5090regstack_pop(regstack, scan, startp)
5091 garray_T *regstack;
5092 char_u **scan;
5093 long *startp;
5094{
5095 regitem_T *rp;
5096
5097 rp = (regitem_T *)((char *)regstack->ga_data + regstack->ga_len) - 1;
5098 *scan = rp->rs_scan;
5099 *startp = rp->rs_startp;
5100
5101 regstack->ga_len -= sizeof(regitem_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102}
5103
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104/*
5105 * regrepeat - repeatedly match something simple, return how many.
5106 * Advances reginput (and reglnum) to just after the matched chars.
5107 */
5108 static int
5109regrepeat(p, maxcount)
5110 char_u *p;
5111 long maxcount; /* maximum number of matches allowed */
5112{
5113 long count = 0;
5114 char_u *scan;
5115 char_u *opnd;
5116 int mask;
5117 int testval = 0;
5118
5119 scan = reginput; /* Make local copy of reginput for speed. */
5120 opnd = OPERAND(p);
5121 switch (OP(p))
5122 {
5123 case ANY:
5124 case ANY + ADD_NL:
5125 while (count < maxcount)
5126 {
5127 /* Matching anything means we continue until end-of-line (or
5128 * end-of-file for ANY + ADD_NL), only limited by maxcount. */
5129 while (*scan != NUL && count < maxcount)
5130 {
5131 ++count;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005132 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 }
5134 if (!WITH_NL(OP(p)) || reglnum == reg_maxline || count == maxcount)
5135 break;
5136 ++count; /* count the line-break */
5137 reg_nextline();
5138 scan = reginput;
5139 if (got_int)
5140 break;
5141 }
5142 break;
5143
5144 case IDENT:
5145 case IDENT + ADD_NL:
5146 testval = TRUE;
5147 /*FALLTHROUGH*/
5148 case SIDENT:
5149 case SIDENT + ADD_NL:
5150 while (count < maxcount)
5151 {
5152 if (vim_isIDc(*scan) && (testval || !VIM_ISDIGIT(*scan)))
5153 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005154 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 }
5156 else if (*scan == NUL)
5157 {
5158 if (!WITH_NL(OP(p)) || reglnum == reg_maxline)
5159 break;
5160 reg_nextline();
5161 scan = reginput;
5162 if (got_int)
5163 break;
5164 }
5165 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5166 ++scan;
5167 else
5168 break;
5169 ++count;
5170 }
5171 break;
5172
5173 case KWORD:
5174 case KWORD + ADD_NL:
5175 testval = TRUE;
5176 /*FALLTHROUGH*/
5177 case SKWORD:
5178 case SKWORD + ADD_NL:
5179 while (count < maxcount)
5180 {
5181 if (vim_iswordp(scan) && (testval || !VIM_ISDIGIT(*scan)))
5182 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005183 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 }
5185 else if (*scan == NUL)
5186 {
5187 if (!WITH_NL(OP(p)) || reglnum == reg_maxline)
5188 break;
5189 reg_nextline();
5190 scan = reginput;
5191 if (got_int)
5192 break;
5193 }
5194 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5195 ++scan;
5196 else
5197 break;
5198 ++count;
5199 }
5200 break;
5201
5202 case FNAME:
5203 case FNAME + ADD_NL:
5204 testval = TRUE;
5205 /*FALLTHROUGH*/
5206 case SFNAME:
5207 case SFNAME + ADD_NL:
5208 while (count < maxcount)
5209 {
5210 if (vim_isfilec(*scan) && (testval || !VIM_ISDIGIT(*scan)))
5211 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005212 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 }
5214 else if (*scan == NUL)
5215 {
5216 if (!WITH_NL(OP(p)) || reglnum == reg_maxline)
5217 break;
5218 reg_nextline();
5219 scan = reginput;
5220 if (got_int)
5221 break;
5222 }
5223 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5224 ++scan;
5225 else
5226 break;
5227 ++count;
5228 }
5229 break;
5230
5231 case PRINT:
5232 case PRINT + ADD_NL:
5233 testval = TRUE;
5234 /*FALLTHROUGH*/
5235 case SPRINT:
5236 case SPRINT + ADD_NL:
5237 while (count < maxcount)
5238 {
5239 if (*scan == NUL)
5240 {
5241 if (!WITH_NL(OP(p)) || reglnum == reg_maxline)
5242 break;
5243 reg_nextline();
5244 scan = reginput;
5245 if (got_int)
5246 break;
5247 }
5248 else if (ptr2cells(scan) == 1 && (testval || !VIM_ISDIGIT(*scan)))
5249 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005250 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 }
5252 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5253 ++scan;
5254 else
5255 break;
5256 ++count;
5257 }
5258 break;
5259
5260 case WHITE:
5261 case WHITE + ADD_NL:
5262 testval = mask = RI_WHITE;
5263do_class:
5264 while (count < maxcount)
5265 {
5266#ifdef FEAT_MBYTE
5267 int l;
5268#endif
5269 if (*scan == NUL)
5270 {
5271 if (!WITH_NL(OP(p)) || reglnum == reg_maxline)
5272 break;
5273 reg_nextline();
5274 scan = reginput;
5275 if (got_int)
5276 break;
5277 }
5278#ifdef FEAT_MBYTE
5279 else if (has_mbyte && (l = (*mb_ptr2len_check)(scan)) > 1)
5280 {
5281 if (testval != 0)
5282 break;
5283 scan += l;
5284 }
5285#endif
5286 else if ((class_tab[*scan] & mask) == testval)
5287 ++scan;
5288 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5289 ++scan;
5290 else
5291 break;
5292 ++count;
5293 }
5294 break;
5295
5296 case NWHITE:
5297 case NWHITE + ADD_NL:
5298 mask = RI_WHITE;
5299 goto do_class;
5300 case DIGIT:
5301 case DIGIT + ADD_NL:
5302 testval = mask = RI_DIGIT;
5303 goto do_class;
5304 case NDIGIT:
5305 case NDIGIT + ADD_NL:
5306 mask = RI_DIGIT;
5307 goto do_class;
5308 case HEX:
5309 case HEX + ADD_NL:
5310 testval = mask = RI_HEX;
5311 goto do_class;
5312 case NHEX:
5313 case NHEX + ADD_NL:
5314 mask = RI_HEX;
5315 goto do_class;
5316 case OCTAL:
5317 case OCTAL + ADD_NL:
5318 testval = mask = RI_OCTAL;
5319 goto do_class;
5320 case NOCTAL:
5321 case NOCTAL + ADD_NL:
5322 mask = RI_OCTAL;
5323 goto do_class;
5324 case WORD:
5325 case WORD + ADD_NL:
5326 testval = mask = RI_WORD;
5327 goto do_class;
5328 case NWORD:
5329 case NWORD + ADD_NL:
5330 mask = RI_WORD;
5331 goto do_class;
5332 case HEAD:
5333 case HEAD + ADD_NL:
5334 testval = mask = RI_HEAD;
5335 goto do_class;
5336 case NHEAD:
5337 case NHEAD + ADD_NL:
5338 mask = RI_HEAD;
5339 goto do_class;
5340 case ALPHA:
5341 case ALPHA + ADD_NL:
5342 testval = mask = RI_ALPHA;
5343 goto do_class;
5344 case NALPHA:
5345 case NALPHA + ADD_NL:
5346 mask = RI_ALPHA;
5347 goto do_class;
5348 case LOWER:
5349 case LOWER + ADD_NL:
5350 testval = mask = RI_LOWER;
5351 goto do_class;
5352 case NLOWER:
5353 case NLOWER + ADD_NL:
5354 mask = RI_LOWER;
5355 goto do_class;
5356 case UPPER:
5357 case UPPER + ADD_NL:
5358 testval = mask = RI_UPPER;
5359 goto do_class;
5360 case NUPPER:
5361 case NUPPER + ADD_NL:
5362 mask = RI_UPPER;
5363 goto do_class;
5364
5365 case EXACTLY:
5366 {
5367 int cu, cl;
5368
5369 /* This doesn't do a multi-byte character, because a MULTIBYTECODE
5370 * would have been used for it. */
5371 if (ireg_ic)
5372 {
5373 cu = TOUPPER_LOC(*opnd);
5374 cl = TOLOWER_LOC(*opnd);
5375 while (count < maxcount && (*scan == cu || *scan == cl))
5376 {
5377 count++;
5378 scan++;
5379 }
5380 }
5381 else
5382 {
5383 cu = *opnd;
5384 while (count < maxcount && *scan == cu)
5385 {
5386 count++;
5387 scan++;
5388 }
5389 }
5390 break;
5391 }
5392
5393#ifdef FEAT_MBYTE
5394 case MULTIBYTECODE:
5395 {
5396 int i, len, cf = 0;
5397
5398 /* Safety check (just in case 'encoding' was changed since
5399 * compiling the program). */
5400 if ((len = (*mb_ptr2len_check)(opnd)) > 1)
5401 {
5402 if (ireg_ic && enc_utf8)
5403 cf = utf_fold(utf_ptr2char(opnd));
5404 while (count < maxcount)
5405 {
5406 for (i = 0; i < len; ++i)
5407 if (opnd[i] != scan[i])
5408 break;
5409 if (i < len && (!ireg_ic || !enc_utf8
5410 || utf_fold(utf_ptr2char(scan)) != cf))
5411 break;
5412 scan += len;
5413 ++count;
5414 }
5415 }
5416 }
5417 break;
5418#endif
5419
5420 case ANYOF:
5421 case ANYOF + ADD_NL:
5422 testval = TRUE;
5423 /*FALLTHROUGH*/
5424
5425 case ANYBUT:
5426 case ANYBUT + ADD_NL:
5427 while (count < maxcount)
5428 {
5429#ifdef FEAT_MBYTE
5430 int len;
5431#endif
5432 if (*scan == NUL)
5433 {
5434 if (!WITH_NL(OP(p)) || reglnum == reg_maxline)
5435 break;
5436 reg_nextline();
5437 scan = reginput;
5438 if (got_int)
5439 break;
5440 }
5441 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5442 ++scan;
5443#ifdef FEAT_MBYTE
5444 else if (has_mbyte && (len = (*mb_ptr2len_check)(scan)) > 1)
5445 {
5446 if ((cstrchr(opnd, (*mb_ptr2char)(scan)) == NULL) == testval)
5447 break;
5448 scan += len;
5449 }
5450#endif
5451 else
5452 {
5453 if ((cstrchr(opnd, *scan) == NULL) == testval)
5454 break;
5455 ++scan;
5456 }
5457 ++count;
5458 }
5459 break;
5460
5461 case NEWL:
5462 while (count < maxcount
5463 && ((*scan == NUL && reglnum < reg_maxline)
5464 || (*scan == '\n' && reg_line_lbr)))
5465 {
5466 count++;
5467 if (reg_line_lbr)
5468 ADVANCE_REGINPUT();
5469 else
5470 reg_nextline();
5471 scan = reginput;
5472 if (got_int)
5473 break;
5474 }
5475 break;
5476
5477 default: /* Oh dear. Called inappropriately. */
5478 EMSG(_(e_re_corr));
5479#ifdef DEBUG
5480 printf("Called regrepeat with op code %d\n", OP(p));
5481#endif
5482 break;
5483 }
5484
5485 reginput = scan;
5486
5487 return (int)count;
5488}
5489
5490/*
5491 * regnext - dig the "next" pointer out of a node
5492 */
5493 static char_u *
5494regnext(p)
5495 char_u *p;
5496{
5497 int offset;
5498
5499 if (p == JUST_CALC_SIZE)
5500 return NULL;
5501
5502 offset = NEXT(p);
5503 if (offset == 0)
5504 return NULL;
5505
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005506 if (OP(p) == BACK || OP(p) == BACKP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 return p - offset;
5508 else
5509 return p + offset;
5510}
5511
5512/*
5513 * Check the regexp program for its magic number.
5514 * Return TRUE if it's wrong.
5515 */
5516 static int
5517prog_magic_wrong()
5518{
5519 if (UCHARAT(REG_MULTI
5520 ? reg_mmatch->regprog->program
5521 : reg_match->regprog->program) != REGMAGIC)
5522 {
5523 EMSG(_(e_re_corr));
5524 return TRUE;
5525 }
5526 return FALSE;
5527}
5528
5529/*
5530 * Cleanup the subexpressions, if this wasn't done yet.
5531 * This construction is used to clear the subexpressions only when they are
5532 * used (to increase speed).
5533 */
5534 static void
5535cleanup_subexpr()
5536{
5537 if (need_clear_subexpr)
5538 {
5539 if (REG_MULTI)
5540 {
5541 /* Use 0xff to set lnum to -1 */
5542 vim_memset(reg_startpos, 0xff, sizeof(lpos_T) * NSUBEXP);
5543 vim_memset(reg_endpos, 0xff, sizeof(lpos_T) * NSUBEXP);
5544 }
5545 else
5546 {
5547 vim_memset(reg_startp, 0, sizeof(char_u *) * NSUBEXP);
5548 vim_memset(reg_endp, 0, sizeof(char_u *) * NSUBEXP);
5549 }
5550 need_clear_subexpr = FALSE;
5551 }
5552}
5553
5554#ifdef FEAT_SYN_HL
5555 static void
5556cleanup_zsubexpr()
5557{
5558 if (need_clear_zsubexpr)
5559 {
5560 if (REG_MULTI)
5561 {
5562 /* Use 0xff to set lnum to -1 */
5563 vim_memset(reg_startzpos, 0xff, sizeof(lpos_T) * NSUBEXP);
5564 vim_memset(reg_endzpos, 0xff, sizeof(lpos_T) * NSUBEXP);
5565 }
5566 else
5567 {
5568 vim_memset(reg_startzp, 0, sizeof(char_u *) * NSUBEXP);
5569 vim_memset(reg_endzp, 0, sizeof(char_u *) * NSUBEXP);
5570 }
5571 need_clear_zsubexpr = FALSE;
5572 }
5573}
5574#endif
5575
5576/*
5577 * Advance reglnum, regline and reginput to the next line.
5578 */
5579 static void
5580reg_nextline()
5581{
5582 regline = reg_getline(++reglnum);
5583 reginput = regline;
5584 fast_breakcheck();
5585}
5586
5587/*
5588 * Save the input line and position in a regsave_T.
5589 */
5590 static void
5591reg_save(save)
5592 regsave_T *save;
5593{
5594 if (REG_MULTI)
5595 {
5596 save->rs_u.pos.col = (colnr_T)(reginput - regline);
5597 save->rs_u.pos.lnum = reglnum;
5598 }
5599 else
5600 save->rs_u.ptr = reginput;
5601}
5602
5603/*
5604 * Restore the input line and position from a regsave_T.
5605 */
5606 static void
5607reg_restore(save)
5608 regsave_T *save;
5609{
5610 if (REG_MULTI)
5611 {
5612 if (reglnum != save->rs_u.pos.lnum)
5613 {
5614 /* only call reg_getline() when the line number changed to save
5615 * a bit of time */
5616 reglnum = save->rs_u.pos.lnum;
5617 regline = reg_getline(reglnum);
5618 }
5619 reginput = regline + save->rs_u.pos.col;
5620 }
5621 else
5622 reginput = save->rs_u.ptr;
5623}
5624
5625/*
5626 * Return TRUE if current position is equal to saved position.
5627 */
5628 static int
5629reg_save_equal(save)
5630 regsave_T *save;
5631{
5632 if (REG_MULTI)
5633 return reglnum == save->rs_u.pos.lnum
5634 && reginput == regline + save->rs_u.pos.col;
5635 return reginput == save->rs_u.ptr;
5636}
5637
5638/*
5639 * Tentatively set the sub-expression start to the current position (after
5640 * calling regmatch() they will have changed). Need to save the existing
5641 * values for when there is no match.
5642 * Use se_save() to use pointer (save_se_multi()) or position (save_se_one()),
5643 * depending on REG_MULTI.
5644 */
5645 static void
5646save_se_multi(savep, posp)
5647 save_se_T *savep;
5648 lpos_T *posp;
5649{
5650 savep->se_u.pos = *posp;
5651 posp->lnum = reglnum;
5652 posp->col = (colnr_T)(reginput - regline);
5653}
5654
5655 static void
5656save_se_one(savep, pp)
5657 save_se_T *savep;
5658 char_u **pp;
5659{
5660 savep->se_u.ptr = *pp;
5661 *pp = reginput;
5662}
5663
5664/*
5665 * Compare a number with the operand of RE_LNUM, RE_COL or RE_VCOL.
5666 */
5667 static int
5668re_num_cmp(val, scan)
5669 long_u val;
5670 char_u *scan;
5671{
5672 long_u n = OPERAND_MIN(scan);
5673
5674 if (OPERAND_CMP(scan) == '>')
5675 return val > n;
5676 if (OPERAND_CMP(scan) == '<')
5677 return val < n;
5678 return val == n;
5679}
5680
5681
5682#ifdef DEBUG
5683
5684/*
5685 * regdump - dump a regexp onto stdout in vaguely comprehensible form
5686 */
5687 static void
5688regdump(pattern, r)
5689 char_u *pattern;
5690 regprog_T *r;
5691{
5692 char_u *s;
5693 int op = EXACTLY; /* Arbitrary non-END op. */
5694 char_u *next;
5695 char_u *end = NULL;
5696
5697 printf("\r\nregcomp(%s):\r\n", pattern);
5698
5699 s = r->program + 1;
5700 /*
5701 * Loop until we find the END that isn't before a referred next (an END
5702 * can also appear in a NOMATCH operand).
5703 */
5704 while (op != END || s <= end)
5705 {
5706 op = OP(s);
5707 printf("%2d%s", (int)(s - r->program), regprop(s)); /* Where, what. */
5708 next = regnext(s);
5709 if (next == NULL) /* Next ptr. */
5710 printf("(0)");
5711 else
5712 printf("(%d)", (int)((s - r->program) + (next - s)));
5713 if (end < next)
5714 end = next;
5715 if (op == BRACE_LIMITS)
5716 {
5717 /* Two short ints */
5718 printf(" minval %ld, maxval %ld", OPERAND_MIN(s), OPERAND_MAX(s));
5719 s += 8;
5720 }
5721 s += 3;
5722 if (op == ANYOF || op == ANYOF + ADD_NL
5723 || op == ANYBUT || op == ANYBUT + ADD_NL
5724 || op == EXACTLY)
5725 {
5726 /* Literal string, where present. */
5727 while (*s != NUL)
5728 printf("%c", *s++);
5729 s++;
5730 }
5731 printf("\r\n");
5732 }
5733
5734 /* Header fields of interest. */
5735 if (r->regstart != NUL)
5736 printf("start `%s' 0x%x; ", r->regstart < 256
5737 ? (char *)transchar(r->regstart)
5738 : "multibyte", r->regstart);
5739 if (r->reganch)
5740 printf("anchored; ");
5741 if (r->regmust != NULL)
5742 printf("must have \"%s\"", r->regmust);
5743 printf("\r\n");
5744}
5745
5746/*
5747 * regprop - printable representation of opcode
5748 */
5749 static char_u *
5750regprop(op)
5751 char_u *op;
5752{
5753 char_u *p;
5754 static char_u buf[50];
5755
5756 (void) strcpy(buf, ":");
5757
5758 switch (OP(op))
5759 {
5760 case BOL:
5761 p = "BOL";
5762 break;
5763 case EOL:
5764 p = "EOL";
5765 break;
5766 case RE_BOF:
5767 p = "BOF";
5768 break;
5769 case RE_EOF:
5770 p = "EOF";
5771 break;
5772 case CURSOR:
5773 p = "CURSOR";
5774 break;
5775 case RE_LNUM:
5776 p = "RE_LNUM";
5777 break;
5778 case RE_COL:
5779 p = "RE_COL";
5780 break;
5781 case RE_VCOL:
5782 p = "RE_VCOL";
5783 break;
5784 case BOW:
5785 p = "BOW";
5786 break;
5787 case EOW:
5788 p = "EOW";
5789 break;
5790 case ANY:
5791 p = "ANY";
5792 break;
5793 case ANY + ADD_NL:
5794 p = "ANY+NL";
5795 break;
5796 case ANYOF:
5797 p = "ANYOF";
5798 break;
5799 case ANYOF + ADD_NL:
5800 p = "ANYOF+NL";
5801 break;
5802 case ANYBUT:
5803 p = "ANYBUT";
5804 break;
5805 case ANYBUT + ADD_NL:
5806 p = "ANYBUT+NL";
5807 break;
5808 case IDENT:
5809 p = "IDENT";
5810 break;
5811 case IDENT + ADD_NL:
5812 p = "IDENT+NL";
5813 break;
5814 case SIDENT:
5815 p = "SIDENT";
5816 break;
5817 case SIDENT + ADD_NL:
5818 p = "SIDENT+NL";
5819 break;
5820 case KWORD:
5821 p = "KWORD";
5822 break;
5823 case KWORD + ADD_NL:
5824 p = "KWORD+NL";
5825 break;
5826 case SKWORD:
5827 p = "SKWORD";
5828 break;
5829 case SKWORD + ADD_NL:
5830 p = "SKWORD+NL";
5831 break;
5832 case FNAME:
5833 p = "FNAME";
5834 break;
5835 case FNAME + ADD_NL:
5836 p = "FNAME+NL";
5837 break;
5838 case SFNAME:
5839 p = "SFNAME";
5840 break;
5841 case SFNAME + ADD_NL:
5842 p = "SFNAME+NL";
5843 break;
5844 case PRINT:
5845 p = "PRINT";
5846 break;
5847 case PRINT + ADD_NL:
5848 p = "PRINT+NL";
5849 break;
5850 case SPRINT:
5851 p = "SPRINT";
5852 break;
5853 case SPRINT + ADD_NL:
5854 p = "SPRINT+NL";
5855 break;
5856 case WHITE:
5857 p = "WHITE";
5858 break;
5859 case WHITE + ADD_NL:
5860 p = "WHITE+NL";
5861 break;
5862 case NWHITE:
5863 p = "NWHITE";
5864 break;
5865 case NWHITE + ADD_NL:
5866 p = "NWHITE+NL";
5867 break;
5868 case DIGIT:
5869 p = "DIGIT";
5870 break;
5871 case DIGIT + ADD_NL:
5872 p = "DIGIT+NL";
5873 break;
5874 case NDIGIT:
5875 p = "NDIGIT";
5876 break;
5877 case NDIGIT + ADD_NL:
5878 p = "NDIGIT+NL";
5879 break;
5880 case HEX:
5881 p = "HEX";
5882 break;
5883 case HEX + ADD_NL:
5884 p = "HEX+NL";
5885 break;
5886 case NHEX:
5887 p = "NHEX";
5888 break;
5889 case NHEX + ADD_NL:
5890 p = "NHEX+NL";
5891 break;
5892 case OCTAL:
5893 p = "OCTAL";
5894 break;
5895 case OCTAL + ADD_NL:
5896 p = "OCTAL+NL";
5897 break;
5898 case NOCTAL:
5899 p = "NOCTAL";
5900 break;
5901 case NOCTAL + ADD_NL:
5902 p = "NOCTAL+NL";
5903 break;
5904 case WORD:
5905 p = "WORD";
5906 break;
5907 case WORD + ADD_NL:
5908 p = "WORD+NL";
5909 break;
5910 case NWORD:
5911 p = "NWORD";
5912 break;
5913 case NWORD + ADD_NL:
5914 p = "NWORD+NL";
5915 break;
5916 case HEAD:
5917 p = "HEAD";
5918 break;
5919 case HEAD + ADD_NL:
5920 p = "HEAD+NL";
5921 break;
5922 case NHEAD:
5923 p = "NHEAD";
5924 break;
5925 case NHEAD + ADD_NL:
5926 p = "NHEAD+NL";
5927 break;
5928 case ALPHA:
5929 p = "ALPHA";
5930 break;
5931 case ALPHA + ADD_NL:
5932 p = "ALPHA+NL";
5933 break;
5934 case NALPHA:
5935 p = "NALPHA";
5936 break;
5937 case NALPHA + ADD_NL:
5938 p = "NALPHA+NL";
5939 break;
5940 case LOWER:
5941 p = "LOWER";
5942 break;
5943 case LOWER + ADD_NL:
5944 p = "LOWER+NL";
5945 break;
5946 case NLOWER:
5947 p = "NLOWER";
5948 break;
5949 case NLOWER + ADD_NL:
5950 p = "NLOWER+NL";
5951 break;
5952 case UPPER:
5953 p = "UPPER";
5954 break;
5955 case UPPER + ADD_NL:
5956 p = "UPPER+NL";
5957 break;
5958 case NUPPER:
5959 p = "NUPPER";
5960 break;
5961 case NUPPER + ADD_NL:
5962 p = "NUPPER+NL";
5963 break;
5964 case BRANCH:
5965 p = "BRANCH";
5966 break;
5967 case EXACTLY:
5968 p = "EXACTLY";
5969 break;
5970 case NOTHING:
5971 p = "NOTHING";
5972 break;
5973 case BACK:
5974 p = "BACK";
5975 break;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005976 case BACKP:
5977 p = "BACKP";
5978 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 case END:
5980 p = "END";
5981 break;
5982 case MOPEN + 0:
5983 p = "MATCH START";
5984 break;
5985 case MOPEN + 1:
5986 case MOPEN + 2:
5987 case MOPEN + 3:
5988 case MOPEN + 4:
5989 case MOPEN + 5:
5990 case MOPEN + 6:
5991 case MOPEN + 7:
5992 case MOPEN + 8:
5993 case MOPEN + 9:
5994 sprintf(buf + STRLEN(buf), "MOPEN%d", OP(op) - MOPEN);
5995 p = NULL;
5996 break;
5997 case MCLOSE + 0:
5998 p = "MATCH END";
5999 break;
6000 case MCLOSE + 1:
6001 case MCLOSE + 2:
6002 case MCLOSE + 3:
6003 case MCLOSE + 4:
6004 case MCLOSE + 5:
6005 case MCLOSE + 6:
6006 case MCLOSE + 7:
6007 case MCLOSE + 8:
6008 case MCLOSE + 9:
6009 sprintf(buf + STRLEN(buf), "MCLOSE%d", OP(op) - MCLOSE);
6010 p = NULL;
6011 break;
6012 case BACKREF + 1:
6013 case BACKREF + 2:
6014 case BACKREF + 3:
6015 case BACKREF + 4:
6016 case BACKREF + 5:
6017 case BACKREF + 6:
6018 case BACKREF + 7:
6019 case BACKREF + 8:
6020 case BACKREF + 9:
6021 sprintf(buf + STRLEN(buf), "BACKREF%d", OP(op) - BACKREF);
6022 p = NULL;
6023 break;
6024 case NOPEN:
6025 p = "NOPEN";
6026 break;
6027 case NCLOSE:
6028 p = "NCLOSE";
6029 break;
6030#ifdef FEAT_SYN_HL
6031 case ZOPEN + 1:
6032 case ZOPEN + 2:
6033 case ZOPEN + 3:
6034 case ZOPEN + 4:
6035 case ZOPEN + 5:
6036 case ZOPEN + 6:
6037 case ZOPEN + 7:
6038 case ZOPEN + 8:
6039 case ZOPEN + 9:
6040 sprintf(buf + STRLEN(buf), "ZOPEN%d", OP(op) - ZOPEN);
6041 p = NULL;
6042 break;
6043 case ZCLOSE + 1:
6044 case ZCLOSE + 2:
6045 case ZCLOSE + 3:
6046 case ZCLOSE + 4:
6047 case ZCLOSE + 5:
6048 case ZCLOSE + 6:
6049 case ZCLOSE + 7:
6050 case ZCLOSE + 8:
6051 case ZCLOSE + 9:
6052 sprintf(buf + STRLEN(buf), "ZCLOSE%d", OP(op) - ZCLOSE);
6053 p = NULL;
6054 break;
6055 case ZREF + 1:
6056 case ZREF + 2:
6057 case ZREF + 3:
6058 case ZREF + 4:
6059 case ZREF + 5:
6060 case ZREF + 6:
6061 case ZREF + 7:
6062 case ZREF + 8:
6063 case ZREF + 9:
6064 sprintf(buf + STRLEN(buf), "ZREF%d", OP(op) - ZREF);
6065 p = NULL;
6066 break;
6067#endif
6068 case STAR:
6069 p = "STAR";
6070 break;
6071 case PLUS:
6072 p = "PLUS";
6073 break;
6074 case NOMATCH:
6075 p = "NOMATCH";
6076 break;
6077 case MATCH:
6078 p = "MATCH";
6079 break;
6080 case BEHIND:
6081 p = "BEHIND";
6082 break;
6083 case NOBEHIND:
6084 p = "NOBEHIND";
6085 break;
6086 case SUBPAT:
6087 p = "SUBPAT";
6088 break;
6089 case BRACE_LIMITS:
6090 p = "BRACE_LIMITS";
6091 break;
6092 case BRACE_SIMPLE:
6093 p = "BRACE_SIMPLE";
6094 break;
6095 case BRACE_COMPLEX + 0:
6096 case BRACE_COMPLEX + 1:
6097 case BRACE_COMPLEX + 2:
6098 case BRACE_COMPLEX + 3:
6099 case BRACE_COMPLEX + 4:
6100 case BRACE_COMPLEX + 5:
6101 case BRACE_COMPLEX + 6:
6102 case BRACE_COMPLEX + 7:
6103 case BRACE_COMPLEX + 8:
6104 case BRACE_COMPLEX + 9:
6105 sprintf(buf + STRLEN(buf), "BRACE_COMPLEX%d", OP(op) - BRACE_COMPLEX);
6106 p = NULL;
6107 break;
6108#ifdef FEAT_MBYTE
6109 case MULTIBYTECODE:
6110 p = "MULTIBYTECODE";
6111 break;
6112#endif
6113 case NEWL:
6114 p = "NEWL";
6115 break;
6116 default:
6117 sprintf(buf + STRLEN(buf), "corrupt %d", OP(op));
6118 p = NULL;
6119 break;
6120 }
6121 if (p != NULL)
6122 (void) strcat(buf, p);
6123 return buf;
6124}
6125#endif
6126
6127#ifdef FEAT_MBYTE
6128static void mb_decompose __ARGS((int c, int *c1, int *c2, int *c3));
6129
6130typedef struct
6131{
6132 int a, b, c;
6133} decomp_T;
6134
6135
6136/* 0xfb20 - 0xfb4f */
6137decomp_T decomp_table[0xfb4f-0xfb20+1] =
6138{
6139 {0x5e2,0,0}, /* 0xfb20 alt ayin */
6140 {0x5d0,0,0}, /* 0xfb21 alt alef */
6141 {0x5d3,0,0}, /* 0xfb22 alt dalet */
6142 {0x5d4,0,0}, /* 0xfb23 alt he */
6143 {0x5db,0,0}, /* 0xfb24 alt kaf */
6144 {0x5dc,0,0}, /* 0xfb25 alt lamed */
6145 {0x5dd,0,0}, /* 0xfb26 alt mem-sofit */
6146 {0x5e8,0,0}, /* 0xfb27 alt resh */
6147 {0x5ea,0,0}, /* 0xfb28 alt tav */
6148 {'+', 0, 0}, /* 0xfb29 alt plus */
6149 {0x5e9, 0x5c1, 0}, /* 0xfb2a shin+shin-dot */
6150 {0x5e9, 0x5c2, 0}, /* 0xfb2b shin+sin-dot */
6151 {0x5e9, 0x5c1, 0x5bc}, /* 0xfb2c shin+shin-dot+dagesh */
6152 {0x5e9, 0x5c2, 0x5bc}, /* 0xfb2d shin+sin-dot+dagesh */
6153 {0x5d0, 0x5b7, 0}, /* 0xfb2e alef+patah */
6154 {0x5d0, 0x5b8, 0}, /* 0xfb2f alef+qamats */
6155 {0x5d0, 0x5b4, 0}, /* 0xfb30 alef+hiriq */
6156 {0x5d1, 0x5bc, 0}, /* 0xfb31 bet+dagesh */
6157 {0x5d2, 0x5bc, 0}, /* 0xfb32 gimel+dagesh */
6158 {0x5d3, 0x5bc, 0}, /* 0xfb33 dalet+dagesh */
6159 {0x5d4, 0x5bc, 0}, /* 0xfb34 he+dagesh */
6160 {0x5d5, 0x5bc, 0}, /* 0xfb35 vav+dagesh */
6161 {0x5d6, 0x5bc, 0}, /* 0xfb36 zayin+dagesh */
6162 {0xfb37, 0, 0}, /* 0xfb37 -- UNUSED */
6163 {0x5d8, 0x5bc, 0}, /* 0xfb38 tet+dagesh */
6164 {0x5d9, 0x5bc, 0}, /* 0xfb39 yud+dagesh */
6165 {0x5da, 0x5bc, 0}, /* 0xfb3a kaf sofit+dagesh */
6166 {0x5db, 0x5bc, 0}, /* 0xfb3b kaf+dagesh */
6167 {0x5dc, 0x5bc, 0}, /* 0xfb3c lamed+dagesh */
6168 {0xfb3d, 0, 0}, /* 0xfb3d -- UNUSED */
6169 {0x5de, 0x5bc, 0}, /* 0xfb3e mem+dagesh */
6170 {0xfb3f, 0, 0}, /* 0xfb3f -- UNUSED */
6171 {0x5e0, 0x5bc, 0}, /* 0xfb40 nun+dagesh */
6172 {0x5e1, 0x5bc, 0}, /* 0xfb41 samech+dagesh */
6173 {0xfb42, 0, 0}, /* 0xfb42 -- UNUSED */
6174 {0x5e3, 0x5bc, 0}, /* 0xfb43 pe sofit+dagesh */
6175 {0x5e4, 0x5bc,0}, /* 0xfb44 pe+dagesh */
6176 {0xfb45, 0, 0}, /* 0xfb45 -- UNUSED */
6177 {0x5e6, 0x5bc, 0}, /* 0xfb46 tsadi+dagesh */
6178 {0x5e7, 0x5bc, 0}, /* 0xfb47 qof+dagesh */
6179 {0x5e8, 0x5bc, 0}, /* 0xfb48 resh+dagesh */
6180 {0x5e9, 0x5bc, 0}, /* 0xfb49 shin+dagesh */
6181 {0x5ea, 0x5bc, 0}, /* 0xfb4a tav+dagesh */
6182 {0x5d5, 0x5b9, 0}, /* 0xfb4b vav+holam */
6183 {0x5d1, 0x5bf, 0}, /* 0xfb4c bet+rafe */
6184 {0x5db, 0x5bf, 0}, /* 0xfb4d kaf+rafe */
6185 {0x5e4, 0x5bf, 0}, /* 0xfb4e pe+rafe */
6186 {0x5d0, 0x5dc, 0} /* 0xfb4f alef-lamed */
6187};
6188
6189 static void
6190mb_decompose(c, c1, c2, c3)
6191 int c, *c1, *c2, *c3;
6192{
6193 decomp_T d;
6194
6195 if (c >= 0x4b20 && c <= 0xfb4f)
6196 {
6197 d = decomp_table[c - 0xfb20];
6198 *c1 = d.a;
6199 *c2 = d.b;
6200 *c3 = d.c;
6201 }
6202 else
6203 {
6204 *c1 = c;
6205 *c2 = *c3 = 0;
6206 }
6207}
6208#endif
6209
6210/*
6211 * Compare two strings, ignore case if ireg_ic set.
6212 * Return 0 if strings match, non-zero otherwise.
6213 * Correct the length "*n" when composing characters are ignored.
6214 */
6215 static int
6216cstrncmp(s1, s2, n)
6217 char_u *s1, *s2;
6218 int *n;
6219{
6220 int result;
6221
6222 if (!ireg_ic)
6223 result = STRNCMP(s1, s2, *n);
6224 else
6225 result = MB_STRNICMP(s1, s2, *n);
6226
6227#ifdef FEAT_MBYTE
6228 /* if it failed and it's utf8 and we want to combineignore: */
6229 if (result != 0 && enc_utf8 && ireg_icombine)
6230 {
6231 char_u *str1, *str2;
6232 int c1, c2, c11, c12;
6233 int ix;
6234 int junk;
6235
6236 /* we have to handle the strcmp ourselves, since it is necessary to
6237 * deal with the composing characters by ignoring them: */
6238 str1 = s1;
6239 str2 = s2;
6240 c1 = c2 = 0;
6241 for (ix = 0; ix < *n; )
6242 {
6243 c1 = mb_ptr2char_adv(&str1);
6244 c2 = mb_ptr2char_adv(&str2);
6245 ix += utf_char2len(c1);
6246
6247 /* decompose the character if necessary, into 'base' characters
6248 * because I don't care about Arabic, I will hard-code the Hebrew
6249 * which I *do* care about! So sue me... */
6250 if (c1 != c2 && (!ireg_ic || utf_fold(c1) != utf_fold(c2)))
6251 {
6252 /* decomposition necessary? */
6253 mb_decompose(c1, &c11, &junk, &junk);
6254 mb_decompose(c2, &c12, &junk, &junk);
6255 c1 = c11;
6256 c2 = c12;
6257 if (c11 != c12 && (!ireg_ic || utf_fold(c11) != utf_fold(c12)))
6258 break;
6259 }
6260 }
6261 result = c2 - c1;
6262 if (result == 0)
6263 *n = (int)(str2 - s2);
6264 }
6265#endif
6266
6267 return result;
6268}
6269
6270/*
6271 * cstrchr: This function is used a lot for simple searches, keep it fast!
6272 */
6273 static char_u *
6274cstrchr(s, c)
6275 char_u *s;
6276 int c;
6277{
6278 char_u *p;
6279 int cc;
6280
6281 if (!ireg_ic
6282#ifdef FEAT_MBYTE
6283 || (!enc_utf8 && mb_char2len(c) > 1)
6284#endif
6285 )
6286 return vim_strchr(s, c);
6287
6288 /* tolower() and toupper() can be slow, comparing twice should be a lot
6289 * faster (esp. when using MS Visual C++!).
6290 * For UTF-8 need to use folded case. */
6291#ifdef FEAT_MBYTE
6292 if (enc_utf8 && c > 0x80)
6293 cc = utf_fold(c);
6294 else
6295#endif
6296 if (isupper(c))
6297 cc = TOLOWER_LOC(c);
6298 else if (islower(c))
6299 cc = TOUPPER_LOC(c);
6300 else
6301 return vim_strchr(s, c);
6302
6303#ifdef FEAT_MBYTE
6304 if (has_mbyte)
6305 {
6306 for (p = s; *p != NUL; p += (*mb_ptr2len_check)(p))
6307 {
6308 if (enc_utf8 && c > 0x80)
6309 {
6310 if (utf_fold(utf_ptr2char(p)) == cc)
6311 return p;
6312 }
6313 else if (*p == c || *p == cc)
6314 return p;
6315 }
6316 }
6317 else
6318#endif
6319 /* Faster version for when there are no multi-byte characters. */
6320 for (p = s; *p != NUL; ++p)
6321 if (*p == c || *p == cc)
6322 return p;
6323
6324 return NULL;
6325}
6326
6327/***************************************************************
6328 * regsub stuff *
6329 ***************************************************************/
6330
6331/* This stuff below really confuses cc on an SGI -- webb */
6332#ifdef __sgi
6333# undef __ARGS
6334# define __ARGS(x) ()
6335#endif
6336
6337/*
6338 * We should define ftpr as a pointer to a function returning a pointer to
6339 * a function returning a pointer to a function ...
6340 * This is impossible, so we declare a pointer to a function returning a
6341 * pointer to a function returning void. This should work for all compilers.
6342 */
6343typedef void (*(*fptr) __ARGS((char_u *, int)))();
6344
6345static fptr do_upper __ARGS((char_u *, int));
6346static fptr do_Upper __ARGS((char_u *, int));
6347static fptr do_lower __ARGS((char_u *, int));
6348static fptr do_Lower __ARGS((char_u *, int));
6349
6350static int vim_regsub_both __ARGS((char_u *source, char_u *dest, int copy, int magic, int backslash));
6351
6352 static fptr
6353do_upper(d, c)
6354 char_u *d;
6355 int c;
6356{
6357 *d = TOUPPER_LOC(c);
6358
6359 return (fptr)NULL;
6360}
6361
6362 static fptr
6363do_Upper(d, c)
6364 char_u *d;
6365 int c;
6366{
6367 *d = TOUPPER_LOC(c);
6368
6369 return (fptr)do_Upper;
6370}
6371
6372 static fptr
6373do_lower(d, c)
6374 char_u *d;
6375 int c;
6376{
6377 *d = TOLOWER_LOC(c);
6378
6379 return (fptr)NULL;
6380}
6381
6382 static fptr
6383do_Lower(d, c)
6384 char_u *d;
6385 int c;
6386{
6387 *d = TOLOWER_LOC(c);
6388
6389 return (fptr)do_Lower;
6390}
6391
6392/*
6393 * regtilde(): Replace tildes in the pattern by the old pattern.
6394 *
6395 * Short explanation of the tilde: It stands for the previous replacement
6396 * pattern. If that previous pattern also contains a ~ we should go back a
6397 * step further... But we insert the previous pattern into the current one
6398 * and remember that.
6399 * This still does not handle the case where "magic" changes. TODO?
6400 *
6401 * The tildes are parsed once before the first call to vim_regsub().
6402 */
6403 char_u *
6404regtilde(source, magic)
6405 char_u *source;
6406 int magic;
6407{
6408 char_u *newsub = source;
6409 char_u *tmpsub;
6410 char_u *p;
6411 int len;
6412 int prevlen;
6413
6414 for (p = newsub; *p; ++p)
6415 {
6416 if ((*p == '~' && magic) || (*p == '\\' && *(p + 1) == '~' && !magic))
6417 {
6418 if (reg_prev_sub != NULL)
6419 {
6420 /* length = len(newsub) - 1 + len(prev_sub) + 1 */
6421 prevlen = (int)STRLEN(reg_prev_sub);
6422 tmpsub = alloc((unsigned)(STRLEN(newsub) + prevlen));
6423 if (tmpsub != NULL)
6424 {
6425 /* copy prefix */
6426 len = (int)(p - newsub); /* not including ~ */
6427 mch_memmove(tmpsub, newsub, (size_t)len);
6428 /* interpretate tilde */
6429 mch_memmove(tmpsub + len, reg_prev_sub, (size_t)prevlen);
6430 /* copy postfix */
6431 if (!magic)
6432 ++p; /* back off \ */
6433 STRCPY(tmpsub + len + prevlen, p + 1);
6434
6435 if (newsub != source) /* already allocated newsub */
6436 vim_free(newsub);
6437 newsub = tmpsub;
6438 p = newsub + len + prevlen;
6439 }
6440 }
6441 else if (magic)
6442 STRCPY(p, p + 1); /* remove '~' */
6443 else
6444 STRCPY(p, p + 2); /* remove '\~' */
6445 --p;
6446 }
6447 else
6448 {
6449 if (*p == '\\' && p[1]) /* skip escaped characters */
6450 ++p;
6451#ifdef FEAT_MBYTE
6452 if (has_mbyte)
6453 p += (*mb_ptr2len_check)(p) - 1;
6454#endif
6455 }
6456 }
6457
6458 vim_free(reg_prev_sub);
6459 if (newsub != source) /* newsub was allocated, just keep it */
6460 reg_prev_sub = newsub;
6461 else /* no ~ found, need to save newsub */
6462 reg_prev_sub = vim_strsave(newsub);
6463 return newsub;
6464}
6465
6466#ifdef FEAT_EVAL
6467static int can_f_submatch = FALSE; /* TRUE when submatch() can be used */
6468
6469/* These pointers are used instead of reg_match and reg_mmatch for
6470 * reg_submatch(). Needed for when the substitution string is an expression
6471 * that contains a call to substitute() and submatch(). */
6472static regmatch_T *submatch_match;
6473static regmmatch_T *submatch_mmatch;
6474#endif
6475
6476#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
6477/*
6478 * vim_regsub() - perform substitutions after a vim_regexec() or
6479 * vim_regexec_multi() match.
6480 *
6481 * If "copy" is TRUE really copy into "dest".
6482 * If "copy" is FALSE nothing is copied, this is just to find out the length
6483 * of the result.
6484 *
6485 * If "backslash" is TRUE, a backslash will be removed later, need to double
6486 * them to keep them, and insert a backslash before a CR to avoid it being
6487 * replaced with a line break later.
6488 *
6489 * Note: The matched text must not change between the call of
6490 * vim_regexec()/vim_regexec_multi() and vim_regsub()! It would make the back
6491 * references invalid!
6492 *
6493 * Returns the size of the replacement, including terminating NUL.
6494 */
6495 int
6496vim_regsub(rmp, source, dest, copy, magic, backslash)
6497 regmatch_T *rmp;
6498 char_u *source;
6499 char_u *dest;
6500 int copy;
6501 int magic;
6502 int backslash;
6503{
6504 reg_match = rmp;
6505 reg_mmatch = NULL;
6506 reg_maxline = 0;
6507 return vim_regsub_both(source, dest, copy, magic, backslash);
6508}
6509#endif
6510
6511 int
6512vim_regsub_multi(rmp, lnum, source, dest, copy, magic, backslash)
6513 regmmatch_T *rmp;
6514 linenr_T lnum;
6515 char_u *source;
6516 char_u *dest;
6517 int copy;
6518 int magic;
6519 int backslash;
6520{
6521 reg_match = NULL;
6522 reg_mmatch = rmp;
6523 reg_buf = curbuf; /* always works on the current buffer! */
6524 reg_firstlnum = lnum;
6525 reg_maxline = curbuf->b_ml.ml_line_count - lnum;
6526 return vim_regsub_both(source, dest, copy, magic, backslash);
6527}
6528
6529 static int
6530vim_regsub_both(source, dest, copy, magic, backslash)
6531 char_u *source;
6532 char_u *dest;
6533 int copy;
6534 int magic;
6535 int backslash;
6536{
6537 char_u *src;
6538 char_u *dst;
6539 char_u *s;
6540 int c;
6541 int no = -1;
6542 fptr func = (fptr)NULL;
6543 linenr_T clnum = 0; /* init for GCC */
6544 int len = 0; /* init for GCC */
6545#ifdef FEAT_EVAL
6546 static char_u *eval_result = NULL;
6547#endif
6548#ifdef FEAT_MBYTE
6549 int l;
6550#endif
6551
6552
6553 /* Be paranoid... */
6554 if (source == NULL || dest == NULL)
6555 {
6556 EMSG(_(e_null));
6557 return 0;
6558 }
6559 if (prog_magic_wrong())
6560 return 0;
6561 src = source;
6562 dst = dest;
6563
6564 /*
6565 * When the substitute part starts with "\=" evaluate it as an expression.
6566 */
6567 if (source[0] == '\\' && source[1] == '='
6568#ifdef FEAT_EVAL
6569 && !can_f_submatch /* can't do this recursively */
6570#endif
6571 )
6572 {
6573#ifdef FEAT_EVAL
6574 /* To make sure that the length doesn't change between checking the
6575 * length and copying the string, and to speed up things, the
6576 * resulting string is saved from the call with "copy" == FALSE to the
6577 * call with "copy" == TRUE. */
6578 if (copy)
6579 {
6580 if (eval_result != NULL)
6581 {
6582 STRCPY(dest, eval_result);
6583 dst += STRLEN(eval_result);
6584 vim_free(eval_result);
6585 eval_result = NULL;
6586 }
6587 }
6588 else
6589 {
6590 linenr_T save_reg_maxline;
6591 win_T *save_reg_win;
6592 int save_ireg_ic;
6593
6594 vim_free(eval_result);
6595
6596 /* The expression may contain substitute(), which calls us
6597 * recursively. Make sure submatch() gets the text from the first
6598 * level. Don't need to save "reg_buf", because
6599 * vim_regexec_multi() can't be called recursively. */
6600 submatch_match = reg_match;
6601 submatch_mmatch = reg_mmatch;
6602 save_reg_maxline = reg_maxline;
6603 save_reg_win = reg_win;
6604 save_ireg_ic = ireg_ic;
6605 can_f_submatch = TRUE;
6606
6607 eval_result = eval_to_string(source + 2, NULL);
6608 if (eval_result != NULL)
6609 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006610 for (s = eval_result; *s != NUL; mb_ptr_adv(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 {
6612 /* Change NL to CR, so that it becomes a line break.
6613 * Skip over a backslashed character. */
6614 if (*s == NL)
6615 *s = CAR;
6616 else if (*s == '\\' && s[1] != NUL)
6617 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 }
6619
6620 dst += STRLEN(eval_result);
6621 }
6622
6623 reg_match = submatch_match;
6624 reg_mmatch = submatch_mmatch;
6625 reg_maxline = save_reg_maxline;
6626 reg_win = save_reg_win;
6627 ireg_ic = save_ireg_ic;
6628 can_f_submatch = FALSE;
6629 }
6630#endif
6631 }
6632 else
6633 while ((c = *src++) != NUL)
6634 {
6635 if (c == '&' && magic)
6636 no = 0;
6637 else if (c == '\\' && *src != NUL)
6638 {
6639 if (*src == '&' && !magic)
6640 {
6641 ++src;
6642 no = 0;
6643 }
6644 else if ('0' <= *src && *src <= '9')
6645 {
6646 no = *src++ - '0';
6647 }
6648 else if (vim_strchr((char_u *)"uUlLeE", *src))
6649 {
6650 switch (*src++)
6651 {
6652 case 'u': func = (fptr)do_upper;
6653 continue;
6654 case 'U': func = (fptr)do_Upper;
6655 continue;
6656 case 'l': func = (fptr)do_lower;
6657 continue;
6658 case 'L': func = (fptr)do_Lower;
6659 continue;
6660 case 'e':
6661 case 'E': func = (fptr)NULL;
6662 continue;
6663 }
6664 }
6665 }
6666 if (no < 0) /* Ordinary character. */
6667 {
6668 if (c == '\\' && *src != NUL)
6669 {
6670 /* Check for abbreviations -- webb */
6671 switch (*src)
6672 {
6673 case 'r': c = CAR; ++src; break;
6674 case 'n': c = NL; ++src; break;
6675 case 't': c = TAB; ++src; break;
6676 /* Oh no! \e already has meaning in subst pat :-( */
6677 /* case 'e': c = ESC; ++src; break; */
6678 case 'b': c = Ctrl_H; ++src; break;
6679
6680 /* If "backslash" is TRUE the backslash will be removed
6681 * later. Used to insert a literal CR. */
6682 default: if (backslash)
6683 {
6684 if (copy)
6685 *dst = '\\';
6686 ++dst;
6687 }
6688 c = *src++;
6689 }
6690 }
6691
6692 /* Write to buffer, if copy is set. */
6693#ifdef FEAT_MBYTE
6694 if (has_mbyte && (l = (*mb_ptr2len_check)(src - 1)) > 1)
6695 {
6696 /* TODO: should use "func" here. */
6697 if (copy)
6698 mch_memmove(dst, src - 1, l);
6699 dst += l - 1;
6700 src += l - 1;
6701 }
6702 else
6703 {
6704#endif
6705 if (copy)
6706 {
6707 if (func == (fptr)NULL) /* just copy */
6708 *dst = c;
6709 else /* change case */
6710 func = (fptr)(func(dst, c));
6711 /* Turbo C complains without the typecast */
6712 }
6713#ifdef FEAT_MBYTE
6714 }
6715#endif
6716 dst++;
6717 }
6718 else
6719 {
6720 if (REG_MULTI)
6721 {
6722 clnum = reg_mmatch->startpos[no].lnum;
6723 if (clnum < 0 || reg_mmatch->endpos[no].lnum < 0)
6724 s = NULL;
6725 else
6726 {
6727 s = reg_getline(clnum) + reg_mmatch->startpos[no].col;
6728 if (reg_mmatch->endpos[no].lnum == clnum)
6729 len = reg_mmatch->endpos[no].col
6730 - reg_mmatch->startpos[no].col;
6731 else
6732 len = (int)STRLEN(s);
6733 }
6734 }
6735 else
6736 {
6737 s = reg_match->startp[no];
6738 if (reg_match->endp[no] == NULL)
6739 s = NULL;
6740 else
6741 len = (int)(reg_match->endp[no] - s);
6742 }
6743 if (s != NULL)
6744 {
6745 for (;;)
6746 {
6747 if (len == 0)
6748 {
6749 if (REG_MULTI)
6750 {
6751 if (reg_mmatch->endpos[no].lnum == clnum)
6752 break;
6753 if (copy)
6754 *dst = CAR;
6755 ++dst;
6756 s = reg_getline(++clnum);
6757 if (reg_mmatch->endpos[no].lnum == clnum)
6758 len = reg_mmatch->endpos[no].col;
6759 else
6760 len = (int)STRLEN(s);
6761 }
6762 else
6763 break;
6764 }
6765 else if (*s == NUL) /* we hit NUL. */
6766 {
6767 if (copy)
6768 EMSG(_(e_re_damg));
6769 goto exit;
6770 }
6771 else
6772 {
6773 if (backslash && (*s == CAR || *s == '\\'))
6774 {
6775 /*
6776 * Insert a backslash in front of a CR, otherwise
6777 * it will be replaced by a line break.
6778 * Number of backslashes will be halved later,
6779 * double them here.
6780 */
6781 if (copy)
6782 {
6783 dst[0] = '\\';
6784 dst[1] = *s;
6785 }
6786 dst += 2;
6787 }
6788#ifdef FEAT_MBYTE
6789 else if (has_mbyte && (l = (*mb_ptr2len_check)(s)) > 1)
6790 {
6791 /* TODO: should use "func" here. */
6792 if (copy)
6793 mch_memmove(dst, s, l);
6794 dst += l;
6795 s += l - 1;
6796 len -= l - 1;
6797 }
6798#endif
6799 else
6800 {
6801 if (copy)
6802 {
6803 if (func == (fptr)NULL) /* just copy */
6804 *dst = *s;
6805 else /* change case */
6806 func = (fptr)(func(dst, *s));
6807 /* Turbo C complains without the typecast */
6808 }
6809 ++dst;
6810 }
6811 ++s;
6812 --len;
6813 }
6814 }
6815 }
6816 no = -1;
6817 }
6818 }
6819 if (copy)
6820 *dst = NUL;
6821
6822exit:
6823 return (int)((dst - dest) + 1);
6824}
6825
6826#ifdef FEAT_EVAL
6827/*
6828 * Used for the submatch() function: get the string from tne n'th submatch in
6829 * allocated memory.
6830 * Returns NULL when not in a ":s" command and for a non-existing submatch.
6831 */
6832 char_u *
6833reg_submatch(no)
6834 int no;
6835{
6836 char_u *retval = NULL;
6837 char_u *s;
6838 int len;
6839 int round;
6840 linenr_T lnum;
6841
6842 if (!can_f_submatch)
6843 return NULL;
6844
6845 if (submatch_match == NULL)
6846 {
6847 /*
6848 * First round: compute the length and allocate memory.
6849 * Second round: copy the text.
6850 */
6851 for (round = 1; round <= 2; ++round)
6852 {
6853 lnum = submatch_mmatch->startpos[no].lnum;
6854 if (lnum < 0 || submatch_mmatch->endpos[no].lnum < 0)
6855 return NULL;
6856
6857 s = reg_getline(lnum) + submatch_mmatch->startpos[no].col;
6858 if (s == NULL) /* anti-crash check, cannot happen? */
6859 break;
6860 if (submatch_mmatch->endpos[no].lnum == lnum)
6861 {
6862 /* Within one line: take form start to end col. */
6863 len = submatch_mmatch->endpos[no].col
6864 - submatch_mmatch->startpos[no].col;
6865 if (round == 2)
6866 {
6867 STRNCPY(retval, s, len);
6868 retval[len] = NUL;
6869 }
6870 ++len;
6871 }
6872 else
6873 {
6874 /* Multiple lines: take start line from start col, middle
6875 * lines completely and end line up to end col. */
6876 len = (int)STRLEN(s);
6877 if (round == 2)
6878 {
6879 STRCPY(retval, s);
6880 retval[len] = '\n';
6881 }
6882 ++len;
6883 ++lnum;
6884 while (lnum < submatch_mmatch->endpos[no].lnum)
6885 {
6886 s = reg_getline(lnum++);
6887 if (round == 2)
6888 STRCPY(retval + len, s);
6889 len += (int)STRLEN(s);
6890 if (round == 2)
6891 retval[len] = '\n';
6892 ++len;
6893 }
6894 if (round == 2)
6895 STRNCPY(retval + len, reg_getline(lnum),
6896 submatch_mmatch->endpos[no].col);
6897 len += submatch_mmatch->endpos[no].col;
6898 if (round == 2)
6899 retval[len] = NUL;
6900 ++len;
6901 }
6902
6903 if (round == 1)
6904 {
6905 retval = lalloc((long_u)len, TRUE);
6906 if (s == NULL)
6907 return NULL;
6908 }
6909 }
6910 }
6911 else
6912 {
6913 if (submatch_match->endp[no] == NULL)
6914 retval = NULL;
6915 else
6916 {
6917 s = submatch_match->startp[no];
6918 retval = vim_strnsave(s, (int)(submatch_match->endp[no] - s));
6919 }
6920 }
6921
6922 return retval;
6923}
6924#endif