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