blob: 6a62b281a1f6150486b36277955b45bfbf431a2f [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 Moolenaar45eeb132005-06-06 21:59:07 +0000328#define EMSG_RET_NULL(m) return (EMSG(m), rc_did_emsg = TRUE, NULL)
329#define EMSG_M_RET_NULL(m, c) return (EMSG2((m), (c) ? "" : "\\"), rc_did_emsg = TRUE, NULL)
330#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));
668static int prog_magic_wrong __ARGS((void));
669static char_u *regnext __ARGS((char_u *));
670static void regc __ARGS((int b));
671#ifdef FEAT_MBYTE
672static void regmbc __ARGS((int c));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000673#else
674# define regmbc(c) regc(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675#endif
676static void reginsert __ARGS((int, char_u *));
677static void reginsert_limits __ARGS((int, long, long, char_u *));
678static char_u *re_put_long __ARGS((char_u *pr, long_u val));
679static int read_limits __ARGS((long *, long *));
680static void regtail __ARGS((char_u *, char_u *));
681static void regoptail __ARGS((char_u *, char_u *));
682
683/*
684 * Return TRUE if compiled regular expression "prog" can match a line break.
685 */
686 int
687re_multiline(prog)
688 regprog_T *prog;
689{
690 return (prog->regflags & RF_HASNL);
691}
692
693/*
694 * Return TRUE if compiled regular expression "prog" looks before the start
695 * position (pattern contains "\@<=" or "\@<!").
696 */
697 int
698re_lookbehind(prog)
699 regprog_T *prog;
700{
701 return (prog->regflags & RF_LOOKBH);
702}
703
704/*
Bram Moolenaardf177f62005-02-22 08:39:57 +0000705 * Check for an equivalence class name "[=a=]". "pp" points to the '['.
706 * Returns a character representing the class. Zero means that no item was
707 * recognized. Otherwise "pp" is advanced to after the item.
708 */
709 static int
710get_equi_class(pp)
711 char_u **pp;
712{
713 int c;
714 int l = 1;
715 char_u *p = *pp;
716
717 if (p[1] == '=')
718 {
719#ifdef FEAT_MBYTE
720 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000721 l = (*mb_ptr2len)(p + 2);
Bram Moolenaardf177f62005-02-22 08:39:57 +0000722#endif
723 if (p[l + 2] == '=' && p[l + 3] == ']')
724 {
725#ifdef FEAT_MBYTE
726 if (has_mbyte)
727 c = mb_ptr2char(p + 2);
728 else
729#endif
730 c = p[2];
731 *pp += l + 4;
732 return c;
733 }
734 }
735 return 0;
736}
737
738/*
739 * Produce the bytes for equivalence class "c".
740 * Currently only handles latin1, latin9 and utf-8.
741 */
742 static void
743reg_equi_class(c)
744 int c;
745{
746#ifdef FEAT_MBYTE
747 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
Bram Moolenaar78622822005-08-23 21:00:13 +0000748 || STRCMP(p_enc, "iso-8859-15") == 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000749#endif
750 {
751 switch (c)
752 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000753 case 'A': case '\300': case '\301': case '\302':
754 case '\303': case '\304': case '\305':
755 regmbc('A'); regmbc('\300'); regmbc('\301');
756 regmbc('\302'); regmbc('\303'); regmbc('\304');
757 regmbc('\305');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000758 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000759 case 'C': case '\307':
760 regmbc('C'); regmbc('\307');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000761 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000762 case 'E': case '\310': case '\311': case '\312': case '\313':
763 regmbc('E'); regmbc('\310'); regmbc('\311');
764 regmbc('\312'); regmbc('\313');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000765 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000766 case 'I': case '\314': case '\315': case '\316': case '\317':
767 regmbc('I'); regmbc('\314'); regmbc('\315');
768 regmbc('\316'); regmbc('\317');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000769 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000770 case 'N': case '\321':
771 regmbc('N'); regmbc('\321');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000772 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000773 case 'O': case '\322': case '\323': case '\324': case '\325':
774 case '\326':
775 regmbc('O'); regmbc('\322'); regmbc('\323');
776 regmbc('\324'); regmbc('\325'); regmbc('\326');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000777 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000778 case 'U': case '\331': case '\332': case '\333': case '\334':
779 regmbc('U'); regmbc('\331'); regmbc('\332');
780 regmbc('\333'); regmbc('\334');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000781 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000782 case 'Y': case '\335':
783 regmbc('Y'); regmbc('\335');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000784 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000785 case 'a': case '\340': case '\341': case '\342':
786 case '\343': case '\344': case '\345':
787 regmbc('a'); regmbc('\340'); regmbc('\341');
788 regmbc('\342'); regmbc('\343'); regmbc('\344');
789 regmbc('\345');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000790 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000791 case 'c': case '\347':
792 regmbc('c'); regmbc('\347');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000793 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000794 case 'e': case '\350': case '\351': case '\352': case '\353':
795 regmbc('e'); regmbc('\350'); regmbc('\351');
796 regmbc('\352'); regmbc('\353');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000797 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000798 case 'i': case '\354': case '\355': case '\356': case '\357':
799 regmbc('i'); regmbc('\354'); regmbc('\355');
800 regmbc('\356'); regmbc('\357');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000801 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000802 case 'n': case '\361':
803 regmbc('n'); regmbc('\361');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000804 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000805 case 'o': case '\362': case '\363': case '\364': case '\365':
806 case '\366':
807 regmbc('o'); regmbc('\362'); regmbc('\363');
808 regmbc('\364'); regmbc('\365'); regmbc('\366');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000809 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000810 case 'u': case '\371': case '\372': case '\373': case '\374':
811 regmbc('u'); regmbc('\371'); regmbc('\372');
812 regmbc('\373'); regmbc('\374');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000813 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000814 case 'y': case '\375': case '\377':
815 regmbc('y'); regmbc('\375'); regmbc('\377');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000816 return;
817 }
818 }
819 regmbc(c);
820}
821
822/*
823 * Check for a collating element "[.a.]". "pp" points to the '['.
824 * Returns a character. Zero means that no item was recognized. Otherwise
825 * "pp" is advanced to after the item.
826 * Currently only single characters are recognized!
827 */
828 static int
829get_coll_element(pp)
830 char_u **pp;
831{
832 int c;
833 int l = 1;
834 char_u *p = *pp;
835
836 if (p[1] == '.')
837 {
838#ifdef FEAT_MBYTE
839 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000840 l = (*mb_ptr2len)(p + 2);
Bram Moolenaardf177f62005-02-22 08:39:57 +0000841#endif
842 if (p[l + 2] == '.' && p[l + 3] == ']')
843 {
844#ifdef FEAT_MBYTE
845 if (has_mbyte)
846 c = mb_ptr2char(p + 2);
847 else
848#endif
849 c = p[2];
850 *pp += l + 4;
851 return c;
852 }
853 }
854 return 0;
855}
856
857
858/*
859 * Skip over a "[]" range.
860 * "p" must point to the character after the '['.
861 * The returned pointer is on the matching ']', or the terminating NUL.
862 */
863 static char_u *
864skip_anyof(p)
865 char_u *p;
866{
867 int cpo_lit; /* 'cpoptions' contains 'l' flag */
868 int cpo_bsl; /* 'cpoptions' contains '\' flag */
869#ifdef FEAT_MBYTE
870 int l;
871#endif
872
Bram Moolenaar3b56eb32005-07-11 22:40:32 +0000873 cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL;
874 cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000875
876 if (*p == '^') /* Complement of range. */
877 ++p;
878 if (*p == ']' || *p == '-')
879 ++p;
880 while (*p != NUL && *p != ']')
881 {
882#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000883 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000884 p += l;
885 else
886#endif
887 if (*p == '-')
888 {
889 ++p;
890 if (*p != ']' && *p != NUL)
891 mb_ptr_adv(p);
892 }
893 else if (*p == '\\'
894 && !cpo_bsl
895 && (vim_strchr(REGEXP_INRANGE, p[1]) != NULL
896 || (!cpo_lit && vim_strchr(REGEXP_ABBR, p[1]) != NULL)))
897 p += 2;
898 else if (*p == '[')
899 {
900 if (get_char_class(&p) == CLASS_NONE
901 && get_equi_class(&p) == 0
902 && get_coll_element(&p) == 0)
903 ++p; /* It was not a class name */
904 }
905 else
906 ++p;
907 }
908
909 return p;
910}
911
912/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 * Skip past regular expression.
Bram Moolenaar748bf032005-02-02 23:04:36 +0000914 * Stop at end of "startp" or where "dirc" is found ('/', '?', etc).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 * Take care of characters with a backslash in front of it.
916 * Skip strings inside [ and ].
917 * When "newp" is not NULL and "dirc" is '?', make an allocated copy of the
918 * expression and change "\?" to "?". If "*newp" is not NULL the expression
919 * is changed in-place.
920 */
921 char_u *
922skip_regexp(startp, dirc, magic, newp)
923 char_u *startp;
924 int dirc;
925 int magic;
926 char_u **newp;
927{
928 int mymagic;
929 char_u *p = startp;
930
931 if (magic)
932 mymagic = MAGIC_ON;
933 else
934 mymagic = MAGIC_OFF;
935
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000936 for (; p[0] != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 {
938 if (p[0] == dirc) /* found end of regexp */
939 break;
940 if ((p[0] == '[' && mymagic >= MAGIC_ON)
941 || (p[0] == '\\' && p[1] == '[' && mymagic <= MAGIC_OFF))
942 {
943 p = skip_anyof(p + 1);
944 if (p[0] == NUL)
945 break;
946 }
947 else if (p[0] == '\\' && p[1] != NUL)
948 {
949 if (dirc == '?' && newp != NULL && p[1] == '?')
950 {
951 /* change "\?" to "?", make a copy first. */
952 if (*newp == NULL)
953 {
954 *newp = vim_strsave(startp);
955 if (*newp != NULL)
956 p = *newp + (p - startp);
957 }
958 if (*newp != NULL)
959 mch_memmove(p, p + 1, STRLEN(p));
960 else
961 ++p;
962 }
963 else
964 ++p; /* skip next character */
965 if (*p == 'v')
966 mymagic = MAGIC_ALL;
967 else if (*p == 'V')
968 mymagic = MAGIC_NONE;
969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 }
971 return p;
972}
973
974/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000975 * vim_regcomp() - compile a regular expression into internal code
976 * Returns the program in allocated space. Returns NULL for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 *
978 * We can't allocate space until we know how big the compiled form will be,
979 * but we can't compile it (and thus know how big it is) until we've got a
980 * place to put the code. So we cheat: we compile it twice, once with code
981 * generation turned off and size counting turned on, and once "for real".
982 * This also means that we don't allocate space until we are sure that the
983 * thing really will compile successfully, and we never have to move the
984 * code and thus invalidate pointers into it. (Note that it has to be in
985 * one piece because vim_free() must be able to free it all.)
986 *
987 * Whether upper/lower case is to be ignored is decided when executing the
988 * program, it does not matter here.
989 *
990 * Beware that the optimization-preparation code in here knows about some
991 * of the structure of the compiled regexp.
992 * "re_flags": RE_MAGIC and/or RE_STRING.
993 */
994 regprog_T *
995vim_regcomp(expr, re_flags)
996 char_u *expr;
997 int re_flags;
998{
999 regprog_T *r;
1000 char_u *scan;
1001 char_u *longest;
1002 int len;
1003 int flags;
1004
1005 if (expr == NULL)
1006 EMSG_RET_NULL(_(e_null));
1007
1008 init_class_tab();
1009
1010 /*
1011 * First pass: determine size, legality.
1012 */
1013 regcomp_start(expr, re_flags);
1014 regcode = JUST_CALC_SIZE;
1015 regc(REGMAGIC);
1016 if (reg(REG_NOPAREN, &flags) == NULL)
1017 return NULL;
1018
1019 /* Small enough for pointer-storage convention? */
1020#ifdef SMALL_MALLOC /* 16 bit storage allocation */
1021 if (regsize >= 65536L - 256L)
1022 EMSG_RET_NULL(_("E339: Pattern too long"));
1023#endif
1024
1025 /* Allocate space. */
1026 r = (regprog_T *)lalloc(sizeof(regprog_T) + regsize, TRUE);
1027 if (r == NULL)
1028 return NULL;
1029
1030 /*
1031 * Second pass: emit code.
1032 */
1033 regcomp_start(expr, re_flags);
1034 regcode = r->program;
1035 regc(REGMAGIC);
1036 if (reg(REG_NOPAREN, &flags) == NULL)
1037 {
1038 vim_free(r);
1039 return NULL;
1040 }
1041
1042 /* Dig out information for optimizations. */
1043 r->regstart = NUL; /* Worst-case defaults. */
1044 r->reganch = 0;
1045 r->regmust = NULL;
1046 r->regmlen = 0;
1047 r->regflags = regflags;
1048 if (flags & HASNL)
1049 r->regflags |= RF_HASNL;
1050 if (flags & HASLOOKBH)
1051 r->regflags |= RF_LOOKBH;
1052#ifdef FEAT_SYN_HL
1053 /* Remember whether this pattern has any \z specials in it. */
1054 r->reghasz = re_has_z;
1055#endif
1056 scan = r->program + 1; /* First BRANCH. */
1057 if (OP(regnext(scan)) == END) /* Only one top-level choice. */
1058 {
1059 scan = OPERAND(scan);
1060
1061 /* Starting-point info. */
1062 if (OP(scan) == BOL || OP(scan) == RE_BOF)
1063 {
1064 r->reganch++;
1065 scan = regnext(scan);
1066 }
1067
1068 if (OP(scan) == EXACTLY)
1069 {
1070#ifdef FEAT_MBYTE
1071 if (has_mbyte)
1072 r->regstart = (*mb_ptr2char)(OPERAND(scan));
1073 else
1074#endif
1075 r->regstart = *OPERAND(scan);
1076 }
1077 else if ((OP(scan) == BOW
1078 || OP(scan) == EOW
1079 || OP(scan) == NOTHING
1080 || OP(scan) == MOPEN + 0 || OP(scan) == NOPEN
1081 || OP(scan) == MCLOSE + 0 || OP(scan) == NCLOSE)
1082 && OP(regnext(scan)) == EXACTLY)
1083 {
1084#ifdef FEAT_MBYTE
1085 if (has_mbyte)
1086 r->regstart = (*mb_ptr2char)(OPERAND(regnext(scan)));
1087 else
1088#endif
1089 r->regstart = *OPERAND(regnext(scan));
1090 }
1091
1092 /*
1093 * If there's something expensive in the r.e., find the longest
1094 * literal string that must appear and make it the regmust. Resolve
1095 * ties in favor of later strings, since the regstart check works
1096 * with the beginning of the r.e. and avoiding duplication
1097 * strengthens checking. Not a strong reason, but sufficient in the
1098 * absence of others.
1099 */
1100 /*
1101 * When the r.e. starts with BOW, it is faster to look for a regmust
1102 * first. Used a lot for "#" and "*" commands. (Added by mool).
1103 */
1104 if ((flags & SPSTART || OP(scan) == BOW || OP(scan) == EOW)
1105 && !(flags & HASNL))
1106 {
1107 longest = NULL;
1108 len = 0;
1109 for (; scan != NULL; scan = regnext(scan))
1110 if (OP(scan) == EXACTLY && STRLEN(OPERAND(scan)) >= (size_t)len)
1111 {
1112 longest = OPERAND(scan);
1113 len = (int)STRLEN(OPERAND(scan));
1114 }
1115 r->regmust = longest;
1116 r->regmlen = len;
1117 }
1118 }
1119#ifdef DEBUG
1120 regdump(expr, r);
1121#endif
1122 return r;
1123}
1124
1125/*
1126 * Setup to parse the regexp. Used once to get the length and once to do it.
1127 */
1128 static void
1129regcomp_start(expr, re_flags)
1130 char_u *expr;
1131 int re_flags; /* see vim_regcomp() */
1132{
1133 initchr(expr);
1134 if (re_flags & RE_MAGIC)
1135 reg_magic = MAGIC_ON;
1136 else
1137 reg_magic = MAGIC_OFF;
1138 reg_string = (re_flags & RE_STRING);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001139 reg_strict = (re_flags & RE_STRICT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140
1141 num_complex_braces = 0;
1142 regnpar = 1;
1143 vim_memset(had_endbrace, 0, sizeof(had_endbrace));
1144#ifdef FEAT_SYN_HL
1145 regnzpar = 1;
1146 re_has_z = 0;
1147#endif
1148 regsize = 0L;
1149 regflags = 0;
1150#if defined(FEAT_SYN_HL) || defined(PROTO)
1151 had_eol = FALSE;
1152#endif
1153}
1154
1155#if defined(FEAT_SYN_HL) || defined(PROTO)
1156/*
1157 * Check if during the previous call to vim_regcomp the EOL item "$" has been
1158 * found. This is messy, but it works fine.
1159 */
1160 int
1161vim_regcomp_had_eol()
1162{
1163 return had_eol;
1164}
1165#endif
1166
1167/*
1168 * reg - regular expression, i.e. main body or parenthesized thing
1169 *
1170 * Caller must absorb opening parenthesis.
1171 *
1172 * Combining parenthesis handling with the base level of regular expression
1173 * is a trifle forced, but the need to tie the tails of the branches to what
1174 * follows makes it hard to avoid.
1175 */
1176 static char_u *
1177reg(paren, flagp)
1178 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1179 int *flagp;
1180{
1181 char_u *ret;
1182 char_u *br;
1183 char_u *ender;
1184 int parno = 0;
1185 int flags;
1186
1187 *flagp = HASWIDTH; /* Tentatively. */
1188
1189#ifdef FEAT_SYN_HL
1190 if (paren == REG_ZPAREN)
1191 {
1192 /* Make a ZOPEN node. */
1193 if (regnzpar >= NSUBEXP)
1194 EMSG_RET_NULL(_("E50: Too many \\z("));
1195 parno = regnzpar;
1196 regnzpar++;
1197 ret = regnode(ZOPEN + parno);
1198 }
1199 else
1200#endif
1201 if (paren == REG_PAREN)
1202 {
1203 /* Make a MOPEN node. */
1204 if (regnpar >= NSUBEXP)
1205 EMSG_M_RET_NULL(_("E51: Too many %s("), reg_magic == MAGIC_ALL);
1206 parno = regnpar;
1207 ++regnpar;
1208 ret = regnode(MOPEN + parno);
1209 }
1210 else if (paren == REG_NPAREN)
1211 {
1212 /* Make a NOPEN node. */
1213 ret = regnode(NOPEN);
1214 }
1215 else
1216 ret = NULL;
1217
1218 /* Pick up the branches, linking them together. */
1219 br = regbranch(&flags);
1220 if (br == NULL)
1221 return NULL;
1222 if (ret != NULL)
1223 regtail(ret, br); /* [MZ]OPEN -> first. */
1224 else
1225 ret = br;
1226 /* If one of the branches can be zero-width, the whole thing can.
1227 * If one of the branches has * at start or matches a line-break, the
1228 * whole thing can. */
1229 if (!(flags & HASWIDTH))
1230 *flagp &= ~HASWIDTH;
1231 *flagp |= flags & (SPSTART | HASNL | HASLOOKBH);
1232 while (peekchr() == Magic('|'))
1233 {
1234 skipchr();
1235 br = regbranch(&flags);
1236 if (br == NULL)
1237 return NULL;
1238 regtail(ret, br); /* BRANCH -> BRANCH. */
1239 if (!(flags & HASWIDTH))
1240 *flagp &= ~HASWIDTH;
1241 *flagp |= flags & (SPSTART | HASNL | HASLOOKBH);
1242 }
1243
1244 /* Make a closing node, and hook it on the end. */
1245 ender = regnode(
1246#ifdef FEAT_SYN_HL
1247 paren == REG_ZPAREN ? ZCLOSE + parno :
1248#endif
1249 paren == REG_PAREN ? MCLOSE + parno :
1250 paren == REG_NPAREN ? NCLOSE : END);
1251 regtail(ret, ender);
1252
1253 /* Hook the tails of the branches to the closing node. */
1254 for (br = ret; br != NULL; br = regnext(br))
1255 regoptail(br, ender);
1256
1257 /* Check for proper termination. */
1258 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1259 {
1260#ifdef FEAT_SYN_HL
1261 if (paren == REG_ZPAREN)
Bram Moolenaar45eeb132005-06-06 21:59:07 +00001262 EMSG_RET_NULL(_("E52: Unmatched \\z("));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 else
1264#endif
1265 if (paren == REG_NPAREN)
Bram Moolenaar45eeb132005-06-06 21:59:07 +00001266 EMSG_M_RET_NULL(_("E53: Unmatched %s%%("), reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 else
Bram Moolenaar45eeb132005-06-06 21:59:07 +00001268 EMSG_M_RET_NULL(_("E54: Unmatched %s("), reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 }
1270 else if (paren == REG_NOPAREN && peekchr() != NUL)
1271 {
1272 if (curchr == Magic(')'))
Bram Moolenaar45eeb132005-06-06 21:59:07 +00001273 EMSG_M_RET_NULL(_("E55: Unmatched %s)"), reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 else
Bram Moolenaar45eeb132005-06-06 21:59:07 +00001275 EMSG_RET_NULL(_(e_trailing)); /* "Can't happen". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 /* NOTREACHED */
1277 }
1278 /*
1279 * Here we set the flag allowing back references to this set of
1280 * parentheses.
1281 */
1282 if (paren == REG_PAREN)
1283 had_endbrace[parno] = TRUE; /* have seen the close paren */
1284 return ret;
1285}
1286
1287/*
1288 * regbranch - one alternative of an | operator
1289 *
1290 * Implements the & operator.
1291 */
1292 static char_u *
1293regbranch(flagp)
1294 int *flagp;
1295{
1296 char_u *ret;
1297 char_u *chain = NULL;
1298 char_u *latest;
1299 int flags;
1300
1301 *flagp = WORST | HASNL; /* Tentatively. */
1302
1303 ret = regnode(BRANCH);
1304 for (;;)
1305 {
1306 latest = regconcat(&flags);
1307 if (latest == NULL)
1308 return NULL;
1309 /* If one of the branches has width, the whole thing has. If one of
1310 * the branches anchors at start-of-line, the whole thing does.
1311 * If one of the branches uses look-behind, the whole thing does. */
1312 *flagp |= flags & (HASWIDTH | SPSTART | HASLOOKBH);
1313 /* If one of the branches doesn't match a line-break, the whole thing
1314 * doesn't. */
1315 *flagp &= ~HASNL | (flags & HASNL);
1316 if (chain != NULL)
1317 regtail(chain, latest);
1318 if (peekchr() != Magic('&'))
1319 break;
1320 skipchr();
1321 regtail(latest, regnode(END)); /* operand ends */
1322 reginsert(MATCH, latest);
1323 chain = latest;
1324 }
1325
1326 return ret;
1327}
1328
1329/*
1330 * regbranch - one alternative of an | or & operator
1331 *
1332 * Implements the concatenation operator.
1333 */
1334 static char_u *
1335regconcat(flagp)
1336 int *flagp;
1337{
1338 char_u *first = NULL;
1339 char_u *chain = NULL;
1340 char_u *latest;
1341 int flags;
1342 int cont = TRUE;
1343
1344 *flagp = WORST; /* Tentatively. */
1345
1346 while (cont)
1347 {
1348 switch (peekchr())
1349 {
1350 case NUL:
1351 case Magic('|'):
1352 case Magic('&'):
1353 case Magic(')'):
1354 cont = FALSE;
1355 break;
1356 case Magic('Z'):
1357#ifdef FEAT_MBYTE
1358 regflags |= RF_ICOMBINE;
1359#endif
1360 skipchr_keepstart();
1361 break;
1362 case Magic('c'):
1363 regflags |= RF_ICASE;
1364 skipchr_keepstart();
1365 break;
1366 case Magic('C'):
1367 regflags |= RF_NOICASE;
1368 skipchr_keepstart();
1369 break;
1370 case Magic('v'):
1371 reg_magic = MAGIC_ALL;
1372 skipchr_keepstart();
1373 curchr = -1;
1374 break;
1375 case Magic('m'):
1376 reg_magic = MAGIC_ON;
1377 skipchr_keepstart();
1378 curchr = -1;
1379 break;
1380 case Magic('M'):
1381 reg_magic = MAGIC_OFF;
1382 skipchr_keepstart();
1383 curchr = -1;
1384 break;
1385 case Magic('V'):
1386 reg_magic = MAGIC_NONE;
1387 skipchr_keepstart();
1388 curchr = -1;
1389 break;
1390 default:
1391 latest = regpiece(&flags);
1392 if (latest == NULL)
1393 return NULL;
1394 *flagp |= flags & (HASWIDTH | HASNL | HASLOOKBH);
1395 if (chain == NULL) /* First piece. */
1396 *flagp |= flags & SPSTART;
1397 else
1398 regtail(chain, latest);
1399 chain = latest;
1400 if (first == NULL)
1401 first = latest;
1402 break;
1403 }
1404 }
1405 if (first == NULL) /* Loop ran zero times. */
1406 first = regnode(NOTHING);
1407 return first;
1408}
1409
1410/*
1411 * regpiece - something followed by possible [*+=]
1412 *
1413 * Note that the branching code sequences used for = and the general cases
1414 * of * and + are somewhat optimized: they use the same NOTHING node as
1415 * both the endmarker for their branch list and the body of the last branch.
1416 * It might seem that this node could be dispensed with entirely, but the
1417 * endmarker role is not redundant.
1418 */
1419 static char_u *
1420regpiece(flagp)
1421 int *flagp;
1422{
1423 char_u *ret;
1424 int op;
1425 char_u *next;
1426 int flags;
1427 long minval;
1428 long maxval;
1429
1430 ret = regatom(&flags);
1431 if (ret == NULL)
1432 return NULL;
1433
1434 op = peekchr();
1435 if (re_multi_type(op) == NOT_MULTI)
1436 {
1437 *flagp = flags;
1438 return ret;
1439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 /* default flags */
1441 *flagp = (WORST | SPSTART | (flags & (HASNL | HASLOOKBH)));
1442
1443 skipchr();
1444 switch (op)
1445 {
1446 case Magic('*'):
1447 if (flags & SIMPLE)
1448 reginsert(STAR, ret);
1449 else
1450 {
1451 /* Emit x* as (x&|), where & means "self". */
1452 reginsert(BRANCH, ret); /* Either x */
1453 regoptail(ret, regnode(BACK)); /* and loop */
1454 regoptail(ret, ret); /* back */
1455 regtail(ret, regnode(BRANCH)); /* or */
1456 regtail(ret, regnode(NOTHING)); /* null. */
1457 }
1458 break;
1459
1460 case Magic('+'):
1461 if (flags & SIMPLE)
1462 reginsert(PLUS, ret);
1463 else
1464 {
1465 /* Emit x+ as x(&|), where & means "self". */
1466 next = regnode(BRANCH); /* Either */
1467 regtail(ret, next);
Bram Moolenaar582fd852005-03-28 20:58:01 +00001468 regtail(regnode(BACK), ret); /* loop back */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 regtail(next, regnode(BRANCH)); /* or */
1470 regtail(ret, regnode(NOTHING)); /* null. */
1471 }
1472 *flagp = (WORST | HASWIDTH | (flags & (HASNL | HASLOOKBH)));
1473 break;
1474
1475 case Magic('@'):
1476 {
1477 int lop = END;
1478
1479 switch (no_Magic(getchr()))
1480 {
1481 case '=': lop = MATCH; break; /* \@= */
1482 case '!': lop = NOMATCH; break; /* \@! */
1483 case '>': lop = SUBPAT; break; /* \@> */
1484 case '<': switch (no_Magic(getchr()))
1485 {
1486 case '=': lop = BEHIND; break; /* \@<= */
1487 case '!': lop = NOBEHIND; break; /* \@<! */
1488 }
1489 }
1490 if (lop == END)
1491 EMSG_M_RET_NULL(_("E59: invalid character after %s@"),
1492 reg_magic == MAGIC_ALL);
1493 /* Look behind must match with behind_pos. */
1494 if (lop == BEHIND || lop == NOBEHIND)
1495 {
1496 regtail(ret, regnode(BHPOS));
1497 *flagp |= HASLOOKBH;
1498 }
1499 regtail(ret, regnode(END)); /* operand ends */
1500 reginsert(lop, ret);
1501 break;
1502 }
1503
1504 case Magic('?'):
1505 case Magic('='):
1506 /* Emit x= as (x|) */
1507 reginsert(BRANCH, ret); /* Either x */
1508 regtail(ret, regnode(BRANCH)); /* or */
1509 next = regnode(NOTHING); /* null. */
1510 regtail(ret, next);
1511 regoptail(ret, next);
1512 break;
1513
1514 case Magic('{'):
1515 if (!read_limits(&minval, &maxval))
1516 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 if (flags & SIMPLE)
1518 {
1519 reginsert(BRACE_SIMPLE, ret);
1520 reginsert_limits(BRACE_LIMITS, minval, maxval, ret);
1521 }
1522 else
1523 {
1524 if (num_complex_braces >= 10)
1525 EMSG_M_RET_NULL(_("E60: Too many complex %s{...}s"),
1526 reg_magic == MAGIC_ALL);
1527 reginsert(BRACE_COMPLEX + num_complex_braces, ret);
1528 regoptail(ret, regnode(BACK));
1529 regoptail(ret, ret);
1530 reginsert_limits(BRACE_LIMITS, minval, maxval, ret);
1531 ++num_complex_braces;
1532 }
1533 if (minval > 0 && maxval > 0)
1534 *flagp = (HASWIDTH | (flags & (HASNL | HASLOOKBH)));
1535 break;
1536 }
1537 if (re_multi_type(peekchr()) != NOT_MULTI)
1538 {
1539 /* Can't have a multi follow a multi. */
1540 if (peekchr() == Magic('*'))
1541 sprintf((char *)IObuff, _("E61: Nested %s*"),
1542 reg_magic >= MAGIC_ON ? "" : "\\");
1543 else
1544 sprintf((char *)IObuff, _("E62: Nested %s%c"),
1545 reg_magic == MAGIC_ALL ? "" : "\\", no_Magic(peekchr()));
1546 EMSG_RET_NULL(IObuff);
1547 }
1548
1549 return ret;
1550}
1551
1552/*
1553 * regatom - the lowest level
1554 *
1555 * Optimization: gobbles an entire sequence of ordinary characters so that
1556 * it can turn them into a single node, which is smaller to store and
1557 * faster to run. Don't do this when one_exactly is set.
1558 */
1559 static char_u *
1560regatom(flagp)
1561 int *flagp;
1562{
1563 char_u *ret;
1564 int flags;
1565 int cpo_lit; /* 'cpoptions' contains 'l' flag */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001566 int cpo_bsl; /* 'cpoptions' contains '\' flag */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 int c;
1568 static char_u *classchars = (char_u *)".iIkKfFpPsSdDxXoOwWhHaAlLuU";
1569 static int classcodes[] = {ANY, IDENT, SIDENT, KWORD, SKWORD,
1570 FNAME, SFNAME, PRINT, SPRINT,
1571 WHITE, NWHITE, DIGIT, NDIGIT,
1572 HEX, NHEX, OCTAL, NOCTAL,
1573 WORD, NWORD, HEAD, NHEAD,
1574 ALPHA, NALPHA, LOWER, NLOWER,
1575 UPPER, NUPPER
1576 };
1577 char_u *p;
1578 int extra = 0;
1579
1580 *flagp = WORST; /* Tentatively. */
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00001581 cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL;
1582 cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583
1584 c = getchr();
1585 switch (c)
1586 {
1587 case Magic('^'):
1588 ret = regnode(BOL);
1589 break;
1590
1591 case Magic('$'):
1592 ret = regnode(EOL);
1593#if defined(FEAT_SYN_HL) || defined(PROTO)
1594 had_eol = TRUE;
1595#endif
1596 break;
1597
1598 case Magic('<'):
1599 ret = regnode(BOW);
1600 break;
1601
1602 case Magic('>'):
1603 ret = regnode(EOW);
1604 break;
1605
1606 case Magic('_'):
1607 c = no_Magic(getchr());
1608 if (c == '^') /* "\_^" is start-of-line */
1609 {
1610 ret = regnode(BOL);
1611 break;
1612 }
1613 if (c == '$') /* "\_$" is end-of-line */
1614 {
1615 ret = regnode(EOL);
1616#if defined(FEAT_SYN_HL) || defined(PROTO)
1617 had_eol = TRUE;
1618#endif
1619 break;
1620 }
1621
1622 extra = ADD_NL;
1623 *flagp |= HASNL;
1624
1625 /* "\_[" is character range plus newline */
1626 if (c == '[')
1627 goto collection;
1628
1629 /* "\_x" is character class plus newline */
1630 /*FALLTHROUGH*/
1631
1632 /*
1633 * Character classes.
1634 */
1635 case Magic('.'):
1636 case Magic('i'):
1637 case Magic('I'):
1638 case Magic('k'):
1639 case Magic('K'):
1640 case Magic('f'):
1641 case Magic('F'):
1642 case Magic('p'):
1643 case Magic('P'):
1644 case Magic('s'):
1645 case Magic('S'):
1646 case Magic('d'):
1647 case Magic('D'):
1648 case Magic('x'):
1649 case Magic('X'):
1650 case Magic('o'):
1651 case Magic('O'):
1652 case Magic('w'):
1653 case Magic('W'):
1654 case Magic('h'):
1655 case Magic('H'):
1656 case Magic('a'):
1657 case Magic('A'):
1658 case Magic('l'):
1659 case Magic('L'):
1660 case Magic('u'):
1661 case Magic('U'):
1662 p = vim_strchr(classchars, no_Magic(c));
1663 if (p == NULL)
1664 EMSG_RET_NULL(_("E63: invalid use of \\_"));
1665 ret = regnode(classcodes[p - classchars] + extra);
1666 *flagp |= HASWIDTH | SIMPLE;
1667 break;
1668
1669 case Magic('n'):
1670 if (reg_string)
1671 {
1672 /* In a string "\n" matches a newline character. */
1673 ret = regnode(EXACTLY);
1674 regc(NL);
1675 regc(NUL);
1676 *flagp |= HASWIDTH | SIMPLE;
1677 }
1678 else
1679 {
1680 /* In buffer text "\n" matches the end of a line. */
1681 ret = regnode(NEWL);
1682 *flagp |= HASWIDTH | HASNL;
1683 }
1684 break;
1685
1686 case Magic('('):
1687 if (one_exactly)
1688 EMSG_ONE_RET_NULL;
1689 ret = reg(REG_PAREN, &flags);
1690 if (ret == NULL)
1691 return NULL;
1692 *flagp |= flags & (HASWIDTH | SPSTART | HASNL | HASLOOKBH);
1693 break;
1694
1695 case NUL:
1696 case Magic('|'):
1697 case Magic('&'):
1698 case Magic(')'):
1699 EMSG_RET_NULL(_(e_internal)); /* Supposed to be caught earlier. */
1700 /* NOTREACHED */
1701
1702 case Magic('='):
1703 case Magic('?'):
1704 case Magic('+'):
1705 case Magic('@'):
1706 case Magic('{'):
1707 case Magic('*'):
1708 c = no_Magic(c);
1709 sprintf((char *)IObuff, _("E64: %s%c follows nothing"),
1710 (c == '*' ? reg_magic >= MAGIC_ON : reg_magic == MAGIC_ALL)
1711 ? "" : "\\", c);
1712 EMSG_RET_NULL(IObuff);
1713 /* NOTREACHED */
1714
1715 case Magic('~'): /* previous substitute pattern */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001716 if (reg_prev_sub != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 {
1718 char_u *lp;
1719
1720 ret = regnode(EXACTLY);
1721 lp = reg_prev_sub;
1722 while (*lp != NUL)
1723 regc(*lp++);
1724 regc(NUL);
1725 if (*reg_prev_sub != NUL)
1726 {
1727 *flagp |= HASWIDTH;
1728 if ((lp - reg_prev_sub) == 1)
1729 *flagp |= SIMPLE;
1730 }
1731 }
1732 else
1733 EMSG_RET_NULL(_(e_nopresub));
1734 break;
1735
1736 case Magic('1'):
1737 case Magic('2'):
1738 case Magic('3'):
1739 case Magic('4'):
1740 case Magic('5'):
1741 case Magic('6'):
1742 case Magic('7'):
1743 case Magic('8'):
1744 case Magic('9'):
1745 {
1746 int refnum;
1747
1748 refnum = c - Magic('0');
1749 /*
1750 * Check if the back reference is legal. We must have seen the
1751 * close brace.
1752 * TODO: Should also check that we don't refer to something
1753 * that is repeated (+*=): what instance of the repetition
1754 * should we match?
1755 */
1756 if (!had_endbrace[refnum])
1757 {
1758 /* Trick: check if "@<=" or "@<!" follows, in which case
1759 * the \1 can appear before the referenced match. */
1760 for (p = regparse; *p != NUL; ++p)
1761 if (p[0] == '@' && p[1] == '<'
1762 && (p[2] == '!' || p[2] == '='))
1763 break;
1764 if (*p == NUL)
1765 EMSG_RET_NULL(_("E65: Illegal back reference"));
1766 }
1767 ret = regnode(BACKREF + refnum);
1768 }
1769 break;
1770
1771#ifdef FEAT_SYN_HL
1772 case Magic('z'):
1773 {
1774 c = no_Magic(getchr());
1775 switch (c)
1776 {
1777 case '(': if (reg_do_extmatch != REX_SET)
1778 EMSG_RET_NULL(_("E66: \\z( not allowed here"));
1779 if (one_exactly)
1780 EMSG_ONE_RET_NULL;
1781 ret = reg(REG_ZPAREN, &flags);
1782 if (ret == NULL)
1783 return NULL;
1784 *flagp |= flags & (HASWIDTH|SPSTART|HASNL|HASLOOKBH);
1785 re_has_z = REX_SET;
1786 break;
1787
1788 case '1':
1789 case '2':
1790 case '3':
1791 case '4':
1792 case '5':
1793 case '6':
1794 case '7':
1795 case '8':
1796 case '9': if (reg_do_extmatch != REX_USE)
1797 EMSG_RET_NULL(_("E67: \\z1 et al. not allowed here"));
1798 ret = regnode(ZREF + c - '0');
1799 re_has_z = REX_USE;
1800 break;
1801
1802 case 's': ret = regnode(MOPEN + 0);
1803 break;
1804
1805 case 'e': ret = regnode(MCLOSE + 0);
1806 break;
1807
1808 default: EMSG_RET_NULL(_("E68: Invalid character after \\z"));
1809 }
1810 }
1811 break;
1812#endif
1813
1814 case Magic('%'):
1815 {
1816 c = no_Magic(getchr());
1817 switch (c)
1818 {
1819 /* () without a back reference */
1820 case '(':
1821 if (one_exactly)
1822 EMSG_ONE_RET_NULL;
1823 ret = reg(REG_NPAREN, &flags);
1824 if (ret == NULL)
1825 return NULL;
1826 *flagp |= flags & (HASWIDTH | SPSTART | HASNL | HASLOOKBH);
1827 break;
1828
1829 /* Catch \%^ and \%$ regardless of where they appear in the
1830 * pattern -- regardless of whether or not it makes sense. */
1831 case '^':
1832 ret = regnode(RE_BOF);
1833 break;
1834
1835 case '$':
1836 ret = regnode(RE_EOF);
1837 break;
1838
1839 case '#':
1840 ret = regnode(CURSOR);
1841 break;
1842
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001843 case 'V':
1844 ret = regnode(RE_VISUAL);
1845 break;
1846
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 /* \%[abc]: Emit as a list of branches, all ending at the last
1848 * branch which matches nothing. */
1849 case '[':
1850 if (one_exactly) /* doesn't nest */
1851 EMSG_ONE_RET_NULL;
1852 {
1853 char_u *lastbranch;
1854 char_u *lastnode = NULL;
1855 char_u *br;
1856
1857 ret = NULL;
1858 while ((c = getchr()) != ']')
1859 {
1860 if (c == NUL)
1861 EMSG_M_RET_NULL(_("E69: Missing ] after %s%%["),
1862 reg_magic == MAGIC_ALL);
1863 br = regnode(BRANCH);
1864 if (ret == NULL)
1865 ret = br;
1866 else
1867 regtail(lastnode, br);
1868
1869 ungetchr();
1870 one_exactly = TRUE;
1871 lastnode = regatom(flagp);
1872 one_exactly = FALSE;
1873 if (lastnode == NULL)
1874 return NULL;
1875 }
1876 if (ret == NULL)
1877 EMSG_M_RET_NULL(_("E70: Empty %s%%[]"),
1878 reg_magic == MAGIC_ALL);
1879 lastbranch = regnode(BRANCH);
1880 br = regnode(NOTHING);
1881 if (ret != JUST_CALC_SIZE)
1882 {
1883 regtail(lastnode, br);
1884 regtail(lastbranch, br);
1885 /* connect all branches to the NOTHING
1886 * branch at the end */
1887 for (br = ret; br != lastnode; )
1888 {
1889 if (OP(br) == BRANCH)
1890 {
1891 regtail(br, lastbranch);
1892 br = OPERAND(br);
1893 }
1894 else
1895 br = regnext(br);
1896 }
1897 }
1898 *flagp &= ~HASWIDTH;
1899 break;
1900 }
1901
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001902 case 'd': /* %d123 decimal */
1903 case 'o': /* %o123 octal */
1904 case 'x': /* %xab hex 2 */
1905 case 'u': /* %uabcd hex 4 */
1906 case 'U': /* %U1234abcd hex 8 */
1907 {
1908 int i;
1909
1910 switch (c)
1911 {
1912 case 'd': i = getdecchrs(); break;
1913 case 'o': i = getoctchrs(); break;
1914 case 'x': i = gethexchrs(2); break;
1915 case 'u': i = gethexchrs(4); break;
1916 case 'U': i = gethexchrs(8); break;
1917 default: i = -1; break;
1918 }
1919
1920 if (i < 0)
1921 EMSG_M_RET_NULL(
1922 _("E678: Invalid character after %s%%[dxouU]"),
1923 reg_magic == MAGIC_ALL);
1924 ret = regnode(EXACTLY);
1925 if (i == 0)
1926 regc(0x0a);
1927 else
1928#ifdef FEAT_MBYTE
1929 regmbc(i);
1930#else
1931 regc(i);
1932#endif
1933 regc(NUL);
1934 *flagp |= HASWIDTH;
1935 break;
1936 }
1937
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 default:
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001939 if (VIM_ISDIGIT(c) || c == '<' || c == '>'
1940 || c == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 {
1942 long_u n = 0;
1943 int cmp;
1944
1945 cmp = c;
1946 if (cmp == '<' || cmp == '>')
1947 c = getchr();
1948 while (VIM_ISDIGIT(c))
1949 {
1950 n = n * 10 + (c - '0');
1951 c = getchr();
1952 }
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00001953 if (c == '\'' && n == 0)
1954 {
1955 /* "\%'m", "\%<'m" and "\%>'m": Mark */
1956 c = getchr();
1957 ret = regnode(RE_MARK);
1958 if (ret == JUST_CALC_SIZE)
1959 regsize += 2;
1960 else
1961 {
1962 *regcode++ = c;
1963 *regcode++ = cmp;
1964 }
1965 break;
1966 }
1967 else if (c == 'l' || c == 'c' || c == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 {
1969 if (c == 'l')
1970 ret = regnode(RE_LNUM);
1971 else if (c == 'c')
1972 ret = regnode(RE_COL);
1973 else
1974 ret = regnode(RE_VCOL);
1975 if (ret == JUST_CALC_SIZE)
1976 regsize += 5;
1977 else
1978 {
1979 /* put the number and the optional
1980 * comparator after the opcode */
1981 regcode = re_put_long(regcode, n);
1982 *regcode++ = cmp;
1983 }
1984 break;
1985 }
1986 }
1987
1988 EMSG_M_RET_NULL(_("E71: Invalid character after %s%%"),
1989 reg_magic == MAGIC_ALL);
1990 }
1991 }
1992 break;
1993
1994 case Magic('['):
1995collection:
1996 {
1997 char_u *lp;
1998
1999 /*
2000 * If there is no matching ']', we assume the '[' is a normal
2001 * character. This makes 'incsearch' and ":help [" work.
2002 */
2003 lp = skip_anyof(regparse);
2004 if (*lp == ']') /* there is a matching ']' */
2005 {
2006 int startc = -1; /* > 0 when next '-' is a range */
2007 int endc;
2008
2009 /*
2010 * In a character class, different parsing rules apply.
2011 * Not even \ is special anymore, nothing is.
2012 */
2013 if (*regparse == '^') /* Complement of range. */
2014 {
2015 ret = regnode(ANYBUT + extra);
2016 regparse++;
2017 }
2018 else
2019 ret = regnode(ANYOF + extra);
2020
2021 /* At the start ']' and '-' mean the literal character. */
2022 if (*regparse == ']' || *regparse == '-')
Bram Moolenaardf177f62005-02-22 08:39:57 +00002023 {
2024 startc = *regparse;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 regc(*regparse++);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027
2028 while (*regparse != NUL && *regparse != ']')
2029 {
2030 if (*regparse == '-')
2031 {
2032 ++regparse;
2033 /* The '-' is not used for a range at the end and
2034 * after or before a '\n'. */
2035 if (*regparse == ']' || *regparse == NUL
2036 || startc == -1
2037 || (regparse[0] == '\\' && regparse[1] == 'n'))
2038 {
2039 regc('-');
2040 startc = '-'; /* [--x] is a range */
2041 }
2042 else
2043 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00002044 /* Also accept "a-[.z.]" */
2045 endc = 0;
2046 if (*regparse == '[')
2047 endc = get_coll_element(&regparse);
2048 if (endc == 0)
2049 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050#ifdef FEAT_MBYTE
Bram Moolenaardf177f62005-02-22 08:39:57 +00002051 if (has_mbyte)
2052 endc = mb_ptr2char_adv(&regparse);
2053 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00002055 endc = *regparse++;
2056 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002057
2058 /* Handle \o40, \x20 and \u20AC style sequences */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002059 if (endc == '\\' && !cpo_lit && !cpo_bsl)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002060 endc = coll_get_char();
2061
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 if (startc > endc)
2063 EMSG_RET_NULL(_(e_invrange));
2064#ifdef FEAT_MBYTE
2065 if (has_mbyte && ((*mb_char2len)(startc) > 1
2066 || (*mb_char2len)(endc) > 1))
2067 {
2068 /* Limit to a range of 256 chars */
2069 if (endc > startc + 256)
2070 EMSG_RET_NULL(_(e_invrange));
2071 while (++startc <= endc)
2072 regmbc(startc);
2073 }
2074 else
2075#endif
2076 {
2077#ifdef EBCDIC
2078 int alpha_only = FALSE;
2079
2080 /* for alphabetical range skip the gaps
2081 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
2082 if (isalpha(startc) && isalpha(endc))
2083 alpha_only = TRUE;
2084#endif
2085 while (++startc <= endc)
2086#ifdef EBCDIC
2087 if (!alpha_only || isalpha(startc))
2088#endif
2089 regc(startc);
2090 }
2091 startc = -1;
2092 }
2093 }
2094 /*
2095 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
2096 * accepts "\t", "\e", etc., but only when the 'l' flag in
2097 * 'cpoptions' is not included.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002098 * Posix doesn't recognize backslash at all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 */
2100 else if (*regparse == '\\'
Bram Moolenaardf177f62005-02-22 08:39:57 +00002101 && !cpo_bsl
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
2103 || (!cpo_lit
2104 && vim_strchr(REGEXP_ABBR,
2105 regparse[1]) != NULL)))
2106 {
2107 regparse++;
2108 if (*regparse == 'n')
2109 {
2110 /* '\n' in range: also match NL */
2111 if (ret != JUST_CALC_SIZE)
2112 {
2113 if (*ret == ANYBUT)
2114 *ret = ANYBUT + ADD_NL;
2115 else if (*ret == ANYOF)
2116 *ret = ANYOF + ADD_NL;
2117 /* else: must have had a \n already */
2118 }
2119 *flagp |= HASNL;
2120 regparse++;
2121 startc = -1;
2122 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002123 else if (*regparse == 'd'
2124 || *regparse == 'o'
2125 || *regparse == 'x'
2126 || *regparse == 'u'
2127 || *regparse == 'U')
2128 {
2129 startc = coll_get_char();
2130 if (startc == 0)
2131 regc(0x0a);
2132 else
2133#ifdef FEAT_MBYTE
2134 regmbc(startc);
2135#else
2136 regc(startc);
2137#endif
2138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 else
2140 {
2141 startc = backslash_trans(*regparse++);
2142 regc(startc);
2143 }
2144 }
2145 else if (*regparse == '[')
2146 {
2147 int c_class;
2148 int cu;
2149
Bram Moolenaardf177f62005-02-22 08:39:57 +00002150 c_class = get_char_class(&regparse);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 startc = -1;
2152 /* Characters assumed to be 8 bits! */
2153 switch (c_class)
2154 {
2155 case CLASS_NONE:
Bram Moolenaardf177f62005-02-22 08:39:57 +00002156 c_class = get_equi_class(&regparse);
2157 if (c_class != 0)
2158 {
2159 /* produce equivalence class */
2160 reg_equi_class(c_class);
2161 }
2162 else if ((c_class =
2163 get_coll_element(&regparse)) != 0)
2164 {
2165 /* produce a collating element */
2166 regmbc(c_class);
2167 }
2168 else
2169 {
2170 /* literal '[', allow [[-x] as a range */
2171 startc = *regparse++;
2172 regc(startc);
2173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 break;
2175 case CLASS_ALNUM:
2176 for (cu = 1; cu <= 255; cu++)
2177 if (isalnum(cu))
2178 regc(cu);
2179 break;
2180 case CLASS_ALPHA:
2181 for (cu = 1; cu <= 255; cu++)
2182 if (isalpha(cu))
2183 regc(cu);
2184 break;
2185 case CLASS_BLANK:
2186 regc(' ');
2187 regc('\t');
2188 break;
2189 case CLASS_CNTRL:
2190 for (cu = 1; cu <= 255; cu++)
2191 if (iscntrl(cu))
2192 regc(cu);
2193 break;
2194 case CLASS_DIGIT:
2195 for (cu = 1; cu <= 255; cu++)
2196 if (VIM_ISDIGIT(cu))
2197 regc(cu);
2198 break;
2199 case CLASS_GRAPH:
2200 for (cu = 1; cu <= 255; cu++)
2201 if (isgraph(cu))
2202 regc(cu);
2203 break;
2204 case CLASS_LOWER:
2205 for (cu = 1; cu <= 255; cu++)
2206 if (islower(cu))
2207 regc(cu);
2208 break;
2209 case CLASS_PRINT:
2210 for (cu = 1; cu <= 255; cu++)
2211 if (vim_isprintc(cu))
2212 regc(cu);
2213 break;
2214 case CLASS_PUNCT:
2215 for (cu = 1; cu <= 255; cu++)
2216 if (ispunct(cu))
2217 regc(cu);
2218 break;
2219 case CLASS_SPACE:
2220 for (cu = 9; cu <= 13; cu++)
2221 regc(cu);
2222 regc(' ');
2223 break;
2224 case CLASS_UPPER:
2225 for (cu = 1; cu <= 255; cu++)
2226 if (isupper(cu))
2227 regc(cu);
2228 break;
2229 case CLASS_XDIGIT:
2230 for (cu = 1; cu <= 255; cu++)
2231 if (vim_isxdigit(cu))
2232 regc(cu);
2233 break;
2234 case CLASS_TAB:
2235 regc('\t');
2236 break;
2237 case CLASS_RETURN:
2238 regc('\r');
2239 break;
2240 case CLASS_BACKSPACE:
2241 regc('\b');
2242 break;
2243 case CLASS_ESCAPE:
2244 regc('\033');
2245 break;
2246 }
2247 }
2248 else
2249 {
2250#ifdef FEAT_MBYTE
2251 if (has_mbyte)
2252 {
2253 int len;
2254
2255 /* produce a multibyte character, including any
2256 * following composing characters */
2257 startc = mb_ptr2char(regparse);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002258 len = (*mb_ptr2len)(regparse);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 if (enc_utf8 && utf_char2len(startc) != len)
2260 startc = -1; /* composing chars */
2261 while (--len >= 0)
2262 regc(*regparse++);
2263 }
2264 else
2265#endif
2266 {
2267 startc = *regparse++;
2268 regc(startc);
2269 }
2270 }
2271 }
2272 regc(NUL);
2273 prevchr_len = 1; /* last char was the ']' */
2274 if (*regparse != ']')
2275 EMSG_RET_NULL(_(e_toomsbra)); /* Cannot happen? */
2276 skipchr(); /* let's be friends with the lexer again */
2277 *flagp |= HASWIDTH | SIMPLE;
2278 break;
2279 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002280 else if (reg_strict)
2281 EMSG_M_RET_NULL(_("E769: Missing ] after %s["),
2282 reg_magic > MAGIC_OFF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 }
2284 /* FALLTHROUGH */
2285
2286 default:
2287 {
2288 int len;
2289
2290#ifdef FEAT_MBYTE
2291 /* A multi-byte character is handled as a separate atom if it's
2292 * before a multi. */
2293 if (has_mbyte && (*mb_char2len)(c) > 1
2294 && re_multi_type(peekchr()) != NOT_MULTI)
2295 {
2296 ret = regnode(MULTIBYTECODE);
2297 regmbc(c);
2298 *flagp |= HASWIDTH | SIMPLE;
2299 break;
2300 }
2301#endif
2302
2303 ret = regnode(EXACTLY);
2304
2305 /*
2306 * Append characters as long as:
2307 * - there is no following multi, we then need the character in
2308 * front of it as a single character operand
2309 * - not running into a Magic character
2310 * - "one_exactly" is not set
2311 * But always emit at least one character. Might be a Multi,
2312 * e.g., a "[" without matching "]".
2313 */
2314 for (len = 0; c != NUL && (len == 0
2315 || (re_multi_type(peekchr()) == NOT_MULTI
2316 && !one_exactly
2317 && !is_Magic(c))); ++len)
2318 {
2319 c = no_Magic(c);
2320#ifdef FEAT_MBYTE
2321 if (has_mbyte)
2322 {
2323 regmbc(c);
2324 if (enc_utf8)
2325 {
2326 int off;
2327 int l;
2328
2329 /* Need to get composing character too, directly
2330 * access regparse for that, because skipchr() skips
2331 * over composing chars. */
2332 ungetchr();
2333 if (*regparse == '\\' && regparse[1] != NUL)
2334 off = 1;
2335 else
2336 off = 0;
2337 for (;;)
2338 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002339 l = utf_ptr2len(regparse + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 if (!UTF_COMPOSINGLIKE(regparse + off,
2341 regparse + off + l))
2342 break;
2343 off += l;
2344 regmbc(utf_ptr2char(regparse + off));
2345 }
2346 skipchr();
2347 }
2348 }
2349 else
2350#endif
2351 regc(c);
2352 c = getchr();
2353 }
2354 ungetchr();
2355
2356 regc(NUL);
2357 *flagp |= HASWIDTH;
2358 if (len == 1)
2359 *flagp |= SIMPLE;
2360 }
2361 break;
2362 }
2363
2364 return ret;
2365}
2366
2367/*
2368 * emit a node
2369 * Return pointer to generated code.
2370 */
2371 static char_u *
2372regnode(op)
2373 int op;
2374{
2375 char_u *ret;
2376
2377 ret = regcode;
2378 if (ret == JUST_CALC_SIZE)
2379 regsize += 3;
2380 else
2381 {
2382 *regcode++ = op;
2383 *regcode++ = NUL; /* Null "next" pointer. */
2384 *regcode++ = NUL;
2385 }
2386 return ret;
2387}
2388
2389/*
2390 * Emit (if appropriate) a byte of code
2391 */
2392 static void
2393regc(b)
2394 int b;
2395{
2396 if (regcode == JUST_CALC_SIZE)
2397 regsize++;
2398 else
2399 *regcode++ = b;
2400}
2401
2402#ifdef FEAT_MBYTE
2403/*
2404 * Emit (if appropriate) a multi-byte character of code
2405 */
2406 static void
2407regmbc(c)
2408 int c;
2409{
2410 if (regcode == JUST_CALC_SIZE)
2411 regsize += (*mb_char2len)(c);
2412 else
2413 regcode += (*mb_char2bytes)(c, regcode);
2414}
2415#endif
2416
2417/*
2418 * reginsert - insert an operator in front of already-emitted operand
2419 *
2420 * Means relocating the operand.
2421 */
2422 static void
2423reginsert(op, opnd)
2424 int op;
2425 char_u *opnd;
2426{
2427 char_u *src;
2428 char_u *dst;
2429 char_u *place;
2430
2431 if (regcode == JUST_CALC_SIZE)
2432 {
2433 regsize += 3;
2434 return;
2435 }
2436 src = regcode;
2437 regcode += 3;
2438 dst = regcode;
2439 while (src > opnd)
2440 *--dst = *--src;
2441
2442 place = opnd; /* Op node, where operand used to be. */
2443 *place++ = op;
2444 *place++ = NUL;
2445 *place = NUL;
2446}
2447
2448/*
2449 * reginsert_limits - insert an operator in front of already-emitted operand.
2450 * The operator has the given limit values as operands. Also set next pointer.
2451 *
2452 * Means relocating the operand.
2453 */
2454 static void
2455reginsert_limits(op, minval, maxval, opnd)
2456 int op;
2457 long minval;
2458 long maxval;
2459 char_u *opnd;
2460{
2461 char_u *src;
2462 char_u *dst;
2463 char_u *place;
2464
2465 if (regcode == JUST_CALC_SIZE)
2466 {
2467 regsize += 11;
2468 return;
2469 }
2470 src = regcode;
2471 regcode += 11;
2472 dst = regcode;
2473 while (src > opnd)
2474 *--dst = *--src;
2475
2476 place = opnd; /* Op node, where operand used to be. */
2477 *place++ = op;
2478 *place++ = NUL;
2479 *place++ = NUL;
2480 place = re_put_long(place, (long_u)minval);
2481 place = re_put_long(place, (long_u)maxval);
2482 regtail(opnd, place);
2483}
2484
2485/*
2486 * Write a long as four bytes at "p" and return pointer to the next char.
2487 */
2488 static char_u *
2489re_put_long(p, val)
2490 char_u *p;
2491 long_u val;
2492{
2493 *p++ = (char_u) ((val >> 24) & 0377);
2494 *p++ = (char_u) ((val >> 16) & 0377);
2495 *p++ = (char_u) ((val >> 8) & 0377);
2496 *p++ = (char_u) (val & 0377);
2497 return p;
2498}
2499
2500/*
2501 * regtail - set the next-pointer at the end of a node chain
2502 */
2503 static void
2504regtail(p, val)
2505 char_u *p;
2506 char_u *val;
2507{
2508 char_u *scan;
2509 char_u *temp;
2510 int offset;
2511
2512 if (p == JUST_CALC_SIZE)
2513 return;
2514
2515 /* Find last node. */
2516 scan = p;
2517 for (;;)
2518 {
2519 temp = regnext(scan);
2520 if (temp == NULL)
2521 break;
2522 scan = temp;
2523 }
2524
Bram Moolenaar582fd852005-03-28 20:58:01 +00002525 if (OP(scan) == BACK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 offset = (int)(scan - val);
2527 else
2528 offset = (int)(val - scan);
2529 *(scan + 1) = (char_u) (((unsigned)offset >> 8) & 0377);
2530 *(scan + 2) = (char_u) (offset & 0377);
2531}
2532
2533/*
2534 * regoptail - regtail on item after a BRANCH; nop if none
2535 */
2536 static void
2537regoptail(p, val)
2538 char_u *p;
2539 char_u *val;
2540{
2541 /* When op is neither BRANCH nor BRACE_COMPLEX0-9, it is "operandless" */
2542 if (p == NULL || p == JUST_CALC_SIZE
2543 || (OP(p) != BRANCH
2544 && (OP(p) < BRACE_COMPLEX || OP(p) > BRACE_COMPLEX + 9)))
2545 return;
2546 regtail(OPERAND(p), val);
2547}
2548
2549/*
2550 * getchr() - get the next character from the pattern. We know about
2551 * magic and such, so therefore we need a lexical analyzer.
2552 */
2553
2554/* static int curchr; */
2555static int prevprevchr;
2556static int prevchr;
2557static int nextchr; /* used for ungetchr() */
2558/*
2559 * Note: prevchr is sometimes -1 when we are not at the start,
2560 * eg in /[ ^I]^ the pattern was never found even if it existed, because ^ was
2561 * taken to be magic -- webb
2562 */
2563static int at_start; /* True when on the first character */
2564static int prev_at_start; /* True when on the second character */
2565
2566 static void
2567initchr(str)
2568 char_u *str;
2569{
2570 regparse = str;
2571 prevchr_len = 0;
2572 curchr = prevprevchr = prevchr = nextchr = -1;
2573 at_start = TRUE;
2574 prev_at_start = FALSE;
2575}
2576
2577 static int
2578peekchr()
2579{
Bram Moolenaardf177f62005-02-22 08:39:57 +00002580 static int after_slash = FALSE;
2581
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 if (curchr == -1)
2583 {
2584 switch (curchr = regparse[0])
2585 {
2586 case '.':
2587 case '[':
2588 case '~':
2589 /* magic when 'magic' is on */
2590 if (reg_magic >= MAGIC_ON)
2591 curchr = Magic(curchr);
2592 break;
2593 case '(':
2594 case ')':
2595 case '{':
2596 case '%':
2597 case '+':
2598 case '=':
2599 case '?':
2600 case '@':
2601 case '!':
2602 case '&':
2603 case '|':
2604 case '<':
2605 case '>':
2606 case '#': /* future ext. */
2607 case '"': /* future ext. */
2608 case '\'': /* future ext. */
2609 case ',': /* future ext. */
2610 case '-': /* future ext. */
2611 case ':': /* future ext. */
2612 case ';': /* future ext. */
2613 case '`': /* future ext. */
2614 case '/': /* Can't be used in / command */
2615 /* magic only after "\v" */
2616 if (reg_magic == MAGIC_ALL)
2617 curchr = Magic(curchr);
2618 break;
2619 case '*':
Bram Moolenaardf177f62005-02-22 08:39:57 +00002620 /* * is not magic as the very first character, eg "?*ptr", when
2621 * after '^', eg "/^*ptr" and when after "\(", "\|", "\&". But
2622 * "\(\*" is not magic, thus must be magic if "after_slash" */
2623 if (reg_magic >= MAGIC_ON
2624 && !at_start
2625 && !(prev_at_start && prevchr == Magic('^'))
2626 && (after_slash
2627 || (prevchr != Magic('(')
2628 && prevchr != Magic('&')
2629 && prevchr != Magic('|'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 curchr = Magic('*');
2631 break;
2632 case '^':
2633 /* '^' is only magic as the very first character and if it's after
2634 * "\(", "\|", "\&' or "\n" */
2635 if (reg_magic >= MAGIC_OFF
2636 && (at_start
2637 || reg_magic == MAGIC_ALL
2638 || prevchr == Magic('(')
2639 || prevchr == Magic('|')
2640 || prevchr == Magic('&')
2641 || prevchr == Magic('n')
2642 || (no_Magic(prevchr) == '('
2643 && prevprevchr == Magic('%'))))
2644 {
2645 curchr = Magic('^');
2646 at_start = TRUE;
2647 prev_at_start = FALSE;
2648 }
2649 break;
2650 case '$':
2651 /* '$' is only magic as the very last char and if it's in front of
2652 * either "\|", "\)", "\&", or "\n" */
2653 if (reg_magic >= MAGIC_OFF)
2654 {
2655 char_u *p = regparse + 1;
2656
2657 /* ignore \c \C \m and \M after '$' */
2658 while (p[0] == '\\' && (p[1] == 'c' || p[1] == 'C'
2659 || p[1] == 'm' || p[1] == 'M' || p[1] == 'Z'))
2660 p += 2;
2661 if (p[0] == NUL
2662 || (p[0] == '\\'
2663 && (p[1] == '|' || p[1] == '&' || p[1] == ')'
2664 || p[1] == 'n'))
2665 || reg_magic == MAGIC_ALL)
2666 curchr = Magic('$');
2667 }
2668 break;
2669 case '\\':
2670 {
2671 int c = regparse[1];
2672
2673 if (c == NUL)
2674 curchr = '\\'; /* trailing '\' */
2675 else if (
2676#ifdef EBCDIC
2677 vim_strchr(META, c)
2678#else
2679 c <= '~' && META_flags[c]
2680#endif
2681 )
2682 {
2683 /*
2684 * META contains everything that may be magic sometimes,
2685 * except ^ and $ ("\^" and "\$" are only magic after
2686 * "\v"). We now fetch the next character and toggle its
2687 * magicness. Therefore, \ is so meta-magic that it is
2688 * not in META.
2689 */
2690 curchr = -1;
2691 prev_at_start = at_start;
2692 at_start = FALSE; /* be able to say "/\*ptr" */
2693 ++regparse;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002694 ++after_slash;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 peekchr();
2696 --regparse;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002697 --after_slash;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 curchr = toggle_Magic(curchr);
2699 }
2700 else if (vim_strchr(REGEXP_ABBR, c))
2701 {
2702 /*
2703 * Handle abbreviations, like "\t" for TAB -- webb
2704 */
2705 curchr = backslash_trans(c);
2706 }
2707 else if (reg_magic == MAGIC_NONE && (c == '$' || c == '^'))
2708 curchr = toggle_Magic(c);
2709 else
2710 {
2711 /*
2712 * Next character can never be (made) magic?
2713 * Then backslashing it won't do anything.
2714 */
2715#ifdef FEAT_MBYTE
2716 if (has_mbyte)
2717 curchr = (*mb_ptr2char)(regparse + 1);
2718 else
2719#endif
2720 curchr = c;
2721 }
2722 break;
2723 }
2724
2725#ifdef FEAT_MBYTE
2726 default:
2727 if (has_mbyte)
2728 curchr = (*mb_ptr2char)(regparse);
2729#endif
2730 }
2731 }
2732
2733 return curchr;
2734}
2735
2736/*
2737 * Eat one lexed character. Do this in a way that we can undo it.
2738 */
2739 static void
2740skipchr()
2741{
2742 /* peekchr() eats a backslash, do the same here */
2743 if (*regparse == '\\')
2744 prevchr_len = 1;
2745 else
2746 prevchr_len = 0;
2747 if (regparse[prevchr_len] != NUL)
2748 {
2749#ifdef FEAT_MBYTE
2750 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002751 prevchr_len += (*mb_ptr2len)(regparse + prevchr_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 else
2753#endif
2754 ++prevchr_len;
2755 }
2756 regparse += prevchr_len;
2757 prev_at_start = at_start;
2758 at_start = FALSE;
2759 prevprevchr = prevchr;
2760 prevchr = curchr;
2761 curchr = nextchr; /* use previously unget char, or -1 */
2762 nextchr = -1;
2763}
2764
2765/*
2766 * Skip a character while keeping the value of prev_at_start for at_start.
2767 * prevchr and prevprevchr are also kept.
2768 */
2769 static void
2770skipchr_keepstart()
2771{
2772 int as = prev_at_start;
2773 int pr = prevchr;
2774 int prpr = prevprevchr;
2775
2776 skipchr();
2777 at_start = as;
2778 prevchr = pr;
2779 prevprevchr = prpr;
2780}
2781
2782 static int
2783getchr()
2784{
2785 int chr = peekchr();
2786
2787 skipchr();
2788 return chr;
2789}
2790
2791/*
2792 * put character back. Works only once!
2793 */
2794 static void
2795ungetchr()
2796{
2797 nextchr = curchr;
2798 curchr = prevchr;
2799 prevchr = prevprevchr;
2800 at_start = prev_at_start;
2801 prev_at_start = FALSE;
2802
2803 /* Backup regparse, so that it's at the same position as before the
2804 * getchr(). */
2805 regparse -= prevchr_len;
2806}
2807
2808/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002809 * Get and return the value of the hex string at the current position.
2810 * Return -1 if there is no valid hex number.
2811 * The position is updated:
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002812 * blahblah\%x20asdf
2813 * before-^ ^-after
2814 * The parameter controls the maximum number of input characters. This will be
2815 * 2 when reading a \%x20 sequence and 4 when reading a \%u20AC sequence.
2816 */
2817 static int
2818gethexchrs(maxinputlen)
2819 int maxinputlen;
2820{
2821 int nr = 0;
2822 int c;
2823 int i;
2824
2825 for (i = 0; i < maxinputlen; ++i)
2826 {
2827 c = regparse[0];
2828 if (!vim_isxdigit(c))
2829 break;
2830 nr <<= 4;
2831 nr |= hex2nr(c);
2832 ++regparse;
2833 }
2834
2835 if (i == 0)
2836 return -1;
2837 return nr;
2838}
2839
2840/*
2841 * get and return the value of the decimal string immediately after the
2842 * current position. Return -1 for invalid. Consumes all digits.
2843 */
2844 static int
2845getdecchrs()
2846{
2847 int nr = 0;
2848 int c;
2849 int i;
2850
2851 for (i = 0; ; ++i)
2852 {
2853 c = regparse[0];
2854 if (c < '0' || c > '9')
2855 break;
2856 nr *= 10;
2857 nr += c - '0';
2858 ++regparse;
2859 }
2860
2861 if (i == 0)
2862 return -1;
2863 return nr;
2864}
2865
2866/*
2867 * get and return the value of the octal string immediately after the current
2868 * position. Return -1 for invalid, or 0-255 for valid. Smart enough to handle
2869 * numbers > 377 correctly (for example, 400 is treated as 40) and doesn't
2870 * treat 8 or 9 as recognised characters. Position is updated:
2871 * blahblah\%o210asdf
2872 * before-^ ^-after
2873 */
2874 static int
2875getoctchrs()
2876{
2877 int nr = 0;
2878 int c;
2879 int i;
2880
2881 for (i = 0; i < 3 && nr < 040; ++i)
2882 {
2883 c = regparse[0];
2884 if (c < '0' || c > '7')
2885 break;
2886 nr <<= 3;
2887 nr |= hex2nr(c);
2888 ++regparse;
2889 }
2890
2891 if (i == 0)
2892 return -1;
2893 return nr;
2894}
2895
2896/*
2897 * Get a number after a backslash that is inside [].
2898 * When nothing is recognized return a backslash.
2899 */
2900 static int
2901coll_get_char()
2902{
2903 int nr = -1;
2904
2905 switch (*regparse++)
2906 {
2907 case 'd': nr = getdecchrs(); break;
2908 case 'o': nr = getoctchrs(); break;
2909 case 'x': nr = gethexchrs(2); break;
2910 case 'u': nr = gethexchrs(4); break;
2911 case 'U': nr = gethexchrs(8); break;
2912 }
2913 if (nr < 0)
2914 {
2915 /* If getting the number fails be backwards compatible: the character
2916 * is a backslash. */
2917 --regparse;
2918 nr = '\\';
2919 }
2920 return nr;
2921}
2922
2923/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 * read_limits - Read two integers to be taken as a minimum and maximum.
2925 * If the first character is '-', then the range is reversed.
2926 * Should end with 'end'. If minval is missing, zero is default, if maxval is
2927 * missing, a very big number is the default.
2928 */
2929 static int
2930read_limits(minval, maxval)
2931 long *minval;
2932 long *maxval;
2933{
2934 int reverse = FALSE;
2935 char_u *first_char;
2936 long tmp;
2937
2938 if (*regparse == '-')
2939 {
2940 /* Starts with '-', so reverse the range later */
2941 regparse++;
2942 reverse = TRUE;
2943 }
2944 first_char = regparse;
2945 *minval = getdigits(&regparse);
2946 if (*regparse == ',') /* There is a comma */
2947 {
2948 if (vim_isdigit(*++regparse))
2949 *maxval = getdigits(&regparse);
2950 else
2951 *maxval = MAX_LIMIT;
2952 }
2953 else if (VIM_ISDIGIT(*first_char))
2954 *maxval = *minval; /* It was \{n} or \{-n} */
2955 else
2956 *maxval = MAX_LIMIT; /* It was \{} or \{-} */
2957 if (*regparse == '\\')
2958 regparse++; /* Allow either \{...} or \{...\} */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002959 if (*regparse != '}')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 {
2961 sprintf((char *)IObuff, _("E554: Syntax error in %s{...}"),
2962 reg_magic == MAGIC_ALL ? "" : "\\");
2963 EMSG_RET_FAIL(IObuff);
2964 }
2965
2966 /*
2967 * Reverse the range if there was a '-', or make sure it is in the right
2968 * order otherwise.
2969 */
2970 if ((!reverse && *minval > *maxval) || (reverse && *minval < *maxval))
2971 {
2972 tmp = *minval;
2973 *minval = *maxval;
2974 *maxval = tmp;
2975 }
2976 skipchr(); /* let's be friends with the lexer again */
2977 return OK;
2978}
2979
2980/*
2981 * vim_regexec and friends
2982 */
2983
2984/*
2985 * Global work variables for vim_regexec().
2986 */
2987
2988/* The current match-position is remembered with these variables: */
2989static linenr_T reglnum; /* line number, relative to first line */
2990static char_u *regline; /* start of current line */
2991static char_u *reginput; /* current input, points into "regline" */
2992
2993static int need_clear_subexpr; /* subexpressions still need to be
2994 * cleared */
2995#ifdef FEAT_SYN_HL
2996static int need_clear_zsubexpr = FALSE; /* extmatch subexpressions
2997 * still need to be cleared */
2998#endif
2999
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000/*
3001 * Structure used to save the current input state, when it needs to be
3002 * restored after trying a match. Used by reg_save() and reg_restore().
Bram Moolenaar582fd852005-03-28 20:58:01 +00003003 * Also stores the length of "backpos".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 */
3005typedef struct
3006{
3007 union
3008 {
3009 char_u *ptr; /* reginput pointer, for single-line regexp */
3010 lpos_T pos; /* reginput pos, for multi-line regexp */
3011 } rs_u;
Bram Moolenaar582fd852005-03-28 20:58:01 +00003012 int rs_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013} regsave_T;
3014
3015/* struct to save start/end pointer/position in for \(\) */
3016typedef struct
3017{
3018 union
3019 {
3020 char_u *ptr;
3021 lpos_T pos;
3022 } se_u;
3023} save_se_T;
3024
3025static char_u *reg_getline __ARGS((linenr_T lnum));
3026static long vim_regexec_both __ARGS((char_u *line, colnr_T col));
3027static long regtry __ARGS((regprog_T *prog, colnr_T col));
3028static void cleanup_subexpr __ARGS((void));
3029#ifdef FEAT_SYN_HL
3030static void cleanup_zsubexpr __ARGS((void));
3031#endif
3032static void reg_nextline __ARGS((void));
Bram Moolenaar582fd852005-03-28 20:58:01 +00003033static void reg_save __ARGS((regsave_T *save, garray_T *gap));
3034static void reg_restore __ARGS((regsave_T *save, garray_T *gap));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035static int reg_save_equal __ARGS((regsave_T *save));
3036static void save_se_multi __ARGS((save_se_T *savep, lpos_T *posp));
3037static void save_se_one __ARGS((save_se_T *savep, char_u **pp));
3038
3039/* Save the sub-expressions before attempting a match. */
3040#define save_se(savep, posp, pp) \
3041 REG_MULTI ? save_se_multi((savep), (posp)) : save_se_one((savep), (pp))
3042
3043/* After a failed match restore the sub-expressions. */
3044#define restore_se(savep, posp, pp) { \
3045 if (REG_MULTI) \
3046 *(posp) = (savep)->se_u.pos; \
3047 else \
3048 *(pp) = (savep)->se_u.ptr; }
3049
3050static int re_num_cmp __ARGS((long_u val, char_u *scan));
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003051static int regmatch __ARGS((char_u *prog));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052static int regrepeat __ARGS((char_u *p, long maxcount));
3053
3054#ifdef DEBUG
3055int regnarrate = 0;
3056#endif
3057
3058/*
3059 * Internal copy of 'ignorecase'. It is set at each call to vim_regexec().
3060 * Normally it gets the value of "rm_ic" or "rmm_ic", but when the pattern
3061 * contains '\c' or '\C' the value is overruled.
3062 */
3063static int ireg_ic;
3064
3065#ifdef FEAT_MBYTE
3066/*
3067 * Similar to ireg_ic, but only for 'combining' characters. Set with \Z flag
3068 * in the regexp. Defaults to false, always.
3069 */
3070static int ireg_icombine;
3071#endif
3072
3073/*
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003074 * Copy of "rmm_maxcol": maximum column to search for a match. Zero when
3075 * there is no maximum.
3076 */
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003077static colnr_T ireg_maxcol;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003078
3079/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 * Sometimes need to save a copy of a line. Since alloc()/free() is very
3081 * slow, we keep one allocated piece of memory and only re-allocate it when
3082 * it's too small. It's freed in vim_regexec_both() when finished.
3083 */
3084static char_u *reg_tofree;
3085static unsigned reg_tofreelen;
3086
3087/*
3088 * These variables are set when executing a regexp to speed up the execution.
3089 * Which ones are set depends on whethere a single-line or multi-line match is
3090 * done:
3091 * single-line multi-line
3092 * reg_match &regmatch_T NULL
3093 * reg_mmatch NULL &regmmatch_T
3094 * reg_startp reg_match->startp <invalid>
3095 * reg_endp reg_match->endp <invalid>
3096 * reg_startpos <invalid> reg_mmatch->startpos
3097 * reg_endpos <invalid> reg_mmatch->endpos
3098 * reg_win NULL window in which to search
3099 * reg_buf <invalid> buffer in which to search
3100 * reg_firstlnum <invalid> first line in which to search
3101 * reg_maxline 0 last line nr
3102 * reg_line_lbr FALSE or TRUE FALSE
3103 */
3104static regmatch_T *reg_match;
3105static regmmatch_T *reg_mmatch;
3106static char_u **reg_startp = NULL;
3107static char_u **reg_endp = NULL;
3108static lpos_T *reg_startpos = NULL;
3109static lpos_T *reg_endpos = NULL;
3110static win_T *reg_win;
3111static buf_T *reg_buf;
3112static linenr_T reg_firstlnum;
3113static linenr_T reg_maxline;
3114static int reg_line_lbr; /* "\n" in string is line break */
3115
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003116/* Values for rs_state in regitem_T. */
3117typedef enum regstate_E
3118{
3119 RS_NOPEN = 0 /* NOPEN and NCLOSE */
3120 , RS_MOPEN /* MOPEN + [0-9] */
3121 , RS_MCLOSE /* MCLOSE + [0-9] */
3122#ifdef FEAT_SYN_HL
3123 , RS_ZOPEN /* ZOPEN + [0-9] */
3124 , RS_ZCLOSE /* ZCLOSE + [0-9] */
3125#endif
3126 , RS_BRANCH /* BRANCH */
3127 , RS_BRCPLX_MORE /* BRACE_COMPLEX and trying one more match */
3128 , RS_BRCPLX_LONG /* BRACE_COMPLEX and trying longest match */
3129 , RS_BRCPLX_SHORT /* BRACE_COMPLEX and trying shortest match */
3130 , RS_NOMATCH /* NOMATCH */
3131 , RS_BEHIND1 /* BEHIND / NOBEHIND matching rest */
3132 , RS_BEHIND2 /* BEHIND / NOBEHIND matching behind part */
3133 , RS_STAR_LONG /* STAR/PLUS/BRACE_SIMPLE longest match */
3134 , RS_STAR_SHORT /* STAR/PLUS/BRACE_SIMPLE shortest match */
3135} regstate_T;
3136
3137/*
3138 * When there are alternatives a regstate_T is put on the regstack to remember
3139 * what we are doing.
3140 * Before it may be another type of item, depending on rs_state, to remember
3141 * more things.
3142 */
3143typedef struct regitem_S
3144{
3145 regstate_T rs_state; /* what we are doing, one of RS_ above */
3146 char_u *rs_scan; /* current node in program */
3147 union
3148 {
3149 save_se_T sesave;
3150 regsave_T regsave;
3151 } rs_un; /* room for saving reginput */
3152 short rs_no; /* submatch nr */
3153} regitem_T;
3154
3155static regitem_T *regstack_push __ARGS((regstate_T state, char_u *scan));
3156static void regstack_pop __ARGS((char_u **scan));
3157
3158/* used for BEHIND and NOBEHIND matching */
3159typedef struct regbehind_S
3160{
3161 regsave_T save_after;
3162 regsave_T save_behind;
3163} regbehind_T;
3164
3165/* used for STAR, PLUS and BRACE_SIMPLE matching */
3166typedef struct regstar_S
3167{
3168 int nextb; /* next byte */
3169 int nextb_ic; /* next byte reverse case */
3170 long count;
3171 long minval;
3172 long maxval;
3173} regstar_T;
3174
3175/* used to store input position when a BACK was encountered, so that we now if
3176 * we made any progress since the last time. */
3177typedef struct backpos_S
3178{
3179 char_u *bp_scan; /* "scan" where BACK was encountered */
3180 regsave_T bp_pos; /* last input position */
3181} backpos_T;
3182
3183/*
3184 * regstack and backpos are used by regmatch(). They are kept over calls to
3185 * avoid invoking malloc() and free() often.
3186 */
3187static garray_T regstack; /* stack with regitem_T items, sometimes
3188 preceded by regstar_T or regbehind_T. */
3189static garray_T backpos; /* table with backpos_T for BACK */
3190
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191/*
3192 * Get pointer to the line "lnum", which is relative to "reg_firstlnum".
3193 */
3194 static char_u *
3195reg_getline(lnum)
3196 linenr_T lnum;
3197{
3198 /* when looking behind for a match/no-match lnum is negative. But we
3199 * can't go before line 1 */
3200 if (reg_firstlnum + lnum < 1)
3201 return NULL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00003202 if (lnum > reg_maxline)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003203 /* Must have matched the "\n" in the last line. */
3204 return (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 return ml_get_buf(reg_buf, reg_firstlnum + lnum, FALSE);
3206}
3207
3208static regsave_T behind_pos;
3209
3210#ifdef FEAT_SYN_HL
3211static char_u *reg_startzp[NSUBEXP]; /* Workspace to mark beginning */
3212static char_u *reg_endzp[NSUBEXP]; /* and end of \z(...\) matches */
3213static lpos_T reg_startzpos[NSUBEXP]; /* idem, beginning pos */
3214static lpos_T reg_endzpos[NSUBEXP]; /* idem, end pos */
3215#endif
3216
3217/* TRUE if using multi-line regexp. */
3218#define REG_MULTI (reg_match == NULL)
3219
3220/*
3221 * Match a regexp against a string.
3222 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
3223 * Uses curbuf for line count and 'iskeyword'.
3224 *
3225 * Return TRUE if there is a match, FALSE if not.
3226 */
3227 int
3228vim_regexec(rmp, line, col)
3229 regmatch_T *rmp;
3230 char_u *line; /* string to match against */
3231 colnr_T col; /* column to start looking for match */
3232{
3233 reg_match = rmp;
3234 reg_mmatch = NULL;
3235 reg_maxline = 0;
3236 reg_line_lbr = FALSE;
3237 reg_win = NULL;
3238 ireg_ic = rmp->rm_ic;
3239#ifdef FEAT_MBYTE
3240 ireg_icombine = FALSE;
3241#endif
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003242 ireg_maxcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 return (vim_regexec_both(line, col) != 0);
3244}
3245
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003246#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
3247 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248/*
3249 * Like vim_regexec(), but consider a "\n" in "line" to be a line break.
3250 */
3251 int
3252vim_regexec_nl(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 = TRUE;
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#endif
3270
3271/*
3272 * Match a regexp against multiple lines.
3273 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
3274 * Uses curbuf for line count and 'iskeyword'.
3275 *
3276 * Return zero if there is no match. Return number of lines contained in the
3277 * match otherwise.
3278 */
3279 long
3280vim_regexec_multi(rmp, win, buf, lnum, col)
3281 regmmatch_T *rmp;
3282 win_T *win; /* window in which to search or NULL */
3283 buf_T *buf; /* buffer in which to search */
3284 linenr_T lnum; /* nr of line to start looking for match */
3285 colnr_T col; /* column to start looking for match */
3286{
3287 long r;
3288 buf_T *save_curbuf = curbuf;
3289
3290 reg_match = NULL;
3291 reg_mmatch = rmp;
3292 reg_buf = buf;
3293 reg_win = win;
3294 reg_firstlnum = lnum;
3295 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
3296 reg_line_lbr = FALSE;
3297 ireg_ic = rmp->rmm_ic;
3298#ifdef FEAT_MBYTE
3299 ireg_icombine = FALSE;
3300#endif
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003301 ireg_maxcol = rmp->rmm_maxcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302
3303 /* Need to switch to buffer "buf" to make vim_iswordc() work. */
3304 curbuf = buf;
3305 r = vim_regexec_both(NULL, col);
3306 curbuf = save_curbuf;
3307
3308 return r;
3309}
3310
3311/*
3312 * Match a regexp against a string ("line" points to the string) or multiple
3313 * lines ("line" is NULL, use reg_getline()).
3314 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 static long
3316vim_regexec_both(line, col)
3317 char_u *line;
3318 colnr_T col; /* column to start looking for match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319{
3320 regprog_T *prog;
3321 char_u *s;
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003322 long retval = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323
3324 reg_tofree = NULL;
3325
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003326 /* Init the regstack empty. Use an item size of 1 byte, since we push
3327 * different things onto it. Use a large grow size to avoid reallocating
3328 * it too often. */
3329 ga_init2(&regstack, 1, 10000);
3330
3331 /* Init the backpos table empty. */
3332 ga_init2(&backpos, sizeof(backpos_T), 10);
3333
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 if (REG_MULTI)
3335 {
3336 prog = reg_mmatch->regprog;
3337 line = reg_getline((linenr_T)0);
3338 reg_startpos = reg_mmatch->startpos;
3339 reg_endpos = reg_mmatch->endpos;
3340 }
3341 else
3342 {
3343 prog = reg_match->regprog;
3344 reg_startp = reg_match->startp;
3345 reg_endp = reg_match->endp;
3346 }
3347
3348 /* Be paranoid... */
3349 if (prog == NULL || line == NULL)
3350 {
3351 EMSG(_(e_null));
3352 goto theend;
3353 }
3354
3355 /* Check validity of program. */
3356 if (prog_magic_wrong())
3357 goto theend;
3358
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003359 /* If the start column is past the maximum column: no need to try. */
3360 if (ireg_maxcol > 0 && col >= ireg_maxcol)
3361 goto theend;
3362
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
3364 if (prog->regflags & RF_ICASE)
3365 ireg_ic = TRUE;
3366 else if (prog->regflags & RF_NOICASE)
3367 ireg_ic = FALSE;
3368
3369#ifdef FEAT_MBYTE
3370 /* If pattern contains "\Z" overrule value of ireg_icombine */
3371 if (prog->regflags & RF_ICOMBINE)
3372 ireg_icombine = TRUE;
3373#endif
3374
3375 /* If there is a "must appear" string, look for it. */
3376 if (prog->regmust != NULL)
3377 {
3378 int c;
3379
3380#ifdef FEAT_MBYTE
3381 if (has_mbyte)
3382 c = (*mb_ptr2char)(prog->regmust);
3383 else
3384#endif
3385 c = *prog->regmust;
3386 s = line + col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003387
3388 /*
3389 * This is used very often, esp. for ":global". Use three versions of
3390 * the loop to avoid overhead of conditions.
3391 */
3392 if (!ireg_ic
3393#ifdef FEAT_MBYTE
3394 && !has_mbyte
3395#endif
3396 )
3397 while ((s = vim_strbyte(s, c)) != NULL)
3398 {
3399 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3400 break; /* Found it. */
3401 ++s;
3402 }
3403#ifdef FEAT_MBYTE
3404 else if (!ireg_ic || (!enc_utf8 && mb_char2len(c) > 1))
3405 while ((s = vim_strchr(s, c)) != NULL)
3406 {
3407 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3408 break; /* Found it. */
3409 mb_ptr_adv(s);
3410 }
3411#endif
3412 else
3413 while ((s = cstrchr(s, c)) != NULL)
3414 {
3415 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3416 break; /* Found it. */
3417 mb_ptr_adv(s);
3418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 if (s == NULL) /* Not present. */
3420 goto theend;
3421 }
3422
3423 regline = line;
3424 reglnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425
3426 /* Simplest case: Anchored match need be tried only once. */
3427 if (prog->reganch)
3428 {
3429 int c;
3430
3431#ifdef FEAT_MBYTE
3432 if (has_mbyte)
3433 c = (*mb_ptr2char)(regline + col);
3434 else
3435#endif
3436 c = regline[col];
3437 if (prog->regstart == NUL
3438 || prog->regstart == c
3439 || (ireg_ic && ((
3440#ifdef FEAT_MBYTE
3441 (enc_utf8 && utf_fold(prog->regstart) == utf_fold(c)))
3442 || (c < 255 && prog->regstart < 255 &&
3443#endif
3444 TOLOWER_LOC(prog->regstart) == TOLOWER_LOC(c)))))
3445 retval = regtry(prog, col);
3446 else
3447 retval = 0;
3448 }
3449 else
3450 {
3451 /* Messy cases: unanchored match. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003452 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 {
3454 if (prog->regstart != NUL)
3455 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003456 /* Skip until the char we know it must start with.
3457 * Used often, do some work to avoid call overhead. */
3458 if (!ireg_ic
3459#ifdef FEAT_MBYTE
3460 && !has_mbyte
3461#endif
3462 )
3463 s = vim_strbyte(regline + col, prog->regstart);
3464 else
3465 s = cstrchr(regline + col, prog->regstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 if (s == NULL)
3467 {
3468 retval = 0;
3469 break;
3470 }
3471 col = (int)(s - regline);
3472 }
3473
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003474 /* Check for maximum column to try. */
3475 if (ireg_maxcol > 0 && col >= ireg_maxcol)
3476 {
3477 retval = 0;
3478 break;
3479 }
3480
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 retval = regtry(prog, col);
3482 if (retval > 0)
3483 break;
3484
3485 /* if not currently on the first line, get it again */
3486 if (reglnum != 0)
3487 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 reglnum = 0;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003489 regline = reg_getline((linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 }
3491 if (regline[col] == NUL)
3492 break;
3493#ifdef FEAT_MBYTE
3494 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003495 col += (*mb_ptr2len)(regline + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 else
3497#endif
3498 ++col;
3499 }
3500 }
3501
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 vim_free(reg_tofree);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003504 ga_clear(&regstack);
3505 ga_clear(&backpos);
3506
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 return retval;
3508}
3509
3510#ifdef FEAT_SYN_HL
3511static reg_extmatch_T *make_extmatch __ARGS((void));
3512
3513/*
3514 * Create a new extmatch and mark it as referenced once.
3515 */
3516 static reg_extmatch_T *
3517make_extmatch()
3518{
3519 reg_extmatch_T *em;
3520
3521 em = (reg_extmatch_T *)alloc_clear((unsigned)sizeof(reg_extmatch_T));
3522 if (em != NULL)
3523 em->refcnt = 1;
3524 return em;
3525}
3526
3527/*
3528 * Add a reference to an extmatch.
3529 */
3530 reg_extmatch_T *
3531ref_extmatch(em)
3532 reg_extmatch_T *em;
3533{
3534 if (em != NULL)
3535 em->refcnt++;
3536 return em;
3537}
3538
3539/*
3540 * Remove a reference to an extmatch. If there are no references left, free
3541 * the info.
3542 */
3543 void
3544unref_extmatch(em)
3545 reg_extmatch_T *em;
3546{
3547 int i;
3548
3549 if (em != NULL && --em->refcnt <= 0)
3550 {
3551 for (i = 0; i < NSUBEXP; ++i)
3552 vim_free(em->matches[i]);
3553 vim_free(em);
3554 }
3555}
3556#endif
3557
3558/*
3559 * regtry - try match of "prog" with at regline["col"].
3560 * Returns 0 for failure, number of lines contained in the match otherwise.
3561 */
3562 static long
3563regtry(prog, col)
3564 regprog_T *prog;
3565 colnr_T col;
3566{
3567 reginput = regline + col;
3568 need_clear_subexpr = TRUE;
3569#ifdef FEAT_SYN_HL
3570 /* Clear the external match subpointers if necessary. */
3571 if (prog->reghasz == REX_SET)
3572 need_clear_zsubexpr = TRUE;
3573#endif
3574
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003575 if (regmatch(prog->program + 1) == 0)
3576 return 0;
3577
3578 cleanup_subexpr();
3579 if (REG_MULTI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003581 if (reg_startpos[0].lnum < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003583 reg_startpos[0].lnum = 0;
3584 reg_startpos[0].col = col;
3585 }
3586 if (reg_endpos[0].lnum < 0)
3587 {
3588 reg_endpos[0].lnum = reglnum;
3589 reg_endpos[0].col = (int)(reginput - regline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 }
3591 else
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003592 /* Use line number of "\ze". */
3593 reglnum = reg_endpos[0].lnum;
3594 }
3595 else
3596 {
3597 if (reg_startp[0] == NULL)
3598 reg_startp[0] = regline + col;
3599 if (reg_endp[0] == NULL)
3600 reg_endp[0] = reginput;
3601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602#ifdef FEAT_SYN_HL
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003603 /* Package any found \z(...\) matches for export. Default is none. */
3604 unref_extmatch(re_extmatch_out);
3605 re_extmatch_out = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003607 if (prog->reghasz == REX_SET)
3608 {
3609 int i;
3610
3611 cleanup_zsubexpr();
3612 re_extmatch_out = make_extmatch();
3613 for (i = 0; i < NSUBEXP; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003615 if (REG_MULTI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003617 /* Only accept single line matches. */
3618 if (reg_startzpos[i].lnum >= 0
3619 && reg_endzpos[i].lnum == reg_startzpos[i].lnum)
3620 re_extmatch_out->matches[i] =
3621 vim_strnsave(reg_getline(reg_startzpos[i].lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 + reg_startzpos[i].col,
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003623 reg_endzpos[i].col - reg_startzpos[i].col);
3624 }
3625 else
3626 {
3627 if (reg_startzp[i] != NULL && reg_endzp[i] != NULL)
3628 re_extmatch_out->matches[i] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 vim_strnsave(reg_startzp[i],
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003630 (int)(reg_endzp[i] - reg_startzp[i]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 }
3632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 }
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003634#endif
3635 return 1 + reglnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636}
3637
3638#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639static int reg_prev_class __ARGS((void));
3640
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641/*
3642 * Get class of previous character.
3643 */
3644 static int
3645reg_prev_class()
3646{
3647 if (reginput > regline)
3648 return mb_get_class(reginput - 1
3649 - (*mb_head_off)(regline, reginput - 1));
3650 return -1;
3651}
3652
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003654#define ADVANCE_REGINPUT() mb_ptr_adv(reginput)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655
3656/*
3657 * The arguments from BRACE_LIMITS are stored here. They are actually local
3658 * to regmatch(), but they are here to reduce the amount of stack space used
3659 * (it can be called recursively many times).
3660 */
3661static long bl_minval;
3662static long bl_maxval;
3663
3664/*
3665 * regmatch - main matching routine
3666 *
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003667 * Conceptually the strategy is simple: Check to see whether the current node
3668 * matches, push an item onto the regstack and loop to see whether the rest
3669 * matches, and then act accordingly. In practice we make some effort to
3670 * avoid using the regstack, in particular by going through "ordinary" nodes
3671 * (that don't need to know whether the rest of the match failed) by a nested
3672 * loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 *
3674 * Returns TRUE when there is a match. Leaves reginput and reglnum just after
3675 * the last matched character.
3676 * Returns FALSE when there is no match. Leaves reginput and reglnum in an
3677 * undefined state!
3678 */
3679 static int
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003680regmatch(scan)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 char_u *scan; /* Current node. */
3682{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003683 char_u *next; /* Next node. */
3684 int op;
3685 int c;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003686 regitem_T *rp;
3687 int no;
3688 int status; /* one of the RA_ values: */
3689#define RA_FAIL 1 /* something failed, abort */
3690#define RA_CONT 2 /* continue in inner loop */
3691#define RA_BREAK 3 /* break inner loop */
3692#define RA_MATCH 4 /* successful match */
3693#define RA_NOMATCH 5 /* didn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003695 /* Init the regstack and backpos table empty. They are initialized and
3696 * freed in vim_regexec_both() to reduce malloc()/free() calls. */
3697 regstack.ga_len = 0;
3698 backpos.ga_len = 0;
Bram Moolenaar582fd852005-03-28 20:58:01 +00003699
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003700 /*
Bram Moolenaar582fd852005-03-28 20:58:01 +00003701 * Repeat until "regstack" is empty.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003702 */
3703 for (;;)
3704 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 /* Some patterns my cause a long time to match, even though they are not
3706 * illegal. E.g., "\([a-z]\+\)\+Q". Allow breaking them with CTRL-C. */
3707 fast_breakcheck();
3708
3709#ifdef DEBUG
3710 if (scan != NULL && regnarrate)
3711 {
3712 mch_errmsg(regprop(scan));
3713 mch_errmsg("(\n");
3714 }
3715#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003716
3717 /*
Bram Moolenaar582fd852005-03-28 20:58:01 +00003718 * Repeat for items that can be matched sequentially, without using the
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003719 * regstack.
3720 */
3721 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003723 if (got_int || scan == NULL)
3724 {
3725 status = RA_FAIL;
3726 break;
3727 }
3728 status = RA_CONT;
3729
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730#ifdef DEBUG
3731 if (regnarrate)
3732 {
3733 mch_errmsg(regprop(scan));
3734 mch_errmsg("...\n");
3735# ifdef FEAT_SYN_HL
3736 if (re_extmatch_in != NULL)
3737 {
3738 int i;
3739
3740 mch_errmsg(_("External submatches:\n"));
3741 for (i = 0; i < NSUBEXP; i++)
3742 {
3743 mch_errmsg(" \"");
3744 if (re_extmatch_in->matches[i] != NULL)
3745 mch_errmsg(re_extmatch_in->matches[i]);
3746 mch_errmsg("\"\n");
3747 }
3748 }
3749# endif
3750 }
3751#endif
3752 next = regnext(scan);
3753
3754 op = OP(scan);
3755 /* Check for character class with NL added. */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003756 if (!reg_line_lbr && WITH_NL(op) && *reginput == NUL
3757 && reglnum <= reg_maxline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 {
3759 reg_nextline();
3760 }
3761 else if (reg_line_lbr && WITH_NL(op) && *reginput == '\n')
3762 {
3763 ADVANCE_REGINPUT();
3764 }
3765 else
3766 {
3767 if (WITH_NL(op))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003768 op -= ADD_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769#ifdef FEAT_MBYTE
3770 if (has_mbyte)
3771 c = (*mb_ptr2char)(reginput);
3772 else
3773#endif
3774 c = *reginput;
3775 switch (op)
3776 {
3777 case BOL:
3778 if (reginput != regline)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003779 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 break;
3781
3782 case EOL:
3783 if (c != NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003784 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 break;
3786
3787 case RE_BOF:
3788 /* Passing -1 to the getline() function provided for the search
3789 * should always return NULL if the current line is the first
3790 * line of the file. */
3791 if (reglnum != 0 || reginput != regline
3792 || (REG_MULTI && reg_getline((linenr_T)-1) != NULL))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003793 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 break;
3795
3796 case RE_EOF:
3797 if (reglnum != reg_maxline || c != NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003798 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 break;
3800
3801 case CURSOR:
3802 /* Check if the buffer is in a window and compare the
3803 * reg_win->w_cursor position to the match position. */
3804 if (reg_win == NULL
3805 || (reglnum + reg_firstlnum != reg_win->w_cursor.lnum)
3806 || ((colnr_T)(reginput - regline) != reg_win->w_cursor.col))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003807 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 break;
3809
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003810 case RE_MARK:
3811 /* Compare the mark position to the match position. NOTE: Always
3812 * uses the current buffer. */
3813 {
3814 int mark = OPERAND(scan)[0];
3815 int cmp = OPERAND(scan)[1];
3816 pos_T *pos;
3817
3818 pos = getmark(mark, FALSE);
3819 if (pos == NULL /* mark doesn't exist) */
3820 || pos->lnum <= 0 /* mark isn't set (in curbuf) */
3821 || (pos->lnum == reglnum + reg_firstlnum
3822 ? (pos->col == (colnr_T)(reginput - regline)
3823 ? (cmp == '<' || cmp == '>')
3824 : (pos->col < (colnr_T)(reginput - regline)
3825 ? cmp != '>'
3826 : cmp != '<'))
3827 : (pos->lnum < reglnum + reg_firstlnum
3828 ? cmp != '>'
3829 : cmp != '<')))
3830 status = RA_NOMATCH;
3831 }
3832 break;
3833
3834 case RE_VISUAL:
3835#ifdef FEAT_VISUAL
3836 /* Check if the buffer is the current buffer. and whether the
3837 * position is inside the Visual area. */
3838 if (reg_buf != curbuf || VIsual.lnum == 0)
3839 status = RA_NOMATCH;
3840 else
3841 {
3842 pos_T top, bot;
3843 linenr_T lnum;
3844 colnr_T col;
3845 win_T *wp = reg_win == NULL ? curwin : reg_win;
3846 int mode;
3847
3848 if (VIsual_active)
3849 {
3850 if (lt(VIsual, wp->w_cursor))
3851 {
3852 top = VIsual;
3853 bot = wp->w_cursor;
3854 }
3855 else
3856 {
3857 top = wp->w_cursor;
3858 bot = VIsual;
3859 }
3860 mode = VIsual_mode;
3861 }
3862 else
3863 {
3864 top = curbuf->b_visual_start;
3865 bot = curbuf->b_visual_end;
3866 mode = curbuf->b_visual_mode;
3867 }
3868 lnum = reglnum + reg_firstlnum;
3869 col = (colnr_T)(reginput - regline);
3870 if (lnum < top.lnum || lnum > bot.lnum)
3871 status = RA_NOMATCH;
3872 else if (mode == 'v')
3873 {
3874 if ((lnum == top.lnum && col < top.col)
3875 || (lnum == bot.lnum
3876 && col >= bot.col + (*p_sel != 'e')))
3877 status = RA_NOMATCH;
3878 }
3879 else if (mode == Ctrl_V)
3880 {
3881 colnr_T start, end;
3882 colnr_T start2, end2;
3883 colnr_T col;
3884
3885 getvvcol(wp, &top, &start, NULL, &end);
3886 getvvcol(wp, &bot, &start2, NULL, &end2);
3887 if (start2 < start)
3888 start = start2;
3889 if (end2 > end)
3890 end = end2;
3891 if (top.col == MAXCOL || bot.col == MAXCOL)
3892 end = MAXCOL;
3893 col = win_linetabsize(wp,
3894 regline, (colnr_T)(reginput - regline));
3895 if (col < start || col > end - (*p_sel == 'e'))
3896 status = RA_NOMATCH;
3897 }
3898 }
3899#else
3900 status = RA_NOMATCH;
3901#endif
3902 break;
3903
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 case RE_LNUM:
3905 if (!REG_MULTI || !re_num_cmp((long_u)(reglnum + reg_firstlnum),
3906 scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003907 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 break;
3909
3910 case RE_COL:
3911 if (!re_num_cmp((long_u)(reginput - regline) + 1, scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003912 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 break;
3914
3915 case RE_VCOL:
3916 if (!re_num_cmp((long_u)win_linetabsize(
3917 reg_win == NULL ? curwin : reg_win,
3918 regline, (colnr_T)(reginput - regline)) + 1, scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003919 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 break;
3921
3922 case BOW: /* \<word; reginput points to w */
3923 if (c == NUL) /* Can't match at end of line */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003924 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925#ifdef FEAT_MBYTE
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003926 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 {
3928 int this_class;
3929
3930 /* Get class of current and previous char (if it exists). */
3931 this_class = mb_get_class(reginput);
3932 if (this_class <= 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003933 status = RA_NOMATCH; /* not on a word at all */
3934 else if (reg_prev_class() == this_class)
3935 status = RA_NOMATCH; /* previous char is in same word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 }
3937#endif
3938 else
3939 {
3940 if (!vim_iswordc(c)
3941 || (reginput > regline && vim_iswordc(reginput[-1])))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003942 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 }
3944 break;
3945
3946 case EOW: /* word\>; reginput points after d */
3947 if (reginput == regline) /* Can't match at start of line */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003948 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949#ifdef FEAT_MBYTE
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003950 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 {
3952 int this_class, prev_class;
3953
3954 /* Get class of current and previous char (if it exists). */
3955 this_class = mb_get_class(reginput);
3956 prev_class = reg_prev_class();
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003957 if (this_class == prev_class
3958 || prev_class == 0 || prev_class == 1)
3959 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003962 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003964 if (!vim_iswordc(reginput[-1])
3965 || (reginput[0] != NUL && vim_iswordc(c)))
3966 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 }
3968 break; /* Matched with EOW */
3969
3970 case ANY:
3971 if (c == NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003972 status = RA_NOMATCH;
3973 else
3974 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 break;
3976
3977 case IDENT:
3978 if (!vim_isIDc(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003979 status = RA_NOMATCH;
3980 else
3981 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 break;
3983
3984 case SIDENT:
3985 if (VIM_ISDIGIT(*reginput) || !vim_isIDc(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003986 status = RA_NOMATCH;
3987 else
3988 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 break;
3990
3991 case KWORD:
3992 if (!vim_iswordp(reginput))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003993 status = RA_NOMATCH;
3994 else
3995 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 break;
3997
3998 case SKWORD:
3999 if (VIM_ISDIGIT(*reginput) || !vim_iswordp(reginput))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004000 status = RA_NOMATCH;
4001 else
4002 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 break;
4004
4005 case FNAME:
4006 if (!vim_isfilec(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004007 status = RA_NOMATCH;
4008 else
4009 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 break;
4011
4012 case SFNAME:
4013 if (VIM_ISDIGIT(*reginput) || !vim_isfilec(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004014 status = RA_NOMATCH;
4015 else
4016 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 break;
4018
4019 case PRINT:
4020 if (ptr2cells(reginput) != 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004021 status = RA_NOMATCH;
4022 else
4023 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 break;
4025
4026 case SPRINT:
4027 if (VIM_ISDIGIT(*reginput) || ptr2cells(reginput) != 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004028 status = RA_NOMATCH;
4029 else
4030 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 break;
4032
4033 case WHITE:
4034 if (!vim_iswhite(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004035 status = RA_NOMATCH;
4036 else
4037 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 break;
4039
4040 case NWHITE:
4041 if (c == NUL || vim_iswhite(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004042 status = RA_NOMATCH;
4043 else
4044 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 break;
4046
4047 case DIGIT:
4048 if (!ri_digit(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004049 status = RA_NOMATCH;
4050 else
4051 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 break;
4053
4054 case NDIGIT:
4055 if (c == NUL || ri_digit(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004056 status = RA_NOMATCH;
4057 else
4058 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 break;
4060
4061 case HEX:
4062 if (!ri_hex(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004063 status = RA_NOMATCH;
4064 else
4065 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 break;
4067
4068 case NHEX:
4069 if (c == NUL || ri_hex(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004070 status = RA_NOMATCH;
4071 else
4072 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 break;
4074
4075 case OCTAL:
4076 if (!ri_octal(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004077 status = RA_NOMATCH;
4078 else
4079 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 break;
4081
4082 case NOCTAL:
4083 if (c == NUL || ri_octal(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004084 status = RA_NOMATCH;
4085 else
4086 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 break;
4088
4089 case WORD:
4090 if (!ri_word(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004091 status = RA_NOMATCH;
4092 else
4093 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 break;
4095
4096 case NWORD:
4097 if (c == NUL || ri_word(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004098 status = RA_NOMATCH;
4099 else
4100 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 break;
4102
4103 case HEAD:
4104 if (!ri_head(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004105 status = RA_NOMATCH;
4106 else
4107 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 break;
4109
4110 case NHEAD:
4111 if (c == NUL || ri_head(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004112 status = RA_NOMATCH;
4113 else
4114 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 break;
4116
4117 case ALPHA:
4118 if (!ri_alpha(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004119 status = RA_NOMATCH;
4120 else
4121 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 break;
4123
4124 case NALPHA:
4125 if (c == NUL || ri_alpha(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004126 status = RA_NOMATCH;
4127 else
4128 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 break;
4130
4131 case LOWER:
4132 if (!ri_lower(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004133 status = RA_NOMATCH;
4134 else
4135 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 break;
4137
4138 case NLOWER:
4139 if (c == NUL || ri_lower(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004140 status = RA_NOMATCH;
4141 else
4142 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 break;
4144
4145 case UPPER:
4146 if (!ri_upper(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004147 status = RA_NOMATCH;
4148 else
4149 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 break;
4151
4152 case NUPPER:
4153 if (c == NUL || ri_upper(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004154 status = RA_NOMATCH;
4155 else
4156 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 break;
4158
4159 case EXACTLY:
4160 {
4161 int len;
4162 char_u *opnd;
4163
4164 opnd = OPERAND(scan);
4165 /* Inline the first byte, for speed. */
4166 if (*opnd != *reginput
4167 && (!ireg_ic || (
4168#ifdef FEAT_MBYTE
4169 !enc_utf8 &&
4170#endif
4171 TOLOWER_LOC(*opnd) != TOLOWER_LOC(*reginput))))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004172 status = RA_NOMATCH;
4173 else if (*opnd == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 {
4175 /* match empty string always works; happens when "~" is
4176 * empty. */
4177 }
4178 else if (opnd[1] == NUL
4179#ifdef FEAT_MBYTE
4180 && !(enc_utf8 && ireg_ic)
4181#endif
4182 )
4183 ++reginput; /* matched a single char */
4184 else
4185 {
4186 len = (int)STRLEN(opnd);
4187 /* Need to match first byte again for multi-byte. */
4188 if (cstrncmp(opnd, reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004189 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190#ifdef FEAT_MBYTE
4191 /* Check for following composing character. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004192 else if (enc_utf8
4193 && UTF_COMPOSINGLIKE(reginput, reginput + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 {
4195 /* raaron: This code makes a composing character get
4196 * ignored, which is the correct behavior (sometimes)
4197 * for voweled Hebrew texts. */
4198 if (!ireg_icombine)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004199 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004202 else
4203 reginput += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 }
4205 }
4206 break;
4207
4208 case ANYOF:
4209 case ANYBUT:
4210 if (c == NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004211 status = RA_NOMATCH;
4212 else if ((cstrchr(OPERAND(scan), c) == NULL) == (op == ANYOF))
4213 status = RA_NOMATCH;
4214 else
4215 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 break;
4217
4218#ifdef FEAT_MBYTE
4219 case MULTIBYTECODE:
4220 if (has_mbyte)
4221 {
4222 int i, len;
4223 char_u *opnd;
4224
4225 opnd = OPERAND(scan);
4226 /* Safety check (just in case 'encoding' was changed since
4227 * compiling the program). */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004228 if ((len = (*mb_ptr2len)(opnd)) < 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004229 {
4230 status = RA_NOMATCH;
4231 break;
4232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 for (i = 0; i < len; ++i)
4234 if (opnd[i] != reginput[i])
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004235 {
4236 status = RA_NOMATCH;
4237 break;
4238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 reginput += len;
4240 }
4241 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004242 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 break;
4244#endif
4245
4246 case NOTHING:
4247 break;
4248
4249 case BACK:
Bram Moolenaar582fd852005-03-28 20:58:01 +00004250 {
4251 int i;
4252 backpos_T *bp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253
Bram Moolenaar582fd852005-03-28 20:58:01 +00004254 /*
4255 * When we run into BACK we need to check if we don't keep
4256 * looping without matching any input. The second and later
4257 * times a BACK is encountered it fails if the input is still
4258 * at the same position as the previous time.
4259 * The positions are stored in "backpos" and found by the
4260 * current value of "scan", the position in the RE program.
4261 */
4262 bp = (backpos_T *)backpos.ga_data;
4263 for (i = 0; i < backpos.ga_len; ++i)
4264 if (bp[i].bp_scan == scan)
4265 break;
4266 if (i == backpos.ga_len)
4267 {
4268 /* First time at this BACK, make room to store the pos. */
4269 if (ga_grow(&backpos, 1) == FAIL)
4270 status = RA_FAIL;
4271 else
4272 {
4273 /* get "ga_data" again, it may have changed */
4274 bp = (backpos_T *)backpos.ga_data;
4275 bp[i].bp_scan = scan;
4276 ++backpos.ga_len;
4277 }
4278 }
4279 else if (reg_save_equal(&bp[i].bp_pos))
4280 /* Still at same position as last time, fail. */
4281 status = RA_NOMATCH;
4282
4283 if (status != RA_FAIL && status != RA_NOMATCH)
4284 reg_save(&bp[i].bp_pos, &backpos);
4285 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004286 break;
4287
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 case MOPEN + 0: /* Match start: \zs */
4289 case MOPEN + 1: /* \( */
4290 case MOPEN + 2:
4291 case MOPEN + 3:
4292 case MOPEN + 4:
4293 case MOPEN + 5:
4294 case MOPEN + 6:
4295 case MOPEN + 7:
4296 case MOPEN + 8:
4297 case MOPEN + 9:
4298 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 no = op - MOPEN;
4300 cleanup_subexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004301 rp = regstack_push(RS_MOPEN, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004302 if (rp == NULL)
4303 status = RA_FAIL;
4304 else
4305 {
4306 rp->rs_no = no;
4307 save_se(&rp->rs_un.sesave, &reg_startpos[no],
4308 &reg_startp[no]);
4309 /* We simply continue and handle the result when done. */
4310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004312 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313
4314 case NOPEN: /* \%( */
4315 case NCLOSE: /* \) after \%( */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004316 if (regstack_push(RS_NOPEN, scan) == NULL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004317 status = RA_FAIL;
4318 /* We simply continue and handle the result when done. */
4319 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320
4321#ifdef FEAT_SYN_HL
4322 case ZOPEN + 1:
4323 case ZOPEN + 2:
4324 case ZOPEN + 3:
4325 case ZOPEN + 4:
4326 case ZOPEN + 5:
4327 case ZOPEN + 6:
4328 case ZOPEN + 7:
4329 case ZOPEN + 8:
4330 case ZOPEN + 9:
4331 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 no = op - ZOPEN;
4333 cleanup_zsubexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004334 rp = regstack_push(RS_ZOPEN, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004335 if (rp == NULL)
4336 status = RA_FAIL;
4337 else
4338 {
4339 rp->rs_no = no;
4340 save_se(&rp->rs_un.sesave, &reg_startzpos[no],
4341 &reg_startzp[no]);
4342 /* We simply continue and handle the result when done. */
4343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004345 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346#endif
4347
4348 case MCLOSE + 0: /* Match end: \ze */
4349 case MCLOSE + 1: /* \) */
4350 case MCLOSE + 2:
4351 case MCLOSE + 3:
4352 case MCLOSE + 4:
4353 case MCLOSE + 5:
4354 case MCLOSE + 6:
4355 case MCLOSE + 7:
4356 case MCLOSE + 8:
4357 case MCLOSE + 9:
4358 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 no = op - MCLOSE;
4360 cleanup_subexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004361 rp = regstack_push(RS_MCLOSE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004362 if (rp == NULL)
4363 status = RA_FAIL;
4364 else
4365 {
4366 rp->rs_no = no;
4367 save_se(&rp->rs_un.sesave, &reg_endpos[no], &reg_endp[no]);
4368 /* We simply continue and handle the result when done. */
4369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004371 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372
4373#ifdef FEAT_SYN_HL
4374 case ZCLOSE + 1: /* \) after \z( */
4375 case ZCLOSE + 2:
4376 case ZCLOSE + 3:
4377 case ZCLOSE + 4:
4378 case ZCLOSE + 5:
4379 case ZCLOSE + 6:
4380 case ZCLOSE + 7:
4381 case ZCLOSE + 8:
4382 case ZCLOSE + 9:
4383 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 no = op - ZCLOSE;
4385 cleanup_zsubexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004386 rp = regstack_push(RS_ZCLOSE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004387 if (rp == NULL)
4388 status = RA_FAIL;
4389 else
4390 {
4391 rp->rs_no = no;
4392 save_se(&rp->rs_un.sesave, &reg_endzpos[no],
4393 &reg_endzp[no]);
4394 /* We simply continue and handle the result when done. */
4395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004397 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398#endif
4399
4400 case BACKREF + 1:
4401 case BACKREF + 2:
4402 case BACKREF + 3:
4403 case BACKREF + 4:
4404 case BACKREF + 5:
4405 case BACKREF + 6:
4406 case BACKREF + 7:
4407 case BACKREF + 8:
4408 case BACKREF + 9:
4409 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 int len;
4411 linenr_T clnum;
4412 colnr_T ccol;
4413 char_u *p;
4414
4415 no = op - BACKREF;
4416 cleanup_subexpr();
4417 if (!REG_MULTI) /* Single-line regexp */
4418 {
4419 if (reg_endp[no] == NULL)
4420 {
4421 /* Backref was not set: Match an empty string. */
4422 len = 0;
4423 }
4424 else
4425 {
4426 /* Compare current input with back-ref in the same
4427 * line. */
4428 len = (int)(reg_endp[no] - reg_startp[no]);
4429 if (cstrncmp(reg_startp[no], reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004430 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 }
4432 }
4433 else /* Multi-line regexp */
4434 {
4435 if (reg_endpos[no].lnum < 0)
4436 {
4437 /* Backref was not set: Match an empty string. */
4438 len = 0;
4439 }
4440 else
4441 {
4442 if (reg_startpos[no].lnum == reglnum
4443 && reg_endpos[no].lnum == reglnum)
4444 {
4445 /* Compare back-ref within the current line. */
4446 len = reg_endpos[no].col - reg_startpos[no].col;
4447 if (cstrncmp(regline + reg_startpos[no].col,
4448 reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004449 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 }
4451 else
4452 {
4453 /* Messy situation: Need to compare between two
4454 * lines. */
4455 ccol = reg_startpos[no].col;
4456 clnum = reg_startpos[no].lnum;
4457 for (;;)
4458 {
4459 /* Since getting one line may invalidate
4460 * the other, need to make copy. Slow! */
4461 if (regline != reg_tofree)
4462 {
4463 len = (int)STRLEN(regline);
4464 if (reg_tofree == NULL
4465 || len >= (int)reg_tofreelen)
4466 {
4467 len += 50; /* get some extra */
4468 vim_free(reg_tofree);
4469 reg_tofree = alloc(len);
4470 if (reg_tofree == NULL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004471 {
4472 status = RA_FAIL; /* outof memory!*/
4473 break;
4474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 reg_tofreelen = len;
4476 }
4477 STRCPY(reg_tofree, regline);
4478 reginput = reg_tofree
4479 + (reginput - regline);
4480 regline = reg_tofree;
4481 }
4482
4483 /* Get the line to compare with. */
4484 p = reg_getline(clnum);
4485 if (clnum == reg_endpos[no].lnum)
4486 len = reg_endpos[no].col - ccol;
4487 else
4488 len = (int)STRLEN(p + ccol);
4489
4490 if (cstrncmp(p + ccol, reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004491 {
4492 status = RA_NOMATCH; /* doesn't match */
4493 break;
4494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 if (clnum == reg_endpos[no].lnum)
4496 break; /* match and at end! */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004497 if (reglnum >= reg_maxline)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004498 {
4499 status = RA_NOMATCH; /* text too short */
4500 break;
4501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502
4503 /* Advance to next line. */
4504 reg_nextline();
4505 ++clnum;
4506 ccol = 0;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004507 if (got_int)
4508 {
4509 status = RA_FAIL;
4510 break;
4511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 }
4513
4514 /* found a match! Note that regline may now point
4515 * to a copy of the line, that should not matter. */
4516 }
4517 }
4518 }
4519
4520 /* Matched the backref, skip over it. */
4521 reginput += len;
4522 }
4523 break;
4524
4525#ifdef FEAT_SYN_HL
4526 case ZREF + 1:
4527 case ZREF + 2:
4528 case ZREF + 3:
4529 case ZREF + 4:
4530 case ZREF + 5:
4531 case ZREF + 6:
4532 case ZREF + 7:
4533 case ZREF + 8:
4534 case ZREF + 9:
4535 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 int len;
4537
4538 cleanup_zsubexpr();
4539 no = op - ZREF;
4540 if (re_extmatch_in != NULL
4541 && re_extmatch_in->matches[no] != NULL)
4542 {
4543 len = (int)STRLEN(re_extmatch_in->matches[no]);
4544 if (cstrncmp(re_extmatch_in->matches[no],
4545 reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004546 status = RA_NOMATCH;
4547 else
4548 reginput += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 }
4550 else
4551 {
4552 /* Backref was not set: Match an empty string. */
4553 }
4554 }
4555 break;
4556#endif
4557
4558 case BRANCH:
4559 {
4560 if (OP(next) != BRANCH) /* No choice. */
4561 next = OPERAND(scan); /* Avoid recursion. */
4562 else
4563 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004564 rp = regstack_push(RS_BRANCH, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004565 if (rp == NULL)
4566 status = RA_FAIL;
4567 else
4568 status = RA_BREAK; /* rest is below */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 }
4570 }
4571 break;
4572
4573 case BRACE_LIMITS:
4574 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 if (OP(next) == BRACE_SIMPLE)
4576 {
4577 bl_minval = OPERAND_MIN(scan);
4578 bl_maxval = OPERAND_MAX(scan);
4579 }
4580 else if (OP(next) >= BRACE_COMPLEX
4581 && OP(next) < BRACE_COMPLEX + 10)
4582 {
4583 no = OP(next) - BRACE_COMPLEX;
4584 brace_min[no] = OPERAND_MIN(scan);
4585 brace_max[no] = OPERAND_MAX(scan);
4586 brace_count[no] = 0;
4587 }
4588 else
4589 {
4590 EMSG(_(e_internal)); /* Shouldn't happen */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004591 status = RA_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 }
4593 }
4594 break;
4595
4596 case BRACE_COMPLEX + 0:
4597 case BRACE_COMPLEX + 1:
4598 case BRACE_COMPLEX + 2:
4599 case BRACE_COMPLEX + 3:
4600 case BRACE_COMPLEX + 4:
4601 case BRACE_COMPLEX + 5:
4602 case BRACE_COMPLEX + 6:
4603 case BRACE_COMPLEX + 7:
4604 case BRACE_COMPLEX + 8:
4605 case BRACE_COMPLEX + 9:
4606 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 no = op - BRACE_COMPLEX;
4608 ++brace_count[no];
4609
4610 /* If not matched enough times yet, try one more */
4611 if (brace_count[no] <= (brace_min[no] <= brace_max[no]
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004612 ? brace_min[no] : brace_max[no]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004614 rp = regstack_push(RS_BRCPLX_MORE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004615 if (rp == NULL)
4616 status = RA_FAIL;
4617 else
4618 {
4619 rp->rs_no = no;
Bram Moolenaar582fd852005-03-28 20:58:01 +00004620 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004621 next = OPERAND(scan);
4622 /* We continue and handle the result when done. */
4623 }
4624 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 }
4626
4627 /* If matched enough times, may try matching some more */
4628 if (brace_min[no] <= brace_max[no])
4629 {
4630 /* Range is the normal way around, use longest match */
4631 if (brace_count[no] <= brace_max[no])
4632 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004633 rp = regstack_push(RS_BRCPLX_LONG, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004634 if (rp == NULL)
4635 status = RA_FAIL;
4636 else
4637 {
4638 rp->rs_no = no;
Bram Moolenaar582fd852005-03-28 20:58:01 +00004639 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004640 next = OPERAND(scan);
4641 /* We continue and handle the result when done. */
4642 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 }
4644 }
4645 else
4646 {
4647 /* Range is backwards, use shortest match first */
4648 if (brace_count[no] <= brace_min[no])
4649 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004650 rp = regstack_push(RS_BRCPLX_SHORT, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004651 if (rp == NULL)
4652 status = RA_FAIL;
4653 else
4654 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00004655 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004656 /* We continue and handle the result when done. */
4657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 }
4659 }
4660 }
4661 break;
4662
4663 case BRACE_SIMPLE:
4664 case STAR:
4665 case PLUS:
4666 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004667 regstar_T rst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668
4669 /*
4670 * Lookahead to avoid useless match attempts when we know
4671 * what character comes next.
4672 */
4673 if (OP(next) == EXACTLY)
4674 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004675 rst.nextb = *OPERAND(next);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 if (ireg_ic)
4677 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004678 if (isupper(rst.nextb))
4679 rst.nextb_ic = TOLOWER_LOC(rst.nextb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004681 rst.nextb_ic = TOUPPER_LOC(rst.nextb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 }
4683 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004684 rst.nextb_ic = rst.nextb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 }
4686 else
4687 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004688 rst.nextb = NUL;
4689 rst.nextb_ic = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 }
4691 if (op != BRACE_SIMPLE)
4692 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004693 rst.minval = (op == STAR) ? 0 : 1;
4694 rst.maxval = MAX_LIMIT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 }
4696 else
4697 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004698 rst.minval = bl_minval;
4699 rst.maxval = bl_maxval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 }
4701
4702 /*
4703 * When maxval > minval, try matching as much as possible, up
4704 * to maxval. When maxval < minval, try matching at least the
4705 * minimal number (since the range is backwards, that's also
4706 * maxval!).
4707 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004708 rst.count = regrepeat(OPERAND(scan), rst.maxval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004711 status = RA_FAIL;
4712 break;
4713 }
4714 if (rst.minval <= rst.maxval
4715 ? rst.count >= rst.minval : rst.count >= rst.maxval)
4716 {
4717 /* It could match. Prepare for trying to match what
4718 * follows. The code is below. Parameters are stored in
4719 * a regstar_T on the regstack. */
Bram Moolenaar916b7af2005-03-16 09:52:38 +00004720 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004721 {
4722 EMSG(_(e_maxmempat));
4723 status = RA_FAIL;
4724 }
4725 else if (ga_grow(&regstack, sizeof(regstar_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004726 status = RA_FAIL;
4727 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004729 regstack.ga_len += sizeof(regstar_T);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004730 rp = regstack_push(rst.minval <= rst.maxval
Bram Moolenaar582fd852005-03-28 20:58:01 +00004731 ? RS_STAR_LONG : RS_STAR_SHORT, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004732 if (rp == NULL)
4733 status = RA_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004735 {
4736 *(((regstar_T *)rp) - 1) = rst;
4737 status = RA_BREAK; /* skip the restore bits */
4738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 }
4740 }
4741 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004742 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 }
4745 break;
4746
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004747 case NOMATCH:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 case MATCH:
4749 case SUBPAT:
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004750 rp = regstack_push(RS_NOMATCH, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004751 if (rp == NULL)
4752 status = RA_FAIL;
4753 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004755 rp->rs_no = op;
Bram Moolenaar582fd852005-03-28 20:58:01 +00004756 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004757 next = OPERAND(scan);
4758 /* We continue and handle the result when done. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 }
4760 break;
4761
4762 case BEHIND:
4763 case NOBEHIND:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004764 /* Need a bit of room to store extra positions. */
Bram Moolenaar916b7af2005-03-16 09:52:38 +00004765 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004766 {
4767 EMSG(_(e_maxmempat));
4768 status = RA_FAIL;
4769 }
4770 else if (ga_grow(&regstack, sizeof(regbehind_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004771 status = RA_FAIL;
4772 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004774 regstack.ga_len += sizeof(regbehind_T);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004775 rp = regstack_push(RS_BEHIND1, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004776 if (rp == NULL)
4777 status = RA_FAIL;
4778 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004780 rp->rs_no = op;
Bram Moolenaar582fd852005-03-28 20:58:01 +00004781 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004782 /* First try if what follows matches. If it does then we
4783 * check the behind match by looping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004786 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787
4788 case BHPOS:
4789 if (REG_MULTI)
4790 {
4791 if (behind_pos.rs_u.pos.col != (colnr_T)(reginput - regline)
4792 || behind_pos.rs_u.pos.lnum != reglnum)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004793 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 }
4795 else if (behind_pos.rs_u.ptr != reginput)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004796 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 break;
4798
4799 case NEWL:
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004800 if ((c != NUL || reglnum > reg_maxline || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 && (c != '\n' || !reg_line_lbr))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004802 status = RA_NOMATCH;
4803 else if (reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 ADVANCE_REGINPUT();
4805 else
4806 reg_nextline();
4807 break;
4808
4809 case END:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004810 status = RA_MATCH; /* Success! */
4811 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812
4813 default:
4814 EMSG(_(e_re_corr));
4815#ifdef DEBUG
4816 printf("Illegal op code %d\n", op);
4817#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004818 status = RA_FAIL;
4819 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 }
4821 }
4822
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004823 /* If we can't continue sequentially, break the inner loop. */
4824 if (status != RA_CONT)
4825 break;
4826
4827 /* Continue in inner loop, advance to next item. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 scan = next;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004829
4830 } /* end of inner loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831
4832 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004833 * If there is something on the regstack execute the code for the state.
Bram Moolenaar582fd852005-03-28 20:58:01 +00004834 * If the state is popped then loop and use the older state.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004836 while (regstack.ga_len > 0 && status != RA_FAIL)
4837 {
4838 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len) - 1;
4839 switch (rp->rs_state)
4840 {
4841 case RS_NOPEN:
4842 /* Result is passed on as-is, simply pop the state. */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004843 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004844 break;
4845
4846 case RS_MOPEN:
4847 /* Pop the state. Restore pointers when there is no match. */
4848 if (status == RA_NOMATCH)
4849 restore_se(&rp->rs_un.sesave, &reg_startpos[rp->rs_no],
4850 &reg_startp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004851 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004852 break;
4853
4854#ifdef FEAT_SYN_HL
4855 case RS_ZOPEN:
4856 /* Pop the state. Restore pointers when there is no match. */
4857 if (status == RA_NOMATCH)
4858 restore_se(&rp->rs_un.sesave, &reg_startzpos[rp->rs_no],
4859 &reg_startzp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004860 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004861 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004863
4864 case RS_MCLOSE:
4865 /* Pop the state. Restore pointers when there is no match. */
4866 if (status == RA_NOMATCH)
4867 restore_se(&rp->rs_un.sesave, &reg_endpos[rp->rs_no],
4868 &reg_endp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004869 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004870 break;
4871
4872#ifdef FEAT_SYN_HL
4873 case RS_ZCLOSE:
4874 /* Pop the state. Restore pointers when there is no match. */
4875 if (status == RA_NOMATCH)
4876 restore_se(&rp->rs_un.sesave, &reg_endzpos[rp->rs_no],
4877 &reg_endzp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004878 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004879 break;
4880#endif
4881
4882 case RS_BRANCH:
4883 if (status == RA_MATCH)
4884 /* this branch matched, use it */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004885 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004886 else
4887 {
4888 if (status != RA_BREAK)
4889 {
4890 /* After a non-matching branch: try next one. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00004891 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004892 scan = rp->rs_scan;
4893 }
4894 if (scan == NULL || OP(scan) != BRANCH)
4895 {
4896 /* no more branches, didn't find a match */
4897 status = RA_NOMATCH;
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004898 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004899 }
4900 else
4901 {
4902 /* Prepare to try a branch. */
4903 rp->rs_scan = regnext(scan);
Bram Moolenaar582fd852005-03-28 20:58:01 +00004904 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004905 scan = OPERAND(scan);
4906 }
4907 }
4908 break;
4909
4910 case RS_BRCPLX_MORE:
4911 /* Pop the state. Restore pointers when there is no match. */
4912 if (status == RA_NOMATCH)
4913 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00004914 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004915 --brace_count[rp->rs_no]; /* decrement match count */
4916 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004917 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004918 break;
4919
4920 case RS_BRCPLX_LONG:
4921 /* Pop the state. Restore pointers when there is no match. */
4922 if (status == RA_NOMATCH)
4923 {
4924 /* There was no match, but we did find enough matches. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00004925 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004926 --brace_count[rp->rs_no];
4927 /* continue with the items after "\{}" */
4928 status = RA_CONT;
4929 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004930 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004931 if (status == RA_CONT)
4932 scan = regnext(scan);
4933 break;
4934
4935 case RS_BRCPLX_SHORT:
4936 /* Pop the state. Restore pointers when there is no match. */
4937 if (status == RA_NOMATCH)
4938 /* There was no match, try to match one more item. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00004939 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004940 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004941 if (status == RA_NOMATCH)
4942 {
4943 scan = OPERAND(scan);
4944 status = RA_CONT;
4945 }
4946 break;
4947
4948 case RS_NOMATCH:
4949 /* Pop the state. If the operand matches for NOMATCH or
4950 * doesn't match for MATCH/SUBPAT, we fail. Otherwise backup,
4951 * except for SUBPAT, and continue with the next item. */
4952 if (status == (rp->rs_no == NOMATCH ? RA_MATCH : RA_NOMATCH))
4953 status = RA_NOMATCH;
4954 else
4955 {
4956 status = RA_CONT;
Bram Moolenaar582fd852005-03-28 20:58:01 +00004957 if (rp->rs_no != SUBPAT) /* zero-width */
4958 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004959 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004960 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004961 if (status == RA_CONT)
4962 scan = regnext(scan);
4963 break;
4964
4965 case RS_BEHIND1:
4966 if (status == RA_NOMATCH)
4967 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004968 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004969 regstack.ga_len -= sizeof(regbehind_T);
4970 }
4971 else
4972 {
4973 /* The stuff after BEHIND/NOBEHIND matches. Now try if
4974 * the behind part does (not) match before the current
4975 * position in the input. This must be done at every
4976 * position in the input and checking if the match ends at
4977 * the current position. */
4978
4979 /* save the position after the found match for next */
Bram Moolenaar582fd852005-03-28 20:58:01 +00004980 reg_save(&(((regbehind_T *)rp) - 1)->save_after, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004981
4982 /* start looking for a match with operand at the current
4983 * postion. Go back one character until we find the
4984 * result, hitting the start of the line or the previous
4985 * line (for multi-line matching).
4986 * Set behind_pos to where the match should end, BHPOS
4987 * will match it. Save the current value. */
4988 (((regbehind_T *)rp) - 1)->save_behind = behind_pos;
4989 behind_pos = rp->rs_un.regsave;
4990
4991 rp->rs_state = RS_BEHIND2;
4992
Bram Moolenaar582fd852005-03-28 20:58:01 +00004993 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004994 scan = OPERAND(rp->rs_scan);
4995 }
4996 break;
4997
4998 case RS_BEHIND2:
4999 /*
5000 * Looping for BEHIND / NOBEHIND match.
5001 */
5002 if (status == RA_MATCH && reg_save_equal(&behind_pos))
5003 {
5004 /* found a match that ends where "next" started */
5005 behind_pos = (((regbehind_T *)rp) - 1)->save_behind;
5006 if (rp->rs_no == BEHIND)
Bram Moolenaar582fd852005-03-28 20:58:01 +00005007 reg_restore(&(((regbehind_T *)rp) - 1)->save_after,
5008 &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005009 else
5010 /* But we didn't want a match. */
5011 status = RA_NOMATCH;
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005012 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005013 regstack.ga_len -= sizeof(regbehind_T);
5014 }
5015 else
5016 {
5017 /* No match: Go back one character. May go to previous
5018 * line once. */
5019 no = OK;
5020 if (REG_MULTI)
5021 {
5022 if (rp->rs_un.regsave.rs_u.pos.col == 0)
5023 {
5024 if (rp->rs_un.regsave.rs_u.pos.lnum
5025 < behind_pos.rs_u.pos.lnum
5026 || reg_getline(
5027 --rp->rs_un.regsave.rs_u.pos.lnum)
5028 == NULL)
5029 no = FAIL;
5030 else
5031 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005032 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005033 rp->rs_un.regsave.rs_u.pos.col =
5034 (colnr_T)STRLEN(regline);
5035 }
5036 }
5037 else
5038 --rp->rs_un.regsave.rs_u.pos.col;
5039 }
5040 else
5041 {
5042 if (rp->rs_un.regsave.rs_u.ptr == regline)
5043 no = FAIL;
5044 else
5045 --rp->rs_un.regsave.rs_u.ptr;
5046 }
5047 if (no == OK)
5048 {
5049 /* Advanced, prepare for finding match again. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005050 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005051 scan = OPERAND(rp->rs_scan);
5052 }
5053 else
5054 {
5055 /* Can't advance. For NOBEHIND that's a match. */
5056 behind_pos = (((regbehind_T *)rp) - 1)->save_behind;
5057 if (rp->rs_no == NOBEHIND)
5058 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005059 reg_restore(&(((regbehind_T *)rp) - 1)->save_after,
5060 &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005061 status = RA_MATCH;
5062 }
5063 else
5064 status = RA_NOMATCH;
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005065 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005066 regstack.ga_len -= sizeof(regbehind_T);
5067 }
5068 }
5069 break;
5070
5071 case RS_STAR_LONG:
5072 case RS_STAR_SHORT:
5073 {
5074 regstar_T *rst = ((regstar_T *)rp) - 1;
5075
5076 if (status == RA_MATCH)
5077 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005078 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005079 regstack.ga_len -= sizeof(regstar_T);
5080 break;
5081 }
5082
5083 /* Tried once already, restore input pointers. */
5084 if (status != RA_BREAK)
Bram Moolenaar582fd852005-03-28 20:58:01 +00005085 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005086
5087 /* Repeat until we found a position where it could match. */
5088 for (;;)
5089 {
5090 if (status != RA_BREAK)
5091 {
5092 /* Tried first position already, advance. */
5093 if (rp->rs_state == RS_STAR_LONG)
5094 {
5095 /* Trying for longest matc, but couldn't or didn't
5096 * match -- back up one char. */
5097 if (--rst->count < rst->minval)
5098 break;
5099 if (reginput == regline)
5100 {
5101 /* backup to last char of previous line */
5102 --reglnum;
5103 regline = reg_getline(reglnum);
5104 /* Just in case regrepeat() didn't count
5105 * right. */
5106 if (regline == NULL)
5107 break;
5108 reginput = regline + STRLEN(regline);
5109 fast_breakcheck();
5110 }
5111 else
5112 mb_ptr_back(regline, reginput);
5113 }
5114 else
5115 {
5116 /* Range is backwards, use shortest match first.
5117 * Careful: maxval and minval are exchanged!
5118 * Couldn't or didn't match: try advancing one
5119 * char. */
5120 if (rst->count == rst->minval
5121 || regrepeat(OPERAND(rp->rs_scan), 1L) == 0)
5122 break;
5123 ++rst->count;
5124 }
5125 if (got_int)
5126 break;
5127 }
5128 else
5129 status = RA_NOMATCH;
5130
5131 /* If it could match, try it. */
5132 if (rst->nextb == NUL || *reginput == rst->nextb
5133 || *reginput == rst->nextb_ic)
5134 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005135 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005136 scan = regnext(rp->rs_scan);
5137 status = RA_CONT;
5138 break;
5139 }
5140 }
5141 if (status != RA_CONT)
5142 {
5143 /* Failed. */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005144 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005145 regstack.ga_len -= sizeof(regstar_T);
5146 status = RA_NOMATCH;
5147 }
5148 }
5149 break;
5150 }
5151
5152 /* If we want to continue the inner loop or didn't pop a state contine
5153 * matching loop */
5154 if (status == RA_CONT || rp == (regitem_T *)
5155 ((char *)regstack.ga_data + regstack.ga_len) - 1)
5156 break;
5157 }
5158
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005159 /* May need to continue with the inner loop, starting at "scan". */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005160 if (status == RA_CONT)
5161 continue;
5162
5163 /*
5164 * If the regstack is empty or something failed we are done.
5165 */
5166 if (regstack.ga_len == 0 || status == RA_FAIL)
5167 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005168 if (scan == NULL)
5169 {
5170 /*
5171 * We get here only if there's trouble -- normally "case END" is
5172 * the terminating point.
5173 */
5174 EMSG(_(e_re_corr));
5175#ifdef DEBUG
5176 printf("Premature EOL\n");
5177#endif
5178 }
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005179 if (status == RA_FAIL)
5180 got_int = TRUE;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005181 return (status == RA_MATCH);
5182 }
5183
5184 } /* End of loop until the regstack is empty. */
5185
5186 /* NOTREACHED */
5187}
5188
5189/*
5190 * Push an item onto the regstack.
5191 * Returns pointer to new item. Returns NULL when out of memory.
5192 */
5193 static regitem_T *
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005194regstack_push(state, scan)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005195 regstate_T state;
5196 char_u *scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005197{
5198 regitem_T *rp;
5199
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005200 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005201 {
5202 EMSG(_(e_maxmempat));
5203 return NULL;
5204 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005205 if (ga_grow(&regstack, sizeof(regitem_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005206 return NULL;
5207
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005208 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005209 rp->rs_state = state;
5210 rp->rs_scan = scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005211
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005212 regstack.ga_len += sizeof(regitem_T);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005213 return rp;
5214}
5215
5216/*
5217 * Pop an item from the regstack.
5218 */
5219 static void
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005220regstack_pop(scan)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005221 char_u **scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005222{
5223 regitem_T *rp;
5224
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005225 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len) - 1;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005226 *scan = rp->rs_scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005227
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005228 regstack.ga_len -= sizeof(regitem_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229}
5230
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231/*
5232 * regrepeat - repeatedly match something simple, return how many.
5233 * Advances reginput (and reglnum) to just after the matched chars.
5234 */
5235 static int
5236regrepeat(p, maxcount)
5237 char_u *p;
5238 long maxcount; /* maximum number of matches allowed */
5239{
5240 long count = 0;
5241 char_u *scan;
5242 char_u *opnd;
5243 int mask;
5244 int testval = 0;
5245
5246 scan = reginput; /* Make local copy of reginput for speed. */
5247 opnd = OPERAND(p);
5248 switch (OP(p))
5249 {
5250 case ANY:
5251 case ANY + ADD_NL:
5252 while (count < maxcount)
5253 {
5254 /* Matching anything means we continue until end-of-line (or
5255 * end-of-file for ANY + ADD_NL), only limited by maxcount. */
5256 while (*scan != NUL && count < maxcount)
5257 {
5258 ++count;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005259 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005261 if (!WITH_NL(OP(p)) || reglnum > reg_maxline || reg_line_lbr
5262 || count == maxcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 break;
5264 ++count; /* count the line-break */
5265 reg_nextline();
5266 scan = reginput;
5267 if (got_int)
5268 break;
5269 }
5270 break;
5271
5272 case IDENT:
5273 case IDENT + ADD_NL:
5274 testval = TRUE;
5275 /*FALLTHROUGH*/
5276 case SIDENT:
5277 case SIDENT + ADD_NL:
5278 while (count < maxcount)
5279 {
5280 if (vim_isIDc(*scan) && (testval || !VIM_ISDIGIT(*scan)))
5281 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005282 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 }
5284 else if (*scan == NUL)
5285 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005286 if (!WITH_NL(OP(p)) || reglnum > reg_maxline || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 break;
5288 reg_nextline();
5289 scan = reginput;
5290 if (got_int)
5291 break;
5292 }
5293 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5294 ++scan;
5295 else
5296 break;
5297 ++count;
5298 }
5299 break;
5300
5301 case KWORD:
5302 case KWORD + ADD_NL:
5303 testval = TRUE;
5304 /*FALLTHROUGH*/
5305 case SKWORD:
5306 case SKWORD + ADD_NL:
5307 while (count < maxcount)
5308 {
5309 if (vim_iswordp(scan) && (testval || !VIM_ISDIGIT(*scan)))
5310 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005311 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 }
5313 else if (*scan == NUL)
5314 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005315 if (!WITH_NL(OP(p)) || reglnum > reg_maxline || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 break;
5317 reg_nextline();
5318 scan = reginput;
5319 if (got_int)
5320 break;
5321 }
5322 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5323 ++scan;
5324 else
5325 break;
5326 ++count;
5327 }
5328 break;
5329
5330 case FNAME:
5331 case FNAME + ADD_NL:
5332 testval = TRUE;
5333 /*FALLTHROUGH*/
5334 case SFNAME:
5335 case SFNAME + ADD_NL:
5336 while (count < maxcount)
5337 {
5338 if (vim_isfilec(*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 PRINT:
5360 case PRINT + ADD_NL:
5361 testval = TRUE;
5362 /*FALLTHROUGH*/
5363 case SPRINT:
5364 case SPRINT + ADD_NL:
5365 while (count < maxcount)
5366 {
5367 if (*scan == NUL)
5368 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005369 if (!WITH_NL(OP(p)) || reglnum > reg_maxline || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 break;
5371 reg_nextline();
5372 scan = reginput;
5373 if (got_int)
5374 break;
5375 }
5376 else if (ptr2cells(scan) == 1 && (testval || !VIM_ISDIGIT(*scan)))
5377 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005378 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 }
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 WHITE:
5389 case WHITE + ADD_NL:
5390 testval = mask = RI_WHITE;
5391do_class:
5392 while (count < maxcount)
5393 {
5394#ifdef FEAT_MBYTE
5395 int l;
5396#endif
5397 if (*scan == NUL)
5398 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005399 if (!WITH_NL(OP(p)) || reglnum > reg_maxline || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 break;
5401 reg_nextline();
5402 scan = reginput;
5403 if (got_int)
5404 break;
5405 }
5406#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005407 else if (has_mbyte && (l = (*mb_ptr2len)(scan)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 {
5409 if (testval != 0)
5410 break;
5411 scan += l;
5412 }
5413#endif
5414 else if ((class_tab[*scan] & mask) == testval)
5415 ++scan;
5416 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5417 ++scan;
5418 else
5419 break;
5420 ++count;
5421 }
5422 break;
5423
5424 case NWHITE:
5425 case NWHITE + ADD_NL:
5426 mask = RI_WHITE;
5427 goto do_class;
5428 case DIGIT:
5429 case DIGIT + ADD_NL:
5430 testval = mask = RI_DIGIT;
5431 goto do_class;
5432 case NDIGIT:
5433 case NDIGIT + ADD_NL:
5434 mask = RI_DIGIT;
5435 goto do_class;
5436 case HEX:
5437 case HEX + ADD_NL:
5438 testval = mask = RI_HEX;
5439 goto do_class;
5440 case NHEX:
5441 case NHEX + ADD_NL:
5442 mask = RI_HEX;
5443 goto do_class;
5444 case OCTAL:
5445 case OCTAL + ADD_NL:
5446 testval = mask = RI_OCTAL;
5447 goto do_class;
5448 case NOCTAL:
5449 case NOCTAL + ADD_NL:
5450 mask = RI_OCTAL;
5451 goto do_class;
5452 case WORD:
5453 case WORD + ADD_NL:
5454 testval = mask = RI_WORD;
5455 goto do_class;
5456 case NWORD:
5457 case NWORD + ADD_NL:
5458 mask = RI_WORD;
5459 goto do_class;
5460 case HEAD:
5461 case HEAD + ADD_NL:
5462 testval = mask = RI_HEAD;
5463 goto do_class;
5464 case NHEAD:
5465 case NHEAD + ADD_NL:
5466 mask = RI_HEAD;
5467 goto do_class;
5468 case ALPHA:
5469 case ALPHA + ADD_NL:
5470 testval = mask = RI_ALPHA;
5471 goto do_class;
5472 case NALPHA:
5473 case NALPHA + ADD_NL:
5474 mask = RI_ALPHA;
5475 goto do_class;
5476 case LOWER:
5477 case LOWER + ADD_NL:
5478 testval = mask = RI_LOWER;
5479 goto do_class;
5480 case NLOWER:
5481 case NLOWER + ADD_NL:
5482 mask = RI_LOWER;
5483 goto do_class;
5484 case UPPER:
5485 case UPPER + ADD_NL:
5486 testval = mask = RI_UPPER;
5487 goto do_class;
5488 case NUPPER:
5489 case NUPPER + ADD_NL:
5490 mask = RI_UPPER;
5491 goto do_class;
5492
5493 case EXACTLY:
5494 {
5495 int cu, cl;
5496
5497 /* This doesn't do a multi-byte character, because a MULTIBYTECODE
5498 * would have been used for it. */
5499 if (ireg_ic)
5500 {
5501 cu = TOUPPER_LOC(*opnd);
5502 cl = TOLOWER_LOC(*opnd);
5503 while (count < maxcount && (*scan == cu || *scan == cl))
5504 {
5505 count++;
5506 scan++;
5507 }
5508 }
5509 else
5510 {
5511 cu = *opnd;
5512 while (count < maxcount && *scan == cu)
5513 {
5514 count++;
5515 scan++;
5516 }
5517 }
5518 break;
5519 }
5520
5521#ifdef FEAT_MBYTE
5522 case MULTIBYTECODE:
5523 {
5524 int i, len, cf = 0;
5525
5526 /* Safety check (just in case 'encoding' was changed since
5527 * compiling the program). */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005528 if ((len = (*mb_ptr2len)(opnd)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 {
5530 if (ireg_ic && enc_utf8)
5531 cf = utf_fold(utf_ptr2char(opnd));
5532 while (count < maxcount)
5533 {
5534 for (i = 0; i < len; ++i)
5535 if (opnd[i] != scan[i])
5536 break;
5537 if (i < len && (!ireg_ic || !enc_utf8
5538 || utf_fold(utf_ptr2char(scan)) != cf))
5539 break;
5540 scan += len;
5541 ++count;
5542 }
5543 }
5544 }
5545 break;
5546#endif
5547
5548 case ANYOF:
5549 case ANYOF + ADD_NL:
5550 testval = TRUE;
5551 /*FALLTHROUGH*/
5552
5553 case ANYBUT:
5554 case ANYBUT + ADD_NL:
5555 while (count < maxcount)
5556 {
5557#ifdef FEAT_MBYTE
5558 int len;
5559#endif
5560 if (*scan == NUL)
5561 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005562 if (!WITH_NL(OP(p)) || reglnum > reg_maxline || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 break;
5564 reg_nextline();
5565 scan = reginput;
5566 if (got_int)
5567 break;
5568 }
5569 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5570 ++scan;
5571#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005572 else if (has_mbyte && (len = (*mb_ptr2len)(scan)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 {
5574 if ((cstrchr(opnd, (*mb_ptr2char)(scan)) == NULL) == testval)
5575 break;
5576 scan += len;
5577 }
5578#endif
5579 else
5580 {
5581 if ((cstrchr(opnd, *scan) == NULL) == testval)
5582 break;
5583 ++scan;
5584 }
5585 ++count;
5586 }
5587 break;
5588
5589 case NEWL:
5590 while (count < maxcount
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005591 && ((*scan == NUL && reglnum <= reg_maxline && !reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 || (*scan == '\n' && reg_line_lbr)))
5593 {
5594 count++;
5595 if (reg_line_lbr)
5596 ADVANCE_REGINPUT();
5597 else
5598 reg_nextline();
5599 scan = reginput;
5600 if (got_int)
5601 break;
5602 }
5603 break;
5604
5605 default: /* Oh dear. Called inappropriately. */
5606 EMSG(_(e_re_corr));
5607#ifdef DEBUG
5608 printf("Called regrepeat with op code %d\n", OP(p));
5609#endif
5610 break;
5611 }
5612
5613 reginput = scan;
5614
5615 return (int)count;
5616}
5617
5618/*
5619 * regnext - dig the "next" pointer out of a node
5620 */
5621 static char_u *
5622regnext(p)
5623 char_u *p;
5624{
5625 int offset;
5626
5627 if (p == JUST_CALC_SIZE)
5628 return NULL;
5629
5630 offset = NEXT(p);
5631 if (offset == 0)
5632 return NULL;
5633
Bram Moolenaar582fd852005-03-28 20:58:01 +00005634 if (OP(p) == BACK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 return p - offset;
5636 else
5637 return p + offset;
5638}
5639
5640/*
5641 * Check the regexp program for its magic number.
5642 * Return TRUE if it's wrong.
5643 */
5644 static int
5645prog_magic_wrong()
5646{
5647 if (UCHARAT(REG_MULTI
5648 ? reg_mmatch->regprog->program
5649 : reg_match->regprog->program) != REGMAGIC)
5650 {
5651 EMSG(_(e_re_corr));
5652 return TRUE;
5653 }
5654 return FALSE;
5655}
5656
5657/*
5658 * Cleanup the subexpressions, if this wasn't done yet.
5659 * This construction is used to clear the subexpressions only when they are
5660 * used (to increase speed).
5661 */
5662 static void
5663cleanup_subexpr()
5664{
5665 if (need_clear_subexpr)
5666 {
5667 if (REG_MULTI)
5668 {
5669 /* Use 0xff to set lnum to -1 */
5670 vim_memset(reg_startpos, 0xff, sizeof(lpos_T) * NSUBEXP);
5671 vim_memset(reg_endpos, 0xff, sizeof(lpos_T) * NSUBEXP);
5672 }
5673 else
5674 {
5675 vim_memset(reg_startp, 0, sizeof(char_u *) * NSUBEXP);
5676 vim_memset(reg_endp, 0, sizeof(char_u *) * NSUBEXP);
5677 }
5678 need_clear_subexpr = FALSE;
5679 }
5680}
5681
5682#ifdef FEAT_SYN_HL
5683 static void
5684cleanup_zsubexpr()
5685{
5686 if (need_clear_zsubexpr)
5687 {
5688 if (REG_MULTI)
5689 {
5690 /* Use 0xff to set lnum to -1 */
5691 vim_memset(reg_startzpos, 0xff, sizeof(lpos_T) * NSUBEXP);
5692 vim_memset(reg_endzpos, 0xff, sizeof(lpos_T) * NSUBEXP);
5693 }
5694 else
5695 {
5696 vim_memset(reg_startzp, 0, sizeof(char_u *) * NSUBEXP);
5697 vim_memset(reg_endzp, 0, sizeof(char_u *) * NSUBEXP);
5698 }
5699 need_clear_zsubexpr = FALSE;
5700 }
5701}
5702#endif
5703
5704/*
5705 * Advance reglnum, regline and reginput to the next line.
5706 */
5707 static void
5708reg_nextline()
5709{
5710 regline = reg_getline(++reglnum);
5711 reginput = regline;
5712 fast_breakcheck();
5713}
5714
5715/*
5716 * Save the input line and position in a regsave_T.
5717 */
5718 static void
Bram Moolenaar582fd852005-03-28 20:58:01 +00005719reg_save(save, gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 regsave_T *save;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005721 garray_T *gap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722{
5723 if (REG_MULTI)
5724 {
5725 save->rs_u.pos.col = (colnr_T)(reginput - regline);
5726 save->rs_u.pos.lnum = reglnum;
5727 }
5728 else
5729 save->rs_u.ptr = reginput;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005730 save->rs_len = gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731}
5732
5733/*
5734 * Restore the input line and position from a regsave_T.
5735 */
5736 static void
Bram Moolenaar582fd852005-03-28 20:58:01 +00005737reg_restore(save, gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 regsave_T *save;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005739 garray_T *gap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740{
5741 if (REG_MULTI)
5742 {
5743 if (reglnum != save->rs_u.pos.lnum)
5744 {
5745 /* only call reg_getline() when the line number changed to save
5746 * a bit of time */
5747 reglnum = save->rs_u.pos.lnum;
5748 regline = reg_getline(reglnum);
5749 }
5750 reginput = regline + save->rs_u.pos.col;
5751 }
5752 else
5753 reginput = save->rs_u.ptr;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005754 gap->ga_len = save->rs_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755}
5756
5757/*
5758 * Return TRUE if current position is equal to saved position.
5759 */
5760 static int
5761reg_save_equal(save)
5762 regsave_T *save;
5763{
5764 if (REG_MULTI)
5765 return reglnum == save->rs_u.pos.lnum
5766 && reginput == regline + save->rs_u.pos.col;
5767 return reginput == save->rs_u.ptr;
5768}
5769
5770/*
5771 * Tentatively set the sub-expression start to the current position (after
5772 * calling regmatch() they will have changed). Need to save the existing
5773 * values for when there is no match.
5774 * Use se_save() to use pointer (save_se_multi()) or position (save_se_one()),
5775 * depending on REG_MULTI.
5776 */
5777 static void
5778save_se_multi(savep, posp)
5779 save_se_T *savep;
5780 lpos_T *posp;
5781{
5782 savep->se_u.pos = *posp;
5783 posp->lnum = reglnum;
5784 posp->col = (colnr_T)(reginput - regline);
5785}
5786
5787 static void
5788save_se_one(savep, pp)
5789 save_se_T *savep;
5790 char_u **pp;
5791{
5792 savep->se_u.ptr = *pp;
5793 *pp = reginput;
5794}
5795
5796/*
5797 * Compare a number with the operand of RE_LNUM, RE_COL or RE_VCOL.
5798 */
5799 static int
5800re_num_cmp(val, scan)
5801 long_u val;
5802 char_u *scan;
5803{
5804 long_u n = OPERAND_MIN(scan);
5805
5806 if (OPERAND_CMP(scan) == '>')
5807 return val > n;
5808 if (OPERAND_CMP(scan) == '<')
5809 return val < n;
5810 return val == n;
5811}
5812
5813
5814#ifdef DEBUG
5815
5816/*
5817 * regdump - dump a regexp onto stdout in vaguely comprehensible form
5818 */
5819 static void
5820regdump(pattern, r)
5821 char_u *pattern;
5822 regprog_T *r;
5823{
5824 char_u *s;
5825 int op = EXACTLY; /* Arbitrary non-END op. */
5826 char_u *next;
5827 char_u *end = NULL;
5828
5829 printf("\r\nregcomp(%s):\r\n", pattern);
5830
5831 s = r->program + 1;
5832 /*
5833 * Loop until we find the END that isn't before a referred next (an END
5834 * can also appear in a NOMATCH operand).
5835 */
5836 while (op != END || s <= end)
5837 {
5838 op = OP(s);
5839 printf("%2d%s", (int)(s - r->program), regprop(s)); /* Where, what. */
5840 next = regnext(s);
5841 if (next == NULL) /* Next ptr. */
5842 printf("(0)");
5843 else
5844 printf("(%d)", (int)((s - r->program) + (next - s)));
5845 if (end < next)
5846 end = next;
5847 if (op == BRACE_LIMITS)
5848 {
5849 /* Two short ints */
5850 printf(" minval %ld, maxval %ld", OPERAND_MIN(s), OPERAND_MAX(s));
5851 s += 8;
5852 }
5853 s += 3;
5854 if (op == ANYOF || op == ANYOF + ADD_NL
5855 || op == ANYBUT || op == ANYBUT + ADD_NL
5856 || op == EXACTLY)
5857 {
5858 /* Literal string, where present. */
5859 while (*s != NUL)
5860 printf("%c", *s++);
5861 s++;
5862 }
5863 printf("\r\n");
5864 }
5865
5866 /* Header fields of interest. */
5867 if (r->regstart != NUL)
5868 printf("start `%s' 0x%x; ", r->regstart < 256
5869 ? (char *)transchar(r->regstart)
5870 : "multibyte", r->regstart);
5871 if (r->reganch)
5872 printf("anchored; ");
5873 if (r->regmust != NULL)
5874 printf("must have \"%s\"", r->regmust);
5875 printf("\r\n");
5876}
5877
5878/*
5879 * regprop - printable representation of opcode
5880 */
5881 static char_u *
5882regprop(op)
5883 char_u *op;
5884{
5885 char_u *p;
5886 static char_u buf[50];
5887
5888 (void) strcpy(buf, ":");
5889
5890 switch (OP(op))
5891 {
5892 case BOL:
5893 p = "BOL";
5894 break;
5895 case EOL:
5896 p = "EOL";
5897 break;
5898 case RE_BOF:
5899 p = "BOF";
5900 break;
5901 case RE_EOF:
5902 p = "EOF";
5903 break;
5904 case CURSOR:
5905 p = "CURSOR";
5906 break;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00005907 case RE_VISUAL:
5908 p = "RE_VISUAL";
5909 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 case RE_LNUM:
5911 p = "RE_LNUM";
5912 break;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00005913 case RE_MARK:
5914 p = "RE_MARK";
5915 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 case RE_COL:
5917 p = "RE_COL";
5918 break;
5919 case RE_VCOL:
5920 p = "RE_VCOL";
5921 break;
5922 case BOW:
5923 p = "BOW";
5924 break;
5925 case EOW:
5926 p = "EOW";
5927 break;
5928 case ANY:
5929 p = "ANY";
5930 break;
5931 case ANY + ADD_NL:
5932 p = "ANY+NL";
5933 break;
5934 case ANYOF:
5935 p = "ANYOF";
5936 break;
5937 case ANYOF + ADD_NL:
5938 p = "ANYOF+NL";
5939 break;
5940 case ANYBUT:
5941 p = "ANYBUT";
5942 break;
5943 case ANYBUT + ADD_NL:
5944 p = "ANYBUT+NL";
5945 break;
5946 case IDENT:
5947 p = "IDENT";
5948 break;
5949 case IDENT + ADD_NL:
5950 p = "IDENT+NL";
5951 break;
5952 case SIDENT:
5953 p = "SIDENT";
5954 break;
5955 case SIDENT + ADD_NL:
5956 p = "SIDENT+NL";
5957 break;
5958 case KWORD:
5959 p = "KWORD";
5960 break;
5961 case KWORD + ADD_NL:
5962 p = "KWORD+NL";
5963 break;
5964 case SKWORD:
5965 p = "SKWORD";
5966 break;
5967 case SKWORD + ADD_NL:
5968 p = "SKWORD+NL";
5969 break;
5970 case FNAME:
5971 p = "FNAME";
5972 break;
5973 case FNAME + ADD_NL:
5974 p = "FNAME+NL";
5975 break;
5976 case SFNAME:
5977 p = "SFNAME";
5978 break;
5979 case SFNAME + ADD_NL:
5980 p = "SFNAME+NL";
5981 break;
5982 case PRINT:
5983 p = "PRINT";
5984 break;
5985 case PRINT + ADD_NL:
5986 p = "PRINT+NL";
5987 break;
5988 case SPRINT:
5989 p = "SPRINT";
5990 break;
5991 case SPRINT + ADD_NL:
5992 p = "SPRINT+NL";
5993 break;
5994 case WHITE:
5995 p = "WHITE";
5996 break;
5997 case WHITE + ADD_NL:
5998 p = "WHITE+NL";
5999 break;
6000 case NWHITE:
6001 p = "NWHITE";
6002 break;
6003 case NWHITE + ADD_NL:
6004 p = "NWHITE+NL";
6005 break;
6006 case DIGIT:
6007 p = "DIGIT";
6008 break;
6009 case DIGIT + ADD_NL:
6010 p = "DIGIT+NL";
6011 break;
6012 case NDIGIT:
6013 p = "NDIGIT";
6014 break;
6015 case NDIGIT + ADD_NL:
6016 p = "NDIGIT+NL";
6017 break;
6018 case HEX:
6019 p = "HEX";
6020 break;
6021 case HEX + ADD_NL:
6022 p = "HEX+NL";
6023 break;
6024 case NHEX:
6025 p = "NHEX";
6026 break;
6027 case NHEX + ADD_NL:
6028 p = "NHEX+NL";
6029 break;
6030 case OCTAL:
6031 p = "OCTAL";
6032 break;
6033 case OCTAL + ADD_NL:
6034 p = "OCTAL+NL";
6035 break;
6036 case NOCTAL:
6037 p = "NOCTAL";
6038 break;
6039 case NOCTAL + ADD_NL:
6040 p = "NOCTAL+NL";
6041 break;
6042 case WORD:
6043 p = "WORD";
6044 break;
6045 case WORD + ADD_NL:
6046 p = "WORD+NL";
6047 break;
6048 case NWORD:
6049 p = "NWORD";
6050 break;
6051 case NWORD + ADD_NL:
6052 p = "NWORD+NL";
6053 break;
6054 case HEAD:
6055 p = "HEAD";
6056 break;
6057 case HEAD + ADD_NL:
6058 p = "HEAD+NL";
6059 break;
6060 case NHEAD:
6061 p = "NHEAD";
6062 break;
6063 case NHEAD + ADD_NL:
6064 p = "NHEAD+NL";
6065 break;
6066 case ALPHA:
6067 p = "ALPHA";
6068 break;
6069 case ALPHA + ADD_NL:
6070 p = "ALPHA+NL";
6071 break;
6072 case NALPHA:
6073 p = "NALPHA";
6074 break;
6075 case NALPHA + ADD_NL:
6076 p = "NALPHA+NL";
6077 break;
6078 case LOWER:
6079 p = "LOWER";
6080 break;
6081 case LOWER + ADD_NL:
6082 p = "LOWER+NL";
6083 break;
6084 case NLOWER:
6085 p = "NLOWER";
6086 break;
6087 case NLOWER + ADD_NL:
6088 p = "NLOWER+NL";
6089 break;
6090 case UPPER:
6091 p = "UPPER";
6092 break;
6093 case UPPER + ADD_NL:
6094 p = "UPPER+NL";
6095 break;
6096 case NUPPER:
6097 p = "NUPPER";
6098 break;
6099 case NUPPER + ADD_NL:
6100 p = "NUPPER+NL";
6101 break;
6102 case BRANCH:
6103 p = "BRANCH";
6104 break;
6105 case EXACTLY:
6106 p = "EXACTLY";
6107 break;
6108 case NOTHING:
6109 p = "NOTHING";
6110 break;
6111 case BACK:
6112 p = "BACK";
6113 break;
6114 case END:
6115 p = "END";
6116 break;
6117 case MOPEN + 0:
6118 p = "MATCH START";
6119 break;
6120 case MOPEN + 1:
6121 case MOPEN + 2:
6122 case MOPEN + 3:
6123 case MOPEN + 4:
6124 case MOPEN + 5:
6125 case MOPEN + 6:
6126 case MOPEN + 7:
6127 case MOPEN + 8:
6128 case MOPEN + 9:
6129 sprintf(buf + STRLEN(buf), "MOPEN%d", OP(op) - MOPEN);
6130 p = NULL;
6131 break;
6132 case MCLOSE + 0:
6133 p = "MATCH END";
6134 break;
6135 case MCLOSE + 1:
6136 case MCLOSE + 2:
6137 case MCLOSE + 3:
6138 case MCLOSE + 4:
6139 case MCLOSE + 5:
6140 case MCLOSE + 6:
6141 case MCLOSE + 7:
6142 case MCLOSE + 8:
6143 case MCLOSE + 9:
6144 sprintf(buf + STRLEN(buf), "MCLOSE%d", OP(op) - MCLOSE);
6145 p = NULL;
6146 break;
6147 case BACKREF + 1:
6148 case BACKREF + 2:
6149 case BACKREF + 3:
6150 case BACKREF + 4:
6151 case BACKREF + 5:
6152 case BACKREF + 6:
6153 case BACKREF + 7:
6154 case BACKREF + 8:
6155 case BACKREF + 9:
6156 sprintf(buf + STRLEN(buf), "BACKREF%d", OP(op) - BACKREF);
6157 p = NULL;
6158 break;
6159 case NOPEN:
6160 p = "NOPEN";
6161 break;
6162 case NCLOSE:
6163 p = "NCLOSE";
6164 break;
6165#ifdef FEAT_SYN_HL
6166 case ZOPEN + 1:
6167 case ZOPEN + 2:
6168 case ZOPEN + 3:
6169 case ZOPEN + 4:
6170 case ZOPEN + 5:
6171 case ZOPEN + 6:
6172 case ZOPEN + 7:
6173 case ZOPEN + 8:
6174 case ZOPEN + 9:
6175 sprintf(buf + STRLEN(buf), "ZOPEN%d", OP(op) - ZOPEN);
6176 p = NULL;
6177 break;
6178 case ZCLOSE + 1:
6179 case ZCLOSE + 2:
6180 case ZCLOSE + 3:
6181 case ZCLOSE + 4:
6182 case ZCLOSE + 5:
6183 case ZCLOSE + 6:
6184 case ZCLOSE + 7:
6185 case ZCLOSE + 8:
6186 case ZCLOSE + 9:
6187 sprintf(buf + STRLEN(buf), "ZCLOSE%d", OP(op) - ZCLOSE);
6188 p = NULL;
6189 break;
6190 case ZREF + 1:
6191 case ZREF + 2:
6192 case ZREF + 3:
6193 case ZREF + 4:
6194 case ZREF + 5:
6195 case ZREF + 6:
6196 case ZREF + 7:
6197 case ZREF + 8:
6198 case ZREF + 9:
6199 sprintf(buf + STRLEN(buf), "ZREF%d", OP(op) - ZREF);
6200 p = NULL;
6201 break;
6202#endif
6203 case STAR:
6204 p = "STAR";
6205 break;
6206 case PLUS:
6207 p = "PLUS";
6208 break;
6209 case NOMATCH:
6210 p = "NOMATCH";
6211 break;
6212 case MATCH:
6213 p = "MATCH";
6214 break;
6215 case BEHIND:
6216 p = "BEHIND";
6217 break;
6218 case NOBEHIND:
6219 p = "NOBEHIND";
6220 break;
6221 case SUBPAT:
6222 p = "SUBPAT";
6223 break;
6224 case BRACE_LIMITS:
6225 p = "BRACE_LIMITS";
6226 break;
6227 case BRACE_SIMPLE:
6228 p = "BRACE_SIMPLE";
6229 break;
6230 case BRACE_COMPLEX + 0:
6231 case BRACE_COMPLEX + 1:
6232 case BRACE_COMPLEX + 2:
6233 case BRACE_COMPLEX + 3:
6234 case BRACE_COMPLEX + 4:
6235 case BRACE_COMPLEX + 5:
6236 case BRACE_COMPLEX + 6:
6237 case BRACE_COMPLEX + 7:
6238 case BRACE_COMPLEX + 8:
6239 case BRACE_COMPLEX + 9:
6240 sprintf(buf + STRLEN(buf), "BRACE_COMPLEX%d", OP(op) - BRACE_COMPLEX);
6241 p = NULL;
6242 break;
6243#ifdef FEAT_MBYTE
6244 case MULTIBYTECODE:
6245 p = "MULTIBYTECODE";
6246 break;
6247#endif
6248 case NEWL:
6249 p = "NEWL";
6250 break;
6251 default:
6252 sprintf(buf + STRLEN(buf), "corrupt %d", OP(op));
6253 p = NULL;
6254 break;
6255 }
6256 if (p != NULL)
6257 (void) strcat(buf, p);
6258 return buf;
6259}
6260#endif
6261
6262#ifdef FEAT_MBYTE
6263static void mb_decompose __ARGS((int c, int *c1, int *c2, int *c3));
6264
6265typedef struct
6266{
6267 int a, b, c;
6268} decomp_T;
6269
6270
6271/* 0xfb20 - 0xfb4f */
Bram Moolenaard6f676d2005-06-01 21:51:55 +00006272static decomp_T decomp_table[0xfb4f-0xfb20+1] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273{
6274 {0x5e2,0,0}, /* 0xfb20 alt ayin */
6275 {0x5d0,0,0}, /* 0xfb21 alt alef */
6276 {0x5d3,0,0}, /* 0xfb22 alt dalet */
6277 {0x5d4,0,0}, /* 0xfb23 alt he */
6278 {0x5db,0,0}, /* 0xfb24 alt kaf */
6279 {0x5dc,0,0}, /* 0xfb25 alt lamed */
6280 {0x5dd,0,0}, /* 0xfb26 alt mem-sofit */
6281 {0x5e8,0,0}, /* 0xfb27 alt resh */
6282 {0x5ea,0,0}, /* 0xfb28 alt tav */
6283 {'+', 0, 0}, /* 0xfb29 alt plus */
6284 {0x5e9, 0x5c1, 0}, /* 0xfb2a shin+shin-dot */
6285 {0x5e9, 0x5c2, 0}, /* 0xfb2b shin+sin-dot */
6286 {0x5e9, 0x5c1, 0x5bc}, /* 0xfb2c shin+shin-dot+dagesh */
6287 {0x5e9, 0x5c2, 0x5bc}, /* 0xfb2d shin+sin-dot+dagesh */
6288 {0x5d0, 0x5b7, 0}, /* 0xfb2e alef+patah */
6289 {0x5d0, 0x5b8, 0}, /* 0xfb2f alef+qamats */
6290 {0x5d0, 0x5b4, 0}, /* 0xfb30 alef+hiriq */
6291 {0x5d1, 0x5bc, 0}, /* 0xfb31 bet+dagesh */
6292 {0x5d2, 0x5bc, 0}, /* 0xfb32 gimel+dagesh */
6293 {0x5d3, 0x5bc, 0}, /* 0xfb33 dalet+dagesh */
6294 {0x5d4, 0x5bc, 0}, /* 0xfb34 he+dagesh */
6295 {0x5d5, 0x5bc, 0}, /* 0xfb35 vav+dagesh */
6296 {0x5d6, 0x5bc, 0}, /* 0xfb36 zayin+dagesh */
6297 {0xfb37, 0, 0}, /* 0xfb37 -- UNUSED */
6298 {0x5d8, 0x5bc, 0}, /* 0xfb38 tet+dagesh */
6299 {0x5d9, 0x5bc, 0}, /* 0xfb39 yud+dagesh */
6300 {0x5da, 0x5bc, 0}, /* 0xfb3a kaf sofit+dagesh */
6301 {0x5db, 0x5bc, 0}, /* 0xfb3b kaf+dagesh */
6302 {0x5dc, 0x5bc, 0}, /* 0xfb3c lamed+dagesh */
6303 {0xfb3d, 0, 0}, /* 0xfb3d -- UNUSED */
6304 {0x5de, 0x5bc, 0}, /* 0xfb3e mem+dagesh */
6305 {0xfb3f, 0, 0}, /* 0xfb3f -- UNUSED */
6306 {0x5e0, 0x5bc, 0}, /* 0xfb40 nun+dagesh */
6307 {0x5e1, 0x5bc, 0}, /* 0xfb41 samech+dagesh */
6308 {0xfb42, 0, 0}, /* 0xfb42 -- UNUSED */
6309 {0x5e3, 0x5bc, 0}, /* 0xfb43 pe sofit+dagesh */
6310 {0x5e4, 0x5bc,0}, /* 0xfb44 pe+dagesh */
6311 {0xfb45, 0, 0}, /* 0xfb45 -- UNUSED */
6312 {0x5e6, 0x5bc, 0}, /* 0xfb46 tsadi+dagesh */
6313 {0x5e7, 0x5bc, 0}, /* 0xfb47 qof+dagesh */
6314 {0x5e8, 0x5bc, 0}, /* 0xfb48 resh+dagesh */
6315 {0x5e9, 0x5bc, 0}, /* 0xfb49 shin+dagesh */
6316 {0x5ea, 0x5bc, 0}, /* 0xfb4a tav+dagesh */
6317 {0x5d5, 0x5b9, 0}, /* 0xfb4b vav+holam */
6318 {0x5d1, 0x5bf, 0}, /* 0xfb4c bet+rafe */
6319 {0x5db, 0x5bf, 0}, /* 0xfb4d kaf+rafe */
6320 {0x5e4, 0x5bf, 0}, /* 0xfb4e pe+rafe */
6321 {0x5d0, 0x5dc, 0} /* 0xfb4f alef-lamed */
6322};
6323
6324 static void
6325mb_decompose(c, c1, c2, c3)
6326 int c, *c1, *c2, *c3;
6327{
6328 decomp_T d;
6329
6330 if (c >= 0x4b20 && c <= 0xfb4f)
6331 {
6332 d = decomp_table[c - 0xfb20];
6333 *c1 = d.a;
6334 *c2 = d.b;
6335 *c3 = d.c;
6336 }
6337 else
6338 {
6339 *c1 = c;
6340 *c2 = *c3 = 0;
6341 }
6342}
6343#endif
6344
6345/*
6346 * Compare two strings, ignore case if ireg_ic set.
6347 * Return 0 if strings match, non-zero otherwise.
6348 * Correct the length "*n" when composing characters are ignored.
6349 */
6350 static int
6351cstrncmp(s1, s2, n)
6352 char_u *s1, *s2;
6353 int *n;
6354{
6355 int result;
6356
6357 if (!ireg_ic)
6358 result = STRNCMP(s1, s2, *n);
6359 else
6360 result = MB_STRNICMP(s1, s2, *n);
6361
6362#ifdef FEAT_MBYTE
6363 /* if it failed and it's utf8 and we want to combineignore: */
6364 if (result != 0 && enc_utf8 && ireg_icombine)
6365 {
6366 char_u *str1, *str2;
6367 int c1, c2, c11, c12;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 int junk;
6369
6370 /* we have to handle the strcmp ourselves, since it is necessary to
6371 * deal with the composing characters by ignoring them: */
6372 str1 = s1;
6373 str2 = s2;
6374 c1 = c2 = 0;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006375 while ((int)(str1 - s1) < *n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 {
6377 c1 = mb_ptr2char_adv(&str1);
6378 c2 = mb_ptr2char_adv(&str2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379
6380 /* decompose the character if necessary, into 'base' characters
6381 * because I don't care about Arabic, I will hard-code the Hebrew
6382 * which I *do* care about! So sue me... */
6383 if (c1 != c2 && (!ireg_ic || utf_fold(c1) != utf_fold(c2)))
6384 {
6385 /* decomposition necessary? */
6386 mb_decompose(c1, &c11, &junk, &junk);
6387 mb_decompose(c2, &c12, &junk, &junk);
6388 c1 = c11;
6389 c2 = c12;
6390 if (c11 != c12 && (!ireg_ic || utf_fold(c11) != utf_fold(c12)))
6391 break;
6392 }
6393 }
6394 result = c2 - c1;
6395 if (result == 0)
6396 *n = (int)(str2 - s2);
6397 }
6398#endif
6399
6400 return result;
6401}
6402
6403/*
6404 * cstrchr: This function is used a lot for simple searches, keep it fast!
6405 */
6406 static char_u *
6407cstrchr(s, c)
6408 char_u *s;
6409 int c;
6410{
6411 char_u *p;
6412 int cc;
6413
6414 if (!ireg_ic
6415#ifdef FEAT_MBYTE
6416 || (!enc_utf8 && mb_char2len(c) > 1)
6417#endif
6418 )
6419 return vim_strchr(s, c);
6420
6421 /* tolower() and toupper() can be slow, comparing twice should be a lot
6422 * faster (esp. when using MS Visual C++!).
6423 * For UTF-8 need to use folded case. */
6424#ifdef FEAT_MBYTE
6425 if (enc_utf8 && c > 0x80)
6426 cc = utf_fold(c);
6427 else
6428#endif
6429 if (isupper(c))
6430 cc = TOLOWER_LOC(c);
6431 else if (islower(c))
6432 cc = TOUPPER_LOC(c);
6433 else
6434 return vim_strchr(s, c);
6435
6436#ifdef FEAT_MBYTE
6437 if (has_mbyte)
6438 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006439 for (p = s; *p != NUL; p += (*mb_ptr2len)(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 {
6441 if (enc_utf8 && c > 0x80)
6442 {
6443 if (utf_fold(utf_ptr2char(p)) == cc)
6444 return p;
6445 }
6446 else if (*p == c || *p == cc)
6447 return p;
6448 }
6449 }
6450 else
6451#endif
6452 /* Faster version for when there are no multi-byte characters. */
6453 for (p = s; *p != NUL; ++p)
6454 if (*p == c || *p == cc)
6455 return p;
6456
6457 return NULL;
6458}
6459
6460/***************************************************************
6461 * regsub stuff *
6462 ***************************************************************/
6463
6464/* This stuff below really confuses cc on an SGI -- webb */
6465#ifdef __sgi
6466# undef __ARGS
6467# define __ARGS(x) ()
6468#endif
6469
6470/*
6471 * We should define ftpr as a pointer to a function returning a pointer to
6472 * a function returning a pointer to a function ...
6473 * This is impossible, so we declare a pointer to a function returning a
6474 * pointer to a function returning void. This should work for all compilers.
6475 */
6476typedef void (*(*fptr) __ARGS((char_u *, int)))();
6477
6478static fptr do_upper __ARGS((char_u *, int));
6479static fptr do_Upper __ARGS((char_u *, int));
6480static fptr do_lower __ARGS((char_u *, int));
6481static fptr do_Lower __ARGS((char_u *, int));
6482
6483static int vim_regsub_both __ARGS((char_u *source, char_u *dest, int copy, int magic, int backslash));
6484
6485 static fptr
6486do_upper(d, c)
6487 char_u *d;
6488 int c;
6489{
6490 *d = TOUPPER_LOC(c);
6491
6492 return (fptr)NULL;
6493}
6494
6495 static fptr
6496do_Upper(d, c)
6497 char_u *d;
6498 int c;
6499{
6500 *d = TOUPPER_LOC(c);
6501
6502 return (fptr)do_Upper;
6503}
6504
6505 static fptr
6506do_lower(d, c)
6507 char_u *d;
6508 int c;
6509{
6510 *d = TOLOWER_LOC(c);
6511
6512 return (fptr)NULL;
6513}
6514
6515 static fptr
6516do_Lower(d, c)
6517 char_u *d;
6518 int c;
6519{
6520 *d = TOLOWER_LOC(c);
6521
6522 return (fptr)do_Lower;
6523}
6524
6525/*
6526 * regtilde(): Replace tildes in the pattern by the old pattern.
6527 *
6528 * Short explanation of the tilde: It stands for the previous replacement
6529 * pattern. If that previous pattern also contains a ~ we should go back a
6530 * step further... But we insert the previous pattern into the current one
6531 * and remember that.
6532 * This still does not handle the case where "magic" changes. TODO?
6533 *
6534 * The tildes are parsed once before the first call to vim_regsub().
6535 */
6536 char_u *
6537regtilde(source, magic)
6538 char_u *source;
6539 int magic;
6540{
6541 char_u *newsub = source;
6542 char_u *tmpsub;
6543 char_u *p;
6544 int len;
6545 int prevlen;
6546
6547 for (p = newsub; *p; ++p)
6548 {
6549 if ((*p == '~' && magic) || (*p == '\\' && *(p + 1) == '~' && !magic))
6550 {
6551 if (reg_prev_sub != NULL)
6552 {
6553 /* length = len(newsub) - 1 + len(prev_sub) + 1 */
6554 prevlen = (int)STRLEN(reg_prev_sub);
6555 tmpsub = alloc((unsigned)(STRLEN(newsub) + prevlen));
6556 if (tmpsub != NULL)
6557 {
6558 /* copy prefix */
6559 len = (int)(p - newsub); /* not including ~ */
6560 mch_memmove(tmpsub, newsub, (size_t)len);
6561 /* interpretate tilde */
6562 mch_memmove(tmpsub + len, reg_prev_sub, (size_t)prevlen);
6563 /* copy postfix */
6564 if (!magic)
6565 ++p; /* back off \ */
6566 STRCPY(tmpsub + len + prevlen, p + 1);
6567
6568 if (newsub != source) /* already allocated newsub */
6569 vim_free(newsub);
6570 newsub = tmpsub;
6571 p = newsub + len + prevlen;
6572 }
6573 }
6574 else if (magic)
6575 STRCPY(p, p + 1); /* remove '~' */
6576 else
6577 STRCPY(p, p + 2); /* remove '\~' */
6578 --p;
6579 }
6580 else
6581 {
6582 if (*p == '\\' && p[1]) /* skip escaped characters */
6583 ++p;
6584#ifdef FEAT_MBYTE
6585 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006586 p += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587#endif
6588 }
6589 }
6590
6591 vim_free(reg_prev_sub);
6592 if (newsub != source) /* newsub was allocated, just keep it */
6593 reg_prev_sub = newsub;
6594 else /* no ~ found, need to save newsub */
6595 reg_prev_sub = vim_strsave(newsub);
6596 return newsub;
6597}
6598
6599#ifdef FEAT_EVAL
6600static int can_f_submatch = FALSE; /* TRUE when submatch() can be used */
6601
6602/* These pointers are used instead of reg_match and reg_mmatch for
6603 * reg_submatch(). Needed for when the substitution string is an expression
6604 * that contains a call to substitute() and submatch(). */
6605static regmatch_T *submatch_match;
6606static regmmatch_T *submatch_mmatch;
6607#endif
6608
6609#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
6610/*
6611 * vim_regsub() - perform substitutions after a vim_regexec() or
6612 * vim_regexec_multi() match.
6613 *
6614 * If "copy" is TRUE really copy into "dest".
6615 * If "copy" is FALSE nothing is copied, this is just to find out the length
6616 * of the result.
6617 *
6618 * If "backslash" is TRUE, a backslash will be removed later, need to double
6619 * them to keep them, and insert a backslash before a CR to avoid it being
6620 * replaced with a line break later.
6621 *
6622 * Note: The matched text must not change between the call of
6623 * vim_regexec()/vim_regexec_multi() and vim_regsub()! It would make the back
6624 * references invalid!
6625 *
6626 * Returns the size of the replacement, including terminating NUL.
6627 */
6628 int
6629vim_regsub(rmp, source, dest, copy, magic, backslash)
6630 regmatch_T *rmp;
6631 char_u *source;
6632 char_u *dest;
6633 int copy;
6634 int magic;
6635 int backslash;
6636{
6637 reg_match = rmp;
6638 reg_mmatch = NULL;
6639 reg_maxline = 0;
6640 return vim_regsub_both(source, dest, copy, magic, backslash);
6641}
6642#endif
6643
6644 int
6645vim_regsub_multi(rmp, lnum, source, dest, copy, magic, backslash)
6646 regmmatch_T *rmp;
6647 linenr_T lnum;
6648 char_u *source;
6649 char_u *dest;
6650 int copy;
6651 int magic;
6652 int backslash;
6653{
6654 reg_match = NULL;
6655 reg_mmatch = rmp;
6656 reg_buf = curbuf; /* always works on the current buffer! */
6657 reg_firstlnum = lnum;
6658 reg_maxline = curbuf->b_ml.ml_line_count - lnum;
6659 return vim_regsub_both(source, dest, copy, magic, backslash);
6660}
6661
6662 static int
6663vim_regsub_both(source, dest, copy, magic, backslash)
6664 char_u *source;
6665 char_u *dest;
6666 int copy;
6667 int magic;
6668 int backslash;
6669{
6670 char_u *src;
6671 char_u *dst;
6672 char_u *s;
6673 int c;
6674 int no = -1;
6675 fptr func = (fptr)NULL;
6676 linenr_T clnum = 0; /* init for GCC */
6677 int len = 0; /* init for GCC */
6678#ifdef FEAT_EVAL
6679 static char_u *eval_result = NULL;
6680#endif
6681#ifdef FEAT_MBYTE
6682 int l;
6683#endif
6684
6685
6686 /* Be paranoid... */
6687 if (source == NULL || dest == NULL)
6688 {
6689 EMSG(_(e_null));
6690 return 0;
6691 }
6692 if (prog_magic_wrong())
6693 return 0;
6694 src = source;
6695 dst = dest;
6696
6697 /*
6698 * When the substitute part starts with "\=" evaluate it as an expression.
6699 */
6700 if (source[0] == '\\' && source[1] == '='
6701#ifdef FEAT_EVAL
6702 && !can_f_submatch /* can't do this recursively */
6703#endif
6704 )
6705 {
6706#ifdef FEAT_EVAL
6707 /* To make sure that the length doesn't change between checking the
6708 * length and copying the string, and to speed up things, the
6709 * resulting string is saved from the call with "copy" == FALSE to the
6710 * call with "copy" == TRUE. */
6711 if (copy)
6712 {
6713 if (eval_result != NULL)
6714 {
6715 STRCPY(dest, eval_result);
6716 dst += STRLEN(eval_result);
6717 vim_free(eval_result);
6718 eval_result = NULL;
6719 }
6720 }
6721 else
6722 {
6723 linenr_T save_reg_maxline;
6724 win_T *save_reg_win;
6725 int save_ireg_ic;
6726
6727 vim_free(eval_result);
6728
6729 /* The expression may contain substitute(), which calls us
6730 * recursively. Make sure submatch() gets the text from the first
6731 * level. Don't need to save "reg_buf", because
6732 * vim_regexec_multi() can't be called recursively. */
6733 submatch_match = reg_match;
6734 submatch_mmatch = reg_mmatch;
6735 save_reg_maxline = reg_maxline;
6736 save_reg_win = reg_win;
6737 save_ireg_ic = ireg_ic;
6738 can_f_submatch = TRUE;
6739
6740 eval_result = eval_to_string(source + 2, NULL);
6741 if (eval_result != NULL)
6742 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006743 for (s = eval_result; *s != NUL; mb_ptr_adv(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 {
6745 /* Change NL to CR, so that it becomes a line break.
6746 * Skip over a backslashed character. */
6747 if (*s == NL)
6748 *s = CAR;
6749 else if (*s == '\\' && s[1] != NUL)
6750 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 }
6752
6753 dst += STRLEN(eval_result);
6754 }
6755
6756 reg_match = submatch_match;
6757 reg_mmatch = submatch_mmatch;
6758 reg_maxline = save_reg_maxline;
6759 reg_win = save_reg_win;
6760 ireg_ic = save_ireg_ic;
6761 can_f_submatch = FALSE;
6762 }
6763#endif
6764 }
6765 else
6766 while ((c = *src++) != NUL)
6767 {
6768 if (c == '&' && magic)
6769 no = 0;
6770 else if (c == '\\' && *src != NUL)
6771 {
6772 if (*src == '&' && !magic)
6773 {
6774 ++src;
6775 no = 0;
6776 }
6777 else if ('0' <= *src && *src <= '9')
6778 {
6779 no = *src++ - '0';
6780 }
6781 else if (vim_strchr((char_u *)"uUlLeE", *src))
6782 {
6783 switch (*src++)
6784 {
6785 case 'u': func = (fptr)do_upper;
6786 continue;
6787 case 'U': func = (fptr)do_Upper;
6788 continue;
6789 case 'l': func = (fptr)do_lower;
6790 continue;
6791 case 'L': func = (fptr)do_Lower;
6792 continue;
6793 case 'e':
6794 case 'E': func = (fptr)NULL;
6795 continue;
6796 }
6797 }
6798 }
6799 if (no < 0) /* Ordinary character. */
6800 {
6801 if (c == '\\' && *src != NUL)
6802 {
6803 /* Check for abbreviations -- webb */
6804 switch (*src)
6805 {
6806 case 'r': c = CAR; ++src; break;
6807 case 'n': c = NL; ++src; break;
6808 case 't': c = TAB; ++src; break;
6809 /* Oh no! \e already has meaning in subst pat :-( */
6810 /* case 'e': c = ESC; ++src; break; */
6811 case 'b': c = Ctrl_H; ++src; break;
6812
6813 /* If "backslash" is TRUE the backslash will be removed
6814 * later. Used to insert a literal CR. */
6815 default: if (backslash)
6816 {
6817 if (copy)
6818 *dst = '\\';
6819 ++dst;
6820 }
6821 c = *src++;
6822 }
6823 }
6824
6825 /* Write to buffer, if copy is set. */
6826#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006827 if (has_mbyte && (l = (*mb_ptr2len)(src - 1)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 {
6829 /* TODO: should use "func" here. */
6830 if (copy)
6831 mch_memmove(dst, src - 1, l);
6832 dst += l - 1;
6833 src += l - 1;
6834 }
6835 else
6836 {
6837#endif
6838 if (copy)
6839 {
6840 if (func == (fptr)NULL) /* just copy */
6841 *dst = c;
6842 else /* change case */
6843 func = (fptr)(func(dst, c));
6844 /* Turbo C complains without the typecast */
6845 }
6846#ifdef FEAT_MBYTE
6847 }
6848#endif
6849 dst++;
6850 }
6851 else
6852 {
6853 if (REG_MULTI)
6854 {
6855 clnum = reg_mmatch->startpos[no].lnum;
6856 if (clnum < 0 || reg_mmatch->endpos[no].lnum < 0)
6857 s = NULL;
6858 else
6859 {
6860 s = reg_getline(clnum) + reg_mmatch->startpos[no].col;
6861 if (reg_mmatch->endpos[no].lnum == clnum)
6862 len = reg_mmatch->endpos[no].col
6863 - reg_mmatch->startpos[no].col;
6864 else
6865 len = (int)STRLEN(s);
6866 }
6867 }
6868 else
6869 {
6870 s = reg_match->startp[no];
6871 if (reg_match->endp[no] == NULL)
6872 s = NULL;
6873 else
6874 len = (int)(reg_match->endp[no] - s);
6875 }
6876 if (s != NULL)
6877 {
6878 for (;;)
6879 {
6880 if (len == 0)
6881 {
6882 if (REG_MULTI)
6883 {
6884 if (reg_mmatch->endpos[no].lnum == clnum)
6885 break;
6886 if (copy)
6887 *dst = CAR;
6888 ++dst;
6889 s = reg_getline(++clnum);
6890 if (reg_mmatch->endpos[no].lnum == clnum)
6891 len = reg_mmatch->endpos[no].col;
6892 else
6893 len = (int)STRLEN(s);
6894 }
6895 else
6896 break;
6897 }
6898 else if (*s == NUL) /* we hit NUL. */
6899 {
6900 if (copy)
6901 EMSG(_(e_re_damg));
6902 goto exit;
6903 }
6904 else
6905 {
6906 if (backslash && (*s == CAR || *s == '\\'))
6907 {
6908 /*
6909 * Insert a backslash in front of a CR, otherwise
6910 * it will be replaced by a line break.
6911 * Number of backslashes will be halved later,
6912 * double them here.
6913 */
6914 if (copy)
6915 {
6916 dst[0] = '\\';
6917 dst[1] = *s;
6918 }
6919 dst += 2;
6920 }
6921#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006922 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 {
6924 /* TODO: should use "func" here. */
6925 if (copy)
6926 mch_memmove(dst, s, l);
6927 dst += l;
6928 s += l - 1;
6929 len -= l - 1;
6930 }
6931#endif
6932 else
6933 {
6934 if (copy)
6935 {
6936 if (func == (fptr)NULL) /* just copy */
6937 *dst = *s;
6938 else /* change case */
6939 func = (fptr)(func(dst, *s));
6940 /* Turbo C complains without the typecast */
6941 }
6942 ++dst;
6943 }
6944 ++s;
6945 --len;
6946 }
6947 }
6948 }
6949 no = -1;
6950 }
6951 }
6952 if (copy)
6953 *dst = NUL;
6954
6955exit:
6956 return (int)((dst - dest) + 1);
6957}
6958
6959#ifdef FEAT_EVAL
6960/*
6961 * Used for the submatch() function: get the string from tne n'th submatch in
6962 * allocated memory.
6963 * Returns NULL when not in a ":s" command and for a non-existing submatch.
6964 */
6965 char_u *
6966reg_submatch(no)
6967 int no;
6968{
6969 char_u *retval = NULL;
6970 char_u *s;
6971 int len;
6972 int round;
6973 linenr_T lnum;
6974
6975 if (!can_f_submatch)
6976 return NULL;
6977
6978 if (submatch_match == NULL)
6979 {
6980 /*
6981 * First round: compute the length and allocate memory.
6982 * Second round: copy the text.
6983 */
6984 for (round = 1; round <= 2; ++round)
6985 {
6986 lnum = submatch_mmatch->startpos[no].lnum;
6987 if (lnum < 0 || submatch_mmatch->endpos[no].lnum < 0)
6988 return NULL;
6989
6990 s = reg_getline(lnum) + submatch_mmatch->startpos[no].col;
6991 if (s == NULL) /* anti-crash check, cannot happen? */
6992 break;
6993 if (submatch_mmatch->endpos[no].lnum == lnum)
6994 {
6995 /* Within one line: take form start to end col. */
6996 len = submatch_mmatch->endpos[no].col
6997 - submatch_mmatch->startpos[no].col;
6998 if (round == 2)
Bram Moolenaarbbebc852005-07-18 21:47:53 +00006999 vim_strncpy(retval, s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 ++len;
7001 }
7002 else
7003 {
7004 /* Multiple lines: take start line from start col, middle
7005 * lines completely and end line up to end col. */
7006 len = (int)STRLEN(s);
7007 if (round == 2)
7008 {
7009 STRCPY(retval, s);
7010 retval[len] = '\n';
7011 }
7012 ++len;
7013 ++lnum;
7014 while (lnum < submatch_mmatch->endpos[no].lnum)
7015 {
7016 s = reg_getline(lnum++);
7017 if (round == 2)
7018 STRCPY(retval + len, s);
7019 len += (int)STRLEN(s);
7020 if (round == 2)
7021 retval[len] = '\n';
7022 ++len;
7023 }
7024 if (round == 2)
7025 STRNCPY(retval + len, reg_getline(lnum),
7026 submatch_mmatch->endpos[no].col);
7027 len += submatch_mmatch->endpos[no].col;
7028 if (round == 2)
7029 retval[len] = NUL;
7030 ++len;
7031 }
7032
7033 if (round == 1)
7034 {
7035 retval = lalloc((long_u)len, TRUE);
7036 if (s == NULL)
7037 return NULL;
7038 }
7039 }
7040 }
7041 else
7042 {
7043 if (submatch_match->endp[no] == NULL)
7044 retval = NULL;
7045 else
7046 {
7047 s = submatch_match->startp[no];
7048 retval = vim_strnsave(s, (int)(submatch_match->endp[no] - s));
7049 }
7050 }
7051
7052 return retval;
7053}
7054#endif