1 /* GNU Emacs routines to deal with syntax tables; also word and list parsing.
2 Copyright (C) 1985, 87, 93, 94, 95, 97, 1998, 1999 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
29 /* Make syntax table lookup grant data in gl_state. */
30 #define SYNTAX_ENTRY_VIA_PROPERTY
33 #include "intervals.h"
35 /* We use these constants in place for comment-style and
36 string-ender-char to distinguish comments/strings started by
37 comment_fence and string_fence codes. */
39 #define ST_COMMENT_STYLE (256 + 1)
40 #define ST_STRING_STYLE (256 + 2)
43 Lisp_Object Qsyntax_table_p
, Qsyntax_table
, Qscan_error
;
45 int words_include_escapes
;
46 int parse_sexp_lookup_properties
;
48 /* Nonzero means `scan-sexps' treat all multibyte characters as symbol. */
49 int multibyte_syntax_as_symbol
;
51 /* Used as a temporary in SYNTAX_ENTRY and other macros in syntax.h,
52 if not compiled with GCC. No need to mark it, since it is used
53 only very temporarily. */
54 Lisp_Object syntax_temp
;
56 /* Non-zero means an open parenthesis in column 0 is always considered
57 to be the start of a defun. Zero means an open parenthesis in
58 column 0 has no special meaning. */
60 int open_paren_in_column_0_is_defun_start
;
62 /* This is the internal form of the parse state used in parse-partial-sexp. */
64 struct lisp_parse_state
66 int depth
; /* Depth at end of parsing. */
67 int instring
; /* -1 if not within string, else desired terminator. */
68 int incomment
; /* -1 if in unnestable comment else comment nesting */
69 int comstyle
; /* comment style a=0, or b=1, or ST_COMMENT_STYLE. */
70 int quoted
; /* Nonzero if just after an escape char at end of parsing */
71 int thislevelstart
; /* Char number of most recent start-of-expression at current level */
72 int prevlevelstart
; /* Char number of start of containing expression */
73 int location
; /* Char number at which parsing stopped. */
74 int mindepth
; /* Minimum depth seen while scanning. */
75 int comstr_start
; /* Position just after last comment/string starter. */
76 Lisp_Object levelstarts
; /* Char numbers of starts-of-expression
77 of levels (starting from outermost). */
80 /* These variables are a cache for finding the start of a defun.
81 find_start_pos is the place for which the defun start was found.
82 find_start_value is the defun start position found for it.
83 find_start_value_byte is the corresponding byte position.
84 find_start_buffer is the buffer it was found in.
85 find_start_begv is the BEGV value when it was found.
86 find_start_modiff is the value of MODIFF when it was found. */
88 static int find_start_pos
;
89 static int find_start_value
;
90 static int find_start_value_byte
;
91 static struct buffer
*find_start_buffer
;
92 static int find_start_begv
;
93 static int find_start_modiff
;
96 static int find_defun_start
P_ ((int, int));
97 static int back_comment
P_ ((int, int, int, int, int, int *, int *));
98 static int char_quoted
P_ ((int, int));
99 static Lisp_Object skip_chars
P_ ((int, int, Lisp_Object
, Lisp_Object
));
100 static Lisp_Object scan_lists
P_ ((int, int, int, int));
101 static void scan_sexps_forward
P_ ((struct lisp_parse_state
*,
103 int, Lisp_Object
, int));
106 struct gl_state_s gl_state
; /* Global state of syntax parser. */
108 INTERVAL
interval_of ();
109 #define INTERVALS_AT_ONCE 10 /* 1 + max-number of intervals
110 to scan to property-change. */
112 /* Update gl_state to an appropriate interval which contains CHARPOS. The
113 sign of COUNT give the relative position of CHARPOS wrt the previously
114 valid interval. If INIT, only [be]_property fields of gl_state are
115 valid at start, the rest is filled basing on OBJECT.
117 `gl_state.*_i' are the intervals, and CHARPOS is further in the search
118 direction than the intervals - or in an interval. We update the
119 current syntax-table basing on the property of this interval, and
120 update the interval to start further than CHARPOS - or be
121 NULL_INTERVAL. We also update lim_property to be the next value of
122 charpos to call this subroutine again - or be before/after the
123 start/end of OBJECT. */
126 update_syntax_table (charpos
, count
, init
, object
)
127 int charpos
, count
, init
;
130 Lisp_Object tmp_table
;
131 int cnt
= 0, invalidate
= 1;
136 gl_state
.start
= gl_state
.b_property
;
137 gl_state
.stop
= gl_state
.e_property
;
138 gl_state
.forward_i
= interval_of (charpos
, object
);
139 i
= gl_state
.backward_i
= gl_state
.forward_i
;
140 gl_state
.left_ok
= gl_state
.right_ok
= 1;
142 if (NULL_INTERVAL_P (i
))
144 /* interval_of updates only ->position of the return value, so
145 update the parents manually to speed up update_interval. */
146 while (!NULL_PARENT (i
))
148 if (AM_RIGHT_CHILD (i
))
149 INTERVAL_PARENT (i
)->position
= i
->position
150 - LEFT_TOTAL_LENGTH (i
) + TOTAL_LENGTH (i
) /* right end */
151 - TOTAL_LENGTH (INTERVAL_PARENT (i
))
152 + LEFT_TOTAL_LENGTH (INTERVAL_PARENT (i
));
154 INTERVAL_PARENT (i
)->position
= i
->position
- LEFT_TOTAL_LENGTH (i
)
156 i
= INTERVAL_PARENT (i
);
158 i
= gl_state
.forward_i
;
159 gl_state
.b_property
= i
->position
- 1 - gl_state
.offset
;
160 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
163 oldi
= i
= count
> 0 ? gl_state
.forward_i
: gl_state
.backward_i
;
165 /* We are guarantied to be called with CHARPOS either in i,
167 if (NULL_INTERVAL_P (i
))
168 error ("Error in syntax_table logic for to-the-end intervals");
169 else if (charpos
< i
->position
) /* Move left. */
172 error ("Error in syntax_table logic for intervals <-");
173 /* Update the interval. */
174 i
= update_interval (i
, charpos
);
175 if (oldi
->position
!= INTERVAL_LAST_POS (i
))
178 gl_state
.right_ok
= 1; /* Invalidate the other end. */
179 gl_state
.forward_i
= i
;
180 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
183 else if (charpos
>= INTERVAL_LAST_POS (i
)) /* Move right. */
186 error ("Error in syntax_table logic for intervals ->");
187 /* Update the interval. */
188 i
= update_interval (i
, charpos
);
189 if (i
->position
!= INTERVAL_LAST_POS (oldi
))
192 gl_state
.left_ok
= 1; /* Invalidate the other end. */
193 gl_state
.backward_i
= i
;
194 gl_state
.b_property
= i
->position
- 1 - gl_state
.offset
;
197 else if (count
> 0 ? gl_state
.right_ok
: gl_state
.left_ok
)
199 /* We do not need to recalculate tmp_table. */
200 tmp_table
= gl_state
.old_prop
;
204 tmp_table
= textget (i
->plist
, Qsyntax_table
);
207 invalidate
= !EQ (tmp_table
, gl_state
.old_prop
); /* Need to invalidate? */
209 if (invalidate
) /* Did not get to adjacent interval. */
210 { /* with the same table => */
211 /* invalidate the old range. */
214 gl_state
.backward_i
= i
;
215 gl_state
.left_ok
= 1; /* Invalidate the other end. */
216 gl_state
.b_property
= i
->position
- 1 - gl_state
.offset
;
220 gl_state
.forward_i
= i
;
221 gl_state
.right_ok
= 1; /* Invalidate the other end. */
222 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
226 gl_state
.current_syntax_table
= tmp_table
;
227 gl_state
.old_prop
= tmp_table
;
228 if (EQ (Fsyntax_table_p (tmp_table
), Qt
))
230 gl_state
.use_global
= 0;
232 else if (CONSP (tmp_table
))
234 gl_state
.use_global
= 1;
235 gl_state
.global_code
= tmp_table
;
239 gl_state
.use_global
= 0;
240 gl_state
.current_syntax_table
= current_buffer
->syntax_table
;
243 while (!NULL_INTERVAL_P (i
))
245 if (cnt
&& !EQ (tmp_table
, textget (i
->plist
, Qsyntax_table
)))
248 gl_state
.right_ok
= 0;
250 gl_state
.left_ok
= 0;
253 else if (cnt
== INTERVALS_AT_ONCE
)
256 gl_state
.right_ok
= 1;
258 gl_state
.left_ok
= 1;
262 i
= count
> 0 ? next_interval (i
) : previous_interval (i
);
264 if (NULL_INTERVAL_P (i
))
265 { /* This property goes to the end. */
267 gl_state
.e_property
= gl_state
.stop
;
269 gl_state
.b_property
= gl_state
.start
;
275 gl_state
.e_property
= i
->position
- gl_state
.offset
;
276 gl_state
.forward_i
= i
;
280 gl_state
.b_property
= i
->position
+ LENGTH (i
) - 1 - gl_state
.offset
;
281 gl_state
.backward_i
= i
;
286 /* Returns TRUE if char at CHARPOS is quoted.
287 Global syntax-table data should be set up already to be good at CHARPOS
288 or after. On return global syntax data is good for lookup at CHARPOS. */
291 char_quoted (charpos
, bytepos
)
292 register int charpos
, bytepos
;
294 register enum syntaxcode code
;
295 register int beg
= BEGV
;
296 register int quoted
= 0;
299 DEC_BOTH (charpos
, bytepos
);
301 while (bytepos
>= beg
)
303 UPDATE_SYNTAX_TABLE_BACKWARD (charpos
);
304 code
= SYNTAX (FETCH_CHAR (bytepos
));
305 if (! (code
== Scharquote
|| code
== Sescape
))
308 DEC_BOTH (charpos
, bytepos
);
312 UPDATE_SYNTAX_TABLE (orig
);
316 /* Return the bytepos one character after BYTEPOS.
317 We assume that BYTEPOS is not at the end of the buffer. */
320 inc_bytepos (bytepos
)
323 if (NILP (current_buffer
->enable_multibyte_characters
))
330 /* Return the bytepos one character before BYTEPOS.
331 We assume that BYTEPOS is not at the start of the buffer. */
334 dec_bytepos (bytepos
)
337 if (NILP (current_buffer
->enable_multibyte_characters
))
344 /* Find a defun-start that is the last one before POS (or nearly the last).
345 We record what we find, so that another call in the same area
346 can return the same value right away.
348 There is no promise at which position the global syntax data is
349 valid on return from the subroutine, so the caller should explicitly
350 update the global data. */
353 find_defun_start (pos
, pos_byte
)
356 int opoint
= PT
, opoint_byte
= PT_BYTE
;
358 /* Use previous finding, if it's valid and applies to this inquiry. */
359 if (current_buffer
== find_start_buffer
360 /* Reuse the defun-start even if POS is a little farther on.
361 POS might be in the next defun, but that's ok.
362 Our value may not be the best possible, but will still be usable. */
363 && pos
<= find_start_pos
+ 1000
364 && pos
>= find_start_value
365 && BEGV
== find_start_begv
366 && MODIFF
== find_start_modiff
)
367 return find_start_value
;
369 /* Back up to start of line. */
370 scan_newline (pos
, pos_byte
, BEGV
, BEGV_BYTE
, -1, 1);
372 /* We optimize syntax-table lookup for rare updates. Thus we accept
373 only those `^\s(' which are good in global _and_ text-property
375 gl_state
.current_syntax_table
= current_buffer
->syntax_table
;
376 gl_state
.use_global
= 0;
377 if (open_paren_in_column_0_is_defun_start
)
381 /* Open-paren at start of line means we may have found our
383 if (SYNTAX (FETCH_CHAR (PT_BYTE
)) == Sopen
)
385 SETUP_SYNTAX_TABLE (PT
+ 1, -1); /* Try again... */
386 if (SYNTAX (FETCH_CHAR (PT_BYTE
)) == Sopen
)
388 /* Now fallback to the default value. */
389 gl_state
.current_syntax_table
= current_buffer
->syntax_table
;
390 gl_state
.use_global
= 0;
392 /* Move to beg of previous line. */
393 scan_newline (PT
, PT_BYTE
, BEGV
, BEGV_BYTE
, -2, 1);
397 /* Record what we found, for the next try. */
398 find_start_value
= PT
;
399 find_start_value_byte
= PT_BYTE
;
400 find_start_buffer
= current_buffer
;
401 find_start_modiff
= MODIFF
;
402 find_start_begv
= BEGV
;
403 find_start_pos
= pos
;
405 TEMP_SET_PT_BOTH (opoint
, opoint_byte
);
407 return find_start_value
;
410 /* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
413 prev_char_comend_first (pos
, pos_byte
)
418 DEC_BOTH (pos
, pos_byte
);
419 UPDATE_SYNTAX_TABLE_BACKWARD (pos
);
420 c
= FETCH_CHAR (pos_byte
);
421 val
= SYNTAX_COMEND_FIRST (c
);
422 UPDATE_SYNTAX_TABLE_FORWARD (pos
+ 1);
426 /* Return the SYNTAX_COMSTART_FIRST of the character before POS, POS_BYTE. */
429 prev_char_comstart_first (pos
, pos_byte
)
434 DEC_BOTH (pos
, pos_byte
);
435 UPDATE_SYNTAX_TABLE_BACKWARD (pos
);
436 c
= FETCH_CHAR (pos_byte
);
437 val
= SYNTAX_COMSTART_FIRST (c
);
438 UPDATE_SYNTAX_TABLE_FORWARD (pos
+ 1);
442 /* Checks whether charpos FROM is at the end of a comment.
443 FROM_BYTE is the bytepos corresponding to FROM.
444 Do not move back before STOP.
446 Return a positive value if we find a comment ending at FROM/FROM_BYTE;
449 If successful, store the charpos of the comment's beginning
450 into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR.
452 Global syntax data remains valid for backward search starting at
453 the returned value (or at FROM, if the search was not successful). */
456 back_comment (from
, from_byte
, stop
, comnested
, comstyle
, charpos_ptr
, bytepos_ptr
)
457 int from
, from_byte
, stop
;
458 int comnested
, comstyle
;
459 int *charpos_ptr
, *bytepos_ptr
;
461 /* Look back, counting the parity of string-quotes,
462 and recording the comment-starters seen.
463 When we reach a safe place, assume that's not in a string;
464 then step the main scan to the earliest comment-starter seen
465 an even number of string quotes away from the safe place.
467 OFROM[I] is position of the earliest comment-starter seen
468 which is I+2X quotes from the comment-end.
469 PARITY is current parity of quotes from the comment end. */
470 int string_style
= -1; /* Presumed outside of any string. */
471 int string_lossage
= 0;
472 int comment_end
= from
;
473 int comment_end_byte
= from_byte
;
474 int comstart_pos
= 0;
476 /* Value that PARITY had, when we reached the position
478 int scanstart
= from
- 1;
479 /* Place where the containing defun starts,
480 or 0 if we didn't come across it yet. */
482 int defun_start_byte
= 0;
483 register enum syntaxcode code
;
484 int nesting
= 1; /* current comment nesting */
487 /* At beginning of range to scan, we're outside of strings;
488 that determines quote parity to the comment-end. */
493 /* Move back and examine a character. */
494 DEC_BOTH (from
, from_byte
);
495 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
497 c
= FETCH_CHAR (from_byte
);
500 /* If this char is the second of a 2-char comment end sequence,
501 back up and give the pair the appropriate syntax. */
502 if (from
> stop
&& SYNTAX_COMEND_SECOND (c
)
503 && prev_char_comend_first (from
, from_byte
))
506 DEC_BOTH (from
, from_byte
);
507 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
508 c
= FETCH_CHAR (from_byte
);
511 /* If this char starts a 2-char comment start sequence,
512 treat it like a 1-char comment starter. */
513 if (from
< scanstart
&& SYNTAX_COMSTART_FIRST (c
))
515 temp_byte
= inc_bytepos (from_byte
);
516 UPDATE_SYNTAX_TABLE_FORWARD (from
+ 1);
517 if (SYNTAX_COMSTART_SECOND (FETCH_CHAR (temp_byte
))
518 && comstyle
== SYNTAX_COMMENT_STYLE (FETCH_CHAR (temp_byte
)))
520 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
522 else if (code
== Scomment
&& comstyle
!= SYNTAX_COMMENT_STYLE (c
))
523 /* Ignore comment starters of a different style. */
526 /* Ignore escaped characters, except comment-enders. */
527 if (code
!= Sendcomment
&& char_quoted (from
, from_byte
))
530 /* Track parity of quotes. */
535 c
= (code
== Sstring_fence
? ST_STRING_STYLE
: ST_COMMENT_STYLE
);
537 /* Track parity of quotes. */
538 if (string_style
== -1)
539 /* Entering a string. */
541 else if (string_style
== c
)
542 /* Leaving the string. */
545 /* If we have two kinds of string delimiters.
546 There's no way to grok this scanning backwards. */
551 /* We've already checked that it is the relevant comstyle. */
552 if (string_style
!= -1 || string_lossage
)
553 /* There are odd string quotes involved, so let's be careful.
554 Test case in Pascal: " { " a { " } */
557 if (comnested
&& --nesting
<= 0)
558 /* nested comments have to be balanced, so we don't need to
559 keep looking for earlier ones. We use here the same (slightly
560 incorrect) reasoning as below: since it is followed by uniform
561 paired string quotes, this comment-start has to be outside of
562 strings, else the comment-end itself would be inside a string. */
565 /* Record comment-starters according to that
566 quote-parity to the comment-end. */
568 comstart_byte
= from_byte
;
575 /* If we find another earlier comment-ender,
576 any comment-starts earlier than that don't count
577 (because they go with the earlier comment-ender). */
578 if (code
== Sendcomment
579 && SYNTAX_COMMENT_STYLE (FETCH_CHAR (from_byte
)) == comstyle
)
587 /* Assume a defun-start point is outside of strings. */
590 || (temp_byte
= dec_bytepos (from_byte
),
591 FETCH_CHAR (temp_byte
) == '\n')))
594 defun_start_byte
= from_byte
;
599 if (comstart_pos
== 0)
602 from_byte
= comment_end_byte
;
603 UPDATE_SYNTAX_TABLE_FORWARD (comment_end
- 1);
605 /* If the earliest comment starter
606 is followed by uniform paired string quotes or none,
607 we know it can't be inside a string
608 since if it were then the comment ender would be inside one.
609 So it does start a comment. Skip back to it. */
613 from_byte
= comstart_byte
;
614 /* Globals are correct now. */
618 struct lisp_parse_state state
;
620 /* We had two kinds of string delimiters mixed up
621 together. Decode this going forwards.
622 Scan fwd from the previous comment ender
623 to the one in question; this records where we
624 last passed a comment starter. */
625 /* If we did not already find the defun start, find it now. */
626 if (defun_start
== 0)
628 defun_start
= find_defun_start (comment_end
, comment_end_byte
);
629 defun_start_byte
= find_start_value_byte
;
631 scan_sexps_forward (&state
,
632 defun_start
, defun_start_byte
,
633 comment_end
- 1, -10000, 0, Qnil
, 0);
636 /* scan_sexps_forward changed the direction of search in
637 global variables, so we need to update it completely. */
639 from
= state
.comstr_start
;
645 from_byte
= CHAR_TO_BYTE (from
);
646 UPDATE_SYNTAX_TABLE_FORWARD (from
- 1);
651 *bytepos_ptr
= from_byte
;
653 return (from
== comment_end
) ? -1 : from
;
656 DEFUN ("syntax-table-p", Fsyntax_table_p
, Ssyntax_table_p
, 1, 1, 0,
657 "Return t if OBJECT is a syntax table.\n\
658 Currently, any char-table counts as a syntax table.")
662 if (CHAR_TABLE_P (object
)
663 && EQ (XCHAR_TABLE (object
)->purpose
, Qsyntax_table
))
669 check_syntax_table (obj
)
672 if (!(CHAR_TABLE_P (obj
)
673 && EQ (XCHAR_TABLE (obj
)->purpose
, Qsyntax_table
)))
674 wrong_type_argument (Qsyntax_table_p
, obj
);
677 DEFUN ("syntax-table", Fsyntax_table
, Ssyntax_table
, 0, 0, 0,
678 "Return the current syntax table.\n\
679 This is the one specified by the current buffer.")
682 return current_buffer
->syntax_table
;
685 DEFUN ("standard-syntax-table", Fstandard_syntax_table
,
686 Sstandard_syntax_table
, 0, 0, 0,
687 "Return the standard syntax table.\n\
688 This is the one used for new buffers.")
691 return Vstandard_syntax_table
;
694 DEFUN ("copy-syntax-table", Fcopy_syntax_table
, Scopy_syntax_table
, 0, 1, 0,
695 "Construct a new syntax table and return it.\n\
696 It is a copy of the TABLE, which defaults to the standard syntax table.")
703 check_syntax_table (table
);
705 table
= Vstandard_syntax_table
;
707 copy
= Fcopy_sequence (table
);
709 /* Only the standard syntax table should have a default element.
710 Other syntax tables should inherit from parents instead. */
711 XCHAR_TABLE (copy
)->defalt
= Qnil
;
713 /* Copied syntax tables should all have parents.
714 If we copied one with no parent, such as the standard syntax table,
715 use the standard syntax table as the copy's parent. */
716 if (NILP (XCHAR_TABLE (copy
)->parent
))
717 Fset_char_table_parent (copy
, Vstandard_syntax_table
);
721 DEFUN ("set-syntax-table", Fset_syntax_table
, Sset_syntax_table
, 1, 1, 0,
722 "Select a new syntax table for the current buffer.\n\
723 One argument, a syntax table.")
728 check_syntax_table (table
);
729 current_buffer
->syntax_table
= table
;
730 /* Indicate that this buffer now has a specified syntax table. */
731 idx
= PER_BUFFER_VAR_IDX (syntax_table
);
732 SET_PER_BUFFER_VALUE_P (current_buffer
, idx
, 1);
736 /* Convert a letter which signifies a syntax code
737 into the code it signifies.
738 This is used by modify-syntax-entry, and other things. */
740 unsigned char syntax_spec_code
[0400] =
741 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
742 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
743 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
744 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
745 (char) Swhitespace
, (char) Scomment_fence
, (char) Sstring
, 0377,
746 (char) Smath
, 0377, 0377, (char) Squote
,
747 (char) Sopen
, (char) Sclose
, 0377, 0377,
748 0377, (char) Swhitespace
, (char) Spunct
, (char) Scharquote
,
749 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
750 0377, 0377, 0377, 0377,
751 (char) Scomment
, 0377, (char) Sendcomment
, 0377,
752 (char) Sinherit
, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
753 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
754 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword
,
755 0377, 0377, 0377, 0377, (char) Sescape
, 0377, 0377, (char) Ssymbol
,
756 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
757 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
758 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword
,
759 0377, 0377, 0377, 0377, (char) Sstring_fence
, 0377, 0377, 0377
762 /* Indexed by syntax code, give the letter that describes it. */
764 char syntax_code_spec
[16] =
766 ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@',
770 /* Indexed by syntax code, give the object (cons of syntax code and
771 nil) to be stored in syntax table. Since these objects can be
772 shared among syntax tables, we generate them in advance. By
773 sharing objects, the function `describe-syntax' can give a more
775 static Lisp_Object Vsyntax_code_object
;
778 /* Look up the value for CHARACTER in syntax table TABLE's parent
779 and its parents. SYNTAX_ENTRY calls this, when TABLE itself has nil
780 for CHARACTER. It's actually used only when not compiled with GCC. */
783 syntax_parent_lookup (table
, character
)
791 table
= XCHAR_TABLE (table
)->parent
;
795 value
= XCHAR_TABLE (table
)->contents
[character
];
801 DEFUN ("char-syntax", Fchar_syntax
, Schar_syntax
, 1, 1, 0,
802 "Return the syntax code of CHARACTER, described by a character.\n\
803 For example, if CHARACTER is a word constituent,\n\
804 the character `w' is returned.\n\
805 The characters that correspond to various syntax codes\n\
806 are listed in the documentation of `modify-syntax-entry'.")
808 Lisp_Object character
;
811 gl_state
.current_syntax_table
= current_buffer
->syntax_table
;
813 gl_state
.use_global
= 0;
814 CHECK_NUMBER (character
, 0);
815 char_int
= XINT (character
);
816 return make_number (syntax_code_spec
[(int) SYNTAX (char_int
)]);
819 DEFUN ("matching-paren", Fmatching_paren
, Smatching_paren
, 1, 1, 0,
820 "Return the matching parenthesis of CHARACTER, or nil if none.")
822 Lisp_Object character
;
825 gl_state
.current_syntax_table
= current_buffer
->syntax_table
;
826 gl_state
.use_global
= 0;
827 CHECK_NUMBER (character
, 0);
828 char_int
= XINT (character
);
829 code
= SYNTAX (char_int
);
830 if (code
== Sopen
|| code
== Sclose
)
831 return SYNTAX_MATCH (char_int
);
835 /* This comment supplies the doc string for modify-syntax-entry,
836 for make-docfile to see. We cannot put this in the real DEFUN
837 due to limits in the Unix cpp.
839 DEFUN ("modify-syntax-entry", foo, bar, 2, 3, 0,
840 "Set syntax for character CHAR according to string S.\n\
841 The syntax is changed only for table TABLE, which defaults to\n\
842 the current buffer's syntax table.\n\
843 The first character of S should be one of the following:\n\
844 Space or - whitespace syntax. w word constituent.\n\
845 _ symbol constituent. . punctuation.\n\
846 ( open-parenthesis. ) close-parenthesis.\n\
847 \" string quote. \\ escape.\n\
848 $ paired delimiter. ' expression quote or prefix operator.\n\
849 < comment starter. > comment ender.\n\
850 / character-quote. @ inherit from `standard-syntax-table'.\n\
852 Only single-character comment start and end sequences are represented thus.\n\
853 Two-character sequences are represented as described below.\n\
854 The second character of S is the matching parenthesis,\n\
855 used only if the first character is `(' or `)'.\n\
856 Any additional characters are flags.\n\
857 Defined flags are the characters 1, 2, 3, 4, b, p, and n.\n\
858 1 means CHAR is the start of a two-char comment start sequence.\n\
859 2 means CHAR is the second character of such a sequence.\n\
860 3 means CHAR is the start of a two-char comment end sequence.\n\
861 4 means CHAR is the second character of such a sequence.\n\
863 There can be up to two orthogonal comment sequences. This is to support\n\
864 language modes such as C++. By default, all comment sequences are of style\n\
865 a, but you can set the comment sequence style to b (on the second character\n\
866 of a comment-start, or the first character of a comment-end sequence) using\n\
868 b means CHAR is part of comment sequence b.\n\
869 n means CHAR is part of a nestable comment sequence.\n\
871 p means CHAR is a prefix character for `backward-prefix-chars';\n\
872 such characters are treated as whitespace when they occur\n\
873 between expressions.")
877 DEFUN ("modify-syntax-entry", Fmodify_syntax_entry
, Smodify_syntax_entry
, 2, 3,
878 /* I really don't know why this is interactive
879 help-form should at least be made useful whilst reading the second arg
881 "cSet syntax for character: \nsSet syntax for %s to: ",
882 0 /* See immediately above */)
883 (c
, newentry
, syntax_table
)
884 Lisp_Object c
, newentry
, syntax_table
;
886 register unsigned char *p
;
887 register enum syntaxcode code
;
892 CHECK_STRING (newentry
, 1);
894 if (NILP (syntax_table
))
895 syntax_table
= current_buffer
->syntax_table
;
897 check_syntax_table (syntax_table
);
899 p
= XSTRING (newentry
)->data
;
900 code
= (enum syntaxcode
) syntax_spec_code
[*p
++];
901 if (((int) code
& 0377) == 0377)
902 error ("invalid syntax description letter: %c", p
[-1]);
904 if (code
== Sinherit
)
906 SET_RAW_SYNTAX_ENTRY (syntax_table
, XINT (c
), Qnil
);
913 int character
= (STRING_CHAR_AND_LENGTH
914 (p
, STRING_BYTES (XSTRING (newentry
)) - 1, len
));
915 XSETINT (match
, character
);
916 if (XFASTINT (match
) == ' ')
956 if (val
< XVECTOR (Vsyntax_code_object
)->size
&& NILP (match
))
957 newentry
= XVECTOR (Vsyntax_code_object
)->contents
[val
];
959 /* Since we can't use a shared object, let's make a new one. */
960 newentry
= Fcons (make_number (val
), match
);
962 SET_RAW_SYNTAX_ENTRY (syntax_table
, XINT (c
), newentry
);
967 /* Dump syntax table to buffer in human-readable format */
970 describe_syntax (value
)
973 register enum syntaxcode code
;
974 char desc
, start1
, start2
, end1
, end2
, prefix
, comstyle
, comnested
;
976 Lisp_Object first
, match_lisp
;
978 Findent_to (make_number (16), make_number (1));
982 insert_string ("default\n");
986 if (CHAR_TABLE_P (value
))
988 insert_string ("deeper char-table ...\n");
994 insert_string ("invalid\n");
998 first
= XCAR (value
);
999 match_lisp
= XCDR (value
);
1001 if (!INTEGERP (first
) || !(NILP (match_lisp
) || INTEGERP (match_lisp
)))
1003 insert_string ("invalid\n");
1007 code
= (enum syntaxcode
) (XINT (first
) & 0377);
1008 start1
= (XINT (first
) >> 16) & 1;
1009 start2
= (XINT (first
) >> 17) & 1;
1010 end1
= (XINT (first
) >> 18) & 1;
1011 end2
= (XINT (first
) >> 19) & 1;
1012 prefix
= (XINT (first
) >> 20) & 1;
1013 comstyle
= (XINT (first
) >> 21) & 1;
1014 comnested
= (XINT (first
) >> 22) & 1;
1016 if ((int) code
< 0 || (int) code
>= (int) Smax
)
1018 insert_string ("invalid");
1021 desc
= syntax_code_spec
[(int) code
];
1023 str
[0] = desc
, str
[1] = 0;
1026 if (NILP (match_lisp
))
1029 insert_char (XINT (match_lisp
));
1048 insert_string ("\twhich means: ");
1050 switch (SWITCH_ENUM_CAST (code
))
1053 insert_string ("whitespace"); break;
1055 insert_string ("punctuation"); break;
1057 insert_string ("word"); break;
1059 insert_string ("symbol"); break;
1061 insert_string ("open"); break;
1063 insert_string ("close"); break;
1065 insert_string ("quote"); break;
1067 insert_string ("string"); break;
1069 insert_string ("math"); break;
1071 insert_string ("escape"); break;
1073 insert_string ("charquote"); break;
1075 insert_string ("comment"); break;
1077 insert_string ("endcomment"); break;
1079 insert_string ("invalid");
1083 if (!NILP (match_lisp
))
1085 insert_string (", matches ");
1086 insert_char (XINT (match_lisp
));
1090 insert_string (",\n\t is the first character of a comment-start sequence");
1092 insert_string (",\n\t is the second character of a comment-start sequence");
1095 insert_string (",\n\t is the first character of a comment-end sequence");
1097 insert_string (",\n\t is the second character of a comment-end sequence");
1099 insert_string (" (comment style b)");
1101 insert_string (" (nestable)");
1104 insert_string (",\n\t is a prefix character for `backward-prefix-chars'");
1106 insert_string ("\n");
1110 describe_syntax_1 (vector
)
1113 struct buffer
*old
= current_buffer
;
1114 set_buffer_internal (XBUFFER (Vstandard_output
));
1115 describe_vector (vector
, Qnil
, describe_syntax
, 0, Qnil
, Qnil
, (int *) 0, 0);
1116 while (! NILP (XCHAR_TABLE (vector
)->parent
))
1118 vector
= XCHAR_TABLE (vector
)->parent
;
1119 insert_string ("\nThe parent syntax table is:");
1120 describe_vector (vector
, Qnil
, describe_syntax
, 0, Qnil
, Qnil
,
1124 call0 (intern ("help-mode"));
1125 set_buffer_internal (old
);
1129 DEFUN ("describe-syntax", Fdescribe_syntax
, Sdescribe_syntax
, 0, 0, "",
1130 "Describe the syntax specifications in the syntax table.\n\
1131 The descriptions are inserted in a buffer, which is then displayed.")
1134 internal_with_output_to_temp_buffer
1135 ("*Help*", describe_syntax_1
, current_buffer
->syntax_table
);
1140 int parse_sexp_ignore_comments
;
1142 /* Return the position across COUNT words from FROM.
1143 If that many words cannot be found before the end of the buffer, return 0.
1144 COUNT negative means scan backward and stop at word beginning. */
1147 scan_words (from
, count
)
1148 register int from
, count
;
1150 register int beg
= BEGV
;
1151 register int end
= ZV
;
1152 register int from_byte
= CHAR_TO_BYTE (from
);
1153 register enum syntaxcode code
;
1159 SETUP_SYNTAX_TABLE (from
, count
);
1170 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1171 ch0
= FETCH_CHAR (from_byte
);
1172 code
= SYNTAX (ch0
);
1173 INC_BOTH (from
, from_byte
);
1174 if (words_include_escapes
1175 && (code
== Sescape
|| code
== Scharquote
))
1180 /* Now CH0 is a character which begins a word and FROM is the
1181 position of the next character. */
1184 if (from
== end
) break;
1185 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1186 ch1
= FETCH_CHAR (from_byte
);
1187 code
= SYNTAX (ch1
);
1188 if (!(words_include_escapes
1189 && (code
== Sescape
|| code
== Scharquote
)))
1190 if (code
!= Sword
|| WORD_BOUNDARY_P (ch0
, ch1
))
1192 INC_BOTH (from
, from_byte
);
1206 DEC_BOTH (from
, from_byte
);
1207 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
1208 ch1
= FETCH_CHAR (from_byte
);
1209 code
= SYNTAX (ch1
);
1210 if (words_include_escapes
1211 && (code
== Sescape
|| code
== Scharquote
))
1216 /* Now CH1 is a character which ends a word and FROM is the
1224 temp_byte
= dec_bytepos (from_byte
);
1225 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
1226 ch0
= FETCH_CHAR (temp_byte
);
1227 code
= SYNTAX (ch0
);
1228 if (!(words_include_escapes
1229 && (code
== Sescape
|| code
== Scharquote
)))
1230 if (code
!= Sword
|| WORD_BOUNDARY_P (ch0
, ch1
))
1232 DEC_BOTH (from
, from_byte
);
1243 DEFUN ("forward-word", Fforward_word
, Sforward_word
, 1, 1, "p",
1244 "Move point forward ARG words (backward if ARG is negative).\n\
1245 Normally returns t.\n\
1246 If an edge of the buffer or a field boundary is reached, point is left there\n\
1247 and the function returns nil. Field boundaries are not noticed if\n\
1248 `inhibit-field-text-motion' is non-nil.")
1253 CHECK_NUMBER (count
, 0);
1255 val
= orig_val
= scan_words (PT
, XINT (count
));
1257 val
= XINT (count
) > 0 ? ZV
: BEGV
;
1259 /* Avoid jumping out of an input field. */
1260 val
= XFASTINT (Fconstrain_to_field (make_number (val
), make_number (PT
),
1264 return val
== orig_val
? Qt
: Qnil
;
1267 Lisp_Object
skip_chars ();
1269 DEFUN ("skip-chars-forward", Fskip_chars_forward
, Sskip_chars_forward
, 1, 2, 0,
1270 "Move point forward, stopping before a char not in STRING, or at pos LIM.\n\
1271 STRING is like the inside of a `[...]' in a regular expression\n\
1272 except that `]' is never special and `\\' quotes `^', `-' or `\\'\n\
1273 (but not as the end of a range; quoting is never needed there).\n\
1274 Thus, with arg \"a-zA-Z\", this skips letters stopping before first nonletter.\n\
1275 With arg \"^a-zA-Z\", skips nonletters stopping before first letter.\n\
1276 Returns the distance traveled, either zero or positive.")
1278 Lisp_Object string
, lim
;
1280 return skip_chars (1, 0, string
, lim
);
1283 DEFUN ("skip-chars-backward", Fskip_chars_backward
, Sskip_chars_backward
, 1, 2, 0,
1284 "Move point backward, stopping after a char not in STRING, or at pos LIM.\n\
1285 See `skip-chars-forward' for details.\n\
1286 Returns the distance traveled, either zero or negative.")
1288 Lisp_Object string
, lim
;
1290 return skip_chars (0, 0, string
, lim
);
1293 DEFUN ("skip-syntax-forward", Fskip_syntax_forward
, Sskip_syntax_forward
, 1, 2, 0,
1294 "Move point forward across chars in specified syntax classes.\n\
1295 SYNTAX is a string of syntax code characters.\n\
1296 Stop before a char whose syntax is not in SYNTAX, or at position LIM.\n\
1297 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.\n\
1298 This function returns the distance traveled, either zero or positive.")
1300 Lisp_Object syntax
, lim
;
1302 return skip_chars (1, 1, syntax
, lim
);
1305 DEFUN ("skip-syntax-backward", Fskip_syntax_backward
, Sskip_syntax_backward
, 1, 2, 0,
1306 "Move point backward across chars in specified syntax classes.\n\
1307 SYNTAX is a string of syntax code characters.\n\
1308 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.\n\
1309 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.\n\
1310 This function returns the distance traveled, either zero or negative.")
1312 Lisp_Object syntax
, lim
;
1314 return skip_chars (0, 1, syntax
, lim
);
1318 skip_chars (forwardp
, syntaxp
, string
, lim
)
1319 int forwardp
, syntaxp
;
1320 Lisp_Object string
, lim
;
1322 register unsigned int c
;
1324 unsigned char fastmap
[0400];
1325 /* If SYNTAXP is 0, STRING may contain multi-byte form of characters
1326 of which codes don't fit in FASTMAP. In that case, we set the
1327 first byte of multibyte form (i.e. base leading-code) in FASTMAP
1328 and set the actual ranges of characters in CHAR_RANGES. In the
1329 form "X-Y" of STRING, both X and Y must belong to the same
1330 character set because a range striding across character sets is
1333 int n_char_ranges
= 0;
1335 register int i
, i_byte
;
1336 int multibyte
= !NILP (current_buffer
->enable_multibyte_characters
);
1337 int string_multibyte
;
1340 CHECK_STRING (string
, 0);
1341 char_ranges
= (int *) alloca (XSTRING (string
)->size
* (sizeof (int)) * 2);
1342 string_multibyte
= STRING_MULTIBYTE (string
);
1343 size_byte
= STRING_BYTES (XSTRING (string
));
1346 XSETINT (lim
, forwardp
? ZV
: BEGV
);
1348 CHECK_NUMBER_COERCE_MARKER (lim
, 0);
1350 /* In any case, don't allow scan outside bounds of buffer. */
1351 if (XINT (lim
) > ZV
)
1352 XSETFASTINT (lim
, ZV
);
1353 if (XINT (lim
) < BEGV
)
1354 XSETFASTINT (lim
, BEGV
);
1356 bzero (fastmap
, sizeof fastmap
);
1360 if (i_byte
< size_byte
1361 && XSTRING (string
)->data
[0] == '^')
1363 negate
= 1; i
++, i_byte
++;
1366 /* Find the characters specified and set their elements of fastmap.
1367 If syntaxp, each character counts as itself.
1368 Otherwise, handle backslashes and ranges specially. */
1370 while (i_byte
< size_byte
)
1372 int c_leading_code
= XSTRING (string
)->data
[i_byte
];
1374 FETCH_STRING_CHAR_ADVANCE (c
, string
, i
, i_byte
);
1376 /* Convert multibyteness between what the string has
1377 and what the buffer has. */
1379 c
= unibyte_char_to_multibyte (c
);
1384 fastmap
[syntax_spec_code
[c
& 0377]] = 1;
1389 if (i_byte
== size_byte
)
1392 c_leading_code
= XSTRING (string
)->data
[i_byte
];
1393 FETCH_STRING_CHAR_ADVANCE (c
, string
, i
, i_byte
);
1395 if (i_byte
< size_byte
1396 && XSTRING (string
)->data
[i_byte
] == '-')
1398 unsigned int c2
, c2_leading_code
;
1400 /* Skip over the dash. */
1403 if (i_byte
== size_byte
)
1406 /* Get the end of the range. */
1407 c2_leading_code
= XSTRING (string
)->data
[i_byte
];
1408 FETCH_STRING_CHAR_ADVANCE (c2
, string
, i
, i_byte
);
1410 if (SINGLE_BYTE_CHAR_P (c
))
1412 if (! SINGLE_BYTE_CHAR_P (c2
))
1413 error ("Invalid charcter range: %s",
1414 XSTRING (string
)->data
);
1423 if (c_leading_code
!= c2_leading_code
)
1424 error ("Invalid charcter range: %s",
1425 XSTRING (string
)->data
);
1426 fastmap
[c_leading_code
] = 1;
1429 char_ranges
[n_char_ranges
++] = c
;
1430 char_ranges
[n_char_ranges
++] = c2
;
1436 fastmap
[c_leading_code
] = 1;
1437 if (!SINGLE_BYTE_CHAR_P (c
))
1439 char_ranges
[n_char_ranges
++] = c
;
1440 char_ranges
[n_char_ranges
++] = c
;
1446 /* If ^ was the first character, complement the fastmap. In
1447 addition, as all multibyte characters have possibility of
1448 matching, set all entries for base leading codes, which is
1449 harmless even if SYNTAXP is 1. */
1452 for (i
= 0; i
< sizeof fastmap
; i
++)
1454 if (!multibyte
|| !BASE_LEADING_CODE_P (i
))
1461 int start_point
= PT
;
1463 int pos_byte
= PT_BYTE
;
1468 SETUP_SYNTAX_TABLE (pos
, forwardp
? 1 : -1);
1473 if (pos
< XINT (lim
))
1474 while (fastmap
[(int) SYNTAX (FETCH_CHAR (pos_byte
))])
1476 /* Since we already checked for multibyteness,
1477 avoid using INC_BOTH which checks again. */
1480 if (pos
>= XINT (lim
))
1482 UPDATE_SYNTAX_TABLE_FORWARD (pos
);
1487 while (pos
< XINT (lim
)
1488 && fastmap
[(int) SYNTAX (FETCH_BYTE (pos
))])
1491 UPDATE_SYNTAX_TABLE_FORWARD (pos
);
1499 while (pos
> XINT (lim
))
1501 int savepos
= pos_byte
;
1502 /* Since we already checked for multibyteness,
1503 avoid using DEC_BOTH which checks again. */
1506 UPDATE_SYNTAX_TABLE_BACKWARD (pos
);
1507 if (!fastmap
[(int) SYNTAX (FETCH_CHAR (pos_byte
))])
1517 if (pos
> XINT (lim
))
1518 while (fastmap
[(int) SYNTAX (FETCH_BYTE (pos
- 1))])
1521 if (pos
<= XINT (lim
))
1523 UPDATE_SYNTAX_TABLE_BACKWARD (pos
- 1);
1533 while (pos
< XINT (lim
) && fastmap
[(c
= FETCH_BYTE (pos_byte
))])
1535 if (!BASE_LEADING_CODE_P (c
))
1536 INC_BOTH (pos
, pos_byte
);
1537 else if (n_char_ranges
)
1539 /* We much check CHAR_RANGES for a multibyte
1541 ch
= FETCH_MULTIBYTE_CHAR (pos_byte
);
1542 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1543 if ((ch
>= char_ranges
[i
] && ch
<= char_ranges
[i
+ 1]))
1545 if (!(negate
^ (i
< n_char_ranges
)))
1548 INC_BOTH (pos
, pos_byte
);
1553 INC_BOTH (pos
, pos_byte
);
1557 while (pos
< XINT (lim
) && fastmap
[FETCH_BYTE (pos
)])
1563 while (pos
> XINT (lim
))
1565 int savepos
= pos_byte
;
1566 DEC_BOTH (pos
, pos_byte
);
1567 if (fastmap
[(c
= FETCH_BYTE (pos_byte
))])
1569 if (!BASE_LEADING_CODE_P (c
))
1571 else if (n_char_ranges
)
1573 /* We much check CHAR_RANGES for a multibyte
1575 ch
= FETCH_MULTIBYTE_CHAR (pos_byte
);
1576 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1577 if (ch
>= char_ranges
[i
] && ch
<= char_ranges
[i
+ 1])
1579 if (!(negate
^ (i
< n_char_ranges
)))
1602 while (pos
> XINT (lim
) && fastmap
[FETCH_BYTE (pos
- 1)])
1607 #if 0 /* Not needed now that a position in mid-character
1608 cannot be specified in Lisp. */
1610 /* INC_POS or DEC_POS might have moved POS over LIM. */
1611 && (forwardp
? (pos
> XINT (lim
)) : (pos
< XINT (lim
))))
1618 SET_PT_BOTH (pos
, pos_byte
);
1621 return make_number (PT
- start_point
);
1625 /* Jump over a comment, assuming we are at the beginning of one.
1626 FROM is the current position.
1627 FROM_BYTE is the bytepos corresponding to FROM.
1628 Do not move past STOP (a charpos).
1629 The comment over which we have to jump is of style STYLE
1630 (either SYNTAX_COMMENT_STYLE(foo) or ST_COMMENT_STYLE).
1631 NESTING should be positive to indicate the nesting at the beginning
1632 for nested comments and should be zero or negative else.
1633 ST_COMMENT_STYLE cannot be nested.
1634 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
1635 (or 0 If the search cannot start in the middle of a two-character).
1637 If successful, return 1 and store the charpos of the comment's end
1638 into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR.
1639 Else, return 0 and store the charpos STOP into *CHARPOS_PTR, the
1640 corresponding bytepos into *BYTEPOS_PTR and the current nesting
1641 (as defined for state.incomment) in *INCOMMENT_PTR.
1643 The comment end is the last character of the comment rather than the
1644 character just after the comment.
1646 Global syntax data is assumed to initially be valid for FROM and
1647 remains valid for forward search starting at the returned position. */
1650 forw_comment (from
, from_byte
, stop
, nesting
, style
, prev_syntax
,
1651 charpos_ptr
, bytepos_ptr
, incomment_ptr
)
1652 int from
, from_byte
, stop
;
1653 int nesting
, style
, prev_syntax
;
1654 int *charpos_ptr
, *bytepos_ptr
, *incomment_ptr
;
1657 register enum syntaxcode code
;
1658 register int syntax
;
1660 if (nesting
<= 0) nesting
= -1;
1662 /* Enter the loop in the middle so that we find
1663 a 2-char comment ender if we start in the middle of it. */
1664 syntax
= prev_syntax
;
1665 if (syntax
!= 0) goto forw_incomment
;
1671 *incomment_ptr
= nesting
;
1672 *charpos_ptr
= from
;
1673 *bytepos_ptr
= from_byte
;
1676 c
= FETCH_CHAR (from_byte
);
1677 syntax
= SYNTAX_WITH_FLAGS (c
);
1678 code
= syntax
& 0xff;
1679 if (code
== Sendcomment
1680 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
) == style
1682 /* we have encountered a comment end of the same style
1683 as the comment sequence which began this comment
1686 if (code
== Scomment_fence
1687 && style
== ST_COMMENT_STYLE
)
1688 /* we have encountered a comment end of the same style
1689 as the comment sequence which began this comment
1694 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
) == style
)
1695 /* we have encountered a nested comment of the same style
1696 as the comment sequence which began this comment section */
1698 INC_BOTH (from
, from_byte
);
1699 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1702 if (from
< stop
&& SYNTAX_FLAGS_COMEND_FIRST (syntax
)
1703 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
) == style
1704 && (c1
= FETCH_CHAR (from_byte
),
1705 SYNTAX_COMEND_SECOND (c1
)))
1708 /* we have encountered a comment end of the same style
1709 as the comment sequence which began this comment
1714 INC_BOTH (from
, from_byte
);
1715 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1720 && SYNTAX_FLAGS_COMSTART_FIRST (syntax
)
1721 && (c1
= FETCH_CHAR (from_byte
),
1722 SYNTAX_COMMENT_STYLE (c1
) == style
1723 && SYNTAX_COMSTART_SECOND (c1
)))
1724 /* we have encountered a nested comment of the same style
1725 as the comment sequence which began this comment
1728 INC_BOTH (from
, from_byte
);
1729 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1733 *charpos_ptr
= from
;
1734 *bytepos_ptr
= from_byte
;
1738 DEFUN ("forward-comment", Fforward_comment
, Sforward_comment
, 1, 1, 0,
1739 "Move forward across up to N comments. If N is negative, move backward.\n\
1740 Stop scanning if we find something other than a comment or whitespace.\n\
1741 Set point to where scanning stops.\n\
1742 If N comments are found as expected, with nothing except whitespace\n\
1743 between them, return t; otherwise return nil.")
1751 register enum syntaxcode code
;
1752 int comstyle
= 0; /* style of comment encountered */
1753 int comnested
= 0; /* whether the comment is nestable or not */
1756 int out_charpos
, out_bytepos
;
1759 CHECK_NUMBER (count
, 0);
1760 count1
= XINT (count
);
1761 stop
= count1
> 0 ? ZV
: BEGV
;
1767 from_byte
= PT_BYTE
;
1769 SETUP_SYNTAX_TABLE (from
, count1
);
1778 SET_PT_BOTH (from
, from_byte
);
1782 c
= FETCH_CHAR (from_byte
);
1784 comstart_first
= SYNTAX_COMSTART_FIRST (c
);
1785 comnested
= SYNTAX_COMMENT_NESTED (c
);
1786 comstyle
= SYNTAX_COMMENT_STYLE (c
);
1787 INC_BOTH (from
, from_byte
);
1788 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1789 if (from
< stop
&& comstart_first
1790 && (c1
= FETCH_CHAR (from_byte
),
1791 SYNTAX_COMSTART_SECOND (c1
)))
1793 /* We have encountered a comment start sequence and we
1794 are ignoring all text inside comments. We must record
1795 the comment style this sequence begins so that later,
1796 only a comment end of the same style actually ends
1797 the comment section. */
1799 comstyle
= SYNTAX_COMMENT_STYLE (c1
);
1800 comnested
= comnested
|| SYNTAX_COMMENT_NESTED (c1
);
1801 INC_BOTH (from
, from_byte
);
1802 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1804 /* FIXME: here we ignore 2-char endcomments while we don't
1805 when going backwards. */
1807 while (code
== Swhitespace
|| code
== Sendcomment
);
1809 if (code
== Scomment_fence
)
1810 comstyle
= ST_COMMENT_STYLE
;
1811 else if (code
!= Scomment
)
1814 DEC_BOTH (from
, from_byte
);
1815 SET_PT_BOTH (from
, from_byte
);
1818 /* We're at the start of a comment. */
1819 found
= forw_comment (from
, from_byte
, stop
, comnested
, comstyle
, 0,
1820 &out_charpos
, &out_bytepos
, &dummy
);
1821 from
= out_charpos
; from_byte
= out_bytepos
;
1825 SET_PT_BOTH (from
, from_byte
);
1828 INC_BOTH (from
, from_byte
);
1829 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1830 /* We have skipped one comment. */
1838 int quoted
, comstart_second
;
1842 SET_PT_BOTH (BEGV
, BEGV_BYTE
);
1847 DEC_BOTH (from
, from_byte
);
1848 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
1849 quoted
= char_quoted (from
, from_byte
);
1852 DEC_BOTH (from
, from_byte
);
1855 c
= FETCH_CHAR (from_byte
);
1858 comnested
= SYNTAX_COMMENT_NESTED (c
);
1859 if (code
== Sendcomment
)
1860 comstyle
= SYNTAX_COMMENT_STYLE (c
);
1861 comstart_second
= SYNTAX_COMSTART_SECOND (c
);
1862 if (from
> stop
&& SYNTAX_COMEND_SECOND (c
)
1863 && prev_char_comend_first (from
, from_byte
)
1864 && !char_quoted (from
- 1, dec_bytepos (from_byte
)))
1866 /* We must record the comment style encountered so that
1867 later, we can match only the proper comment begin
1868 sequence of the same style. */
1869 DEC_BOTH (from
, from_byte
);
1871 /* Calling char_quoted, above, set up global syntax position
1872 at the new value of FROM. */
1873 c1
= FETCH_CHAR (from_byte
);
1874 comstyle
= SYNTAX_COMMENT_STYLE (c1
);
1875 comnested
= comnested
|| SYNTAX_COMMENT_NESTED (c1
);
1877 if (from
> stop
&& comstart_second
1878 && prev_char_comstart_first (from
, from_byte
)
1879 && !char_quoted (from
- 1, dec_bytepos (from_byte
)))
1882 DEC_BOTH (from
, from_byte
);
1885 if (code
== Scomment_fence
)
1887 /* Skip until first preceding unquoted comment_fence. */
1888 int found
= 0, ini
= from
, ini_byte
= from_byte
;
1892 DEC_BOTH (from
, from_byte
);
1895 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
1896 c
= FETCH_CHAR (from_byte
);
1897 if (SYNTAX (c
) == Scomment_fence
1898 && !char_quoted (from
, from_byte
))
1906 from
= ini
; /* Set point to ini + 1. */
1907 from_byte
= ini_byte
;
1911 else if (code
== Sendcomment
)
1913 found
= back_comment (from
, from_byte
, stop
, comnested
, comstyle
,
1914 &out_charpos
, &out_bytepos
);
1917 #if 0 /* cc-mode (and maybe others) relies on the bogus behavior. */
1918 /* Failure: we should go back to the end of this
1919 not-quite-endcomment. */
1920 if (SYNTAX(c
) != code
)
1921 /* It was a two-char Sendcomment. */
1922 INC_BOTH (from
, from_byte
);
1927 /* We have skipped one comment. */
1928 from
= out_charpos
, from_byte
= out_bytepos
;
1931 else if (code
!= Swhitespace
&& code
!= Scomment
)
1935 INC_BOTH (from
, from_byte
);
1936 SET_PT_BOTH (from
, from_byte
);
1944 SET_PT_BOTH (from
, from_byte
);
1949 /* Return syntax code of character C if C is a single byte character
1950 or `multibyte_symbol_p' is zero. Otherwise, retrun Ssymbol. */
1952 #define SYNTAX_WITH_MULTIBYTE_CHECK(c) \
1953 ((SINGLE_BYTE_CHAR_P (c) || !multibyte_symbol_p) \
1954 ? SYNTAX (c) : Ssymbol)
1957 scan_lists (from
, count
, depth
, sexpflag
)
1959 int count
, depth
, sexpflag
;
1962 register int stop
= count
> 0 ? ZV
: BEGV
;
1967 register enum syntaxcode code
, temp_code
;
1968 int min_depth
= depth
; /* Err out if depth gets less than this. */
1969 int comstyle
= 0; /* style of comment encountered */
1970 int comnested
= 0; /* whether the comment is nestable or not */
1972 int last_good
= from
;
1975 int out_bytepos
, out_charpos
;
1977 int multibyte_symbol_p
= sexpflag
&& multibyte_syntax_as_symbol
;
1979 if (depth
> 0) min_depth
= 0;
1981 if (from
> ZV
) from
= ZV
;
1982 if (from
< BEGV
) from
= BEGV
;
1984 from_byte
= CHAR_TO_BYTE (from
);
1989 SETUP_SYNTAX_TABLE (from
, count
);
1994 int comstart_first
, prefix
;
1995 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1996 c
= FETCH_CHAR (from_byte
);
1997 code
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
1998 comstart_first
= SYNTAX_COMSTART_FIRST (c
);
1999 comnested
= SYNTAX_COMMENT_NESTED (c
);
2000 comstyle
= SYNTAX_COMMENT_STYLE (c
);
2001 prefix
= SYNTAX_PREFIX (c
);
2002 if (depth
== min_depth
)
2004 INC_BOTH (from
, from_byte
);
2005 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2006 if (from
< stop
&& comstart_first
2007 && SYNTAX_COMSTART_SECOND (FETCH_CHAR (from_byte
))
2008 && parse_sexp_ignore_comments
)
2010 /* we have encountered a comment start sequence and we
2011 are ignoring all text inside comments. We must record
2012 the comment style this sequence begins so that later,
2013 only a comment end of the same style actually ends
2014 the comment section */
2016 c1
= FETCH_CHAR (from_byte
);
2017 comstyle
= SYNTAX_COMMENT_STYLE (c1
);
2018 comnested
= comnested
|| SYNTAX_COMMENT_NESTED (c1
);
2019 INC_BOTH (from
, from_byte
);
2020 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2026 switch (SWITCH_ENUM_CAST (code
))
2030 if (from
== stop
) goto lose
;
2031 INC_BOTH (from
, from_byte
);
2032 /* treat following character as a word constituent */
2035 if (depth
|| !sexpflag
) break;
2036 /* This word counts as a sexp; return at end of it. */
2039 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2041 /* Some compilers can't handle this inside the switch. */
2042 c
= FETCH_CHAR (from_byte
);
2043 temp
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2048 INC_BOTH (from
, from_byte
);
2049 if (from
== stop
) goto lose
;
2058 INC_BOTH (from
, from_byte
);
2062 case Scomment_fence
:
2063 comstyle
= ST_COMMENT_STYLE
;
2066 if (!parse_sexp_ignore_comments
) break;
2067 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2068 found
= forw_comment (from
, from_byte
, stop
,
2069 comnested
, comstyle
, 0,
2070 &out_charpos
, &out_bytepos
, &dummy
);
2071 from
= out_charpos
, from_byte
= out_bytepos
;
2078 INC_BOTH (from
, from_byte
);
2079 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2085 if (from
!= stop
&& c
== FETCH_CHAR (from_byte
))
2087 INC_BOTH (from
, from_byte
);
2097 if (!++depth
) goto done
;
2102 if (!--depth
) goto done
;
2103 if (depth
< min_depth
)
2104 Fsignal (Qscan_error
,
2105 Fcons (build_string ("Containing expression ends prematurely"),
2106 Fcons (make_number (last_good
),
2107 Fcons (make_number (from
), Qnil
))));
2112 temp_pos
= dec_bytepos (from_byte
);
2113 stringterm
= FETCH_CHAR (temp_pos
);
2116 if (from
>= stop
) goto lose
;
2117 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2118 c
= FETCH_CHAR (from_byte
);
2121 : SYNTAX_WITH_MULTIBYTE_CHECK (c
) == Sstring_fence
)
2124 /* Some compilers can't handle this inside the switch. */
2125 temp
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2130 INC_BOTH (from
, from_byte
);
2132 INC_BOTH (from
, from_byte
);
2134 INC_BOTH (from
, from_byte
);
2135 if (!depth
&& sexpflag
) goto done
;
2140 /* Reached end of buffer. Error if within object, return nil if between */
2141 if (depth
) goto lose
;
2146 /* End of object reached */
2155 DEC_BOTH (from
, from_byte
);
2156 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2157 c
= FETCH_CHAR (from_byte
);
2158 code
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2159 if (depth
== min_depth
)
2162 comnested
= SYNTAX_COMMENT_NESTED (c
);
2163 if (code
== Sendcomment
)
2164 comstyle
= SYNTAX_COMMENT_STYLE (c
);
2165 if (from
> stop
&& SYNTAX_COMEND_SECOND (c
)
2166 && prev_char_comend_first (from
, from_byte
)
2167 && parse_sexp_ignore_comments
)
2169 /* We must record the comment style encountered so that
2170 later, we can match only the proper comment begin
2171 sequence of the same style. */
2172 DEC_BOTH (from
, from_byte
);
2173 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2175 c1
= FETCH_CHAR (from_byte
);
2176 comstyle
= SYNTAX_COMMENT_STYLE (c1
);
2177 comnested
= comnested
|| SYNTAX_COMMENT_NESTED (c1
);
2180 /* Quoting turns anything except a comment-ender
2181 into a word character. Note that this cannot be true
2182 if we decremented FROM in the if-statement above. */
2183 if (code
!= Sendcomment
&& char_quoted (from
, from_byte
))
2185 else if (SYNTAX_PREFIX (c
))
2188 switch (SWITCH_ENUM_CAST (code
))
2194 if (depth
|| !sexpflag
) break;
2195 /* This word counts as a sexp; count object finished
2196 after passing it. */
2199 temp_pos
= from_byte
;
2200 if (! NILP (current_buffer
->enable_multibyte_characters
))
2204 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2205 c1
= FETCH_CHAR (temp_pos
);
2206 temp_code
= SYNTAX_WITH_MULTIBYTE_CHECK (c1
);
2207 /* Don't allow comment-end to be quoted. */
2208 if (temp_code
== Sendcomment
)
2210 quoted
= char_quoted (from
- 1, temp_pos
);
2213 DEC_BOTH (from
, from_byte
);
2214 temp_pos
= dec_bytepos (temp_pos
);
2215 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2217 c1
= FETCH_CHAR (temp_pos
);
2218 temp_code
= SYNTAX_WITH_MULTIBYTE_CHECK (c1
);
2219 if (! (quoted
|| temp_code
== Sword
2220 || temp_code
== Ssymbol
2221 || temp_code
== Squote
))
2223 DEC_BOTH (from
, from_byte
);
2230 temp_pos
= dec_bytepos (from_byte
);
2231 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2232 if (from
!= stop
&& c
== FETCH_CHAR (temp_pos
))
2233 DEC_BOTH (from
, from_byte
);
2242 if (!++depth
) goto done2
;
2247 if (!--depth
) goto done2
;
2248 if (depth
< min_depth
)
2249 Fsignal (Qscan_error
,
2250 Fcons (build_string ("Containing expression ends prematurely"),
2251 Fcons (make_number (last_good
),
2252 Fcons (make_number (from
), Qnil
))));
2256 if (!parse_sexp_ignore_comments
)
2258 found
= back_comment (from
, from_byte
, stop
, comnested
, comstyle
,
2259 &out_charpos
, &out_bytepos
);
2260 /* FIXME: if found == -1, then it really wasn't a comment-end.
2261 For single-char Sendcomment, we can't do much about it apart
2262 from skipping the char.
2263 For 2-char endcomments, we could try again, taking both
2264 chars as separate entities, but it's a lot of trouble
2265 for very little gain, so we don't bother either. -sm */
2267 from
= out_charpos
, from_byte
= out_bytepos
;
2270 case Scomment_fence
:
2274 DEC_BOTH (from
, from_byte
);
2275 if (from
== stop
) goto lose
;
2276 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2277 if (!char_quoted (from
, from_byte
)
2278 && (c
= FETCH_CHAR (from_byte
),
2279 SYNTAX_WITH_MULTIBYTE_CHECK (c
) == code
))
2282 if (code
== Sstring_fence
&& !depth
&& sexpflag
) goto done2
;
2286 stringterm
= FETCH_CHAR (from_byte
);
2289 if (from
== stop
) goto lose
;
2290 temp_pos
= from_byte
;
2291 if (! NILP (current_buffer
->enable_multibyte_characters
))
2295 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2296 if (!char_quoted (from
- 1, temp_pos
)
2297 && stringterm
== FETCH_CHAR (temp_pos
))
2299 DEC_BOTH (from
, from_byte
);
2301 DEC_BOTH (from
, from_byte
);
2302 if (!depth
&& sexpflag
) goto done2
;
2307 /* Reached start of buffer. Error if within object, return nil if between */
2308 if (depth
) goto lose
;
2319 XSETFASTINT (val
, from
);
2323 Fsignal (Qscan_error
,
2324 Fcons (build_string ("Unbalanced parentheses"),
2325 Fcons (make_number (last_good
),
2326 Fcons (make_number (from
), Qnil
))));
2331 DEFUN ("scan-lists", Fscan_lists
, Sscan_lists
, 3, 3, 0,
2332 "Scan from character number FROM by COUNT lists.\n\
2333 Returns the character number of the position thus found.\n\
2335 If DEPTH is nonzero, paren depth begins counting from that value,\n\
2336 only places where the depth in parentheses becomes zero\n\
2337 are candidates for stopping; COUNT such places are counted.\n\
2338 Thus, a positive value for DEPTH means go out levels.\n\
2340 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.\n\
2342 If the beginning or end of (the accessible part of) the buffer is reached\n\
2343 and the depth is wrong, an error is signaled.\n\
2344 If the depth is right but the count is not used up, nil is returned.")
2345 (from
, count
, depth
)
2346 Lisp_Object from
, count
, depth
;
2348 CHECK_NUMBER (from
, 0);
2349 CHECK_NUMBER (count
, 1);
2350 CHECK_NUMBER (depth
, 2);
2352 return scan_lists (XINT (from
), XINT (count
), XINT (depth
), 0);
2355 DEFUN ("scan-sexps", Fscan_sexps
, Sscan_sexps
, 2, 2, 0,
2356 "Scan from character number FROM by COUNT balanced expressions.\n\
2357 If COUNT is negative, scan backwards.\n\
2358 Returns the character number of the position thus found.\n\
2360 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.\n\
2362 If the beginning or end of (the accessible part of) the buffer is reached\n\
2363 in the middle of a parenthetical grouping, an error is signaled.\n\
2364 If the beginning or end is reached between groupings\n\
2365 but before count is used up, nil is returned.")
2367 Lisp_Object from
, count
;
2369 CHECK_NUMBER (from
, 0);
2370 CHECK_NUMBER (count
, 1);
2372 return scan_lists (XINT (from
), XINT (count
), 0, 1);
2375 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars
, Sbackward_prefix_chars
,
2377 "Move point backward over any number of chars with prefix syntax.\n\
2378 This includes chars with \"quote\" or \"prefix\" syntax (' or p).")
2383 int opoint_byte
= PT_BYTE
;
2385 int pos_byte
= PT_BYTE
;
2390 SET_PT_BOTH (opoint
, opoint_byte
);
2395 SETUP_SYNTAX_TABLE (pos
, -1);
2397 DEC_BOTH (pos
, pos_byte
);
2399 while (!char_quoted (pos
, pos_byte
)
2400 /* Previous statement updates syntax table. */
2401 && ((c
= FETCH_CHAR (pos_byte
), SYNTAX (c
) == Squote
)
2402 || SYNTAX_PREFIX (c
)))
2405 opoint_byte
= pos_byte
;
2408 DEC_BOTH (pos
, pos_byte
);
2411 SET_PT_BOTH (opoint
, opoint_byte
);
2416 /* Parse forward from FROM / FROM_BYTE to END,
2417 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
2418 and return a description of the state of the parse at END.
2419 If STOPBEFORE is nonzero, stop at the start of an atom.
2420 If COMMENTSTOP is 1, stop at the start of a comment.
2421 If COMMENTSTOP is -1, stop at the start or end of a comment,
2422 after the beginning of a string, or after the end of a string. */
2425 scan_sexps_forward (stateptr
, from
, from_byte
, end
, targetdepth
,
2426 stopbefore
, oldstate
, commentstop
)
2427 struct lisp_parse_state
*stateptr
;
2429 int end
, targetdepth
, stopbefore
;
2430 Lisp_Object oldstate
;
2433 struct lisp_parse_state state
;
2435 register enum syntaxcode code
;
2438 struct level
{ int last
, prev
; };
2439 struct level levelstart
[100];
2440 register struct level
*curlevel
= levelstart
;
2441 struct level
*endlevel
= levelstart
+ 100;
2442 register int depth
; /* Paren depth of current scanning location.
2443 level - levelstart equals this except
2444 when the depth becomes negative. */
2445 int mindepth
; /* Lowest DEPTH value seen. */
2446 int start_quoted
= 0; /* Nonzero means starting after a char quote */
2448 int prev_from
; /* Keep one character before FROM. */
2450 int prev_from_syntax
;
2451 int boundary_stop
= commentstop
== -1;
2454 int out_bytepos
, out_charpos
;
2458 prev_from_byte
= from_byte
;
2460 DEC_BOTH (prev_from
, prev_from_byte
);
2462 /* Use this macro instead of `from++'. */
2464 do { prev_from = from; \
2465 prev_from_byte = from_byte; \
2467 = SYNTAX_WITH_FLAGS (FETCH_CHAR (prev_from_byte)); \
2468 INC_BOTH (from, from_byte); \
2469 UPDATE_SYNTAX_TABLE_FORWARD (from); \
2475 if (NILP (oldstate
))
2478 state
.instring
= -1;
2479 state
.incomment
= 0;
2480 state
.comstyle
= 0; /* comment style a by default. */
2481 state
.comstr_start
= -1; /* no comment/string seen. */
2485 tem
= Fcar (oldstate
);
2491 oldstate
= Fcdr (oldstate
);
2492 oldstate
= Fcdr (oldstate
);
2493 oldstate
= Fcdr (oldstate
);
2494 tem
= Fcar (oldstate
);
2495 /* Check whether we are inside string_fence-style string: */
2496 state
.instring
= (!NILP (tem
)
2497 ? (INTEGERP (tem
) ? XINT (tem
) : ST_STRING_STYLE
)
2500 oldstate
= Fcdr (oldstate
);
2501 tem
= Fcar (oldstate
);
2502 state
.incomment
= (!NILP (tem
)
2503 ? (INTEGERP (tem
) ? XINT (tem
) : -1)
2506 oldstate
= Fcdr (oldstate
);
2507 tem
= Fcar (oldstate
);
2508 start_quoted
= !NILP (tem
);
2510 /* if the eighth element of the list is nil, we are in comment
2511 style a. If it is non-nil, we are in comment style b */
2512 oldstate
= Fcdr (oldstate
);
2513 oldstate
= Fcdr (oldstate
);
2514 tem
= Fcar (oldstate
);
2515 state
.comstyle
= NILP (tem
) ? 0 : (EQ (tem
, Qsyntax_table
)
2516 ? ST_COMMENT_STYLE
: 1);
2518 oldstate
= Fcdr (oldstate
);
2519 tem
= Fcar (oldstate
);
2520 state
.comstr_start
= NILP (tem
) ? -1 : XINT (tem
) ;
2521 oldstate
= Fcdr (oldstate
);
2522 tem
= Fcar (oldstate
);
2523 while (!NILP (tem
)) /* >= second enclosing sexps. */
2525 /* curlevel++->last ran into compiler bug on Apollo */
2526 curlevel
->last
= XINT (Fcar (tem
));
2527 if (++curlevel
== endlevel
)
2528 error ("Nesting too deep for parser");
2529 curlevel
->prev
= -1;
2530 curlevel
->last
= -1;
2537 curlevel
->prev
= -1;
2538 curlevel
->last
= -1;
2540 SETUP_SYNTAX_TABLE (prev_from
, 1);
2541 prev_from_syntax
= SYNTAX_WITH_FLAGS (FETCH_CHAR (prev_from_byte
));
2542 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2544 /* Enter the loop at a place appropriate for initial state. */
2546 if (state
.incomment
)
2547 goto startincomment
;
2548 if (state
.instring
>= 0)
2550 nofence
= state
.instring
!= ST_STRING_STYLE
;
2552 goto startquotedinstring
;
2555 else if (start_quoted
)
2558 #if 0 /* This seems to be redundant with the identical code above. */
2559 SETUP_SYNTAX_TABLE (prev_from
, 1);
2560 prev_from_syntax
= SYNTAX_WITH_FLAGS (FETCH_CHAR (prev_from_byte
));
2561 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2567 code
= prev_from_syntax
& 0xff;
2569 if (code
== Scomment
)
2571 state
.comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax
);
2572 state
.incomment
= (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax
) ?
2574 state
.comstr_start
= prev_from
;
2576 else if (code
== Scomment_fence
)
2578 /* Record the comment style we have entered so that only
2579 the comment-end sequence of the same style actually
2580 terminates the comment section. */
2581 state
.comstyle
= ST_COMMENT_STYLE
;
2582 state
.incomment
= -1;
2583 state
.comstr_start
= prev_from
;
2586 else if (from
< end
)
2587 if (SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax
))
2588 if (c1
= FETCH_CHAR (from_byte
),
2589 SYNTAX_COMSTART_SECOND (c1
))
2590 /* Duplicate code to avoid a complex if-expression
2591 which causes trouble for the SGI compiler. */
2593 /* Record the comment style we have entered so that only
2594 the comment-end sequence of the same style actually
2595 terminates the comment section. */
2596 state
.comstyle
= SYNTAX_COMMENT_STYLE (FETCH_CHAR (from_byte
));
2597 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax
);
2598 comnested
= comnested
|| SYNTAX_COMMENT_NESTED (c1
);
2599 state
.incomment
= comnested
? 1 : -1;
2600 state
.comstr_start
= prev_from
;
2605 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax
))
2607 switch (SWITCH_ENUM_CAST (code
))
2611 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
2612 curlevel
->last
= prev_from
;
2614 if (from
== end
) goto endquoted
;
2617 /* treat following character as a word constituent */
2620 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
2621 curlevel
->last
= prev_from
;
2625 /* Some compilers can't handle this inside the switch. */
2626 temp
= SYNTAX (FETCH_CHAR (from_byte
));
2632 if (from
== end
) goto endquoted
;
2644 curlevel
->prev
= curlevel
->last
;
2648 if (commentstop
|| boundary_stop
) goto done
;
2650 /* The (from == BEGV) test was to enter the loop in the middle so
2651 that we find a 2-char comment ender even if we start in the
2652 middle of it. We don't want to do that if we're just at the
2653 beginning of the comment (think of (*) ... (*)). */
2654 found
= forw_comment (from
, from_byte
, end
,
2655 state
.incomment
, state
.comstyle
,
2656 (from
== BEGV
|| from
< state
.comstr_start
+ 3)
2657 ? 0 : prev_from_syntax
,
2658 &out_charpos
, &out_bytepos
, &state
.incomment
);
2659 from
= out_charpos
; from_byte
= out_bytepos
;
2660 /* Beware! prev_from and friends are invalid now.
2661 Luckily, the `done' doesn't use them and the INC_FROM
2662 sets them to a sane value without looking at them. */
2663 if (!found
) goto done
;
2665 state
.incomment
= 0;
2666 state
.comstyle
= 0; /* reset the comment style */
2667 if (boundary_stop
) goto done
;
2671 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
2673 /* curlevel++->last ran into compiler bug on Apollo */
2674 curlevel
->last
= prev_from
;
2675 if (++curlevel
== endlevel
)
2676 error ("Nesting too deep for parser");
2677 curlevel
->prev
= -1;
2678 curlevel
->last
= -1;
2679 if (targetdepth
== depth
) goto done
;
2684 if (depth
< mindepth
)
2686 if (curlevel
!= levelstart
)
2688 curlevel
->prev
= curlevel
->last
;
2689 if (targetdepth
== depth
) goto done
;
2694 state
.comstr_start
= from
- 1;
2695 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
2696 curlevel
->last
= prev_from
;
2697 state
.instring
= (code
== Sstring
2698 ? (FETCH_CHAR (prev_from_byte
))
2700 if (boundary_stop
) goto done
;
2703 nofence
= state
.instring
!= ST_STRING_STYLE
;
2709 if (from
>= end
) goto done
;
2710 c
= FETCH_CHAR (from_byte
);
2711 /* Some compilers can't handle this inside the switch. */
2714 /* Check TEMP here so that if the char has
2715 a syntax-table property which says it is NOT
2716 a string character, it does not end the string. */
2717 if (nofence
&& c
== state
.instring
&& temp
== Sstring
)
2723 if (!nofence
) goto string_end
;
2728 startquotedinstring
:
2729 if (from
>= end
) goto endquoted
;
2735 state
.instring
= -1;
2736 curlevel
->prev
= curlevel
->last
;
2738 if (boundary_stop
) goto done
;
2747 stop
: /* Here if stopping before start of sexp. */
2748 from
= prev_from
; /* We have just fetched the char that starts it; */
2749 goto done
; /* but return the position before it. */
2754 state
.depth
= depth
;
2755 state
.mindepth
= mindepth
;
2756 state
.thislevelstart
= curlevel
->prev
;
2757 state
.prevlevelstart
2758 = (curlevel
== levelstart
) ? -1 : (curlevel
- 1)->last
;
2759 state
.location
= from
;
2760 state
.levelstarts
= Qnil
;
2761 while (--curlevel
>= levelstart
)
2762 state
.levelstarts
= Fcons (make_number (curlevel
->last
),
2769 /* This comment supplies the doc string for parse-partial-sexp,
2770 for make-docfile to see. We cannot put this in the real DEFUN
2771 due to limits in the Unix cpp.
2773 DEFUN ("parse-partial-sexp", Ffoo, Sfoo, 2, 6, 0,
2774 "Parse Lisp syntax starting at FROM until TO; return status of parse at TO.\n\
2775 Parsing stops at TO or when certain criteria are met;\n\
2776 point is set to where parsing stops.\n\
2777 If fifth arg STATE is omitted or nil,\n\
2778 parsing assumes that FROM is the beginning of a function.\n\
2779 Value is a list of ten elements describing final state of parsing:\n\
2780 0. depth in parens.\n\
2781 1. character address of start of innermost containing list; nil if none.\n\
2782 2. character address of start of last complete sexp terminated.\n\
2783 3. non-nil if inside a string.\n\
2784 (it is the character that will terminate the string,\n\
2785 or t if the string should be terminated by a generic string delimiter.)\n\
2786 4. nil if outside a comment, t if inside a non-nestable comment, \n\
2787 else an integer (the current comment nesting).\n\
2788 5. t if following a quote character.\n\
2789 6. the minimum paren-depth encountered during this scan.\n\
2790 7. t if in a comment of style b; symbol `syntax-table' if the comment\n\
2791 should be terminated by a generic comment delimiter.\n\
2792 8. character address of start of comment or string; nil if not in one.\n\
2793 9. Intermediate data for continuation of parsing (subject to change).\n\
2794 If third arg TARGETDEPTH is non-nil, parsing stops if the depth\n\
2795 in parentheses becomes equal to TARGETDEPTH.\n\
2796 Fourth arg STOPBEFORE non-nil means stop when come to\n\
2797 any character that starts a sexp.\n\
2798 Fifth arg STATE is a nine-element list like what this function returns.\n\
2799 It is used to initialize the state of the parse. Elements number 1, 2, 6\n\
2800 and 8 are ignored; you can leave off element 8 (the last) entirely.\n\
2801 Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.\n\
2802 If it is symbol `syntax-table', stop after the start of a comment or a\n\
2803 string, or after end of a comment or a string.")
2804 (from, to, targetdepth, stopbefore, state, commentstop)
2807 DEFUN ("parse-partial-sexp", Fparse_partial_sexp
, Sparse_partial_sexp
, 2, 6, 0,
2808 0 /* See immediately above */)
2809 (from
, to
, targetdepth
, stopbefore
, oldstate
, commentstop
)
2810 Lisp_Object from
, to
, targetdepth
, stopbefore
, oldstate
, commentstop
;
2812 struct lisp_parse_state state
;
2815 if (!NILP (targetdepth
))
2817 CHECK_NUMBER (targetdepth
, 3);
2818 target
= XINT (targetdepth
);
2821 target
= -100000; /* We won't reach this depth */
2823 validate_region (&from
, &to
);
2824 scan_sexps_forward (&state
, XINT (from
), CHAR_TO_BYTE (XINT (from
)),
2826 target
, !NILP (stopbefore
), oldstate
,
2828 ? 0 : (EQ (commentstop
, Qsyntax_table
) ? -1 : 1)));
2830 SET_PT (state
.location
);
2832 return Fcons (make_number (state
.depth
),
2833 Fcons (state
.prevlevelstart
< 0 ? Qnil
: make_number (state
.prevlevelstart
),
2834 Fcons (state
.thislevelstart
< 0 ? Qnil
: make_number (state
.thislevelstart
),
2835 Fcons (state
.instring
>= 0
2836 ? (state
.instring
== ST_STRING_STYLE
2837 ? Qt
: make_number (state
.instring
)) : Qnil
,
2838 Fcons (state
.incomment
< 0 ? Qt
:
2839 (state
.incomment
== 0 ? Qnil
:
2840 make_number (state
.incomment
)),
2841 Fcons (state
.quoted
? Qt
: Qnil
,
2842 Fcons (make_number (state
.mindepth
),
2843 Fcons ((state
.comstyle
2844 ? (state
.comstyle
== ST_COMMENT_STYLE
2845 ? Qsyntax_table
: Qt
) :
2847 Fcons (((state
.incomment
2848 || (state
.instring
>= 0))
2849 ? make_number (state
.comstr_start
)
2851 Fcons (state
.levelstarts
, Qnil
))))))))));
2860 /* This has to be done here, before we call Fmake_char_table. */
2861 Qsyntax_table
= intern ("syntax-table");
2862 staticpro (&Qsyntax_table
);
2864 /* Intern this now in case it isn't already done.
2865 Setting this variable twice is harmless.
2866 But don't staticpro it here--that is done in alloc.c. */
2867 Qchar_table_extra_slots
= intern ("char-table-extra-slots");
2869 /* Create objects which can be shared among syntax tables. */
2870 Vsyntax_code_object
= Fmake_vector (make_number (13), Qnil
);
2871 for (i
= 0; i
< XVECTOR (Vsyntax_code_object
)->size
; i
++)
2872 XVECTOR (Vsyntax_code_object
)->contents
[i
]
2873 = Fcons (make_number (i
), Qnil
);
2875 /* Now we are ready to set up this property, so we can
2876 create syntax tables. */
2877 Fput (Qsyntax_table
, Qchar_table_extra_slots
, make_number (0));
2879 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Swhitespace
];
2881 Vstandard_syntax_table
= Fmake_char_table (Qsyntax_table
, temp
);
2883 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Sword
];
2884 for (i
= 'a'; i
<= 'z'; i
++)
2885 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
2886 for (i
= 'A'; i
<= 'Z'; i
++)
2887 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
2888 for (i
= '0'; i
<= '9'; i
++)
2889 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
2891 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '$', temp
);
2892 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '%', temp
);
2894 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '(',
2895 Fcons (make_number (Sopen
), make_number (')')));
2896 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ')',
2897 Fcons (make_number (Sclose
), make_number ('(')));
2898 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '[',
2899 Fcons (make_number (Sopen
), make_number (']')));
2900 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ']',
2901 Fcons (make_number (Sclose
), make_number ('[')));
2902 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '{',
2903 Fcons (make_number (Sopen
), make_number ('}')));
2904 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '}',
2905 Fcons (make_number (Sclose
), make_number ('{')));
2906 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '"',
2907 Fcons (make_number ((int) Sstring
), Qnil
));
2908 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\\',
2909 Fcons (make_number ((int) Sescape
), Qnil
));
2911 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Ssymbol
];
2912 for (i
= 0; i
< 10; i
++)
2914 c
= "_-+*/&|<>="[i
];
2915 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, c
, temp
);
2918 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Spunct
];
2919 for (i
= 0; i
< 12; i
++)
2921 c
= ".,;:?!#@~^'`"[i
];
2922 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, c
, temp
);
2925 /* All multibyte characters have syntax `word' by default. */
2926 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Sword
];
2927 for (i
= CHAR_TABLE_SINGLE_BYTE_SLOTS
; i
< CHAR_TABLE_ORDINARY_SLOTS
; i
++)
2928 XCHAR_TABLE (Vstandard_syntax_table
)->contents
[i
] = temp
;
2934 Qsyntax_table_p
= intern ("syntax-table-p");
2935 staticpro (&Qsyntax_table_p
);
2937 staticpro (&Vsyntax_code_object
);
2939 Qscan_error
= intern ("scan-error");
2940 staticpro (&Qscan_error
);
2941 Fput (Qscan_error
, Qerror_conditions
,
2942 Fcons (Qscan_error
, Fcons (Qerror
, Qnil
)));
2943 Fput (Qscan_error
, Qerror_message
,
2944 build_string ("Scan error"));
2946 DEFVAR_BOOL ("parse-sexp-ignore-comments", &parse_sexp_ignore_comments
,
2947 "Non-nil means `forward-sexp', etc., should treat comments as whitespace.");
2949 DEFVAR_BOOL ("parse-sexp-lookup-properties", &parse_sexp_lookup_properties
,
2950 "Non-nil means `forward-sexp', etc., grant `syntax-table' property.\n\
2951 The value of this property should be either a syntax table, or a cons\n\
2952 of the form (SYNTAXCODE . MATCHCHAR), SYNTAXCODE being the numeric\n\
2953 syntax code, MATCHCHAR being nil or the character to match (which is\n\
2954 relevant only for open/close type.");
2956 words_include_escapes
= 0;
2957 DEFVAR_BOOL ("words-include-escapes", &words_include_escapes
,
2958 "Non-nil means `forward-word', etc., should treat escape chars part of words.");
2960 DEFVAR_BOOL ("multibyte-syntax-as-symbol", &multibyte_syntax_as_symbol
,
2961 "Non-nil means `scan-sexps' treats all multibyte characters as symbol.");
2962 multibyte_syntax_as_symbol
= 0;
2964 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
2965 &open_paren_in_column_0_is_defun_start
,
2966 "Non-nil means an open paren in column 0 denotes the start of a defun.");
2967 open_paren_in_column_0_is_defun_start
= 1;
2969 defsubr (&Ssyntax_table_p
);
2970 defsubr (&Ssyntax_table
);
2971 defsubr (&Sstandard_syntax_table
);
2972 defsubr (&Scopy_syntax_table
);
2973 defsubr (&Sset_syntax_table
);
2974 defsubr (&Schar_syntax
);
2975 defsubr (&Smatching_paren
);
2976 defsubr (&Smodify_syntax_entry
);
2977 defsubr (&Sdescribe_syntax
);
2979 defsubr (&Sforward_word
);
2981 defsubr (&Sskip_chars_forward
);
2982 defsubr (&Sskip_chars_backward
);
2983 defsubr (&Sskip_syntax_forward
);
2984 defsubr (&Sskip_syntax_backward
);
2986 defsubr (&Sforward_comment
);
2987 defsubr (&Sscan_lists
);
2988 defsubr (&Sscan_sexps
);
2989 defsubr (&Sbackward_prefix_chars
);
2990 defsubr (&Sparse_partial_sexp
);