blob: e0bfb030587beac24ec408202b8c7b1b6f9647f3 [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
Bram Moolenaar71fe80d2006-01-22 23:25:56 +0000232#define RE_MARK 207 /* mark cmp Match mark position */
233#define RE_VISUAL 208 /* Match Visual area */
234
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235/*
236 * Magic characters have a special meaning, they don't match literally.
237 * Magic characters are negative. This separates them from literal characters
238 * (possibly multi-byte). Only ASCII characters can be Magic.
239 */
240#define Magic(x) ((int)(x) - 256)
241#define un_Magic(x) ((x) + 256)
242#define is_Magic(x) ((x) < 0)
243
244static int no_Magic __ARGS((int x));
245static int toggle_Magic __ARGS((int x));
246
247 static int
248no_Magic(x)
249 int x;
250{
251 if (is_Magic(x))
252 return un_Magic(x);
253 return x;
254}
255
256 static int
257toggle_Magic(x)
258 int x;
259{
260 if (is_Magic(x))
261 return un_Magic(x);
262 return Magic(x);
263}
264
265/*
266 * The first byte of the regexp internal "program" is actually this magic
267 * number; the start node begins in the second byte. It's used to catch the
268 * most severe mutilation of the program by the caller.
269 */
270
271#define REGMAGIC 0234
272
273/*
274 * Opcode notes:
275 *
276 * BRANCH The set of branches constituting a single choice are hooked
277 * together with their "next" pointers, since precedence prevents
278 * anything being concatenated to any individual branch. The
279 * "next" pointer of the last BRANCH in a choice points to the
280 * thing following the whole choice. This is also where the
281 * final "next" pointer of each individual branch points; each
282 * branch starts with the operand node of a BRANCH node.
283 *
284 * BACK Normal "next" pointers all implicitly point forward; BACK
285 * exists to make loop structures possible.
286 *
287 * STAR,PLUS '=', and complex '*' and '+', are implemented as circular
288 * BRANCH structures using BACK. Simple cases (one character
289 * per match) are implemented with STAR and PLUS for speed
290 * and to minimize recursive plunges.
291 *
292 * BRACE_LIMITS This is always followed by a BRACE_SIMPLE or BRACE_COMPLEX
293 * node, and defines the min and max limits to be used for that
294 * node.
295 *
296 * MOPEN,MCLOSE ...are numbered at compile time.
297 * ZOPEN,ZCLOSE ...ditto
298 */
299
300/*
301 * A node is one char of opcode followed by two chars of "next" pointer.
302 * "Next" pointers are stored as two 8-bit bytes, high order first. The
303 * value is a positive offset from the opcode of the node containing it.
304 * An operand, if any, simply follows the node. (Note that much of the
305 * code generation knows about this implicit relationship.)
306 *
307 * Using two bytes for the "next" pointer is vast overkill for most things,
308 * but allows patterns to get big without disasters.
309 */
310#define OP(p) ((int)*(p))
311#define NEXT(p) (((*((p) + 1) & 0377) << 8) + (*((p) + 2) & 0377))
312#define OPERAND(p) ((p) + 3)
313/* Obtain an operand that was stored as four bytes, MSB first. */
314#define OPERAND_MIN(p) (((long)(p)[3] << 24) + ((long)(p)[4] << 16) \
315 + ((long)(p)[5] << 8) + (long)(p)[6])
316/* Obtain a second operand stored as four bytes. */
317#define OPERAND_MAX(p) OPERAND_MIN((p) + 4)
318/* Obtain a second single-byte operand stored after a four bytes operand. */
319#define OPERAND_CMP(p) (p)[7]
320
321/*
322 * Utility definitions.
323 */
324#define UCHARAT(p) ((int)*(char_u *)(p))
325
326/* Used for an error (down from) vim_regcomp(): give the error message, set
327 * rc_did_emsg and return NULL */
Bram Moolenaar98692072006-02-04 00:57:42 +0000328#define EMSG_RET_NULL(m) return (EMSG(m), rc_did_emsg = TRUE, (void *)NULL)
329#define EMSG_M_RET_NULL(m, c) return (EMSG2((m), (c) ? "" : "\\"), rc_did_emsg = TRUE, (void *)NULL)
Bram Moolenaar45eeb132005-06-06 21:59:07 +0000330#define EMSG_RET_FAIL(m) return (EMSG(m), rc_did_emsg = TRUE, FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331#define EMSG_ONE_RET_NULL EMSG_M_RET_NULL(_("E369: invalid item in %s%%[]"), reg_magic == MAGIC_ALL)
332
333#define MAX_LIMIT (32767L << 16L)
334
335static int re_multi_type __ARGS((int));
336static int cstrncmp __ARGS((char_u *s1, char_u *s2, int *n));
337static char_u *cstrchr __ARGS((char_u *, int));
338
339#ifdef DEBUG
340static void regdump __ARGS((char_u *, regprog_T *));
341static char_u *regprop __ARGS((char_u *));
342#endif
343
344#define NOT_MULTI 0
345#define MULTI_ONE 1
346#define MULTI_MULT 2
347/*
348 * Return NOT_MULTI if c is not a "multi" operator.
349 * Return MULTI_ONE if c is a single "multi" operator.
350 * Return MULTI_MULT if c is a multi "multi" operator.
351 */
352 static int
353re_multi_type(c)
354 int c;
355{
356 if (c == Magic('@') || c == Magic('=') || c == Magic('?'))
357 return MULTI_ONE;
358 if (c == Magic('*') || c == Magic('+') || c == Magic('{'))
359 return MULTI_MULT;
360 return NOT_MULTI;
361}
362
363/*
364 * Flags to be passed up and down.
365 */
366#define HASWIDTH 0x1 /* Known never to match null string. */
367#define SIMPLE 0x2 /* Simple enough to be STAR/PLUS operand. */
368#define SPSTART 0x4 /* Starts with * or +. */
369#define HASNL 0x8 /* Contains some \n. */
370#define HASLOOKBH 0x10 /* Contains "\@<=" or "\@<!". */
371#define WORST 0 /* Worst case. */
372
373/*
374 * When regcode is set to this value, code is not emitted and size is computed
375 * instead.
376 */
377#define JUST_CALC_SIZE ((char_u *) -1)
378
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000379static char_u *reg_prev_sub = NULL;
380
381#if defined(EXITFREE) || defined(PROTO)
382 void
383free_regexp_stuff()
384{
385 vim_free(reg_prev_sub);
386}
387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388
389/*
390 * REGEXP_INRANGE contains all characters which are always special in a []
391 * range after '\'.
392 * REGEXP_ABBR contains all characters which act as abbreviations after '\'.
393 * These are:
394 * \n - New line (NL).
395 * \r - Carriage Return (CR).
396 * \t - Tab (TAB).
397 * \e - Escape (ESC).
398 * \b - Backspace (Ctrl_H).
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000399 * \d - Character code in decimal, eg \d123
400 * \o - Character code in octal, eg \o80
401 * \x - Character code in hex, eg \x4a
402 * \u - Multibyte character code, eg \u20ac
403 * \U - Long multibyte character code, eg \U12345678
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 */
405static char_u REGEXP_INRANGE[] = "]^-n\\";
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000406static char_u REGEXP_ABBR[] = "nrtebdoxuU";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407
408static int backslash_trans __ARGS((int c));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000409static int get_char_class __ARGS((char_u **pp));
410static int get_equi_class __ARGS((char_u **pp));
411static void reg_equi_class __ARGS((int c));
412static int get_coll_element __ARGS((char_u **pp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413static char_u *skip_anyof __ARGS((char_u *p));
414static void init_class_tab __ARGS((void));
415
416/*
417 * Translate '\x' to its control character, except "\n", which is Magic.
418 */
419 static int
420backslash_trans(c)
421 int c;
422{
423 switch (c)
424 {
425 case 'r': return CAR;
426 case 't': return TAB;
427 case 'e': return ESC;
428 case 'b': return BS;
429 }
430 return c;
431}
432
433/*
Bram Moolenaardf177f62005-02-22 08:39:57 +0000434 * Check for a character class name "[:name:]". "pp" points to the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 * Returns one of the CLASS_ items. CLASS_NONE means that no item was
436 * recognized. Otherwise "pp" is advanced to after the item.
437 */
438 static int
Bram Moolenaardf177f62005-02-22 08:39:57 +0000439get_char_class(pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 char_u **pp;
441{
442 static const char *(class_names[]) =
443 {
444 "alnum:]",
445#define CLASS_ALNUM 0
446 "alpha:]",
447#define CLASS_ALPHA 1
448 "blank:]",
449#define CLASS_BLANK 2
450 "cntrl:]",
451#define CLASS_CNTRL 3
452 "digit:]",
453#define CLASS_DIGIT 4
454 "graph:]",
455#define CLASS_GRAPH 5
456 "lower:]",
457#define CLASS_LOWER 6
458 "print:]",
459#define CLASS_PRINT 7
460 "punct:]",
461#define CLASS_PUNCT 8
462 "space:]",
463#define CLASS_SPACE 9
464 "upper:]",
465#define CLASS_UPPER 10
466 "xdigit:]",
467#define CLASS_XDIGIT 11
468 "tab:]",
469#define CLASS_TAB 12
470 "return:]",
471#define CLASS_RETURN 13
472 "backspace:]",
473#define CLASS_BACKSPACE 14
474 "escape:]",
475#define CLASS_ESCAPE 15
476 };
477#define CLASS_NONE 99
478 int i;
479
480 if ((*pp)[1] == ':')
481 {
482 for (i = 0; i < sizeof(class_names) / sizeof(*class_names); ++i)
483 if (STRNCMP(*pp + 2, class_names[i], STRLEN(class_names[i])) == 0)
484 {
485 *pp += STRLEN(class_names[i]) + 2;
486 return i;
487 }
488 }
489 return CLASS_NONE;
490}
491
492/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 * Specific version of character class functions.
494 * Using a table to keep this fast.
495 */
496static short class_tab[256];
497
498#define RI_DIGIT 0x01
499#define RI_HEX 0x02
500#define RI_OCTAL 0x04
501#define RI_WORD 0x08
502#define RI_HEAD 0x10
503#define RI_ALPHA 0x20
504#define RI_LOWER 0x40
505#define RI_UPPER 0x80
506#define RI_WHITE 0x100
507
508 static void
509init_class_tab()
510{
511 int i;
512 static int done = FALSE;
513
514 if (done)
515 return;
516
517 for (i = 0; i < 256; ++i)
518 {
519 if (i >= '0' && i <= '7')
520 class_tab[i] = RI_DIGIT + RI_HEX + RI_OCTAL + RI_WORD;
521 else if (i >= '8' && i <= '9')
522 class_tab[i] = RI_DIGIT + RI_HEX + RI_WORD;
523 else if (i >= 'a' && i <= 'f')
524 class_tab[i] = RI_HEX + RI_WORD + RI_HEAD + RI_ALPHA + RI_LOWER;
525#ifdef EBCDIC
526 else if ((i >= 'g' && i <= 'i') || (i >= 'j' && i <= 'r')
527 || (i >= 's' && i <= 'z'))
528#else
529 else if (i >= 'g' && i <= 'z')
530#endif
531 class_tab[i] = RI_WORD + RI_HEAD + RI_ALPHA + RI_LOWER;
532 else if (i >= 'A' && i <= 'F')
533 class_tab[i] = RI_HEX + RI_WORD + RI_HEAD + RI_ALPHA + RI_UPPER;
534#ifdef EBCDIC
535 else if ((i >= 'G' && i <= 'I') || ( i >= 'J' && i <= 'R')
536 || (i >= 'S' && i <= 'Z'))
537#else
538 else if (i >= 'G' && i <= 'Z')
539#endif
540 class_tab[i] = RI_WORD + RI_HEAD + RI_ALPHA + RI_UPPER;
541 else if (i == '_')
542 class_tab[i] = RI_WORD + RI_HEAD;
543 else
544 class_tab[i] = 0;
545 }
546 class_tab[' '] |= RI_WHITE;
547 class_tab['\t'] |= RI_WHITE;
548 done = TRUE;
549}
550
551#ifdef FEAT_MBYTE
552# define ri_digit(c) (c < 0x100 && (class_tab[c] & RI_DIGIT))
553# define ri_hex(c) (c < 0x100 && (class_tab[c] & RI_HEX))
554# define ri_octal(c) (c < 0x100 && (class_tab[c] & RI_OCTAL))
555# define ri_word(c) (c < 0x100 && (class_tab[c] & RI_WORD))
556# define ri_head(c) (c < 0x100 && (class_tab[c] & RI_HEAD))
557# define ri_alpha(c) (c < 0x100 && (class_tab[c] & RI_ALPHA))
558# define ri_lower(c) (c < 0x100 && (class_tab[c] & RI_LOWER))
559# define ri_upper(c) (c < 0x100 && (class_tab[c] & RI_UPPER))
560# define ri_white(c) (c < 0x100 && (class_tab[c] & RI_WHITE))
561#else
562# define ri_digit(c) (class_tab[c] & RI_DIGIT)
563# define ri_hex(c) (class_tab[c] & RI_HEX)
564# define ri_octal(c) (class_tab[c] & RI_OCTAL)
565# define ri_word(c) (class_tab[c] & RI_WORD)
566# define ri_head(c) (class_tab[c] & RI_HEAD)
567# define ri_alpha(c) (class_tab[c] & RI_ALPHA)
568# define ri_lower(c) (class_tab[c] & RI_LOWER)
569# define ri_upper(c) (class_tab[c] & RI_UPPER)
570# define ri_white(c) (class_tab[c] & RI_WHITE)
571#endif
572
573/* flags for regflags */
574#define RF_ICASE 1 /* ignore case */
575#define RF_NOICASE 2 /* don't ignore case */
576#define RF_HASNL 4 /* can match a NL */
577#define RF_ICOMBINE 8 /* ignore combining characters */
578#define RF_LOOKBH 16 /* uses "\@<=" or "\@<!" */
579
580/*
581 * Global work variables for vim_regcomp().
582 */
583
584static char_u *regparse; /* Input-scan pointer. */
585static int prevchr_len; /* byte length of previous char */
586static int num_complex_braces; /* Complex \{...} count */
587static int regnpar; /* () count. */
588#ifdef FEAT_SYN_HL
589static int regnzpar; /* \z() count. */
590static int re_has_z; /* \z item detected */
591#endif
592static char_u *regcode; /* Code-emit pointer, or JUST_CALC_SIZE */
593static long regsize; /* Code size. */
594static char_u had_endbrace[NSUBEXP]; /* flags, TRUE if end of () found */
595static unsigned regflags; /* RF_ flags for prog */
596static long brace_min[10]; /* Minimums for complex brace repeats */
597static long brace_max[10]; /* Maximums for complex brace repeats */
598static int brace_count[10]; /* Current counts for complex brace repeats */
599#if defined(FEAT_SYN_HL) || defined(PROTO)
600static int had_eol; /* TRUE when EOL found by vim_regcomp() */
601#endif
602static int one_exactly = FALSE; /* only do one char for EXACTLY */
603
604static int reg_magic; /* magicness of the pattern: */
605#define MAGIC_NONE 1 /* "\V" very unmagic */
606#define MAGIC_OFF 2 /* "\M" or 'magic' off */
607#define MAGIC_ON 3 /* "\m" or 'magic' */
608#define MAGIC_ALL 4 /* "\v" very magic */
609
610static int reg_string; /* matching with a string instead of a buffer
611 line */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000612static int reg_strict; /* "[abc" is illegal */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613
614/*
615 * META contains all characters that may be magic, except '^' and '$'.
616 */
617
618#ifdef EBCDIC
619static char_u META[] = "%&()*+.123456789<=>?@ACDFHIKLMOPSUVWX[_acdfhiklmnopsuvwxz{|~";
620#else
621/* META[] is used often enough to justify turning it into a table. */
622static char_u META_flags[] = {
623 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
624 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
625/* % & ( ) * + . */
626 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0,
627/* 1 2 3 4 5 6 7 8 9 < = > ? */
628 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1,
629/* @ A C D F H I K L M O */
630 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1,
631/* P S U V W X Z [ _ */
632 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1,
633/* a c d f h i k l m n o */
634 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1,
635/* p s u v w x z { | ~ */
636 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1
637};
638#endif
639
640static int curchr;
641
642/* arguments for reg() */
643#define REG_NOPAREN 0 /* toplevel reg() */
644#define REG_PAREN 1 /* \(\) */
645#define REG_ZPAREN 2 /* \z(\) */
646#define REG_NPAREN 3 /* \%(\) */
647
648/*
649 * Forward declarations for vim_regcomp()'s friends.
650 */
651static void initchr __ARGS((char_u *));
652static int getchr __ARGS((void));
653static void skipchr_keepstart __ARGS((void));
654static int peekchr __ARGS((void));
655static void skipchr __ARGS((void));
656static void ungetchr __ARGS((void));
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000657static int gethexchrs __ARGS((int maxinputlen));
658static int getoctchrs __ARGS((void));
659static int getdecchrs __ARGS((void));
660static int coll_get_char __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661static void regcomp_start __ARGS((char_u *expr, int flags));
662static char_u *reg __ARGS((int, int *));
663static char_u *regbranch __ARGS((int *flagp));
664static char_u *regconcat __ARGS((int *flagp));
665static char_u *regpiece __ARGS((int *));
666static char_u *regatom __ARGS((int *));
667static char_u *regnode __ARGS((int));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000668#ifdef FEAT_MBYTE
669static int use_multibytecode __ARGS((int c));
670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671static int prog_magic_wrong __ARGS((void));
672static char_u *regnext __ARGS((char_u *));
673static void regc __ARGS((int b));
674#ifdef FEAT_MBYTE
675static void regmbc __ARGS((int c));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000676#else
677# define regmbc(c) regc(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678#endif
679static void reginsert __ARGS((int, char_u *));
680static void reginsert_limits __ARGS((int, long, long, char_u *));
681static char_u *re_put_long __ARGS((char_u *pr, long_u val));
682static int read_limits __ARGS((long *, long *));
683static void regtail __ARGS((char_u *, char_u *));
684static void regoptail __ARGS((char_u *, char_u *));
685
686/*
687 * Return TRUE if compiled regular expression "prog" can match a line break.
688 */
689 int
690re_multiline(prog)
691 regprog_T *prog;
692{
693 return (prog->regflags & RF_HASNL);
694}
695
696/*
697 * Return TRUE if compiled regular expression "prog" looks before the start
698 * position (pattern contains "\@<=" or "\@<!").
699 */
700 int
701re_lookbehind(prog)
702 regprog_T *prog;
703{
704 return (prog->regflags & RF_LOOKBH);
705}
706
707/*
Bram Moolenaardf177f62005-02-22 08:39:57 +0000708 * Check for an equivalence class name "[=a=]". "pp" points to the '['.
709 * Returns a character representing the class. Zero means that no item was
710 * recognized. Otherwise "pp" is advanced to after the item.
711 */
712 static int
713get_equi_class(pp)
714 char_u **pp;
715{
716 int c;
717 int l = 1;
718 char_u *p = *pp;
719
720 if (p[1] == '=')
721 {
722#ifdef FEAT_MBYTE
723 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000724 l = (*mb_ptr2len)(p + 2);
Bram Moolenaardf177f62005-02-22 08:39:57 +0000725#endif
726 if (p[l + 2] == '=' && p[l + 3] == ']')
727 {
728#ifdef FEAT_MBYTE
729 if (has_mbyte)
730 c = mb_ptr2char(p + 2);
731 else
732#endif
733 c = p[2];
734 *pp += l + 4;
735 return c;
736 }
737 }
738 return 0;
739}
740
741/*
742 * Produce the bytes for equivalence class "c".
743 * Currently only handles latin1, latin9 and utf-8.
744 */
745 static void
746reg_equi_class(c)
747 int c;
748{
749#ifdef FEAT_MBYTE
750 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
Bram Moolenaar78622822005-08-23 21:00:13 +0000751 || STRCMP(p_enc, "iso-8859-15") == 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000752#endif
753 {
754 switch (c)
755 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000756 case 'A': case '\300': case '\301': case '\302':
757 case '\303': case '\304': case '\305':
758 regmbc('A'); regmbc('\300'); regmbc('\301');
759 regmbc('\302'); regmbc('\303'); regmbc('\304');
760 regmbc('\305');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000761 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000762 case 'C': case '\307':
763 regmbc('C'); regmbc('\307');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000764 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000765 case 'E': case '\310': case '\311': case '\312': case '\313':
766 regmbc('E'); regmbc('\310'); regmbc('\311');
767 regmbc('\312'); regmbc('\313');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000768 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000769 case 'I': case '\314': case '\315': case '\316': case '\317':
770 regmbc('I'); regmbc('\314'); regmbc('\315');
771 regmbc('\316'); regmbc('\317');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000772 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000773 case 'N': case '\321':
774 regmbc('N'); regmbc('\321');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000775 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000776 case 'O': case '\322': case '\323': case '\324': case '\325':
777 case '\326':
778 regmbc('O'); regmbc('\322'); regmbc('\323');
779 regmbc('\324'); regmbc('\325'); regmbc('\326');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000780 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000781 case 'U': case '\331': case '\332': case '\333': case '\334':
782 regmbc('U'); regmbc('\331'); regmbc('\332');
783 regmbc('\333'); regmbc('\334');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000784 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000785 case 'Y': case '\335':
786 regmbc('Y'); regmbc('\335');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000787 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000788 case 'a': case '\340': case '\341': case '\342':
789 case '\343': case '\344': case '\345':
790 regmbc('a'); regmbc('\340'); regmbc('\341');
791 regmbc('\342'); regmbc('\343'); regmbc('\344');
792 regmbc('\345');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000793 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000794 case 'c': case '\347':
795 regmbc('c'); regmbc('\347');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000796 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000797 case 'e': case '\350': case '\351': case '\352': case '\353':
798 regmbc('e'); regmbc('\350'); regmbc('\351');
799 regmbc('\352'); regmbc('\353');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000800 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000801 case 'i': case '\354': case '\355': case '\356': case '\357':
802 regmbc('i'); regmbc('\354'); regmbc('\355');
803 regmbc('\356'); regmbc('\357');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000804 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000805 case 'n': case '\361':
806 regmbc('n'); regmbc('\361');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000807 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000808 case 'o': case '\362': case '\363': case '\364': case '\365':
809 case '\366':
810 regmbc('o'); regmbc('\362'); regmbc('\363');
811 regmbc('\364'); regmbc('\365'); regmbc('\366');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000812 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000813 case 'u': case '\371': case '\372': case '\373': case '\374':
814 regmbc('u'); regmbc('\371'); regmbc('\372');
815 regmbc('\373'); regmbc('\374');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000816 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000817 case 'y': case '\375': case '\377':
818 regmbc('y'); regmbc('\375'); regmbc('\377');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000819 return;
820 }
821 }
822 regmbc(c);
823}
824
825/*
826 * Check for a collating element "[.a.]". "pp" points to the '['.
827 * Returns a character. Zero means that no item was recognized. Otherwise
828 * "pp" is advanced to after the item.
829 * Currently only single characters are recognized!
830 */
831 static int
832get_coll_element(pp)
833 char_u **pp;
834{
835 int c;
836 int l = 1;
837 char_u *p = *pp;
838
839 if (p[1] == '.')
840 {
841#ifdef FEAT_MBYTE
842 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000843 l = (*mb_ptr2len)(p + 2);
Bram Moolenaardf177f62005-02-22 08:39:57 +0000844#endif
845 if (p[l + 2] == '.' && p[l + 3] == ']')
846 {
847#ifdef FEAT_MBYTE
848 if (has_mbyte)
849 c = mb_ptr2char(p + 2);
850 else
851#endif
852 c = p[2];
853 *pp += l + 4;
854 return c;
855 }
856 }
857 return 0;
858}
859
860
861/*
862 * Skip over a "[]" range.
863 * "p" must point to the character after the '['.
864 * The returned pointer is on the matching ']', or the terminating NUL.
865 */
866 static char_u *
867skip_anyof(p)
868 char_u *p;
869{
870 int cpo_lit; /* 'cpoptions' contains 'l' flag */
871 int cpo_bsl; /* 'cpoptions' contains '\' flag */
872#ifdef FEAT_MBYTE
873 int l;
874#endif
875
Bram Moolenaar3b56eb32005-07-11 22:40:32 +0000876 cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL;
877 cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000878
879 if (*p == '^') /* Complement of range. */
880 ++p;
881 if (*p == ']' || *p == '-')
882 ++p;
883 while (*p != NUL && *p != ']')
884 {
885#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000886 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000887 p += l;
888 else
889#endif
890 if (*p == '-')
891 {
892 ++p;
893 if (*p != ']' && *p != NUL)
894 mb_ptr_adv(p);
895 }
896 else if (*p == '\\'
897 && !cpo_bsl
898 && (vim_strchr(REGEXP_INRANGE, p[1]) != NULL
899 || (!cpo_lit && vim_strchr(REGEXP_ABBR, p[1]) != NULL)))
900 p += 2;
901 else if (*p == '[')
902 {
903 if (get_char_class(&p) == CLASS_NONE
904 && get_equi_class(&p) == 0
905 && get_coll_element(&p) == 0)
906 ++p; /* It was not a class name */
907 }
908 else
909 ++p;
910 }
911
912 return p;
913}
914
915/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 * Skip past regular expression.
Bram Moolenaar748bf032005-02-02 23:04:36 +0000917 * Stop at end of "startp" or where "dirc" is found ('/', '?', etc).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 * Take care of characters with a backslash in front of it.
919 * Skip strings inside [ and ].
920 * When "newp" is not NULL and "dirc" is '?', make an allocated copy of the
921 * expression and change "\?" to "?". If "*newp" is not NULL the expression
922 * is changed in-place.
923 */
924 char_u *
925skip_regexp(startp, dirc, magic, newp)
926 char_u *startp;
927 int dirc;
928 int magic;
929 char_u **newp;
930{
931 int mymagic;
932 char_u *p = startp;
933
934 if (magic)
935 mymagic = MAGIC_ON;
936 else
937 mymagic = MAGIC_OFF;
938
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000939 for (; p[0] != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 {
941 if (p[0] == dirc) /* found end of regexp */
942 break;
943 if ((p[0] == '[' && mymagic >= MAGIC_ON)
944 || (p[0] == '\\' && p[1] == '[' && mymagic <= MAGIC_OFF))
945 {
946 p = skip_anyof(p + 1);
947 if (p[0] == NUL)
948 break;
949 }
950 else if (p[0] == '\\' && p[1] != NUL)
951 {
952 if (dirc == '?' && newp != NULL && p[1] == '?')
953 {
954 /* change "\?" to "?", make a copy first. */
955 if (*newp == NULL)
956 {
957 *newp = vim_strsave(startp);
958 if (*newp != NULL)
959 p = *newp + (p - startp);
960 }
961 if (*newp != NULL)
962 mch_memmove(p, p + 1, STRLEN(p));
963 else
964 ++p;
965 }
966 else
967 ++p; /* skip next character */
968 if (*p == 'v')
969 mymagic = MAGIC_ALL;
970 else if (*p == 'V')
971 mymagic = MAGIC_NONE;
972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 }
974 return p;
975}
976
977/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000978 * vim_regcomp() - compile a regular expression into internal code
979 * Returns the program in allocated space. Returns NULL for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 *
981 * We can't allocate space until we know how big the compiled form will be,
982 * but we can't compile it (and thus know how big it is) until we've got a
983 * place to put the code. So we cheat: we compile it twice, once with code
984 * generation turned off and size counting turned on, and once "for real".
985 * This also means that we don't allocate space until we are sure that the
986 * thing really will compile successfully, and we never have to move the
987 * code and thus invalidate pointers into it. (Note that it has to be in
988 * one piece because vim_free() must be able to free it all.)
989 *
990 * Whether upper/lower case is to be ignored is decided when executing the
991 * program, it does not matter here.
992 *
993 * Beware that the optimization-preparation code in here knows about some
994 * of the structure of the compiled regexp.
995 * "re_flags": RE_MAGIC and/or RE_STRING.
996 */
997 regprog_T *
998vim_regcomp(expr, re_flags)
999 char_u *expr;
1000 int re_flags;
1001{
1002 regprog_T *r;
1003 char_u *scan;
1004 char_u *longest;
1005 int len;
1006 int flags;
1007
1008 if (expr == NULL)
1009 EMSG_RET_NULL(_(e_null));
1010
1011 init_class_tab();
1012
1013 /*
1014 * First pass: determine size, legality.
1015 */
1016 regcomp_start(expr, re_flags);
1017 regcode = JUST_CALC_SIZE;
1018 regc(REGMAGIC);
1019 if (reg(REG_NOPAREN, &flags) == NULL)
1020 return NULL;
1021
1022 /* Small enough for pointer-storage convention? */
1023#ifdef SMALL_MALLOC /* 16 bit storage allocation */
1024 if (regsize >= 65536L - 256L)
1025 EMSG_RET_NULL(_("E339: Pattern too long"));
1026#endif
1027
1028 /* Allocate space. */
1029 r = (regprog_T *)lalloc(sizeof(regprog_T) + regsize, TRUE);
1030 if (r == NULL)
1031 return NULL;
1032
1033 /*
1034 * Second pass: emit code.
1035 */
1036 regcomp_start(expr, re_flags);
1037 regcode = r->program;
1038 regc(REGMAGIC);
1039 if (reg(REG_NOPAREN, &flags) == NULL)
1040 {
1041 vim_free(r);
1042 return NULL;
1043 }
1044
1045 /* Dig out information for optimizations. */
1046 r->regstart = NUL; /* Worst-case defaults. */
1047 r->reganch = 0;
1048 r->regmust = NULL;
1049 r->regmlen = 0;
1050 r->regflags = regflags;
1051 if (flags & HASNL)
1052 r->regflags |= RF_HASNL;
1053 if (flags & HASLOOKBH)
1054 r->regflags |= RF_LOOKBH;
1055#ifdef FEAT_SYN_HL
1056 /* Remember whether this pattern has any \z specials in it. */
1057 r->reghasz = re_has_z;
1058#endif
1059 scan = r->program + 1; /* First BRANCH. */
1060 if (OP(regnext(scan)) == END) /* Only one top-level choice. */
1061 {
1062 scan = OPERAND(scan);
1063
1064 /* Starting-point info. */
1065 if (OP(scan) == BOL || OP(scan) == RE_BOF)
1066 {
1067 r->reganch++;
1068 scan = regnext(scan);
1069 }
1070
1071 if (OP(scan) == EXACTLY)
1072 {
1073#ifdef FEAT_MBYTE
1074 if (has_mbyte)
1075 r->regstart = (*mb_ptr2char)(OPERAND(scan));
1076 else
1077#endif
1078 r->regstart = *OPERAND(scan);
1079 }
1080 else if ((OP(scan) == BOW
1081 || OP(scan) == EOW
1082 || OP(scan) == NOTHING
1083 || OP(scan) == MOPEN + 0 || OP(scan) == NOPEN
1084 || OP(scan) == MCLOSE + 0 || OP(scan) == NCLOSE)
1085 && OP(regnext(scan)) == EXACTLY)
1086 {
1087#ifdef FEAT_MBYTE
1088 if (has_mbyte)
1089 r->regstart = (*mb_ptr2char)(OPERAND(regnext(scan)));
1090 else
1091#endif
1092 r->regstart = *OPERAND(regnext(scan));
1093 }
1094
1095 /*
1096 * If there's something expensive in the r.e., find the longest
1097 * literal string that must appear and make it the regmust. Resolve
1098 * ties in favor of later strings, since the regstart check works
1099 * with the beginning of the r.e. and avoiding duplication
1100 * strengthens checking. Not a strong reason, but sufficient in the
1101 * absence of others.
1102 */
1103 /*
1104 * When the r.e. starts with BOW, it is faster to look for a regmust
1105 * first. Used a lot for "#" and "*" commands. (Added by mool).
1106 */
1107 if ((flags & SPSTART || OP(scan) == BOW || OP(scan) == EOW)
1108 && !(flags & HASNL))
1109 {
1110 longest = NULL;
1111 len = 0;
1112 for (; scan != NULL; scan = regnext(scan))
1113 if (OP(scan) == EXACTLY && STRLEN(OPERAND(scan)) >= (size_t)len)
1114 {
1115 longest = OPERAND(scan);
1116 len = (int)STRLEN(OPERAND(scan));
1117 }
1118 r->regmust = longest;
1119 r->regmlen = len;
1120 }
1121 }
1122#ifdef DEBUG
1123 regdump(expr, r);
1124#endif
1125 return r;
1126}
1127
1128/*
1129 * Setup to parse the regexp. Used once to get the length and once to do it.
1130 */
1131 static void
1132regcomp_start(expr, re_flags)
1133 char_u *expr;
1134 int re_flags; /* see vim_regcomp() */
1135{
1136 initchr(expr);
1137 if (re_flags & RE_MAGIC)
1138 reg_magic = MAGIC_ON;
1139 else
1140 reg_magic = MAGIC_OFF;
1141 reg_string = (re_flags & RE_STRING);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001142 reg_strict = (re_flags & RE_STRICT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143
1144 num_complex_braces = 0;
1145 regnpar = 1;
1146 vim_memset(had_endbrace, 0, sizeof(had_endbrace));
1147#ifdef FEAT_SYN_HL
1148 regnzpar = 1;
1149 re_has_z = 0;
1150#endif
1151 regsize = 0L;
1152 regflags = 0;
1153#if defined(FEAT_SYN_HL) || defined(PROTO)
1154 had_eol = FALSE;
1155#endif
1156}
1157
1158#if defined(FEAT_SYN_HL) || defined(PROTO)
1159/*
1160 * Check if during the previous call to vim_regcomp the EOL item "$" has been
1161 * found. This is messy, but it works fine.
1162 */
1163 int
1164vim_regcomp_had_eol()
1165{
1166 return had_eol;
1167}
1168#endif
1169
1170/*
1171 * reg - regular expression, i.e. main body or parenthesized thing
1172 *
1173 * Caller must absorb opening parenthesis.
1174 *
1175 * Combining parenthesis handling with the base level of regular expression
1176 * is a trifle forced, but the need to tie the tails of the branches to what
1177 * follows makes it hard to avoid.
1178 */
1179 static char_u *
1180reg(paren, flagp)
1181 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1182 int *flagp;
1183{
1184 char_u *ret;
1185 char_u *br;
1186 char_u *ender;
1187 int parno = 0;
1188 int flags;
1189
1190 *flagp = HASWIDTH; /* Tentatively. */
1191
1192#ifdef FEAT_SYN_HL
1193 if (paren == REG_ZPAREN)
1194 {
1195 /* Make a ZOPEN node. */
1196 if (regnzpar >= NSUBEXP)
1197 EMSG_RET_NULL(_("E50: Too many \\z("));
1198 parno = regnzpar;
1199 regnzpar++;
1200 ret = regnode(ZOPEN + parno);
1201 }
1202 else
1203#endif
1204 if (paren == REG_PAREN)
1205 {
1206 /* Make a MOPEN node. */
1207 if (regnpar >= NSUBEXP)
1208 EMSG_M_RET_NULL(_("E51: Too many %s("), reg_magic == MAGIC_ALL);
1209 parno = regnpar;
1210 ++regnpar;
1211 ret = regnode(MOPEN + parno);
1212 }
1213 else if (paren == REG_NPAREN)
1214 {
1215 /* Make a NOPEN node. */
1216 ret = regnode(NOPEN);
1217 }
1218 else
1219 ret = NULL;
1220
1221 /* Pick up the branches, linking them together. */
1222 br = regbranch(&flags);
1223 if (br == NULL)
1224 return NULL;
1225 if (ret != NULL)
1226 regtail(ret, br); /* [MZ]OPEN -> first. */
1227 else
1228 ret = br;
1229 /* If one of the branches can be zero-width, the whole thing can.
1230 * If one of the branches has * at start or matches a line-break, the
1231 * whole thing can. */
1232 if (!(flags & HASWIDTH))
1233 *flagp &= ~HASWIDTH;
1234 *flagp |= flags & (SPSTART | HASNL | HASLOOKBH);
1235 while (peekchr() == Magic('|'))
1236 {
1237 skipchr();
1238 br = regbranch(&flags);
1239 if (br == NULL)
1240 return NULL;
1241 regtail(ret, br); /* BRANCH -> BRANCH. */
1242 if (!(flags & HASWIDTH))
1243 *flagp &= ~HASWIDTH;
1244 *flagp |= flags & (SPSTART | HASNL | HASLOOKBH);
1245 }
1246
1247 /* Make a closing node, and hook it on the end. */
1248 ender = regnode(
1249#ifdef FEAT_SYN_HL
1250 paren == REG_ZPAREN ? ZCLOSE + parno :
1251#endif
1252 paren == REG_PAREN ? MCLOSE + parno :
1253 paren == REG_NPAREN ? NCLOSE : END);
1254 regtail(ret, ender);
1255
1256 /* Hook the tails of the branches to the closing node. */
1257 for (br = ret; br != NULL; br = regnext(br))
1258 regoptail(br, ender);
1259
1260 /* Check for proper termination. */
1261 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1262 {
1263#ifdef FEAT_SYN_HL
1264 if (paren == REG_ZPAREN)
Bram Moolenaar45eeb132005-06-06 21:59:07 +00001265 EMSG_RET_NULL(_("E52: Unmatched \\z("));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 else
1267#endif
1268 if (paren == REG_NPAREN)
Bram Moolenaar45eeb132005-06-06 21:59:07 +00001269 EMSG_M_RET_NULL(_("E53: Unmatched %s%%("), reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 else
Bram Moolenaar45eeb132005-06-06 21:59:07 +00001271 EMSG_M_RET_NULL(_("E54: Unmatched %s("), reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 }
1273 else if (paren == REG_NOPAREN && peekchr() != NUL)
1274 {
1275 if (curchr == Magic(')'))
Bram Moolenaar45eeb132005-06-06 21:59:07 +00001276 EMSG_M_RET_NULL(_("E55: Unmatched %s)"), reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 else
Bram Moolenaar45eeb132005-06-06 21:59:07 +00001278 EMSG_RET_NULL(_(e_trailing)); /* "Can't happen". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 /* NOTREACHED */
1280 }
1281 /*
1282 * Here we set the flag allowing back references to this set of
1283 * parentheses.
1284 */
1285 if (paren == REG_PAREN)
1286 had_endbrace[parno] = TRUE; /* have seen the close paren */
1287 return ret;
1288}
1289
1290/*
1291 * regbranch - one alternative of an | operator
1292 *
1293 * Implements the & operator.
1294 */
1295 static char_u *
1296regbranch(flagp)
1297 int *flagp;
1298{
1299 char_u *ret;
1300 char_u *chain = NULL;
1301 char_u *latest;
1302 int flags;
1303
1304 *flagp = WORST | HASNL; /* Tentatively. */
1305
1306 ret = regnode(BRANCH);
1307 for (;;)
1308 {
1309 latest = regconcat(&flags);
1310 if (latest == NULL)
1311 return NULL;
1312 /* If one of the branches has width, the whole thing has. If one of
1313 * the branches anchors at start-of-line, the whole thing does.
1314 * If one of the branches uses look-behind, the whole thing does. */
1315 *flagp |= flags & (HASWIDTH | SPSTART | HASLOOKBH);
1316 /* If one of the branches doesn't match a line-break, the whole thing
1317 * doesn't. */
1318 *flagp &= ~HASNL | (flags & HASNL);
1319 if (chain != NULL)
1320 regtail(chain, latest);
1321 if (peekchr() != Magic('&'))
1322 break;
1323 skipchr();
1324 regtail(latest, regnode(END)); /* operand ends */
1325 reginsert(MATCH, latest);
1326 chain = latest;
1327 }
1328
1329 return ret;
1330}
1331
1332/*
1333 * regbranch - one alternative of an | or & operator
1334 *
1335 * Implements the concatenation operator.
1336 */
1337 static char_u *
1338regconcat(flagp)
1339 int *flagp;
1340{
1341 char_u *first = NULL;
1342 char_u *chain = NULL;
1343 char_u *latest;
1344 int flags;
1345 int cont = TRUE;
1346
1347 *flagp = WORST; /* Tentatively. */
1348
1349 while (cont)
1350 {
1351 switch (peekchr())
1352 {
1353 case NUL:
1354 case Magic('|'):
1355 case Magic('&'):
1356 case Magic(')'):
1357 cont = FALSE;
1358 break;
1359 case Magic('Z'):
1360#ifdef FEAT_MBYTE
1361 regflags |= RF_ICOMBINE;
1362#endif
1363 skipchr_keepstart();
1364 break;
1365 case Magic('c'):
1366 regflags |= RF_ICASE;
1367 skipchr_keepstart();
1368 break;
1369 case Magic('C'):
1370 regflags |= RF_NOICASE;
1371 skipchr_keepstart();
1372 break;
1373 case Magic('v'):
1374 reg_magic = MAGIC_ALL;
1375 skipchr_keepstart();
1376 curchr = -1;
1377 break;
1378 case Magic('m'):
1379 reg_magic = MAGIC_ON;
1380 skipchr_keepstart();
1381 curchr = -1;
1382 break;
1383 case Magic('M'):
1384 reg_magic = MAGIC_OFF;
1385 skipchr_keepstart();
1386 curchr = -1;
1387 break;
1388 case Magic('V'):
1389 reg_magic = MAGIC_NONE;
1390 skipchr_keepstart();
1391 curchr = -1;
1392 break;
1393 default:
1394 latest = regpiece(&flags);
1395 if (latest == NULL)
1396 return NULL;
1397 *flagp |= flags & (HASWIDTH | HASNL | HASLOOKBH);
1398 if (chain == NULL) /* First piece. */
1399 *flagp |= flags & SPSTART;
1400 else
1401 regtail(chain, latest);
1402 chain = latest;
1403 if (first == NULL)
1404 first = latest;
1405 break;
1406 }
1407 }
1408 if (first == NULL) /* Loop ran zero times. */
1409 first = regnode(NOTHING);
1410 return first;
1411}
1412
1413/*
1414 * regpiece - something followed by possible [*+=]
1415 *
1416 * Note that the branching code sequences used for = and the general cases
1417 * of * and + are somewhat optimized: they use the same NOTHING node as
1418 * both the endmarker for their branch list and the body of the last branch.
1419 * It might seem that this node could be dispensed with entirely, but the
1420 * endmarker role is not redundant.
1421 */
1422 static char_u *
1423regpiece(flagp)
1424 int *flagp;
1425{
1426 char_u *ret;
1427 int op;
1428 char_u *next;
1429 int flags;
1430 long minval;
1431 long maxval;
1432
1433 ret = regatom(&flags);
1434 if (ret == NULL)
1435 return NULL;
1436
1437 op = peekchr();
1438 if (re_multi_type(op) == NOT_MULTI)
1439 {
1440 *flagp = flags;
1441 return ret;
1442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 /* default flags */
1444 *flagp = (WORST | SPSTART | (flags & (HASNL | HASLOOKBH)));
1445
1446 skipchr();
1447 switch (op)
1448 {
1449 case Magic('*'):
1450 if (flags & SIMPLE)
1451 reginsert(STAR, ret);
1452 else
1453 {
1454 /* Emit x* as (x&|), where & means "self". */
1455 reginsert(BRANCH, ret); /* Either x */
1456 regoptail(ret, regnode(BACK)); /* and loop */
1457 regoptail(ret, ret); /* back */
1458 regtail(ret, regnode(BRANCH)); /* or */
1459 regtail(ret, regnode(NOTHING)); /* null. */
1460 }
1461 break;
1462
1463 case Magic('+'):
1464 if (flags & SIMPLE)
1465 reginsert(PLUS, ret);
1466 else
1467 {
1468 /* Emit x+ as x(&|), where & means "self". */
1469 next = regnode(BRANCH); /* Either */
1470 regtail(ret, next);
Bram Moolenaar582fd852005-03-28 20:58:01 +00001471 regtail(regnode(BACK), ret); /* loop back */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 regtail(next, regnode(BRANCH)); /* or */
1473 regtail(ret, regnode(NOTHING)); /* null. */
1474 }
1475 *flagp = (WORST | HASWIDTH | (flags & (HASNL | HASLOOKBH)));
1476 break;
1477
1478 case Magic('@'):
1479 {
1480 int lop = END;
1481
1482 switch (no_Magic(getchr()))
1483 {
1484 case '=': lop = MATCH; break; /* \@= */
1485 case '!': lop = NOMATCH; break; /* \@! */
1486 case '>': lop = SUBPAT; break; /* \@> */
1487 case '<': switch (no_Magic(getchr()))
1488 {
1489 case '=': lop = BEHIND; break; /* \@<= */
1490 case '!': lop = NOBEHIND; break; /* \@<! */
1491 }
1492 }
1493 if (lop == END)
1494 EMSG_M_RET_NULL(_("E59: invalid character after %s@"),
1495 reg_magic == MAGIC_ALL);
1496 /* Look behind must match with behind_pos. */
1497 if (lop == BEHIND || lop == NOBEHIND)
1498 {
1499 regtail(ret, regnode(BHPOS));
1500 *flagp |= HASLOOKBH;
1501 }
1502 regtail(ret, regnode(END)); /* operand ends */
1503 reginsert(lop, ret);
1504 break;
1505 }
1506
1507 case Magic('?'):
1508 case Magic('='):
1509 /* Emit x= as (x|) */
1510 reginsert(BRANCH, ret); /* Either x */
1511 regtail(ret, regnode(BRANCH)); /* or */
1512 next = regnode(NOTHING); /* null. */
1513 regtail(ret, next);
1514 regoptail(ret, next);
1515 break;
1516
1517 case Magic('{'):
1518 if (!read_limits(&minval, &maxval))
1519 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 if (flags & SIMPLE)
1521 {
1522 reginsert(BRACE_SIMPLE, ret);
1523 reginsert_limits(BRACE_LIMITS, minval, maxval, ret);
1524 }
1525 else
1526 {
1527 if (num_complex_braces >= 10)
1528 EMSG_M_RET_NULL(_("E60: Too many complex %s{...}s"),
1529 reg_magic == MAGIC_ALL);
1530 reginsert(BRACE_COMPLEX + num_complex_braces, ret);
1531 regoptail(ret, regnode(BACK));
1532 regoptail(ret, ret);
1533 reginsert_limits(BRACE_LIMITS, minval, maxval, ret);
1534 ++num_complex_braces;
1535 }
1536 if (minval > 0 && maxval > 0)
1537 *flagp = (HASWIDTH | (flags & (HASNL | HASLOOKBH)));
1538 break;
1539 }
1540 if (re_multi_type(peekchr()) != NOT_MULTI)
1541 {
1542 /* Can't have a multi follow a multi. */
1543 if (peekchr() == Magic('*'))
1544 sprintf((char *)IObuff, _("E61: Nested %s*"),
1545 reg_magic >= MAGIC_ON ? "" : "\\");
1546 else
1547 sprintf((char *)IObuff, _("E62: Nested %s%c"),
1548 reg_magic == MAGIC_ALL ? "" : "\\", no_Magic(peekchr()));
1549 EMSG_RET_NULL(IObuff);
1550 }
1551
1552 return ret;
1553}
1554
1555/*
1556 * regatom - the lowest level
1557 *
1558 * Optimization: gobbles an entire sequence of ordinary characters so that
1559 * it can turn them into a single node, which is smaller to store and
1560 * faster to run. Don't do this when one_exactly is set.
1561 */
1562 static char_u *
1563regatom(flagp)
1564 int *flagp;
1565{
1566 char_u *ret;
1567 int flags;
1568 int cpo_lit; /* 'cpoptions' contains 'l' flag */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001569 int cpo_bsl; /* 'cpoptions' contains '\' flag */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 int c;
1571 static char_u *classchars = (char_u *)".iIkKfFpPsSdDxXoOwWhHaAlLuU";
1572 static int classcodes[] = {ANY, IDENT, SIDENT, KWORD, SKWORD,
1573 FNAME, SFNAME, PRINT, SPRINT,
1574 WHITE, NWHITE, DIGIT, NDIGIT,
1575 HEX, NHEX, OCTAL, NOCTAL,
1576 WORD, NWORD, HEAD, NHEAD,
1577 ALPHA, NALPHA, LOWER, NLOWER,
1578 UPPER, NUPPER
1579 };
1580 char_u *p;
1581 int extra = 0;
1582
1583 *flagp = WORST; /* Tentatively. */
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00001584 cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL;
1585 cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586
1587 c = getchr();
1588 switch (c)
1589 {
1590 case Magic('^'):
1591 ret = regnode(BOL);
1592 break;
1593
1594 case Magic('$'):
1595 ret = regnode(EOL);
1596#if defined(FEAT_SYN_HL) || defined(PROTO)
1597 had_eol = TRUE;
1598#endif
1599 break;
1600
1601 case Magic('<'):
1602 ret = regnode(BOW);
1603 break;
1604
1605 case Magic('>'):
1606 ret = regnode(EOW);
1607 break;
1608
1609 case Magic('_'):
1610 c = no_Magic(getchr());
1611 if (c == '^') /* "\_^" is start-of-line */
1612 {
1613 ret = regnode(BOL);
1614 break;
1615 }
1616 if (c == '$') /* "\_$" is end-of-line */
1617 {
1618 ret = regnode(EOL);
1619#if defined(FEAT_SYN_HL) || defined(PROTO)
1620 had_eol = TRUE;
1621#endif
1622 break;
1623 }
1624
1625 extra = ADD_NL;
1626 *flagp |= HASNL;
1627
1628 /* "\_[" is character range plus newline */
1629 if (c == '[')
1630 goto collection;
1631
1632 /* "\_x" is character class plus newline */
1633 /*FALLTHROUGH*/
1634
1635 /*
1636 * Character classes.
1637 */
1638 case Magic('.'):
1639 case Magic('i'):
1640 case Magic('I'):
1641 case Magic('k'):
1642 case Magic('K'):
1643 case Magic('f'):
1644 case Magic('F'):
1645 case Magic('p'):
1646 case Magic('P'):
1647 case Magic('s'):
1648 case Magic('S'):
1649 case Magic('d'):
1650 case Magic('D'):
1651 case Magic('x'):
1652 case Magic('X'):
1653 case Magic('o'):
1654 case Magic('O'):
1655 case Magic('w'):
1656 case Magic('W'):
1657 case Magic('h'):
1658 case Magic('H'):
1659 case Magic('a'):
1660 case Magic('A'):
1661 case Magic('l'):
1662 case Magic('L'):
1663 case Magic('u'):
1664 case Magic('U'):
1665 p = vim_strchr(classchars, no_Magic(c));
1666 if (p == NULL)
1667 EMSG_RET_NULL(_("E63: invalid use of \\_"));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001668#ifdef FEAT_MBYTE
1669 /* When '.' is followed by a composing char ignore the dot, so that
1670 * the composing char is matched here. */
1671 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
1672 {
1673 c = getchr();
1674 goto do_multibyte;
1675 }
1676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 ret = regnode(classcodes[p - classchars] + extra);
1678 *flagp |= HASWIDTH | SIMPLE;
1679 break;
1680
1681 case Magic('n'):
1682 if (reg_string)
1683 {
1684 /* In a string "\n" matches a newline character. */
1685 ret = regnode(EXACTLY);
1686 regc(NL);
1687 regc(NUL);
1688 *flagp |= HASWIDTH | SIMPLE;
1689 }
1690 else
1691 {
1692 /* In buffer text "\n" matches the end of a line. */
1693 ret = regnode(NEWL);
1694 *flagp |= HASWIDTH | HASNL;
1695 }
1696 break;
1697
1698 case Magic('('):
1699 if (one_exactly)
1700 EMSG_ONE_RET_NULL;
1701 ret = reg(REG_PAREN, &flags);
1702 if (ret == NULL)
1703 return NULL;
1704 *flagp |= flags & (HASWIDTH | SPSTART | HASNL | HASLOOKBH);
1705 break;
1706
1707 case NUL:
1708 case Magic('|'):
1709 case Magic('&'):
1710 case Magic(')'):
1711 EMSG_RET_NULL(_(e_internal)); /* Supposed to be caught earlier. */
1712 /* NOTREACHED */
1713
1714 case Magic('='):
1715 case Magic('?'):
1716 case Magic('+'):
1717 case Magic('@'):
1718 case Magic('{'):
1719 case Magic('*'):
1720 c = no_Magic(c);
1721 sprintf((char *)IObuff, _("E64: %s%c follows nothing"),
1722 (c == '*' ? reg_magic >= MAGIC_ON : reg_magic == MAGIC_ALL)
1723 ? "" : "\\", c);
1724 EMSG_RET_NULL(IObuff);
1725 /* NOTREACHED */
1726
1727 case Magic('~'): /* previous substitute pattern */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001728 if (reg_prev_sub != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 {
1730 char_u *lp;
1731
1732 ret = regnode(EXACTLY);
1733 lp = reg_prev_sub;
1734 while (*lp != NUL)
1735 regc(*lp++);
1736 regc(NUL);
1737 if (*reg_prev_sub != NUL)
1738 {
1739 *flagp |= HASWIDTH;
1740 if ((lp - reg_prev_sub) == 1)
1741 *flagp |= SIMPLE;
1742 }
1743 }
1744 else
1745 EMSG_RET_NULL(_(e_nopresub));
1746 break;
1747
1748 case Magic('1'):
1749 case Magic('2'):
1750 case Magic('3'):
1751 case Magic('4'):
1752 case Magic('5'):
1753 case Magic('6'):
1754 case Magic('7'):
1755 case Magic('8'):
1756 case Magic('9'):
1757 {
1758 int refnum;
1759
1760 refnum = c - Magic('0');
1761 /*
1762 * Check if the back reference is legal. We must have seen the
1763 * close brace.
1764 * TODO: Should also check that we don't refer to something
1765 * that is repeated (+*=): what instance of the repetition
1766 * should we match?
1767 */
1768 if (!had_endbrace[refnum])
1769 {
1770 /* Trick: check if "@<=" or "@<!" follows, in which case
1771 * the \1 can appear before the referenced match. */
1772 for (p = regparse; *p != NUL; ++p)
1773 if (p[0] == '@' && p[1] == '<'
1774 && (p[2] == '!' || p[2] == '='))
1775 break;
1776 if (*p == NUL)
1777 EMSG_RET_NULL(_("E65: Illegal back reference"));
1778 }
1779 ret = regnode(BACKREF + refnum);
1780 }
1781 break;
1782
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 case Magic('z'):
1784 {
1785 c = no_Magic(getchr());
1786 switch (c)
1787 {
Bram Moolenaarc4956c82006-03-12 21:58:43 +00001788#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 case '(': if (reg_do_extmatch != REX_SET)
1790 EMSG_RET_NULL(_("E66: \\z( not allowed here"));
1791 if (one_exactly)
1792 EMSG_ONE_RET_NULL;
1793 ret = reg(REG_ZPAREN, &flags);
1794 if (ret == NULL)
1795 return NULL;
1796 *flagp |= flags & (HASWIDTH|SPSTART|HASNL|HASLOOKBH);
1797 re_has_z = REX_SET;
1798 break;
1799
1800 case '1':
1801 case '2':
1802 case '3':
1803 case '4':
1804 case '5':
1805 case '6':
1806 case '7':
1807 case '8':
1808 case '9': if (reg_do_extmatch != REX_USE)
1809 EMSG_RET_NULL(_("E67: \\z1 et al. not allowed here"));
1810 ret = regnode(ZREF + c - '0');
1811 re_has_z = REX_USE;
1812 break;
Bram Moolenaarc4956c82006-03-12 21:58:43 +00001813#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814
1815 case 's': ret = regnode(MOPEN + 0);
1816 break;
1817
1818 case 'e': ret = regnode(MCLOSE + 0);
1819 break;
1820
1821 default: EMSG_RET_NULL(_("E68: Invalid character after \\z"));
1822 }
1823 }
1824 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825
1826 case Magic('%'):
1827 {
1828 c = no_Magic(getchr());
1829 switch (c)
1830 {
1831 /* () without a back reference */
1832 case '(':
1833 if (one_exactly)
1834 EMSG_ONE_RET_NULL;
1835 ret = reg(REG_NPAREN, &flags);
1836 if (ret == NULL)
1837 return NULL;
1838 *flagp |= flags & (HASWIDTH | SPSTART | HASNL | HASLOOKBH);
1839 break;
1840
1841 /* Catch \%^ and \%$ regardless of where they appear in the
1842 * pattern -- regardless of whether or not it makes sense. */
1843 case '^':
1844 ret = regnode(RE_BOF);
1845 break;
1846
1847 case '$':
1848 ret = regnode(RE_EOF);
1849 break;
1850
1851 case '#':
1852 ret = regnode(CURSOR);
1853 break;
1854
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001855 case 'V':
1856 ret = regnode(RE_VISUAL);
1857 break;
1858
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 /* \%[abc]: Emit as a list of branches, all ending at the last
1860 * branch which matches nothing. */
1861 case '[':
1862 if (one_exactly) /* doesn't nest */
1863 EMSG_ONE_RET_NULL;
1864 {
1865 char_u *lastbranch;
1866 char_u *lastnode = NULL;
1867 char_u *br;
1868
1869 ret = NULL;
1870 while ((c = getchr()) != ']')
1871 {
1872 if (c == NUL)
1873 EMSG_M_RET_NULL(_("E69: Missing ] after %s%%["),
1874 reg_magic == MAGIC_ALL);
1875 br = regnode(BRANCH);
1876 if (ret == NULL)
1877 ret = br;
1878 else
1879 regtail(lastnode, br);
1880
1881 ungetchr();
1882 one_exactly = TRUE;
1883 lastnode = regatom(flagp);
1884 one_exactly = FALSE;
1885 if (lastnode == NULL)
1886 return NULL;
1887 }
1888 if (ret == NULL)
1889 EMSG_M_RET_NULL(_("E70: Empty %s%%[]"),
1890 reg_magic == MAGIC_ALL);
1891 lastbranch = regnode(BRANCH);
1892 br = regnode(NOTHING);
1893 if (ret != JUST_CALC_SIZE)
1894 {
1895 regtail(lastnode, br);
1896 regtail(lastbranch, br);
1897 /* connect all branches to the NOTHING
1898 * branch at the end */
1899 for (br = ret; br != lastnode; )
1900 {
1901 if (OP(br) == BRANCH)
1902 {
1903 regtail(br, lastbranch);
1904 br = OPERAND(br);
1905 }
1906 else
1907 br = regnext(br);
1908 }
1909 }
1910 *flagp &= ~HASWIDTH;
1911 break;
1912 }
1913
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001914 case 'd': /* %d123 decimal */
1915 case 'o': /* %o123 octal */
1916 case 'x': /* %xab hex 2 */
1917 case 'u': /* %uabcd hex 4 */
1918 case 'U': /* %U1234abcd hex 8 */
1919 {
1920 int i;
1921
1922 switch (c)
1923 {
1924 case 'd': i = getdecchrs(); break;
1925 case 'o': i = getoctchrs(); break;
1926 case 'x': i = gethexchrs(2); break;
1927 case 'u': i = gethexchrs(4); break;
1928 case 'U': i = gethexchrs(8); break;
1929 default: i = -1; break;
1930 }
1931
1932 if (i < 0)
1933 EMSG_M_RET_NULL(
1934 _("E678: Invalid character after %s%%[dxouU]"),
1935 reg_magic == MAGIC_ALL);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001936#ifdef FEAT_MBYTE
1937 if (use_multibytecode(i))
1938 ret = regnode(MULTIBYTECODE);
1939 else
1940#endif
1941 ret = regnode(EXACTLY);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001942 if (i == 0)
1943 regc(0x0a);
1944 else
1945#ifdef FEAT_MBYTE
1946 regmbc(i);
1947#else
1948 regc(i);
1949#endif
1950 regc(NUL);
1951 *flagp |= HASWIDTH;
1952 break;
1953 }
1954
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 default:
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001956 if (VIM_ISDIGIT(c) || c == '<' || c == '>'
1957 || c == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 {
1959 long_u n = 0;
1960 int cmp;
1961
1962 cmp = c;
1963 if (cmp == '<' || cmp == '>')
1964 c = getchr();
1965 while (VIM_ISDIGIT(c))
1966 {
1967 n = n * 10 + (c - '0');
1968 c = getchr();
1969 }
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001970 if (c == '\'' && n == 0)
1971 {
1972 /* "\%'m", "\%<'m" and "\%>'m": Mark */
1973 c = getchr();
1974 ret = regnode(RE_MARK);
1975 if (ret == JUST_CALC_SIZE)
1976 regsize += 2;
1977 else
1978 {
1979 *regcode++ = c;
1980 *regcode++ = cmp;
1981 }
1982 break;
1983 }
1984 else if (c == 'l' || c == 'c' || c == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 {
1986 if (c == 'l')
1987 ret = regnode(RE_LNUM);
1988 else if (c == 'c')
1989 ret = regnode(RE_COL);
1990 else
1991 ret = regnode(RE_VCOL);
1992 if (ret == JUST_CALC_SIZE)
1993 regsize += 5;
1994 else
1995 {
1996 /* put the number and the optional
1997 * comparator after the opcode */
1998 regcode = re_put_long(regcode, n);
1999 *regcode++ = cmp;
2000 }
2001 break;
2002 }
2003 }
2004
2005 EMSG_M_RET_NULL(_("E71: Invalid character after %s%%"),
2006 reg_magic == MAGIC_ALL);
2007 }
2008 }
2009 break;
2010
2011 case Magic('['):
2012collection:
2013 {
2014 char_u *lp;
2015
2016 /*
2017 * If there is no matching ']', we assume the '[' is a normal
2018 * character. This makes 'incsearch' and ":help [" work.
2019 */
2020 lp = skip_anyof(regparse);
2021 if (*lp == ']') /* there is a matching ']' */
2022 {
2023 int startc = -1; /* > 0 when next '-' is a range */
2024 int endc;
2025
2026 /*
2027 * In a character class, different parsing rules apply.
2028 * Not even \ is special anymore, nothing is.
2029 */
2030 if (*regparse == '^') /* Complement of range. */
2031 {
2032 ret = regnode(ANYBUT + extra);
2033 regparse++;
2034 }
2035 else
2036 ret = regnode(ANYOF + extra);
2037
2038 /* At the start ']' and '-' mean the literal character. */
2039 if (*regparse == ']' || *regparse == '-')
Bram Moolenaardf177f62005-02-22 08:39:57 +00002040 {
2041 startc = *regparse;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 regc(*regparse++);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044
2045 while (*regparse != NUL && *regparse != ']')
2046 {
2047 if (*regparse == '-')
2048 {
2049 ++regparse;
2050 /* The '-' is not used for a range at the end and
2051 * after or before a '\n'. */
2052 if (*regparse == ']' || *regparse == NUL
2053 || startc == -1
2054 || (regparse[0] == '\\' && regparse[1] == 'n'))
2055 {
2056 regc('-');
2057 startc = '-'; /* [--x] is a range */
2058 }
2059 else
2060 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00002061 /* Also accept "a-[.z.]" */
2062 endc = 0;
2063 if (*regparse == '[')
2064 endc = get_coll_element(&regparse);
2065 if (endc == 0)
2066 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067#ifdef FEAT_MBYTE
Bram Moolenaardf177f62005-02-22 08:39:57 +00002068 if (has_mbyte)
2069 endc = mb_ptr2char_adv(&regparse);
2070 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00002072 endc = *regparse++;
2073 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002074
2075 /* Handle \o40, \x20 and \u20AC style sequences */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002076 if (endc == '\\' && !cpo_lit && !cpo_bsl)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002077 endc = coll_get_char();
2078
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 if (startc > endc)
2080 EMSG_RET_NULL(_(e_invrange));
2081#ifdef FEAT_MBYTE
2082 if (has_mbyte && ((*mb_char2len)(startc) > 1
2083 || (*mb_char2len)(endc) > 1))
2084 {
2085 /* Limit to a range of 256 chars */
2086 if (endc > startc + 256)
2087 EMSG_RET_NULL(_(e_invrange));
2088 while (++startc <= endc)
2089 regmbc(startc);
2090 }
2091 else
2092#endif
2093 {
2094#ifdef EBCDIC
2095 int alpha_only = FALSE;
2096
2097 /* for alphabetical range skip the gaps
2098 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
2099 if (isalpha(startc) && isalpha(endc))
2100 alpha_only = TRUE;
2101#endif
2102 while (++startc <= endc)
2103#ifdef EBCDIC
2104 if (!alpha_only || isalpha(startc))
2105#endif
2106 regc(startc);
2107 }
2108 startc = -1;
2109 }
2110 }
2111 /*
2112 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
2113 * accepts "\t", "\e", etc., but only when the 'l' flag in
2114 * 'cpoptions' is not included.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002115 * Posix doesn't recognize backslash at all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 */
2117 else if (*regparse == '\\'
Bram Moolenaardf177f62005-02-22 08:39:57 +00002118 && !cpo_bsl
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
2120 || (!cpo_lit
2121 && vim_strchr(REGEXP_ABBR,
2122 regparse[1]) != NULL)))
2123 {
2124 regparse++;
2125 if (*regparse == 'n')
2126 {
2127 /* '\n' in range: also match NL */
2128 if (ret != JUST_CALC_SIZE)
2129 {
2130 if (*ret == ANYBUT)
2131 *ret = ANYBUT + ADD_NL;
2132 else if (*ret == ANYOF)
2133 *ret = ANYOF + ADD_NL;
2134 /* else: must have had a \n already */
2135 }
2136 *flagp |= HASNL;
2137 regparse++;
2138 startc = -1;
2139 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002140 else if (*regparse == 'd'
2141 || *regparse == 'o'
2142 || *regparse == 'x'
2143 || *regparse == 'u'
2144 || *regparse == 'U')
2145 {
2146 startc = coll_get_char();
2147 if (startc == 0)
2148 regc(0x0a);
2149 else
2150#ifdef FEAT_MBYTE
2151 regmbc(startc);
2152#else
2153 regc(startc);
2154#endif
2155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 else
2157 {
2158 startc = backslash_trans(*regparse++);
2159 regc(startc);
2160 }
2161 }
2162 else if (*regparse == '[')
2163 {
2164 int c_class;
2165 int cu;
2166
Bram Moolenaardf177f62005-02-22 08:39:57 +00002167 c_class = get_char_class(&regparse);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 startc = -1;
2169 /* Characters assumed to be 8 bits! */
2170 switch (c_class)
2171 {
2172 case CLASS_NONE:
Bram Moolenaardf177f62005-02-22 08:39:57 +00002173 c_class = get_equi_class(&regparse);
2174 if (c_class != 0)
2175 {
2176 /* produce equivalence class */
2177 reg_equi_class(c_class);
2178 }
2179 else if ((c_class =
2180 get_coll_element(&regparse)) != 0)
2181 {
2182 /* produce a collating element */
2183 regmbc(c_class);
2184 }
2185 else
2186 {
2187 /* literal '[', allow [[-x] as a range */
2188 startc = *regparse++;
2189 regc(startc);
2190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 break;
2192 case CLASS_ALNUM:
2193 for (cu = 1; cu <= 255; cu++)
2194 if (isalnum(cu))
2195 regc(cu);
2196 break;
2197 case CLASS_ALPHA:
2198 for (cu = 1; cu <= 255; cu++)
2199 if (isalpha(cu))
2200 regc(cu);
2201 break;
2202 case CLASS_BLANK:
2203 regc(' ');
2204 regc('\t');
2205 break;
2206 case CLASS_CNTRL:
2207 for (cu = 1; cu <= 255; cu++)
2208 if (iscntrl(cu))
2209 regc(cu);
2210 break;
2211 case CLASS_DIGIT:
2212 for (cu = 1; cu <= 255; cu++)
2213 if (VIM_ISDIGIT(cu))
2214 regc(cu);
2215 break;
2216 case CLASS_GRAPH:
2217 for (cu = 1; cu <= 255; cu++)
2218 if (isgraph(cu))
2219 regc(cu);
2220 break;
2221 case CLASS_LOWER:
2222 for (cu = 1; cu <= 255; cu++)
2223 if (islower(cu))
2224 regc(cu);
2225 break;
2226 case CLASS_PRINT:
2227 for (cu = 1; cu <= 255; cu++)
2228 if (vim_isprintc(cu))
2229 regc(cu);
2230 break;
2231 case CLASS_PUNCT:
2232 for (cu = 1; cu <= 255; cu++)
2233 if (ispunct(cu))
2234 regc(cu);
2235 break;
2236 case CLASS_SPACE:
2237 for (cu = 9; cu <= 13; cu++)
2238 regc(cu);
2239 regc(' ');
2240 break;
2241 case CLASS_UPPER:
2242 for (cu = 1; cu <= 255; cu++)
2243 if (isupper(cu))
2244 regc(cu);
2245 break;
2246 case CLASS_XDIGIT:
2247 for (cu = 1; cu <= 255; cu++)
2248 if (vim_isxdigit(cu))
2249 regc(cu);
2250 break;
2251 case CLASS_TAB:
2252 regc('\t');
2253 break;
2254 case CLASS_RETURN:
2255 regc('\r');
2256 break;
2257 case CLASS_BACKSPACE:
2258 regc('\b');
2259 break;
2260 case CLASS_ESCAPE:
2261 regc('\033');
2262 break;
2263 }
2264 }
2265 else
2266 {
2267#ifdef FEAT_MBYTE
2268 if (has_mbyte)
2269 {
2270 int len;
2271
2272 /* produce a multibyte character, including any
2273 * following composing characters */
2274 startc = mb_ptr2char(regparse);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002275 len = (*mb_ptr2len)(regparse);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 if (enc_utf8 && utf_char2len(startc) != len)
2277 startc = -1; /* composing chars */
2278 while (--len >= 0)
2279 regc(*regparse++);
2280 }
2281 else
2282#endif
2283 {
2284 startc = *regparse++;
2285 regc(startc);
2286 }
2287 }
2288 }
2289 regc(NUL);
2290 prevchr_len = 1; /* last char was the ']' */
2291 if (*regparse != ']')
2292 EMSG_RET_NULL(_(e_toomsbra)); /* Cannot happen? */
2293 skipchr(); /* let's be friends with the lexer again */
2294 *flagp |= HASWIDTH | SIMPLE;
2295 break;
2296 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002297 else if (reg_strict)
2298 EMSG_M_RET_NULL(_("E769: Missing ] after %s["),
2299 reg_magic > MAGIC_OFF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 }
2301 /* FALLTHROUGH */
2302
2303 default:
2304 {
2305 int len;
2306
2307#ifdef FEAT_MBYTE
2308 /* A multi-byte character is handled as a separate atom if it's
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002309 * before a multi and when it's a composing char. */
2310 if (use_multibytecode(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002312do_multibyte:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 ret = regnode(MULTIBYTECODE);
2314 regmbc(c);
2315 *flagp |= HASWIDTH | SIMPLE;
2316 break;
2317 }
2318#endif
2319
2320 ret = regnode(EXACTLY);
2321
2322 /*
2323 * Append characters as long as:
2324 * - there is no following multi, we then need the character in
2325 * front of it as a single character operand
2326 * - not running into a Magic character
2327 * - "one_exactly" is not set
2328 * But always emit at least one character. Might be a Multi,
2329 * e.g., a "[" without matching "]".
2330 */
2331 for (len = 0; c != NUL && (len == 0
2332 || (re_multi_type(peekchr()) == NOT_MULTI
2333 && !one_exactly
2334 && !is_Magic(c))); ++len)
2335 {
2336 c = no_Magic(c);
2337#ifdef FEAT_MBYTE
2338 if (has_mbyte)
2339 {
2340 regmbc(c);
2341 if (enc_utf8)
2342 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 int l;
2344
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002345 /* Need to get composing character too. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 for (;;)
2347 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002348 l = utf_ptr2len(regparse);
2349 if (!UTF_COMPOSINGLIKE(regparse, regparse + l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 break;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002351 regmbc(utf_ptr2char(regparse));
2352 skipchr();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 }
2355 }
2356 else
2357#endif
2358 regc(c);
2359 c = getchr();
2360 }
2361 ungetchr();
2362
2363 regc(NUL);
2364 *flagp |= HASWIDTH;
2365 if (len == 1)
2366 *flagp |= SIMPLE;
2367 }
2368 break;
2369 }
2370
2371 return ret;
2372}
2373
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002374#ifdef FEAT_MBYTE
2375/*
2376 * Return TRUE if MULTIBYTECODE should be used instead of EXACTLY for
2377 * character "c".
2378 */
2379 static int
2380use_multibytecode(c)
2381 int c;
2382{
2383 return has_mbyte && (*mb_char2len)(c) > 1
2384 && (re_multi_type(peekchr()) != NOT_MULTI
2385 || (enc_utf8 && utf_iscomposing(c)));
2386}
2387#endif
2388
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389/*
2390 * emit a node
2391 * Return pointer to generated code.
2392 */
2393 static char_u *
2394regnode(op)
2395 int op;
2396{
2397 char_u *ret;
2398
2399 ret = regcode;
2400 if (ret == JUST_CALC_SIZE)
2401 regsize += 3;
2402 else
2403 {
2404 *regcode++ = op;
2405 *regcode++ = NUL; /* Null "next" pointer. */
2406 *regcode++ = NUL;
2407 }
2408 return ret;
2409}
2410
2411/*
2412 * Emit (if appropriate) a byte of code
2413 */
2414 static void
2415regc(b)
2416 int b;
2417{
2418 if (regcode == JUST_CALC_SIZE)
2419 regsize++;
2420 else
2421 *regcode++ = b;
2422}
2423
2424#ifdef FEAT_MBYTE
2425/*
2426 * Emit (if appropriate) a multi-byte character of code
2427 */
2428 static void
2429regmbc(c)
2430 int c;
2431{
2432 if (regcode == JUST_CALC_SIZE)
2433 regsize += (*mb_char2len)(c);
2434 else
2435 regcode += (*mb_char2bytes)(c, regcode);
2436}
2437#endif
2438
2439/*
2440 * reginsert - insert an operator in front of already-emitted operand
2441 *
2442 * Means relocating the operand.
2443 */
2444 static void
2445reginsert(op, opnd)
2446 int op;
2447 char_u *opnd;
2448{
2449 char_u *src;
2450 char_u *dst;
2451 char_u *place;
2452
2453 if (regcode == JUST_CALC_SIZE)
2454 {
2455 regsize += 3;
2456 return;
2457 }
2458 src = regcode;
2459 regcode += 3;
2460 dst = regcode;
2461 while (src > opnd)
2462 *--dst = *--src;
2463
2464 place = opnd; /* Op node, where operand used to be. */
2465 *place++ = op;
2466 *place++ = NUL;
2467 *place = NUL;
2468}
2469
2470/*
2471 * reginsert_limits - insert an operator in front of already-emitted operand.
2472 * The operator has the given limit values as operands. Also set next pointer.
2473 *
2474 * Means relocating the operand.
2475 */
2476 static void
2477reginsert_limits(op, minval, maxval, opnd)
2478 int op;
2479 long minval;
2480 long maxval;
2481 char_u *opnd;
2482{
2483 char_u *src;
2484 char_u *dst;
2485 char_u *place;
2486
2487 if (regcode == JUST_CALC_SIZE)
2488 {
2489 regsize += 11;
2490 return;
2491 }
2492 src = regcode;
2493 regcode += 11;
2494 dst = regcode;
2495 while (src > opnd)
2496 *--dst = *--src;
2497
2498 place = opnd; /* Op node, where operand used to be. */
2499 *place++ = op;
2500 *place++ = NUL;
2501 *place++ = NUL;
2502 place = re_put_long(place, (long_u)minval);
2503 place = re_put_long(place, (long_u)maxval);
2504 regtail(opnd, place);
2505}
2506
2507/*
2508 * Write a long as four bytes at "p" and return pointer to the next char.
2509 */
2510 static char_u *
2511re_put_long(p, val)
2512 char_u *p;
2513 long_u val;
2514{
2515 *p++ = (char_u) ((val >> 24) & 0377);
2516 *p++ = (char_u) ((val >> 16) & 0377);
2517 *p++ = (char_u) ((val >> 8) & 0377);
2518 *p++ = (char_u) (val & 0377);
2519 return p;
2520}
2521
2522/*
2523 * regtail - set the next-pointer at the end of a node chain
2524 */
2525 static void
2526regtail(p, val)
2527 char_u *p;
2528 char_u *val;
2529{
2530 char_u *scan;
2531 char_u *temp;
2532 int offset;
2533
2534 if (p == JUST_CALC_SIZE)
2535 return;
2536
2537 /* Find last node. */
2538 scan = p;
2539 for (;;)
2540 {
2541 temp = regnext(scan);
2542 if (temp == NULL)
2543 break;
2544 scan = temp;
2545 }
2546
Bram Moolenaar582fd852005-03-28 20:58:01 +00002547 if (OP(scan) == BACK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 offset = (int)(scan - val);
2549 else
2550 offset = (int)(val - scan);
2551 *(scan + 1) = (char_u) (((unsigned)offset >> 8) & 0377);
2552 *(scan + 2) = (char_u) (offset & 0377);
2553}
2554
2555/*
2556 * regoptail - regtail on item after a BRANCH; nop if none
2557 */
2558 static void
2559regoptail(p, val)
2560 char_u *p;
2561 char_u *val;
2562{
2563 /* When op is neither BRANCH nor BRACE_COMPLEX0-9, it is "operandless" */
2564 if (p == NULL || p == JUST_CALC_SIZE
2565 || (OP(p) != BRANCH
2566 && (OP(p) < BRACE_COMPLEX || OP(p) > BRACE_COMPLEX + 9)))
2567 return;
2568 regtail(OPERAND(p), val);
2569}
2570
2571/*
2572 * getchr() - get the next character from the pattern. We know about
2573 * magic and such, so therefore we need a lexical analyzer.
2574 */
2575
2576/* static int curchr; */
2577static int prevprevchr;
2578static int prevchr;
2579static int nextchr; /* used for ungetchr() */
2580/*
2581 * Note: prevchr is sometimes -1 when we are not at the start,
2582 * eg in /[ ^I]^ the pattern was never found even if it existed, because ^ was
2583 * taken to be magic -- webb
2584 */
2585static int at_start; /* True when on the first character */
2586static int prev_at_start; /* True when on the second character */
2587
2588 static void
2589initchr(str)
2590 char_u *str;
2591{
2592 regparse = str;
2593 prevchr_len = 0;
2594 curchr = prevprevchr = prevchr = nextchr = -1;
2595 at_start = TRUE;
2596 prev_at_start = FALSE;
2597}
2598
2599 static int
2600peekchr()
2601{
Bram Moolenaardf177f62005-02-22 08:39:57 +00002602 static int after_slash = FALSE;
2603
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 if (curchr == -1)
2605 {
2606 switch (curchr = regparse[0])
2607 {
2608 case '.':
2609 case '[':
2610 case '~':
2611 /* magic when 'magic' is on */
2612 if (reg_magic >= MAGIC_ON)
2613 curchr = Magic(curchr);
2614 break;
2615 case '(':
2616 case ')':
2617 case '{':
2618 case '%':
2619 case '+':
2620 case '=':
2621 case '?':
2622 case '@':
2623 case '!':
2624 case '&':
2625 case '|':
2626 case '<':
2627 case '>':
2628 case '#': /* future ext. */
2629 case '"': /* future ext. */
2630 case '\'': /* future ext. */
2631 case ',': /* future ext. */
2632 case '-': /* future ext. */
2633 case ':': /* future ext. */
2634 case ';': /* future ext. */
2635 case '`': /* future ext. */
2636 case '/': /* Can't be used in / command */
2637 /* magic only after "\v" */
2638 if (reg_magic == MAGIC_ALL)
2639 curchr = Magic(curchr);
2640 break;
2641 case '*':
Bram Moolenaardf177f62005-02-22 08:39:57 +00002642 /* * is not magic as the very first character, eg "?*ptr", when
2643 * after '^', eg "/^*ptr" and when after "\(", "\|", "\&". But
2644 * "\(\*" is not magic, thus must be magic if "after_slash" */
2645 if (reg_magic >= MAGIC_ON
2646 && !at_start
2647 && !(prev_at_start && prevchr == Magic('^'))
2648 && (after_slash
2649 || (prevchr != Magic('(')
2650 && prevchr != Magic('&')
2651 && prevchr != Magic('|'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 curchr = Magic('*');
2653 break;
2654 case '^':
2655 /* '^' is only magic as the very first character and if it's after
2656 * "\(", "\|", "\&' or "\n" */
2657 if (reg_magic >= MAGIC_OFF
2658 && (at_start
2659 || reg_magic == MAGIC_ALL
2660 || prevchr == Magic('(')
2661 || prevchr == Magic('|')
2662 || prevchr == Magic('&')
2663 || prevchr == Magic('n')
2664 || (no_Magic(prevchr) == '('
2665 && prevprevchr == Magic('%'))))
2666 {
2667 curchr = Magic('^');
2668 at_start = TRUE;
2669 prev_at_start = FALSE;
2670 }
2671 break;
2672 case '$':
2673 /* '$' is only magic as the very last char and if it's in front of
2674 * either "\|", "\)", "\&", or "\n" */
2675 if (reg_magic >= MAGIC_OFF)
2676 {
2677 char_u *p = regparse + 1;
2678
2679 /* ignore \c \C \m and \M after '$' */
2680 while (p[0] == '\\' && (p[1] == 'c' || p[1] == 'C'
2681 || p[1] == 'm' || p[1] == 'M' || p[1] == 'Z'))
2682 p += 2;
2683 if (p[0] == NUL
2684 || (p[0] == '\\'
2685 && (p[1] == '|' || p[1] == '&' || p[1] == ')'
2686 || p[1] == 'n'))
2687 || reg_magic == MAGIC_ALL)
2688 curchr = Magic('$');
2689 }
2690 break;
2691 case '\\':
2692 {
2693 int c = regparse[1];
2694
2695 if (c == NUL)
2696 curchr = '\\'; /* trailing '\' */
2697 else if (
2698#ifdef EBCDIC
2699 vim_strchr(META, c)
2700#else
2701 c <= '~' && META_flags[c]
2702#endif
2703 )
2704 {
2705 /*
2706 * META contains everything that may be magic sometimes,
2707 * except ^ and $ ("\^" and "\$" are only magic after
2708 * "\v"). We now fetch the next character and toggle its
2709 * magicness. Therefore, \ is so meta-magic that it is
2710 * not in META.
2711 */
2712 curchr = -1;
2713 prev_at_start = at_start;
2714 at_start = FALSE; /* be able to say "/\*ptr" */
2715 ++regparse;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002716 ++after_slash;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 peekchr();
2718 --regparse;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002719 --after_slash;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 curchr = toggle_Magic(curchr);
2721 }
2722 else if (vim_strchr(REGEXP_ABBR, c))
2723 {
2724 /*
2725 * Handle abbreviations, like "\t" for TAB -- webb
2726 */
2727 curchr = backslash_trans(c);
2728 }
2729 else if (reg_magic == MAGIC_NONE && (c == '$' || c == '^'))
2730 curchr = toggle_Magic(c);
2731 else
2732 {
2733 /*
2734 * Next character can never be (made) magic?
2735 * Then backslashing it won't do anything.
2736 */
2737#ifdef FEAT_MBYTE
2738 if (has_mbyte)
2739 curchr = (*mb_ptr2char)(regparse + 1);
2740 else
2741#endif
2742 curchr = c;
2743 }
2744 break;
2745 }
2746
2747#ifdef FEAT_MBYTE
2748 default:
2749 if (has_mbyte)
2750 curchr = (*mb_ptr2char)(regparse);
2751#endif
2752 }
2753 }
2754
2755 return curchr;
2756}
2757
2758/*
2759 * Eat one lexed character. Do this in a way that we can undo it.
2760 */
2761 static void
2762skipchr()
2763{
2764 /* peekchr() eats a backslash, do the same here */
2765 if (*regparse == '\\')
2766 prevchr_len = 1;
2767 else
2768 prevchr_len = 0;
2769 if (regparse[prevchr_len] != NUL)
2770 {
2771#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002772 if (enc_utf8)
2773 prevchr_len += utf_char2len(mb_ptr2char(regparse + prevchr_len));
2774 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002775 prevchr_len += (*mb_ptr2len)(regparse + prevchr_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776 else
2777#endif
2778 ++prevchr_len;
2779 }
2780 regparse += prevchr_len;
2781 prev_at_start = at_start;
2782 at_start = FALSE;
2783 prevprevchr = prevchr;
2784 prevchr = curchr;
2785 curchr = nextchr; /* use previously unget char, or -1 */
2786 nextchr = -1;
2787}
2788
2789/*
2790 * Skip a character while keeping the value of prev_at_start for at_start.
2791 * prevchr and prevprevchr are also kept.
2792 */
2793 static void
2794skipchr_keepstart()
2795{
2796 int as = prev_at_start;
2797 int pr = prevchr;
2798 int prpr = prevprevchr;
2799
2800 skipchr();
2801 at_start = as;
2802 prevchr = pr;
2803 prevprevchr = prpr;
2804}
2805
2806 static int
2807getchr()
2808{
2809 int chr = peekchr();
2810
2811 skipchr();
2812 return chr;
2813}
2814
2815/*
2816 * put character back. Works only once!
2817 */
2818 static void
2819ungetchr()
2820{
2821 nextchr = curchr;
2822 curchr = prevchr;
2823 prevchr = prevprevchr;
2824 at_start = prev_at_start;
2825 prev_at_start = FALSE;
2826
2827 /* Backup regparse, so that it's at the same position as before the
2828 * getchr(). */
2829 regparse -= prevchr_len;
2830}
2831
2832/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002833 * Get and return the value of the hex string at the current position.
2834 * Return -1 if there is no valid hex number.
2835 * The position is updated:
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002836 * blahblah\%x20asdf
2837 * before-^ ^-after
2838 * The parameter controls the maximum number of input characters. This will be
2839 * 2 when reading a \%x20 sequence and 4 when reading a \%u20AC sequence.
2840 */
2841 static int
2842gethexchrs(maxinputlen)
2843 int maxinputlen;
2844{
2845 int nr = 0;
2846 int c;
2847 int i;
2848
2849 for (i = 0; i < maxinputlen; ++i)
2850 {
2851 c = regparse[0];
2852 if (!vim_isxdigit(c))
2853 break;
2854 nr <<= 4;
2855 nr |= hex2nr(c);
2856 ++regparse;
2857 }
2858
2859 if (i == 0)
2860 return -1;
2861 return nr;
2862}
2863
2864/*
2865 * get and return the value of the decimal string immediately after the
2866 * current position. Return -1 for invalid. Consumes all digits.
2867 */
2868 static int
2869getdecchrs()
2870{
2871 int nr = 0;
2872 int c;
2873 int i;
2874
2875 for (i = 0; ; ++i)
2876 {
2877 c = regparse[0];
2878 if (c < '0' || c > '9')
2879 break;
2880 nr *= 10;
2881 nr += c - '0';
2882 ++regparse;
2883 }
2884
2885 if (i == 0)
2886 return -1;
2887 return nr;
2888}
2889
2890/*
2891 * get and return the value of the octal string immediately after the current
2892 * position. Return -1 for invalid, or 0-255 for valid. Smart enough to handle
2893 * numbers > 377 correctly (for example, 400 is treated as 40) and doesn't
2894 * treat 8 or 9 as recognised characters. Position is updated:
2895 * blahblah\%o210asdf
2896 * before-^ ^-after
2897 */
2898 static int
2899getoctchrs()
2900{
2901 int nr = 0;
2902 int c;
2903 int i;
2904
2905 for (i = 0; i < 3 && nr < 040; ++i)
2906 {
2907 c = regparse[0];
2908 if (c < '0' || c > '7')
2909 break;
2910 nr <<= 3;
2911 nr |= hex2nr(c);
2912 ++regparse;
2913 }
2914
2915 if (i == 0)
2916 return -1;
2917 return nr;
2918}
2919
2920/*
2921 * Get a number after a backslash that is inside [].
2922 * When nothing is recognized return a backslash.
2923 */
2924 static int
2925coll_get_char()
2926{
2927 int nr = -1;
2928
2929 switch (*regparse++)
2930 {
2931 case 'd': nr = getdecchrs(); break;
2932 case 'o': nr = getoctchrs(); break;
2933 case 'x': nr = gethexchrs(2); break;
2934 case 'u': nr = gethexchrs(4); break;
2935 case 'U': nr = gethexchrs(8); break;
2936 }
2937 if (nr < 0)
2938 {
2939 /* If getting the number fails be backwards compatible: the character
2940 * is a backslash. */
2941 --regparse;
2942 nr = '\\';
2943 }
2944 return nr;
2945}
2946
2947/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 * read_limits - Read two integers to be taken as a minimum and maximum.
2949 * If the first character is '-', then the range is reversed.
2950 * Should end with 'end'. If minval is missing, zero is default, if maxval is
2951 * missing, a very big number is the default.
2952 */
2953 static int
2954read_limits(minval, maxval)
2955 long *minval;
2956 long *maxval;
2957{
2958 int reverse = FALSE;
2959 char_u *first_char;
2960 long tmp;
2961
2962 if (*regparse == '-')
2963 {
2964 /* Starts with '-', so reverse the range later */
2965 regparse++;
2966 reverse = TRUE;
2967 }
2968 first_char = regparse;
2969 *minval = getdigits(&regparse);
2970 if (*regparse == ',') /* There is a comma */
2971 {
2972 if (vim_isdigit(*++regparse))
2973 *maxval = getdigits(&regparse);
2974 else
2975 *maxval = MAX_LIMIT;
2976 }
2977 else if (VIM_ISDIGIT(*first_char))
2978 *maxval = *minval; /* It was \{n} or \{-n} */
2979 else
2980 *maxval = MAX_LIMIT; /* It was \{} or \{-} */
2981 if (*regparse == '\\')
2982 regparse++; /* Allow either \{...} or \{...\} */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002983 if (*regparse != '}')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 {
2985 sprintf((char *)IObuff, _("E554: Syntax error in %s{...}"),
2986 reg_magic == MAGIC_ALL ? "" : "\\");
2987 EMSG_RET_FAIL(IObuff);
2988 }
2989
2990 /*
2991 * Reverse the range if there was a '-', or make sure it is in the right
2992 * order otherwise.
2993 */
2994 if ((!reverse && *minval > *maxval) || (reverse && *minval < *maxval))
2995 {
2996 tmp = *minval;
2997 *minval = *maxval;
2998 *maxval = tmp;
2999 }
3000 skipchr(); /* let's be friends with the lexer again */
3001 return OK;
3002}
3003
3004/*
3005 * vim_regexec and friends
3006 */
3007
3008/*
3009 * Global work variables for vim_regexec().
3010 */
3011
3012/* The current match-position is remembered with these variables: */
3013static linenr_T reglnum; /* line number, relative to first line */
3014static char_u *regline; /* start of current line */
3015static char_u *reginput; /* current input, points into "regline" */
3016
3017static int need_clear_subexpr; /* subexpressions still need to be
3018 * cleared */
3019#ifdef FEAT_SYN_HL
3020static int need_clear_zsubexpr = FALSE; /* extmatch subexpressions
3021 * still need to be cleared */
3022#endif
3023
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024/*
3025 * Structure used to save the current input state, when it needs to be
3026 * restored after trying a match. Used by reg_save() and reg_restore().
Bram Moolenaar582fd852005-03-28 20:58:01 +00003027 * Also stores the length of "backpos".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 */
3029typedef struct
3030{
3031 union
3032 {
3033 char_u *ptr; /* reginput pointer, for single-line regexp */
3034 lpos_T pos; /* reginput pos, for multi-line regexp */
3035 } rs_u;
Bram Moolenaar582fd852005-03-28 20:58:01 +00003036 int rs_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037} regsave_T;
3038
3039/* struct to save start/end pointer/position in for \(\) */
3040typedef struct
3041{
3042 union
3043 {
3044 char_u *ptr;
3045 lpos_T pos;
3046 } se_u;
3047} save_se_T;
3048
3049static char_u *reg_getline __ARGS((linenr_T lnum));
3050static long vim_regexec_both __ARGS((char_u *line, colnr_T col));
3051static long regtry __ARGS((regprog_T *prog, colnr_T col));
3052static void cleanup_subexpr __ARGS((void));
3053#ifdef FEAT_SYN_HL
3054static void cleanup_zsubexpr __ARGS((void));
3055#endif
3056static void reg_nextline __ARGS((void));
Bram Moolenaar582fd852005-03-28 20:58:01 +00003057static void reg_save __ARGS((regsave_T *save, garray_T *gap));
3058static void reg_restore __ARGS((regsave_T *save, garray_T *gap));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059static int reg_save_equal __ARGS((regsave_T *save));
3060static void save_se_multi __ARGS((save_se_T *savep, lpos_T *posp));
3061static void save_se_one __ARGS((save_se_T *savep, char_u **pp));
3062
3063/* Save the sub-expressions before attempting a match. */
3064#define save_se(savep, posp, pp) \
3065 REG_MULTI ? save_se_multi((savep), (posp)) : save_se_one((savep), (pp))
3066
3067/* After a failed match restore the sub-expressions. */
3068#define restore_se(savep, posp, pp) { \
3069 if (REG_MULTI) \
3070 *(posp) = (savep)->se_u.pos; \
3071 else \
3072 *(pp) = (savep)->se_u.ptr; }
3073
3074static int re_num_cmp __ARGS((long_u val, char_u *scan));
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003075static int regmatch __ARGS((char_u *prog));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076static int regrepeat __ARGS((char_u *p, long maxcount));
3077
3078#ifdef DEBUG
3079int regnarrate = 0;
3080#endif
3081
3082/*
3083 * Internal copy of 'ignorecase'. It is set at each call to vim_regexec().
3084 * Normally it gets the value of "rm_ic" or "rmm_ic", but when the pattern
3085 * contains '\c' or '\C' the value is overruled.
3086 */
3087static int ireg_ic;
3088
3089#ifdef FEAT_MBYTE
3090/*
3091 * Similar to ireg_ic, but only for 'combining' characters. Set with \Z flag
3092 * in the regexp. Defaults to false, always.
3093 */
3094static int ireg_icombine;
3095#endif
3096
3097/*
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003098 * Copy of "rmm_maxcol": maximum column to search for a match. Zero when
3099 * there is no maximum.
3100 */
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003101static colnr_T ireg_maxcol;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003102
3103/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 * Sometimes need to save a copy of a line. Since alloc()/free() is very
3105 * slow, we keep one allocated piece of memory and only re-allocate it when
3106 * it's too small. It's freed in vim_regexec_both() when finished.
3107 */
3108static char_u *reg_tofree;
3109static unsigned reg_tofreelen;
3110
3111/*
3112 * These variables are set when executing a regexp to speed up the execution.
3113 * Which ones are set depends on whethere a single-line or multi-line match is
3114 * done:
3115 * single-line multi-line
3116 * reg_match &regmatch_T NULL
3117 * reg_mmatch NULL &regmmatch_T
3118 * reg_startp reg_match->startp <invalid>
3119 * reg_endp reg_match->endp <invalid>
3120 * reg_startpos <invalid> reg_mmatch->startpos
3121 * reg_endpos <invalid> reg_mmatch->endpos
3122 * reg_win NULL window in which to search
3123 * reg_buf <invalid> buffer in which to search
3124 * reg_firstlnum <invalid> first line in which to search
3125 * reg_maxline 0 last line nr
3126 * reg_line_lbr FALSE or TRUE FALSE
3127 */
3128static regmatch_T *reg_match;
3129static regmmatch_T *reg_mmatch;
3130static char_u **reg_startp = NULL;
3131static char_u **reg_endp = NULL;
3132static lpos_T *reg_startpos = NULL;
3133static lpos_T *reg_endpos = NULL;
3134static win_T *reg_win;
3135static buf_T *reg_buf;
3136static linenr_T reg_firstlnum;
3137static linenr_T reg_maxline;
3138static int reg_line_lbr; /* "\n" in string is line break */
3139
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003140/* Values for rs_state in regitem_T. */
3141typedef enum regstate_E
3142{
3143 RS_NOPEN = 0 /* NOPEN and NCLOSE */
3144 , RS_MOPEN /* MOPEN + [0-9] */
3145 , RS_MCLOSE /* MCLOSE + [0-9] */
3146#ifdef FEAT_SYN_HL
3147 , RS_ZOPEN /* ZOPEN + [0-9] */
3148 , RS_ZCLOSE /* ZCLOSE + [0-9] */
3149#endif
3150 , RS_BRANCH /* BRANCH */
3151 , RS_BRCPLX_MORE /* BRACE_COMPLEX and trying one more match */
3152 , RS_BRCPLX_LONG /* BRACE_COMPLEX and trying longest match */
3153 , RS_BRCPLX_SHORT /* BRACE_COMPLEX and trying shortest match */
3154 , RS_NOMATCH /* NOMATCH */
3155 , RS_BEHIND1 /* BEHIND / NOBEHIND matching rest */
3156 , RS_BEHIND2 /* BEHIND / NOBEHIND matching behind part */
3157 , RS_STAR_LONG /* STAR/PLUS/BRACE_SIMPLE longest match */
3158 , RS_STAR_SHORT /* STAR/PLUS/BRACE_SIMPLE shortest match */
3159} regstate_T;
3160
3161/*
3162 * When there are alternatives a regstate_T is put on the regstack to remember
3163 * what we are doing.
3164 * Before it may be another type of item, depending on rs_state, to remember
3165 * more things.
3166 */
3167typedef struct regitem_S
3168{
3169 regstate_T rs_state; /* what we are doing, one of RS_ above */
3170 char_u *rs_scan; /* current node in program */
3171 union
3172 {
3173 save_se_T sesave;
3174 regsave_T regsave;
3175 } rs_un; /* room for saving reginput */
3176 short rs_no; /* submatch nr */
3177} regitem_T;
3178
3179static regitem_T *regstack_push __ARGS((regstate_T state, char_u *scan));
3180static void regstack_pop __ARGS((char_u **scan));
3181
3182/* used for BEHIND and NOBEHIND matching */
3183typedef struct regbehind_S
3184{
3185 regsave_T save_after;
3186 regsave_T save_behind;
3187} regbehind_T;
3188
3189/* used for STAR, PLUS and BRACE_SIMPLE matching */
3190typedef struct regstar_S
3191{
3192 int nextb; /* next byte */
3193 int nextb_ic; /* next byte reverse case */
3194 long count;
3195 long minval;
3196 long maxval;
3197} regstar_T;
3198
3199/* used to store input position when a BACK was encountered, so that we now if
3200 * we made any progress since the last time. */
3201typedef struct backpos_S
3202{
3203 char_u *bp_scan; /* "scan" where BACK was encountered */
3204 regsave_T bp_pos; /* last input position */
3205} backpos_T;
3206
3207/*
3208 * regstack and backpos are used by regmatch(). They are kept over calls to
3209 * avoid invoking malloc() and free() often.
3210 */
3211static garray_T regstack; /* stack with regitem_T items, sometimes
3212 preceded by regstar_T or regbehind_T. */
3213static garray_T backpos; /* table with backpos_T for BACK */
3214
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215/*
3216 * Get pointer to the line "lnum", which is relative to "reg_firstlnum".
3217 */
3218 static char_u *
3219reg_getline(lnum)
3220 linenr_T lnum;
3221{
3222 /* when looking behind for a match/no-match lnum is negative. But we
3223 * can't go before line 1 */
3224 if (reg_firstlnum + lnum < 1)
3225 return NULL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00003226 if (lnum > reg_maxline)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003227 /* Must have matched the "\n" in the last line. */
3228 return (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 return ml_get_buf(reg_buf, reg_firstlnum + lnum, FALSE);
3230}
3231
3232static regsave_T behind_pos;
3233
3234#ifdef FEAT_SYN_HL
3235static char_u *reg_startzp[NSUBEXP]; /* Workspace to mark beginning */
3236static char_u *reg_endzp[NSUBEXP]; /* and end of \z(...\) matches */
3237static lpos_T reg_startzpos[NSUBEXP]; /* idem, beginning pos */
3238static lpos_T reg_endzpos[NSUBEXP]; /* idem, end pos */
3239#endif
3240
3241/* TRUE if using multi-line regexp. */
3242#define REG_MULTI (reg_match == NULL)
3243
3244/*
3245 * Match a regexp against a string.
3246 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
3247 * Uses curbuf for line count and 'iskeyword'.
3248 *
3249 * Return TRUE if there is a match, FALSE if not.
3250 */
3251 int
3252vim_regexec(rmp, line, col)
3253 regmatch_T *rmp;
3254 char_u *line; /* string to match against */
3255 colnr_T col; /* column to start looking for match */
3256{
3257 reg_match = rmp;
3258 reg_mmatch = NULL;
3259 reg_maxline = 0;
3260 reg_line_lbr = FALSE;
3261 reg_win = NULL;
3262 ireg_ic = rmp->rm_ic;
3263#ifdef FEAT_MBYTE
3264 ireg_icombine = FALSE;
3265#endif
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003266 ireg_maxcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 return (vim_regexec_both(line, col) != 0);
3268}
3269
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003270#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
3271 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272/*
3273 * Like vim_regexec(), but consider a "\n" in "line" to be a line break.
3274 */
3275 int
3276vim_regexec_nl(rmp, line, col)
3277 regmatch_T *rmp;
3278 char_u *line; /* string to match against */
3279 colnr_T col; /* column to start looking for match */
3280{
3281 reg_match = rmp;
3282 reg_mmatch = NULL;
3283 reg_maxline = 0;
3284 reg_line_lbr = TRUE;
3285 reg_win = NULL;
3286 ireg_ic = rmp->rm_ic;
3287#ifdef FEAT_MBYTE
3288 ireg_icombine = FALSE;
3289#endif
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003290 ireg_maxcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 return (vim_regexec_both(line, col) != 0);
3292}
3293#endif
3294
3295/*
3296 * Match a regexp against multiple lines.
3297 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
3298 * Uses curbuf for line count and 'iskeyword'.
3299 *
3300 * Return zero if there is no match. Return number of lines contained in the
3301 * match otherwise.
3302 */
3303 long
3304vim_regexec_multi(rmp, win, buf, lnum, col)
3305 regmmatch_T *rmp;
3306 win_T *win; /* window in which to search or NULL */
3307 buf_T *buf; /* buffer in which to search */
3308 linenr_T lnum; /* nr of line to start looking for match */
3309 colnr_T col; /* column to start looking for match */
3310{
3311 long r;
3312 buf_T *save_curbuf = curbuf;
3313
3314 reg_match = NULL;
3315 reg_mmatch = rmp;
3316 reg_buf = buf;
3317 reg_win = win;
3318 reg_firstlnum = lnum;
3319 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
3320 reg_line_lbr = FALSE;
3321 ireg_ic = rmp->rmm_ic;
3322#ifdef FEAT_MBYTE
3323 ireg_icombine = FALSE;
3324#endif
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003325 ireg_maxcol = rmp->rmm_maxcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326
3327 /* Need to switch to buffer "buf" to make vim_iswordc() work. */
3328 curbuf = buf;
3329 r = vim_regexec_both(NULL, col);
3330 curbuf = save_curbuf;
3331
3332 return r;
3333}
3334
3335/*
3336 * Match a regexp against a string ("line" points to the string) or multiple
3337 * lines ("line" is NULL, use reg_getline()).
3338 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 static long
3340vim_regexec_both(line, col)
3341 char_u *line;
3342 colnr_T col; /* column to start looking for match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343{
3344 regprog_T *prog;
3345 char_u *s;
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003346 long retval = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347
3348 reg_tofree = NULL;
3349
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003350 /* Init the regstack empty. Use an item size of 1 byte, since we push
3351 * different things onto it. Use a large grow size to avoid reallocating
3352 * it too often. */
3353 ga_init2(&regstack, 1, 10000);
3354
3355 /* Init the backpos table empty. */
3356 ga_init2(&backpos, sizeof(backpos_T), 10);
3357
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 if (REG_MULTI)
3359 {
3360 prog = reg_mmatch->regprog;
3361 line = reg_getline((linenr_T)0);
3362 reg_startpos = reg_mmatch->startpos;
3363 reg_endpos = reg_mmatch->endpos;
3364 }
3365 else
3366 {
3367 prog = reg_match->regprog;
3368 reg_startp = reg_match->startp;
3369 reg_endp = reg_match->endp;
3370 }
3371
3372 /* Be paranoid... */
3373 if (prog == NULL || line == NULL)
3374 {
3375 EMSG(_(e_null));
3376 goto theend;
3377 }
3378
3379 /* Check validity of program. */
3380 if (prog_magic_wrong())
3381 goto theend;
3382
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003383 /* If the start column is past the maximum column: no need to try. */
3384 if (ireg_maxcol > 0 && col >= ireg_maxcol)
3385 goto theend;
3386
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
3388 if (prog->regflags & RF_ICASE)
3389 ireg_ic = TRUE;
3390 else if (prog->regflags & RF_NOICASE)
3391 ireg_ic = FALSE;
3392
3393#ifdef FEAT_MBYTE
3394 /* If pattern contains "\Z" overrule value of ireg_icombine */
3395 if (prog->regflags & RF_ICOMBINE)
3396 ireg_icombine = TRUE;
3397#endif
3398
3399 /* If there is a "must appear" string, look for it. */
3400 if (prog->regmust != NULL)
3401 {
3402 int c;
3403
3404#ifdef FEAT_MBYTE
3405 if (has_mbyte)
3406 c = (*mb_ptr2char)(prog->regmust);
3407 else
3408#endif
3409 c = *prog->regmust;
3410 s = line + col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003411
3412 /*
3413 * This is used very often, esp. for ":global". Use three versions of
3414 * the loop to avoid overhead of conditions.
3415 */
3416 if (!ireg_ic
3417#ifdef FEAT_MBYTE
3418 && !has_mbyte
3419#endif
3420 )
3421 while ((s = vim_strbyte(s, c)) != NULL)
3422 {
3423 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3424 break; /* Found it. */
3425 ++s;
3426 }
3427#ifdef FEAT_MBYTE
3428 else if (!ireg_ic || (!enc_utf8 && mb_char2len(c) > 1))
3429 while ((s = vim_strchr(s, c)) != NULL)
3430 {
3431 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3432 break; /* Found it. */
3433 mb_ptr_adv(s);
3434 }
3435#endif
3436 else
3437 while ((s = cstrchr(s, c)) != NULL)
3438 {
3439 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3440 break; /* Found it. */
3441 mb_ptr_adv(s);
3442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 if (s == NULL) /* Not present. */
3444 goto theend;
3445 }
3446
3447 regline = line;
3448 reglnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449
3450 /* Simplest case: Anchored match need be tried only once. */
3451 if (prog->reganch)
3452 {
3453 int c;
3454
3455#ifdef FEAT_MBYTE
3456 if (has_mbyte)
3457 c = (*mb_ptr2char)(regline + col);
3458 else
3459#endif
3460 c = regline[col];
3461 if (prog->regstart == NUL
3462 || prog->regstart == c
3463 || (ireg_ic && ((
3464#ifdef FEAT_MBYTE
3465 (enc_utf8 && utf_fold(prog->regstart) == utf_fold(c)))
3466 || (c < 255 && prog->regstart < 255 &&
3467#endif
3468 TOLOWER_LOC(prog->regstart) == TOLOWER_LOC(c)))))
3469 retval = regtry(prog, col);
3470 else
3471 retval = 0;
3472 }
3473 else
3474 {
3475 /* Messy cases: unanchored match. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003476 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 {
3478 if (prog->regstart != NUL)
3479 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003480 /* Skip until the char we know it must start with.
3481 * Used often, do some work to avoid call overhead. */
3482 if (!ireg_ic
3483#ifdef FEAT_MBYTE
3484 && !has_mbyte
3485#endif
3486 )
3487 s = vim_strbyte(regline + col, prog->regstart);
3488 else
3489 s = cstrchr(regline + col, prog->regstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 if (s == NULL)
3491 {
3492 retval = 0;
3493 break;
3494 }
3495 col = (int)(s - regline);
3496 }
3497
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003498 /* Check for maximum column to try. */
3499 if (ireg_maxcol > 0 && col >= ireg_maxcol)
3500 {
3501 retval = 0;
3502 break;
3503 }
3504
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 retval = regtry(prog, col);
3506 if (retval > 0)
3507 break;
3508
3509 /* if not currently on the first line, get it again */
3510 if (reglnum != 0)
3511 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 reglnum = 0;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003513 regline = reg_getline((linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 }
3515 if (regline[col] == NUL)
3516 break;
3517#ifdef FEAT_MBYTE
3518 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003519 col += (*mb_ptr2len)(regline + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 else
3521#endif
3522 ++col;
3523 }
3524 }
3525
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 vim_free(reg_tofree);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003528 ga_clear(&regstack);
3529 ga_clear(&backpos);
3530
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 return retval;
3532}
3533
3534#ifdef FEAT_SYN_HL
3535static reg_extmatch_T *make_extmatch __ARGS((void));
3536
3537/*
3538 * Create a new extmatch and mark it as referenced once.
3539 */
3540 static reg_extmatch_T *
3541make_extmatch()
3542{
3543 reg_extmatch_T *em;
3544
3545 em = (reg_extmatch_T *)alloc_clear((unsigned)sizeof(reg_extmatch_T));
3546 if (em != NULL)
3547 em->refcnt = 1;
3548 return em;
3549}
3550
3551/*
3552 * Add a reference to an extmatch.
3553 */
3554 reg_extmatch_T *
3555ref_extmatch(em)
3556 reg_extmatch_T *em;
3557{
3558 if (em != NULL)
3559 em->refcnt++;
3560 return em;
3561}
3562
3563/*
3564 * Remove a reference to an extmatch. If there are no references left, free
3565 * the info.
3566 */
3567 void
3568unref_extmatch(em)
3569 reg_extmatch_T *em;
3570{
3571 int i;
3572
3573 if (em != NULL && --em->refcnt <= 0)
3574 {
3575 for (i = 0; i < NSUBEXP; ++i)
3576 vim_free(em->matches[i]);
3577 vim_free(em);
3578 }
3579}
3580#endif
3581
3582/*
3583 * regtry - try match of "prog" with at regline["col"].
3584 * Returns 0 for failure, number of lines contained in the match otherwise.
3585 */
3586 static long
3587regtry(prog, col)
3588 regprog_T *prog;
3589 colnr_T col;
3590{
3591 reginput = regline + col;
3592 need_clear_subexpr = TRUE;
3593#ifdef FEAT_SYN_HL
3594 /* Clear the external match subpointers if necessary. */
3595 if (prog->reghasz == REX_SET)
3596 need_clear_zsubexpr = TRUE;
3597#endif
3598
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003599 if (regmatch(prog->program + 1) == 0)
3600 return 0;
3601
3602 cleanup_subexpr();
3603 if (REG_MULTI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003605 if (reg_startpos[0].lnum < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003607 reg_startpos[0].lnum = 0;
3608 reg_startpos[0].col = col;
3609 }
3610 if (reg_endpos[0].lnum < 0)
3611 {
3612 reg_endpos[0].lnum = reglnum;
3613 reg_endpos[0].col = (int)(reginput - regline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 }
3615 else
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003616 /* Use line number of "\ze". */
3617 reglnum = reg_endpos[0].lnum;
3618 }
3619 else
3620 {
3621 if (reg_startp[0] == NULL)
3622 reg_startp[0] = regline + col;
3623 if (reg_endp[0] == NULL)
3624 reg_endp[0] = reginput;
3625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626#ifdef FEAT_SYN_HL
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003627 /* Package any found \z(...\) matches for export. Default is none. */
3628 unref_extmatch(re_extmatch_out);
3629 re_extmatch_out = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003631 if (prog->reghasz == REX_SET)
3632 {
3633 int i;
3634
3635 cleanup_zsubexpr();
3636 re_extmatch_out = make_extmatch();
3637 for (i = 0; i < NSUBEXP; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003639 if (REG_MULTI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003641 /* Only accept single line matches. */
3642 if (reg_startzpos[i].lnum >= 0
3643 && reg_endzpos[i].lnum == reg_startzpos[i].lnum)
3644 re_extmatch_out->matches[i] =
3645 vim_strnsave(reg_getline(reg_startzpos[i].lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 + reg_startzpos[i].col,
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003647 reg_endzpos[i].col - reg_startzpos[i].col);
3648 }
3649 else
3650 {
3651 if (reg_startzp[i] != NULL && reg_endzp[i] != NULL)
3652 re_extmatch_out->matches[i] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 vim_strnsave(reg_startzp[i],
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003654 (int)(reg_endzp[i] - reg_startzp[i]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 }
3656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 }
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003658#endif
3659 return 1 + reglnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660}
3661
3662#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663static int reg_prev_class __ARGS((void));
3664
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665/*
3666 * Get class of previous character.
3667 */
3668 static int
3669reg_prev_class()
3670{
3671 if (reginput > regline)
3672 return mb_get_class(reginput - 1
3673 - (*mb_head_off)(regline, reginput - 1));
3674 return -1;
3675}
3676
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003678#define ADVANCE_REGINPUT() mb_ptr_adv(reginput)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679
3680/*
3681 * The arguments from BRACE_LIMITS are stored here. They are actually local
3682 * to regmatch(), but they are here to reduce the amount of stack space used
3683 * (it can be called recursively many times).
3684 */
3685static long bl_minval;
3686static long bl_maxval;
3687
3688/*
3689 * regmatch - main matching routine
3690 *
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003691 * Conceptually the strategy is simple: Check to see whether the current node
3692 * matches, push an item onto the regstack and loop to see whether the rest
3693 * matches, and then act accordingly. In practice we make some effort to
3694 * avoid using the regstack, in particular by going through "ordinary" nodes
3695 * (that don't need to know whether the rest of the match failed) by a nested
3696 * loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 *
3698 * Returns TRUE when there is a match. Leaves reginput and reglnum just after
3699 * the last matched character.
3700 * Returns FALSE when there is no match. Leaves reginput and reglnum in an
3701 * undefined state!
3702 */
3703 static int
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003704regmatch(scan)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 char_u *scan; /* Current node. */
3706{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003707 char_u *next; /* Next node. */
3708 int op;
3709 int c;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003710 regitem_T *rp;
3711 int no;
3712 int status; /* one of the RA_ values: */
3713#define RA_FAIL 1 /* something failed, abort */
3714#define RA_CONT 2 /* continue in inner loop */
3715#define RA_BREAK 3 /* break inner loop */
3716#define RA_MATCH 4 /* successful match */
3717#define RA_NOMATCH 5 /* didn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003719 /* Init the regstack and backpos table empty. They are initialized and
3720 * freed in vim_regexec_both() to reduce malloc()/free() calls. */
3721 regstack.ga_len = 0;
3722 backpos.ga_len = 0;
Bram Moolenaar582fd852005-03-28 20:58:01 +00003723
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003724 /*
Bram Moolenaar582fd852005-03-28 20:58:01 +00003725 * Repeat until "regstack" is empty.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003726 */
3727 for (;;)
3728 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 /* Some patterns my cause a long time to match, even though they are not
3730 * illegal. E.g., "\([a-z]\+\)\+Q". Allow breaking them with CTRL-C. */
3731 fast_breakcheck();
3732
3733#ifdef DEBUG
3734 if (scan != NULL && regnarrate)
3735 {
3736 mch_errmsg(regprop(scan));
3737 mch_errmsg("(\n");
3738 }
3739#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003740
3741 /*
Bram Moolenaar582fd852005-03-28 20:58:01 +00003742 * Repeat for items that can be matched sequentially, without using the
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003743 * regstack.
3744 */
3745 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003747 if (got_int || scan == NULL)
3748 {
3749 status = RA_FAIL;
3750 break;
3751 }
3752 status = RA_CONT;
3753
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754#ifdef DEBUG
3755 if (regnarrate)
3756 {
3757 mch_errmsg(regprop(scan));
3758 mch_errmsg("...\n");
3759# ifdef FEAT_SYN_HL
3760 if (re_extmatch_in != NULL)
3761 {
3762 int i;
3763
3764 mch_errmsg(_("External submatches:\n"));
3765 for (i = 0; i < NSUBEXP; i++)
3766 {
3767 mch_errmsg(" \"");
3768 if (re_extmatch_in->matches[i] != NULL)
3769 mch_errmsg(re_extmatch_in->matches[i]);
3770 mch_errmsg("\"\n");
3771 }
3772 }
3773# endif
3774 }
3775#endif
3776 next = regnext(scan);
3777
3778 op = OP(scan);
3779 /* Check for character class with NL added. */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003780 if (!reg_line_lbr && WITH_NL(op) && *reginput == NUL
3781 && reglnum <= reg_maxline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 {
3783 reg_nextline();
3784 }
3785 else if (reg_line_lbr && WITH_NL(op) && *reginput == '\n')
3786 {
3787 ADVANCE_REGINPUT();
3788 }
3789 else
3790 {
3791 if (WITH_NL(op))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003792 op -= ADD_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793#ifdef FEAT_MBYTE
3794 if (has_mbyte)
3795 c = (*mb_ptr2char)(reginput);
3796 else
3797#endif
3798 c = *reginput;
3799 switch (op)
3800 {
3801 case BOL:
3802 if (reginput != regline)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003803 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 break;
3805
3806 case EOL:
3807 if (c != NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003808 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 break;
3810
3811 case RE_BOF:
3812 /* Passing -1 to the getline() function provided for the search
3813 * should always return NULL if the current line is the first
3814 * line of the file. */
3815 if (reglnum != 0 || reginput != regline
3816 || (REG_MULTI && reg_getline((linenr_T)-1) != NULL))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003817 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 break;
3819
3820 case RE_EOF:
3821 if (reglnum != reg_maxline || c != NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003822 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 break;
3824
3825 case CURSOR:
3826 /* Check if the buffer is in a window and compare the
3827 * reg_win->w_cursor position to the match position. */
3828 if (reg_win == NULL
3829 || (reglnum + reg_firstlnum != reg_win->w_cursor.lnum)
3830 || ((colnr_T)(reginput - regline) != reg_win->w_cursor.col))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003831 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 break;
3833
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003834 case RE_MARK:
3835 /* Compare the mark position to the match position. NOTE: Always
3836 * uses the current buffer. */
3837 {
3838 int mark = OPERAND(scan)[0];
3839 int cmp = OPERAND(scan)[1];
3840 pos_T *pos;
3841
3842 pos = getmark(mark, FALSE);
3843 if (pos == NULL /* mark doesn't exist) */
3844 || pos->lnum <= 0 /* mark isn't set (in curbuf) */
3845 || (pos->lnum == reglnum + reg_firstlnum
3846 ? (pos->col == (colnr_T)(reginput - regline)
3847 ? (cmp == '<' || cmp == '>')
3848 : (pos->col < (colnr_T)(reginput - regline)
3849 ? cmp != '>'
3850 : cmp != '<'))
3851 : (pos->lnum < reglnum + reg_firstlnum
3852 ? cmp != '>'
3853 : cmp != '<')))
3854 status = RA_NOMATCH;
3855 }
3856 break;
3857
3858 case RE_VISUAL:
3859#ifdef FEAT_VISUAL
3860 /* Check if the buffer is the current buffer. and whether the
3861 * position is inside the Visual area. */
3862 if (reg_buf != curbuf || VIsual.lnum == 0)
3863 status = RA_NOMATCH;
3864 else
3865 {
3866 pos_T top, bot;
3867 linenr_T lnum;
3868 colnr_T col;
3869 win_T *wp = reg_win == NULL ? curwin : reg_win;
3870 int mode;
3871
3872 if (VIsual_active)
3873 {
3874 if (lt(VIsual, wp->w_cursor))
3875 {
3876 top = VIsual;
3877 bot = wp->w_cursor;
3878 }
3879 else
3880 {
3881 top = wp->w_cursor;
3882 bot = VIsual;
3883 }
3884 mode = VIsual_mode;
3885 }
3886 else
3887 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003888 if (lt(curbuf->b_visual.vi_start, curbuf->b_visual.vi_end))
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003889 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003890 top = curbuf->b_visual.vi_start;
3891 bot = curbuf->b_visual.vi_end;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003892 }
3893 else
3894 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003895 top = curbuf->b_visual.vi_end;
3896 bot = curbuf->b_visual.vi_start;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003897 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003898 mode = curbuf->b_visual.vi_mode;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003899 }
3900 lnum = reglnum + reg_firstlnum;
3901 col = (colnr_T)(reginput - regline);
3902 if (lnum < top.lnum || lnum > bot.lnum)
3903 status = RA_NOMATCH;
3904 else if (mode == 'v')
3905 {
3906 if ((lnum == top.lnum && col < top.col)
3907 || (lnum == bot.lnum
3908 && col >= bot.col + (*p_sel != 'e')))
3909 status = RA_NOMATCH;
3910 }
3911 else if (mode == Ctrl_V)
3912 {
3913 colnr_T start, end;
3914 colnr_T start2, end2;
3915 colnr_T col;
3916
3917 getvvcol(wp, &top, &start, NULL, &end);
3918 getvvcol(wp, &bot, &start2, NULL, &end2);
3919 if (start2 < start)
3920 start = start2;
3921 if (end2 > end)
3922 end = end2;
3923 if (top.col == MAXCOL || bot.col == MAXCOL)
3924 end = MAXCOL;
3925 col = win_linetabsize(wp,
3926 regline, (colnr_T)(reginput - regline));
3927 if (col < start || col > end - (*p_sel == 'e'))
3928 status = RA_NOMATCH;
3929 }
3930 }
3931#else
3932 status = RA_NOMATCH;
3933#endif
3934 break;
3935
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 case RE_LNUM:
3937 if (!REG_MULTI || !re_num_cmp((long_u)(reglnum + reg_firstlnum),
3938 scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003939 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 break;
3941
3942 case RE_COL:
3943 if (!re_num_cmp((long_u)(reginput - regline) + 1, scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003944 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 break;
3946
3947 case RE_VCOL:
3948 if (!re_num_cmp((long_u)win_linetabsize(
3949 reg_win == NULL ? curwin : reg_win,
3950 regline, (colnr_T)(reginput - regline)) + 1, scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003951 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 break;
3953
3954 case BOW: /* \<word; reginput points to w */
3955 if (c == NUL) /* Can't match at end of line */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003956 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957#ifdef FEAT_MBYTE
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003958 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 {
3960 int this_class;
3961
3962 /* Get class of current and previous char (if it exists). */
3963 this_class = mb_get_class(reginput);
3964 if (this_class <= 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003965 status = RA_NOMATCH; /* not on a word at all */
3966 else if (reg_prev_class() == this_class)
3967 status = RA_NOMATCH; /* previous char is in same word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 }
3969#endif
3970 else
3971 {
3972 if (!vim_iswordc(c)
3973 || (reginput > regline && vim_iswordc(reginput[-1])))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003974 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 }
3976 break;
3977
3978 case EOW: /* word\>; reginput points after d */
3979 if (reginput == regline) /* Can't match at start of line */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003980 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981#ifdef FEAT_MBYTE
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003982 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 {
3984 int this_class, prev_class;
3985
3986 /* Get class of current and previous char (if it exists). */
3987 this_class = mb_get_class(reginput);
3988 prev_class = reg_prev_class();
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003989 if (this_class == prev_class
3990 || prev_class == 0 || prev_class == 1)
3991 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003994 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003996 if (!vim_iswordc(reginput[-1])
3997 || (reginput[0] != NUL && vim_iswordc(c)))
3998 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 }
4000 break; /* Matched with EOW */
4001
4002 case ANY:
4003 if (c == NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004004 status = RA_NOMATCH;
4005 else
4006 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 break;
4008
4009 case IDENT:
4010 if (!vim_isIDc(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004011 status = RA_NOMATCH;
4012 else
4013 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 break;
4015
4016 case SIDENT:
4017 if (VIM_ISDIGIT(*reginput) || !vim_isIDc(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004018 status = RA_NOMATCH;
4019 else
4020 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 break;
4022
4023 case KWORD:
4024 if (!vim_iswordp(reginput))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004025 status = RA_NOMATCH;
4026 else
4027 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 break;
4029
4030 case SKWORD:
4031 if (VIM_ISDIGIT(*reginput) || !vim_iswordp(reginput))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004032 status = RA_NOMATCH;
4033 else
4034 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 break;
4036
4037 case FNAME:
4038 if (!vim_isfilec(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004039 status = RA_NOMATCH;
4040 else
4041 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 break;
4043
4044 case SFNAME:
4045 if (VIM_ISDIGIT(*reginput) || !vim_isfilec(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004046 status = RA_NOMATCH;
4047 else
4048 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 break;
4050
4051 case PRINT:
4052 if (ptr2cells(reginput) != 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004053 status = RA_NOMATCH;
4054 else
4055 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 break;
4057
4058 case SPRINT:
4059 if (VIM_ISDIGIT(*reginput) || ptr2cells(reginput) != 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004060 status = RA_NOMATCH;
4061 else
4062 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 break;
4064
4065 case WHITE:
4066 if (!vim_iswhite(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004067 status = RA_NOMATCH;
4068 else
4069 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 break;
4071
4072 case NWHITE:
4073 if (c == NUL || vim_iswhite(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004074 status = RA_NOMATCH;
4075 else
4076 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 break;
4078
4079 case DIGIT:
4080 if (!ri_digit(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004081 status = RA_NOMATCH;
4082 else
4083 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 break;
4085
4086 case NDIGIT:
4087 if (c == NUL || ri_digit(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004088 status = RA_NOMATCH;
4089 else
4090 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 break;
4092
4093 case HEX:
4094 if (!ri_hex(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004095 status = RA_NOMATCH;
4096 else
4097 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 break;
4099
4100 case NHEX:
4101 if (c == NUL || ri_hex(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004102 status = RA_NOMATCH;
4103 else
4104 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 break;
4106
4107 case OCTAL:
4108 if (!ri_octal(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004109 status = RA_NOMATCH;
4110 else
4111 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 break;
4113
4114 case NOCTAL:
4115 if (c == NUL || ri_octal(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004116 status = RA_NOMATCH;
4117 else
4118 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 break;
4120
4121 case WORD:
4122 if (!ri_word(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004123 status = RA_NOMATCH;
4124 else
4125 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 break;
4127
4128 case NWORD:
4129 if (c == NUL || ri_word(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004130 status = RA_NOMATCH;
4131 else
4132 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 break;
4134
4135 case HEAD:
4136 if (!ri_head(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004137 status = RA_NOMATCH;
4138 else
4139 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 break;
4141
4142 case NHEAD:
4143 if (c == NUL || ri_head(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004144 status = RA_NOMATCH;
4145 else
4146 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 break;
4148
4149 case ALPHA:
4150 if (!ri_alpha(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004151 status = RA_NOMATCH;
4152 else
4153 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 break;
4155
4156 case NALPHA:
4157 if (c == NUL || ri_alpha(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004158 status = RA_NOMATCH;
4159 else
4160 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 break;
4162
4163 case LOWER:
4164 if (!ri_lower(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004165 status = RA_NOMATCH;
4166 else
4167 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 break;
4169
4170 case NLOWER:
4171 if (c == NUL || ri_lower(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004172 status = RA_NOMATCH;
4173 else
4174 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 break;
4176
4177 case UPPER:
4178 if (!ri_upper(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004179 status = RA_NOMATCH;
4180 else
4181 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 break;
4183
4184 case NUPPER:
4185 if (c == NUL || ri_upper(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004186 status = RA_NOMATCH;
4187 else
4188 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 break;
4190
4191 case EXACTLY:
4192 {
4193 int len;
4194 char_u *opnd;
4195
4196 opnd = OPERAND(scan);
4197 /* Inline the first byte, for speed. */
4198 if (*opnd != *reginput
4199 && (!ireg_ic || (
4200#ifdef FEAT_MBYTE
4201 !enc_utf8 &&
4202#endif
4203 TOLOWER_LOC(*opnd) != TOLOWER_LOC(*reginput))))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004204 status = RA_NOMATCH;
4205 else if (*opnd == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 {
4207 /* match empty string always works; happens when "~" is
4208 * empty. */
4209 }
4210 else if (opnd[1] == NUL
4211#ifdef FEAT_MBYTE
4212 && !(enc_utf8 && ireg_ic)
4213#endif
4214 )
4215 ++reginput; /* matched a single char */
4216 else
4217 {
4218 len = (int)STRLEN(opnd);
4219 /* Need to match first byte again for multi-byte. */
4220 if (cstrncmp(opnd, reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004221 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222#ifdef FEAT_MBYTE
4223 /* Check for following composing character. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004224 else if (enc_utf8
4225 && UTF_COMPOSINGLIKE(reginput, reginput + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 {
4227 /* raaron: This code makes a composing character get
4228 * ignored, which is the correct behavior (sometimes)
4229 * for voweled Hebrew texts. */
4230 if (!ireg_icombine)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004231 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004234 else
4235 reginput += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 }
4237 }
4238 break;
4239
4240 case ANYOF:
4241 case ANYBUT:
4242 if (c == NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004243 status = RA_NOMATCH;
4244 else if ((cstrchr(OPERAND(scan), c) == NULL) == (op == ANYOF))
4245 status = RA_NOMATCH;
4246 else
4247 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 break;
4249
4250#ifdef FEAT_MBYTE
4251 case MULTIBYTECODE:
4252 if (has_mbyte)
4253 {
4254 int i, len;
4255 char_u *opnd;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004256 int opndc, inpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257
4258 opnd = OPERAND(scan);
4259 /* Safety check (just in case 'encoding' was changed since
4260 * compiling the program). */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004261 if ((len = (*mb_ptr2len)(opnd)) < 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004262 {
4263 status = RA_NOMATCH;
4264 break;
4265 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004266 if (enc_utf8)
4267 opndc = mb_ptr2char(opnd);
4268 if (enc_utf8 && utf_iscomposing(opndc))
4269 {
4270 /* When only a composing char is given match at any
4271 * position where that composing char appears. */
4272 status = RA_NOMATCH;
4273 for (i = 0; reginput[i] != NUL; i += utf_char2len(inpc))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004274 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004275 inpc = mb_ptr2char(reginput + i);
4276 if (!utf_iscomposing(inpc))
4277 {
4278 if (i > 0)
4279 break;
4280 }
4281 else if (opndc == inpc)
4282 {
4283 /* Include all following composing chars. */
4284 len = i + mb_ptr2len(reginput + i);
4285 status = RA_MATCH;
4286 break;
4287 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004288 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004289 }
4290 else
4291 for (i = 0; i < len; ++i)
4292 if (opnd[i] != reginput[i])
4293 {
4294 status = RA_NOMATCH;
4295 break;
4296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 reginput += len;
4298 }
4299 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004300 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 break;
4302#endif
4303
4304 case NOTHING:
4305 break;
4306
4307 case BACK:
Bram Moolenaar582fd852005-03-28 20:58:01 +00004308 {
4309 int i;
4310 backpos_T *bp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311
Bram Moolenaar582fd852005-03-28 20:58:01 +00004312 /*
4313 * When we run into BACK we need to check if we don't keep
4314 * looping without matching any input. The second and later
4315 * times a BACK is encountered it fails if the input is still
4316 * at the same position as the previous time.
4317 * The positions are stored in "backpos" and found by the
4318 * current value of "scan", the position in the RE program.
4319 */
4320 bp = (backpos_T *)backpos.ga_data;
4321 for (i = 0; i < backpos.ga_len; ++i)
4322 if (bp[i].bp_scan == scan)
4323 break;
4324 if (i == backpos.ga_len)
4325 {
4326 /* First time at this BACK, make room to store the pos. */
4327 if (ga_grow(&backpos, 1) == FAIL)
4328 status = RA_FAIL;
4329 else
4330 {
4331 /* get "ga_data" again, it may have changed */
4332 bp = (backpos_T *)backpos.ga_data;
4333 bp[i].bp_scan = scan;
4334 ++backpos.ga_len;
4335 }
4336 }
4337 else if (reg_save_equal(&bp[i].bp_pos))
4338 /* Still at same position as last time, fail. */
4339 status = RA_NOMATCH;
4340
4341 if (status != RA_FAIL && status != RA_NOMATCH)
4342 reg_save(&bp[i].bp_pos, &backpos);
4343 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004344 break;
4345
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 case MOPEN + 0: /* Match start: \zs */
4347 case MOPEN + 1: /* \( */
4348 case MOPEN + 2:
4349 case MOPEN + 3:
4350 case MOPEN + 4:
4351 case MOPEN + 5:
4352 case MOPEN + 6:
4353 case MOPEN + 7:
4354 case MOPEN + 8:
4355 case MOPEN + 9:
4356 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 no = op - MOPEN;
4358 cleanup_subexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004359 rp = regstack_push(RS_MOPEN, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004360 if (rp == NULL)
4361 status = RA_FAIL;
4362 else
4363 {
4364 rp->rs_no = no;
4365 save_se(&rp->rs_un.sesave, &reg_startpos[no],
4366 &reg_startp[no]);
4367 /* We simply continue and handle the result when done. */
4368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004370 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371
4372 case NOPEN: /* \%( */
4373 case NCLOSE: /* \) after \%( */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004374 if (regstack_push(RS_NOPEN, scan) == NULL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004375 status = RA_FAIL;
4376 /* We simply continue and handle the result when done. */
4377 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378
4379#ifdef FEAT_SYN_HL
4380 case ZOPEN + 1:
4381 case ZOPEN + 2:
4382 case ZOPEN + 3:
4383 case ZOPEN + 4:
4384 case ZOPEN + 5:
4385 case ZOPEN + 6:
4386 case ZOPEN + 7:
4387 case ZOPEN + 8:
4388 case ZOPEN + 9:
4389 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 no = op - ZOPEN;
4391 cleanup_zsubexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004392 rp = regstack_push(RS_ZOPEN, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004393 if (rp == NULL)
4394 status = RA_FAIL;
4395 else
4396 {
4397 rp->rs_no = no;
4398 save_se(&rp->rs_un.sesave, &reg_startzpos[no],
4399 &reg_startzp[no]);
4400 /* We simply continue and handle the result when done. */
4401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004403 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404#endif
4405
4406 case MCLOSE + 0: /* Match end: \ze */
4407 case MCLOSE + 1: /* \) */
4408 case MCLOSE + 2:
4409 case MCLOSE + 3:
4410 case MCLOSE + 4:
4411 case MCLOSE + 5:
4412 case MCLOSE + 6:
4413 case MCLOSE + 7:
4414 case MCLOSE + 8:
4415 case MCLOSE + 9:
4416 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 no = op - MCLOSE;
4418 cleanup_subexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004419 rp = regstack_push(RS_MCLOSE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004420 if (rp == NULL)
4421 status = RA_FAIL;
4422 else
4423 {
4424 rp->rs_no = no;
4425 save_se(&rp->rs_un.sesave, &reg_endpos[no], &reg_endp[no]);
4426 /* We simply continue and handle the result when done. */
4427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004429 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430
4431#ifdef FEAT_SYN_HL
4432 case ZCLOSE + 1: /* \) after \z( */
4433 case ZCLOSE + 2:
4434 case ZCLOSE + 3:
4435 case ZCLOSE + 4:
4436 case ZCLOSE + 5:
4437 case ZCLOSE + 6:
4438 case ZCLOSE + 7:
4439 case ZCLOSE + 8:
4440 case ZCLOSE + 9:
4441 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 no = op - ZCLOSE;
4443 cleanup_zsubexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004444 rp = regstack_push(RS_ZCLOSE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004445 if (rp == NULL)
4446 status = RA_FAIL;
4447 else
4448 {
4449 rp->rs_no = no;
4450 save_se(&rp->rs_un.sesave, &reg_endzpos[no],
4451 &reg_endzp[no]);
4452 /* We simply continue and handle the result when done. */
4453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004455 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456#endif
4457
4458 case BACKREF + 1:
4459 case BACKREF + 2:
4460 case BACKREF + 3:
4461 case BACKREF + 4:
4462 case BACKREF + 5:
4463 case BACKREF + 6:
4464 case BACKREF + 7:
4465 case BACKREF + 8:
4466 case BACKREF + 9:
4467 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 int len;
4469 linenr_T clnum;
4470 colnr_T ccol;
4471 char_u *p;
4472
4473 no = op - BACKREF;
4474 cleanup_subexpr();
4475 if (!REG_MULTI) /* Single-line regexp */
4476 {
4477 if (reg_endp[no] == NULL)
4478 {
4479 /* Backref was not set: Match an empty string. */
4480 len = 0;
4481 }
4482 else
4483 {
4484 /* Compare current input with back-ref in the same
4485 * line. */
4486 len = (int)(reg_endp[no] - reg_startp[no]);
4487 if (cstrncmp(reg_startp[no], reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004488 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 }
4490 }
4491 else /* Multi-line regexp */
4492 {
4493 if (reg_endpos[no].lnum < 0)
4494 {
4495 /* Backref was not set: Match an empty string. */
4496 len = 0;
4497 }
4498 else
4499 {
4500 if (reg_startpos[no].lnum == reglnum
4501 && reg_endpos[no].lnum == reglnum)
4502 {
4503 /* Compare back-ref within the current line. */
4504 len = reg_endpos[no].col - reg_startpos[no].col;
4505 if (cstrncmp(regline + reg_startpos[no].col,
4506 reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004507 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 }
4509 else
4510 {
4511 /* Messy situation: Need to compare between two
4512 * lines. */
4513 ccol = reg_startpos[no].col;
4514 clnum = reg_startpos[no].lnum;
4515 for (;;)
4516 {
4517 /* Since getting one line may invalidate
4518 * the other, need to make copy. Slow! */
4519 if (regline != reg_tofree)
4520 {
4521 len = (int)STRLEN(regline);
4522 if (reg_tofree == NULL
4523 || len >= (int)reg_tofreelen)
4524 {
4525 len += 50; /* get some extra */
4526 vim_free(reg_tofree);
4527 reg_tofree = alloc(len);
4528 if (reg_tofree == NULL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004529 {
4530 status = RA_FAIL; /* outof memory!*/
4531 break;
4532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 reg_tofreelen = len;
4534 }
4535 STRCPY(reg_tofree, regline);
4536 reginput = reg_tofree
4537 + (reginput - regline);
4538 regline = reg_tofree;
4539 }
4540
4541 /* Get the line to compare with. */
4542 p = reg_getline(clnum);
4543 if (clnum == reg_endpos[no].lnum)
4544 len = reg_endpos[no].col - ccol;
4545 else
4546 len = (int)STRLEN(p + ccol);
4547
4548 if (cstrncmp(p + ccol, reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004549 {
4550 status = RA_NOMATCH; /* doesn't match */
4551 break;
4552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 if (clnum == reg_endpos[no].lnum)
4554 break; /* match and at end! */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004555 if (reglnum >= reg_maxline)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004556 {
4557 status = RA_NOMATCH; /* text too short */
4558 break;
4559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560
4561 /* Advance to next line. */
4562 reg_nextline();
4563 ++clnum;
4564 ccol = 0;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004565 if (got_int)
4566 {
4567 status = RA_FAIL;
4568 break;
4569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 }
4571
4572 /* found a match! Note that regline may now point
4573 * to a copy of the line, that should not matter. */
4574 }
4575 }
4576 }
4577
4578 /* Matched the backref, skip over it. */
4579 reginput += len;
4580 }
4581 break;
4582
4583#ifdef FEAT_SYN_HL
4584 case ZREF + 1:
4585 case ZREF + 2:
4586 case ZREF + 3:
4587 case ZREF + 4:
4588 case ZREF + 5:
4589 case ZREF + 6:
4590 case ZREF + 7:
4591 case ZREF + 8:
4592 case ZREF + 9:
4593 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 int len;
4595
4596 cleanup_zsubexpr();
4597 no = op - ZREF;
4598 if (re_extmatch_in != NULL
4599 && re_extmatch_in->matches[no] != NULL)
4600 {
4601 len = (int)STRLEN(re_extmatch_in->matches[no]);
4602 if (cstrncmp(re_extmatch_in->matches[no],
4603 reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004604 status = RA_NOMATCH;
4605 else
4606 reginput += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 }
4608 else
4609 {
4610 /* Backref was not set: Match an empty string. */
4611 }
4612 }
4613 break;
4614#endif
4615
4616 case BRANCH:
4617 {
4618 if (OP(next) != BRANCH) /* No choice. */
4619 next = OPERAND(scan); /* Avoid recursion. */
4620 else
4621 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004622 rp = regstack_push(RS_BRANCH, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004623 if (rp == NULL)
4624 status = RA_FAIL;
4625 else
4626 status = RA_BREAK; /* rest is below */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 }
4628 }
4629 break;
4630
4631 case BRACE_LIMITS:
4632 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 if (OP(next) == BRACE_SIMPLE)
4634 {
4635 bl_minval = OPERAND_MIN(scan);
4636 bl_maxval = OPERAND_MAX(scan);
4637 }
4638 else if (OP(next) >= BRACE_COMPLEX
4639 && OP(next) < BRACE_COMPLEX + 10)
4640 {
4641 no = OP(next) - BRACE_COMPLEX;
4642 brace_min[no] = OPERAND_MIN(scan);
4643 brace_max[no] = OPERAND_MAX(scan);
4644 brace_count[no] = 0;
4645 }
4646 else
4647 {
4648 EMSG(_(e_internal)); /* Shouldn't happen */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004649 status = RA_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 }
4651 }
4652 break;
4653
4654 case BRACE_COMPLEX + 0:
4655 case BRACE_COMPLEX + 1:
4656 case BRACE_COMPLEX + 2:
4657 case BRACE_COMPLEX + 3:
4658 case BRACE_COMPLEX + 4:
4659 case BRACE_COMPLEX + 5:
4660 case BRACE_COMPLEX + 6:
4661 case BRACE_COMPLEX + 7:
4662 case BRACE_COMPLEX + 8:
4663 case BRACE_COMPLEX + 9:
4664 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 no = op - BRACE_COMPLEX;
4666 ++brace_count[no];
4667
4668 /* If not matched enough times yet, try one more */
4669 if (brace_count[no] <= (brace_min[no] <= brace_max[no]
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004670 ? brace_min[no] : brace_max[no]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004672 rp = regstack_push(RS_BRCPLX_MORE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004673 if (rp == NULL)
4674 status = RA_FAIL;
4675 else
4676 {
4677 rp->rs_no = no;
Bram Moolenaar582fd852005-03-28 20:58:01 +00004678 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004679 next = OPERAND(scan);
4680 /* We continue and handle the result when done. */
4681 }
4682 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 }
4684
4685 /* If matched enough times, may try matching some more */
4686 if (brace_min[no] <= brace_max[no])
4687 {
4688 /* Range is the normal way around, use longest match */
4689 if (brace_count[no] <= brace_max[no])
4690 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004691 rp = regstack_push(RS_BRCPLX_LONG, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004692 if (rp == NULL)
4693 status = RA_FAIL;
4694 else
4695 {
4696 rp->rs_no = no;
Bram Moolenaar582fd852005-03-28 20:58:01 +00004697 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004698 next = OPERAND(scan);
4699 /* We continue and handle the result when done. */
4700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 }
4702 }
4703 else
4704 {
4705 /* Range is backwards, use shortest match first */
4706 if (brace_count[no] <= brace_min[no])
4707 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004708 rp = regstack_push(RS_BRCPLX_SHORT, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004709 if (rp == NULL)
4710 status = RA_FAIL;
4711 else
4712 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00004713 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004714 /* We continue and handle the result when done. */
4715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 }
4717 }
4718 }
4719 break;
4720
4721 case BRACE_SIMPLE:
4722 case STAR:
4723 case PLUS:
4724 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004725 regstar_T rst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726
4727 /*
4728 * Lookahead to avoid useless match attempts when we know
4729 * what character comes next.
4730 */
4731 if (OP(next) == EXACTLY)
4732 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004733 rst.nextb = *OPERAND(next);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 if (ireg_ic)
4735 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004736 if (isupper(rst.nextb))
4737 rst.nextb_ic = TOLOWER_LOC(rst.nextb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004739 rst.nextb_ic = TOUPPER_LOC(rst.nextb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 }
4741 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004742 rst.nextb_ic = rst.nextb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 }
4744 else
4745 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004746 rst.nextb = NUL;
4747 rst.nextb_ic = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 }
4749 if (op != BRACE_SIMPLE)
4750 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004751 rst.minval = (op == STAR) ? 0 : 1;
4752 rst.maxval = MAX_LIMIT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 }
4754 else
4755 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004756 rst.minval = bl_minval;
4757 rst.maxval = bl_maxval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 }
4759
4760 /*
4761 * When maxval > minval, try matching as much as possible, up
4762 * to maxval. When maxval < minval, try matching at least the
4763 * minimal number (since the range is backwards, that's also
4764 * maxval!).
4765 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004766 rst.count = regrepeat(OPERAND(scan), rst.maxval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004769 status = RA_FAIL;
4770 break;
4771 }
4772 if (rst.minval <= rst.maxval
4773 ? rst.count >= rst.minval : rst.count >= rst.maxval)
4774 {
4775 /* It could match. Prepare for trying to match what
4776 * follows. The code is below. Parameters are stored in
4777 * a regstar_T on the regstack. */
Bram Moolenaar916b7af2005-03-16 09:52:38 +00004778 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004779 {
4780 EMSG(_(e_maxmempat));
4781 status = RA_FAIL;
4782 }
4783 else if (ga_grow(&regstack, sizeof(regstar_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004784 status = RA_FAIL;
4785 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004787 regstack.ga_len += sizeof(regstar_T);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004788 rp = regstack_push(rst.minval <= rst.maxval
Bram Moolenaar582fd852005-03-28 20:58:01 +00004789 ? RS_STAR_LONG : RS_STAR_SHORT, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004790 if (rp == NULL)
4791 status = RA_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004793 {
4794 *(((regstar_T *)rp) - 1) = rst;
4795 status = RA_BREAK; /* skip the restore bits */
4796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 }
4798 }
4799 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004800 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 }
4803 break;
4804
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004805 case NOMATCH:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 case MATCH:
4807 case SUBPAT:
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004808 rp = regstack_push(RS_NOMATCH, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004809 if (rp == NULL)
4810 status = RA_FAIL;
4811 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004813 rp->rs_no = op;
Bram Moolenaar582fd852005-03-28 20:58:01 +00004814 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004815 next = OPERAND(scan);
4816 /* We continue and handle the result when done. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 }
4818 break;
4819
4820 case BEHIND:
4821 case NOBEHIND:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004822 /* Need a bit of room to store extra positions. */
Bram Moolenaar916b7af2005-03-16 09:52:38 +00004823 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004824 {
4825 EMSG(_(e_maxmempat));
4826 status = RA_FAIL;
4827 }
4828 else if (ga_grow(&regstack, sizeof(regbehind_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004829 status = RA_FAIL;
4830 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004832 regstack.ga_len += sizeof(regbehind_T);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004833 rp = regstack_push(RS_BEHIND1, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004834 if (rp == NULL)
4835 status = RA_FAIL;
4836 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004838 rp->rs_no = op;
Bram Moolenaar582fd852005-03-28 20:58:01 +00004839 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004840 /* First try if what follows matches. If it does then we
4841 * check the behind match by looping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004844 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845
4846 case BHPOS:
4847 if (REG_MULTI)
4848 {
4849 if (behind_pos.rs_u.pos.col != (colnr_T)(reginput - regline)
4850 || behind_pos.rs_u.pos.lnum != reglnum)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004851 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 }
4853 else if (behind_pos.rs_u.ptr != reginput)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004854 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 break;
4856
4857 case NEWL:
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004858 if ((c != NUL || reglnum > reg_maxline || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 && (c != '\n' || !reg_line_lbr))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004860 status = RA_NOMATCH;
4861 else if (reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 ADVANCE_REGINPUT();
4863 else
4864 reg_nextline();
4865 break;
4866
4867 case END:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004868 status = RA_MATCH; /* Success! */
4869 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870
4871 default:
4872 EMSG(_(e_re_corr));
4873#ifdef DEBUG
4874 printf("Illegal op code %d\n", op);
4875#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004876 status = RA_FAIL;
4877 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 }
4879 }
4880
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004881 /* If we can't continue sequentially, break the inner loop. */
4882 if (status != RA_CONT)
4883 break;
4884
4885 /* Continue in inner loop, advance to next item. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 scan = next;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004887
4888 } /* end of inner loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889
4890 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004891 * If there is something on the regstack execute the code for the state.
Bram Moolenaar582fd852005-03-28 20:58:01 +00004892 * If the state is popped then loop and use the older state.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004894 while (regstack.ga_len > 0 && status != RA_FAIL)
4895 {
4896 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len) - 1;
4897 switch (rp->rs_state)
4898 {
4899 case RS_NOPEN:
4900 /* Result is passed on as-is, simply pop the state. */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004901 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004902 break;
4903
4904 case RS_MOPEN:
4905 /* Pop the state. Restore pointers when there is no match. */
4906 if (status == RA_NOMATCH)
4907 restore_se(&rp->rs_un.sesave, &reg_startpos[rp->rs_no],
4908 &reg_startp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004909 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004910 break;
4911
4912#ifdef FEAT_SYN_HL
4913 case RS_ZOPEN:
4914 /* Pop the state. Restore pointers when there is no match. */
4915 if (status == RA_NOMATCH)
4916 restore_se(&rp->rs_un.sesave, &reg_startzpos[rp->rs_no],
4917 &reg_startzp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004918 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004919 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004921
4922 case RS_MCLOSE:
4923 /* Pop the state. Restore pointers when there is no match. */
4924 if (status == RA_NOMATCH)
4925 restore_se(&rp->rs_un.sesave, &reg_endpos[rp->rs_no],
4926 &reg_endp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004927 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004928 break;
4929
4930#ifdef FEAT_SYN_HL
4931 case RS_ZCLOSE:
4932 /* Pop the state. Restore pointers when there is no match. */
4933 if (status == RA_NOMATCH)
4934 restore_se(&rp->rs_un.sesave, &reg_endzpos[rp->rs_no],
4935 &reg_endzp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004936 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004937 break;
4938#endif
4939
4940 case RS_BRANCH:
4941 if (status == RA_MATCH)
4942 /* this branch matched, use it */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004943 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004944 else
4945 {
4946 if (status != RA_BREAK)
4947 {
4948 /* After a non-matching branch: try next one. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00004949 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004950 scan = rp->rs_scan;
4951 }
4952 if (scan == NULL || OP(scan) != BRANCH)
4953 {
4954 /* no more branches, didn't find a match */
4955 status = RA_NOMATCH;
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004956 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004957 }
4958 else
4959 {
4960 /* Prepare to try a branch. */
4961 rp->rs_scan = regnext(scan);
Bram Moolenaar582fd852005-03-28 20:58:01 +00004962 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004963 scan = OPERAND(scan);
4964 }
4965 }
4966 break;
4967
4968 case RS_BRCPLX_MORE:
4969 /* Pop the state. Restore pointers when there is no match. */
4970 if (status == RA_NOMATCH)
4971 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00004972 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004973 --brace_count[rp->rs_no]; /* decrement match count */
4974 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004975 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004976 break;
4977
4978 case RS_BRCPLX_LONG:
4979 /* Pop the state. Restore pointers when there is no match. */
4980 if (status == RA_NOMATCH)
4981 {
4982 /* There was no match, but we did find enough matches. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00004983 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004984 --brace_count[rp->rs_no];
4985 /* continue with the items after "\{}" */
4986 status = RA_CONT;
4987 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004988 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004989 if (status == RA_CONT)
4990 scan = regnext(scan);
4991 break;
4992
4993 case RS_BRCPLX_SHORT:
4994 /* Pop the state. Restore pointers when there is no match. */
4995 if (status == RA_NOMATCH)
4996 /* There was no match, try to match one more item. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00004997 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004998 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004999 if (status == RA_NOMATCH)
5000 {
5001 scan = OPERAND(scan);
5002 status = RA_CONT;
5003 }
5004 break;
5005
5006 case RS_NOMATCH:
5007 /* Pop the state. If the operand matches for NOMATCH or
5008 * doesn't match for MATCH/SUBPAT, we fail. Otherwise backup,
5009 * except for SUBPAT, and continue with the next item. */
5010 if (status == (rp->rs_no == NOMATCH ? RA_MATCH : RA_NOMATCH))
5011 status = RA_NOMATCH;
5012 else
5013 {
5014 status = RA_CONT;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005015 if (rp->rs_no != SUBPAT) /* zero-width */
5016 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005017 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005018 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005019 if (status == RA_CONT)
5020 scan = regnext(scan);
5021 break;
5022
5023 case RS_BEHIND1:
5024 if (status == RA_NOMATCH)
5025 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005026 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005027 regstack.ga_len -= sizeof(regbehind_T);
5028 }
5029 else
5030 {
5031 /* The stuff after BEHIND/NOBEHIND matches. Now try if
5032 * the behind part does (not) match before the current
5033 * position in the input. This must be done at every
5034 * position in the input and checking if the match ends at
5035 * the current position. */
5036
5037 /* save the position after the found match for next */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005038 reg_save(&(((regbehind_T *)rp) - 1)->save_after, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005039
5040 /* start looking for a match with operand at the current
5041 * postion. Go back one character until we find the
5042 * result, hitting the start of the line or the previous
5043 * line (for multi-line matching).
5044 * Set behind_pos to where the match should end, BHPOS
5045 * will match it. Save the current value. */
5046 (((regbehind_T *)rp) - 1)->save_behind = behind_pos;
5047 behind_pos = rp->rs_un.regsave;
5048
5049 rp->rs_state = RS_BEHIND2;
5050
Bram Moolenaar582fd852005-03-28 20:58:01 +00005051 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005052 scan = OPERAND(rp->rs_scan);
5053 }
5054 break;
5055
5056 case RS_BEHIND2:
5057 /*
5058 * Looping for BEHIND / NOBEHIND match.
5059 */
5060 if (status == RA_MATCH && reg_save_equal(&behind_pos))
5061 {
5062 /* found a match that ends where "next" started */
5063 behind_pos = (((regbehind_T *)rp) - 1)->save_behind;
5064 if (rp->rs_no == BEHIND)
Bram Moolenaar582fd852005-03-28 20:58:01 +00005065 reg_restore(&(((regbehind_T *)rp) - 1)->save_after,
5066 &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005067 else
5068 /* But we didn't want a match. */
5069 status = RA_NOMATCH;
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005070 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005071 regstack.ga_len -= sizeof(regbehind_T);
5072 }
5073 else
5074 {
5075 /* No match: Go back one character. May go to previous
5076 * line once. */
5077 no = OK;
5078 if (REG_MULTI)
5079 {
5080 if (rp->rs_un.regsave.rs_u.pos.col == 0)
5081 {
5082 if (rp->rs_un.regsave.rs_u.pos.lnum
5083 < behind_pos.rs_u.pos.lnum
5084 || reg_getline(
5085 --rp->rs_un.regsave.rs_u.pos.lnum)
5086 == NULL)
5087 no = FAIL;
5088 else
5089 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005090 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005091 rp->rs_un.regsave.rs_u.pos.col =
5092 (colnr_T)STRLEN(regline);
5093 }
5094 }
5095 else
5096 --rp->rs_un.regsave.rs_u.pos.col;
5097 }
5098 else
5099 {
5100 if (rp->rs_un.regsave.rs_u.ptr == regline)
5101 no = FAIL;
5102 else
5103 --rp->rs_un.regsave.rs_u.ptr;
5104 }
5105 if (no == OK)
5106 {
5107 /* Advanced, prepare for finding match again. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005108 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005109 scan = OPERAND(rp->rs_scan);
5110 }
5111 else
5112 {
5113 /* Can't advance. For NOBEHIND that's a match. */
5114 behind_pos = (((regbehind_T *)rp) - 1)->save_behind;
5115 if (rp->rs_no == NOBEHIND)
5116 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005117 reg_restore(&(((regbehind_T *)rp) - 1)->save_after,
5118 &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005119 status = RA_MATCH;
5120 }
5121 else
5122 status = RA_NOMATCH;
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005123 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005124 regstack.ga_len -= sizeof(regbehind_T);
5125 }
5126 }
5127 break;
5128
5129 case RS_STAR_LONG:
5130 case RS_STAR_SHORT:
5131 {
5132 regstar_T *rst = ((regstar_T *)rp) - 1;
5133
5134 if (status == RA_MATCH)
5135 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005136 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005137 regstack.ga_len -= sizeof(regstar_T);
5138 break;
5139 }
5140
5141 /* Tried once already, restore input pointers. */
5142 if (status != RA_BREAK)
Bram Moolenaar582fd852005-03-28 20:58:01 +00005143 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005144
5145 /* Repeat until we found a position where it could match. */
5146 for (;;)
5147 {
5148 if (status != RA_BREAK)
5149 {
5150 /* Tried first position already, advance. */
5151 if (rp->rs_state == RS_STAR_LONG)
5152 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005153 /* Trying for longest match, but couldn't or
5154 * didn't match -- back up one char. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005155 if (--rst->count < rst->minval)
5156 break;
5157 if (reginput == regline)
5158 {
5159 /* backup to last char of previous line */
5160 --reglnum;
5161 regline = reg_getline(reglnum);
5162 /* Just in case regrepeat() didn't count
5163 * right. */
5164 if (regline == NULL)
5165 break;
5166 reginput = regline + STRLEN(regline);
5167 fast_breakcheck();
5168 }
5169 else
5170 mb_ptr_back(regline, reginput);
5171 }
5172 else
5173 {
5174 /* Range is backwards, use shortest match first.
5175 * Careful: maxval and minval are exchanged!
5176 * Couldn't or didn't match: try advancing one
5177 * char. */
5178 if (rst->count == rst->minval
5179 || regrepeat(OPERAND(rp->rs_scan), 1L) == 0)
5180 break;
5181 ++rst->count;
5182 }
5183 if (got_int)
5184 break;
5185 }
5186 else
5187 status = RA_NOMATCH;
5188
5189 /* If it could match, try it. */
5190 if (rst->nextb == NUL || *reginput == rst->nextb
5191 || *reginput == rst->nextb_ic)
5192 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005193 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005194 scan = regnext(rp->rs_scan);
5195 status = RA_CONT;
5196 break;
5197 }
5198 }
5199 if (status != RA_CONT)
5200 {
5201 /* Failed. */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005202 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005203 regstack.ga_len -= sizeof(regstar_T);
5204 status = RA_NOMATCH;
5205 }
5206 }
5207 break;
5208 }
5209
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005210 /* If we want to continue the inner loop or didn't pop a state
5211 * continue matching loop */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005212 if (status == RA_CONT || rp == (regitem_T *)
5213 ((char *)regstack.ga_data + regstack.ga_len) - 1)
5214 break;
5215 }
5216
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005217 /* May need to continue with the inner loop, starting at "scan". */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005218 if (status == RA_CONT)
5219 continue;
5220
5221 /*
5222 * If the regstack is empty or something failed we are done.
5223 */
5224 if (regstack.ga_len == 0 || status == RA_FAIL)
5225 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005226 if (scan == NULL)
5227 {
5228 /*
5229 * We get here only if there's trouble -- normally "case END" is
5230 * the terminating point.
5231 */
5232 EMSG(_(e_re_corr));
5233#ifdef DEBUG
5234 printf("Premature EOL\n");
5235#endif
5236 }
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005237 if (status == RA_FAIL)
5238 got_int = TRUE;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005239 return (status == RA_MATCH);
5240 }
5241
5242 } /* End of loop until the regstack is empty. */
5243
5244 /* NOTREACHED */
5245}
5246
5247/*
5248 * Push an item onto the regstack.
5249 * Returns pointer to new item. Returns NULL when out of memory.
5250 */
5251 static regitem_T *
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005252regstack_push(state, scan)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005253 regstate_T state;
5254 char_u *scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005255{
5256 regitem_T *rp;
5257
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005258 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005259 {
5260 EMSG(_(e_maxmempat));
5261 return NULL;
5262 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005263 if (ga_grow(&regstack, sizeof(regitem_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005264 return NULL;
5265
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005266 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005267 rp->rs_state = state;
5268 rp->rs_scan = scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005269
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005270 regstack.ga_len += sizeof(regitem_T);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005271 return rp;
5272}
5273
5274/*
5275 * Pop an item from the regstack.
5276 */
5277 static void
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005278regstack_pop(scan)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005279 char_u **scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005280{
5281 regitem_T *rp;
5282
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005283 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len) - 1;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005284 *scan = rp->rs_scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005285
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005286 regstack.ga_len -= sizeof(regitem_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287}
5288
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289/*
5290 * regrepeat - repeatedly match something simple, return how many.
5291 * Advances reginput (and reglnum) to just after the matched chars.
5292 */
5293 static int
5294regrepeat(p, maxcount)
5295 char_u *p;
5296 long maxcount; /* maximum number of matches allowed */
5297{
5298 long count = 0;
5299 char_u *scan;
5300 char_u *opnd;
5301 int mask;
5302 int testval = 0;
5303
5304 scan = reginput; /* Make local copy of reginput for speed. */
5305 opnd = OPERAND(p);
5306 switch (OP(p))
5307 {
5308 case ANY:
5309 case ANY + ADD_NL:
5310 while (count < maxcount)
5311 {
5312 /* Matching anything means we continue until end-of-line (or
5313 * end-of-file for ANY + ADD_NL), only limited by maxcount. */
5314 while (*scan != NUL && count < maxcount)
5315 {
5316 ++count;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005317 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005319 if (!WITH_NL(OP(p)) || reglnum > reg_maxline || reg_line_lbr
5320 || count == maxcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 break;
5322 ++count; /* count the line-break */
5323 reg_nextline();
5324 scan = reginput;
5325 if (got_int)
5326 break;
5327 }
5328 break;
5329
5330 case IDENT:
5331 case IDENT + ADD_NL:
5332 testval = TRUE;
5333 /*FALLTHROUGH*/
5334 case SIDENT:
5335 case SIDENT + ADD_NL:
5336 while (count < maxcount)
5337 {
5338 if (vim_isIDc(*scan) && (testval || !VIM_ISDIGIT(*scan)))
5339 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005340 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 }
5342 else if (*scan == NUL)
5343 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005344 if (!WITH_NL(OP(p)) || reglnum > reg_maxline || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 break;
5346 reg_nextline();
5347 scan = reginput;
5348 if (got_int)
5349 break;
5350 }
5351 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5352 ++scan;
5353 else
5354 break;
5355 ++count;
5356 }
5357 break;
5358
5359 case KWORD:
5360 case KWORD + ADD_NL:
5361 testval = TRUE;
5362 /*FALLTHROUGH*/
5363 case SKWORD:
5364 case SKWORD + ADD_NL:
5365 while (count < maxcount)
5366 {
5367 if (vim_iswordp(scan) && (testval || !VIM_ISDIGIT(*scan)))
5368 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005369 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 }
5371 else if (*scan == NUL)
5372 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005373 if (!WITH_NL(OP(p)) || reglnum > reg_maxline || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 break;
5375 reg_nextline();
5376 scan = reginput;
5377 if (got_int)
5378 break;
5379 }
5380 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5381 ++scan;
5382 else
5383 break;
5384 ++count;
5385 }
5386 break;
5387
5388 case FNAME:
5389 case FNAME + ADD_NL:
5390 testval = TRUE;
5391 /*FALLTHROUGH*/
5392 case SFNAME:
5393 case SFNAME + ADD_NL:
5394 while (count < maxcount)
5395 {
5396 if (vim_isfilec(*scan) && (testval || !VIM_ISDIGIT(*scan)))
5397 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005398 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 }
5400 else if (*scan == NUL)
5401 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005402 if (!WITH_NL(OP(p)) || reglnum > reg_maxline || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 break;
5404 reg_nextline();
5405 scan = reginput;
5406 if (got_int)
5407 break;
5408 }
5409 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5410 ++scan;
5411 else
5412 break;
5413 ++count;
5414 }
5415 break;
5416
5417 case PRINT:
5418 case PRINT + ADD_NL:
5419 testval = TRUE;
5420 /*FALLTHROUGH*/
5421 case SPRINT:
5422 case SPRINT + ADD_NL:
5423 while (count < maxcount)
5424 {
5425 if (*scan == NUL)
5426 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005427 if (!WITH_NL(OP(p)) || reglnum > reg_maxline || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 break;
5429 reg_nextline();
5430 scan = reginput;
5431 if (got_int)
5432 break;
5433 }
5434 else if (ptr2cells(scan) == 1 && (testval || !VIM_ISDIGIT(*scan)))
5435 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005436 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 }
5438 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5439 ++scan;
5440 else
5441 break;
5442 ++count;
5443 }
5444 break;
5445
5446 case WHITE:
5447 case WHITE + ADD_NL:
5448 testval = mask = RI_WHITE;
5449do_class:
5450 while (count < maxcount)
5451 {
5452#ifdef FEAT_MBYTE
5453 int l;
5454#endif
5455 if (*scan == NUL)
5456 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005457 if (!WITH_NL(OP(p)) || reglnum > reg_maxline || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 break;
5459 reg_nextline();
5460 scan = reginput;
5461 if (got_int)
5462 break;
5463 }
5464#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005465 else if (has_mbyte && (l = (*mb_ptr2len)(scan)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 {
5467 if (testval != 0)
5468 break;
5469 scan += l;
5470 }
5471#endif
5472 else if ((class_tab[*scan] & mask) == testval)
5473 ++scan;
5474 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5475 ++scan;
5476 else
5477 break;
5478 ++count;
5479 }
5480 break;
5481
5482 case NWHITE:
5483 case NWHITE + ADD_NL:
5484 mask = RI_WHITE;
5485 goto do_class;
5486 case DIGIT:
5487 case DIGIT + ADD_NL:
5488 testval = mask = RI_DIGIT;
5489 goto do_class;
5490 case NDIGIT:
5491 case NDIGIT + ADD_NL:
5492 mask = RI_DIGIT;
5493 goto do_class;
5494 case HEX:
5495 case HEX + ADD_NL:
5496 testval = mask = RI_HEX;
5497 goto do_class;
5498 case NHEX:
5499 case NHEX + ADD_NL:
5500 mask = RI_HEX;
5501 goto do_class;
5502 case OCTAL:
5503 case OCTAL + ADD_NL:
5504 testval = mask = RI_OCTAL;
5505 goto do_class;
5506 case NOCTAL:
5507 case NOCTAL + ADD_NL:
5508 mask = RI_OCTAL;
5509 goto do_class;
5510 case WORD:
5511 case WORD + ADD_NL:
5512 testval = mask = RI_WORD;
5513 goto do_class;
5514 case NWORD:
5515 case NWORD + ADD_NL:
5516 mask = RI_WORD;
5517 goto do_class;
5518 case HEAD:
5519 case HEAD + ADD_NL:
5520 testval = mask = RI_HEAD;
5521 goto do_class;
5522 case NHEAD:
5523 case NHEAD + ADD_NL:
5524 mask = RI_HEAD;
5525 goto do_class;
5526 case ALPHA:
5527 case ALPHA + ADD_NL:
5528 testval = mask = RI_ALPHA;
5529 goto do_class;
5530 case NALPHA:
5531 case NALPHA + ADD_NL:
5532 mask = RI_ALPHA;
5533 goto do_class;
5534 case LOWER:
5535 case LOWER + ADD_NL:
5536 testval = mask = RI_LOWER;
5537 goto do_class;
5538 case NLOWER:
5539 case NLOWER + ADD_NL:
5540 mask = RI_LOWER;
5541 goto do_class;
5542 case UPPER:
5543 case UPPER + ADD_NL:
5544 testval = mask = RI_UPPER;
5545 goto do_class;
5546 case NUPPER:
5547 case NUPPER + ADD_NL:
5548 mask = RI_UPPER;
5549 goto do_class;
5550
5551 case EXACTLY:
5552 {
5553 int cu, cl;
5554
5555 /* This doesn't do a multi-byte character, because a MULTIBYTECODE
5556 * would have been used for it. */
5557 if (ireg_ic)
5558 {
5559 cu = TOUPPER_LOC(*opnd);
5560 cl = TOLOWER_LOC(*opnd);
5561 while (count < maxcount && (*scan == cu || *scan == cl))
5562 {
5563 count++;
5564 scan++;
5565 }
5566 }
5567 else
5568 {
5569 cu = *opnd;
5570 while (count < maxcount && *scan == cu)
5571 {
5572 count++;
5573 scan++;
5574 }
5575 }
5576 break;
5577 }
5578
5579#ifdef FEAT_MBYTE
5580 case MULTIBYTECODE:
5581 {
5582 int i, len, cf = 0;
5583
5584 /* Safety check (just in case 'encoding' was changed since
5585 * compiling the program). */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005586 if ((len = (*mb_ptr2len)(opnd)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 {
5588 if (ireg_ic && enc_utf8)
5589 cf = utf_fold(utf_ptr2char(opnd));
5590 while (count < maxcount)
5591 {
5592 for (i = 0; i < len; ++i)
5593 if (opnd[i] != scan[i])
5594 break;
5595 if (i < len && (!ireg_ic || !enc_utf8
5596 || utf_fold(utf_ptr2char(scan)) != cf))
5597 break;
5598 scan += len;
5599 ++count;
5600 }
5601 }
5602 }
5603 break;
5604#endif
5605
5606 case ANYOF:
5607 case ANYOF + ADD_NL:
5608 testval = TRUE;
5609 /*FALLTHROUGH*/
5610
5611 case ANYBUT:
5612 case ANYBUT + ADD_NL:
5613 while (count < maxcount)
5614 {
5615#ifdef FEAT_MBYTE
5616 int len;
5617#endif
5618 if (*scan == NUL)
5619 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005620 if (!WITH_NL(OP(p)) || reglnum > reg_maxline || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 break;
5622 reg_nextline();
5623 scan = reginput;
5624 if (got_int)
5625 break;
5626 }
5627 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5628 ++scan;
5629#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005630 else if (has_mbyte && (len = (*mb_ptr2len)(scan)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 {
5632 if ((cstrchr(opnd, (*mb_ptr2char)(scan)) == NULL) == testval)
5633 break;
5634 scan += len;
5635 }
5636#endif
5637 else
5638 {
5639 if ((cstrchr(opnd, *scan) == NULL) == testval)
5640 break;
5641 ++scan;
5642 }
5643 ++count;
5644 }
5645 break;
5646
5647 case NEWL:
5648 while (count < maxcount
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005649 && ((*scan == NUL && reglnum <= reg_maxline && !reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 || (*scan == '\n' && reg_line_lbr)))
5651 {
5652 count++;
5653 if (reg_line_lbr)
5654 ADVANCE_REGINPUT();
5655 else
5656 reg_nextline();
5657 scan = reginput;
5658 if (got_int)
5659 break;
5660 }
5661 break;
5662
5663 default: /* Oh dear. Called inappropriately. */
5664 EMSG(_(e_re_corr));
5665#ifdef DEBUG
5666 printf("Called regrepeat with op code %d\n", OP(p));
5667#endif
5668 break;
5669 }
5670
5671 reginput = scan;
5672
5673 return (int)count;
5674}
5675
5676/*
5677 * regnext - dig the "next" pointer out of a node
5678 */
5679 static char_u *
5680regnext(p)
5681 char_u *p;
5682{
5683 int offset;
5684
5685 if (p == JUST_CALC_SIZE)
5686 return NULL;
5687
5688 offset = NEXT(p);
5689 if (offset == 0)
5690 return NULL;
5691
Bram Moolenaar582fd852005-03-28 20:58:01 +00005692 if (OP(p) == BACK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 return p - offset;
5694 else
5695 return p + offset;
5696}
5697
5698/*
5699 * Check the regexp program for its magic number.
5700 * Return TRUE if it's wrong.
5701 */
5702 static int
5703prog_magic_wrong()
5704{
5705 if (UCHARAT(REG_MULTI
5706 ? reg_mmatch->regprog->program
5707 : reg_match->regprog->program) != REGMAGIC)
5708 {
5709 EMSG(_(e_re_corr));
5710 return TRUE;
5711 }
5712 return FALSE;
5713}
5714
5715/*
5716 * Cleanup the subexpressions, if this wasn't done yet.
5717 * This construction is used to clear the subexpressions only when they are
5718 * used (to increase speed).
5719 */
5720 static void
5721cleanup_subexpr()
5722{
5723 if (need_clear_subexpr)
5724 {
5725 if (REG_MULTI)
5726 {
5727 /* Use 0xff to set lnum to -1 */
5728 vim_memset(reg_startpos, 0xff, sizeof(lpos_T) * NSUBEXP);
5729 vim_memset(reg_endpos, 0xff, sizeof(lpos_T) * NSUBEXP);
5730 }
5731 else
5732 {
5733 vim_memset(reg_startp, 0, sizeof(char_u *) * NSUBEXP);
5734 vim_memset(reg_endp, 0, sizeof(char_u *) * NSUBEXP);
5735 }
5736 need_clear_subexpr = FALSE;
5737 }
5738}
5739
5740#ifdef FEAT_SYN_HL
5741 static void
5742cleanup_zsubexpr()
5743{
5744 if (need_clear_zsubexpr)
5745 {
5746 if (REG_MULTI)
5747 {
5748 /* Use 0xff to set lnum to -1 */
5749 vim_memset(reg_startzpos, 0xff, sizeof(lpos_T) * NSUBEXP);
5750 vim_memset(reg_endzpos, 0xff, sizeof(lpos_T) * NSUBEXP);
5751 }
5752 else
5753 {
5754 vim_memset(reg_startzp, 0, sizeof(char_u *) * NSUBEXP);
5755 vim_memset(reg_endzp, 0, sizeof(char_u *) * NSUBEXP);
5756 }
5757 need_clear_zsubexpr = FALSE;
5758 }
5759}
5760#endif
5761
5762/*
5763 * Advance reglnum, regline and reginput to the next line.
5764 */
5765 static void
5766reg_nextline()
5767{
5768 regline = reg_getline(++reglnum);
5769 reginput = regline;
5770 fast_breakcheck();
5771}
5772
5773/*
5774 * Save the input line and position in a regsave_T.
5775 */
5776 static void
Bram Moolenaar582fd852005-03-28 20:58:01 +00005777reg_save(save, gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 regsave_T *save;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005779 garray_T *gap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780{
5781 if (REG_MULTI)
5782 {
5783 save->rs_u.pos.col = (colnr_T)(reginput - regline);
5784 save->rs_u.pos.lnum = reglnum;
5785 }
5786 else
5787 save->rs_u.ptr = reginput;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005788 save->rs_len = gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789}
5790
5791/*
5792 * Restore the input line and position from a regsave_T.
5793 */
5794 static void
Bram Moolenaar582fd852005-03-28 20:58:01 +00005795reg_restore(save, gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 regsave_T *save;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005797 garray_T *gap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798{
5799 if (REG_MULTI)
5800 {
5801 if (reglnum != save->rs_u.pos.lnum)
5802 {
5803 /* only call reg_getline() when the line number changed to save
5804 * a bit of time */
5805 reglnum = save->rs_u.pos.lnum;
5806 regline = reg_getline(reglnum);
5807 }
5808 reginput = regline + save->rs_u.pos.col;
5809 }
5810 else
5811 reginput = save->rs_u.ptr;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005812 gap->ga_len = save->rs_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813}
5814
5815/*
5816 * Return TRUE if current position is equal to saved position.
5817 */
5818 static int
5819reg_save_equal(save)
5820 regsave_T *save;
5821{
5822 if (REG_MULTI)
5823 return reglnum == save->rs_u.pos.lnum
5824 && reginput == regline + save->rs_u.pos.col;
5825 return reginput == save->rs_u.ptr;
5826}
5827
5828/*
5829 * Tentatively set the sub-expression start to the current position (after
5830 * calling regmatch() they will have changed). Need to save the existing
5831 * values for when there is no match.
5832 * Use se_save() to use pointer (save_se_multi()) or position (save_se_one()),
5833 * depending on REG_MULTI.
5834 */
5835 static void
5836save_se_multi(savep, posp)
5837 save_se_T *savep;
5838 lpos_T *posp;
5839{
5840 savep->se_u.pos = *posp;
5841 posp->lnum = reglnum;
5842 posp->col = (colnr_T)(reginput - regline);
5843}
5844
5845 static void
5846save_se_one(savep, pp)
5847 save_se_T *savep;
5848 char_u **pp;
5849{
5850 savep->se_u.ptr = *pp;
5851 *pp = reginput;
5852}
5853
5854/*
5855 * Compare a number with the operand of RE_LNUM, RE_COL or RE_VCOL.
5856 */
5857 static int
5858re_num_cmp(val, scan)
5859 long_u val;
5860 char_u *scan;
5861{
5862 long_u n = OPERAND_MIN(scan);
5863
5864 if (OPERAND_CMP(scan) == '>')
5865 return val > n;
5866 if (OPERAND_CMP(scan) == '<')
5867 return val < n;
5868 return val == n;
5869}
5870
5871
5872#ifdef DEBUG
5873
5874/*
5875 * regdump - dump a regexp onto stdout in vaguely comprehensible form
5876 */
5877 static void
5878regdump(pattern, r)
5879 char_u *pattern;
5880 regprog_T *r;
5881{
5882 char_u *s;
5883 int op = EXACTLY; /* Arbitrary non-END op. */
5884 char_u *next;
5885 char_u *end = NULL;
5886
5887 printf("\r\nregcomp(%s):\r\n", pattern);
5888
5889 s = r->program + 1;
5890 /*
5891 * Loop until we find the END that isn't before a referred next (an END
5892 * can also appear in a NOMATCH operand).
5893 */
5894 while (op != END || s <= end)
5895 {
5896 op = OP(s);
5897 printf("%2d%s", (int)(s - r->program), regprop(s)); /* Where, what. */
5898 next = regnext(s);
5899 if (next == NULL) /* Next ptr. */
5900 printf("(0)");
5901 else
5902 printf("(%d)", (int)((s - r->program) + (next - s)));
5903 if (end < next)
5904 end = next;
5905 if (op == BRACE_LIMITS)
5906 {
5907 /* Two short ints */
5908 printf(" minval %ld, maxval %ld", OPERAND_MIN(s), OPERAND_MAX(s));
5909 s += 8;
5910 }
5911 s += 3;
5912 if (op == ANYOF || op == ANYOF + ADD_NL
5913 || op == ANYBUT || op == ANYBUT + ADD_NL
5914 || op == EXACTLY)
5915 {
5916 /* Literal string, where present. */
5917 while (*s != NUL)
5918 printf("%c", *s++);
5919 s++;
5920 }
5921 printf("\r\n");
5922 }
5923
5924 /* Header fields of interest. */
5925 if (r->regstart != NUL)
5926 printf("start `%s' 0x%x; ", r->regstart < 256
5927 ? (char *)transchar(r->regstart)
5928 : "multibyte", r->regstart);
5929 if (r->reganch)
5930 printf("anchored; ");
5931 if (r->regmust != NULL)
5932 printf("must have \"%s\"", r->regmust);
5933 printf("\r\n");
5934}
5935
5936/*
5937 * regprop - printable representation of opcode
5938 */
5939 static char_u *
5940regprop(op)
5941 char_u *op;
5942{
5943 char_u *p;
5944 static char_u buf[50];
5945
5946 (void) strcpy(buf, ":");
5947
5948 switch (OP(op))
5949 {
5950 case BOL:
5951 p = "BOL";
5952 break;
5953 case EOL:
5954 p = "EOL";
5955 break;
5956 case RE_BOF:
5957 p = "BOF";
5958 break;
5959 case RE_EOF:
5960 p = "EOF";
5961 break;
5962 case CURSOR:
5963 p = "CURSOR";
5964 break;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00005965 case RE_VISUAL:
5966 p = "RE_VISUAL";
5967 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 case RE_LNUM:
5969 p = "RE_LNUM";
5970 break;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00005971 case RE_MARK:
5972 p = "RE_MARK";
5973 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 case RE_COL:
5975 p = "RE_COL";
5976 break;
5977 case RE_VCOL:
5978 p = "RE_VCOL";
5979 break;
5980 case BOW:
5981 p = "BOW";
5982 break;
5983 case EOW:
5984 p = "EOW";
5985 break;
5986 case ANY:
5987 p = "ANY";
5988 break;
5989 case ANY + ADD_NL:
5990 p = "ANY+NL";
5991 break;
5992 case ANYOF:
5993 p = "ANYOF";
5994 break;
5995 case ANYOF + ADD_NL:
5996 p = "ANYOF+NL";
5997 break;
5998 case ANYBUT:
5999 p = "ANYBUT";
6000 break;
6001 case ANYBUT + ADD_NL:
6002 p = "ANYBUT+NL";
6003 break;
6004 case IDENT:
6005 p = "IDENT";
6006 break;
6007 case IDENT + ADD_NL:
6008 p = "IDENT+NL";
6009 break;
6010 case SIDENT:
6011 p = "SIDENT";
6012 break;
6013 case SIDENT + ADD_NL:
6014 p = "SIDENT+NL";
6015 break;
6016 case KWORD:
6017 p = "KWORD";
6018 break;
6019 case KWORD + ADD_NL:
6020 p = "KWORD+NL";
6021 break;
6022 case SKWORD:
6023 p = "SKWORD";
6024 break;
6025 case SKWORD + ADD_NL:
6026 p = "SKWORD+NL";
6027 break;
6028 case FNAME:
6029 p = "FNAME";
6030 break;
6031 case FNAME + ADD_NL:
6032 p = "FNAME+NL";
6033 break;
6034 case SFNAME:
6035 p = "SFNAME";
6036 break;
6037 case SFNAME + ADD_NL:
6038 p = "SFNAME+NL";
6039 break;
6040 case PRINT:
6041 p = "PRINT";
6042 break;
6043 case PRINT + ADD_NL:
6044 p = "PRINT+NL";
6045 break;
6046 case SPRINT:
6047 p = "SPRINT";
6048 break;
6049 case SPRINT + ADD_NL:
6050 p = "SPRINT+NL";
6051 break;
6052 case WHITE:
6053 p = "WHITE";
6054 break;
6055 case WHITE + ADD_NL:
6056 p = "WHITE+NL";
6057 break;
6058 case NWHITE:
6059 p = "NWHITE";
6060 break;
6061 case NWHITE + ADD_NL:
6062 p = "NWHITE+NL";
6063 break;
6064 case DIGIT:
6065 p = "DIGIT";
6066 break;
6067 case DIGIT + ADD_NL:
6068 p = "DIGIT+NL";
6069 break;
6070 case NDIGIT:
6071 p = "NDIGIT";
6072 break;
6073 case NDIGIT + ADD_NL:
6074 p = "NDIGIT+NL";
6075 break;
6076 case HEX:
6077 p = "HEX";
6078 break;
6079 case HEX + ADD_NL:
6080 p = "HEX+NL";
6081 break;
6082 case NHEX:
6083 p = "NHEX";
6084 break;
6085 case NHEX + ADD_NL:
6086 p = "NHEX+NL";
6087 break;
6088 case OCTAL:
6089 p = "OCTAL";
6090 break;
6091 case OCTAL + ADD_NL:
6092 p = "OCTAL+NL";
6093 break;
6094 case NOCTAL:
6095 p = "NOCTAL";
6096 break;
6097 case NOCTAL + ADD_NL:
6098 p = "NOCTAL+NL";
6099 break;
6100 case WORD:
6101 p = "WORD";
6102 break;
6103 case WORD + ADD_NL:
6104 p = "WORD+NL";
6105 break;
6106 case NWORD:
6107 p = "NWORD";
6108 break;
6109 case NWORD + ADD_NL:
6110 p = "NWORD+NL";
6111 break;
6112 case HEAD:
6113 p = "HEAD";
6114 break;
6115 case HEAD + ADD_NL:
6116 p = "HEAD+NL";
6117 break;
6118 case NHEAD:
6119 p = "NHEAD";
6120 break;
6121 case NHEAD + ADD_NL:
6122 p = "NHEAD+NL";
6123 break;
6124 case ALPHA:
6125 p = "ALPHA";
6126 break;
6127 case ALPHA + ADD_NL:
6128 p = "ALPHA+NL";
6129 break;
6130 case NALPHA:
6131 p = "NALPHA";
6132 break;
6133 case NALPHA + ADD_NL:
6134 p = "NALPHA+NL";
6135 break;
6136 case LOWER:
6137 p = "LOWER";
6138 break;
6139 case LOWER + ADD_NL:
6140 p = "LOWER+NL";
6141 break;
6142 case NLOWER:
6143 p = "NLOWER";
6144 break;
6145 case NLOWER + ADD_NL:
6146 p = "NLOWER+NL";
6147 break;
6148 case UPPER:
6149 p = "UPPER";
6150 break;
6151 case UPPER + ADD_NL:
6152 p = "UPPER+NL";
6153 break;
6154 case NUPPER:
6155 p = "NUPPER";
6156 break;
6157 case NUPPER + ADD_NL:
6158 p = "NUPPER+NL";
6159 break;
6160 case BRANCH:
6161 p = "BRANCH";
6162 break;
6163 case EXACTLY:
6164 p = "EXACTLY";
6165 break;
6166 case NOTHING:
6167 p = "NOTHING";
6168 break;
6169 case BACK:
6170 p = "BACK";
6171 break;
6172 case END:
6173 p = "END";
6174 break;
6175 case MOPEN + 0:
6176 p = "MATCH START";
6177 break;
6178 case MOPEN + 1:
6179 case MOPEN + 2:
6180 case MOPEN + 3:
6181 case MOPEN + 4:
6182 case MOPEN + 5:
6183 case MOPEN + 6:
6184 case MOPEN + 7:
6185 case MOPEN + 8:
6186 case MOPEN + 9:
6187 sprintf(buf + STRLEN(buf), "MOPEN%d", OP(op) - MOPEN);
6188 p = NULL;
6189 break;
6190 case MCLOSE + 0:
6191 p = "MATCH END";
6192 break;
6193 case MCLOSE + 1:
6194 case MCLOSE + 2:
6195 case MCLOSE + 3:
6196 case MCLOSE + 4:
6197 case MCLOSE + 5:
6198 case MCLOSE + 6:
6199 case MCLOSE + 7:
6200 case MCLOSE + 8:
6201 case MCLOSE + 9:
6202 sprintf(buf + STRLEN(buf), "MCLOSE%d", OP(op) - MCLOSE);
6203 p = NULL;
6204 break;
6205 case BACKREF + 1:
6206 case BACKREF + 2:
6207 case BACKREF + 3:
6208 case BACKREF + 4:
6209 case BACKREF + 5:
6210 case BACKREF + 6:
6211 case BACKREF + 7:
6212 case BACKREF + 8:
6213 case BACKREF + 9:
6214 sprintf(buf + STRLEN(buf), "BACKREF%d", OP(op) - BACKREF);
6215 p = NULL;
6216 break;
6217 case NOPEN:
6218 p = "NOPEN";
6219 break;
6220 case NCLOSE:
6221 p = "NCLOSE";
6222 break;
6223#ifdef FEAT_SYN_HL
6224 case ZOPEN + 1:
6225 case ZOPEN + 2:
6226 case ZOPEN + 3:
6227 case ZOPEN + 4:
6228 case ZOPEN + 5:
6229 case ZOPEN + 6:
6230 case ZOPEN + 7:
6231 case ZOPEN + 8:
6232 case ZOPEN + 9:
6233 sprintf(buf + STRLEN(buf), "ZOPEN%d", OP(op) - ZOPEN);
6234 p = NULL;
6235 break;
6236 case ZCLOSE + 1:
6237 case ZCLOSE + 2:
6238 case ZCLOSE + 3:
6239 case ZCLOSE + 4:
6240 case ZCLOSE + 5:
6241 case ZCLOSE + 6:
6242 case ZCLOSE + 7:
6243 case ZCLOSE + 8:
6244 case ZCLOSE + 9:
6245 sprintf(buf + STRLEN(buf), "ZCLOSE%d", OP(op) - ZCLOSE);
6246 p = NULL;
6247 break;
6248 case ZREF + 1:
6249 case ZREF + 2:
6250 case ZREF + 3:
6251 case ZREF + 4:
6252 case ZREF + 5:
6253 case ZREF + 6:
6254 case ZREF + 7:
6255 case ZREF + 8:
6256 case ZREF + 9:
6257 sprintf(buf + STRLEN(buf), "ZREF%d", OP(op) - ZREF);
6258 p = NULL;
6259 break;
6260#endif
6261 case STAR:
6262 p = "STAR";
6263 break;
6264 case PLUS:
6265 p = "PLUS";
6266 break;
6267 case NOMATCH:
6268 p = "NOMATCH";
6269 break;
6270 case MATCH:
6271 p = "MATCH";
6272 break;
6273 case BEHIND:
6274 p = "BEHIND";
6275 break;
6276 case NOBEHIND:
6277 p = "NOBEHIND";
6278 break;
6279 case SUBPAT:
6280 p = "SUBPAT";
6281 break;
6282 case BRACE_LIMITS:
6283 p = "BRACE_LIMITS";
6284 break;
6285 case BRACE_SIMPLE:
6286 p = "BRACE_SIMPLE";
6287 break;
6288 case BRACE_COMPLEX + 0:
6289 case BRACE_COMPLEX + 1:
6290 case BRACE_COMPLEX + 2:
6291 case BRACE_COMPLEX + 3:
6292 case BRACE_COMPLEX + 4:
6293 case BRACE_COMPLEX + 5:
6294 case BRACE_COMPLEX + 6:
6295 case BRACE_COMPLEX + 7:
6296 case BRACE_COMPLEX + 8:
6297 case BRACE_COMPLEX + 9:
6298 sprintf(buf + STRLEN(buf), "BRACE_COMPLEX%d", OP(op) - BRACE_COMPLEX);
6299 p = NULL;
6300 break;
6301#ifdef FEAT_MBYTE
6302 case MULTIBYTECODE:
6303 p = "MULTIBYTECODE";
6304 break;
6305#endif
6306 case NEWL:
6307 p = "NEWL";
6308 break;
6309 default:
6310 sprintf(buf + STRLEN(buf), "corrupt %d", OP(op));
6311 p = NULL;
6312 break;
6313 }
6314 if (p != NULL)
6315 (void) strcat(buf, p);
6316 return buf;
6317}
6318#endif
6319
6320#ifdef FEAT_MBYTE
6321static void mb_decompose __ARGS((int c, int *c1, int *c2, int *c3));
6322
6323typedef struct
6324{
6325 int a, b, c;
6326} decomp_T;
6327
6328
6329/* 0xfb20 - 0xfb4f */
Bram Moolenaard6f676d2005-06-01 21:51:55 +00006330static decomp_T decomp_table[0xfb4f-0xfb20+1] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331{
6332 {0x5e2,0,0}, /* 0xfb20 alt ayin */
6333 {0x5d0,0,0}, /* 0xfb21 alt alef */
6334 {0x5d3,0,0}, /* 0xfb22 alt dalet */
6335 {0x5d4,0,0}, /* 0xfb23 alt he */
6336 {0x5db,0,0}, /* 0xfb24 alt kaf */
6337 {0x5dc,0,0}, /* 0xfb25 alt lamed */
6338 {0x5dd,0,0}, /* 0xfb26 alt mem-sofit */
6339 {0x5e8,0,0}, /* 0xfb27 alt resh */
6340 {0x5ea,0,0}, /* 0xfb28 alt tav */
6341 {'+', 0, 0}, /* 0xfb29 alt plus */
6342 {0x5e9, 0x5c1, 0}, /* 0xfb2a shin+shin-dot */
6343 {0x5e9, 0x5c2, 0}, /* 0xfb2b shin+sin-dot */
6344 {0x5e9, 0x5c1, 0x5bc}, /* 0xfb2c shin+shin-dot+dagesh */
6345 {0x5e9, 0x5c2, 0x5bc}, /* 0xfb2d shin+sin-dot+dagesh */
6346 {0x5d0, 0x5b7, 0}, /* 0xfb2e alef+patah */
6347 {0x5d0, 0x5b8, 0}, /* 0xfb2f alef+qamats */
6348 {0x5d0, 0x5b4, 0}, /* 0xfb30 alef+hiriq */
6349 {0x5d1, 0x5bc, 0}, /* 0xfb31 bet+dagesh */
6350 {0x5d2, 0x5bc, 0}, /* 0xfb32 gimel+dagesh */
6351 {0x5d3, 0x5bc, 0}, /* 0xfb33 dalet+dagesh */
6352 {0x5d4, 0x5bc, 0}, /* 0xfb34 he+dagesh */
6353 {0x5d5, 0x5bc, 0}, /* 0xfb35 vav+dagesh */
6354 {0x5d6, 0x5bc, 0}, /* 0xfb36 zayin+dagesh */
6355 {0xfb37, 0, 0}, /* 0xfb37 -- UNUSED */
6356 {0x5d8, 0x5bc, 0}, /* 0xfb38 tet+dagesh */
6357 {0x5d9, 0x5bc, 0}, /* 0xfb39 yud+dagesh */
6358 {0x5da, 0x5bc, 0}, /* 0xfb3a kaf sofit+dagesh */
6359 {0x5db, 0x5bc, 0}, /* 0xfb3b kaf+dagesh */
6360 {0x5dc, 0x5bc, 0}, /* 0xfb3c lamed+dagesh */
6361 {0xfb3d, 0, 0}, /* 0xfb3d -- UNUSED */
6362 {0x5de, 0x5bc, 0}, /* 0xfb3e mem+dagesh */
6363 {0xfb3f, 0, 0}, /* 0xfb3f -- UNUSED */
6364 {0x5e0, 0x5bc, 0}, /* 0xfb40 nun+dagesh */
6365 {0x5e1, 0x5bc, 0}, /* 0xfb41 samech+dagesh */
6366 {0xfb42, 0, 0}, /* 0xfb42 -- UNUSED */
6367 {0x5e3, 0x5bc, 0}, /* 0xfb43 pe sofit+dagesh */
6368 {0x5e4, 0x5bc,0}, /* 0xfb44 pe+dagesh */
6369 {0xfb45, 0, 0}, /* 0xfb45 -- UNUSED */
6370 {0x5e6, 0x5bc, 0}, /* 0xfb46 tsadi+dagesh */
6371 {0x5e7, 0x5bc, 0}, /* 0xfb47 qof+dagesh */
6372 {0x5e8, 0x5bc, 0}, /* 0xfb48 resh+dagesh */
6373 {0x5e9, 0x5bc, 0}, /* 0xfb49 shin+dagesh */
6374 {0x5ea, 0x5bc, 0}, /* 0xfb4a tav+dagesh */
6375 {0x5d5, 0x5b9, 0}, /* 0xfb4b vav+holam */
6376 {0x5d1, 0x5bf, 0}, /* 0xfb4c bet+rafe */
6377 {0x5db, 0x5bf, 0}, /* 0xfb4d kaf+rafe */
6378 {0x5e4, 0x5bf, 0}, /* 0xfb4e pe+rafe */
6379 {0x5d0, 0x5dc, 0} /* 0xfb4f alef-lamed */
6380};
6381
6382 static void
6383mb_decompose(c, c1, c2, c3)
6384 int c, *c1, *c2, *c3;
6385{
6386 decomp_T d;
6387
6388 if (c >= 0x4b20 && c <= 0xfb4f)
6389 {
6390 d = decomp_table[c - 0xfb20];
6391 *c1 = d.a;
6392 *c2 = d.b;
6393 *c3 = d.c;
6394 }
6395 else
6396 {
6397 *c1 = c;
6398 *c2 = *c3 = 0;
6399 }
6400}
6401#endif
6402
6403/*
6404 * Compare two strings, ignore case if ireg_ic set.
6405 * Return 0 if strings match, non-zero otherwise.
6406 * Correct the length "*n" when composing characters are ignored.
6407 */
6408 static int
6409cstrncmp(s1, s2, n)
6410 char_u *s1, *s2;
6411 int *n;
6412{
6413 int result;
6414
6415 if (!ireg_ic)
6416 result = STRNCMP(s1, s2, *n);
6417 else
6418 result = MB_STRNICMP(s1, s2, *n);
6419
6420#ifdef FEAT_MBYTE
6421 /* if it failed and it's utf8 and we want to combineignore: */
6422 if (result != 0 && enc_utf8 && ireg_icombine)
6423 {
6424 char_u *str1, *str2;
6425 int c1, c2, c11, c12;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 int junk;
6427
6428 /* we have to handle the strcmp ourselves, since it is necessary to
6429 * deal with the composing characters by ignoring them: */
6430 str1 = s1;
6431 str2 = s2;
6432 c1 = c2 = 0;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006433 while ((int)(str1 - s1) < *n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 {
6435 c1 = mb_ptr2char_adv(&str1);
6436 c2 = mb_ptr2char_adv(&str2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437
6438 /* decompose the character if necessary, into 'base' characters
6439 * because I don't care about Arabic, I will hard-code the Hebrew
6440 * which I *do* care about! So sue me... */
6441 if (c1 != c2 && (!ireg_ic || utf_fold(c1) != utf_fold(c2)))
6442 {
6443 /* decomposition necessary? */
6444 mb_decompose(c1, &c11, &junk, &junk);
6445 mb_decompose(c2, &c12, &junk, &junk);
6446 c1 = c11;
6447 c2 = c12;
6448 if (c11 != c12 && (!ireg_ic || utf_fold(c11) != utf_fold(c12)))
6449 break;
6450 }
6451 }
6452 result = c2 - c1;
6453 if (result == 0)
6454 *n = (int)(str2 - s2);
6455 }
6456#endif
6457
6458 return result;
6459}
6460
6461/*
6462 * cstrchr: This function is used a lot for simple searches, keep it fast!
6463 */
6464 static char_u *
6465cstrchr(s, c)
6466 char_u *s;
6467 int c;
6468{
6469 char_u *p;
6470 int cc;
6471
6472 if (!ireg_ic
6473#ifdef FEAT_MBYTE
6474 || (!enc_utf8 && mb_char2len(c) > 1)
6475#endif
6476 )
6477 return vim_strchr(s, c);
6478
6479 /* tolower() and toupper() can be slow, comparing twice should be a lot
6480 * faster (esp. when using MS Visual C++!).
6481 * For UTF-8 need to use folded case. */
6482#ifdef FEAT_MBYTE
6483 if (enc_utf8 && c > 0x80)
6484 cc = utf_fold(c);
6485 else
6486#endif
6487 if (isupper(c))
6488 cc = TOLOWER_LOC(c);
6489 else if (islower(c))
6490 cc = TOUPPER_LOC(c);
6491 else
6492 return vim_strchr(s, c);
6493
6494#ifdef FEAT_MBYTE
6495 if (has_mbyte)
6496 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006497 for (p = s; *p != NUL; p += (*mb_ptr2len)(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 {
6499 if (enc_utf8 && c > 0x80)
6500 {
6501 if (utf_fold(utf_ptr2char(p)) == cc)
6502 return p;
6503 }
6504 else if (*p == c || *p == cc)
6505 return p;
6506 }
6507 }
6508 else
6509#endif
6510 /* Faster version for when there are no multi-byte characters. */
6511 for (p = s; *p != NUL; ++p)
6512 if (*p == c || *p == cc)
6513 return p;
6514
6515 return NULL;
6516}
6517
6518/***************************************************************
6519 * regsub stuff *
6520 ***************************************************************/
6521
6522/* This stuff below really confuses cc on an SGI -- webb */
6523#ifdef __sgi
6524# undef __ARGS
6525# define __ARGS(x) ()
6526#endif
6527
6528/*
6529 * We should define ftpr as a pointer to a function returning a pointer to
6530 * a function returning a pointer to a function ...
6531 * This is impossible, so we declare a pointer to a function returning a
6532 * pointer to a function returning void. This should work for all compilers.
6533 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00006534typedef void (*(*fptr_T) __ARGS((int *, int)))();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00006536static fptr_T do_upper __ARGS((int *, int));
6537static fptr_T do_Upper __ARGS((int *, int));
6538static fptr_T do_lower __ARGS((int *, int));
6539static fptr_T do_Lower __ARGS((int *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540
6541static int vim_regsub_both __ARGS((char_u *source, char_u *dest, int copy, int magic, int backslash));
6542
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00006543 static fptr_T
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544do_upper(d, c)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00006545 int *d;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 int c;
6547{
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00006548 *d = MB_TOUPPER(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00006550 return (fptr_T)NULL;
6551}
6552
6553 static fptr_T
6554do_Upper(d, c)
6555 int *d;
6556 int c;
6557{
6558 *d = MB_TOUPPER(c);
6559
6560 return (fptr_T)do_Upper;
6561}
6562
6563 static fptr_T
6564do_lower(d, c)
6565 int *d;
6566 int c;
6567{
6568 *d = MB_TOLOWER(c);
6569
6570 return (fptr_T)NULL;
6571}
6572
6573 static fptr_T
6574do_Lower(d, c)
6575 int *d;
6576 int c;
6577{
6578 *d = MB_TOLOWER(c);
6579
6580 return (fptr_T)do_Lower;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581}
6582
6583/*
6584 * regtilde(): Replace tildes in the pattern by the old pattern.
6585 *
6586 * Short explanation of the tilde: It stands for the previous replacement
6587 * pattern. If that previous pattern also contains a ~ we should go back a
6588 * step further... But we insert the previous pattern into the current one
6589 * and remember that.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00006590 * This still does not handle the case where "magic" changes. So require the
6591 * user to keep his hands off of "magic".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 *
6593 * The tildes are parsed once before the first call to vim_regsub().
6594 */
6595 char_u *
6596regtilde(source, magic)
6597 char_u *source;
6598 int magic;
6599{
6600 char_u *newsub = source;
6601 char_u *tmpsub;
6602 char_u *p;
6603 int len;
6604 int prevlen;
6605
6606 for (p = newsub; *p; ++p)
6607 {
6608 if ((*p == '~' && magic) || (*p == '\\' && *(p + 1) == '~' && !magic))
6609 {
6610 if (reg_prev_sub != NULL)
6611 {
6612 /* length = len(newsub) - 1 + len(prev_sub) + 1 */
6613 prevlen = (int)STRLEN(reg_prev_sub);
6614 tmpsub = alloc((unsigned)(STRLEN(newsub) + prevlen));
6615 if (tmpsub != NULL)
6616 {
6617 /* copy prefix */
6618 len = (int)(p - newsub); /* not including ~ */
6619 mch_memmove(tmpsub, newsub, (size_t)len);
6620 /* interpretate tilde */
6621 mch_memmove(tmpsub + len, reg_prev_sub, (size_t)prevlen);
6622 /* copy postfix */
6623 if (!magic)
6624 ++p; /* back off \ */
6625 STRCPY(tmpsub + len + prevlen, p + 1);
6626
6627 if (newsub != source) /* already allocated newsub */
6628 vim_free(newsub);
6629 newsub = tmpsub;
6630 p = newsub + len + prevlen;
6631 }
6632 }
6633 else if (magic)
6634 STRCPY(p, p + 1); /* remove '~' */
6635 else
6636 STRCPY(p, p + 2); /* remove '\~' */
6637 --p;
6638 }
6639 else
6640 {
6641 if (*p == '\\' && p[1]) /* skip escaped characters */
6642 ++p;
6643#ifdef FEAT_MBYTE
6644 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006645 p += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646#endif
6647 }
6648 }
6649
6650 vim_free(reg_prev_sub);
6651 if (newsub != source) /* newsub was allocated, just keep it */
6652 reg_prev_sub = newsub;
6653 else /* no ~ found, need to save newsub */
6654 reg_prev_sub = vim_strsave(newsub);
6655 return newsub;
6656}
6657
6658#ifdef FEAT_EVAL
6659static int can_f_submatch = FALSE; /* TRUE when submatch() can be used */
6660
6661/* These pointers are used instead of reg_match and reg_mmatch for
6662 * reg_submatch(). Needed for when the substitution string is an expression
6663 * that contains a call to substitute() and submatch(). */
6664static regmatch_T *submatch_match;
6665static regmmatch_T *submatch_mmatch;
6666#endif
6667
6668#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
6669/*
6670 * vim_regsub() - perform substitutions after a vim_regexec() or
6671 * vim_regexec_multi() match.
6672 *
6673 * If "copy" is TRUE really copy into "dest".
6674 * If "copy" is FALSE nothing is copied, this is just to find out the length
6675 * of the result.
6676 *
6677 * If "backslash" is TRUE, a backslash will be removed later, need to double
6678 * them to keep them, and insert a backslash before a CR to avoid it being
6679 * replaced with a line break later.
6680 *
6681 * Note: The matched text must not change between the call of
6682 * vim_regexec()/vim_regexec_multi() and vim_regsub()! It would make the back
6683 * references invalid!
6684 *
6685 * Returns the size of the replacement, including terminating NUL.
6686 */
6687 int
6688vim_regsub(rmp, source, dest, copy, magic, backslash)
6689 regmatch_T *rmp;
6690 char_u *source;
6691 char_u *dest;
6692 int copy;
6693 int magic;
6694 int backslash;
6695{
6696 reg_match = rmp;
6697 reg_mmatch = NULL;
6698 reg_maxline = 0;
6699 return vim_regsub_both(source, dest, copy, magic, backslash);
6700}
6701#endif
6702
6703 int
6704vim_regsub_multi(rmp, lnum, source, dest, copy, magic, backslash)
6705 regmmatch_T *rmp;
6706 linenr_T lnum;
6707 char_u *source;
6708 char_u *dest;
6709 int copy;
6710 int magic;
6711 int backslash;
6712{
6713 reg_match = NULL;
6714 reg_mmatch = rmp;
6715 reg_buf = curbuf; /* always works on the current buffer! */
6716 reg_firstlnum = lnum;
6717 reg_maxline = curbuf->b_ml.ml_line_count - lnum;
6718 return vim_regsub_both(source, dest, copy, magic, backslash);
6719}
6720
6721 static int
6722vim_regsub_both(source, dest, copy, magic, backslash)
6723 char_u *source;
6724 char_u *dest;
6725 int copy;
6726 int magic;
6727 int backslash;
6728{
6729 char_u *src;
6730 char_u *dst;
6731 char_u *s;
6732 int c;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00006733 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 int no = -1;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00006735 fptr_T func = (fptr_T)NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 linenr_T clnum = 0; /* init for GCC */
6737 int len = 0; /* init for GCC */
6738#ifdef FEAT_EVAL
6739 static char_u *eval_result = NULL;
6740#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741
6742 /* Be paranoid... */
6743 if (source == NULL || dest == NULL)
6744 {
6745 EMSG(_(e_null));
6746 return 0;
6747 }
6748 if (prog_magic_wrong())
6749 return 0;
6750 src = source;
6751 dst = dest;
6752
6753 /*
6754 * When the substitute part starts with "\=" evaluate it as an expression.
6755 */
6756 if (source[0] == '\\' && source[1] == '='
6757#ifdef FEAT_EVAL
6758 && !can_f_submatch /* can't do this recursively */
6759#endif
6760 )
6761 {
6762#ifdef FEAT_EVAL
6763 /* To make sure that the length doesn't change between checking the
6764 * length and copying the string, and to speed up things, the
6765 * resulting string is saved from the call with "copy" == FALSE to the
6766 * call with "copy" == TRUE. */
6767 if (copy)
6768 {
6769 if (eval_result != NULL)
6770 {
6771 STRCPY(dest, eval_result);
6772 dst += STRLEN(eval_result);
6773 vim_free(eval_result);
6774 eval_result = NULL;
6775 }
6776 }
6777 else
6778 {
6779 linenr_T save_reg_maxline;
6780 win_T *save_reg_win;
6781 int save_ireg_ic;
6782
6783 vim_free(eval_result);
6784
6785 /* The expression may contain substitute(), which calls us
6786 * recursively. Make sure submatch() gets the text from the first
6787 * level. Don't need to save "reg_buf", because
6788 * vim_regexec_multi() can't be called recursively. */
6789 submatch_match = reg_match;
6790 submatch_mmatch = reg_mmatch;
6791 save_reg_maxline = reg_maxline;
6792 save_reg_win = reg_win;
6793 save_ireg_ic = ireg_ic;
6794 can_f_submatch = TRUE;
6795
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006796 eval_result = eval_to_string(source + 2, NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 if (eval_result != NULL)
6798 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006799 for (s = eval_result; *s != NUL; mb_ptr_adv(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 {
6801 /* Change NL to CR, so that it becomes a line break.
6802 * Skip over a backslashed character. */
6803 if (*s == NL)
6804 *s = CAR;
6805 else if (*s == '\\' && s[1] != NUL)
6806 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 }
6808
6809 dst += STRLEN(eval_result);
6810 }
6811
6812 reg_match = submatch_match;
6813 reg_mmatch = submatch_mmatch;
6814 reg_maxline = save_reg_maxline;
6815 reg_win = save_reg_win;
6816 ireg_ic = save_ireg_ic;
6817 can_f_submatch = FALSE;
6818 }
6819#endif
6820 }
6821 else
6822 while ((c = *src++) != NUL)
6823 {
6824 if (c == '&' && magic)
6825 no = 0;
6826 else if (c == '\\' && *src != NUL)
6827 {
6828 if (*src == '&' && !magic)
6829 {
6830 ++src;
6831 no = 0;
6832 }
6833 else if ('0' <= *src && *src <= '9')
6834 {
6835 no = *src++ - '0';
6836 }
6837 else if (vim_strchr((char_u *)"uUlLeE", *src))
6838 {
6839 switch (*src++)
6840 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00006841 case 'u': func = (fptr_T)do_upper;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 continue;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00006843 case 'U': func = (fptr_T)do_Upper;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 continue;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00006845 case 'l': func = (fptr_T)do_lower;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 continue;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00006847 case 'L': func = (fptr_T)do_Lower;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 continue;
6849 case 'e':
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00006850 case 'E': func = (fptr_T)NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 continue;
6852 }
6853 }
6854 }
6855 if (no < 0) /* Ordinary character. */
6856 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00006857 if (c == K_SPECIAL && src[0] != NUL && src[1] != NUL)
6858 {
6859 /* Copy a specialy key as-is. */
6860 if (copy)
6861 {
6862 *dst++ = c;
6863 *dst++ = *src++;
6864 *dst++ = *src++;
6865 }
6866 else
6867 {
6868 dst += 3;
6869 src += 2;
6870 }
6871 continue;
6872 }
6873
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 if (c == '\\' && *src != NUL)
6875 {
6876 /* Check for abbreviations -- webb */
6877 switch (*src)
6878 {
6879 case 'r': c = CAR; ++src; break;
6880 case 'n': c = NL; ++src; break;
6881 case 't': c = TAB; ++src; break;
6882 /* Oh no! \e already has meaning in subst pat :-( */
6883 /* case 'e': c = ESC; ++src; break; */
6884 case 'b': c = Ctrl_H; ++src; break;
6885
6886 /* If "backslash" is TRUE the backslash will be removed
6887 * later. Used to insert a literal CR. */
6888 default: if (backslash)
6889 {
6890 if (copy)
6891 *dst = '\\';
6892 ++dst;
6893 }
6894 c = *src++;
6895 }
6896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897#ifdef FEAT_MBYTE
Bram Moolenaardb552d602006-03-23 22:59:57 +00006898 else if (has_mbyte)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00006899 c = mb_ptr2char(src - 1);
6900#endif
6901
Bram Moolenaardb552d602006-03-23 22:59:57 +00006902 /* Write to buffer, if copy is set. */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00006903 if (func == (fptr_T)NULL) /* just copy */
6904 cc = c;
6905 else
6906 /* Turbo C complains without the typecast */
6907 func = (fptr_T)(func(&cc, c));
6908
6909#ifdef FEAT_MBYTE
6910 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00006912 src += mb_ptr2len(src - 1) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 if (copy)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00006914 mb_char2bytes(cc, dst);
6915 dst += mb_char2len(cc) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 }
6917 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918#endif
6919 if (copy)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00006920 *dst = cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 dst++;
6922 }
6923 else
6924 {
6925 if (REG_MULTI)
6926 {
6927 clnum = reg_mmatch->startpos[no].lnum;
6928 if (clnum < 0 || reg_mmatch->endpos[no].lnum < 0)
6929 s = NULL;
6930 else
6931 {
6932 s = reg_getline(clnum) + reg_mmatch->startpos[no].col;
6933 if (reg_mmatch->endpos[no].lnum == clnum)
6934 len = reg_mmatch->endpos[no].col
6935 - reg_mmatch->startpos[no].col;
6936 else
6937 len = (int)STRLEN(s);
6938 }
6939 }
6940 else
6941 {
6942 s = reg_match->startp[no];
6943 if (reg_match->endp[no] == NULL)
6944 s = NULL;
6945 else
6946 len = (int)(reg_match->endp[no] - s);
6947 }
6948 if (s != NULL)
6949 {
6950 for (;;)
6951 {
6952 if (len == 0)
6953 {
6954 if (REG_MULTI)
6955 {
6956 if (reg_mmatch->endpos[no].lnum == clnum)
6957 break;
6958 if (copy)
6959 *dst = CAR;
6960 ++dst;
6961 s = reg_getline(++clnum);
6962 if (reg_mmatch->endpos[no].lnum == clnum)
6963 len = reg_mmatch->endpos[no].col;
6964 else
6965 len = (int)STRLEN(s);
6966 }
6967 else
6968 break;
6969 }
6970 else if (*s == NUL) /* we hit NUL. */
6971 {
6972 if (copy)
6973 EMSG(_(e_re_damg));
6974 goto exit;
6975 }
6976 else
6977 {
6978 if (backslash && (*s == CAR || *s == '\\'))
6979 {
6980 /*
6981 * Insert a backslash in front of a CR, otherwise
6982 * it will be replaced by a line break.
6983 * Number of backslashes will be halved later,
6984 * double them here.
6985 */
6986 if (copy)
6987 {
6988 dst[0] = '\\';
6989 dst[1] = *s;
6990 }
6991 dst += 2;
6992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 else
6994 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00006995#ifdef FEAT_MBYTE
6996 if (has_mbyte)
6997 c = mb_ptr2char(s);
6998 else
6999#endif
7000 c = *s;
7001
7002 if (func == (fptr_T)NULL) /* just copy */
7003 cc = c;
7004 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 /* Turbo C complains without the typecast */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007006 func = (fptr_T)(func(&cc, c));
7007
7008#ifdef FEAT_MBYTE
7009 if (has_mbyte)
7010 {
7011 int l = mb_ptr2len(s) - 1;
7012
7013 s += l;
7014 len -= l;
7015 if (copy)
7016 mb_char2bytes(cc, dst);
7017 dst += mb_char2len(cc) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007019 else
7020#endif
7021 if (copy)
7022 *dst = cc;
7023 dst++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007025
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 ++s;
7027 --len;
7028 }
7029 }
7030 }
7031 no = -1;
7032 }
7033 }
7034 if (copy)
7035 *dst = NUL;
7036
7037exit:
7038 return (int)((dst - dest) + 1);
7039}
7040
7041#ifdef FEAT_EVAL
7042/*
7043 * Used for the submatch() function: get the string from tne n'th submatch in
7044 * allocated memory.
7045 * Returns NULL when not in a ":s" command and for a non-existing submatch.
7046 */
7047 char_u *
7048reg_submatch(no)
7049 int no;
7050{
7051 char_u *retval = NULL;
7052 char_u *s;
7053 int len;
7054 int round;
7055 linenr_T lnum;
7056
7057 if (!can_f_submatch)
7058 return NULL;
7059
7060 if (submatch_match == NULL)
7061 {
7062 /*
7063 * First round: compute the length and allocate memory.
7064 * Second round: copy the text.
7065 */
7066 for (round = 1; round <= 2; ++round)
7067 {
7068 lnum = submatch_mmatch->startpos[no].lnum;
7069 if (lnum < 0 || submatch_mmatch->endpos[no].lnum < 0)
7070 return NULL;
7071
7072 s = reg_getline(lnum) + submatch_mmatch->startpos[no].col;
7073 if (s == NULL) /* anti-crash check, cannot happen? */
7074 break;
7075 if (submatch_mmatch->endpos[no].lnum == lnum)
7076 {
7077 /* Within one line: take form start to end col. */
7078 len = submatch_mmatch->endpos[no].col
7079 - submatch_mmatch->startpos[no].col;
7080 if (round == 2)
Bram Moolenaarbbebc852005-07-18 21:47:53 +00007081 vim_strncpy(retval, s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 ++len;
7083 }
7084 else
7085 {
7086 /* Multiple lines: take start line from start col, middle
7087 * lines completely and end line up to end col. */
7088 len = (int)STRLEN(s);
7089 if (round == 2)
7090 {
7091 STRCPY(retval, s);
7092 retval[len] = '\n';
7093 }
7094 ++len;
7095 ++lnum;
7096 while (lnum < submatch_mmatch->endpos[no].lnum)
7097 {
7098 s = reg_getline(lnum++);
7099 if (round == 2)
7100 STRCPY(retval + len, s);
7101 len += (int)STRLEN(s);
7102 if (round == 2)
7103 retval[len] = '\n';
7104 ++len;
7105 }
7106 if (round == 2)
7107 STRNCPY(retval + len, reg_getline(lnum),
7108 submatch_mmatch->endpos[no].col);
7109 len += submatch_mmatch->endpos[no].col;
7110 if (round == 2)
7111 retval[len] = NUL;
7112 ++len;
7113 }
7114
7115 if (round == 1)
7116 {
7117 retval = lalloc((long_u)len, TRUE);
7118 if (s == NULL)
7119 return NULL;
7120 }
7121 }
7122 }
7123 else
7124 {
7125 if (submatch_match->endp[no] == NULL)
7126 retval = NULL;
7127 else
7128 {
7129 s = submatch_match->startp[no];
7130 retval = vim_strnsave(s, (int)(submatch_match->endp[no] - s));
7131 }
7132 }
7133
7134 return retval;
7135}
7136#endif