1 /* GNU Emacs routines to deal with syntax tables; also word and list parsing.
2 Copyright (C) 1985, 1987, 1993-1995, 1997-1999, 2001-2012
3 Free 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/>. */
24 #include <sys/types.h>
29 #include "character.h"
33 /* Make syntax table lookup grant data in gl_state. */
34 #define SYNTAX_ENTRY_VIA_PROPERTY
37 #include "intervals.h"
40 /* Then there are seven single-bit flags that have the following meanings:
41 1. This character is the first of a two-character comment-start sequence.
42 2. This character is the second of a two-character comment-start sequence.
43 3. This character is the first of a two-character comment-end sequence.
44 4. This character is the second of a two-character comment-end sequence.
45 5. This character is a prefix, for backward-prefix-chars.
46 6. The char is part of a delimiter for comments of style "b".
47 7. This character is part of a nestable comment sequence.
48 8. The char is part of a delimiter for comments of style "c".
49 Note that any two-character sequence whose first character has flag 1
50 and whose second character has flag 2 will be interpreted as a comment start.
52 bit 6 and 8 are used to discriminate between different comment styles.
53 Languages such as C++ allow two orthogonal syntax start/end pairs
54 and bit 6 is used to determine whether a comment-end or Scommentend
55 ends style a or b. Comment markers can start style a, b, c, or bc.
56 Style a is always the default.
57 For 2-char comment markers, the style b flag is only looked up on the second
58 char of the comment marker and on the first char of the comment ender.
59 For style c (like to for the nested flag), the flag can be placed on any
63 /* These macros extract specific flags from an integer
64 that holds the syntax code and the flags. */
66 #define SYNTAX_FLAGS_COMSTART_FIRST(flags) (((flags) >> 16) & 1)
68 #define SYNTAX_FLAGS_COMSTART_SECOND(flags) (((flags) >> 17) & 1)
70 #define SYNTAX_FLAGS_COMEND_FIRST(flags) (((flags) >> 18) & 1)
72 #define SYNTAX_FLAGS_COMEND_SECOND(flags) (((flags) >> 19) & 1)
74 #define SYNTAX_FLAGS_PREFIX(flags) (((flags) >> 20) & 1)
76 #define SYNTAX_FLAGS_COMMENT_STYLEB(flags) (((flags) >> 21) & 1)
77 #define SYNTAX_FLAGS_COMMENT_STYLEC(flags) (((flags) >> 22) & 2)
78 /* FLAGS should be the flags of the main char of the comment marker, e.g.
79 the second for comstart and the first for comend. */
80 #define SYNTAX_FLAGS_COMMENT_STYLE(flags, other_flags) \
81 (SYNTAX_FLAGS_COMMENT_STYLEB (flags) \
82 | SYNTAX_FLAGS_COMMENT_STYLEC (flags) \
83 | SYNTAX_FLAGS_COMMENT_STYLEC (other_flags))
85 #define SYNTAX_FLAGS_COMMENT_NESTED(flags) (((flags) >> 22) & 1)
87 /* These macros extract a particular flag for a given character. */
89 #define SYNTAX_COMEND_FIRST(c) \
90 (SYNTAX_FLAGS_COMEND_FIRST (SYNTAX_WITH_FLAGS (c)))
91 #define SYNTAX_PREFIX(c) (SYNTAX_FLAGS_PREFIX (SYNTAX_WITH_FLAGS (c)))
93 /* We use these constants in place for comment-style and
94 string-ender-char to distinguish comments/strings started by
95 comment_fence and string_fence codes. */
97 #define ST_COMMENT_STYLE (256 + 1)
98 #define ST_STRING_STYLE (256 + 2)
100 static Lisp_Object Qsyntax_table_p
;
101 static Lisp_Object Qsyntax_table
, Qscan_error
;
104 /* Used as a temporary in SYNTAX_ENTRY and other macros in syntax.h,
105 if not compiled with GCC. No need to mark it, since it is used
106 only very temporarily. */
107 Lisp_Object syntax_temp
;
110 /* This is the internal form of the parse state used in parse-partial-sexp. */
112 struct lisp_parse_state
114 EMACS_INT depth
; /* Depth at end of parsing. */
115 int instring
; /* -1 if not within string, else desired terminator. */
116 EMACS_INT incomment
; /* -1 if in unnestable comment else comment nesting */
117 int comstyle
; /* comment style a=0, or b=1, or ST_COMMENT_STYLE. */
118 int quoted
; /* Nonzero if just after an escape char at end of parsing */
119 EMACS_INT mindepth
; /* Minimum depth seen while scanning. */
120 /* Char number of most recent start-of-expression at current level */
121 ptrdiff_t thislevelstart
;
122 /* Char number of start of containing expression */
123 ptrdiff_t prevlevelstart
;
124 ptrdiff_t location
; /* Char number at which parsing stopped. */
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
Fsyntax_table_p (Lisp_Object
);
147 static Lisp_Object
skip_chars (int, Lisp_Object
, Lisp_Object
, int);
148 static Lisp_Object
skip_syntaxes (int, Lisp_Object
, Lisp_Object
);
149 static Lisp_Object
scan_lists (EMACS_INT
, EMACS_INT
, EMACS_INT
, int);
150 static void scan_sexps_forward (struct lisp_parse_state
*,
151 ptrdiff_t, ptrdiff_t, ptrdiff_t, EMACS_INT
,
152 int, Lisp_Object
, int);
153 static int in_classes (int, Lisp_Object
);
155 /* Whether the syntax of the character C has the prefix flag set. */
156 int syntax_prefix_flag_p (int c
)
158 return SYNTAX_PREFIX (c
);
161 struct gl_state_s gl_state
; /* Global state of syntax parser. */
163 #define INTERVALS_AT_ONCE 10 /* 1 + max-number of intervals
164 to scan to property-change. */
166 /* Update gl_state to an appropriate interval which contains CHARPOS. The
167 sign of COUNT give the relative position of CHARPOS wrt the previously
168 valid interval. If INIT, only [be]_property fields of gl_state are
169 valid at start, the rest is filled basing on OBJECT.
171 `gl_state.*_i' are the intervals, and CHARPOS is further in the search
172 direction than the intervals - or in an interval. We update the
173 current syntax-table basing on the property of this interval, and
174 update the interval to start further than CHARPOS - or be
175 NULL_INTERVAL. We also update lim_property to be the next value of
176 charpos to call this subroutine again - or be before/after the
177 start/end of OBJECT. */
180 update_syntax_table (ptrdiff_t charpos
, EMACS_INT count
, int init
,
183 Lisp_Object tmp_table
;
190 gl_state
.old_prop
= Qnil
;
191 gl_state
.start
= gl_state
.b_property
;
192 gl_state
.stop
= gl_state
.e_property
;
193 i
= interval_of (charpos
, object
);
194 gl_state
.backward_i
= gl_state
.forward_i
= i
;
196 if (NULL_INTERVAL_P (i
))
198 /* interval_of updates only ->position of the return value, so
199 update the parents manually to speed up update_interval. */
200 while (!NULL_PARENT (i
))
202 if (AM_RIGHT_CHILD (i
))
203 INTERVAL_PARENT (i
)->position
= i
->position
204 - LEFT_TOTAL_LENGTH (i
) + TOTAL_LENGTH (i
) /* right end */
205 - TOTAL_LENGTH (INTERVAL_PARENT (i
))
206 + LEFT_TOTAL_LENGTH (INTERVAL_PARENT (i
));
208 INTERVAL_PARENT (i
)->position
= i
->position
- LEFT_TOTAL_LENGTH (i
)
210 i
= INTERVAL_PARENT (i
);
212 i
= gl_state
.forward_i
;
213 gl_state
.b_property
= i
->position
- gl_state
.offset
;
214 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
217 i
= count
> 0 ? gl_state
.forward_i
: gl_state
.backward_i
;
219 /* We are guaranteed to be called with CHARPOS either in i,
221 if (NULL_INTERVAL_P (i
))
222 error ("Error in syntax_table logic for to-the-end intervals");
223 else if (charpos
< i
->position
) /* Move left. */
226 error ("Error in syntax_table logic for intervals <-");
227 /* Update the interval. */
228 i
= update_interval (i
, charpos
);
229 if (INTERVAL_LAST_POS (i
) != gl_state
.b_property
)
232 gl_state
.forward_i
= i
;
233 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
236 else if (charpos
>= INTERVAL_LAST_POS (i
)) /* Move right. */
239 error ("Error in syntax_table logic for intervals ->");
240 /* Update the interval. */
241 i
= update_interval (i
, charpos
);
242 if (i
->position
!= gl_state
.e_property
)
245 gl_state
.backward_i
= i
;
246 gl_state
.b_property
= i
->position
- gl_state
.offset
;
251 tmp_table
= textget (i
->plist
, Qsyntax_table
);
254 invalidate
= !EQ (tmp_table
, gl_state
.old_prop
); /* Need to invalidate? */
256 if (invalidate
) /* Did not get to adjacent interval. */
257 { /* with the same table => */
258 /* invalidate the old range. */
261 gl_state
.backward_i
= i
;
262 gl_state
.b_property
= i
->position
- gl_state
.offset
;
266 gl_state
.forward_i
= i
;
267 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
271 if (!EQ (tmp_table
, gl_state
.old_prop
))
273 gl_state
.current_syntax_table
= tmp_table
;
274 gl_state
.old_prop
= tmp_table
;
275 if (EQ (Fsyntax_table_p (tmp_table
), Qt
))
277 gl_state
.use_global
= 0;
279 else if (CONSP (tmp_table
))
281 gl_state
.use_global
= 1;
282 gl_state
.global_code
= tmp_table
;
286 gl_state
.use_global
= 0;
287 gl_state
.current_syntax_table
= BVAR (current_buffer
, syntax_table
);
291 while (!NULL_INTERVAL_P (i
))
293 if (cnt
&& !EQ (tmp_table
, textget (i
->plist
, Qsyntax_table
)))
297 gl_state
.e_property
= i
->position
- gl_state
.offset
;
298 gl_state
.forward_i
= i
;
303 = i
->position
+ LENGTH (i
) - gl_state
.offset
;
304 gl_state
.backward_i
= i
;
308 else if (cnt
== INTERVALS_AT_ONCE
)
313 = i
->position
+ LENGTH (i
) - gl_state
.offset
314 /* e_property at EOB is not set to ZV but to ZV+1, so that
315 we can do INC(from);UPDATE_SYNTAX_TABLE_FORWARD without
316 having to check eob between the two. */
317 + (NULL_INTERVAL_P (next_interval (i
)) ? 1 : 0);
318 gl_state
.forward_i
= i
;
322 gl_state
.b_property
= i
->position
- gl_state
.offset
;
323 gl_state
.backward_i
= i
;
328 i
= count
> 0 ? next_interval (i
) : previous_interval (i
);
330 eassert (NULL_INTERVAL_P (i
)); /* This property goes to the end. */
332 gl_state
.e_property
= gl_state
.stop
;
334 gl_state
.b_property
= gl_state
.start
;
337 /* Returns TRUE if char at CHARPOS is quoted.
338 Global syntax-table data should be set up already to be good at CHARPOS
339 or after. On return global syntax data is good for lookup at CHARPOS. */
342 char_quoted (ptrdiff_t charpos
, ptrdiff_t bytepos
)
344 register enum syntaxcode code
;
345 register ptrdiff_t beg
= BEGV
;
346 register int quoted
= 0;
347 ptrdiff_t orig
= charpos
;
349 while (charpos
> beg
)
352 DEC_BOTH (charpos
, bytepos
);
354 UPDATE_SYNTAX_TABLE_BACKWARD (charpos
);
355 c
= FETCH_CHAR_AS_MULTIBYTE (bytepos
);
357 if (! (code
== Scharquote
|| code
== Sescape
))
363 UPDATE_SYNTAX_TABLE (orig
);
367 /* Return the bytepos one character before BYTEPOS.
368 We assume that BYTEPOS is not at the start of the buffer. */
370 static inline ptrdiff_t
371 dec_bytepos (ptrdiff_t bytepos
)
373 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
380 /* Return a defun-start position before POS and not too far before.
381 It should be the last one before POS, or nearly the last.
383 When open_paren_in_column_0_is_defun_start is nonzero,
384 only the beginning of the buffer is treated as a defun-start.
386 We record the information about where the scan started
387 and what its result was, so that another call in the same area
388 can return the same value very quickly.
390 There is no promise at which position the global syntax data is
391 valid on return from the subroutine, so the caller should explicitly
392 update the global data. */
395 find_defun_start (ptrdiff_t pos
, ptrdiff_t pos_byte
)
397 ptrdiff_t opoint
= PT
, opoint_byte
= PT_BYTE
;
399 if (!open_paren_in_column_0_is_defun_start
)
401 find_start_value
= BEGV
;
402 find_start_value_byte
= BEGV_BYTE
;
403 find_start_buffer
= current_buffer
;
404 find_start_modiff
= MODIFF
;
405 find_start_begv
= BEGV
;
406 find_start_pos
= pos
;
410 /* Use previous finding, if it's valid and applies to this inquiry. */
411 if (current_buffer
== find_start_buffer
412 /* Reuse the defun-start even if POS is a little farther on.
413 POS might be in the next defun, but that's ok.
414 Our value may not be the best possible, but will still be usable. */
415 && pos
<= find_start_pos
+ 1000
416 && pos
>= find_start_value
417 && BEGV
== find_start_begv
418 && MODIFF
== find_start_modiff
)
419 return find_start_value
;
421 /* Back up to start of line. */
422 scan_newline (pos
, pos_byte
, BEGV
, BEGV_BYTE
, -1, 1);
424 /* We optimize syntax-table lookup for rare updates. Thus we accept
425 only those `^\s(' which are good in global _and_ text-property
427 SETUP_BUFFER_SYNTAX_TABLE ();
432 /* Open-paren at start of line means we may have found our
434 c
= FETCH_CHAR_AS_MULTIBYTE (PT_BYTE
);
435 if (SYNTAX (c
) == Sopen
)
437 SETUP_SYNTAX_TABLE (PT
+ 1, -1); /* Try again... */
438 c
= FETCH_CHAR_AS_MULTIBYTE (PT_BYTE
);
439 if (SYNTAX (c
) == Sopen
)
441 /* Now fallback to the default value. */
442 SETUP_BUFFER_SYNTAX_TABLE ();
444 /* Move to beg of previous line. */
445 scan_newline (PT
, PT_BYTE
, BEGV
, BEGV_BYTE
, -2, 1);
448 /* Record what we found, for the next try. */
449 find_start_value
= PT
;
450 find_start_value_byte
= PT_BYTE
;
451 find_start_buffer
= current_buffer
;
452 find_start_modiff
= MODIFF
;
453 find_start_begv
= BEGV
;
454 find_start_pos
= pos
;
456 TEMP_SET_PT_BOTH (opoint
, opoint_byte
);
458 return find_start_value
;
461 /* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
464 prev_char_comend_first (ptrdiff_t pos
, ptrdiff_t pos_byte
)
468 DEC_BOTH (pos
, pos_byte
);
469 UPDATE_SYNTAX_TABLE_BACKWARD (pos
);
470 c
= FETCH_CHAR (pos_byte
);
471 val
= SYNTAX_COMEND_FIRST (c
);
472 UPDATE_SYNTAX_TABLE_FORWARD (pos
+ 1);
476 /* Return the SYNTAX_COMSTART_FIRST of the character before POS, POS_BYTE. */
479 * prev_char_comstart_first (pos, pos_byte)
484 * DEC_BOTH (pos, pos_byte);
485 * UPDATE_SYNTAX_TABLE_BACKWARD (pos);
486 * c = FETCH_CHAR (pos_byte);
487 * val = SYNTAX_COMSTART_FIRST (c);
488 * UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
492 /* Checks whether charpos FROM is at the end of a comment.
493 FROM_BYTE is the bytepos corresponding to FROM.
494 Do not move back before STOP.
496 Return a positive value if we find a comment ending at FROM/FROM_BYTE;
499 If successful, store the charpos of the comment's beginning
500 into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR.
502 Global syntax data remains valid for backward search starting at
503 the returned value (or at FROM, if the search was not successful). */
506 back_comment (ptrdiff_t from
, ptrdiff_t from_byte
, ptrdiff_t stop
, int comnested
, int comstyle
, ptrdiff_t *charpos_ptr
, ptrdiff_t *bytepos_ptr
)
508 /* Look back, counting the parity of string-quotes,
509 and recording the comment-starters seen.
510 When we reach a safe place, assume that's not in a string;
511 then step the main scan to the earliest comment-starter seen
512 an even number of string quotes away from the safe place.
514 OFROM[I] is position of the earliest comment-starter seen
515 which is I+2X quotes from the comment-end.
516 PARITY is current parity of quotes from the comment end. */
517 int string_style
= -1; /* Presumed outside of any string. */
518 int string_lossage
= 0;
519 /* Not a real lossage: indicates that we have passed a matching comment
520 starter plus a non-matching comment-ender, meaning that any matching
521 comment-starter we might see later could be a false positive (hidden
522 inside another comment).
523 Test case: { a (* b } c (* d *) */
524 int comment_lossage
= 0;
525 ptrdiff_t comment_end
= from
;
526 ptrdiff_t comment_end_byte
= from_byte
;
527 ptrdiff_t comstart_pos
= 0;
528 ptrdiff_t comstart_byte
IF_LINT (= 0);
529 /* Place where the containing defun starts,
530 or 0 if we didn't come across it yet. */
531 ptrdiff_t defun_start
= 0;
532 ptrdiff_t defun_start_byte
= 0;
533 register enum syntaxcode code
;
534 int nesting
= 1; /* current comment nesting */
538 /* FIXME: A }} comment-ender style leads to incorrect behavior
539 in the case of {{ c }}} because we ignore the last two chars which are
540 assumed to be comment-enders although they aren't. */
542 /* At beginning of range to scan, we're outside of strings;
543 that determines quote parity to the comment-end. */
547 int prev_syntax
, com2start
, com2end
;
550 /* Move back and examine a character. */
551 DEC_BOTH (from
, from_byte
);
552 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
554 prev_syntax
= syntax
;
555 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
556 syntax
= SYNTAX_WITH_FLAGS (c
);
559 /* Check for 2-char comment markers. */
560 com2start
= (SYNTAX_FLAGS_COMSTART_FIRST (syntax
)
561 && SYNTAX_FLAGS_COMSTART_SECOND (prev_syntax
)
563 == SYNTAX_FLAGS_COMMENT_STYLE (prev_syntax
, syntax
))
564 && (SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax
)
565 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
)) == comnested
);
566 com2end
= (SYNTAX_FLAGS_COMEND_FIRST (syntax
)
567 && SYNTAX_FLAGS_COMEND_SECOND (prev_syntax
));
568 comstart
= (com2start
|| code
== Scomment
);
570 /* Nasty cases with overlapping 2-char comment markers:
571 - snmp-mode: -- c -- foo -- c --
579 /* If a 2-char comment sequence partly overlaps with another,
580 we don't try to be clever. E.g. |*| in C, or }% in modes that
581 have %..\n and %{..}%. */
582 if (from
> stop
&& (com2end
|| comstart
))
584 ptrdiff_t next
= from
, next_byte
= from_byte
;
585 int next_c
, next_syntax
;
586 DEC_BOTH (next
, next_byte
);
587 UPDATE_SYNTAX_TABLE_BACKWARD (next
);
588 next_c
= FETCH_CHAR_AS_MULTIBYTE (next_byte
);
589 next_syntax
= SYNTAX_WITH_FLAGS (next_c
);
590 if (((comstart
|| comnested
)
591 && SYNTAX_FLAGS_COMEND_SECOND (syntax
)
592 && SYNTAX_FLAGS_COMEND_FIRST (next_syntax
))
593 || ((com2end
|| comnested
)
594 && SYNTAX_FLAGS_COMSTART_SECOND (syntax
)
596 == SYNTAX_FLAGS_COMMENT_STYLE (syntax
, prev_syntax
))
597 && SYNTAX_FLAGS_COMSTART_FIRST (next_syntax
)))
599 /* UPDATE_SYNTAX_TABLE_FORWARD (next + 1); */
602 if (com2start
&& comstart_pos
== 0)
603 /* We're looking at a comment starter. But it might be a comment
604 ender as well (see snmp-mode). The first time we see one, we
605 need to consider it as a comment starter,
606 and the subsequent times as a comment ender. */
609 /* Turn a 2-char comment sequences into the appropriate syntax. */
614 /* Ignore comment starters of a different style. */
615 else if (code
== Scomment
616 && (comstyle
!= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0)
617 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
) != comnested
))
620 /* Ignore escaped characters, except comment-enders. */
621 if (code
!= Sendcomment
&& char_quoted (from
, from_byte
))
628 c
= (code
== Sstring_fence
? ST_STRING_STYLE
: ST_COMMENT_STYLE
);
630 /* Track parity of quotes. */
631 if (string_style
== -1)
632 /* Entering a string. */
634 else if (string_style
== c
)
635 /* Leaving the string. */
638 /* If we have two kinds of string delimiters.
639 There's no way to grok this scanning backwards. */
644 /* We've already checked that it is the relevant comstyle. */
645 if (string_style
!= -1 || comment_lossage
|| string_lossage
)
646 /* There are odd string quotes involved, so let's be careful.
647 Test case in Pascal: " { " a { " } */
652 /* Record best comment-starter so far. */
654 comstart_byte
= from_byte
;
656 else if (--nesting
<= 0)
657 /* nested comments have to be balanced, so we don't need to
658 keep looking for earlier ones. We use here the same (slightly
659 incorrect) reasoning as below: since it is followed by uniform
660 paired string quotes, this comment-start has to be outside of
661 strings, else the comment-end itself would be inside a string. */
666 if (SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == comstyle
667 && ((com2end
&& SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax
))
668 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
)) == comnested
)
669 /* This is the same style of comment ender as ours. */
674 /* Anything before that can't count because it would match
675 this comment-ender rather than ours. */
676 from
= stop
; /* Break out of the loop. */
678 else if (comstart_pos
!= 0 || c
!= '\n')
679 /* We're mixing comment styles here, so we'd better be careful.
680 The (comstart_pos != 0 || c != '\n') check is not quite correct
681 (we should just always set comment_lossage), but removing it
682 would imply that any multiline comment in C would go through
683 lossage, which seems overkill.
684 The failure should only happen in the rare cases such as
690 /* Assume a defun-start point is outside of strings. */
691 if (open_paren_in_column_0_is_defun_start
693 || (temp_byte
= dec_bytepos (from_byte
),
694 FETCH_CHAR (temp_byte
) == '\n')))
697 defun_start_byte
= from_byte
;
698 from
= stop
; /* Break out of the loop. */
707 if (comstart_pos
== 0)
710 from_byte
= comment_end_byte
;
711 UPDATE_SYNTAX_TABLE_FORWARD (comment_end
- 1);
713 /* If comstart_pos is set and we get here (ie. didn't jump to `lossage'
714 or `done'), then we've found the beginning of the non-nested comment. */
715 else if (1) /* !comnested */
718 from_byte
= comstart_byte
;
719 UPDATE_SYNTAX_TABLE_FORWARD (from
- 1);
723 struct lisp_parse_state state
;
725 /* We had two kinds of string delimiters mixed up
726 together. Decode this going forwards.
727 Scan fwd from a known safe place (beginning-of-defun)
728 to the one in question; this records where we
729 last passed a comment starter. */
730 /* If we did not already find the defun start, find it now. */
731 if (defun_start
== 0)
733 defun_start
= find_defun_start (comment_end
, comment_end_byte
);
734 defun_start_byte
= find_start_value_byte
;
738 scan_sexps_forward (&state
,
739 defun_start
, defun_start_byte
,
740 comment_end
, TYPE_MINIMUM (EMACS_INT
),
742 defun_start
= comment_end
;
743 if (state
.incomment
== (comnested
? 1 : -1)
744 && state
.comstyle
== comstyle
)
745 from
= state
.comstr_start
;
750 /* If comment_end is inside some other comment, maybe ours
751 is nested, so we need to try again from within the
752 surrounding comment. Example: { a (* " *) */
754 /* FIXME: We should advance by one or two chars. */
755 defun_start
= state
.comstr_start
+ 2;
756 defun_start_byte
= CHAR_TO_BYTE (defun_start
);
759 } while (defun_start
< comment_end
);
761 from_byte
= CHAR_TO_BYTE (from
);
762 UPDATE_SYNTAX_TABLE_FORWARD (from
- 1);
767 *bytepos_ptr
= from_byte
;
769 return (from
== comment_end
) ? -1 : from
;
772 DEFUN ("syntax-table-p", Fsyntax_table_p
, Ssyntax_table_p
, 1, 1, 0,
773 doc
: /* Return t if OBJECT is a syntax table.
774 Currently, any char-table counts as a syntax table. */)
777 if (CHAR_TABLE_P (object
)
778 && EQ (XCHAR_TABLE (object
)->purpose
, Qsyntax_table
))
784 check_syntax_table (Lisp_Object obj
)
786 CHECK_TYPE (CHAR_TABLE_P (obj
) && EQ (XCHAR_TABLE (obj
)->purpose
, Qsyntax_table
),
787 Qsyntax_table_p
, obj
);
790 DEFUN ("syntax-table", Fsyntax_table
, Ssyntax_table
, 0, 0, 0,
791 doc
: /* Return the current syntax table.
792 This is the one specified by the current buffer. */)
795 return BVAR (current_buffer
, syntax_table
);
798 DEFUN ("standard-syntax-table", Fstandard_syntax_table
,
799 Sstandard_syntax_table
, 0, 0, 0,
800 doc
: /* Return the standard syntax table.
801 This is the one used for new buffers. */)
804 return Vstandard_syntax_table
;
807 DEFUN ("copy-syntax-table", Fcopy_syntax_table
, Scopy_syntax_table
, 0, 1, 0,
808 doc
: /* Construct a new syntax table and return it.
809 It is a copy of the TABLE, which defaults to the standard syntax table. */)
815 check_syntax_table (table
);
817 table
= Vstandard_syntax_table
;
819 copy
= Fcopy_sequence (table
);
821 /* Only the standard syntax table should have a default element.
822 Other syntax tables should inherit from parents instead. */
823 XCHAR_TABLE (copy
)->defalt
= Qnil
;
825 /* Copied syntax tables should all have parents.
826 If we copied one with no parent, such as the standard syntax table,
827 use the standard syntax table as the copy's parent. */
828 if (NILP (XCHAR_TABLE (copy
)->parent
))
829 Fset_char_table_parent (copy
, Vstandard_syntax_table
);
833 DEFUN ("set-syntax-table", Fset_syntax_table
, Sset_syntax_table
, 1, 1, 0,
834 doc
: /* Select a new syntax table for the current buffer.
835 One argument, a syntax table. */)
839 check_syntax_table (table
);
840 BVAR (current_buffer
, syntax_table
) = table
;
841 /* Indicate that this buffer now has a specified syntax table. */
842 idx
= PER_BUFFER_VAR_IDX (syntax_table
);
843 SET_PER_BUFFER_VALUE_P (current_buffer
, idx
, 1);
847 /* Convert a letter which signifies a syntax code
848 into the code it signifies.
849 This is used by modify-syntax-entry, and other things. */
851 unsigned char syntax_spec_code
[0400] =
852 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
853 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
854 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
855 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
856 (char) Swhitespace
, (char) Scomment_fence
, (char) Sstring
, 0377,
857 (char) Smath
, 0377, 0377, (char) Squote
,
858 (char) Sopen
, (char) Sclose
, 0377, 0377,
859 0377, (char) Swhitespace
, (char) Spunct
, (char) Scharquote
,
860 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
861 0377, 0377, 0377, 0377,
862 (char) Scomment
, 0377, (char) Sendcomment
, 0377,
863 (char) Sinherit
, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
864 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
865 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword
,
866 0377, 0377, 0377, 0377, (char) Sescape
, 0377, 0377, (char) Ssymbol
,
867 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
868 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
869 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword
,
870 0377, 0377, 0377, 0377, (char) Sstring_fence
, 0377, 0377, 0377
873 /* Indexed by syntax code, give the letter that describes it. */
875 char syntax_code_spec
[16] =
877 ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@',
881 /* Indexed by syntax code, give the object (cons of syntax code and
882 nil) to be stored in syntax table. Since these objects can be
883 shared among syntax tables, we generate them in advance. By
884 sharing objects, the function `describe-syntax' can give a more
886 static Lisp_Object Vsyntax_code_object
;
889 DEFUN ("char-syntax", Fchar_syntax
, Schar_syntax
, 1, 1, 0,
890 doc
: /* Return the syntax code of CHARACTER, described by a character.
891 For example, if CHARACTER is a word constituent, the
892 character `w' (119) is returned.
893 The characters that correspond to various syntax codes
894 are listed in the documentation of `modify-syntax-entry'. */)
895 (Lisp_Object character
)
898 CHECK_CHARACTER (character
);
899 char_int
= XINT (character
);
900 SETUP_BUFFER_SYNTAX_TABLE ();
901 return make_number (syntax_code_spec
[(int) SYNTAX (char_int
)]);
904 DEFUN ("matching-paren", Fmatching_paren
, Smatching_paren
, 1, 1, 0,
905 doc
: /* Return the matching parenthesis of CHARACTER, or nil if none. */)
906 (Lisp_Object character
)
909 CHECK_NUMBER (character
);
910 char_int
= XINT (character
);
911 SETUP_BUFFER_SYNTAX_TABLE ();
912 code
= SYNTAX (char_int
);
913 if (code
== Sopen
|| code
== Sclose
)
914 return SYNTAX_MATCH (char_int
);
918 DEFUN ("string-to-syntax", Fstring_to_syntax
, Sstring_to_syntax
, 1, 1, 0,
919 doc
: /* Convert a syntax specification STRING into syntax cell form.
920 STRING should be a string as it is allowed as argument of
921 `modify-syntax-entry'. Value is the equivalent cons cell
922 \(CODE . MATCHING-CHAR) that can be used as value of a `syntax-table'
926 register const unsigned char *p
;
927 register enum syntaxcode code
;
931 CHECK_STRING (string
);
934 code
= (enum syntaxcode
) syntax_spec_code
[*p
++];
935 if (((int) code
& 0377) == 0377)
936 error ("Invalid syntax description letter: %c", p
[-1]);
938 if (code
== Sinherit
)
944 int character
= STRING_CHAR_AND_LENGTH (p
, len
);
945 XSETINT (match
, character
);
946 if (XFASTINT (match
) == ' ')
990 if (val
< ASIZE (Vsyntax_code_object
) && NILP (match
))
991 return XVECTOR (Vsyntax_code_object
)->contents
[val
];
993 /* Since we can't use a shared object, let's make a new one. */
994 return Fcons (make_number (val
), match
);
997 /* I really don't know why this is interactive
998 help-form should at least be made useful whilst reading the second arg. */
999 DEFUN ("modify-syntax-entry", Fmodify_syntax_entry
, Smodify_syntax_entry
, 2, 3,
1000 "cSet syntax for character: \nsSet syntax for %s to: ",
1001 doc
: /* Set syntax for character CHAR according to string NEWENTRY.
1002 The syntax is changed only for table SYNTAX-TABLE, which defaults to
1003 the current buffer's syntax table.
1004 CHAR may be a cons (MIN . MAX), in which case, syntaxes of all characters
1005 in the range MIN to MAX are changed.
1006 The first character of NEWENTRY should be one of the following:
1007 Space or - whitespace syntax. w word constituent.
1008 _ symbol constituent. . punctuation.
1009 ( open-parenthesis. ) close-parenthesis.
1010 " string quote. \\ escape.
1011 $ paired delimiter. ' expression quote or prefix operator.
1012 < comment starter. > comment ender.
1013 / character-quote. @ inherit from `standard-syntax-table'.
1014 | generic string fence. ! generic comment fence.
1016 Only single-character comment start and end sequences are represented thus.
1017 Two-character sequences are represented as described below.
1018 The second character of NEWENTRY is the matching parenthesis,
1019 used only if the first character is `(' or `)'.
1020 Any additional characters are flags.
1021 Defined flags are the characters 1, 2, 3, 4, b, p, and n.
1022 1 means CHAR is the start of a two-char comment start sequence.
1023 2 means CHAR is the second character of such a sequence.
1024 3 means CHAR is the start of a two-char comment end sequence.
1025 4 means CHAR is the second character of such a sequence.
1027 There can be several orthogonal comment sequences. This is to support
1028 language modes such as C++. By default, all comment sequences are of style
1029 a, but you can set the comment sequence style to b (on the second character
1030 of a comment-start, and the first character of a comment-end sequence) and/or
1031 c (on any of its chars) using this flag:
1032 b means CHAR is part of comment sequence b.
1033 c means CHAR is part of comment sequence c.
1034 n means CHAR is part of a nestable comment sequence.
1036 p means CHAR is a prefix character for `backward-prefix-chars';
1037 such characters are treated as whitespace when they occur
1038 between expressions.
1039 usage: (modify-syntax-entry CHAR NEWENTRY &optional SYNTAX-TABLE) */)
1040 (Lisp_Object c
, Lisp_Object newentry
, Lisp_Object syntax_table
)
1044 CHECK_CHARACTER_CAR (c
);
1045 CHECK_CHARACTER_CDR (c
);
1048 CHECK_CHARACTER (c
);
1050 if (NILP (syntax_table
))
1051 syntax_table
= BVAR (current_buffer
, syntax_table
);
1053 check_syntax_table (syntax_table
);
1055 newentry
= Fstring_to_syntax (newentry
);
1057 SET_RAW_SYNTAX_ENTRY_RANGE (syntax_table
, c
, newentry
);
1059 SET_RAW_SYNTAX_ENTRY (syntax_table
, XINT (c
), newentry
);
1061 /* We clear the regexp cache, since character classes can now have
1062 different values from those in the compiled regexps.*/
1063 clear_regexp_cache ();
1068 /* Dump syntax table to buffer in human-readable format */
1070 DEFUN ("internal-describe-syntax-value", Finternal_describe_syntax_value
,
1071 Sinternal_describe_syntax_value
, 1, 1, 0,
1072 doc
: /* Insert a description of the internal syntax description SYNTAX at point. */)
1073 (Lisp_Object syntax
)
1075 register enum syntaxcode code
;
1077 char desc
, start1
, start2
, end1
, end2
, prefix
,
1078 comstyleb
, comstylec
, comnested
;
1080 Lisp_Object first
, match_lisp
, value
= syntax
;
1084 insert_string ("default");
1088 if (CHAR_TABLE_P (value
))
1090 insert_string ("deeper char-table ...");
1096 insert_string ("invalid");
1100 first
= XCAR (value
);
1101 match_lisp
= XCDR (value
);
1103 if (!INTEGERP (first
) || !(NILP (match_lisp
) || CHARACTERP (match_lisp
)))
1105 insert_string ("invalid");
1109 syntax_code
= XINT (first
) & INT_MAX
;
1110 code
= (enum syntaxcode
) (syntax_code
& 0377);
1111 start1
= SYNTAX_FLAGS_COMSTART_FIRST (syntax_code
);
1112 start2
= SYNTAX_FLAGS_COMSTART_SECOND (syntax_code
);;
1113 end1
= SYNTAX_FLAGS_COMEND_FIRST (syntax_code
);
1114 end2
= SYNTAX_FLAGS_COMEND_SECOND (syntax_code
);
1115 prefix
= SYNTAX_FLAGS_PREFIX (syntax_code
);
1116 comstyleb
= SYNTAX_FLAGS_COMMENT_STYLEB (syntax_code
);
1117 comstylec
= SYNTAX_FLAGS_COMMENT_STYLEC (syntax_code
);
1118 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax_code
);
1120 if ((int) code
< 0 || (int) code
>= (int) Smax
)
1122 insert_string ("invalid");
1125 desc
= syntax_code_spec
[(int) code
];
1127 str
[0] = desc
, str
[1] = 0;
1130 if (NILP (match_lisp
))
1133 insert_char (XINT (match_lisp
));
1154 insert_string ("\twhich means: ");
1156 switch (SWITCH_ENUM_CAST (code
))
1159 insert_string ("whitespace"); break;
1161 insert_string ("punctuation"); break;
1163 insert_string ("word"); break;
1165 insert_string ("symbol"); break;
1167 insert_string ("open"); break;
1169 insert_string ("close"); break;
1171 insert_string ("prefix"); break;
1173 insert_string ("string"); break;
1175 insert_string ("math"); break;
1177 insert_string ("escape"); break;
1179 insert_string ("charquote"); break;
1181 insert_string ("comment"); break;
1183 insert_string ("endcomment"); break;
1185 insert_string ("inherit"); break;
1186 case Scomment_fence
:
1187 insert_string ("comment fence"); break;
1189 insert_string ("string fence"); break;
1191 insert_string ("invalid");
1195 if (!NILP (match_lisp
))
1197 insert_string (", matches ");
1198 insert_char (XINT (match_lisp
));
1202 insert_string (",\n\t is the first character of a comment-start sequence");
1204 insert_string (",\n\t is the second character of a comment-start sequence");
1207 insert_string (",\n\t is the first character of a comment-end sequence");
1209 insert_string (",\n\t is the second character of a comment-end sequence");
1211 insert_string (" (comment style b)");
1213 insert_string (" (comment style c)");
1215 insert_string (" (nestable)");
1218 insert_string (",\n\t is a prefix character for `backward-prefix-chars'");
1223 /* Return the position across COUNT words from FROM.
1224 If that many words cannot be found before the end of the buffer, return 0.
1225 COUNT negative means scan backward and stop at word beginning. */
1228 scan_words (register ptrdiff_t from
, register EMACS_INT count
)
1230 register ptrdiff_t beg
= BEGV
;
1231 register ptrdiff_t end
= ZV
;
1232 register ptrdiff_t from_byte
= CHAR_TO_BYTE (from
);
1233 register enum syntaxcode code
;
1235 Lisp_Object func
, pos
;
1240 SETUP_SYNTAX_TABLE (from
, count
);
1251 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1252 ch0
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1253 code
= SYNTAX (ch0
);
1254 INC_BOTH (from
, from_byte
);
1255 if (words_include_escapes
1256 && (code
== Sescape
|| code
== Scharquote
))
1261 /* Now CH0 is a character which begins a word and FROM is the
1262 position of the next character. */
1263 func
= CHAR_TABLE_REF (Vfind_word_boundary_function_table
, ch0
);
1264 if (! NILP (Ffboundp (func
)))
1266 pos
= call2 (func
, make_number (from
- 1), make_number (end
));
1267 if (INTEGERP (pos
) && from
< XINT (pos
) && XINT (pos
) <= ZV
)
1270 from_byte
= CHAR_TO_BYTE (from
);
1277 if (from
== end
) break;
1278 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1279 ch1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1280 code
= SYNTAX (ch1
);
1282 && (! words_include_escapes
1283 || (code
!= Sescape
&& code
!= Scharquote
)))
1284 || word_boundary_p (ch0
, ch1
))
1286 INC_BOTH (from
, from_byte
);
1301 DEC_BOTH (from
, from_byte
);
1302 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
1303 ch1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1304 code
= SYNTAX (ch1
);
1305 if (words_include_escapes
1306 && (code
== Sescape
|| code
== Scharquote
))
1311 /* Now CH1 is a character which ends a word and FROM is the
1313 func
= CHAR_TABLE_REF (Vfind_word_boundary_function_table
, ch1
);
1314 if (! NILP (Ffboundp (func
)))
1316 pos
= call2 (func
, make_number (from
), make_number (beg
));
1317 if (INTEGERP (pos
) && BEGV
<= XINT (pos
) && XINT (pos
) < from
)
1320 from_byte
= CHAR_TO_BYTE (from
);
1329 DEC_BOTH (from
, from_byte
);
1330 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
1331 ch0
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1332 code
= SYNTAX (ch0
);
1334 && (! words_include_escapes
1335 || (code
!= Sescape
&& code
!= Scharquote
)))
1336 || word_boundary_p (ch0
, ch1
))
1338 INC_BOTH (from
, from_byte
);
1352 DEFUN ("forward-word", Fforward_word
, Sforward_word
, 0, 1, "^p",
1353 doc
: /* Move point forward ARG words (backward if ARG is negative).
1355 If an edge of the buffer or a field boundary is reached, point is left there
1356 and the function returns nil. Field boundaries are not noticed if
1357 `inhibit-field-text-motion' is non-nil. */)
1361 ptrdiff_t orig_val
, val
;
1364 XSETFASTINT (arg
, 1);
1368 val
= orig_val
= scan_words (PT
, XINT (arg
));
1370 val
= XINT (arg
) > 0 ? ZV
: BEGV
;
1372 /* Avoid jumping out of an input field. */
1373 tmp
= Fconstrain_to_field (make_number (val
), make_number (PT
),
1375 val
= XFASTINT (tmp
);
1378 return val
== orig_val
? Qt
: Qnil
;
1381 DEFUN ("skip-chars-forward", Fskip_chars_forward
, Sskip_chars_forward
, 1, 2, 0,
1382 doc
: /* Move point forward, stopping before a char not in STRING, or at pos LIM.
1383 STRING is like the inside of a `[...]' in a regular expression
1384 except that `]' is never special and `\\' quotes `^', `-' or `\\'
1385 (but not at the end of a range; quoting is never needed there).
1386 Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
1387 With arg "^a-zA-Z", skips nonletters stopping before first letter.
1388 Char classes, e.g. `[:alpha:]', are supported.
1390 Returns the distance traveled, either zero or positive. */)
1391 (Lisp_Object string
, Lisp_Object lim
)
1393 return skip_chars (1, string
, lim
, 1);
1396 DEFUN ("skip-chars-backward", Fskip_chars_backward
, Sskip_chars_backward
, 1, 2, 0,
1397 doc
: /* Move point backward, stopping after a char not in STRING, or at pos LIM.
1398 See `skip-chars-forward' for details.
1399 Returns the distance traveled, either zero or negative. */)
1400 (Lisp_Object string
, Lisp_Object lim
)
1402 return skip_chars (0, string
, lim
, 1);
1405 DEFUN ("skip-syntax-forward", Fskip_syntax_forward
, Sskip_syntax_forward
, 1, 2, 0,
1406 doc
: /* Move point forward across chars in specified syntax classes.
1407 SYNTAX is a string of syntax code characters.
1408 Stop before a char whose syntax is not in SYNTAX, or at position LIM.
1409 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1410 This function returns the distance traveled, either zero or positive. */)
1411 (Lisp_Object syntax
, Lisp_Object lim
)
1413 return skip_syntaxes (1, syntax
, lim
);
1416 DEFUN ("skip-syntax-backward", Fskip_syntax_backward
, Sskip_syntax_backward
, 1, 2, 0,
1417 doc
: /* Move point backward across chars in specified syntax classes.
1418 SYNTAX is a string of syntax code characters.
1419 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.
1420 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1421 This function returns the distance traveled, either zero or negative. */)
1422 (Lisp_Object syntax
, Lisp_Object lim
)
1424 return skip_syntaxes (0, syntax
, lim
);
1428 skip_chars (int forwardp
, Lisp_Object string
, Lisp_Object lim
, int handle_iso_classes
)
1430 register unsigned int c
;
1431 unsigned char fastmap
[0400];
1432 /* Store the ranges of non-ASCII characters. */
1433 int *char_ranges
IF_LINT (= NULL
);
1434 int n_char_ranges
= 0;
1436 register ptrdiff_t i
, i_byte
;
1437 /* Set to 1 if the current buffer is multibyte and the region
1438 contains non-ASCII chars. */
1440 /* Set to 1 if STRING is multibyte and it contains non-ASCII
1442 int string_multibyte
;
1443 ptrdiff_t size_byte
;
1444 const unsigned char *str
;
1446 Lisp_Object iso_classes
;
1448 CHECK_STRING (string
);
1452 XSETINT (lim
, forwardp
? ZV
: BEGV
);
1454 CHECK_NUMBER_COERCE_MARKER (lim
);
1456 /* In any case, don't allow scan outside bounds of buffer. */
1457 if (XINT (lim
) > ZV
)
1458 XSETFASTINT (lim
, ZV
);
1459 if (XINT (lim
) < BEGV
)
1460 XSETFASTINT (lim
, BEGV
);
1462 multibyte
= (!NILP (BVAR (current_buffer
, enable_multibyte_characters
))
1463 && (XINT (lim
) - PT
!= CHAR_TO_BYTE (XINT (lim
)) - PT_BYTE
));
1464 string_multibyte
= SBYTES (string
) > SCHARS (string
);
1466 memset (fastmap
, 0, sizeof fastmap
);
1468 str
= SDATA (string
);
1469 size_byte
= SBYTES (string
);
1472 if (i_byte
< size_byte
1473 && SREF (string
, 0) == '^')
1475 negate
= 1; i_byte
++;
1478 /* Find the characters specified and set their elements of fastmap.
1479 Handle backslashes and ranges specially.
1481 If STRING contains non-ASCII characters, setup char_ranges for
1482 them and use fastmap only for their leading codes. */
1484 if (! string_multibyte
)
1486 int string_has_eight_bit
= 0;
1488 /* At first setup fastmap. */
1489 while (i_byte
< size_byte
)
1493 if (handle_iso_classes
&& c
== '['
1494 && i_byte
< size_byte
1495 && str
[i_byte
] == ':')
1497 const unsigned char *class_beg
= str
+ i_byte
+ 1;
1498 const unsigned char *class_end
= class_beg
;
1499 const unsigned char *class_limit
= str
+ size_byte
- 2;
1500 /* Leave room for the null. */
1501 unsigned char class_name
[CHAR_CLASS_MAX_LENGTH
+ 1];
1504 if (class_limit
- class_beg
> CHAR_CLASS_MAX_LENGTH
)
1505 class_limit
= class_beg
+ CHAR_CLASS_MAX_LENGTH
;
1507 while (class_end
< class_limit
1508 && *class_end
>= 'a' && *class_end
<= 'z')
1511 if (class_end
== class_beg
1512 || *class_end
!= ':' || class_end
[1] != ']')
1513 goto not_a_class_name
;
1515 memcpy (class_name
, class_beg
, class_end
- class_beg
);
1516 class_name
[class_end
- class_beg
] = 0;
1518 cc
= re_wctype (class_name
);
1520 error ("Invalid ISO C character class");
1522 iso_classes
= Fcons (make_number (cc
), iso_classes
);
1524 i_byte
= class_end
+ 2 - str
;
1531 if (i_byte
== size_byte
)
1536 /* Treat `-' as range character only if another character
1538 if (i_byte
+ 1 < size_byte
1539 && str
[i_byte
] == '-')
1543 /* Skip over the dash. */
1546 /* Get the end of the range. */
1549 && i_byte
< size_byte
)
1554 unsigned lim2
= c2
+ 1;
1557 if (! ASCII_CHAR_P (c2
))
1558 string_has_eight_bit
= 1;
1564 if (! ASCII_CHAR_P (c
))
1565 string_has_eight_bit
= 1;
1569 /* If the current range is multibyte and STRING contains
1570 eight-bit chars, arrange fastmap and setup char_ranges for
1571 the corresponding multibyte chars. */
1572 if (multibyte
&& string_has_eight_bit
)
1574 unsigned char fastmap2
[0400];
1575 int range_start_byte
, range_start_char
;
1577 memcpy (fastmap
+ 0200, fastmap2
+ 0200, 0200);
1578 memset (fastmap
+ 0200, 0, 0200);
1579 /* We are sure that this loop stops. */
1580 for (i
= 0200; ! fastmap2
[i
]; i
++);
1581 c
= BYTE8_TO_CHAR (i
);
1582 fastmap
[CHAR_LEADING_CODE (c
)] = 1;
1583 range_start_byte
= i
;
1584 range_start_char
= c
;
1585 char_ranges
= (int *) alloca (sizeof (int) * 128 * 2);
1586 for (i
= 129; i
< 0400; i
++)
1588 c
= BYTE8_TO_CHAR (i
);
1589 fastmap
[CHAR_LEADING_CODE (c
)] = 1;
1590 if (i
- range_start_byte
!= c
- range_start_char
)
1592 char_ranges
[n_char_ranges
++] = range_start_char
;
1593 char_ranges
[n_char_ranges
++] = ((i
- 1 - range_start_byte
)
1594 + range_start_char
);
1595 range_start_byte
= i
;
1596 range_start_char
= c
;
1599 char_ranges
[n_char_ranges
++] = range_start_char
;
1600 char_ranges
[n_char_ranges
++] = ((i
- 1 - range_start_byte
)
1601 + range_start_char
);
1604 else /* STRING is multibyte */
1606 char_ranges
= (int *) alloca (sizeof (int) * SCHARS (string
) * 2);
1608 while (i_byte
< size_byte
)
1610 unsigned char leading_code
;
1612 leading_code
= str
[i_byte
];
1613 c
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1616 if (handle_iso_classes
&& c
== '['
1617 && i_byte
< size_byte
1618 && STRING_CHAR (str
+ i_byte
) == ':')
1620 const unsigned char *class_beg
= str
+ i_byte
+ 1;
1621 const unsigned char *class_end
= class_beg
;
1622 const unsigned char *class_limit
= str
+ size_byte
- 2;
1623 /* Leave room for the null. */
1624 unsigned char class_name
[CHAR_CLASS_MAX_LENGTH
+ 1];
1627 if (class_limit
- class_beg
> CHAR_CLASS_MAX_LENGTH
)
1628 class_limit
= class_beg
+ CHAR_CLASS_MAX_LENGTH
;
1630 while (class_end
< class_limit
1631 && *class_end
>= 'a' && *class_end
<= 'z')
1634 if (class_end
== class_beg
1635 || *class_end
!= ':' || class_end
[1] != ']')
1636 goto not_a_class_name_multibyte
;
1638 memcpy (class_name
, class_beg
, class_end
- class_beg
);
1639 class_name
[class_end
- class_beg
] = 0;
1641 cc
= re_wctype (class_name
);
1643 error ("Invalid ISO C character class");
1645 iso_classes
= Fcons (make_number (cc
), iso_classes
);
1647 i_byte
= class_end
+ 2 - str
;
1651 not_a_class_name_multibyte
:
1654 if (i_byte
== size_byte
)
1657 leading_code
= str
[i_byte
];
1658 c
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1661 /* Treat `-' as range character only if another character
1663 if (i_byte
+ 1 < size_byte
1664 && str
[i_byte
] == '-')
1667 unsigned char leading_code2
;
1669 /* Skip over the dash. */
1672 /* Get the end of the range. */
1673 leading_code2
= str
[i_byte
];
1674 c2
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1678 && i_byte
< size_byte
)
1680 leading_code2
= str
[i_byte
];
1681 c2
=STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1687 if (ASCII_CHAR_P (c
))
1689 while (c
<= c2
&& c
< 0x80)
1691 leading_code
= CHAR_LEADING_CODE (c
);
1693 if (! ASCII_CHAR_P (c
))
1695 unsigned lim2
= leading_code2
+ 1;
1696 while (leading_code
< lim2
)
1697 fastmap
[leading_code
++] = 1;
1700 char_ranges
[n_char_ranges
++] = c
;
1701 char_ranges
[n_char_ranges
++] = c2
;
1707 if (ASCII_CHAR_P (c
))
1711 fastmap
[leading_code
] = 1;
1712 char_ranges
[n_char_ranges
++] = c
;
1713 char_ranges
[n_char_ranges
++] = c
;
1718 /* If the current range is unibyte and STRING contains non-ASCII
1719 chars, arrange fastmap for the corresponding unibyte
1722 if (! multibyte
&& n_char_ranges
> 0)
1724 memset (fastmap
+ 0200, 0, 0200);
1725 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1727 int c1
= char_ranges
[i
];
1728 unsigned lim2
= char_ranges
[i
+ 1] + 1;
1730 for (; c1
< lim2
; c1
++)
1732 int b
= CHAR_TO_BYTE_SAFE (c1
);
1740 /* If ^ was the first character, complement the fastmap. */
1744 for (i
= 0; i
< sizeof fastmap
; i
++)
1748 for (i
= 0; i
< 0200; i
++)
1750 /* All non-ASCII chars possibly match. */
1751 for (; i
< sizeof fastmap
; i
++)
1757 ptrdiff_t start_point
= PT
;
1759 ptrdiff_t pos_byte
= PT_BYTE
;
1760 unsigned char *p
= PT_ADDR
, *endp
, *stop
;
1764 endp
= (XINT (lim
) == GPT
) ? GPT_ADDR
: CHAR_POS_ADDR (XINT (lim
));
1765 stop
= (pos
< GPT
&& GPT
< XINT (lim
)) ? GPT_ADDR
: endp
;
1769 endp
= CHAR_POS_ADDR (XINT (lim
));
1770 stop
= (pos
>= GPT
&& GPT
> XINT (lim
)) ? GAP_END_ADDR
: endp
;
1774 /* This code may look up syntax tables using macros that rely on the
1775 gl_state object. To make sure this object is not out of date,
1776 let's initialize it manually.
1777 We ignore syntax-table text-properties for now, since that's
1778 what we've done in the past. */
1779 SETUP_BUFFER_SYNTAX_TABLE ();
1794 c
= STRING_CHAR_AND_LENGTH (p
, nbytes
);
1795 if (! NILP (iso_classes
) && in_classes (c
, iso_classes
))
1805 if (! ASCII_CHAR_P (c
))
1807 /* As we are looking at a multibyte character, we
1808 must look up the character in the table
1809 CHAR_RANGES. If there's no data in the table,
1810 that character is not what we want to skip. */
1812 /* The following code do the right thing even if
1813 n_char_ranges is zero (i.e. no data in
1815 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1816 if (c
>= char_ranges
[i
] && c
<= char_ranges
[i
+ 1])
1818 if (!(negate
^ (i
< n_char_ranges
)))
1822 p
+= nbytes
, pos
++, pos_byte
+= nbytes
;
1835 if (!NILP (iso_classes
) && in_classes (*p
, iso_classes
))
1840 goto fwd_unibyte_ok
;
1846 p
++, pos
++, pos_byte
++;
1854 unsigned char *prev_p
;
1864 while (--p
>= stop
&& ! CHAR_HEAD_P (*p
));
1865 c
= STRING_CHAR (p
);
1867 if (! NILP (iso_classes
) && in_classes (c
, iso_classes
))
1877 if (! ASCII_CHAR_P (c
))
1879 /* See the comment in the previous similar code. */
1880 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1881 if (c
>= char_ranges
[i
] && c
<= char_ranges
[i
+ 1])
1883 if (!(negate
^ (i
< n_char_ranges
)))
1887 pos
--, pos_byte
-= prev_p
- p
;
1900 if (! NILP (iso_classes
) && in_classes (p
[-1], iso_classes
))
1905 goto back_unibyte_ok
;
1908 if (!fastmap
[p
[-1]])
1911 p
--, pos
--, pos_byte
--;
1915 SET_PT_BOTH (pos
, pos_byte
);
1918 return make_number (PT
- start_point
);
1924 skip_syntaxes (int forwardp
, Lisp_Object string
, Lisp_Object lim
)
1926 register unsigned int c
;
1927 unsigned char fastmap
[0400];
1929 register ptrdiff_t i
, i_byte
;
1931 ptrdiff_t size_byte
;
1934 CHECK_STRING (string
);
1937 XSETINT (lim
, forwardp
? ZV
: BEGV
);
1939 CHECK_NUMBER_COERCE_MARKER (lim
);
1941 /* In any case, don't allow scan outside bounds of buffer. */
1942 if (XINT (lim
) > ZV
)
1943 XSETFASTINT (lim
, ZV
);
1944 if (XINT (lim
) < BEGV
)
1945 XSETFASTINT (lim
, BEGV
);
1947 if (forwardp
? (PT
>= XFASTINT (lim
)) : (PT
<= XFASTINT (lim
)))
1948 return make_number (0);
1950 multibyte
= (!NILP (BVAR (current_buffer
, enable_multibyte_characters
))
1951 && (XINT (lim
) - PT
!= CHAR_TO_BYTE (XINT (lim
)) - PT_BYTE
));
1953 memset (fastmap
, 0, sizeof fastmap
);
1955 if (SBYTES (string
) > SCHARS (string
))
1956 /* As this is very rare case (syntax spec is ASCII only), don't
1957 consider efficiency. */
1958 string
= string_make_unibyte (string
);
1960 str
= SDATA (string
);
1961 size_byte
= SBYTES (string
);
1964 if (i_byte
< size_byte
1965 && SREF (string
, 0) == '^')
1967 negate
= 1; i_byte
++;
1970 /* Find the syntaxes specified and set their elements of fastmap. */
1972 while (i_byte
< size_byte
)
1975 fastmap
[syntax_spec_code
[c
]] = 1;
1978 /* If ^ was the first character, complement the fastmap. */
1980 for (i
= 0; i
< sizeof fastmap
; i
++)
1984 ptrdiff_t start_point
= PT
;
1986 ptrdiff_t pos_byte
= PT_BYTE
;
1987 unsigned char *p
= PT_ADDR
, *endp
, *stop
;
1991 endp
= (XINT (lim
) == GPT
) ? GPT_ADDR
: CHAR_POS_ADDR (XINT (lim
));
1992 stop
= (pos
< GPT
&& GPT
< XINT (lim
)) ? GPT_ADDR
: endp
;
1996 endp
= CHAR_POS_ADDR (XINT (lim
));
1997 stop
= (pos
>= GPT
&& GPT
> XINT (lim
)) ? GAP_END_ADDR
: endp
;
2001 SETUP_SYNTAX_TABLE (pos
, forwardp
? 1 : -1);
2017 c
= STRING_CHAR_AND_LENGTH (p
, nbytes
);
2018 if (! fastmap
[(int) SYNTAX (c
)])
2020 p
+= nbytes
, pos
++, pos_byte
+= nbytes
;
2021 UPDATE_SYNTAX_TABLE_FORWARD (pos
);
2035 if (! fastmap
[(int) SYNTAX (*p
)])
2037 p
++, pos
++, pos_byte
++;
2038 UPDATE_SYNTAX_TABLE_FORWARD (pos
);
2048 unsigned char *prev_p
;
2057 UPDATE_SYNTAX_TABLE_BACKWARD (pos
- 1);
2059 while (--p
>= stop
&& ! CHAR_HEAD_P (*p
));
2060 c
= STRING_CHAR (p
);
2061 if (! fastmap
[(int) SYNTAX (c
)])
2063 pos
--, pos_byte
-= prev_p
- p
;
2077 UPDATE_SYNTAX_TABLE_BACKWARD (pos
- 1);
2078 if (! fastmap
[(int) SYNTAX (p
[-1])])
2080 p
--, pos
--, pos_byte
--;
2085 SET_PT_BOTH (pos
, pos_byte
);
2088 return make_number (PT
- start_point
);
2092 /* Return 1 if character C belongs to one of the ISO classes
2093 in the list ISO_CLASSES. Each class is represented by an
2094 integer which is its type according to re_wctype. */
2097 in_classes (int c
, Lisp_Object iso_classes
)
2101 while (CONSP (iso_classes
))
2104 elt
= XCAR (iso_classes
);
2105 iso_classes
= XCDR (iso_classes
);
2107 if (re_iswctype (c
, XFASTINT (elt
)))
2114 /* Jump over a comment, assuming we are at the beginning of one.
2115 FROM is the current position.
2116 FROM_BYTE is the bytepos corresponding to FROM.
2117 Do not move past STOP (a charpos).
2118 The comment over which we have to jump is of style STYLE
2119 (either SYNTAX_FLAGS_COMMENT_STYLE(foo) or ST_COMMENT_STYLE).
2120 NESTING should be positive to indicate the nesting at the beginning
2121 for nested comments and should be zero or negative else.
2122 ST_COMMENT_STYLE cannot be nested.
2123 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
2124 (or 0 If the search cannot start in the middle of a two-character).
2126 If successful, return 1 and store the charpos of the comment's end
2127 into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR.
2128 Else, return 0 and store the charpos STOP into *CHARPOS_PTR, the
2129 corresponding bytepos into *BYTEPOS_PTR and the current nesting
2130 (as defined for state.incomment) in *INCOMMENT_PTR.
2132 The comment end is the last character of the comment rather than the
2133 character just after the comment.
2135 Global syntax data is assumed to initially be valid for FROM and
2136 remains valid for forward search starting at the returned position. */
2139 forw_comment (ptrdiff_t from
, ptrdiff_t from_byte
, ptrdiff_t stop
,
2140 EMACS_INT nesting
, int style
, int prev_syntax
,
2141 ptrdiff_t *charpos_ptr
, ptrdiff_t *bytepos_ptr
,
2142 EMACS_INT
*incomment_ptr
)
2145 register enum syntaxcode code
;
2146 register int syntax
, other_syntax
;
2148 if (nesting
<= 0) nesting
= -1;
2150 /* Enter the loop in the middle so that we find
2151 a 2-char comment ender if we start in the middle of it. */
2152 syntax
= prev_syntax
;
2153 if (syntax
!= 0) goto forw_incomment
;
2159 *incomment_ptr
= nesting
;
2160 *charpos_ptr
= from
;
2161 *bytepos_ptr
= from_byte
;
2164 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2165 syntax
= SYNTAX_WITH_FLAGS (c
);
2166 code
= syntax
& 0xff;
2167 if (code
== Sendcomment
2168 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == style
2169 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ?
2170 (nesting
> 0 && --nesting
== 0) : nesting
< 0))
2171 /* we have encountered a comment end of the same style
2172 as the comment sequence which began this comment
2175 if (code
== Scomment_fence
2176 && style
== ST_COMMENT_STYLE
)
2177 /* we have encountered a comment end of the same style
2178 as the comment sequence which began this comment
2183 && SYNTAX_FLAGS_COMMENT_NESTED (syntax
)
2184 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == style
)
2185 /* we have encountered a nested comment of the same style
2186 as the comment sequence which began this comment section */
2188 INC_BOTH (from
, from_byte
);
2189 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2192 if (from
< stop
&& SYNTAX_FLAGS_COMEND_FIRST (syntax
)
2193 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2194 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2195 SYNTAX_FLAGS_COMEND_SECOND (other_syntax
))
2196 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, other_syntax
) == style
2197 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ||
2198 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
))
2199 ? nesting
> 0 : nesting
< 0))
2202 /* we have encountered a comment end of the same style
2203 as the comment sequence which began this comment
2208 INC_BOTH (from
, from_byte
);
2209 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2214 && SYNTAX_FLAGS_COMSTART_FIRST (syntax
)
2215 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2216 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2217 SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
) == style
2218 && SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
))
2219 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ||
2220 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
)))
2221 /* we have encountered a nested comment of the same style
2222 as the comment sequence which began this comment
2225 INC_BOTH (from
, from_byte
);
2226 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2230 *charpos_ptr
= from
;
2231 *bytepos_ptr
= from_byte
;
2235 DEFUN ("forward-comment", Fforward_comment
, Sforward_comment
, 1, 1, 0,
2237 Move forward across up to COUNT comments. If COUNT is negative, move backward.
2238 Stop scanning if we find something other than a comment or whitespace.
2239 Set point to where scanning stops.
2240 If COUNT comments are found as expected, with nothing except whitespace
2241 between them, return t; otherwise return nil. */)
2244 register ptrdiff_t from
;
2245 ptrdiff_t from_byte
;
2246 register ptrdiff_t stop
;
2248 register enum syntaxcode code
;
2249 int comstyle
= 0; /* style of comment encountered */
2250 int comnested
= 0; /* whether the comment is nestable or not */
2253 ptrdiff_t out_charpos
, out_bytepos
;
2256 CHECK_NUMBER (count
);
2257 count1
= XINT (count
);
2258 stop
= count1
> 0 ? ZV
: BEGV
;
2264 from_byte
= PT_BYTE
;
2266 SETUP_SYNTAX_TABLE (from
, count1
);
2271 int comstart_first
, syntax
, other_syntax
;
2275 SET_PT_BOTH (from
, from_byte
);
2279 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2280 syntax
= SYNTAX_WITH_FLAGS (c
);
2282 comstart_first
= SYNTAX_FLAGS_COMSTART_FIRST (syntax
);
2283 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2284 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2285 INC_BOTH (from
, from_byte
);
2286 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2287 if (from
< stop
&& comstart_first
2288 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2289 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2290 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
)))
2292 /* We have encountered a comment start sequence and we
2293 are ignoring all text inside comments. We must record
2294 the comment style this sequence begins so that later,
2295 only a comment end of the same style actually ends
2296 the comment section. */
2298 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2300 = comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2301 INC_BOTH (from
, from_byte
);
2302 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2305 while (code
== Swhitespace
|| (code
== Sendcomment
&& c
== '\n'));
2307 if (code
== Scomment_fence
)
2308 comstyle
= ST_COMMENT_STYLE
;
2309 else if (code
!= Scomment
)
2312 DEC_BOTH (from
, from_byte
);
2313 SET_PT_BOTH (from
, from_byte
);
2316 /* We're at the start of a comment. */
2317 found
= forw_comment (from
, from_byte
, stop
, comnested
, comstyle
, 0,
2318 &out_charpos
, &out_bytepos
, &dummy
);
2319 from
= out_charpos
; from_byte
= out_bytepos
;
2323 SET_PT_BOTH (from
, from_byte
);
2326 INC_BOTH (from
, from_byte
);
2327 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2328 /* We have skipped one comment. */
2340 SET_PT_BOTH (BEGV
, BEGV_BYTE
);
2345 DEC_BOTH (from
, from_byte
);
2346 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
2347 quoted
= char_quoted (from
, from_byte
);
2348 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2349 syntax
= SYNTAX_WITH_FLAGS (c
);
2352 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2353 if (code
== Sendcomment
)
2354 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2355 if (from
> stop
&& SYNTAX_FLAGS_COMEND_SECOND (syntax
)
2356 && prev_char_comend_first (from
, from_byte
)
2357 && !char_quoted (from
- 1, dec_bytepos (from_byte
)))
2360 /* We must record the comment style encountered so that
2361 later, we can match only the proper comment begin
2362 sequence of the same style. */
2363 DEC_BOTH (from
, from_byte
);
2365 /* Calling char_quoted, above, set up global syntax position
2366 at the new value of FROM. */
2367 c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2368 other_syntax
= SYNTAX_WITH_FLAGS (c1
);
2369 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2371 = comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2374 if (code
== Scomment_fence
)
2376 /* Skip until first preceding unquoted comment_fence. */
2377 int fence_found
= 0;
2378 ptrdiff_t ini
= from
, ini_byte
= from_byte
;
2382 DEC_BOTH (from
, from_byte
);
2383 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2384 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2385 if (SYNTAX (c
) == Scomment_fence
2386 && !char_quoted (from
, from_byte
))
2391 else if (from
== stop
)
2394 if (fence_found
== 0)
2396 from
= ini
; /* Set point to ini + 1. */
2397 from_byte
= ini_byte
;
2401 /* We have skipped one comment. */
2404 else if (code
== Sendcomment
)
2406 found
= back_comment (from
, from_byte
, stop
, comnested
, comstyle
,
2407 &out_charpos
, &out_bytepos
);
2411 /* This end-of-line is not an end-of-comment.
2412 Treat it like a whitespace.
2413 CC-mode (and maybe others) relies on this behavior. */
2417 /* Failure: we should go back to the end of this
2418 not-quite-endcomment. */
2419 if (SYNTAX (c
) != code
)
2420 /* It was a two-char Sendcomment. */
2421 INC_BOTH (from
, from_byte
);
2427 /* We have skipped one comment. */
2428 from
= out_charpos
, from_byte
= out_bytepos
;
2432 else if (code
!= Swhitespace
|| quoted
)
2436 INC_BOTH (from
, from_byte
);
2437 SET_PT_BOTH (from
, from_byte
);
2445 SET_PT_BOTH (from
, from_byte
);
2450 /* Return syntax code of character C if C is an ASCII character
2451 or `multibyte_symbol_p' is zero. Otherwise, return Ssymbol. */
2453 #define SYNTAX_WITH_MULTIBYTE_CHECK(c) \
2454 ((ASCII_CHAR_P (c) || !multibyte_symbol_p) \
2455 ? SYNTAX (c) : Ssymbol)
2458 scan_lists (register EMACS_INT from
, EMACS_INT count
, EMACS_INT depth
, int sexpflag
)
2461 register ptrdiff_t stop
= count
> 0 ? ZV
: BEGV
;
2466 register enum syntaxcode code
, temp_code
;
2467 EMACS_INT min_depth
= depth
; /* Err out if depth gets less than this. */
2468 int comstyle
= 0; /* style of comment encountered */
2469 int comnested
= 0; /* whether the comment is nestable or not */
2471 EMACS_INT last_good
= from
;
2473 ptrdiff_t from_byte
;
2474 ptrdiff_t out_bytepos
, out_charpos
;
2477 int multibyte_symbol_p
= sexpflag
&& multibyte_syntax_as_symbol
;
2479 if (depth
> 0) min_depth
= 0;
2481 if (from
> ZV
) from
= ZV
;
2482 if (from
< BEGV
) from
= BEGV
;
2484 from_byte
= CHAR_TO_BYTE (from
);
2489 SETUP_SYNTAX_TABLE (from
, count
);
2494 int comstart_first
, prefix
, syntax
, other_syntax
;
2495 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2496 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2497 syntax
= SYNTAX_WITH_FLAGS (c
);
2498 code
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2499 comstart_first
= SYNTAX_FLAGS_COMSTART_FIRST (syntax
);
2500 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2501 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2502 prefix
= SYNTAX_FLAGS_PREFIX (syntax
);
2503 if (depth
== min_depth
)
2505 INC_BOTH (from
, from_byte
);
2506 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2507 if (from
< stop
&& comstart_first
2508 && (c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2509 other_syntax
= SYNTAX_WITH_FLAGS (c
),
2510 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
))
2511 && parse_sexp_ignore_comments
)
2513 /* we have encountered a comment start sequence and we
2514 are ignoring all text inside comments. We must record
2515 the comment style this sequence begins so that later,
2516 only a comment end of the same style actually ends
2517 the comment section */
2519 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2521 = comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2522 INC_BOTH (from
, from_byte
);
2523 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2529 switch (SWITCH_ENUM_CAST (code
))
2535 INC_BOTH (from
, from_byte
);
2536 /* treat following character as a word constituent */
2539 if (depth
|| !sexpflag
) break;
2540 /* This word counts as a sexp; return at end of it. */
2543 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2545 /* Some compilers can't handle this inside the switch. */
2546 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2547 temp
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2552 INC_BOTH (from
, from_byte
);
2563 INC_BOTH (from
, from_byte
);
2567 case Scomment_fence
:
2568 comstyle
= ST_COMMENT_STYLE
;
2571 if (!parse_sexp_ignore_comments
) break;
2572 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2573 found
= forw_comment (from
, from_byte
, stop
,
2574 comnested
, comstyle
, 0,
2575 &out_charpos
, &out_bytepos
, &dummy
);
2576 from
= out_charpos
, from_byte
= out_bytepos
;
2583 INC_BOTH (from
, from_byte
);
2584 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2590 if (from
!= stop
&& c
== FETCH_CHAR_AS_MULTIBYTE (from_byte
))
2592 INC_BOTH (from
, from_byte
);
2602 if (!++depth
) goto done
;
2607 if (!--depth
) goto done
;
2608 if (depth
< min_depth
)
2609 xsignal3 (Qscan_error
,
2610 build_string ("Containing expression ends prematurely"),
2611 make_number (last_good
), make_number (from
));
2616 temp_pos
= dec_bytepos (from_byte
);
2617 stringterm
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2622 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2623 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2626 && SYNTAX_WITH_MULTIBYTE_CHECK (c
) == Sstring
)
2627 : SYNTAX_WITH_MULTIBYTE_CHECK (c
) == Sstring_fence
)
2630 /* Some compilers can't handle this inside the switch. */
2631 temp
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2636 INC_BOTH (from
, from_byte
);
2638 INC_BOTH (from
, from_byte
);
2640 INC_BOTH (from
, from_byte
);
2641 if (!depth
&& sexpflag
) goto done
;
2644 /* Ignore whitespace, punctuation, quote, endcomment. */
2649 /* Reached end of buffer. Error if within object, return nil if between */
2656 /* End of object reached */
2666 DEC_BOTH (from
, from_byte
);
2667 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2668 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2669 syntax
= SYNTAX_WITH_FLAGS (c
);
2670 code
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2671 if (depth
== min_depth
)
2674 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2675 if (code
== Sendcomment
)
2676 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2677 if (from
> stop
&& SYNTAX_FLAGS_COMEND_SECOND (syntax
)
2678 && prev_char_comend_first (from
, from_byte
)
2679 && parse_sexp_ignore_comments
)
2681 /* We must record the comment style encountered so that
2682 later, we can match only the proper comment begin
2683 sequence of the same style. */
2684 int c2
, other_syntax
;
2685 DEC_BOTH (from
, from_byte
);
2686 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2688 c2
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2689 other_syntax
= SYNTAX_WITH_FLAGS (c2
);
2690 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2692 = comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2695 /* Quoting turns anything except a comment-ender
2696 into a word character. Note that this cannot be true
2697 if we decremented FROM in the if-statement above. */
2698 if (code
!= Sendcomment
&& char_quoted (from
, from_byte
))
2700 DEC_BOTH (from
, from_byte
);
2703 else if (SYNTAX_FLAGS_PREFIX (syntax
))
2706 switch (SWITCH_ENUM_CAST (code
))
2712 if (depth
|| !sexpflag
) break;
2713 /* This word counts as a sexp; count object finished
2714 after passing it. */
2717 temp_pos
= from_byte
;
2718 if (! NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
2722 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2723 c1
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2724 temp_code
= SYNTAX_WITH_MULTIBYTE_CHECK (c1
);
2725 /* Don't allow comment-end to be quoted. */
2726 if (temp_code
== Sendcomment
)
2728 quoted
= char_quoted (from
- 1, temp_pos
);
2731 DEC_BOTH (from
, from_byte
);
2732 temp_pos
= dec_bytepos (temp_pos
);
2733 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2735 c1
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2736 temp_code
= SYNTAX_WITH_MULTIBYTE_CHECK (c1
);
2737 if (! (quoted
|| temp_code
== Sword
2738 || temp_code
== Ssymbol
2739 || temp_code
== Squote
))
2741 DEC_BOTH (from
, from_byte
);
2748 temp_pos
= dec_bytepos (from_byte
);
2749 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2750 if (from
!= stop
&& c
== FETCH_CHAR_AS_MULTIBYTE (temp_pos
))
2751 DEC_BOTH (from
, from_byte
);
2760 if (!++depth
) goto done2
;
2765 if (!--depth
) goto done2
;
2766 if (depth
< min_depth
)
2767 xsignal3 (Qscan_error
,
2768 build_string ("Containing expression ends prematurely"),
2769 make_number (last_good
), make_number (from
));
2773 if (!parse_sexp_ignore_comments
)
2775 found
= back_comment (from
, from_byte
, stop
, comnested
, comstyle
,
2776 &out_charpos
, &out_bytepos
);
2777 /* FIXME: if found == -1, then it really wasn't a comment-end.
2778 For single-char Sendcomment, we can't do much about it apart
2779 from skipping the char.
2780 For 2-char endcomments, we could try again, taking both
2781 chars as separate entities, but it's a lot of trouble
2782 for very little gain, so we don't bother either. -sm */
2784 from
= out_charpos
, from_byte
= out_bytepos
;
2787 case Scomment_fence
:
2793 DEC_BOTH (from
, from_byte
);
2794 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2795 if (!char_quoted (from
, from_byte
)
2796 && (c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2797 SYNTAX_WITH_MULTIBYTE_CHECK (c
) == code
))
2800 if (code
== Sstring_fence
&& !depth
&& sexpflag
) goto done2
;
2804 stringterm
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2809 DEC_BOTH (from
, from_byte
);
2810 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2811 if (!char_quoted (from
, from_byte
)
2813 == (c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
)))
2814 && SYNTAX_WITH_MULTIBYTE_CHECK (c
) == Sstring
)
2817 if (!depth
&& sexpflag
) goto done2
;
2820 /* Ignore whitespace, punctuation, quote, endcomment. */
2825 /* Reached start of buffer. Error if within object, return nil if between */
2838 XSETFASTINT (val
, from
);
2842 xsignal3 (Qscan_error
,
2843 build_string ("Unbalanced parentheses"),
2844 make_number (last_good
), make_number (from
));
2847 DEFUN ("scan-lists", Fscan_lists
, Sscan_lists
, 3, 3, 0,
2848 doc
: /* Scan from character number FROM by COUNT lists.
2849 Scan forward if COUNT is positive, backward if COUNT is negative.
2850 Return the character number of the position thus found.
2852 A \"list", in this context, refers to a balanced parenthetical
2853 grouping, as determined by the syntax table.
2855 If DEPTH is nonzero, treat that as the nesting depth of the starting
2856 point (i.e. the starting point is DEPTH parentheses deep). This
2857 function scans over parentheses until the depth goes to zero COUNT
2858 times. Hence, positive DEPTH moves out that number of levels of
2859 parentheses, while negative DEPTH moves to a deeper level.
2861 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2863 If we reach the beginning or end of the accessible part of the buffer
2864 before we have scanned over COUNT lists, return nil if the depth at
2865 that point is zero, and signal a error if the depth is nonzero. */)
2866 (Lisp_Object from
, Lisp_Object count
, Lisp_Object depth
)
2868 CHECK_NUMBER (from
);
2869 CHECK_NUMBER (count
);
2870 CHECK_NUMBER (depth
);
2872 return scan_lists (XINT (from
), XINT (count
), XINT (depth
), 0);
2875 DEFUN ("scan-sexps", Fscan_sexps
, Sscan_sexps
, 2, 2, 0,
2876 doc
: /* Scan from character number FROM by COUNT balanced expressions.
2877 If COUNT is negative, scan backwards.
2878 Returns the character number of the position thus found.
2880 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2882 If the beginning or end of (the accessible part of) the buffer is reached
2883 in the middle of a parenthetical grouping, an error is signaled.
2884 If the beginning or end is reached between groupings
2885 but before count is used up, nil is returned. */)
2886 (Lisp_Object from
, Lisp_Object count
)
2888 CHECK_NUMBER (from
);
2889 CHECK_NUMBER (count
);
2891 return scan_lists (XINT (from
), XINT (count
), 0, 1);
2894 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars
, Sbackward_prefix_chars
,
2896 doc
: /* Move point backward over any number of chars with prefix syntax.
2897 This includes chars with "quote" or "prefix" syntax (' or p). */)
2900 ptrdiff_t beg
= BEGV
;
2901 ptrdiff_t opoint
= PT
;
2902 ptrdiff_t opoint_byte
= PT_BYTE
;
2904 ptrdiff_t pos_byte
= PT_BYTE
;
2909 SET_PT_BOTH (opoint
, opoint_byte
);
2914 SETUP_SYNTAX_TABLE (pos
, -1);
2916 DEC_BOTH (pos
, pos_byte
);
2918 while (!char_quoted (pos
, pos_byte
)
2919 /* Previous statement updates syntax table. */
2920 && ((c
= FETCH_CHAR_AS_MULTIBYTE (pos_byte
), SYNTAX (c
) == Squote
)
2921 || SYNTAX_PREFIX (c
)))
2924 opoint_byte
= pos_byte
;
2927 DEC_BOTH (pos
, pos_byte
);
2930 SET_PT_BOTH (opoint
, opoint_byte
);
2935 /* Parse forward from FROM / FROM_BYTE to END,
2936 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
2937 and return a description of the state of the parse at END.
2938 If STOPBEFORE is nonzero, stop at the start of an atom.
2939 If COMMENTSTOP is 1, stop at the start of a comment.
2940 If COMMENTSTOP is -1, stop at the start or end of a comment,
2941 after the beginning of a string, or after the end of a string. */
2944 scan_sexps_forward (struct lisp_parse_state
*stateptr
,
2945 ptrdiff_t from
, ptrdiff_t from_byte
, ptrdiff_t end
,
2946 EMACS_INT targetdepth
, int stopbefore
,
2947 Lisp_Object oldstate
, int commentstop
)
2949 struct lisp_parse_state state
;
2951 register enum syntaxcode code
;
2954 struct level
{ ptrdiff_t last
, prev
; };
2955 struct level levelstart
[100];
2956 register struct level
*curlevel
= levelstart
;
2957 struct level
*endlevel
= levelstart
+ 100;
2958 register EMACS_INT depth
; /* Paren depth of current scanning location.
2959 level - levelstart equals this except
2960 when the depth becomes negative. */
2961 EMACS_INT mindepth
; /* Lowest DEPTH value seen. */
2962 int start_quoted
= 0; /* Nonzero means starting after a char quote */
2964 ptrdiff_t prev_from
; /* Keep one character before FROM. */
2965 ptrdiff_t prev_from_byte
;
2966 int prev_from_syntax
;
2967 int boundary_stop
= commentstop
== -1;
2970 ptrdiff_t out_bytepos
, out_charpos
;
2974 prev_from_byte
= from_byte
;
2976 DEC_BOTH (prev_from
, prev_from_byte
);
2978 /* Use this macro instead of `from++'. */
2980 do { prev_from = from; \
2981 prev_from_byte = from_byte; \
2982 temp = FETCH_CHAR_AS_MULTIBYTE (prev_from_byte); \
2983 prev_from_syntax = SYNTAX_WITH_FLAGS (temp); \
2984 INC_BOTH (from, from_byte); \
2986 UPDATE_SYNTAX_TABLE_FORWARD (from); \
2992 if (NILP (oldstate
))
2995 state
.instring
= -1;
2996 state
.incomment
= 0;
2997 state
.comstyle
= 0; /* comment style a by default. */
2998 state
.comstr_start
= -1; /* no comment/string seen. */
3002 tem
= Fcar (oldstate
);
3008 oldstate
= Fcdr (oldstate
);
3009 oldstate
= Fcdr (oldstate
);
3010 oldstate
= Fcdr (oldstate
);
3011 tem
= Fcar (oldstate
);
3012 /* Check whether we are inside string_fence-style string: */
3013 state
.instring
= (!NILP (tem
)
3014 ? (CHARACTERP (tem
) ? XFASTINT (tem
) : ST_STRING_STYLE
)
3017 oldstate
= Fcdr (oldstate
);
3018 tem
= Fcar (oldstate
);
3019 state
.incomment
= (!NILP (tem
)
3020 ? (INTEGERP (tem
) ? XINT (tem
) : -1)
3023 oldstate
= Fcdr (oldstate
);
3024 tem
= Fcar (oldstate
);
3025 start_quoted
= !NILP (tem
);
3027 /* if the eighth element of the list is nil, we are in comment
3028 style a. If it is non-nil, we are in comment style b */
3029 oldstate
= Fcdr (oldstate
);
3030 oldstate
= Fcdr (oldstate
);
3031 tem
= Fcar (oldstate
);
3032 state
.comstyle
= (NILP (tem
)
3034 : (RANGED_INTEGERP (0, tem
, ST_COMMENT_STYLE
)
3036 : ST_COMMENT_STYLE
));
3038 oldstate
= Fcdr (oldstate
);
3039 tem
= Fcar (oldstate
);
3040 state
.comstr_start
=
3041 RANGED_INTEGERP (PTRDIFF_MIN
, tem
, PTRDIFF_MAX
) ? XINT (tem
) : -1;
3042 oldstate
= Fcdr (oldstate
);
3043 tem
= Fcar (oldstate
);
3044 while (!NILP (tem
)) /* >= second enclosing sexps. */
3046 Lisp_Object temhd
= Fcar (tem
);
3047 if (RANGED_INTEGERP (PTRDIFF_MIN
, temhd
, PTRDIFF_MAX
))
3048 curlevel
->last
= XINT (temhd
);
3049 if (++curlevel
== endlevel
)
3050 curlevel
--; /* error ("Nesting too deep for parser"); */
3051 curlevel
->prev
= -1;
3052 curlevel
->last
= -1;
3059 curlevel
->prev
= -1;
3060 curlevel
->last
= -1;
3062 SETUP_SYNTAX_TABLE (prev_from
, 1);
3063 temp
= FETCH_CHAR (prev_from_byte
);
3064 prev_from_syntax
= SYNTAX_WITH_FLAGS (temp
);
3065 UPDATE_SYNTAX_TABLE_FORWARD (from
);
3067 /* Enter the loop at a place appropriate for initial state. */
3069 if (state
.incomment
)
3070 goto startincomment
;
3071 if (state
.instring
>= 0)
3073 nofence
= state
.instring
!= ST_STRING_STYLE
;
3075 goto startquotedinstring
;
3078 else if (start_quoted
)
3085 code
= prev_from_syntax
& 0xff;
3088 && SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax
)
3089 && (c1
= FETCH_CHAR (from_byte
),
3090 syntax
= SYNTAX_WITH_FLAGS (c1
),
3091 SYNTAX_FLAGS_COMSTART_SECOND (syntax
)))
3092 /* Duplicate code to avoid a complex if-expression
3093 which causes trouble for the SGI compiler. */
3095 /* Record the comment style we have entered so that only
3096 the comment-end sequence of the same style actually
3097 terminates the comment section. */
3099 = SYNTAX_FLAGS_COMMENT_STYLE (syntax
, prev_from_syntax
);
3100 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax
);
3101 comnested
= comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
3102 state
.incomment
= comnested
? 1 : -1;
3103 state
.comstr_start
= prev_from
;
3107 else if (code
== Scomment_fence
)
3109 /* Record the comment style we have entered so that only
3110 the comment-end sequence of the same style actually
3111 terminates the comment section. */
3112 state
.comstyle
= ST_COMMENT_STYLE
;
3113 state
.incomment
= -1;
3114 state
.comstr_start
= prev_from
;
3117 else if (code
== Scomment
)
3119 state
.comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax
, 0);
3120 state
.incomment
= (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax
) ?
3122 state
.comstr_start
= prev_from
;
3125 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax
))
3127 switch (SWITCH_ENUM_CAST (code
))
3131 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3132 curlevel
->last
= prev_from
;
3134 if (from
== end
) goto endquoted
;
3137 /* treat following character as a word constituent */
3140 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3141 curlevel
->last
= prev_from
;
3145 /* Some compilers can't handle this inside the switch. */
3146 temp
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
3147 temp
= SYNTAX (temp
);
3153 if (from
== end
) goto endquoted
;
3165 curlevel
->prev
= curlevel
->last
;
3168 case Scomment_fence
: /* Can't happen because it's handled above. */
3170 if (commentstop
|| boundary_stop
) goto done
;
3172 /* The (from == BEGV) test was to enter the loop in the middle so
3173 that we find a 2-char comment ender even if we start in the
3174 middle of it. We don't want to do that if we're just at the
3175 beginning of the comment (think of (*) ... (*)). */
3176 found
= forw_comment (from
, from_byte
, end
,
3177 state
.incomment
, state
.comstyle
,
3178 (from
== BEGV
|| from
< state
.comstr_start
+ 3)
3179 ? 0 : prev_from_syntax
,
3180 &out_charpos
, &out_bytepos
, &state
.incomment
);
3181 from
= out_charpos
; from_byte
= out_bytepos
;
3182 /* Beware! prev_from and friends are invalid now.
3183 Luckily, the `done' doesn't use them and the INC_FROM
3184 sets them to a sane value without looking at them. */
3185 if (!found
) goto done
;
3187 state
.incomment
= 0;
3188 state
.comstyle
= 0; /* reset the comment style */
3189 if (boundary_stop
) goto done
;
3193 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3195 /* curlevel++->last ran into compiler bug on Apollo */
3196 curlevel
->last
= prev_from
;
3197 if (++curlevel
== endlevel
)
3198 curlevel
--; /* error ("Nesting too deep for parser"); */
3199 curlevel
->prev
= -1;
3200 curlevel
->last
= -1;
3201 if (targetdepth
== depth
) goto done
;
3206 if (depth
< mindepth
)
3208 if (curlevel
!= levelstart
)
3210 curlevel
->prev
= curlevel
->last
;
3211 if (targetdepth
== depth
) goto done
;
3216 state
.comstr_start
= from
- 1;
3217 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3218 curlevel
->last
= prev_from
;
3219 state
.instring
= (code
== Sstring
3220 ? (FETCH_CHAR_AS_MULTIBYTE (prev_from_byte
))
3222 if (boundary_stop
) goto done
;
3225 nofence
= state
.instring
!= ST_STRING_STYLE
;
3231 if (from
>= end
) goto done
;
3232 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
3233 /* Some compilers can't handle this inside the switch. */
3236 /* Check TEMP here so that if the char has
3237 a syntax-table property which says it is NOT
3238 a string character, it does not end the string. */
3239 if (nofence
&& c
== state
.instring
&& temp
== Sstring
)
3245 if (!nofence
) goto string_end
;
3250 startquotedinstring
:
3251 if (from
>= end
) goto endquoted
;
3257 state
.instring
= -1;
3258 curlevel
->prev
= curlevel
->last
;
3260 if (boundary_stop
) goto done
;
3264 /* FIXME: We should do something with it. */
3267 /* Ignore whitespace, punctuation, quote, endcomment. */
3273 stop
: /* Here if stopping before start of sexp. */
3274 from
= prev_from
; /* We have just fetched the char that starts it; */
3275 goto done
; /* but return the position before it. */
3280 state
.depth
= depth
;
3281 state
.mindepth
= mindepth
;
3282 state
.thislevelstart
= curlevel
->prev
;
3283 state
.prevlevelstart
3284 = (curlevel
== levelstart
) ? -1 : (curlevel
- 1)->last
;
3285 state
.location
= from
;
3286 state
.levelstarts
= Qnil
;
3287 while (curlevel
> levelstart
)
3288 state
.levelstarts
= Fcons (make_number ((--curlevel
)->last
),
3295 DEFUN ("parse-partial-sexp", Fparse_partial_sexp
, Sparse_partial_sexp
, 2, 6, 0,
3296 doc
: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
3297 Parsing stops at TO or when certain criteria are met;
3298 point is set to where parsing stops.
3299 If fifth arg OLDSTATE is omitted or nil,
3300 parsing assumes that FROM is the beginning of a function.
3301 Value is a list of elements describing final state of parsing:
3303 1. character address of start of innermost containing list; nil if none.
3304 2. character address of start of last complete sexp terminated.
3305 3. non-nil if inside a string.
3306 (it is the character that will terminate the string,
3307 or t if the string should be terminated by a generic string delimiter.)
3308 4. nil if outside a comment, t if inside a non-nestable comment,
3309 else an integer (the current comment nesting).
3310 5. t if following a quote character.
3311 6. the minimum paren-depth encountered during this scan.
3312 7. style of comment, if any.
3313 8. character address of start of comment or string; nil if not in one.
3314 9. Intermediate data for continuation of parsing (subject to change).
3315 If third arg TARGETDEPTH is non-nil, parsing stops if the depth
3316 in parentheses becomes equal to TARGETDEPTH.
3317 Fourth arg STOPBEFORE non-nil means stop when come to
3318 any character that starts a sexp.
3319 Fifth arg OLDSTATE is a list like what this function returns.
3320 It is used to initialize the state of the parse. Elements number 1, 2, 6
3322 Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.
3323 If it is symbol `syntax-table', stop after the start of a comment or a
3324 string, or after end of a comment or a string. */)
3325 (Lisp_Object from
, Lisp_Object to
, Lisp_Object targetdepth
, Lisp_Object stopbefore
, Lisp_Object oldstate
, Lisp_Object commentstop
)
3327 struct lisp_parse_state state
;
3330 if (!NILP (targetdepth
))
3332 CHECK_NUMBER (targetdepth
);
3333 target
= XINT (targetdepth
);
3336 target
= TYPE_MINIMUM (EMACS_INT
); /* We won't reach this depth */
3338 validate_region (&from
, &to
);
3339 scan_sexps_forward (&state
, XINT (from
), CHAR_TO_BYTE (XINT (from
)),
3341 target
, !NILP (stopbefore
), oldstate
,
3343 ? 0 : (EQ (commentstop
, Qsyntax_table
) ? -1 : 1)));
3345 SET_PT (state
.location
);
3347 return Fcons (make_number (state
.depth
),
3348 Fcons (state
.prevlevelstart
< 0
3349 ? Qnil
: make_number (state
.prevlevelstart
),
3350 Fcons (state
.thislevelstart
< 0
3351 ? Qnil
: make_number (state
.thislevelstart
),
3352 Fcons (state
.instring
>= 0
3353 ? (state
.instring
== ST_STRING_STYLE
3354 ? Qt
: make_number (state
.instring
)) : Qnil
,
3355 Fcons (state
.incomment
< 0 ? Qt
:
3356 (state
.incomment
== 0 ? Qnil
:
3357 make_number (state
.incomment
)),
3358 Fcons (state
.quoted
? Qt
: Qnil
,
3359 Fcons (make_number (state
.mindepth
),
3360 Fcons ((state
.comstyle
3361 ? (state
.comstyle
== ST_COMMENT_STYLE
3363 : make_number (state
.comstyle
))
3365 Fcons (((state
.incomment
3366 || (state
.instring
>= 0))
3367 ? make_number (state
.comstr_start
)
3369 Fcons (state
.levelstarts
, Qnil
))))))))));
3373 init_syntax_once (void)
3378 /* This has to be done here, before we call Fmake_char_table. */
3379 DEFSYM (Qsyntax_table
, "syntax-table");
3381 /* Intern_C_String this now in case it isn't already done.
3382 Setting this variable twice is harmless.
3383 But don't staticpro it here--that is done in alloc.c. */
3384 Qchar_table_extra_slots
= intern_c_string ("char-table-extra-slots");
3386 /* Create objects which can be shared among syntax tables. */
3387 Vsyntax_code_object
= Fmake_vector (make_number (Smax
), Qnil
);
3388 for (i
= 0; i
< ASIZE (Vsyntax_code_object
); i
++)
3389 XVECTOR (Vsyntax_code_object
)->contents
[i
]
3390 = Fcons (make_number (i
), Qnil
);
3392 /* Now we are ready to set up this property, so we can
3393 create syntax tables. */
3394 Fput (Qsyntax_table
, Qchar_table_extra_slots
, make_number (0));
3396 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Swhitespace
];
3398 Vstandard_syntax_table
= Fmake_char_table (Qsyntax_table
, temp
);
3400 /* Control characters should not be whitespace. */
3401 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Spunct
];
3402 for (i
= 0; i
<= ' ' - 1; i
++)
3403 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3404 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 0177, temp
);
3406 /* Except that a few really are whitespace. */
3407 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Swhitespace
];
3408 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ' ', temp
);
3409 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\t', temp
);
3410 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\n', temp
);
3411 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 015, temp
);
3412 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 014, temp
);
3414 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Sword
];
3415 for (i
= 'a'; i
<= 'z'; i
++)
3416 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3417 for (i
= 'A'; i
<= 'Z'; i
++)
3418 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3419 for (i
= '0'; i
<= '9'; i
++)
3420 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3422 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '$', temp
);
3423 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '%', temp
);
3425 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '(',
3426 Fcons (make_number (Sopen
), make_number (')')));
3427 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ')',
3428 Fcons (make_number (Sclose
), make_number ('(')));
3429 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '[',
3430 Fcons (make_number (Sopen
), make_number (']')));
3431 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ']',
3432 Fcons (make_number (Sclose
), make_number ('[')));
3433 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '{',
3434 Fcons (make_number (Sopen
), make_number ('}')));
3435 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '}',
3436 Fcons (make_number (Sclose
), make_number ('{')));
3437 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '"',
3438 Fcons (make_number ((int) Sstring
), Qnil
));
3439 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\\',
3440 Fcons (make_number ((int) Sescape
), Qnil
));
3442 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Ssymbol
];
3443 for (i
= 0; i
< 10; i
++)
3445 c
= "_-+*/&|<>="[i
];
3446 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, c
, temp
);
3449 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Spunct
];
3450 for (i
= 0; i
< 12; i
++)
3452 c
= ".,;:?!#@~^'`"[i
];
3453 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, c
, temp
);
3456 /* All multibyte characters have syntax `word' by default. */
3457 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Sword
];
3458 char_table_set_range (Vstandard_syntax_table
, 0x80, MAX_CHAR
, temp
);
3462 syms_of_syntax (void)
3464 DEFSYM (Qsyntax_table_p
, "syntax-table-p");
3466 staticpro (&Vsyntax_code_object
);
3468 staticpro (&gl_state
.object
);
3469 staticpro (&gl_state
.global_code
);
3470 staticpro (&gl_state
.current_syntax_table
);
3471 staticpro (&gl_state
.old_prop
);
3473 /* Defined in regex.c */
3474 staticpro (&re_match_object
);
3476 DEFSYM (Qscan_error
, "scan-error");
3477 Fput (Qscan_error
, Qerror_conditions
,
3478 pure_cons (Qscan_error
, pure_cons (Qerror
, Qnil
)));
3479 Fput (Qscan_error
, Qerror_message
,
3480 make_pure_c_string ("Scan error"));
3482 DEFVAR_BOOL ("parse-sexp-ignore-comments", parse_sexp_ignore_comments
,
3483 doc
: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
3485 DEFVAR_BOOL ("parse-sexp-lookup-properties", parse_sexp_lookup_properties
,
3486 doc
: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3487 Otherwise, that text property is simply ignored.
3488 See the info node `(elisp)Syntax Properties' for a description of the
3489 `syntax-table' property. */);
3491 words_include_escapes
= 0;
3492 DEFVAR_BOOL ("words-include-escapes", words_include_escapes
,
3493 doc
: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
3495 DEFVAR_BOOL ("multibyte-syntax-as-symbol", multibyte_syntax_as_symbol
,
3496 doc
: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
3497 multibyte_syntax_as_symbol
= 0;
3499 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3500 open_paren_in_column_0_is_defun_start
,
3501 doc
: /* Non-nil means an open paren in column 0 denotes the start of a defun. */);
3502 open_paren_in_column_0_is_defun_start
= 1;
3505 DEFVAR_LISP ("find-word-boundary-function-table",
3506 Vfind_word_boundary_function_table
,
3508 Char table of functions to search for the word boundary.
3509 Each function is called with two arguments; POS and LIMIT.
3510 POS and LIMIT are character positions in the current buffer.
3512 If POS is less than LIMIT, POS is at the first character of a word,
3513 and the return value of a function is a position after the last
3514 character of that word.
3516 If POS is not less than LIMIT, POS is at the last character of a word,
3517 and the return value of a function is a position at the first
3518 character of that word.
3520 In both cases, LIMIT bounds the search. */);
3521 Vfind_word_boundary_function_table
= Fmake_char_table (Qnil
, Qnil
);
3523 defsubr (&Ssyntax_table_p
);
3524 defsubr (&Ssyntax_table
);
3525 defsubr (&Sstandard_syntax_table
);
3526 defsubr (&Scopy_syntax_table
);
3527 defsubr (&Sset_syntax_table
);
3528 defsubr (&Schar_syntax
);
3529 defsubr (&Smatching_paren
);
3530 defsubr (&Sstring_to_syntax
);
3531 defsubr (&Smodify_syntax_entry
);
3532 defsubr (&Sinternal_describe_syntax_value
);
3534 defsubr (&Sforward_word
);
3536 defsubr (&Sskip_chars_forward
);
3537 defsubr (&Sskip_chars_backward
);
3538 defsubr (&Sskip_syntax_forward
);
3539 defsubr (&Sskip_syntax_backward
);
3541 defsubr (&Sforward_comment
);
3542 defsubr (&Sscan_lists
);
3543 defsubr (&Sscan_sexps
);
3544 defsubr (&Sbackward_prefix_chars
);
3545 defsubr (&Sparse_partial_sexp
);