blob: 3cd7cc99147ea673e4cf1af88939ec111120a0cc [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
3187/*
3188 * Match a regexp against a string ("line" points to the string) or multiple
3189 * lines ("line" is NULL, use reg_getline()).
3190 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 static long
3192vim_regexec_both(line, col)
3193 char_u *line;
3194 colnr_T col; /* column to start looking for match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195{
3196 regprog_T *prog;
3197 char_u *s;
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003198 long retval = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199
3200 reg_tofree = NULL;
3201
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 if (REG_MULTI)
3203 {
3204 prog = reg_mmatch->regprog;
3205 line = reg_getline((linenr_T)0);
3206 reg_startpos = reg_mmatch->startpos;
3207 reg_endpos = reg_mmatch->endpos;
3208 }
3209 else
3210 {
3211 prog = reg_match->regprog;
3212 reg_startp = reg_match->startp;
3213 reg_endp = reg_match->endp;
3214 }
3215
3216 /* Be paranoid... */
3217 if (prog == NULL || line == NULL)
3218 {
3219 EMSG(_(e_null));
3220 goto theend;
3221 }
3222
3223 /* Check validity of program. */
3224 if (prog_magic_wrong())
3225 goto theend;
3226
3227 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
3228 if (prog->regflags & RF_ICASE)
3229 ireg_ic = TRUE;
3230 else if (prog->regflags & RF_NOICASE)
3231 ireg_ic = FALSE;
3232
3233#ifdef FEAT_MBYTE
3234 /* If pattern contains "\Z" overrule value of ireg_icombine */
3235 if (prog->regflags & RF_ICOMBINE)
3236 ireg_icombine = TRUE;
3237#endif
3238
3239 /* If there is a "must appear" string, look for it. */
3240 if (prog->regmust != NULL)
3241 {
3242 int c;
3243
3244#ifdef FEAT_MBYTE
3245 if (has_mbyte)
3246 c = (*mb_ptr2char)(prog->regmust);
3247 else
3248#endif
3249 c = *prog->regmust;
3250 s = line + col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003251
3252 /*
3253 * This is used very often, esp. for ":global". Use three versions of
3254 * the loop to avoid overhead of conditions.
3255 */
3256 if (!ireg_ic
3257#ifdef FEAT_MBYTE
3258 && !has_mbyte
3259#endif
3260 )
3261 while ((s = vim_strbyte(s, c)) != NULL)
3262 {
3263 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3264 break; /* Found it. */
3265 ++s;
3266 }
3267#ifdef FEAT_MBYTE
3268 else if (!ireg_ic || (!enc_utf8 && mb_char2len(c) > 1))
3269 while ((s = vim_strchr(s, c)) != NULL)
3270 {
3271 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3272 break; /* Found it. */
3273 mb_ptr_adv(s);
3274 }
3275#endif
3276 else
3277 while ((s = cstrchr(s, c)) != NULL)
3278 {
3279 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3280 break; /* Found it. */
3281 mb_ptr_adv(s);
3282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 if (s == NULL) /* Not present. */
3284 goto theend;
3285 }
3286
3287 regline = line;
3288 reglnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289
3290 /* Simplest case: Anchored match need be tried only once. */
3291 if (prog->reganch)
3292 {
3293 int c;
3294
3295#ifdef FEAT_MBYTE
3296 if (has_mbyte)
3297 c = (*mb_ptr2char)(regline + col);
3298 else
3299#endif
3300 c = regline[col];
3301 if (prog->regstart == NUL
3302 || prog->regstart == c
3303 || (ireg_ic && ((
3304#ifdef FEAT_MBYTE
3305 (enc_utf8 && utf_fold(prog->regstart) == utf_fold(c)))
3306 || (c < 255 && prog->regstart < 255 &&
3307#endif
3308 TOLOWER_LOC(prog->regstart) == TOLOWER_LOC(c)))))
3309 retval = regtry(prog, col);
3310 else
3311 retval = 0;
3312 }
3313 else
3314 {
3315 /* Messy cases: unanchored match. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003316 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 {
3318 if (prog->regstart != NUL)
3319 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003320 /* Skip until the char we know it must start with.
3321 * Used often, do some work to avoid call overhead. */
3322 if (!ireg_ic
3323#ifdef FEAT_MBYTE
3324 && !has_mbyte
3325#endif
3326 )
3327 s = vim_strbyte(regline + col, prog->regstart);
3328 else
3329 s = cstrchr(regline + col, prog->regstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 if (s == NULL)
3331 {
3332 retval = 0;
3333 break;
3334 }
3335 col = (int)(s - regline);
3336 }
3337
3338 retval = regtry(prog, col);
3339 if (retval > 0)
3340 break;
3341
3342 /* if not currently on the first line, get it again */
3343 if (reglnum != 0)
3344 {
3345 regline = reg_getline((linenr_T)0);
3346 reglnum = 0;
3347 }
3348 if (regline[col] == NUL)
3349 break;
3350#ifdef FEAT_MBYTE
3351 if (has_mbyte)
3352 col += (*mb_ptr2len_check)(regline + col);
3353 else
3354#endif
3355 ++col;
3356 }
3357 }
3358
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359theend:
3360 /* Didn't find a match. */
3361 vim_free(reg_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 return retval;
3363}
3364
3365#ifdef FEAT_SYN_HL
3366static reg_extmatch_T *make_extmatch __ARGS((void));
3367
3368/*
3369 * Create a new extmatch and mark it as referenced once.
3370 */
3371 static reg_extmatch_T *
3372make_extmatch()
3373{
3374 reg_extmatch_T *em;
3375
3376 em = (reg_extmatch_T *)alloc_clear((unsigned)sizeof(reg_extmatch_T));
3377 if (em != NULL)
3378 em->refcnt = 1;
3379 return em;
3380}
3381
3382/*
3383 * Add a reference to an extmatch.
3384 */
3385 reg_extmatch_T *
3386ref_extmatch(em)
3387 reg_extmatch_T *em;
3388{
3389 if (em != NULL)
3390 em->refcnt++;
3391 return em;
3392}
3393
3394/*
3395 * Remove a reference to an extmatch. If there are no references left, free
3396 * the info.
3397 */
3398 void
3399unref_extmatch(em)
3400 reg_extmatch_T *em;
3401{
3402 int i;
3403
3404 if (em != NULL && --em->refcnt <= 0)
3405 {
3406 for (i = 0; i < NSUBEXP; ++i)
3407 vim_free(em->matches[i]);
3408 vim_free(em);
3409 }
3410}
3411#endif
3412
3413/*
3414 * regtry - try match of "prog" with at regline["col"].
3415 * Returns 0 for failure, number of lines contained in the match otherwise.
3416 */
3417 static long
3418regtry(prog, col)
3419 regprog_T *prog;
3420 colnr_T col;
3421{
3422 reginput = regline + col;
3423 need_clear_subexpr = TRUE;
3424#ifdef FEAT_SYN_HL
3425 /* Clear the external match subpointers if necessary. */
3426 if (prog->reghasz == REX_SET)
3427 need_clear_zsubexpr = TRUE;
3428#endif
3429
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003430 if (regmatch(prog->program + 1) == 0)
3431 return 0;
3432
3433 cleanup_subexpr();
3434 if (REG_MULTI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003436 if (reg_startpos[0].lnum < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003438 reg_startpos[0].lnum = 0;
3439 reg_startpos[0].col = col;
3440 }
3441 if (reg_endpos[0].lnum < 0)
3442 {
3443 reg_endpos[0].lnum = reglnum;
3444 reg_endpos[0].col = (int)(reginput - regline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 }
3446 else
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003447 /* Use line number of "\ze". */
3448 reglnum = reg_endpos[0].lnum;
3449 }
3450 else
3451 {
3452 if (reg_startp[0] == NULL)
3453 reg_startp[0] = regline + col;
3454 if (reg_endp[0] == NULL)
3455 reg_endp[0] = reginput;
3456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457#ifdef FEAT_SYN_HL
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003458 /* Package any found \z(...\) matches for export. Default is none. */
3459 unref_extmatch(re_extmatch_out);
3460 re_extmatch_out = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003462 if (prog->reghasz == REX_SET)
3463 {
3464 int i;
3465
3466 cleanup_zsubexpr();
3467 re_extmatch_out = make_extmatch();
3468 for (i = 0; i < NSUBEXP; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003470 if (REG_MULTI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003472 /* Only accept single line matches. */
3473 if (reg_startzpos[i].lnum >= 0
3474 && reg_endzpos[i].lnum == reg_startzpos[i].lnum)
3475 re_extmatch_out->matches[i] =
3476 vim_strnsave(reg_getline(reg_startzpos[i].lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 + reg_startzpos[i].col,
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003478 reg_endzpos[i].col - reg_startzpos[i].col);
3479 }
3480 else
3481 {
3482 if (reg_startzp[i] != NULL && reg_endzp[i] != NULL)
3483 re_extmatch_out->matches[i] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 vim_strnsave(reg_startzp[i],
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003485 (int)(reg_endzp[i] - reg_startzp[i]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 }
3487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 }
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003489#endif
3490 return 1 + reglnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491}
3492
3493#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494static int reg_prev_class __ARGS((void));
3495
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496/*
3497 * Get class of previous character.
3498 */
3499 static int
3500reg_prev_class()
3501{
3502 if (reginput > regline)
3503 return mb_get_class(reginput - 1
3504 - (*mb_head_off)(regline, reginput - 1));
3505 return -1;
3506}
3507
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003509#define ADVANCE_REGINPUT() mb_ptr_adv(reginput)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510
3511/*
3512 * The arguments from BRACE_LIMITS are stored here. They are actually local
3513 * to regmatch(), but they are here to reduce the amount of stack space used
3514 * (it can be called recursively many times).
3515 */
3516static long bl_minval;
3517static long bl_maxval;
3518
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00003519/* Values for rs_state in regitem_T. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003520typedef enum regstate_E
3521{
3522 RS_NOPEN = 0 /* NOPEN and NCLOSE */
3523 , RS_MOPEN /* MOPEN + [0-9] */
3524 , RS_MCLOSE /* MCLOSE + [0-9] */
3525#ifdef FEAT_SYN_HL
3526 , RS_ZOPEN /* ZOPEN + [0-9] */
3527 , RS_ZCLOSE /* ZCLOSE + [0-9] */
3528#endif
3529 , RS_BRANCH /* BRANCH */
3530 , RS_BRCPLX_MORE /* BRACE_COMPLEX and trying one more match */
3531 , RS_BRCPLX_LONG /* BRACE_COMPLEX and trying longest match */
3532 , RS_BRCPLX_SHORT /* BRACE_COMPLEX and trying shortest match */
3533 , RS_NOMATCH /* NOMATCH */
3534 , RS_BEHIND1 /* BEHIND / NOBEHIND matching rest */
3535 , RS_BEHIND2 /* BEHIND / NOBEHIND matching behind part */
3536 , RS_STAR_LONG /* STAR/PLUS/BRACE_SIMPLE longest match */
3537 , RS_STAR_SHORT /* STAR/PLUS/BRACE_SIMPLE shortest match */
3538} regstate_T;
3539
3540/*
3541 * When there are alternatives a regstate_T is put on the regstack to remember
3542 * what we are doing.
3543 * Before it may be another type of item, depending on rs_state, to remember
3544 * more things.
3545 */
3546typedef struct regitem_S
3547{
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00003548 regstate_T rs_state; /* what we are doing, one of RS_ above */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003549 char_u *rs_scan; /* current node in program */
3550 long rs_startp; /* start position for BACK (offset) */
3551 union
3552 {
3553 save_se_T sesave;
3554 regsave_T regsave;
3555 } rs_un; /* room for saving reginput */
3556 short rs_no; /* submatch nr */
3557} regitem_T;
3558
3559static regitem_T *regstack_push __ARGS((garray_T *regstack, regstate_T state, char_u *scan, long startp));
3560static void regstack_pop __ARGS((garray_T *regstack, char_u **scan, long *startp));
3561
3562/* used for BEHIND and NOBEHIND matching */
3563typedef struct regbehind_S
3564{
3565 regsave_T save_after;
3566 regsave_T save_behind;
3567} regbehind_T;
3568
3569/* used for STAR, PLUS and BRACE_SIMPLE matching */
3570typedef struct regstar_S
3571{
3572 int nextb; /* next byte */
3573 int nextb_ic; /* next byte reverse case */
3574 long count;
3575 long minval;
3576 long maxval;
3577} regstar_T;
3578
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579/*
3580 * regmatch - main matching routine
3581 *
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003582 * Conceptually the strategy is simple: Check to see whether the current node
3583 * matches, push an item onto the regstack and loop to see whether the rest
3584 * matches, and then act accordingly. In practice we make some effort to
3585 * avoid using the regstack, in particular by going through "ordinary" nodes
3586 * (that don't need to know whether the rest of the match failed) by a nested
3587 * loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 *
3589 * Returns TRUE when there is a match. Leaves reginput and reglnum just after
3590 * the last matched character.
3591 * Returns FALSE when there is no match. Leaves reginput and reglnum in an
3592 * undefined state!
3593 */
3594 static int
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003595regmatch(scan)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 char_u *scan; /* Current node. */
3597{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003598 char_u *next; /* Next node. */
3599 int op;
3600 int c;
3601 garray_T regstack;
3602 regitem_T *rp;
3603 int no;
3604 int status; /* one of the RA_ values: */
3605#define RA_FAIL 1 /* something failed, abort */
3606#define RA_CONT 2 /* continue in inner loop */
3607#define RA_BREAK 3 /* break inner loop */
3608#define RA_MATCH 4 /* successful match */
3609#define RA_NOMATCH 5 /* didn't match */
3610 long startp = 0; /* start position for BACK, offset to
3611 regstack.ga_data */
3612#define STARTP2REGS(startp) (regsave_T *)(((char *)regstack.ga_data) + startp)
3613#define REGS2STARTP(p) (long)((char *)p - (char *)regstack.ga_data)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003615 /* Init the regstack empty. Use an item size of 1 byte, since we push
3616 * different things onto it. Use a large grow size to avoid reallocating
3617 * it too often. */
3618 ga_init2(&regstack, 1, 10000);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003620 /*
3621 * Repeat until the stack is empty.
3622 */
3623 for (;;)
3624 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 /* Some patterns my cause a long time to match, even though they are not
3626 * illegal. E.g., "\([a-z]\+\)\+Q". Allow breaking them with CTRL-C. */
3627 fast_breakcheck();
3628
3629#ifdef DEBUG
3630 if (scan != NULL && regnarrate)
3631 {
3632 mch_errmsg(regprop(scan));
3633 mch_errmsg("(\n");
3634 }
3635#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003636
3637 /*
3638 * Repeat for items that can be matched sequential, without using the
3639 * regstack.
3640 */
3641 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003643 if (got_int || scan == NULL)
3644 {
3645 status = RA_FAIL;
3646 break;
3647 }
3648 status = RA_CONT;
3649
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650#ifdef DEBUG
3651 if (regnarrate)
3652 {
3653 mch_errmsg(regprop(scan));
3654 mch_errmsg("...\n");
3655# ifdef FEAT_SYN_HL
3656 if (re_extmatch_in != NULL)
3657 {
3658 int i;
3659
3660 mch_errmsg(_("External submatches:\n"));
3661 for (i = 0; i < NSUBEXP; i++)
3662 {
3663 mch_errmsg(" \"");
3664 if (re_extmatch_in->matches[i] != NULL)
3665 mch_errmsg(re_extmatch_in->matches[i]);
3666 mch_errmsg("\"\n");
3667 }
3668 }
3669# endif
3670 }
3671#endif
3672 next = regnext(scan);
3673
3674 op = OP(scan);
3675 /* Check for character class with NL added. */
3676 if (WITH_NL(op) && *reginput == NUL && reglnum < reg_maxline)
3677 {
3678 reg_nextline();
3679 }
3680 else if (reg_line_lbr && WITH_NL(op) && *reginput == '\n')
3681 {
3682 ADVANCE_REGINPUT();
3683 }
3684 else
3685 {
3686 if (WITH_NL(op))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003687 op -= ADD_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688#ifdef FEAT_MBYTE
3689 if (has_mbyte)
3690 c = (*mb_ptr2char)(reginput);
3691 else
3692#endif
3693 c = *reginput;
3694 switch (op)
3695 {
3696 case BOL:
3697 if (reginput != regline)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003698 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 break;
3700
3701 case EOL:
3702 if (c != NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003703 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 break;
3705
3706 case RE_BOF:
3707 /* Passing -1 to the getline() function provided for the search
3708 * should always return NULL if the current line is the first
3709 * line of the file. */
3710 if (reglnum != 0 || reginput != regline
3711 || (REG_MULTI && reg_getline((linenr_T)-1) != NULL))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003712 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 break;
3714
3715 case RE_EOF:
3716 if (reglnum != reg_maxline || c != NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003717 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 break;
3719
3720 case CURSOR:
3721 /* Check if the buffer is in a window and compare the
3722 * reg_win->w_cursor position to the match position. */
3723 if (reg_win == NULL
3724 || (reglnum + reg_firstlnum != reg_win->w_cursor.lnum)
3725 || ((colnr_T)(reginput - regline) != reg_win->w_cursor.col))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003726 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 break;
3728
3729 case RE_LNUM:
3730 if (!REG_MULTI || !re_num_cmp((long_u)(reglnum + reg_firstlnum),
3731 scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003732 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 break;
3734
3735 case RE_COL:
3736 if (!re_num_cmp((long_u)(reginput - regline) + 1, scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003737 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 break;
3739
3740 case RE_VCOL:
3741 if (!re_num_cmp((long_u)win_linetabsize(
3742 reg_win == NULL ? curwin : reg_win,
3743 regline, (colnr_T)(reginput - regline)) + 1, scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003744 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 break;
3746
3747 case BOW: /* \<word; reginput points to w */
3748 if (c == NUL) /* Can't match at end of line */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003749 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750#ifdef FEAT_MBYTE
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003751 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 {
3753 int this_class;
3754
3755 /* Get class of current and previous char (if it exists). */
3756 this_class = mb_get_class(reginput);
3757 if (this_class <= 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003758 status = RA_NOMATCH; /* not on a word at all */
3759 else if (reg_prev_class() == this_class)
3760 status = RA_NOMATCH; /* previous char is in same word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 }
3762#endif
3763 else
3764 {
3765 if (!vim_iswordc(c)
3766 || (reginput > regline && vim_iswordc(reginput[-1])))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003767 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 }
3769 break;
3770
3771 case EOW: /* word\>; reginput points after d */
3772 if (reginput == regline) /* Can't match at start of line */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003773 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774#ifdef FEAT_MBYTE
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003775 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 {
3777 int this_class, prev_class;
3778
3779 /* Get class of current and previous char (if it exists). */
3780 this_class = mb_get_class(reginput);
3781 prev_class = reg_prev_class();
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003782 if (this_class == prev_class
3783 || prev_class == 0 || prev_class == 1)
3784 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003787 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003789 if (!vim_iswordc(reginput[-1])
3790 || (reginput[0] != NUL && vim_iswordc(c)))
3791 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 }
3793 break; /* Matched with EOW */
3794
3795 case ANY:
3796 if (c == NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003797 status = RA_NOMATCH;
3798 else
3799 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 break;
3801
3802 case IDENT:
3803 if (!vim_isIDc(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003804 status = RA_NOMATCH;
3805 else
3806 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 break;
3808
3809 case SIDENT:
3810 if (VIM_ISDIGIT(*reginput) || !vim_isIDc(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003811 status = RA_NOMATCH;
3812 else
3813 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 break;
3815
3816 case KWORD:
3817 if (!vim_iswordp(reginput))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003818 status = RA_NOMATCH;
3819 else
3820 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 break;
3822
3823 case SKWORD:
3824 if (VIM_ISDIGIT(*reginput) || !vim_iswordp(reginput))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003825 status = RA_NOMATCH;
3826 else
3827 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 break;
3829
3830 case FNAME:
3831 if (!vim_isfilec(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003832 status = RA_NOMATCH;
3833 else
3834 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 break;
3836
3837 case SFNAME:
3838 if (VIM_ISDIGIT(*reginput) || !vim_isfilec(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003839 status = RA_NOMATCH;
3840 else
3841 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 break;
3843
3844 case PRINT:
3845 if (ptr2cells(reginput) != 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003846 status = RA_NOMATCH;
3847 else
3848 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 break;
3850
3851 case SPRINT:
3852 if (VIM_ISDIGIT(*reginput) || ptr2cells(reginput) != 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003853 status = RA_NOMATCH;
3854 else
3855 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 break;
3857
3858 case WHITE:
3859 if (!vim_iswhite(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003860 status = RA_NOMATCH;
3861 else
3862 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 break;
3864
3865 case NWHITE:
3866 if (c == NUL || vim_iswhite(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003867 status = RA_NOMATCH;
3868 else
3869 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 break;
3871
3872 case DIGIT:
3873 if (!ri_digit(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003874 status = RA_NOMATCH;
3875 else
3876 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 break;
3878
3879 case NDIGIT:
3880 if (c == NUL || ri_digit(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003881 status = RA_NOMATCH;
3882 else
3883 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 break;
3885
3886 case HEX:
3887 if (!ri_hex(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003888 status = RA_NOMATCH;
3889 else
3890 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 break;
3892
3893 case NHEX:
3894 if (c == NUL || ri_hex(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003895 status = RA_NOMATCH;
3896 else
3897 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 break;
3899
3900 case OCTAL:
3901 if (!ri_octal(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003902 status = RA_NOMATCH;
3903 else
3904 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 break;
3906
3907 case NOCTAL:
3908 if (c == NUL || ri_octal(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003909 status = RA_NOMATCH;
3910 else
3911 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 break;
3913
3914 case WORD:
3915 if (!ri_word(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003916 status = RA_NOMATCH;
3917 else
3918 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 break;
3920
3921 case NWORD:
3922 if (c == NUL || ri_word(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003923 status = RA_NOMATCH;
3924 else
3925 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 break;
3927
3928 case HEAD:
3929 if (!ri_head(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003930 status = RA_NOMATCH;
3931 else
3932 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 break;
3934
3935 case NHEAD:
3936 if (c == NUL || ri_head(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003937 status = RA_NOMATCH;
3938 else
3939 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 break;
3941
3942 case ALPHA:
3943 if (!ri_alpha(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003944 status = RA_NOMATCH;
3945 else
3946 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 break;
3948
3949 case NALPHA:
3950 if (c == NUL || ri_alpha(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003951 status = RA_NOMATCH;
3952 else
3953 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 break;
3955
3956 case LOWER:
3957 if (!ri_lower(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003958 status = RA_NOMATCH;
3959 else
3960 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 break;
3962
3963 case NLOWER:
3964 if (c == NUL || ri_lower(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003965 status = RA_NOMATCH;
3966 else
3967 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 break;
3969
3970 case UPPER:
3971 if (!ri_upper(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003972 status = RA_NOMATCH;
3973 else
3974 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 break;
3976
3977 case NUPPER:
3978 if (c == NUL || ri_upper(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003979 status = RA_NOMATCH;
3980 else
3981 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 break;
3983
3984 case EXACTLY:
3985 {
3986 int len;
3987 char_u *opnd;
3988
3989 opnd = OPERAND(scan);
3990 /* Inline the first byte, for speed. */
3991 if (*opnd != *reginput
3992 && (!ireg_ic || (
3993#ifdef FEAT_MBYTE
3994 !enc_utf8 &&
3995#endif
3996 TOLOWER_LOC(*opnd) != TOLOWER_LOC(*reginput))))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003997 status = RA_NOMATCH;
3998 else if (*opnd == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 {
4000 /* match empty string always works; happens when "~" is
4001 * empty. */
4002 }
4003 else if (opnd[1] == NUL
4004#ifdef FEAT_MBYTE
4005 && !(enc_utf8 && ireg_ic)
4006#endif
4007 )
4008 ++reginput; /* matched a single char */
4009 else
4010 {
4011 len = (int)STRLEN(opnd);
4012 /* Need to match first byte again for multi-byte. */
4013 if (cstrncmp(opnd, reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004014 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015#ifdef FEAT_MBYTE
4016 /* Check for following composing character. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004017 else if (enc_utf8
4018 && UTF_COMPOSINGLIKE(reginput, reginput + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 {
4020 /* raaron: This code makes a composing character get
4021 * ignored, which is the correct behavior (sometimes)
4022 * for voweled Hebrew texts. */
4023 if (!ireg_icombine)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004024 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004027 else
4028 reginput += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 }
4030 }
4031 break;
4032
4033 case ANYOF:
4034 case ANYBUT:
4035 if (c == NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004036 status = RA_NOMATCH;
4037 else if ((cstrchr(OPERAND(scan), c) == NULL) == (op == ANYOF))
4038 status = RA_NOMATCH;
4039 else
4040 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 break;
4042
4043#ifdef FEAT_MBYTE
4044 case MULTIBYTECODE:
4045 if (has_mbyte)
4046 {
4047 int i, len;
4048 char_u *opnd;
4049
4050 opnd = OPERAND(scan);
4051 /* Safety check (just in case 'encoding' was changed since
4052 * compiling the program). */
4053 if ((len = (*mb_ptr2len_check)(opnd)) < 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004054 {
4055 status = RA_NOMATCH;
4056 break;
4057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 for (i = 0; i < len; ++i)
4059 if (opnd[i] != reginput[i])
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004060 {
4061 status = RA_NOMATCH;
4062 break;
4063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 reginput += len;
4065 }
4066 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004067 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 break;
4069#endif
4070
4071 case NOTHING:
4072 break;
4073
4074 case BACK:
Bram Moolenaardf177f62005-02-22 08:39:57 +00004075 /* When we run into BACK without matching something non-empty, we
4076 * fail. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004077 if (startp != 0 && reg_save_equal(STARTP2REGS(startp)))
4078 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 break;
4080
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004081 case BACKP:
4082 break;
4083
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 case MOPEN + 0: /* Match start: \zs */
4085 case MOPEN + 1: /* \( */
4086 case MOPEN + 2:
4087 case MOPEN + 3:
4088 case MOPEN + 4:
4089 case MOPEN + 5:
4090 case MOPEN + 6:
4091 case MOPEN + 7:
4092 case MOPEN + 8:
4093 case MOPEN + 9:
4094 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 no = op - MOPEN;
4096 cleanup_subexpr();
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004097 rp = regstack_push(&regstack, RS_MOPEN, scan, startp);
4098 if (rp == NULL)
4099 status = RA_FAIL;
4100 else
4101 {
4102 rp->rs_no = no;
4103 save_se(&rp->rs_un.sesave, &reg_startpos[no],
4104 &reg_startp[no]);
4105 /* We simply continue and handle the result when done. */
4106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004108 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109
4110 case NOPEN: /* \%( */
4111 case NCLOSE: /* \) after \%( */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004112 if (regstack_push(&regstack, RS_NOPEN, scan, startp) == NULL)
4113 status = RA_FAIL;
4114 /* We simply continue and handle the result when done. */
4115 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116
4117#ifdef FEAT_SYN_HL
4118 case ZOPEN + 1:
4119 case ZOPEN + 2:
4120 case ZOPEN + 3:
4121 case ZOPEN + 4:
4122 case ZOPEN + 5:
4123 case ZOPEN + 6:
4124 case ZOPEN + 7:
4125 case ZOPEN + 8:
4126 case ZOPEN + 9:
4127 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 no = op - ZOPEN;
4129 cleanup_zsubexpr();
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004130 rp = regstack_push(&regstack, RS_ZOPEN, scan, startp);
4131 if (rp == NULL)
4132 status = RA_FAIL;
4133 else
4134 {
4135 rp->rs_no = no;
4136 save_se(&rp->rs_un.sesave, &reg_startzpos[no],
4137 &reg_startzp[no]);
4138 /* We simply continue and handle the result when done. */
4139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004141 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142#endif
4143
4144 case MCLOSE + 0: /* Match end: \ze */
4145 case MCLOSE + 1: /* \) */
4146 case MCLOSE + 2:
4147 case MCLOSE + 3:
4148 case MCLOSE + 4:
4149 case MCLOSE + 5:
4150 case MCLOSE + 6:
4151 case MCLOSE + 7:
4152 case MCLOSE + 8:
4153 case MCLOSE + 9:
4154 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 no = op - MCLOSE;
4156 cleanup_subexpr();
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004157 rp = regstack_push(&regstack, RS_MCLOSE, scan, startp);
4158 if (rp == NULL)
4159 status = RA_FAIL;
4160 else
4161 {
4162 rp->rs_no = no;
4163 save_se(&rp->rs_un.sesave, &reg_endpos[no], &reg_endp[no]);
4164 /* We simply continue and handle the result when done. */
4165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004167 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168
4169#ifdef FEAT_SYN_HL
4170 case ZCLOSE + 1: /* \) after \z( */
4171 case ZCLOSE + 2:
4172 case ZCLOSE + 3:
4173 case ZCLOSE + 4:
4174 case ZCLOSE + 5:
4175 case ZCLOSE + 6:
4176 case ZCLOSE + 7:
4177 case ZCLOSE + 8:
4178 case ZCLOSE + 9:
4179 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 no = op - ZCLOSE;
4181 cleanup_zsubexpr();
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004182 rp = regstack_push(&regstack, RS_ZCLOSE, scan, startp);
4183 if (rp == NULL)
4184 status = RA_FAIL;
4185 else
4186 {
4187 rp->rs_no = no;
4188 save_se(&rp->rs_un.sesave, &reg_endzpos[no],
4189 &reg_endzp[no]);
4190 /* We simply continue and handle the result when done. */
4191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004193 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194#endif
4195
4196 case BACKREF + 1:
4197 case BACKREF + 2:
4198 case BACKREF + 3:
4199 case BACKREF + 4:
4200 case BACKREF + 5:
4201 case BACKREF + 6:
4202 case BACKREF + 7:
4203 case BACKREF + 8:
4204 case BACKREF + 9:
4205 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 int len;
4207 linenr_T clnum;
4208 colnr_T ccol;
4209 char_u *p;
4210
4211 no = op - BACKREF;
4212 cleanup_subexpr();
4213 if (!REG_MULTI) /* Single-line regexp */
4214 {
4215 if (reg_endp[no] == NULL)
4216 {
4217 /* Backref was not set: Match an empty string. */
4218 len = 0;
4219 }
4220 else
4221 {
4222 /* Compare current input with back-ref in the same
4223 * line. */
4224 len = (int)(reg_endp[no] - reg_startp[no]);
4225 if (cstrncmp(reg_startp[no], reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004226 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 }
4228 }
4229 else /* Multi-line regexp */
4230 {
4231 if (reg_endpos[no].lnum < 0)
4232 {
4233 /* Backref was not set: Match an empty string. */
4234 len = 0;
4235 }
4236 else
4237 {
4238 if (reg_startpos[no].lnum == reglnum
4239 && reg_endpos[no].lnum == reglnum)
4240 {
4241 /* Compare back-ref within the current line. */
4242 len = reg_endpos[no].col - reg_startpos[no].col;
4243 if (cstrncmp(regline + reg_startpos[no].col,
4244 reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004245 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 }
4247 else
4248 {
4249 /* Messy situation: Need to compare between two
4250 * lines. */
4251 ccol = reg_startpos[no].col;
4252 clnum = reg_startpos[no].lnum;
4253 for (;;)
4254 {
4255 /* Since getting one line may invalidate
4256 * the other, need to make copy. Slow! */
4257 if (regline != reg_tofree)
4258 {
4259 len = (int)STRLEN(regline);
4260 if (reg_tofree == NULL
4261 || len >= (int)reg_tofreelen)
4262 {
4263 len += 50; /* get some extra */
4264 vim_free(reg_tofree);
4265 reg_tofree = alloc(len);
4266 if (reg_tofree == NULL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004267 {
4268 status = RA_FAIL; /* outof memory!*/
4269 break;
4270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 reg_tofreelen = len;
4272 }
4273 STRCPY(reg_tofree, regline);
4274 reginput = reg_tofree
4275 + (reginput - regline);
4276 regline = reg_tofree;
4277 }
4278
4279 /* Get the line to compare with. */
4280 p = reg_getline(clnum);
4281 if (clnum == reg_endpos[no].lnum)
4282 len = reg_endpos[no].col - ccol;
4283 else
4284 len = (int)STRLEN(p + ccol);
4285
4286 if (cstrncmp(p + ccol, reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004287 {
4288 status = RA_NOMATCH; /* doesn't match */
4289 break;
4290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 if (clnum == reg_endpos[no].lnum)
4292 break; /* match and at end! */
4293 if (reglnum == reg_maxline)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004294 {
4295 status = RA_NOMATCH; /* text too short */
4296 break;
4297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298
4299 /* Advance to next line. */
4300 reg_nextline();
4301 ++clnum;
4302 ccol = 0;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004303 if (got_int)
4304 {
4305 status = RA_FAIL;
4306 break;
4307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 }
4309
4310 /* found a match! Note that regline may now point
4311 * to a copy of the line, that should not matter. */
4312 }
4313 }
4314 }
4315
4316 /* Matched the backref, skip over it. */
4317 reginput += len;
4318 }
4319 break;
4320
4321#ifdef FEAT_SYN_HL
4322 case ZREF + 1:
4323 case ZREF + 2:
4324 case ZREF + 3:
4325 case ZREF + 4:
4326 case ZREF + 5:
4327 case ZREF + 6:
4328 case ZREF + 7:
4329 case ZREF + 8:
4330 case ZREF + 9:
4331 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 int len;
4333
4334 cleanup_zsubexpr();
4335 no = op - ZREF;
4336 if (re_extmatch_in != NULL
4337 && re_extmatch_in->matches[no] != NULL)
4338 {
4339 len = (int)STRLEN(re_extmatch_in->matches[no]);
4340 if (cstrncmp(re_extmatch_in->matches[no],
4341 reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004342 status = RA_NOMATCH;
4343 else
4344 reginput += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 }
4346 else
4347 {
4348 /* Backref was not set: Match an empty string. */
4349 }
4350 }
4351 break;
4352#endif
4353
4354 case BRANCH:
4355 {
4356 if (OP(next) != BRANCH) /* No choice. */
4357 next = OPERAND(scan); /* Avoid recursion. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004358 else if (startp != 0 && OP(OPERAND(scan)) == BACKP
4359 && reg_save_equal(STARTP2REGS(startp)))
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004360 /* \+ with something empty before it */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004361 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 else
4363 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004364 rp = regstack_push(&regstack, RS_BRANCH, scan, startp);
4365 if (rp == NULL)
4366 status = RA_FAIL;
4367 else
4368 status = RA_BREAK; /* rest is below */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 }
4370 }
4371 break;
4372
4373 case BRACE_LIMITS:
4374 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 if (OP(next) == BRACE_SIMPLE)
4376 {
4377 bl_minval = OPERAND_MIN(scan);
4378 bl_maxval = OPERAND_MAX(scan);
4379 }
4380 else if (OP(next) >= BRACE_COMPLEX
4381 && OP(next) < BRACE_COMPLEX + 10)
4382 {
4383 no = OP(next) - BRACE_COMPLEX;
4384 brace_min[no] = OPERAND_MIN(scan);
4385 brace_max[no] = OPERAND_MAX(scan);
4386 brace_count[no] = 0;
4387 }
4388 else
4389 {
4390 EMSG(_(e_internal)); /* Shouldn't happen */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004391 status = RA_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 }
4393 }
4394 break;
4395
4396 case BRACE_COMPLEX + 0:
4397 case BRACE_COMPLEX + 1:
4398 case BRACE_COMPLEX + 2:
4399 case BRACE_COMPLEX + 3:
4400 case BRACE_COMPLEX + 4:
4401 case BRACE_COMPLEX + 5:
4402 case BRACE_COMPLEX + 6:
4403 case BRACE_COMPLEX + 7:
4404 case BRACE_COMPLEX + 8:
4405 case BRACE_COMPLEX + 9:
4406 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 no = op - BRACE_COMPLEX;
4408 ++brace_count[no];
4409
4410 /* If not matched enough times yet, try one more */
4411 if (brace_count[no] <= (brace_min[no] <= brace_max[no]
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004412 ? brace_min[no] : brace_max[no]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004414 rp = regstack_push(&regstack, RS_BRCPLX_MORE, scan, startp);
4415 if (rp == NULL)
4416 status = RA_FAIL;
4417 else
4418 {
4419 rp->rs_no = no;
4420 reg_save(&rp->rs_un.regsave);
4421 startp = REGS2STARTP(&rp->rs_un.regsave);
4422 next = OPERAND(scan);
4423 /* We continue and handle the result when done. */
4424 }
4425 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 }
4427
4428 /* If matched enough times, may try matching some more */
4429 if (brace_min[no] <= brace_max[no])
4430 {
4431 /* Range is the normal way around, use longest match */
4432 if (brace_count[no] <= brace_max[no])
4433 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004434 rp = regstack_push(&regstack, RS_BRCPLX_LONG,
4435 scan, startp);
4436 if (rp == NULL)
4437 status = RA_FAIL;
4438 else
4439 {
4440 rp->rs_no = no;
4441 reg_save(&rp->rs_un.regsave);
4442 startp = REGS2STARTP(&rp->rs_un.regsave);
4443 next = OPERAND(scan);
4444 /* We continue and handle the result when done. */
4445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 }
4447 }
4448 else
4449 {
4450 /* Range is backwards, use shortest match first */
4451 if (brace_count[no] <= brace_min[no])
4452 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004453 rp = regstack_push(&regstack, RS_BRCPLX_SHORT,
4454 scan, startp);
4455 if (rp == NULL)
4456 status = RA_FAIL;
4457 else
4458 {
4459 reg_save(&rp->rs_un.regsave);
4460 startp = REGS2STARTP(&rp->rs_un.regsave);
4461 /* We continue and handle the result when done. */
4462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 }
4464 }
4465 }
4466 break;
4467
4468 case BRACE_SIMPLE:
4469 case STAR:
4470 case PLUS:
4471 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004472 regstar_T rst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473
4474 /*
4475 * Lookahead to avoid useless match attempts when we know
4476 * what character comes next.
4477 */
4478 if (OP(next) == EXACTLY)
4479 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004480 rst.nextb = *OPERAND(next);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 if (ireg_ic)
4482 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004483 if (isupper(rst.nextb))
4484 rst.nextb_ic = TOLOWER_LOC(rst.nextb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004486 rst.nextb_ic = TOUPPER_LOC(rst.nextb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 }
4488 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004489 rst.nextb_ic = rst.nextb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 }
4491 else
4492 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004493 rst.nextb = NUL;
4494 rst.nextb_ic = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 }
4496 if (op != BRACE_SIMPLE)
4497 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004498 rst.minval = (op == STAR) ? 0 : 1;
4499 rst.maxval = MAX_LIMIT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 }
4501 else
4502 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004503 rst.minval = bl_minval;
4504 rst.maxval = bl_maxval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 }
4506
4507 /*
4508 * When maxval > minval, try matching as much as possible, up
4509 * to maxval. When maxval < minval, try matching at least the
4510 * minimal number (since the range is backwards, that's also
4511 * maxval!).
4512 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004513 rst.count = regrepeat(OPERAND(scan), rst.maxval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004516 status = RA_FAIL;
4517 break;
4518 }
4519 if (rst.minval <= rst.maxval
4520 ? rst.count >= rst.minval : rst.count >= rst.maxval)
4521 {
4522 /* It could match. Prepare for trying to match what
4523 * follows. The code is below. Parameters are stored in
4524 * a regstar_T on the regstack. */
Bram Moolenaar916b7af2005-03-16 09:52:38 +00004525 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004526 {
4527 EMSG(_(e_maxmempat));
4528 status = RA_FAIL;
4529 }
4530 else if (ga_grow(&regstack, sizeof(regstar_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004531 status = RA_FAIL;
4532 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004534 regstack.ga_len += sizeof(regstar_T);
4535 rp = regstack_push(&regstack, rst.minval <= rst.maxval
4536 ? RS_STAR_LONG : RS_STAR_SHORT, scan, startp);
4537 if (rp == NULL)
4538 status = RA_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004540 {
4541 *(((regstar_T *)rp) - 1) = rst;
4542 status = RA_BREAK; /* skip the restore bits */
4543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 }
4545 }
4546 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004547 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 }
4550 break;
4551
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004552 case NOMATCH:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 case MATCH:
4554 case SUBPAT:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004555 rp = regstack_push(&regstack, RS_NOMATCH, scan, startp);
4556 if (rp == NULL)
4557 status = RA_FAIL;
4558 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004560 rp->rs_no = op;
4561 reg_save(&rp->rs_un.regsave);
4562 next = OPERAND(scan);
4563 /* We continue and handle the result when done. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 }
4565 break;
4566
4567 case BEHIND:
4568 case NOBEHIND:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004569 /* Need a bit of room to store extra positions. */
Bram Moolenaar916b7af2005-03-16 09:52:38 +00004570 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004571 {
4572 EMSG(_(e_maxmempat));
4573 status = RA_FAIL;
4574 }
4575 else if (ga_grow(&regstack, sizeof(regbehind_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004576 status = RA_FAIL;
4577 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004579 regstack.ga_len += sizeof(regbehind_T);
4580 rp = regstack_push(&regstack, RS_BEHIND1, scan, startp);
4581 if (rp == NULL)
4582 status = RA_FAIL;
4583 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004585 rp->rs_no = op;
4586 reg_save(&rp->rs_un.regsave);
4587 /* First try if what follows matches. If it does then we
4588 * check the behind match by looping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004591 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592
4593 case BHPOS:
4594 if (REG_MULTI)
4595 {
4596 if (behind_pos.rs_u.pos.col != (colnr_T)(reginput - regline)
4597 || behind_pos.rs_u.pos.lnum != reglnum)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004598 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 }
4600 else if (behind_pos.rs_u.ptr != reginput)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004601 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 break;
4603
4604 case NEWL:
4605 if ((c != NUL || reglnum == reg_maxline)
4606 && (c != '\n' || !reg_line_lbr))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004607 status = RA_NOMATCH;
4608 else if (reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 ADVANCE_REGINPUT();
4610 else
4611 reg_nextline();
4612 break;
4613
4614 case END:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004615 status = RA_MATCH; /* Success! */
4616 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617
4618 default:
4619 EMSG(_(e_re_corr));
4620#ifdef DEBUG
4621 printf("Illegal op code %d\n", op);
4622#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004623 status = RA_FAIL;
4624 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 }
4626 }
4627
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004628 /* If we can't continue sequentially, break the inner loop. */
4629 if (status != RA_CONT)
4630 break;
4631
4632 /* Continue in inner loop, advance to next item. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 scan = next;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004634
4635 } /* end of inner loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636
4637 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004638 * If there is something on the regstack execute the code for the state.
4639 * If the state is popped then loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004641 while (regstack.ga_len > 0 && status != RA_FAIL)
4642 {
4643 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len) - 1;
4644 switch (rp->rs_state)
4645 {
4646 case RS_NOPEN:
4647 /* Result is passed on as-is, simply pop the state. */
4648 regstack_pop(&regstack, &scan, &startp);
4649 break;
4650
4651 case RS_MOPEN:
4652 /* Pop the state. Restore pointers when there is no match. */
4653 if (status == RA_NOMATCH)
4654 restore_se(&rp->rs_un.sesave, &reg_startpos[rp->rs_no],
4655 &reg_startp[rp->rs_no]);
4656 regstack_pop(&regstack, &scan, &startp);
4657 break;
4658
4659#ifdef FEAT_SYN_HL
4660 case RS_ZOPEN:
4661 /* Pop the state. Restore pointers when there is no match. */
4662 if (status == RA_NOMATCH)
4663 restore_se(&rp->rs_un.sesave, &reg_startzpos[rp->rs_no],
4664 &reg_startzp[rp->rs_no]);
4665 regstack_pop(&regstack, &scan, &startp);
4666 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004668
4669 case RS_MCLOSE:
4670 /* Pop the state. Restore pointers when there is no match. */
4671 if (status == RA_NOMATCH)
4672 restore_se(&rp->rs_un.sesave, &reg_endpos[rp->rs_no],
4673 &reg_endp[rp->rs_no]);
4674 regstack_pop(&regstack, &scan, &startp);
4675 break;
4676
4677#ifdef FEAT_SYN_HL
4678 case RS_ZCLOSE:
4679 /* Pop the state. Restore pointers when there is no match. */
4680 if (status == RA_NOMATCH)
4681 restore_se(&rp->rs_un.sesave, &reg_endzpos[rp->rs_no],
4682 &reg_endzp[rp->rs_no]);
4683 regstack_pop(&regstack, &scan, &startp);
4684 break;
4685#endif
4686
4687 case RS_BRANCH:
4688 if (status == RA_MATCH)
4689 /* this branch matched, use it */
4690 regstack_pop(&regstack, &scan, &startp);
4691 else
4692 {
4693 if (status != RA_BREAK)
4694 {
4695 /* After a non-matching branch: try next one. */
4696 reg_restore(&rp->rs_un.regsave);
4697 scan = rp->rs_scan;
4698 }
4699 if (scan == NULL || OP(scan) != BRANCH)
4700 {
4701 /* no more branches, didn't find a match */
4702 status = RA_NOMATCH;
4703 regstack_pop(&regstack, &scan, &startp);
4704 }
4705 else
4706 {
4707 /* Prepare to try a branch. */
4708 rp->rs_scan = regnext(scan);
4709 reg_save(&rp->rs_un.regsave);
4710 startp = REGS2STARTP(&rp->rs_un.regsave);
4711 scan = OPERAND(scan);
4712 }
4713 }
4714 break;
4715
4716 case RS_BRCPLX_MORE:
4717 /* Pop the state. Restore pointers when there is no match. */
4718 if (status == RA_NOMATCH)
4719 {
4720 reg_restore(&rp->rs_un.regsave);
4721 --brace_count[rp->rs_no]; /* decrement match count */
4722 }
4723 regstack_pop(&regstack, &scan, &startp);
4724 break;
4725
4726 case RS_BRCPLX_LONG:
4727 /* Pop the state. Restore pointers when there is no match. */
4728 if (status == RA_NOMATCH)
4729 {
4730 /* There was no match, but we did find enough matches. */
4731 reg_restore(&rp->rs_un.regsave);
4732 --brace_count[rp->rs_no];
4733 /* continue with the items after "\{}" */
4734 status = RA_CONT;
4735 }
4736 regstack_pop(&regstack, &scan, &startp);
4737 if (status == RA_CONT)
4738 scan = regnext(scan);
4739 break;
4740
4741 case RS_BRCPLX_SHORT:
4742 /* Pop the state. Restore pointers when there is no match. */
4743 if (status == RA_NOMATCH)
4744 /* There was no match, try to match one more item. */
4745 reg_restore(&rp->rs_un.regsave);
4746 regstack_pop(&regstack, &scan, &startp);
4747 if (status == RA_NOMATCH)
4748 {
4749 scan = OPERAND(scan);
4750 status = RA_CONT;
4751 }
4752 break;
4753
4754 case RS_NOMATCH:
4755 /* Pop the state. If the operand matches for NOMATCH or
4756 * doesn't match for MATCH/SUBPAT, we fail. Otherwise backup,
4757 * except for SUBPAT, and continue with the next item. */
4758 if (status == (rp->rs_no == NOMATCH ? RA_MATCH : RA_NOMATCH))
4759 status = RA_NOMATCH;
4760 else
4761 {
4762 status = RA_CONT;
4763 if (rp->rs_no != SUBPAT)
4764 reg_restore(&rp->rs_un.regsave); /* zero-width */
4765 }
4766 regstack_pop(&regstack, &scan, &startp);
4767 if (status == RA_CONT)
4768 scan = regnext(scan);
4769 break;
4770
4771 case RS_BEHIND1:
4772 if (status == RA_NOMATCH)
4773 {
4774 regstack_pop(&regstack, &scan, &startp);
4775 regstack.ga_len -= sizeof(regbehind_T);
4776 }
4777 else
4778 {
4779 /* The stuff after BEHIND/NOBEHIND matches. Now try if
4780 * the behind part does (not) match before the current
4781 * position in the input. This must be done at every
4782 * position in the input and checking if the match ends at
4783 * the current position. */
4784
4785 /* save the position after the found match for next */
4786 reg_save(&(((regbehind_T *)rp) - 1)->save_after);
4787
4788 /* start looking for a match with operand at the current
4789 * postion. Go back one character until we find the
4790 * result, hitting the start of the line or the previous
4791 * line (for multi-line matching).
4792 * Set behind_pos to where the match should end, BHPOS
4793 * will match it. Save the current value. */
4794 (((regbehind_T *)rp) - 1)->save_behind = behind_pos;
4795 behind_pos = rp->rs_un.regsave;
4796
4797 rp->rs_state = RS_BEHIND2;
4798
4799 reg_restore(&rp->rs_un.regsave);
4800 scan = OPERAND(rp->rs_scan);
4801 }
4802 break;
4803
4804 case RS_BEHIND2:
4805 /*
4806 * Looping for BEHIND / NOBEHIND match.
4807 */
4808 if (status == RA_MATCH && reg_save_equal(&behind_pos))
4809 {
4810 /* found a match that ends where "next" started */
4811 behind_pos = (((regbehind_T *)rp) - 1)->save_behind;
4812 if (rp->rs_no == BEHIND)
4813 reg_restore(&(((regbehind_T *)rp) - 1)->save_after);
4814 else
4815 /* But we didn't want a match. */
4816 status = RA_NOMATCH;
4817 regstack_pop(&regstack, &scan, &startp);
4818 regstack.ga_len -= sizeof(regbehind_T);
4819 }
4820 else
4821 {
4822 /* No match: Go back one character. May go to previous
4823 * line once. */
4824 no = OK;
4825 if (REG_MULTI)
4826 {
4827 if (rp->rs_un.regsave.rs_u.pos.col == 0)
4828 {
4829 if (rp->rs_un.regsave.rs_u.pos.lnum
4830 < behind_pos.rs_u.pos.lnum
4831 || reg_getline(
4832 --rp->rs_un.regsave.rs_u.pos.lnum)
4833 == NULL)
4834 no = FAIL;
4835 else
4836 {
4837 reg_restore(&rp->rs_un.regsave);
4838 rp->rs_un.regsave.rs_u.pos.col =
4839 (colnr_T)STRLEN(regline);
4840 }
4841 }
4842 else
4843 --rp->rs_un.regsave.rs_u.pos.col;
4844 }
4845 else
4846 {
4847 if (rp->rs_un.regsave.rs_u.ptr == regline)
4848 no = FAIL;
4849 else
4850 --rp->rs_un.regsave.rs_u.ptr;
4851 }
4852 if (no == OK)
4853 {
4854 /* Advanced, prepare for finding match again. */
4855 reg_restore(&rp->rs_un.regsave);
4856 scan = OPERAND(rp->rs_scan);
4857 }
4858 else
4859 {
4860 /* Can't advance. For NOBEHIND that's a match. */
4861 behind_pos = (((regbehind_T *)rp) - 1)->save_behind;
4862 if (rp->rs_no == NOBEHIND)
4863 {
4864 reg_restore(&(((regbehind_T *)rp) - 1)->save_after);
4865 status = RA_MATCH;
4866 }
4867 else
4868 status = RA_NOMATCH;
4869 regstack_pop(&regstack, &scan, &startp);
4870 regstack.ga_len -= sizeof(regbehind_T);
4871 }
4872 }
4873 break;
4874
4875 case RS_STAR_LONG:
4876 case RS_STAR_SHORT:
4877 {
4878 regstar_T *rst = ((regstar_T *)rp) - 1;
4879
4880 if (status == RA_MATCH)
4881 {
4882 regstack_pop(&regstack, &scan, &startp);
4883 regstack.ga_len -= sizeof(regstar_T);
4884 break;
4885 }
4886
4887 /* Tried once already, restore input pointers. */
4888 if (status != RA_BREAK)
4889 reg_restore(&rp->rs_un.regsave);
4890
4891 /* Repeat until we found a position where it could match. */
4892 for (;;)
4893 {
4894 if (status != RA_BREAK)
4895 {
4896 /* Tried first position already, advance. */
4897 if (rp->rs_state == RS_STAR_LONG)
4898 {
4899 /* Trying for longest matc, but couldn't or didn't
4900 * match -- back up one char. */
4901 if (--rst->count < rst->minval)
4902 break;
4903 if (reginput == regline)
4904 {
4905 /* backup to last char of previous line */
4906 --reglnum;
4907 regline = reg_getline(reglnum);
4908 /* Just in case regrepeat() didn't count
4909 * right. */
4910 if (regline == NULL)
4911 break;
4912 reginput = regline + STRLEN(regline);
4913 fast_breakcheck();
4914 }
4915 else
4916 mb_ptr_back(regline, reginput);
4917 }
4918 else
4919 {
4920 /* Range is backwards, use shortest match first.
4921 * Careful: maxval and minval are exchanged!
4922 * Couldn't or didn't match: try advancing one
4923 * char. */
4924 if (rst->count == rst->minval
4925 || regrepeat(OPERAND(rp->rs_scan), 1L) == 0)
4926 break;
4927 ++rst->count;
4928 }
4929 if (got_int)
4930 break;
4931 }
4932 else
4933 status = RA_NOMATCH;
4934
4935 /* If it could match, try it. */
4936 if (rst->nextb == NUL || *reginput == rst->nextb
4937 || *reginput == rst->nextb_ic)
4938 {
4939 reg_save(&rp->rs_un.regsave);
4940 scan = regnext(rp->rs_scan);
4941 status = RA_CONT;
4942 break;
4943 }
4944 }
4945 if (status != RA_CONT)
4946 {
4947 /* Failed. */
4948 regstack_pop(&regstack, &scan, &startp);
4949 regstack.ga_len -= sizeof(regstar_T);
4950 status = RA_NOMATCH;
4951 }
4952 }
4953 break;
4954 }
4955
4956 /* If we want to continue the inner loop or didn't pop a state contine
4957 * matching loop */
4958 if (status == RA_CONT || rp == (regitem_T *)
4959 ((char *)regstack.ga_data + regstack.ga_len) - 1)
4960 break;
4961 }
4962
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004963 /* May need to continue with the inner loop, starting at "scan". */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004964 if (status == RA_CONT)
4965 continue;
4966
4967 /*
4968 * If the regstack is empty or something failed we are done.
4969 */
4970 if (regstack.ga_len == 0 || status == RA_FAIL)
4971 {
4972 ga_clear(&regstack);
4973 if (scan == NULL)
4974 {
4975 /*
4976 * We get here only if there's trouble -- normally "case END" is
4977 * the terminating point.
4978 */
4979 EMSG(_(e_re_corr));
4980#ifdef DEBUG
4981 printf("Premature EOL\n");
4982#endif
4983 }
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004984 if (status == RA_FAIL)
4985 got_int = TRUE;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004986 return (status == RA_MATCH);
4987 }
4988
4989 } /* End of loop until the regstack is empty. */
4990
4991 /* NOTREACHED */
4992}
4993
4994/*
4995 * Push an item onto the regstack.
4996 * Returns pointer to new item. Returns NULL when out of memory.
4997 */
4998 static regitem_T *
4999regstack_push(regstack, state, scan, startp)
5000 garray_T *regstack;
5001 regstate_T state;
5002 char_u *scan;
5003 long startp;
5004{
5005 regitem_T *rp;
5006
Bram Moolenaar916b7af2005-03-16 09:52:38 +00005007 if ((long)((unsigned)regstack->ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005008 {
5009 EMSG(_(e_maxmempat));
5010 return NULL;
5011 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005012 if (ga_grow(regstack, sizeof(regitem_T)) == FAIL)
5013 return NULL;
5014
5015 rp = (regitem_T *)((char *)regstack->ga_data + regstack->ga_len);
5016 rp->rs_state = state;
5017 rp->rs_scan = scan;
5018 rp->rs_startp = startp;
5019
5020 regstack->ga_len += sizeof(regitem_T);
5021 return rp;
5022}
5023
5024/*
5025 * Pop an item from the regstack.
5026 */
5027 static void
5028regstack_pop(regstack, scan, startp)
5029 garray_T *regstack;
5030 char_u **scan;
5031 long *startp;
5032{
5033 regitem_T *rp;
5034
5035 rp = (regitem_T *)((char *)regstack->ga_data + regstack->ga_len) - 1;
5036 *scan = rp->rs_scan;
5037 *startp = rp->rs_startp;
5038
5039 regstack->ga_len -= sizeof(regitem_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040}
5041
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042/*
5043 * regrepeat - repeatedly match something simple, return how many.
5044 * Advances reginput (and reglnum) to just after the matched chars.
5045 */
5046 static int
5047regrepeat(p, maxcount)
5048 char_u *p;
5049 long maxcount; /* maximum number of matches allowed */
5050{
5051 long count = 0;
5052 char_u *scan;
5053 char_u *opnd;
5054 int mask;
5055 int testval = 0;
5056
5057 scan = reginput; /* Make local copy of reginput for speed. */
5058 opnd = OPERAND(p);
5059 switch (OP(p))
5060 {
5061 case ANY:
5062 case ANY + ADD_NL:
5063 while (count < maxcount)
5064 {
5065 /* Matching anything means we continue until end-of-line (or
5066 * end-of-file for ANY + ADD_NL), only limited by maxcount. */
5067 while (*scan != NUL && count < maxcount)
5068 {
5069 ++count;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005070 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 }
5072 if (!WITH_NL(OP(p)) || reglnum == reg_maxline || count == maxcount)
5073 break;
5074 ++count; /* count the line-break */
5075 reg_nextline();
5076 scan = reginput;
5077 if (got_int)
5078 break;
5079 }
5080 break;
5081
5082 case IDENT:
5083 case IDENT + ADD_NL:
5084 testval = TRUE;
5085 /*FALLTHROUGH*/
5086 case SIDENT:
5087 case SIDENT + ADD_NL:
5088 while (count < maxcount)
5089 {
5090 if (vim_isIDc(*scan) && (testval || !VIM_ISDIGIT(*scan)))
5091 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005092 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 }
5094 else if (*scan == NUL)
5095 {
5096 if (!WITH_NL(OP(p)) || reglnum == reg_maxline)
5097 break;
5098 reg_nextline();
5099 scan = reginput;
5100 if (got_int)
5101 break;
5102 }
5103 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5104 ++scan;
5105 else
5106 break;
5107 ++count;
5108 }
5109 break;
5110
5111 case KWORD:
5112 case KWORD + ADD_NL:
5113 testval = TRUE;
5114 /*FALLTHROUGH*/
5115 case SKWORD:
5116 case SKWORD + ADD_NL:
5117 while (count < maxcount)
5118 {
5119 if (vim_iswordp(scan) && (testval || !VIM_ISDIGIT(*scan)))
5120 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005121 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 }
5123 else if (*scan == NUL)
5124 {
5125 if (!WITH_NL(OP(p)) || reglnum == reg_maxline)
5126 break;
5127 reg_nextline();
5128 scan = reginput;
5129 if (got_int)
5130 break;
5131 }
5132 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5133 ++scan;
5134 else
5135 break;
5136 ++count;
5137 }
5138 break;
5139
5140 case FNAME:
5141 case FNAME + ADD_NL:
5142 testval = TRUE;
5143 /*FALLTHROUGH*/
5144 case SFNAME:
5145 case SFNAME + ADD_NL:
5146 while (count < maxcount)
5147 {
5148 if (vim_isfilec(*scan) && (testval || !VIM_ISDIGIT(*scan)))
5149 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005150 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 }
5152 else if (*scan == NUL)
5153 {
5154 if (!WITH_NL(OP(p)) || reglnum == reg_maxline)
5155 break;
5156 reg_nextline();
5157 scan = reginput;
5158 if (got_int)
5159 break;
5160 }
5161 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5162 ++scan;
5163 else
5164 break;
5165 ++count;
5166 }
5167 break;
5168
5169 case PRINT:
5170 case PRINT + ADD_NL:
5171 testval = TRUE;
5172 /*FALLTHROUGH*/
5173 case SPRINT:
5174 case SPRINT + ADD_NL:
5175 while (count < maxcount)
5176 {
5177 if (*scan == NUL)
5178 {
5179 if (!WITH_NL(OP(p)) || reglnum == reg_maxline)
5180 break;
5181 reg_nextline();
5182 scan = reginput;
5183 if (got_int)
5184 break;
5185 }
5186 else if (ptr2cells(scan) == 1 && (testval || !VIM_ISDIGIT(*scan)))
5187 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005188 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 }
5190 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5191 ++scan;
5192 else
5193 break;
5194 ++count;
5195 }
5196 break;
5197
5198 case WHITE:
5199 case WHITE + ADD_NL:
5200 testval = mask = RI_WHITE;
5201do_class:
5202 while (count < maxcount)
5203 {
5204#ifdef FEAT_MBYTE
5205 int l;
5206#endif
5207 if (*scan == NUL)
5208 {
5209 if (!WITH_NL(OP(p)) || reglnum == reg_maxline)
5210 break;
5211 reg_nextline();
5212 scan = reginput;
5213 if (got_int)
5214 break;
5215 }
5216#ifdef FEAT_MBYTE
5217 else if (has_mbyte && (l = (*mb_ptr2len_check)(scan)) > 1)
5218 {
5219 if (testval != 0)
5220 break;
5221 scan += l;
5222 }
5223#endif
5224 else if ((class_tab[*scan] & mask) == testval)
5225 ++scan;
5226 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5227 ++scan;
5228 else
5229 break;
5230 ++count;
5231 }
5232 break;
5233
5234 case NWHITE:
5235 case NWHITE + ADD_NL:
5236 mask = RI_WHITE;
5237 goto do_class;
5238 case DIGIT:
5239 case DIGIT + ADD_NL:
5240 testval = mask = RI_DIGIT;
5241 goto do_class;
5242 case NDIGIT:
5243 case NDIGIT + ADD_NL:
5244 mask = RI_DIGIT;
5245 goto do_class;
5246 case HEX:
5247 case HEX + ADD_NL:
5248 testval = mask = RI_HEX;
5249 goto do_class;
5250 case NHEX:
5251 case NHEX + ADD_NL:
5252 mask = RI_HEX;
5253 goto do_class;
5254 case OCTAL:
5255 case OCTAL + ADD_NL:
5256 testval = mask = RI_OCTAL;
5257 goto do_class;
5258 case NOCTAL:
5259 case NOCTAL + ADD_NL:
5260 mask = RI_OCTAL;
5261 goto do_class;
5262 case WORD:
5263 case WORD + ADD_NL:
5264 testval = mask = RI_WORD;
5265 goto do_class;
5266 case NWORD:
5267 case NWORD + ADD_NL:
5268 mask = RI_WORD;
5269 goto do_class;
5270 case HEAD:
5271 case HEAD + ADD_NL:
5272 testval = mask = RI_HEAD;
5273 goto do_class;
5274 case NHEAD:
5275 case NHEAD + ADD_NL:
5276 mask = RI_HEAD;
5277 goto do_class;
5278 case ALPHA:
5279 case ALPHA + ADD_NL:
5280 testval = mask = RI_ALPHA;
5281 goto do_class;
5282 case NALPHA:
5283 case NALPHA + ADD_NL:
5284 mask = RI_ALPHA;
5285 goto do_class;
5286 case LOWER:
5287 case LOWER + ADD_NL:
5288 testval = mask = RI_LOWER;
5289 goto do_class;
5290 case NLOWER:
5291 case NLOWER + ADD_NL:
5292 mask = RI_LOWER;
5293 goto do_class;
5294 case UPPER:
5295 case UPPER + ADD_NL:
5296 testval = mask = RI_UPPER;
5297 goto do_class;
5298 case NUPPER:
5299 case NUPPER + ADD_NL:
5300 mask = RI_UPPER;
5301 goto do_class;
5302
5303 case EXACTLY:
5304 {
5305 int cu, cl;
5306
5307 /* This doesn't do a multi-byte character, because a MULTIBYTECODE
5308 * would have been used for it. */
5309 if (ireg_ic)
5310 {
5311 cu = TOUPPER_LOC(*opnd);
5312 cl = TOLOWER_LOC(*opnd);
5313 while (count < maxcount && (*scan == cu || *scan == cl))
5314 {
5315 count++;
5316 scan++;
5317 }
5318 }
5319 else
5320 {
5321 cu = *opnd;
5322 while (count < maxcount && *scan == cu)
5323 {
5324 count++;
5325 scan++;
5326 }
5327 }
5328 break;
5329 }
5330
5331#ifdef FEAT_MBYTE
5332 case MULTIBYTECODE:
5333 {
5334 int i, len, cf = 0;
5335
5336 /* Safety check (just in case 'encoding' was changed since
5337 * compiling the program). */
5338 if ((len = (*mb_ptr2len_check)(opnd)) > 1)
5339 {
5340 if (ireg_ic && enc_utf8)
5341 cf = utf_fold(utf_ptr2char(opnd));
5342 while (count < maxcount)
5343 {
5344 for (i = 0; i < len; ++i)
5345 if (opnd[i] != scan[i])
5346 break;
5347 if (i < len && (!ireg_ic || !enc_utf8
5348 || utf_fold(utf_ptr2char(scan)) != cf))
5349 break;
5350 scan += len;
5351 ++count;
5352 }
5353 }
5354 }
5355 break;
5356#endif
5357
5358 case ANYOF:
5359 case ANYOF + ADD_NL:
5360 testval = TRUE;
5361 /*FALLTHROUGH*/
5362
5363 case ANYBUT:
5364 case ANYBUT + ADD_NL:
5365 while (count < maxcount)
5366 {
5367#ifdef FEAT_MBYTE
5368 int len;
5369#endif
5370 if (*scan == NUL)
5371 {
5372 if (!WITH_NL(OP(p)) || reglnum == reg_maxline)
5373 break;
5374 reg_nextline();
5375 scan = reginput;
5376 if (got_int)
5377 break;
5378 }
5379 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5380 ++scan;
5381#ifdef FEAT_MBYTE
5382 else if (has_mbyte && (len = (*mb_ptr2len_check)(scan)) > 1)
5383 {
5384 if ((cstrchr(opnd, (*mb_ptr2char)(scan)) == NULL) == testval)
5385 break;
5386 scan += len;
5387 }
5388#endif
5389 else
5390 {
5391 if ((cstrchr(opnd, *scan) == NULL) == testval)
5392 break;
5393 ++scan;
5394 }
5395 ++count;
5396 }
5397 break;
5398
5399 case NEWL:
5400 while (count < maxcount
5401 && ((*scan == NUL && reglnum < reg_maxline)
5402 || (*scan == '\n' && reg_line_lbr)))
5403 {
5404 count++;
5405 if (reg_line_lbr)
5406 ADVANCE_REGINPUT();
5407 else
5408 reg_nextline();
5409 scan = reginput;
5410 if (got_int)
5411 break;
5412 }
5413 break;
5414
5415 default: /* Oh dear. Called inappropriately. */
5416 EMSG(_(e_re_corr));
5417#ifdef DEBUG
5418 printf("Called regrepeat with op code %d\n", OP(p));
5419#endif
5420 break;
5421 }
5422
5423 reginput = scan;
5424
5425 return (int)count;
5426}
5427
5428/*
5429 * regnext - dig the "next" pointer out of a node
5430 */
5431 static char_u *
5432regnext(p)
5433 char_u *p;
5434{
5435 int offset;
5436
5437 if (p == JUST_CALC_SIZE)
5438 return NULL;
5439
5440 offset = NEXT(p);
5441 if (offset == 0)
5442 return NULL;
5443
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005444 if (OP(p) == BACK || OP(p) == BACKP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 return p - offset;
5446 else
5447 return p + offset;
5448}
5449
5450/*
5451 * Check the regexp program for its magic number.
5452 * Return TRUE if it's wrong.
5453 */
5454 static int
5455prog_magic_wrong()
5456{
5457 if (UCHARAT(REG_MULTI
5458 ? reg_mmatch->regprog->program
5459 : reg_match->regprog->program) != REGMAGIC)
5460 {
5461 EMSG(_(e_re_corr));
5462 return TRUE;
5463 }
5464 return FALSE;
5465}
5466
5467/*
5468 * Cleanup the subexpressions, if this wasn't done yet.
5469 * This construction is used to clear the subexpressions only when they are
5470 * used (to increase speed).
5471 */
5472 static void
5473cleanup_subexpr()
5474{
5475 if (need_clear_subexpr)
5476 {
5477 if (REG_MULTI)
5478 {
5479 /* Use 0xff to set lnum to -1 */
5480 vim_memset(reg_startpos, 0xff, sizeof(lpos_T) * NSUBEXP);
5481 vim_memset(reg_endpos, 0xff, sizeof(lpos_T) * NSUBEXP);
5482 }
5483 else
5484 {
5485 vim_memset(reg_startp, 0, sizeof(char_u *) * NSUBEXP);
5486 vim_memset(reg_endp, 0, sizeof(char_u *) * NSUBEXP);
5487 }
5488 need_clear_subexpr = FALSE;
5489 }
5490}
5491
5492#ifdef FEAT_SYN_HL
5493 static void
5494cleanup_zsubexpr()
5495{
5496 if (need_clear_zsubexpr)
5497 {
5498 if (REG_MULTI)
5499 {
5500 /* Use 0xff to set lnum to -1 */
5501 vim_memset(reg_startzpos, 0xff, sizeof(lpos_T) * NSUBEXP);
5502 vim_memset(reg_endzpos, 0xff, sizeof(lpos_T) * NSUBEXP);
5503 }
5504 else
5505 {
5506 vim_memset(reg_startzp, 0, sizeof(char_u *) * NSUBEXP);
5507 vim_memset(reg_endzp, 0, sizeof(char_u *) * NSUBEXP);
5508 }
5509 need_clear_zsubexpr = FALSE;
5510 }
5511}
5512#endif
5513
5514/*
5515 * Advance reglnum, regline and reginput to the next line.
5516 */
5517 static void
5518reg_nextline()
5519{
5520 regline = reg_getline(++reglnum);
5521 reginput = regline;
5522 fast_breakcheck();
5523}
5524
5525/*
5526 * Save the input line and position in a regsave_T.
5527 */
5528 static void
5529reg_save(save)
5530 regsave_T *save;
5531{
5532 if (REG_MULTI)
5533 {
5534 save->rs_u.pos.col = (colnr_T)(reginput - regline);
5535 save->rs_u.pos.lnum = reglnum;
5536 }
5537 else
5538 save->rs_u.ptr = reginput;
5539}
5540
5541/*
5542 * Restore the input line and position from a regsave_T.
5543 */
5544 static void
5545reg_restore(save)
5546 regsave_T *save;
5547{
5548 if (REG_MULTI)
5549 {
5550 if (reglnum != save->rs_u.pos.lnum)
5551 {
5552 /* only call reg_getline() when the line number changed to save
5553 * a bit of time */
5554 reglnum = save->rs_u.pos.lnum;
5555 regline = reg_getline(reglnum);
5556 }
5557 reginput = regline + save->rs_u.pos.col;
5558 }
5559 else
5560 reginput = save->rs_u.ptr;
5561}
5562
5563/*
5564 * Return TRUE if current position is equal to saved position.
5565 */
5566 static int
5567reg_save_equal(save)
5568 regsave_T *save;
5569{
5570 if (REG_MULTI)
5571 return reglnum == save->rs_u.pos.lnum
5572 && reginput == regline + save->rs_u.pos.col;
5573 return reginput == save->rs_u.ptr;
5574}
5575
5576/*
5577 * Tentatively set the sub-expression start to the current position (after
5578 * calling regmatch() they will have changed). Need to save the existing
5579 * values for when there is no match.
5580 * Use se_save() to use pointer (save_se_multi()) or position (save_se_one()),
5581 * depending on REG_MULTI.
5582 */
5583 static void
5584save_se_multi(savep, posp)
5585 save_se_T *savep;
5586 lpos_T *posp;
5587{
5588 savep->se_u.pos = *posp;
5589 posp->lnum = reglnum;
5590 posp->col = (colnr_T)(reginput - regline);
5591}
5592
5593 static void
5594save_se_one(savep, pp)
5595 save_se_T *savep;
5596 char_u **pp;
5597{
5598 savep->se_u.ptr = *pp;
5599 *pp = reginput;
5600}
5601
5602/*
5603 * Compare a number with the operand of RE_LNUM, RE_COL or RE_VCOL.
5604 */
5605 static int
5606re_num_cmp(val, scan)
5607 long_u val;
5608 char_u *scan;
5609{
5610 long_u n = OPERAND_MIN(scan);
5611
5612 if (OPERAND_CMP(scan) == '>')
5613 return val > n;
5614 if (OPERAND_CMP(scan) == '<')
5615 return val < n;
5616 return val == n;
5617}
5618
5619
5620#ifdef DEBUG
5621
5622/*
5623 * regdump - dump a regexp onto stdout in vaguely comprehensible form
5624 */
5625 static void
5626regdump(pattern, r)
5627 char_u *pattern;
5628 regprog_T *r;
5629{
5630 char_u *s;
5631 int op = EXACTLY; /* Arbitrary non-END op. */
5632 char_u *next;
5633 char_u *end = NULL;
5634
5635 printf("\r\nregcomp(%s):\r\n", pattern);
5636
5637 s = r->program + 1;
5638 /*
5639 * Loop until we find the END that isn't before a referred next (an END
5640 * can also appear in a NOMATCH operand).
5641 */
5642 while (op != END || s <= end)
5643 {
5644 op = OP(s);
5645 printf("%2d%s", (int)(s - r->program), regprop(s)); /* Where, what. */
5646 next = regnext(s);
5647 if (next == NULL) /* Next ptr. */
5648 printf("(0)");
5649 else
5650 printf("(%d)", (int)((s - r->program) + (next - s)));
5651 if (end < next)
5652 end = next;
5653 if (op == BRACE_LIMITS)
5654 {
5655 /* Two short ints */
5656 printf(" minval %ld, maxval %ld", OPERAND_MIN(s), OPERAND_MAX(s));
5657 s += 8;
5658 }
5659 s += 3;
5660 if (op == ANYOF || op == ANYOF + ADD_NL
5661 || op == ANYBUT || op == ANYBUT + ADD_NL
5662 || op == EXACTLY)
5663 {
5664 /* Literal string, where present. */
5665 while (*s != NUL)
5666 printf("%c", *s++);
5667 s++;
5668 }
5669 printf("\r\n");
5670 }
5671
5672 /* Header fields of interest. */
5673 if (r->regstart != NUL)
5674 printf("start `%s' 0x%x; ", r->regstart < 256
5675 ? (char *)transchar(r->regstart)
5676 : "multibyte", r->regstart);
5677 if (r->reganch)
5678 printf("anchored; ");
5679 if (r->regmust != NULL)
5680 printf("must have \"%s\"", r->regmust);
5681 printf("\r\n");
5682}
5683
5684/*
5685 * regprop - printable representation of opcode
5686 */
5687 static char_u *
5688regprop(op)
5689 char_u *op;
5690{
5691 char_u *p;
5692 static char_u buf[50];
5693
5694 (void) strcpy(buf, ":");
5695
5696 switch (OP(op))
5697 {
5698 case BOL:
5699 p = "BOL";
5700 break;
5701 case EOL:
5702 p = "EOL";
5703 break;
5704 case RE_BOF:
5705 p = "BOF";
5706 break;
5707 case RE_EOF:
5708 p = "EOF";
5709 break;
5710 case CURSOR:
5711 p = "CURSOR";
5712 break;
5713 case RE_LNUM:
5714 p = "RE_LNUM";
5715 break;
5716 case RE_COL:
5717 p = "RE_COL";
5718 break;
5719 case RE_VCOL:
5720 p = "RE_VCOL";
5721 break;
5722 case BOW:
5723 p = "BOW";
5724 break;
5725 case EOW:
5726 p = "EOW";
5727 break;
5728 case ANY:
5729 p = "ANY";
5730 break;
5731 case ANY + ADD_NL:
5732 p = "ANY+NL";
5733 break;
5734 case ANYOF:
5735 p = "ANYOF";
5736 break;
5737 case ANYOF + ADD_NL:
5738 p = "ANYOF+NL";
5739 break;
5740 case ANYBUT:
5741 p = "ANYBUT";
5742 break;
5743 case ANYBUT + ADD_NL:
5744 p = "ANYBUT+NL";
5745 break;
5746 case IDENT:
5747 p = "IDENT";
5748 break;
5749 case IDENT + ADD_NL:
5750 p = "IDENT+NL";
5751 break;
5752 case SIDENT:
5753 p = "SIDENT";
5754 break;
5755 case SIDENT + ADD_NL:
5756 p = "SIDENT+NL";
5757 break;
5758 case KWORD:
5759 p = "KWORD";
5760 break;
5761 case KWORD + ADD_NL:
5762 p = "KWORD+NL";
5763 break;
5764 case SKWORD:
5765 p = "SKWORD";
5766 break;
5767 case SKWORD + ADD_NL:
5768 p = "SKWORD+NL";
5769 break;
5770 case FNAME:
5771 p = "FNAME";
5772 break;
5773 case FNAME + ADD_NL:
5774 p = "FNAME+NL";
5775 break;
5776 case SFNAME:
5777 p = "SFNAME";
5778 break;
5779 case SFNAME + ADD_NL:
5780 p = "SFNAME+NL";
5781 break;
5782 case PRINT:
5783 p = "PRINT";
5784 break;
5785 case PRINT + ADD_NL:
5786 p = "PRINT+NL";
5787 break;
5788 case SPRINT:
5789 p = "SPRINT";
5790 break;
5791 case SPRINT + ADD_NL:
5792 p = "SPRINT+NL";
5793 break;
5794 case WHITE:
5795 p = "WHITE";
5796 break;
5797 case WHITE + ADD_NL:
5798 p = "WHITE+NL";
5799 break;
5800 case NWHITE:
5801 p = "NWHITE";
5802 break;
5803 case NWHITE + ADD_NL:
5804 p = "NWHITE+NL";
5805 break;
5806 case DIGIT:
5807 p = "DIGIT";
5808 break;
5809 case DIGIT + ADD_NL:
5810 p = "DIGIT+NL";
5811 break;
5812 case NDIGIT:
5813 p = "NDIGIT";
5814 break;
5815 case NDIGIT + ADD_NL:
5816 p = "NDIGIT+NL";
5817 break;
5818 case HEX:
5819 p = "HEX";
5820 break;
5821 case HEX + ADD_NL:
5822 p = "HEX+NL";
5823 break;
5824 case NHEX:
5825 p = "NHEX";
5826 break;
5827 case NHEX + ADD_NL:
5828 p = "NHEX+NL";
5829 break;
5830 case OCTAL:
5831 p = "OCTAL";
5832 break;
5833 case OCTAL + ADD_NL:
5834 p = "OCTAL+NL";
5835 break;
5836 case NOCTAL:
5837 p = "NOCTAL";
5838 break;
5839 case NOCTAL + ADD_NL:
5840 p = "NOCTAL+NL";
5841 break;
5842 case WORD:
5843 p = "WORD";
5844 break;
5845 case WORD + ADD_NL:
5846 p = "WORD+NL";
5847 break;
5848 case NWORD:
5849 p = "NWORD";
5850 break;
5851 case NWORD + ADD_NL:
5852 p = "NWORD+NL";
5853 break;
5854 case HEAD:
5855 p = "HEAD";
5856 break;
5857 case HEAD + ADD_NL:
5858 p = "HEAD+NL";
5859 break;
5860 case NHEAD:
5861 p = "NHEAD";
5862 break;
5863 case NHEAD + ADD_NL:
5864 p = "NHEAD+NL";
5865 break;
5866 case ALPHA:
5867 p = "ALPHA";
5868 break;
5869 case ALPHA + ADD_NL:
5870 p = "ALPHA+NL";
5871 break;
5872 case NALPHA:
5873 p = "NALPHA";
5874 break;
5875 case NALPHA + ADD_NL:
5876 p = "NALPHA+NL";
5877 break;
5878 case LOWER:
5879 p = "LOWER";
5880 break;
5881 case LOWER + ADD_NL:
5882 p = "LOWER+NL";
5883 break;
5884 case NLOWER:
5885 p = "NLOWER";
5886 break;
5887 case NLOWER + ADD_NL:
5888 p = "NLOWER+NL";
5889 break;
5890 case UPPER:
5891 p = "UPPER";
5892 break;
5893 case UPPER + ADD_NL:
5894 p = "UPPER+NL";
5895 break;
5896 case NUPPER:
5897 p = "NUPPER";
5898 break;
5899 case NUPPER + ADD_NL:
5900 p = "NUPPER+NL";
5901 break;
5902 case BRANCH:
5903 p = "BRANCH";
5904 break;
5905 case EXACTLY:
5906 p = "EXACTLY";
5907 break;
5908 case NOTHING:
5909 p = "NOTHING";
5910 break;
5911 case BACK:
5912 p = "BACK";
5913 break;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00005914 case BACKP:
5915 p = "BACKP";
5916 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 case END:
5918 p = "END";
5919 break;
5920 case MOPEN + 0:
5921 p = "MATCH START";
5922 break;
5923 case MOPEN + 1:
5924 case MOPEN + 2:
5925 case MOPEN + 3:
5926 case MOPEN + 4:
5927 case MOPEN + 5:
5928 case MOPEN + 6:
5929 case MOPEN + 7:
5930 case MOPEN + 8:
5931 case MOPEN + 9:
5932 sprintf(buf + STRLEN(buf), "MOPEN%d", OP(op) - MOPEN);
5933 p = NULL;
5934 break;
5935 case MCLOSE + 0:
5936 p = "MATCH END";
5937 break;
5938 case MCLOSE + 1:
5939 case MCLOSE + 2:
5940 case MCLOSE + 3:
5941 case MCLOSE + 4:
5942 case MCLOSE + 5:
5943 case MCLOSE + 6:
5944 case MCLOSE + 7:
5945 case MCLOSE + 8:
5946 case MCLOSE + 9:
5947 sprintf(buf + STRLEN(buf), "MCLOSE%d", OP(op) - MCLOSE);
5948 p = NULL;
5949 break;
5950 case BACKREF + 1:
5951 case BACKREF + 2:
5952 case BACKREF + 3:
5953 case BACKREF + 4:
5954 case BACKREF + 5:
5955 case BACKREF + 6:
5956 case BACKREF + 7:
5957 case BACKREF + 8:
5958 case BACKREF + 9:
5959 sprintf(buf + STRLEN(buf), "BACKREF%d", OP(op) - BACKREF);
5960 p = NULL;
5961 break;
5962 case NOPEN:
5963 p = "NOPEN";
5964 break;
5965 case NCLOSE:
5966 p = "NCLOSE";
5967 break;
5968#ifdef FEAT_SYN_HL
5969 case ZOPEN + 1:
5970 case ZOPEN + 2:
5971 case ZOPEN + 3:
5972 case ZOPEN + 4:
5973 case ZOPEN + 5:
5974 case ZOPEN + 6:
5975 case ZOPEN + 7:
5976 case ZOPEN + 8:
5977 case ZOPEN + 9:
5978 sprintf(buf + STRLEN(buf), "ZOPEN%d", OP(op) - ZOPEN);
5979 p = NULL;
5980 break;
5981 case ZCLOSE + 1:
5982 case ZCLOSE + 2:
5983 case ZCLOSE + 3:
5984 case ZCLOSE + 4:
5985 case ZCLOSE + 5:
5986 case ZCLOSE + 6:
5987 case ZCLOSE + 7:
5988 case ZCLOSE + 8:
5989 case ZCLOSE + 9:
5990 sprintf(buf + STRLEN(buf), "ZCLOSE%d", OP(op) - ZCLOSE);
5991 p = NULL;
5992 break;
5993 case ZREF + 1:
5994 case ZREF + 2:
5995 case ZREF + 3:
5996 case ZREF + 4:
5997 case ZREF + 5:
5998 case ZREF + 6:
5999 case ZREF + 7:
6000 case ZREF + 8:
6001 case ZREF + 9:
6002 sprintf(buf + STRLEN(buf), "ZREF%d", OP(op) - ZREF);
6003 p = NULL;
6004 break;
6005#endif
6006 case STAR:
6007 p = "STAR";
6008 break;
6009 case PLUS:
6010 p = "PLUS";
6011 break;
6012 case NOMATCH:
6013 p = "NOMATCH";
6014 break;
6015 case MATCH:
6016 p = "MATCH";
6017 break;
6018 case BEHIND:
6019 p = "BEHIND";
6020 break;
6021 case NOBEHIND:
6022 p = "NOBEHIND";
6023 break;
6024 case SUBPAT:
6025 p = "SUBPAT";
6026 break;
6027 case BRACE_LIMITS:
6028 p = "BRACE_LIMITS";
6029 break;
6030 case BRACE_SIMPLE:
6031 p = "BRACE_SIMPLE";
6032 break;
6033 case BRACE_COMPLEX + 0:
6034 case BRACE_COMPLEX + 1:
6035 case BRACE_COMPLEX + 2:
6036 case BRACE_COMPLEX + 3:
6037 case BRACE_COMPLEX + 4:
6038 case BRACE_COMPLEX + 5:
6039 case BRACE_COMPLEX + 6:
6040 case BRACE_COMPLEX + 7:
6041 case BRACE_COMPLEX + 8:
6042 case BRACE_COMPLEX + 9:
6043 sprintf(buf + STRLEN(buf), "BRACE_COMPLEX%d", OP(op) - BRACE_COMPLEX);
6044 p = NULL;
6045 break;
6046#ifdef FEAT_MBYTE
6047 case MULTIBYTECODE:
6048 p = "MULTIBYTECODE";
6049 break;
6050#endif
6051 case NEWL:
6052 p = "NEWL";
6053 break;
6054 default:
6055 sprintf(buf + STRLEN(buf), "corrupt %d", OP(op));
6056 p = NULL;
6057 break;
6058 }
6059 if (p != NULL)
6060 (void) strcat(buf, p);
6061 return buf;
6062}
6063#endif
6064
6065#ifdef FEAT_MBYTE
6066static void mb_decompose __ARGS((int c, int *c1, int *c2, int *c3));
6067
6068typedef struct
6069{
6070 int a, b, c;
6071} decomp_T;
6072
6073
6074/* 0xfb20 - 0xfb4f */
6075decomp_T decomp_table[0xfb4f-0xfb20+1] =
6076{
6077 {0x5e2,0,0}, /* 0xfb20 alt ayin */
6078 {0x5d0,0,0}, /* 0xfb21 alt alef */
6079 {0x5d3,0,0}, /* 0xfb22 alt dalet */
6080 {0x5d4,0,0}, /* 0xfb23 alt he */
6081 {0x5db,0,0}, /* 0xfb24 alt kaf */
6082 {0x5dc,0,0}, /* 0xfb25 alt lamed */
6083 {0x5dd,0,0}, /* 0xfb26 alt mem-sofit */
6084 {0x5e8,0,0}, /* 0xfb27 alt resh */
6085 {0x5ea,0,0}, /* 0xfb28 alt tav */
6086 {'+', 0, 0}, /* 0xfb29 alt plus */
6087 {0x5e9, 0x5c1, 0}, /* 0xfb2a shin+shin-dot */
6088 {0x5e9, 0x5c2, 0}, /* 0xfb2b shin+sin-dot */
6089 {0x5e9, 0x5c1, 0x5bc}, /* 0xfb2c shin+shin-dot+dagesh */
6090 {0x5e9, 0x5c2, 0x5bc}, /* 0xfb2d shin+sin-dot+dagesh */
6091 {0x5d0, 0x5b7, 0}, /* 0xfb2e alef+patah */
6092 {0x5d0, 0x5b8, 0}, /* 0xfb2f alef+qamats */
6093 {0x5d0, 0x5b4, 0}, /* 0xfb30 alef+hiriq */
6094 {0x5d1, 0x5bc, 0}, /* 0xfb31 bet+dagesh */
6095 {0x5d2, 0x5bc, 0}, /* 0xfb32 gimel+dagesh */
6096 {0x5d3, 0x5bc, 0}, /* 0xfb33 dalet+dagesh */
6097 {0x5d4, 0x5bc, 0}, /* 0xfb34 he+dagesh */
6098 {0x5d5, 0x5bc, 0}, /* 0xfb35 vav+dagesh */
6099 {0x5d6, 0x5bc, 0}, /* 0xfb36 zayin+dagesh */
6100 {0xfb37, 0, 0}, /* 0xfb37 -- UNUSED */
6101 {0x5d8, 0x5bc, 0}, /* 0xfb38 tet+dagesh */
6102 {0x5d9, 0x5bc, 0}, /* 0xfb39 yud+dagesh */
6103 {0x5da, 0x5bc, 0}, /* 0xfb3a kaf sofit+dagesh */
6104 {0x5db, 0x5bc, 0}, /* 0xfb3b kaf+dagesh */
6105 {0x5dc, 0x5bc, 0}, /* 0xfb3c lamed+dagesh */
6106 {0xfb3d, 0, 0}, /* 0xfb3d -- UNUSED */
6107 {0x5de, 0x5bc, 0}, /* 0xfb3e mem+dagesh */
6108 {0xfb3f, 0, 0}, /* 0xfb3f -- UNUSED */
6109 {0x5e0, 0x5bc, 0}, /* 0xfb40 nun+dagesh */
6110 {0x5e1, 0x5bc, 0}, /* 0xfb41 samech+dagesh */
6111 {0xfb42, 0, 0}, /* 0xfb42 -- UNUSED */
6112 {0x5e3, 0x5bc, 0}, /* 0xfb43 pe sofit+dagesh */
6113 {0x5e4, 0x5bc,0}, /* 0xfb44 pe+dagesh */
6114 {0xfb45, 0, 0}, /* 0xfb45 -- UNUSED */
6115 {0x5e6, 0x5bc, 0}, /* 0xfb46 tsadi+dagesh */
6116 {0x5e7, 0x5bc, 0}, /* 0xfb47 qof+dagesh */
6117 {0x5e8, 0x5bc, 0}, /* 0xfb48 resh+dagesh */
6118 {0x5e9, 0x5bc, 0}, /* 0xfb49 shin+dagesh */
6119 {0x5ea, 0x5bc, 0}, /* 0xfb4a tav+dagesh */
6120 {0x5d5, 0x5b9, 0}, /* 0xfb4b vav+holam */
6121 {0x5d1, 0x5bf, 0}, /* 0xfb4c bet+rafe */
6122 {0x5db, 0x5bf, 0}, /* 0xfb4d kaf+rafe */
6123 {0x5e4, 0x5bf, 0}, /* 0xfb4e pe+rafe */
6124 {0x5d0, 0x5dc, 0} /* 0xfb4f alef-lamed */
6125};
6126
6127 static void
6128mb_decompose(c, c1, c2, c3)
6129 int c, *c1, *c2, *c3;
6130{
6131 decomp_T d;
6132
6133 if (c >= 0x4b20 && c <= 0xfb4f)
6134 {
6135 d = decomp_table[c - 0xfb20];
6136 *c1 = d.a;
6137 *c2 = d.b;
6138 *c3 = d.c;
6139 }
6140 else
6141 {
6142 *c1 = c;
6143 *c2 = *c3 = 0;
6144 }
6145}
6146#endif
6147
6148/*
6149 * Compare two strings, ignore case if ireg_ic set.
6150 * Return 0 if strings match, non-zero otherwise.
6151 * Correct the length "*n" when composing characters are ignored.
6152 */
6153 static int
6154cstrncmp(s1, s2, n)
6155 char_u *s1, *s2;
6156 int *n;
6157{
6158 int result;
6159
6160 if (!ireg_ic)
6161 result = STRNCMP(s1, s2, *n);
6162 else
6163 result = MB_STRNICMP(s1, s2, *n);
6164
6165#ifdef FEAT_MBYTE
6166 /* if it failed and it's utf8 and we want to combineignore: */
6167 if (result != 0 && enc_utf8 && ireg_icombine)
6168 {
6169 char_u *str1, *str2;
6170 int c1, c2, c11, c12;
6171 int ix;
6172 int junk;
6173
6174 /* we have to handle the strcmp ourselves, since it is necessary to
6175 * deal with the composing characters by ignoring them: */
6176 str1 = s1;
6177 str2 = s2;
6178 c1 = c2 = 0;
6179 for (ix = 0; ix < *n; )
6180 {
6181 c1 = mb_ptr2char_adv(&str1);
6182 c2 = mb_ptr2char_adv(&str2);
6183 ix += utf_char2len(c1);
6184
6185 /* decompose the character if necessary, into 'base' characters
6186 * because I don't care about Arabic, I will hard-code the Hebrew
6187 * which I *do* care about! So sue me... */
6188 if (c1 != c2 && (!ireg_ic || utf_fold(c1) != utf_fold(c2)))
6189 {
6190 /* decomposition necessary? */
6191 mb_decompose(c1, &c11, &junk, &junk);
6192 mb_decompose(c2, &c12, &junk, &junk);
6193 c1 = c11;
6194 c2 = c12;
6195 if (c11 != c12 && (!ireg_ic || utf_fold(c11) != utf_fold(c12)))
6196 break;
6197 }
6198 }
6199 result = c2 - c1;
6200 if (result == 0)
6201 *n = (int)(str2 - s2);
6202 }
6203#endif
6204
6205 return result;
6206}
6207
6208/*
6209 * cstrchr: This function is used a lot for simple searches, keep it fast!
6210 */
6211 static char_u *
6212cstrchr(s, c)
6213 char_u *s;
6214 int c;
6215{
6216 char_u *p;
6217 int cc;
6218
6219 if (!ireg_ic
6220#ifdef FEAT_MBYTE
6221 || (!enc_utf8 && mb_char2len(c) > 1)
6222#endif
6223 )
6224 return vim_strchr(s, c);
6225
6226 /* tolower() and toupper() can be slow, comparing twice should be a lot
6227 * faster (esp. when using MS Visual C++!).
6228 * For UTF-8 need to use folded case. */
6229#ifdef FEAT_MBYTE
6230 if (enc_utf8 && c > 0x80)
6231 cc = utf_fold(c);
6232 else
6233#endif
6234 if (isupper(c))
6235 cc = TOLOWER_LOC(c);
6236 else if (islower(c))
6237 cc = TOUPPER_LOC(c);
6238 else
6239 return vim_strchr(s, c);
6240
6241#ifdef FEAT_MBYTE
6242 if (has_mbyte)
6243 {
6244 for (p = s; *p != NUL; p += (*mb_ptr2len_check)(p))
6245 {
6246 if (enc_utf8 && c > 0x80)
6247 {
6248 if (utf_fold(utf_ptr2char(p)) == cc)
6249 return p;
6250 }
6251 else if (*p == c || *p == cc)
6252 return p;
6253 }
6254 }
6255 else
6256#endif
6257 /* Faster version for when there are no multi-byte characters. */
6258 for (p = s; *p != NUL; ++p)
6259 if (*p == c || *p == cc)
6260 return p;
6261
6262 return NULL;
6263}
6264
6265/***************************************************************
6266 * regsub stuff *
6267 ***************************************************************/
6268
6269/* This stuff below really confuses cc on an SGI -- webb */
6270#ifdef __sgi
6271# undef __ARGS
6272# define __ARGS(x) ()
6273#endif
6274
6275/*
6276 * We should define ftpr as a pointer to a function returning a pointer to
6277 * a function returning a pointer to a function ...
6278 * This is impossible, so we declare a pointer to a function returning a
6279 * pointer to a function returning void. This should work for all compilers.
6280 */
6281typedef void (*(*fptr) __ARGS((char_u *, int)))();
6282
6283static fptr do_upper __ARGS((char_u *, int));
6284static fptr do_Upper __ARGS((char_u *, int));
6285static fptr do_lower __ARGS((char_u *, int));
6286static fptr do_Lower __ARGS((char_u *, int));
6287
6288static int vim_regsub_both __ARGS((char_u *source, char_u *dest, int copy, int magic, int backslash));
6289
6290 static fptr
6291do_upper(d, c)
6292 char_u *d;
6293 int c;
6294{
6295 *d = TOUPPER_LOC(c);
6296
6297 return (fptr)NULL;
6298}
6299
6300 static fptr
6301do_Upper(d, c)
6302 char_u *d;
6303 int c;
6304{
6305 *d = TOUPPER_LOC(c);
6306
6307 return (fptr)do_Upper;
6308}
6309
6310 static fptr
6311do_lower(d, c)
6312 char_u *d;
6313 int c;
6314{
6315 *d = TOLOWER_LOC(c);
6316
6317 return (fptr)NULL;
6318}
6319
6320 static fptr
6321do_Lower(d, c)
6322 char_u *d;
6323 int c;
6324{
6325 *d = TOLOWER_LOC(c);
6326
6327 return (fptr)do_Lower;
6328}
6329
6330/*
6331 * regtilde(): Replace tildes in the pattern by the old pattern.
6332 *
6333 * Short explanation of the tilde: It stands for the previous replacement
6334 * pattern. If that previous pattern also contains a ~ we should go back a
6335 * step further... But we insert the previous pattern into the current one
6336 * and remember that.
6337 * This still does not handle the case where "magic" changes. TODO?
6338 *
6339 * The tildes are parsed once before the first call to vim_regsub().
6340 */
6341 char_u *
6342regtilde(source, magic)
6343 char_u *source;
6344 int magic;
6345{
6346 char_u *newsub = source;
6347 char_u *tmpsub;
6348 char_u *p;
6349 int len;
6350 int prevlen;
6351
6352 for (p = newsub; *p; ++p)
6353 {
6354 if ((*p == '~' && magic) || (*p == '\\' && *(p + 1) == '~' && !magic))
6355 {
6356 if (reg_prev_sub != NULL)
6357 {
6358 /* length = len(newsub) - 1 + len(prev_sub) + 1 */
6359 prevlen = (int)STRLEN(reg_prev_sub);
6360 tmpsub = alloc((unsigned)(STRLEN(newsub) + prevlen));
6361 if (tmpsub != NULL)
6362 {
6363 /* copy prefix */
6364 len = (int)(p - newsub); /* not including ~ */
6365 mch_memmove(tmpsub, newsub, (size_t)len);
6366 /* interpretate tilde */
6367 mch_memmove(tmpsub + len, reg_prev_sub, (size_t)prevlen);
6368 /* copy postfix */
6369 if (!magic)
6370 ++p; /* back off \ */
6371 STRCPY(tmpsub + len + prevlen, p + 1);
6372
6373 if (newsub != source) /* already allocated newsub */
6374 vim_free(newsub);
6375 newsub = tmpsub;
6376 p = newsub + len + prevlen;
6377 }
6378 }
6379 else if (magic)
6380 STRCPY(p, p + 1); /* remove '~' */
6381 else
6382 STRCPY(p, p + 2); /* remove '\~' */
6383 --p;
6384 }
6385 else
6386 {
6387 if (*p == '\\' && p[1]) /* skip escaped characters */
6388 ++p;
6389#ifdef FEAT_MBYTE
6390 if (has_mbyte)
6391 p += (*mb_ptr2len_check)(p) - 1;
6392#endif
6393 }
6394 }
6395
6396 vim_free(reg_prev_sub);
6397 if (newsub != source) /* newsub was allocated, just keep it */
6398 reg_prev_sub = newsub;
6399 else /* no ~ found, need to save newsub */
6400 reg_prev_sub = vim_strsave(newsub);
6401 return newsub;
6402}
6403
6404#ifdef FEAT_EVAL
6405static int can_f_submatch = FALSE; /* TRUE when submatch() can be used */
6406
6407/* These pointers are used instead of reg_match and reg_mmatch for
6408 * reg_submatch(). Needed for when the substitution string is an expression
6409 * that contains a call to substitute() and submatch(). */
6410static regmatch_T *submatch_match;
6411static regmmatch_T *submatch_mmatch;
6412#endif
6413
6414#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
6415/*
6416 * vim_regsub() - perform substitutions after a vim_regexec() or
6417 * vim_regexec_multi() match.
6418 *
6419 * If "copy" is TRUE really copy into "dest".
6420 * If "copy" is FALSE nothing is copied, this is just to find out the length
6421 * of the result.
6422 *
6423 * If "backslash" is TRUE, a backslash will be removed later, need to double
6424 * them to keep them, and insert a backslash before a CR to avoid it being
6425 * replaced with a line break later.
6426 *
6427 * Note: The matched text must not change between the call of
6428 * vim_regexec()/vim_regexec_multi() and vim_regsub()! It would make the back
6429 * references invalid!
6430 *
6431 * Returns the size of the replacement, including terminating NUL.
6432 */
6433 int
6434vim_regsub(rmp, source, dest, copy, magic, backslash)
6435 regmatch_T *rmp;
6436 char_u *source;
6437 char_u *dest;
6438 int copy;
6439 int magic;
6440 int backslash;
6441{
6442 reg_match = rmp;
6443 reg_mmatch = NULL;
6444 reg_maxline = 0;
6445 return vim_regsub_both(source, dest, copy, magic, backslash);
6446}
6447#endif
6448
6449 int
6450vim_regsub_multi(rmp, lnum, source, dest, copy, magic, backslash)
6451 regmmatch_T *rmp;
6452 linenr_T lnum;
6453 char_u *source;
6454 char_u *dest;
6455 int copy;
6456 int magic;
6457 int backslash;
6458{
6459 reg_match = NULL;
6460 reg_mmatch = rmp;
6461 reg_buf = curbuf; /* always works on the current buffer! */
6462 reg_firstlnum = lnum;
6463 reg_maxline = curbuf->b_ml.ml_line_count - lnum;
6464 return vim_regsub_both(source, dest, copy, magic, backslash);
6465}
6466
6467 static int
6468vim_regsub_both(source, dest, copy, magic, backslash)
6469 char_u *source;
6470 char_u *dest;
6471 int copy;
6472 int magic;
6473 int backslash;
6474{
6475 char_u *src;
6476 char_u *dst;
6477 char_u *s;
6478 int c;
6479 int no = -1;
6480 fptr func = (fptr)NULL;
6481 linenr_T clnum = 0; /* init for GCC */
6482 int len = 0; /* init for GCC */
6483#ifdef FEAT_EVAL
6484 static char_u *eval_result = NULL;
6485#endif
6486#ifdef FEAT_MBYTE
6487 int l;
6488#endif
6489
6490
6491 /* Be paranoid... */
6492 if (source == NULL || dest == NULL)
6493 {
6494 EMSG(_(e_null));
6495 return 0;
6496 }
6497 if (prog_magic_wrong())
6498 return 0;
6499 src = source;
6500 dst = dest;
6501
6502 /*
6503 * When the substitute part starts with "\=" evaluate it as an expression.
6504 */
6505 if (source[0] == '\\' && source[1] == '='
6506#ifdef FEAT_EVAL
6507 && !can_f_submatch /* can't do this recursively */
6508#endif
6509 )
6510 {
6511#ifdef FEAT_EVAL
6512 /* To make sure that the length doesn't change between checking the
6513 * length and copying the string, and to speed up things, the
6514 * resulting string is saved from the call with "copy" == FALSE to the
6515 * call with "copy" == TRUE. */
6516 if (copy)
6517 {
6518 if (eval_result != NULL)
6519 {
6520 STRCPY(dest, eval_result);
6521 dst += STRLEN(eval_result);
6522 vim_free(eval_result);
6523 eval_result = NULL;
6524 }
6525 }
6526 else
6527 {
6528 linenr_T save_reg_maxline;
6529 win_T *save_reg_win;
6530 int save_ireg_ic;
6531
6532 vim_free(eval_result);
6533
6534 /* The expression may contain substitute(), which calls us
6535 * recursively. Make sure submatch() gets the text from the first
6536 * level. Don't need to save "reg_buf", because
6537 * vim_regexec_multi() can't be called recursively. */
6538 submatch_match = reg_match;
6539 submatch_mmatch = reg_mmatch;
6540 save_reg_maxline = reg_maxline;
6541 save_reg_win = reg_win;
6542 save_ireg_ic = ireg_ic;
6543 can_f_submatch = TRUE;
6544
6545 eval_result = eval_to_string(source + 2, NULL);
6546 if (eval_result != NULL)
6547 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006548 for (s = eval_result; *s != NUL; mb_ptr_adv(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 {
6550 /* Change NL to CR, so that it becomes a line break.
6551 * Skip over a backslashed character. */
6552 if (*s == NL)
6553 *s = CAR;
6554 else if (*s == '\\' && s[1] != NUL)
6555 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 }
6557
6558 dst += STRLEN(eval_result);
6559 }
6560
6561 reg_match = submatch_match;
6562 reg_mmatch = submatch_mmatch;
6563 reg_maxline = save_reg_maxline;
6564 reg_win = save_reg_win;
6565 ireg_ic = save_ireg_ic;
6566 can_f_submatch = FALSE;
6567 }
6568#endif
6569 }
6570 else
6571 while ((c = *src++) != NUL)
6572 {
6573 if (c == '&' && magic)
6574 no = 0;
6575 else if (c == '\\' && *src != NUL)
6576 {
6577 if (*src == '&' && !magic)
6578 {
6579 ++src;
6580 no = 0;
6581 }
6582 else if ('0' <= *src && *src <= '9')
6583 {
6584 no = *src++ - '0';
6585 }
6586 else if (vim_strchr((char_u *)"uUlLeE", *src))
6587 {
6588 switch (*src++)
6589 {
6590 case 'u': func = (fptr)do_upper;
6591 continue;
6592 case 'U': func = (fptr)do_Upper;
6593 continue;
6594 case 'l': func = (fptr)do_lower;
6595 continue;
6596 case 'L': func = (fptr)do_Lower;
6597 continue;
6598 case 'e':
6599 case 'E': func = (fptr)NULL;
6600 continue;
6601 }
6602 }
6603 }
6604 if (no < 0) /* Ordinary character. */
6605 {
6606 if (c == '\\' && *src != NUL)
6607 {
6608 /* Check for abbreviations -- webb */
6609 switch (*src)
6610 {
6611 case 'r': c = CAR; ++src; break;
6612 case 'n': c = NL; ++src; break;
6613 case 't': c = TAB; ++src; break;
6614 /* Oh no! \e already has meaning in subst pat :-( */
6615 /* case 'e': c = ESC; ++src; break; */
6616 case 'b': c = Ctrl_H; ++src; break;
6617
6618 /* If "backslash" is TRUE the backslash will be removed
6619 * later. Used to insert a literal CR. */
6620 default: if (backslash)
6621 {
6622 if (copy)
6623 *dst = '\\';
6624 ++dst;
6625 }
6626 c = *src++;
6627 }
6628 }
6629
6630 /* Write to buffer, if copy is set. */
6631#ifdef FEAT_MBYTE
6632 if (has_mbyte && (l = (*mb_ptr2len_check)(src - 1)) > 1)
6633 {
6634 /* TODO: should use "func" here. */
6635 if (copy)
6636 mch_memmove(dst, src - 1, l);
6637 dst += l - 1;
6638 src += l - 1;
6639 }
6640 else
6641 {
6642#endif
6643 if (copy)
6644 {
6645 if (func == (fptr)NULL) /* just copy */
6646 *dst = c;
6647 else /* change case */
6648 func = (fptr)(func(dst, c));
6649 /* Turbo C complains without the typecast */
6650 }
6651#ifdef FEAT_MBYTE
6652 }
6653#endif
6654 dst++;
6655 }
6656 else
6657 {
6658 if (REG_MULTI)
6659 {
6660 clnum = reg_mmatch->startpos[no].lnum;
6661 if (clnum < 0 || reg_mmatch->endpos[no].lnum < 0)
6662 s = NULL;
6663 else
6664 {
6665 s = reg_getline(clnum) + reg_mmatch->startpos[no].col;
6666 if (reg_mmatch->endpos[no].lnum == clnum)
6667 len = reg_mmatch->endpos[no].col
6668 - reg_mmatch->startpos[no].col;
6669 else
6670 len = (int)STRLEN(s);
6671 }
6672 }
6673 else
6674 {
6675 s = reg_match->startp[no];
6676 if (reg_match->endp[no] == NULL)
6677 s = NULL;
6678 else
6679 len = (int)(reg_match->endp[no] - s);
6680 }
6681 if (s != NULL)
6682 {
6683 for (;;)
6684 {
6685 if (len == 0)
6686 {
6687 if (REG_MULTI)
6688 {
6689 if (reg_mmatch->endpos[no].lnum == clnum)
6690 break;
6691 if (copy)
6692 *dst = CAR;
6693 ++dst;
6694 s = reg_getline(++clnum);
6695 if (reg_mmatch->endpos[no].lnum == clnum)
6696 len = reg_mmatch->endpos[no].col;
6697 else
6698 len = (int)STRLEN(s);
6699 }
6700 else
6701 break;
6702 }
6703 else if (*s == NUL) /* we hit NUL. */
6704 {
6705 if (copy)
6706 EMSG(_(e_re_damg));
6707 goto exit;
6708 }
6709 else
6710 {
6711 if (backslash && (*s == CAR || *s == '\\'))
6712 {
6713 /*
6714 * Insert a backslash in front of a CR, otherwise
6715 * it will be replaced by a line break.
6716 * Number of backslashes will be halved later,
6717 * double them here.
6718 */
6719 if (copy)
6720 {
6721 dst[0] = '\\';
6722 dst[1] = *s;
6723 }
6724 dst += 2;
6725 }
6726#ifdef FEAT_MBYTE
6727 else if (has_mbyte && (l = (*mb_ptr2len_check)(s)) > 1)
6728 {
6729 /* TODO: should use "func" here. */
6730 if (copy)
6731 mch_memmove(dst, s, l);
6732 dst += l;
6733 s += l - 1;
6734 len -= l - 1;
6735 }
6736#endif
6737 else
6738 {
6739 if (copy)
6740 {
6741 if (func == (fptr)NULL) /* just copy */
6742 *dst = *s;
6743 else /* change case */
6744 func = (fptr)(func(dst, *s));
6745 /* Turbo C complains without the typecast */
6746 }
6747 ++dst;
6748 }
6749 ++s;
6750 --len;
6751 }
6752 }
6753 }
6754 no = -1;
6755 }
6756 }
6757 if (copy)
6758 *dst = NUL;
6759
6760exit:
6761 return (int)((dst - dest) + 1);
6762}
6763
6764#ifdef FEAT_EVAL
6765/*
6766 * Used for the submatch() function: get the string from tne n'th submatch in
6767 * allocated memory.
6768 * Returns NULL when not in a ":s" command and for a non-existing submatch.
6769 */
6770 char_u *
6771reg_submatch(no)
6772 int no;
6773{
6774 char_u *retval = NULL;
6775 char_u *s;
6776 int len;
6777 int round;
6778 linenr_T lnum;
6779
6780 if (!can_f_submatch)
6781 return NULL;
6782
6783 if (submatch_match == NULL)
6784 {
6785 /*
6786 * First round: compute the length and allocate memory.
6787 * Second round: copy the text.
6788 */
6789 for (round = 1; round <= 2; ++round)
6790 {
6791 lnum = submatch_mmatch->startpos[no].lnum;
6792 if (lnum < 0 || submatch_mmatch->endpos[no].lnum < 0)
6793 return NULL;
6794
6795 s = reg_getline(lnum) + submatch_mmatch->startpos[no].col;
6796 if (s == NULL) /* anti-crash check, cannot happen? */
6797 break;
6798 if (submatch_mmatch->endpos[no].lnum == lnum)
6799 {
6800 /* Within one line: take form start to end col. */
6801 len = submatch_mmatch->endpos[no].col
6802 - submatch_mmatch->startpos[no].col;
6803 if (round == 2)
6804 {
6805 STRNCPY(retval, s, len);
6806 retval[len] = NUL;
6807 }
6808 ++len;
6809 }
6810 else
6811 {
6812 /* Multiple lines: take start line from start col, middle
6813 * lines completely and end line up to end col. */
6814 len = (int)STRLEN(s);
6815 if (round == 2)
6816 {
6817 STRCPY(retval, s);
6818 retval[len] = '\n';
6819 }
6820 ++len;
6821 ++lnum;
6822 while (lnum < submatch_mmatch->endpos[no].lnum)
6823 {
6824 s = reg_getline(lnum++);
6825 if (round == 2)
6826 STRCPY(retval + len, s);
6827 len += (int)STRLEN(s);
6828 if (round == 2)
6829 retval[len] = '\n';
6830 ++len;
6831 }
6832 if (round == 2)
6833 STRNCPY(retval + len, reg_getline(lnum),
6834 submatch_mmatch->endpos[no].col);
6835 len += submatch_mmatch->endpos[no].col;
6836 if (round == 2)
6837 retval[len] = NUL;
6838 ++len;
6839 }
6840
6841 if (round == 1)
6842 {
6843 retval = lalloc((long_u)len, TRUE);
6844 if (s == NULL)
6845 return NULL;
6846 }
6847 }
6848 }
6849 else
6850 {
6851 if (submatch_match->endp[no] == NULL)
6852 retval = NULL;
6853 else
6854 {
6855 s = submatch_match->startp[no];
6856 retval = vim_strnsave(s, (int)(submatch_match->endp[no] - s));
6857 }
6858 }
6859
6860 return retval;
6861}
6862#endif