This patch adds most of syntax-assist code of Emacs 20.2.90 to 19.33. Note that the contents of the patch rms-emacs-20.2-experimental-patch-narrow+dirfiles+skipcomment+skipsyntax-better is not included, but rms-emacs-20.2-patch-inf-loop is. Note also that font-lock.el, lazy-lock.el, and emacs-lisp/regexp-opt.el of Emacs 20.2.92 may be used with 19.33 after application of this patch. Enjoy, Ilya diff -pru emacs/19.33/src/dired.c ./19.33\src/dired.c --- emacs/19.33/src/dired.c Sun Aug 4 16:34:28 1996 +++ ./19.33/src/dired.c Tue Mar 11 00:12:26 1997 @@ -178,6 +178,7 @@ If NOSORT is non-nil, the list is not so list = Qnil; dirnamelen = XSTRING (directory)->size; + re_match_object = Qt; /* Loop reading blocks */ while (1) diff -pru emacs/19.33/src/intervals.c ./19.33\src/intervals.c --- emacs/19.33/src/intervals.c Sun Aug 4 16:35:06 1996 +++ ./19.33/src/intervals.c Sun Mar 1 04:05:02 1998 @@ -670,6 +670,55 @@ previous_interval (interval) return NULL_INTERVAL; } + +/* Find the interval containing POS given some non-NULL INTERVAL + in the same tree. Note that we need to update interval->position + if we go down the tree. */ +INTERVAL +update_interval (i, pos) + register INTERVAL i; + int pos; +{ + while (1) + { + if (NULL_INTERVAL_P (i)) + return NULL_INTERVAL; + + if (pos < i->position) + { + /* Move left. */ + if (pos >= i->position - TOTAL_LENGTH (i->left)) + { + i->left->position = i->position - TOTAL_LENGTH (i->left) + + LEFT_TOTAL_LENGTH (i->left); + i = i->left; /* Move to the left child */ + } + else if (NULL_PARENT (i)) + error ("Point before start of properties"); + else + i = i->parent; + continue; + } + else if (pos >= INTERVAL_LAST_POS (i)) + { + /* Move right. */ + if (pos < INTERVAL_LAST_POS (i) + TOTAL_LENGTH (i->right)) + { + i->right->position = INTERVAL_LAST_POS (i) + + LEFT_TOTAL_LENGTH (i->right); + i = i->right; /* Move to the right child */ + } + else if (NULL_PARENT (i)) + error ("Point after end of properties"); + else + i = i->parent; + continue; + } + else + return i; + } +} + #if 0 /* Traverse a path down the interval tree TREE to the interval diff -pru emacs/19.33/src/lisp.h ./19.33\src/lisp.h --- emacs/19.33/src/lisp.h Sun Aug 4 15:21:48 1996 +++ ./19.33/src/lisp.h Sun Mar 1 04:01:58 1998 @@ -449,6 +449,11 @@ struct interval unsigned int total_length; /* Length of myself and both children. */ unsigned int position; /* Cache of interval's character position. */ + /* This field is usually updated + simultaneously with an interval + traversal, there is no guaranty + that it is valid for a random + interval. */ struct interval *left; /* Intervals which precede me. */ struct interval *right; /* Intervals which succeed me. */ diff -pru emacs/19.33/src/regex.c ./19.33\src/regex.c --- emacs/19.33/src/regex.c Thu Aug 15 23:12:44 1996 +++ ./19.33/src/regex.c Fri Mar 14 18:19:00 1997 @@ -28,6 +28,12 @@ #undef _GNU_SOURCE #define _GNU_SOURCE +/* Converts the pointer to the char to BEG-based offset from the start. */ +#define PTR_TO_OFFSET(d) \ + POS_AS_IN_BUFFER (MATCHING_IN_FIRST_STRING \ + ? (d) - string1 : (d) - (string2 - size1)) +#define POS_AS_IN_BUFFER(p) ((p) + 1) + #ifdef HAVE_CONFIG_H #include #endif @@ -54,6 +60,10 @@ #include "lisp.h" #include "buffer.h" + +/* Make syntax table lookup grant data in gl_state. */ +#define SYNTAX_ENTRY_VIA_PROPERTY + #include "syntax.h" #else /* not emacs */ @@ -376,7 +386,7 @@ typedef enum corresponding start_memory when we're at a stop_memory.) */ stop_memory, - /* Match a duplicate of something remembered. Followed by one + /* Match a duplicate of something remembered. Followed by one byte containing the register number. */ duplicate, @@ -421,7 +431,7 @@ typedef enum maybe_pop_jump, /* Jump to following two-byte address, and push a dummy failure - point. This failure point will be thrown away if an attempt + point. This failure point will be thrown away if an attempt is made to use it for a failure. A `+' construct makes this before the first repeat. Also used as an intermediary kind of jump when compiling an alternative. */ @@ -1814,7 +1824,7 @@ regex_compile (pattern, size, syntax, bu goto normal_char; handle_plus: case '*': - /* If there is no previous pattern... */ + /* If there is no previous pattern... */ if (!laststart) { if (syntax & RE_CONTEXT_INVALID_OPS) @@ -1929,7 +1939,7 @@ regex_compile (pattern, size, syntax, bu { /* At least one repetition is required, so insert a `dummy_failure_jump' before the initial - `on_failure_jump' instruction of the loop. This + `on_failure_jump' instruction of the loop. This effects a skip over that instruction the first time we hit that loop. */ GET_BUFFER_SPACE (3); @@ -2369,7 +2379,7 @@ regex_compile (pattern, size, syntax, bu if (upper_bound < 0) upper_bound = RE_DUP_MAX; } else - /* Interval such as `{1}' => match exactly once. */ + /* Interval such as `{1}' => match exactly once. */ upper_bound = lower_bound; if (lower_bound < 0 || upper_bound > RE_DUP_MAX @@ -3043,21 +3053,33 @@ re_compile_fastmap (bufp) } #ifdef emacs + case wordbound: + case notwordbound: + case wordbeg: + case wordend: case syntaxspec: + /* This match depends on text properties. These end with + aborting optimizations. */ + bufp->can_be_null = 1; + goto done; +#if 0 k = *p++; for (j = 0; j < (1 << BYTEWIDTH); j++) if (SYNTAX (j) == (enum syntaxcode) k) fastmap[j] = 1; break; - +#endif case notsyntaxspec: + bufp->can_be_null = 1; + goto done; +#if 0 k = *p++; for (j = 0; j < (1 << BYTEWIDTH); j++) if (SYNTAX (j) != (enum syntaxcode) k) fastmap[j] = 1; break; - +#endif /* All cases after this match the empty string. These end with `continue'. */ @@ -3075,10 +3097,12 @@ re_compile_fastmap (bufp) case endline: case begbuf: case endbuf: +#ifndef emacs case wordbound: case notwordbound: case wordbeg: case wordend: +#endif case push_dummy_failure: continue; @@ -3234,7 +3258,7 @@ re_set_registers (bufp, regs, num_regs, /* Searching routines. */ /* Like re_search_2, below, but only one string is specified, and - doesn't let you say where to stop matching. */ + doesn't let you say where to stop matching. */ int re_search (bufp, string, size, startpos, range, regs) @@ -3323,6 +3347,13 @@ re_search_2 (bufp, string1, size1, strin if (re_compile_fastmap (bufp) == -2) return -2; +#ifdef emacs + SETUP_SYNTAX_TABLE_FOR_OBJECT (re_match_object, + POS_AS_IN_BUFFER (startpos > 0 + ? startpos - 1 : startpos), + 1); +#endif + /* Loop through the string, looking for a place to start matching. */ for (;;) { @@ -3508,6 +3539,11 @@ re_match (bufp, string, size, pos, regs) } #endif /* not emacs */ +#ifdef emacs +/* In Emacs, this is the string or buffer in which we + are matching. It is used for looking up syntax properties. */ +Lisp_Object re_match_object; +#endif /* re_match_2 matches the compiled pattern in BUFP against the the (virtual) concatenation of STRING1 and STRING2 (of length SIZE1 @@ -3531,7 +3567,15 @@ re_match_2 (bufp, string1, size1, string struct re_registers *regs; int stop; { - int result = re_match_2_internal (bufp, string1, size1, string2, size2, + int result; + +#ifdef emacs + SETUP_SYNTAX_TABLE_FOR_OBJECT (re_match_object, + POS_AS_IN_BUFFER (pos > 0 ? pos - 1 : pos), + 1); +#endif + + result = re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop); alloca (0); return result; @@ -3603,7 +3647,7 @@ re_match_2_internal (bufp, string1, size unsigned lowest_active_reg = NO_LOWEST_ACTIVE_REG; unsigned highest_active_reg = NO_HIGHEST_ACTIVE_REG; - /* Information on the contents of registers. These are pointers into + /* Information on the contents of registers. These are pointers into the input strings; they record just what was matched (on this attempt) by a subexpression part of the pattern, that is, the regnum-th regstart pointer points to where in the pattern we began @@ -3624,7 +3668,7 @@ re_match_2_internal (bufp, string1, size #endif /* The is_active field of reg_info helps us keep track of which (possibly - nested) subexpressions we are currently in. The matched_something + nested) subexpressions we are currently in. The matched_something field of reg_info[reg_num] helps us tell whether or not we have matched any of the pattern so far this time through the reg_num-th subexpression. These two fields get reset each time through any @@ -3636,7 +3680,7 @@ re_match_2_internal (bufp, string1, size /* The following record the register info as found in the above variables when we find a match better than any we've seen before. This happens as we backtrack through the failure points, which in - turn happens only if we have not yet matched the entire string. */ + turn happens only if we have not yet matched the entire string. */ unsigned best_regs_set = false; #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global. */ const char **best_regstart, **best_regend; @@ -3713,7 +3757,7 @@ re_match_2_internal (bufp, string1, size } /* Initialize subexpression text positions to -1 to mark ones that no - start_memory/stop_memory has been seen for. Also initialize the + start_memory/stop_memory has been seen for. Also initialize the register information struct. */ for (mcnt = 1; mcnt < num_regs; mcnt++) { @@ -3826,7 +3870,7 @@ re_match_2_internal (bufp, string1, size /* If no failure points, don't restore garbage. And if last match is real best match, don't restore second - best one. */ + best one. */ else if (best_regs_set && !best_match_p) { restore_best_regs: @@ -4250,7 +4294,7 @@ re_match_2_internal (bufp, string1, size if (dend2 == end_match_2) break; if (dend2 == regend[regno]) break; - /* End of string1 => advance to string2. */ + /* End of string1 => advance to string2. */ d2 = string2; dend2 = regend[regno]; } @@ -4423,7 +4467,7 @@ re_match_2_internal (bufp, string1, size register unsigned char *p2 = p; /* Compare the beginning of the repeat with what in the - pattern follows its end. If we can establish that there + pattern follows its end. If we can establish that there is nothing that they would both match, i.e., that we would have to backtrack because of (as in, e.g., `a*a') then we can change to pop_failure_jump, because we'll @@ -4702,12 +4746,20 @@ re_match_2_internal (bufp, string1, size case wordbound: { boolean prevchar, thischar; + int pos1 = PTR_TO_OFFSET (d - 1); DEBUG_PRINT1 ("EXECUTING wordbound.\n"); if (AT_STRINGS_BEG (d) || AT_STRINGS_END (d)) break; +#ifdef emacs + UPDATE_SYNTAX_TABLE (pos1 ? pos1 : 1); +#endif prevchar = WORDCHAR_P (d - 1); +#ifdef emacs + pos1++; + UPDATE_SYNTAX_TABLE_FORWARD (pos1); +#endif thischar = WORDCHAR_P (d); if (prevchar != thischar) break; @@ -4717,12 +4769,20 @@ re_match_2_internal (bufp, string1, size case notwordbound: { boolean prevchar, thischar; + int pos1 = PTR_TO_OFFSET (d - 1); DEBUG_PRINT1 ("EXECUTING notwordbound.\n"); if (AT_STRINGS_BEG (d) || AT_STRINGS_END (d)) goto fail; +#ifdef emacs + UPDATE_SYNTAX_TABLE (pos1 ? pos1 : 1); +#endif prevchar = WORDCHAR_P (d - 1); +#ifdef emacs + pos1++; + UPDATE_SYNTAX_TABLE_FORWARD (pos1); +#endif thischar = WORDCHAR_P (d); if (prevchar != thischar) goto fail; @@ -4731,18 +4791,45 @@ re_match_2_internal (bufp, string1, size #endif case wordbeg: - DEBUG_PRINT1 ("EXECUTING wordbeg.\n"); - if (WORDCHAR_P (d) && (AT_STRINGS_BEG (d) || !WORDCHAR_P (d - 1))) - break; - goto fail; + { + int pos1 = PTR_TO_OFFSET (d - 1); +#ifdef emacs + UPDATE_SYNTAX_TABLE (pos1 ? pos1 : 1); +#endif + DEBUG_PRINT1 ("EXECUTING wordbeg.\n"); + if ((AT_STRINGS_BEG (d) || !WORDCHAR_P (d - 1)) + && !AT_STRINGS_END (d)) + { +#ifdef emacs + pos1++; + UPDATE_SYNTAX_TABLE_FORWARD (pos1); +#endif + if (WORDCHAR_P (d)) + break; + } + goto fail; + } case wordend: - DEBUG_PRINT1 ("EXECUTING wordend.\n"); - if (!AT_STRINGS_BEG (d) && WORDCHAR_P (d - 1) - && (!WORDCHAR_P (d) || AT_STRINGS_END (d))) - break; - goto fail; - + { + int pos1 = PTR_TO_OFFSET (d - 1); +#ifdef emacs + UPDATE_SYNTAX_TABLE (pos1 ? pos1 : 1); +#endif + DEBUG_PRINT1 ("EXECUTING wordend.\n"); + if (!AT_STRINGS_BEG (d) && WORDCHAR_P (d - 1)) + { + if (AT_STRINGS_END (d)) + break; +#ifdef emacs + UPDATE_SYNTAX_TABLE_FORWARD (pos1 + 1); +#endif + if (!WORDCHAR_P (d)) + break; + } + goto fail; + } + #ifdef emacs case before_dot: DEBUG_PRINT1 ("EXECUTING before_dot.\n"); @@ -4772,6 +4859,12 @@ re_match_2_internal (bufp, string1, size mcnt = (int) Sword; matchsyntax: PREFETCH (); +#ifdef emacs + { + int pos1 = PTR_TO_OFFSET (d); + UPDATE_SYNTAX_TABLE (pos1); + } +#endif /* Can't use *d++ here; SYNTAX may be an unsafe macro. */ d++; if (SYNTAX (d[-1]) != (enum syntaxcode) mcnt) @@ -4789,6 +4882,12 @@ re_match_2_internal (bufp, string1, size mcnt = (int) Sword; matchnotsyntax: PREFETCH (); +#ifdef emacs + { + int pos1 = PTR_TO_OFFSET (d); + UPDATE_SYNTAX_TABLE (pos1); + } +#endif /* Can't use *d++ here; SYNTAX may be an unsafe macro. */ d++; if (SYNTAX (d[-1]) == (enum syntaxcode) mcnt) @@ -4822,7 +4921,7 @@ re_match_2_internal (bufp, string1, size continue; /* Successfully executed one pattern command; keep going. */ - /* We goto here if a matching operation fails. */ + /* We goto here if a matching operation fails. */ fail: if (!FAIL_STACK_EMPTY ()) { /* A restart point is known. Restore to that state. */ @@ -4999,7 +5098,7 @@ group_match_null_string_p (p, end, reg_i /* Similar to group_match_null_string_p, but doesn't deal with alternatives: It expects P to be the first byte of a single alternative and END one - byte past the last. The alternative can contain groups. */ + byte past the last. The alternative can contain groups. */ static boolean alt_match_null_string_p (p, end, reg_info) diff -pru emacs/19.33/src/regex.h ./19.33\src/regex.h --- emacs/19.33/src/regex.h Sat Feb 17 20:26:50 1996 +++ ./19.33/src/regex.h Mon Mar 10 23:35:56 1997 @@ -140,6 +140,13 @@ typedef unsigned reg_syntax_t; stored in the pattern buffer, so changing this does not affect already-compiled regexps. */ extern reg_syntax_t re_syntax_options; + +#ifdef emacs +/* In Emacs, this is the string or buffer in which we + are matching. It is used for looking up syntax properties. */ +extern Lisp_Object re_match_object; +#endif + /* Define combinations of the above bits for the standard possibilities. (The [[[ comments delimit what gets put into the Texinfo file, so diff -pru emacs/19.33/src/search.c ./19.33\src/search.c --- emacs/19.33/src/search.c Sun Aug 4 15:22:40 1996 +++ ./19.33/src/search.c Mon Jul 28 00:51:00 1997 @@ -30,11 +30,12 @@ Boston, MA 02111-1307, USA. */ #include #include "regex.h" -#define REGEXP_CACHE_SIZE 5 +#define REGEXP_CACHE_SIZE 20 /* If the regexp is non-nil, then the buffer contains the compiled form of that regexp, suitable for searching. */ -struct regexp_cache { +struct regexp_cache +{ struct regexp_cache *next; Lisp_Object regexp; struct re_pattern_buffer buf; @@ -156,7 +157,8 @@ compile_pattern (pattern, regp, translat for (cpp = &searchbuf_head; ; cpp = &cp->next) { cp = *cpp; - if (!NILP (Fstring_equal (cp->regexp, pattern)) + if (XSTRING (cp->regexp)->size == XSTRING (pattern)->size + && !NILP (Fstring_equal (cp->regexp, pattern)) && cp->buf.translate == translate && cp->posix == posix) break; @@ -221,25 +223,27 @@ looking_at_1 (string, posix) /* Get pointers and sizes of the two strings that make up the visible portion of the buffer. */ - p1 = BEGV_ADDR; - s1 = GPT - BEGV; + p1 = BEG_ADDR; + s1 = GPT - 1; p2 = GAP_END_ADDR; s2 = ZV - GPT; if (s1 < 0) { p2 = p1; - s2 = ZV - BEGV; + s2 = ZV - 1; s1 = 0; } if (s2 < 0) { - s1 = ZV - BEGV; + s1 = ZV - 1; s2 = 0; } + + re_match_object = Qnil; i = re_match_2 (bufp, (char *) p1, s1, (char *) p2, s2, - point - BEGV, &search_regs, - ZV - BEGV); + PT - 1, &search_regs, + ZV - 1); if (i == -2) matcher_overflow (); @@ -312,6 +316,8 @@ string_match_1 (regexp, string, start, p ? DOWNCASE_TABLE : 0), posix); immediate_quit = 1; + re_match_object = string; + val = re_search (bufp, (char *) XSTRING (string)->data, XSTRING (string)->size, s, XSTRING (string)->size - s, &search_regs); @@ -361,6 +367,8 @@ fast_string_match (regexp, string) bufp = compile_pattern (regexp, 0, 0, 0); immediate_quit = 1; + re_match_object = string; + val = re_search (bufp, (char *) XSTRING (string)->data, XSTRING (string)->size, 0, XSTRING (string)->size, 0); @@ -635,171 +643,6 @@ find_before_next_newline (from, to, cnt) return pos; } -Lisp_Object skip_chars (); - -DEFUN ("skip-chars-forward", Fskip_chars_forward, Sskip_chars_forward, 1, 2, 0, - "Move point forward, stopping before a char not in STRING, or at pos LIM.\n\ -STRING is like the inside of a `[...]' in a regular expression\n\ -except that `]' is never special and `\\' quotes `^', `-' or `\\'.\n\ -Thus, with arg \"a-zA-Z\", this skips letters stopping before first nonletter.\n\ -With arg \"^a-zA-Z\", skips nonletters stopping before first letter.\n\ -Returns the distance traveled, either zero or positive.") - (string, lim) - Lisp_Object string, lim; -{ - return skip_chars (1, 0, string, lim); -} - -DEFUN ("skip-chars-backward", Fskip_chars_backward, Sskip_chars_backward, 1, 2, 0, - "Move point backward, stopping after a char not in STRING, or at pos LIM.\n\ -See `skip-chars-forward' for details.\n\ -Returns the distance traveled, either zero or negative.") - (string, lim) - Lisp_Object string, lim; -{ - return skip_chars (0, 0, string, lim); -} - -DEFUN ("skip-syntax-forward", Fskip_syntax_forward, Sskip_syntax_forward, 1, 2, 0, - "Move point forward across chars in specified syntax classes.\n\ -SYNTAX is a string of syntax code characters.\n\ -Stop before a char whose syntax is not in SYNTAX, or at position LIM.\n\ -If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.\n\ -This function returns the distance traveled, either zero or positive.") - (syntax, lim) - Lisp_Object syntax, lim; -{ - return skip_chars (1, 1, syntax, lim); -} - -DEFUN ("skip-syntax-backward", Fskip_syntax_backward, Sskip_syntax_backward, 1, 2, 0, - "Move point backward across chars in specified syntax classes.\n\ -SYNTAX is a string of syntax code characters.\n\ -Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.\n\ -If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.\n\ -This function returns the distance traveled, either zero or negative.") - (syntax, lim) - Lisp_Object syntax, lim; -{ - return skip_chars (0, 1, syntax, lim); -} - -Lisp_Object -skip_chars (forwardp, syntaxp, string, lim) - int forwardp, syntaxp; - Lisp_Object string, lim; -{ - register unsigned char *p, *pend; - register unsigned char c; - unsigned char fastmap[0400]; - int negate = 0; - register int i; - - CHECK_STRING (string, 0); - - if (NILP (lim)) - XSETINT (lim, forwardp ? ZV : BEGV); - else - CHECK_NUMBER_COERCE_MARKER (lim, 1); - - /* In any case, don't allow scan outside bounds of buffer. */ - /* jla turned this off, for no known reason. - bfox turned the ZV part on, and rms turned the - BEGV part back on. */ - if (XINT (lim) > ZV) - XSETFASTINT (lim, ZV); - if (XINT (lim) < BEGV) - XSETFASTINT (lim, BEGV); - - p = XSTRING (string)->data; - pend = p + XSTRING (string)->size; - bzero (fastmap, sizeof fastmap); - - if (p != pend && *p == '^') - { - negate = 1; p++; - } - - /* Find the characters specified and set their elements of fastmap. - If syntaxp, each character counts as itself. - Otherwise, handle backslashes and ranges specially */ - - while (p != pend) - { - c = *p++; - if (syntaxp) - fastmap[c] = 1; - else - { - if (c == '\\') - { - if (p == pend) break; - c = *p++; - } - if (p != pend && *p == '-') - { - p++; - if (p == pend) break; - while (c <= *p) - { - fastmap[c] = 1; - c++; - } - p++; - } - else - fastmap[c] = 1; - } - } - - if (syntaxp && fastmap['-'] != 0) - fastmap[' '] = 1; - - /* If ^ was the first character, complement the fastmap. */ - - if (negate) - for (i = 0; i < sizeof fastmap; i++) - fastmap[i] ^= 1; - - { - int start_point = point; - - immediate_quit = 1; - if (syntaxp) - { - - if (forwardp) - { - while (point < XINT (lim) - && fastmap[(unsigned char) syntax_code_spec[(int) SYNTAX (FETCH_CHAR (point))]]) - SET_PT (point + 1); - } - else - { - while (point > XINT (lim) - && fastmap[(unsigned char) syntax_code_spec[(int) SYNTAX (FETCH_CHAR (point - 1))]]) - SET_PT (point - 1); - } - } - else - { - if (forwardp) - { - while (point < XINT (lim) && fastmap[FETCH_CHAR (point)]) - SET_PT (point + 1); - } - else - { - while (point > XINT (lim) && fastmap[FETCH_CHAR (point - 1)]) - SET_PT (point - 1); - } - } - immediate_quit = 0; - - return make_number (point - start_point); - } -} - /* Subroutines of Lisp buffer search functions. */ static Lisp_Object @@ -826,7 +669,7 @@ search_command (string, bound, noerror, { CHECK_NUMBER_COERCE_MARKER (bound, 1); lim = XINT (bound); - if (n > 0 ? lim < point : lim > point) + if (n > 0 ? lim < PT : lim > PT) error ("Invalid search bound (wrong side of point)"); if (lim > ZV) lim = ZV; @@ -834,7 +677,7 @@ search_command (string, bound, noerror, lim = BEGV; } - np = search_buffer (string, point, lim, n, RE, + np = search_buffer (string, PT, lim, n, RE, (!NILP (current_buffer->case_fold_search) ? XCHAR_TABLE (current_buffer->case_canon_table)->contents : 0), @@ -965,35 +808,37 @@ search_buffer (string, pos, lim, n, RE, /* Get pointers and sizes of the two strings that make up the visible portion of the buffer. */ - p1 = BEGV_ADDR; - s1 = GPT - BEGV; + p1 = BEG_ADDR; + s1 = GPT - 1; p2 = GAP_END_ADDR; s2 = ZV - GPT; if (s1 < 0) { p2 = p1; - s2 = ZV - BEGV; + s2 = ZV - 1; s1 = 0; } if (s2 < 0) { - s1 = ZV - BEGV; + s1 = ZV - 1; s2 = 0; } + re_match_object = Qnil; + while (n < 0) { int val; val = re_search_2 (bufp, (char *) p1, s1, (char *) p2, s2, - pos - BEGV, lim - pos, &search_regs, + pos - 1, lim - pos, &search_regs, /* Don't allow match past current point */ - pos - BEGV); + pos - 1); if (val == -2) { matcher_overflow (); } if (val >= 0) { - j = BEGV; + j = 1; for (i = 0; i < search_regs.num_regs; i++) if (search_regs.start[i] >= 0) { @@ -1015,15 +860,15 @@ search_buffer (string, pos, lim, n, RE, { int val; val = re_search_2 (bufp, (char *) p1, s1, (char *) p2, s2, - pos - BEGV, lim - pos, &search_regs, - lim - BEGV); + pos - 1, lim - pos, &search_regs, + lim - 1); if (val == -2) { matcher_overflow (); } if (val >= 0) { - j = BEGV; + j = 1; for (i = 0; i < search_regs.num_regs; i++) if (search_regs.start[i] >= 0) { @@ -1183,7 +1028,7 @@ search_buffer (string, pos, lim, n, RE, while ((EMACS_INT) cursor <= (EMACS_INT) p_limit) cursor += BM_tab[*cursor]; else - while ((unsigned EMACS_INT) cursor <= (unsigned EMACS_INT) p_limit) + while ((EMACS_UINT) cursor <= (EMACS_UINT) p_limit) cursor += BM_tab[*cursor]; } else @@ -1192,7 +1037,7 @@ search_buffer (string, pos, lim, n, RE, while ((EMACS_INT) cursor >= (EMACS_INT) p_limit) cursor += BM_tab[*cursor]; else - while ((unsigned EMACS_INT) cursor >= (unsigned EMACS_INT) p_limit) + while ((EMACS_UINT) cursor >= (EMACS_UINT) p_limit) cursor += BM_tab[*cursor]; } /* If you are here, cursor is beyond the end of the searched region. */ @@ -1745,7 +1590,7 @@ since only regular expressions have dist for (pos = 0; pos < XSTRING (newtext)->size; pos++) { - int offset = point - search_regs.start[sub]; + int offset = PT - search_regs.start[sub]; c = XSTRING (newtext)->data[pos]; if (c == '\\') @@ -1773,13 +1618,13 @@ since only regular expressions have dist UNGCPRO; } - inslen = point - (search_regs.start[sub]); + inslen = PT - (search_regs.start[sub]); del_range (search_regs.start[sub] + inslen, search_regs.end[sub] + inslen); if (case_action == all_caps) - Fupcase_region (make_number (point - inslen), make_number (point)); + Fupcase_region (make_number (PT - inslen), make_number (PT)); else if (case_action == cap_initial) - Fupcase_initials_region (make_number (point - inslen), make_number (point)); + Fupcase_initials_region (make_number (PT - inslen), make_number (PT)); return Qnil; } @@ -1827,14 +1672,21 @@ Zero means the entire text matched by th return match_limit (subexp, 0); } -DEFUN ("match-data", Fmatch_data, Smatch_data, 0, 0, 0, +DEFUN ("match-data", Fmatch_data, Smatch_data, 0, 2, 0, "Return a list containing all info on what the last search matched.\n\ Element 2N is `(match-beginning N)'; element 2N + 1 is `(match-end N)'.\n\ All the elements are markers or nil (nil if the Nth pair didn't match)\n\ if the last match was on a buffer; integers or nil if a string was matched.\n\ -Use `store-match-data' to reinstate the data in this list.") - () +Use `store-match-data' to reinstate the data in this list.\n\ +\n\ +If INTEGERS (the optional first argument) is non-nil, always use integers\n\ +\(rather than markers) to represent buffer positions.\n\ +If REUSE is a list, reuse it as part of the value. If REUSE is long enough\n\ +to hold all the values, and if INTEGERS is non-nil, no consing is done.") + (integers, reuse) + Lisp_Object integers, reuse; { + Lisp_Object tail, prev; Lisp_Object *data; int i, len; @@ -1850,7 +1702,8 @@ Use `store-match-data' to reinstate the int start = search_regs.start[i]; if (start >= 0) { - if (EQ (last_thing_searched, Qt)) + if (EQ (last_thing_searched, Qt) + || ! NILP (integers)) { XSETFASTINT (data[2 * i], start); XSETFASTINT (data[2 * i + 1], search_regs.end[i]); @@ -1875,7 +1728,29 @@ Use `store-match-data' to reinstate the else data[2 * i] = data [2 * i + 1] = Qnil; } - return Flist (2 * len + 2, data); + + /* If REUSE is not usable, cons up the values and return them. */ + if (! CONSP (reuse)) + return Flist (2 * len + 2, data); + + /* If REUSE is a list, store as many value elements as will fit + into the elements of REUSE. */ + for (i = 0, tail = reuse; CONSP (tail); + i++, tail = XCONS (tail)->cdr) + { + if (i < 2 * len + 2) + XCONS (tail)->car = data[i]; + else + XCONS (tail)->car = Qnil; + prev = tail; + } + + /* If we couldn't fit all value elements into REUSE, + cons up the rest of them and add them to the end of REUSE. */ + if (i < 2 * len + 2) + XCONS (prev)->cdr = Flist (2 * len + 2 - i, data + i); + + return reuse; } @@ -2072,10 +1947,6 @@ syms_of_search () defsubr (&Sposix_looking_at); defsubr (&Sstring_match); defsubr (&Sposix_string_match); - defsubr (&Sskip_chars_forward); - defsubr (&Sskip_chars_backward); - defsubr (&Sskip_syntax_forward); - defsubr (&Sskip_syntax_backward); defsubr (&Ssearch_forward); defsubr (&Ssearch_backward); defsubr (&Sword_search_forward); @@ -2091,3 +1962,4 @@ syms_of_search () defsubr (&Sstore_match_data); defsubr (&Sregexp_quote); } + diff -pru emacs/19.33/src/syntax.c ./19.33\src/syntax.c --- emacs/19.33/src/syntax.c Tue Feb 27 22:00:24 1996 +++ ./19.33/src/syntax.c Sun Mar 1 04:03:00 1998 @@ -24,14 +24,27 @@ Boston, MA 02111-1307, USA. */ #include "lisp.h" #include "commands.h" #include "buffer.h" + +/* Make syntax table lookup grant data in gl_state. */ +#define SYNTAX_ENTRY_VIA_PROPERTY + #include "syntax.h" +#include "intervals.h" + +/* We use these constants in place for comment-style and + string-ender-char to distinguish comments/strings started by + comment_fence and string_fence codes. */ -Lisp_Object Qsyntax_table_p, Qsyntax_table; +#define ST_COMMENT_STYLE (256 + 1) +#define ST_STRING_STYLE (256 + 2) + +Lisp_Object Qsyntax_table_p, Qsyntax_table, Qscan_error; static void scan_sexps_forward (); static int char_quoted (); int words_include_escapes; +int parse_sexp_lookup_properties; /* Used as a temporary in SYNTAX_ENTRY and other macros in syntax.h, if not compiled with GCC. No need to mark it, since it is used @@ -42,16 +55,16 @@ Lisp_Object syntax_temp; struct lisp_parse_state { - int depth; /* Depth at end of parsing */ - int instring; /* -1 if not within string, else desired terminator. */ - int incomment; /* Nonzero if within a comment at end of parsing */ - int comstyle; /* comment style a=0, or b=1 */ + int depth; /* Depth at end of parsing. */ + int instring; /* -1 if not within string, else desired terminator. */ + int incomment; /* Nonzero if within a comment at end of parsing. */ + int comstyle; /* comment style a=0, or b=1, or ST_COMMENT_STYLE. */ int quoted; /* Nonzero if just after an escape char at end of parsing */ int thislevelstart; /* Char number of most recent start-of-expression at current level */ int prevlevelstart; /* Char number of start of containing expression */ - int location; /* Char number at which parsing stopped. */ + int location; /* Char number at which parsing stopped. */ int mindepth; /* Minimum depth seen while scanning. */ - int comstart; /* Position just after last comment starter. */ + int comstr_start; /* Position just after last comment/string starter. */ }; /* These variables are a cache for finding the start of a defun. @@ -66,10 +79,220 @@ static int find_start_value; static struct buffer *find_start_buffer; static int find_start_begv; static int find_start_modiff; + + +struct gl_state_s gl_state; /* Global state of syntax parser. */ + +INTERVAL interval_of (); +#define INTERVALS_AT_ONCE 10 /* 1 + max-number of intervals + to scan to property-change. */ + +/* + Update gl_state to an appropriate interval which contains POS. The + sign of COUNT give the relative position of POS wrt the previously + valid interval. If INIT, only [be]_property fields of gl_state are + valid at start, the rest is filled basing on OBJECT. + + `gl_state.*_i' are the intervals, and pos is further in the search + direction than the intervals - or in an interval. We update the + current syntax-table basing on the property of this interval, and + update the interval to start further than POS - or be + NULL_INTERVAL. We also update lim_property to be the next value of + pos to call this subroutine again - or be before/after the + start/end of OBJECT. */ + +void +update_syntax_table (pos, count, init, object) + int pos, count, init; + Lisp_Object object; +{ + Lisp_Object tmp_table; + int cnt = 0, doing_extra = 0, invalidate = 1; + INTERVAL i, oldi; + + if (init) + { + gl_state.start = gl_state.b_property; + gl_state.stop = gl_state.e_property; + gl_state.forward_i = interval_of (pos, object); + i = gl_state.backward_i = gl_state.forward_i; + gl_state.left_ok = gl_state.right_ok = 1; + invalidate = 0; + if (NULL_INTERVAL_P (i)) + return; + /* interval_of () updates only ->position of the return value, + update the parents manually to speed up update_interval. */ + while (!NULL_PARENT (i)) + { + if (AM_RIGHT_CHILD (i)) + i->parent->position = i->position + - LEFT_TOTAL_LENGTH (i) + TOTAL_LENGTH (i) /* right end */ + - TOTAL_LENGTH (i->parent) + + LEFT_TOTAL_LENGTH (i->parent); + else + i->parent->position = i->position - LEFT_TOTAL_LENGTH (i) + + TOTAL_LENGTH (i); + i = i->parent; + } + i = gl_state.forward_i; + gl_state.b_property = i->position - 1; + gl_state.e_property = INTERVAL_LAST_POS (i); + goto update; + } + oldi = i = count > 0 ? gl_state.forward_i : gl_state.backward_i; + + /* We are guarantied to be called with pos either in i, of further off. */ + if (NULL_INTERVAL_P (i)) + error ("Error in syntax_table logic for to-the-end intervals"); + else if (pos < i->position) /* Move left. */ + { + if (count > 0) + error ("Error in syntax_table logic for intervals <-."); + /* Update the interval. */ + i = update_interval (i, pos); + if (oldi->position != INTERVAL_LAST_POS (i)) + { + invalidate = 0; + gl_state.right_ok = 1; /* Invalidate the other end. */ + gl_state.forward_i = i; + gl_state.e_property = INTERVAL_LAST_POS (i); + } + } + else if (pos >= INTERVAL_LAST_POS (i)) /* Move right. */ + { + if (count < 0) + error ("Error in syntax_table logic for intervals ->."); + /* Update the interval. */ + i = update_interval (i, pos); + if (i->position != INTERVAL_LAST_POS (oldi)) + { + invalidate = 0; + gl_state.left_ok = 1; /* Invalidate the other end. */ + gl_state.backward_i = i; + gl_state.b_property = i->position - 1; + } + } + else if (count > 0 ? gl_state.right_ok : gl_state.left_ok) + { + /* We do not need to recalculate tmp_table. */ + tmp_table = gl_state.old_prop; + } + + update: + tmp_table = textget (i->plist, Qsyntax_table); + + if (invalidate) + invalidate = !EQ (tmp_table, gl_state.old_prop); /* Need to invalidate? */ + + if (invalidate) /* Did not get to adjacent interval. */ + { /* with the same table => */ + /* invalidate the old range. */ + if (count > 0) + { + gl_state.backward_i = i; + gl_state.left_ok = 1; /* Invalidate the other end. */ + gl_state.b_property = i->position - 1; + } + else + { + gl_state.forward_i = i; + gl_state.right_ok = 1; /* Invalidate the other end. */ + gl_state.e_property = INTERVAL_LAST_POS (i); + } + } + + gl_state.current_syntax_table = tmp_table; + gl_state.old_prop = tmp_table; + if (Fsyntax_table_p (tmp_table) == Qt) + { + gl_state.use_global = 0; + } + else if (CONSP (tmp_table)) + { + gl_state.use_global = 1; + gl_state.global_code = tmp_table; + } + else + { + gl_state.use_global = 0; + gl_state.current_syntax_table = current_buffer->syntax_table; + } + + while (!NULL_INTERVAL_P (i)) + { + if (cnt && !EQ (tmp_table, textget (i->plist, Qsyntax_table))) + { + if (count > 0) + gl_state.right_ok = 0; + else + gl_state.left_ok = 0; + break; + } + else if (cnt == INTERVALS_AT_ONCE) + { + if (count > 0) + gl_state.right_ok = 1; + else + gl_state.left_ok = 1; + break; + } + cnt++; + i = count > 0 ? next_interval (i) : previous_interval (i); + } + if (NULL_INTERVAL_P (i)) + { /* This property goes to the end. */ + if (count > 0) + gl_state.e_property = gl_state.stop; + else + gl_state.b_property = gl_state.start; + } + else + { + if (count > 0) + { + gl_state.e_property = i->position; + gl_state.forward_i = i; + } + else + { + gl_state.b_property = i->position + LENGTH (i) - 1; + gl_state.backward_i = i; + } + } +} + +/* Returns TRUE if char at POS is quoted. + Global syntax-table data should be set up already. + On return global syntax data is good for lookup at POS. */ +static int +char_quoted (pos) + register int pos; +{ + register enum syntaxcode code; + register int beg = BEGV; + register int quoted = 0; + int oldp = pos; + + pos--; + while (pos >= beg + && ( UPDATE_SYNTAX_TABLE_BACKWARD (pos), 1) + && ((code = SYNTAX (FETCH_CHAR (pos))) == Scharquote + || code == Sescape)) + { + pos--, quoted = !quoted; + } + UPDATE_SYNTAX_TABLE (oldp); + return quoted; +} + /* Find a defun-start that is the last one before POS (or nearly the last). We record what we find, so that another call in the same area - can return the same value right away. */ + can return the same value right away. + + There is no promise at which position the global syntax data is + valid on return from the subroutine, so the caller should explicitly + update the global data. */ static int find_defun_start (pos) @@ -92,11 +315,23 @@ find_defun_start (pos) /* Back up to start of line. */ tem = scan_buffer ('\n', pos, BEGV, -1, &shortage, 1); + /* We optimize syntax-table lookup for rare updates. Thus we accept + only those `^\s(' which are good in global _and_ text-property + syntax-tables. */ + gl_state.current_syntax_table = current_buffer->syntax_table; + gl_state.use_global = 0; while (tem > BEGV) { /* Open-paren at start of line means we found our defun-start. */ if (SYNTAX (FETCH_CHAR (tem)) == Sopen) - break; + { + SETUP_SYNTAX_TABLE (tem + 1, -1); /* Try again... */ + if (SYNTAX (FETCH_CHAR (tem)) == Sopen) + break; + /* Now fallback to the default value. */ + gl_state.current_syntax_table = current_buffer->syntax_table; + gl_state.use_global = 0; + } /* Move to beg of previous line. */ tem = scan_buffer ('\n', tem, BEGV, -2, &shortage, 1); } @@ -111,6 +346,158 @@ find_defun_start (pos) return find_start_value; } +/* Checks whether FROM is the end of comment. Does not try to + fallback more than to STOP. + Returns -1 if cannot find comment ending at from, otherwise start + of comment. Global syntax data remains valid for + backward search starting at the returned value (or at FROM, if + the search was not successful). */ + +static int +back_comment (from, stop) + int from, stop; +{ + /* Look back, counting the parity of string-quotes, + and recording the comment-starters seen. + When we reach a safe place, assume that's not in a string; + then step the main scan to the earliest comment-starter seen + an even number of string quotes away from the safe place. + + OFROM[I] is position of the earliest comment-starter seen + which is I+2X quotes from the comment-end. + PARITY is current parity of quotes from the comment end. */ + int parity = 0; + int my_stringend = 0; + int string_lossage = 0; + int comment_end = from; + int comstart_pos = 0; + int comstart_parity = 0; + int scanstart = from - 1; + register enum syntaxcode code; + int c, comstyle = 0; + + /* At beginning of range to scan, we're outside of strings; + that determines quote parity to the comment-end. */ + while (from != stop) + { + /* Move back and examine a character. */ + from--; + UPDATE_SYNTAX_TABLE_BACKWARD (from); + + c = FETCH_CHAR (from); + code = SYNTAX (c); + + /* If this char is the second of a 2-char comment sequence, + back up and give the pair the appropriate syntax. */ + if (from > stop && SYNTAX_COMEND_SECOND (c) + && SYNTAX_COMEND_FIRST (FETCH_CHAR (from - 1))) + { + code = Sendcomment; + from--; + /* This is apparently the best we can do: */ + UPDATE_SYNTAX_TABLE_BACKWARD (from); + c = FETCH_CHAR (from); + } + + /* If this char starts a 2-char comment start sequence, + treat it like a 1-char comment starter. */ + if (from < scanstart && SYNTAX_COMSTART_FIRST (c) + && SYNTAX_COMSTART_SECOND (FETCH_CHAR (from + 1)) + && comstyle == SYNTAX_COMMENT_STYLE (FETCH_CHAR (from + 1))) + code = Scomment; + + /* Ignore escaped characters. */ + if (char_quoted (from)) + continue; + + /* Track parity of quotes. */ + if (code == Sstring) + { + parity ^= 1; + if (my_stringend == 0) + my_stringend = c; + /* If we have two kinds of string delimiters. + There's no way to grok this scanning backwards. */ + else if (my_stringend != c) + string_lossage = 1; + } + + if (code == Sstring_fence || code == Scomment_fence) + { + parity ^= 1; + if (my_stringend == 0) + my_stringend = + code == Sstring_fence ? ST_STRING_STYLE : ST_COMMENT_STYLE; + /* If we have two kinds of string delimiters. + There's no way to grok this scanning backwards. */ + else if (my_stringend != (code == Sstring_fence + ? ST_STRING_STYLE : ST_COMMENT_STYLE)) + string_lossage = 1; + } + + /* Record comment-starters according to that + quote-parity to the comment-end. */ + if (code == Scomment) + { + comstart_parity = parity; + comstart_pos = from; + } + + /* If we find another earlier comment-ender, + any comment-starts earlier than that don't count + (because they go with the earlier comment-ender). */ + if (code == Sendcomment + && SYNTAX_COMMENT_STYLE (FETCH_CHAR (from)) == comstyle) + break; + + /* Assume a defun-start point is outside of strings. */ + if (code == Sopen + && (from == stop || FETCH_CHAR (from - 1) == '\n')) + break; + } + + if (comstart_pos == 0) + { + from = comment_end; + UPDATE_SYNTAX_TABLE_FORWARD (comment_end - 1); + } + /* If the earliest comment starter + is followed by uniform paired string quotes or none, + we know it can't be inside a string + since if it were then the comment ender would be inside one. + So it does start a comment. Skip back to it. */ + else if (comstart_parity == 0 && !string_lossage) + { + from = comstart_pos; + /* Globals are correct now. */ + } + else + { + /* We had two kinds of string delimiters mixed up + together. Decode this going forwards. + Scan fwd from the previous comment ender + to the one in question; this records where we + last passed a comment starter. */ + struct lisp_parse_state state; + scan_sexps_forward (&state, find_defun_start (comment_end), + comment_end - 1, -10000, 0, Qnil, 0); + if (state.incomment) + { + /* scan_sexps_forward changed the direction of search in + global variables, so we need to update it completely. */ + + from = state.comstr_start; + } + else + { + from = comment_end; + } + UPDATE_SYNTAX_TABLE_FORWARD (from - 1); + } + + return from; +} + DEFUN ("syntax-table-p", Fsyntax_table_p, Ssyntax_table_p, 1, 1, 0, "Return t if OBJECT is a syntax table.\n\ Currently, any char-table counts as a syntax table.") @@ -192,14 +579,14 @@ One argument, a syntax table.") /* Convert a letter which signifies a syntax code into the code it signifies. - This is used by modify-syntax-entry, and other things. */ + This is used by modify-syntax-entry, and other things. */ unsigned char syntax_spec_code[0400] = { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, - (char) Swhitespace, 0377, (char) Sstring, 0377, + (char) Swhitespace, (char) Scomment_fence, (char) Sstring, 0377, (char) Smath, 0377, 0377, (char) Squote, (char) Sopen, (char) Sclose, 0377, 0377, 0377, (char) Swhitespace, (char) Spunct, (char) Scharquote, @@ -213,14 +600,15 @@ unsigned char syntax_spec_code[0400] = 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */ 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword, - 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377 + 0377, 0377, 0377, 0377, (char) Sstring_fence, 0377, 0377, 0377 }; -/* Indexed by syntax code, give the letter that describes it. */ +/* Indexed by syntax code, give the letter that describes it. */ -char syntax_code_spec[14] = +char syntax_code_spec[16] = { - ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@' + ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@', + '!', '|' }; /* Look up the value for CHARACTER in syntax table TABLE's parent @@ -256,6 +644,9 @@ are listed in the documentation of `modi Lisp_Object character; { int char_int; + gl_state.current_syntax_table = current_buffer->syntax_table; + + gl_state.use_global = 0; CHECK_NUMBER (character, 0); char_int = XINT (character); return make_number (syntax_code_spec[(int) SYNTAX (char_int)]); @@ -267,6 +658,8 @@ DEFUN ("matching-paren", Fmatching_paren Lisp_Object character; { int char_int, code; + gl_state.current_syntax_table = current_buffer->syntax_table; + gl_state.use_global = 0; CHECK_NUMBER (character, 0); char_int = XINT (character); code = SYNTAX (char_int); @@ -303,7 +696,7 @@ Defined flags are the characters 1, 2, 3 3 means CHAR is the start of a two-char comment end sequence.\n\ 4 means CHAR is the second character of such a sequence.\n\ \n\ -There can be up to two orthogonal comment sequences. This is to support\n\ +There can be up to two orthogonal comment sequences. This is to support\n\ language modes such as C++. By default, all comment sequences are of style\n\ a, but you can set the comment sequence style to b (on the second character\n\ of a comment-start, or the first character of a comment-end sequence) using\n\ @@ -545,6 +938,8 @@ The descriptions are inserted in a buffe return Qnil; } +int parse_sexp_ignore_comments; + /* Return the position across COUNT words from FROM. If that many words cannot be found before the end of the buffer, return 0. COUNT negative means scan backward and stop at word beginning. */ @@ -560,6 +955,8 @@ scan_words (from, count) immediate_quit = 1; QUIT; + SETUP_SYNTAX_TABLE (from, count); + while (count > 0) { while (1) @@ -569,6 +966,7 @@ scan_words (from, count) immediate_quit = 0; return 0; } + UPDATE_SYNTAX_TABLE_FORWARD (from); charcode = FETCH_CHAR (from); code = SYNTAX (charcode); if (words_include_escapes @@ -581,6 +979,7 @@ scan_words (from, count) while (1) { if (from == end) break; + UPDATE_SYNTAX_TABLE_FORWARD (from); charcode = FETCH_CHAR (from); code = SYNTAX (charcode); if (!(words_include_escapes @@ -600,6 +999,7 @@ scan_words (from, count) immediate_quit = 0; return 0; } + UPDATE_SYNTAX_TABLE_BACKWARD (from - 1); charcode = FETCH_CHAR (from - 1); code = SYNTAX (charcode); if (words_include_escapes @@ -612,6 +1012,7 @@ scan_words (from, count) while (1) { if (from == beg) break; + UPDATE_SYNTAX_TABLE_BACKWARD (from - 1); charcode = FETCH_CHAR (from - 1); code = SYNTAX (charcode); if (!(words_include_escapes @@ -639,7 +1040,7 @@ and nil is returned.") int val; CHECK_NUMBER (count, 0); - if (!(val = scan_words (point, XINT (count)))) + if (!(val = scan_words (PT, XINT (count)))) { SET_PT (XINT (count) > 0 ? ZV : BEGV); return Qnil; @@ -648,6 +1049,180 @@ and nil is returned.") return Qt; } +Lisp_Object skip_chars (); + +DEFUN ("skip-chars-forward", Fskip_chars_forward, Sskip_chars_forward, 1, 2, 0, + "Move point forward, stopping before a char not in STRING, or at pos LIM.\n\ +STRING is like the inside of a `[...]' in a regular expression\n\ +except that `]' is never special and `\\' quotes `^', `-' or `\\'.\n\ +Thus, with arg \"a-zA-Z\", this skips letters stopping before first nonletter.\n\ +With arg \"^a-zA-Z\", skips nonletters stopping before first letter.\n\ +Returns the distance traveled, either zero or positive.") + (string, lim) + Lisp_Object string, lim; +{ + return skip_chars (1, 0, string, lim); +} + +DEFUN ("skip-chars-backward", Fskip_chars_backward, Sskip_chars_backward, 1, 2, 0, + "Move point backward, stopping after a char not in STRING, or at pos LIM.\n\ +See `skip-chars-forward' for details.\n\ +Returns the distance traveled, either zero or negative.") + (string, lim) + Lisp_Object string, lim; +{ + return skip_chars (0, 0, string, lim); +} + +DEFUN ("skip-syntax-forward", Fskip_syntax_forward, Sskip_syntax_forward, 1, 2, 0, + "Move point forward across chars in specified syntax classes.\n\ +SYNTAX is a string of syntax code characters.\n\ +Stop before a char whose syntax is not in SYNTAX, or at position LIM.\n\ +If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.\n\ +This function returns the distance traveled, either zero or positive.") + (syntax, lim) + Lisp_Object syntax, lim; +{ + return skip_chars (1, 1, syntax, lim); +} + +DEFUN ("skip-syntax-backward", Fskip_syntax_backward, Sskip_syntax_backward, 1, 2, 0, + "Move point backward across chars in specified syntax classes.\n\ +SYNTAX is a string of syntax code characters.\n\ +Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.\n\ +If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.\n\ +This function returns the distance traveled, either zero or negative.") + (syntax, lim) + Lisp_Object syntax, lim; +{ + return skip_chars (0, 1, syntax, lim); +} + +Lisp_Object +skip_chars (forwardp, syntaxp, string, lim) + int forwardp, syntaxp; + Lisp_Object string, lim; +{ + register unsigned char *p, *pend; + register unsigned char c; + unsigned char fastmap[0400]; + int negate = 0; + register int i; + + CHECK_STRING (string, 0); + + if (NILP (lim)) + XSETINT (lim, forwardp ? ZV : BEGV); + else + CHECK_NUMBER_COERCE_MARKER (lim, 1); + + /* In any case, don't allow scan outside bounds of buffer. */ + /* jla turned this off, for no known reason. + bfox turned the ZV part on, and rms turned the + BEGV part back on. */ + if (XINT (lim) > ZV) + XSETFASTINT (lim, ZV); + if (XINT (lim) < BEGV) + XSETFASTINT (lim, BEGV); + + p = XSTRING (string)->data; + pend = p + XSTRING (string)->size; + bzero (fastmap, sizeof fastmap); + + if (p != pend && *p == '^') + { + negate = 1; p++; + } + + /* Find the characters specified and set their elements of fastmap. + If syntaxp, each character counts as itself. + Otherwise, handle backslashes and ranges specially. */ + + while (p != pend) + { + c = *p++; + if (syntaxp) + fastmap[syntax_spec_code[c]] = 1; + else + { + if (c == '\\') + { + if (p == pend) break; + c = *p++; + } + if (p != pend && *p == '-') + { + p++; + if (p == pend) break; + while (c <= *p) + { + fastmap[c] = 1; + c++; + } + p++; + } + else + fastmap[c] = 1; + } + } + + /* If ^ was the first character, complement the fastmap. */ + + if (negate) + for (i = 0; i < sizeof fastmap; i++) + fastmap[i] ^= 1; + + { + int start_point = PT; + int pos = PT; + + immediate_quit = 1; + if (syntaxp) + { + SETUP_SYNTAX_TABLE (pos, forwardp ? 1 : -1); + if (forwardp) + { + if (pos < XINT (lim)) + while (fastmap[(int) SYNTAX (FETCH_CHAR (pos))]) + { + pos++; + if (pos >= XINT (lim)) + break; + UPDATE_SYNTAX_TABLE_FORWARD (pos); + } + } + else + { + if (pos > XINT (lim)) + while (fastmap[(int) SYNTAX (FETCH_CHAR (pos - 1))]) + { + pos--; + if (pos <= XINT (lim)) + break; + UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1); + } + } + } + else + { + if (forwardp) + { + while (pos < XINT (lim) && fastmap[FETCH_CHAR (pos)]) + pos++; + } + else + { + while (pos > XINT (lim) && fastmap[FETCH_CHAR (pos - 1)]) + pos--; + } + } + SET_PT (pos); + immediate_quit = 0; + + return make_number (PT - start_point); + } +} + DEFUN ("forward-comment", Fforward_comment, Sforward_comment, 1, 1, 0, "Move forward across up to N comments. If N is negative, move backward.\n\ Stop scanning if we find something other than a comment or whitespace.\n\ @@ -663,19 +1238,20 @@ between them, return t; otherwise return register enum syntaxcode code; int comstyle = 0; /* style of comment encountered */ int found; - int count1; + int icount; CHECK_NUMBER (count, 0); - count1 = XINT (count); + icount = XINT (count); + stop = icount > 0 ? ZV : BEGV; immediate_quit = 1; QUIT; from = PT; - while (count1 > 0) + SETUP_SYNTAX_TABLE (from, icount); + while (icount > 0) { - stop = ZV; do { if (from == stop) @@ -684,6 +1260,7 @@ between them, return t; otherwise return immediate_quit = 0; return Qnil; } + UPDATE_SYNTAX_TABLE_FORWARD (from); c = FETCH_CHAR (from); code = SYNTAX (c); from++; @@ -703,7 +1280,7 @@ between them, return t; otherwise return } } while (code == Swhitespace || code == Sendcomment); - if (code != Scomment) + if (code != Scomment && code != Scomment_fence) { immediate_quit = 0; SET_PT (from - 1); @@ -718,6 +1295,7 @@ between them, return t; otherwise return SET_PT (from); return Qnil; } + UPDATE_SYNTAX_TABLE_FORWARD (from); c = FETCH_CHAR (from); from++; if (SYNTAX (c) == Sendcomment @@ -726,6 +1304,12 @@ between them, return t; otherwise return as the comment sequence which began this comment section */ break; + if (SYNTAX (c) == Scomment_fence + && comstyle == ST_COMMENT_STYLE) + /* we have encountered a comment end of the same style + as the comment sequence which began this comment + section. */ + break; if (from < stop && SYNTAX_COMEND_FIRST (c) && (c1 = FETCH_CHAR (from), SYNTAX_COMEND_SECOND (c1)) @@ -736,12 +1320,11 @@ between them, return t; otherwise return { from++; break; } } /* We have skipped one comment. */ - count1--; + icount--; } - while (count1 < 0) + while (icount < 0) { - stop = BEGV; while (from > stop) { int quoted; @@ -749,7 +1332,11 @@ between them, return t; otherwise return from--; quoted = char_quoted (from); if (quoted) - from--; + { + from--; + goto leave; + } + UPDATE_SYNTAX_TABLE_BACKWARD (from); c = FETCH_CHAR (from); code = SYNTAX (c); comstyle = 0; @@ -768,7 +1355,28 @@ between them, return t; otherwise return from--; } - if (code == Sendcomment && !quoted) + if (code == Scomment_fence) + { + /* Skip until first preceding unquoted comment_fence. */ + int found = 0, ini = from; + + while (--from != stop) + { + UPDATE_SYNTAX_TABLE_BACKWARD (from); + c = FETCH_CHAR (from); + if (SYNTAX (c) == Scomment_fence && !char_quoted (from)) + { + found = 1; + break; + } + } + if (found == 0) + { + from = ini; /* Set point to ini + 1. */ + goto leave; + } + } + else if (code == Sendcomment) { #if 0 if (code != SYNTAX (c)) @@ -799,7 +1407,9 @@ between them, return t; otherwise return break; } #endif /* 0 */ - + found = back_comment (from, stop); + if (found != -1) from = found; +#if 0 /* Look back, counting the parity of string-quotes, and recording the comment-starters seen. When we reach a safe place, assume that's not in a string; @@ -825,6 +1435,7 @@ between them, return t; otherwise return /* Move back and examine a character. */ from--; + UPDATE_SYNTAX_TABLE_BACKWARD (from); c = FETCH_CHAR (from); code = SYNTAX (c); @@ -902,24 +1513,26 @@ between them, return t; otherwise return scan_sexps_forward (&state, find_defun_start (comment_end), comment_end - 1, -10000, 0, Qnil, 0); if (state.incomment) - from = state.comstart; + from = state.comstr_start; else /* We can't grok this as a comment; scan it normally. */ from = comment_end; } } +#endif /* 0 */ /* We have skipped one comment. */ break; } - else if ((code != Swhitespace && code != Scomment) || quoted) + else if (code != Swhitespace && code != Scomment) { + leave: immediate_quit = 0; SET_PT (from + 1); return Qnil; } } - count1++; + icount++; } SET_PT (from); @@ -927,49 +1540,52 @@ between them, return t; otherwise return return Qt; } -int parse_sexp_ignore_comments; - Lisp_Object scan_lists (from, count, depth, sexpflag) register int from; int count, depth, sexpflag; { Lisp_Object val; - register int stop; + register int stop = count > 0 ? ZV : BEGV; register int c; unsigned char stringterm; int quoted; int mathexit = 0; register enum syntaxcode code; - int min_depth = depth; /* Err out if depth gets less than this. */ + int min_depth = depth; /* Err out if depth gets less than this. */ int comstyle = 0; /* style of comment encountered */ + int last_good = from; + int found; if (depth > 0) min_depth = 0; immediate_quit = 1; QUIT; + SETUP_SYNTAX_TABLE (from, count); while (count > 0) { - stop = ZV; while (from < stop) { + UPDATE_SYNTAX_TABLE_FORWARD (from); c = FETCH_CHAR (from); code = SYNTAX (c); + if (depth == min_depth) + last_good = from; from++; if (from < stop && SYNTAX_COMSTART_FIRST (c) && SYNTAX_COMSTART_SECOND (FETCH_CHAR (from)) && parse_sexp_ignore_comments) { /* we have encountered a comment start sequence and we - are ignoring all text inside comments. we must record + are ignoring all text inside comments. We must record the comment style this sequence begins so that later, only a comment end of the same style actually ends the comment section */ code = Scomment; comstyle = SYNTAX_COMMENT_STYLE (FETCH_CHAR (from)); from++; - } + } if (SYNTAX_PREFIX (c)) continue; @@ -984,9 +1600,10 @@ scan_lists (from, count, depth, sexpflag case Sword: case Ssymbol: if (depth || !sexpflag) break; - /* This word counts as a sexp; return at end of it. */ + /* This word counts as a sexp; return at end of it. */ while (from < stop) { + UPDATE_SYNTAX_TABLE_FORWARD (from); switch (SWITCH_ENUM_CAST (SYNTAX (FETCH_CHAR (from)))) { case Scharquote: @@ -1006,6 +1623,7 @@ scan_lists (from, count, depth, sexpflag goto done; case Scomment: + case Scomment_fence: if (!parse_sexp_ignore_comments) break; while (1) { @@ -1015,17 +1633,21 @@ scan_lists (from, count, depth, sexpflag goto done; goto lose; } + UPDATE_SYNTAX_TABLE_FORWARD (from); c = FETCH_CHAR (from); - if (SYNTAX (c) == Sendcomment - && SYNTAX_COMMENT_STYLE (c) == comstyle) + from++; + if (code == Scomment + ? (SYNTAX (c) == Sendcomment + && SYNTAX_COMMENT_STYLE (c) == comstyle) + : (SYNTAX (c) == Scomment_fence)) /* we have encountered a comment end of the same style as the comment sequence which began this comment section */ break; - from++; if (from < stop && SYNTAX_COMEND_FIRST (c) && SYNTAX_COMEND_SECOND (FETCH_CHAR (from)) - && SYNTAX_COMMENT_STYLE (c) == comstyle) + && SYNTAX_COMMENT_STYLE (c) == comstyle + && code == Scomment) /* we have encountered a comment end of the same style as the comment sequence which began this comment section */ @@ -1053,15 +1675,23 @@ scan_lists (from, count, depth, sexpflag close1: if (!--depth) goto done; if (depth < min_depth) - error ("Containing expression ends prematurely"); + Fsignal (Qscan_error, + Fcons (build_string ("Containing expression ends prematurely"), + Fcons (make_number (last_good), + Fcons (make_number (from), Qnil)))); break; case Sstring: + case Sstring_fence: stringterm = FETCH_CHAR (from - 1); while (1) { if (from >= stop) goto lose; - if (FETCH_CHAR (from) == stringterm) break; + UPDATE_SYNTAX_TABLE_FORWARD (from); + if (code == Sstring + ? (FETCH_CHAR (from) == stringterm) + : SYNTAX (FETCH_CHAR (from)) == Sstring_fence) + break; switch (SWITCH_ENUM_CAST (SYNTAX (FETCH_CHAR (from)))) { case Scharquote: @@ -1089,14 +1719,19 @@ scan_lists (from, count, depth, sexpflag while (count < 0) { - stop = BEGV; while (from > stop) { from--; + UPDATE_SYNTAX_TABLE_BACKWARD (from); if (quoted = char_quoted (from)) - from--; + { + from--; + UPDATE_SYNTAX_TABLE_BACKWARD (from); + } c = FETCH_CHAR (from); code = SYNTAX (c); + if (depth == min_depth) + last_good = from; comstyle = 0; if (code == Sendcomment) comstyle = SYNTAX_COMMENT_STYLE (c); @@ -1121,23 +1756,34 @@ scan_lists (from, count, depth, sexpflag case Sword: case Ssymbol: if (depth || !sexpflag) break; - /* This word counts as a sexp; count object finished after passing it. */ + /* This word counts as a sexp; count object finished + after passing it. */ while (from > stop) { - quoted = char_quoted (from - 1); - if (quoted) - from--; - if (! (quoted || SYNTAX (FETCH_CHAR (from - 1)) == Sword - || SYNTAX (FETCH_CHAR (from - 1)) == Ssymbol - || SYNTAX (FETCH_CHAR (from - 1)) == Squote)) - goto done2; from--; + UPDATE_SYNTAX_TABLE_BACKWARD (from); + quoted = char_quoted (from); + if (quoted) + { + from--; + UPDATE_SYNTAX_TABLE_BACKWARD (from); + } + if (! (quoted || SYNTAX (FETCH_CHAR (from)) == Sword + || SYNTAX (FETCH_CHAR (from)) == Ssymbol + || SYNTAX (FETCH_CHAR (from)) == Squote)) + /* Updating `from' here will not desyncronize + intervals, since it will be immediately --. */ + { + from++; + goto done2; + } } goto done2; case Smath: if (!sexpflag) break; + UPDATE_SYNTAX_TABLE_BACKWARD (from - 1); if (from != stop && c == FETCH_CHAR (from - 1)) from--; if (mathexit) @@ -1155,7 +1801,10 @@ scan_lists (from, count, depth, sexpflag open2: if (!--depth) goto done2; if (depth < min_depth) - error ("Containing expression ends prematurely"); + Fsignal (Qscan_error, + Fcons (build_string ("Containing expression ends prematurely"), + Fcons (make_number (last_good), + Fcons (make_number (from), Qnil)))); break; case Sendcomment: @@ -1188,7 +1837,9 @@ scan_lists (from, count, depth, sexpflag break; } #endif /* 0 */ - + found = back_comment (from, stop); + if (found != -1) from = found; +#if 0 /* Look back, counting the parity of string-quotes, and recording the comment-starters seen. When we reach a safe place, assume that's not in a string; @@ -1213,6 +1864,7 @@ scan_lists (from, count, depth, sexpflag { /* Move back and examine a character. */ from--; + UPDATE_SYNTAX_TABLE_BACKWARD (from); c = FETCH_CHAR (from); code = SYNTAX (c); @@ -1224,6 +1876,7 @@ scan_lists (from, count, depth, sexpflag { code = Sendcomment; from--; + UPDATE_SYNTAX_TABLE_BACKWARD (from); c = FETCH_CHAR (from); } @@ -1291,25 +1944,40 @@ scan_lists (from, count, depth, sexpflag scan_sexps_forward (&state, find_defun_start (comment_end), comment_end - 1, -10000, 0, Qnil, 0); if (state.incomment) - from = state.comstart; + from = state.comstr_start; else /* We can't grok this as a comment; scan it normally. */ from = comment_end; } } +#endif /* 0 */ break; + case Scomment_fence: + case Sstring_fence: + while (1) + { + from--; + if (from == stop) goto lose; + UPDATE_SYNTAX_TABLE_BACKWARD (from); + if (!char_quoted (from) + && SYNTAX (FETCH_CHAR (from)) == code) + break; + } + if (code == Sstring_fence && !depth && sexpflag) goto done2; + break; + case Sstring: stringterm = FETCH_CHAR (from); while (1) { + from--; if (from == stop) goto lose; - if (!char_quoted (from - 1) - && stringterm == FETCH_CHAR (from - 1)) + UPDATE_SYNTAX_TABLE_BACKWARD (from); + if (!char_quoted (from) + && stringterm == FETCH_CHAR (from)) break; - from--; } - from--; if (!depth && sexpflag) goto done2; break; } @@ -1331,23 +1999,12 @@ scan_lists (from, count, depth, sexpflag return val; lose: - error ("Unbalanced parentheses"); - /* NOTREACHED */ -} + Fsignal (Qscan_error, + Fcons (build_string ("Unbalanced parentheses"), + Fcons (make_number (last_good), + Fcons (make_number (from), Qnil)))); -static int -char_quoted (pos) - register int pos; -{ - register enum syntaxcode code; - register int beg = BEGV; - register int quoted = 0; - - while (pos > beg - && ((code = SYNTAX (FETCH_CHAR (pos - 1))) == Scharquote - || code == Sescape)) - pos--, quoted = !quoted; - return quoted; + /* NOTREACHED */ } DEFUN ("scan-lists", Fscan_lists, Sscan_lists, 3, 3, 0, @@ -1401,14 +2058,23 @@ This includes chars with \"quote\" or \" () { int beg = BEGV; - int pos = point; + int pos = PT; + + if (pos > beg) + { + SETUP_SYNTAX_TABLE (pos, -1); + } + pos--; - while (pos > beg && !char_quoted (pos - 1) - && (SYNTAX (FETCH_CHAR (pos - 1)) == Squote - || SYNTAX_PREFIX (FETCH_CHAR (pos - 1)))) - pos--; + while (pos >= beg && !char_quoted (pos) + && (SYNTAX (FETCH_CHAR (pos)) == Squote + || SYNTAX_PREFIX (FETCH_CHAR (pos)))) + { + pos--; + UPDATE_SYNTAX_TABLE_BACKWARD (pos); + } - SET_PT (pos); + SET_PT (pos + 1); return Qnil; } @@ -1442,16 +2108,21 @@ scan_sexps_forward (stateptr, from, end, int mindepth; /* Lowest DEPTH value seen. */ int start_quoted = 0; /* Nonzero means starting after a char quote */ Lisp_Object tem; + int boundary_stop = commentstop == -1; + int nofence; immediate_quit = 1; QUIT; + SETUP_SYNTAX_TABLE (from, 1); + if (NILP (oldstate)) { depth = 0; state.instring = -1; state.incomment = 0; - state.comstyle = 0; /* comment style a by default */ + state.comstyle = 0; /* comment style a by default. */ + state.comstr_start = -1; /* no comment/string seen. */ } else { @@ -1465,7 +2136,10 @@ scan_sexps_forward (stateptr, from, end, oldstate = Fcdr (oldstate); oldstate = Fcdr (oldstate); tem = Fcar (oldstate); - state.instring = !NILP (tem) ? XINT (tem) : -1; + /* Check whether we are inside string_fence-style string: */ + state.instring = ( !NILP (tem) + ? ( INTEGERP (tem) ? XINT (tem) : ST_STRING_STYLE) + : -1); oldstate = Fcdr (oldstate); tem = Fcar (oldstate); @@ -1476,11 +2150,16 @@ scan_sexps_forward (stateptr, from, end, start_quoted = !NILP (tem); /* if the eight element of the list is nil, we are in comment - style a. if it is non-nil, we are in comment style b */ + style a. If it is non-nil, we are in comment style b */ oldstate = Fcdr (oldstate); oldstate = Fcdr (oldstate); tem = Fcar (oldstate); - state.comstyle = !NILP (tem); + state.comstyle = NILP (tem) ? 0 : ( EQ (tem, Qsyntax_table) + ? ST_COMMENT_STYLE : 1 ); + + oldstate = Fcdr (oldstate); + tem = Fcar (oldstate); + state.comstr_start = NILP (tem) ? -1 : XINT (tem) ; } state.quoted = 0; mindepth = depth; @@ -1488,11 +2167,12 @@ scan_sexps_forward (stateptr, from, end, curlevel->prev = -1; curlevel->last = -1; - /* Enter the loop at a place appropriate for initial state. */ + /* Enter the loop at a place appropriate for initial state. */ if (state.incomment) goto startincomment; if (state.instring >= 0) { + nofence = state.instring != ST_STRING_STYLE; if (start_quoted) goto startquotedinstring; goto startinstring; } @@ -1500,23 +2180,27 @@ scan_sexps_forward (stateptr, from, end, while (from < end) { + UPDATE_SYNTAX_TABLE_FORWARD (from); code = SYNTAX (FETCH_CHAR (from)); from++; if (code == Scomment) - state.comstart = from-1; + state.comstr_start = from-1; - else if (from < end && SYNTAX_COMSTART_FIRST (FETCH_CHAR (from - 1)) - && SYNTAX_COMSTART_SECOND (FETCH_CHAR (from))) + else if (code == Scomment_fence + || (from < end && SYNTAX_COMSTART_FIRST (FETCH_CHAR (from - 1)) + && SYNTAX_COMSTART_SECOND (FETCH_CHAR (from)))) { /* Record the comment style we have entered so that only the comment-end sequence of the same style actually terminates the comment section. */ + state.comstyle = ( code == Scomment_fence + ? ST_COMMENT_STYLE + : SYNTAX_COMMENT_STYLE (FETCH_CHAR (from))); + state.comstr_start = from-1; + if (code != Scomment_fence) from++; code = Scomment; - state.comstyle = SYNTAX_COMMENT_STYLE (FETCH_CHAR (from)); - state.comstart = from-1; - from++; } - + if (SYNTAX_PREFIX (FETCH_CHAR (from - 1))) continue; switch (SWITCH_ENUM_CAST (code)) @@ -1537,6 +2221,7 @@ scan_sexps_forward (stateptr, from, end, symstarted: while (from < end) { + UPDATE_SYNTAX_TABLE_FORWARD (from); switch (SWITCH_ENUM_CAST (SYNTAX (FETCH_CHAR (from)))) { case Scharquote: @@ -1558,7 +2243,7 @@ scan_sexps_forward (stateptr, from, end, break; startincomment: - if (commentstop) + if (commentstop == 1) goto done; if (from != BEGV) { @@ -1568,14 +2253,17 @@ scan_sexps_forward (stateptr, from, end, goto startincomment_1; } /* At beginning of buffer, enter the loop the ordinary way. */ + state.incomment = 1; + goto commentloop; case Scomment: state.incomment = 1; - if (commentstop) - goto done; + if (commentstop || boundary_stop) goto done; + commentloop: while (1) { if (from == end) goto done; + UPDATE_SYNTAX_TABLE_FORWARD (from); prev = FETCH_CHAR (from); if (SYNTAX (prev) == Sendcomment && SYNTAX_COMMENT_STYLE (prev) == state.comstyle) @@ -1583,6 +2271,9 @@ scan_sexps_forward (stateptr, from, end, of the same style as the start sequence has been encountered. */ break; + if (state.comstyle == ST_COMMENT_STYLE + && SYNTAX (prev) == Scomment_fence) + break; from++; startincomment_1: if (from < end && SYNTAX_COMEND_FIRST (prev) @@ -1591,10 +2282,12 @@ scan_sexps_forward (stateptr, from, end, /* Only terminate the comment section if the end-comment sequence of the same style as the start sequence has been encountered. */ - { from++; break; } + break; } + from++; state.incomment = 0; state.comstyle = 0; /* reset the comment style */ + if (boundary_stop) goto done; break; case Sopen: @@ -1620,27 +2313,43 @@ scan_sexps_forward (stateptr, from, end, break; case Sstring: + case Sstring_fence: + state.comstr_start = from - 1; if (stopbefore) goto stop; /* this arg means stop at sexp start */ curlevel->last = from - 1; - state.instring = FETCH_CHAR (from - 1); + + state.instring = (code == Sstring + ? (FETCH_CHAR (from - 1)) + : ST_STRING_STYLE); + if (boundary_stop) goto done; startinstring: - while (1) - { - if (from >= end) goto done; - if (FETCH_CHAR (from) == state.instring) break; - switch (SWITCH_ENUM_CAST (SYNTAX (FETCH_CHAR (from)))) - { - case Scharquote: - case Sescape: - from++; - startquotedinstring: - if (from >= end) goto endquoted; - } - from++; - } + { + nofence = state.instring != ST_STRING_STYLE; + + while (1) + { + if (from >= end) goto done; + if (nofence && FETCH_CHAR (from) == state.instring) break; + UPDATE_SYNTAX_TABLE_FORWARD (from); + switch (SWITCH_ENUM_CAST (SYNTAX (FETCH_CHAR (from)))) + { + case Sstring_fence: + if (!nofence) goto string_end; + break; + case Scharquote: + case Sescape: + from++; + startquotedinstring: + if (from >= end) goto endquoted; + } + from++; + } + } + string_end: state.instring = -1; curlevel->prev = curlevel->last; from++; + if (boundary_stop) goto done; break; case Smath: @@ -1649,9 +2358,9 @@ scan_sexps_forward (stateptr, from, end, } goto done; - stop: /* Here if stopping before start of sexp. */ + stop: /* Here if stopping before start of sexp. */ from--; /* We have just fetched the char that starts it; */ - goto done; /* but return the position before it. */ + goto done; /* but return the position before it. */ endquoted: state.quoted = 1; @@ -1677,16 +2386,20 @@ Parsing stops at TO or when certain crit point is set to where parsing stops.\n\ If fifth arg STATE is omitted or nil,\n\ parsing assumes that FROM is the beginning of a function.\n\ -Value is a list of eight elements describing final state of parsing:\n\ +Value is a list of nine elements describing final state of parsing:\n\ 0. depth in parens.\n\ 1. character address of start of innermost containing list; nil if none.\n\ 2. character address of start of last complete sexp terminated.\n\ 3. non-nil if inside a string.\n\ - (it is the character that will terminate the string.)\n\ + (it is the character that will terminate the string,\n\ + or t if the string should be terminated by an explicit\n\ + `syntax-table' property.)\n\ 4. t if inside a comment.\n\ 5. t if following a quote character.\n\ 6. the minimum paren-depth encountered during this scan.\n\ - 7. t if in a comment of style `b'.\n\ + 7. t if in a comment of style `b'; `syntax-table' if given by an explicit\n\ + `syntax-table' property.\n\ + 8. character address of start of last comment or string; nil if none.\n\ If third arg TARGETDEPTH is non-nil, parsing stops if the depth\n\ in parentheses becomes equal to TARGETDEPTH.\n\ Fourth arg STOPBEFORE non-nil means stop when come to\n\ @@ -1694,7 +2407,9 @@ Fourth arg STOPBEFORE non-nil means stop Fifth arg STATE is an eight-list like what this function returns.\n\ It is used to initialize the state of the parse. Its second and third elements are ignored. -Sixth args COMMENTSTOP non-nil means stop at the start of a comment.") +Sixth arg COMMENTSTOP non-nil means stop at the start of a comment. If\n\ +it is `syntax-table', stop after the start of a comment or a string, or\n\ +after end of a comment or a string.") (from, to, targetdepth, stopbefore, state, commentstop) */ @@ -1717,19 +2432,25 @@ DEFUN ("parse-partial-sexp", Fparse_part validate_region (&from, &to); scan_sexps_forward (&state, XINT (from), XINT (to), target, !NILP (stopbefore), oldstate, - !NILP (commentstop)); + (NILP (commentstop) + ? 0 : (EQ (commentstop, Qsyntax_table) ? -1 : 1))); SET_PT (state.location); return Fcons (make_number (state.depth), Fcons (state.prevlevelstart < 0 ? Qnil : make_number (state.prevlevelstart), Fcons (state.thislevelstart < 0 ? Qnil : make_number (state.thislevelstart), - Fcons (state.instring >= 0 ? make_number (state.instring) : Qnil, + Fcons (state.instring >= 0 + ? (state.instring == ST_STRING_STYLE + ? Qt : make_number (state.instring)) : Qnil, Fcons (state.incomment ? Qt : Qnil, Fcons (state.quoted ? Qt : Qnil, Fcons (make_number (state.mindepth), - Fcons (state.comstyle ? Qt : Qnil, - Qnil)))))))); + Fcons (state.comstyle + ? (state.comstyle == ST_COMMENT_STYLE + ? Qsyntax_table : Qt) : Qnil, + Fcons (state.comstr_start != -1 ? make_number (state.comstr_start) : Qnil, + Qnil))))))))); } init_syntax_once () @@ -1796,9 +2517,23 @@ syms_of_syntax () Qsyntax_table_p = intern ("syntax-table-p"); staticpro (&Qsyntax_table_p); + Qscan_error = intern ("scan-error"); + staticpro (&Qscan_error); + Fput (Qscan_error, Qerror_conditions, + Fcons (Qerror, Qnil)); + Fput (Qscan_error, Qerror_message, + build_string ("Scan error")); + DEFVAR_BOOL ("parse-sexp-ignore-comments", &parse_sexp_ignore_comments, "Non-nil means `forward-sexp', etc., should treat comments as whitespace."); + DEFVAR_BOOL ("parse-sexp-lookup-properties", &parse_sexp_lookup_properties, + "Non-nil means `forward-sexp', etc., grant `syntax-table' property.\n\ +The value of this property should be either a syntax table, or a cons\n\ +of the form (SYNTAXCODE . MATCHCHAR), SYNTAXCODE being the numeric\n\ +syntax code, MATCHCHAR being nil or the character to match (which is\n\ +relevant only for open/close type."); + words_include_escapes = 0; DEFVAR_BOOL ("words-include-escapes", &words_include_escapes, "Non-nil means `forward-word', etc., should treat escape chars part of words."); @@ -1815,9 +2550,15 @@ syms_of_syntax () defsubr (&Sforward_word); + defsubr (&Sskip_chars_forward); + defsubr (&Sskip_chars_backward); + defsubr (&Sskip_syntax_forward); + defsubr (&Sskip_syntax_backward); + defsubr (&Sforward_comment); defsubr (&Sscan_lists); defsubr (&Sscan_sexps); defsubr (&Sbackward_prefix_chars); defsubr (&Sparse_partial_sexp); } + diff -pru emacs/19.33/src/syntax.h ./19.33\src/syntax.h --- emacs/19.33/src/syntax.h Sat Jan 20 00:59:44 1996 +++ ./19.33/src/syntax.h Fri Mar 14 17:57:36 1997 @@ -21,6 +21,7 @@ Boston, MA 02111-1307, USA. */ extern Lisp_Object Qsyntax_table_p; extern Lisp_Object Fsyntax_table_p (), Fsyntax_table (), Fset_syntax_table (); +extern void update_syntax_table (); /* The standard syntax table is stored where it will automatically be used in all new buffers. */ @@ -42,12 +43,16 @@ enum syntaxcode Sclose, /* for an ending delimiter */ Squote, /* for a prefix character like Lisp ' */ Sstring, /* for a string-grouping character like Lisp " */ - Smath, /* for delimiters like $ in Tex. */ + Smath, /* for delimiters like $ in Tex. */ Sescape, /* for a character that begins a C-style escape */ Scharquote, /* for a character that quotes the following character */ Scomment, /* for a comment-starting character */ Sendcomment, /* for a comment-ending character */ Sinherit, /* use the standard syntax table for this character */ + Scomment_fence, /* Starts/ends comment which is delimited on the + other side by a char with the same syntaxcode. */ + Sstring_fence, /* Starts/ends string which is delimited on the + other side by a char with the same syntaxcode. */ Smax /* Upper bound on codes that are meaningful */ }; @@ -73,14 +78,28 @@ enum syntaxcode : XCHAR_TABLE (table)->contents[(unsigned char) (c)] = (val)) #endif -/* Extract the information from the entry for character C - in syntax table TABLE. Do inheritance. */ +/* SYNTAX_ENTRY extracts the information from the entry for character C + in syntax table TABLE, or from globally kept data (gl_state). + Does inheritance. */ +/* CURRENT_SYNTAX_TABLE gives the syntax table valid for current + position, it is either the buffer's syntax table, or syntax table + found in text properties. */ + +#ifdef SYNTAX_ENTRY_VIA_PROPERTY +# define SYNTAX_ENTRY(c) \ + (gl_state.use_global ? gl_state.global_code : SYNTAX_ENTRY_INT (c)) +# define CURRENT_SYNTAX_TABLE gl_state.current_syntax_table +#else +# define SYNTAX_ENTRY SYNTAX_ENTRY_INT +# define CURRENT_SYNTAX_TABLE current_buffer->syntax_table +#endif #ifdef __GNUC__ -#define SYNTAX_ENTRY(c) \ + +#define SYNTAX_ENTRY_INT(c) \ ({ Lisp_Object temp, table; \ unsigned char cc = (c); \ - table = current_buffer->syntax_table; \ + table = CURRENT_SYNTAX_TABLE; \ while (!NILP (table)) \ { \ temp = RAW_SYNTAX_ENTRY (table, cc); \ @@ -114,12 +133,12 @@ enum syntaxcode extern Lisp_Object syntax_temp; extern Lisp_Object syntax_parent_lookup (); -#define SYNTAX_ENTRY(c) \ +#define SYNTAX_ENTRY_INT(c) \ (syntax_temp \ - = RAW_SYNTAX_ENTRY (current_buffer->syntax_table, (c)), \ + = RAW_SYNTAX_ENTRY (CURRENT_SYNTAX_TABLE, (c)), \ (NILP (syntax_temp) \ ? (syntax_temp \ - = syntax_parent_lookup (current_buffer->syntax_table, \ + = syntax_parent_lookup (CURRENT_SYNTAX_TABLE, \ (unsigned char) (c))) \ : syntax_temp)) @@ -154,7 +173,7 @@ extern Lisp_Object syntax_parent_lookup bit 6 is used to discriminate between two different comment styles. Languages such as C++ allow two orthogonal syntax start/end pairs and bit 6 is used to determine whether a comment-end or Scommentend - ends style a or b. Comment start sequences can start style a or b. + ends style a or b. Comment start sequences can start style a or b. Style a is always the default. */ @@ -173,10 +192,98 @@ extern Lisp_Object syntax_parent_lookup /* This array, indexed by a character, contains the syntax code which that character signifies (as a char). For example, - (enum syntaxcode) syntax_spec_code['w'] is Sword. */ + (enum syntaxcode) syntax_spec_code['w'] is Sword. */ extern unsigned char syntax_spec_code[0400]; -/* Indexed by syntax code, give the letter that describes it. */ +/* Indexed by syntax code, give the letter that describes it. */ + +extern char syntax_code_spec[16]; -extern char syntax_code_spec[14]; +/* Make syntax table state (gl_state) good for pos, assuming it is + currently good for a position before POS. */ +#define UPDATE_SYNTAX_TABLE_FORWARD(pos) \ + ((pos) >= gl_state.e_property ? \ + ( update_syntax_table ((pos), 1, 0), 1 ) : 0) + + +/* Make syntax table state (gl_state) good for pos, assuming it is + currently good for a position after POS. */ +#define UPDATE_SYNTAX_TABLE_BACKWARD(pos) \ + ((pos) <= gl_state.b_property ? \ + ( update_syntax_table ((pos), -1, 0), 1 ) : 0) + +/* Make syntax table good for pos. */ +#define UPDATE_SYNTAX_TABLE(pos) \ + ((pos) <= gl_state.b_property ? \ + ( update_syntax_table ((pos), -1, 0), 1 ) : \ + ( (pos) >= gl_state.e_property ? \ + ( update_syntax_table ((pos), 1, 0), 1 ) : 0)) + +/* This macro should be called with FROM at the start of forward + search, or after the last position of the backward search. It + makes sure that the first char is picked up with correct table, so + one does not need to call UPDATE_SYNTAX_TABLE immediately after the + call. + Sign of COUNT gives the direction of the search. + */ + +#define SETUP_SYNTAX_TABLE(from,count) \ + gl_state.b_property = BEGV - 1; \ + gl_state.e_property = ZV + 1; \ + gl_state.use_global = 0; \ + gl_state.current_syntax_table = current_buffer->syntax_table; \ + if (parse_sexp_lookup_properties) \ + update_syntax_table ((count) > 0 ? (from) : (from) - 1, (count), 1, Qnil); + +/* Same as above, but in OBJECT. If OBJECT is nil, use current buffer. + If it is t, ignore properties altogether. */ + +#define SETUP_SYNTAX_TABLE_FOR_OBJECT(object, from, count) \ + if (BUFFERP (object)) \ + { \ + gl_state.b_property = BEGV - 1; \ + gl_state.e_property = ZV; \ + } \ + else if (EQ (object, Qt)) \ + { \ + gl_state.b_property = - 1; \ + gl_state.e_property = 1500000000; \ + } \ + else \ + { \ + gl_state.b_property = 0; \ + gl_state.e_property = 1 + XSTRING (object)->size; \ + } \ + gl_state.use_global = 0; \ + gl_state.current_syntax_table = current_buffer->syntax_table; \ + if (parse_sexp_lookup_properties) \ + update_syntax_table (count > 0 ? (from) : (from) - 1, count, 1, object); + +struct gl_state_s +{ + int start; /* Where to stop. */ + int stop; /* Where to stop. */ + int use_global; /* Whether to use global_code + or c_s_t. */ + Lisp_Object global_code; /* Syntax code of current char. */ + Lisp_Object current_syntax_table; /* Syntax table for current pos. */ + Lisp_Object old_prop; /* Syntax-table prop at prev pos. */ + int b_property; /* Last index where c_s_t is + not valid. */ + int e_property; /* First index where c_s_t is + not valid. */ + INTERVAL forward_i; /* Where to start lookup on forward */ + INTERVAL backward_i; /* or backward movement. The + data in c_s_t is valid + between these intervals, + and possibly at the + intervals too, depending + on: */ + char left_ok; + char right_ok; +}; + +extern struct gl_state_s gl_state; +extern int parse_sexp_lookup_properties; +extern INTERVAL interval_of(); diff -pru emacs/19.33/src/textprop.c ./19.33\src/textprop.c --- emacs/19.33/src/textprop.c Sat Feb 17 20:27:16 1996 +++ ./19.33/src/textprop.c Mon Jul 28 00:41:46 1997 @@ -44,12 +44,12 @@ Boston, MA 02111-1307, USA. */ only once on the list. Although some code i.e., remove_properties, handles the more general case, the uniqueness of properties is necessary for the system to remain consistent. This requirement - is enforced by the subrs installing properties onto the intervals. */ + is enforced by the subrs installing properties onto the intervals. */ /* The rest of the file is within this conditional */ #ifdef USE_TEXT_PROPERTIES -/* Types of hooks. */ +/* Types of hooks. */ Lisp_Object Qmouse_left; Lisp_Object Qmouse_entered; Lisp_Object Qpoint_left; @@ -57,7 +57,7 @@ Lisp_Object Qpoint_entered; Lisp_Object Qcategory; Lisp_Object Qlocal_map; -/* Visual properties text (including strings) may have. */ +/* Visual properties text (including strings) may have. */ Lisp_Object Qforeground, Qbackground, Qfont, Qunderline, Qstipple; Lisp_Object Qinvisible, Qread_only, Qintangible; @@ -116,7 +116,7 @@ validate_interval_range (object, begin, CHECK_NUMBER_COERCE_MARKER (*end, 0); /* If we are asked for a point, but from a subr which operates - on a range, then return nothing. */ + on a range, then return nothing. */ if (EQ (*begin, *end) && begin != end) return NULL_INTERVAL; @@ -137,7 +137,7 @@ validate_interval_range (object, begin, args_out_of_range (*begin, *end); i = BUF_INTERVALS (b); - /* If there's no text, there are no properties. */ + /* If there's no text, there are no properties. */ if (BUF_BEGV (b) == BUF_ZV (b)) return NULL_INTERVAL; @@ -171,7 +171,7 @@ validate_interval_range (object, begin, /* Validate LIST as a property list. If LIST is not a list, then make one consisting of (LIST nil). Otherwise, verify that LIST - is even numbered and thus suitable as a plist. */ + is even numbered and thus suitable as a plist. */ static Lisp_Object validate_plist (list) @@ -198,7 +198,7 @@ validate_plist (list) } /* Return nonzero if interval I has all the properties, - with the same values, of list PLIST. */ + with the same values, of list PLIST. */ static int interval_has_all_properties (plist, i) @@ -208,7 +208,7 @@ interval_has_all_properties (plist, i) register Lisp_Object tail1, tail2, sym1, sym2; register int found; - /* Go through each element of PLIST. */ + /* Go through each element of PLIST. */ for (tail1 = plist; ! NILP (tail1); tail1 = Fcdr (Fcdr (tail1))) { sym1 = Fcar (tail1); @@ -219,11 +219,11 @@ interval_has_all_properties (plist, i) if (EQ (sym1, Fcar (tail2))) { /* Found the same property on both lists. If the - values are unequal, return zero. */ + values are unequal, return zero. */ if (! EQ (Fcar (Fcdr (tail1)), Fcar (Fcdr (tail2)))) return 0; - /* Property has same value on both lists; go to next one. */ + /* Property has same value on both lists; go to next one. */ found = 1; break; } @@ -236,7 +236,7 @@ interval_has_all_properties (plist, i) } /* Return nonzero if the plist of interval I has any of the - properties of PLIST, regardless of their values. */ + properties of PLIST, regardless of their values. */ static INLINE int interval_has_some_properties (plist, i) @@ -245,7 +245,7 @@ interval_has_some_properties (plist, i) { register Lisp_Object tail1, tail2, sym; - /* Go through each element of PLIST. */ + /* Go through each element of PLIST. */ for (tail1 = plist; ! NILP (tail1); tail1 = Fcdr (Fcdr (tail1))) { sym = Fcar (tail1); @@ -359,7 +359,7 @@ add_properties (plist, i, object) I and its plist are also protected, via OBJECT. */ GCPRO3 (tail1, sym1, val1); - /* Go through each element of PLIST. */ + /* Go through each element of PLIST. */ for (tail1 = plist; ! NILP (tail1); tail1 = Fcdr (Fcdr (tail1))) { sym1 = Fcar (tail1); @@ -375,11 +375,11 @@ add_properties (plist, i, object) register Lisp_Object this_cdr; this_cdr = Fcdr (tail2); - /* Found the property. Now check its value. */ + /* Found the property. Now check its value. */ found = 1; /* The properties have the same value on both lists. - Continue to the next property. */ + Continue to the next property. */ if (EQ (val1, Fcar (this_cdr))) break; @@ -436,7 +436,7 @@ remove_properties (plist, i, object) register int changed = 0; current_plist = i->plist; - /* Go through each element of plist. */ + /* Go through each element of plist. */ for (tail1 = plist; ! NILP (tail1); tail1 = Fcdr (Fcdr (tail1))) { sym = Fcar (tail1); @@ -491,7 +491,7 @@ remove_properties (plist, i, object) #if 0 /* Remove all properties from interval I. Return non-zero - if this changes the interval. */ + if this changes the interval. */ static INLINE int erase_properties (i) @@ -505,6 +505,49 @@ erase_properties (i) } #endif +/* Returns the interval of the POSITION in OBJECT. + POSITION is BEG-based. */ + +INTERVAL +interval_of (position, object) + int position; + Lisp_Object object; +{ + register INTERVAL i; + int beg, end; + + if (NILP (object)) + XSETBUFFER (object, current_buffer); + + CHECK_STRING_OR_BUFFER (object, 0); + + if (BUFFERP (object)) + { + register struct buffer *b = XBUFFER (object); + + beg = BUF_BEGV (b); + end = BUF_ZV (b); + i = BUF_INTERVALS (b); + } + else + { + register struct Lisp_String *s = XSTRING (object); + + /* We expect position to be 1-based. */ + beg = BEG; + end = s->size + BEG; + i = s->intervals; + } + + if (!(beg <= position && position <= end) + && !(BUFFERP (object) && (position == beg - 1 && beg > 1))) /* lookbehind */ + args_out_of_range (position, position); + if (beg == end || NULL_INTERVAL_P (i)) + return NULL_INTERVAL; + + return find_interval (i, position); +} + DEFUN ("text-properties-at", Ftext_properties_at, Stext_properties_at, 1, 2, 0, "Return the list of properties held by the character at POSITION\n\ @@ -876,11 +919,11 @@ Return t if any property value actually GCPRO1 (properties); /* If we're not starting on an interval boundary, we have to - split this interval. */ + split this interval. */ if (i->position != s) { /* If this interval already has the properties, we can - skip it. */ + skip it. */ if (interval_has_all_properties (properties, i)) { int got = (LENGTH (i) - (s - i->position)); @@ -1093,7 +1136,7 @@ Return t if any property was actually re if (i->position != s) { /* No properties on this first interval -- return if - it covers the entire region. */ + it covers the entire region. */ if (! interval_has_some_properties (properties, i)) { int got = (LENGTH (i) - (s - i->position)); @@ -1244,7 +1287,7 @@ is the string or buffer containing the t register int got; register INTERVAL unchanged = i; - /* If there are properties here, then this text will be modified. */ + /* If there are properties here, then this text will be modified. */ if (! NILP (i->plist)) { i = split_interval_right (unchanged, s - unchanged->position); @@ -1269,7 +1312,7 @@ is the string or buffer containing the t else if (LENGTH (i) - (s - i->position) <= len) return Qnil; /* The amount of text to change extends past I, so just note - how much we've gotten. */ + how much we've gotten. */ else got = LENGTH (i) - (s - i->position); @@ -1278,7 +1321,7 @@ is the string or buffer containing the t i = next_interval (i); } - /* We are starting at the beginning of an interval, I. */ + /* We are starting at the beginning of an interval, I. */ while (len > 0) { if (LENGTH (i) >= len) @@ -1312,7 +1355,7 @@ is the string or buffer containing the t else { modified += ! NILP (i->plist); - /* Merging I will give it the properties of PREV_CHANGED. */ + /* Merging I will give it the properties of PREV_CHANGED. */ prev_changed = i = merge_interval_left (i); } @@ -1392,7 +1435,7 @@ copy_text_properties (start, end, src, p if (! NILP (plist)) { /* Must defer modifications to the interval tree in case src - and dest refer to the same string or buffer. */ + and dest refer to the same string or buffer. */ stuff = Fcons (Fcons (make_number (p), Fcons (make_number (p + len), Fcons (plist, Qnil))), @@ -1501,7 +1544,7 @@ verify_interval_modification (buf, start if (NILP (Vinhibit_read_only) || CONSP (Vinhibit_read_only)) { /* If I and PREV differ we need to check for the read-only - property together with its stickiness. If either I or + property together with its stickiness. If either I or PREV are 0, this check is all we need. We have to take special care, since read-only may be indirectly defined via the category property. */