1 /* GNU Emacs routines to deal with syntax tables; also word and list parsing.
2 Copyright (C) 1985, 1987, 1993, 1994, 1995, 1997, 1998, 1999, 2001,
3 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
28 #include "character.h"
32 /* Make syntax table lookup grant data in gl_state. */
33 #define SYNTAX_ENTRY_VIA_PROPERTY
36 #include "intervals.h"
39 /* Then there are seven single-bit flags that 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 bit 6 and 8 are used to discriminate between different comment styles.
52 Languages such as C++ allow two orthogonal syntax start/end pairs
53 and bit 6 is used to determine 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 only looked up on the second
57 char of the comment marker and on the first char of the comment ender.
58 For style c (like to for the nested flag), the flag can be placed on any
62 /* These macros extract specific flags from an integer
63 that holds the syntax code and the flags. */
65 #define SYNTAX_FLAGS_COMSTART_FIRST(flags) (((flags) >> 16) & 1)
67 #define SYNTAX_FLAGS_COMSTART_SECOND(flags) (((flags) >> 17) & 1)
69 #define SYNTAX_FLAGS_COMEND_FIRST(flags) (((flags) >> 18) & 1)
71 #define SYNTAX_FLAGS_COMEND_SECOND(flags) (((flags) >> 19) & 1)
73 #define SYNTAX_FLAGS_PREFIX(flags) (((flags) >> 20) & 1)
75 #define SYNTAX_FLAGS_COMMENT_STYLEB(flags) (((flags) >> 21) & 1)
76 #define SYNTAX_FLAGS_COMMENT_STYLEC(flags) (((flags) >> 22) & 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_STYLEC (flags) \
82 | SYNTAX_FLAGS_COMMENT_STYLEC (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 Lisp_Object Qsyntax_table_p
, Qsyntax_table
, Qscan_error
;
101 int words_include_escapes
;
102 int parse_sexp_lookup_properties
;
104 /* Nonzero means `scan-sexps' treat all multibyte characters as symbol. */
105 int multibyte_syntax_as_symbol
;
107 /* Used as a temporary in SYNTAX_ENTRY and other macros in syntax.h,
108 if not compiled with GCC. No need to mark it, since it is used
109 only very temporarily. */
110 Lisp_Object syntax_temp
;
112 /* Non-zero means an open parenthesis in column 0 is always considered
113 to be the start of a defun. Zero means an open parenthesis in
114 column 0 has no special meaning. */
116 int open_paren_in_column_0_is_defun_start
;
118 /* This is the internal form of the parse state used in parse-partial-sexp. */
120 struct lisp_parse_state
122 int depth
; /* Depth at end of parsing. */
123 int instring
; /* -1 if not within string, else desired terminator. */
124 int incomment
; /* -1 if in unnestable comment else comment nesting */
125 int comstyle
; /* comment style a=0, or b=1, or ST_COMMENT_STYLE. */
126 int quoted
; /* Nonzero if just after an escape char at end of parsing */
127 int mindepth
; /* Minimum depth seen while scanning. */
128 /* Char number of most recent start-of-expression at current level */
129 EMACS_INT thislevelstart
;
130 /* Char number of start of containing expression */
131 EMACS_INT prevlevelstart
;
132 EMACS_INT location
; /* Char number at which parsing stopped. */
133 EMACS_INT comstr_start
; /* Position of last comment/string starter. */
134 Lisp_Object levelstarts
; /* Char numbers of starts-of-expression
135 of levels (starting from outermost). */
138 /* These variables are a cache for finding the start of a defun.
139 find_start_pos is the place for which the defun start was found.
140 find_start_value is the defun start position found for it.
141 find_start_value_byte is the corresponding byte position.
142 find_start_buffer is the buffer it was found in.
143 find_start_begv is the BEGV value when it was found.
144 find_start_modiff is the value of MODIFF when it was found. */
146 static EMACS_INT find_start_pos
;
147 static EMACS_INT find_start_value
;
148 static EMACS_INT find_start_value_byte
;
149 static struct buffer
*find_start_buffer
;
150 static EMACS_INT find_start_begv
;
151 static int find_start_modiff
;
154 static Lisp_Object
skip_chars (int, Lisp_Object
, Lisp_Object
, int);
155 static Lisp_Object
skip_syntaxes (int, Lisp_Object
, Lisp_Object
);
156 static Lisp_Object
scan_lists (EMACS_INT
, EMACS_INT
, EMACS_INT
, int);
157 static void scan_sexps_forward (struct lisp_parse_state
*,
158 EMACS_INT
, EMACS_INT
, EMACS_INT
, int,
159 int, Lisp_Object
, int);
160 static int in_classes (int, Lisp_Object
);
162 /* Whether the syntax of the character C has the prefix flag set. */
163 int syntax_prefix_flag_p (int c
)
165 return SYNTAX_PREFIX (c
);
168 struct gl_state_s gl_state
; /* Global state of syntax parser. */
170 INTERVAL
interval_of (int, Lisp_Object
);
171 #define INTERVALS_AT_ONCE 10 /* 1 + max-number of intervals
172 to scan to property-change. */
174 /* Update gl_state to an appropriate interval which contains CHARPOS. The
175 sign of COUNT give the relative position of CHARPOS wrt the previously
176 valid interval. If INIT, only [be]_property fields of gl_state are
177 valid at start, the rest is filled basing on OBJECT.
179 `gl_state.*_i' are the intervals, and CHARPOS is further in the search
180 direction than the intervals - or in an interval. We update the
181 current syntax-table basing on the property of this interval, and
182 update the interval to start further than CHARPOS - or be
183 NULL_INTERVAL. We also update lim_property to be the next value of
184 charpos to call this subroutine again - or be before/after the
185 start/end of OBJECT. */
188 update_syntax_table (EMACS_INT charpos
, int count
, int init
,
191 Lisp_Object tmp_table
;
192 int cnt
= 0, invalidate
= 1;
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
;
203 if (NULL_INTERVAL_P (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,
228 if (NULL_INTERVAL_P (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
= current_buffer
->syntax_table
;
298 while (!NULL_INTERVAL_P (i
))
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 + (NULL_INTERVAL_P (next_interval (i
)) ? 1 : 0);
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 (NULL_INTERVAL_P (i
)); /* 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 (EMACS_INT charpos
, EMACS_INT bytepos
)
351 register enum syntaxcode code
;
352 register EMACS_INT beg
= BEGV
;
353 register int quoted
= 0;
354 EMACS_INT 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 after BYTEPOS.
375 We assume that BYTEPOS is not at the end of the buffer. */
378 inc_bytepos (EMACS_INT bytepos
)
380 if (NILP (current_buffer
->enable_multibyte_characters
))
387 /* Return the bytepos one character before BYTEPOS.
388 We assume that BYTEPOS is not at the start of the buffer. */
391 dec_bytepos (EMACS_INT bytepos
)
393 if (NILP (current_buffer
->enable_multibyte_characters
))
400 /* Return a defun-start position before POS and not too far before.
401 It should be the last one before POS, or nearly the last.
403 When open_paren_in_column_0_is_defun_start is nonzero,
404 only the beginning of the buffer is treated as a defun-start.
406 We record the information about where the scan started
407 and what its result was, so that another call in the same area
408 can return the same value very quickly.
410 There is no promise at which position the global syntax data is
411 valid on return from the subroutine, so the caller should explicitly
412 update the global data. */
415 find_defun_start (EMACS_INT pos
, EMACS_INT pos_byte
)
417 EMACS_INT opoint
= PT
, opoint_byte
= PT_BYTE
;
419 if (!open_paren_in_column_0_is_defun_start
)
421 find_start_value_byte
= BEGV_BYTE
;
425 /* Use previous finding, if it's valid and applies to this inquiry. */
426 if (current_buffer
== find_start_buffer
427 /* Reuse the defun-start even if POS is a little farther on.
428 POS might be in the next defun, but that's ok.
429 Our value may not be the best possible, but will still be usable. */
430 && pos
<= find_start_pos
+ 1000
431 && pos
>= find_start_value
432 && BEGV
== find_start_begv
433 && MODIFF
== find_start_modiff
)
434 return find_start_value
;
436 /* Back up to start of line. */
437 scan_newline (pos
, pos_byte
, BEGV
, BEGV_BYTE
, -1, 1);
439 /* We optimize syntax-table lookup for rare updates. Thus we accept
440 only those `^\s(' which are good in global _and_ text-property
442 SETUP_BUFFER_SYNTAX_TABLE ();
447 /* Open-paren at start of line means we may have found our
449 c
= FETCH_CHAR_AS_MULTIBYTE (PT_BYTE
);
450 if (SYNTAX (c
) == Sopen
)
452 SETUP_SYNTAX_TABLE (PT
+ 1, -1); /* Try again... */
453 c
= FETCH_CHAR_AS_MULTIBYTE (PT_BYTE
);
454 if (SYNTAX (c
) == Sopen
)
456 /* Now fallback to the default value. */
457 SETUP_BUFFER_SYNTAX_TABLE ();
459 /* Move to beg of previous line. */
460 scan_newline (PT
, PT_BYTE
, BEGV
, BEGV_BYTE
, -2, 1);
463 /* Record what we found, for the next try. */
464 find_start_value
= PT
;
465 find_start_value_byte
= PT_BYTE
;
466 find_start_buffer
= current_buffer
;
467 find_start_modiff
= MODIFF
;
468 find_start_begv
= BEGV
;
469 find_start_pos
= pos
;
471 TEMP_SET_PT_BOTH (opoint
, opoint_byte
);
473 return find_start_value
;
476 /* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
479 prev_char_comend_first (EMACS_INT pos
, EMACS_INT pos_byte
)
483 DEC_BOTH (pos
, pos_byte
);
484 UPDATE_SYNTAX_TABLE_BACKWARD (pos
);
485 c
= FETCH_CHAR (pos_byte
);
486 val
= SYNTAX_COMEND_FIRST (c
);
487 UPDATE_SYNTAX_TABLE_FORWARD (pos
+ 1);
491 /* Return the SYNTAX_COMSTART_FIRST of the character before POS, POS_BYTE. */
494 * prev_char_comstart_first (pos, pos_byte)
499 * DEC_BOTH (pos, pos_byte);
500 * UPDATE_SYNTAX_TABLE_BACKWARD (pos);
501 * c = FETCH_CHAR (pos_byte);
502 * val = SYNTAX_COMSTART_FIRST (c);
503 * UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
507 /* Checks whether charpos FROM is at the end of a comment.
508 FROM_BYTE is the bytepos corresponding to FROM.
509 Do not move back before STOP.
511 Return a positive value if we find a comment ending at FROM/FROM_BYTE;
514 If successful, store the charpos of the comment's beginning
515 into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR.
517 Global syntax data remains valid for backward search starting at
518 the returned value (or at FROM, if the search was not successful). */
521 back_comment (EMACS_INT from
, EMACS_INT from_byte
, EMACS_INT stop
, int comnested
, int comstyle
, EMACS_INT
*charpos_ptr
, EMACS_INT
*bytepos_ptr
)
523 /* Look back, counting the parity of string-quotes,
524 and recording the comment-starters seen.
525 When we reach a safe place, assume that's not in a string;
526 then step the main scan to the earliest comment-starter seen
527 an even number of string quotes away from the safe place.
529 OFROM[I] is position of the earliest comment-starter seen
530 which is I+2X quotes from the comment-end.
531 PARITY is current parity of quotes from the comment end. */
532 int string_style
= -1; /* Presumed outside of any string. */
533 int string_lossage
= 0;
534 /* Not a real lossage: indicates that we have passed a matching comment
535 starter plus a non-matching comment-ender, meaning that any matching
536 comment-starter we might see later could be a false positive (hidden
537 inside another comment).
538 Test case: { a (* b } c (* d *) */
539 int comment_lossage
= 0;
540 EMACS_INT comment_end
= from
;
541 EMACS_INT comment_end_byte
= from_byte
;
542 EMACS_INT comstart_pos
= 0;
543 EMACS_INT comstart_byte
;
544 /* Place where the containing defun starts,
545 or 0 if we didn't come across it yet. */
546 EMACS_INT defun_start
= 0;
547 EMACS_INT defun_start_byte
= 0;
548 register enum syntaxcode code
;
549 int nesting
= 1; /* current comment nesting */
553 /* FIXME: A }} comment-ender style leads to incorrect behavior
554 in the case of {{ c }}} because we ignore the last two chars which are
555 assumed to be comment-enders although they aren't. */
557 /* At beginning of range to scan, we're outside of strings;
558 that determines quote parity to the comment-end. */
562 int prev_syntax
, com2start
, com2end
;
565 /* Move back and examine a character. */
566 DEC_BOTH (from
, from_byte
);
567 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
569 prev_syntax
= syntax
;
570 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
571 syntax
= SYNTAX_WITH_FLAGS (c
);
574 /* Check for 2-char comment markers. */
575 com2start
= (SYNTAX_FLAGS_COMSTART_FIRST (syntax
)
576 && SYNTAX_FLAGS_COMSTART_SECOND (prev_syntax
)
578 == SYNTAX_FLAGS_COMMENT_STYLE (prev_syntax
, syntax
))
579 && (SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax
)
580 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
)) == comnested
);
581 com2end
= (SYNTAX_FLAGS_COMEND_FIRST (syntax
)
582 && SYNTAX_FLAGS_COMEND_SECOND (prev_syntax
));
583 comstart
= (com2start
|| code
== Scomment
);
585 /* Nasty cases with overlapping 2-char comment markers:
586 - snmp-mode: -- c -- foo -- c --
594 /* If a 2-char comment sequence partly overlaps with another,
595 we don't try to be clever. E.g. |*| in C, or }% in modes that
596 have %..\n and %{..}%. */
597 if (from
> stop
&& (com2end
|| comstart
))
599 EMACS_INT next
= from
, next_byte
= from_byte
;
600 int next_c
, next_syntax
;
601 DEC_BOTH (next
, next_byte
);
602 UPDATE_SYNTAX_TABLE_BACKWARD (next
);
603 next_c
= FETCH_CHAR_AS_MULTIBYTE (next_byte
);
604 next_syntax
= SYNTAX_WITH_FLAGS (next_c
);
605 if (((comstart
|| comnested
)
606 && SYNTAX_FLAGS_COMEND_SECOND (syntax
)
607 && SYNTAX_FLAGS_COMEND_FIRST (next_syntax
))
608 || ((com2end
|| comnested
)
609 && SYNTAX_FLAGS_COMSTART_SECOND (syntax
)
611 == SYNTAX_FLAGS_COMMENT_STYLE (syntax
, prev_syntax
))
612 && SYNTAX_FLAGS_COMSTART_FIRST (next_syntax
)))
614 /* UPDATE_SYNTAX_TABLE_FORWARD (next + 1); */
617 if (com2start
&& comstart_pos
== 0)
618 /* We're looking at a comment starter. But it might be a comment
619 ender as well (see snmp-mode). The first time we see one, we
620 need to consider it as a comment starter,
621 and the subsequent times as a comment ender. */
624 /* Turn a 2-char comment sequences into the appropriate syntax. */
629 /* Ignore comment starters of a different style. */
630 else if (code
== Scomment
631 && (comstyle
!= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0)
632 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
) != comnested
))
635 /* Ignore escaped characters, except comment-enders. */
636 if (code
!= Sendcomment
&& char_quoted (from
, from_byte
))
643 c
= (code
== Sstring_fence
? ST_STRING_STYLE
: ST_COMMENT_STYLE
);
645 /* Track parity of quotes. */
646 if (string_style
== -1)
647 /* Entering a string. */
649 else if (string_style
== c
)
650 /* Leaving the string. */
653 /* If we have two kinds of string delimiters.
654 There's no way to grok this scanning backwards. */
659 /* We've already checked that it is the relevant comstyle. */
660 if (string_style
!= -1 || comment_lossage
|| string_lossage
)
661 /* There are odd string quotes involved, so let's be careful.
662 Test case in Pascal: " { " a { " } */
667 /* Record best comment-starter so far. */
669 comstart_byte
= from_byte
;
671 else if (--nesting
<= 0)
672 /* nested comments have to be balanced, so we don't need to
673 keep looking for earlier ones. We use here the same (slightly
674 incorrect) reasoning as below: since it is followed by uniform
675 paired string quotes, this comment-start has to be outside of
676 strings, else the comment-end itself would be inside a string. */
681 if (SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == comstyle
682 && ((com2end
&& SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax
))
683 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
)) == comnested
)
684 /* This is the same style of comment ender as ours. */
689 /* Anything before that can't count because it would match
690 this comment-ender rather than ours. */
691 from
= stop
; /* Break out of the loop. */
693 else if (comstart_pos
!= 0 || c
!= '\n')
694 /* We're mixing comment styles here, so we'd better be careful.
695 The (comstart_pos != 0 || c != '\n') check is not quite correct
696 (we should just always set comment_lossage), but removing it
697 would imply that any multiline comment in C would go through
698 lossage, which seems overkill.
699 The failure should only happen in the rare cases such as
705 /* Assume a defun-start point is outside of strings. */
706 if (open_paren_in_column_0_is_defun_start
708 || (temp_byte
= dec_bytepos (from_byte
),
709 FETCH_CHAR (temp_byte
) == '\n')))
712 defun_start_byte
= from_byte
;
713 from
= stop
; /* Break out of the loop. */
722 if (comstart_pos
== 0)
725 from_byte
= comment_end_byte
;
726 UPDATE_SYNTAX_TABLE_FORWARD (comment_end
- 1);
728 /* If comstart_pos is set and we get here (ie. didn't jump to `lossage'
729 or `done'), then we've found the beginning of the non-nested comment. */
730 else if (1) /* !comnested */
733 from_byte
= comstart_byte
;
734 UPDATE_SYNTAX_TABLE_FORWARD (from
- 1);
738 struct lisp_parse_state state
;
740 /* We had two kinds of string delimiters mixed up
741 together. Decode this going forwards.
742 Scan fwd from a known safe place (beginning-of-defun)
743 to the one in question; this records where we
744 last passed a comment starter. */
745 /* If we did not already find the defun start, find it now. */
746 if (defun_start
== 0)
748 defun_start
= find_defun_start (comment_end
, comment_end_byte
);
749 defun_start_byte
= find_start_value_byte
;
753 scan_sexps_forward (&state
,
754 defun_start
, defun_start_byte
,
755 comment_end
, -10000, 0, Qnil
, 0);
756 defun_start
= comment_end
;
757 if (state
.incomment
== (comnested
? 1 : -1)
758 && state
.comstyle
== comstyle
)
759 from
= state
.comstr_start
;
764 /* If comment_end is inside some other comment, maybe ours
765 is nested, so we need to try again from within the
766 surrounding comment. Example: { a (* " *) */
768 /* FIXME: We should advance by one or two chars. */
769 defun_start
= state
.comstr_start
+ 2;
770 defun_start_byte
= CHAR_TO_BYTE (defun_start
);
773 } while (defun_start
< comment_end
);
775 from_byte
= CHAR_TO_BYTE (from
);
776 UPDATE_SYNTAX_TABLE_FORWARD (from
- 1);
781 *bytepos_ptr
= from_byte
;
783 return (from
== comment_end
) ? -1 : from
;
786 DEFUN ("syntax-table-p", Fsyntax_table_p
, Ssyntax_table_p
, 1, 1, 0,
787 doc
: /* Return t if OBJECT is a syntax table.
788 Currently, any char-table counts as a syntax table. */)
791 if (CHAR_TABLE_P (object
)
792 && EQ (XCHAR_TABLE (object
)->purpose
, Qsyntax_table
))
798 check_syntax_table (Lisp_Object obj
)
800 CHECK_TYPE (CHAR_TABLE_P (obj
) && EQ (XCHAR_TABLE (obj
)->purpose
, Qsyntax_table
),
801 Qsyntax_table_p
, obj
);
804 DEFUN ("syntax-table", Fsyntax_table
, Ssyntax_table
, 0, 0, 0,
805 doc
: /* Return the current syntax table.
806 This is the one specified by the current buffer. */)
809 return current_buffer
->syntax_table
;
812 DEFUN ("standard-syntax-table", Fstandard_syntax_table
,
813 Sstandard_syntax_table
, 0, 0, 0,
814 doc
: /* Return the standard syntax table.
815 This is the one used for new buffers. */)
818 return Vstandard_syntax_table
;
821 DEFUN ("copy-syntax-table", Fcopy_syntax_table
, Scopy_syntax_table
, 0, 1, 0,
822 doc
: /* Construct a new syntax table and return it.
823 It is a copy of the TABLE, which defaults to the standard syntax table. */)
829 check_syntax_table (table
);
831 table
= Vstandard_syntax_table
;
833 copy
= Fcopy_sequence (table
);
835 /* Only the standard syntax table should have a default element.
836 Other syntax tables should inherit from parents instead. */
837 XCHAR_TABLE (copy
)->defalt
= Qnil
;
839 /* Copied syntax tables should all have parents.
840 If we copied one with no parent, such as the standard syntax table,
841 use the standard syntax table as the copy's parent. */
842 if (NILP (XCHAR_TABLE (copy
)->parent
))
843 Fset_char_table_parent (copy
, Vstandard_syntax_table
);
847 DEFUN ("set-syntax-table", Fset_syntax_table
, Sset_syntax_table
, 1, 1, 0,
848 doc
: /* Select a new syntax table for the current buffer.
849 One argument, a syntax table. */)
853 check_syntax_table (table
);
854 current_buffer
->syntax_table
= table
;
855 /* Indicate that this buffer now has a specified syntax table. */
856 idx
= PER_BUFFER_VAR_IDX (syntax_table
);
857 SET_PER_BUFFER_VALUE_P (current_buffer
, idx
, 1);
861 /* Convert a letter which signifies a syntax code
862 into the code it signifies.
863 This is used by modify-syntax-entry, and other things. */
865 unsigned char syntax_spec_code
[0400] =
866 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
867 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
868 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
869 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
870 (char) Swhitespace
, (char) Scomment_fence
, (char) Sstring
, 0377,
871 (char) Smath
, 0377, 0377, (char) Squote
,
872 (char) Sopen
, (char) Sclose
, 0377, 0377,
873 0377, (char) Swhitespace
, (char) Spunct
, (char) Scharquote
,
874 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
875 0377, 0377, 0377, 0377,
876 (char) Scomment
, 0377, (char) Sendcomment
, 0377,
877 (char) Sinherit
, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
878 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
879 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword
,
880 0377, 0377, 0377, 0377, (char) Sescape
, 0377, 0377, (char) Ssymbol
,
881 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
882 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
883 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword
,
884 0377, 0377, 0377, 0377, (char) Sstring_fence
, 0377, 0377, 0377
887 /* Indexed by syntax code, give the letter that describes it. */
889 char syntax_code_spec
[16] =
891 ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@',
895 /* Indexed by syntax code, give the object (cons of syntax code and
896 nil) to be stored in syntax table. Since these objects can be
897 shared among syntax tables, we generate them in advance. By
898 sharing objects, the function `describe-syntax' can give a more
900 static Lisp_Object Vsyntax_code_object
;
903 DEFUN ("char-syntax", Fchar_syntax
, Schar_syntax
, 1, 1, 0,
904 doc
: /* Return the syntax code of CHARACTER, described by a character.
905 For example, if CHARACTER is a word constituent, the
906 character `w' (119) is returned.
907 The characters that correspond to various syntax codes
908 are listed in the documentation of `modify-syntax-entry'. */)
909 (Lisp_Object character
)
912 CHECK_CHARACTER (character
);
913 char_int
= XINT (character
);
914 SETUP_BUFFER_SYNTAX_TABLE ();
915 return make_number (syntax_code_spec
[(int) SYNTAX (char_int
)]);
918 DEFUN ("matching-paren", Fmatching_paren
, Smatching_paren
, 1, 1, 0,
919 doc
: /* Return the matching parenthesis of CHARACTER, or nil if none. */)
920 (Lisp_Object character
)
923 CHECK_NUMBER (character
);
924 char_int
= XINT (character
);
925 SETUP_BUFFER_SYNTAX_TABLE ();
926 code
= SYNTAX (char_int
);
927 if (code
== Sopen
|| code
== Sclose
)
928 return SYNTAX_MATCH (char_int
);
932 DEFUN ("string-to-syntax", Fstring_to_syntax
, Sstring_to_syntax
, 1, 1, 0,
933 doc
: /* Convert a syntax specification STRING into syntax cell form.
934 STRING should be a string as it is allowed as argument of
935 `modify-syntax-entry'. Value is the equivalent cons cell
936 \(CODE . MATCHING-CHAR) that can be used as value of a `syntax-table'
940 register const unsigned char *p
;
941 register enum syntaxcode code
;
945 CHECK_STRING (string
);
948 code
= (enum syntaxcode
) syntax_spec_code
[*p
++];
949 if (((int) code
& 0377) == 0377)
950 error ("Invalid syntax description letter: %c", p
[-1]);
952 if (code
== Sinherit
)
958 int character
= STRING_CHAR_AND_LENGTH (p
, len
);
959 XSETINT (match
, character
);
960 if (XFASTINT (match
) == ' ')
1004 if (val
< XVECTOR (Vsyntax_code_object
)->size
&& NILP (match
))
1005 return XVECTOR (Vsyntax_code_object
)->contents
[val
];
1007 /* Since we can't use a shared object, let's make a new one. */
1008 return Fcons (make_number (val
), match
);
1011 /* I really don't know why this is interactive
1012 help-form should at least be made useful whilst reading the second arg. */
1013 DEFUN ("modify-syntax-entry", Fmodify_syntax_entry
, Smodify_syntax_entry
, 2, 3,
1014 "cSet syntax for character: \nsSet syntax for %s to: ",
1015 doc
: /* Set syntax for character CHAR according to string NEWENTRY.
1016 The syntax is changed only for table SYNTAX-TABLE, which defaults to
1017 the current buffer's syntax table.
1018 CHAR may be a cons (MIN . MAX), in which case, syntaxes of all characters
1019 in the range MIN to MAX are changed.
1020 The first character of NEWENTRY should be one of the following:
1021 Space or - whitespace syntax. w word constituent.
1022 _ symbol constituent. . punctuation.
1023 ( open-parenthesis. ) close-parenthesis.
1024 " string quote. \\ escape.
1025 $ paired delimiter. ' expression quote or prefix operator.
1026 < comment starter. > comment ender.
1027 / character-quote. @ inherit from `standard-syntax-table'.
1028 | generic string fence. ! generic comment fence.
1030 Only single-character comment start and end sequences are represented thus.
1031 Two-character sequences are represented as described below.
1032 The second character of NEWENTRY is the matching parenthesis,
1033 used only if the first character is `(' or `)'.
1034 Any additional characters are flags.
1035 Defined flags are the characters 1, 2, 3, 4, b, p, and n.
1036 1 means CHAR is the start of a two-char comment start sequence.
1037 2 means CHAR is the second character of such a sequence.
1038 3 means CHAR is the start of a two-char comment end sequence.
1039 4 means CHAR is the second character of such a sequence.
1041 There can be several orthogonal comment sequences. This is to support
1042 language modes such as C++. By default, all comment sequences are of style
1043 a, but you can set the comment sequence style to b (on the second character
1044 of a comment-start, and the first character of a comment-end sequence) and/or
1045 c (on any of its chars) using this flag:
1046 b means CHAR is part of comment sequence b.
1047 c means CHAR is part of comment sequence c.
1048 n means CHAR is part of a nestable comment sequence.
1050 p means CHAR is a prefix character for `backward-prefix-chars';
1051 such characters are treated as whitespace when they occur
1052 between expressions.
1053 usage: (modify-syntax-entry CHAR NEWENTRY &optional SYNTAX-TABLE) */)
1054 (Lisp_Object c
, Lisp_Object newentry
, Lisp_Object syntax_table
)
1058 CHECK_CHARACTER_CAR (c
);
1059 CHECK_CHARACTER_CDR (c
);
1062 CHECK_CHARACTER (c
);
1064 if (NILP (syntax_table
))
1065 syntax_table
= current_buffer
->syntax_table
;
1067 check_syntax_table (syntax_table
);
1069 newentry
= Fstring_to_syntax (newentry
);
1071 SET_RAW_SYNTAX_ENTRY_RANGE (syntax_table
, c
, newentry
);
1073 SET_RAW_SYNTAX_ENTRY (syntax_table
, XINT (c
), newentry
);
1075 /* We clear the regexp cache, since character classes can now have
1076 different values from those in the compiled regexps.*/
1077 clear_regexp_cache ();
1082 /* Dump syntax table to buffer in human-readable format */
1084 DEFUN ("internal-describe-syntax-value", Finternal_describe_syntax_value
,
1085 Sinternal_describe_syntax_value
, 1, 1, 0,
1086 doc
: /* Insert a description of the internal syntax description SYNTAX at point. */)
1087 (Lisp_Object syntax
)
1089 register enum syntaxcode code
;
1091 char desc
, start1
, start2
, end1
, end2
, prefix
,
1092 comstyleb
, comstylec
, comnested
;
1094 Lisp_Object first
, match_lisp
, value
= syntax
;
1098 insert_string ("default");
1102 if (CHAR_TABLE_P (value
))
1104 insert_string ("deeper char-table ...");
1110 insert_string ("invalid");
1114 first
= XCAR (value
);
1115 match_lisp
= XCDR (value
);
1117 if (!INTEGERP (first
) || !(NILP (match_lisp
) || INTEGERP (match_lisp
)))
1119 insert_string ("invalid");
1123 syntax_code
= XINT (first
);
1124 code
= (enum syntaxcode
) (syntax_code
& 0377);
1125 start1
= SYNTAX_FLAGS_COMSTART_FIRST (syntax_code
);
1126 start2
= SYNTAX_FLAGS_COMSTART_SECOND (syntax_code
);;
1127 end1
= SYNTAX_FLAGS_COMEND_FIRST (syntax_code
);
1128 end2
= SYNTAX_FLAGS_COMEND_SECOND (syntax_code
);
1129 prefix
= SYNTAX_FLAGS_PREFIX (syntax_code
);
1130 comstyleb
= SYNTAX_FLAGS_COMMENT_STYLEB (syntax_code
);
1131 comstylec
= SYNTAX_FLAGS_COMMENT_STYLEC (syntax_code
);
1132 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax_code
);
1134 if ((int) code
< 0 || (int) code
>= (int) Smax
)
1136 insert_string ("invalid");
1139 desc
= syntax_code_spec
[(int) code
];
1141 str
[0] = desc
, str
[1] = 0;
1144 if (NILP (match_lisp
))
1147 insert_char (XINT (match_lisp
));
1168 insert_string ("\twhich means: ");
1170 switch (SWITCH_ENUM_CAST (code
))
1173 insert_string ("whitespace"); break;
1175 insert_string ("punctuation"); break;
1177 insert_string ("word"); break;
1179 insert_string ("symbol"); break;
1181 insert_string ("open"); break;
1183 insert_string ("close"); break;
1185 insert_string ("prefix"); break;
1187 insert_string ("string"); break;
1189 insert_string ("math"); break;
1191 insert_string ("escape"); break;
1193 insert_string ("charquote"); break;
1195 insert_string ("comment"); break;
1197 insert_string ("endcomment"); break;
1199 insert_string ("inherit"); break;
1200 case Scomment_fence
:
1201 insert_string ("comment fence"); break;
1203 insert_string ("string fence"); break;
1205 insert_string ("invalid");
1209 if (!NILP (match_lisp
))
1211 insert_string (", matches ");
1212 insert_char (XINT (match_lisp
));
1216 insert_string (",\n\t is the first character of a comment-start sequence");
1218 insert_string (",\n\t is the second character of a comment-start sequence");
1221 insert_string (",\n\t is the first character of a comment-end sequence");
1223 insert_string (",\n\t is the second character of a comment-end sequence");
1225 insert_string (" (comment style b)");
1227 insert_string (" (comment style c)");
1229 insert_string (" (nestable)");
1232 insert_string (",\n\t is a prefix character for `backward-prefix-chars'");
1237 int parse_sexp_ignore_comments
;
1239 /* Char-table of functions that find the next or previous word
1241 Lisp_Object Vfind_word_boundary_function_table
;
1243 /* Return the position across COUNT words from FROM.
1244 If that many words cannot be found before the end of the buffer, return 0.
1245 COUNT negative means scan backward and stop at word beginning. */
1248 scan_words (register EMACS_INT from
, register EMACS_INT count
)
1250 register EMACS_INT beg
= BEGV
;
1251 register EMACS_INT end
= ZV
;
1252 register EMACS_INT from_byte
= CHAR_TO_BYTE (from
);
1253 register enum syntaxcode code
;
1255 Lisp_Object func
, script
, pos
;
1260 SETUP_SYNTAX_TABLE (from
, count
);
1271 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1272 ch0
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1273 code
= SYNTAX (ch0
);
1274 INC_BOTH (from
, from_byte
);
1275 if (words_include_escapes
1276 && (code
== Sescape
|| code
== Scharquote
))
1281 /* Now CH0 is a character which begins a word and FROM is the
1282 position of the next character. */
1283 func
= CHAR_TABLE_REF (Vfind_word_boundary_function_table
, ch0
);
1284 if (! NILP (Ffboundp (func
)))
1286 pos
= call2 (func
, make_number (from
- 1), make_number (end
));
1287 if (INTEGERP (pos
) && XINT (pos
) > from
)
1290 from_byte
= CHAR_TO_BYTE (from
);
1295 script
= CHAR_TABLE_REF (Vchar_script_table
, ch0
);
1298 if (from
== end
) break;
1299 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1300 ch1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1301 code
= SYNTAX (ch1
);
1303 && (! words_include_escapes
1304 || (code
!= Sescape
&& code
!= Scharquote
)))
1305 || word_boundary_p (ch0
, ch1
))
1307 INC_BOTH (from
, from_byte
);
1322 DEC_BOTH (from
, from_byte
);
1323 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
1324 ch1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1325 code
= SYNTAX (ch1
);
1326 if (words_include_escapes
1327 && (code
== Sescape
|| code
== Scharquote
))
1332 /* Now CH1 is a character which ends a word and FROM is the
1334 func
= CHAR_TABLE_REF (Vfind_word_boundary_function_table
, ch1
);
1335 if (! NILP (Ffboundp (func
)))
1337 pos
= call2 (func
, make_number (from
), make_number (beg
));
1338 if (INTEGERP (pos
) && XINT (pos
) < from
)
1341 from_byte
= CHAR_TO_BYTE (from
);
1346 script
= CHAR_TABLE_REF (Vchar_script_table
, ch1
);
1351 DEC_BOTH (from
, from_byte
);
1352 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
1353 ch0
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1354 code
= SYNTAX (ch0
);
1356 && (! words_include_escapes
1357 || (code
!= Sescape
&& code
!= Scharquote
)))
1358 || word_boundary_p (ch0
, ch1
))
1360 INC_BOTH (from
, from_byte
);
1374 DEFUN ("forward-word", Fforward_word
, Sforward_word
, 0, 1, "^p",
1375 doc
: /* Move point forward ARG words (backward if ARG is negative).
1377 If an edge of the buffer or a field boundary is reached, point is left there
1378 and the function returns nil. Field boundaries are not noticed if
1379 `inhibit-field-text-motion' is non-nil. */)
1386 XSETFASTINT (arg
, 1);
1390 val
= orig_val
= scan_words (PT
, XINT (arg
));
1392 val
= XINT (arg
) > 0 ? ZV
: BEGV
;
1394 /* Avoid jumping out of an input field. */
1395 tmp
= Fconstrain_to_field (make_number (val
), make_number (PT
),
1397 val
= XFASTINT (tmp
);
1400 return val
== orig_val
? Qt
: Qnil
;
1403 Lisp_Object
skip_chars (int, Lisp_Object
, Lisp_Object
, int);
1405 DEFUN ("skip-chars-forward", Fskip_chars_forward
, Sskip_chars_forward
, 1, 2, 0,
1406 doc
: /* Move point forward, stopping before a char not in STRING, or at pos LIM.
1407 STRING is like the inside of a `[...]' in a regular expression
1408 except that `]' is never special and `\\' quotes `^', `-' or `\\'
1409 (but not at the end of a range; quoting is never needed there).
1410 Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
1411 With arg "^a-zA-Z", skips nonletters stopping before first letter.
1412 Char classes, e.g. `[:alpha:]', are supported.
1414 Returns the distance traveled, either zero or positive. */)
1415 (Lisp_Object string
, Lisp_Object lim
)
1417 return skip_chars (1, string
, lim
, 1);
1420 DEFUN ("skip-chars-backward", Fskip_chars_backward
, Sskip_chars_backward
, 1, 2, 0,
1421 doc
: /* Move point backward, stopping after a char not in STRING, or at pos LIM.
1422 See `skip-chars-forward' for details.
1423 Returns the distance traveled, either zero or negative. */)
1424 (Lisp_Object string
, Lisp_Object lim
)
1426 return skip_chars (0, string
, lim
, 1);
1429 DEFUN ("skip-syntax-forward", Fskip_syntax_forward
, Sskip_syntax_forward
, 1, 2, 0,
1430 doc
: /* Move point forward across chars in specified syntax classes.
1431 SYNTAX is a string of syntax code characters.
1432 Stop before a char whose syntax is not in SYNTAX, or at position LIM.
1433 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1434 This function returns the distance traveled, either zero or positive. */)
1435 (Lisp_Object syntax
, Lisp_Object lim
)
1437 return skip_syntaxes (1, syntax
, lim
);
1440 DEFUN ("skip-syntax-backward", Fskip_syntax_backward
, Sskip_syntax_backward
, 1, 2, 0,
1441 doc
: /* Move point backward across chars in specified syntax classes.
1442 SYNTAX is a string of syntax code characters.
1443 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.
1444 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1445 This function returns the distance traveled, either zero or negative. */)
1446 (Lisp_Object syntax
, Lisp_Object lim
)
1448 return skip_syntaxes (0, syntax
, lim
);
1452 skip_chars (int forwardp
, Lisp_Object string
, Lisp_Object lim
, int handle_iso_classes
)
1454 register unsigned int c
;
1455 unsigned char fastmap
[0400];
1456 /* Store the ranges of non-ASCII characters. */
1458 int n_char_ranges
= 0;
1460 register EMACS_INT i
, i_byte
;
1461 /* Set to 1 if the current buffer is multibyte and the region
1462 contains non-ASCII chars. */
1464 /* Set to 1 if STRING is multibyte and it contains non-ASCII
1466 int string_multibyte
;
1467 EMACS_INT size_byte
;
1468 const unsigned char *str
;
1470 Lisp_Object iso_classes
;
1472 CHECK_STRING (string
);
1476 XSETINT (lim
, forwardp
? ZV
: BEGV
);
1478 CHECK_NUMBER_COERCE_MARKER (lim
);
1480 /* In any case, don't allow scan outside bounds of buffer. */
1481 if (XINT (lim
) > ZV
)
1482 XSETFASTINT (lim
, ZV
);
1483 if (XINT (lim
) < BEGV
)
1484 XSETFASTINT (lim
, BEGV
);
1486 multibyte
= (!NILP (current_buffer
->enable_multibyte_characters
)
1487 && (XINT (lim
) - PT
!= CHAR_TO_BYTE (XINT (lim
)) - PT_BYTE
));
1488 string_multibyte
= SBYTES (string
) > SCHARS (string
);
1490 memset (fastmap
, 0, sizeof fastmap
);
1492 str
= SDATA (string
);
1493 size_byte
= SBYTES (string
);
1496 if (i_byte
< size_byte
1497 && SREF (string
, 0) == '^')
1499 negate
= 1; i_byte
++;
1502 /* Find the characters specified and set their elements of fastmap.
1503 Handle backslashes and ranges specially.
1505 If STRING contains non-ASCII characters, setup char_ranges for
1506 them and use fastmap only for their leading codes. */
1508 if (! string_multibyte
)
1510 int string_has_eight_bit
= 0;
1512 /* At first setup fastmap. */
1513 while (i_byte
< size_byte
)
1517 if (handle_iso_classes
&& c
== '['
1518 && i_byte
< size_byte
1519 && str
[i_byte
] == ':')
1521 const unsigned char *class_beg
= str
+ i_byte
+ 1;
1522 const unsigned char *class_end
= class_beg
;
1523 const unsigned char *class_limit
= str
+ size_byte
- 2;
1524 /* Leave room for the null. */
1525 unsigned char class_name
[CHAR_CLASS_MAX_LENGTH
+ 1];
1528 if (class_limit
- class_beg
> CHAR_CLASS_MAX_LENGTH
)
1529 class_limit
= class_beg
+ CHAR_CLASS_MAX_LENGTH
;
1531 while (class_end
< class_limit
1532 && *class_end
>= 'a' && *class_end
<= 'z')
1535 if (class_end
== class_beg
1536 || *class_end
!= ':' || class_end
[1] != ']')
1537 goto not_a_class_name
;
1539 memcpy (class_name
, class_beg
, class_end
- class_beg
);
1540 class_name
[class_end
- class_beg
] = 0;
1542 cc
= re_wctype (class_name
);
1544 error ("Invalid ISO C character class");
1546 iso_classes
= Fcons (make_number (cc
), iso_classes
);
1548 i_byte
= class_end
+ 2 - str
;
1555 if (i_byte
== size_byte
)
1560 /* Treat `-' as range character only if another character
1562 if (i_byte
+ 1 < size_byte
1563 && str
[i_byte
] == '-')
1567 /* Skip over the dash. */
1570 /* Get the end of the range. */
1573 && i_byte
< size_byte
)
1580 if (! ASCII_CHAR_P (c2
))
1581 string_has_eight_bit
= 1;
1587 if (! ASCII_CHAR_P (c
))
1588 string_has_eight_bit
= 1;
1592 /* If the current range is multibyte and STRING contains
1593 eight-bit chars, arrange fastmap and setup char_ranges for
1594 the corresponding multibyte chars. */
1595 if (multibyte
&& string_has_eight_bit
)
1597 unsigned char fastmap2
[0400];
1598 int range_start_byte
, range_start_char
;
1600 memcpy (fastmap
+ 0200, fastmap2
+ 0200, 0200);
1601 memset (fastmap
+ 0200, 0, 0200);
1602 /* We are sure that this loop stops. */
1603 for (i
= 0200; ! fastmap2
[i
]; i
++);
1604 c
= BYTE8_TO_CHAR (i
);
1605 fastmap
[CHAR_LEADING_CODE (c
)] = 1;
1606 range_start_byte
= i
;
1607 range_start_char
= c
;
1608 char_ranges
= (int *) alloca (sizeof (int) * 128 * 2);
1609 for (i
= 129; i
< 0400; i
++)
1611 c
= BYTE8_TO_CHAR (i
);
1612 fastmap
[CHAR_LEADING_CODE (c
)] = 1;
1613 if (i
- range_start_byte
!= c
- range_start_char
)
1615 char_ranges
[n_char_ranges
++] = range_start_char
;
1616 char_ranges
[n_char_ranges
++] = ((i
- 1 - range_start_byte
)
1617 + range_start_char
);
1618 range_start_byte
= i
;
1619 range_start_char
= c
;
1622 char_ranges
[n_char_ranges
++] = range_start_char
;
1623 char_ranges
[n_char_ranges
++] = ((i
- 1 - range_start_byte
)
1624 + range_start_char
);
1627 else /* STRING is multibyte */
1629 char_ranges
= (int *) alloca (sizeof (int) * SCHARS (string
) * 2);
1631 while (i_byte
< size_byte
)
1633 unsigned char leading_code
;
1635 leading_code
= str
[i_byte
];
1636 c
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1639 if (handle_iso_classes
&& c
== '['
1640 && i_byte
< size_byte
1641 && STRING_CHAR (str
+ i_byte
) == ':')
1643 const unsigned char *class_beg
= str
+ i_byte
+ 1;
1644 const unsigned char *class_end
= class_beg
;
1645 const unsigned char *class_limit
= str
+ size_byte
- 2;
1646 /* Leave room for the null. */
1647 unsigned char class_name
[CHAR_CLASS_MAX_LENGTH
+ 1];
1650 if (class_limit
- class_beg
> CHAR_CLASS_MAX_LENGTH
)
1651 class_limit
= class_beg
+ CHAR_CLASS_MAX_LENGTH
;
1653 while (class_end
< class_limit
1654 && *class_end
>= 'a' && *class_end
<= 'z')
1657 if (class_end
== class_beg
1658 || *class_end
!= ':' || class_end
[1] != ']')
1659 goto not_a_class_name_multibyte
;
1661 memcpy (class_name
, class_beg
, class_end
- class_beg
);
1662 class_name
[class_end
- class_beg
] = 0;
1664 cc
= re_wctype (class_name
);
1666 error ("Invalid ISO C character class");
1668 iso_classes
= Fcons (make_number (cc
), iso_classes
);
1670 i_byte
= class_end
+ 2 - str
;
1674 not_a_class_name_multibyte
:
1677 if (i_byte
== size_byte
)
1680 leading_code
= str
[i_byte
];
1681 c
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1684 /* Treat `-' as range character only if another character
1686 if (i_byte
+ 1 < size_byte
1687 && str
[i_byte
] == '-')
1690 unsigned char leading_code2
;
1692 /* Skip over the dash. */
1695 /* Get the end of the range. */
1696 leading_code2
= str
[i_byte
];
1697 c2
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1701 && i_byte
< size_byte
)
1703 leading_code2
= str
[i_byte
];
1704 c2
=STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1710 if (ASCII_CHAR_P (c
))
1712 while (c
<= c2
&& c
< 0x80)
1714 leading_code
= CHAR_LEADING_CODE (c
);
1716 if (! ASCII_CHAR_P (c
))
1718 while (leading_code
<= leading_code2
)
1719 fastmap
[leading_code
++] = 1;
1722 char_ranges
[n_char_ranges
++] = c
;
1723 char_ranges
[n_char_ranges
++] = c2
;
1729 if (ASCII_CHAR_P (c
))
1733 fastmap
[leading_code
] = 1;
1734 char_ranges
[n_char_ranges
++] = c
;
1735 char_ranges
[n_char_ranges
++] = c
;
1740 /* If the current range is unibyte and STRING contains non-ASCII
1741 chars, arrange fastmap for the corresponding unibyte
1744 if (! multibyte
&& n_char_ranges
> 0)
1746 memset (fastmap
+ 0200, 0, 0200);
1747 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1749 int c1
= char_ranges
[i
];
1750 int c2
= char_ranges
[i
+ 1];
1752 for (; c1
<= c2
; c1
++)
1754 int b
= CHAR_TO_BYTE_SAFE (c1
);
1762 /* If ^ was the first character, complement the fastmap. */
1766 for (i
= 0; i
< sizeof fastmap
; i
++)
1770 for (i
= 0; i
< 0200; i
++)
1772 /* All non-ASCII chars possibly match. */
1773 for (; i
< sizeof fastmap
; i
++)
1779 EMACS_INT start_point
= PT
;
1781 EMACS_INT pos_byte
= PT_BYTE
;
1782 unsigned char *p
= PT_ADDR
, *endp
, *stop
;
1786 endp
= (XINT (lim
) == GPT
) ? GPT_ADDR
: CHAR_POS_ADDR (XINT (lim
));
1787 stop
= (pos
< GPT
&& GPT
< XINT (lim
)) ? GPT_ADDR
: endp
;
1791 endp
= CHAR_POS_ADDR (XINT (lim
));
1792 stop
= (pos
>= GPT
&& GPT
> XINT (lim
)) ? GAP_END_ADDR
: endp
;
1796 /* This code may look up syntax tables using macros that rely on the
1797 gl_state object. To make sure this object is not out of date,
1798 let's initialize it manually.
1799 We ignore syntax-table text-properties for now, since that's
1800 what we've done in the past. */
1801 SETUP_BUFFER_SYNTAX_TABLE ();
1816 c
= STRING_CHAR_AND_LENGTH (p
, nbytes
);
1817 if (! NILP (iso_classes
) && in_classes (c
, iso_classes
))
1827 if (! ASCII_CHAR_P (c
))
1829 /* As we are looking at a multibyte character, we
1830 must look up the character in the table
1831 CHAR_RANGES. If there's no data in the table,
1832 that character is not what we want to skip. */
1834 /* The following code do the right thing even if
1835 n_char_ranges is zero (i.e. no data in
1837 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1838 if (c
>= char_ranges
[i
] && c
<= char_ranges
[i
+ 1])
1840 if (!(negate
^ (i
< n_char_ranges
)))
1844 p
+= nbytes
, pos
++, pos_byte
+= nbytes
;
1857 if (!NILP (iso_classes
) && in_classes (*p
, iso_classes
))
1862 goto fwd_unibyte_ok
;
1868 p
++, pos
++, pos_byte
++;
1876 unsigned char *prev_p
;
1886 while (--p
>= stop
&& ! CHAR_HEAD_P (*p
));
1887 c
= STRING_CHAR (p
);
1889 if (! NILP (iso_classes
) && in_classes (c
, iso_classes
))
1899 if (! ASCII_CHAR_P (c
))
1901 /* See the comment in the previous similar code. */
1902 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1903 if (c
>= char_ranges
[i
] && c
<= char_ranges
[i
+ 1])
1905 if (!(negate
^ (i
< n_char_ranges
)))
1909 pos
--, pos_byte
-= prev_p
- p
;
1922 if (! NILP (iso_classes
) && in_classes (p
[-1], iso_classes
))
1927 goto back_unibyte_ok
;
1930 if (!fastmap
[p
[-1]])
1933 p
--, pos
--, pos_byte
--;
1937 SET_PT_BOTH (pos
, pos_byte
);
1940 return make_number (PT
- start_point
);
1946 skip_syntaxes (int forwardp
, Lisp_Object string
, Lisp_Object lim
)
1948 register unsigned int c
;
1949 unsigned char fastmap
[0400];
1951 register EMACS_INT i
, i_byte
;
1953 EMACS_INT size_byte
;
1956 CHECK_STRING (string
);
1959 XSETINT (lim
, forwardp
? ZV
: BEGV
);
1961 CHECK_NUMBER_COERCE_MARKER (lim
);
1963 /* In any case, don't allow scan outside bounds of buffer. */
1964 if (XINT (lim
) > ZV
)
1965 XSETFASTINT (lim
, ZV
);
1966 if (XINT (lim
) < BEGV
)
1967 XSETFASTINT (lim
, BEGV
);
1969 if (forwardp
? (PT
>= XFASTINT (lim
)) : (PT
<= XFASTINT (lim
)))
1970 return make_number (0);
1972 multibyte
= (!NILP (current_buffer
->enable_multibyte_characters
)
1973 && (XINT (lim
) - PT
!= CHAR_TO_BYTE (XINT (lim
)) - PT_BYTE
));
1975 memset (fastmap
, 0, sizeof fastmap
);
1977 if (SBYTES (string
) > SCHARS (string
))
1978 /* As this is very rare case (syntax spec is ASCII only), don't
1979 consider efficiency. */
1980 string
= string_make_unibyte (string
);
1982 str
= SDATA (string
);
1983 size_byte
= SBYTES (string
);
1986 if (i_byte
< size_byte
1987 && SREF (string
, 0) == '^')
1989 negate
= 1; i_byte
++;
1992 /* Find the syntaxes specified and set their elements of fastmap. */
1994 while (i_byte
< size_byte
)
1997 fastmap
[syntax_spec_code
[c
]] = 1;
2000 /* If ^ was the first character, complement the fastmap. */
2002 for (i
= 0; i
< sizeof fastmap
; i
++)
2006 EMACS_INT start_point
= PT
;
2008 EMACS_INT pos_byte
= PT_BYTE
;
2009 unsigned char *p
= PT_ADDR
, *endp
, *stop
;
2013 endp
= (XINT (lim
) == GPT
) ? GPT_ADDR
: CHAR_POS_ADDR (XINT (lim
));
2014 stop
= (pos
< GPT
&& GPT
< XINT (lim
)) ? GPT_ADDR
: endp
;
2018 endp
= CHAR_POS_ADDR (XINT (lim
));
2019 stop
= (pos
>= GPT
&& GPT
> XINT (lim
)) ? GAP_END_ADDR
: endp
;
2023 SETUP_SYNTAX_TABLE (pos
, forwardp
? 1 : -1);
2039 c
= STRING_CHAR_AND_LENGTH (p
, nbytes
);
2040 if (! fastmap
[(int) SYNTAX (c
)])
2042 p
+= nbytes
, pos
++, pos_byte
+= nbytes
;
2043 UPDATE_SYNTAX_TABLE_FORWARD (pos
);
2057 if (! fastmap
[(int) SYNTAX (*p
)])
2059 p
++, pos
++, pos_byte
++;
2060 UPDATE_SYNTAX_TABLE_FORWARD (pos
);
2070 unsigned char *prev_p
;
2079 UPDATE_SYNTAX_TABLE_BACKWARD (pos
- 1);
2081 while (--p
>= stop
&& ! CHAR_HEAD_P (*p
));
2082 c
= STRING_CHAR (p
);
2083 if (! fastmap
[(int) SYNTAX (c
)])
2085 pos
--, pos_byte
-= prev_p
- p
;
2099 UPDATE_SYNTAX_TABLE_BACKWARD (pos
- 1);
2100 if (! fastmap
[(int) SYNTAX (p
[-1])])
2102 p
--, pos
--, pos_byte
--;
2107 SET_PT_BOTH (pos
, pos_byte
);
2110 return make_number (PT
- start_point
);
2114 /* Return 1 if character C belongs to one of the ISO classes
2115 in the list ISO_CLASSES. Each class is represented by an
2116 integer which is its type according to re_wctype. */
2119 in_classes (int c
, Lisp_Object iso_classes
)
2123 while (CONSP (iso_classes
))
2126 elt
= XCAR (iso_classes
);
2127 iso_classes
= XCDR (iso_classes
);
2129 if (re_iswctype (c
, XFASTINT (elt
)))
2136 /* Jump over a comment, assuming we are at the beginning of one.
2137 FROM is the current position.
2138 FROM_BYTE is the bytepos corresponding to FROM.
2139 Do not move past STOP (a charpos).
2140 The comment over which we have to jump is of style STYLE
2141 (either SYNTAX_FLAGS_COMMENT_STYLE(foo) or ST_COMMENT_STYLE).
2142 NESTING should be positive to indicate the nesting at the beginning
2143 for nested comments and should be zero or negative else.
2144 ST_COMMENT_STYLE cannot be nested.
2145 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
2146 (or 0 If the search cannot start in the middle of a two-character).
2148 If successful, return 1 and store the charpos of the comment's end
2149 into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR.
2150 Else, return 0 and store the charpos STOP into *CHARPOS_PTR, the
2151 corresponding bytepos into *BYTEPOS_PTR and the current nesting
2152 (as defined for state.incomment) in *INCOMMENT_PTR.
2154 The comment end is the last character of the comment rather than the
2155 character just after the comment.
2157 Global syntax data is assumed to initially be valid for FROM and
2158 remains valid for forward search starting at the returned position. */
2161 forw_comment (EMACS_INT from
, EMACS_INT from_byte
, EMACS_INT stop
,
2162 int nesting
, int style
, int prev_syntax
,
2163 EMACS_INT
*charpos_ptr
, EMACS_INT
*bytepos_ptr
,
2167 register enum syntaxcode code
;
2168 register int syntax
, other_syntax
;
2170 if (nesting
<= 0) nesting
= -1;
2172 /* Enter the loop in the middle so that we find
2173 a 2-char comment ender if we start in the middle of it. */
2174 syntax
= prev_syntax
;
2175 if (syntax
!= 0) goto forw_incomment
;
2181 *incomment_ptr
= nesting
;
2182 *charpos_ptr
= from
;
2183 *bytepos_ptr
= from_byte
;
2186 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2187 syntax
= SYNTAX_WITH_FLAGS (c
);
2188 code
= syntax
& 0xff;
2189 if (code
== Sendcomment
2190 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == style
2191 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ?
2192 (nesting
> 0 && --nesting
== 0) : nesting
< 0))
2193 /* we have encountered a comment end of the same style
2194 as the comment sequence which began this comment
2197 if (code
== Scomment_fence
2198 && style
== ST_COMMENT_STYLE
)
2199 /* we have encountered a comment end of the same style
2200 as the comment sequence which began this comment
2205 && SYNTAX_FLAGS_COMMENT_NESTED (syntax
)
2206 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == style
)
2207 /* we have encountered a nested comment of the same style
2208 as the comment sequence which began this comment section */
2210 INC_BOTH (from
, from_byte
);
2211 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2214 if (from
< stop
&& SYNTAX_FLAGS_COMEND_FIRST (syntax
)
2215 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2216 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2217 SYNTAX_FLAGS_COMEND_SECOND (other_syntax
))
2218 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, other_syntax
) == style
2219 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ||
2220 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
))
2221 ? nesting
> 0 : nesting
< 0))
2224 /* we have encountered a comment end of the same style
2225 as the comment sequence which began this comment
2230 INC_BOTH (from
, from_byte
);
2231 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2236 && SYNTAX_FLAGS_COMSTART_FIRST (syntax
)
2237 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2238 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2239 SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
) == style
2240 && SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
))
2241 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ||
2242 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
)))
2243 /* we have encountered a nested comment of the same style
2244 as the comment sequence which began this comment
2247 INC_BOTH (from
, from_byte
);
2248 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2252 *charpos_ptr
= from
;
2253 *bytepos_ptr
= from_byte
;
2257 DEFUN ("forward-comment", Fforward_comment
, Sforward_comment
, 1, 1, 0,
2259 Move forward across up to COUNT comments. If COUNT is negative, move backward.
2260 Stop scanning if we find something other than a comment or whitespace.
2261 Set point to where scanning stops.
2262 If COUNT comments are found as expected, with nothing except whitespace
2263 between them, return t; otherwise return nil. */)
2266 register EMACS_INT from
;
2267 EMACS_INT from_byte
;
2268 register EMACS_INT stop
;
2270 register enum syntaxcode code
;
2271 int comstyle
= 0; /* style of comment encountered */
2272 int comnested
= 0; /* whether the comment is nestable or not */
2275 EMACS_INT out_charpos
, out_bytepos
;
2278 CHECK_NUMBER (count
);
2279 count1
= XINT (count
);
2280 stop
= count1
> 0 ? ZV
: BEGV
;
2286 from_byte
= PT_BYTE
;
2288 SETUP_SYNTAX_TABLE (from
, count1
);
2293 int comstart_first
, syntax
, other_syntax
;
2297 SET_PT_BOTH (from
, from_byte
);
2301 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2302 syntax
= SYNTAX_WITH_FLAGS (c
);
2304 comstart_first
= SYNTAX_FLAGS_COMSTART_FIRST (syntax
);
2305 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2306 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2307 INC_BOTH (from
, from_byte
);
2308 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2309 if (from
< stop
&& comstart_first
2310 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2311 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2312 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
)))
2314 /* We have encountered a comment start sequence and we
2315 are ignoring all text inside comments. We must record
2316 the comment style this sequence begins so that later,
2317 only a comment end of the same style actually ends
2318 the comment section. */
2320 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2322 = comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2323 INC_BOTH (from
, from_byte
);
2324 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2327 while (code
== Swhitespace
|| (code
== Sendcomment
&& c
== '\n'));
2329 if (code
== Scomment_fence
)
2330 comstyle
= ST_COMMENT_STYLE
;
2331 else if (code
!= Scomment
)
2334 DEC_BOTH (from
, from_byte
);
2335 SET_PT_BOTH (from
, from_byte
);
2338 /* We're at the start of a comment. */
2339 found
= forw_comment (from
, from_byte
, stop
, comnested
, comstyle
, 0,
2340 &out_charpos
, &out_bytepos
, &dummy
);
2341 from
= out_charpos
; from_byte
= out_bytepos
;
2345 SET_PT_BOTH (from
, from_byte
);
2348 INC_BOTH (from
, from_byte
);
2349 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2350 /* We have skipped one comment. */
2362 SET_PT_BOTH (BEGV
, BEGV_BYTE
);
2367 DEC_BOTH (from
, from_byte
);
2368 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
2369 quoted
= char_quoted (from
, from_byte
);
2370 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2371 syntax
= SYNTAX_WITH_FLAGS (c
);
2374 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2375 if (code
== Sendcomment
)
2376 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2377 if (from
> stop
&& SYNTAX_FLAGS_COMEND_SECOND (syntax
)
2378 && prev_char_comend_first (from
, from_byte
)
2379 && !char_quoted (from
- 1, dec_bytepos (from_byte
)))
2382 /* We must record the comment style encountered so that
2383 later, we can match only the proper comment begin
2384 sequence of the same style. */
2385 DEC_BOTH (from
, from_byte
);
2387 /* Calling char_quoted, above, set up global syntax position
2388 at the new value of FROM. */
2389 c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2390 other_syntax
= SYNTAX_WITH_FLAGS (c1
);
2391 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2393 = comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2396 if (code
== Scomment_fence
)
2398 /* Skip until first preceding unquoted comment_fence. */
2400 EMACS_INT ini
= from
, ini_byte
= from_byte
;
2404 DEC_BOTH (from
, from_byte
);
2405 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2406 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2407 if (SYNTAX (c
) == Scomment_fence
2408 && !char_quoted (from
, from_byte
))
2413 else if (from
== stop
)
2418 from
= ini
; /* Set point to ini + 1. */
2419 from_byte
= ini_byte
;
2423 /* We have skipped one comment. */
2426 else if (code
== Sendcomment
)
2428 found
= back_comment (from
, from_byte
, stop
, comnested
, comstyle
,
2429 &out_charpos
, &out_bytepos
);
2433 /* This end-of-line is not an end-of-comment.
2434 Treat it like a whitespace.
2435 CC-mode (and maybe others) relies on this behavior. */
2439 /* Failure: we should go back to the end of this
2440 not-quite-endcomment. */
2441 if (SYNTAX (c
) != code
)
2442 /* It was a two-char Sendcomment. */
2443 INC_BOTH (from
, from_byte
);
2449 /* We have skipped one comment. */
2450 from
= out_charpos
, from_byte
= out_bytepos
;
2454 else if (code
!= Swhitespace
|| quoted
)
2458 INC_BOTH (from
, from_byte
);
2459 SET_PT_BOTH (from
, from_byte
);
2467 SET_PT_BOTH (from
, from_byte
);
2472 /* Return syntax code of character C if C is an ASCII character
2473 or `multibyte_symbol_p' is zero. Otherwise, return Ssymbol. */
2475 #define SYNTAX_WITH_MULTIBYTE_CHECK(c) \
2476 ((ASCII_CHAR_P (c) || !multibyte_symbol_p) \
2477 ? SYNTAX (c) : Ssymbol)
2480 scan_lists (register EMACS_INT from
, EMACS_INT count
, EMACS_INT depth
, int sexpflag
)
2483 register EMACS_INT stop
= count
> 0 ? ZV
: BEGV
;
2488 register enum syntaxcode code
, temp_code
;
2489 int min_depth
= depth
; /* Err out if depth gets less than this. */
2490 int comstyle
= 0; /* style of comment encountered */
2491 int comnested
= 0; /* whether the comment is nestable or not */
2493 EMACS_INT last_good
= from
;
2495 EMACS_INT from_byte
;
2496 EMACS_INT out_bytepos
, out_charpos
;
2498 int multibyte_symbol_p
= sexpflag
&& multibyte_syntax_as_symbol
;
2500 if (depth
> 0) min_depth
= 0;
2502 if (from
> ZV
) from
= ZV
;
2503 if (from
< BEGV
) from
= BEGV
;
2505 from_byte
= CHAR_TO_BYTE (from
);
2510 SETUP_SYNTAX_TABLE (from
, count
);
2515 int comstart_first
, prefix
, syntax
, other_syntax
;
2516 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2517 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2518 syntax
= SYNTAX_WITH_FLAGS (c
);
2519 code
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2520 comstart_first
= SYNTAX_FLAGS_COMSTART_FIRST (syntax
);
2521 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2522 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2523 prefix
= SYNTAX_FLAGS_PREFIX (syntax
);
2524 if (depth
== min_depth
)
2526 INC_BOTH (from
, from_byte
);
2527 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2528 if (from
< stop
&& comstart_first
2529 && (c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2530 other_syntax
= SYNTAX_WITH_FLAGS (c
),
2531 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
))
2532 && parse_sexp_ignore_comments
)
2534 /* we have encountered a comment start sequence and we
2535 are ignoring all text inside comments. We must record
2536 the comment style this sequence begins so that later,
2537 only a comment end of the same style actually ends
2538 the comment section */
2540 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2542 = comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2543 INC_BOTH (from
, from_byte
);
2544 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2550 switch (SWITCH_ENUM_CAST (code
))
2556 INC_BOTH (from
, from_byte
);
2557 /* treat following character as a word constituent */
2560 if (depth
|| !sexpflag
) break;
2561 /* This word counts as a sexp; return at end of it. */
2564 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2566 /* Some compilers can't handle this inside the switch. */
2567 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2568 temp
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2573 INC_BOTH (from
, from_byte
);
2584 INC_BOTH (from
, from_byte
);
2588 case Scomment_fence
:
2589 comstyle
= ST_COMMENT_STYLE
;
2592 if (!parse_sexp_ignore_comments
) break;
2593 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2594 found
= forw_comment (from
, from_byte
, stop
,
2595 comnested
, comstyle
, 0,
2596 &out_charpos
, &out_bytepos
, &dummy
);
2597 from
= out_charpos
, from_byte
= out_bytepos
;
2604 INC_BOTH (from
, from_byte
);
2605 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2611 if (from
!= stop
&& c
== FETCH_CHAR_AS_MULTIBYTE (from_byte
))
2613 INC_BOTH (from
, from_byte
);
2623 if (!++depth
) goto done
;
2628 if (!--depth
) goto done
;
2629 if (depth
< min_depth
)
2630 xsignal3 (Qscan_error
,
2631 build_string ("Containing expression ends prematurely"),
2632 make_number (last_good
), make_number (from
));
2637 temp_pos
= dec_bytepos (from_byte
);
2638 stringterm
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2643 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2644 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2647 && SYNTAX_WITH_MULTIBYTE_CHECK (c
) == Sstring
)
2648 : SYNTAX_WITH_MULTIBYTE_CHECK (c
) == Sstring_fence
)
2651 /* Some compilers can't handle this inside the switch. */
2652 temp
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2657 INC_BOTH (from
, from_byte
);
2659 INC_BOTH (from
, from_byte
);
2661 INC_BOTH (from
, from_byte
);
2662 if (!depth
&& sexpflag
) goto done
;
2665 /* Ignore whitespace, punctuation, quote, endcomment. */
2670 /* Reached end of buffer. Error if within object, return nil if between */
2677 /* End of object reached */
2687 DEC_BOTH (from
, from_byte
);
2688 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2689 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2690 syntax
= SYNTAX_WITH_FLAGS (c
);
2691 code
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2692 if (depth
== min_depth
)
2695 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2696 if (code
== Sendcomment
)
2697 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2698 if (from
> stop
&& SYNTAX_FLAGS_COMEND_SECOND (syntax
)
2699 && prev_char_comend_first (from
, from_byte
)
2700 && parse_sexp_ignore_comments
)
2702 /* We must record the comment style encountered so that
2703 later, we can match only the proper comment begin
2704 sequence of the same style. */
2705 int c1
, other_syntax
;
2706 DEC_BOTH (from
, from_byte
);
2707 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2709 c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2710 other_syntax
= SYNTAX_WITH_FLAGS (c1
);
2711 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2713 = comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2716 /* Quoting turns anything except a comment-ender
2717 into a word character. Note that this cannot be true
2718 if we decremented FROM in the if-statement above. */
2719 if (code
!= Sendcomment
&& char_quoted (from
, from_byte
))
2721 DEC_BOTH (from
, from_byte
);
2724 else if (SYNTAX_FLAGS_PREFIX (syntax
))
2727 switch (SWITCH_ENUM_CAST (code
))
2733 if (depth
|| !sexpflag
) break;
2734 /* This word counts as a sexp; count object finished
2735 after passing it. */
2738 temp_pos
= from_byte
;
2739 if (! NILP (current_buffer
->enable_multibyte_characters
))
2743 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2744 c1
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2745 temp_code
= SYNTAX_WITH_MULTIBYTE_CHECK (c1
);
2746 /* Don't allow comment-end to be quoted. */
2747 if (temp_code
== Sendcomment
)
2749 quoted
= char_quoted (from
- 1, temp_pos
);
2752 DEC_BOTH (from
, from_byte
);
2753 temp_pos
= dec_bytepos (temp_pos
);
2754 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2756 c1
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2757 temp_code
= SYNTAX_WITH_MULTIBYTE_CHECK (c1
);
2758 if (! (quoted
|| temp_code
== Sword
2759 || temp_code
== Ssymbol
2760 || temp_code
== Squote
))
2762 DEC_BOTH (from
, from_byte
);
2769 temp_pos
= dec_bytepos (from_byte
);
2770 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2771 if (from
!= stop
&& c
== FETCH_CHAR_AS_MULTIBYTE (temp_pos
))
2772 DEC_BOTH (from
, from_byte
);
2781 if (!++depth
) goto done2
;
2786 if (!--depth
) goto done2
;
2787 if (depth
< min_depth
)
2788 xsignal3 (Qscan_error
,
2789 build_string ("Containing expression ends prematurely"),
2790 make_number (last_good
), make_number (from
));
2794 if (!parse_sexp_ignore_comments
)
2796 found
= back_comment (from
, from_byte
, stop
, comnested
, comstyle
,
2797 &out_charpos
, &out_bytepos
);
2798 /* FIXME: if found == -1, then it really wasn't a comment-end.
2799 For single-char Sendcomment, we can't do much about it apart
2800 from skipping the char.
2801 For 2-char endcomments, we could try again, taking both
2802 chars as separate entities, but it's a lot of trouble
2803 for very little gain, so we don't bother either. -sm */
2805 from
= out_charpos
, from_byte
= out_bytepos
;
2808 case Scomment_fence
:
2814 DEC_BOTH (from
, from_byte
);
2815 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2816 if (!char_quoted (from
, from_byte
)
2817 && (c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2818 SYNTAX_WITH_MULTIBYTE_CHECK (c
) == code
))
2821 if (code
== Sstring_fence
&& !depth
&& sexpflag
) goto done2
;
2825 stringterm
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2830 DEC_BOTH (from
, from_byte
);
2831 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2832 if (!char_quoted (from
, from_byte
)
2834 == (c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
)))
2835 && SYNTAX_WITH_MULTIBYTE_CHECK (c
) == Sstring
)
2838 if (!depth
&& sexpflag
) goto done2
;
2841 /* Ignore whitespace, punctuation, quote, endcomment. */
2846 /* Reached start of buffer. Error if within object, return nil if between */
2859 XSETFASTINT (val
, from
);
2863 xsignal3 (Qscan_error
,
2864 build_string ("Unbalanced parentheses"),
2865 make_number (last_good
), make_number (from
));
2868 DEFUN ("scan-lists", Fscan_lists
, Sscan_lists
, 3, 3, 0,
2869 doc
: /* Scan from character number FROM by COUNT lists.
2870 Returns the character number of the position thus found.
2872 If DEPTH is nonzero, paren depth begins counting from that value,
2873 only places where the depth in parentheses becomes zero
2874 are candidates for stopping; COUNT such places are counted.
2875 Thus, a positive value for DEPTH means go out levels.
2877 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2879 If the beginning or end of (the accessible part of) the buffer is reached
2880 and the depth is wrong, an error is signaled.
2881 If the depth is right but the count is not used up, nil is returned. */)
2882 (Lisp_Object from
, Lisp_Object count
, Lisp_Object depth
)
2884 CHECK_NUMBER (from
);
2885 CHECK_NUMBER (count
);
2886 CHECK_NUMBER (depth
);
2888 return scan_lists (XINT (from
), XINT (count
), XINT (depth
), 0);
2891 DEFUN ("scan-sexps", Fscan_sexps
, Sscan_sexps
, 2, 2, 0,
2892 doc
: /* Scan from character number FROM by COUNT balanced expressions.
2893 If COUNT is negative, scan backwards.
2894 Returns the character number of the position thus found.
2896 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2898 If the beginning or end of (the accessible part of) the buffer is reached
2899 in the middle of a parenthetical grouping, an error is signaled.
2900 If the beginning or end is reached between groupings
2901 but before count is used up, nil is returned. */)
2902 (Lisp_Object from
, Lisp_Object count
)
2904 CHECK_NUMBER (from
);
2905 CHECK_NUMBER (count
);
2907 return scan_lists (XINT (from
), XINT (count
), 0, 1);
2910 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars
, Sbackward_prefix_chars
,
2912 doc
: /* Move point backward over any number of chars with prefix syntax.
2913 This includes chars with "quote" or "prefix" syntax (' or p). */)
2916 EMACS_INT beg
= BEGV
;
2917 EMACS_INT opoint
= PT
;
2918 EMACS_INT opoint_byte
= PT_BYTE
;
2920 EMACS_INT pos_byte
= PT_BYTE
;
2925 SET_PT_BOTH (opoint
, opoint_byte
);
2930 SETUP_SYNTAX_TABLE (pos
, -1);
2932 DEC_BOTH (pos
, pos_byte
);
2934 while (!char_quoted (pos
, pos_byte
)
2935 /* Previous statement updates syntax table. */
2936 && ((c
= FETCH_CHAR_AS_MULTIBYTE (pos_byte
), SYNTAX (c
) == Squote
)
2937 || SYNTAX_PREFIX (c
)))
2940 opoint_byte
= pos_byte
;
2943 DEC_BOTH (pos
, pos_byte
);
2946 SET_PT_BOTH (opoint
, opoint_byte
);
2951 /* Parse forward from FROM / FROM_BYTE to END,
2952 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
2953 and return a description of the state of the parse at END.
2954 If STOPBEFORE is nonzero, stop at the start of an atom.
2955 If COMMENTSTOP is 1, stop at the start of a comment.
2956 If COMMENTSTOP is -1, stop at the start or end of a comment,
2957 after the beginning of a string, or after the end of a string. */
2960 scan_sexps_forward (struct lisp_parse_state
*stateptr
,
2961 EMACS_INT from
, EMACS_INT from_byte
, EMACS_INT end
,
2962 int targetdepth
, int stopbefore
,
2963 Lisp_Object oldstate
, int commentstop
)
2965 struct lisp_parse_state state
;
2967 register enum syntaxcode code
;
2970 struct level
{ int last
, prev
; };
2971 struct level levelstart
[100];
2972 register struct level
*curlevel
= levelstart
;
2973 struct level
*endlevel
= levelstart
+ 100;
2974 register int depth
; /* Paren depth of current scanning location.
2975 level - levelstart equals this except
2976 when the depth becomes negative. */
2977 int mindepth
; /* Lowest DEPTH value seen. */
2978 int start_quoted
= 0; /* Nonzero means starting after a char quote */
2980 EMACS_INT prev_from
; /* Keep one character before FROM. */
2981 EMACS_INT prev_from_byte
;
2982 int prev_from_syntax
;
2983 int boundary_stop
= commentstop
== -1;
2986 EMACS_INT out_bytepos
, out_charpos
;
2990 prev_from_byte
= from_byte
;
2992 DEC_BOTH (prev_from
, prev_from_byte
);
2994 /* Use this macro instead of `from++'. */
2996 do { prev_from = from; \
2997 prev_from_byte = from_byte; \
2998 temp = FETCH_CHAR_AS_MULTIBYTE (prev_from_byte); \
2999 prev_from_syntax = SYNTAX_WITH_FLAGS (temp); \
3000 INC_BOTH (from, from_byte); \
3002 UPDATE_SYNTAX_TABLE_FORWARD (from); \
3008 if (NILP (oldstate
))
3011 state
.instring
= -1;
3012 state
.incomment
= 0;
3013 state
.comstyle
= 0; /* comment style a by default. */
3014 state
.comstr_start
= -1; /* no comment/string seen. */
3018 tem
= Fcar (oldstate
);
3024 oldstate
= Fcdr (oldstate
);
3025 oldstate
= Fcdr (oldstate
);
3026 oldstate
= Fcdr (oldstate
);
3027 tem
= Fcar (oldstate
);
3028 /* Check whether we are inside string_fence-style string: */
3029 state
.instring
= (!NILP (tem
)
3030 ? (INTEGERP (tem
) ? XINT (tem
) : ST_STRING_STYLE
)
3033 oldstate
= Fcdr (oldstate
);
3034 tem
= Fcar (oldstate
);
3035 state
.incomment
= (!NILP (tem
)
3036 ? (INTEGERP (tem
) ? XINT (tem
) : -1)
3039 oldstate
= Fcdr (oldstate
);
3040 tem
= Fcar (oldstate
);
3041 start_quoted
= !NILP (tem
);
3043 /* if the eighth element of the list is nil, we are in comment
3044 style a. If it is non-nil, we are in comment style b */
3045 oldstate
= Fcdr (oldstate
);
3046 oldstate
= Fcdr (oldstate
);
3047 tem
= Fcar (oldstate
);
3048 state
.comstyle
= (NILP (tem
)
3050 : (EQ (tem
, Qsyntax_table
)
3052 : INTEGERP (tem
) ? XINT (tem
) : 1));
3054 oldstate
= Fcdr (oldstate
);
3055 tem
= Fcar (oldstate
);
3056 state
.comstr_start
= NILP (tem
) ? -1 : XINT (tem
) ;
3057 oldstate
= Fcdr (oldstate
);
3058 tem
= Fcar (oldstate
);
3059 while (!NILP (tem
)) /* >= second enclosing sexps. */
3061 /* curlevel++->last ran into compiler bug on Apollo */
3062 curlevel
->last
= XINT (Fcar (tem
));
3063 if (++curlevel
== endlevel
)
3064 curlevel
--; /* error ("Nesting too deep for parser"); */
3065 curlevel
->prev
= -1;
3066 curlevel
->last
= -1;
3073 curlevel
->prev
= -1;
3074 curlevel
->last
= -1;
3076 SETUP_SYNTAX_TABLE (prev_from
, 1);
3077 temp
= FETCH_CHAR (prev_from_byte
);
3078 prev_from_syntax
= SYNTAX_WITH_FLAGS (temp
);
3079 UPDATE_SYNTAX_TABLE_FORWARD (from
);
3081 /* Enter the loop at a place appropriate for initial state. */
3083 if (state
.incomment
)
3084 goto startincomment
;
3085 if (state
.instring
>= 0)
3087 nofence
= state
.instring
!= ST_STRING_STYLE
;
3089 goto startquotedinstring
;
3092 else if (start_quoted
)
3099 code
= prev_from_syntax
& 0xff;
3102 && SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax
)
3103 && (c1
= FETCH_CHAR (from_byte
),
3104 syntax
= SYNTAX_WITH_FLAGS (c1
),
3105 SYNTAX_FLAGS_COMSTART_SECOND (syntax
)))
3106 /* Duplicate code to avoid a complex if-expression
3107 which causes trouble for the SGI compiler. */
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. */
3113 = SYNTAX_FLAGS_COMMENT_STYLE (syntax
, prev_from_syntax
);
3114 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax
);
3115 comnested
= comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
3116 state
.incomment
= comnested
? 1 : -1;
3117 state
.comstr_start
= prev_from
;
3121 else if (code
== Scomment_fence
)
3123 /* Record the comment style we have entered so that only
3124 the comment-end sequence of the same style actually
3125 terminates the comment section. */
3126 state
.comstyle
= ST_COMMENT_STYLE
;
3127 state
.incomment
= -1;
3128 state
.comstr_start
= prev_from
;
3131 else if (code
== Scomment
)
3133 state
.comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax
, 0);
3134 state
.incomment
= (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax
) ?
3136 state
.comstr_start
= prev_from
;
3139 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax
))
3141 switch (SWITCH_ENUM_CAST (code
))
3145 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3146 curlevel
->last
= prev_from
;
3148 if (from
== end
) goto endquoted
;
3151 /* treat following character as a word constituent */
3154 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3155 curlevel
->last
= prev_from
;
3159 /* Some compilers can't handle this inside the switch. */
3160 temp
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
3161 temp
= SYNTAX (temp
);
3167 if (from
== end
) goto endquoted
;
3179 curlevel
->prev
= curlevel
->last
;
3182 case Scomment_fence
: /* Can't happen because it's handled above. */
3184 if (commentstop
|| boundary_stop
) goto done
;
3186 /* The (from == BEGV) test was to enter the loop in the middle so
3187 that we find a 2-char comment ender even if we start in the
3188 middle of it. We don't want to do that if we're just at the
3189 beginning of the comment (think of (*) ... (*)). */
3190 found
= forw_comment (from
, from_byte
, end
,
3191 state
.incomment
, state
.comstyle
,
3192 (from
== BEGV
|| from
< state
.comstr_start
+ 3)
3193 ? 0 : prev_from_syntax
,
3194 &out_charpos
, &out_bytepos
, &state
.incomment
);
3195 from
= out_charpos
; from_byte
= out_bytepos
;
3196 /* Beware! prev_from and friends are invalid now.
3197 Luckily, the `done' doesn't use them and the INC_FROM
3198 sets them to a sane value without looking at them. */
3199 if (!found
) goto done
;
3201 state
.incomment
= 0;
3202 state
.comstyle
= 0; /* reset the comment style */
3203 if (boundary_stop
) goto done
;
3207 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3209 /* curlevel++->last ran into compiler bug on Apollo */
3210 curlevel
->last
= prev_from
;
3211 if (++curlevel
== endlevel
)
3212 curlevel
--; /* error ("Nesting too deep for parser"); */
3213 curlevel
->prev
= -1;
3214 curlevel
->last
= -1;
3215 if (targetdepth
== depth
) goto done
;
3220 if (depth
< mindepth
)
3222 if (curlevel
!= levelstart
)
3224 curlevel
->prev
= curlevel
->last
;
3225 if (targetdepth
== depth
) goto done
;
3230 state
.comstr_start
= from
- 1;
3231 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3232 curlevel
->last
= prev_from
;
3233 state
.instring
= (code
== Sstring
3234 ? (FETCH_CHAR_AS_MULTIBYTE (prev_from_byte
))
3236 if (boundary_stop
) goto done
;
3239 nofence
= state
.instring
!= ST_STRING_STYLE
;
3245 if (from
>= end
) goto done
;
3246 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
3247 /* Some compilers can't handle this inside the switch. */
3250 /* Check TEMP here so that if the char has
3251 a syntax-table property which says it is NOT
3252 a string character, it does not end the string. */
3253 if (nofence
&& c
== state
.instring
&& temp
== Sstring
)
3259 if (!nofence
) goto string_end
;
3264 startquotedinstring
:
3265 if (from
>= end
) goto endquoted
;
3271 state
.instring
= -1;
3272 curlevel
->prev
= curlevel
->last
;
3274 if (boundary_stop
) goto done
;
3278 /* FIXME: We should do something with it. */
3281 /* Ignore whitespace, punctuation, quote, endcomment. */
3287 stop
: /* Here if stopping before start of sexp. */
3288 from
= prev_from
; /* We have just fetched the char that starts it; */
3289 goto done
; /* but return the position before it. */
3294 state
.depth
= depth
;
3295 state
.mindepth
= mindepth
;
3296 state
.thislevelstart
= curlevel
->prev
;
3297 state
.prevlevelstart
3298 = (curlevel
== levelstart
) ? -1 : (curlevel
- 1)->last
;
3299 state
.location
= from
;
3300 state
.levelstarts
= Qnil
;
3301 while (--curlevel
>= levelstart
)
3302 state
.levelstarts
= Fcons (make_number (curlevel
->last
),
3309 DEFUN ("parse-partial-sexp", Fparse_partial_sexp
, Sparse_partial_sexp
, 2, 6, 0,
3310 doc
: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
3311 Parsing stops at TO or when certain criteria are met;
3312 point is set to where parsing stops.
3313 If fifth arg OLDSTATE is omitted or nil,
3314 parsing assumes that FROM is the beginning of a function.
3315 Value is a list of elements describing final state of parsing:
3317 1. character address of start of innermost containing list; nil if none.
3318 2. character address of start of last complete sexp terminated.
3319 3. non-nil if inside a string.
3320 (it is the character that will terminate the string,
3321 or t if the string should be terminated by a generic string delimiter.)
3322 4. nil if outside a comment, t if inside a non-nestable comment,
3323 else an integer (the current comment nesting).
3324 5. t if following a quote character.
3325 6. the minimum paren-depth encountered during this scan.
3326 7. style of comment, if any.
3327 8. character address of start of comment or string; nil if not in one.
3328 9. Intermediate data for continuation of parsing (subject to change).
3329 If third arg TARGETDEPTH is non-nil, parsing stops if the depth
3330 in parentheses becomes equal to TARGETDEPTH.
3331 Fourth arg STOPBEFORE non-nil means stop when come to
3332 any character that starts a sexp.
3333 Fifth arg OLDSTATE is a list like what this function returns.
3334 It is used to initialize the state of the parse. Elements number 1, 2, 6
3336 Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.
3337 If it is symbol `syntax-table', stop after the start of a comment or a
3338 string, or after end of a comment or a string. */)
3339 (Lisp_Object from
, Lisp_Object to
, Lisp_Object targetdepth
, Lisp_Object stopbefore
, Lisp_Object oldstate
, Lisp_Object commentstop
)
3341 struct lisp_parse_state state
;
3344 if (!NILP (targetdepth
))
3346 CHECK_NUMBER (targetdepth
);
3347 target
= XINT (targetdepth
);
3350 target
= -100000; /* We won't reach this depth */
3352 validate_region (&from
, &to
);
3353 scan_sexps_forward (&state
, XINT (from
), CHAR_TO_BYTE (XINT (from
)),
3355 target
, !NILP (stopbefore
), oldstate
,
3357 ? 0 : (EQ (commentstop
, Qsyntax_table
) ? -1 : 1)));
3359 SET_PT (state
.location
);
3361 return Fcons (make_number (state
.depth
),
3362 Fcons (state
.prevlevelstart
< 0
3363 ? Qnil
: make_number (state
.prevlevelstart
),
3364 Fcons (state
.thislevelstart
< 0
3365 ? Qnil
: make_number (state
.thislevelstart
),
3366 Fcons (state
.instring
>= 0
3367 ? (state
.instring
== ST_STRING_STYLE
3368 ? Qt
: make_number (state
.instring
)) : Qnil
,
3369 Fcons (state
.incomment
< 0 ? Qt
:
3370 (state
.incomment
== 0 ? Qnil
:
3371 make_number (state
.incomment
)),
3372 Fcons (state
.quoted
? Qt
: Qnil
,
3373 Fcons (make_number (state
.mindepth
),
3374 Fcons ((state
.comstyle
3375 ? (state
.comstyle
== ST_COMMENT_STYLE
3377 : make_number (state
.comstyle
))
3379 Fcons (((state
.incomment
3380 || (state
.instring
>= 0))
3381 ? make_number (state
.comstr_start
)
3383 Fcons (state
.levelstarts
, Qnil
))))))))));
3387 init_syntax_once (void)
3392 /* This has to be done here, before we call Fmake_char_table. */
3393 Qsyntax_table
= intern_c_string ("syntax-table");
3394 staticpro (&Qsyntax_table
);
3396 /* Intern_C_String this now in case it isn't already done.
3397 Setting this variable twice is harmless.
3398 But don't staticpro it here--that is done in alloc.c. */
3399 Qchar_table_extra_slots
= intern_c_string ("char-table-extra-slots");
3401 /* Create objects which can be shared among syntax tables. */
3402 Vsyntax_code_object
= Fmake_vector (make_number (Smax
), Qnil
);
3403 for (i
= 0; i
< XVECTOR (Vsyntax_code_object
)->size
; i
++)
3404 XVECTOR (Vsyntax_code_object
)->contents
[i
]
3405 = Fcons (make_number (i
), Qnil
);
3407 /* Now we are ready to set up this property, so we can
3408 create syntax tables. */
3409 Fput (Qsyntax_table
, Qchar_table_extra_slots
, make_number (0));
3411 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Swhitespace
];
3413 Vstandard_syntax_table
= Fmake_char_table (Qsyntax_table
, temp
);
3415 /* Control characters should not be whitespace. */
3416 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Spunct
];
3417 for (i
= 0; i
<= ' ' - 1; i
++)
3418 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3419 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 0177, temp
);
3421 /* Except that a few really are whitespace. */
3422 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Swhitespace
];
3423 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ' ', temp
);
3424 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\t', temp
);
3425 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\n', temp
);
3426 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 015, temp
);
3427 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 014, temp
);
3429 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Sword
];
3430 for (i
= 'a'; i
<= 'z'; i
++)
3431 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3432 for (i
= 'A'; i
<= 'Z'; i
++)
3433 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3434 for (i
= '0'; i
<= '9'; i
++)
3435 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3437 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '$', temp
);
3438 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '%', temp
);
3440 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '(',
3441 Fcons (make_number (Sopen
), make_number (')')));
3442 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ')',
3443 Fcons (make_number (Sclose
), make_number ('(')));
3444 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '[',
3445 Fcons (make_number (Sopen
), make_number (']')));
3446 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ']',
3447 Fcons (make_number (Sclose
), make_number ('[')));
3448 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '{',
3449 Fcons (make_number (Sopen
), make_number ('}')));
3450 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '}',
3451 Fcons (make_number (Sclose
), make_number ('{')));
3452 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '"',
3453 Fcons (make_number ((int) Sstring
), Qnil
));
3454 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\\',
3455 Fcons (make_number ((int) Sescape
), Qnil
));
3457 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Ssymbol
];
3458 for (i
= 0; i
< 10; i
++)
3460 c
= "_-+*/&|<>="[i
];
3461 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, c
, temp
);
3464 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Spunct
];
3465 for (i
= 0; i
< 12; i
++)
3467 c
= ".,;:?!#@~^'`"[i
];
3468 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, c
, temp
);
3471 /* All multibyte characters have syntax `word' by default. */
3472 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Sword
];
3473 char_table_set_range (Vstandard_syntax_table
, 0x80, MAX_CHAR
, temp
);
3477 syms_of_syntax (void)
3479 Qsyntax_table_p
= intern_c_string ("syntax-table-p");
3480 staticpro (&Qsyntax_table_p
);
3482 staticpro (&Vsyntax_code_object
);
3484 staticpro (&gl_state
.object
);
3485 staticpro (&gl_state
.global_code
);
3486 staticpro (&gl_state
.current_syntax_table
);
3487 staticpro (&gl_state
.old_prop
);
3489 /* Defined in regex.c */
3490 staticpro (&re_match_object
);
3492 Qscan_error
= intern_c_string ("scan-error");
3493 staticpro (&Qscan_error
);
3494 Fput (Qscan_error
, Qerror_conditions
,
3495 pure_cons (Qscan_error
, pure_cons (Qerror
, Qnil
)));
3496 Fput (Qscan_error
, Qerror_message
,
3497 make_pure_c_string ("Scan error"));
3499 DEFVAR_BOOL ("parse-sexp-ignore-comments", &parse_sexp_ignore_comments
,
3500 doc
: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
3502 DEFVAR_BOOL ("parse-sexp-lookup-properties", &parse_sexp_lookup_properties
,
3503 doc
: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3504 Otherwise, that text property is simply ignored.
3505 See the info node `(elisp)Syntax Properties' for a description of the
3506 `syntax-table' property. */);
3508 words_include_escapes
= 0;
3509 DEFVAR_BOOL ("words-include-escapes", &words_include_escapes
,
3510 doc
: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
3512 DEFVAR_BOOL ("multibyte-syntax-as-symbol", &multibyte_syntax_as_symbol
,
3513 doc
: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
3514 multibyte_syntax_as_symbol
= 0;
3516 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3517 &open_paren_in_column_0_is_defun_start
,
3518 doc
: /* *Non-nil means an open paren in column 0 denotes the start of a defun. */);
3519 open_paren_in_column_0_is_defun_start
= 1;
3522 DEFVAR_LISP ("find-word-boundary-function-table",
3523 &Vfind_word_boundary_function_table
,
3525 Char table of functions to search for the word boundary.
3526 Each function is called with two arguments; POS and LIMIT.
3527 POS and LIMIT are character positions in the current buffer.
3529 If POS is less than LIMIT, POS is at the first character of a word,
3530 and the return value of a function is a position after the last
3531 character of that word.
3533 If POS is not less than LIMIT, POS is at the last character of a word,
3534 and the return value of a function is a position at the first
3535 character of that word.
3537 In both cases, LIMIT bounds the search. */);
3538 Vfind_word_boundary_function_table
= Fmake_char_table (Qnil
, Qnil
);
3540 defsubr (&Ssyntax_table_p
);
3541 defsubr (&Ssyntax_table
);
3542 defsubr (&Sstandard_syntax_table
);
3543 defsubr (&Scopy_syntax_table
);
3544 defsubr (&Sset_syntax_table
);
3545 defsubr (&Schar_syntax
);
3546 defsubr (&Smatching_paren
);
3547 defsubr (&Sstring_to_syntax
);
3548 defsubr (&Smodify_syntax_entry
);
3549 defsubr (&Sinternal_describe_syntax_value
);
3551 defsubr (&Sforward_word
);
3553 defsubr (&Sskip_chars_forward
);
3554 defsubr (&Sskip_chars_backward
);
3555 defsubr (&Sskip_syntax_forward
);
3556 defsubr (&Sskip_syntax_backward
);
3558 defsubr (&Sforward_comment
);
3559 defsubr (&Sscan_lists
);
3560 defsubr (&Sscan_sexps
);
3561 defsubr (&Sbackward_prefix_chars
);
3562 defsubr (&Sparse_partial_sexp
);
3565 /* arch-tag: 3e297b9f-088e-4b64-8f4c-fb0b3443e412
3566 (do not change this comment) */