blob: df5589c28ef8f25ddc3409b7430f316e5526b581 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * Handling of regular expressions: vim_regcomp(), vim_regexec(), vim_regsub()
4 *
5 * NOTICE:
6 *
7 * This is NOT the original regular expression code as written by Henry
8 * Spencer. This code has been modified specifically for use with the VIM
9 * editor, and should not be used separately from Vim. If you want a good
10 * regular expression library, get the original code. The copyright notice
11 * that follows is from the original.
12 *
13 * END NOTICE
14 *
15 * Copyright (c) 1986 by University of Toronto.
16 * Written by Henry Spencer. Not derived from licensed software.
17 *
18 * Permission is granted to anyone to use this software for any
19 * purpose on any computer system, and to redistribute it freely,
20 * subject to the following restrictions:
21 *
22 * 1. The author is not responsible for the consequences of use of
23 * this software, no matter how awful, even if they arise
24 * from defects in it.
25 *
26 * 2. The origin of this software must not be misrepresented, either
27 * by explicit claim or by omission.
28 *
29 * 3. Altered versions must be plainly marked as such, and must not
30 * be misrepresented as being the original software.
31 *
32 * Beware that some of this code is subtly aware of the way operator
33 * precedence is structured in regular expressions. Serious changes in
34 * regular-expression syntax might require a total rethink.
35 *
Bram Moolenaarc0197e22004-09-13 20:26:32 +000036 * Changes have been made by Tony Andrews, Olaf 'Rhialto' Seibert, Robert
37 * Webb, Ciaran McCreesh and Bram Moolenaar.
Bram Moolenaar071d4272004-06-13 20:20:40 +000038 * Named character class support added by Walter Briscoe (1998 Jul 01)
39 */
40
41#include "vim.h"
42
43#undef DEBUG
44
45/*
46 * The "internal use only" fields in regexp.h are present to pass info from
47 * compile to execute that permits the execute phase to run lots faster on
48 * simple cases. They are:
49 *
50 * regstart char that must begin a match; NUL if none obvious; Can be a
51 * multi-byte character.
52 * reganch is the match anchored (at beginning-of-line only)?
53 * regmust string (pointer into program) that match must include, or NULL
54 * regmlen length of regmust string
55 * regflags RF_ values or'ed together
56 *
57 * Regstart and reganch permit very fast decisions on suitable starting points
58 * for a match, cutting down the work a lot. Regmust permits fast rejection
59 * of lines that cannot possibly match. The regmust tests are costly enough
60 * that vim_regcomp() supplies a regmust only if the r.e. contains something
61 * potentially expensive (at present, the only such thing detected is * or +
62 * at the start of the r.e., which can involve a lot of backup). Regmlen is
63 * supplied because the test in vim_regexec() needs it and vim_regcomp() is
64 * computing it anyway.
65 */
66
67/*
68 * Structure for regexp "program". This is essentially a linear encoding
69 * of a nondeterministic finite-state machine (aka syntax charts or
70 * "railroad normal form" in parsing technology). Each node is an opcode
71 * plus a "next" pointer, possibly plus an operand. "Next" pointers of
72 * all nodes except BRANCH and BRACES_COMPLEX implement concatenation; a "next"
73 * pointer with a BRANCH on both ends of it is connecting two alternatives.
74 * (Here we have one of the subtle syntax dependencies: an individual BRANCH
75 * (as opposed to a collection of them) is never concatenated with anything
76 * because of operator precedence). The "next" pointer of a BRACES_COMPLEX
Bram Moolenaardf177f62005-02-22 08:39:57 +000077 * node points to the node after the stuff to be repeated.
78 * The operand of some types of node is a literal string; for others, it is a
79 * node leading into a sub-FSM. In particular, the operand of a BRANCH node
80 * is the first node of the branch.
81 * (NB this is *not* a tree structure: the tail of the branch connects to the
82 * thing following the set of BRANCHes.)
Bram Moolenaar071d4272004-06-13 20:20:40 +000083 *
84 * pattern is coded like:
85 *
86 * +-----------------+
87 * | V
88 * <aa>\|<bb> BRANCH <aa> BRANCH <bb> --> END
89 * | ^ | ^
90 * +------+ +----------+
91 *
92 *
93 * +------------------+
94 * V |
95 * <aa>* BRANCH BRANCH <aa> --> BACK BRANCH --> NOTHING --> END
96 * | | ^ ^
97 * | +---------------+ |
98 * +---------------------------------------------+
99 *
100 *
Bram Moolenaardf177f62005-02-22 08:39:57 +0000101 * +----------------------+
102 * V |
Bram Moolenaar582fd852005-03-28 20:58:01 +0000103 * <aa>\+ BRANCH <aa> --> BRANCH --> BACK BRANCH --> NOTHING --> END
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000104 * | | ^ ^
105 * | +-----------+ |
106 * +--------------------------------------------------+
Bram Moolenaardf177f62005-02-22 08:39:57 +0000107 *
108 *
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109 * +-------------------------+
110 * V |
111 * <aa>\{} BRANCH BRACE_LIMITS --> BRACE_COMPLEX <aa> --> BACK END
112 * | | ^
113 * | +----------------+
114 * +-----------------------------------------------+
115 *
116 *
117 * <aa>\@!<bb> BRANCH NOMATCH <aa> --> END <bb> --> END
118 * | | ^ ^
119 * | +----------------+ |
120 * +--------------------------------+
121 *
122 * +---------+
123 * | V
124 * \z[abc] BRANCH BRANCH a BRANCH b BRANCH c BRANCH NOTHING --> END
125 * | | | | ^ ^
126 * | | | +-----+ |
127 * | | +----------------+ |
128 * | +---------------------------+ |
129 * +------------------------------------------------------+
130 *
131 * They all start with a BRANCH for "\|" alternaties, even when there is only
132 * one alternative.
133 */
134
135/*
136 * The opcodes are:
137 */
138
139/* definition number opnd? meaning */
140#define END 0 /* End of program or NOMATCH operand. */
141#define BOL 1 /* Match "" at beginning of line. */
142#define EOL 2 /* Match "" at end of line. */
143#define BRANCH 3 /* node Match this alternative, or the
144 * next... */
145#define BACK 4 /* Match "", "next" ptr points backward. */
146#define EXACTLY 5 /* str Match this string. */
147#define NOTHING 6 /* Match empty string. */
148#define STAR 7 /* node Match this (simple) thing 0 or more
149 * times. */
150#define PLUS 8 /* node Match this (simple) thing 1 or more
151 * times. */
152#define MATCH 9 /* node match the operand zero-width */
153#define NOMATCH 10 /* node check for no match with operand */
154#define BEHIND 11 /* node look behind for a match with operand */
155#define NOBEHIND 12 /* node look behind for no match with operand */
156#define SUBPAT 13 /* node match the operand here */
157#define BRACE_SIMPLE 14 /* node Match this (simple) thing between m and
158 * n times (\{m,n\}). */
159#define BOW 15 /* Match "" after [^a-zA-Z0-9_] */
160#define EOW 16 /* Match "" at [^a-zA-Z0-9_] */
161#define BRACE_LIMITS 17 /* nr nr define the min & max for BRACE_SIMPLE
162 * and BRACE_COMPLEX. */
163#define NEWL 18 /* Match line-break */
164#define BHPOS 19 /* End position for BEHIND or NOBEHIND */
165
166
167/* character classes: 20-48 normal, 50-78 include a line-break */
168#define ADD_NL 30
169#define FIRST_NL ANY + ADD_NL
170#define ANY 20 /* Match any one character. */
171#define ANYOF 21 /* str Match any character in this string. */
172#define ANYBUT 22 /* str Match any character not in this
173 * string. */
174#define IDENT 23 /* Match identifier char */
175#define SIDENT 24 /* Match identifier char but no digit */
176#define KWORD 25 /* Match keyword char */
177#define SKWORD 26 /* Match word char but no digit */
178#define FNAME 27 /* Match file name char */
179#define SFNAME 28 /* Match file name char but no digit */
180#define PRINT 29 /* Match printable char */
181#define SPRINT 30 /* Match printable char but no digit */
182#define WHITE 31 /* Match whitespace char */
183#define NWHITE 32 /* Match non-whitespace char */
184#define DIGIT 33 /* Match digit char */
185#define NDIGIT 34 /* Match non-digit char */
186#define HEX 35 /* Match hex char */
187#define NHEX 36 /* Match non-hex char */
188#define OCTAL 37 /* Match octal char */
189#define NOCTAL 38 /* Match non-octal char */
190#define WORD 39 /* Match word char */
191#define NWORD 40 /* Match non-word char */
192#define HEAD 41 /* Match head char */
193#define NHEAD 42 /* Match non-head char */
194#define ALPHA 43 /* Match alpha char */
195#define NALPHA 44 /* Match non-alpha char */
196#define LOWER 45 /* Match lowercase char */
197#define NLOWER 46 /* Match non-lowercase char */
198#define UPPER 47 /* Match uppercase char */
199#define NUPPER 48 /* Match non-uppercase char */
200#define LAST_NL NUPPER + ADD_NL
201#define WITH_NL(op) ((op) >= FIRST_NL && (op) <= LAST_NL)
202
203#define MOPEN 80 /* -89 Mark this point in input as start of
204 * \( subexpr. MOPEN + 0 marks start of
205 * match. */
206#define MCLOSE 90 /* -99 Analogous to MOPEN. MCLOSE + 0 marks
207 * end of match. */
208#define BACKREF 100 /* -109 node Match same string again \1-\9 */
209
210#ifdef FEAT_SYN_HL
211# define ZOPEN 110 /* -119 Mark this point in input as start of
212 * \z( subexpr. */
213# define ZCLOSE 120 /* -129 Analogous to ZOPEN. */
214# define ZREF 130 /* -139 node Match external submatch \z1-\z9 */
215#endif
216
217#define BRACE_COMPLEX 140 /* -149 node Match nodes between m & n times */
218
219#define NOPEN 150 /* Mark this point in input as start of
220 \%( subexpr. */
221#define NCLOSE 151 /* Analogous to NOPEN. */
222
223#define MULTIBYTECODE 200 /* mbc Match one multi-byte character */
224#define RE_BOF 201 /* Match "" at beginning of file. */
225#define RE_EOF 202 /* Match "" at end of file. */
226#define CURSOR 203 /* Match location of cursor. */
227
228#define RE_LNUM 204 /* nr cmp Match line number */
229#define RE_COL 205 /* nr cmp Match column number */
230#define RE_VCOL 206 /* nr cmp Match virtual column number */
231
Bram Moolenaar71fe80d2006-01-22 23:25:56 +0000232#define RE_MARK 207 /* mark cmp Match mark position */
233#define RE_VISUAL 208 /* Match Visual area */
234
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235/*
236 * Magic characters have a special meaning, they don't match literally.
237 * Magic characters are negative. This separates them from literal characters
238 * (possibly multi-byte). Only ASCII characters can be Magic.
239 */
240#define Magic(x) ((int)(x) - 256)
241#define un_Magic(x) ((x) + 256)
242#define is_Magic(x) ((x) < 0)
243
244static int no_Magic __ARGS((int x));
245static int toggle_Magic __ARGS((int x));
246
247 static int
248no_Magic(x)
249 int x;
250{
251 if (is_Magic(x))
252 return un_Magic(x);
253 return x;
254}
255
256 static int
257toggle_Magic(x)
258 int x;
259{
260 if (is_Magic(x))
261 return un_Magic(x);
262 return Magic(x);
263}
264
265/*
266 * The first byte of the regexp internal "program" is actually this magic
267 * number; the start node begins in the second byte. It's used to catch the
268 * most severe mutilation of the program by the caller.
269 */
270
271#define REGMAGIC 0234
272
273/*
274 * Opcode notes:
275 *
276 * BRANCH The set of branches constituting a single choice are hooked
277 * together with their "next" pointers, since precedence prevents
278 * anything being concatenated to any individual branch. The
279 * "next" pointer of the last BRANCH in a choice points to the
280 * thing following the whole choice. This is also where the
281 * final "next" pointer of each individual branch points; each
282 * branch starts with the operand node of a BRANCH node.
283 *
284 * BACK Normal "next" pointers all implicitly point forward; BACK
285 * exists to make loop structures possible.
286 *
287 * STAR,PLUS '=', and complex '*' and '+', are implemented as circular
288 * BRANCH structures using BACK. Simple cases (one character
289 * per match) are implemented with STAR and PLUS for speed
290 * and to minimize recursive plunges.
291 *
292 * BRACE_LIMITS This is always followed by a BRACE_SIMPLE or BRACE_COMPLEX
293 * node, and defines the min and max limits to be used for that
294 * node.
295 *
296 * MOPEN,MCLOSE ...are numbered at compile time.
297 * ZOPEN,ZCLOSE ...ditto
298 */
299
300/*
301 * A node is one char of opcode followed by two chars of "next" pointer.
302 * "Next" pointers are stored as two 8-bit bytes, high order first. The
303 * value is a positive offset from the opcode of the node containing it.
304 * An operand, if any, simply follows the node. (Note that much of the
305 * code generation knows about this implicit relationship.)
306 *
307 * Using two bytes for the "next" pointer is vast overkill for most things,
308 * but allows patterns to get big without disasters.
309 */
310#define OP(p) ((int)*(p))
311#define NEXT(p) (((*((p) + 1) & 0377) << 8) + (*((p) + 2) & 0377))
312#define OPERAND(p) ((p) + 3)
313/* Obtain an operand that was stored as four bytes, MSB first. */
314#define OPERAND_MIN(p) (((long)(p)[3] << 24) + ((long)(p)[4] << 16) \
315 + ((long)(p)[5] << 8) + (long)(p)[6])
316/* Obtain a second operand stored as four bytes. */
317#define OPERAND_MAX(p) OPERAND_MIN((p) + 4)
318/* Obtain a second single-byte operand stored after a four bytes operand. */
319#define OPERAND_CMP(p) (p)[7]
320
321/*
322 * Utility definitions.
323 */
324#define UCHARAT(p) ((int)*(char_u *)(p))
325
326/* Used for an error (down from) vim_regcomp(): give the error message, set
327 * rc_did_emsg and return NULL */
Bram Moolenaar98692072006-02-04 00:57:42 +0000328#define EMSG_RET_NULL(m) return (EMSG(m), rc_did_emsg = TRUE, (void *)NULL)
329#define EMSG_M_RET_NULL(m, c) return (EMSG2((m), (c) ? "" : "\\"), rc_did_emsg = TRUE, (void *)NULL)
Bram Moolenaar45eeb132005-06-06 21:59:07 +0000330#define EMSG_RET_FAIL(m) return (EMSG(m), rc_did_emsg = TRUE, FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331#define EMSG_ONE_RET_NULL EMSG_M_RET_NULL(_("E369: invalid item in %s%%[]"), reg_magic == MAGIC_ALL)
332
333#define MAX_LIMIT (32767L << 16L)
334
335static int re_multi_type __ARGS((int));
336static int cstrncmp __ARGS((char_u *s1, char_u *s2, int *n));
337static char_u *cstrchr __ARGS((char_u *, int));
338
339#ifdef DEBUG
340static void regdump __ARGS((char_u *, regprog_T *));
341static char_u *regprop __ARGS((char_u *));
342#endif
343
344#define NOT_MULTI 0
345#define MULTI_ONE 1
346#define MULTI_MULT 2
347/*
348 * Return NOT_MULTI if c is not a "multi" operator.
349 * Return MULTI_ONE if c is a single "multi" operator.
350 * Return MULTI_MULT if c is a multi "multi" operator.
351 */
352 static int
353re_multi_type(c)
354 int c;
355{
356 if (c == Magic('@') || c == Magic('=') || c == Magic('?'))
357 return MULTI_ONE;
358 if (c == Magic('*') || c == Magic('+') || c == Magic('{'))
359 return MULTI_MULT;
360 return NOT_MULTI;
361}
362
363/*
364 * Flags to be passed up and down.
365 */
366#define HASWIDTH 0x1 /* Known never to match null string. */
367#define SIMPLE 0x2 /* Simple enough to be STAR/PLUS operand. */
368#define SPSTART 0x4 /* Starts with * or +. */
369#define HASNL 0x8 /* Contains some \n. */
370#define HASLOOKBH 0x10 /* Contains "\@<=" or "\@<!". */
371#define WORST 0 /* Worst case. */
372
373/*
374 * When regcode is set to this value, code is not emitted and size is computed
375 * instead.
376 */
377#define JUST_CALC_SIZE ((char_u *) -1)
378
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000379static char_u *reg_prev_sub = NULL;
380
381#if defined(EXITFREE) || defined(PROTO)
382 void
383free_regexp_stuff()
384{
385 vim_free(reg_prev_sub);
386}
387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388
389/*
390 * REGEXP_INRANGE contains all characters which are always special in a []
391 * range after '\'.
392 * REGEXP_ABBR contains all characters which act as abbreviations after '\'.
393 * These are:
394 * \n - New line (NL).
395 * \r - Carriage Return (CR).
396 * \t - Tab (TAB).
397 * \e - Escape (ESC).
398 * \b - Backspace (Ctrl_H).
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000399 * \d - Character code in decimal, eg \d123
400 * \o - Character code in octal, eg \o80
401 * \x - Character code in hex, eg \x4a
402 * \u - Multibyte character code, eg \u20ac
403 * \U - Long multibyte character code, eg \U12345678
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 */
405static char_u REGEXP_INRANGE[] = "]^-n\\";
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000406static char_u REGEXP_ABBR[] = "nrtebdoxuU";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407
408static int backslash_trans __ARGS((int c));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000409static int get_char_class __ARGS((char_u **pp));
410static int get_equi_class __ARGS((char_u **pp));
411static void reg_equi_class __ARGS((int c));
412static int get_coll_element __ARGS((char_u **pp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413static char_u *skip_anyof __ARGS((char_u *p));
414static void init_class_tab __ARGS((void));
415
416/*
417 * Translate '\x' to its control character, except "\n", which is Magic.
418 */
419 static int
420backslash_trans(c)
421 int c;
422{
423 switch (c)
424 {
425 case 'r': return CAR;
426 case 't': return TAB;
427 case 'e': return ESC;
428 case 'b': return BS;
429 }
430 return c;
431}
432
433/*
Bram Moolenaardf177f62005-02-22 08:39:57 +0000434 * Check for a character class name "[:name:]". "pp" points to the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 * Returns one of the CLASS_ items. CLASS_NONE means that no item was
436 * recognized. Otherwise "pp" is advanced to after the item.
437 */
438 static int
Bram Moolenaardf177f62005-02-22 08:39:57 +0000439get_char_class(pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 char_u **pp;
441{
442 static const char *(class_names[]) =
443 {
444 "alnum:]",
445#define CLASS_ALNUM 0
446 "alpha:]",
447#define CLASS_ALPHA 1
448 "blank:]",
449#define CLASS_BLANK 2
450 "cntrl:]",
451#define CLASS_CNTRL 3
452 "digit:]",
453#define CLASS_DIGIT 4
454 "graph:]",
455#define CLASS_GRAPH 5
456 "lower:]",
457#define CLASS_LOWER 6
458 "print:]",
459#define CLASS_PRINT 7
460 "punct:]",
461#define CLASS_PUNCT 8
462 "space:]",
463#define CLASS_SPACE 9
464 "upper:]",
465#define CLASS_UPPER 10
466 "xdigit:]",
467#define CLASS_XDIGIT 11
468 "tab:]",
469#define CLASS_TAB 12
470 "return:]",
471#define CLASS_RETURN 13
472 "backspace:]",
473#define CLASS_BACKSPACE 14
474 "escape:]",
475#define CLASS_ESCAPE 15
476 };
477#define CLASS_NONE 99
478 int i;
479
480 if ((*pp)[1] == ':')
481 {
482 for (i = 0; i < sizeof(class_names) / sizeof(*class_names); ++i)
483 if (STRNCMP(*pp + 2, class_names[i], STRLEN(class_names[i])) == 0)
484 {
485 *pp += STRLEN(class_names[i]) + 2;
486 return i;
487 }
488 }
489 return CLASS_NONE;
490}
491
492/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 * Specific version of character class functions.
494 * Using a table to keep this fast.
495 */
496static short class_tab[256];
497
498#define RI_DIGIT 0x01
499#define RI_HEX 0x02
500#define RI_OCTAL 0x04
501#define RI_WORD 0x08
502#define RI_HEAD 0x10
503#define RI_ALPHA 0x20
504#define RI_LOWER 0x40
505#define RI_UPPER 0x80
506#define RI_WHITE 0x100
507
508 static void
509init_class_tab()
510{
511 int i;
512 static int done = FALSE;
513
514 if (done)
515 return;
516
517 for (i = 0; i < 256; ++i)
518 {
519 if (i >= '0' && i <= '7')
520 class_tab[i] = RI_DIGIT + RI_HEX + RI_OCTAL + RI_WORD;
521 else if (i >= '8' && i <= '9')
522 class_tab[i] = RI_DIGIT + RI_HEX + RI_WORD;
523 else if (i >= 'a' && i <= 'f')
524 class_tab[i] = RI_HEX + RI_WORD + RI_HEAD + RI_ALPHA + RI_LOWER;
525#ifdef EBCDIC
526 else if ((i >= 'g' && i <= 'i') || (i >= 'j' && i <= 'r')
527 || (i >= 's' && i <= 'z'))
528#else
529 else if (i >= 'g' && i <= 'z')
530#endif
531 class_tab[i] = RI_WORD + RI_HEAD + RI_ALPHA + RI_LOWER;
532 else if (i >= 'A' && i <= 'F')
533 class_tab[i] = RI_HEX + RI_WORD + RI_HEAD + RI_ALPHA + RI_UPPER;
534#ifdef EBCDIC
535 else if ((i >= 'G' && i <= 'I') || ( i >= 'J' && i <= 'R')
536 || (i >= 'S' && i <= 'Z'))
537#else
538 else if (i >= 'G' && i <= 'Z')
539#endif
540 class_tab[i] = RI_WORD + RI_HEAD + RI_ALPHA + RI_UPPER;
541 else if (i == '_')
542 class_tab[i] = RI_WORD + RI_HEAD;
543 else
544 class_tab[i] = 0;
545 }
546 class_tab[' '] |= RI_WHITE;
547 class_tab['\t'] |= RI_WHITE;
548 done = TRUE;
549}
550
551#ifdef FEAT_MBYTE
552# define ri_digit(c) (c < 0x100 && (class_tab[c] & RI_DIGIT))
553# define ri_hex(c) (c < 0x100 && (class_tab[c] & RI_HEX))
554# define ri_octal(c) (c < 0x100 && (class_tab[c] & RI_OCTAL))
555# define ri_word(c) (c < 0x100 && (class_tab[c] & RI_WORD))
556# define ri_head(c) (c < 0x100 && (class_tab[c] & RI_HEAD))
557# define ri_alpha(c) (c < 0x100 && (class_tab[c] & RI_ALPHA))
558# define ri_lower(c) (c < 0x100 && (class_tab[c] & RI_LOWER))
559# define ri_upper(c) (c < 0x100 && (class_tab[c] & RI_UPPER))
560# define ri_white(c) (c < 0x100 && (class_tab[c] & RI_WHITE))
561#else
562# define ri_digit(c) (class_tab[c] & RI_DIGIT)
563# define ri_hex(c) (class_tab[c] & RI_HEX)
564# define ri_octal(c) (class_tab[c] & RI_OCTAL)
565# define ri_word(c) (class_tab[c] & RI_WORD)
566# define ri_head(c) (class_tab[c] & RI_HEAD)
567# define ri_alpha(c) (class_tab[c] & RI_ALPHA)
568# define ri_lower(c) (class_tab[c] & RI_LOWER)
569# define ri_upper(c) (class_tab[c] & RI_UPPER)
570# define ri_white(c) (class_tab[c] & RI_WHITE)
571#endif
572
573/* flags for regflags */
574#define RF_ICASE 1 /* ignore case */
575#define RF_NOICASE 2 /* don't ignore case */
576#define RF_HASNL 4 /* can match a NL */
577#define RF_ICOMBINE 8 /* ignore combining characters */
578#define RF_LOOKBH 16 /* uses "\@<=" or "\@<!" */
579
580/*
581 * Global work variables for vim_regcomp().
582 */
583
584static char_u *regparse; /* Input-scan pointer. */
585static int prevchr_len; /* byte length of previous char */
586static int num_complex_braces; /* Complex \{...} count */
587static int regnpar; /* () count. */
588#ifdef FEAT_SYN_HL
589static int regnzpar; /* \z() count. */
590static int re_has_z; /* \z item detected */
591#endif
592static char_u *regcode; /* Code-emit pointer, or JUST_CALC_SIZE */
593static long regsize; /* Code size. */
594static char_u had_endbrace[NSUBEXP]; /* flags, TRUE if end of () found */
595static unsigned regflags; /* RF_ flags for prog */
596static long brace_min[10]; /* Minimums for complex brace repeats */
597static long brace_max[10]; /* Maximums for complex brace repeats */
598static int brace_count[10]; /* Current counts for complex brace repeats */
599#if defined(FEAT_SYN_HL) || defined(PROTO)
600static int had_eol; /* TRUE when EOL found by vim_regcomp() */
601#endif
602static int one_exactly = FALSE; /* only do one char for EXACTLY */
603
604static int reg_magic; /* magicness of the pattern: */
605#define MAGIC_NONE 1 /* "\V" very unmagic */
606#define MAGIC_OFF 2 /* "\M" or 'magic' off */
607#define MAGIC_ON 3 /* "\m" or 'magic' */
608#define MAGIC_ALL 4 /* "\v" very magic */
609
610static int reg_string; /* matching with a string instead of a buffer
611 line */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000612static int reg_strict; /* "[abc" is illegal */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613
614/*
615 * META contains all characters that may be magic, except '^' and '$'.
616 */
617
618#ifdef EBCDIC
619static char_u META[] = "%&()*+.123456789<=>?@ACDFHIKLMOPSUVWX[_acdfhiklmnopsuvwxz{|~";
620#else
621/* META[] is used often enough to justify turning it into a table. */
622static char_u META_flags[] = {
623 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
624 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
625/* % & ( ) * + . */
626 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0,
627/* 1 2 3 4 5 6 7 8 9 < = > ? */
628 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1,
629/* @ A C D F H I K L M O */
630 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1,
631/* P S U V W X Z [ _ */
632 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1,
633/* a c d f h i k l m n o */
634 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1,
635/* p s u v w x z { | ~ */
636 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1
637};
638#endif
639
640static int curchr;
641
642/* arguments for reg() */
643#define REG_NOPAREN 0 /* toplevel reg() */
644#define REG_PAREN 1 /* \(\) */
645#define REG_ZPAREN 2 /* \z(\) */
646#define REG_NPAREN 3 /* \%(\) */
647
648/*
649 * Forward declarations for vim_regcomp()'s friends.
650 */
651static void initchr __ARGS((char_u *));
652static int getchr __ARGS((void));
653static void skipchr_keepstart __ARGS((void));
654static int peekchr __ARGS((void));
655static void skipchr __ARGS((void));
656static void ungetchr __ARGS((void));
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000657static int gethexchrs __ARGS((int maxinputlen));
658static int getoctchrs __ARGS((void));
659static int getdecchrs __ARGS((void));
660static int coll_get_char __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661static void regcomp_start __ARGS((char_u *expr, int flags));
662static char_u *reg __ARGS((int, int *));
663static char_u *regbranch __ARGS((int *flagp));
664static char_u *regconcat __ARGS((int *flagp));
665static char_u *regpiece __ARGS((int *));
666static char_u *regatom __ARGS((int *));
667static char_u *regnode __ARGS((int));
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 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003864 if (lt(curbuf->b_visual.vi_start, curbuf->b_visual.vi_end))
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003865 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003866 top = curbuf->b_visual.vi_start;
3867 bot = curbuf->b_visual.vi_end;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003868 }
3869 else
3870 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003871 top = curbuf->b_visual.vi_end;
3872 bot = curbuf->b_visual.vi_start;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003873 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003874 mode = curbuf->b_visual.vi_mode;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00003875 }
3876 lnum = reglnum + reg_firstlnum;
3877 col = (colnr_T)(reginput - regline);
3878 if (lnum < top.lnum || lnum > bot.lnum)
3879 status = RA_NOMATCH;
3880 else if (mode == 'v')
3881 {
3882 if ((lnum == top.lnum && col < top.col)
3883 || (lnum == bot.lnum
3884 && col >= bot.col + (*p_sel != 'e')))
3885 status = RA_NOMATCH;
3886 }
3887 else if (mode == Ctrl_V)
3888 {
3889 colnr_T start, end;
3890 colnr_T start2, end2;
3891 colnr_T col;
3892
3893 getvvcol(wp, &top, &start, NULL, &end);
3894 getvvcol(wp, &bot, &start2, NULL, &end2);
3895 if (start2 < start)
3896 start = start2;
3897 if (end2 > end)
3898 end = end2;
3899 if (top.col == MAXCOL || bot.col == MAXCOL)
3900 end = MAXCOL;
3901 col = win_linetabsize(wp,
3902 regline, (colnr_T)(reginput - regline));
3903 if (col < start || col > end - (*p_sel == 'e'))
3904 status = RA_NOMATCH;
3905 }
3906 }
3907#else
3908 status = RA_NOMATCH;
3909#endif
3910 break;
3911
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 case RE_LNUM:
3913 if (!REG_MULTI || !re_num_cmp((long_u)(reglnum + reg_firstlnum),
3914 scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003915 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 break;
3917
3918 case RE_COL:
3919 if (!re_num_cmp((long_u)(reginput - regline) + 1, scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003920 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 break;
3922
3923 case RE_VCOL:
3924 if (!re_num_cmp((long_u)win_linetabsize(
3925 reg_win == NULL ? curwin : reg_win,
3926 regline, (colnr_T)(reginput - regline)) + 1, scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003927 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 break;
3929
3930 case BOW: /* \<word; reginput points to w */
3931 if (c == NUL) /* Can't match at end of line */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003932 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933#ifdef FEAT_MBYTE
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003934 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 {
3936 int this_class;
3937
3938 /* Get class of current and previous char (if it exists). */
3939 this_class = mb_get_class(reginput);
3940 if (this_class <= 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003941 status = RA_NOMATCH; /* not on a word at all */
3942 else if (reg_prev_class() == this_class)
3943 status = RA_NOMATCH; /* previous char is in same word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 }
3945#endif
3946 else
3947 {
3948 if (!vim_iswordc(c)
3949 || (reginput > regline && vim_iswordc(reginput[-1])))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003950 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 }
3952 break;
3953
3954 case EOW: /* word\>; reginput points after d */
3955 if (reginput == regline) /* Can't match at start of line */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003956 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957#ifdef FEAT_MBYTE
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003958 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 {
3960 int this_class, prev_class;
3961
3962 /* Get class of current and previous char (if it exists). */
3963 this_class = mb_get_class(reginput);
3964 prev_class = reg_prev_class();
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003965 if (this_class == prev_class
3966 || prev_class == 0 || prev_class == 1)
3967 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003970 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003972 if (!vim_iswordc(reginput[-1])
3973 || (reginput[0] != NUL && vim_iswordc(c)))
3974 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 }
3976 break; /* Matched with EOW */
3977
3978 case ANY:
3979 if (c == NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003980 status = RA_NOMATCH;
3981 else
3982 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 break;
3984
3985 case IDENT:
3986 if (!vim_isIDc(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003987 status = RA_NOMATCH;
3988 else
3989 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 break;
3991
3992 case SIDENT:
3993 if (VIM_ISDIGIT(*reginput) || !vim_isIDc(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003994 status = RA_NOMATCH;
3995 else
3996 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 break;
3998
3999 case KWORD:
4000 if (!vim_iswordp(reginput))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004001 status = RA_NOMATCH;
4002 else
4003 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 break;
4005
4006 case SKWORD:
4007 if (VIM_ISDIGIT(*reginput) || !vim_iswordp(reginput))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004008 status = RA_NOMATCH;
4009 else
4010 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 break;
4012
4013 case FNAME:
4014 if (!vim_isfilec(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004015 status = RA_NOMATCH;
4016 else
4017 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 break;
4019
4020 case SFNAME:
4021 if (VIM_ISDIGIT(*reginput) || !vim_isfilec(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004022 status = RA_NOMATCH;
4023 else
4024 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 break;
4026
4027 case PRINT:
4028 if (ptr2cells(reginput) != 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004029 status = RA_NOMATCH;
4030 else
4031 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 break;
4033
4034 case SPRINT:
4035 if (VIM_ISDIGIT(*reginput) || ptr2cells(reginput) != 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004036 status = RA_NOMATCH;
4037 else
4038 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 break;
4040
4041 case WHITE:
4042 if (!vim_iswhite(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004043 status = RA_NOMATCH;
4044 else
4045 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 break;
4047
4048 case NWHITE:
4049 if (c == NUL || vim_iswhite(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004050 status = RA_NOMATCH;
4051 else
4052 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 break;
4054
4055 case DIGIT:
4056 if (!ri_digit(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004057 status = RA_NOMATCH;
4058 else
4059 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 break;
4061
4062 case NDIGIT:
4063 if (c == NUL || ri_digit(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004064 status = RA_NOMATCH;
4065 else
4066 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 break;
4068
4069 case HEX:
4070 if (!ri_hex(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004071 status = RA_NOMATCH;
4072 else
4073 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 break;
4075
4076 case NHEX:
4077 if (c == NUL || ri_hex(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004078 status = RA_NOMATCH;
4079 else
4080 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 break;
4082
4083 case OCTAL:
4084 if (!ri_octal(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004085 status = RA_NOMATCH;
4086 else
4087 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 break;
4089
4090 case NOCTAL:
4091 if (c == NUL || ri_octal(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004092 status = RA_NOMATCH;
4093 else
4094 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 break;
4096
4097 case WORD:
4098 if (!ri_word(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004099 status = RA_NOMATCH;
4100 else
4101 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 break;
4103
4104 case NWORD:
4105 if (c == NUL || ri_word(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004106 status = RA_NOMATCH;
4107 else
4108 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 break;
4110
4111 case HEAD:
4112 if (!ri_head(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004113 status = RA_NOMATCH;
4114 else
4115 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 break;
4117
4118 case NHEAD:
4119 if (c == NUL || ri_head(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004120 status = RA_NOMATCH;
4121 else
4122 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 break;
4124
4125 case ALPHA:
4126 if (!ri_alpha(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004127 status = RA_NOMATCH;
4128 else
4129 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 break;
4131
4132 case NALPHA:
4133 if (c == NUL || ri_alpha(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004134 status = RA_NOMATCH;
4135 else
4136 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 break;
4138
4139 case LOWER:
4140 if (!ri_lower(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004141 status = RA_NOMATCH;
4142 else
4143 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 break;
4145
4146 case NLOWER:
4147 if (c == NUL || ri_lower(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004148 status = RA_NOMATCH;
4149 else
4150 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 break;
4152
4153 case UPPER:
4154 if (!ri_upper(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004155 status = RA_NOMATCH;
4156 else
4157 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 break;
4159
4160 case NUPPER:
4161 if (c == NUL || ri_upper(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004162 status = RA_NOMATCH;
4163 else
4164 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 break;
4166
4167 case EXACTLY:
4168 {
4169 int len;
4170 char_u *opnd;
4171
4172 opnd = OPERAND(scan);
4173 /* Inline the first byte, for speed. */
4174 if (*opnd != *reginput
4175 && (!ireg_ic || (
4176#ifdef FEAT_MBYTE
4177 !enc_utf8 &&
4178#endif
4179 TOLOWER_LOC(*opnd) != TOLOWER_LOC(*reginput))))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004180 status = RA_NOMATCH;
4181 else if (*opnd == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 {
4183 /* match empty string always works; happens when "~" is
4184 * empty. */
4185 }
4186 else if (opnd[1] == NUL
4187#ifdef FEAT_MBYTE
4188 && !(enc_utf8 && ireg_ic)
4189#endif
4190 )
4191 ++reginput; /* matched a single char */
4192 else
4193 {
4194 len = (int)STRLEN(opnd);
4195 /* Need to match first byte again for multi-byte. */
4196 if (cstrncmp(opnd, reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004197 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198#ifdef FEAT_MBYTE
4199 /* Check for following composing character. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004200 else if (enc_utf8
4201 && UTF_COMPOSINGLIKE(reginput, reginput + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 {
4203 /* raaron: This code makes a composing character get
4204 * ignored, which is the correct behavior (sometimes)
4205 * for voweled Hebrew texts. */
4206 if (!ireg_icombine)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004207 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004210 else
4211 reginput += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 }
4213 }
4214 break;
4215
4216 case ANYOF:
4217 case ANYBUT:
4218 if (c == NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004219 status = RA_NOMATCH;
4220 else if ((cstrchr(OPERAND(scan), c) == NULL) == (op == ANYOF))
4221 status = RA_NOMATCH;
4222 else
4223 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 break;
4225
4226#ifdef FEAT_MBYTE
4227 case MULTIBYTECODE:
4228 if (has_mbyte)
4229 {
4230 int i, len;
4231 char_u *opnd;
4232
4233 opnd = OPERAND(scan);
4234 /* Safety check (just in case 'encoding' was changed since
4235 * compiling the program). */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004236 if ((len = (*mb_ptr2len)(opnd)) < 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004237 {
4238 status = RA_NOMATCH;
4239 break;
4240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 for (i = 0; i < len; ++i)
4242 if (opnd[i] != reginput[i])
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004243 {
4244 status = RA_NOMATCH;
4245 break;
4246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 reginput += len;
4248 }
4249 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004250 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 break;
4252#endif
4253
4254 case NOTHING:
4255 break;
4256
4257 case BACK:
Bram Moolenaar582fd852005-03-28 20:58:01 +00004258 {
4259 int i;
4260 backpos_T *bp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261
Bram Moolenaar582fd852005-03-28 20:58:01 +00004262 /*
4263 * When we run into BACK we need to check if we don't keep
4264 * looping without matching any input. The second and later
4265 * times a BACK is encountered it fails if the input is still
4266 * at the same position as the previous time.
4267 * The positions are stored in "backpos" and found by the
4268 * current value of "scan", the position in the RE program.
4269 */
4270 bp = (backpos_T *)backpos.ga_data;
4271 for (i = 0; i < backpos.ga_len; ++i)
4272 if (bp[i].bp_scan == scan)
4273 break;
4274 if (i == backpos.ga_len)
4275 {
4276 /* First time at this BACK, make room to store the pos. */
4277 if (ga_grow(&backpos, 1) == FAIL)
4278 status = RA_FAIL;
4279 else
4280 {
4281 /* get "ga_data" again, it may have changed */
4282 bp = (backpos_T *)backpos.ga_data;
4283 bp[i].bp_scan = scan;
4284 ++backpos.ga_len;
4285 }
4286 }
4287 else if (reg_save_equal(&bp[i].bp_pos))
4288 /* Still at same position as last time, fail. */
4289 status = RA_NOMATCH;
4290
4291 if (status != RA_FAIL && status != RA_NOMATCH)
4292 reg_save(&bp[i].bp_pos, &backpos);
4293 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004294 break;
4295
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 case MOPEN + 0: /* Match start: \zs */
4297 case MOPEN + 1: /* \( */
4298 case MOPEN + 2:
4299 case MOPEN + 3:
4300 case MOPEN + 4:
4301 case MOPEN + 5:
4302 case MOPEN + 6:
4303 case MOPEN + 7:
4304 case MOPEN + 8:
4305 case MOPEN + 9:
4306 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 no = op - MOPEN;
4308 cleanup_subexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004309 rp = regstack_push(RS_MOPEN, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004310 if (rp == NULL)
4311 status = RA_FAIL;
4312 else
4313 {
4314 rp->rs_no = no;
4315 save_se(&rp->rs_un.sesave, &reg_startpos[no],
4316 &reg_startp[no]);
4317 /* We simply continue and handle the result when done. */
4318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004320 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321
4322 case NOPEN: /* \%( */
4323 case NCLOSE: /* \) after \%( */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004324 if (regstack_push(RS_NOPEN, scan) == NULL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004325 status = RA_FAIL;
4326 /* We simply continue and handle the result when done. */
4327 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328
4329#ifdef FEAT_SYN_HL
4330 case ZOPEN + 1:
4331 case ZOPEN + 2:
4332 case ZOPEN + 3:
4333 case ZOPEN + 4:
4334 case ZOPEN + 5:
4335 case ZOPEN + 6:
4336 case ZOPEN + 7:
4337 case ZOPEN + 8:
4338 case ZOPEN + 9:
4339 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 no = op - ZOPEN;
4341 cleanup_zsubexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004342 rp = regstack_push(RS_ZOPEN, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004343 if (rp == NULL)
4344 status = RA_FAIL;
4345 else
4346 {
4347 rp->rs_no = no;
4348 save_se(&rp->rs_un.sesave, &reg_startzpos[no],
4349 &reg_startzp[no]);
4350 /* We simply continue and handle the result when done. */
4351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004353 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354#endif
4355
4356 case MCLOSE + 0: /* Match end: \ze */
4357 case MCLOSE + 1: /* \) */
4358 case MCLOSE + 2:
4359 case MCLOSE + 3:
4360 case MCLOSE + 4:
4361 case MCLOSE + 5:
4362 case MCLOSE + 6:
4363 case MCLOSE + 7:
4364 case MCLOSE + 8:
4365 case MCLOSE + 9:
4366 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 no = op - MCLOSE;
4368 cleanup_subexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004369 rp = regstack_push(RS_MCLOSE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004370 if (rp == NULL)
4371 status = RA_FAIL;
4372 else
4373 {
4374 rp->rs_no = no;
4375 save_se(&rp->rs_un.sesave, &reg_endpos[no], &reg_endp[no]);
4376 /* We simply continue and handle the result when done. */
4377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004379 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380
4381#ifdef FEAT_SYN_HL
4382 case ZCLOSE + 1: /* \) after \z( */
4383 case ZCLOSE + 2:
4384 case ZCLOSE + 3:
4385 case ZCLOSE + 4:
4386 case ZCLOSE + 5:
4387 case ZCLOSE + 6:
4388 case ZCLOSE + 7:
4389 case ZCLOSE + 8:
4390 case ZCLOSE + 9:
4391 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 no = op - ZCLOSE;
4393 cleanup_zsubexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004394 rp = regstack_push(RS_ZCLOSE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004395 if (rp == NULL)
4396 status = RA_FAIL;
4397 else
4398 {
4399 rp->rs_no = no;
4400 save_se(&rp->rs_un.sesave, &reg_endzpos[no],
4401 &reg_endzp[no]);
4402 /* We simply continue and handle the result when done. */
4403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004405 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406#endif
4407
4408 case BACKREF + 1:
4409 case BACKREF + 2:
4410 case BACKREF + 3:
4411 case BACKREF + 4:
4412 case BACKREF + 5:
4413 case BACKREF + 6:
4414 case BACKREF + 7:
4415 case BACKREF + 8:
4416 case BACKREF + 9:
4417 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 int len;
4419 linenr_T clnum;
4420 colnr_T ccol;
4421 char_u *p;
4422
4423 no = op - BACKREF;
4424 cleanup_subexpr();
4425 if (!REG_MULTI) /* Single-line regexp */
4426 {
4427 if (reg_endp[no] == NULL)
4428 {
4429 /* Backref was not set: Match an empty string. */
4430 len = 0;
4431 }
4432 else
4433 {
4434 /* Compare current input with back-ref in the same
4435 * line. */
4436 len = (int)(reg_endp[no] - reg_startp[no]);
4437 if (cstrncmp(reg_startp[no], reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004438 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 }
4440 }
4441 else /* Multi-line regexp */
4442 {
4443 if (reg_endpos[no].lnum < 0)
4444 {
4445 /* Backref was not set: Match an empty string. */
4446 len = 0;
4447 }
4448 else
4449 {
4450 if (reg_startpos[no].lnum == reglnum
4451 && reg_endpos[no].lnum == reglnum)
4452 {
4453 /* Compare back-ref within the current line. */
4454 len = reg_endpos[no].col - reg_startpos[no].col;
4455 if (cstrncmp(regline + reg_startpos[no].col,
4456 reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004457 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 }
4459 else
4460 {
4461 /* Messy situation: Need to compare between two
4462 * lines. */
4463 ccol = reg_startpos[no].col;
4464 clnum = reg_startpos[no].lnum;
4465 for (;;)
4466 {
4467 /* Since getting one line may invalidate
4468 * the other, need to make copy. Slow! */
4469 if (regline != reg_tofree)
4470 {
4471 len = (int)STRLEN(regline);
4472 if (reg_tofree == NULL
4473 || len >= (int)reg_tofreelen)
4474 {
4475 len += 50; /* get some extra */
4476 vim_free(reg_tofree);
4477 reg_tofree = alloc(len);
4478 if (reg_tofree == NULL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004479 {
4480 status = RA_FAIL; /* outof memory!*/
4481 break;
4482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 reg_tofreelen = len;
4484 }
4485 STRCPY(reg_tofree, regline);
4486 reginput = reg_tofree
4487 + (reginput - regline);
4488 regline = reg_tofree;
4489 }
4490
4491 /* Get the line to compare with. */
4492 p = reg_getline(clnum);
4493 if (clnum == reg_endpos[no].lnum)
4494 len = reg_endpos[no].col - ccol;
4495 else
4496 len = (int)STRLEN(p + ccol);
4497
4498 if (cstrncmp(p + ccol, reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004499 {
4500 status = RA_NOMATCH; /* doesn't match */
4501 break;
4502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 if (clnum == reg_endpos[no].lnum)
4504 break; /* match and at end! */
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004505 if (reglnum >= reg_maxline)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004506 {
4507 status = RA_NOMATCH; /* text too short */
4508 break;
4509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510
4511 /* Advance to next line. */
4512 reg_nextline();
4513 ++clnum;
4514 ccol = 0;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004515 if (got_int)
4516 {
4517 status = RA_FAIL;
4518 break;
4519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 }
4521
4522 /* found a match! Note that regline may now point
4523 * to a copy of the line, that should not matter. */
4524 }
4525 }
4526 }
4527
4528 /* Matched the backref, skip over it. */
4529 reginput += len;
4530 }
4531 break;
4532
4533#ifdef FEAT_SYN_HL
4534 case ZREF + 1:
4535 case ZREF + 2:
4536 case ZREF + 3:
4537 case ZREF + 4:
4538 case ZREF + 5:
4539 case ZREF + 6:
4540 case ZREF + 7:
4541 case ZREF + 8:
4542 case ZREF + 9:
4543 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 int len;
4545
4546 cleanup_zsubexpr();
4547 no = op - ZREF;
4548 if (re_extmatch_in != NULL
4549 && re_extmatch_in->matches[no] != NULL)
4550 {
4551 len = (int)STRLEN(re_extmatch_in->matches[no]);
4552 if (cstrncmp(re_extmatch_in->matches[no],
4553 reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004554 status = RA_NOMATCH;
4555 else
4556 reginput += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 }
4558 else
4559 {
4560 /* Backref was not set: Match an empty string. */
4561 }
4562 }
4563 break;
4564#endif
4565
4566 case BRANCH:
4567 {
4568 if (OP(next) != BRANCH) /* No choice. */
4569 next = OPERAND(scan); /* Avoid recursion. */
4570 else
4571 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004572 rp = regstack_push(RS_BRANCH, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004573 if (rp == NULL)
4574 status = RA_FAIL;
4575 else
4576 status = RA_BREAK; /* rest is below */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 }
4578 }
4579 break;
4580
4581 case BRACE_LIMITS:
4582 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 if (OP(next) == BRACE_SIMPLE)
4584 {
4585 bl_minval = OPERAND_MIN(scan);
4586 bl_maxval = OPERAND_MAX(scan);
4587 }
4588 else if (OP(next) >= BRACE_COMPLEX
4589 && OP(next) < BRACE_COMPLEX + 10)
4590 {
4591 no = OP(next) - BRACE_COMPLEX;
4592 brace_min[no] = OPERAND_MIN(scan);
4593 brace_max[no] = OPERAND_MAX(scan);
4594 brace_count[no] = 0;
4595 }
4596 else
4597 {
4598 EMSG(_(e_internal)); /* Shouldn't happen */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004599 status = RA_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 }
4601 }
4602 break;
4603
4604 case BRACE_COMPLEX + 0:
4605 case BRACE_COMPLEX + 1:
4606 case BRACE_COMPLEX + 2:
4607 case BRACE_COMPLEX + 3:
4608 case BRACE_COMPLEX + 4:
4609 case BRACE_COMPLEX + 5:
4610 case BRACE_COMPLEX + 6:
4611 case BRACE_COMPLEX + 7:
4612 case BRACE_COMPLEX + 8:
4613 case BRACE_COMPLEX + 9:
4614 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 no = op - BRACE_COMPLEX;
4616 ++brace_count[no];
4617
4618 /* If not matched enough times yet, try one more */
4619 if (brace_count[no] <= (brace_min[no] <= brace_max[no]
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004620 ? brace_min[no] : brace_max[no]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004622 rp = regstack_push(RS_BRCPLX_MORE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004623 if (rp == NULL)
4624 status = RA_FAIL;
4625 else
4626 {
4627 rp->rs_no = no;
Bram Moolenaar582fd852005-03-28 20:58:01 +00004628 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004629 next = OPERAND(scan);
4630 /* We continue and handle the result when done. */
4631 }
4632 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 }
4634
4635 /* If matched enough times, may try matching some more */
4636 if (brace_min[no] <= brace_max[no])
4637 {
4638 /* Range is the normal way around, use longest match */
4639 if (brace_count[no] <= brace_max[no])
4640 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004641 rp = regstack_push(RS_BRCPLX_LONG, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004642 if (rp == NULL)
4643 status = RA_FAIL;
4644 else
4645 {
4646 rp->rs_no = no;
Bram Moolenaar582fd852005-03-28 20:58:01 +00004647 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004648 next = OPERAND(scan);
4649 /* We continue and handle the result when done. */
4650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 }
4652 }
4653 else
4654 {
4655 /* Range is backwards, use shortest match first */
4656 if (brace_count[no] <= brace_min[no])
4657 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004658 rp = regstack_push(RS_BRCPLX_SHORT, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004659 if (rp == NULL)
4660 status = RA_FAIL;
4661 else
4662 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00004663 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004664 /* We continue and handle the result when done. */
4665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 }
4667 }
4668 }
4669 break;
4670
4671 case BRACE_SIMPLE:
4672 case STAR:
4673 case PLUS:
4674 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004675 regstar_T rst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676
4677 /*
4678 * Lookahead to avoid useless match attempts when we know
4679 * what character comes next.
4680 */
4681 if (OP(next) == EXACTLY)
4682 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004683 rst.nextb = *OPERAND(next);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 if (ireg_ic)
4685 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004686 if (isupper(rst.nextb))
4687 rst.nextb_ic = TOLOWER_LOC(rst.nextb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004689 rst.nextb_ic = TOUPPER_LOC(rst.nextb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 }
4691 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004692 rst.nextb_ic = rst.nextb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 }
4694 else
4695 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004696 rst.nextb = NUL;
4697 rst.nextb_ic = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 }
4699 if (op != BRACE_SIMPLE)
4700 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004701 rst.minval = (op == STAR) ? 0 : 1;
4702 rst.maxval = MAX_LIMIT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 }
4704 else
4705 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004706 rst.minval = bl_minval;
4707 rst.maxval = bl_maxval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 }
4709
4710 /*
4711 * When maxval > minval, try matching as much as possible, up
4712 * to maxval. When maxval < minval, try matching at least the
4713 * minimal number (since the range is backwards, that's also
4714 * maxval!).
4715 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004716 rst.count = regrepeat(OPERAND(scan), rst.maxval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004719 status = RA_FAIL;
4720 break;
4721 }
4722 if (rst.minval <= rst.maxval
4723 ? rst.count >= rst.minval : rst.count >= rst.maxval)
4724 {
4725 /* It could match. Prepare for trying to match what
4726 * follows. The code is below. Parameters are stored in
4727 * a regstar_T on the regstack. */
Bram Moolenaar916b7af2005-03-16 09:52:38 +00004728 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004729 {
4730 EMSG(_(e_maxmempat));
4731 status = RA_FAIL;
4732 }
4733 else if (ga_grow(&regstack, sizeof(regstar_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004734 status = RA_FAIL;
4735 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004737 regstack.ga_len += sizeof(regstar_T);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004738 rp = regstack_push(rst.minval <= rst.maxval
Bram Moolenaar582fd852005-03-28 20:58:01 +00004739 ? RS_STAR_LONG : RS_STAR_SHORT, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004740 if (rp == NULL)
4741 status = RA_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004743 {
4744 *(((regstar_T *)rp) - 1) = rst;
4745 status = RA_BREAK; /* skip the restore bits */
4746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 }
4748 }
4749 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004750 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 }
4753 break;
4754
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004755 case NOMATCH:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 case MATCH:
4757 case SUBPAT:
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004758 rp = regstack_push(RS_NOMATCH, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004759 if (rp == NULL)
4760 status = RA_FAIL;
4761 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004763 rp->rs_no = op;
Bram Moolenaar582fd852005-03-28 20:58:01 +00004764 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004765 next = OPERAND(scan);
4766 /* We continue and handle the result when done. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 }
4768 break;
4769
4770 case BEHIND:
4771 case NOBEHIND:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004772 /* Need a bit of room to store extra positions. */
Bram Moolenaar916b7af2005-03-16 09:52:38 +00004773 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004774 {
4775 EMSG(_(e_maxmempat));
4776 status = RA_FAIL;
4777 }
4778 else if (ga_grow(&regstack, sizeof(regbehind_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004779 status = RA_FAIL;
4780 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004782 regstack.ga_len += sizeof(regbehind_T);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004783 rp = regstack_push(RS_BEHIND1, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004784 if (rp == NULL)
4785 status = RA_FAIL;
4786 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004788 rp->rs_no = op;
Bram Moolenaar582fd852005-03-28 20:58:01 +00004789 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004790 /* First try if what follows matches. If it does then we
4791 * check the behind match by looping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004794 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795
4796 case BHPOS:
4797 if (REG_MULTI)
4798 {
4799 if (behind_pos.rs_u.pos.col != (colnr_T)(reginput - regline)
4800 || behind_pos.rs_u.pos.lnum != reglnum)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004801 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 }
4803 else if (behind_pos.rs_u.ptr != reginput)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004804 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 break;
4806
4807 case NEWL:
Bram Moolenaarae5bce12005-08-15 21:41:48 +00004808 if ((c != NUL || reglnum > reg_maxline || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 && (c != '\n' || !reg_line_lbr))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004810 status = RA_NOMATCH;
4811 else if (reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 ADVANCE_REGINPUT();
4813 else
4814 reg_nextline();
4815 break;
4816
4817 case END:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004818 status = RA_MATCH; /* Success! */
4819 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820
4821 default:
4822 EMSG(_(e_re_corr));
4823#ifdef DEBUG
4824 printf("Illegal op code %d\n", op);
4825#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004826 status = RA_FAIL;
4827 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 }
4829 }
4830
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004831 /* If we can't continue sequentially, break the inner loop. */
4832 if (status != RA_CONT)
4833 break;
4834
4835 /* Continue in inner loop, advance to next item. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 scan = next;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004837
4838 } /* end of inner loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839
4840 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004841 * If there is something on the regstack execute the code for the state.
Bram Moolenaar582fd852005-03-28 20:58:01 +00004842 * If the state is popped then loop and use the older state.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004844 while (regstack.ga_len > 0 && status != RA_FAIL)
4845 {
4846 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len) - 1;
4847 switch (rp->rs_state)
4848 {
4849 case RS_NOPEN:
4850 /* Result is passed on as-is, simply pop the state. */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004851 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004852 break;
4853
4854 case RS_MOPEN:
4855 /* Pop the state. Restore pointers when there is no match. */
4856 if (status == RA_NOMATCH)
4857 restore_se(&rp->rs_un.sesave, &reg_startpos[rp->rs_no],
4858 &reg_startp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004859 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004860 break;
4861
4862#ifdef FEAT_SYN_HL
4863 case RS_ZOPEN:
4864 /* Pop the state. Restore pointers when there is no match. */
4865 if (status == RA_NOMATCH)
4866 restore_se(&rp->rs_un.sesave, &reg_startzpos[rp->rs_no],
4867 &reg_startzp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004868 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004869 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004871
4872 case RS_MCLOSE:
4873 /* Pop the state. Restore pointers when there is no match. */
4874 if (status == RA_NOMATCH)
4875 restore_se(&rp->rs_un.sesave, &reg_endpos[rp->rs_no],
4876 &reg_endp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004877 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004878 break;
4879
4880#ifdef FEAT_SYN_HL
4881 case RS_ZCLOSE:
4882 /* Pop the state. Restore pointers when there is no match. */
4883 if (status == RA_NOMATCH)
4884 restore_se(&rp->rs_un.sesave, &reg_endzpos[rp->rs_no],
4885 &reg_endzp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004886 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004887 break;
4888#endif
4889
4890 case RS_BRANCH:
4891 if (status == RA_MATCH)
4892 /* this branch matched, use it */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004893 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004894 else
4895 {
4896 if (status != RA_BREAK)
4897 {
4898 /* After a non-matching branch: try next one. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00004899 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004900 scan = rp->rs_scan;
4901 }
4902 if (scan == NULL || OP(scan) != BRANCH)
4903 {
4904 /* no more branches, didn't find a match */
4905 status = RA_NOMATCH;
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004906 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004907 }
4908 else
4909 {
4910 /* Prepare to try a branch. */
4911 rp->rs_scan = regnext(scan);
Bram Moolenaar582fd852005-03-28 20:58:01 +00004912 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004913 scan = OPERAND(scan);
4914 }
4915 }
4916 break;
4917
4918 case RS_BRCPLX_MORE:
4919 /* Pop the state. Restore pointers when there is no match. */
4920 if (status == RA_NOMATCH)
4921 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00004922 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004923 --brace_count[rp->rs_no]; /* decrement match count */
4924 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004925 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004926 break;
4927
4928 case RS_BRCPLX_LONG:
4929 /* Pop the state. Restore pointers when there is no match. */
4930 if (status == RA_NOMATCH)
4931 {
4932 /* There was no match, but we did find enough matches. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00004933 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004934 --brace_count[rp->rs_no];
4935 /* continue with the items after "\{}" */
4936 status = RA_CONT;
4937 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004938 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004939 if (status == RA_CONT)
4940 scan = regnext(scan);
4941 break;
4942
4943 case RS_BRCPLX_SHORT:
4944 /* Pop the state. Restore pointers when there is no match. */
4945 if (status == RA_NOMATCH)
4946 /* There was no match, try to match one more item. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00004947 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004948 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004949 if (status == RA_NOMATCH)
4950 {
4951 scan = OPERAND(scan);
4952 status = RA_CONT;
4953 }
4954 break;
4955
4956 case RS_NOMATCH:
4957 /* Pop the state. If the operand matches for NOMATCH or
4958 * doesn't match for MATCH/SUBPAT, we fail. Otherwise backup,
4959 * except for SUBPAT, and continue with the next item. */
4960 if (status == (rp->rs_no == NOMATCH ? RA_MATCH : RA_NOMATCH))
4961 status = RA_NOMATCH;
4962 else
4963 {
4964 status = RA_CONT;
Bram Moolenaar582fd852005-03-28 20:58:01 +00004965 if (rp->rs_no != SUBPAT) /* zero-width */
4966 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004967 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004968 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004969 if (status == RA_CONT)
4970 scan = regnext(scan);
4971 break;
4972
4973 case RS_BEHIND1:
4974 if (status == RA_NOMATCH)
4975 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004976 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004977 regstack.ga_len -= sizeof(regbehind_T);
4978 }
4979 else
4980 {
4981 /* The stuff after BEHIND/NOBEHIND matches. Now try if
4982 * the behind part does (not) match before the current
4983 * position in the input. This must be done at every
4984 * position in the input and checking if the match ends at
4985 * the current position. */
4986
4987 /* save the position after the found match for next */
Bram Moolenaar582fd852005-03-28 20:58:01 +00004988 reg_save(&(((regbehind_T *)rp) - 1)->save_after, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004989
4990 /* start looking for a match with operand at the current
4991 * postion. Go back one character until we find the
4992 * result, hitting the start of the line or the previous
4993 * line (for multi-line matching).
4994 * Set behind_pos to where the match should end, BHPOS
4995 * will match it. Save the current value. */
4996 (((regbehind_T *)rp) - 1)->save_behind = behind_pos;
4997 behind_pos = rp->rs_un.regsave;
4998
4999 rp->rs_state = RS_BEHIND2;
5000
Bram Moolenaar582fd852005-03-28 20:58:01 +00005001 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005002 scan = OPERAND(rp->rs_scan);
5003 }
5004 break;
5005
5006 case RS_BEHIND2:
5007 /*
5008 * Looping for BEHIND / NOBEHIND match.
5009 */
5010 if (status == RA_MATCH && reg_save_equal(&behind_pos))
5011 {
5012 /* found a match that ends where "next" started */
5013 behind_pos = (((regbehind_T *)rp) - 1)->save_behind;
5014 if (rp->rs_no == BEHIND)
Bram Moolenaar582fd852005-03-28 20:58:01 +00005015 reg_restore(&(((regbehind_T *)rp) - 1)->save_after,
5016 &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005017 else
5018 /* But we didn't want a match. */
5019 status = RA_NOMATCH;
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005020 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005021 regstack.ga_len -= sizeof(regbehind_T);
5022 }
5023 else
5024 {
5025 /* No match: Go back one character. May go to previous
5026 * line once. */
5027 no = OK;
5028 if (REG_MULTI)
5029 {
5030 if (rp->rs_un.regsave.rs_u.pos.col == 0)
5031 {
5032 if (rp->rs_un.regsave.rs_u.pos.lnum
5033 < behind_pos.rs_u.pos.lnum
5034 || reg_getline(
5035 --rp->rs_un.regsave.rs_u.pos.lnum)
5036 == NULL)
5037 no = FAIL;
5038 else
5039 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005040 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005041 rp->rs_un.regsave.rs_u.pos.col =
5042 (colnr_T)STRLEN(regline);
5043 }
5044 }
5045 else
5046 --rp->rs_un.regsave.rs_u.pos.col;
5047 }
5048 else
5049 {
5050 if (rp->rs_un.regsave.rs_u.ptr == regline)
5051 no = FAIL;
5052 else
5053 --rp->rs_un.regsave.rs_u.ptr;
5054 }
5055 if (no == OK)
5056 {
5057 /* Advanced, prepare for finding match again. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005058 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005059 scan = OPERAND(rp->rs_scan);
5060 }
5061 else
5062 {
5063 /* Can't advance. For NOBEHIND that's a match. */
5064 behind_pos = (((regbehind_T *)rp) - 1)->save_behind;
5065 if (rp->rs_no == NOBEHIND)
5066 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005067 reg_restore(&(((regbehind_T *)rp) - 1)->save_after,
5068 &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005069 status = RA_MATCH;
5070 }
5071 else
5072 status = RA_NOMATCH;
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005073 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005074 regstack.ga_len -= sizeof(regbehind_T);
5075 }
5076 }
5077 break;
5078
5079 case RS_STAR_LONG:
5080 case RS_STAR_SHORT:
5081 {
5082 regstar_T *rst = ((regstar_T *)rp) - 1;
5083
5084 if (status == RA_MATCH)
5085 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005086 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005087 regstack.ga_len -= sizeof(regstar_T);
5088 break;
5089 }
5090
5091 /* Tried once already, restore input pointers. */
5092 if (status != RA_BREAK)
Bram Moolenaar582fd852005-03-28 20:58:01 +00005093 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005094
5095 /* Repeat until we found a position where it could match. */
5096 for (;;)
5097 {
5098 if (status != RA_BREAK)
5099 {
5100 /* Tried first position already, advance. */
5101 if (rp->rs_state == RS_STAR_LONG)
5102 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005103 /* Trying for longest match, but couldn't or
5104 * didn't match -- back up one char. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005105 if (--rst->count < rst->minval)
5106 break;
5107 if (reginput == regline)
5108 {
5109 /* backup to last char of previous line */
5110 --reglnum;
5111 regline = reg_getline(reglnum);
5112 /* Just in case regrepeat() didn't count
5113 * right. */
5114 if (regline == NULL)
5115 break;
5116 reginput = regline + STRLEN(regline);
5117 fast_breakcheck();
5118 }
5119 else
5120 mb_ptr_back(regline, reginput);
5121 }
5122 else
5123 {
5124 /* Range is backwards, use shortest match first.
5125 * Careful: maxval and minval are exchanged!
5126 * Couldn't or didn't match: try advancing one
5127 * char. */
5128 if (rst->count == rst->minval
5129 || regrepeat(OPERAND(rp->rs_scan), 1L) == 0)
5130 break;
5131 ++rst->count;
5132 }
5133 if (got_int)
5134 break;
5135 }
5136 else
5137 status = RA_NOMATCH;
5138
5139 /* If it could match, try it. */
5140 if (rst->nextb == NUL || *reginput == rst->nextb
5141 || *reginput == rst->nextb_ic)
5142 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005143 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005144 scan = regnext(rp->rs_scan);
5145 status = RA_CONT;
5146 break;
5147 }
5148 }
5149 if (status != RA_CONT)
5150 {
5151 /* Failed. */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005152 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005153 regstack.ga_len -= sizeof(regstar_T);
5154 status = RA_NOMATCH;
5155 }
5156 }
5157 break;
5158 }
5159
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005160 /* If we want to continue the inner loop or didn't pop a state
5161 * continue matching loop */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005162 if (status == RA_CONT || rp == (regitem_T *)
5163 ((char *)regstack.ga_data + regstack.ga_len) - 1)
5164 break;
5165 }
5166
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005167 /* May need to continue with the inner loop, starting at "scan". */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005168 if (status == RA_CONT)
5169 continue;
5170
5171 /*
5172 * If the regstack is empty or something failed we are done.
5173 */
5174 if (regstack.ga_len == 0 || status == RA_FAIL)
5175 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005176 if (scan == NULL)
5177 {
5178 /*
5179 * We get here only if there's trouble -- normally "case END" is
5180 * the terminating point.
5181 */
5182 EMSG(_(e_re_corr));
5183#ifdef DEBUG
5184 printf("Premature EOL\n");
5185#endif
5186 }
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005187 if (status == RA_FAIL)
5188 got_int = TRUE;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005189 return (status == RA_MATCH);
5190 }
5191
5192 } /* End of loop until the regstack is empty. */
5193
5194 /* NOTREACHED */
5195}
5196
5197/*
5198 * Push an item onto the regstack.
5199 * Returns pointer to new item. Returns NULL when out of memory.
5200 */
5201 static regitem_T *
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005202regstack_push(state, scan)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005203 regstate_T state;
5204 char_u *scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005205{
5206 regitem_T *rp;
5207
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005208 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005209 {
5210 EMSG(_(e_maxmempat));
5211 return NULL;
5212 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005213 if (ga_grow(&regstack, sizeof(regitem_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005214 return NULL;
5215
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005216 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005217 rp->rs_state = state;
5218 rp->rs_scan = scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005219
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005220 regstack.ga_len += sizeof(regitem_T);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005221 return rp;
5222}
5223
5224/*
5225 * Pop an item from the regstack.
5226 */
5227 static void
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005228regstack_pop(scan)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005229 char_u **scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005230{
5231 regitem_T *rp;
5232
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005233 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len) - 1;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005234 *scan = rp->rs_scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005235
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005236 regstack.ga_len -= sizeof(regitem_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237}
5238
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239/*
5240 * regrepeat - repeatedly match something simple, return how many.
5241 * Advances reginput (and reglnum) to just after the matched chars.
5242 */
5243 static int
5244regrepeat(p, maxcount)
5245 char_u *p;
5246 long maxcount; /* maximum number of matches allowed */
5247{
5248 long count = 0;
5249 char_u *scan;
5250 char_u *opnd;
5251 int mask;
5252 int testval = 0;
5253
5254 scan = reginput; /* Make local copy of reginput for speed. */
5255 opnd = OPERAND(p);
5256 switch (OP(p))
5257 {
5258 case ANY:
5259 case ANY + ADD_NL:
5260 while (count < maxcount)
5261 {
5262 /* Matching anything means we continue until end-of-line (or
5263 * end-of-file for ANY + ADD_NL), only limited by maxcount. */
5264 while (*scan != NUL && count < maxcount)
5265 {
5266 ++count;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005267 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005269 if (!WITH_NL(OP(p)) || reglnum > reg_maxline || reg_line_lbr
5270 || count == maxcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 break;
5272 ++count; /* count the line-break */
5273 reg_nextline();
5274 scan = reginput;
5275 if (got_int)
5276 break;
5277 }
5278 break;
5279
5280 case IDENT:
5281 case IDENT + ADD_NL:
5282 testval = TRUE;
5283 /*FALLTHROUGH*/
5284 case SIDENT:
5285 case SIDENT + ADD_NL:
5286 while (count < maxcount)
5287 {
5288 if (vim_isIDc(*scan) && (testval || !VIM_ISDIGIT(*scan)))
5289 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005290 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 }
5292 else if (*scan == NUL)
5293 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005294 if (!WITH_NL(OP(p)) || reglnum > reg_maxline || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 break;
5296 reg_nextline();
5297 scan = reginput;
5298 if (got_int)
5299 break;
5300 }
5301 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5302 ++scan;
5303 else
5304 break;
5305 ++count;
5306 }
5307 break;
5308
5309 case KWORD:
5310 case KWORD + ADD_NL:
5311 testval = TRUE;
5312 /*FALLTHROUGH*/
5313 case SKWORD:
5314 case SKWORD + ADD_NL:
5315 while (count < maxcount)
5316 {
5317 if (vim_iswordp(scan) && (testval || !VIM_ISDIGIT(*scan)))
5318 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005319 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 }
5321 else if (*scan == NUL)
5322 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005323 if (!WITH_NL(OP(p)) || reglnum > reg_maxline || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 break;
5325 reg_nextline();
5326 scan = reginput;
5327 if (got_int)
5328 break;
5329 }
5330 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5331 ++scan;
5332 else
5333 break;
5334 ++count;
5335 }
5336 break;
5337
5338 case FNAME:
5339 case FNAME + ADD_NL:
5340 testval = TRUE;
5341 /*FALLTHROUGH*/
5342 case SFNAME:
5343 case SFNAME + ADD_NL:
5344 while (count < maxcount)
5345 {
5346 if (vim_isfilec(*scan) && (testval || !VIM_ISDIGIT(*scan)))
5347 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005348 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 }
5350 else if (*scan == NUL)
5351 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005352 if (!WITH_NL(OP(p)) || reglnum > reg_maxline || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 break;
5354 reg_nextline();
5355 scan = reginput;
5356 if (got_int)
5357 break;
5358 }
5359 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5360 ++scan;
5361 else
5362 break;
5363 ++count;
5364 }
5365 break;
5366
5367 case PRINT:
5368 case PRINT + ADD_NL:
5369 testval = TRUE;
5370 /*FALLTHROUGH*/
5371 case SPRINT:
5372 case SPRINT + ADD_NL:
5373 while (count < maxcount)
5374 {
5375 if (*scan == NUL)
5376 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005377 if (!WITH_NL(OP(p)) || reglnum > reg_maxline || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 break;
5379 reg_nextline();
5380 scan = reginput;
5381 if (got_int)
5382 break;
5383 }
5384 else if (ptr2cells(scan) == 1 && (testval || !VIM_ISDIGIT(*scan)))
5385 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005386 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 }
5388 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5389 ++scan;
5390 else
5391 break;
5392 ++count;
5393 }
5394 break;
5395
5396 case WHITE:
5397 case WHITE + ADD_NL:
5398 testval = mask = RI_WHITE;
5399do_class:
5400 while (count < maxcount)
5401 {
5402#ifdef FEAT_MBYTE
5403 int l;
5404#endif
5405 if (*scan == NUL)
5406 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005407 if (!WITH_NL(OP(p)) || reglnum > reg_maxline || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 break;
5409 reg_nextline();
5410 scan = reginput;
5411 if (got_int)
5412 break;
5413 }
5414#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005415 else if (has_mbyte && (l = (*mb_ptr2len)(scan)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 {
5417 if (testval != 0)
5418 break;
5419 scan += l;
5420 }
5421#endif
5422 else if ((class_tab[*scan] & mask) == testval)
5423 ++scan;
5424 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5425 ++scan;
5426 else
5427 break;
5428 ++count;
5429 }
5430 break;
5431
5432 case NWHITE:
5433 case NWHITE + ADD_NL:
5434 mask = RI_WHITE;
5435 goto do_class;
5436 case DIGIT:
5437 case DIGIT + ADD_NL:
5438 testval = mask = RI_DIGIT;
5439 goto do_class;
5440 case NDIGIT:
5441 case NDIGIT + ADD_NL:
5442 mask = RI_DIGIT;
5443 goto do_class;
5444 case HEX:
5445 case HEX + ADD_NL:
5446 testval = mask = RI_HEX;
5447 goto do_class;
5448 case NHEX:
5449 case NHEX + ADD_NL:
5450 mask = RI_HEX;
5451 goto do_class;
5452 case OCTAL:
5453 case OCTAL + ADD_NL:
5454 testval = mask = RI_OCTAL;
5455 goto do_class;
5456 case NOCTAL:
5457 case NOCTAL + ADD_NL:
5458 mask = RI_OCTAL;
5459 goto do_class;
5460 case WORD:
5461 case WORD + ADD_NL:
5462 testval = mask = RI_WORD;
5463 goto do_class;
5464 case NWORD:
5465 case NWORD + ADD_NL:
5466 mask = RI_WORD;
5467 goto do_class;
5468 case HEAD:
5469 case HEAD + ADD_NL:
5470 testval = mask = RI_HEAD;
5471 goto do_class;
5472 case NHEAD:
5473 case NHEAD + ADD_NL:
5474 mask = RI_HEAD;
5475 goto do_class;
5476 case ALPHA:
5477 case ALPHA + ADD_NL:
5478 testval = mask = RI_ALPHA;
5479 goto do_class;
5480 case NALPHA:
5481 case NALPHA + ADD_NL:
5482 mask = RI_ALPHA;
5483 goto do_class;
5484 case LOWER:
5485 case LOWER + ADD_NL:
5486 testval = mask = RI_LOWER;
5487 goto do_class;
5488 case NLOWER:
5489 case NLOWER + ADD_NL:
5490 mask = RI_LOWER;
5491 goto do_class;
5492 case UPPER:
5493 case UPPER + ADD_NL:
5494 testval = mask = RI_UPPER;
5495 goto do_class;
5496 case NUPPER:
5497 case NUPPER + ADD_NL:
5498 mask = RI_UPPER;
5499 goto do_class;
5500
5501 case EXACTLY:
5502 {
5503 int cu, cl;
5504
5505 /* This doesn't do a multi-byte character, because a MULTIBYTECODE
5506 * would have been used for it. */
5507 if (ireg_ic)
5508 {
5509 cu = TOUPPER_LOC(*opnd);
5510 cl = TOLOWER_LOC(*opnd);
5511 while (count < maxcount && (*scan == cu || *scan == cl))
5512 {
5513 count++;
5514 scan++;
5515 }
5516 }
5517 else
5518 {
5519 cu = *opnd;
5520 while (count < maxcount && *scan == cu)
5521 {
5522 count++;
5523 scan++;
5524 }
5525 }
5526 break;
5527 }
5528
5529#ifdef FEAT_MBYTE
5530 case MULTIBYTECODE:
5531 {
5532 int i, len, cf = 0;
5533
5534 /* Safety check (just in case 'encoding' was changed since
5535 * compiling the program). */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005536 if ((len = (*mb_ptr2len)(opnd)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 {
5538 if (ireg_ic && enc_utf8)
5539 cf = utf_fold(utf_ptr2char(opnd));
5540 while (count < maxcount)
5541 {
5542 for (i = 0; i < len; ++i)
5543 if (opnd[i] != scan[i])
5544 break;
5545 if (i < len && (!ireg_ic || !enc_utf8
5546 || utf_fold(utf_ptr2char(scan)) != cf))
5547 break;
5548 scan += len;
5549 ++count;
5550 }
5551 }
5552 }
5553 break;
5554#endif
5555
5556 case ANYOF:
5557 case ANYOF + ADD_NL:
5558 testval = TRUE;
5559 /*FALLTHROUGH*/
5560
5561 case ANYBUT:
5562 case ANYBUT + ADD_NL:
5563 while (count < maxcount)
5564 {
5565#ifdef FEAT_MBYTE
5566 int len;
5567#endif
5568 if (*scan == NUL)
5569 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005570 if (!WITH_NL(OP(p)) || reglnum > reg_maxline || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 break;
5572 reg_nextline();
5573 scan = reginput;
5574 if (got_int)
5575 break;
5576 }
5577 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5578 ++scan;
5579#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005580 else if (has_mbyte && (len = (*mb_ptr2len)(scan)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 {
5582 if ((cstrchr(opnd, (*mb_ptr2char)(scan)) == NULL) == testval)
5583 break;
5584 scan += len;
5585 }
5586#endif
5587 else
5588 {
5589 if ((cstrchr(opnd, *scan) == NULL) == testval)
5590 break;
5591 ++scan;
5592 }
5593 ++count;
5594 }
5595 break;
5596
5597 case NEWL:
5598 while (count < maxcount
Bram Moolenaarae5bce12005-08-15 21:41:48 +00005599 && ((*scan == NUL && reglnum <= reg_maxline && !reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 || (*scan == '\n' && reg_line_lbr)))
5601 {
5602 count++;
5603 if (reg_line_lbr)
5604 ADVANCE_REGINPUT();
5605 else
5606 reg_nextline();
5607 scan = reginput;
5608 if (got_int)
5609 break;
5610 }
5611 break;
5612
5613 default: /* Oh dear. Called inappropriately. */
5614 EMSG(_(e_re_corr));
5615#ifdef DEBUG
5616 printf("Called regrepeat with op code %d\n", OP(p));
5617#endif
5618 break;
5619 }
5620
5621 reginput = scan;
5622
5623 return (int)count;
5624}
5625
5626/*
5627 * regnext - dig the "next" pointer out of a node
5628 */
5629 static char_u *
5630regnext(p)
5631 char_u *p;
5632{
5633 int offset;
5634
5635 if (p == JUST_CALC_SIZE)
5636 return NULL;
5637
5638 offset = NEXT(p);
5639 if (offset == 0)
5640 return NULL;
5641
Bram Moolenaar582fd852005-03-28 20:58:01 +00005642 if (OP(p) == BACK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 return p - offset;
5644 else
5645 return p + offset;
5646}
5647
5648/*
5649 * Check the regexp program for its magic number.
5650 * Return TRUE if it's wrong.
5651 */
5652 static int
5653prog_magic_wrong()
5654{
5655 if (UCHARAT(REG_MULTI
5656 ? reg_mmatch->regprog->program
5657 : reg_match->regprog->program) != REGMAGIC)
5658 {
5659 EMSG(_(e_re_corr));
5660 return TRUE;
5661 }
5662 return FALSE;
5663}
5664
5665/*
5666 * Cleanup the subexpressions, if this wasn't done yet.
5667 * This construction is used to clear the subexpressions only when they are
5668 * used (to increase speed).
5669 */
5670 static void
5671cleanup_subexpr()
5672{
5673 if (need_clear_subexpr)
5674 {
5675 if (REG_MULTI)
5676 {
5677 /* Use 0xff to set lnum to -1 */
5678 vim_memset(reg_startpos, 0xff, sizeof(lpos_T) * NSUBEXP);
5679 vim_memset(reg_endpos, 0xff, sizeof(lpos_T) * NSUBEXP);
5680 }
5681 else
5682 {
5683 vim_memset(reg_startp, 0, sizeof(char_u *) * NSUBEXP);
5684 vim_memset(reg_endp, 0, sizeof(char_u *) * NSUBEXP);
5685 }
5686 need_clear_subexpr = FALSE;
5687 }
5688}
5689
5690#ifdef FEAT_SYN_HL
5691 static void
5692cleanup_zsubexpr()
5693{
5694 if (need_clear_zsubexpr)
5695 {
5696 if (REG_MULTI)
5697 {
5698 /* Use 0xff to set lnum to -1 */
5699 vim_memset(reg_startzpos, 0xff, sizeof(lpos_T) * NSUBEXP);
5700 vim_memset(reg_endzpos, 0xff, sizeof(lpos_T) * NSUBEXP);
5701 }
5702 else
5703 {
5704 vim_memset(reg_startzp, 0, sizeof(char_u *) * NSUBEXP);
5705 vim_memset(reg_endzp, 0, sizeof(char_u *) * NSUBEXP);
5706 }
5707 need_clear_zsubexpr = FALSE;
5708 }
5709}
5710#endif
5711
5712/*
5713 * Advance reglnum, regline and reginput to the next line.
5714 */
5715 static void
5716reg_nextline()
5717{
5718 regline = reg_getline(++reglnum);
5719 reginput = regline;
5720 fast_breakcheck();
5721}
5722
5723/*
5724 * Save the input line and position in a regsave_T.
5725 */
5726 static void
Bram Moolenaar582fd852005-03-28 20:58:01 +00005727reg_save(save, gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 regsave_T *save;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005729 garray_T *gap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730{
5731 if (REG_MULTI)
5732 {
5733 save->rs_u.pos.col = (colnr_T)(reginput - regline);
5734 save->rs_u.pos.lnum = reglnum;
5735 }
5736 else
5737 save->rs_u.ptr = reginput;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005738 save->rs_len = gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739}
5740
5741/*
5742 * Restore the input line and position from a regsave_T.
5743 */
5744 static void
Bram Moolenaar582fd852005-03-28 20:58:01 +00005745reg_restore(save, gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 regsave_T *save;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005747 garray_T *gap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748{
5749 if (REG_MULTI)
5750 {
5751 if (reglnum != save->rs_u.pos.lnum)
5752 {
5753 /* only call reg_getline() when the line number changed to save
5754 * a bit of time */
5755 reglnum = save->rs_u.pos.lnum;
5756 regline = reg_getline(reglnum);
5757 }
5758 reginput = regline + save->rs_u.pos.col;
5759 }
5760 else
5761 reginput = save->rs_u.ptr;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005762 gap->ga_len = save->rs_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763}
5764
5765/*
5766 * Return TRUE if current position is equal to saved position.
5767 */
5768 static int
5769reg_save_equal(save)
5770 regsave_T *save;
5771{
5772 if (REG_MULTI)
5773 return reglnum == save->rs_u.pos.lnum
5774 && reginput == regline + save->rs_u.pos.col;
5775 return reginput == save->rs_u.ptr;
5776}
5777
5778/*
5779 * Tentatively set the sub-expression start to the current position (after
5780 * calling regmatch() they will have changed). Need to save the existing
5781 * values for when there is no match.
5782 * Use se_save() to use pointer (save_se_multi()) or position (save_se_one()),
5783 * depending on REG_MULTI.
5784 */
5785 static void
5786save_se_multi(savep, posp)
5787 save_se_T *savep;
5788 lpos_T *posp;
5789{
5790 savep->se_u.pos = *posp;
5791 posp->lnum = reglnum;
5792 posp->col = (colnr_T)(reginput - regline);
5793}
5794
5795 static void
5796save_se_one(savep, pp)
5797 save_se_T *savep;
5798 char_u **pp;
5799{
5800 savep->se_u.ptr = *pp;
5801 *pp = reginput;
5802}
5803
5804/*
5805 * Compare a number with the operand of RE_LNUM, RE_COL or RE_VCOL.
5806 */
5807 static int
5808re_num_cmp(val, scan)
5809 long_u val;
5810 char_u *scan;
5811{
5812 long_u n = OPERAND_MIN(scan);
5813
5814 if (OPERAND_CMP(scan) == '>')
5815 return val > n;
5816 if (OPERAND_CMP(scan) == '<')
5817 return val < n;
5818 return val == n;
5819}
5820
5821
5822#ifdef DEBUG
5823
5824/*
5825 * regdump - dump a regexp onto stdout in vaguely comprehensible form
5826 */
5827 static void
5828regdump(pattern, r)
5829 char_u *pattern;
5830 regprog_T *r;
5831{
5832 char_u *s;
5833 int op = EXACTLY; /* Arbitrary non-END op. */
5834 char_u *next;
5835 char_u *end = NULL;
5836
5837 printf("\r\nregcomp(%s):\r\n", pattern);
5838
5839 s = r->program + 1;
5840 /*
5841 * Loop until we find the END that isn't before a referred next (an END
5842 * can also appear in a NOMATCH operand).
5843 */
5844 while (op != END || s <= end)
5845 {
5846 op = OP(s);
5847 printf("%2d%s", (int)(s - r->program), regprop(s)); /* Where, what. */
5848 next = regnext(s);
5849 if (next == NULL) /* Next ptr. */
5850 printf("(0)");
5851 else
5852 printf("(%d)", (int)((s - r->program) + (next - s)));
5853 if (end < next)
5854 end = next;
5855 if (op == BRACE_LIMITS)
5856 {
5857 /* Two short ints */
5858 printf(" minval %ld, maxval %ld", OPERAND_MIN(s), OPERAND_MAX(s));
5859 s += 8;
5860 }
5861 s += 3;
5862 if (op == ANYOF || op == ANYOF + ADD_NL
5863 || op == ANYBUT || op == ANYBUT + ADD_NL
5864 || op == EXACTLY)
5865 {
5866 /* Literal string, where present. */
5867 while (*s != NUL)
5868 printf("%c", *s++);
5869 s++;
5870 }
5871 printf("\r\n");
5872 }
5873
5874 /* Header fields of interest. */
5875 if (r->regstart != NUL)
5876 printf("start `%s' 0x%x; ", r->regstart < 256
5877 ? (char *)transchar(r->regstart)
5878 : "multibyte", r->regstart);
5879 if (r->reganch)
5880 printf("anchored; ");
5881 if (r->regmust != NULL)
5882 printf("must have \"%s\"", r->regmust);
5883 printf("\r\n");
5884}
5885
5886/*
5887 * regprop - printable representation of opcode
5888 */
5889 static char_u *
5890regprop(op)
5891 char_u *op;
5892{
5893 char_u *p;
5894 static char_u buf[50];
5895
5896 (void) strcpy(buf, ":");
5897
5898 switch (OP(op))
5899 {
5900 case BOL:
5901 p = "BOL";
5902 break;
5903 case EOL:
5904 p = "EOL";
5905 break;
5906 case RE_BOF:
5907 p = "BOF";
5908 break;
5909 case RE_EOF:
5910 p = "EOF";
5911 break;
5912 case CURSOR:
5913 p = "CURSOR";
5914 break;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00005915 case RE_VISUAL:
5916 p = "RE_VISUAL";
5917 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 case RE_LNUM:
5919 p = "RE_LNUM";
5920 break;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00005921 case RE_MARK:
5922 p = "RE_MARK";
5923 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 case RE_COL:
5925 p = "RE_COL";
5926 break;
5927 case RE_VCOL:
5928 p = "RE_VCOL";
5929 break;
5930 case BOW:
5931 p = "BOW";
5932 break;
5933 case EOW:
5934 p = "EOW";
5935 break;
5936 case ANY:
5937 p = "ANY";
5938 break;
5939 case ANY + ADD_NL:
5940 p = "ANY+NL";
5941 break;
5942 case ANYOF:
5943 p = "ANYOF";
5944 break;
5945 case ANYOF + ADD_NL:
5946 p = "ANYOF+NL";
5947 break;
5948 case ANYBUT:
5949 p = "ANYBUT";
5950 break;
5951 case ANYBUT + ADD_NL:
5952 p = "ANYBUT+NL";
5953 break;
5954 case IDENT:
5955 p = "IDENT";
5956 break;
5957 case IDENT + ADD_NL:
5958 p = "IDENT+NL";
5959 break;
5960 case SIDENT:
5961 p = "SIDENT";
5962 break;
5963 case SIDENT + ADD_NL:
5964 p = "SIDENT+NL";
5965 break;
5966 case KWORD:
5967 p = "KWORD";
5968 break;
5969 case KWORD + ADD_NL:
5970 p = "KWORD+NL";
5971 break;
5972 case SKWORD:
5973 p = "SKWORD";
5974 break;
5975 case SKWORD + ADD_NL:
5976 p = "SKWORD+NL";
5977 break;
5978 case FNAME:
5979 p = "FNAME";
5980 break;
5981 case FNAME + ADD_NL:
5982 p = "FNAME+NL";
5983 break;
5984 case SFNAME:
5985 p = "SFNAME";
5986 break;
5987 case SFNAME + ADD_NL:
5988 p = "SFNAME+NL";
5989 break;
5990 case PRINT:
5991 p = "PRINT";
5992 break;
5993 case PRINT + ADD_NL:
5994 p = "PRINT+NL";
5995 break;
5996 case SPRINT:
5997 p = "SPRINT";
5998 break;
5999 case SPRINT + ADD_NL:
6000 p = "SPRINT+NL";
6001 break;
6002 case WHITE:
6003 p = "WHITE";
6004 break;
6005 case WHITE + ADD_NL:
6006 p = "WHITE+NL";
6007 break;
6008 case NWHITE:
6009 p = "NWHITE";
6010 break;
6011 case NWHITE + ADD_NL:
6012 p = "NWHITE+NL";
6013 break;
6014 case DIGIT:
6015 p = "DIGIT";
6016 break;
6017 case DIGIT + ADD_NL:
6018 p = "DIGIT+NL";
6019 break;
6020 case NDIGIT:
6021 p = "NDIGIT";
6022 break;
6023 case NDIGIT + ADD_NL:
6024 p = "NDIGIT+NL";
6025 break;
6026 case HEX:
6027 p = "HEX";
6028 break;
6029 case HEX + ADD_NL:
6030 p = "HEX+NL";
6031 break;
6032 case NHEX:
6033 p = "NHEX";
6034 break;
6035 case NHEX + ADD_NL:
6036 p = "NHEX+NL";
6037 break;
6038 case OCTAL:
6039 p = "OCTAL";
6040 break;
6041 case OCTAL + ADD_NL:
6042 p = "OCTAL+NL";
6043 break;
6044 case NOCTAL:
6045 p = "NOCTAL";
6046 break;
6047 case NOCTAL + ADD_NL:
6048 p = "NOCTAL+NL";
6049 break;
6050 case WORD:
6051 p = "WORD";
6052 break;
6053 case WORD + ADD_NL:
6054 p = "WORD+NL";
6055 break;
6056 case NWORD:
6057 p = "NWORD";
6058 break;
6059 case NWORD + ADD_NL:
6060 p = "NWORD+NL";
6061 break;
6062 case HEAD:
6063 p = "HEAD";
6064 break;
6065 case HEAD + ADD_NL:
6066 p = "HEAD+NL";
6067 break;
6068 case NHEAD:
6069 p = "NHEAD";
6070 break;
6071 case NHEAD + ADD_NL:
6072 p = "NHEAD+NL";
6073 break;
6074 case ALPHA:
6075 p = "ALPHA";
6076 break;
6077 case ALPHA + ADD_NL:
6078 p = "ALPHA+NL";
6079 break;
6080 case NALPHA:
6081 p = "NALPHA";
6082 break;
6083 case NALPHA + ADD_NL:
6084 p = "NALPHA+NL";
6085 break;
6086 case LOWER:
6087 p = "LOWER";
6088 break;
6089 case LOWER + ADD_NL:
6090 p = "LOWER+NL";
6091 break;
6092 case NLOWER:
6093 p = "NLOWER";
6094 break;
6095 case NLOWER + ADD_NL:
6096 p = "NLOWER+NL";
6097 break;
6098 case UPPER:
6099 p = "UPPER";
6100 break;
6101 case UPPER + ADD_NL:
6102 p = "UPPER+NL";
6103 break;
6104 case NUPPER:
6105 p = "NUPPER";
6106 break;
6107 case NUPPER + ADD_NL:
6108 p = "NUPPER+NL";
6109 break;
6110 case BRANCH:
6111 p = "BRANCH";
6112 break;
6113 case EXACTLY:
6114 p = "EXACTLY";
6115 break;
6116 case NOTHING:
6117 p = "NOTHING";
6118 break;
6119 case BACK:
6120 p = "BACK";
6121 break;
6122 case END:
6123 p = "END";
6124 break;
6125 case MOPEN + 0:
6126 p = "MATCH START";
6127 break;
6128 case MOPEN + 1:
6129 case MOPEN + 2:
6130 case MOPEN + 3:
6131 case MOPEN + 4:
6132 case MOPEN + 5:
6133 case MOPEN + 6:
6134 case MOPEN + 7:
6135 case MOPEN + 8:
6136 case MOPEN + 9:
6137 sprintf(buf + STRLEN(buf), "MOPEN%d", OP(op) - MOPEN);
6138 p = NULL;
6139 break;
6140 case MCLOSE + 0:
6141 p = "MATCH END";
6142 break;
6143 case MCLOSE + 1:
6144 case MCLOSE + 2:
6145 case MCLOSE + 3:
6146 case MCLOSE + 4:
6147 case MCLOSE + 5:
6148 case MCLOSE + 6:
6149 case MCLOSE + 7:
6150 case MCLOSE + 8:
6151 case MCLOSE + 9:
6152 sprintf(buf + STRLEN(buf), "MCLOSE%d", OP(op) - MCLOSE);
6153 p = NULL;
6154 break;
6155 case BACKREF + 1:
6156 case BACKREF + 2:
6157 case BACKREF + 3:
6158 case BACKREF + 4:
6159 case BACKREF + 5:
6160 case BACKREF + 6:
6161 case BACKREF + 7:
6162 case BACKREF + 8:
6163 case BACKREF + 9:
6164 sprintf(buf + STRLEN(buf), "BACKREF%d", OP(op) - BACKREF);
6165 p = NULL;
6166 break;
6167 case NOPEN:
6168 p = "NOPEN";
6169 break;
6170 case NCLOSE:
6171 p = "NCLOSE";
6172 break;
6173#ifdef FEAT_SYN_HL
6174 case ZOPEN + 1:
6175 case ZOPEN + 2:
6176 case ZOPEN + 3:
6177 case ZOPEN + 4:
6178 case ZOPEN + 5:
6179 case ZOPEN + 6:
6180 case ZOPEN + 7:
6181 case ZOPEN + 8:
6182 case ZOPEN + 9:
6183 sprintf(buf + STRLEN(buf), "ZOPEN%d", OP(op) - ZOPEN);
6184 p = NULL;
6185 break;
6186 case ZCLOSE + 1:
6187 case ZCLOSE + 2:
6188 case ZCLOSE + 3:
6189 case ZCLOSE + 4:
6190 case ZCLOSE + 5:
6191 case ZCLOSE + 6:
6192 case ZCLOSE + 7:
6193 case ZCLOSE + 8:
6194 case ZCLOSE + 9:
6195 sprintf(buf + STRLEN(buf), "ZCLOSE%d", OP(op) - ZCLOSE);
6196 p = NULL;
6197 break;
6198 case ZREF + 1:
6199 case ZREF + 2:
6200 case ZREF + 3:
6201 case ZREF + 4:
6202 case ZREF + 5:
6203 case ZREF + 6:
6204 case ZREF + 7:
6205 case ZREF + 8:
6206 case ZREF + 9:
6207 sprintf(buf + STRLEN(buf), "ZREF%d", OP(op) - ZREF);
6208 p = NULL;
6209 break;
6210#endif
6211 case STAR:
6212 p = "STAR";
6213 break;
6214 case PLUS:
6215 p = "PLUS";
6216 break;
6217 case NOMATCH:
6218 p = "NOMATCH";
6219 break;
6220 case MATCH:
6221 p = "MATCH";
6222 break;
6223 case BEHIND:
6224 p = "BEHIND";
6225 break;
6226 case NOBEHIND:
6227 p = "NOBEHIND";
6228 break;
6229 case SUBPAT:
6230 p = "SUBPAT";
6231 break;
6232 case BRACE_LIMITS:
6233 p = "BRACE_LIMITS";
6234 break;
6235 case BRACE_SIMPLE:
6236 p = "BRACE_SIMPLE";
6237 break;
6238 case BRACE_COMPLEX + 0:
6239 case BRACE_COMPLEX + 1:
6240 case BRACE_COMPLEX + 2:
6241 case BRACE_COMPLEX + 3:
6242 case BRACE_COMPLEX + 4:
6243 case BRACE_COMPLEX + 5:
6244 case BRACE_COMPLEX + 6:
6245 case BRACE_COMPLEX + 7:
6246 case BRACE_COMPLEX + 8:
6247 case BRACE_COMPLEX + 9:
6248 sprintf(buf + STRLEN(buf), "BRACE_COMPLEX%d", OP(op) - BRACE_COMPLEX);
6249 p = NULL;
6250 break;
6251#ifdef FEAT_MBYTE
6252 case MULTIBYTECODE:
6253 p = "MULTIBYTECODE";
6254 break;
6255#endif
6256 case NEWL:
6257 p = "NEWL";
6258 break;
6259 default:
6260 sprintf(buf + STRLEN(buf), "corrupt %d", OP(op));
6261 p = NULL;
6262 break;
6263 }
6264 if (p != NULL)
6265 (void) strcat(buf, p);
6266 return buf;
6267}
6268#endif
6269
6270#ifdef FEAT_MBYTE
6271static void mb_decompose __ARGS((int c, int *c1, int *c2, int *c3));
6272
6273typedef struct
6274{
6275 int a, b, c;
6276} decomp_T;
6277
6278
6279/* 0xfb20 - 0xfb4f */
Bram Moolenaard6f676d2005-06-01 21:51:55 +00006280static decomp_T decomp_table[0xfb4f-0xfb20+1] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281{
6282 {0x5e2,0,0}, /* 0xfb20 alt ayin */
6283 {0x5d0,0,0}, /* 0xfb21 alt alef */
6284 {0x5d3,0,0}, /* 0xfb22 alt dalet */
6285 {0x5d4,0,0}, /* 0xfb23 alt he */
6286 {0x5db,0,0}, /* 0xfb24 alt kaf */
6287 {0x5dc,0,0}, /* 0xfb25 alt lamed */
6288 {0x5dd,0,0}, /* 0xfb26 alt mem-sofit */
6289 {0x5e8,0,0}, /* 0xfb27 alt resh */
6290 {0x5ea,0,0}, /* 0xfb28 alt tav */
6291 {'+', 0, 0}, /* 0xfb29 alt plus */
6292 {0x5e9, 0x5c1, 0}, /* 0xfb2a shin+shin-dot */
6293 {0x5e9, 0x5c2, 0}, /* 0xfb2b shin+sin-dot */
6294 {0x5e9, 0x5c1, 0x5bc}, /* 0xfb2c shin+shin-dot+dagesh */
6295 {0x5e9, 0x5c2, 0x5bc}, /* 0xfb2d shin+sin-dot+dagesh */
6296 {0x5d0, 0x5b7, 0}, /* 0xfb2e alef+patah */
6297 {0x5d0, 0x5b8, 0}, /* 0xfb2f alef+qamats */
6298 {0x5d0, 0x5b4, 0}, /* 0xfb30 alef+hiriq */
6299 {0x5d1, 0x5bc, 0}, /* 0xfb31 bet+dagesh */
6300 {0x5d2, 0x5bc, 0}, /* 0xfb32 gimel+dagesh */
6301 {0x5d3, 0x5bc, 0}, /* 0xfb33 dalet+dagesh */
6302 {0x5d4, 0x5bc, 0}, /* 0xfb34 he+dagesh */
6303 {0x5d5, 0x5bc, 0}, /* 0xfb35 vav+dagesh */
6304 {0x5d6, 0x5bc, 0}, /* 0xfb36 zayin+dagesh */
6305 {0xfb37, 0, 0}, /* 0xfb37 -- UNUSED */
6306 {0x5d8, 0x5bc, 0}, /* 0xfb38 tet+dagesh */
6307 {0x5d9, 0x5bc, 0}, /* 0xfb39 yud+dagesh */
6308 {0x5da, 0x5bc, 0}, /* 0xfb3a kaf sofit+dagesh */
6309 {0x5db, 0x5bc, 0}, /* 0xfb3b kaf+dagesh */
6310 {0x5dc, 0x5bc, 0}, /* 0xfb3c lamed+dagesh */
6311 {0xfb3d, 0, 0}, /* 0xfb3d -- UNUSED */
6312 {0x5de, 0x5bc, 0}, /* 0xfb3e mem+dagesh */
6313 {0xfb3f, 0, 0}, /* 0xfb3f -- UNUSED */
6314 {0x5e0, 0x5bc, 0}, /* 0xfb40 nun+dagesh */
6315 {0x5e1, 0x5bc, 0}, /* 0xfb41 samech+dagesh */
6316 {0xfb42, 0, 0}, /* 0xfb42 -- UNUSED */
6317 {0x5e3, 0x5bc, 0}, /* 0xfb43 pe sofit+dagesh */
6318 {0x5e4, 0x5bc,0}, /* 0xfb44 pe+dagesh */
6319 {0xfb45, 0, 0}, /* 0xfb45 -- UNUSED */
6320 {0x5e6, 0x5bc, 0}, /* 0xfb46 tsadi+dagesh */
6321 {0x5e7, 0x5bc, 0}, /* 0xfb47 qof+dagesh */
6322 {0x5e8, 0x5bc, 0}, /* 0xfb48 resh+dagesh */
6323 {0x5e9, 0x5bc, 0}, /* 0xfb49 shin+dagesh */
6324 {0x5ea, 0x5bc, 0}, /* 0xfb4a tav+dagesh */
6325 {0x5d5, 0x5b9, 0}, /* 0xfb4b vav+holam */
6326 {0x5d1, 0x5bf, 0}, /* 0xfb4c bet+rafe */
6327 {0x5db, 0x5bf, 0}, /* 0xfb4d kaf+rafe */
6328 {0x5e4, 0x5bf, 0}, /* 0xfb4e pe+rafe */
6329 {0x5d0, 0x5dc, 0} /* 0xfb4f alef-lamed */
6330};
6331
6332 static void
6333mb_decompose(c, c1, c2, c3)
6334 int c, *c1, *c2, *c3;
6335{
6336 decomp_T d;
6337
6338 if (c >= 0x4b20 && c <= 0xfb4f)
6339 {
6340 d = decomp_table[c - 0xfb20];
6341 *c1 = d.a;
6342 *c2 = d.b;
6343 *c3 = d.c;
6344 }
6345 else
6346 {
6347 *c1 = c;
6348 *c2 = *c3 = 0;
6349 }
6350}
6351#endif
6352
6353/*
6354 * Compare two strings, ignore case if ireg_ic set.
6355 * Return 0 if strings match, non-zero otherwise.
6356 * Correct the length "*n" when composing characters are ignored.
6357 */
6358 static int
6359cstrncmp(s1, s2, n)
6360 char_u *s1, *s2;
6361 int *n;
6362{
6363 int result;
6364
6365 if (!ireg_ic)
6366 result = STRNCMP(s1, s2, *n);
6367 else
6368 result = MB_STRNICMP(s1, s2, *n);
6369
6370#ifdef FEAT_MBYTE
6371 /* if it failed and it's utf8 and we want to combineignore: */
6372 if (result != 0 && enc_utf8 && ireg_icombine)
6373 {
6374 char_u *str1, *str2;
6375 int c1, c2, c11, c12;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376 int junk;
6377
6378 /* we have to handle the strcmp ourselves, since it is necessary to
6379 * deal with the composing characters by ignoring them: */
6380 str1 = s1;
6381 str2 = s2;
6382 c1 = c2 = 0;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006383 while ((int)(str1 - s1) < *n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 {
6385 c1 = mb_ptr2char_adv(&str1);
6386 c2 = mb_ptr2char_adv(&str2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387
6388 /* decompose the character if necessary, into 'base' characters
6389 * because I don't care about Arabic, I will hard-code the Hebrew
6390 * which I *do* care about! So sue me... */
6391 if (c1 != c2 && (!ireg_ic || utf_fold(c1) != utf_fold(c2)))
6392 {
6393 /* decomposition necessary? */
6394 mb_decompose(c1, &c11, &junk, &junk);
6395 mb_decompose(c2, &c12, &junk, &junk);
6396 c1 = c11;
6397 c2 = c12;
6398 if (c11 != c12 && (!ireg_ic || utf_fold(c11) != utf_fold(c12)))
6399 break;
6400 }
6401 }
6402 result = c2 - c1;
6403 if (result == 0)
6404 *n = (int)(str2 - s2);
6405 }
6406#endif
6407
6408 return result;
6409}
6410
6411/*
6412 * cstrchr: This function is used a lot for simple searches, keep it fast!
6413 */
6414 static char_u *
6415cstrchr(s, c)
6416 char_u *s;
6417 int c;
6418{
6419 char_u *p;
6420 int cc;
6421
6422 if (!ireg_ic
6423#ifdef FEAT_MBYTE
6424 || (!enc_utf8 && mb_char2len(c) > 1)
6425#endif
6426 )
6427 return vim_strchr(s, c);
6428
6429 /* tolower() and toupper() can be slow, comparing twice should be a lot
6430 * faster (esp. when using MS Visual C++!).
6431 * For UTF-8 need to use folded case. */
6432#ifdef FEAT_MBYTE
6433 if (enc_utf8 && c > 0x80)
6434 cc = utf_fold(c);
6435 else
6436#endif
6437 if (isupper(c))
6438 cc = TOLOWER_LOC(c);
6439 else if (islower(c))
6440 cc = TOUPPER_LOC(c);
6441 else
6442 return vim_strchr(s, c);
6443
6444#ifdef FEAT_MBYTE
6445 if (has_mbyte)
6446 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006447 for (p = s; *p != NUL; p += (*mb_ptr2len)(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 {
6449 if (enc_utf8 && c > 0x80)
6450 {
6451 if (utf_fold(utf_ptr2char(p)) == cc)
6452 return p;
6453 }
6454 else if (*p == c || *p == cc)
6455 return p;
6456 }
6457 }
6458 else
6459#endif
6460 /* Faster version for when there are no multi-byte characters. */
6461 for (p = s; *p != NUL; ++p)
6462 if (*p == c || *p == cc)
6463 return p;
6464
6465 return NULL;
6466}
6467
6468/***************************************************************
6469 * regsub stuff *
6470 ***************************************************************/
6471
6472/* This stuff below really confuses cc on an SGI -- webb */
6473#ifdef __sgi
6474# undef __ARGS
6475# define __ARGS(x) ()
6476#endif
6477
6478/*
6479 * We should define ftpr as a pointer to a function returning a pointer to
6480 * a function returning a pointer to a function ...
6481 * This is impossible, so we declare a pointer to a function returning a
6482 * pointer to a function returning void. This should work for all compilers.
6483 */
6484typedef void (*(*fptr) __ARGS((char_u *, int)))();
6485
6486static fptr do_upper __ARGS((char_u *, int));
6487static fptr do_Upper __ARGS((char_u *, int));
6488static fptr do_lower __ARGS((char_u *, int));
6489static fptr do_Lower __ARGS((char_u *, int));
6490
6491static int vim_regsub_both __ARGS((char_u *source, char_u *dest, int copy, int magic, int backslash));
6492
6493 static fptr
6494do_upper(d, c)
6495 char_u *d;
6496 int c;
6497{
6498 *d = TOUPPER_LOC(c);
6499
6500 return (fptr)NULL;
6501}
6502
6503 static fptr
6504do_Upper(d, c)
6505 char_u *d;
6506 int c;
6507{
6508 *d = TOUPPER_LOC(c);
6509
6510 return (fptr)do_Upper;
6511}
6512
6513 static fptr
6514do_lower(d, c)
6515 char_u *d;
6516 int c;
6517{
6518 *d = TOLOWER_LOC(c);
6519
6520 return (fptr)NULL;
6521}
6522
6523 static fptr
6524do_Lower(d, c)
6525 char_u *d;
6526 int c;
6527{
6528 *d = TOLOWER_LOC(c);
6529
6530 return (fptr)do_Lower;
6531}
6532
6533/*
6534 * regtilde(): Replace tildes in the pattern by the old pattern.
6535 *
6536 * Short explanation of the tilde: It stands for the previous replacement
6537 * pattern. If that previous pattern also contains a ~ we should go back a
6538 * step further... But we insert the previous pattern into the current one
6539 * and remember that.
6540 * This still does not handle the case where "magic" changes. TODO?
6541 *
6542 * The tildes are parsed once before the first call to vim_regsub().
6543 */
6544 char_u *
6545regtilde(source, magic)
6546 char_u *source;
6547 int magic;
6548{
6549 char_u *newsub = source;
6550 char_u *tmpsub;
6551 char_u *p;
6552 int len;
6553 int prevlen;
6554
6555 for (p = newsub; *p; ++p)
6556 {
6557 if ((*p == '~' && magic) || (*p == '\\' && *(p + 1) == '~' && !magic))
6558 {
6559 if (reg_prev_sub != NULL)
6560 {
6561 /* length = len(newsub) - 1 + len(prev_sub) + 1 */
6562 prevlen = (int)STRLEN(reg_prev_sub);
6563 tmpsub = alloc((unsigned)(STRLEN(newsub) + prevlen));
6564 if (tmpsub != NULL)
6565 {
6566 /* copy prefix */
6567 len = (int)(p - newsub); /* not including ~ */
6568 mch_memmove(tmpsub, newsub, (size_t)len);
6569 /* interpretate tilde */
6570 mch_memmove(tmpsub + len, reg_prev_sub, (size_t)prevlen);
6571 /* copy postfix */
6572 if (!magic)
6573 ++p; /* back off \ */
6574 STRCPY(tmpsub + len + prevlen, p + 1);
6575
6576 if (newsub != source) /* already allocated newsub */
6577 vim_free(newsub);
6578 newsub = tmpsub;
6579 p = newsub + len + prevlen;
6580 }
6581 }
6582 else if (magic)
6583 STRCPY(p, p + 1); /* remove '~' */
6584 else
6585 STRCPY(p, p + 2); /* remove '\~' */
6586 --p;
6587 }
6588 else
6589 {
6590 if (*p == '\\' && p[1]) /* skip escaped characters */
6591 ++p;
6592#ifdef FEAT_MBYTE
6593 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006594 p += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595#endif
6596 }
6597 }
6598
6599 vim_free(reg_prev_sub);
6600 if (newsub != source) /* newsub was allocated, just keep it */
6601 reg_prev_sub = newsub;
6602 else /* no ~ found, need to save newsub */
6603 reg_prev_sub = vim_strsave(newsub);
6604 return newsub;
6605}
6606
6607#ifdef FEAT_EVAL
6608static int can_f_submatch = FALSE; /* TRUE when submatch() can be used */
6609
6610/* These pointers are used instead of reg_match and reg_mmatch for
6611 * reg_submatch(). Needed for when the substitution string is an expression
6612 * that contains a call to substitute() and submatch(). */
6613static regmatch_T *submatch_match;
6614static regmmatch_T *submatch_mmatch;
6615#endif
6616
6617#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
6618/*
6619 * vim_regsub() - perform substitutions after a vim_regexec() or
6620 * vim_regexec_multi() match.
6621 *
6622 * If "copy" is TRUE really copy into "dest".
6623 * If "copy" is FALSE nothing is copied, this is just to find out the length
6624 * of the result.
6625 *
6626 * If "backslash" is TRUE, a backslash will be removed later, need to double
6627 * them to keep them, and insert a backslash before a CR to avoid it being
6628 * replaced with a line break later.
6629 *
6630 * Note: The matched text must not change between the call of
6631 * vim_regexec()/vim_regexec_multi() and vim_regsub()! It would make the back
6632 * references invalid!
6633 *
6634 * Returns the size of the replacement, including terminating NUL.
6635 */
6636 int
6637vim_regsub(rmp, source, dest, copy, magic, backslash)
6638 regmatch_T *rmp;
6639 char_u *source;
6640 char_u *dest;
6641 int copy;
6642 int magic;
6643 int backslash;
6644{
6645 reg_match = rmp;
6646 reg_mmatch = NULL;
6647 reg_maxline = 0;
6648 return vim_regsub_both(source, dest, copy, magic, backslash);
6649}
6650#endif
6651
6652 int
6653vim_regsub_multi(rmp, lnum, source, dest, copy, magic, backslash)
6654 regmmatch_T *rmp;
6655 linenr_T lnum;
6656 char_u *source;
6657 char_u *dest;
6658 int copy;
6659 int magic;
6660 int backslash;
6661{
6662 reg_match = NULL;
6663 reg_mmatch = rmp;
6664 reg_buf = curbuf; /* always works on the current buffer! */
6665 reg_firstlnum = lnum;
6666 reg_maxline = curbuf->b_ml.ml_line_count - lnum;
6667 return vim_regsub_both(source, dest, copy, magic, backslash);
6668}
6669
6670 static int
6671vim_regsub_both(source, dest, copy, magic, backslash)
6672 char_u *source;
6673 char_u *dest;
6674 int copy;
6675 int magic;
6676 int backslash;
6677{
6678 char_u *src;
6679 char_u *dst;
6680 char_u *s;
6681 int c;
6682 int no = -1;
6683 fptr func = (fptr)NULL;
6684 linenr_T clnum = 0; /* init for GCC */
6685 int len = 0; /* init for GCC */
6686#ifdef FEAT_EVAL
6687 static char_u *eval_result = NULL;
6688#endif
6689#ifdef FEAT_MBYTE
6690 int l;
6691#endif
6692
6693
6694 /* Be paranoid... */
6695 if (source == NULL || dest == NULL)
6696 {
6697 EMSG(_(e_null));
6698 return 0;
6699 }
6700 if (prog_magic_wrong())
6701 return 0;
6702 src = source;
6703 dst = dest;
6704
6705 /*
6706 * When the substitute part starts with "\=" evaluate it as an expression.
6707 */
6708 if (source[0] == '\\' && source[1] == '='
6709#ifdef FEAT_EVAL
6710 && !can_f_submatch /* can't do this recursively */
6711#endif
6712 )
6713 {
6714#ifdef FEAT_EVAL
6715 /* To make sure that the length doesn't change between checking the
6716 * length and copying the string, and to speed up things, the
6717 * resulting string is saved from the call with "copy" == FALSE to the
6718 * call with "copy" == TRUE. */
6719 if (copy)
6720 {
6721 if (eval_result != NULL)
6722 {
6723 STRCPY(dest, eval_result);
6724 dst += STRLEN(eval_result);
6725 vim_free(eval_result);
6726 eval_result = NULL;
6727 }
6728 }
6729 else
6730 {
6731 linenr_T save_reg_maxline;
6732 win_T *save_reg_win;
6733 int save_ireg_ic;
6734
6735 vim_free(eval_result);
6736
6737 /* The expression may contain substitute(), which calls us
6738 * recursively. Make sure submatch() gets the text from the first
6739 * level. Don't need to save "reg_buf", because
6740 * vim_regexec_multi() can't be called recursively. */
6741 submatch_match = reg_match;
6742 submatch_mmatch = reg_mmatch;
6743 save_reg_maxline = reg_maxline;
6744 save_reg_win = reg_win;
6745 save_ireg_ic = ireg_ic;
6746 can_f_submatch = TRUE;
6747
6748 eval_result = eval_to_string(source + 2, NULL);
6749 if (eval_result != NULL)
6750 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006751 for (s = eval_result; *s != NUL; mb_ptr_adv(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 {
6753 /* Change NL to CR, so that it becomes a line break.
6754 * Skip over a backslashed character. */
6755 if (*s == NL)
6756 *s = CAR;
6757 else if (*s == '\\' && s[1] != NUL)
6758 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 }
6760
6761 dst += STRLEN(eval_result);
6762 }
6763
6764 reg_match = submatch_match;
6765 reg_mmatch = submatch_mmatch;
6766 reg_maxline = save_reg_maxline;
6767 reg_win = save_reg_win;
6768 ireg_ic = save_ireg_ic;
6769 can_f_submatch = FALSE;
6770 }
6771#endif
6772 }
6773 else
6774 while ((c = *src++) != NUL)
6775 {
6776 if (c == '&' && magic)
6777 no = 0;
6778 else if (c == '\\' && *src != NUL)
6779 {
6780 if (*src == '&' && !magic)
6781 {
6782 ++src;
6783 no = 0;
6784 }
6785 else if ('0' <= *src && *src <= '9')
6786 {
6787 no = *src++ - '0';
6788 }
6789 else if (vim_strchr((char_u *)"uUlLeE", *src))
6790 {
6791 switch (*src++)
6792 {
6793 case 'u': func = (fptr)do_upper;
6794 continue;
6795 case 'U': func = (fptr)do_Upper;
6796 continue;
6797 case 'l': func = (fptr)do_lower;
6798 continue;
6799 case 'L': func = (fptr)do_Lower;
6800 continue;
6801 case 'e':
6802 case 'E': func = (fptr)NULL;
6803 continue;
6804 }
6805 }
6806 }
6807 if (no < 0) /* Ordinary character. */
6808 {
6809 if (c == '\\' && *src != NUL)
6810 {
6811 /* Check for abbreviations -- webb */
6812 switch (*src)
6813 {
6814 case 'r': c = CAR; ++src; break;
6815 case 'n': c = NL; ++src; break;
6816 case 't': c = TAB; ++src; break;
6817 /* Oh no! \e already has meaning in subst pat :-( */
6818 /* case 'e': c = ESC; ++src; break; */
6819 case 'b': c = Ctrl_H; ++src; break;
6820
6821 /* If "backslash" is TRUE the backslash will be removed
6822 * later. Used to insert a literal CR. */
6823 default: if (backslash)
6824 {
6825 if (copy)
6826 *dst = '\\';
6827 ++dst;
6828 }
6829 c = *src++;
6830 }
6831 }
6832
6833 /* Write to buffer, if copy is set. */
6834#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006835 if (has_mbyte && (l = (*mb_ptr2len)(src - 1)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 {
6837 /* TODO: should use "func" here. */
6838 if (copy)
6839 mch_memmove(dst, src - 1, l);
6840 dst += l - 1;
6841 src += l - 1;
6842 }
6843 else
6844 {
6845#endif
6846 if (copy)
6847 {
6848 if (func == (fptr)NULL) /* just copy */
6849 *dst = c;
6850 else /* change case */
6851 func = (fptr)(func(dst, c));
6852 /* Turbo C complains without the typecast */
6853 }
6854#ifdef FEAT_MBYTE
6855 }
6856#endif
6857 dst++;
6858 }
6859 else
6860 {
6861 if (REG_MULTI)
6862 {
6863 clnum = reg_mmatch->startpos[no].lnum;
6864 if (clnum < 0 || reg_mmatch->endpos[no].lnum < 0)
6865 s = NULL;
6866 else
6867 {
6868 s = reg_getline(clnum) + reg_mmatch->startpos[no].col;
6869 if (reg_mmatch->endpos[no].lnum == clnum)
6870 len = reg_mmatch->endpos[no].col
6871 - reg_mmatch->startpos[no].col;
6872 else
6873 len = (int)STRLEN(s);
6874 }
6875 }
6876 else
6877 {
6878 s = reg_match->startp[no];
6879 if (reg_match->endp[no] == NULL)
6880 s = NULL;
6881 else
6882 len = (int)(reg_match->endp[no] - s);
6883 }
6884 if (s != NULL)
6885 {
6886 for (;;)
6887 {
6888 if (len == 0)
6889 {
6890 if (REG_MULTI)
6891 {
6892 if (reg_mmatch->endpos[no].lnum == clnum)
6893 break;
6894 if (copy)
6895 *dst = CAR;
6896 ++dst;
6897 s = reg_getline(++clnum);
6898 if (reg_mmatch->endpos[no].lnum == clnum)
6899 len = reg_mmatch->endpos[no].col;
6900 else
6901 len = (int)STRLEN(s);
6902 }
6903 else
6904 break;
6905 }
6906 else if (*s == NUL) /* we hit NUL. */
6907 {
6908 if (copy)
6909 EMSG(_(e_re_damg));
6910 goto exit;
6911 }
6912 else
6913 {
6914 if (backslash && (*s == CAR || *s == '\\'))
6915 {
6916 /*
6917 * Insert a backslash in front of a CR, otherwise
6918 * it will be replaced by a line break.
6919 * Number of backslashes will be halved later,
6920 * double them here.
6921 */
6922 if (copy)
6923 {
6924 dst[0] = '\\';
6925 dst[1] = *s;
6926 }
6927 dst += 2;
6928 }
6929#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006930 else if (has_mbyte && (l = (*mb_ptr2len)(s)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 {
6932 /* TODO: should use "func" here. */
6933 if (copy)
6934 mch_memmove(dst, s, l);
6935 dst += l;
6936 s += l - 1;
6937 len -= l - 1;
6938 }
6939#endif
6940 else
6941 {
6942 if (copy)
6943 {
6944 if (func == (fptr)NULL) /* just copy */
6945 *dst = *s;
6946 else /* change case */
6947 func = (fptr)(func(dst, *s));
6948 /* Turbo C complains without the typecast */
6949 }
6950 ++dst;
6951 }
6952 ++s;
6953 --len;
6954 }
6955 }
6956 }
6957 no = -1;
6958 }
6959 }
6960 if (copy)
6961 *dst = NUL;
6962
6963exit:
6964 return (int)((dst - dest) + 1);
6965}
6966
6967#ifdef FEAT_EVAL
6968/*
6969 * Used for the submatch() function: get the string from tne n'th submatch in
6970 * allocated memory.
6971 * Returns NULL when not in a ":s" command and for a non-existing submatch.
6972 */
6973 char_u *
6974reg_submatch(no)
6975 int no;
6976{
6977 char_u *retval = NULL;
6978 char_u *s;
6979 int len;
6980 int round;
6981 linenr_T lnum;
6982
6983 if (!can_f_submatch)
6984 return NULL;
6985
6986 if (submatch_match == NULL)
6987 {
6988 /*
6989 * First round: compute the length and allocate memory.
6990 * Second round: copy the text.
6991 */
6992 for (round = 1; round <= 2; ++round)
6993 {
6994 lnum = submatch_mmatch->startpos[no].lnum;
6995 if (lnum < 0 || submatch_mmatch->endpos[no].lnum < 0)
6996 return NULL;
6997
6998 s = reg_getline(lnum) + submatch_mmatch->startpos[no].col;
6999 if (s == NULL) /* anti-crash check, cannot happen? */
7000 break;
7001 if (submatch_mmatch->endpos[no].lnum == lnum)
7002 {
7003 /* Within one line: take form start to end col. */
7004 len = submatch_mmatch->endpos[no].col
7005 - submatch_mmatch->startpos[no].col;
7006 if (round == 2)
Bram Moolenaarbbebc852005-07-18 21:47:53 +00007007 vim_strncpy(retval, s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 ++len;
7009 }
7010 else
7011 {
7012 /* Multiple lines: take start line from start col, middle
7013 * lines completely and end line up to end col. */
7014 len = (int)STRLEN(s);
7015 if (round == 2)
7016 {
7017 STRCPY(retval, s);
7018 retval[len] = '\n';
7019 }
7020 ++len;
7021 ++lnum;
7022 while (lnum < submatch_mmatch->endpos[no].lnum)
7023 {
7024 s = reg_getline(lnum++);
7025 if (round == 2)
7026 STRCPY(retval + len, s);
7027 len += (int)STRLEN(s);
7028 if (round == 2)
7029 retval[len] = '\n';
7030 ++len;
7031 }
7032 if (round == 2)
7033 STRNCPY(retval + len, reg_getline(lnum),
7034 submatch_mmatch->endpos[no].col);
7035 len += submatch_mmatch->endpos[no].col;
7036 if (round == 2)
7037 retval[len] = NUL;
7038 ++len;
7039 }
7040
7041 if (round == 1)
7042 {
7043 retval = lalloc((long_u)len, TRUE);
7044 if (s == NULL)
7045 return NULL;
7046 }
7047 }
7048 }
7049 else
7050 {
7051 if (submatch_match->endp[no] == NULL)
7052 retval = NULL;
7053 else
7054 {
7055 s = submatch_match->startp[no];
7056 retval = vim_strnsave(s, (int)(submatch_match->endp[no] - s));
7057 }
7058 }
7059
7060 return retval;
7061}
7062#endif