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/>. */
24 #include <sys/types.h>
29 #include "character.h"
33 /* Make syntax table lookup grant data in gl_state. */
34 #define SYNTAX_ENTRY_VIA_PROPERTY
37 #include "intervals.h"
40 /* Then there are seven single-bit flags that have the following meanings:
41 1. This character is the first of a two-character comment-start sequence.
42 2. This character is the second of a two-character comment-start sequence.
43 3. This character is the first of a two-character comment-end sequence.
44 4. This character is the second of a two-character comment-end sequence.
45 5. This character is a prefix, for backward-prefix-chars.
46 6. The char is part of a delimiter for comments of style "b".
47 7. This character is part of a nestable comment sequence.
48 8. The char is part of a delimiter for comments of style "c".
49 Note that any two-character sequence whose first character has flag 1
50 and whose second character has flag 2 will be interpreted as a comment start.
52 bit 6 and 8 are used to discriminate between different comment styles.
53 Languages such as C++ allow two orthogonal syntax start/end pairs
54 and bit 6 is used to determine whether a comment-end or Scommentend
55 ends style a or b. Comment markers can start style a, b, c, or bc.
56 Style a is always the default.
57 For 2-char comment markers, the style b flag is only looked up on the second
58 char of the comment marker and on the first char of the comment ender.
59 For style c (like to for the nested flag), the flag can be placed on any
63 /* These macros extract specific flags from an integer
64 that holds the syntax code and the flags. */
66 #define SYNTAX_FLAGS_COMSTART_FIRST(flags) (((flags) >> 16) & 1)
68 #define SYNTAX_FLAGS_COMSTART_SECOND(flags) (((flags) >> 17) & 1)
70 #define SYNTAX_FLAGS_COMEND_FIRST(flags) (((flags) >> 18) & 1)
72 #define SYNTAX_FLAGS_COMEND_SECOND(flags) (((flags) >> 19) & 1)
74 #define SYNTAX_FLAGS_PREFIX(flags) (((flags) >> 20) & 1)
76 #define SYNTAX_FLAGS_COMMENT_STYLEB(flags) (((flags) >> 21) & 1)
77 #define SYNTAX_FLAGS_COMMENT_STYLEC(flags) (((flags) >> 22) & 2)
78 /* FLAGS should be the flags of the main char of the comment marker, e.g.
79 the second for comstart and the first for comend. */
80 #define SYNTAX_FLAGS_COMMENT_STYLE(flags, other_flags) \
81 (SYNTAX_FLAGS_COMMENT_STYLEB (flags) \
82 | SYNTAX_FLAGS_COMMENT_STYLEC (flags) \
83 | SYNTAX_FLAGS_COMMENT_STYLEC (other_flags))
85 #define SYNTAX_FLAGS_COMMENT_NESTED(flags) (((flags) >> 22) & 1)
87 /* These macros extract a particular flag for a given character. */
89 #define SYNTAX_COMEND_FIRST(c) \
90 (SYNTAX_FLAGS_COMEND_FIRST (SYNTAX_WITH_FLAGS (c)))
91 #define SYNTAX_PREFIX(c) (SYNTAX_FLAGS_PREFIX (SYNTAX_WITH_FLAGS (c)))
93 /* We use these constants in place for comment-style and
94 string-ender-char to distinguish comments/strings started by
95 comment_fence and string_fence codes. */
97 #define ST_COMMENT_STYLE (256 + 1)
98 #define ST_STRING_STYLE (256 + 2)
100 static Lisp_Object Qsyntax_table_p
;
101 static Lisp_Object Qsyntax_table
, Qscan_error
;
104 /* Used as a temporary in SYNTAX_ENTRY and other macros in syntax.h,
105 if not compiled with GCC. No need to mark it, since it is used
106 only very temporarily. */
107 Lisp_Object syntax_temp
;
110 /* This is the internal form of the parse state used in parse-partial-sexp. */
112 struct lisp_parse_state
114 int depth
; /* Depth at end of parsing. */
115 int instring
; /* -1 if not within string, else desired terminator. */
116 int incomment
; /* -1 if in unnestable comment else comment nesting */
117 int comstyle
; /* comment style a=0, or b=1, or ST_COMMENT_STYLE. */
118 int quoted
; /* Nonzero if just after an escape char at end of parsing */
119 int mindepth
; /* Minimum depth seen while scanning. */
120 /* Char number of most recent start-of-expression at current level */
121 EMACS_INT thislevelstart
;
122 /* Char number of start of containing expression */
123 EMACS_INT prevlevelstart
;
124 EMACS_INT location
; /* Char number at which parsing stopped. */
125 EMACS_INT comstr_start
; /* Position of last comment/string starter. */
126 Lisp_Object levelstarts
; /* Char numbers of starts-of-expression
127 of levels (starting from outermost). */
130 /* These variables are a cache for finding the start of a defun.
131 find_start_pos is the place for which the defun start was found.
132 find_start_value is the defun start position found for it.
133 find_start_value_byte is the corresponding byte position.
134 find_start_buffer is the buffer it was found in.
135 find_start_begv is the BEGV value when it was found.
136 find_start_modiff is the value of MODIFF when it was found. */
138 static EMACS_INT find_start_pos
;
139 static EMACS_INT find_start_value
;
140 static EMACS_INT find_start_value_byte
;
141 static struct buffer
*find_start_buffer
;
142 static EMACS_INT find_start_begv
;
143 static int find_start_modiff
;
146 static Lisp_Object
Fsyntax_table_p (Lisp_Object
);
147 static Lisp_Object
skip_chars (int, Lisp_Object
, Lisp_Object
, int);
148 static Lisp_Object
skip_syntaxes (int, Lisp_Object
, Lisp_Object
);
149 static Lisp_Object
scan_lists (EMACS_INT
, EMACS_INT
, EMACS_INT
, int);
150 static void scan_sexps_forward (struct lisp_parse_state
*,
151 EMACS_INT
, EMACS_INT
, EMACS_INT
, int,
152 int, Lisp_Object
, int);
153 static int in_classes (int, Lisp_Object
);
155 /* Whether the syntax of the character C has the prefix flag set. */
156 int syntax_prefix_flag_p (int c
)
158 return SYNTAX_PREFIX (c
);
161 struct gl_state_s gl_state
; /* Global state of syntax parser. */
163 #define INTERVALS_AT_ONCE 10 /* 1 + max-number of intervals
164 to scan to property-change. */
166 /* Update gl_state to an appropriate interval which contains CHARPOS. The
167 sign of COUNT give the relative position of CHARPOS wrt the previously
168 valid interval. If INIT, only [be]_property fields of gl_state are
169 valid at start, the rest is filled basing on OBJECT.
171 `gl_state.*_i' are the intervals, and CHARPOS is further in the search
172 direction than the intervals - or in an interval. We update the
173 current syntax-table basing on the property of this interval, and
174 update the interval to start further than CHARPOS - or be
175 NULL_INTERVAL. We also update lim_property to be the next value of
176 charpos to call this subroutine again - or be before/after the
177 start/end of OBJECT. */
180 update_syntax_table (EMACS_INT charpos
, EMACS_INT count
, int init
,
183 Lisp_Object tmp_table
;
190 gl_state
.old_prop
= Qnil
;
191 gl_state
.start
= gl_state
.b_property
;
192 gl_state
.stop
= gl_state
.e_property
;
193 i
= interval_of (charpos
, object
);
194 gl_state
.backward_i
= gl_state
.forward_i
= i
;
196 if (NULL_INTERVAL_P (i
))
198 /* interval_of updates only ->position of the return value, so
199 update the parents manually to speed up update_interval. */
200 while (!NULL_PARENT (i
))
202 if (AM_RIGHT_CHILD (i
))
203 INTERVAL_PARENT (i
)->position
= i
->position
204 - LEFT_TOTAL_LENGTH (i
) + TOTAL_LENGTH (i
) /* right end */
205 - TOTAL_LENGTH (INTERVAL_PARENT (i
))
206 + LEFT_TOTAL_LENGTH (INTERVAL_PARENT (i
));
208 INTERVAL_PARENT (i
)->position
= i
->position
- LEFT_TOTAL_LENGTH (i
)
210 i
= INTERVAL_PARENT (i
);
212 i
= gl_state
.forward_i
;
213 gl_state
.b_property
= i
->position
- gl_state
.offset
;
214 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
217 i
= count
> 0 ? gl_state
.forward_i
: gl_state
.backward_i
;
219 /* We are guaranteed to be called with CHARPOS either in i,
221 if (NULL_INTERVAL_P (i
))
222 error ("Error in syntax_table logic for to-the-end intervals");
223 else if (charpos
< i
->position
) /* Move left. */
226 error ("Error in syntax_table logic for intervals <-");
227 /* Update the interval. */
228 i
= update_interval (i
, charpos
);
229 if (INTERVAL_LAST_POS (i
) != gl_state
.b_property
)
232 gl_state
.forward_i
= i
;
233 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
236 else if (charpos
>= INTERVAL_LAST_POS (i
)) /* Move right. */
239 error ("Error in syntax_table logic for intervals ->");
240 /* Update the interval. */
241 i
= update_interval (i
, charpos
);
242 if (i
->position
!= gl_state
.e_property
)
245 gl_state
.backward_i
= i
;
246 gl_state
.b_property
= i
->position
- gl_state
.offset
;
251 tmp_table
= textget (i
->plist
, Qsyntax_table
);
254 invalidate
= !EQ (tmp_table
, gl_state
.old_prop
); /* Need to invalidate? */
256 if (invalidate
) /* Did not get to adjacent interval. */
257 { /* with the same table => */
258 /* invalidate the old range. */
261 gl_state
.backward_i
= i
;
262 gl_state
.b_property
= i
->position
- gl_state
.offset
;
266 gl_state
.forward_i
= i
;
267 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
271 if (!EQ (tmp_table
, gl_state
.old_prop
))
273 gl_state
.current_syntax_table
= tmp_table
;
274 gl_state
.old_prop
= tmp_table
;
275 if (EQ (Fsyntax_table_p (tmp_table
), Qt
))
277 gl_state
.use_global
= 0;
279 else if (CONSP (tmp_table
))
281 gl_state
.use_global
= 1;
282 gl_state
.global_code
= tmp_table
;
286 gl_state
.use_global
= 0;
287 gl_state
.current_syntax_table
= BVAR (current_buffer
, syntax_table
);
291 while (!NULL_INTERVAL_P (i
))
293 if (cnt
&& !EQ (tmp_table
, textget (i
->plist
, Qsyntax_table
)))
297 gl_state
.e_property
= i
->position
- gl_state
.offset
;
298 gl_state
.forward_i
= i
;
303 = i
->position
+ LENGTH (i
) - gl_state
.offset
;
304 gl_state
.backward_i
= i
;
308 else if (cnt
== INTERVALS_AT_ONCE
)
313 = i
->position
+ LENGTH (i
) - gl_state
.offset
314 /* e_property at EOB is not set to ZV but to ZV+1, so that
315 we can do INC(from);UPDATE_SYNTAX_TABLE_FORWARD without
316 having to check eob between the two. */
317 + (NULL_INTERVAL_P (next_interval (i
)) ? 1 : 0);
318 gl_state
.forward_i
= i
;
322 gl_state
.b_property
= i
->position
- gl_state
.offset
;
323 gl_state
.backward_i
= i
;
328 i
= count
> 0 ? next_interval (i
) : previous_interval (i
);
330 eassert (NULL_INTERVAL_P (i
)); /* This property goes to the end. */
332 gl_state
.e_property
= gl_state
.stop
;
334 gl_state
.b_property
= gl_state
.start
;
337 /* Returns TRUE if char at CHARPOS is quoted.
338 Global syntax-table data should be set up already to be good at CHARPOS
339 or after. On return global syntax data is good for lookup at CHARPOS. */
342 char_quoted (EMACS_INT charpos
, EMACS_INT bytepos
)
344 register enum syntaxcode code
;
345 register EMACS_INT beg
= BEGV
;
346 register int quoted
= 0;
347 EMACS_INT orig
= charpos
;
349 while (charpos
> beg
)
352 DEC_BOTH (charpos
, bytepos
);
354 UPDATE_SYNTAX_TABLE_BACKWARD (charpos
);
355 c
= FETCH_CHAR_AS_MULTIBYTE (bytepos
);
357 if (! (code
== Scharquote
|| code
== Sescape
))
363 UPDATE_SYNTAX_TABLE (orig
);
367 /* Return the bytepos one character before BYTEPOS.
368 We assume that BYTEPOS is not at the start of the buffer. */
370 static inline EMACS_INT
371 dec_bytepos (EMACS_INT bytepos
)
373 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
380 /* Return a defun-start position before POS and not too far before.
381 It should be the last one before POS, or nearly the last.
383 When open_paren_in_column_0_is_defun_start is nonzero,
384 only the beginning of the buffer is treated as a defun-start.
386 We record the information about where the scan started
387 and what its result was, so that another call in the same area
388 can return the same value very quickly.
390 There is no promise at which position the global syntax data is
391 valid on return from the subroutine, so the caller should explicitly
392 update the global data. */
395 find_defun_start (EMACS_INT pos
, EMACS_INT pos_byte
)
397 EMACS_INT opoint
= PT
, opoint_byte
= PT_BYTE
;
399 if (!open_paren_in_column_0_is_defun_start
)
401 find_start_value_byte
= BEGV_BYTE
;
405 /* Use previous finding, if it's valid and applies to this inquiry. */
406 if (current_buffer
== find_start_buffer
407 /* Reuse the defun-start even if POS is a little farther on.
408 POS might be in the next defun, but that's ok.
409 Our value may not be the best possible, but will still be usable. */
410 && pos
<= find_start_pos
+ 1000
411 && pos
>= find_start_value
412 && BEGV
== find_start_begv
413 && MODIFF
== find_start_modiff
)
414 return find_start_value
;
416 /* Back up to start of line. */
417 scan_newline (pos
, pos_byte
, BEGV
, BEGV_BYTE
, -1, 1);
419 /* We optimize syntax-table lookup for rare updates. Thus we accept
420 only those `^\s(' which are good in global _and_ text-property
422 SETUP_BUFFER_SYNTAX_TABLE ();
427 /* Open-paren at start of line means we may have found our
429 c
= FETCH_CHAR_AS_MULTIBYTE (PT_BYTE
);
430 if (SYNTAX (c
) == Sopen
)
432 SETUP_SYNTAX_TABLE (PT
+ 1, -1); /* Try again... */
433 c
= FETCH_CHAR_AS_MULTIBYTE (PT_BYTE
);
434 if (SYNTAX (c
) == Sopen
)
436 /* Now fallback to the default value. */
437 SETUP_BUFFER_SYNTAX_TABLE ();
439 /* Move to beg of previous line. */
440 scan_newline (PT
, PT_BYTE
, BEGV
, BEGV_BYTE
, -2, 1);
443 /* Record what we found, for the next try. */
444 find_start_value
= PT
;
445 find_start_value_byte
= PT_BYTE
;
446 find_start_buffer
= current_buffer
;
447 find_start_modiff
= MODIFF
;
448 find_start_begv
= BEGV
;
449 find_start_pos
= pos
;
451 TEMP_SET_PT_BOTH (opoint
, opoint_byte
);
453 return find_start_value
;
456 /* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
459 prev_char_comend_first (EMACS_INT pos
, EMACS_INT pos_byte
)
463 DEC_BOTH (pos
, pos_byte
);
464 UPDATE_SYNTAX_TABLE_BACKWARD (pos
);
465 c
= FETCH_CHAR (pos_byte
);
466 val
= SYNTAX_COMEND_FIRST (c
);
467 UPDATE_SYNTAX_TABLE_FORWARD (pos
+ 1);
471 /* Return the SYNTAX_COMSTART_FIRST of the character before POS, POS_BYTE. */
474 * prev_char_comstart_first (pos, pos_byte)
479 * DEC_BOTH (pos, pos_byte);
480 * UPDATE_SYNTAX_TABLE_BACKWARD (pos);
481 * c = FETCH_CHAR (pos_byte);
482 * val = SYNTAX_COMSTART_FIRST (c);
483 * UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
487 /* Checks whether charpos FROM is at the end of a comment.
488 FROM_BYTE is the bytepos corresponding to FROM.
489 Do not move back before STOP.
491 Return a positive value if we find a comment ending at FROM/FROM_BYTE;
494 If successful, store the charpos of the comment's beginning
495 into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR.
497 Global syntax data remains valid for backward search starting at
498 the returned value (or at FROM, if the search was not successful). */
501 back_comment (EMACS_INT from
, EMACS_INT from_byte
, EMACS_INT stop
, int comnested
, int comstyle
, EMACS_INT
*charpos_ptr
, EMACS_INT
*bytepos_ptr
)
503 /* Look back, counting the parity of string-quotes,
504 and recording the comment-starters seen.
505 When we reach a safe place, assume that's not in a string;
506 then step the main scan to the earliest comment-starter seen
507 an even number of string quotes away from the safe place.
509 OFROM[I] is position of the earliest comment-starter seen
510 which is I+2X quotes from the comment-end.
511 PARITY is current parity of quotes from the comment end. */
512 int string_style
= -1; /* Presumed outside of any string. */
513 int string_lossage
= 0;
514 /* Not a real lossage: indicates that we have passed a matching comment
515 starter plus a non-matching comment-ender, meaning that any matching
516 comment-starter we might see later could be a false positive (hidden
517 inside another comment).
518 Test case: { a (* b } c (* d *) */
519 int comment_lossage
= 0;
520 EMACS_INT comment_end
= from
;
521 EMACS_INT comment_end_byte
= from_byte
;
522 EMACS_INT comstart_pos
= 0;
523 EMACS_INT comstart_byte
IF_LINT (= 0);
524 /* Place where the containing defun starts,
525 or 0 if we didn't come across it yet. */
526 EMACS_INT defun_start
= 0;
527 EMACS_INT defun_start_byte
= 0;
528 register enum syntaxcode code
;
529 int nesting
= 1; /* current comment nesting */
533 /* FIXME: A }} comment-ender style leads to incorrect behavior
534 in the case of {{ c }}} because we ignore the last two chars which are
535 assumed to be comment-enders although they aren't. */
537 /* At beginning of range to scan, we're outside of strings;
538 that determines quote parity to the comment-end. */
542 int prev_syntax
, com2start
, com2end
;
545 /* Move back and examine a character. */
546 DEC_BOTH (from
, from_byte
);
547 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
549 prev_syntax
= syntax
;
550 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
551 syntax
= SYNTAX_WITH_FLAGS (c
);
554 /* Check for 2-char comment markers. */
555 com2start
= (SYNTAX_FLAGS_COMSTART_FIRST (syntax
)
556 && SYNTAX_FLAGS_COMSTART_SECOND (prev_syntax
)
558 == SYNTAX_FLAGS_COMMENT_STYLE (prev_syntax
, syntax
))
559 && (SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax
)
560 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
)) == comnested
);
561 com2end
= (SYNTAX_FLAGS_COMEND_FIRST (syntax
)
562 && SYNTAX_FLAGS_COMEND_SECOND (prev_syntax
));
563 comstart
= (com2start
|| code
== Scomment
);
565 /* Nasty cases with overlapping 2-char comment markers:
566 - snmp-mode: -- c -- foo -- c --
574 /* If a 2-char comment sequence partly overlaps with another,
575 we don't try to be clever. E.g. |*| in C, or }% in modes that
576 have %..\n and %{..}%. */
577 if (from
> stop
&& (com2end
|| comstart
))
579 EMACS_INT next
= from
, next_byte
= from_byte
;
580 int next_c
, next_syntax
;
581 DEC_BOTH (next
, next_byte
);
582 UPDATE_SYNTAX_TABLE_BACKWARD (next
);
583 next_c
= FETCH_CHAR_AS_MULTIBYTE (next_byte
);
584 next_syntax
= SYNTAX_WITH_FLAGS (next_c
);
585 if (((comstart
|| comnested
)
586 && SYNTAX_FLAGS_COMEND_SECOND (syntax
)
587 && SYNTAX_FLAGS_COMEND_FIRST (next_syntax
))
588 || ((com2end
|| comnested
)
589 && SYNTAX_FLAGS_COMSTART_SECOND (syntax
)
591 == SYNTAX_FLAGS_COMMENT_STYLE (syntax
, prev_syntax
))
592 && SYNTAX_FLAGS_COMSTART_FIRST (next_syntax
)))
594 /* UPDATE_SYNTAX_TABLE_FORWARD (next + 1); */
597 if (com2start
&& comstart_pos
== 0)
598 /* We're looking at a comment starter. But it might be a comment
599 ender as well (see snmp-mode). The first time we see one, we
600 need to consider it as a comment starter,
601 and the subsequent times as a comment ender. */
604 /* Turn a 2-char comment sequences into the appropriate syntax. */
609 /* Ignore comment starters of a different style. */
610 else if (code
== Scomment
611 && (comstyle
!= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0)
612 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
) != comnested
))
615 /* Ignore escaped characters, except comment-enders. */
616 if (code
!= Sendcomment
&& char_quoted (from
, from_byte
))
623 c
= (code
== Sstring_fence
? ST_STRING_STYLE
: ST_COMMENT_STYLE
);
625 /* Track parity of quotes. */
626 if (string_style
== -1)
627 /* Entering a string. */
629 else if (string_style
== c
)
630 /* Leaving the string. */
633 /* If we have two kinds of string delimiters.
634 There's no way to grok this scanning backwards. */
639 /* We've already checked that it is the relevant comstyle. */
640 if (string_style
!= -1 || comment_lossage
|| string_lossage
)
641 /* There are odd string quotes involved, so let's be careful.
642 Test case in Pascal: " { " a { " } */
647 /* Record best comment-starter so far. */
649 comstart_byte
= from_byte
;
651 else if (--nesting
<= 0)
652 /* nested comments have to be balanced, so we don't need to
653 keep looking for earlier ones. We use here the same (slightly
654 incorrect) reasoning as below: since it is followed by uniform
655 paired string quotes, this comment-start has to be outside of
656 strings, else the comment-end itself would be inside a string. */
661 if (SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == comstyle
662 && ((com2end
&& SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax
))
663 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
)) == comnested
)
664 /* This is the same style of comment ender as ours. */
669 /* Anything before that can't count because it would match
670 this comment-ender rather than ours. */
671 from
= stop
; /* Break out of the loop. */
673 else if (comstart_pos
!= 0 || c
!= '\n')
674 /* We're mixing comment styles here, so we'd better be careful.
675 The (comstart_pos != 0 || c != '\n') check is not quite correct
676 (we should just always set comment_lossage), but removing it
677 would imply that any multiline comment in C would go through
678 lossage, which seems overkill.
679 The failure should only happen in the rare cases such as
685 /* Assume a defun-start point is outside of strings. */
686 if (open_paren_in_column_0_is_defun_start
688 || (temp_byte
= dec_bytepos (from_byte
),
689 FETCH_CHAR (temp_byte
) == '\n')))
692 defun_start_byte
= from_byte
;
693 from
= stop
; /* Break out of the loop. */
702 if (comstart_pos
== 0)
705 from_byte
= comment_end_byte
;
706 UPDATE_SYNTAX_TABLE_FORWARD (comment_end
- 1);
708 /* If comstart_pos is set and we get here (ie. didn't jump to `lossage'
709 or `done'), then we've found the beginning of the non-nested comment. */
710 else if (1) /* !comnested */
713 from_byte
= comstart_byte
;
714 UPDATE_SYNTAX_TABLE_FORWARD (from
- 1);
718 struct lisp_parse_state state
;
720 /* We had two kinds of string delimiters mixed up
721 together. Decode this going forwards.
722 Scan fwd from a known safe place (beginning-of-defun)
723 to the one in question; this records where we
724 last passed a comment starter. */
725 /* If we did not already find the defun start, find it now. */
726 if (defun_start
== 0)
728 defun_start
= find_defun_start (comment_end
, comment_end_byte
);
729 defun_start_byte
= find_start_value_byte
;
733 scan_sexps_forward (&state
,
734 defun_start
, defun_start_byte
,
735 comment_end
, -10000, 0, Qnil
, 0);
736 defun_start
= comment_end
;
737 if (state
.incomment
== (comnested
? 1 : -1)
738 && state
.comstyle
== comstyle
)
739 from
= state
.comstr_start
;
744 /* If comment_end is inside some other comment, maybe ours
745 is nested, so we need to try again from within the
746 surrounding comment. Example: { a (* " *) */
748 /* FIXME: We should advance by one or two chars. */
749 defun_start
= state
.comstr_start
+ 2;
750 defun_start_byte
= CHAR_TO_BYTE (defun_start
);
753 } while (defun_start
< comment_end
);
755 from_byte
= CHAR_TO_BYTE (from
);
756 UPDATE_SYNTAX_TABLE_FORWARD (from
- 1);
761 *bytepos_ptr
= from_byte
;
763 return (from
== comment_end
) ? -1 : from
;
766 DEFUN ("syntax-table-p", Fsyntax_table_p
, Ssyntax_table_p
, 1, 1, 0,
767 doc
: /* Return t if OBJECT is a syntax table.
768 Currently, any char-table counts as a syntax table. */)
771 if (CHAR_TABLE_P (object
)
772 && EQ (XCHAR_TABLE (object
)->purpose
, Qsyntax_table
))
778 check_syntax_table (Lisp_Object obj
)
780 CHECK_TYPE (CHAR_TABLE_P (obj
) && EQ (XCHAR_TABLE (obj
)->purpose
, Qsyntax_table
),
781 Qsyntax_table_p
, obj
);
784 DEFUN ("syntax-table", Fsyntax_table
, Ssyntax_table
, 0, 0, 0,
785 doc
: /* Return the current syntax table.
786 This is the one specified by the current buffer. */)
789 return BVAR (current_buffer
, syntax_table
);
792 DEFUN ("standard-syntax-table", Fstandard_syntax_table
,
793 Sstandard_syntax_table
, 0, 0, 0,
794 doc
: /* Return the standard syntax table.
795 This is the one used for new buffers. */)
798 return Vstandard_syntax_table
;
801 DEFUN ("copy-syntax-table", Fcopy_syntax_table
, Scopy_syntax_table
, 0, 1, 0,
802 doc
: /* Construct a new syntax table and return it.
803 It is a copy of the TABLE, which defaults to the standard syntax table. */)
809 check_syntax_table (table
);
811 table
= Vstandard_syntax_table
;
813 copy
= Fcopy_sequence (table
);
815 /* Only the standard syntax table should have a default element.
816 Other syntax tables should inherit from parents instead. */
817 XCHAR_TABLE (copy
)->defalt
= Qnil
;
819 /* Copied syntax tables should all have parents.
820 If we copied one with no parent, such as the standard syntax table,
821 use the standard syntax table as the copy's parent. */
822 if (NILP (XCHAR_TABLE (copy
)->parent
))
823 Fset_char_table_parent (copy
, Vstandard_syntax_table
);
827 DEFUN ("set-syntax-table", Fset_syntax_table
, Sset_syntax_table
, 1, 1, 0,
828 doc
: /* Select a new syntax table for the current buffer.
829 One argument, a syntax table. */)
833 check_syntax_table (table
);
834 BVAR (current_buffer
, syntax_table
) = table
;
835 /* Indicate that this buffer now has a specified syntax table. */
836 idx
= PER_BUFFER_VAR_IDX (syntax_table
);
837 SET_PER_BUFFER_VALUE_P (current_buffer
, idx
, 1);
841 /* Convert a letter which signifies a syntax code
842 into the code it signifies.
843 This is used by modify-syntax-entry, and other things. */
845 unsigned char syntax_spec_code
[0400] =
846 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
847 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
848 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
849 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
850 (char) Swhitespace
, (char) Scomment_fence
, (char) Sstring
, 0377,
851 (char) Smath
, 0377, 0377, (char) Squote
,
852 (char) Sopen
, (char) Sclose
, 0377, 0377,
853 0377, (char) Swhitespace
, (char) Spunct
, (char) Scharquote
,
854 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
855 0377, 0377, 0377, 0377,
856 (char) Scomment
, 0377, (char) Sendcomment
, 0377,
857 (char) Sinherit
, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
858 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
859 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword
,
860 0377, 0377, 0377, 0377, (char) Sescape
, 0377, 0377, (char) Ssymbol
,
861 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
862 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
863 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword
,
864 0377, 0377, 0377, 0377, (char) Sstring_fence
, 0377, 0377, 0377
867 /* Indexed by syntax code, give the letter that describes it. */
869 char syntax_code_spec
[16] =
871 ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@',
875 /* Indexed by syntax code, give the object (cons of syntax code and
876 nil) to be stored in syntax table. Since these objects can be
877 shared among syntax tables, we generate them in advance. By
878 sharing objects, the function `describe-syntax' can give a more
880 static Lisp_Object Vsyntax_code_object
;
883 DEFUN ("char-syntax", Fchar_syntax
, Schar_syntax
, 1, 1, 0,
884 doc
: /* Return the syntax code of CHARACTER, described by a character.
885 For example, if CHARACTER is a word constituent, the
886 character `w' (119) is returned.
887 The characters that correspond to various syntax codes
888 are listed in the documentation of `modify-syntax-entry'. */)
889 (Lisp_Object character
)
892 CHECK_CHARACTER (character
);
893 char_int
= XINT (character
);
894 SETUP_BUFFER_SYNTAX_TABLE ();
895 return make_number (syntax_code_spec
[(int) SYNTAX (char_int
)]);
898 DEFUN ("matching-paren", Fmatching_paren
, Smatching_paren
, 1, 1, 0,
899 doc
: /* Return the matching parenthesis of CHARACTER, or nil if none. */)
900 (Lisp_Object character
)
903 CHECK_NUMBER (character
);
904 char_int
= XINT (character
);
905 SETUP_BUFFER_SYNTAX_TABLE ();
906 code
= SYNTAX (char_int
);
907 if (code
== Sopen
|| code
== Sclose
)
908 return SYNTAX_MATCH (char_int
);
912 DEFUN ("string-to-syntax", Fstring_to_syntax
, Sstring_to_syntax
, 1, 1, 0,
913 doc
: /* Convert a syntax specification STRING into syntax cell form.
914 STRING should be a string as it is allowed as argument of
915 `modify-syntax-entry'. Value is the equivalent cons cell
916 \(CODE . MATCHING-CHAR) that can be used as value of a `syntax-table'
920 register const unsigned char *p
;
921 register enum syntaxcode code
;
925 CHECK_STRING (string
);
928 code
= (enum syntaxcode
) syntax_spec_code
[*p
++];
929 if (((int) code
& 0377) == 0377)
930 error ("Invalid syntax description letter: %c", p
[-1]);
932 if (code
== Sinherit
)
938 int character
= STRING_CHAR_AND_LENGTH (p
, len
);
939 XSETINT (match
, character
);
940 if (XFASTINT (match
) == ' ')
984 if (val
< ASIZE (Vsyntax_code_object
) && NILP (match
))
985 return XVECTOR (Vsyntax_code_object
)->contents
[val
];
987 /* Since we can't use a shared object, let's make a new one. */
988 return Fcons (make_number (val
), match
);
991 /* I really don't know why this is interactive
992 help-form should at least be made useful whilst reading the second arg. */
993 DEFUN ("modify-syntax-entry", Fmodify_syntax_entry
, Smodify_syntax_entry
, 2, 3,
994 "cSet syntax for character: \nsSet syntax for %s to: ",
995 doc
: /* Set syntax for character CHAR according to string NEWENTRY.
996 The syntax is changed only for table SYNTAX-TABLE, which defaults to
997 the current buffer's syntax table.
998 CHAR may be a cons (MIN . MAX), in which case, syntaxes of all characters
999 in the range MIN to MAX are changed.
1000 The first character of NEWENTRY should be one of the following:
1001 Space or - whitespace syntax. w word constituent.
1002 _ symbol constituent. . punctuation.
1003 ( open-parenthesis. ) close-parenthesis.
1004 " string quote. \\ escape.
1005 $ paired delimiter. ' expression quote or prefix operator.
1006 < comment starter. > comment ender.
1007 / character-quote. @ inherit from `standard-syntax-table'.
1008 | generic string fence. ! generic comment fence.
1010 Only single-character comment start and end sequences are represented thus.
1011 Two-character sequences are represented as described below.
1012 The second character of NEWENTRY is the matching parenthesis,
1013 used only if the first character is `(' or `)'.
1014 Any additional characters are flags.
1015 Defined flags are the characters 1, 2, 3, 4, b, p, and n.
1016 1 means CHAR is the start of a two-char comment start sequence.
1017 2 means CHAR is the second character of such a sequence.
1018 3 means CHAR is the start of a two-char comment end sequence.
1019 4 means CHAR is the second character of such a sequence.
1021 There can be several orthogonal comment sequences. This is to support
1022 language modes such as C++. By default, all comment sequences are of style
1023 a, but you can set the comment sequence style to b (on the second character
1024 of a comment-start, and the first character of a comment-end sequence) and/or
1025 c (on any of its chars) using this flag:
1026 b means CHAR is part of comment sequence b.
1027 c means CHAR is part of comment sequence c.
1028 n means CHAR is part of a nestable comment sequence.
1030 p means CHAR is a prefix character for `backward-prefix-chars';
1031 such characters are treated as whitespace when they occur
1032 between expressions.
1033 usage: (modify-syntax-entry CHAR NEWENTRY &optional SYNTAX-TABLE) */)
1034 (Lisp_Object c
, Lisp_Object newentry
, Lisp_Object syntax_table
)
1038 CHECK_CHARACTER_CAR (c
);
1039 CHECK_CHARACTER_CDR (c
);
1042 CHECK_CHARACTER (c
);
1044 if (NILP (syntax_table
))
1045 syntax_table
= BVAR (current_buffer
, syntax_table
);
1047 check_syntax_table (syntax_table
);
1049 newentry
= Fstring_to_syntax (newentry
);
1051 SET_RAW_SYNTAX_ENTRY_RANGE (syntax_table
, c
, newentry
);
1053 SET_RAW_SYNTAX_ENTRY (syntax_table
, XINT (c
), newentry
);
1055 /* We clear the regexp cache, since character classes can now have
1056 different values from those in the compiled regexps.*/
1057 clear_regexp_cache ();
1062 /* Dump syntax table to buffer in human-readable format */
1064 DEFUN ("internal-describe-syntax-value", Finternal_describe_syntax_value
,
1065 Sinternal_describe_syntax_value
, 1, 1, 0,
1066 doc
: /* Insert a description of the internal syntax description SYNTAX at point. */)
1067 (Lisp_Object syntax
)
1069 register enum syntaxcode code
;
1071 char desc
, start1
, start2
, end1
, end2
, prefix
,
1072 comstyleb
, comstylec
, comnested
;
1074 Lisp_Object first
, match_lisp
, value
= syntax
;
1078 insert_string ("default");
1082 if (CHAR_TABLE_P (value
))
1084 insert_string ("deeper char-table ...");
1090 insert_string ("invalid");
1094 first
= XCAR (value
);
1095 match_lisp
= XCDR (value
);
1097 if (!INTEGERP (first
) || !(NILP (match_lisp
) || INTEGERP (match_lisp
)))
1099 insert_string ("invalid");
1103 syntax_code
= XINT (first
);
1104 code
= (enum syntaxcode
) (syntax_code
& 0377);
1105 start1
= SYNTAX_FLAGS_COMSTART_FIRST (syntax_code
);
1106 start2
= SYNTAX_FLAGS_COMSTART_SECOND (syntax_code
);;
1107 end1
= SYNTAX_FLAGS_COMEND_FIRST (syntax_code
);
1108 end2
= SYNTAX_FLAGS_COMEND_SECOND (syntax_code
);
1109 prefix
= SYNTAX_FLAGS_PREFIX (syntax_code
);
1110 comstyleb
= SYNTAX_FLAGS_COMMENT_STYLEB (syntax_code
);
1111 comstylec
= SYNTAX_FLAGS_COMMENT_STYLEC (syntax_code
);
1112 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax_code
);
1114 if ((int) code
< 0 || (int) code
>= (int) Smax
)
1116 insert_string ("invalid");
1119 desc
= syntax_code_spec
[(int) code
];
1121 str
[0] = desc
, str
[1] = 0;
1124 if (NILP (match_lisp
))
1127 insert_char (XINT (match_lisp
));
1148 insert_string ("\twhich means: ");
1150 switch (SWITCH_ENUM_CAST (code
))
1153 insert_string ("whitespace"); break;
1155 insert_string ("punctuation"); break;
1157 insert_string ("word"); break;
1159 insert_string ("symbol"); break;
1161 insert_string ("open"); break;
1163 insert_string ("close"); break;
1165 insert_string ("prefix"); break;
1167 insert_string ("string"); break;
1169 insert_string ("math"); break;
1171 insert_string ("escape"); break;
1173 insert_string ("charquote"); break;
1175 insert_string ("comment"); break;
1177 insert_string ("endcomment"); break;
1179 insert_string ("inherit"); break;
1180 case Scomment_fence
:
1181 insert_string ("comment fence"); break;
1183 insert_string ("string fence"); break;
1185 insert_string ("invalid");
1189 if (!NILP (match_lisp
))
1191 insert_string (", matches ");
1192 insert_char (XINT (match_lisp
));
1196 insert_string (",\n\t is the first character of a comment-start sequence");
1198 insert_string (",\n\t is the second character of a comment-start sequence");
1201 insert_string (",\n\t is the first character of a comment-end sequence");
1203 insert_string (",\n\t is the second character of a comment-end sequence");
1205 insert_string (" (comment style b)");
1207 insert_string (" (comment style c)");
1209 insert_string (" (nestable)");
1212 insert_string (",\n\t is a prefix character for `backward-prefix-chars'");
1217 /* Return the position across COUNT words from FROM.
1218 If that many words cannot be found before the end of the buffer, return 0.
1219 COUNT negative means scan backward and stop at word beginning. */
1222 scan_words (register EMACS_INT from
, register EMACS_INT count
)
1224 register EMACS_INT beg
= BEGV
;
1225 register EMACS_INT end
= ZV
;
1226 register EMACS_INT from_byte
= CHAR_TO_BYTE (from
);
1227 register enum syntaxcode code
;
1229 Lisp_Object func
, pos
;
1234 SETUP_SYNTAX_TABLE (from
, count
);
1245 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1246 ch0
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1247 code
= SYNTAX (ch0
);
1248 INC_BOTH (from
, from_byte
);
1249 if (words_include_escapes
1250 && (code
== Sescape
|| code
== Scharquote
))
1255 /* Now CH0 is a character which begins a word and FROM is the
1256 position of the next character. */
1257 func
= CHAR_TABLE_REF (Vfind_word_boundary_function_table
, ch0
);
1258 if (! NILP (Ffboundp (func
)))
1260 pos
= call2 (func
, make_number (from
- 1), make_number (end
));
1261 if (INTEGERP (pos
) && XINT (pos
) > from
)
1264 from_byte
= CHAR_TO_BYTE (from
);
1271 if (from
== end
) break;
1272 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1273 ch1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1274 code
= SYNTAX (ch1
);
1276 && (! words_include_escapes
1277 || (code
!= Sescape
&& code
!= Scharquote
)))
1278 || word_boundary_p (ch0
, ch1
))
1280 INC_BOTH (from
, from_byte
);
1295 DEC_BOTH (from
, from_byte
);
1296 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
1297 ch1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1298 code
= SYNTAX (ch1
);
1299 if (words_include_escapes
1300 && (code
== Sescape
|| code
== Scharquote
))
1305 /* Now CH1 is a character which ends a word and FROM is the
1307 func
= CHAR_TABLE_REF (Vfind_word_boundary_function_table
, ch1
);
1308 if (! NILP (Ffboundp (func
)))
1310 pos
= call2 (func
, make_number (from
), make_number (beg
));
1311 if (INTEGERP (pos
) && XINT (pos
) < from
)
1314 from_byte
= CHAR_TO_BYTE (from
);
1323 DEC_BOTH (from
, from_byte
);
1324 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
1325 ch0
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1326 code
= SYNTAX (ch0
);
1328 && (! words_include_escapes
1329 || (code
!= Sescape
&& code
!= Scharquote
)))
1330 || word_boundary_p (ch0
, ch1
))
1332 INC_BOTH (from
, from_byte
);
1346 DEFUN ("forward-word", Fforward_word
, Sforward_word
, 0, 1, "^p",
1347 doc
: /* Move point forward ARG words (backward if ARG is negative).
1349 If an edge of the buffer or a field boundary is reached, point is left there
1350 and the function returns nil. Field boundaries are not noticed if
1351 `inhibit-field-text-motion' is non-nil. */)
1358 XSETFASTINT (arg
, 1);
1362 val
= orig_val
= scan_words (PT
, XINT (arg
));
1364 val
= XINT (arg
) > 0 ? ZV
: BEGV
;
1366 /* Avoid jumping out of an input field. */
1367 tmp
= Fconstrain_to_field (make_number (val
), make_number (PT
),
1369 val
= XFASTINT (tmp
);
1372 return val
== orig_val
? Qt
: Qnil
;
1375 DEFUN ("skip-chars-forward", Fskip_chars_forward
, Sskip_chars_forward
, 1, 2, 0,
1376 doc
: /* Move point forward, stopping before a char not in STRING, or at pos LIM.
1377 STRING is like the inside of a `[...]' in a regular expression
1378 except that `]' is never special and `\\' quotes `^', `-' or `\\'
1379 (but not at the end of a range; quoting is never needed there).
1380 Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
1381 With arg "^a-zA-Z", skips nonletters stopping before first letter.
1382 Char classes, e.g. `[:alpha:]', are supported.
1384 Returns the distance traveled, either zero or positive. */)
1385 (Lisp_Object string
, Lisp_Object lim
)
1387 return skip_chars (1, string
, lim
, 1);
1390 DEFUN ("skip-chars-backward", Fskip_chars_backward
, Sskip_chars_backward
, 1, 2, 0,
1391 doc
: /* Move point backward, stopping after a char not in STRING, or at pos LIM.
1392 See `skip-chars-forward' for details.
1393 Returns the distance traveled, either zero or negative. */)
1394 (Lisp_Object string
, Lisp_Object lim
)
1396 return skip_chars (0, string
, lim
, 1);
1399 DEFUN ("skip-syntax-forward", Fskip_syntax_forward
, Sskip_syntax_forward
, 1, 2, 0,
1400 doc
: /* Move point forward across chars in specified syntax classes.
1401 SYNTAX is a string of syntax code characters.
1402 Stop before a char whose syntax is not in SYNTAX, or at position LIM.
1403 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1404 This function returns the distance traveled, either zero or positive. */)
1405 (Lisp_Object syntax
, Lisp_Object lim
)
1407 return skip_syntaxes (1, syntax
, lim
);
1410 DEFUN ("skip-syntax-backward", Fskip_syntax_backward
, Sskip_syntax_backward
, 1, 2, 0,
1411 doc
: /* Move point backward across chars in specified syntax classes.
1412 SYNTAX is a string of syntax code characters.
1413 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.
1414 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1415 This function returns the distance traveled, either zero or negative. */)
1416 (Lisp_Object syntax
, Lisp_Object lim
)
1418 return skip_syntaxes (0, syntax
, lim
);
1422 skip_chars (int forwardp
, Lisp_Object string
, Lisp_Object lim
, int handle_iso_classes
)
1424 register unsigned int c
;
1425 unsigned char fastmap
[0400];
1426 /* Store the ranges of non-ASCII characters. */
1427 int *char_ranges
IF_LINT (= NULL
);
1428 int n_char_ranges
= 0;
1430 register EMACS_INT i
, i_byte
;
1431 /* Set to 1 if the current buffer is multibyte and the region
1432 contains non-ASCII chars. */
1434 /* Set to 1 if STRING is multibyte and it contains non-ASCII
1436 int string_multibyte
;
1437 EMACS_INT size_byte
;
1438 const unsigned char *str
;
1440 Lisp_Object iso_classes
;
1442 CHECK_STRING (string
);
1446 XSETINT (lim
, forwardp
? ZV
: BEGV
);
1448 CHECK_NUMBER_COERCE_MARKER (lim
);
1450 /* In any case, don't allow scan outside bounds of buffer. */
1451 if (XINT (lim
) > ZV
)
1452 XSETFASTINT (lim
, ZV
);
1453 if (XINT (lim
) < BEGV
)
1454 XSETFASTINT (lim
, BEGV
);
1456 multibyte
= (!NILP (BVAR (current_buffer
, enable_multibyte_characters
))
1457 && (XINT (lim
) - PT
!= CHAR_TO_BYTE (XINT (lim
)) - PT_BYTE
));
1458 string_multibyte
= SBYTES (string
) > SCHARS (string
);
1460 memset (fastmap
, 0, sizeof fastmap
);
1462 str
= SDATA (string
);
1463 size_byte
= SBYTES (string
);
1466 if (i_byte
< size_byte
1467 && SREF (string
, 0) == '^')
1469 negate
= 1; i_byte
++;
1472 /* Find the characters specified and set their elements of fastmap.
1473 Handle backslashes and ranges specially.
1475 If STRING contains non-ASCII characters, setup char_ranges for
1476 them and use fastmap only for their leading codes. */
1478 if (! string_multibyte
)
1480 int string_has_eight_bit
= 0;
1482 /* At first setup fastmap. */
1483 while (i_byte
< size_byte
)
1487 if (handle_iso_classes
&& c
== '['
1488 && i_byte
< size_byte
1489 && str
[i_byte
] == ':')
1491 const unsigned char *class_beg
= str
+ i_byte
+ 1;
1492 const unsigned char *class_end
= class_beg
;
1493 const unsigned char *class_limit
= str
+ size_byte
- 2;
1494 /* Leave room for the null. */
1495 unsigned char class_name
[CHAR_CLASS_MAX_LENGTH
+ 1];
1498 if (class_limit
- class_beg
> CHAR_CLASS_MAX_LENGTH
)
1499 class_limit
= class_beg
+ CHAR_CLASS_MAX_LENGTH
;
1501 while (class_end
< class_limit
1502 && *class_end
>= 'a' && *class_end
<= 'z')
1505 if (class_end
== class_beg
1506 || *class_end
!= ':' || class_end
[1] != ']')
1507 goto not_a_class_name
;
1509 memcpy (class_name
, class_beg
, class_end
- class_beg
);
1510 class_name
[class_end
- class_beg
] = 0;
1512 cc
= re_wctype (class_name
);
1514 error ("Invalid ISO C character class");
1516 iso_classes
= Fcons (make_number (cc
), iso_classes
);
1518 i_byte
= class_end
+ 2 - str
;
1525 if (i_byte
== size_byte
)
1530 /* Treat `-' as range character only if another character
1532 if (i_byte
+ 1 < size_byte
1533 && str
[i_byte
] == '-')
1537 /* Skip over the dash. */
1540 /* Get the end of the range. */
1543 && i_byte
< size_byte
)
1548 unsigned lim2
= c2
+ 1;
1551 if (! ASCII_CHAR_P (c2
))
1552 string_has_eight_bit
= 1;
1558 if (! ASCII_CHAR_P (c
))
1559 string_has_eight_bit
= 1;
1563 /* If the current range is multibyte and STRING contains
1564 eight-bit chars, arrange fastmap and setup char_ranges for
1565 the corresponding multibyte chars. */
1566 if (multibyte
&& string_has_eight_bit
)
1568 unsigned char fastmap2
[0400];
1569 int range_start_byte
, range_start_char
;
1571 memcpy (fastmap
+ 0200, fastmap2
+ 0200, 0200);
1572 memset (fastmap
+ 0200, 0, 0200);
1573 /* We are sure that this loop stops. */
1574 for (i
= 0200; ! fastmap2
[i
]; i
++);
1575 c
= BYTE8_TO_CHAR (i
);
1576 fastmap
[CHAR_LEADING_CODE (c
)] = 1;
1577 range_start_byte
= i
;
1578 range_start_char
= c
;
1579 char_ranges
= (int *) alloca (sizeof (int) * 128 * 2);
1580 for (i
= 129; i
< 0400; i
++)
1582 c
= BYTE8_TO_CHAR (i
);
1583 fastmap
[CHAR_LEADING_CODE (c
)] = 1;
1584 if (i
- range_start_byte
!= c
- range_start_char
)
1586 char_ranges
[n_char_ranges
++] = range_start_char
;
1587 char_ranges
[n_char_ranges
++] = ((i
- 1 - range_start_byte
)
1588 + range_start_char
);
1589 range_start_byte
= i
;
1590 range_start_char
= c
;
1593 char_ranges
[n_char_ranges
++] = range_start_char
;
1594 char_ranges
[n_char_ranges
++] = ((i
- 1 - range_start_byte
)
1595 + range_start_char
);
1598 else /* STRING is multibyte */
1600 char_ranges
= (int *) alloca (sizeof (int) * SCHARS (string
) * 2);
1602 while (i_byte
< size_byte
)
1604 unsigned char leading_code
;
1606 leading_code
= str
[i_byte
];
1607 c
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1610 if (handle_iso_classes
&& c
== '['
1611 && i_byte
< size_byte
1612 && STRING_CHAR (str
+ i_byte
) == ':')
1614 const unsigned char *class_beg
= str
+ i_byte
+ 1;
1615 const unsigned char *class_end
= class_beg
;
1616 const unsigned char *class_limit
= str
+ size_byte
- 2;
1617 /* Leave room for the null. */
1618 unsigned char class_name
[CHAR_CLASS_MAX_LENGTH
+ 1];
1621 if (class_limit
- class_beg
> CHAR_CLASS_MAX_LENGTH
)
1622 class_limit
= class_beg
+ CHAR_CLASS_MAX_LENGTH
;
1624 while (class_end
< class_limit
1625 && *class_end
>= 'a' && *class_end
<= 'z')
1628 if (class_end
== class_beg
1629 || *class_end
!= ':' || class_end
[1] != ']')
1630 goto not_a_class_name_multibyte
;
1632 memcpy (class_name
, class_beg
, class_end
- class_beg
);
1633 class_name
[class_end
- class_beg
] = 0;
1635 cc
= re_wctype (class_name
);
1637 error ("Invalid ISO C character class");
1639 iso_classes
= Fcons (make_number (cc
), iso_classes
);
1641 i_byte
= class_end
+ 2 - str
;
1645 not_a_class_name_multibyte
:
1648 if (i_byte
== size_byte
)
1651 leading_code
= str
[i_byte
];
1652 c
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1655 /* Treat `-' as range character only if another character
1657 if (i_byte
+ 1 < size_byte
1658 && str
[i_byte
] == '-')
1661 unsigned char leading_code2
;
1663 /* Skip over the dash. */
1666 /* Get the end of the range. */
1667 leading_code2
= str
[i_byte
];
1668 c2
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1672 && i_byte
< size_byte
)
1674 leading_code2
= str
[i_byte
];
1675 c2
=STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1681 if (ASCII_CHAR_P (c
))
1683 while (c
<= c2
&& c
< 0x80)
1685 leading_code
= CHAR_LEADING_CODE (c
);
1687 if (! ASCII_CHAR_P (c
))
1689 unsigned lim2
= leading_code2
+ 1;
1690 while (leading_code
< lim2
)
1691 fastmap
[leading_code
++] = 1;
1694 char_ranges
[n_char_ranges
++] = c
;
1695 char_ranges
[n_char_ranges
++] = c2
;
1701 if (ASCII_CHAR_P (c
))
1705 fastmap
[leading_code
] = 1;
1706 char_ranges
[n_char_ranges
++] = c
;
1707 char_ranges
[n_char_ranges
++] = c
;
1712 /* If the current range is unibyte and STRING contains non-ASCII
1713 chars, arrange fastmap for the corresponding unibyte
1716 if (! multibyte
&& n_char_ranges
> 0)
1718 memset (fastmap
+ 0200, 0, 0200);
1719 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1721 int c1
= char_ranges
[i
];
1722 unsigned lim2
= char_ranges
[i
+ 1] + 1;
1724 for (; c1
< lim2
; c1
++)
1726 int b
= CHAR_TO_BYTE_SAFE (c1
);
1734 /* If ^ was the first character, complement the fastmap. */
1738 for (i
= 0; i
< sizeof fastmap
; i
++)
1742 for (i
= 0; i
< 0200; i
++)
1744 /* All non-ASCII chars possibly match. */
1745 for (; i
< sizeof fastmap
; i
++)
1751 EMACS_INT start_point
= PT
;
1753 EMACS_INT pos_byte
= PT_BYTE
;
1754 unsigned char *p
= PT_ADDR
, *endp
, *stop
;
1758 endp
= (XINT (lim
) == GPT
) ? GPT_ADDR
: CHAR_POS_ADDR (XINT (lim
));
1759 stop
= (pos
< GPT
&& GPT
< XINT (lim
)) ? GPT_ADDR
: endp
;
1763 endp
= CHAR_POS_ADDR (XINT (lim
));
1764 stop
= (pos
>= GPT
&& GPT
> XINT (lim
)) ? GAP_END_ADDR
: endp
;
1768 /* This code may look up syntax tables using macros that rely on the
1769 gl_state object. To make sure this object is not out of date,
1770 let's initialize it manually.
1771 We ignore syntax-table text-properties for now, since that's
1772 what we've done in the past. */
1773 SETUP_BUFFER_SYNTAX_TABLE ();
1788 c
= STRING_CHAR_AND_LENGTH (p
, nbytes
);
1789 if (! NILP (iso_classes
) && in_classes (c
, iso_classes
))
1799 if (! ASCII_CHAR_P (c
))
1801 /* As we are looking at a multibyte character, we
1802 must look up the character in the table
1803 CHAR_RANGES. If there's no data in the table,
1804 that character is not what we want to skip. */
1806 /* The following code do the right thing even if
1807 n_char_ranges is zero (i.e. no data in
1809 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1810 if (c
>= char_ranges
[i
] && c
<= char_ranges
[i
+ 1])
1812 if (!(negate
^ (i
< n_char_ranges
)))
1816 p
+= nbytes
, pos
++, pos_byte
+= nbytes
;
1829 if (!NILP (iso_classes
) && in_classes (*p
, iso_classes
))
1834 goto fwd_unibyte_ok
;
1840 p
++, pos
++, pos_byte
++;
1848 unsigned char *prev_p
;
1858 while (--p
>= stop
&& ! CHAR_HEAD_P (*p
));
1859 c
= STRING_CHAR (p
);
1861 if (! NILP (iso_classes
) && in_classes (c
, iso_classes
))
1871 if (! ASCII_CHAR_P (c
))
1873 /* See the comment in the previous similar code. */
1874 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1875 if (c
>= char_ranges
[i
] && c
<= char_ranges
[i
+ 1])
1877 if (!(negate
^ (i
< n_char_ranges
)))
1881 pos
--, pos_byte
-= prev_p
- p
;
1894 if (! NILP (iso_classes
) && in_classes (p
[-1], iso_classes
))
1899 goto back_unibyte_ok
;
1902 if (!fastmap
[p
[-1]])
1905 p
--, pos
--, pos_byte
--;
1909 SET_PT_BOTH (pos
, pos_byte
);
1912 return make_number (PT
- start_point
);
1918 skip_syntaxes (int forwardp
, Lisp_Object string
, Lisp_Object lim
)
1920 register unsigned int c
;
1921 unsigned char fastmap
[0400];
1923 register EMACS_INT i
, i_byte
;
1925 EMACS_INT size_byte
;
1928 CHECK_STRING (string
);
1931 XSETINT (lim
, forwardp
? ZV
: BEGV
);
1933 CHECK_NUMBER_COERCE_MARKER (lim
);
1935 /* In any case, don't allow scan outside bounds of buffer. */
1936 if (XINT (lim
) > ZV
)
1937 XSETFASTINT (lim
, ZV
);
1938 if (XINT (lim
) < BEGV
)
1939 XSETFASTINT (lim
, BEGV
);
1941 if (forwardp
? (PT
>= XFASTINT (lim
)) : (PT
<= XFASTINT (lim
)))
1942 return make_number (0);
1944 multibyte
= (!NILP (BVAR (current_buffer
, enable_multibyte_characters
))
1945 && (XINT (lim
) - PT
!= CHAR_TO_BYTE (XINT (lim
)) - PT_BYTE
));
1947 memset (fastmap
, 0, sizeof fastmap
);
1949 if (SBYTES (string
) > SCHARS (string
))
1950 /* As this is very rare case (syntax spec is ASCII only), don't
1951 consider efficiency. */
1952 string
= string_make_unibyte (string
);
1954 str
= SDATA (string
);
1955 size_byte
= SBYTES (string
);
1958 if (i_byte
< size_byte
1959 && SREF (string
, 0) == '^')
1961 negate
= 1; i_byte
++;
1964 /* Find the syntaxes specified and set their elements of fastmap. */
1966 while (i_byte
< size_byte
)
1969 fastmap
[syntax_spec_code
[c
]] = 1;
1972 /* If ^ was the first character, complement the fastmap. */
1974 for (i
= 0; i
< sizeof fastmap
; i
++)
1978 EMACS_INT start_point
= PT
;
1980 EMACS_INT pos_byte
= PT_BYTE
;
1981 unsigned char *p
= PT_ADDR
, *endp
, *stop
;
1985 endp
= (XINT (lim
) == GPT
) ? GPT_ADDR
: CHAR_POS_ADDR (XINT (lim
));
1986 stop
= (pos
< GPT
&& GPT
< XINT (lim
)) ? GPT_ADDR
: endp
;
1990 endp
= CHAR_POS_ADDR (XINT (lim
));
1991 stop
= (pos
>= GPT
&& GPT
> XINT (lim
)) ? GAP_END_ADDR
: endp
;
1995 SETUP_SYNTAX_TABLE (pos
, forwardp
? 1 : -1);
2011 c
= STRING_CHAR_AND_LENGTH (p
, nbytes
);
2012 if (! fastmap
[(int) SYNTAX (c
)])
2014 p
+= nbytes
, pos
++, pos_byte
+= nbytes
;
2015 UPDATE_SYNTAX_TABLE_FORWARD (pos
);
2029 if (! fastmap
[(int) SYNTAX (*p
)])
2031 p
++, pos
++, pos_byte
++;
2032 UPDATE_SYNTAX_TABLE_FORWARD (pos
);
2042 unsigned char *prev_p
;
2051 UPDATE_SYNTAX_TABLE_BACKWARD (pos
- 1);
2053 while (--p
>= stop
&& ! CHAR_HEAD_P (*p
));
2054 c
= STRING_CHAR (p
);
2055 if (! fastmap
[(int) SYNTAX (c
)])
2057 pos
--, pos_byte
-= prev_p
- p
;
2071 UPDATE_SYNTAX_TABLE_BACKWARD (pos
- 1);
2072 if (! fastmap
[(int) SYNTAX (p
[-1])])
2074 p
--, pos
--, pos_byte
--;
2079 SET_PT_BOTH (pos
, pos_byte
);
2082 return make_number (PT
- start_point
);
2086 /* Return 1 if character C belongs to one of the ISO classes
2087 in the list ISO_CLASSES. Each class is represented by an
2088 integer which is its type according to re_wctype. */
2091 in_classes (int c
, Lisp_Object iso_classes
)
2095 while (CONSP (iso_classes
))
2098 elt
= XCAR (iso_classes
);
2099 iso_classes
= XCDR (iso_classes
);
2101 if (re_iswctype (c
, XFASTINT (elt
)))
2108 /* Jump over a comment, assuming we are at the beginning of one.
2109 FROM is the current position.
2110 FROM_BYTE is the bytepos corresponding to FROM.
2111 Do not move past STOP (a charpos).
2112 The comment over which we have to jump is of style STYLE
2113 (either SYNTAX_FLAGS_COMMENT_STYLE(foo) or ST_COMMENT_STYLE).
2114 NESTING should be positive to indicate the nesting at the beginning
2115 for nested comments and should be zero or negative else.
2116 ST_COMMENT_STYLE cannot be nested.
2117 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
2118 (or 0 If the search cannot start in the middle of a two-character).
2120 If successful, return 1 and store the charpos of the comment's end
2121 into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR.
2122 Else, return 0 and store the charpos STOP into *CHARPOS_PTR, the
2123 corresponding bytepos into *BYTEPOS_PTR and the current nesting
2124 (as defined for state.incomment) in *INCOMMENT_PTR.
2126 The comment end is the last character of the comment rather than the
2127 character just after the comment.
2129 Global syntax data is assumed to initially be valid for FROM and
2130 remains valid for forward search starting at the returned position. */
2133 forw_comment (EMACS_INT from
, EMACS_INT from_byte
, EMACS_INT stop
,
2134 int nesting
, int style
, int prev_syntax
,
2135 EMACS_INT
*charpos_ptr
, EMACS_INT
*bytepos_ptr
,
2139 register enum syntaxcode code
;
2140 register int syntax
, other_syntax
;
2142 if (nesting
<= 0) nesting
= -1;
2144 /* Enter the loop in the middle so that we find
2145 a 2-char comment ender if we start in the middle of it. */
2146 syntax
= prev_syntax
;
2147 if (syntax
!= 0) goto forw_incomment
;
2153 *incomment_ptr
= nesting
;
2154 *charpos_ptr
= from
;
2155 *bytepos_ptr
= from_byte
;
2158 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2159 syntax
= SYNTAX_WITH_FLAGS (c
);
2160 code
= syntax
& 0xff;
2161 if (code
== Sendcomment
2162 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == style
2163 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ?
2164 (nesting
> 0 && --nesting
== 0) : nesting
< 0))
2165 /* we have encountered a comment end of the same style
2166 as the comment sequence which began this comment
2169 if (code
== Scomment_fence
2170 && style
== ST_COMMENT_STYLE
)
2171 /* we have encountered a comment end of the same style
2172 as the comment sequence which began this comment
2177 && SYNTAX_FLAGS_COMMENT_NESTED (syntax
)
2178 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == style
)
2179 /* we have encountered a nested comment of the same style
2180 as the comment sequence which began this comment section */
2182 INC_BOTH (from
, from_byte
);
2183 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2186 if (from
< stop
&& SYNTAX_FLAGS_COMEND_FIRST (syntax
)
2187 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2188 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2189 SYNTAX_FLAGS_COMEND_SECOND (other_syntax
))
2190 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, other_syntax
) == style
2191 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ||
2192 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
))
2193 ? nesting
> 0 : nesting
< 0))
2196 /* we have encountered a comment end of the same style
2197 as the comment sequence which began this comment
2202 INC_BOTH (from
, from_byte
);
2203 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2208 && SYNTAX_FLAGS_COMSTART_FIRST (syntax
)
2209 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2210 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2211 SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
) == style
2212 && SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
))
2213 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ||
2214 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
)))
2215 /* we have encountered a nested comment of the same style
2216 as the comment sequence which began this comment
2219 INC_BOTH (from
, from_byte
);
2220 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2224 *charpos_ptr
= from
;
2225 *bytepos_ptr
= from_byte
;
2229 DEFUN ("forward-comment", Fforward_comment
, Sforward_comment
, 1, 1, 0,
2231 Move forward across up to COUNT comments. If COUNT is negative, move backward.
2232 Stop scanning if we find something other than a comment or whitespace.
2233 Set point to where scanning stops.
2234 If COUNT comments are found as expected, with nothing except whitespace
2235 between them, return t; otherwise return nil. */)
2238 register EMACS_INT from
;
2239 EMACS_INT from_byte
;
2240 register EMACS_INT stop
;
2242 register enum syntaxcode code
;
2243 int comstyle
= 0; /* style of comment encountered */
2244 int comnested
= 0; /* whether the comment is nestable or not */
2247 EMACS_INT out_charpos
, out_bytepos
;
2250 CHECK_NUMBER (count
);
2251 count1
= XINT (count
);
2252 stop
= count1
> 0 ? ZV
: BEGV
;
2258 from_byte
= PT_BYTE
;
2260 SETUP_SYNTAX_TABLE (from
, count1
);
2265 int comstart_first
, syntax
, other_syntax
;
2269 SET_PT_BOTH (from
, from_byte
);
2273 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2274 syntax
= SYNTAX_WITH_FLAGS (c
);
2276 comstart_first
= SYNTAX_FLAGS_COMSTART_FIRST (syntax
);
2277 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2278 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2279 INC_BOTH (from
, from_byte
);
2280 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2281 if (from
< stop
&& comstart_first
2282 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2283 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2284 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
)))
2286 /* We have encountered a comment start sequence and we
2287 are ignoring all text inside comments. We must record
2288 the comment style this sequence begins so that later,
2289 only a comment end of the same style actually ends
2290 the comment section. */
2292 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2294 = comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2295 INC_BOTH (from
, from_byte
);
2296 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2299 while (code
== Swhitespace
|| (code
== Sendcomment
&& c
== '\n'));
2301 if (code
== Scomment_fence
)
2302 comstyle
= ST_COMMENT_STYLE
;
2303 else if (code
!= Scomment
)
2306 DEC_BOTH (from
, from_byte
);
2307 SET_PT_BOTH (from
, from_byte
);
2310 /* We're at the start of a comment. */
2311 found
= forw_comment (from
, from_byte
, stop
, comnested
, comstyle
, 0,
2312 &out_charpos
, &out_bytepos
, &dummy
);
2313 from
= out_charpos
; from_byte
= out_bytepos
;
2317 SET_PT_BOTH (from
, from_byte
);
2320 INC_BOTH (from
, from_byte
);
2321 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2322 /* We have skipped one comment. */
2334 SET_PT_BOTH (BEGV
, BEGV_BYTE
);
2339 DEC_BOTH (from
, from_byte
);
2340 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
2341 quoted
= char_quoted (from
, from_byte
);
2342 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2343 syntax
= SYNTAX_WITH_FLAGS (c
);
2346 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2347 if (code
== Sendcomment
)
2348 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2349 if (from
> stop
&& SYNTAX_FLAGS_COMEND_SECOND (syntax
)
2350 && prev_char_comend_first (from
, from_byte
)
2351 && !char_quoted (from
- 1, dec_bytepos (from_byte
)))
2354 /* We must record the comment style encountered so that
2355 later, we can match only the proper comment begin
2356 sequence of the same style. */
2357 DEC_BOTH (from
, from_byte
);
2359 /* Calling char_quoted, above, set up global syntax position
2360 at the new value of FROM. */
2361 c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2362 other_syntax
= SYNTAX_WITH_FLAGS (c1
);
2363 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2365 = comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2368 if (code
== Scomment_fence
)
2370 /* Skip until first preceding unquoted comment_fence. */
2371 int fence_found
= 0;
2372 EMACS_INT ini
= from
, ini_byte
= from_byte
;
2376 DEC_BOTH (from
, from_byte
);
2377 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2378 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2379 if (SYNTAX (c
) == Scomment_fence
2380 && !char_quoted (from
, from_byte
))
2385 else if (from
== stop
)
2388 if (fence_found
== 0)
2390 from
= ini
; /* Set point to ini + 1. */
2391 from_byte
= ini_byte
;
2395 /* We have skipped one comment. */
2398 else if (code
== Sendcomment
)
2400 found
= back_comment (from
, from_byte
, stop
, comnested
, comstyle
,
2401 &out_charpos
, &out_bytepos
);
2405 /* This end-of-line is not an end-of-comment.
2406 Treat it like a whitespace.
2407 CC-mode (and maybe others) relies on this behavior. */
2411 /* Failure: we should go back to the end of this
2412 not-quite-endcomment. */
2413 if (SYNTAX (c
) != code
)
2414 /* It was a two-char Sendcomment. */
2415 INC_BOTH (from
, from_byte
);
2421 /* We have skipped one comment. */
2422 from
= out_charpos
, from_byte
= out_bytepos
;
2426 else if (code
!= Swhitespace
|| quoted
)
2430 INC_BOTH (from
, from_byte
);
2431 SET_PT_BOTH (from
, from_byte
);
2439 SET_PT_BOTH (from
, from_byte
);
2444 /* Return syntax code of character C if C is an ASCII character
2445 or `multibyte_symbol_p' is zero. Otherwise, return Ssymbol. */
2447 #define SYNTAX_WITH_MULTIBYTE_CHECK(c) \
2448 ((ASCII_CHAR_P (c) || !multibyte_symbol_p) \
2449 ? SYNTAX (c) : Ssymbol)
2452 scan_lists (register EMACS_INT from
, EMACS_INT count
, EMACS_INT depth
, int sexpflag
)
2455 register EMACS_INT stop
= count
> 0 ? ZV
: BEGV
;
2460 register enum syntaxcode code
, temp_code
;
2461 int min_depth
= depth
; /* Err out if depth gets less than this. */
2462 int comstyle
= 0; /* style of comment encountered */
2463 int comnested
= 0; /* whether the comment is nestable or not */
2465 EMACS_INT last_good
= from
;
2467 EMACS_INT from_byte
;
2468 EMACS_INT out_bytepos
, out_charpos
;
2470 int multibyte_symbol_p
= sexpflag
&& multibyte_syntax_as_symbol
;
2472 if (depth
> 0) min_depth
= 0;
2474 if (from
> ZV
) from
= ZV
;
2475 if (from
< BEGV
) from
= BEGV
;
2477 from_byte
= CHAR_TO_BYTE (from
);
2482 SETUP_SYNTAX_TABLE (from
, count
);
2487 int comstart_first
, prefix
, syntax
, other_syntax
;
2488 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2489 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2490 syntax
= SYNTAX_WITH_FLAGS (c
);
2491 code
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2492 comstart_first
= SYNTAX_FLAGS_COMSTART_FIRST (syntax
);
2493 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2494 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2495 prefix
= SYNTAX_FLAGS_PREFIX (syntax
);
2496 if (depth
== min_depth
)
2498 INC_BOTH (from
, from_byte
);
2499 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2500 if (from
< stop
&& comstart_first
2501 && (c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2502 other_syntax
= SYNTAX_WITH_FLAGS (c
),
2503 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
))
2504 && parse_sexp_ignore_comments
)
2506 /* we have encountered a comment start sequence and we
2507 are ignoring all text inside comments. We must record
2508 the comment style this sequence begins so that later,
2509 only a comment end of the same style actually ends
2510 the comment section */
2512 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2514 = comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2515 INC_BOTH (from
, from_byte
);
2516 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2522 switch (SWITCH_ENUM_CAST (code
))
2528 INC_BOTH (from
, from_byte
);
2529 /* treat following character as a word constituent */
2532 if (depth
|| !sexpflag
) break;
2533 /* This word counts as a sexp; return at end of it. */
2536 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2538 /* Some compilers can't handle this inside the switch. */
2539 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2540 temp
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2545 INC_BOTH (from
, from_byte
);
2556 INC_BOTH (from
, from_byte
);
2560 case Scomment_fence
:
2561 comstyle
= ST_COMMENT_STYLE
;
2564 if (!parse_sexp_ignore_comments
) break;
2565 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2566 found
= forw_comment (from
, from_byte
, stop
,
2567 comnested
, comstyle
, 0,
2568 &out_charpos
, &out_bytepos
, &dummy
);
2569 from
= out_charpos
, from_byte
= out_bytepos
;
2576 INC_BOTH (from
, from_byte
);
2577 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2583 if (from
!= stop
&& c
== FETCH_CHAR_AS_MULTIBYTE (from_byte
))
2585 INC_BOTH (from
, from_byte
);
2595 if (!++depth
) goto done
;
2600 if (!--depth
) goto done
;
2601 if (depth
< min_depth
)
2602 xsignal3 (Qscan_error
,
2603 build_string ("Containing expression ends prematurely"),
2604 make_number (last_good
), make_number (from
));
2609 temp_pos
= dec_bytepos (from_byte
);
2610 stringterm
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2615 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2616 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2619 && SYNTAX_WITH_MULTIBYTE_CHECK (c
) == Sstring
)
2620 : SYNTAX_WITH_MULTIBYTE_CHECK (c
) == Sstring_fence
)
2623 /* Some compilers can't handle this inside the switch. */
2624 temp
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2629 INC_BOTH (from
, from_byte
);
2631 INC_BOTH (from
, from_byte
);
2633 INC_BOTH (from
, from_byte
);
2634 if (!depth
&& sexpflag
) goto done
;
2637 /* Ignore whitespace, punctuation, quote, endcomment. */
2642 /* Reached end of buffer. Error if within object, return nil if between */
2649 /* End of object reached */
2659 DEC_BOTH (from
, from_byte
);
2660 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2661 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2662 syntax
= SYNTAX_WITH_FLAGS (c
);
2663 code
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2664 if (depth
== min_depth
)
2667 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2668 if (code
== Sendcomment
)
2669 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2670 if (from
> stop
&& SYNTAX_FLAGS_COMEND_SECOND (syntax
)
2671 && prev_char_comend_first (from
, from_byte
)
2672 && parse_sexp_ignore_comments
)
2674 /* We must record the comment style encountered so that
2675 later, we can match only the proper comment begin
2676 sequence of the same style. */
2677 int c2
, other_syntax
;
2678 DEC_BOTH (from
, from_byte
);
2679 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2681 c2
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2682 other_syntax
= SYNTAX_WITH_FLAGS (c2
);
2683 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2685 = comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2688 /* Quoting turns anything except a comment-ender
2689 into a word character. Note that this cannot be true
2690 if we decremented FROM in the if-statement above. */
2691 if (code
!= Sendcomment
&& char_quoted (from
, from_byte
))
2693 DEC_BOTH (from
, from_byte
);
2696 else if (SYNTAX_FLAGS_PREFIX (syntax
))
2699 switch (SWITCH_ENUM_CAST (code
))
2705 if (depth
|| !sexpflag
) break;
2706 /* This word counts as a sexp; count object finished
2707 after passing it. */
2710 temp_pos
= from_byte
;
2711 if (! NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
2715 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2716 c1
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2717 temp_code
= SYNTAX_WITH_MULTIBYTE_CHECK (c1
);
2718 /* Don't allow comment-end to be quoted. */
2719 if (temp_code
== Sendcomment
)
2721 quoted
= char_quoted (from
- 1, temp_pos
);
2724 DEC_BOTH (from
, from_byte
);
2725 temp_pos
= dec_bytepos (temp_pos
);
2726 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2728 c1
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2729 temp_code
= SYNTAX_WITH_MULTIBYTE_CHECK (c1
);
2730 if (! (quoted
|| temp_code
== Sword
2731 || temp_code
== Ssymbol
2732 || temp_code
== Squote
))
2734 DEC_BOTH (from
, from_byte
);
2741 temp_pos
= dec_bytepos (from_byte
);
2742 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2743 if (from
!= stop
&& c
== FETCH_CHAR_AS_MULTIBYTE (temp_pos
))
2744 DEC_BOTH (from
, from_byte
);
2753 if (!++depth
) goto done2
;
2758 if (!--depth
) goto done2
;
2759 if (depth
< min_depth
)
2760 xsignal3 (Qscan_error
,
2761 build_string ("Containing expression ends prematurely"),
2762 make_number (last_good
), make_number (from
));
2766 if (!parse_sexp_ignore_comments
)
2768 found
= back_comment (from
, from_byte
, stop
, comnested
, comstyle
,
2769 &out_charpos
, &out_bytepos
);
2770 /* FIXME: if found == -1, then it really wasn't a comment-end.
2771 For single-char Sendcomment, we can't do much about it apart
2772 from skipping the char.
2773 For 2-char endcomments, we could try again, taking both
2774 chars as separate entities, but it's a lot of trouble
2775 for very little gain, so we don't bother either. -sm */
2777 from
= out_charpos
, from_byte
= out_bytepos
;
2780 case Scomment_fence
:
2786 DEC_BOTH (from
, from_byte
);
2787 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2788 if (!char_quoted (from
, from_byte
)
2789 && (c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2790 SYNTAX_WITH_MULTIBYTE_CHECK (c
) == code
))
2793 if (code
== Sstring_fence
&& !depth
&& sexpflag
) goto done2
;
2797 stringterm
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2802 DEC_BOTH (from
, from_byte
);
2803 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2804 if (!char_quoted (from
, from_byte
)
2806 == (c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
)))
2807 && SYNTAX_WITH_MULTIBYTE_CHECK (c
) == Sstring
)
2810 if (!depth
&& sexpflag
) goto done2
;
2813 /* Ignore whitespace, punctuation, quote, endcomment. */
2818 /* Reached start of buffer. Error if within object, return nil if between */
2831 XSETFASTINT (val
, from
);
2835 xsignal3 (Qscan_error
,
2836 build_string ("Unbalanced parentheses"),
2837 make_number (last_good
), make_number (from
));
2840 DEFUN ("scan-lists", Fscan_lists
, Sscan_lists
, 3, 3, 0,
2841 doc
: /* Scan from character number FROM by COUNT lists.
2842 Returns the character number of the position thus found.
2844 If DEPTH is nonzero, paren depth begins counting from that value,
2845 only places where the depth in parentheses becomes zero
2846 are candidates for stopping; COUNT such places are counted.
2847 Thus, a positive value for DEPTH means go out levels.
2849 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2851 If the beginning or end of (the accessible part of) the buffer is reached
2852 and the depth is wrong, an error is signaled.
2853 If the depth is right but the count is not used up, nil is returned. */)
2854 (Lisp_Object from
, Lisp_Object count
, Lisp_Object depth
)
2856 CHECK_NUMBER (from
);
2857 CHECK_NUMBER (count
);
2858 CHECK_NUMBER (depth
);
2860 return scan_lists (XINT (from
), XINT (count
), XINT (depth
), 0);
2863 DEFUN ("scan-sexps", Fscan_sexps
, Sscan_sexps
, 2, 2, 0,
2864 doc
: /* Scan from character number FROM by COUNT balanced expressions.
2865 If COUNT is negative, scan backwards.
2866 Returns the character number of the position thus found.
2868 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2870 If the beginning or end of (the accessible part of) the buffer is reached
2871 in the middle of a parenthetical grouping, an error is signaled.
2872 If the beginning or end is reached between groupings
2873 but before count is used up, nil is returned. */)
2874 (Lisp_Object from
, Lisp_Object count
)
2876 CHECK_NUMBER (from
);
2877 CHECK_NUMBER (count
);
2879 return scan_lists (XINT (from
), XINT (count
), 0, 1);
2882 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars
, Sbackward_prefix_chars
,
2884 doc
: /* Move point backward over any number of chars with prefix syntax.
2885 This includes chars with "quote" or "prefix" syntax (' or p). */)
2888 EMACS_INT beg
= BEGV
;
2889 EMACS_INT opoint
= PT
;
2890 EMACS_INT opoint_byte
= PT_BYTE
;
2892 EMACS_INT pos_byte
= PT_BYTE
;
2897 SET_PT_BOTH (opoint
, opoint_byte
);
2902 SETUP_SYNTAX_TABLE (pos
, -1);
2904 DEC_BOTH (pos
, pos_byte
);
2906 while (!char_quoted (pos
, pos_byte
)
2907 /* Previous statement updates syntax table. */
2908 && ((c
= FETCH_CHAR_AS_MULTIBYTE (pos_byte
), SYNTAX (c
) == Squote
)
2909 || SYNTAX_PREFIX (c
)))
2912 opoint_byte
= pos_byte
;
2915 DEC_BOTH (pos
, pos_byte
);
2918 SET_PT_BOTH (opoint
, opoint_byte
);
2923 /* Parse forward from FROM / FROM_BYTE to END,
2924 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
2925 and return a description of the state of the parse at END.
2926 If STOPBEFORE is nonzero, stop at the start of an atom.
2927 If COMMENTSTOP is 1, stop at the start of a comment.
2928 If COMMENTSTOP is -1, stop at the start or end of a comment,
2929 after the beginning of a string, or after the end of a string. */
2932 scan_sexps_forward (struct lisp_parse_state
*stateptr
,
2933 EMACS_INT from
, EMACS_INT from_byte
, EMACS_INT end
,
2934 int targetdepth
, int stopbefore
,
2935 Lisp_Object oldstate
, int commentstop
)
2937 struct lisp_parse_state state
;
2939 register enum syntaxcode code
;
2942 struct level
{ int last
, prev
; };
2943 struct level levelstart
[100];
2944 register struct level
*curlevel
= levelstart
;
2945 struct level
*endlevel
= levelstart
+ 100;
2946 register int depth
; /* Paren depth of current scanning location.
2947 level - levelstart equals this except
2948 when the depth becomes negative. */
2949 int mindepth
; /* Lowest DEPTH value seen. */
2950 int start_quoted
= 0; /* Nonzero means starting after a char quote */
2952 EMACS_INT prev_from
; /* Keep one character before FROM. */
2953 EMACS_INT prev_from_byte
;
2954 int prev_from_syntax
;
2955 int boundary_stop
= commentstop
== -1;
2958 EMACS_INT out_bytepos
, out_charpos
;
2962 prev_from_byte
= from_byte
;
2964 DEC_BOTH (prev_from
, prev_from_byte
);
2966 /* Use this macro instead of `from++'. */
2968 do { prev_from = from; \
2969 prev_from_byte = from_byte; \
2970 temp = FETCH_CHAR_AS_MULTIBYTE (prev_from_byte); \
2971 prev_from_syntax = SYNTAX_WITH_FLAGS (temp); \
2972 INC_BOTH (from, from_byte); \
2974 UPDATE_SYNTAX_TABLE_FORWARD (from); \
2980 if (NILP (oldstate
))
2983 state
.instring
= -1;
2984 state
.incomment
= 0;
2985 state
.comstyle
= 0; /* comment style a by default. */
2986 state
.comstr_start
= -1; /* no comment/string seen. */
2990 tem
= Fcar (oldstate
);
2996 oldstate
= Fcdr (oldstate
);
2997 oldstate
= Fcdr (oldstate
);
2998 oldstate
= Fcdr (oldstate
);
2999 tem
= Fcar (oldstate
);
3000 /* Check whether we are inside string_fence-style string: */
3001 state
.instring
= (!NILP (tem
)
3002 ? (INTEGERP (tem
) ? XINT (tem
) : ST_STRING_STYLE
)
3005 oldstate
= Fcdr (oldstate
);
3006 tem
= Fcar (oldstate
);
3007 state
.incomment
= (!NILP (tem
)
3008 ? (INTEGERP (tem
) ? XINT (tem
) : -1)
3011 oldstate
= Fcdr (oldstate
);
3012 tem
= Fcar (oldstate
);
3013 start_quoted
= !NILP (tem
);
3015 /* if the eighth element of the list is nil, we are in comment
3016 style a. If it is non-nil, we are in comment style b */
3017 oldstate
= Fcdr (oldstate
);
3018 oldstate
= Fcdr (oldstate
);
3019 tem
= Fcar (oldstate
);
3020 state
.comstyle
= (NILP (tem
)
3022 : (EQ (tem
, Qsyntax_table
)
3024 : INTEGERP (tem
) ? XINT (tem
) : 1));
3026 oldstate
= Fcdr (oldstate
);
3027 tem
= Fcar (oldstate
);
3028 state
.comstr_start
= NILP (tem
) ? -1 : XINT (tem
) ;
3029 oldstate
= Fcdr (oldstate
);
3030 tem
= Fcar (oldstate
);
3031 while (!NILP (tem
)) /* >= second enclosing sexps. */
3033 /* curlevel++->last ran into compiler bug on Apollo */
3034 curlevel
->last
= XINT (Fcar (tem
));
3035 if (++curlevel
== endlevel
)
3036 curlevel
--; /* error ("Nesting too deep for parser"); */
3037 curlevel
->prev
= -1;
3038 curlevel
->last
= -1;
3045 curlevel
->prev
= -1;
3046 curlevel
->last
= -1;
3048 SETUP_SYNTAX_TABLE (prev_from
, 1);
3049 temp
= FETCH_CHAR (prev_from_byte
);
3050 prev_from_syntax
= SYNTAX_WITH_FLAGS (temp
);
3051 UPDATE_SYNTAX_TABLE_FORWARD (from
);
3053 /* Enter the loop at a place appropriate for initial state. */
3055 if (state
.incomment
)
3056 goto startincomment
;
3057 if (state
.instring
>= 0)
3059 nofence
= state
.instring
!= ST_STRING_STYLE
;
3061 goto startquotedinstring
;
3064 else if (start_quoted
)
3071 code
= prev_from_syntax
& 0xff;
3074 && SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax
)
3075 && (c1
= FETCH_CHAR (from_byte
),
3076 syntax
= SYNTAX_WITH_FLAGS (c1
),
3077 SYNTAX_FLAGS_COMSTART_SECOND (syntax
)))
3078 /* Duplicate code to avoid a complex if-expression
3079 which causes trouble for the SGI compiler. */
3081 /* Record the comment style we have entered so that only
3082 the comment-end sequence of the same style actually
3083 terminates the comment section. */
3085 = SYNTAX_FLAGS_COMMENT_STYLE (syntax
, prev_from_syntax
);
3086 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax
);
3087 comnested
= comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
3088 state
.incomment
= comnested
? 1 : -1;
3089 state
.comstr_start
= prev_from
;
3093 else if (code
== Scomment_fence
)
3095 /* Record the comment style we have entered so that only
3096 the comment-end sequence of the same style actually
3097 terminates the comment section. */
3098 state
.comstyle
= ST_COMMENT_STYLE
;
3099 state
.incomment
= -1;
3100 state
.comstr_start
= prev_from
;
3103 else if (code
== Scomment
)
3105 state
.comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax
, 0);
3106 state
.incomment
= (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax
) ?
3108 state
.comstr_start
= prev_from
;
3111 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax
))
3113 switch (SWITCH_ENUM_CAST (code
))
3117 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3118 curlevel
->last
= prev_from
;
3120 if (from
== end
) goto endquoted
;
3123 /* treat following character as a word constituent */
3126 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3127 curlevel
->last
= prev_from
;
3131 /* Some compilers can't handle this inside the switch. */
3132 temp
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
3133 temp
= SYNTAX (temp
);
3139 if (from
== end
) goto endquoted
;
3151 curlevel
->prev
= curlevel
->last
;
3154 case Scomment_fence
: /* Can't happen because it's handled above. */
3156 if (commentstop
|| boundary_stop
) goto done
;
3158 /* The (from == BEGV) test was to enter the loop in the middle so
3159 that we find a 2-char comment ender even if we start in the
3160 middle of it. We don't want to do that if we're just at the
3161 beginning of the comment (think of (*) ... (*)). */
3162 found
= forw_comment (from
, from_byte
, end
,
3163 state
.incomment
, state
.comstyle
,
3164 (from
== BEGV
|| from
< state
.comstr_start
+ 3)
3165 ? 0 : prev_from_syntax
,
3166 &out_charpos
, &out_bytepos
, &state
.incomment
);
3167 from
= out_charpos
; from_byte
= out_bytepos
;
3168 /* Beware! prev_from and friends are invalid now.
3169 Luckily, the `done' doesn't use them and the INC_FROM
3170 sets them to a sane value without looking at them. */
3171 if (!found
) goto done
;
3173 state
.incomment
= 0;
3174 state
.comstyle
= 0; /* reset the comment style */
3175 if (boundary_stop
) goto done
;
3179 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3181 /* curlevel++->last ran into compiler bug on Apollo */
3182 curlevel
->last
= prev_from
;
3183 if (++curlevel
== endlevel
)
3184 curlevel
--; /* error ("Nesting too deep for parser"); */
3185 curlevel
->prev
= -1;
3186 curlevel
->last
= -1;
3187 if (targetdepth
== depth
) goto done
;
3192 if (depth
< mindepth
)
3194 if (curlevel
!= levelstart
)
3196 curlevel
->prev
= curlevel
->last
;
3197 if (targetdepth
== depth
) goto done
;
3202 state
.comstr_start
= from
- 1;
3203 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3204 curlevel
->last
= prev_from
;
3205 state
.instring
= (code
== Sstring
3206 ? (FETCH_CHAR_AS_MULTIBYTE (prev_from_byte
))
3208 if (boundary_stop
) goto done
;
3211 nofence
= state
.instring
!= ST_STRING_STYLE
;
3217 if (from
>= end
) goto done
;
3218 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
3219 /* Some compilers can't handle this inside the switch. */
3222 /* Check TEMP here so that if the char has
3223 a syntax-table property which says it is NOT
3224 a string character, it does not end the string. */
3225 if (nofence
&& c
== state
.instring
&& temp
== Sstring
)
3231 if (!nofence
) goto string_end
;
3236 startquotedinstring
:
3237 if (from
>= end
) goto endquoted
;
3243 state
.instring
= -1;
3244 curlevel
->prev
= curlevel
->last
;
3246 if (boundary_stop
) goto done
;
3250 /* FIXME: We should do something with it. */
3253 /* Ignore whitespace, punctuation, quote, endcomment. */
3259 stop
: /* Here if stopping before start of sexp. */
3260 from
= prev_from
; /* We have just fetched the char that starts it; */
3261 goto done
; /* but return the position before it. */
3266 state
.depth
= depth
;
3267 state
.mindepth
= mindepth
;
3268 state
.thislevelstart
= curlevel
->prev
;
3269 state
.prevlevelstart
3270 = (curlevel
== levelstart
) ? -1 : (curlevel
- 1)->last
;
3271 state
.location
= from
;
3272 state
.levelstarts
= Qnil
;
3273 while (curlevel
> levelstart
)
3274 state
.levelstarts
= Fcons (make_number ((--curlevel
)->last
),
3281 DEFUN ("parse-partial-sexp", Fparse_partial_sexp
, Sparse_partial_sexp
, 2, 6, 0,
3282 doc
: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
3283 Parsing stops at TO or when certain criteria are met;
3284 point is set to where parsing stops.
3285 If fifth arg OLDSTATE is omitted or nil,
3286 parsing assumes that FROM is the beginning of a function.
3287 Value is a list of elements describing final state of parsing:
3289 1. character address of start of innermost containing list; nil if none.
3290 2. character address of start of last complete sexp terminated.
3291 3. non-nil if inside a string.
3292 (it is the character that will terminate the string,
3293 or t if the string should be terminated by a generic string delimiter.)
3294 4. nil if outside a comment, t if inside a non-nestable comment,
3295 else an integer (the current comment nesting).
3296 5. t if following a quote character.
3297 6. the minimum paren-depth encountered during this scan.
3298 7. style of comment, if any.
3299 8. character address of start of comment or string; nil if not in one.
3300 9. Intermediate data for continuation of parsing (subject to change).
3301 If third arg TARGETDEPTH is non-nil, parsing stops if the depth
3302 in parentheses becomes equal to TARGETDEPTH.
3303 Fourth arg STOPBEFORE non-nil means stop when come to
3304 any character that starts a sexp.
3305 Fifth arg OLDSTATE is a list like what this function returns.
3306 It is used to initialize the state of the parse. Elements number 1, 2, 6
3308 Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.
3309 If it is symbol `syntax-table', stop after the start of a comment or a
3310 string, or after end of a comment or a string. */)
3311 (Lisp_Object from
, Lisp_Object to
, Lisp_Object targetdepth
, Lisp_Object stopbefore
, Lisp_Object oldstate
, Lisp_Object commentstop
)
3313 struct lisp_parse_state state
;
3316 if (!NILP (targetdepth
))
3318 CHECK_NUMBER (targetdepth
);
3319 target
= XINT (targetdepth
);
3322 target
= -100000; /* We won't reach this depth */
3324 validate_region (&from
, &to
);
3325 scan_sexps_forward (&state
, XINT (from
), CHAR_TO_BYTE (XINT (from
)),
3327 target
, !NILP (stopbefore
), oldstate
,
3329 ? 0 : (EQ (commentstop
, Qsyntax_table
) ? -1 : 1)));
3331 SET_PT (state
.location
);
3333 return Fcons (make_number (state
.depth
),
3334 Fcons (state
.prevlevelstart
< 0
3335 ? Qnil
: make_number (state
.prevlevelstart
),
3336 Fcons (state
.thislevelstart
< 0
3337 ? Qnil
: make_number (state
.thislevelstart
),
3338 Fcons (state
.instring
>= 0
3339 ? (state
.instring
== ST_STRING_STYLE
3340 ? Qt
: make_number (state
.instring
)) : Qnil
,
3341 Fcons (state
.incomment
< 0 ? Qt
:
3342 (state
.incomment
== 0 ? Qnil
:
3343 make_number (state
.incomment
)),
3344 Fcons (state
.quoted
? Qt
: Qnil
,
3345 Fcons (make_number (state
.mindepth
),
3346 Fcons ((state
.comstyle
3347 ? (state
.comstyle
== ST_COMMENT_STYLE
3349 : make_number (state
.comstyle
))
3351 Fcons (((state
.incomment
3352 || (state
.instring
>= 0))
3353 ? make_number (state
.comstr_start
)
3355 Fcons (state
.levelstarts
, Qnil
))))))))));
3359 init_syntax_once (void)
3364 /* This has to be done here, before we call Fmake_char_table. */
3365 Qsyntax_table
= intern_c_string ("syntax-table");
3366 staticpro (&Qsyntax_table
);
3368 /* Intern_C_String this now in case it isn't already done.
3369 Setting this variable twice is harmless.
3370 But don't staticpro it here--that is done in alloc.c. */
3371 Qchar_table_extra_slots
= intern_c_string ("char-table-extra-slots");
3373 /* Create objects which can be shared among syntax tables. */
3374 Vsyntax_code_object
= Fmake_vector (make_number (Smax
), Qnil
);
3375 for (i
= 0; i
< ASIZE (Vsyntax_code_object
); i
++)
3376 XVECTOR (Vsyntax_code_object
)->contents
[i
]
3377 = Fcons (make_number (i
), Qnil
);
3379 /* Now we are ready to set up this property, so we can
3380 create syntax tables. */
3381 Fput (Qsyntax_table
, Qchar_table_extra_slots
, make_number (0));
3383 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Swhitespace
];
3385 Vstandard_syntax_table
= Fmake_char_table (Qsyntax_table
, temp
);
3387 /* Control characters should not be whitespace. */
3388 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Spunct
];
3389 for (i
= 0; i
<= ' ' - 1; i
++)
3390 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3391 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 0177, temp
);
3393 /* Except that a few really are whitespace. */
3394 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Swhitespace
];
3395 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ' ', temp
);
3396 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\t', temp
);
3397 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\n', temp
);
3398 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 015, temp
);
3399 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 014, temp
);
3401 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Sword
];
3402 for (i
= 'a'; i
<= 'z'; i
++)
3403 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3404 for (i
= 'A'; i
<= 'Z'; i
++)
3405 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3406 for (i
= '0'; i
<= '9'; i
++)
3407 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3409 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '$', temp
);
3410 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '%', temp
);
3412 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '(',
3413 Fcons (make_number (Sopen
), make_number (')')));
3414 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ')',
3415 Fcons (make_number (Sclose
), make_number ('(')));
3416 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '[',
3417 Fcons (make_number (Sopen
), make_number (']')));
3418 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ']',
3419 Fcons (make_number (Sclose
), make_number ('[')));
3420 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '{',
3421 Fcons (make_number (Sopen
), make_number ('}')));
3422 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '}',
3423 Fcons (make_number (Sclose
), make_number ('{')));
3424 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '"',
3425 Fcons (make_number ((int) Sstring
), Qnil
));
3426 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\\',
3427 Fcons (make_number ((int) Sescape
), Qnil
));
3429 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Ssymbol
];
3430 for (i
= 0; i
< 10; i
++)
3432 c
= "_-+*/&|<>="[i
];
3433 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, c
, temp
);
3436 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Spunct
];
3437 for (i
= 0; i
< 12; i
++)
3439 c
= ".,;:?!#@~^'`"[i
];
3440 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, c
, temp
);
3443 /* All multibyte characters have syntax `word' by default. */
3444 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Sword
];
3445 char_table_set_range (Vstandard_syntax_table
, 0x80, MAX_CHAR
, temp
);
3449 syms_of_syntax (void)
3451 Qsyntax_table_p
= intern_c_string ("syntax-table-p");
3452 staticpro (&Qsyntax_table_p
);
3454 staticpro (&Vsyntax_code_object
);
3456 staticpro (&gl_state
.object
);
3457 staticpro (&gl_state
.global_code
);
3458 staticpro (&gl_state
.current_syntax_table
);
3459 staticpro (&gl_state
.old_prop
);
3461 /* Defined in regex.c */
3462 staticpro (&re_match_object
);
3464 Qscan_error
= intern_c_string ("scan-error");
3465 staticpro (&Qscan_error
);
3466 Fput (Qscan_error
, Qerror_conditions
,
3467 pure_cons (Qscan_error
, pure_cons (Qerror
, Qnil
)));
3468 Fput (Qscan_error
, Qerror_message
,
3469 make_pure_c_string ("Scan error"));
3471 DEFVAR_BOOL ("parse-sexp-ignore-comments", parse_sexp_ignore_comments
,
3472 doc
: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
3474 DEFVAR_BOOL ("parse-sexp-lookup-properties", parse_sexp_lookup_properties
,
3475 doc
: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3476 Otherwise, that text property is simply ignored.
3477 See the info node `(elisp)Syntax Properties' for a description of the
3478 `syntax-table' property. */);
3480 words_include_escapes
= 0;
3481 DEFVAR_BOOL ("words-include-escapes", words_include_escapes
,
3482 doc
: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
3484 DEFVAR_BOOL ("multibyte-syntax-as-symbol", multibyte_syntax_as_symbol
,
3485 doc
: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
3486 multibyte_syntax_as_symbol
= 0;
3488 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3489 open_paren_in_column_0_is_defun_start
,
3490 doc
: /* *Non-nil means an open paren in column 0 denotes the start of a defun. */);
3491 open_paren_in_column_0_is_defun_start
= 1;
3494 DEFVAR_LISP ("find-word-boundary-function-table",
3495 Vfind_word_boundary_function_table
,
3497 Char table of functions to search for the word boundary.
3498 Each function is called with two arguments; POS and LIMIT.
3499 POS and LIMIT are character positions in the current buffer.
3501 If POS is less than LIMIT, POS is at the first character of a word,
3502 and the return value of a function is a position after the last
3503 character of that word.
3505 If POS is not less than LIMIT, POS is at the last character of a word,
3506 and the return value of a function is a position at the first
3507 character of that word.
3509 In both cases, LIMIT bounds the search. */);
3510 Vfind_word_boundary_function_table
= Fmake_char_table (Qnil
, Qnil
);
3512 defsubr (&Ssyntax_table_p
);
3513 defsubr (&Ssyntax_table
);
3514 defsubr (&Sstandard_syntax_table
);
3515 defsubr (&Scopy_syntax_table
);
3516 defsubr (&Sset_syntax_table
);
3517 defsubr (&Schar_syntax
);
3518 defsubr (&Smatching_paren
);
3519 defsubr (&Sstring_to_syntax
);
3520 defsubr (&Smodify_syntax_entry
);
3521 defsubr (&Sinternal_describe_syntax_value
);
3523 defsubr (&Sforward_word
);
3525 defsubr (&Sskip_chars_forward
);
3526 defsubr (&Sskip_chars_backward
);
3527 defsubr (&Sskip_syntax_forward
);
3528 defsubr (&Sskip_syntax_backward
);
3530 defsubr (&Sforward_comment
);
3531 defsubr (&Sscan_lists
);
3532 defsubr (&Sscan_sexps
);
3533 defsubr (&Sbackward_prefix_chars
);
3534 defsubr (&Sparse_partial_sexp
);