1 /* GNU Emacs routines to deal with syntax tables; also word and list parsing.
2 Copyright (C) 1985, 1987, 1993-1995, 1997-1999, 2001-2013 Free
3 Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
23 #include <sys/types.h>
27 #include "character.h"
32 /* Make syntax table lookup grant data in gl_state. */
33 #define SYNTAX_ENTRY_VIA_PROPERTY
36 #include "intervals.h"
39 /* Eight single-bit flags have the following meanings:
40 1. This character is the first of a two-character comment-start sequence.
41 2. This character is the second of a two-character comment-start sequence.
42 3. This character is the first of a two-character comment-end sequence.
43 4. This character is the second of a two-character comment-end sequence.
44 5. This character is a prefix, for backward-prefix-chars.
45 6. The char is part of a delimiter for comments of style "b".
46 7. This character is part of a nestable comment sequence.
47 8. The char is part of a delimiter for comments of style "c".
48 Note that any two-character sequence whose first character has flag 1
49 and whose second character has flag 2 will be interpreted as a comment start.
51 Bits 6 and 8 discriminate among different comment styles.
52 Languages such as C++ allow two orthogonal syntax start/end pairs
53 and bit 6 determines whether a comment-end or Scommentend
54 ends style a or b. Comment markers can start style a, b, c, or bc.
55 Style a is always the default.
56 For 2-char comment markers, the style b flag is looked up only on the second
57 char of the comment marker and on the first char of the comment ender.
58 For style c (like the nested flag), the flag can be placed on any of
61 /* These macros extract specific flags from an integer
62 that holds the syntax code and the flags. */
64 #define SYNTAX_FLAGS_COMSTART_FIRST(flags) (((flags) >> 16) & 1)
66 #define SYNTAX_FLAGS_COMSTART_SECOND(flags) (((flags) >> 17) & 1)
68 #define SYNTAX_FLAGS_COMEND_FIRST(flags) (((flags) >> 18) & 1)
70 #define SYNTAX_FLAGS_COMEND_SECOND(flags) (((flags) >> 19) & 1)
72 #define SYNTAX_FLAGS_PREFIX(flags) (((flags) >> 20) & 1)
74 #define SYNTAX_FLAGS_COMMENT_STYLEB(flags) (((flags) >> 21) & 1)
75 #define SYNTAX_FLAGS_COMMENT_STYLEC(flags) (((flags) >> 23) & 1)
76 #define SYNTAX_FLAGS_COMMENT_STYLEC2(flags) (((flags) >> 22) & 2) /* C * 2 */
77 /* FLAGS should be the flags of the main char of the comment marker, e.g.
78 the second for comstart and the first for comend. */
79 #define SYNTAX_FLAGS_COMMENT_STYLE(flags, other_flags) \
80 (SYNTAX_FLAGS_COMMENT_STYLEB (flags) \
81 | SYNTAX_FLAGS_COMMENT_STYLEC2 (flags) \
82 | SYNTAX_FLAGS_COMMENT_STYLEC2 (other_flags))
84 #define SYNTAX_FLAGS_COMMENT_NESTED(flags) (((flags) >> 22) & 1)
86 /* These macros extract a particular flag for a given character. */
88 #define SYNTAX_COMEND_FIRST(c) \
89 (SYNTAX_FLAGS_COMEND_FIRST (SYNTAX_WITH_FLAGS (c)))
90 #define SYNTAX_PREFIX(c) (SYNTAX_FLAGS_PREFIX (SYNTAX_WITH_FLAGS (c)))
92 /* We use these constants in place for comment-style and
93 string-ender-char to distinguish comments/strings started by
94 comment_fence and string_fence codes. */
96 #define ST_COMMENT_STYLE (256 + 1)
97 #define ST_STRING_STYLE (256 + 2)
99 static Lisp_Object Qsyntax_table_p
;
100 static Lisp_Object Qsyntax_table
, Qscan_error
;
103 /* Used as a temporary in SYNTAX_ENTRY and other macros in syntax.h,
104 if not compiled with GCC. No need to mark it, since it is used
105 only very temporarily. */
106 Lisp_Object syntax_temp
;
109 /* This is the internal form of the parse state used in parse-partial-sexp. */
111 struct lisp_parse_state
113 EMACS_INT depth
; /* Depth at end of parsing. */
114 int instring
; /* -1 if not within string, else desired terminator. */
115 EMACS_INT incomment
; /* -1 if in unnestable comment else comment nesting */
116 int comstyle
; /* comment style a=0, or b=1, or ST_COMMENT_STYLE. */
117 bool quoted
; /* True if just after an escape char at end of parsing. */
118 EMACS_INT mindepth
; /* Minimum depth seen while scanning. */
119 /* Char number of most recent start-of-expression at current level */
120 ptrdiff_t thislevelstart
;
121 /* Char number of start of containing expression */
122 ptrdiff_t prevlevelstart
;
123 ptrdiff_t location
; /* Char number at which parsing stopped. */
124 ptrdiff_t location_byte
; /* Corresponding byte position. */
125 ptrdiff_t comstr_start
; /* Position of last comment/string starter. */
126 Lisp_Object levelstarts
; /* Char numbers of starts-of-expression
127 of levels (starting from outermost). */
130 /* These variables are a cache for finding the start of a defun.
131 find_start_pos is the place for which the defun start was found.
132 find_start_value is the defun start position found for it.
133 find_start_value_byte is the corresponding byte position.
134 find_start_buffer is the buffer it was found in.
135 find_start_begv is the BEGV value when it was found.
136 find_start_modiff is the value of MODIFF when it was found. */
138 static ptrdiff_t find_start_pos
;
139 static ptrdiff_t find_start_value
;
140 static ptrdiff_t find_start_value_byte
;
141 static struct buffer
*find_start_buffer
;
142 static ptrdiff_t find_start_begv
;
143 static EMACS_INT find_start_modiff
;
146 static Lisp_Object
skip_chars (bool, Lisp_Object
, Lisp_Object
, bool);
147 static Lisp_Object
skip_syntaxes (bool, Lisp_Object
, Lisp_Object
);
148 static Lisp_Object
scan_lists (EMACS_INT
, EMACS_INT
, EMACS_INT
, bool);
149 static void scan_sexps_forward (struct lisp_parse_state
*,
150 ptrdiff_t, ptrdiff_t, ptrdiff_t, EMACS_INT
,
151 bool, Lisp_Object
, int);
152 static bool in_classes (int, Lisp_Object
);
154 /* This setter is used only in this file, so it can be private. */
156 bset_syntax_table (struct buffer
*b
, Lisp_Object val
)
158 b
->INTERNAL_FIELD (syntax_table
) = val
;
161 /* Whether the syntax of the character C has the prefix flag set. */
163 syntax_prefix_flag_p (int c
)
165 return SYNTAX_PREFIX (c
);
168 struct gl_state_s gl_state
; /* Global state of syntax parser. */
170 #define INTERVALS_AT_ONCE 10 /* 1 + max-number of intervals
171 to scan to property-change. */
173 /* Update gl_state to an appropriate interval which contains CHARPOS. The
174 sign of COUNT give the relative position of CHARPOS wrt the previously
175 valid interval. If INIT, only [be]_property fields of gl_state are
176 valid at start, the rest is filled basing on OBJECT.
178 `gl_state.*_i' are the intervals, and CHARPOS is further in the search
179 direction than the intervals - or in an interval. We update the
180 current syntax-table basing on the property of this interval, and
181 update the interval to start further than CHARPOS - or be
182 NULL. We also update lim_property to be the next value of
183 charpos to call this subroutine again - or be before/after the
184 start/end of OBJECT. */
187 update_syntax_table (ptrdiff_t charpos
, EMACS_INT count
, bool init
,
190 Lisp_Object tmp_table
;
197 gl_state
.old_prop
= Qnil
;
198 gl_state
.start
= gl_state
.b_property
;
199 gl_state
.stop
= gl_state
.e_property
;
200 i
= interval_of (charpos
, object
);
201 gl_state
.backward_i
= gl_state
.forward_i
= i
;
205 /* interval_of updates only ->position of the return value, so
206 update the parents manually to speed up update_interval. */
207 while (!NULL_PARENT (i
))
209 if (AM_RIGHT_CHILD (i
))
210 INTERVAL_PARENT (i
)->position
= i
->position
211 - LEFT_TOTAL_LENGTH (i
) + TOTAL_LENGTH (i
) /* right end */
212 - TOTAL_LENGTH (INTERVAL_PARENT (i
))
213 + LEFT_TOTAL_LENGTH (INTERVAL_PARENT (i
));
215 INTERVAL_PARENT (i
)->position
= i
->position
- LEFT_TOTAL_LENGTH (i
)
217 i
= INTERVAL_PARENT (i
);
219 i
= gl_state
.forward_i
;
220 gl_state
.b_property
= i
->position
- gl_state
.offset
;
221 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
224 i
= count
> 0 ? gl_state
.forward_i
: gl_state
.backward_i
;
226 /* We are guaranteed to be called with CHARPOS either in i,
229 error ("Error in syntax_table logic for to-the-end intervals");
230 else if (charpos
< i
->position
) /* Move left. */
233 error ("Error in syntax_table logic for intervals <-");
234 /* Update the interval. */
235 i
= update_interval (i
, charpos
);
236 if (INTERVAL_LAST_POS (i
) != gl_state
.b_property
)
239 gl_state
.forward_i
= i
;
240 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
243 else if (charpos
>= INTERVAL_LAST_POS (i
)) /* Move right. */
246 error ("Error in syntax_table logic for intervals ->");
247 /* Update the interval. */
248 i
= update_interval (i
, charpos
);
249 if (i
->position
!= gl_state
.e_property
)
252 gl_state
.backward_i
= i
;
253 gl_state
.b_property
= i
->position
- gl_state
.offset
;
258 tmp_table
= textget (i
->plist
, Qsyntax_table
);
261 invalidate
= !EQ (tmp_table
, gl_state
.old_prop
); /* Need to invalidate? */
263 if (invalidate
) /* Did not get to adjacent interval. */
264 { /* with the same table => */
265 /* invalidate the old range. */
268 gl_state
.backward_i
= i
;
269 gl_state
.b_property
= i
->position
- gl_state
.offset
;
273 gl_state
.forward_i
= i
;
274 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
278 if (!EQ (tmp_table
, gl_state
.old_prop
))
280 gl_state
.current_syntax_table
= tmp_table
;
281 gl_state
.old_prop
= tmp_table
;
282 if (EQ (Fsyntax_table_p (tmp_table
), Qt
))
284 gl_state
.use_global
= 0;
286 else if (CONSP (tmp_table
))
288 gl_state
.use_global
= 1;
289 gl_state
.global_code
= tmp_table
;
293 gl_state
.use_global
= 0;
294 gl_state
.current_syntax_table
= BVAR (current_buffer
, syntax_table
);
300 if (cnt
&& !EQ (tmp_table
, textget (i
->plist
, Qsyntax_table
)))
304 gl_state
.e_property
= i
->position
- gl_state
.offset
;
305 gl_state
.forward_i
= i
;
310 = i
->position
+ LENGTH (i
) - gl_state
.offset
;
311 gl_state
.backward_i
= i
;
315 else if (cnt
== INTERVALS_AT_ONCE
)
320 = i
->position
+ LENGTH (i
) - gl_state
.offset
321 /* e_property at EOB is not set to ZV but to ZV+1, so that
322 we can do INC(from);UPDATE_SYNTAX_TABLE_FORWARD without
323 having to check eob between the two. */
324 + (next_interval (i
) ? 0 : 1);
325 gl_state
.forward_i
= i
;
329 gl_state
.b_property
= i
->position
- gl_state
.offset
;
330 gl_state
.backward_i
= i
;
335 i
= count
> 0 ? next_interval (i
) : previous_interval (i
);
337 eassert (i
== NULL
); /* This property goes to the end. */
339 gl_state
.e_property
= gl_state
.stop
;
341 gl_state
.b_property
= gl_state
.start
;
344 /* Returns true if char at CHARPOS is quoted.
345 Global syntax-table data should be set up already to be good at CHARPOS
346 or after. On return global syntax data is good for lookup at CHARPOS. */
349 char_quoted (ptrdiff_t charpos
, ptrdiff_t bytepos
)
351 enum syntaxcode code
;
352 ptrdiff_t beg
= BEGV
;
354 ptrdiff_t orig
= charpos
;
356 while (charpos
> beg
)
359 DEC_BOTH (charpos
, bytepos
);
361 UPDATE_SYNTAX_TABLE_BACKWARD (charpos
);
362 c
= FETCH_CHAR_AS_MULTIBYTE (bytepos
);
364 if (! (code
== Scharquote
|| code
== Sescape
))
370 UPDATE_SYNTAX_TABLE (orig
);
374 /* Return the bytepos one character before BYTEPOS.
375 We assume that BYTEPOS is not at the start of the buffer. */
378 dec_bytepos (ptrdiff_t bytepos
)
380 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
387 /* Return a defun-start position before POS and not too far before.
388 It should be the last one before POS, or nearly the last.
390 When open_paren_in_column_0_is_defun_start is nonzero,
391 only the beginning of the buffer is treated as a defun-start.
393 We record the information about where the scan started
394 and what its result was, so that another call in the same area
395 can return the same value very quickly.
397 There is no promise at which position the global syntax data is
398 valid on return from the subroutine, so the caller should explicitly
399 update the global data. */
402 find_defun_start (ptrdiff_t pos
, ptrdiff_t pos_byte
)
404 ptrdiff_t opoint
= PT
, opoint_byte
= PT_BYTE
;
406 if (!open_paren_in_column_0_is_defun_start
)
408 find_start_value
= BEGV
;
409 find_start_value_byte
= BEGV_BYTE
;
410 find_start_buffer
= current_buffer
;
411 find_start_modiff
= MODIFF
;
412 find_start_begv
= BEGV
;
413 find_start_pos
= pos
;
417 /* Use previous finding, if it's valid and applies to this inquiry. */
418 if (current_buffer
== find_start_buffer
419 /* Reuse the defun-start even if POS is a little farther on.
420 POS might be in the next defun, but that's ok.
421 Our value may not be the best possible, but will still be usable. */
422 && pos
<= find_start_pos
+ 1000
423 && pos
>= find_start_value
424 && BEGV
== find_start_begv
425 && MODIFF
== find_start_modiff
)
426 return find_start_value
;
428 /* Back up to start of line. */
429 scan_newline (pos
, pos_byte
, BEGV
, BEGV_BYTE
, -1, 1);
431 /* We optimize syntax-table lookup for rare updates. Thus we accept
432 only those `^\s(' which are good in global _and_ text-property
434 SETUP_BUFFER_SYNTAX_TABLE ();
439 /* Open-paren at start of line means we may have found our
441 c
= FETCH_CHAR_AS_MULTIBYTE (PT_BYTE
);
442 if (SYNTAX (c
) == Sopen
)
444 SETUP_SYNTAX_TABLE (PT
+ 1, -1); /* Try again... */
445 c
= FETCH_CHAR_AS_MULTIBYTE (PT_BYTE
);
446 if (SYNTAX (c
) == Sopen
)
448 /* Now fallback to the default value. */
449 SETUP_BUFFER_SYNTAX_TABLE ();
451 /* Move to beg of previous line. */
452 scan_newline (PT
, PT_BYTE
, BEGV
, BEGV_BYTE
, -2, 1);
455 /* Record what we found, for the next try. */
456 find_start_value
= PT
;
457 find_start_value_byte
= PT_BYTE
;
458 find_start_buffer
= current_buffer
;
459 find_start_modiff
= MODIFF
;
460 find_start_begv
= BEGV
;
461 find_start_pos
= pos
;
463 TEMP_SET_PT_BOTH (opoint
, opoint_byte
);
465 return find_start_value
;
468 /* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
471 prev_char_comend_first (ptrdiff_t pos
, ptrdiff_t pos_byte
)
476 DEC_BOTH (pos
, pos_byte
);
477 UPDATE_SYNTAX_TABLE_BACKWARD (pos
);
478 c
= FETCH_CHAR (pos_byte
);
479 val
= SYNTAX_COMEND_FIRST (c
);
480 UPDATE_SYNTAX_TABLE_FORWARD (pos
+ 1);
484 /* Check whether charpos FROM is at the end of a comment.
485 FROM_BYTE is the bytepos corresponding to FROM.
486 Do not move back before STOP.
488 Return true if we find a comment ending at FROM/FROM_BYTE.
490 If successful, store the charpos of the comment's beginning
491 into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR.
493 Global syntax data remains valid for backward search starting at
494 the returned value (or at FROM, if the search was not successful). */
497 back_comment (ptrdiff_t from
, ptrdiff_t from_byte
, ptrdiff_t stop
,
498 bool comnested
, int comstyle
, ptrdiff_t *charpos_ptr
,
499 ptrdiff_t *bytepos_ptr
)
501 /* Look back, counting the parity of string-quotes,
502 and recording the comment-starters seen.
503 When we reach a safe place, assume that's not in a string;
504 then step the main scan to the earliest comment-starter seen
505 an even number of string quotes away from the safe place.
507 OFROM[I] is position of the earliest comment-starter seen
508 which is I+2X quotes from the comment-end.
509 PARITY is current parity of quotes from the comment end. */
510 int string_style
= -1; /* Presumed outside of any string. */
511 bool string_lossage
= 0;
512 /* Not a real lossage: indicates that we have passed a matching comment
513 starter plus a non-matching comment-ender, meaning that any matching
514 comment-starter we might see later could be a false positive (hidden
515 inside another comment).
516 Test case: { a (* b } c (* d *) */
517 bool comment_lossage
= 0;
518 ptrdiff_t comment_end
= from
;
519 ptrdiff_t comment_end_byte
= from_byte
;
520 ptrdiff_t comstart_pos
= 0;
521 ptrdiff_t comstart_byte
IF_LINT (= 0);
522 /* Place where the containing defun starts,
523 or 0 if we didn't come across it yet. */
524 ptrdiff_t defun_start
= 0;
525 ptrdiff_t defun_start_byte
= 0;
526 enum syntaxcode code
;
527 ptrdiff_t nesting
= 1; /* current comment nesting */
531 /* FIXME: A }} comment-ender style leads to incorrect behavior
532 in the case of {{ c }}} because we ignore the last two chars which are
533 assumed to be comment-enders although they aren't. */
535 /* At beginning of range to scan, we're outside of strings;
536 that determines quote parity to the comment-end. */
541 bool com2start
, com2end
, comstart
;
543 /* Move back and examine a character. */
544 DEC_BOTH (from
, from_byte
);
545 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
547 prev_syntax
= syntax
;
548 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
549 syntax
= SYNTAX_WITH_FLAGS (c
);
552 /* Check for 2-char comment markers. */
553 com2start
= (SYNTAX_FLAGS_COMSTART_FIRST (syntax
)
554 && SYNTAX_FLAGS_COMSTART_SECOND (prev_syntax
)
556 == SYNTAX_FLAGS_COMMENT_STYLE (prev_syntax
, syntax
))
557 && (SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax
)
558 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
)) == comnested
);
559 com2end
= (SYNTAX_FLAGS_COMEND_FIRST (syntax
)
560 && SYNTAX_FLAGS_COMEND_SECOND (prev_syntax
));
561 comstart
= (com2start
|| code
== Scomment
);
563 /* Nasty cases with overlapping 2-char comment markers:
564 - snmp-mode: -- c -- foo -- c --
572 /* If a 2-char comment sequence partly overlaps with another,
573 we don't try to be clever. E.g. |*| in C, or }% in modes that
574 have %..\n and %{..}%. */
575 if (from
> stop
&& (com2end
|| comstart
))
577 ptrdiff_t next
= from
, next_byte
= from_byte
;
578 int next_c
, next_syntax
;
579 DEC_BOTH (next
, next_byte
);
580 UPDATE_SYNTAX_TABLE_BACKWARD (next
);
581 next_c
= FETCH_CHAR_AS_MULTIBYTE (next_byte
);
582 next_syntax
= SYNTAX_WITH_FLAGS (next_c
);
583 if (((comstart
|| comnested
)
584 && SYNTAX_FLAGS_COMEND_SECOND (syntax
)
585 && SYNTAX_FLAGS_COMEND_FIRST (next_syntax
))
586 || ((com2end
|| comnested
)
587 && SYNTAX_FLAGS_COMSTART_SECOND (syntax
)
589 == SYNTAX_FLAGS_COMMENT_STYLE (syntax
, prev_syntax
))
590 && SYNTAX_FLAGS_COMSTART_FIRST (next_syntax
)))
592 /* UPDATE_SYNTAX_TABLE_FORWARD (next + 1); */
595 if (com2start
&& comstart_pos
== 0)
596 /* We're looking at a comment starter. But it might be a comment
597 ender as well (see snmp-mode). The first time we see one, we
598 need to consider it as a comment starter,
599 and the subsequent times as a comment ender. */
602 /* Turn a 2-char comment sequences into the appropriate syntax. */
607 /* Ignore comment starters of a different style. */
608 else if (code
== Scomment
609 && (comstyle
!= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0)
610 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
) != comnested
))
613 /* Ignore escaped characters, except comment-enders. */
614 if (code
!= Sendcomment
&& char_quoted (from
, from_byte
))
621 c
= (code
== Sstring_fence
? ST_STRING_STYLE
: ST_COMMENT_STYLE
);
623 /* Track parity of quotes. */
624 if (string_style
== -1)
625 /* Entering a string. */
627 else if (string_style
== c
)
628 /* Leaving the string. */
631 /* If we have two kinds of string delimiters.
632 There's no way to grok this scanning backwards. */
637 /* We've already checked that it is the relevant comstyle. */
638 if (string_style
!= -1 || comment_lossage
|| string_lossage
)
639 /* There are odd string quotes involved, so let's be careful.
640 Test case in Pascal: " { " a { " } */
645 /* Record best comment-starter so far. */
647 comstart_byte
= from_byte
;
649 else if (--nesting
<= 0)
650 /* nested comments have to be balanced, so we don't need to
651 keep looking for earlier ones. We use here the same (slightly
652 incorrect) reasoning as below: since it is followed by uniform
653 paired string quotes, this comment-start has to be outside of
654 strings, else the comment-end itself would be inside a string. */
659 if (SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == comstyle
660 && ((com2end
&& SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax
))
661 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
)) == comnested
)
662 /* This is the same style of comment ender as ours. */
667 /* Anything before that can't count because it would match
668 this comment-ender rather than ours. */
669 from
= stop
; /* Break out of the loop. */
671 else if (comstart_pos
!= 0 || c
!= '\n')
672 /* We're mixing comment styles here, so we'd better be careful.
673 The (comstart_pos != 0 || c != '\n') check is not quite correct
674 (we should just always set comment_lossage), but removing it
675 would imply that any multiline comment in C would go through
676 lossage, which seems overkill.
677 The failure should only happen in the rare cases such as
683 /* Assume a defun-start point is outside of strings. */
684 if (open_paren_in_column_0_is_defun_start
686 || (temp_byte
= dec_bytepos (from_byte
),
687 FETCH_CHAR (temp_byte
) == '\n')))
690 defun_start_byte
= from_byte
;
691 from
= stop
; /* Break out of the loop. */
700 if (comstart_pos
== 0)
703 from_byte
= comment_end_byte
;
704 UPDATE_SYNTAX_TABLE_FORWARD (comment_end
- 1);
706 /* If comstart_pos is set and we get here (ie. didn't jump to `lossage'
707 or `done'), then we've found the beginning of the non-nested comment. */
708 else if (1) /* !comnested */
711 from_byte
= comstart_byte
;
712 UPDATE_SYNTAX_TABLE_FORWARD (from
- 1);
716 struct lisp_parse_state state
;
718 /* We had two kinds of string delimiters mixed up
719 together. Decode this going forwards.
720 Scan fwd from a known safe place (beginning-of-defun)
721 to the one in question; this records where we
722 last passed a comment starter. */
723 /* If we did not already find the defun start, find it now. */
724 if (defun_start
== 0)
726 defun_start
= find_defun_start (comment_end
, comment_end_byte
);
727 defun_start_byte
= find_start_value_byte
;
731 scan_sexps_forward (&state
,
732 defun_start
, defun_start_byte
,
733 comment_end
, TYPE_MINIMUM (EMACS_INT
),
735 defun_start
= comment_end
;
736 if (state
.incomment
== (comnested
? 1 : -1)
737 && state
.comstyle
== comstyle
)
738 from
= state
.comstr_start
;
743 /* If comment_end is inside some other comment, maybe ours
744 is nested, so we need to try again from within the
745 surrounding comment. Example: { a (* " *) */
747 /* FIXME: We should advance by one or two chars. */
748 defun_start
= state
.comstr_start
+ 2;
749 defun_start_byte
= CHAR_TO_BYTE (defun_start
);
752 } while (defun_start
< comment_end
);
754 from_byte
= CHAR_TO_BYTE (from
);
755 UPDATE_SYNTAX_TABLE_FORWARD (from
- 1);
760 *bytepos_ptr
= from_byte
;
762 return from
!= comment_end
;
765 DEFUN ("syntax-table-p", Fsyntax_table_p
, Ssyntax_table_p
, 1, 1, 0,
766 doc
: /* Return t if OBJECT is a syntax table.
767 Currently, any char-table counts as a syntax table. */)
770 if (CHAR_TABLE_P (object
)
771 && EQ (XCHAR_TABLE (object
)->purpose
, Qsyntax_table
))
777 check_syntax_table (Lisp_Object obj
)
779 CHECK_TYPE (CHAR_TABLE_P (obj
) && EQ (XCHAR_TABLE (obj
)->purpose
, Qsyntax_table
),
780 Qsyntax_table_p
, obj
);
783 DEFUN ("syntax-table", Fsyntax_table
, Ssyntax_table
, 0, 0, 0,
784 doc
: /* Return the current syntax table.
785 This is the one specified by the current buffer. */)
788 return BVAR (current_buffer
, syntax_table
);
791 DEFUN ("standard-syntax-table", Fstandard_syntax_table
,
792 Sstandard_syntax_table
, 0, 0, 0,
793 doc
: /* Return the standard syntax table.
794 This is the one used for new buffers. */)
797 return Vstandard_syntax_table
;
800 DEFUN ("copy-syntax-table", Fcopy_syntax_table
, Scopy_syntax_table
, 0, 1, 0,
801 doc
: /* Construct a new syntax table and return it.
802 It is a copy of the TABLE, which defaults to the standard syntax table. */)
808 check_syntax_table (table
);
810 table
= Vstandard_syntax_table
;
812 copy
= Fcopy_sequence (table
);
814 /* Only the standard syntax table should have a default element.
815 Other syntax tables should inherit from parents instead. */
816 set_char_table_defalt (copy
, Qnil
);
818 /* Copied syntax tables should all have parents.
819 If we copied one with no parent, such as the standard syntax table,
820 use the standard syntax table as the copy's parent. */
821 if (NILP (XCHAR_TABLE (copy
)->parent
))
822 Fset_char_table_parent (copy
, Vstandard_syntax_table
);
826 DEFUN ("set-syntax-table", Fset_syntax_table
, Sset_syntax_table
, 1, 1, 0,
827 doc
: /* Select a new syntax table for the current buffer.
828 One argument, a syntax table. */)
832 check_syntax_table (table
);
833 bset_syntax_table (current_buffer
, table
);
834 /* Indicate that this buffer now has a specified syntax table. */
835 idx
= PER_BUFFER_VAR_IDX (syntax_table
);
836 SET_PER_BUFFER_VALUE_P (current_buffer
, idx
, 1);
840 /* Convert a letter which signifies a syntax code
841 into the code it signifies.
842 This is used by modify-syntax-entry, and other things. */
844 unsigned char const syntax_spec_code
[0400] =
845 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
846 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
847 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
848 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
849 Swhitespace
, Scomment_fence
, Sstring
, 0377, Smath
, 0377, 0377, Squote
,
850 Sopen
, Sclose
, 0377, 0377, 0377, Swhitespace
, Spunct
, Scharquote
,
851 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
852 0377, 0377, 0377, 0377, Scomment
, 0377, Sendcomment
, 0377,
853 Sinherit
, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
854 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
855 0377, 0377, 0377, 0377, 0377, 0377, 0377, Sword
,
856 0377, 0377, 0377, 0377, Sescape
, 0377, 0377, Ssymbol
,
857 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
858 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
859 0377, 0377, 0377, 0377, 0377, 0377, 0377, Sword
,
860 0377, 0377, 0377, 0377, Sstring_fence
, 0377, 0377, 0377
863 /* Indexed by syntax code, give the letter that describes it. */
865 char const syntax_code_spec
[16] =
867 ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@',
871 /* Indexed by syntax code, give the object (cons of syntax code and
872 nil) to be stored in syntax table. Since these objects can be
873 shared among syntax tables, we generate them in advance. By
874 sharing objects, the function `describe-syntax' can give a more
876 static Lisp_Object Vsyntax_code_object
;
879 DEFUN ("char-syntax", Fchar_syntax
, Schar_syntax
, 1, 1, 0,
880 doc
: /* Return the syntax code of CHARACTER, described by a character.
881 For example, if CHARACTER is a word constituent, the
882 character `w' (119) is returned.
883 The characters that correspond to various syntax codes
884 are listed in the documentation of `modify-syntax-entry'. */)
885 (Lisp_Object character
)
888 CHECK_CHARACTER (character
);
889 char_int
= XINT (character
);
890 SETUP_BUFFER_SYNTAX_TABLE ();
891 return make_number (syntax_code_spec
[SYNTAX (char_int
)]);
894 DEFUN ("matching-paren", Fmatching_paren
, Smatching_paren
, 1, 1, 0,
895 doc
: /* Return the matching parenthesis of CHARACTER, or nil if none. */)
896 (Lisp_Object character
)
899 enum syntaxcode code
;
900 CHECK_CHARACTER (character
);
901 char_int
= XINT (character
);
902 SETUP_BUFFER_SYNTAX_TABLE ();
903 code
= SYNTAX (char_int
);
904 if (code
== Sopen
|| code
== Sclose
)
905 return SYNTAX_MATCH (char_int
);
909 DEFUN ("string-to-syntax", Fstring_to_syntax
, Sstring_to_syntax
, 1, 1, 0,
910 doc
: /* Convert a syntax descriptor STRING into a raw syntax descriptor.
911 STRING should be a string of the form allowed as argument of
912 `modify-syntax-entry'. The return value is a raw syntax descriptor: a
913 cons cell \(CODE . MATCHING-CHAR) which can be used, for example, as
914 the value of a `syntax-table' text property. */)
917 const unsigned char *p
;
921 CHECK_STRING (string
);
924 val
= syntax_spec_code
[*p
++];
926 error ("Invalid syntax description letter: %c", p
[-1]);
934 int character
= STRING_CHAR_AND_LENGTH (p
, len
);
935 XSETINT (match
, character
);
936 if (XFASTINT (match
) == ' ')
979 if (val
< ASIZE (Vsyntax_code_object
) && NILP (match
))
980 return AREF (Vsyntax_code_object
, val
);
982 /* Since we can't use a shared object, let's make a new one. */
983 return Fcons (make_number (val
), match
);
986 /* I really don't know why this is interactive
987 help-form should at least be made useful whilst reading the second arg. */
988 DEFUN ("modify-syntax-entry", Fmodify_syntax_entry
, Smodify_syntax_entry
, 2, 3,
989 "cSet syntax for character: \nsSet syntax for %s to: ",
990 doc
: /* Set syntax for character CHAR according to string NEWENTRY.
991 The syntax is changed only for table SYNTAX-TABLE, which defaults to
992 the current buffer's syntax table.
993 CHAR may be a cons (MIN . MAX), in which case, syntaxes of all characters
994 in the range MIN to MAX are changed.
995 The first character of NEWENTRY should be one of the following:
996 Space or - whitespace syntax. w word constituent.
997 _ symbol constituent. . punctuation.
998 ( open-parenthesis. ) close-parenthesis.
999 " string quote. \\ escape.
1000 $ paired delimiter. ' expression quote or prefix operator.
1001 < comment starter. > comment ender.
1002 / character-quote. @ inherit from parent table.
1003 | generic string fence. ! generic comment fence.
1005 Only single-character comment start and end sequences are represented thus.
1006 Two-character sequences are represented as described below.
1007 The second character of NEWENTRY is the matching parenthesis,
1008 used only if the first character is `(' or `)'.
1009 Any additional characters are flags.
1010 Defined flags are the characters 1, 2, 3, 4, b, p, and n.
1011 1 means CHAR is the start of a two-char comment start sequence.
1012 2 means CHAR is the second character of such a sequence.
1013 3 means CHAR is the start of a two-char comment end sequence.
1014 4 means CHAR is the second character of such a sequence.
1016 There can be several orthogonal comment sequences. This is to support
1017 language modes such as C++. By default, all comment sequences are of style
1018 a, but you can set the comment sequence style to b (on the second character
1019 of a comment-start, and the first character of a comment-end sequence) and/or
1020 c (on any of its chars) using this flag:
1021 b means CHAR is part of comment sequence b.
1022 c means CHAR is part of comment sequence c.
1023 n means CHAR is part of a nestable comment sequence.
1025 p means CHAR is a prefix character for `backward-prefix-chars';
1026 such characters are treated as whitespace when they occur
1027 between expressions.
1028 usage: (modify-syntax-entry CHAR NEWENTRY &optional SYNTAX-TABLE) */)
1029 (Lisp_Object c
, Lisp_Object newentry
, Lisp_Object syntax_table
)
1033 CHECK_CHARACTER_CAR (c
);
1034 CHECK_CHARACTER_CDR (c
);
1037 CHECK_CHARACTER (c
);
1039 if (NILP (syntax_table
))
1040 syntax_table
= BVAR (current_buffer
, syntax_table
);
1042 check_syntax_table (syntax_table
);
1044 newentry
= Fstring_to_syntax (newentry
);
1046 SET_RAW_SYNTAX_ENTRY_RANGE (syntax_table
, c
, newentry
);
1048 SET_RAW_SYNTAX_ENTRY (syntax_table
, XINT (c
), newentry
);
1050 /* We clear the regexp cache, since character classes can now have
1051 different values from those in the compiled regexps.*/
1052 clear_regexp_cache ();
1057 /* Dump syntax table to buffer in human-readable format */
1059 DEFUN ("internal-describe-syntax-value", Finternal_describe_syntax_value
,
1060 Sinternal_describe_syntax_value
, 1, 1, 0,
1061 doc
: /* Insert a description of the internal syntax description SYNTAX at point. */)
1062 (Lisp_Object syntax
)
1064 int code
, syntax_code
;
1065 bool start1
, start2
, end1
, end2
, prefix
, comstyleb
, comstylec
, comnested
;
1067 Lisp_Object first
, match_lisp
, value
= syntax
;
1071 insert_string ("default");
1075 if (CHAR_TABLE_P (value
))
1077 insert_string ("deeper char-table ...");
1083 insert_string ("invalid");
1087 first
= XCAR (value
);
1088 match_lisp
= XCDR (value
);
1090 if (!INTEGERP (first
) || !(NILP (match_lisp
) || CHARACTERP (match_lisp
)))
1092 insert_string ("invalid");
1096 syntax_code
= XINT (first
) & INT_MAX
;
1097 code
= syntax_code
& 0377;
1098 start1
= SYNTAX_FLAGS_COMSTART_FIRST (syntax_code
);
1099 start2
= SYNTAX_FLAGS_COMSTART_SECOND (syntax_code
);;
1100 end1
= SYNTAX_FLAGS_COMEND_FIRST (syntax_code
);
1101 end2
= SYNTAX_FLAGS_COMEND_SECOND (syntax_code
);
1102 prefix
= SYNTAX_FLAGS_PREFIX (syntax_code
);
1103 comstyleb
= SYNTAX_FLAGS_COMMENT_STYLEB (syntax_code
);
1104 comstylec
= SYNTAX_FLAGS_COMMENT_STYLEC (syntax_code
);
1105 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax_code
);
1109 insert_string ("invalid");
1113 str
[0] = syntax_code_spec
[code
], str
[1] = 0;
1116 if (NILP (match_lisp
))
1119 insert_char (XINT (match_lisp
));
1140 insert_string ("\twhich means: ");
1145 insert_string ("whitespace"); break;
1147 insert_string ("punctuation"); break;
1149 insert_string ("word"); break;
1151 insert_string ("symbol"); break;
1153 insert_string ("open"); break;
1155 insert_string ("close"); break;
1157 insert_string ("prefix"); break;
1159 insert_string ("string"); break;
1161 insert_string ("math"); break;
1163 insert_string ("escape"); break;
1165 insert_string ("charquote"); break;
1167 insert_string ("comment"); break;
1169 insert_string ("endcomment"); break;
1171 insert_string ("inherit"); break;
1172 case Scomment_fence
:
1173 insert_string ("comment fence"); break;
1175 insert_string ("string fence"); break;
1177 insert_string ("invalid");
1181 if (!NILP (match_lisp
))
1183 insert_string (", matches ");
1184 insert_char (XINT (match_lisp
));
1188 insert_string (",\n\t is the first character of a comment-start sequence");
1190 insert_string (",\n\t is the second character of a comment-start sequence");
1193 insert_string (",\n\t is the first character of a comment-end sequence");
1195 insert_string (",\n\t is the second character of a comment-end sequence");
1197 insert_string (" (comment style b)");
1199 insert_string (" (comment style c)");
1201 insert_string (" (nestable)");
1204 insert_string (",\n\t is a prefix character for `backward-prefix-chars'");
1209 /* Return the position across COUNT words from FROM.
1210 If that many words cannot be found before the end of the buffer, return 0.
1211 COUNT negative means scan backward and stop at word beginning. */
1214 scan_words (register ptrdiff_t from
, register EMACS_INT count
)
1216 register ptrdiff_t beg
= BEGV
;
1217 register ptrdiff_t end
= ZV
;
1218 register ptrdiff_t from_byte
= CHAR_TO_BYTE (from
);
1219 register enum syntaxcode code
;
1221 Lisp_Object func
, pos
;
1226 SETUP_SYNTAX_TABLE (from
, count
);
1237 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1238 ch0
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1239 code
= SYNTAX (ch0
);
1240 INC_BOTH (from
, from_byte
);
1241 if (words_include_escapes
1242 && (code
== Sescape
|| code
== Scharquote
))
1247 /* Now CH0 is a character which begins a word and FROM is the
1248 position of the next character. */
1249 func
= CHAR_TABLE_REF (Vfind_word_boundary_function_table
, ch0
);
1250 if (! NILP (Ffboundp (func
)))
1252 pos
= call2 (func
, make_number (from
- 1), make_number (end
));
1253 if (INTEGERP (pos
) && from
< XINT (pos
) && XINT (pos
) <= ZV
)
1256 from_byte
= CHAR_TO_BYTE (from
);
1263 if (from
== end
) break;
1264 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1265 ch1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1266 code
= SYNTAX (ch1
);
1268 && (! words_include_escapes
1269 || (code
!= Sescape
&& code
!= Scharquote
)))
1270 || word_boundary_p (ch0
, ch1
))
1272 INC_BOTH (from
, from_byte
);
1287 DEC_BOTH (from
, from_byte
);
1288 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
1289 ch1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1290 code
= SYNTAX (ch1
);
1291 if (words_include_escapes
1292 && (code
== Sescape
|| code
== Scharquote
))
1297 /* Now CH1 is a character which ends a word and FROM is the
1299 func
= CHAR_TABLE_REF (Vfind_word_boundary_function_table
, ch1
);
1300 if (! NILP (Ffboundp (func
)))
1302 pos
= call2 (func
, make_number (from
), make_number (beg
));
1303 if (INTEGERP (pos
) && BEGV
<= XINT (pos
) && XINT (pos
) < from
)
1306 from_byte
= CHAR_TO_BYTE (from
);
1315 DEC_BOTH (from
, from_byte
);
1316 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
1317 ch0
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1318 code
= SYNTAX (ch0
);
1320 && (! words_include_escapes
1321 || (code
!= Sescape
&& code
!= Scharquote
)))
1322 || word_boundary_p (ch0
, ch1
))
1324 INC_BOTH (from
, from_byte
);
1338 DEFUN ("forward-word", Fforward_word
, Sforward_word
, 0, 1, "^p",
1339 doc
: /* Move point forward ARG words (backward if ARG is negative).
1341 If an edge of the buffer or a field boundary is reached, point is left there
1342 and the function returns nil. Field boundaries are not noticed if
1343 `inhibit-field-text-motion' is non-nil. */)
1347 ptrdiff_t orig_val
, val
;
1350 XSETFASTINT (arg
, 1);
1354 val
= orig_val
= scan_words (PT
, XINT (arg
));
1356 val
= XINT (arg
) > 0 ? ZV
: BEGV
;
1358 /* Avoid jumping out of an input field. */
1359 tmp
= Fconstrain_to_field (make_number (val
), make_number (PT
),
1361 val
= XFASTINT (tmp
);
1364 return val
== orig_val
? Qt
: Qnil
;
1367 DEFUN ("skip-chars-forward", Fskip_chars_forward
, Sskip_chars_forward
, 1, 2, 0,
1368 doc
: /* Move point forward, stopping before a char not in STRING, or at pos LIM.
1369 STRING is like the inside of a `[...]' in a regular expression
1370 except that `]' is never special and `\\' quotes `^', `-' or `\\'
1371 (but not at the end of a range; quoting is never needed there).
1372 Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
1373 With arg "^a-zA-Z", skips nonletters stopping before first letter.
1374 Char classes, e.g. `[:alpha:]', are supported.
1376 Returns the distance traveled, either zero or positive. */)
1377 (Lisp_Object string
, Lisp_Object lim
)
1379 return skip_chars (1, string
, lim
, 1);
1382 DEFUN ("skip-chars-backward", Fskip_chars_backward
, Sskip_chars_backward
, 1, 2, 0,
1383 doc
: /* Move point backward, stopping after a char not in STRING, or at pos LIM.
1384 See `skip-chars-forward' for details.
1385 Returns the distance traveled, either zero or negative. */)
1386 (Lisp_Object string
, Lisp_Object lim
)
1388 return skip_chars (0, string
, lim
, 1);
1391 DEFUN ("skip-syntax-forward", Fskip_syntax_forward
, Sskip_syntax_forward
, 1, 2, 0,
1392 doc
: /* Move point forward across chars in specified syntax classes.
1393 SYNTAX is a string of syntax code characters.
1394 Stop before a char whose syntax is not in SYNTAX, or at position LIM.
1395 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1396 This function returns the distance traveled, either zero or positive. */)
1397 (Lisp_Object syntax
, Lisp_Object lim
)
1399 return skip_syntaxes (1, syntax
, lim
);
1402 DEFUN ("skip-syntax-backward", Fskip_syntax_backward
, Sskip_syntax_backward
, 1, 2, 0,
1403 doc
: /* Move point backward across chars in specified syntax classes.
1404 SYNTAX is a string of syntax code characters.
1405 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.
1406 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1407 This function returns the distance traveled, either zero or negative. */)
1408 (Lisp_Object syntax
, Lisp_Object lim
)
1410 return skip_syntaxes (0, syntax
, lim
);
1414 skip_chars (bool forwardp
, Lisp_Object string
, Lisp_Object lim
,
1415 bool handle_iso_classes
)
1419 /* Store the ranges of non-ASCII characters. */
1420 int *char_ranges
IF_LINT (= NULL
);
1421 int n_char_ranges
= 0;
1423 ptrdiff_t i
, i_byte
;
1424 /* True if the current buffer is multibyte and the region contains
1427 /* True if STRING is multibyte and it contains non-ASCII chars. */
1428 bool string_multibyte
;
1429 ptrdiff_t size_byte
;
1430 const unsigned char *str
;
1432 Lisp_Object iso_classes
;
1434 CHECK_STRING (string
);
1438 XSETINT (lim
, forwardp
? ZV
: BEGV
);
1440 CHECK_NUMBER_COERCE_MARKER (lim
);
1442 /* In any case, don't allow scan outside bounds of buffer. */
1443 if (XINT (lim
) > ZV
)
1444 XSETFASTINT (lim
, ZV
);
1445 if (XINT (lim
) < BEGV
)
1446 XSETFASTINT (lim
, BEGV
);
1448 multibyte
= (!NILP (BVAR (current_buffer
, enable_multibyte_characters
))
1449 && (XINT (lim
) - PT
!= CHAR_TO_BYTE (XINT (lim
)) - PT_BYTE
));
1450 string_multibyte
= SBYTES (string
) > SCHARS (string
);
1452 memset (fastmap
, 0, sizeof fastmap
);
1454 str
= SDATA (string
);
1455 size_byte
= SBYTES (string
);
1458 if (i_byte
< size_byte
1459 && SREF (string
, 0) == '^')
1461 negate
= 1; i_byte
++;
1464 /* Find the characters specified and set their elements of fastmap.
1465 Handle backslashes and ranges specially.
1467 If STRING contains non-ASCII characters, setup char_ranges for
1468 them and use fastmap only for their leading codes. */
1470 if (! string_multibyte
)
1472 bool string_has_eight_bit
= 0;
1474 /* At first setup fastmap. */
1475 while (i_byte
< size_byte
)
1479 if (handle_iso_classes
&& c
== '['
1480 && i_byte
< size_byte
1481 && str
[i_byte
] == ':')
1483 const unsigned char *class_beg
= str
+ i_byte
+ 1;
1484 const unsigned char *class_end
= class_beg
;
1485 const unsigned char *class_limit
= str
+ size_byte
- 2;
1486 /* Leave room for the null. */
1487 unsigned char class_name
[CHAR_CLASS_MAX_LENGTH
+ 1];
1490 if (class_limit
- class_beg
> CHAR_CLASS_MAX_LENGTH
)
1491 class_limit
= class_beg
+ CHAR_CLASS_MAX_LENGTH
;
1493 while (class_end
< class_limit
1494 && *class_end
>= 'a' && *class_end
<= 'z')
1497 if (class_end
== class_beg
1498 || *class_end
!= ':' || class_end
[1] != ']')
1499 goto not_a_class_name
;
1501 memcpy (class_name
, class_beg
, class_end
- class_beg
);
1502 class_name
[class_end
- class_beg
] = 0;
1504 cc
= re_wctype (class_name
);
1506 error ("Invalid ISO C character class");
1508 iso_classes
= Fcons (make_number (cc
), iso_classes
);
1510 i_byte
= class_end
+ 2 - str
;
1517 if (i_byte
== size_byte
)
1522 /* Treat `-' as range character only if another character
1524 if (i_byte
+ 1 < size_byte
1525 && str
[i_byte
] == '-')
1529 /* Skip over the dash. */
1532 /* Get the end of the range. */
1535 && i_byte
< size_byte
)
1543 if (! ASCII_CHAR_P (c2
))
1544 string_has_eight_bit
= 1;
1550 if (! ASCII_CHAR_P (c
))
1551 string_has_eight_bit
= 1;
1555 /* If the current range is multibyte and STRING contains
1556 eight-bit chars, arrange fastmap and setup char_ranges for
1557 the corresponding multibyte chars. */
1558 if (multibyte
&& string_has_eight_bit
)
1561 char himap
[0200 + 1];
1562 memcpy (himap
, fastmap
+ 0200, 0200);
1564 memset (fastmap
+ 0200, 0, 0200);
1565 char_ranges
= alloca (sizeof *char_ranges
* 128 * 2);
1568 while ((p1
= memchr (himap
+ i
, 1, 0200 - i
)))
1570 /* Deduce the next range C..C2 from the next clump of 1s
1571 in HIMAP starting with &HIMAP[I]. HIMAP is the high
1572 order half of the old FASTMAP. */
1573 int c2
, leading_code
;
1575 c
= BYTE8_TO_CHAR (i
+ 0200);
1577 c2
= BYTE8_TO_CHAR (i
+ 0200 - 1);
1579 char_ranges
[n_char_ranges
++] = c
;
1580 char_ranges
[n_char_ranges
++] = c2
;
1581 leading_code
= CHAR_LEADING_CODE (c
);
1582 memset (fastmap
+ leading_code
, 1,
1583 CHAR_LEADING_CODE (c2
) - leading_code
+ 1);
1587 else /* STRING is multibyte */
1589 char_ranges
= alloca (sizeof *char_ranges
* SCHARS (string
) * 2);
1591 while (i_byte
< size_byte
)
1593 int leading_code
= str
[i_byte
];
1594 c
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1597 if (handle_iso_classes
&& c
== '['
1598 && i_byte
< size_byte
1599 && STRING_CHAR (str
+ i_byte
) == ':')
1601 const unsigned char *class_beg
= str
+ i_byte
+ 1;
1602 const unsigned char *class_end
= class_beg
;
1603 const unsigned char *class_limit
= str
+ size_byte
- 2;
1604 /* Leave room for the null. */
1605 unsigned char class_name
[CHAR_CLASS_MAX_LENGTH
+ 1];
1608 if (class_limit
- class_beg
> CHAR_CLASS_MAX_LENGTH
)
1609 class_limit
= class_beg
+ CHAR_CLASS_MAX_LENGTH
;
1611 while (class_end
< class_limit
1612 && *class_end
>= 'a' && *class_end
<= 'z')
1615 if (class_end
== class_beg
1616 || *class_end
!= ':' || class_end
[1] != ']')
1617 goto not_a_class_name_multibyte
;
1619 memcpy (class_name
, class_beg
, class_end
- class_beg
);
1620 class_name
[class_end
- class_beg
] = 0;
1622 cc
= re_wctype (class_name
);
1624 error ("Invalid ISO C character class");
1626 iso_classes
= Fcons (make_number (cc
), iso_classes
);
1628 i_byte
= class_end
+ 2 - str
;
1632 not_a_class_name_multibyte
:
1635 if (i_byte
== size_byte
)
1638 leading_code
= str
[i_byte
];
1639 c
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1642 /* Treat `-' as range character only if another character
1644 if (i_byte
+ 1 < size_byte
1645 && str
[i_byte
] == '-')
1647 int c2
, leading_code2
;
1649 /* Skip over the dash. */
1652 /* Get the end of the range. */
1653 leading_code2
= str
[i_byte
];
1654 c2
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1658 && i_byte
< size_byte
)
1660 leading_code2
= str
[i_byte
];
1661 c2
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1667 if (ASCII_CHAR_P (c
))
1669 while (c
<= c2
&& c
< 0x80)
1671 leading_code
= CHAR_LEADING_CODE (c
);
1673 if (! ASCII_CHAR_P (c
))
1675 int lim2
= leading_code2
+ 1;
1676 while (leading_code
< lim2
)
1677 fastmap
[leading_code
++] = 1;
1680 char_ranges
[n_char_ranges
++] = c
;
1681 char_ranges
[n_char_ranges
++] = c2
;
1687 if (ASCII_CHAR_P (c
))
1691 fastmap
[leading_code
] = 1;
1692 char_ranges
[n_char_ranges
++] = c
;
1693 char_ranges
[n_char_ranges
++] = c
;
1698 /* If the current range is unibyte and STRING contains non-ASCII
1699 chars, arrange fastmap for the corresponding unibyte
1702 if (! multibyte
&& n_char_ranges
> 0)
1704 memset (fastmap
+ 0200, 0, 0200);
1705 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1707 int c1
= char_ranges
[i
];
1708 int lim2
= char_ranges
[i
+ 1] + 1;
1710 for (; c1
< lim2
; c1
++)
1712 int b
= CHAR_TO_BYTE_SAFE (c1
);
1720 /* If ^ was the first character, complement the fastmap. */
1724 for (i
= 0; i
< sizeof fastmap
; i
++)
1728 for (i
= 0; i
< 0200; i
++)
1730 /* All non-ASCII chars possibly match. */
1731 for (; i
< sizeof fastmap
; i
++)
1737 ptrdiff_t start_point
= PT
;
1739 ptrdiff_t pos_byte
= PT_BYTE
;
1740 unsigned char *p
= PT_ADDR
, *endp
, *stop
;
1744 endp
= (XINT (lim
) == GPT
) ? GPT_ADDR
: CHAR_POS_ADDR (XINT (lim
));
1745 stop
= (pos
< GPT
&& GPT
< XINT (lim
)) ? GPT_ADDR
: endp
;
1749 endp
= CHAR_POS_ADDR (XINT (lim
));
1750 stop
= (pos
>= GPT
&& GPT
> XINT (lim
)) ? GAP_END_ADDR
: endp
;
1754 /* This code may look up syntax tables using macros that rely on the
1755 gl_state object. To make sure this object is not out of date,
1756 let's initialize it manually.
1757 We ignore syntax-table text-properties for now, since that's
1758 what we've done in the past. */
1759 SETUP_BUFFER_SYNTAX_TABLE ();
1774 c
= STRING_CHAR_AND_LENGTH (p
, nbytes
);
1775 if (! NILP (iso_classes
) && in_classes (c
, iso_classes
))
1785 if (! ASCII_CHAR_P (c
))
1787 /* As we are looking at a multibyte character, we
1788 must look up the character in the table
1789 CHAR_RANGES. If there's no data in the table,
1790 that character is not what we want to skip. */
1792 /* The following code do the right thing even if
1793 n_char_ranges is zero (i.e. no data in
1795 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1796 if (c
>= char_ranges
[i
] && c
<= char_ranges
[i
+ 1])
1798 if (!(negate
^ (i
< n_char_ranges
)))
1802 p
+= nbytes
, pos
++, pos_byte
+= nbytes
;
1815 if (!NILP (iso_classes
) && in_classes (*p
, iso_classes
))
1820 goto fwd_unibyte_ok
;
1826 p
++, pos
++, pos_byte
++;
1834 unsigned char *prev_p
;
1844 while (--p
>= stop
&& ! CHAR_HEAD_P (*p
));
1845 c
= STRING_CHAR (p
);
1847 if (! NILP (iso_classes
) && in_classes (c
, iso_classes
))
1857 if (! ASCII_CHAR_P (c
))
1859 /* See the comment in the previous similar code. */
1860 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1861 if (c
>= char_ranges
[i
] && c
<= char_ranges
[i
+ 1])
1863 if (!(negate
^ (i
< n_char_ranges
)))
1867 pos
--, pos_byte
-= prev_p
- p
;
1880 if (! NILP (iso_classes
) && in_classes (p
[-1], iso_classes
))
1885 goto back_unibyte_ok
;
1888 if (!fastmap
[p
[-1]])
1891 p
--, pos
--, pos_byte
--;
1895 SET_PT_BOTH (pos
, pos_byte
);
1898 return make_number (PT
- start_point
);
1904 skip_syntaxes (bool forwardp
, Lisp_Object string
, Lisp_Object lim
)
1907 unsigned char fastmap
[0400];
1909 ptrdiff_t i
, i_byte
;
1911 ptrdiff_t size_byte
;
1914 CHECK_STRING (string
);
1917 XSETINT (lim
, forwardp
? ZV
: BEGV
);
1919 CHECK_NUMBER_COERCE_MARKER (lim
);
1921 /* In any case, don't allow scan outside bounds of buffer. */
1922 if (XINT (lim
) > ZV
)
1923 XSETFASTINT (lim
, ZV
);
1924 if (XINT (lim
) < BEGV
)
1925 XSETFASTINT (lim
, BEGV
);
1927 if (forwardp
? (PT
>= XFASTINT (lim
)) : (PT
<= XFASTINT (lim
)))
1928 return make_number (0);
1930 multibyte
= (!NILP (BVAR (current_buffer
, enable_multibyte_characters
))
1931 && (XINT (lim
) - PT
!= CHAR_TO_BYTE (XINT (lim
)) - PT_BYTE
));
1933 memset (fastmap
, 0, sizeof fastmap
);
1935 if (SBYTES (string
) > SCHARS (string
))
1936 /* As this is very rare case (syntax spec is ASCII only), don't
1937 consider efficiency. */
1938 string
= string_make_unibyte (string
);
1940 str
= SDATA (string
);
1941 size_byte
= SBYTES (string
);
1944 if (i_byte
< size_byte
1945 && SREF (string
, 0) == '^')
1947 negate
= 1; i_byte
++;
1950 /* Find the syntaxes specified and set their elements of fastmap. */
1952 while (i_byte
< size_byte
)
1955 fastmap
[syntax_spec_code
[c
]] = 1;
1958 /* If ^ was the first character, complement the fastmap. */
1960 for (i
= 0; i
< sizeof fastmap
; i
++)
1964 ptrdiff_t start_point
= PT
;
1966 ptrdiff_t pos_byte
= PT_BYTE
;
1967 unsigned char *p
= PT_ADDR
, *endp
, *stop
;
1971 endp
= (XINT (lim
) == GPT
) ? GPT_ADDR
: CHAR_POS_ADDR (XINT (lim
));
1972 stop
= (pos
< GPT
&& GPT
< XINT (lim
)) ? GPT_ADDR
: endp
;
1976 endp
= CHAR_POS_ADDR (XINT (lim
));
1977 stop
= (pos
>= GPT
&& GPT
> XINT (lim
)) ? GAP_END_ADDR
: endp
;
1981 SETUP_SYNTAX_TABLE (pos
, forwardp
? 1 : -1);
1997 c
= STRING_CHAR_AND_LENGTH (p
, nbytes
);
1998 if (! fastmap
[SYNTAX (c
)])
2000 p
+= nbytes
, pos
++, pos_byte
+= nbytes
;
2001 UPDATE_SYNTAX_TABLE_FORWARD (pos
);
2015 if (! fastmap
[SYNTAX (*p
)])
2017 p
++, pos
++, pos_byte
++;
2018 UPDATE_SYNTAX_TABLE_FORWARD (pos
);
2028 unsigned char *prev_p
;
2037 UPDATE_SYNTAX_TABLE_BACKWARD (pos
- 1);
2039 while (--p
>= stop
&& ! CHAR_HEAD_P (*p
));
2040 c
= STRING_CHAR (p
);
2041 if (! fastmap
[SYNTAX (c
)])
2043 pos
--, pos_byte
-= prev_p
- p
;
2057 UPDATE_SYNTAX_TABLE_BACKWARD (pos
- 1);
2058 if (! fastmap
[SYNTAX (p
[-1])])
2060 p
--, pos
--, pos_byte
--;
2065 SET_PT_BOTH (pos
, pos_byte
);
2068 return make_number (PT
- start_point
);
2072 /* Return true if character C belongs to one of the ISO classes
2073 in the list ISO_CLASSES. Each class is represented by an
2074 integer which is its type according to re_wctype. */
2077 in_classes (int c
, Lisp_Object iso_classes
)
2079 bool fits_class
= 0;
2081 while (CONSP (iso_classes
))
2084 elt
= XCAR (iso_classes
);
2085 iso_classes
= XCDR (iso_classes
);
2087 if (re_iswctype (c
, XFASTINT (elt
)))
2094 /* Jump over a comment, assuming we are at the beginning of one.
2095 FROM is the current position.
2096 FROM_BYTE is the bytepos corresponding to FROM.
2097 Do not move past STOP (a charpos).
2098 The comment over which we have to jump is of style STYLE
2099 (either SYNTAX_FLAGS_COMMENT_STYLE (foo) or ST_COMMENT_STYLE).
2100 NESTING should be positive to indicate the nesting at the beginning
2101 for nested comments and should be zero or negative else.
2102 ST_COMMENT_STYLE cannot be nested.
2103 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
2104 (or 0 If the search cannot start in the middle of a two-character).
2106 If successful, return true and store the charpos of the comment's end
2107 into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR.
2108 Else, return false and store the charpos STOP into *CHARPOS_PTR, the
2109 corresponding bytepos into *BYTEPOS_PTR and the current nesting
2110 (as defined for state.incomment) in *INCOMMENT_PTR.
2112 The comment end is the last character of the comment rather than the
2113 character just after the comment.
2115 Global syntax data is assumed to initially be valid for FROM and
2116 remains valid for forward search starting at the returned position. */
2119 forw_comment (ptrdiff_t from
, ptrdiff_t from_byte
, ptrdiff_t stop
,
2120 EMACS_INT nesting
, int style
, int prev_syntax
,
2121 ptrdiff_t *charpos_ptr
, ptrdiff_t *bytepos_ptr
,
2122 EMACS_INT
*incomment_ptr
)
2125 register enum syntaxcode code
;
2126 register int syntax
, other_syntax
;
2128 if (nesting
<= 0) nesting
= -1;
2130 /* Enter the loop in the middle so that we find
2131 a 2-char comment ender if we start in the middle of it. */
2132 syntax
= prev_syntax
;
2133 if (syntax
!= 0) goto forw_incomment
;
2139 *incomment_ptr
= nesting
;
2140 *charpos_ptr
= from
;
2141 *bytepos_ptr
= from_byte
;
2144 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2145 syntax
= SYNTAX_WITH_FLAGS (c
);
2146 code
= syntax
& 0xff;
2147 if (code
== Sendcomment
2148 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == style
2149 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ?
2150 (nesting
> 0 && --nesting
== 0) : nesting
< 0))
2151 /* we have encountered a comment end of the same style
2152 as the comment sequence which began this comment
2155 if (code
== Scomment_fence
2156 && style
== ST_COMMENT_STYLE
)
2157 /* we have encountered a comment end of the same style
2158 as the comment sequence which began this comment
2163 && SYNTAX_FLAGS_COMMENT_NESTED (syntax
)
2164 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == style
)
2165 /* we have encountered a nested comment of the same style
2166 as the comment sequence which began this comment section */
2168 INC_BOTH (from
, from_byte
);
2169 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2172 if (from
< stop
&& SYNTAX_FLAGS_COMEND_FIRST (syntax
)
2173 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2174 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2175 SYNTAX_FLAGS_COMEND_SECOND (other_syntax
))
2176 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, other_syntax
) == style
2177 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ||
2178 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
))
2179 ? nesting
> 0 : nesting
< 0))
2182 /* we have encountered a comment end of the same style
2183 as the comment sequence which began this comment
2188 INC_BOTH (from
, from_byte
);
2189 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2194 && SYNTAX_FLAGS_COMSTART_FIRST (syntax
)
2195 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2196 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2197 SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
) == style
2198 && SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
))
2199 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ||
2200 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
)))
2201 /* we have encountered a nested comment of the same style
2202 as the comment sequence which began this comment
2205 INC_BOTH (from
, from_byte
);
2206 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2210 *charpos_ptr
= from
;
2211 *bytepos_ptr
= from_byte
;
2215 DEFUN ("forward-comment", Fforward_comment
, Sforward_comment
, 1, 1, 0,
2217 Move forward across up to COUNT comments. If COUNT is negative, move backward.
2218 Stop scanning if we find something other than a comment or whitespace.
2219 Set point to where scanning stops.
2220 If COUNT comments are found as expected, with nothing except whitespace
2221 between them, return t; otherwise return nil. */)
2224 ptrdiff_t from
, from_byte
, stop
;
2226 enum syntaxcode code
;
2227 int comstyle
= 0; /* style of comment encountered */
2228 bool comnested
= 0; /* whether the comment is nestable or not */
2231 ptrdiff_t out_charpos
, out_bytepos
;
2234 CHECK_NUMBER (count
);
2235 count1
= XINT (count
);
2236 stop
= count1
> 0 ? ZV
: BEGV
;
2242 from_byte
= PT_BYTE
;
2244 SETUP_SYNTAX_TABLE (from
, count1
);
2249 bool comstart_first
;
2250 int syntax
, other_syntax
;
2254 SET_PT_BOTH (from
, from_byte
);
2258 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2259 syntax
= SYNTAX_WITH_FLAGS (c
);
2261 comstart_first
= SYNTAX_FLAGS_COMSTART_FIRST (syntax
);
2262 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2263 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2264 INC_BOTH (from
, from_byte
);
2265 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2266 if (from
< stop
&& comstart_first
2267 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2268 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2269 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
)))
2271 /* We have encountered a comment start sequence and we
2272 are ignoring all text inside comments. We must record
2273 the comment style this sequence begins so that later,
2274 only a comment end of the same style actually ends
2275 the comment section. */
2277 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2278 comnested
|= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2279 INC_BOTH (from
, from_byte
);
2280 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2283 while (code
== Swhitespace
|| (code
== Sendcomment
&& c
== '\n'));
2285 if (code
== Scomment_fence
)
2286 comstyle
= ST_COMMENT_STYLE
;
2287 else if (code
!= Scomment
)
2290 DEC_BOTH (from
, from_byte
);
2291 SET_PT_BOTH (from
, from_byte
);
2294 /* We're at the start of a comment. */
2295 found
= forw_comment (from
, from_byte
, stop
, comnested
, comstyle
, 0,
2296 &out_charpos
, &out_bytepos
, &dummy
);
2297 from
= out_charpos
; from_byte
= out_bytepos
;
2301 SET_PT_BOTH (from
, from_byte
);
2304 INC_BOTH (from
, from_byte
);
2305 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2306 /* We have skipped one comment. */
2319 SET_PT_BOTH (BEGV
, BEGV_BYTE
);
2324 DEC_BOTH (from
, from_byte
);
2325 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
2326 quoted
= char_quoted (from
, from_byte
);
2327 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2328 syntax
= SYNTAX_WITH_FLAGS (c
);
2331 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2332 if (code
== Sendcomment
)
2333 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2334 if (from
> stop
&& SYNTAX_FLAGS_COMEND_SECOND (syntax
)
2335 && prev_char_comend_first (from
, from_byte
)
2336 && !char_quoted (from
- 1, dec_bytepos (from_byte
)))
2339 /* We must record the comment style encountered so that
2340 later, we can match only the proper comment begin
2341 sequence of the same style. */
2342 DEC_BOTH (from
, from_byte
);
2344 /* Calling char_quoted, above, set up global syntax position
2345 at the new value of FROM. */
2346 c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2347 other_syntax
= SYNTAX_WITH_FLAGS (c1
);
2348 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2349 comnested
|= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2352 if (code
== Scomment_fence
)
2354 /* Skip until first preceding unquoted comment_fence. */
2355 bool fence_found
= 0;
2356 ptrdiff_t ini
= from
, ini_byte
= from_byte
;
2360 DEC_BOTH (from
, from_byte
);
2361 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2362 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2363 if (SYNTAX (c
) == Scomment_fence
2364 && !char_quoted (from
, from_byte
))
2369 else if (from
== stop
)
2372 if (fence_found
== 0)
2374 from
= ini
; /* Set point to ini + 1. */
2375 from_byte
= ini_byte
;
2379 /* We have skipped one comment. */
2382 else if (code
== Sendcomment
)
2384 found
= back_comment (from
, from_byte
, stop
, comnested
, comstyle
,
2385 &out_charpos
, &out_bytepos
);
2389 /* This end-of-line is not an end-of-comment.
2390 Treat it like a whitespace.
2391 CC-mode (and maybe others) relies on this behavior. */
2395 /* Failure: we should go back to the end of this
2396 not-quite-endcomment. */
2397 if (SYNTAX (c
) != code
)
2398 /* It was a two-char Sendcomment. */
2399 INC_BOTH (from
, from_byte
);
2405 /* We have skipped one comment. */
2406 from
= out_charpos
, from_byte
= out_bytepos
;
2410 else if (code
!= Swhitespace
|| quoted
)
2414 INC_BOTH (from
, from_byte
);
2415 SET_PT_BOTH (from
, from_byte
);
2423 SET_PT_BOTH (from
, from_byte
);
2428 /* Return syntax code of character C if C is an ASCII character
2429 or `multibyte_symbol_p' is zero. Otherwise, return Ssymbol. */
2431 #define SYNTAX_WITH_MULTIBYTE_CHECK(c) \
2432 ((ASCII_CHAR_P (c) || !multibyte_symbol_p) \
2433 ? SYNTAX (c) : Ssymbol)
2436 scan_lists (EMACS_INT from
, EMACS_INT count
, EMACS_INT depth
, bool sexpflag
)
2439 ptrdiff_t stop
= count
> 0 ? ZV
: BEGV
;
2444 enum syntaxcode code
, temp_code
, c_code
;
2445 EMACS_INT min_depth
= depth
; /* Err out if depth gets less than this. */
2446 int comstyle
= 0; /* style of comment encountered */
2447 bool comnested
= 0; /* whether the comment is nestable or not */
2449 EMACS_INT last_good
= from
;
2451 ptrdiff_t from_byte
;
2452 ptrdiff_t out_bytepos
, out_charpos
;
2454 bool multibyte_symbol_p
= sexpflag
&& multibyte_syntax_as_symbol
;
2456 if (depth
> 0) min_depth
= 0;
2458 if (from
> ZV
) from
= ZV
;
2459 if (from
< BEGV
) from
= BEGV
;
2461 from_byte
= CHAR_TO_BYTE (from
);
2466 SETUP_SYNTAX_TABLE (from
, count
);
2471 bool comstart_first
, prefix
;
2472 int syntax
, other_syntax
;
2473 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2474 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2475 syntax
= SYNTAX_WITH_FLAGS (c
);
2476 code
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2477 comstart_first
= SYNTAX_FLAGS_COMSTART_FIRST (syntax
);
2478 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2479 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2480 prefix
= SYNTAX_FLAGS_PREFIX (syntax
);
2481 if (depth
== min_depth
)
2483 INC_BOTH (from
, from_byte
);
2484 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2485 if (from
< stop
&& comstart_first
2486 && (c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2487 other_syntax
= SYNTAX_WITH_FLAGS (c
),
2488 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
))
2489 && parse_sexp_ignore_comments
)
2491 /* we have encountered a comment start sequence and we
2492 are ignoring all text inside comments. We must record
2493 the comment style this sequence begins so that later,
2494 only a comment end of the same style actually ends
2495 the comment section */
2497 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2498 comnested
|= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2499 INC_BOTH (from
, from_byte
);
2500 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2512 INC_BOTH (from
, from_byte
);
2513 /* treat following character as a word constituent */
2516 if (depth
|| !sexpflag
) break;
2517 /* This word counts as a sexp; return at end of it. */
2520 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2522 /* Some compilers can't handle this inside the switch. */
2523 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2524 c_code
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2529 INC_BOTH (from
, from_byte
);
2540 INC_BOTH (from
, from_byte
);
2544 case Scomment_fence
:
2545 comstyle
= ST_COMMENT_STYLE
;
2548 if (!parse_sexp_ignore_comments
) break;
2549 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2550 found
= forw_comment (from
, from_byte
, stop
,
2551 comnested
, comstyle
, 0,
2552 &out_charpos
, &out_bytepos
, &dummy
);
2553 from
= out_charpos
, from_byte
= out_bytepos
;
2560 INC_BOTH (from
, from_byte
);
2561 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2567 if (from
!= stop
&& c
== FETCH_CHAR_AS_MULTIBYTE (from_byte
))
2569 INC_BOTH (from
, from_byte
);
2579 if (!++depth
) goto done
;
2584 if (!--depth
) goto done
;
2585 if (depth
< min_depth
)
2586 xsignal3 (Qscan_error
,
2587 build_string ("Containing expression ends prematurely"),
2588 make_number (last_good
), make_number (from
));
2593 temp_pos
= dec_bytepos (from_byte
);
2594 stringterm
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2599 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2600 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2603 && SYNTAX_WITH_MULTIBYTE_CHECK (c
) == Sstring
)
2604 : SYNTAX_WITH_MULTIBYTE_CHECK (c
) == Sstring_fence
)
2607 /* Some compilers can't handle this inside the switch. */
2608 c_code
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2613 INC_BOTH (from
, from_byte
);
2615 INC_BOTH (from
, from_byte
);
2617 INC_BOTH (from
, from_byte
);
2618 if (!depth
&& sexpflag
) goto done
;
2621 /* Ignore whitespace, punctuation, quote, endcomment. */
2626 /* Reached end of buffer. Error if within object, return nil if between */
2633 /* End of object reached */
2643 DEC_BOTH (from
, from_byte
);
2644 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2645 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2646 syntax
= SYNTAX_WITH_FLAGS (c
);
2647 code
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2648 if (depth
== min_depth
)
2651 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2652 if (code
== Sendcomment
)
2653 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2654 if (from
> stop
&& SYNTAX_FLAGS_COMEND_SECOND (syntax
)
2655 && prev_char_comend_first (from
, from_byte
)
2656 && parse_sexp_ignore_comments
)
2658 /* We must record the comment style encountered so that
2659 later, we can match only the proper comment begin
2660 sequence of the same style. */
2661 int c2
, other_syntax
;
2662 DEC_BOTH (from
, from_byte
);
2663 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2665 c2
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2666 other_syntax
= SYNTAX_WITH_FLAGS (c2
);
2667 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2668 comnested
|= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2671 /* Quoting turns anything except a comment-ender
2672 into a word character. Note that this cannot be true
2673 if we decremented FROM in the if-statement above. */
2674 if (code
!= Sendcomment
&& char_quoted (from
, from_byte
))
2676 DEC_BOTH (from
, from_byte
);
2679 else if (SYNTAX_FLAGS_PREFIX (syntax
))
2688 if (depth
|| !sexpflag
) break;
2689 /* This word counts as a sexp; count object finished
2690 after passing it. */
2693 temp_pos
= from_byte
;
2694 if (! NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
2698 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2699 c1
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2700 temp_code
= SYNTAX_WITH_MULTIBYTE_CHECK (c1
);
2701 /* Don't allow comment-end to be quoted. */
2702 if (temp_code
== Sendcomment
)
2704 quoted
= char_quoted (from
- 1, temp_pos
);
2707 DEC_BOTH (from
, from_byte
);
2708 temp_pos
= dec_bytepos (temp_pos
);
2709 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2711 c1
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2712 temp_code
= SYNTAX_WITH_MULTIBYTE_CHECK (c1
);
2713 if (! (quoted
|| temp_code
== Sword
2714 || temp_code
== Ssymbol
2715 || temp_code
== Squote
))
2717 DEC_BOTH (from
, from_byte
);
2724 temp_pos
= dec_bytepos (from_byte
);
2725 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2726 if (from
!= stop
&& c
== FETCH_CHAR_AS_MULTIBYTE (temp_pos
))
2727 DEC_BOTH (from
, from_byte
);
2736 if (!++depth
) goto done2
;
2741 if (!--depth
) goto done2
;
2742 if (depth
< min_depth
)
2743 xsignal3 (Qscan_error
,
2744 build_string ("Containing expression ends prematurely"),
2745 make_number (last_good
), make_number (from
));
2749 if (!parse_sexp_ignore_comments
)
2751 found
= back_comment (from
, from_byte
, stop
, comnested
, comstyle
,
2752 &out_charpos
, &out_bytepos
);
2753 /* FIXME: if !found, it really wasn't a comment-end.
2754 For single-char Sendcomment, we can't do much about it apart
2755 from skipping the char.
2756 For 2-char endcomments, we could try again, taking both
2757 chars as separate entities, but it's a lot of trouble
2758 for very little gain, so we don't bother either. -sm */
2760 from
= out_charpos
, from_byte
= out_bytepos
;
2763 case Scomment_fence
:
2769 DEC_BOTH (from
, from_byte
);
2770 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2771 if (!char_quoted (from
, from_byte
)
2772 && (c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2773 SYNTAX_WITH_MULTIBYTE_CHECK (c
) == code
))
2776 if (code
== Sstring_fence
&& !depth
&& sexpflag
) goto done2
;
2780 stringterm
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2785 DEC_BOTH (from
, from_byte
);
2786 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2787 if (!char_quoted (from
, from_byte
)
2789 == (c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
)))
2790 && SYNTAX_WITH_MULTIBYTE_CHECK (c
) == Sstring
)
2793 if (!depth
&& sexpflag
) goto done2
;
2796 /* Ignore whitespace, punctuation, quote, endcomment. */
2801 /* Reached start of buffer. Error if within object, return nil if between */
2814 XSETFASTINT (val
, from
);
2818 xsignal3 (Qscan_error
,
2819 build_string ("Unbalanced parentheses"),
2820 make_number (last_good
), make_number (from
));
2823 DEFUN ("scan-lists", Fscan_lists
, Sscan_lists
, 3, 3, 0,
2824 doc
: /* Scan from character number FROM by COUNT lists.
2825 Scan forward if COUNT is positive, backward if COUNT is negative.
2826 Return the character number of the position thus found.
2828 A \"list", in this context, refers to a balanced parenthetical
2829 grouping, as determined by the syntax table.
2831 If DEPTH is nonzero, treat that as the nesting depth of the starting
2832 point (i.e. the starting point is DEPTH parentheses deep). This
2833 function scans over parentheses until the depth goes to zero COUNT
2834 times. Hence, positive DEPTH moves out that number of levels of
2835 parentheses, while negative DEPTH moves to a deeper level.
2837 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2839 If we reach the beginning or end of the accessible part of the buffer
2840 before we have scanned over COUNT lists, return nil if the depth at
2841 that point is zero, and signal a error if the depth is nonzero. */)
2842 (Lisp_Object from
, Lisp_Object count
, Lisp_Object depth
)
2844 CHECK_NUMBER (from
);
2845 CHECK_NUMBER (count
);
2846 CHECK_NUMBER (depth
);
2848 return scan_lists (XINT (from
), XINT (count
), XINT (depth
), 0);
2851 DEFUN ("scan-sexps", Fscan_sexps
, Sscan_sexps
, 2, 2, 0,
2852 doc
: /* Scan from character number FROM by COUNT balanced expressions.
2853 If COUNT is negative, scan backwards.
2854 Returns the character number of the position thus found.
2856 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2858 If the beginning or end of (the accessible part of) the buffer is reached
2859 in the middle of a parenthetical grouping, an error is signaled.
2860 If the beginning or end is reached between groupings
2861 but before count is used up, nil is returned. */)
2862 (Lisp_Object from
, Lisp_Object count
)
2864 CHECK_NUMBER (from
);
2865 CHECK_NUMBER (count
);
2867 return scan_lists (XINT (from
), XINT (count
), 0, 1);
2870 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars
, Sbackward_prefix_chars
,
2872 doc
: /* Move point backward over any number of chars with prefix syntax.
2873 This includes chars with "quote" or "prefix" syntax (' or p). */)
2876 ptrdiff_t beg
= BEGV
;
2877 ptrdiff_t opoint
= PT
;
2878 ptrdiff_t opoint_byte
= PT_BYTE
;
2880 ptrdiff_t pos_byte
= PT_BYTE
;
2885 SET_PT_BOTH (opoint
, opoint_byte
);
2890 SETUP_SYNTAX_TABLE (pos
, -1);
2892 DEC_BOTH (pos
, pos_byte
);
2894 while (!char_quoted (pos
, pos_byte
)
2895 /* Previous statement updates syntax table. */
2896 && ((c
= FETCH_CHAR_AS_MULTIBYTE (pos_byte
), SYNTAX (c
) == Squote
)
2897 || SYNTAX_PREFIX (c
)))
2900 opoint_byte
= pos_byte
;
2903 DEC_BOTH (pos
, pos_byte
);
2906 SET_PT_BOTH (opoint
, opoint_byte
);
2911 /* Parse forward from FROM / FROM_BYTE to END,
2912 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
2913 and return a description of the state of the parse at END.
2914 If STOPBEFORE, stop at the start of an atom.
2915 If COMMENTSTOP is 1, stop at the start of a comment.
2916 If COMMENTSTOP is -1, stop at the start or end of a comment,
2917 after the beginning of a string, or after the end of a string. */
2920 scan_sexps_forward (struct lisp_parse_state
*stateptr
,
2921 ptrdiff_t from
, ptrdiff_t from_byte
, ptrdiff_t end
,
2922 EMACS_INT targetdepth
, bool stopbefore
,
2923 Lisp_Object oldstate
, int commentstop
)
2925 struct lisp_parse_state state
;
2926 enum syntaxcode code
;
2929 struct level
{ ptrdiff_t last
, prev
; };
2930 struct level levelstart
[100];
2931 struct level
*curlevel
= levelstart
;
2932 struct level
*endlevel
= levelstart
+ 100;
2933 EMACS_INT depth
; /* Paren depth of current scanning location.
2934 level - levelstart equals this except
2935 when the depth becomes negative. */
2936 EMACS_INT mindepth
; /* Lowest DEPTH value seen. */
2937 bool start_quoted
= 0; /* True means starting after a char quote. */
2939 ptrdiff_t prev_from
; /* Keep one character before FROM. */
2940 ptrdiff_t prev_from_byte
;
2941 int prev_from_syntax
;
2942 bool boundary_stop
= commentstop
== -1;
2945 ptrdiff_t out_bytepos
, out_charpos
;
2949 prev_from_byte
= from_byte
;
2951 DEC_BOTH (prev_from
, prev_from_byte
);
2953 /* Use this macro instead of `from++'. */
2955 do { prev_from = from; \
2956 prev_from_byte = from_byte; \
2957 temp = FETCH_CHAR_AS_MULTIBYTE (prev_from_byte); \
2958 prev_from_syntax = SYNTAX_WITH_FLAGS (temp); \
2959 INC_BOTH (from, from_byte); \
2961 UPDATE_SYNTAX_TABLE_FORWARD (from); \
2967 if (NILP (oldstate
))
2970 state
.instring
= -1;
2971 state
.incomment
= 0;
2972 state
.comstyle
= 0; /* comment style a by default. */
2973 state
.comstr_start
= -1; /* no comment/string seen. */
2977 tem
= Fcar (oldstate
);
2983 oldstate
= Fcdr (oldstate
);
2984 oldstate
= Fcdr (oldstate
);
2985 oldstate
= Fcdr (oldstate
);
2986 tem
= Fcar (oldstate
);
2987 /* Check whether we are inside string_fence-style string: */
2988 state
.instring
= (!NILP (tem
)
2989 ? (CHARACTERP (tem
) ? XFASTINT (tem
) : ST_STRING_STYLE
)
2992 oldstate
= Fcdr (oldstate
);
2993 tem
= Fcar (oldstate
);
2994 state
.incomment
= (!NILP (tem
)
2995 ? (INTEGERP (tem
) ? XINT (tem
) : -1)
2998 oldstate
= Fcdr (oldstate
);
2999 tem
= Fcar (oldstate
);
3000 start_quoted
= !NILP (tem
);
3002 /* if the eighth element of the list is nil, we are in comment
3003 style a. If it is non-nil, we are in comment style b */
3004 oldstate
= Fcdr (oldstate
);
3005 oldstate
= Fcdr (oldstate
);
3006 tem
= Fcar (oldstate
);
3007 state
.comstyle
= (NILP (tem
)
3009 : (RANGED_INTEGERP (0, tem
, ST_COMMENT_STYLE
)
3011 : ST_COMMENT_STYLE
));
3013 oldstate
= Fcdr (oldstate
);
3014 tem
= Fcar (oldstate
);
3015 state
.comstr_start
=
3016 RANGED_INTEGERP (PTRDIFF_MIN
, tem
, PTRDIFF_MAX
) ? XINT (tem
) : -1;
3017 oldstate
= Fcdr (oldstate
);
3018 tem
= Fcar (oldstate
);
3019 while (!NILP (tem
)) /* >= second enclosing sexps. */
3021 Lisp_Object temhd
= Fcar (tem
);
3022 if (RANGED_INTEGERP (PTRDIFF_MIN
, temhd
, PTRDIFF_MAX
))
3023 curlevel
->last
= XINT (temhd
);
3024 if (++curlevel
== endlevel
)
3025 curlevel
--; /* error ("Nesting too deep for parser"); */
3026 curlevel
->prev
= -1;
3027 curlevel
->last
= -1;
3034 curlevel
->prev
= -1;
3035 curlevel
->last
= -1;
3037 SETUP_SYNTAX_TABLE (prev_from
, 1);
3038 temp
= FETCH_CHAR (prev_from_byte
);
3039 prev_from_syntax
= SYNTAX_WITH_FLAGS (temp
);
3040 UPDATE_SYNTAX_TABLE_FORWARD (from
);
3042 /* Enter the loop at a place appropriate for initial state. */
3044 if (state
.incomment
)
3045 goto startincomment
;
3046 if (state
.instring
>= 0)
3048 nofence
= state
.instring
!= ST_STRING_STYLE
;
3050 goto startquotedinstring
;
3053 else if (start_quoted
)
3060 code
= prev_from_syntax
& 0xff;
3063 && SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax
)
3064 && (c1
= FETCH_CHAR (from_byte
),
3065 syntax
= SYNTAX_WITH_FLAGS (c1
),
3066 SYNTAX_FLAGS_COMSTART_SECOND (syntax
)))
3067 /* Duplicate code to avoid a complex if-expression
3068 which causes trouble for the SGI compiler. */
3070 /* Record the comment style we have entered so that only
3071 the comment-end sequence of the same style actually
3072 terminates the comment section. */
3074 = SYNTAX_FLAGS_COMMENT_STYLE (syntax
, prev_from_syntax
);
3075 comnested
= (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax
)
3076 | SYNTAX_FLAGS_COMMENT_NESTED (syntax
));
3077 state
.incomment
= comnested
? 1 : -1;
3078 state
.comstr_start
= prev_from
;
3082 else if (code
== Scomment_fence
)
3084 /* Record the comment style we have entered so that only
3085 the comment-end sequence of the same style actually
3086 terminates the comment section. */
3087 state
.comstyle
= ST_COMMENT_STYLE
;
3088 state
.incomment
= -1;
3089 state
.comstr_start
= prev_from
;
3092 else if (code
== Scomment
)
3094 state
.comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax
, 0);
3095 state
.incomment
= (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax
) ?
3097 state
.comstr_start
= prev_from
;
3100 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax
))
3106 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3107 curlevel
->last
= prev_from
;
3109 if (from
== end
) goto endquoted
;
3112 /* treat following character as a word constituent */
3115 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3116 curlevel
->last
= prev_from
;
3120 /* Some compilers can't handle this inside the switch. */
3121 int symchar
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
3122 enum syntaxcode symcharcode
= SYNTAX (symchar
);
3123 switch (symcharcode
)
3128 if (from
== end
) goto endquoted
;
3140 curlevel
->prev
= curlevel
->last
;
3143 case Scomment_fence
: /* Can't happen because it's handled above. */
3145 if (commentstop
|| boundary_stop
) goto done
;
3147 /* The (from == BEGV) test was to enter the loop in the middle so
3148 that we find a 2-char comment ender even if we start in the
3149 middle of it. We don't want to do that if we're just at the
3150 beginning of the comment (think of (*) ... (*)). */
3151 found
= forw_comment (from
, from_byte
, end
,
3152 state
.incomment
, state
.comstyle
,
3153 (from
== BEGV
|| from
< state
.comstr_start
+ 3)
3154 ? 0 : prev_from_syntax
,
3155 &out_charpos
, &out_bytepos
, &state
.incomment
);
3156 from
= out_charpos
; from_byte
= out_bytepos
;
3157 /* Beware! prev_from and friends are invalid now.
3158 Luckily, the `done' doesn't use them and the INC_FROM
3159 sets them to a sane value without looking at them. */
3160 if (!found
) goto done
;
3162 state
.incomment
= 0;
3163 state
.comstyle
= 0; /* reset the comment style */
3164 if (boundary_stop
) goto done
;
3168 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3170 /* curlevel++->last ran into compiler bug on Apollo */
3171 curlevel
->last
= prev_from
;
3172 if (++curlevel
== endlevel
)
3173 curlevel
--; /* error ("Nesting too deep for parser"); */
3174 curlevel
->prev
= -1;
3175 curlevel
->last
= -1;
3176 if (targetdepth
== depth
) goto done
;
3181 if (depth
< mindepth
)
3183 if (curlevel
!= levelstart
)
3185 curlevel
->prev
= curlevel
->last
;
3186 if (targetdepth
== depth
) goto done
;
3191 state
.comstr_start
= from
- 1;
3192 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3193 curlevel
->last
= prev_from
;
3194 state
.instring
= (code
== Sstring
3195 ? (FETCH_CHAR_AS_MULTIBYTE (prev_from_byte
))
3197 if (boundary_stop
) goto done
;
3200 nofence
= state
.instring
!= ST_STRING_STYLE
;
3205 enum syntaxcode c_code
;
3207 if (from
>= end
) goto done
;
3208 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
3209 /* Some compilers can't handle this inside the switch. */
3210 c_code
= SYNTAX (c
);
3212 /* Check C_CODE here so that if the char has
3213 a syntax-table property which says it is NOT
3214 a string character, it does not end the string. */
3215 if (nofence
&& c
== state
.instring
&& c_code
== Sstring
)
3221 if (!nofence
) goto string_end
;
3226 startquotedinstring
:
3227 if (from
>= end
) goto endquoted
;
3233 state
.instring
= -1;
3234 curlevel
->prev
= curlevel
->last
;
3236 if (boundary_stop
) goto done
;
3240 /* FIXME: We should do something with it. */
3243 /* Ignore whitespace, punctuation, quote, endcomment. */
3249 stop
: /* Here if stopping before start of sexp. */
3250 from
= prev_from
; /* We have just fetched the char that starts it; */
3251 from_byte
= prev_from_byte
;
3252 goto done
; /* but return the position before it. */
3257 state
.depth
= depth
;
3258 state
.mindepth
= mindepth
;
3259 state
.thislevelstart
= curlevel
->prev
;
3260 state
.prevlevelstart
3261 = (curlevel
== levelstart
) ? -1 : (curlevel
- 1)->last
;
3262 state
.location
= from
;
3263 state
.location_byte
= from_byte
;
3264 state
.levelstarts
= Qnil
;
3265 while (curlevel
> levelstart
)
3266 state
.levelstarts
= Fcons (make_number ((--curlevel
)->last
),
3273 DEFUN ("parse-partial-sexp", Fparse_partial_sexp
, Sparse_partial_sexp
, 2, 6, 0,
3274 doc
: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
3275 Parsing stops at TO or when certain criteria are met;
3276 point is set to where parsing stops.
3277 If fifth arg OLDSTATE is omitted or nil,
3278 parsing assumes that FROM is the beginning of a function.
3279 Value is a list of elements describing final state of parsing:
3281 1. character address of start of innermost containing list; nil if none.
3282 2. character address of start of last complete sexp terminated.
3283 3. non-nil if inside a string.
3284 (it is the character that will terminate the string,
3285 or t if the string should be terminated by a generic string delimiter.)
3286 4. nil if outside a comment, t if inside a non-nestable comment,
3287 else an integer (the current comment nesting).
3288 5. t if following a quote character.
3289 6. the minimum paren-depth encountered during this scan.
3290 7. style of comment, if any.
3291 8. character address of start of comment or string; nil if not in one.
3292 9. Intermediate data for continuation of parsing (subject to change).
3293 If third arg TARGETDEPTH is non-nil, parsing stops if the depth
3294 in parentheses becomes equal to TARGETDEPTH.
3295 Fourth arg STOPBEFORE non-nil means stop when come to
3296 any character that starts a sexp.
3297 Fifth arg OLDSTATE is a list like what this function returns.
3298 It is used to initialize the state of the parse. Elements number 1, 2, 6
3300 Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.
3301 If it is symbol `syntax-table', stop after the start of a comment or a
3302 string, or after end of a comment or a string. */)
3303 (Lisp_Object from
, Lisp_Object to
, Lisp_Object targetdepth
,
3304 Lisp_Object stopbefore
, Lisp_Object oldstate
, Lisp_Object commentstop
)
3306 struct lisp_parse_state state
;
3309 if (!NILP (targetdepth
))
3311 CHECK_NUMBER (targetdepth
);
3312 target
= XINT (targetdepth
);
3315 target
= TYPE_MINIMUM (EMACS_INT
); /* We won't reach this depth */
3317 validate_region (&from
, &to
);
3318 scan_sexps_forward (&state
, XINT (from
), CHAR_TO_BYTE (XINT (from
)),
3320 target
, !NILP (stopbefore
), oldstate
,
3322 ? 0 : (EQ (commentstop
, Qsyntax_table
) ? -1 : 1)));
3324 SET_PT_BOTH (state
.location
, state
.location_byte
);
3326 return Fcons (make_number (state
.depth
),
3327 Fcons (state
.prevlevelstart
< 0
3328 ? Qnil
: make_number (state
.prevlevelstart
),
3329 Fcons (state
.thislevelstart
< 0
3330 ? Qnil
: make_number (state
.thislevelstart
),
3331 Fcons (state
.instring
>= 0
3332 ? (state
.instring
== ST_STRING_STYLE
3333 ? Qt
: make_number (state
.instring
)) : Qnil
,
3334 Fcons (state
.incomment
< 0 ? Qt
:
3335 (state
.incomment
== 0 ? Qnil
:
3336 make_number (state
.incomment
)),
3337 Fcons (state
.quoted
? Qt
: Qnil
,
3338 Fcons (make_number (state
.mindepth
),
3339 Fcons ((state
.comstyle
3340 ? (state
.comstyle
== ST_COMMENT_STYLE
3342 : make_number (state
.comstyle
))
3344 Fcons (((state
.incomment
3345 || (state
.instring
>= 0))
3346 ? make_number (state
.comstr_start
)
3348 Fcons (state
.levelstarts
, Qnil
))))))))));
3352 init_syntax_once (void)
3357 /* This has to be done here, before we call Fmake_char_table. */
3358 DEFSYM (Qsyntax_table
, "syntax-table");
3360 /* Intern_C_String this now in case it isn't already done.
3361 Setting this variable twice is harmless.
3362 But don't staticpro it here--that is done in alloc.c. */
3363 Qchar_table_extra_slots
= intern_c_string ("char-table-extra-slots");
3365 /* Create objects which can be shared among syntax tables. */
3366 Vsyntax_code_object
= make_uninit_vector (Smax
);
3367 for (i
= 0; i
< Smax
; i
++)
3368 ASET (Vsyntax_code_object
, i
, Fcons (make_number (i
), Qnil
));
3370 /* Now we are ready to set up this property, so we can
3371 create syntax tables. */
3372 Fput (Qsyntax_table
, Qchar_table_extra_slots
, make_number (0));
3374 temp
= AREF (Vsyntax_code_object
, Swhitespace
);
3376 Vstandard_syntax_table
= Fmake_char_table (Qsyntax_table
, temp
);
3378 /* Control characters should not be whitespace. */
3379 temp
= AREF (Vsyntax_code_object
, Spunct
);
3380 for (i
= 0; i
<= ' ' - 1; i
++)
3381 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3382 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 0177, temp
);
3384 /* Except that a few really are whitespace. */
3385 temp
= AREF (Vsyntax_code_object
, Swhitespace
);
3386 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ' ', temp
);
3387 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\t', temp
);
3388 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\n', temp
);
3389 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 015, temp
);
3390 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 014, temp
);
3392 temp
= AREF (Vsyntax_code_object
, Sword
);
3393 for (i
= 'a'; i
<= 'z'; i
++)
3394 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3395 for (i
= 'A'; i
<= 'Z'; i
++)
3396 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3397 for (i
= '0'; i
<= '9'; i
++)
3398 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3400 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '$', temp
);
3401 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '%', temp
);
3403 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '(',
3404 Fcons (make_number (Sopen
), make_number (')')));
3405 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ')',
3406 Fcons (make_number (Sclose
), make_number ('(')));
3407 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '[',
3408 Fcons (make_number (Sopen
), make_number (']')));
3409 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ']',
3410 Fcons (make_number (Sclose
), make_number ('[')));
3411 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '{',
3412 Fcons (make_number (Sopen
), make_number ('}')));
3413 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '}',
3414 Fcons (make_number (Sclose
), make_number ('{')));
3415 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '"',
3416 Fcons (make_number (Sstring
), Qnil
));
3417 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\\',
3418 Fcons (make_number (Sescape
), Qnil
));
3420 temp
= AREF (Vsyntax_code_object
, Ssymbol
);
3421 for (i
= 0; i
< 10; i
++)
3423 c
= "_-+*/&|<>="[i
];
3424 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, c
, temp
);
3427 temp
= AREF (Vsyntax_code_object
, Spunct
);
3428 for (i
= 0; i
< 12; i
++)
3430 c
= ".,;:?!#@~^'`"[i
];
3431 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, c
, temp
);
3434 /* All multibyte characters have syntax `word' by default. */
3435 temp
= AREF (Vsyntax_code_object
, Sword
);
3436 char_table_set_range (Vstandard_syntax_table
, 0x80, MAX_CHAR
, temp
);
3440 syms_of_syntax (void)
3442 DEFSYM (Qsyntax_table_p
, "syntax-table-p");
3444 staticpro (&Vsyntax_code_object
);
3446 staticpro (&gl_state
.object
);
3447 staticpro (&gl_state
.global_code
);
3448 staticpro (&gl_state
.current_syntax_table
);
3449 staticpro (&gl_state
.old_prop
);
3451 /* Defined in regex.c */
3452 staticpro (&re_match_object
);
3454 DEFSYM (Qscan_error
, "scan-error");
3455 Fput (Qscan_error
, Qerror_conditions
,
3456 listn (CONSTYPE_PURE
, 2, Qscan_error
, Qerror
));
3457 Fput (Qscan_error
, Qerror_message
,
3458 build_pure_c_string ("Scan error"));
3460 DEFVAR_BOOL ("parse-sexp-ignore-comments", parse_sexp_ignore_comments
,
3461 doc
: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
3463 DEFVAR_BOOL ("parse-sexp-lookup-properties", parse_sexp_lookup_properties
,
3464 doc
: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3465 Otherwise, that text property is simply ignored.
3466 See the info node `(elisp)Syntax Properties' for a description of the
3467 `syntax-table' property. */);
3469 words_include_escapes
= 0;
3470 DEFVAR_BOOL ("words-include-escapes", words_include_escapes
,
3471 doc
: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
3473 DEFVAR_BOOL ("multibyte-syntax-as-symbol", multibyte_syntax_as_symbol
,
3474 doc
: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
3475 multibyte_syntax_as_symbol
= 0;
3477 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3478 open_paren_in_column_0_is_defun_start
,
3479 doc
: /* Non-nil means an open paren in column 0 denotes the start of a defun. */);
3480 open_paren_in_column_0_is_defun_start
= 1;
3483 DEFVAR_LISP ("find-word-boundary-function-table",
3484 Vfind_word_boundary_function_table
,
3486 Char table of functions to search for the word boundary.
3487 Each function is called with two arguments; POS and LIMIT.
3488 POS and LIMIT are character positions in the current buffer.
3490 If POS is less than LIMIT, POS is at the first character of a word,
3491 and the return value of a function is a position after the last
3492 character of that word.
3494 If POS is not less than LIMIT, POS is at the last character of a word,
3495 and the return value of a function is a position at the first
3496 character of that word.
3498 In both cases, LIMIT bounds the search. */);
3499 Vfind_word_boundary_function_table
= Fmake_char_table (Qnil
, Qnil
);
3501 defsubr (&Ssyntax_table_p
);
3502 defsubr (&Ssyntax_table
);
3503 defsubr (&Sstandard_syntax_table
);
3504 defsubr (&Scopy_syntax_table
);
3505 defsubr (&Sset_syntax_table
);
3506 defsubr (&Schar_syntax
);
3507 defsubr (&Smatching_paren
);
3508 defsubr (&Sstring_to_syntax
);
3509 defsubr (&Smodify_syntax_entry
);
3510 defsubr (&Sinternal_describe_syntax_value
);
3512 defsubr (&Sforward_word
);
3514 defsubr (&Sskip_chars_forward
);
3515 defsubr (&Sskip_chars_backward
);
3516 defsubr (&Sskip_syntax_forward
);
3517 defsubr (&Sskip_syntax_backward
);
3519 defsubr (&Sforward_comment
);
3520 defsubr (&Sscan_lists
);
3521 defsubr (&Sscan_sexps
);
3522 defsubr (&Sbackward_prefix_chars
);
3523 defsubr (&Sparse_partial_sexp
);