1 /* GNU Emacs routines to deal with syntax tables; also word and list parsing.
2 Copyright (C) 1985, 1987, 1993-1995, 1997-1999, 2001-2011
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
27 #include "character.h"
31 /* Make syntax table lookup grant data in gl_state. */
32 #define SYNTAX_ENTRY_VIA_PROPERTY
35 #include "intervals.h"
38 /* Then there are seven single-bit flags that have the following meanings:
39 1. This character is the first of a two-character comment-start sequence.
40 2. This character is the second of a two-character comment-start sequence.
41 3. This character is the first of a two-character comment-end sequence.
42 4. This character is the second of a two-character comment-end sequence.
43 5. This character is a prefix, for backward-prefix-chars.
44 6. The char is part of a delimiter for comments of style "b".
45 7. This character is part of a nestable comment sequence.
46 8. The char is part of a delimiter for comments of style "c".
47 Note that any two-character sequence whose first character has flag 1
48 and whose second character has flag 2 will be interpreted as a comment start.
50 bit 6 and 8 are used to discriminate between different comment styles.
51 Languages such as C++ allow two orthogonal syntax start/end pairs
52 and bit 6 is used to determine whether a comment-end or Scommentend
53 ends style a or b. Comment markers can start style a, b, c, or bc.
54 Style a is always the default.
55 For 2-char comment markers, the style b flag is only looked up on the second
56 char of the comment marker and on the first char of the comment ender.
57 For style c (like to for the nested flag), the flag can be placed on any
61 /* These macros extract specific flags from an integer
62 that holds the syntax code and the flags. */
64 #define SYNTAX_FLAGS_COMSTART_FIRST(flags) (((flags) >> 16) & 1)
66 #define SYNTAX_FLAGS_COMSTART_SECOND(flags) (((flags) >> 17) & 1)
68 #define SYNTAX_FLAGS_COMEND_FIRST(flags) (((flags) >> 18) & 1)
70 #define SYNTAX_FLAGS_COMEND_SECOND(flags) (((flags) >> 19) & 1)
72 #define SYNTAX_FLAGS_PREFIX(flags) (((flags) >> 20) & 1)
74 #define SYNTAX_FLAGS_COMMENT_STYLEB(flags) (((flags) >> 21) & 1)
75 #define SYNTAX_FLAGS_COMMENT_STYLEC(flags) (((flags) >> 22) & 2)
76 /* FLAGS should be the flags of the main char of the comment marker, e.g.
77 the second for comstart and the first for comend. */
78 #define SYNTAX_FLAGS_COMMENT_STYLE(flags, other_flags) \
79 (SYNTAX_FLAGS_COMMENT_STYLEB (flags) \
80 | SYNTAX_FLAGS_COMMENT_STYLEC (flags) \
81 | SYNTAX_FLAGS_COMMENT_STYLEC (other_flags))
83 #define SYNTAX_FLAGS_COMMENT_NESTED(flags) (((flags) >> 22) & 1)
85 /* These macros extract a particular flag for a given character. */
87 #define SYNTAX_COMEND_FIRST(c) \
88 (SYNTAX_FLAGS_COMEND_FIRST (SYNTAX_WITH_FLAGS (c)))
89 #define SYNTAX_PREFIX(c) (SYNTAX_FLAGS_PREFIX (SYNTAX_WITH_FLAGS (c)))
91 /* We use these constants in place for comment-style and
92 string-ender-char to distinguish comments/strings started by
93 comment_fence and string_fence codes. */
95 #define ST_COMMENT_STYLE (256 + 1)
96 #define ST_STRING_STYLE (256 + 2)
98 Lisp_Object Qsyntax_table_p
, Qsyntax_table
, Qscan_error
;
100 /* Used as a temporary in SYNTAX_ENTRY and other macros in syntax.h,
101 if not compiled with GCC. No need to mark it, since it is used
102 only very temporarily. */
103 Lisp_Object syntax_temp
;
105 /* This is the internal form of the parse state used in parse-partial-sexp. */
107 struct lisp_parse_state
109 int depth
; /* Depth at end of parsing. */
110 int instring
; /* -1 if not within string, else desired terminator. */
111 int incomment
; /* -1 if in unnestable comment else comment nesting */
112 int comstyle
; /* comment style a=0, or b=1, or ST_COMMENT_STYLE. */
113 int quoted
; /* Nonzero if just after an escape char at end of parsing */
114 int mindepth
; /* Minimum depth seen while scanning. */
115 /* Char number of most recent start-of-expression at current level */
116 EMACS_INT thislevelstart
;
117 /* Char number of start of containing expression */
118 EMACS_INT prevlevelstart
;
119 EMACS_INT location
; /* Char number at which parsing stopped. */
120 EMACS_INT comstr_start
; /* Position of last comment/string starter. */
121 Lisp_Object levelstarts
; /* Char numbers of starts-of-expression
122 of levels (starting from outermost). */
125 /* These variables are a cache for finding the start of a defun.
126 find_start_pos is the place for which the defun start was found.
127 find_start_value is the defun start position found for it.
128 find_start_value_byte is the corresponding byte position.
129 find_start_buffer is the buffer it was found in.
130 find_start_begv is the BEGV value when it was found.
131 find_start_modiff is the value of MODIFF when it was found. */
133 static EMACS_INT find_start_pos
;
134 static EMACS_INT find_start_value
;
135 static EMACS_INT find_start_value_byte
;
136 static struct buffer
*find_start_buffer
;
137 static EMACS_INT find_start_begv
;
138 static int find_start_modiff
;
141 INFUN (Fsyntax_table_p
, 1);
142 static Lisp_Object
skip_chars (int, Lisp_Object
, Lisp_Object
, int);
143 static Lisp_Object
skip_syntaxes (int, Lisp_Object
, Lisp_Object
);
144 static Lisp_Object
scan_lists (EMACS_INT
, EMACS_INT
, EMACS_INT
, int);
145 static void scan_sexps_forward (struct lisp_parse_state
*,
146 EMACS_INT
, EMACS_INT
, EMACS_INT
, int,
147 int, Lisp_Object
, int);
148 static int in_classes (int, Lisp_Object
);
150 /* Whether the syntax of the character C has the prefix flag set. */
151 int syntax_prefix_flag_p (int c
)
153 return SYNTAX_PREFIX (c
);
156 struct gl_state_s gl_state
; /* Global state of syntax parser. */
158 #define INTERVALS_AT_ONCE 10 /* 1 + max-number of intervals
159 to scan to property-change. */
161 /* Update gl_state to an appropriate interval which contains CHARPOS. The
162 sign of COUNT give the relative position of CHARPOS wrt the previously
163 valid interval. If INIT, only [be]_property fields of gl_state are
164 valid at start, the rest is filled basing on OBJECT.
166 `gl_state.*_i' are the intervals, and CHARPOS is further in the search
167 direction than the intervals - or in an interval. We update the
168 current syntax-table basing on the property of this interval, and
169 update the interval to start further than CHARPOS - or be
170 NULL_INTERVAL. We also update lim_property to be the next value of
171 charpos to call this subroutine again - or be before/after the
172 start/end of OBJECT. */
175 update_syntax_table (EMACS_INT charpos
, int count
, int init
,
178 Lisp_Object tmp_table
;
185 gl_state
.old_prop
= Qnil
;
186 gl_state
.start
= gl_state
.b_property
;
187 gl_state
.stop
= gl_state
.e_property
;
188 i
= interval_of (charpos
, object
);
189 gl_state
.backward_i
= gl_state
.forward_i
= i
;
191 if (NULL_INTERVAL_P (i
))
193 /* interval_of updates only ->position of the return value, so
194 update the parents manually to speed up update_interval. */
195 while (!NULL_PARENT (i
))
197 if (AM_RIGHT_CHILD (i
))
198 INTERVAL_PARENT (i
)->position
= i
->position
199 - LEFT_TOTAL_LENGTH (i
) + TOTAL_LENGTH (i
) /* right end */
200 - TOTAL_LENGTH (INTERVAL_PARENT (i
))
201 + LEFT_TOTAL_LENGTH (INTERVAL_PARENT (i
));
203 INTERVAL_PARENT (i
)->position
= i
->position
- LEFT_TOTAL_LENGTH (i
)
205 i
= INTERVAL_PARENT (i
);
207 i
= gl_state
.forward_i
;
208 gl_state
.b_property
= i
->position
- gl_state
.offset
;
209 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
212 i
= count
> 0 ? gl_state
.forward_i
: gl_state
.backward_i
;
214 /* We are guaranteed to be called with CHARPOS either in i,
216 if (NULL_INTERVAL_P (i
))
217 error ("Error in syntax_table logic for to-the-end intervals");
218 else if (charpos
< i
->position
) /* Move left. */
221 error ("Error in syntax_table logic for intervals <-");
222 /* Update the interval. */
223 i
= update_interval (i
, charpos
);
224 if (INTERVAL_LAST_POS (i
) != gl_state
.b_property
)
227 gl_state
.forward_i
= i
;
228 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
231 else if (charpos
>= INTERVAL_LAST_POS (i
)) /* Move right. */
234 error ("Error in syntax_table logic for intervals ->");
235 /* Update the interval. */
236 i
= update_interval (i
, charpos
);
237 if (i
->position
!= gl_state
.e_property
)
240 gl_state
.backward_i
= i
;
241 gl_state
.b_property
= i
->position
- gl_state
.offset
;
246 tmp_table
= textget (i
->plist
, Qsyntax_table
);
249 invalidate
= !EQ (tmp_table
, gl_state
.old_prop
); /* Need to invalidate? */
251 if (invalidate
) /* Did not get to adjacent interval. */
252 { /* with the same table => */
253 /* invalidate the old range. */
256 gl_state
.backward_i
= i
;
257 gl_state
.b_property
= i
->position
- gl_state
.offset
;
261 gl_state
.forward_i
= i
;
262 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
266 if (!EQ (tmp_table
, gl_state
.old_prop
))
268 gl_state
.current_syntax_table
= tmp_table
;
269 gl_state
.old_prop
= tmp_table
;
270 if (EQ (Fsyntax_table_p (tmp_table
), Qt
))
272 gl_state
.use_global
= 0;
274 else if (CONSP (tmp_table
))
276 gl_state
.use_global
= 1;
277 gl_state
.global_code
= tmp_table
;
281 gl_state
.use_global
= 0;
282 gl_state
.current_syntax_table
= BVAR (current_buffer
, syntax_table
);
286 while (!NULL_INTERVAL_P (i
))
288 if (cnt
&& !EQ (tmp_table
, textget (i
->plist
, Qsyntax_table
)))
292 gl_state
.e_property
= i
->position
- gl_state
.offset
;
293 gl_state
.forward_i
= i
;
298 = i
->position
+ LENGTH (i
) - gl_state
.offset
;
299 gl_state
.backward_i
= i
;
303 else if (cnt
== INTERVALS_AT_ONCE
)
308 = i
->position
+ LENGTH (i
) - gl_state
.offset
309 /* e_property at EOB is not set to ZV but to ZV+1, so that
310 we can do INC(from);UPDATE_SYNTAX_TABLE_FORWARD without
311 having to check eob between the two. */
312 + (NULL_INTERVAL_P (next_interval (i
)) ? 1 : 0);
313 gl_state
.forward_i
= i
;
317 gl_state
.b_property
= i
->position
- gl_state
.offset
;
318 gl_state
.backward_i
= i
;
323 i
= count
> 0 ? next_interval (i
) : previous_interval (i
);
325 eassert (NULL_INTERVAL_P (i
)); /* This property goes to the end. */
327 gl_state
.e_property
= gl_state
.stop
;
329 gl_state
.b_property
= gl_state
.start
;
332 /* Returns TRUE if char at CHARPOS is quoted.
333 Global syntax-table data should be set up already to be good at CHARPOS
334 or after. On return global syntax data is good for lookup at CHARPOS. */
337 char_quoted (EMACS_INT charpos
, EMACS_INT bytepos
)
339 register enum syntaxcode code
;
340 register EMACS_INT beg
= BEGV
;
341 register int quoted
= 0;
342 EMACS_INT orig
= charpos
;
344 while (charpos
> beg
)
347 DEC_BOTH (charpos
, bytepos
);
349 UPDATE_SYNTAX_TABLE_BACKWARD (charpos
);
350 c
= FETCH_CHAR_AS_MULTIBYTE (bytepos
);
352 if (! (code
== Scharquote
|| code
== Sescape
))
358 UPDATE_SYNTAX_TABLE (orig
);
362 /* Return the bytepos one character before BYTEPOS.
363 We assume that BYTEPOS is not at the start of the buffer. */
365 static INLINE EMACS_INT
366 dec_bytepos (EMACS_INT bytepos
)
368 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
375 /* Return a defun-start position before POS and not too far before.
376 It should be the last one before POS, or nearly the last.
378 When open_paren_in_column_0_is_defun_start is nonzero,
379 only the beginning of the buffer is treated as a defun-start.
381 We record the information about where the scan started
382 and what its result was, so that another call in the same area
383 can return the same value very quickly.
385 There is no promise at which position the global syntax data is
386 valid on return from the subroutine, so the caller should explicitly
387 update the global data. */
390 find_defun_start (EMACS_INT pos
, EMACS_INT pos_byte
)
392 EMACS_INT opoint
= PT
, opoint_byte
= PT_BYTE
;
394 if (!open_paren_in_column_0_is_defun_start
)
396 find_start_value_byte
= BEGV_BYTE
;
400 /* Use previous finding, if it's valid and applies to this inquiry. */
401 if (current_buffer
== find_start_buffer
402 /* Reuse the defun-start even if POS is a little farther on.
403 POS might be in the next defun, but that's ok.
404 Our value may not be the best possible, but will still be usable. */
405 && pos
<= find_start_pos
+ 1000
406 && pos
>= find_start_value
407 && BEGV
== find_start_begv
408 && MODIFF
== find_start_modiff
)
409 return find_start_value
;
411 /* Back up to start of line. */
412 scan_newline (pos
, pos_byte
, BEGV
, BEGV_BYTE
, -1, 1);
414 /* We optimize syntax-table lookup for rare updates. Thus we accept
415 only those `^\s(' which are good in global _and_ text-property
417 SETUP_BUFFER_SYNTAX_TABLE ();
422 /* Open-paren at start of line means we may have found our
424 c
= FETCH_CHAR_AS_MULTIBYTE (PT_BYTE
);
425 if (SYNTAX (c
) == Sopen
)
427 SETUP_SYNTAX_TABLE (PT
+ 1, -1); /* Try again... */
428 c
= FETCH_CHAR_AS_MULTIBYTE (PT_BYTE
);
429 if (SYNTAX (c
) == Sopen
)
431 /* Now fallback to the default value. */
432 SETUP_BUFFER_SYNTAX_TABLE ();
434 /* Move to beg of previous line. */
435 scan_newline (PT
, PT_BYTE
, BEGV
, BEGV_BYTE
, -2, 1);
438 /* Record what we found, for the next try. */
439 find_start_value
= PT
;
440 find_start_value_byte
= PT_BYTE
;
441 find_start_buffer
= current_buffer
;
442 find_start_modiff
= MODIFF
;
443 find_start_begv
= BEGV
;
444 find_start_pos
= pos
;
446 TEMP_SET_PT_BOTH (opoint
, opoint_byte
);
448 return find_start_value
;
451 /* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
454 prev_char_comend_first (EMACS_INT pos
, EMACS_INT pos_byte
)
458 DEC_BOTH (pos
, pos_byte
);
459 UPDATE_SYNTAX_TABLE_BACKWARD (pos
);
460 c
= FETCH_CHAR (pos_byte
);
461 val
= SYNTAX_COMEND_FIRST (c
);
462 UPDATE_SYNTAX_TABLE_FORWARD (pos
+ 1);
466 /* Return the SYNTAX_COMSTART_FIRST of the character before POS, POS_BYTE. */
469 * prev_char_comstart_first (pos, pos_byte)
474 * DEC_BOTH (pos, pos_byte);
475 * UPDATE_SYNTAX_TABLE_BACKWARD (pos);
476 * c = FETCH_CHAR (pos_byte);
477 * val = SYNTAX_COMSTART_FIRST (c);
478 * UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
482 /* Checks whether charpos FROM is at the end of a comment.
483 FROM_BYTE is the bytepos corresponding to FROM.
484 Do not move back before STOP.
486 Return a positive value if we find a comment ending at FROM/FROM_BYTE;
489 If successful, store the charpos of the comment's beginning
490 into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR.
492 Global syntax data remains valid for backward search starting at
493 the returned value (or at FROM, if the search was not successful). */
496 back_comment (EMACS_INT from
, EMACS_INT from_byte
, EMACS_INT stop
, int comnested
, int comstyle
, EMACS_INT
*charpos_ptr
, EMACS_INT
*bytepos_ptr
)
498 /* Look back, counting the parity of string-quotes,
499 and recording the comment-starters seen.
500 When we reach a safe place, assume that's not in a string;
501 then step the main scan to the earliest comment-starter seen
502 an even number of string quotes away from the safe place.
504 OFROM[I] is position of the earliest comment-starter seen
505 which is I+2X quotes from the comment-end.
506 PARITY is current parity of quotes from the comment end. */
507 int string_style
= -1; /* Presumed outside of any string. */
508 int string_lossage
= 0;
509 /* Not a real lossage: indicates that we have passed a matching comment
510 starter plus a non-matching comment-ender, meaning that any matching
511 comment-starter we might see later could be a false positive (hidden
512 inside another comment).
513 Test case: { a (* b } c (* d *) */
514 int comment_lossage
= 0;
515 EMACS_INT comment_end
= from
;
516 EMACS_INT comment_end_byte
= from_byte
;
517 EMACS_INT comstart_pos
= 0;
518 EMACS_INT comstart_byte
IF_LINT (= 0);
519 /* Place where the containing defun starts,
520 or 0 if we didn't come across it yet. */
521 EMACS_INT defun_start
= 0;
522 EMACS_INT defun_start_byte
= 0;
523 register enum syntaxcode code
;
524 int nesting
= 1; /* current comment nesting */
528 /* FIXME: A }} comment-ender style leads to incorrect behavior
529 in the case of {{ c }}} because we ignore the last two chars which are
530 assumed to be comment-enders although they aren't. */
532 /* At beginning of range to scan, we're outside of strings;
533 that determines quote parity to the comment-end. */
537 int prev_syntax
, com2start
, com2end
;
540 /* Move back and examine a character. */
541 DEC_BOTH (from
, from_byte
);
542 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
544 prev_syntax
= syntax
;
545 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
546 syntax
= SYNTAX_WITH_FLAGS (c
);
549 /* Check for 2-char comment markers. */
550 com2start
= (SYNTAX_FLAGS_COMSTART_FIRST (syntax
)
551 && SYNTAX_FLAGS_COMSTART_SECOND (prev_syntax
)
553 == SYNTAX_FLAGS_COMMENT_STYLE (prev_syntax
, syntax
))
554 && (SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax
)
555 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
)) == comnested
);
556 com2end
= (SYNTAX_FLAGS_COMEND_FIRST (syntax
)
557 && SYNTAX_FLAGS_COMEND_SECOND (prev_syntax
));
558 comstart
= (com2start
|| code
== Scomment
);
560 /* Nasty cases with overlapping 2-char comment markers:
561 - snmp-mode: -- c -- foo -- c --
569 /* If a 2-char comment sequence partly overlaps with another,
570 we don't try to be clever. E.g. |*| in C, or }% in modes that
571 have %..\n and %{..}%. */
572 if (from
> stop
&& (com2end
|| comstart
))
574 EMACS_INT next
= from
, next_byte
= from_byte
;
575 int next_c
, next_syntax
;
576 DEC_BOTH (next
, next_byte
);
577 UPDATE_SYNTAX_TABLE_BACKWARD (next
);
578 next_c
= FETCH_CHAR_AS_MULTIBYTE (next_byte
);
579 next_syntax
= SYNTAX_WITH_FLAGS (next_c
);
580 if (((comstart
|| comnested
)
581 && SYNTAX_FLAGS_COMEND_SECOND (syntax
)
582 && SYNTAX_FLAGS_COMEND_FIRST (next_syntax
))
583 || ((com2end
|| comnested
)
584 && SYNTAX_FLAGS_COMSTART_SECOND (syntax
)
586 == SYNTAX_FLAGS_COMMENT_STYLE (syntax
, prev_syntax
))
587 && SYNTAX_FLAGS_COMSTART_FIRST (next_syntax
)))
589 /* UPDATE_SYNTAX_TABLE_FORWARD (next + 1); */
592 if (com2start
&& comstart_pos
== 0)
593 /* We're looking at a comment starter. But it might be a comment
594 ender as well (see snmp-mode). The first time we see one, we
595 need to consider it as a comment starter,
596 and the subsequent times as a comment ender. */
599 /* Turn a 2-char comment sequences into the appropriate syntax. */
604 /* Ignore comment starters of a different style. */
605 else if (code
== Scomment
606 && (comstyle
!= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0)
607 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
) != comnested
))
610 /* Ignore escaped characters, except comment-enders. */
611 if (code
!= Sendcomment
&& char_quoted (from
, from_byte
))
618 c
= (code
== Sstring_fence
? ST_STRING_STYLE
: ST_COMMENT_STYLE
);
620 /* Track parity of quotes. */
621 if (string_style
== -1)
622 /* Entering a string. */
624 else if (string_style
== c
)
625 /* Leaving the string. */
628 /* If we have two kinds of string delimiters.
629 There's no way to grok this scanning backwards. */
634 /* We've already checked that it is the relevant comstyle. */
635 if (string_style
!= -1 || comment_lossage
|| string_lossage
)
636 /* There are odd string quotes involved, so let's be careful.
637 Test case in Pascal: " { " a { " } */
642 /* Record best comment-starter so far. */
644 comstart_byte
= from_byte
;
646 else if (--nesting
<= 0)
647 /* nested comments have to be balanced, so we don't need to
648 keep looking for earlier ones. We use here the same (slightly
649 incorrect) reasoning as below: since it is followed by uniform
650 paired string quotes, this comment-start has to be outside of
651 strings, else the comment-end itself would be inside a string. */
656 if (SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == comstyle
657 && ((com2end
&& SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax
))
658 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
)) == comnested
)
659 /* This is the same style of comment ender as ours. */
664 /* Anything before that can't count because it would match
665 this comment-ender rather than ours. */
666 from
= stop
; /* Break out of the loop. */
668 else if (comstart_pos
!= 0 || c
!= '\n')
669 /* We're mixing comment styles here, so we'd better be careful.
670 The (comstart_pos != 0 || c != '\n') check is not quite correct
671 (we should just always set comment_lossage), but removing it
672 would imply that any multiline comment in C would go through
673 lossage, which seems overkill.
674 The failure should only happen in the rare cases such as
680 /* Assume a defun-start point is outside of strings. */
681 if (open_paren_in_column_0_is_defun_start
683 || (temp_byte
= dec_bytepos (from_byte
),
684 FETCH_CHAR (temp_byte
) == '\n')))
687 defun_start_byte
= from_byte
;
688 from
= stop
; /* Break out of the loop. */
697 if (comstart_pos
== 0)
700 from_byte
= comment_end_byte
;
701 UPDATE_SYNTAX_TABLE_FORWARD (comment_end
- 1);
703 /* If comstart_pos is set and we get here (ie. didn't jump to `lossage'
704 or `done'), then we've found the beginning of the non-nested comment. */
705 else if (1) /* !comnested */
708 from_byte
= comstart_byte
;
709 UPDATE_SYNTAX_TABLE_FORWARD (from
- 1);
713 struct lisp_parse_state state
;
715 /* We had two kinds of string delimiters mixed up
716 together. Decode this going forwards.
717 Scan fwd from a known safe place (beginning-of-defun)
718 to the one in question; this records where we
719 last passed a comment starter. */
720 /* If we did not already find the defun start, find it now. */
721 if (defun_start
== 0)
723 defun_start
= find_defun_start (comment_end
, comment_end_byte
);
724 defun_start_byte
= find_start_value_byte
;
728 scan_sexps_forward (&state
,
729 defun_start
, defun_start_byte
,
730 comment_end
, -10000, 0, Qnil
, 0);
731 defun_start
= comment_end
;
732 if (state
.incomment
== (comnested
? 1 : -1)
733 && state
.comstyle
== comstyle
)
734 from
= state
.comstr_start
;
739 /* If comment_end is inside some other comment, maybe ours
740 is nested, so we need to try again from within the
741 surrounding comment. Example: { a (* " *) */
743 /* FIXME: We should advance by one or two chars. */
744 defun_start
= state
.comstr_start
+ 2;
745 defun_start_byte
= CHAR_TO_BYTE (defun_start
);
748 } while (defun_start
< comment_end
);
750 from_byte
= CHAR_TO_BYTE (from
);
751 UPDATE_SYNTAX_TABLE_FORWARD (from
- 1);
756 *bytepos_ptr
= from_byte
;
758 return (from
== comment_end
) ? -1 : from
;
761 DEFUN ("syntax-table-p", Fsyntax_table_p
, Ssyntax_table_p
, 1, 1, 0,
762 doc
: /* Return t if OBJECT is a syntax table.
763 Currently, any char-table counts as a syntax table. */)
766 if (CHAR_TABLE_P (object
)
767 && EQ (XCHAR_TABLE (object
)->purpose
, Qsyntax_table
))
773 check_syntax_table (Lisp_Object obj
)
775 CHECK_TYPE (CHAR_TABLE_P (obj
) && EQ (XCHAR_TABLE (obj
)->purpose
, Qsyntax_table
),
776 Qsyntax_table_p
, obj
);
779 DEFUN ("syntax-table", Fsyntax_table
, Ssyntax_table
, 0, 0, 0,
780 doc
: /* Return the current syntax table.
781 This is the one specified by the current buffer. */)
784 return BVAR (current_buffer
, syntax_table
);
787 DEFUN ("standard-syntax-table", Fstandard_syntax_table
,
788 Sstandard_syntax_table
, 0, 0, 0,
789 doc
: /* Return the standard syntax table.
790 This is the one used for new buffers. */)
793 return Vstandard_syntax_table
;
796 DEFUN ("copy-syntax-table", Fcopy_syntax_table
, Scopy_syntax_table
, 0, 1, 0,
797 doc
: /* Construct a new syntax table and return it.
798 It is a copy of the TABLE, which defaults to the standard syntax table. */)
804 check_syntax_table (table
);
806 table
= Vstandard_syntax_table
;
808 copy
= Fcopy_sequence (table
);
810 /* Only the standard syntax table should have a default element.
811 Other syntax tables should inherit from parents instead. */
812 XCHAR_TABLE (copy
)->defalt
= Qnil
;
814 /* Copied syntax tables should all have parents.
815 If we copied one with no parent, such as the standard syntax table,
816 use the standard syntax table as the copy's parent. */
817 if (NILP (XCHAR_TABLE (copy
)->parent
))
818 Fset_char_table_parent (copy
, Vstandard_syntax_table
);
822 DEFUN ("set-syntax-table", Fset_syntax_table
, Sset_syntax_table
, 1, 1, 0,
823 doc
: /* Select a new syntax table for the current buffer.
824 One argument, a syntax table. */)
828 check_syntax_table (table
);
829 BVAR (current_buffer
, syntax_table
) = table
;
830 /* Indicate that this buffer now has a specified syntax table. */
831 idx
= PER_BUFFER_VAR_IDX (syntax_table
);
832 SET_PER_BUFFER_VALUE_P (current_buffer
, idx
, 1);
836 /* Convert a letter which signifies a syntax code
837 into the code it signifies.
838 This is used by modify-syntax-entry, and other things. */
840 unsigned char syntax_spec_code
[0400] =
841 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
842 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
843 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
844 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
845 (char) Swhitespace
, (char) Scomment_fence
, (char) Sstring
, 0377,
846 (char) Smath
, 0377, 0377, (char) Squote
,
847 (char) Sopen
, (char) Sclose
, 0377, 0377,
848 0377, (char) Swhitespace
, (char) Spunct
, (char) Scharquote
,
849 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
850 0377, 0377, 0377, 0377,
851 (char) Scomment
, 0377, (char) Sendcomment
, 0377,
852 (char) Sinherit
, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
853 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
854 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword
,
855 0377, 0377, 0377, 0377, (char) Sescape
, 0377, 0377, (char) Ssymbol
,
856 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
857 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
858 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword
,
859 0377, 0377, 0377, 0377, (char) Sstring_fence
, 0377, 0377, 0377
862 /* Indexed by syntax code, give the letter that describes it. */
864 char syntax_code_spec
[16] =
866 ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@',
870 /* Indexed by syntax code, give the object (cons of syntax code and
871 nil) to be stored in syntax table. Since these objects can be
872 shared among syntax tables, we generate them in advance. By
873 sharing objects, the function `describe-syntax' can give a more
875 static Lisp_Object Vsyntax_code_object
;
878 DEFUN ("char-syntax", Fchar_syntax
, Schar_syntax
, 1, 1, 0,
879 doc
: /* Return the syntax code of CHARACTER, described by a character.
880 For example, if CHARACTER is a word constituent, the
881 character `w' (119) is returned.
882 The characters that correspond to various syntax codes
883 are listed in the documentation of `modify-syntax-entry'. */)
884 (Lisp_Object character
)
887 CHECK_CHARACTER (character
);
888 char_int
= XINT (character
);
889 SETUP_BUFFER_SYNTAX_TABLE ();
890 return make_number (syntax_code_spec
[(int) SYNTAX (char_int
)]);
893 DEFUN ("matching-paren", Fmatching_paren
, Smatching_paren
, 1, 1, 0,
894 doc
: /* Return the matching parenthesis of CHARACTER, or nil if none. */)
895 (Lisp_Object character
)
898 CHECK_NUMBER (character
);
899 char_int
= XINT (character
);
900 SETUP_BUFFER_SYNTAX_TABLE ();
901 code
= SYNTAX (char_int
);
902 if (code
== Sopen
|| code
== Sclose
)
903 return SYNTAX_MATCH (char_int
);
907 DEFUN ("string-to-syntax", Fstring_to_syntax
, Sstring_to_syntax
, 1, 1, 0,
908 doc
: /* Convert a syntax specification STRING into syntax cell form.
909 STRING should be a string as it is allowed as argument of
910 `modify-syntax-entry'. Value is the equivalent cons cell
911 \(CODE . MATCHING-CHAR) that can be used as value of a `syntax-table'
915 register const unsigned char *p
;
916 register enum syntaxcode code
;
920 CHECK_STRING (string
);
923 code
= (enum syntaxcode
) syntax_spec_code
[*p
++];
924 if (((int) code
& 0377) == 0377)
925 error ("Invalid syntax description letter: %c", p
[-1]);
927 if (code
== Sinherit
)
933 int character
= STRING_CHAR_AND_LENGTH (p
, len
);
934 XSETINT (match
, character
);
935 if (XFASTINT (match
) == ' ')
979 if (val
< XVECTOR (Vsyntax_code_object
)->size
&& NILP (match
))
980 return XVECTOR (Vsyntax_code_object
)->contents
[val
];
982 /* Since we can't use a shared object, let's make a new one. */
983 return Fcons (make_number (val
), match
);
986 /* I really don't know why this is interactive
987 help-form should at least be made useful whilst reading the second arg. */
988 DEFUN ("modify-syntax-entry", Fmodify_syntax_entry
, Smodify_syntax_entry
, 2, 3,
989 "cSet syntax for character: \nsSet syntax for %s to: ",
990 doc
: /* Set syntax for character CHAR according to string NEWENTRY.
991 The syntax is changed only for table SYNTAX-TABLE, which defaults to
992 the current buffer's syntax table.
993 CHAR may be a cons (MIN . MAX), in which case, syntaxes of all characters
994 in the range MIN to MAX are changed.
995 The first character of NEWENTRY should be one of the following:
996 Space or - whitespace syntax. w word constituent.
997 _ symbol constituent. . punctuation.
998 ( open-parenthesis. ) close-parenthesis.
999 " string quote. \\ escape.
1000 $ paired delimiter. ' expression quote or prefix operator.
1001 < comment starter. > comment ender.
1002 / character-quote. @ inherit from `standard-syntax-table'.
1003 | generic string fence. ! generic comment fence.
1005 Only single-character comment start and end sequences are represented thus.
1006 Two-character sequences are represented as described below.
1007 The second character of NEWENTRY is the matching parenthesis,
1008 used only if the first character is `(' or `)'.
1009 Any additional characters are flags.
1010 Defined flags are the characters 1, 2, 3, 4, b, p, and n.
1011 1 means CHAR is the start of a two-char comment start sequence.
1012 2 means CHAR is the second character of such a sequence.
1013 3 means CHAR is the start of a two-char comment end sequence.
1014 4 means CHAR is the second character of such a sequence.
1016 There can be several orthogonal comment sequences. This is to support
1017 language modes such as C++. By default, all comment sequences are of style
1018 a, but you can set the comment sequence style to b (on the second character
1019 of a comment-start, and the first character of a comment-end sequence) and/or
1020 c (on any of its chars) using this flag:
1021 b means CHAR is part of comment sequence b.
1022 c means CHAR is part of comment sequence c.
1023 n means CHAR is part of a nestable comment sequence.
1025 p means CHAR is a prefix character for `backward-prefix-chars';
1026 such characters are treated as whitespace when they occur
1027 between expressions.
1028 usage: (modify-syntax-entry CHAR NEWENTRY &optional SYNTAX-TABLE) */)
1029 (Lisp_Object c
, Lisp_Object newentry
, Lisp_Object syntax_table
)
1033 CHECK_CHARACTER_CAR (c
);
1034 CHECK_CHARACTER_CDR (c
);
1037 CHECK_CHARACTER (c
);
1039 if (NILP (syntax_table
))
1040 syntax_table
= BVAR (current_buffer
, syntax_table
);
1042 check_syntax_table (syntax_table
);
1044 newentry
= Fstring_to_syntax (newentry
);
1046 SET_RAW_SYNTAX_ENTRY_RANGE (syntax_table
, c
, newentry
);
1048 SET_RAW_SYNTAX_ENTRY (syntax_table
, XINT (c
), newentry
);
1050 /* We clear the regexp cache, since character classes can now have
1051 different values from those in the compiled regexps.*/
1052 clear_regexp_cache ();
1057 /* Dump syntax table to buffer in human-readable format */
1059 DEFUN ("internal-describe-syntax-value", Finternal_describe_syntax_value
,
1060 Sinternal_describe_syntax_value
, 1, 1, 0,
1061 doc
: /* Insert a description of the internal syntax description SYNTAX at point. */)
1062 (Lisp_Object syntax
)
1064 register enum syntaxcode code
;
1066 char desc
, start1
, start2
, end1
, end2
, prefix
,
1067 comstyleb
, comstylec
, comnested
;
1069 Lisp_Object first
, match_lisp
, value
= syntax
;
1073 insert_string ("default");
1077 if (CHAR_TABLE_P (value
))
1079 insert_string ("deeper char-table ...");
1085 insert_string ("invalid");
1089 first
= XCAR (value
);
1090 match_lisp
= XCDR (value
);
1092 if (!INTEGERP (first
) || !(NILP (match_lisp
) || INTEGERP (match_lisp
)))
1094 insert_string ("invalid");
1098 syntax_code
= XINT (first
);
1099 code
= (enum syntaxcode
) (syntax_code
& 0377);
1100 start1
= SYNTAX_FLAGS_COMSTART_FIRST (syntax_code
);
1101 start2
= SYNTAX_FLAGS_COMSTART_SECOND (syntax_code
);;
1102 end1
= SYNTAX_FLAGS_COMEND_FIRST (syntax_code
);
1103 end2
= SYNTAX_FLAGS_COMEND_SECOND (syntax_code
);
1104 prefix
= SYNTAX_FLAGS_PREFIX (syntax_code
);
1105 comstyleb
= SYNTAX_FLAGS_COMMENT_STYLEB (syntax_code
);
1106 comstylec
= SYNTAX_FLAGS_COMMENT_STYLEC (syntax_code
);
1107 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax_code
);
1109 if ((int) code
< 0 || (int) code
>= (int) Smax
)
1111 insert_string ("invalid");
1114 desc
= syntax_code_spec
[(int) code
];
1116 str
[0] = desc
, str
[1] = 0;
1119 if (NILP (match_lisp
))
1122 insert_char (XINT (match_lisp
));
1143 insert_string ("\twhich means: ");
1145 switch (SWITCH_ENUM_CAST (code
))
1148 insert_string ("whitespace"); break;
1150 insert_string ("punctuation"); break;
1152 insert_string ("word"); break;
1154 insert_string ("symbol"); break;
1156 insert_string ("open"); break;
1158 insert_string ("close"); break;
1160 insert_string ("prefix"); break;
1162 insert_string ("string"); break;
1164 insert_string ("math"); break;
1166 insert_string ("escape"); break;
1168 insert_string ("charquote"); break;
1170 insert_string ("comment"); break;
1172 insert_string ("endcomment"); break;
1174 insert_string ("inherit"); break;
1175 case Scomment_fence
:
1176 insert_string ("comment fence"); break;
1178 insert_string ("string fence"); break;
1180 insert_string ("invalid");
1184 if (!NILP (match_lisp
))
1186 insert_string (", matches ");
1187 insert_char (XINT (match_lisp
));
1191 insert_string (",\n\t is the first character of a comment-start sequence");
1193 insert_string (",\n\t is the second character of a comment-start sequence");
1196 insert_string (",\n\t is the first character of a comment-end sequence");
1198 insert_string (",\n\t is the second character of a comment-end sequence");
1200 insert_string (" (comment style b)");
1202 insert_string (" (comment style c)");
1204 insert_string (" (nestable)");
1207 insert_string (",\n\t is a prefix character for `backward-prefix-chars'");
1212 /* Return the position across COUNT words from FROM.
1213 If that many words cannot be found before the end of the buffer, return 0.
1214 COUNT negative means scan backward and stop at word beginning. */
1217 scan_words (register EMACS_INT from
, register EMACS_INT count
)
1219 register EMACS_INT beg
= BEGV
;
1220 register EMACS_INT end
= ZV
;
1221 register EMACS_INT from_byte
= CHAR_TO_BYTE (from
);
1222 register enum syntaxcode code
;
1224 Lisp_Object func
, pos
;
1229 SETUP_SYNTAX_TABLE (from
, count
);
1240 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1241 ch0
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1242 code
= SYNTAX (ch0
);
1243 INC_BOTH (from
, from_byte
);
1244 if (words_include_escapes
1245 && (code
== Sescape
|| code
== Scharquote
))
1250 /* Now CH0 is a character which begins a word and FROM is the
1251 position of the next character. */
1252 func
= CHAR_TABLE_REF (Vfind_word_boundary_function_table
, ch0
);
1253 if (! NILP (Ffboundp (func
)))
1255 pos
= call2 (func
, make_number (from
- 1), make_number (end
));
1256 if (INTEGERP (pos
) && XINT (pos
) > from
)
1259 from_byte
= CHAR_TO_BYTE (from
);
1266 if (from
== end
) break;
1267 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1268 ch1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1269 code
= SYNTAX (ch1
);
1271 && (! words_include_escapes
1272 || (code
!= Sescape
&& code
!= Scharquote
)))
1273 || word_boundary_p (ch0
, ch1
))
1275 INC_BOTH (from
, from_byte
);
1290 DEC_BOTH (from
, from_byte
);
1291 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
1292 ch1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1293 code
= SYNTAX (ch1
);
1294 if (words_include_escapes
1295 && (code
== Sescape
|| code
== Scharquote
))
1300 /* Now CH1 is a character which ends a word and FROM is the
1302 func
= CHAR_TABLE_REF (Vfind_word_boundary_function_table
, ch1
);
1303 if (! NILP (Ffboundp (func
)))
1305 pos
= call2 (func
, make_number (from
), make_number (beg
));
1306 if (INTEGERP (pos
) && XINT (pos
) < from
)
1309 from_byte
= CHAR_TO_BYTE (from
);
1318 DEC_BOTH (from
, from_byte
);
1319 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
1320 ch0
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1321 code
= SYNTAX (ch0
);
1323 && (! words_include_escapes
1324 || (code
!= Sescape
&& code
!= Scharquote
)))
1325 || word_boundary_p (ch0
, ch1
))
1327 INC_BOTH (from
, from_byte
);
1341 DEFUE ("forward-word", Fforward_word
, Sforward_word
, 0, 1, "^p",
1342 doc
: /* Move point forward ARG words (backward if ARG is negative).
1344 If an edge of the buffer or a field boundary is reached, point is left there
1345 and the function returns nil. Field boundaries are not noticed if
1346 `inhibit-field-text-motion' is non-nil. */)
1353 XSETFASTINT (arg
, 1);
1357 val
= orig_val
= scan_words (PT
, XINT (arg
));
1359 val
= XINT (arg
) > 0 ? ZV
: BEGV
;
1361 /* Avoid jumping out of an input field. */
1362 tmp
= Fconstrain_to_field (make_number (val
), make_number (PT
),
1364 val
= XFASTINT (tmp
);
1367 return val
== orig_val
? Qt
: Qnil
;
1370 DEFUE ("skip-chars-forward", Fskip_chars_forward
, Sskip_chars_forward
, 1, 2, 0,
1371 doc
: /* Move point forward, stopping before a char not in STRING, or at pos LIM.
1372 STRING is like the inside of a `[...]' in a regular expression
1373 except that `]' is never special and `\\' quotes `^', `-' or `\\'
1374 (but not at the end of a range; quoting is never needed there).
1375 Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
1376 With arg "^a-zA-Z", skips nonletters stopping before first letter.
1377 Char classes, e.g. `[:alpha:]', are supported.
1379 Returns the distance traveled, either zero or positive. */)
1380 (Lisp_Object string
, Lisp_Object lim
)
1382 return skip_chars (1, string
, lim
, 1);
1385 DEFUE ("skip-chars-backward", Fskip_chars_backward
, Sskip_chars_backward
, 1, 2, 0,
1386 doc
: /* Move point backward, stopping after a char not in STRING, or at pos LIM.
1387 See `skip-chars-forward' for details.
1388 Returns the distance traveled, either zero or negative. */)
1389 (Lisp_Object string
, Lisp_Object lim
)
1391 return skip_chars (0, string
, lim
, 1);
1394 DEFUN ("skip-syntax-forward", Fskip_syntax_forward
, Sskip_syntax_forward
, 1, 2, 0,
1395 doc
: /* Move point forward across chars in specified syntax classes.
1396 SYNTAX is a string of syntax code characters.
1397 Stop before a char whose syntax is not in SYNTAX, or at position LIM.
1398 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1399 This function returns the distance traveled, either zero or positive. */)
1400 (Lisp_Object syntax
, Lisp_Object lim
)
1402 return skip_syntaxes (1, syntax
, lim
);
1405 DEFUN ("skip-syntax-backward", Fskip_syntax_backward
, Sskip_syntax_backward
, 1, 2, 0,
1406 doc
: /* Move point backward across chars in specified syntax classes.
1407 SYNTAX is a string of syntax code characters.
1408 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.
1409 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1410 This function returns the distance traveled, either zero or negative. */)
1411 (Lisp_Object syntax
, Lisp_Object lim
)
1413 return skip_syntaxes (0, syntax
, lim
);
1417 skip_chars (int forwardp
, Lisp_Object string
, Lisp_Object lim
, int handle_iso_classes
)
1419 register unsigned int c
;
1420 unsigned char fastmap
[0400];
1421 /* Store the ranges of non-ASCII characters. */
1422 int *char_ranges
IF_LINT (= NULL
);
1423 int n_char_ranges
= 0;
1425 register EMACS_INT i
, i_byte
;
1426 /* Set to 1 if the current buffer is multibyte and the region
1427 contains non-ASCII chars. */
1429 /* Set to 1 if STRING is multibyte and it contains non-ASCII
1431 int string_multibyte
;
1432 EMACS_INT size_byte
;
1433 const unsigned char *str
;
1435 Lisp_Object iso_classes
;
1437 CHECK_STRING (string
);
1441 XSETINT (lim
, forwardp
? ZV
: BEGV
);
1443 CHECK_NUMBER_COERCE_MARKER (lim
);
1445 /* In any case, don't allow scan outside bounds of buffer. */
1446 if (XINT (lim
) > ZV
)
1447 XSETFASTINT (lim
, ZV
);
1448 if (XINT (lim
) < BEGV
)
1449 XSETFASTINT (lim
, BEGV
);
1451 multibyte
= (!NILP (BVAR (current_buffer
, enable_multibyte_characters
))
1452 && (XINT (lim
) - PT
!= CHAR_TO_BYTE (XINT (lim
)) - PT_BYTE
));
1453 string_multibyte
= SBYTES (string
) > SCHARS (string
);
1455 memset (fastmap
, 0, sizeof fastmap
);
1457 str
= SDATA (string
);
1458 size_byte
= SBYTES (string
);
1461 if (i_byte
< size_byte
1462 && SREF (string
, 0) == '^')
1464 negate
= 1; i_byte
++;
1467 /* Find the characters specified and set their elements of fastmap.
1468 Handle backslashes and ranges specially.
1470 If STRING contains non-ASCII characters, setup char_ranges for
1471 them and use fastmap only for their leading codes. */
1473 if (! string_multibyte
)
1475 int string_has_eight_bit
= 0;
1477 /* At first setup fastmap. */
1478 while (i_byte
< size_byte
)
1482 if (handle_iso_classes
&& c
== '['
1483 && i_byte
< size_byte
1484 && str
[i_byte
] == ':')
1486 const unsigned char *class_beg
= str
+ i_byte
+ 1;
1487 const unsigned char *class_end
= class_beg
;
1488 const unsigned char *class_limit
= str
+ size_byte
- 2;
1489 /* Leave room for the null. */
1490 unsigned char class_name
[CHAR_CLASS_MAX_LENGTH
+ 1];
1493 if (class_limit
- class_beg
> CHAR_CLASS_MAX_LENGTH
)
1494 class_limit
= class_beg
+ CHAR_CLASS_MAX_LENGTH
;
1496 while (class_end
< class_limit
1497 && *class_end
>= 'a' && *class_end
<= 'z')
1500 if (class_end
== class_beg
1501 || *class_end
!= ':' || class_end
[1] != ']')
1502 goto not_a_class_name
;
1504 memcpy (class_name
, class_beg
, class_end
- class_beg
);
1505 class_name
[class_end
- class_beg
] = 0;
1507 cc
= re_wctype (class_name
);
1509 error ("Invalid ISO C character class");
1511 iso_classes
= Fcons (make_number (cc
), iso_classes
);
1513 i_byte
= class_end
+ 2 - str
;
1520 if (i_byte
== size_byte
)
1525 /* Treat `-' as range character only if another character
1527 if (i_byte
+ 1 < size_byte
1528 && str
[i_byte
] == '-')
1532 /* Skip over the dash. */
1535 /* Get the end of the range. */
1538 && i_byte
< size_byte
)
1543 unsigned lim2
= c2
+ 1;
1546 if (! ASCII_CHAR_P (c2
))
1547 string_has_eight_bit
= 1;
1553 if (! ASCII_CHAR_P (c
))
1554 string_has_eight_bit
= 1;
1558 /* If the current range is multibyte and STRING contains
1559 eight-bit chars, arrange fastmap and setup char_ranges for
1560 the corresponding multibyte chars. */
1561 if (multibyte
&& string_has_eight_bit
)
1563 unsigned char fastmap2
[0400];
1564 int range_start_byte
, range_start_char
;
1566 memcpy (fastmap
+ 0200, fastmap2
+ 0200, 0200);
1567 memset (fastmap
+ 0200, 0, 0200);
1568 /* We are sure that this loop stops. */
1569 for (i
= 0200; ! fastmap2
[i
]; i
++);
1570 c
= BYTE8_TO_CHAR (i
);
1571 fastmap
[CHAR_LEADING_CODE (c
)] = 1;
1572 range_start_byte
= i
;
1573 range_start_char
= c
;
1574 char_ranges
= (int *) alloca (sizeof (int) * 128 * 2);
1575 for (i
= 129; i
< 0400; i
++)
1577 c
= BYTE8_TO_CHAR (i
);
1578 fastmap
[CHAR_LEADING_CODE (c
)] = 1;
1579 if (i
- range_start_byte
!= c
- range_start_char
)
1581 char_ranges
[n_char_ranges
++] = range_start_char
;
1582 char_ranges
[n_char_ranges
++] = ((i
- 1 - range_start_byte
)
1583 + range_start_char
);
1584 range_start_byte
= i
;
1585 range_start_char
= c
;
1588 char_ranges
[n_char_ranges
++] = range_start_char
;
1589 char_ranges
[n_char_ranges
++] = ((i
- 1 - range_start_byte
)
1590 + range_start_char
);
1593 else /* STRING is multibyte */
1595 char_ranges
= (int *) alloca (sizeof (int) * SCHARS (string
) * 2);
1597 while (i_byte
< size_byte
)
1599 unsigned char leading_code
;
1601 leading_code
= str
[i_byte
];
1602 c
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1605 if (handle_iso_classes
&& c
== '['
1606 && i_byte
< size_byte
1607 && STRING_CHAR (str
+ i_byte
) == ':')
1609 const unsigned char *class_beg
= str
+ i_byte
+ 1;
1610 const unsigned char *class_end
= class_beg
;
1611 const unsigned char *class_limit
= str
+ size_byte
- 2;
1612 /* Leave room for the null. */
1613 unsigned char class_name
[CHAR_CLASS_MAX_LENGTH
+ 1];
1616 if (class_limit
- class_beg
> CHAR_CLASS_MAX_LENGTH
)
1617 class_limit
= class_beg
+ CHAR_CLASS_MAX_LENGTH
;
1619 while (class_end
< class_limit
1620 && *class_end
>= 'a' && *class_end
<= 'z')
1623 if (class_end
== class_beg
1624 || *class_end
!= ':' || class_end
[1] != ']')
1625 goto not_a_class_name_multibyte
;
1627 memcpy (class_name
, class_beg
, class_end
- class_beg
);
1628 class_name
[class_end
- class_beg
] = 0;
1630 cc
= re_wctype (class_name
);
1632 error ("Invalid ISO C character class");
1634 iso_classes
= Fcons (make_number (cc
), iso_classes
);
1636 i_byte
= class_end
+ 2 - str
;
1640 not_a_class_name_multibyte
:
1643 if (i_byte
== size_byte
)
1646 leading_code
= str
[i_byte
];
1647 c
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1650 /* Treat `-' as range character only if another character
1652 if (i_byte
+ 1 < size_byte
1653 && str
[i_byte
] == '-')
1656 unsigned char leading_code2
;
1658 /* Skip over the dash. */
1661 /* Get the end of the range. */
1662 leading_code2
= str
[i_byte
];
1663 c2
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1667 && i_byte
< size_byte
)
1669 leading_code2
= str
[i_byte
];
1670 c2
=STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1676 if (ASCII_CHAR_P (c
))
1678 while (c
<= c2
&& c
< 0x80)
1680 leading_code
= CHAR_LEADING_CODE (c
);
1682 if (! ASCII_CHAR_P (c
))
1684 unsigned lim2
= leading_code2
+ 1;
1685 while (leading_code
< lim2
)
1686 fastmap
[leading_code
++] = 1;
1689 char_ranges
[n_char_ranges
++] = c
;
1690 char_ranges
[n_char_ranges
++] = c2
;
1696 if (ASCII_CHAR_P (c
))
1700 fastmap
[leading_code
] = 1;
1701 char_ranges
[n_char_ranges
++] = c
;
1702 char_ranges
[n_char_ranges
++] = c
;
1707 /* If the current range is unibyte and STRING contains non-ASCII
1708 chars, arrange fastmap for the corresponding unibyte
1711 if (! multibyte
&& n_char_ranges
> 0)
1713 memset (fastmap
+ 0200, 0, 0200);
1714 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1716 int c1
= char_ranges
[i
];
1717 unsigned lim2
= char_ranges
[i
+ 1] + 1;
1719 for (; c1
< lim2
; c1
++)
1721 int b
= CHAR_TO_BYTE_SAFE (c1
);
1729 /* If ^ was the first character, complement the fastmap. */
1733 for (i
= 0; i
< sizeof fastmap
; i
++)
1737 for (i
= 0; i
< 0200; i
++)
1739 /* All non-ASCII chars possibly match. */
1740 for (; i
< sizeof fastmap
; i
++)
1746 EMACS_INT start_point
= PT
;
1748 EMACS_INT pos_byte
= PT_BYTE
;
1749 unsigned char *p
= PT_ADDR
, *endp
, *stop
;
1753 endp
= (XINT (lim
) == GPT
) ? GPT_ADDR
: CHAR_POS_ADDR (XINT (lim
));
1754 stop
= (pos
< GPT
&& GPT
< XINT (lim
)) ? GPT_ADDR
: endp
;
1758 endp
= CHAR_POS_ADDR (XINT (lim
));
1759 stop
= (pos
>= GPT
&& GPT
> XINT (lim
)) ? GAP_END_ADDR
: endp
;
1763 /* This code may look up syntax tables using macros that rely on the
1764 gl_state object. To make sure this object is not out of date,
1765 let's initialize it manually.
1766 We ignore syntax-table text-properties for now, since that's
1767 what we've done in the past. */
1768 SETUP_BUFFER_SYNTAX_TABLE ();
1783 c
= STRING_CHAR_AND_LENGTH (p
, nbytes
);
1784 if (! NILP (iso_classes
) && in_classes (c
, iso_classes
))
1794 if (! ASCII_CHAR_P (c
))
1796 /* As we are looking at a multibyte character, we
1797 must look up the character in the table
1798 CHAR_RANGES. If there's no data in the table,
1799 that character is not what we want to skip. */
1801 /* The following code do the right thing even if
1802 n_char_ranges is zero (i.e. no data in
1804 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1805 if (c
>= char_ranges
[i
] && c
<= char_ranges
[i
+ 1])
1807 if (!(negate
^ (i
< n_char_ranges
)))
1811 p
+= nbytes
, pos
++, pos_byte
+= nbytes
;
1824 if (!NILP (iso_classes
) && in_classes (*p
, iso_classes
))
1829 goto fwd_unibyte_ok
;
1835 p
++, pos
++, pos_byte
++;
1843 unsigned char *prev_p
;
1853 while (--p
>= stop
&& ! CHAR_HEAD_P (*p
));
1854 c
= STRING_CHAR (p
);
1856 if (! NILP (iso_classes
) && in_classes (c
, iso_classes
))
1866 if (! ASCII_CHAR_P (c
))
1868 /* See the comment in the previous similar code. */
1869 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1870 if (c
>= char_ranges
[i
] && c
<= char_ranges
[i
+ 1])
1872 if (!(negate
^ (i
< n_char_ranges
)))
1876 pos
--, pos_byte
-= prev_p
- p
;
1889 if (! NILP (iso_classes
) && in_classes (p
[-1], iso_classes
))
1894 goto back_unibyte_ok
;
1897 if (!fastmap
[p
[-1]])
1900 p
--, pos
--, pos_byte
--;
1904 SET_PT_BOTH (pos
, pos_byte
);
1907 return make_number (PT
- start_point
);
1913 skip_syntaxes (int forwardp
, Lisp_Object string
, Lisp_Object lim
)
1915 register unsigned int c
;
1916 unsigned char fastmap
[0400];
1918 register EMACS_INT i
, i_byte
;
1920 EMACS_INT size_byte
;
1923 CHECK_STRING (string
);
1926 XSETINT (lim
, forwardp
? ZV
: BEGV
);
1928 CHECK_NUMBER_COERCE_MARKER (lim
);
1930 /* In any case, don't allow scan outside bounds of buffer. */
1931 if (XINT (lim
) > ZV
)
1932 XSETFASTINT (lim
, ZV
);
1933 if (XINT (lim
) < BEGV
)
1934 XSETFASTINT (lim
, BEGV
);
1936 if (forwardp
? (PT
>= XFASTINT (lim
)) : (PT
<= XFASTINT (lim
)))
1937 return make_number (0);
1939 multibyte
= (!NILP (BVAR (current_buffer
, enable_multibyte_characters
))
1940 && (XINT (lim
) - PT
!= CHAR_TO_BYTE (XINT (lim
)) - PT_BYTE
));
1942 memset (fastmap
, 0, sizeof fastmap
);
1944 if (SBYTES (string
) > SCHARS (string
))
1945 /* As this is very rare case (syntax spec is ASCII only), don't
1946 consider efficiency. */
1947 string
= string_make_unibyte (string
);
1949 str
= SDATA (string
);
1950 size_byte
= SBYTES (string
);
1953 if (i_byte
< size_byte
1954 && SREF (string
, 0) == '^')
1956 negate
= 1; i_byte
++;
1959 /* Find the syntaxes specified and set their elements of fastmap. */
1961 while (i_byte
< size_byte
)
1964 fastmap
[syntax_spec_code
[c
]] = 1;
1967 /* If ^ was the first character, complement the fastmap. */
1969 for (i
= 0; i
< sizeof fastmap
; i
++)
1973 EMACS_INT start_point
= PT
;
1975 EMACS_INT pos_byte
= PT_BYTE
;
1976 unsigned char *p
= PT_ADDR
, *endp
, *stop
;
1980 endp
= (XINT (lim
) == GPT
) ? GPT_ADDR
: CHAR_POS_ADDR (XINT (lim
));
1981 stop
= (pos
< GPT
&& GPT
< XINT (lim
)) ? GPT_ADDR
: endp
;
1985 endp
= CHAR_POS_ADDR (XINT (lim
));
1986 stop
= (pos
>= GPT
&& GPT
> XINT (lim
)) ? GAP_END_ADDR
: endp
;
1990 SETUP_SYNTAX_TABLE (pos
, forwardp
? 1 : -1);
2006 c
= STRING_CHAR_AND_LENGTH (p
, nbytes
);
2007 if (! fastmap
[(int) SYNTAX (c
)])
2009 p
+= nbytes
, pos
++, pos_byte
+= nbytes
;
2010 UPDATE_SYNTAX_TABLE_FORWARD (pos
);
2024 if (! fastmap
[(int) SYNTAX (*p
)])
2026 p
++, pos
++, pos_byte
++;
2027 UPDATE_SYNTAX_TABLE_FORWARD (pos
);
2037 unsigned char *prev_p
;
2046 UPDATE_SYNTAX_TABLE_BACKWARD (pos
- 1);
2048 while (--p
>= stop
&& ! CHAR_HEAD_P (*p
));
2049 c
= STRING_CHAR (p
);
2050 if (! fastmap
[(int) SYNTAX (c
)])
2052 pos
--, pos_byte
-= prev_p
- p
;
2066 UPDATE_SYNTAX_TABLE_BACKWARD (pos
- 1);
2067 if (! fastmap
[(int) SYNTAX (p
[-1])])
2069 p
--, pos
--, pos_byte
--;
2074 SET_PT_BOTH (pos
, pos_byte
);
2077 return make_number (PT
- start_point
);
2081 /* Return 1 if character C belongs to one of the ISO classes
2082 in the list ISO_CLASSES. Each class is represented by an
2083 integer which is its type according to re_wctype. */
2086 in_classes (int c
, Lisp_Object iso_classes
)
2090 while (CONSP (iso_classes
))
2093 elt
= XCAR (iso_classes
);
2094 iso_classes
= XCDR (iso_classes
);
2096 if (re_iswctype (c
, XFASTINT (elt
)))
2103 /* Jump over a comment, assuming we are at the beginning of one.
2104 FROM is the current position.
2105 FROM_BYTE is the bytepos corresponding to FROM.
2106 Do not move past STOP (a charpos).
2107 The comment over which we have to jump is of style STYLE
2108 (either SYNTAX_FLAGS_COMMENT_STYLE(foo) or ST_COMMENT_STYLE).
2109 NESTING should be positive to indicate the nesting at the beginning
2110 for nested comments and should be zero or negative else.
2111 ST_COMMENT_STYLE cannot be nested.
2112 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
2113 (or 0 If the search cannot start in the middle of a two-character).
2115 If successful, return 1 and store the charpos of the comment's end
2116 into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR.
2117 Else, return 0 and store the charpos STOP into *CHARPOS_PTR, the
2118 corresponding bytepos into *BYTEPOS_PTR and the current nesting
2119 (as defined for state.incomment) in *INCOMMENT_PTR.
2121 The comment end is the last character of the comment rather than the
2122 character just after the comment.
2124 Global syntax data is assumed to initially be valid for FROM and
2125 remains valid for forward search starting at the returned position. */
2128 forw_comment (EMACS_INT from
, EMACS_INT from_byte
, EMACS_INT stop
,
2129 int nesting
, int style
, int prev_syntax
,
2130 EMACS_INT
*charpos_ptr
, EMACS_INT
*bytepos_ptr
,
2134 register enum syntaxcode code
;
2135 register int syntax
, other_syntax
;
2137 if (nesting
<= 0) nesting
= -1;
2139 /* Enter the loop in the middle so that we find
2140 a 2-char comment ender if we start in the middle of it. */
2141 syntax
= prev_syntax
;
2142 if (syntax
!= 0) goto forw_incomment
;
2148 *incomment_ptr
= nesting
;
2149 *charpos_ptr
= from
;
2150 *bytepos_ptr
= from_byte
;
2153 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2154 syntax
= SYNTAX_WITH_FLAGS (c
);
2155 code
= syntax
& 0xff;
2156 if (code
== Sendcomment
2157 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == style
2158 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ?
2159 (nesting
> 0 && --nesting
== 0) : nesting
< 0))
2160 /* we have encountered a comment end of the same style
2161 as the comment sequence which began this comment
2164 if (code
== Scomment_fence
2165 && style
== ST_COMMENT_STYLE
)
2166 /* we have encountered a comment end of the same style
2167 as the comment sequence which began this comment
2172 && SYNTAX_FLAGS_COMMENT_NESTED (syntax
)
2173 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == style
)
2174 /* we have encountered a nested comment of the same style
2175 as the comment sequence which began this comment section */
2177 INC_BOTH (from
, from_byte
);
2178 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2181 if (from
< stop
&& SYNTAX_FLAGS_COMEND_FIRST (syntax
)
2182 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2183 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2184 SYNTAX_FLAGS_COMEND_SECOND (other_syntax
))
2185 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, other_syntax
) == style
2186 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ||
2187 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
))
2188 ? nesting
> 0 : nesting
< 0))
2191 /* we have encountered a comment end of the same style
2192 as the comment sequence which began this comment
2197 INC_BOTH (from
, from_byte
);
2198 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2203 && SYNTAX_FLAGS_COMSTART_FIRST (syntax
)
2204 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2205 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2206 SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
) == style
2207 && SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
))
2208 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ||
2209 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
)))
2210 /* we have encountered a nested comment of the same style
2211 as the comment sequence which began this comment
2214 INC_BOTH (from
, from_byte
);
2215 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2219 *charpos_ptr
= from
;
2220 *bytepos_ptr
= from_byte
;
2224 DEFUN ("forward-comment", Fforward_comment
, Sforward_comment
, 1, 1, 0,
2226 Move forward across up to COUNT comments. If COUNT is negative, move backward.
2227 Stop scanning if we find something other than a comment or whitespace.
2228 Set point to where scanning stops.
2229 If COUNT comments are found as expected, with nothing except whitespace
2230 between them, return t; otherwise return nil. */)
2233 register EMACS_INT from
;
2234 EMACS_INT from_byte
;
2235 register EMACS_INT stop
;
2237 register enum syntaxcode code
;
2238 int comstyle
= 0; /* style of comment encountered */
2239 int comnested
= 0; /* whether the comment is nestable or not */
2242 EMACS_INT out_charpos
, out_bytepos
;
2245 CHECK_NUMBER (count
);
2246 count1
= XINT (count
);
2247 stop
= count1
> 0 ? ZV
: BEGV
;
2253 from_byte
= PT_BYTE
;
2255 SETUP_SYNTAX_TABLE (from
, count1
);
2260 int comstart_first
, syntax
, other_syntax
;
2264 SET_PT_BOTH (from
, from_byte
);
2268 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2269 syntax
= SYNTAX_WITH_FLAGS (c
);
2271 comstart_first
= SYNTAX_FLAGS_COMSTART_FIRST (syntax
);
2272 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2273 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2274 INC_BOTH (from
, from_byte
);
2275 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2276 if (from
< stop
&& comstart_first
2277 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2278 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2279 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
)))
2281 /* We have encountered a comment start sequence and we
2282 are ignoring all text inside comments. We must record
2283 the comment style this sequence begins so that later,
2284 only a comment end of the same style actually ends
2285 the comment section. */
2287 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2289 = comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2290 INC_BOTH (from
, from_byte
);
2291 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2294 while (code
== Swhitespace
|| (code
== Sendcomment
&& c
== '\n'));
2296 if (code
== Scomment_fence
)
2297 comstyle
= ST_COMMENT_STYLE
;
2298 else if (code
!= Scomment
)
2301 DEC_BOTH (from
, from_byte
);
2302 SET_PT_BOTH (from
, from_byte
);
2305 /* We're at the start of a comment. */
2306 found
= forw_comment (from
, from_byte
, stop
, comnested
, comstyle
, 0,
2307 &out_charpos
, &out_bytepos
, &dummy
);
2308 from
= out_charpos
; from_byte
= out_bytepos
;
2312 SET_PT_BOTH (from
, from_byte
);
2315 INC_BOTH (from
, from_byte
);
2316 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2317 /* We have skipped one comment. */
2329 SET_PT_BOTH (BEGV
, BEGV_BYTE
);
2334 DEC_BOTH (from
, from_byte
);
2335 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
2336 quoted
= char_quoted (from
, from_byte
);
2337 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2338 syntax
= SYNTAX_WITH_FLAGS (c
);
2341 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2342 if (code
== Sendcomment
)
2343 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2344 if (from
> stop
&& SYNTAX_FLAGS_COMEND_SECOND (syntax
)
2345 && prev_char_comend_first (from
, from_byte
)
2346 && !char_quoted (from
- 1, dec_bytepos (from_byte
)))
2349 /* We must record the comment style encountered so that
2350 later, we can match only the proper comment begin
2351 sequence of the same style. */
2352 DEC_BOTH (from
, from_byte
);
2354 /* Calling char_quoted, above, set up global syntax position
2355 at the new value of FROM. */
2356 c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2357 other_syntax
= SYNTAX_WITH_FLAGS (c1
);
2358 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2360 = comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2363 if (code
== Scomment_fence
)
2365 /* Skip until first preceding unquoted comment_fence. */
2366 int fence_found
= 0;
2367 EMACS_INT ini
= from
, ini_byte
= from_byte
;
2371 DEC_BOTH (from
, from_byte
);
2372 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2373 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2374 if (SYNTAX (c
) == Scomment_fence
2375 && !char_quoted (from
, from_byte
))
2380 else if (from
== stop
)
2383 if (fence_found
== 0)
2385 from
= ini
; /* Set point to ini + 1. */
2386 from_byte
= ini_byte
;
2390 /* We have skipped one comment. */
2393 else if (code
== Sendcomment
)
2395 found
= back_comment (from
, from_byte
, stop
, comnested
, comstyle
,
2396 &out_charpos
, &out_bytepos
);
2400 /* This end-of-line is not an end-of-comment.
2401 Treat it like a whitespace.
2402 CC-mode (and maybe others) relies on this behavior. */
2406 /* Failure: we should go back to the end of this
2407 not-quite-endcomment. */
2408 if (SYNTAX (c
) != code
)
2409 /* It was a two-char Sendcomment. */
2410 INC_BOTH (from
, from_byte
);
2416 /* We have skipped one comment. */
2417 from
= out_charpos
, from_byte
= out_bytepos
;
2421 else if (code
!= Swhitespace
|| quoted
)
2425 INC_BOTH (from
, from_byte
);
2426 SET_PT_BOTH (from
, from_byte
);
2434 SET_PT_BOTH (from
, from_byte
);
2439 /* Return syntax code of character C if C is an ASCII character
2440 or `multibyte_symbol_p' is zero. Otherwise, return Ssymbol. */
2442 #define SYNTAX_WITH_MULTIBYTE_CHECK(c) \
2443 ((ASCII_CHAR_P (c) || !multibyte_symbol_p) \
2444 ? SYNTAX (c) : Ssymbol)
2447 scan_lists (register EMACS_INT from
, EMACS_INT count
, EMACS_INT depth
, int sexpflag
)
2450 register EMACS_INT stop
= count
> 0 ? ZV
: BEGV
;
2455 register enum syntaxcode code
, temp_code
;
2456 int min_depth
= depth
; /* Err out if depth gets less than this. */
2457 int comstyle
= 0; /* style of comment encountered */
2458 int comnested
= 0; /* whether the comment is nestable or not */
2460 EMACS_INT last_good
= from
;
2462 EMACS_INT from_byte
;
2463 EMACS_INT out_bytepos
, out_charpos
;
2465 int multibyte_symbol_p
= sexpflag
&& multibyte_syntax_as_symbol
;
2467 if (depth
> 0) min_depth
= 0;
2469 if (from
> ZV
) from
= ZV
;
2470 if (from
< BEGV
) from
= BEGV
;
2472 from_byte
= CHAR_TO_BYTE (from
);
2477 SETUP_SYNTAX_TABLE (from
, count
);
2482 int comstart_first
, prefix
, syntax
, other_syntax
;
2483 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2484 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2485 syntax
= SYNTAX_WITH_FLAGS (c
);
2486 code
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2487 comstart_first
= SYNTAX_FLAGS_COMSTART_FIRST (syntax
);
2488 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2489 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2490 prefix
= SYNTAX_FLAGS_PREFIX (syntax
);
2491 if (depth
== min_depth
)
2493 INC_BOTH (from
, from_byte
);
2494 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2495 if (from
< stop
&& comstart_first
2496 && (c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2497 other_syntax
= SYNTAX_WITH_FLAGS (c
),
2498 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
))
2499 && parse_sexp_ignore_comments
)
2501 /* we have encountered a comment start sequence and we
2502 are ignoring all text inside comments. We must record
2503 the comment style this sequence begins so that later,
2504 only a comment end of the same style actually ends
2505 the comment section */
2507 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2509 = comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2510 INC_BOTH (from
, from_byte
);
2511 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2517 switch (SWITCH_ENUM_CAST (code
))
2523 INC_BOTH (from
, from_byte
);
2524 /* treat following character as a word constituent */
2527 if (depth
|| !sexpflag
) break;
2528 /* This word counts as a sexp; return at end of it. */
2531 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2533 /* Some compilers can't handle this inside the switch. */
2534 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2535 temp
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2540 INC_BOTH (from
, from_byte
);
2551 INC_BOTH (from
, from_byte
);
2555 case Scomment_fence
:
2556 comstyle
= ST_COMMENT_STYLE
;
2559 if (!parse_sexp_ignore_comments
) break;
2560 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2561 found
= forw_comment (from
, from_byte
, stop
,
2562 comnested
, comstyle
, 0,
2563 &out_charpos
, &out_bytepos
, &dummy
);
2564 from
= out_charpos
, from_byte
= out_bytepos
;
2571 INC_BOTH (from
, from_byte
);
2572 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2578 if (from
!= stop
&& c
== FETCH_CHAR_AS_MULTIBYTE (from_byte
))
2580 INC_BOTH (from
, from_byte
);
2590 if (!++depth
) goto done
;
2595 if (!--depth
) goto done
;
2596 if (depth
< min_depth
)
2597 xsignal3 (Qscan_error
,
2598 build_string ("Containing expression ends prematurely"),
2599 make_number (last_good
), make_number (from
));
2604 temp_pos
= dec_bytepos (from_byte
);
2605 stringterm
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2610 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2611 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2614 && SYNTAX_WITH_MULTIBYTE_CHECK (c
) == Sstring
)
2615 : SYNTAX_WITH_MULTIBYTE_CHECK (c
) == Sstring_fence
)
2618 /* Some compilers can't handle this inside the switch. */
2619 temp
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2624 INC_BOTH (from
, from_byte
);
2626 INC_BOTH (from
, from_byte
);
2628 INC_BOTH (from
, from_byte
);
2629 if (!depth
&& sexpflag
) goto done
;
2632 /* Ignore whitespace, punctuation, quote, endcomment. */
2637 /* Reached end of buffer. Error if within object, return nil if between */
2644 /* End of object reached */
2654 DEC_BOTH (from
, from_byte
);
2655 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2656 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2657 syntax
= SYNTAX_WITH_FLAGS (c
);
2658 code
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2659 if (depth
== min_depth
)
2662 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2663 if (code
== Sendcomment
)
2664 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2665 if (from
> stop
&& SYNTAX_FLAGS_COMEND_SECOND (syntax
)
2666 && prev_char_comend_first (from
, from_byte
)
2667 && parse_sexp_ignore_comments
)
2669 /* We must record the comment style encountered so that
2670 later, we can match only the proper comment begin
2671 sequence of the same style. */
2672 int c2
, other_syntax
;
2673 DEC_BOTH (from
, from_byte
);
2674 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2676 c2
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2677 other_syntax
= SYNTAX_WITH_FLAGS (c2
);
2678 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2680 = comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2683 /* Quoting turns anything except a comment-ender
2684 into a word character. Note that this cannot be true
2685 if we decremented FROM in the if-statement above. */
2686 if (code
!= Sendcomment
&& char_quoted (from
, from_byte
))
2688 DEC_BOTH (from
, from_byte
);
2691 else if (SYNTAX_FLAGS_PREFIX (syntax
))
2694 switch (SWITCH_ENUM_CAST (code
))
2700 if (depth
|| !sexpflag
) break;
2701 /* This word counts as a sexp; count object finished
2702 after passing it. */
2705 temp_pos
= from_byte
;
2706 if (! NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
2710 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2711 c1
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2712 temp_code
= SYNTAX_WITH_MULTIBYTE_CHECK (c1
);
2713 /* Don't allow comment-end to be quoted. */
2714 if (temp_code
== Sendcomment
)
2716 quoted
= char_quoted (from
- 1, temp_pos
);
2719 DEC_BOTH (from
, from_byte
);
2720 temp_pos
= dec_bytepos (temp_pos
);
2721 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2723 c1
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2724 temp_code
= SYNTAX_WITH_MULTIBYTE_CHECK (c1
);
2725 if (! (quoted
|| temp_code
== Sword
2726 || temp_code
== Ssymbol
2727 || temp_code
== Squote
))
2729 DEC_BOTH (from
, from_byte
);
2736 temp_pos
= dec_bytepos (from_byte
);
2737 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2738 if (from
!= stop
&& c
== FETCH_CHAR_AS_MULTIBYTE (temp_pos
))
2739 DEC_BOTH (from
, from_byte
);
2748 if (!++depth
) goto done2
;
2753 if (!--depth
) goto done2
;
2754 if (depth
< min_depth
)
2755 xsignal3 (Qscan_error
,
2756 build_string ("Containing expression ends prematurely"),
2757 make_number (last_good
), make_number (from
));
2761 if (!parse_sexp_ignore_comments
)
2763 found
= back_comment (from
, from_byte
, stop
, comnested
, comstyle
,
2764 &out_charpos
, &out_bytepos
);
2765 /* FIXME: if found == -1, then it really wasn't a comment-end.
2766 For single-char Sendcomment, we can't do much about it apart
2767 from skipping the char.
2768 For 2-char endcomments, we could try again, taking both
2769 chars as separate entities, but it's a lot of trouble
2770 for very little gain, so we don't bother either. -sm */
2772 from
= out_charpos
, from_byte
= out_bytepos
;
2775 case Scomment_fence
:
2781 DEC_BOTH (from
, from_byte
);
2782 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2783 if (!char_quoted (from
, from_byte
)
2784 && (c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2785 SYNTAX_WITH_MULTIBYTE_CHECK (c
) == code
))
2788 if (code
== Sstring_fence
&& !depth
&& sexpflag
) goto done2
;
2792 stringterm
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2797 DEC_BOTH (from
, from_byte
);
2798 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2799 if (!char_quoted (from
, from_byte
)
2801 == (c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
)))
2802 && SYNTAX_WITH_MULTIBYTE_CHECK (c
) == Sstring
)
2805 if (!depth
&& sexpflag
) goto done2
;
2808 /* Ignore whitespace, punctuation, quote, endcomment. */
2813 /* Reached start of buffer. Error if within object, return nil if between */
2826 XSETFASTINT (val
, from
);
2830 xsignal3 (Qscan_error
,
2831 build_string ("Unbalanced parentheses"),
2832 make_number (last_good
), make_number (from
));
2835 DEFUN ("scan-lists", Fscan_lists
, Sscan_lists
, 3, 3, 0,
2836 doc
: /* Scan from character number FROM by COUNT lists.
2837 Returns the character number of the position thus found.
2839 If DEPTH is nonzero, paren depth begins counting from that value,
2840 only places where the depth in parentheses becomes zero
2841 are candidates for stopping; COUNT such places are counted.
2842 Thus, a positive value for DEPTH means go out levels.
2844 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2846 If the beginning or end of (the accessible part of) the buffer is reached
2847 and the depth is wrong, an error is signaled.
2848 If the depth is right but the count is not used up, nil is returned. */)
2849 (Lisp_Object from
, Lisp_Object count
, Lisp_Object depth
)
2851 CHECK_NUMBER (from
);
2852 CHECK_NUMBER (count
);
2853 CHECK_NUMBER (depth
);
2855 return scan_lists (XINT (from
), XINT (count
), XINT (depth
), 0);
2858 DEFUN ("scan-sexps", Fscan_sexps
, Sscan_sexps
, 2, 2, 0,
2859 doc
: /* Scan from character number FROM by COUNT balanced expressions.
2860 If COUNT is negative, scan backwards.
2861 Returns the character number of the position thus found.
2863 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2865 If the beginning or end of (the accessible part of) the buffer is reached
2866 in the middle of a parenthetical grouping, an error is signaled.
2867 If the beginning or end is reached between groupings
2868 but before count is used up, nil is returned. */)
2869 (Lisp_Object from
, Lisp_Object count
)
2871 CHECK_NUMBER (from
);
2872 CHECK_NUMBER (count
);
2874 return scan_lists (XINT (from
), XINT (count
), 0, 1);
2877 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars
, Sbackward_prefix_chars
,
2879 doc
: /* Move point backward over any number of chars with prefix syntax.
2880 This includes chars with "quote" or "prefix" syntax (' or p). */)
2883 EMACS_INT beg
= BEGV
;
2884 EMACS_INT opoint
= PT
;
2885 EMACS_INT opoint_byte
= PT_BYTE
;
2887 EMACS_INT pos_byte
= PT_BYTE
;
2892 SET_PT_BOTH (opoint
, opoint_byte
);
2897 SETUP_SYNTAX_TABLE (pos
, -1);
2899 DEC_BOTH (pos
, pos_byte
);
2901 while (!char_quoted (pos
, pos_byte
)
2902 /* Previous statement updates syntax table. */
2903 && ((c
= FETCH_CHAR_AS_MULTIBYTE (pos_byte
), SYNTAX (c
) == Squote
)
2904 || SYNTAX_PREFIX (c
)))
2907 opoint_byte
= pos_byte
;
2910 DEC_BOTH (pos
, pos_byte
);
2913 SET_PT_BOTH (opoint
, opoint_byte
);
2918 /* Parse forward from FROM / FROM_BYTE to END,
2919 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
2920 and return a description of the state of the parse at END.
2921 If STOPBEFORE is nonzero, stop at the start of an atom.
2922 If COMMENTSTOP is 1, stop at the start of a comment.
2923 If COMMENTSTOP is -1, stop at the start or end of a comment,
2924 after the beginning of a string, or after the end of a string. */
2927 scan_sexps_forward (struct lisp_parse_state
*stateptr
,
2928 EMACS_INT from
, EMACS_INT from_byte
, EMACS_INT end
,
2929 int targetdepth
, int stopbefore
,
2930 Lisp_Object oldstate
, int commentstop
)
2932 struct lisp_parse_state state
;
2934 register enum syntaxcode code
;
2937 struct level
{ int last
, prev
; };
2938 struct level levelstart
[100];
2939 register struct level
*curlevel
= levelstart
;
2940 struct level
*endlevel
= levelstart
+ 100;
2941 register int depth
; /* Paren depth of current scanning location.
2942 level - levelstart equals this except
2943 when the depth becomes negative. */
2944 int mindepth
; /* Lowest DEPTH value seen. */
2945 int start_quoted
= 0; /* Nonzero means starting after a char quote */
2947 EMACS_INT prev_from
; /* Keep one character before FROM. */
2948 EMACS_INT prev_from_byte
;
2949 int prev_from_syntax
;
2950 int boundary_stop
= commentstop
== -1;
2953 EMACS_INT out_bytepos
, out_charpos
;
2957 prev_from_byte
= from_byte
;
2959 DEC_BOTH (prev_from
, prev_from_byte
);
2961 /* Use this macro instead of `from++'. */
2963 do { prev_from = from; \
2964 prev_from_byte = from_byte; \
2965 temp = FETCH_CHAR_AS_MULTIBYTE (prev_from_byte); \
2966 prev_from_syntax = SYNTAX_WITH_FLAGS (temp); \
2967 INC_BOTH (from, from_byte); \
2969 UPDATE_SYNTAX_TABLE_FORWARD (from); \
2975 if (NILP (oldstate
))
2978 state
.instring
= -1;
2979 state
.incomment
= 0;
2980 state
.comstyle
= 0; /* comment style a by default. */
2981 state
.comstr_start
= -1; /* no comment/string seen. */
2985 tem
= Fcar (oldstate
);
2991 oldstate
= Fcdr (oldstate
);
2992 oldstate
= Fcdr (oldstate
);
2993 oldstate
= Fcdr (oldstate
);
2994 tem
= Fcar (oldstate
);
2995 /* Check whether we are inside string_fence-style string: */
2996 state
.instring
= (!NILP (tem
)
2997 ? (INTEGERP (tem
) ? XINT (tem
) : ST_STRING_STYLE
)
3000 oldstate
= Fcdr (oldstate
);
3001 tem
= Fcar (oldstate
);
3002 state
.incomment
= (!NILP (tem
)
3003 ? (INTEGERP (tem
) ? XINT (tem
) : -1)
3006 oldstate
= Fcdr (oldstate
);
3007 tem
= Fcar (oldstate
);
3008 start_quoted
= !NILP (tem
);
3010 /* if the eighth element of the list is nil, we are in comment
3011 style a. If it is non-nil, we are in comment style b */
3012 oldstate
= Fcdr (oldstate
);
3013 oldstate
= Fcdr (oldstate
);
3014 tem
= Fcar (oldstate
);
3015 state
.comstyle
= (NILP (tem
)
3017 : (EQ (tem
, Qsyntax_table
)
3019 : INTEGERP (tem
) ? XINT (tem
) : 1));
3021 oldstate
= Fcdr (oldstate
);
3022 tem
= Fcar (oldstate
);
3023 state
.comstr_start
= NILP (tem
) ? -1 : XINT (tem
) ;
3024 oldstate
= Fcdr (oldstate
);
3025 tem
= Fcar (oldstate
);
3026 while (!NILP (tem
)) /* >= second enclosing sexps. */
3028 /* curlevel++->last ran into compiler bug on Apollo */
3029 curlevel
->last
= XINT (Fcar (tem
));
3030 if (++curlevel
== endlevel
)
3031 curlevel
--; /* error ("Nesting too deep for parser"); */
3032 curlevel
->prev
= -1;
3033 curlevel
->last
= -1;
3040 curlevel
->prev
= -1;
3041 curlevel
->last
= -1;
3043 SETUP_SYNTAX_TABLE (prev_from
, 1);
3044 temp
= FETCH_CHAR (prev_from_byte
);
3045 prev_from_syntax
= SYNTAX_WITH_FLAGS (temp
);
3046 UPDATE_SYNTAX_TABLE_FORWARD (from
);
3048 /* Enter the loop at a place appropriate for initial state. */
3050 if (state
.incomment
)
3051 goto startincomment
;
3052 if (state
.instring
>= 0)
3054 nofence
= state
.instring
!= ST_STRING_STYLE
;
3056 goto startquotedinstring
;
3059 else if (start_quoted
)
3066 code
= prev_from_syntax
& 0xff;
3069 && SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax
)
3070 && (c1
= FETCH_CHAR (from_byte
),
3071 syntax
= SYNTAX_WITH_FLAGS (c1
),
3072 SYNTAX_FLAGS_COMSTART_SECOND (syntax
)))
3073 /* Duplicate code to avoid a complex if-expression
3074 which causes trouble for the SGI compiler. */
3076 /* Record the comment style we have entered so that only
3077 the comment-end sequence of the same style actually
3078 terminates the comment section. */
3080 = SYNTAX_FLAGS_COMMENT_STYLE (syntax
, prev_from_syntax
);
3081 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax
);
3082 comnested
= comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
3083 state
.incomment
= comnested
? 1 : -1;
3084 state
.comstr_start
= prev_from
;
3088 else if (code
== Scomment_fence
)
3090 /* Record the comment style we have entered so that only
3091 the comment-end sequence of the same style actually
3092 terminates the comment section. */
3093 state
.comstyle
= ST_COMMENT_STYLE
;
3094 state
.incomment
= -1;
3095 state
.comstr_start
= prev_from
;
3098 else if (code
== Scomment
)
3100 state
.comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax
, 0);
3101 state
.incomment
= (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax
) ?
3103 state
.comstr_start
= prev_from
;
3106 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax
))
3108 switch (SWITCH_ENUM_CAST (code
))
3112 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3113 curlevel
->last
= prev_from
;
3115 if (from
== end
) goto endquoted
;
3118 /* treat following character as a word constituent */
3121 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3122 curlevel
->last
= prev_from
;
3126 /* Some compilers can't handle this inside the switch. */
3127 temp
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
3128 temp
= SYNTAX (temp
);
3134 if (from
== end
) goto endquoted
;
3146 curlevel
->prev
= curlevel
->last
;
3149 case Scomment_fence
: /* Can't happen because it's handled above. */
3151 if (commentstop
|| boundary_stop
) goto done
;
3153 /* The (from == BEGV) test was to enter the loop in the middle so
3154 that we find a 2-char comment ender even if we start in the
3155 middle of it. We don't want to do that if we're just at the
3156 beginning of the comment (think of (*) ... (*)). */
3157 found
= forw_comment (from
, from_byte
, end
,
3158 state
.incomment
, state
.comstyle
,
3159 (from
== BEGV
|| from
< state
.comstr_start
+ 3)
3160 ? 0 : prev_from_syntax
,
3161 &out_charpos
, &out_bytepos
, &state
.incomment
);
3162 from
= out_charpos
; from_byte
= out_bytepos
;
3163 /* Beware! prev_from and friends are invalid now.
3164 Luckily, the `done' doesn't use them and the INC_FROM
3165 sets them to a sane value without looking at them. */
3166 if (!found
) goto done
;
3168 state
.incomment
= 0;
3169 state
.comstyle
= 0; /* reset the comment style */
3170 if (boundary_stop
) goto done
;
3174 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3176 /* curlevel++->last ran into compiler bug on Apollo */
3177 curlevel
->last
= prev_from
;
3178 if (++curlevel
== endlevel
)
3179 curlevel
--; /* error ("Nesting too deep for parser"); */
3180 curlevel
->prev
= -1;
3181 curlevel
->last
= -1;
3182 if (targetdepth
== depth
) goto done
;
3187 if (depth
< mindepth
)
3189 if (curlevel
!= levelstart
)
3191 curlevel
->prev
= curlevel
->last
;
3192 if (targetdepth
== depth
) goto done
;
3197 state
.comstr_start
= from
- 1;
3198 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3199 curlevel
->last
= prev_from
;
3200 state
.instring
= (code
== Sstring
3201 ? (FETCH_CHAR_AS_MULTIBYTE (prev_from_byte
))
3203 if (boundary_stop
) goto done
;
3206 nofence
= state
.instring
!= ST_STRING_STYLE
;
3212 if (from
>= end
) goto done
;
3213 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
3214 /* Some compilers can't handle this inside the switch. */
3217 /* Check TEMP here so that if the char has
3218 a syntax-table property which says it is NOT
3219 a string character, it does not end the string. */
3220 if (nofence
&& c
== state
.instring
&& temp
== Sstring
)
3226 if (!nofence
) goto string_end
;
3231 startquotedinstring
:
3232 if (from
>= end
) goto endquoted
;
3238 state
.instring
= -1;
3239 curlevel
->prev
= curlevel
->last
;
3241 if (boundary_stop
) goto done
;
3245 /* FIXME: We should do something with it. */
3248 /* Ignore whitespace, punctuation, quote, endcomment. */
3254 stop
: /* Here if stopping before start of sexp. */
3255 from
= prev_from
; /* We have just fetched the char that starts it; */
3256 goto done
; /* but return the position before it. */
3261 state
.depth
= depth
;
3262 state
.mindepth
= mindepth
;
3263 state
.thislevelstart
= curlevel
->prev
;
3264 state
.prevlevelstart
3265 = (curlevel
== levelstart
) ? -1 : (curlevel
- 1)->last
;
3266 state
.location
= from
;
3267 state
.levelstarts
= Qnil
;
3268 while (curlevel
> levelstart
)
3269 state
.levelstarts
= Fcons (make_number ((--curlevel
)->last
),
3276 DEFUN ("parse-partial-sexp", Fparse_partial_sexp
, Sparse_partial_sexp
, 2, 6, 0,
3277 doc
: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
3278 Parsing stops at TO or when certain criteria are met;
3279 point is set to where parsing stops.
3280 If fifth arg OLDSTATE is omitted or nil,
3281 parsing assumes that FROM is the beginning of a function.
3282 Value is a list of elements describing final state of parsing:
3284 1. character address of start of innermost containing list; nil if none.
3285 2. character address of start of last complete sexp terminated.
3286 3. non-nil if inside a string.
3287 (it is the character that will terminate the string,
3288 or t if the string should be terminated by a generic string delimiter.)
3289 4. nil if outside a comment, t if inside a non-nestable comment,
3290 else an integer (the current comment nesting).
3291 5. t if following a quote character.
3292 6. the minimum paren-depth encountered during this scan.
3293 7. style of comment, if any.
3294 8. character address of start of comment or string; nil if not in one.
3295 9. Intermediate data for continuation of parsing (subject to change).
3296 If third arg TARGETDEPTH is non-nil, parsing stops if the depth
3297 in parentheses becomes equal to TARGETDEPTH.
3298 Fourth arg STOPBEFORE non-nil means stop when come to
3299 any character that starts a sexp.
3300 Fifth arg OLDSTATE is a list like what this function returns.
3301 It is used to initialize the state of the parse. Elements number 1, 2, 6
3303 Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.
3304 If it is symbol `syntax-table', stop after the start of a comment or a
3305 string, or after end of a comment or a string. */)
3306 (Lisp_Object from
, Lisp_Object to
, Lisp_Object targetdepth
, Lisp_Object stopbefore
, Lisp_Object oldstate
, Lisp_Object commentstop
)
3308 struct lisp_parse_state state
;
3311 if (!NILP (targetdepth
))
3313 CHECK_NUMBER (targetdepth
);
3314 target
= XINT (targetdepth
);
3317 target
= -100000; /* We won't reach this depth */
3319 validate_region (&from
, &to
);
3320 scan_sexps_forward (&state
, XINT (from
), CHAR_TO_BYTE (XINT (from
)),
3322 target
, !NILP (stopbefore
), oldstate
,
3324 ? 0 : (EQ (commentstop
, Qsyntax_table
) ? -1 : 1)));
3326 SET_PT (state
.location
);
3328 return Fcons (make_number (state
.depth
),
3329 Fcons (state
.prevlevelstart
< 0
3330 ? Qnil
: make_number (state
.prevlevelstart
),
3331 Fcons (state
.thislevelstart
< 0
3332 ? Qnil
: make_number (state
.thislevelstart
),
3333 Fcons (state
.instring
>= 0
3334 ? (state
.instring
== ST_STRING_STYLE
3335 ? Qt
: make_number (state
.instring
)) : Qnil
,
3336 Fcons (state
.incomment
< 0 ? Qt
:
3337 (state
.incomment
== 0 ? Qnil
:
3338 make_number (state
.incomment
)),
3339 Fcons (state
.quoted
? Qt
: Qnil
,
3340 Fcons (make_number (state
.mindepth
),
3341 Fcons ((state
.comstyle
3342 ? (state
.comstyle
== ST_COMMENT_STYLE
3344 : make_number (state
.comstyle
))
3346 Fcons (((state
.incomment
3347 || (state
.instring
>= 0))
3348 ? make_number (state
.comstr_start
)
3350 Fcons (state
.levelstarts
, Qnil
))))))))));
3354 init_syntax_once (void)
3359 /* This has to be done here, before we call Fmake_char_table. */
3360 Qsyntax_table
= intern_c_string ("syntax-table");
3361 staticpro (&Qsyntax_table
);
3363 /* Intern_C_String this now in case it isn't already done.
3364 Setting this variable twice is harmless.
3365 But don't staticpro it here--that is done in alloc.c. */
3366 Qchar_table_extra_slots
= intern_c_string ("char-table-extra-slots");
3368 /* Create objects which can be shared among syntax tables. */
3369 Vsyntax_code_object
= Fmake_vector (make_number (Smax
), Qnil
);
3370 for (i
= 0; i
< XVECTOR (Vsyntax_code_object
)->size
; i
++)
3371 XVECTOR (Vsyntax_code_object
)->contents
[i
]
3372 = Fcons (make_number (i
), Qnil
);
3374 /* Now we are ready to set up this property, so we can
3375 create syntax tables. */
3376 Fput (Qsyntax_table
, Qchar_table_extra_slots
, make_number (0));
3378 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Swhitespace
];
3380 Vstandard_syntax_table
= Fmake_char_table (Qsyntax_table
, temp
);
3382 /* Control characters should not be whitespace. */
3383 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Spunct
];
3384 for (i
= 0; i
<= ' ' - 1; i
++)
3385 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3386 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 0177, temp
);
3388 /* Except that a few really are whitespace. */
3389 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Swhitespace
];
3390 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ' ', temp
);
3391 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\t', temp
);
3392 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\n', temp
);
3393 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 015, temp
);
3394 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 014, temp
);
3396 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Sword
];
3397 for (i
= 'a'; i
<= 'z'; i
++)
3398 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3399 for (i
= 'A'; i
<= 'Z'; i
++)
3400 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3401 for (i
= '0'; i
<= '9'; i
++)
3402 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3404 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '$', temp
);
3405 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '%', temp
);
3407 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '(',
3408 Fcons (make_number (Sopen
), make_number (')')));
3409 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ')',
3410 Fcons (make_number (Sclose
), make_number ('(')));
3411 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '[',
3412 Fcons (make_number (Sopen
), make_number (']')));
3413 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ']',
3414 Fcons (make_number (Sclose
), make_number ('[')));
3415 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '{',
3416 Fcons (make_number (Sopen
), make_number ('}')));
3417 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '}',
3418 Fcons (make_number (Sclose
), make_number ('{')));
3419 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '"',
3420 Fcons (make_number ((int) Sstring
), Qnil
));
3421 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\\',
3422 Fcons (make_number ((int) Sescape
), Qnil
));
3424 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Ssymbol
];
3425 for (i
= 0; i
< 10; i
++)
3427 c
= "_-+*/&|<>="[i
];
3428 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, c
, temp
);
3431 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Spunct
];
3432 for (i
= 0; i
< 12; i
++)
3434 c
= ".,;:?!#@~^'`"[i
];
3435 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, c
, temp
);
3438 /* All multibyte characters have syntax `word' by default. */
3439 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Sword
];
3440 char_table_set_range (Vstandard_syntax_table
, 0x80, MAX_CHAR
, temp
);
3444 syms_of_syntax (void)
3446 Qsyntax_table_p
= intern_c_string ("syntax-table-p");
3447 staticpro (&Qsyntax_table_p
);
3449 staticpro (&Vsyntax_code_object
);
3451 staticpro (&gl_state
.object
);
3452 staticpro (&gl_state
.global_code
);
3453 staticpro (&gl_state
.current_syntax_table
);
3454 staticpro (&gl_state
.old_prop
);
3456 /* Defined in regex.c */
3457 staticpro (&re_match_object
);
3459 Qscan_error
= intern_c_string ("scan-error");
3460 staticpro (&Qscan_error
);
3461 Fput (Qscan_error
, Qerror_conditions
,
3462 pure_cons (Qscan_error
, pure_cons (Qerror
, Qnil
)));
3463 Fput (Qscan_error
, Qerror_message
,
3464 make_pure_c_string ("Scan error"));
3466 DEFVAR_BOOL ("parse-sexp-ignore-comments", parse_sexp_ignore_comments
,
3467 doc
: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
3469 DEFVAR_BOOL ("parse-sexp-lookup-properties", parse_sexp_lookup_properties
,
3470 doc
: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3471 Otherwise, that text property is simply ignored.
3472 See the info node `(elisp)Syntax Properties' for a description of the
3473 `syntax-table' property. */);
3475 words_include_escapes
= 0;
3476 DEFVAR_BOOL ("words-include-escapes", words_include_escapes
,
3477 doc
: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
3479 DEFVAR_BOOL ("multibyte-syntax-as-symbol", multibyte_syntax_as_symbol
,
3480 doc
: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
3481 multibyte_syntax_as_symbol
= 0;
3483 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3484 open_paren_in_column_0_is_defun_start
,
3485 doc
: /* *Non-nil means an open paren in column 0 denotes the start of a defun. */);
3486 open_paren_in_column_0_is_defun_start
= 1;
3489 DEFVAR_LISP ("find-word-boundary-function-table",
3490 Vfind_word_boundary_function_table
,
3492 Char table of functions to search for the word boundary.
3493 Each function is called with two arguments; POS and LIMIT.
3494 POS and LIMIT are character positions in the current buffer.
3496 If POS is less than LIMIT, POS is at the first character of a word,
3497 and the return value of a function is a position after the last
3498 character of that word.
3500 If POS is not less than LIMIT, POS is at the last character of a word,
3501 and the return value of a function is a position at the first
3502 character of that word.
3504 In both cases, LIMIT bounds the search. */);
3505 Vfind_word_boundary_function_table
= Fmake_char_table (Qnil
, Qnil
);
3507 defsubr (&Ssyntax_table_p
);
3508 defsubr (&Ssyntax_table
);
3509 defsubr (&Sstandard_syntax_table
);
3510 defsubr (&Scopy_syntax_table
);
3511 defsubr (&Sset_syntax_table
);
3512 defsubr (&Schar_syntax
);
3513 defsubr (&Smatching_paren
);
3514 defsubr (&Sstring_to_syntax
);
3515 defsubr (&Smodify_syntax_entry
);
3516 defsubr (&Sinternal_describe_syntax_value
);
3518 defsubr (&Sforward_word
);
3520 defsubr (&Sskip_chars_forward
);
3521 defsubr (&Sskip_chars_backward
);
3522 defsubr (&Sskip_syntax_forward
);
3523 defsubr (&Sskip_syntax_backward
);
3525 defsubr (&Sforward_comment
);
3526 defsubr (&Sscan_lists
);
3527 defsubr (&Sscan_sexps
);
3528 defsubr (&Sbackward_prefix_chars
);
3529 defsubr (&Sparse_partial_sexp
);