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, 2004 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. */
31 /* Make syntax table lookup grant data in gl_state. */
32 #define SYNTAX_ENTRY_VIA_PROPERTY
35 #include "intervals.h"
37 /* We use these constants in place for comment-style and
38 string-ender-char to distinguish comments/strings started by
39 comment_fence and string_fence codes. */
41 #define ST_COMMENT_STYLE (256 + 1)
42 #define ST_STRING_STYLE (256 + 2)
45 Lisp_Object Qsyntax_table_p
, Qsyntax_table
, Qscan_error
;
47 int words_include_escapes
;
48 int parse_sexp_lookup_properties
;
50 /* Nonzero means `scan-sexps' treat all multibyte characters as symbol. */
51 int multibyte_syntax_as_symbol
;
53 /* Used as a temporary in SYNTAX_ENTRY and other macros in syntax.h,
54 if not compiled with GCC. No need to mark it, since it is used
55 only very temporarily. */
56 Lisp_Object syntax_temp
;
58 /* Non-zero means an open parenthesis in column 0 is always considered
59 to be the start of a defun. Zero means an open parenthesis in
60 column 0 has no special meaning. */
62 int open_paren_in_column_0_is_defun_start
;
64 /* This is the internal form of the parse state used in parse-partial-sexp. */
66 struct lisp_parse_state
68 int depth
; /* Depth at end of parsing. */
69 int instring
; /* -1 if not within string, else desired terminator. */
70 int incomment
; /* -1 if in unnestable comment else comment nesting */
71 int comstyle
; /* comment style a=0, or b=1, or ST_COMMENT_STYLE. */
72 int quoted
; /* Nonzero if just after an escape char at end of parsing */
73 int thislevelstart
; /* Char number of most recent start-of-expression at current level */
74 int prevlevelstart
; /* Char number of start of containing expression */
75 int location
; /* Char number at which parsing stopped. */
76 int mindepth
; /* Minimum depth seen while scanning. */
77 int comstr_start
; /* Position just after last comment/string starter. */
78 Lisp_Object levelstarts
; /* Char numbers of starts-of-expression
79 of levels (starting from outermost). */
82 /* These variables are a cache for finding the start of a defun.
83 find_start_pos is the place for which the defun start was found.
84 find_start_value is the defun start position found for it.
85 find_start_value_byte is the corresponding byte position.
86 find_start_buffer is the buffer it was found in.
87 find_start_begv is the BEGV value when it was found.
88 find_start_modiff is the value of MODIFF when it was found. */
90 static int find_start_pos
;
91 static int find_start_value
;
92 static int find_start_value_byte
;
93 static struct buffer
*find_start_buffer
;
94 static int find_start_begv
;
95 static int find_start_modiff
;
98 static int find_defun_start
P_ ((int, int));
99 static int back_comment
P_ ((int, int, int, int, int, int *, int *));
100 static int char_quoted
P_ ((int, int));
101 static Lisp_Object skip_chars
P_ ((int, int, Lisp_Object
, Lisp_Object
, int));
102 static Lisp_Object scan_lists
P_ ((int, int, int, int));
103 static void scan_sexps_forward
P_ ((struct lisp_parse_state
*,
105 int, Lisp_Object
, int));
106 static int in_classes
P_ ((int, Lisp_Object
));
109 struct gl_state_s gl_state
; /* Global state of syntax parser. */
111 INTERVAL
interval_of ();
112 #define INTERVALS_AT_ONCE 10 /* 1 + max-number of intervals
113 to scan to property-change. */
115 /* Update gl_state to an appropriate interval which contains CHARPOS. The
116 sign of COUNT give the relative position of CHARPOS wrt the previously
117 valid interval. If INIT, only [be]_property fields of gl_state are
118 valid at start, the rest is filled basing on OBJECT.
120 `gl_state.*_i' are the intervals, and CHARPOS is further in the search
121 direction than the intervals - or in an interval. We update the
122 current syntax-table basing on the property of this interval, and
123 update the interval to start further than CHARPOS - or be
124 NULL_INTERVAL. We also update lim_property to be the next value of
125 charpos to call this subroutine again - or be before/after the
126 start/end of OBJECT. */
129 update_syntax_table (charpos
, count
, init
, object
)
130 int charpos
, count
, init
;
133 Lisp_Object tmp_table
;
134 int cnt
= 0, invalidate
= 1;
139 gl_state
.old_prop
= Qnil
;
140 gl_state
.start
= gl_state
.b_property
;
141 gl_state
.stop
= gl_state
.e_property
;
142 i
= interval_of (charpos
, object
);
143 gl_state
.backward_i
= gl_state
.forward_i
= i
;
145 if (NULL_INTERVAL_P (i
))
147 /* interval_of updates only ->position of the return value, so
148 update the parents manually to speed up update_interval. */
149 while (!NULL_PARENT (i
))
151 if (AM_RIGHT_CHILD (i
))
152 INTERVAL_PARENT (i
)->position
= i
->position
153 - LEFT_TOTAL_LENGTH (i
) + TOTAL_LENGTH (i
) /* right end */
154 - TOTAL_LENGTH (INTERVAL_PARENT (i
))
155 + LEFT_TOTAL_LENGTH (INTERVAL_PARENT (i
));
157 INTERVAL_PARENT (i
)->position
= i
->position
- LEFT_TOTAL_LENGTH (i
)
159 i
= INTERVAL_PARENT (i
);
161 i
= gl_state
.forward_i
;
162 gl_state
.b_property
= i
->position
- gl_state
.offset
;
163 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
166 oldi
= i
= count
> 0 ? gl_state
.forward_i
: gl_state
.backward_i
;
168 /* We are guaranteed to be called with CHARPOS either in i,
170 if (NULL_INTERVAL_P (i
))
171 error ("Error in syntax_table logic for to-the-end intervals");
172 else if (charpos
< i
->position
) /* Move left. */
175 error ("Error in syntax_table logic for intervals <-");
176 /* Update the interval. */
177 i
= update_interval (i
, charpos
);
178 if (INTERVAL_LAST_POS (i
) != gl_state
.b_property
)
181 gl_state
.forward_i
= i
;
182 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
185 else if (charpos
>= INTERVAL_LAST_POS (i
)) /* Move right. */
188 error ("Error in syntax_table logic for intervals ->");
189 /* Update the interval. */
190 i
= update_interval (i
, charpos
);
191 if (i
->position
!= gl_state
.e_property
)
194 gl_state
.backward_i
= i
;
195 gl_state
.b_property
= i
->position
- gl_state
.offset
;
200 tmp_table
= textget (i
->plist
, Qsyntax_table
);
203 invalidate
= !EQ (tmp_table
, gl_state
.old_prop
); /* Need to invalidate? */
205 if (invalidate
) /* Did not get to adjacent interval. */
206 { /* with the same table => */
207 /* invalidate the old range. */
210 gl_state
.backward_i
= i
;
211 gl_state
.b_property
= i
->position
- gl_state
.offset
;
215 gl_state
.forward_i
= i
;
216 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
220 if (!EQ (tmp_table
, gl_state
.old_prop
))
222 gl_state
.current_syntax_table
= tmp_table
;
223 gl_state
.old_prop
= tmp_table
;
224 if (EQ (Fsyntax_table_p (tmp_table
), Qt
))
226 gl_state
.use_global
= 0;
228 else if (CONSP (tmp_table
))
230 gl_state
.use_global
= 1;
231 gl_state
.global_code
= tmp_table
;
235 gl_state
.use_global
= 0;
236 gl_state
.current_syntax_table
= current_buffer
->syntax_table
;
240 while (!NULL_INTERVAL_P (i
))
242 if (cnt
&& !EQ (tmp_table
, textget (i
->plist
, Qsyntax_table
)))
246 gl_state
.e_property
= i
->position
- gl_state
.offset
;
247 gl_state
.forward_i
= i
;
251 gl_state
.b_property
= i
->position
+ LENGTH (i
) - gl_state
.offset
;
252 gl_state
.backward_i
= i
;
256 else if (cnt
== INTERVALS_AT_ONCE
)
260 gl_state
.e_property
= i
->position
+ LENGTH (i
) - gl_state
.offset
;
261 gl_state
.forward_i
= i
;
265 gl_state
.b_property
= i
->position
- gl_state
.offset
;
266 gl_state
.backward_i
= i
;
271 i
= count
> 0 ? next_interval (i
) : previous_interval (i
);
273 eassert (NULL_INTERVAL_P (i
)); /* This property goes to the end. */
275 gl_state
.e_property
= gl_state
.stop
;
277 gl_state
.b_property
= gl_state
.start
;
280 /* Returns TRUE if char at CHARPOS is quoted.
281 Global syntax-table data should be set up already to be good at CHARPOS
282 or after. On return global syntax data is good for lookup at CHARPOS. */
285 char_quoted (charpos
, bytepos
)
286 register int charpos
, bytepos
;
288 register enum syntaxcode code
;
289 register int beg
= BEGV
;
290 register int quoted
= 0;
293 DEC_BOTH (charpos
, bytepos
);
295 while (charpos
>= beg
)
299 UPDATE_SYNTAX_TABLE_BACKWARD (charpos
);
300 c
= FETCH_CHAR (bytepos
);
302 if (! (code
== Scharquote
|| code
== Sescape
))
305 DEC_BOTH (charpos
, bytepos
);
309 UPDATE_SYNTAX_TABLE (orig
);
313 /* Return the bytepos one character after BYTEPOS.
314 We assume that BYTEPOS is not at the end of the buffer. */
317 inc_bytepos (bytepos
)
320 if (NILP (current_buffer
->enable_multibyte_characters
))
327 /* Return the bytepos one character before BYTEPOS.
328 We assume that BYTEPOS is not at the start of the buffer. */
331 dec_bytepos (bytepos
)
334 if (NILP (current_buffer
->enable_multibyte_characters
))
341 /* Return a defun-start position before before POS and not too far before.
342 It should be the last one before POS, or nearly the last.
344 When open_paren_in_column_0_is_defun_start is nonzero,
345 only the beginning of the buffer is treated as a defun-start.
347 We record the information about where the scan started
348 and what its result was, so that another call in the same area
349 can return the same value very quickly.
351 There is no promise at which position the global syntax data is
352 valid on return from the subroutine, so the caller should explicitly
353 update the global data. */
356 find_defun_start (pos
, pos_byte
)
359 int opoint
= PT
, opoint_byte
= PT_BYTE
;
361 if (!open_paren_in_column_0_is_defun_start
)
363 find_start_value_byte
= BEGV_BYTE
;
367 /* Use previous finding, if it's valid and applies to this inquiry. */
368 if (current_buffer
== find_start_buffer
369 /* Reuse the defun-start even if POS is a little farther on.
370 POS might be in the next defun, but that's ok.
371 Our value may not be the best possible, but will still be usable. */
372 && pos
<= find_start_pos
+ 1000
373 && pos
>= find_start_value
374 && BEGV
== find_start_begv
375 && MODIFF
== find_start_modiff
)
376 return find_start_value
;
378 /* Back up to start of line. */
379 scan_newline (pos
, pos_byte
, BEGV
, BEGV_BYTE
, -1, 1);
381 /* We optimize syntax-table lookup for rare updates. Thus we accept
382 only those `^\s(' which are good in global _and_ text-property
384 gl_state
.current_syntax_table
= current_buffer
->syntax_table
;
385 gl_state
.use_global
= 0;
390 /* Open-paren at start of line means we may have found our
392 c
= FETCH_CHAR (PT_BYTE
);
393 if (SYNTAX (c
) == Sopen
)
395 SETUP_SYNTAX_TABLE (PT
+ 1, -1); /* Try again... */
396 c
= FETCH_CHAR (PT_BYTE
);
397 if (SYNTAX (c
) == Sopen
)
399 /* Now fallback to the default value. */
400 gl_state
.current_syntax_table
= current_buffer
->syntax_table
;
401 gl_state
.use_global
= 0;
403 /* Move to beg of previous line. */
404 scan_newline (PT
, PT_BYTE
, BEGV
, BEGV_BYTE
, -2, 1);
407 /* Record what we found, for the next try. */
408 find_start_value
= PT
;
409 find_start_value_byte
= PT_BYTE
;
410 find_start_buffer
= current_buffer
;
411 find_start_modiff
= MODIFF
;
412 find_start_begv
= BEGV
;
413 find_start_pos
= pos
;
415 TEMP_SET_PT_BOTH (opoint
, opoint_byte
);
417 return find_start_value
;
420 /* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
423 prev_char_comend_first (pos
, pos_byte
)
428 DEC_BOTH (pos
, pos_byte
);
429 UPDATE_SYNTAX_TABLE_BACKWARD (pos
);
430 c
= FETCH_CHAR (pos_byte
);
431 val
= SYNTAX_COMEND_FIRST (c
);
432 UPDATE_SYNTAX_TABLE_FORWARD (pos
+ 1);
436 /* Return the SYNTAX_COMSTART_FIRST of the character before POS, POS_BYTE. */
439 * prev_char_comstart_first (pos, pos_byte)
444 * DEC_BOTH (pos, pos_byte);
445 * UPDATE_SYNTAX_TABLE_BACKWARD (pos);
446 * c = FETCH_CHAR (pos_byte);
447 * val = SYNTAX_COMSTART_FIRST (c);
448 * UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
452 /* Checks whether charpos FROM is at the end of a comment.
453 FROM_BYTE is the bytepos corresponding to FROM.
454 Do not move back before STOP.
456 Return a positive value if we find a comment ending at FROM/FROM_BYTE;
459 If successful, store the charpos of the comment's beginning
460 into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR.
462 Global syntax data remains valid for backward search starting at
463 the returned value (or at FROM, if the search was not successful). */
466 back_comment (from
, from_byte
, stop
, comnested
, comstyle
, charpos_ptr
, bytepos_ptr
)
467 int from
, from_byte
, stop
;
468 int comnested
, comstyle
;
469 int *charpos_ptr
, *bytepos_ptr
;
471 /* Look back, counting the parity of string-quotes,
472 and recording the comment-starters seen.
473 When we reach a safe place, assume that's not in a string;
474 then step the main scan to the earliest comment-starter seen
475 an even number of string quotes away from the safe place.
477 OFROM[I] is position of the earliest comment-starter seen
478 which is I+2X quotes from the comment-end.
479 PARITY is current parity of quotes from the comment end. */
480 int string_style
= -1; /* Presumed outside of any string. */
481 int string_lossage
= 0;
482 /* Not a real lossage: indicates that we have passed a matching comment
483 starter plus a non-matching comment-ender, meaning that any matching
484 comment-starter we might see later could be a false positive (hidden
485 inside another comment).
486 Test case: { a (* b } c (* d *) */
487 int comment_lossage
= 0;
488 int comment_end
= from
;
489 int comment_end_byte
= from_byte
;
490 int comstart_pos
= 0;
492 /* Place where the containing defun starts,
493 or 0 if we didn't come across it yet. */
495 int defun_start_byte
= 0;
496 register enum syntaxcode code
;
497 int nesting
= 1; /* current comment nesting */
501 /* FIXME: A }} comment-ender style leads to incorrect behavior
502 in the case of {{ c }}} because we ignore the last two chars which are
503 assumed to be comment-enders although they aren't. */
505 /* At beginning of range to scan, we're outside of strings;
506 that determines quote parity to the comment-end. */
509 int temp_byte
, prev_syntax
;
510 int com2start
, com2end
;
512 /* Move back and examine a character. */
513 DEC_BOTH (from
, from_byte
);
514 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
516 prev_syntax
= syntax
;
517 c
= FETCH_CHAR (from_byte
);
518 syntax
= SYNTAX_WITH_FLAGS (c
);
521 /* Check for 2-char comment markers. */
522 com2start
= (SYNTAX_FLAGS_COMSTART_FIRST (syntax
)
523 && SYNTAX_FLAGS_COMSTART_SECOND (prev_syntax
)
524 && comstyle
== SYNTAX_FLAGS_COMMENT_STYLE (prev_syntax
)
525 && (SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax
)
526 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
)) == comnested
);
527 com2end
= (SYNTAX_FLAGS_COMEND_FIRST (syntax
)
528 && SYNTAX_FLAGS_COMEND_SECOND (prev_syntax
));
530 /* Nasty cases with overlapping 2-char comment markers:
531 - snmp-mode: -- c -- foo -- c --
539 /* If a 2-char comment sequence partly overlaps with another,
540 we don't try to be clever. */
541 if (from
> stop
&& (com2end
|| com2start
))
543 int next
= from
, next_byte
= from_byte
, next_c
, next_syntax
;
544 DEC_BOTH (next
, next_byte
);
545 UPDATE_SYNTAX_TABLE_BACKWARD (next
);
546 next_c
= FETCH_CHAR (next_byte
);
547 next_syntax
= SYNTAX_WITH_FLAGS (next_c
);
548 if (((com2start
|| comnested
)
549 && SYNTAX_FLAGS_COMEND_SECOND (syntax
)
550 && SYNTAX_FLAGS_COMEND_FIRST (next_syntax
))
551 || ((com2end
|| comnested
)
552 && SYNTAX_FLAGS_COMSTART_SECOND (syntax
)
553 && comstyle
== SYNTAX_FLAGS_COMMENT_STYLE (syntax
)
554 && SYNTAX_FLAGS_COMSTART_FIRST (next_syntax
)))
556 /* UPDATE_SYNTAX_TABLE_FORWARD (next + 1); */
559 if (com2start
&& comstart_pos
== 0)
560 /* We're looking at a comment starter. But it might be a comment
561 ender as well (see snmp-mode). The first time we see one, we
562 need to consider it as a comment starter,
563 and the subsequent times as a comment ender. */
566 /* Turn a 2-char comment sequences into the appropriate syntax. */
571 /* Ignore comment starters of a different style. */
572 else if (code
== Scomment
573 && (comstyle
!= SYNTAX_FLAGS_COMMENT_STYLE (syntax
)
574 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
) != comnested
))
577 /* Ignore escaped characters, except comment-enders. */
578 if (code
!= Sendcomment
&& char_quoted (from
, from_byte
))
585 c
= (code
== Sstring_fence
? ST_STRING_STYLE
: ST_COMMENT_STYLE
);
587 /* Track parity of quotes. */
588 if (string_style
== -1)
589 /* Entering a string. */
591 else if (string_style
== c
)
592 /* Leaving the string. */
595 /* If we have two kinds of string delimiters.
596 There's no way to grok this scanning backwards. */
601 /* We've already checked that it is the relevant comstyle. */
602 if (string_style
!= -1 || comment_lossage
|| string_lossage
)
603 /* There are odd string quotes involved, so let's be careful.
604 Test case in Pascal: " { " a { " } */
609 /* Record best comment-starter so far. */
611 comstart_byte
= from_byte
;
613 else if (--nesting
<= 0)
614 /* nested comments have to be balanced, so we don't need to
615 keep looking for earlier ones. We use here the same (slightly
616 incorrect) reasoning as below: since it is followed by uniform
617 paired string quotes, this comment-start has to be outside of
618 strings, else the comment-end itself would be inside a string. */
623 if (SYNTAX_FLAGS_COMMENT_STYLE (syntax
) == comstyle
624 && ((com2end
&& SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax
))
625 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
)) == comnested
)
626 /* This is the same style of comment ender as ours. */
631 /* Anything before that can't count because it would match
632 this comment-ender rather than ours. */
633 from
= stop
; /* Break out of the loop. */
635 else if (comstart_pos
!= 0 || c
!= '\n')
636 /* We're mixing comment styles here, so we'd better be careful.
637 The (comstart_pos != 0 || c != '\n') check is not quite correct
638 (we should just always set comment_lossage), but removing it
639 would imply that any multiline comment in C would go through
640 lossage, which seems overkill.
641 The failure should only happen in the rare cases such as
647 /* Assume a defun-start point is outside of strings. */
648 if (open_paren_in_column_0_is_defun_start
650 || (temp_byte
= dec_bytepos (from_byte
),
651 FETCH_CHAR (temp_byte
) == '\n')))
654 defun_start_byte
= from_byte
;
655 from
= stop
; /* Break out of the loop. */
664 if (comstart_pos
== 0)
667 from_byte
= comment_end_byte
;
668 UPDATE_SYNTAX_TABLE_FORWARD (comment_end
- 1);
670 /* If comstart_pos is set and we get here (ie. didn't jump to `lossage'
671 or `done'), then we've found the beginning of the non-nested comment. */
672 else if (1) /* !comnested */
675 from_byte
= comstart_byte
;
676 UPDATE_SYNTAX_TABLE_FORWARD (from
- 1);
680 struct lisp_parse_state state
;
682 /* We had two kinds of string delimiters mixed up
683 together. Decode this going forwards.
684 Scan fwd from a known safe place (beginning-of-defun)
685 to the one in question; this records where we
686 last passed a comment starter. */
687 /* If we did not already find the defun start, find it now. */
688 if (defun_start
== 0)
690 defun_start
= find_defun_start (comment_end
, comment_end_byte
);
691 defun_start_byte
= find_start_value_byte
;
695 scan_sexps_forward (&state
,
696 defun_start
, defun_start_byte
,
697 comment_end
, -10000, 0, Qnil
, 0);
698 defun_start
= comment_end
;
699 if (state
.incomment
== (comnested
? 1 : -1)
700 && state
.comstyle
== comstyle
)
701 from
= state
.comstr_start
;
706 /* If comment_end is inside some other comment, maybe ours
707 is nested, so we need to try again from within the
708 surrounding comment. Example: { a (* " *) */
710 /* FIXME: We should advance by one or two chars. */
711 defun_start
= state
.comstr_start
+ 2;
712 defun_start_byte
= CHAR_TO_BYTE (defun_start
);
715 } while (defun_start
< comment_end
);
717 from_byte
= CHAR_TO_BYTE (from
);
718 UPDATE_SYNTAX_TABLE_FORWARD (from
- 1);
723 *bytepos_ptr
= from_byte
;
725 return (from
== comment_end
) ? -1 : from
;
728 DEFUN ("syntax-table-p", Fsyntax_table_p
, Ssyntax_table_p
, 1, 1, 0,
729 doc
: /* Return t if OBJECT is a syntax table.
730 Currently, any char-table counts as a syntax table. */)
734 if (CHAR_TABLE_P (object
)
735 && EQ (XCHAR_TABLE (object
)->purpose
, Qsyntax_table
))
741 check_syntax_table (obj
)
744 if (!(CHAR_TABLE_P (obj
)
745 && EQ (XCHAR_TABLE (obj
)->purpose
, Qsyntax_table
)))
746 wrong_type_argument (Qsyntax_table_p
, obj
);
749 DEFUN ("syntax-table", Fsyntax_table
, Ssyntax_table
, 0, 0, 0,
750 doc
: /* Return the current syntax table.
751 This is the one specified by the current buffer. */)
754 return current_buffer
->syntax_table
;
757 DEFUN ("standard-syntax-table", Fstandard_syntax_table
,
758 Sstandard_syntax_table
, 0, 0, 0,
759 doc
: /* Return the standard syntax table.
760 This is the one used for new buffers. */)
763 return Vstandard_syntax_table
;
766 DEFUN ("copy-syntax-table", Fcopy_syntax_table
, Scopy_syntax_table
, 0, 1, 0,
767 doc
: /* Construct a new syntax table and return it.
768 It is a copy of the TABLE, which defaults to the standard syntax table. */)
775 check_syntax_table (table
);
777 table
= Vstandard_syntax_table
;
779 copy
= Fcopy_sequence (table
);
781 /* Only the standard syntax table should have a default element.
782 Other syntax tables should inherit from parents instead. */
783 XCHAR_TABLE (copy
)->defalt
= Qnil
;
785 /* Copied syntax tables should all have parents.
786 If we copied one with no parent, such as the standard syntax table,
787 use the standard syntax table as the copy's parent. */
788 if (NILP (XCHAR_TABLE (copy
)->parent
))
789 Fset_char_table_parent (copy
, Vstandard_syntax_table
);
793 DEFUN ("set-syntax-table", Fset_syntax_table
, Sset_syntax_table
, 1, 1, 0,
794 doc
: /* Select a new syntax table for the current buffer.
795 One argument, a syntax table. */)
800 check_syntax_table (table
);
801 current_buffer
->syntax_table
= table
;
802 /* Indicate that this buffer now has a specified syntax table. */
803 idx
= PER_BUFFER_VAR_IDX (syntax_table
);
804 SET_PER_BUFFER_VALUE_P (current_buffer
, idx
, 1);
808 /* Convert a letter which signifies a syntax code
809 into the code it signifies.
810 This is used by modify-syntax-entry, and other things. */
812 unsigned char syntax_spec_code
[0400] =
813 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
814 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
815 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
816 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
817 (char) Swhitespace
, (char) Scomment_fence
, (char) Sstring
, 0377,
818 (char) Smath
, 0377, 0377, (char) Squote
,
819 (char) Sopen
, (char) Sclose
, 0377, 0377,
820 0377, (char) Swhitespace
, (char) Spunct
, (char) Scharquote
,
821 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
822 0377, 0377, 0377, 0377,
823 (char) Scomment
, 0377, (char) Sendcomment
, 0377,
824 (char) Sinherit
, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
825 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
826 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword
,
827 0377, 0377, 0377, 0377, (char) Sescape
, 0377, 0377, (char) Ssymbol
,
828 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
829 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
830 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword
,
831 0377, 0377, 0377, 0377, (char) Sstring_fence
, 0377, 0377, 0377
834 /* Indexed by syntax code, give the letter that describes it. */
836 char syntax_code_spec
[16] =
838 ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@',
842 /* Indexed by syntax code, give the object (cons of syntax code and
843 nil) to be stored in syntax table. Since these objects can be
844 shared among syntax tables, we generate them in advance. By
845 sharing objects, the function `describe-syntax' can give a more
847 static Lisp_Object Vsyntax_code_object
;
850 /* Look up the value for CHARACTER in syntax table TABLE's parent
851 and its parents. SYNTAX_ENTRY calls this, when TABLE itself has nil
852 for CHARACTER. It's actually used only when not compiled with GCC. */
855 syntax_parent_lookup (table
, character
)
863 table
= XCHAR_TABLE (table
)->parent
;
867 value
= XCHAR_TABLE (table
)->contents
[character
];
873 DEFUN ("char-syntax", Fchar_syntax
, Schar_syntax
, 1, 1, 0,
874 doc
: /* Return the syntax code of CHARACTER, described by a character.
875 For example, if CHARACTER is a word constituent,
876 the character `w' is returned.
877 The characters that correspond to various syntax codes
878 are listed in the documentation of `modify-syntax-entry'. */)
880 Lisp_Object character
;
883 gl_state
.current_syntax_table
= current_buffer
->syntax_table
;
885 gl_state
.use_global
= 0;
886 CHECK_NUMBER (character
);
887 char_int
= XINT (character
);
888 return make_number (syntax_code_spec
[(int) SYNTAX (char_int
)]);
891 DEFUN ("matching-paren", Fmatching_paren
, Smatching_paren
, 1, 1, 0,
892 doc
: /* Return the matching parenthesis of CHARACTER, or nil if none. */)
894 Lisp_Object character
;
897 gl_state
.current_syntax_table
= current_buffer
->syntax_table
;
898 gl_state
.use_global
= 0;
899 CHECK_NUMBER (character
);
900 char_int
= XINT (character
);
901 code
= SYNTAX (char_int
);
902 if (code
== Sopen
|| code
== Sclose
)
903 return SYNTAX_MATCH (char_int
);
907 DEFUN ("string-to-syntax", Fstring_to_syntax
, Sstring_to_syntax
, 1, 1, 0,
908 doc
: /* Convert a syntax specification STRING into syntax cell form.
909 STRING should be a string as it is allowed as argument of
910 `modify-syntax-entry'. Value is the equivalent cons cell
911 (CODE . MATCHING-CHAR) that can be used as value of a `syntax-table'
916 register const unsigned char *p
;
917 register enum syntaxcode code
;
921 CHECK_STRING (string
);
924 code
= (enum syntaxcode
) syntax_spec_code
[*p
++];
925 if (((int) code
& 0377) == 0377)
926 error ("invalid syntax description letter: %c", p
[-1]);
928 if (code
== Sinherit
)
934 int character
= (STRING_CHAR_AND_LENGTH
935 (p
, SBYTES (string
) - 1, len
));
936 XSETINT (match
, character
);
937 if (XFASTINT (match
) == ' ')
977 if (val
< XVECTOR (Vsyntax_code_object
)->size
&& NILP (match
))
978 return XVECTOR (Vsyntax_code_object
)->contents
[val
];
980 /* Since we can't use a shared object, let's make a new one. */
981 return Fcons (make_number (val
), match
);
984 /* I really don't know why this is interactive
985 help-form should at least be made useful whilst reading the second arg. */
986 DEFUN ("modify-syntax-entry", Fmodify_syntax_entry
, Smodify_syntax_entry
, 2, 3,
987 "cSet syntax for character: \nsSet syntax for %s to: ",
988 doc
: /* Set syntax for character CHAR according to string NEWENTRY.
989 The syntax is changed only for table SYNTAX-TABLE, which defaults to
990 the current buffer's syntax table.
991 The first character of NEWENTRY should be one of the following:
992 Space or - whitespace syntax. w word constituent.
993 _ symbol constituent. . punctuation.
994 ( open-parenthesis. ) close-parenthesis.
995 " string quote. \\ escape.
996 $ paired delimiter. ' expression quote or prefix operator.
997 < comment starter. > comment ender.
998 / character-quote. @ inherit from `standard-syntax-table'.
999 | generic string fence. ! generic comment fence.
1001 Only single-character comment start and end sequences are represented thus.
1002 Two-character sequences are represented as described below.
1003 The second character of NEWENTRY is the matching parenthesis,
1004 used only if the first character is `(' or `)'.
1005 Any additional characters are flags.
1006 Defined flags are the characters 1, 2, 3, 4, b, p, and n.
1007 1 means CHAR is the start of a two-char comment start sequence.
1008 2 means CHAR is the second character of such a sequence.
1009 3 means CHAR is the start of a two-char comment end sequence.
1010 4 means CHAR is the second character of such a sequence.
1012 There can be up to two orthogonal comment sequences. This is to support
1013 language modes such as C++. By default, all comment sequences are of style
1014 a, but you can set the comment sequence style to b (on the second character
1015 of a comment-start, or the first character of a comment-end sequence) using
1017 b means CHAR is part of comment sequence b.
1018 n means CHAR is part of a nestable comment sequence.
1020 p means CHAR is a prefix character for `backward-prefix-chars';
1021 such characters are treated as whitespace when they occur
1022 between expressions.
1023 usage: (modify-syntax-entry CHAR NEWENTRY &optional SYNTAX-TABLE) */)
1024 (c
, newentry
, syntax_table
)
1025 Lisp_Object c
, newentry
, syntax_table
;
1029 if (NILP (syntax_table
))
1030 syntax_table
= current_buffer
->syntax_table
;
1032 check_syntax_table (syntax_table
);
1034 SET_RAW_SYNTAX_ENTRY (syntax_table
, XINT (c
), Fstring_to_syntax (newentry
));
1038 /* Dump syntax table to buffer in human-readable format */
1040 DEFUN ("internal-describe-syntax-value", Finternal_describe_syntax_value
,
1041 Sinternal_describe_syntax_value
, 1, 1, 0,
1042 doc
: /* Insert a description of the internal syntax description SYNTAX at point. */)
1046 register enum syntaxcode code
;
1047 char desc
, start1
, start2
, end1
, end2
, prefix
, comstyle
, comnested
;
1049 Lisp_Object first
, match_lisp
, value
= syntax
;
1053 insert_string ("default");
1057 if (CHAR_TABLE_P (value
))
1059 insert_string ("deeper char-table ...");
1065 insert_string ("invalid");
1069 first
= XCAR (value
);
1070 match_lisp
= XCDR (value
);
1072 if (!INTEGERP (first
) || !(NILP (match_lisp
) || INTEGERP (match_lisp
)))
1074 insert_string ("invalid");
1078 code
= (enum syntaxcode
) (XINT (first
) & 0377);
1079 start1
= (XINT (first
) >> 16) & 1;
1080 start2
= (XINT (first
) >> 17) & 1;
1081 end1
= (XINT (first
) >> 18) & 1;
1082 end2
= (XINT (first
) >> 19) & 1;
1083 prefix
= (XINT (first
) >> 20) & 1;
1084 comstyle
= (XINT (first
) >> 21) & 1;
1085 comnested
= (XINT (first
) >> 22) & 1;
1087 if ((int) code
< 0 || (int) code
>= (int) Smax
)
1089 insert_string ("invalid");
1092 desc
= syntax_code_spec
[(int) code
];
1094 str
[0] = desc
, str
[1] = 0;
1097 if (NILP (match_lisp
))
1100 insert_char (XINT (match_lisp
));
1119 insert_string ("\twhich means: ");
1121 switch (SWITCH_ENUM_CAST (code
))
1124 insert_string ("whitespace"); break;
1126 insert_string ("punctuation"); break;
1128 insert_string ("word"); break;
1130 insert_string ("symbol"); break;
1132 insert_string ("open"); break;
1134 insert_string ("close"); break;
1136 insert_string ("prefix"); break;
1138 insert_string ("string"); break;
1140 insert_string ("math"); break;
1142 insert_string ("escape"); break;
1144 insert_string ("charquote"); break;
1146 insert_string ("comment"); break;
1148 insert_string ("endcomment"); break;
1150 insert_string ("inherit"); break;
1151 case Scomment_fence
:
1152 insert_string ("comment fence"); break;
1154 insert_string ("string fence"); break;
1156 insert_string ("invalid");
1160 if (!NILP (match_lisp
))
1162 insert_string (", matches ");
1163 insert_char (XINT (match_lisp
));
1167 insert_string (",\n\t is the first character of a comment-start sequence");
1169 insert_string (",\n\t is the second character of a comment-start sequence");
1172 insert_string (",\n\t is the first character of a comment-end sequence");
1174 insert_string (",\n\t is the second character of a comment-end sequence");
1176 insert_string (" (comment style b)");
1178 insert_string (" (nestable)");
1181 insert_string (",\n\t is a prefix character for `backward-prefix-chars'");
1186 int parse_sexp_ignore_comments
;
1188 /* Return the position across COUNT words from FROM.
1189 If that many words cannot be found before the end of the buffer, return 0.
1190 COUNT negative means scan backward and stop at word beginning. */
1193 scan_words (from
, count
)
1194 register int from
, count
;
1196 register int beg
= BEGV
;
1197 register int end
= ZV
;
1198 register int from_byte
= CHAR_TO_BYTE (from
);
1199 register enum syntaxcode code
;
1205 SETUP_SYNTAX_TABLE (from
, count
);
1216 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1217 ch0
= FETCH_CHAR (from_byte
);
1218 code
= SYNTAX (ch0
);
1219 INC_BOTH (from
, from_byte
);
1220 if (words_include_escapes
1221 && (code
== Sescape
|| code
== Scharquote
))
1226 /* Now CH0 is a character which begins a word and FROM is the
1227 position of the next character. */
1230 if (from
== end
) break;
1231 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1232 ch1
= FETCH_CHAR (from_byte
);
1233 code
= SYNTAX (ch1
);
1234 if (!(words_include_escapes
1235 && (code
== Sescape
|| code
== Scharquote
)))
1236 if (code
!= Sword
|| WORD_BOUNDARY_P (ch0
, ch1
))
1238 INC_BOTH (from
, from_byte
);
1252 DEC_BOTH (from
, from_byte
);
1253 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
1254 ch1
= FETCH_CHAR (from_byte
);
1255 code
= SYNTAX (ch1
);
1256 if (words_include_escapes
1257 && (code
== Sescape
|| code
== Scharquote
))
1262 /* Now CH1 is a character which ends a word and FROM is the
1270 temp_byte
= dec_bytepos (from_byte
);
1271 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
1272 ch0
= FETCH_CHAR (temp_byte
);
1273 code
= SYNTAX (ch0
);
1274 if (!(words_include_escapes
1275 && (code
== Sescape
|| code
== Scharquote
)))
1276 if (code
!= Sword
|| WORD_BOUNDARY_P (ch0
, ch1
))
1278 DEC_BOTH (from
, from_byte
);
1289 DEFUN ("forward-word", Fforward_word
, Sforward_word
, 0, 1, "p",
1290 doc
: /* Move point forward ARG words (backward if ARG is negative).
1292 If an edge of the buffer or a field boundary is reached, point is left there
1293 and the function returns nil. Field boundaries are not noticed if
1294 `inhibit-field-text-motion' is non-nil. */)
1301 XSETFASTINT (arg
, 1);
1305 val
= orig_val
= scan_words (PT
, XINT (arg
));
1307 val
= XINT (arg
) > 0 ? ZV
: BEGV
;
1309 /* Avoid jumping out of an input field. */
1310 val
= XFASTINT (Fconstrain_to_field (make_number (val
), make_number (PT
),
1314 return val
== orig_val
? Qt
: Qnil
;
1317 Lisp_Object
skip_chars ();
1319 DEFUN ("skip-chars-forward", Fskip_chars_forward
, Sskip_chars_forward
, 1, 2, 0,
1320 doc
: /* Move point forward, stopping before a char not in STRING, or at pos LIM.
1321 STRING is like the inside of a `[...]' in a regular expression
1322 except that `]' is never special and `\\' quotes `^', `-' or `\\'
1323 (but not as the end of a range; quoting is never needed there).
1324 Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
1325 With arg "^a-zA-Z", skips nonletters stopping before first letter.
1326 Char classes, e.g. `[:alpha:]', are supported.
1328 Returns the distance traveled, either zero or positive. */)
1330 Lisp_Object string
, lim
;
1332 return skip_chars (1, 0, string
, lim
, 1);
1335 DEFUN ("skip-chars-backward", Fskip_chars_backward
, Sskip_chars_backward
, 1, 2, 0,
1336 doc
: /* Move point backward, stopping after a char not in STRING, or at pos LIM.
1337 See `skip-chars-forward' for details.
1338 Returns the distance traveled, either zero or negative. */)
1340 Lisp_Object string
, lim
;
1342 return skip_chars (0, 0, string
, lim
, 1);
1345 DEFUN ("skip-syntax-forward", Fskip_syntax_forward
, Sskip_syntax_forward
, 1, 2, 0,
1346 doc
: /* Move point forward across chars in specified syntax classes.
1347 SYNTAX is a string of syntax code characters.
1348 Stop before a char whose syntax is not in SYNTAX, or at position LIM.
1349 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1350 This function returns the distance traveled, either zero or positive. */)
1352 Lisp_Object syntax
, lim
;
1354 return skip_chars (1, 1, syntax
, lim
, 0);
1357 DEFUN ("skip-syntax-backward", Fskip_syntax_backward
, Sskip_syntax_backward
, 1, 2, 0,
1358 doc
: /* Move point backward across chars in specified syntax classes.
1359 SYNTAX is a string of syntax code characters.
1360 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.
1361 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1362 This function returns the distance traveled, either zero or negative. */)
1364 Lisp_Object syntax
, lim
;
1366 return skip_chars (0, 1, syntax
, lim
, 0);
1370 skip_chars (forwardp
, syntaxp
, string
, lim
, handle_iso_classes
)
1371 int forwardp
, syntaxp
;
1372 Lisp_Object string
, lim
;
1373 int handle_iso_classes
;
1375 register unsigned int c
;
1376 unsigned char fastmap
[0400];
1377 /* If SYNTAXP is 0, STRING may contain multi-byte form of characters
1378 of which codes don't fit in FASTMAP. In that case, set the
1379 ranges of characters in CHAR_RANGES. */
1381 int n_char_ranges
= 0;
1383 register int i
, i_byte
;
1384 int multibyte
= !NILP (current_buffer
->enable_multibyte_characters
);
1385 int string_multibyte
;
1387 const unsigned char *str
;
1389 Lisp_Object iso_classes
;
1391 CHECK_STRING (string
);
1392 char_ranges
= (int *) alloca (SCHARS (string
) * (sizeof (int)) * 2);
1393 string_multibyte
= STRING_MULTIBYTE (string
);
1394 str
= SDATA (string
);
1395 size_byte
= SBYTES (string
);
1398 /* Adjust the multibyteness of the string to that of the buffer. */
1399 if (multibyte
!= string_multibyte
)
1404 nbytes
= count_size_as_multibyte (SDATA (string
),
1407 nbytes
= SCHARS (string
);
1408 if (nbytes
!= size_byte
)
1410 unsigned char *tmp
= (unsigned char *) alloca (nbytes
);
1411 copy_text (SDATA (string
), tmp
, size_byte
,
1412 string_multibyte
, multibyte
);
1419 XSETINT (lim
, forwardp
? ZV
: BEGV
);
1421 CHECK_NUMBER_COERCE_MARKER (lim
);
1423 /* In any case, don't allow scan outside bounds of buffer. */
1424 if (XINT (lim
) > ZV
)
1425 XSETFASTINT (lim
, ZV
);
1426 if (XINT (lim
) < BEGV
)
1427 XSETFASTINT (lim
, BEGV
);
1429 bzero (fastmap
, sizeof fastmap
);
1433 if (i_byte
< size_byte
1434 && SREF (string
, 0) == '^')
1436 negate
= 1; i_byte
++;
1439 /* Find the characters specified and set their elements of fastmap.
1440 If syntaxp, each character counts as itself.
1441 Otherwise, handle backslashes and ranges specially. */
1443 while (i_byte
< size_byte
)
1445 c
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, size_byte
- i_byte
, len
);
1449 fastmap
[syntax_spec_code
[c
& 0377]] = 1;
1452 if (handle_iso_classes
&& c
== '['
1453 && i_byte
< size_byte
1454 && STRING_CHAR (str
+ i_byte
, size_byte
- i_byte
) == ':')
1456 const unsigned char *class_beg
= str
+ i_byte
+ 1;
1457 const unsigned char *class_end
= class_beg
;
1458 const unsigned char *class_limit
= str
+ size_byte
- 2;
1459 /* Leave room for the null. */
1460 unsigned char class_name
[CHAR_CLASS_MAX_LENGTH
+ 1];
1463 if (class_limit
- class_beg
> CHAR_CLASS_MAX_LENGTH
)
1464 class_limit
= class_beg
+ CHAR_CLASS_MAX_LENGTH
;
1466 while (class_end
< class_limit
1467 && *class_end
>= 'a' && *class_end
<= 'z')
1470 if (class_end
== class_beg
1471 || *class_end
!= ':' || class_end
[1] != ']')
1472 goto not_a_class_name
;
1474 bcopy (class_beg
, class_name
, class_end
- class_beg
);
1475 class_name
[class_end
- class_beg
] = 0;
1477 cc
= re_wctype (class_name
);
1479 error ("Invalid ISO C character class");
1481 iso_classes
= Fcons (make_number (cc
), iso_classes
);
1483 i_byte
= class_end
+ 2 - str
;
1490 if (i_byte
== size_byte
)
1493 c
= STRING_CHAR_AND_LENGTH (str
+ i_byte
,
1494 size_byte
- i_byte
, len
);
1497 /* Treat `-' as range character only if another character
1499 if (i_byte
+ 1 < size_byte
1500 && str
[i_byte
] == '-')
1504 /* Skip over the dash. */
1507 /* Get the end of the range. */
1508 c2
= STRING_CHAR_AND_LENGTH (str
+ i_byte
,
1509 size_byte
- i_byte
, len
);
1512 if (SINGLE_BYTE_CHAR_P (c
))
1514 if (! SINGLE_BYTE_CHAR_P (c2
))
1516 /* Handle a range starting with a character of
1517 less than 256, and ending with a character of
1518 not less than 256. Split that into two
1519 ranges, the low one ending at 0377, and the
1520 high one starting at the smallest character
1521 in the charset of C2 and ending at C2. */
1522 int charset
= CHAR_CHARSET (c2
);
1523 int c1
= MAKE_CHAR (charset
, 0, 0);
1525 char_ranges
[n_char_ranges
++] = c1
;
1526 char_ranges
[n_char_ranges
++] = c2
;
1535 else if (c
<= c2
) /* Both C and C2 are multibyte char. */
1537 char_ranges
[n_char_ranges
++] = c
;
1538 char_ranges
[n_char_ranges
++] = c2
;
1543 if (SINGLE_BYTE_CHAR_P (c
))
1547 char_ranges
[n_char_ranges
++] = c
;
1548 char_ranges
[n_char_ranges
++] = c
;
1554 /* If ^ was the first character, complement the fastmap. */
1556 for (i
= 0; i
< sizeof fastmap
; i
++)
1560 int start_point
= PT
;
1562 int pos_byte
= PT_BYTE
;
1563 unsigned char *p
= PT_ADDR
, *endp
, *stop
;
1567 endp
= (XINT (lim
) == GPT
) ? GPT_ADDR
: CHAR_POS_ADDR (XINT (lim
));
1568 stop
= (pos
< GPT
&& GPT
< XINT (lim
)) ? GPT_ADDR
: endp
;
1572 endp
= CHAR_POS_ADDR (XINT (lim
));
1573 stop
= (pos
>= GPT
&& GPT
> XINT (lim
)) ? GAP_END_ADDR
: endp
;
1579 SETUP_SYNTAX_TABLE (pos
, forwardp
? 1 : -1);
1594 c
= STRING_CHAR_AND_LENGTH (p
, MAX_MULTIBYTE_LENGTH
, nbytes
);
1595 if (! fastmap
[(int) SYNTAX (c
)])
1597 p
+= nbytes
, pos
++, pos_byte
+= nbytes
;
1598 UPDATE_SYNTAX_TABLE_FORWARD (pos
);
1610 if (! fastmap
[(int) SYNTAX (*p
)])
1613 UPDATE_SYNTAX_TABLE_FORWARD (pos
);
1621 unsigned char *prev_p
;
1632 while (--p
>= stop
&& ! CHAR_HEAD_P (*p
));
1633 PARSE_MULTIBYTE_SEQ (p
, MAX_MULTIBYTE_LENGTH
, nbytes
);
1634 if (prev_p
- p
> nbytes
)
1635 p
= prev_p
- 1, c
= *p
, nbytes
= 1;
1637 c
= STRING_CHAR (p
, MAX_MULTIBYTE_LENGTH
);
1638 pos
--, pos_byte
-= nbytes
;
1639 UPDATE_SYNTAX_TABLE_BACKWARD (pos
);
1640 if (! fastmap
[(int) SYNTAX (c
)])
1657 if (! fastmap
[(int) SYNTAX (p
[-1])])
1660 UPDATE_SYNTAX_TABLE_BACKWARD (pos
- 1);
1680 c
= STRING_CHAR_AND_LENGTH (p
, MAX_MULTIBYTE_LENGTH
, nbytes
);
1682 if (! NILP (iso_classes
) && in_classes (c
, iso_classes
))
1690 if (SINGLE_BYTE_CHAR_P (c
))
1697 /* If we are looking at a multibyte character,
1698 we must look up the character in the table
1699 CHAR_RANGES. If there's no data in the
1700 table, that character is not what we want to
1703 /* The following code do the right thing even if
1704 n_char_ranges is zero (i.e. no data in
1706 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1707 if (c
>= char_ranges
[i
] && c
<= char_ranges
[i
+ 1])
1709 if (!(negate
^ (i
< n_char_ranges
)))
1713 p
+= nbytes
, pos
++, pos_byte
+= nbytes
;
1726 if (!NILP (iso_classes
) && in_classes (*p
, iso_classes
))
1731 goto fwd_unibyte_ok
;
1745 unsigned char *prev_p
;
1756 while (--p
>= stop
&& ! CHAR_HEAD_P (*p
));
1757 PARSE_MULTIBYTE_SEQ (p
, MAX_MULTIBYTE_LENGTH
, nbytes
);
1758 if (prev_p
- p
> nbytes
)
1759 p
= prev_p
- 1, c
= *p
, nbytes
= 1;
1761 c
= STRING_CHAR (p
, MAX_MULTIBYTE_LENGTH
);
1763 if (! NILP (iso_classes
) && in_classes (c
, iso_classes
))
1771 if (SINGLE_BYTE_CHAR_P (c
))
1778 /* See the comment in the previous similar code. */
1779 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1780 if (c
>= char_ranges
[i
] && c
<= char_ranges
[i
+ 1])
1782 if (!(negate
^ (i
< n_char_ranges
)))
1786 pos
--, pos_byte
-= nbytes
;
1799 if (! NILP (iso_classes
) && in_classes (p
[-1], iso_classes
))
1804 goto back_unibyte_ok
;
1807 if (!fastmap
[p
[-1]])
1815 #if 0 /* Not needed now that a position in mid-character
1816 cannot be specified in Lisp. */
1818 /* INC_POS or DEC_POS might have moved POS over LIM. */
1819 && (forwardp
? (pos
> XINT (lim
)) : (pos
< XINT (lim
))))
1826 SET_PT_BOTH (pos
, pos_byte
);
1829 return make_number (PT
- start_point
);
1833 /* Return 1 if character C belongs to one of the ISO classes
1834 in the list ISO_CLASSES. Each class is represented by an
1835 integer which is its type according to re_wctype. */
1838 in_classes (c
, iso_classes
)
1840 Lisp_Object iso_classes
;
1844 while (! NILP (iso_classes
))
1847 elt
= XCAR (iso_classes
);
1848 iso_classes
= XCDR (iso_classes
);
1850 if (re_iswctype (c
, XFASTINT (elt
)))
1857 /* Jump over a comment, assuming we are at the beginning of one.
1858 FROM is the current position.
1859 FROM_BYTE is the bytepos corresponding to FROM.
1860 Do not move past STOP (a charpos).
1861 The comment over which we have to jump is of style STYLE
1862 (either SYNTAX_COMMENT_STYLE(foo) or ST_COMMENT_STYLE).
1863 NESTING should be positive to indicate the nesting at the beginning
1864 for nested comments and should be zero or negative else.
1865 ST_COMMENT_STYLE cannot be nested.
1866 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
1867 (or 0 If the search cannot start in the middle of a two-character).
1869 If successful, return 1 and store the charpos of the comment's end
1870 into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR.
1871 Else, return 0 and store the charpos STOP into *CHARPOS_PTR, the
1872 corresponding bytepos into *BYTEPOS_PTR and the current nesting
1873 (as defined for state.incomment) in *INCOMMENT_PTR.
1875 The comment end is the last character of the comment rather than the
1876 character just after the comment.
1878 Global syntax data is assumed to initially be valid for FROM and
1879 remains valid for forward search starting at the returned position. */
1882 forw_comment (from
, from_byte
, stop
, nesting
, style
, prev_syntax
,
1883 charpos_ptr
, bytepos_ptr
, incomment_ptr
)
1884 int from
, from_byte
, stop
;
1885 int nesting
, style
, prev_syntax
;
1886 int *charpos_ptr
, *bytepos_ptr
, *incomment_ptr
;
1889 register enum syntaxcode code
;
1890 register int syntax
;
1892 if (nesting
<= 0) nesting
= -1;
1894 /* Enter the loop in the middle so that we find
1895 a 2-char comment ender if we start in the middle of it. */
1896 syntax
= prev_syntax
;
1897 if (syntax
!= 0) goto forw_incomment
;
1903 *incomment_ptr
= nesting
;
1904 *charpos_ptr
= from
;
1905 *bytepos_ptr
= from_byte
;
1908 c
= FETCH_CHAR (from_byte
);
1909 syntax
= SYNTAX_WITH_FLAGS (c
);
1910 code
= syntax
& 0xff;
1911 if (code
== Sendcomment
1912 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
) == style
1913 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ?
1914 (nesting
> 0 && --nesting
== 0) : nesting
< 0))
1915 /* we have encountered a comment end of the same style
1916 as the comment sequence which began this comment
1919 if (code
== Scomment_fence
1920 && style
== ST_COMMENT_STYLE
)
1921 /* we have encountered a comment end of the same style
1922 as the comment sequence which began this comment
1927 && SYNTAX_FLAGS_COMMENT_NESTED (syntax
)
1928 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
) == style
)
1929 /* we have encountered a nested comment of the same style
1930 as the comment sequence which began this comment section */
1932 INC_BOTH (from
, from_byte
);
1933 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1936 if (from
< stop
&& SYNTAX_FLAGS_COMEND_FIRST (syntax
)
1937 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
) == style
1938 && (c1
= FETCH_CHAR (from_byte
),
1939 SYNTAX_COMEND_SECOND (c1
))
1940 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ||
1941 SYNTAX_COMMENT_NESTED (c1
)) ? nesting
> 0 : nesting
< 0))
1944 /* we have encountered a comment end of the same style
1945 as the comment sequence which began this comment
1950 INC_BOTH (from
, from_byte
);
1951 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1956 && SYNTAX_FLAGS_COMSTART_FIRST (syntax
)
1957 && (c1
= FETCH_CHAR (from_byte
),
1958 SYNTAX_COMMENT_STYLE (c1
) == style
1959 && SYNTAX_COMSTART_SECOND (c1
))
1960 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ||
1961 SYNTAX_COMMENT_NESTED (c1
)))
1962 /* we have encountered a nested comment of the same style
1963 as the comment sequence which began this comment
1966 INC_BOTH (from
, from_byte
);
1967 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1971 *charpos_ptr
= from
;
1972 *bytepos_ptr
= from_byte
;
1976 DEFUN ("forward-comment", Fforward_comment
, Sforward_comment
, 1, 1, 0,
1978 Move forward across up to COUNT comments. If COUNT is negative, move backward.
1979 Stop scanning if we find something other than a comment or whitespace.
1980 Set point to where scanning stops.
1981 If COUNT comments are found as expected, with nothing except whitespace
1982 between them, return t; otherwise return nil. */)
1990 register enum syntaxcode code
;
1991 int comstyle
= 0; /* style of comment encountered */
1992 int comnested
= 0; /* whether the comment is nestable or not */
1995 int out_charpos
, out_bytepos
;
1998 CHECK_NUMBER (count
);
1999 count1
= XINT (count
);
2000 stop
= count1
> 0 ? ZV
: BEGV
;
2006 from_byte
= PT_BYTE
;
2008 SETUP_SYNTAX_TABLE (from
, count1
);
2017 SET_PT_BOTH (from
, from_byte
);
2021 c
= FETCH_CHAR (from_byte
);
2023 comstart_first
= SYNTAX_COMSTART_FIRST (c
);
2024 comnested
= SYNTAX_COMMENT_NESTED (c
);
2025 comstyle
= SYNTAX_COMMENT_STYLE (c
);
2026 INC_BOTH (from
, from_byte
);
2027 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2028 if (from
< stop
&& comstart_first
2029 && (c1
= FETCH_CHAR (from_byte
),
2030 SYNTAX_COMSTART_SECOND (c1
)))
2032 /* We have encountered a comment start sequence and we
2033 are ignoring all text inside comments. We must record
2034 the comment style this sequence begins so that later,
2035 only a comment end of the same style actually ends
2036 the comment section. */
2038 comstyle
= SYNTAX_COMMENT_STYLE (c1
);
2039 comnested
= comnested
|| SYNTAX_COMMENT_NESTED (c1
);
2040 INC_BOTH (from
, from_byte
);
2041 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2044 while (code
== Swhitespace
|| (code
== Sendcomment
&& c
== '\n'));
2046 if (code
== Scomment_fence
)
2047 comstyle
= ST_COMMENT_STYLE
;
2048 else if (code
!= Scomment
)
2051 DEC_BOTH (from
, from_byte
);
2052 SET_PT_BOTH (from
, from_byte
);
2055 /* We're at the start of a comment. */
2056 found
= forw_comment (from
, from_byte
, stop
, comnested
, comstyle
, 0,
2057 &out_charpos
, &out_bytepos
, &dummy
);
2058 from
= out_charpos
; from_byte
= out_bytepos
;
2062 SET_PT_BOTH (from
, from_byte
);
2065 INC_BOTH (from
, from_byte
);
2066 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2067 /* We have skipped one comment. */
2079 SET_PT_BOTH (BEGV
, BEGV_BYTE
);
2084 DEC_BOTH (from
, from_byte
);
2085 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
2086 quoted
= char_quoted (from
, from_byte
);
2087 c
= FETCH_CHAR (from_byte
);
2090 comnested
= SYNTAX_COMMENT_NESTED (c
);
2091 if (code
== Sendcomment
)
2092 comstyle
= SYNTAX_COMMENT_STYLE (c
);
2093 if (from
> stop
&& SYNTAX_COMEND_SECOND (c
)
2094 && prev_char_comend_first (from
, from_byte
)
2095 && !char_quoted (from
- 1, dec_bytepos (from_byte
)))
2097 /* We must record the comment style encountered so that
2098 later, we can match only the proper comment begin
2099 sequence of the same style. */
2100 DEC_BOTH (from
, from_byte
);
2102 /* Calling char_quoted, above, set up global syntax position
2103 at the new value of FROM. */
2104 c1
= FETCH_CHAR (from_byte
);
2105 comstyle
= SYNTAX_COMMENT_STYLE (c1
);
2106 comnested
= comnested
|| SYNTAX_COMMENT_NESTED (c1
);
2109 if (code
== Scomment_fence
)
2111 /* Skip until first preceding unquoted comment_fence. */
2112 int found
= 0, ini
= from
, ini_byte
= from_byte
;
2116 DEC_BOTH (from
, from_byte
);
2119 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2120 c
= FETCH_CHAR (from_byte
);
2121 if (SYNTAX (c
) == Scomment_fence
2122 && !char_quoted (from
, from_byte
))
2130 from
= ini
; /* Set point to ini + 1. */
2131 from_byte
= ini_byte
;
2135 else if (code
== Sendcomment
)
2137 found
= back_comment (from
, from_byte
, stop
, comnested
, comstyle
,
2138 &out_charpos
, &out_bytepos
);
2142 /* This end-of-line is not an end-of-comment.
2143 Treat it like a whitespace.
2144 CC-mode (and maybe others) relies on this behavior. */
2148 /* Failure: we should go back to the end of this
2149 not-quite-endcomment. */
2150 if (SYNTAX(c
) != code
)
2151 /* It was a two-char Sendcomment. */
2152 INC_BOTH (from
, from_byte
);
2158 /* We have skipped one comment. */
2159 from
= out_charpos
, from_byte
= out_bytepos
;
2163 else if (code
!= Swhitespace
|| quoted
)
2167 INC_BOTH (from
, from_byte
);
2168 SET_PT_BOTH (from
, from_byte
);
2176 SET_PT_BOTH (from
, from_byte
);
2181 /* Return syntax code of character C if C is a single byte character
2182 or `multibyte_symbol_p' is zero. Otherwise, return Ssymbol. */
2184 #define SYNTAX_WITH_MULTIBYTE_CHECK(c) \
2185 ((SINGLE_BYTE_CHAR_P (c) || !multibyte_symbol_p) \
2186 ? SYNTAX (c) : Ssymbol)
2189 scan_lists (from
, count
, depth
, sexpflag
)
2191 int count
, depth
, sexpflag
;
2194 register int stop
= count
> 0 ? ZV
: BEGV
;
2199 register enum syntaxcode code
, temp_code
;
2200 int min_depth
= depth
; /* Err out if depth gets less than this. */
2201 int comstyle
= 0; /* style of comment encountered */
2202 int comnested
= 0; /* whether the comment is nestable or not */
2204 int last_good
= from
;
2207 int out_bytepos
, out_charpos
;
2209 int multibyte_symbol_p
= sexpflag
&& multibyte_syntax_as_symbol
;
2211 if (depth
> 0) min_depth
= 0;
2213 if (from
> ZV
) from
= ZV
;
2214 if (from
< BEGV
) from
= BEGV
;
2216 from_byte
= CHAR_TO_BYTE (from
);
2221 SETUP_SYNTAX_TABLE (from
, count
);
2226 int comstart_first
, prefix
;
2227 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2228 c
= FETCH_CHAR (from_byte
);
2229 code
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2230 comstart_first
= SYNTAX_COMSTART_FIRST (c
);
2231 comnested
= SYNTAX_COMMENT_NESTED (c
);
2232 comstyle
= SYNTAX_COMMENT_STYLE (c
);
2233 prefix
= SYNTAX_PREFIX (c
);
2234 if (depth
== min_depth
)
2236 INC_BOTH (from
, from_byte
);
2237 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2238 if (from
< stop
&& comstart_first
2239 && (c
= FETCH_CHAR (from_byte
), SYNTAX_COMSTART_SECOND (c
))
2240 && parse_sexp_ignore_comments
)
2242 /* we have encountered a comment start sequence and we
2243 are ignoring all text inside comments. We must record
2244 the comment style this sequence begins so that later,
2245 only a comment end of the same style actually ends
2246 the comment section */
2248 c1
= FETCH_CHAR (from_byte
);
2249 comstyle
= SYNTAX_COMMENT_STYLE (c1
);
2250 comnested
= comnested
|| SYNTAX_COMMENT_NESTED (c1
);
2251 INC_BOTH (from
, from_byte
);
2252 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2258 switch (SWITCH_ENUM_CAST (code
))
2262 if (from
== stop
) goto lose
;
2263 INC_BOTH (from
, from_byte
);
2264 /* treat following character as a word constituent */
2267 if (depth
|| !sexpflag
) break;
2268 /* This word counts as a sexp; return at end of it. */
2271 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2273 /* Some compilers can't handle this inside the switch. */
2274 c
= FETCH_CHAR (from_byte
);
2275 temp
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2280 INC_BOTH (from
, from_byte
);
2281 if (from
== stop
) goto lose
;
2290 INC_BOTH (from
, from_byte
);
2294 case Scomment_fence
:
2295 comstyle
= ST_COMMENT_STYLE
;
2298 if (!parse_sexp_ignore_comments
) break;
2299 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2300 found
= forw_comment (from
, from_byte
, stop
,
2301 comnested
, comstyle
, 0,
2302 &out_charpos
, &out_bytepos
, &dummy
);
2303 from
= out_charpos
, from_byte
= out_bytepos
;
2310 INC_BOTH (from
, from_byte
);
2311 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2317 if (from
!= stop
&& c
== FETCH_CHAR (from_byte
))
2319 INC_BOTH (from
, from_byte
);
2329 if (!++depth
) goto done
;
2334 if (!--depth
) goto done
;
2335 if (depth
< min_depth
)
2336 Fsignal (Qscan_error
,
2337 Fcons (build_string ("Containing expression ends prematurely"),
2338 Fcons (make_number (last_good
),
2339 Fcons (make_number (from
), Qnil
))));
2344 temp_pos
= dec_bytepos (from_byte
);
2345 stringterm
= FETCH_CHAR (temp_pos
);
2348 if (from
>= stop
) goto lose
;
2349 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2350 c
= FETCH_CHAR (from_byte
);
2353 && SYNTAX_WITH_MULTIBYTE_CHECK (c
) == Sstring
)
2354 : SYNTAX_WITH_MULTIBYTE_CHECK (c
) == Sstring_fence
)
2357 /* Some compilers can't handle this inside the switch. */
2358 temp
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2363 INC_BOTH (from
, from_byte
);
2365 INC_BOTH (from
, from_byte
);
2367 INC_BOTH (from
, from_byte
);
2368 if (!depth
&& sexpflag
) goto done
;
2371 /* Ignore whitespace, punctuation, quote, endcomment. */
2376 /* Reached end of buffer. Error if within object, return nil if between */
2377 if (depth
) goto lose
;
2382 /* End of object reached */
2391 DEC_BOTH (from
, from_byte
);
2392 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2393 c
= FETCH_CHAR (from_byte
);
2394 code
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2395 if (depth
== min_depth
)
2398 comnested
= SYNTAX_COMMENT_NESTED (c
);
2399 if (code
== Sendcomment
)
2400 comstyle
= SYNTAX_COMMENT_STYLE (c
);
2401 if (from
> stop
&& SYNTAX_COMEND_SECOND (c
)
2402 && prev_char_comend_first (from
, from_byte
)
2403 && parse_sexp_ignore_comments
)
2405 /* We must record the comment style encountered so that
2406 later, we can match only the proper comment begin
2407 sequence of the same style. */
2408 DEC_BOTH (from
, from_byte
);
2409 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2411 c1
= FETCH_CHAR (from_byte
);
2412 comstyle
= SYNTAX_COMMENT_STYLE (c1
);
2413 comnested
= comnested
|| SYNTAX_COMMENT_NESTED (c1
);
2416 /* Quoting turns anything except a comment-ender
2417 into a word character. Note that this cannot be true
2418 if we decremented FROM in the if-statement above. */
2419 if (code
!= Sendcomment
&& char_quoted (from
, from_byte
))
2421 DEC_BOTH (from
, from_byte
);
2424 else if (SYNTAX_PREFIX (c
))
2427 switch (SWITCH_ENUM_CAST (code
))
2433 if (depth
|| !sexpflag
) break;
2434 /* This word counts as a sexp; count object finished
2435 after passing it. */
2438 temp_pos
= from_byte
;
2439 if (! NILP (current_buffer
->enable_multibyte_characters
))
2443 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2444 c1
= FETCH_CHAR (temp_pos
);
2445 temp_code
= SYNTAX_WITH_MULTIBYTE_CHECK (c1
);
2446 /* Don't allow comment-end to be quoted. */
2447 if (temp_code
== Sendcomment
)
2449 quoted
= char_quoted (from
- 1, temp_pos
);
2452 DEC_BOTH (from
, from_byte
);
2453 temp_pos
= dec_bytepos (temp_pos
);
2454 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2456 c1
= FETCH_CHAR (temp_pos
);
2457 temp_code
= SYNTAX_WITH_MULTIBYTE_CHECK (c1
);
2458 if (! (quoted
|| temp_code
== Sword
2459 || temp_code
== Ssymbol
2460 || temp_code
== Squote
))
2462 DEC_BOTH (from
, from_byte
);
2469 temp_pos
= dec_bytepos (from_byte
);
2470 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2471 if (from
!= stop
&& c
== FETCH_CHAR (temp_pos
))
2472 DEC_BOTH (from
, from_byte
);
2481 if (!++depth
) goto done2
;
2486 if (!--depth
) goto done2
;
2487 if (depth
< min_depth
)
2488 Fsignal (Qscan_error
,
2489 Fcons (build_string ("Containing expression ends prematurely"),
2490 Fcons (make_number (last_good
),
2491 Fcons (make_number (from
), Qnil
))));
2495 if (!parse_sexp_ignore_comments
)
2497 found
= back_comment (from
, from_byte
, stop
, comnested
, comstyle
,
2498 &out_charpos
, &out_bytepos
);
2499 /* FIXME: if found == -1, then it really wasn't a comment-end.
2500 For single-char Sendcomment, we can't do much about it apart
2501 from skipping the char.
2502 For 2-char endcomments, we could try again, taking both
2503 chars as separate entities, but it's a lot of trouble
2504 for very little gain, so we don't bother either. -sm */
2506 from
= out_charpos
, from_byte
= out_bytepos
;
2509 case Scomment_fence
:
2513 if (from
== stop
) goto lose
;
2514 DEC_BOTH (from
, from_byte
);
2515 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2516 if (!char_quoted (from
, from_byte
)
2517 && (c
= FETCH_CHAR (from_byte
),
2518 SYNTAX_WITH_MULTIBYTE_CHECK (c
) == code
))
2521 if (code
== Sstring_fence
&& !depth
&& sexpflag
) goto done2
;
2525 stringterm
= FETCH_CHAR (from_byte
);
2528 if (from
== stop
) goto lose
;
2529 DEC_BOTH (from
, from_byte
);
2530 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2531 if (!char_quoted (from
, from_byte
)
2532 && stringterm
== (c
= FETCH_CHAR (from_byte
))
2533 && SYNTAX_WITH_MULTIBYTE_CHECK (c
) == Sstring
)
2536 if (!depth
&& sexpflag
) goto done2
;
2539 /* Ignore whitespace, punctuation, quote, endcomment. */
2544 /* Reached start of buffer. Error if within object, return nil if between */
2545 if (depth
) goto lose
;
2556 XSETFASTINT (val
, from
);
2560 Fsignal (Qscan_error
,
2561 Fcons (build_string ("Unbalanced parentheses"),
2562 Fcons (make_number (last_good
),
2563 Fcons (make_number (from
), Qnil
))));
2568 DEFUN ("scan-lists", Fscan_lists
, Sscan_lists
, 3, 3, 0,
2569 doc
: /* Scan from character number FROM by COUNT lists.
2570 Returns the character number of the position thus found.
2572 If DEPTH is nonzero, paren depth begins counting from that value,
2573 only places where the depth in parentheses becomes zero
2574 are candidates for stopping; COUNT such places are counted.
2575 Thus, a positive value for DEPTH means go out levels.
2577 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2579 If the beginning or end of (the accessible part of) the buffer is reached
2580 and the depth is wrong, an error is signaled.
2581 If the depth is right but the count is not used up, nil is returned. */)
2582 (from
, count
, depth
)
2583 Lisp_Object from
, count
, depth
;
2585 CHECK_NUMBER (from
);
2586 CHECK_NUMBER (count
);
2587 CHECK_NUMBER (depth
);
2589 return scan_lists (XINT (from
), XINT (count
), XINT (depth
), 0);
2592 DEFUN ("scan-sexps", Fscan_sexps
, Sscan_sexps
, 2, 2, 0,
2593 doc
: /* Scan from character number FROM by COUNT balanced expressions.
2594 If COUNT is negative, scan backwards.
2595 Returns the character number of the position thus found.
2597 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2599 If the beginning or end of (the accessible part of) the buffer is reached
2600 in the middle of a parenthetical grouping, an error is signaled.
2601 If the beginning or end is reached between groupings
2602 but before count is used up, nil is returned. */)
2604 Lisp_Object from
, count
;
2606 CHECK_NUMBER (from
);
2607 CHECK_NUMBER (count
);
2609 return scan_lists (XINT (from
), XINT (count
), 0, 1);
2612 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars
, Sbackward_prefix_chars
,
2614 doc
: /* Move point backward over any number of chars with prefix syntax.
2615 This includes chars with "quote" or "prefix" syntax (' or p). */)
2620 int opoint_byte
= PT_BYTE
;
2622 int pos_byte
= PT_BYTE
;
2627 SET_PT_BOTH (opoint
, opoint_byte
);
2632 SETUP_SYNTAX_TABLE (pos
, -1);
2634 DEC_BOTH (pos
, pos_byte
);
2636 while (!char_quoted (pos
, pos_byte
)
2637 /* Previous statement updates syntax table. */
2638 && ((c
= FETCH_CHAR (pos_byte
), SYNTAX (c
) == Squote
)
2639 || SYNTAX_PREFIX (c
)))
2642 opoint_byte
= pos_byte
;
2645 DEC_BOTH (pos
, pos_byte
);
2648 SET_PT_BOTH (opoint
, opoint_byte
);
2653 /* Parse forward from FROM / FROM_BYTE to END,
2654 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
2655 and return a description of the state of the parse at END.
2656 If STOPBEFORE is nonzero, stop at the start of an atom.
2657 If COMMENTSTOP is 1, stop at the start of a comment.
2658 If COMMENTSTOP is -1, stop at the start or end of a comment,
2659 after the beginning of a string, or after the end of a string. */
2662 scan_sexps_forward (stateptr
, from
, from_byte
, end
, targetdepth
,
2663 stopbefore
, oldstate
, commentstop
)
2664 struct lisp_parse_state
*stateptr
;
2666 int end
, targetdepth
, stopbefore
, from_byte
;
2667 Lisp_Object oldstate
;
2670 struct lisp_parse_state state
;
2672 register enum syntaxcode code
;
2675 struct level
{ int last
, prev
; };
2676 struct level levelstart
[100];
2677 register struct level
*curlevel
= levelstart
;
2678 struct level
*endlevel
= levelstart
+ 100;
2679 register int depth
; /* Paren depth of current scanning location.
2680 level - levelstart equals this except
2681 when the depth becomes negative. */
2682 int mindepth
; /* Lowest DEPTH value seen. */
2683 int start_quoted
= 0; /* Nonzero means starting after a char quote */
2685 int prev_from
; /* Keep one character before FROM. */
2687 int prev_from_syntax
;
2688 int boundary_stop
= commentstop
== -1;
2691 int out_bytepos
, out_charpos
;
2695 prev_from_byte
= from_byte
;
2697 DEC_BOTH (prev_from
, prev_from_byte
);
2699 /* Use this macro instead of `from++'. */
2701 do { prev_from = from; \
2702 prev_from_byte = from_byte; \
2703 temp = FETCH_CHAR (prev_from_byte); \
2704 prev_from_syntax = SYNTAX_WITH_FLAGS (temp); \
2705 INC_BOTH (from, from_byte); \
2707 UPDATE_SYNTAX_TABLE_FORWARD (from); \
2713 if (NILP (oldstate
))
2716 state
.instring
= -1;
2717 state
.incomment
= 0;
2718 state
.comstyle
= 0; /* comment style a by default. */
2719 state
.comstr_start
= -1; /* no comment/string seen. */
2723 tem
= Fcar (oldstate
);
2729 oldstate
= Fcdr (oldstate
);
2730 oldstate
= Fcdr (oldstate
);
2731 oldstate
= Fcdr (oldstate
);
2732 tem
= Fcar (oldstate
);
2733 /* Check whether we are inside string_fence-style string: */
2734 state
.instring
= (!NILP (tem
)
2735 ? (INTEGERP (tem
) ? XINT (tem
) : ST_STRING_STYLE
)
2738 oldstate
= Fcdr (oldstate
);
2739 tem
= Fcar (oldstate
);
2740 state
.incomment
= (!NILP (tem
)
2741 ? (INTEGERP (tem
) ? XINT (tem
) : -1)
2744 oldstate
= Fcdr (oldstate
);
2745 tem
= Fcar (oldstate
);
2746 start_quoted
= !NILP (tem
);
2748 /* if the eighth element of the list is nil, we are in comment
2749 style a. If it is non-nil, we are in comment style b */
2750 oldstate
= Fcdr (oldstate
);
2751 oldstate
= Fcdr (oldstate
);
2752 tem
= Fcar (oldstate
);
2753 state
.comstyle
= NILP (tem
) ? 0 : (EQ (tem
, Qsyntax_table
)
2754 ? ST_COMMENT_STYLE
: 1);
2756 oldstate
= Fcdr (oldstate
);
2757 tem
= Fcar (oldstate
);
2758 state
.comstr_start
= NILP (tem
) ? -1 : XINT (tem
) ;
2759 oldstate
= Fcdr (oldstate
);
2760 tem
= Fcar (oldstate
);
2761 while (!NILP (tem
)) /* >= second enclosing sexps. */
2763 /* curlevel++->last ran into compiler bug on Apollo */
2764 curlevel
->last
= XINT (Fcar (tem
));
2765 if (++curlevel
== endlevel
)
2766 curlevel
--; /* error ("Nesting too deep for parser"); */
2767 curlevel
->prev
= -1;
2768 curlevel
->last
= -1;
2775 curlevel
->prev
= -1;
2776 curlevel
->last
= -1;
2778 SETUP_SYNTAX_TABLE (prev_from
, 1);
2779 temp
= FETCH_CHAR (prev_from_byte
);
2780 prev_from_syntax
= SYNTAX_WITH_FLAGS (temp
);
2781 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2783 /* Enter the loop at a place appropriate for initial state. */
2785 if (state
.incomment
)
2786 goto startincomment
;
2787 if (state
.instring
>= 0)
2789 nofence
= state
.instring
!= ST_STRING_STYLE
;
2791 goto startquotedinstring
;
2794 else if (start_quoted
)
2800 code
= prev_from_syntax
& 0xff;
2802 if (code
== Scomment
)
2804 state
.comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax
);
2805 state
.incomment
= (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax
) ?
2807 state
.comstr_start
= prev_from
;
2809 else if (code
== Scomment_fence
)
2811 /* Record the comment style we have entered so that only
2812 the comment-end sequence of the same style actually
2813 terminates the comment section. */
2814 state
.comstyle
= ST_COMMENT_STYLE
;
2815 state
.incomment
= -1;
2816 state
.comstr_start
= prev_from
;
2819 else if (from
< end
)
2820 if (SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax
))
2821 if (c1
= FETCH_CHAR (from_byte
),
2822 SYNTAX_COMSTART_SECOND (c1
))
2823 /* Duplicate code to avoid a complex if-expression
2824 which causes trouble for the SGI compiler. */
2826 /* Record the comment style we have entered so that only
2827 the comment-end sequence of the same style actually
2828 terminates the comment section. */
2829 state
.comstyle
= SYNTAX_COMMENT_STYLE (c1
);
2830 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax
);
2831 comnested
= comnested
|| SYNTAX_COMMENT_NESTED (c1
);
2832 state
.incomment
= comnested
? 1 : -1;
2833 state
.comstr_start
= prev_from
;
2838 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax
))
2840 switch (SWITCH_ENUM_CAST (code
))
2844 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
2845 curlevel
->last
= prev_from
;
2847 if (from
== end
) goto endquoted
;
2850 /* treat following character as a word constituent */
2853 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
2854 curlevel
->last
= prev_from
;
2858 /* Some compilers can't handle this inside the switch. */
2859 temp
= FETCH_CHAR (from_byte
);
2860 temp
= SYNTAX (temp
);
2866 if (from
== end
) goto endquoted
;
2878 curlevel
->prev
= curlevel
->last
;
2881 case Scomment_fence
: /* Can't happen because it's handled above. */
2883 if (commentstop
|| boundary_stop
) goto done
;
2885 /* The (from == BEGV) test was to enter the loop in the middle so
2886 that we find a 2-char comment ender even if we start in the
2887 middle of it. We don't want to do that if we're just at the
2888 beginning of the comment (think of (*) ... (*)). */
2889 found
= forw_comment (from
, from_byte
, end
,
2890 state
.incomment
, state
.comstyle
,
2891 (from
== BEGV
|| from
< state
.comstr_start
+ 3)
2892 ? 0 : prev_from_syntax
,
2893 &out_charpos
, &out_bytepos
, &state
.incomment
);
2894 from
= out_charpos
; from_byte
= out_bytepos
;
2895 /* Beware! prev_from and friends are invalid now.
2896 Luckily, the `done' doesn't use them and the INC_FROM
2897 sets them to a sane value without looking at them. */
2898 if (!found
) goto done
;
2900 state
.incomment
= 0;
2901 state
.comstyle
= 0; /* reset the comment style */
2902 if (boundary_stop
) goto done
;
2906 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
2908 /* curlevel++->last ran into compiler bug on Apollo */
2909 curlevel
->last
= prev_from
;
2910 if (++curlevel
== endlevel
)
2911 curlevel
--; /* error ("Nesting too deep for parser"); */
2912 curlevel
->prev
= -1;
2913 curlevel
->last
= -1;
2914 if (targetdepth
== depth
) goto done
;
2919 if (depth
< mindepth
)
2921 if (curlevel
!= levelstart
)
2923 curlevel
->prev
= curlevel
->last
;
2924 if (targetdepth
== depth
) goto done
;
2929 state
.comstr_start
= from
- 1;
2930 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
2931 curlevel
->last
= prev_from
;
2932 state
.instring
= (code
== Sstring
2933 ? (FETCH_CHAR (prev_from_byte
))
2935 if (boundary_stop
) goto done
;
2938 nofence
= state
.instring
!= ST_STRING_STYLE
;
2944 if (from
>= end
) goto done
;
2945 c
= FETCH_CHAR (from_byte
);
2946 /* Some compilers can't handle this inside the switch. */
2949 /* Check TEMP here so that if the char has
2950 a syntax-table property which says it is NOT
2951 a string character, it does not end the string. */
2952 if (nofence
&& c
== state
.instring
&& temp
== Sstring
)
2958 if (!nofence
) goto string_end
;
2963 startquotedinstring
:
2964 if (from
>= end
) goto endquoted
;
2970 state
.instring
= -1;
2971 curlevel
->prev
= curlevel
->last
;
2973 if (boundary_stop
) goto done
;
2977 /* FIXME: We should do something with it. */
2980 /* Ignore whitespace, punctuation, quote, endcomment. */
2986 stop
: /* Here if stopping before start of sexp. */
2987 from
= prev_from
; /* We have just fetched the char that starts it; */
2988 goto done
; /* but return the position before it. */
2993 state
.depth
= depth
;
2994 state
.mindepth
= mindepth
;
2995 state
.thislevelstart
= curlevel
->prev
;
2996 state
.prevlevelstart
2997 = (curlevel
== levelstart
) ? -1 : (curlevel
- 1)->last
;
2998 state
.location
= from
;
2999 state
.levelstarts
= Qnil
;
3000 while (--curlevel
>= levelstart
)
3001 state
.levelstarts
= Fcons (make_number (curlevel
->last
),
3008 DEFUN ("parse-partial-sexp", Fparse_partial_sexp
, Sparse_partial_sexp
, 2, 6, 0,
3009 doc
: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
3010 Parsing stops at TO or when certain criteria are met;
3011 point is set to where parsing stops.
3012 If fifth arg OLDSTATE is omitted or nil,
3013 parsing assumes that FROM is the beginning of a function.
3014 Value is a list of ten elements describing final state of parsing:
3016 1. character address of start of innermost containing list; nil if none.
3017 2. character address of start of last complete sexp terminated.
3018 3. non-nil if inside a string.
3019 (it is the character that will terminate the string,
3020 or t if the string should be terminated by a generic string delimiter.)
3021 4. nil if outside a comment, t if inside a non-nestable comment,
3022 else an integer (the current comment nesting).
3023 5. t if following a quote character.
3024 6. the minimum paren-depth encountered during this scan.
3025 7. t if in a comment of style b; symbol `syntax-table' if the comment
3026 should be terminated by a generic comment delimiter.
3027 8. character address of start of comment or string; nil if not in one.
3028 9. Intermediate data for continuation of parsing (subject to change).
3029 If third arg TARGETDEPTH is non-nil, parsing stops if the depth
3030 in parentheses becomes equal to TARGETDEPTH.
3031 Fourth arg STOPBEFORE non-nil means stop when come to
3032 any character that starts a sexp.
3033 Fifth arg OLDSTATE is a nine-element list like what this function returns.
3034 It is used to initialize the state of the parse. Elements number 1, 2, 6
3035 and 8 are ignored; you can leave off element 8 (the last) entirely.
3036 Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.
3037 If it is symbol `syntax-table', stop after the start of a comment or a
3038 string, or after end of a comment or a string. */)
3039 (from
, to
, targetdepth
, stopbefore
, oldstate
, commentstop
)
3040 Lisp_Object from
, to
, targetdepth
, stopbefore
, oldstate
, commentstop
;
3042 struct lisp_parse_state state
;
3045 if (!NILP (targetdepth
))
3047 CHECK_NUMBER (targetdepth
);
3048 target
= XINT (targetdepth
);
3051 target
= -100000; /* We won't reach this depth */
3053 validate_region (&from
, &to
);
3054 scan_sexps_forward (&state
, XINT (from
), CHAR_TO_BYTE (XINT (from
)),
3056 target
, !NILP (stopbefore
), oldstate
,
3058 ? 0 : (EQ (commentstop
, Qsyntax_table
) ? -1 : 1)));
3060 SET_PT (state
.location
);
3062 return Fcons (make_number (state
.depth
),
3063 Fcons (state
.prevlevelstart
< 0 ? Qnil
: make_number (state
.prevlevelstart
),
3064 Fcons (state
.thislevelstart
< 0 ? Qnil
: make_number (state
.thislevelstart
),
3065 Fcons (state
.instring
>= 0
3066 ? (state
.instring
== ST_STRING_STYLE
3067 ? Qt
: make_number (state
.instring
)) : Qnil
,
3068 Fcons (state
.incomment
< 0 ? Qt
:
3069 (state
.incomment
== 0 ? Qnil
:
3070 make_number (state
.incomment
)),
3071 Fcons (state
.quoted
? Qt
: Qnil
,
3072 Fcons (make_number (state
.mindepth
),
3073 Fcons ((state
.comstyle
3074 ? (state
.comstyle
== ST_COMMENT_STYLE
3075 ? Qsyntax_table
: Qt
) :
3077 Fcons (((state
.incomment
3078 || (state
.instring
>= 0))
3079 ? make_number (state
.comstr_start
)
3081 Fcons (state
.levelstarts
, Qnil
))))))))));
3090 /* This has to be done here, before we call Fmake_char_table. */
3091 Qsyntax_table
= intern ("syntax-table");
3092 staticpro (&Qsyntax_table
);
3094 /* Intern this now in case it isn't already done.
3095 Setting this variable twice is harmless.
3096 But don't staticpro it here--that is done in alloc.c. */
3097 Qchar_table_extra_slots
= intern ("char-table-extra-slots");
3099 /* Create objects which can be shared among syntax tables. */
3100 Vsyntax_code_object
= Fmake_vector (make_number (Smax
), Qnil
);
3101 for (i
= 0; i
< XVECTOR (Vsyntax_code_object
)->size
; i
++)
3102 XVECTOR (Vsyntax_code_object
)->contents
[i
]
3103 = Fcons (make_number (i
), Qnil
);
3105 /* Now we are ready to set up this property, so we can
3106 create syntax tables. */
3107 Fput (Qsyntax_table
, Qchar_table_extra_slots
, make_number (0));
3109 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Swhitespace
];
3111 Vstandard_syntax_table
= Fmake_char_table (Qsyntax_table
, temp
);
3113 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Sword
];
3114 for (i
= 'a'; i
<= 'z'; i
++)
3115 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3116 for (i
= 'A'; i
<= 'Z'; i
++)
3117 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3118 for (i
= '0'; i
<= '9'; i
++)
3119 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3121 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '$', temp
);
3122 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '%', temp
);
3124 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '(',
3125 Fcons (make_number (Sopen
), make_number (')')));
3126 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ')',
3127 Fcons (make_number (Sclose
), make_number ('(')));
3128 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '[',
3129 Fcons (make_number (Sopen
), make_number (']')));
3130 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ']',
3131 Fcons (make_number (Sclose
), make_number ('[')));
3132 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '{',
3133 Fcons (make_number (Sopen
), make_number ('}')));
3134 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '}',
3135 Fcons (make_number (Sclose
), make_number ('{')));
3136 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '"',
3137 Fcons (make_number ((int) Sstring
), Qnil
));
3138 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\\',
3139 Fcons (make_number ((int) Sescape
), Qnil
));
3141 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Ssymbol
];
3142 for (i
= 0; i
< 10; i
++)
3144 c
= "_-+*/&|<>="[i
];
3145 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, c
, temp
);
3148 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Spunct
];
3149 for (i
= 0; i
< 12; i
++)
3151 c
= ".,;:?!#@~^'`"[i
];
3152 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, c
, temp
);
3155 /* All multibyte characters have syntax `word' by default. */
3156 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Sword
];
3157 for (i
= CHAR_TABLE_SINGLE_BYTE_SLOTS
; i
< CHAR_TABLE_ORDINARY_SLOTS
; i
++)
3158 XCHAR_TABLE (Vstandard_syntax_table
)->contents
[i
] = temp
;
3164 Qsyntax_table_p
= intern ("syntax-table-p");
3165 staticpro (&Qsyntax_table_p
);
3167 staticpro (&Vsyntax_code_object
);
3169 Qscan_error
= intern ("scan-error");
3170 staticpro (&Qscan_error
);
3171 Fput (Qscan_error
, Qerror_conditions
,
3172 Fcons (Qscan_error
, Fcons (Qerror
, Qnil
)));
3173 Fput (Qscan_error
, Qerror_message
,
3174 build_string ("Scan error"));
3176 DEFVAR_BOOL ("parse-sexp-ignore-comments", &parse_sexp_ignore_comments
,
3177 doc
: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
3179 DEFVAR_BOOL ("parse-sexp-lookup-properties", &parse_sexp_lookup_properties
,
3180 doc
: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3181 Otherwise, that text property is simply ignored.
3182 See the info node `(elisp)Syntax Properties' for a description of the
3183 `syntax-table' property. */);
3185 words_include_escapes
= 0;
3186 DEFVAR_BOOL ("words-include-escapes", &words_include_escapes
,
3187 doc
: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
3189 DEFVAR_BOOL ("multibyte-syntax-as-symbol", &multibyte_syntax_as_symbol
,
3190 doc
: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
3191 multibyte_syntax_as_symbol
= 0;
3193 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3194 &open_paren_in_column_0_is_defun_start
,
3195 doc
: /* *Non-nil means an open paren in column 0 denotes the start of a defun. */);
3196 open_paren_in_column_0_is_defun_start
= 1;
3198 defsubr (&Ssyntax_table_p
);
3199 defsubr (&Ssyntax_table
);
3200 defsubr (&Sstandard_syntax_table
);
3201 defsubr (&Scopy_syntax_table
);
3202 defsubr (&Sset_syntax_table
);
3203 defsubr (&Schar_syntax
);
3204 defsubr (&Smatching_paren
);
3205 defsubr (&Sstring_to_syntax
);
3206 defsubr (&Smodify_syntax_entry
);
3207 defsubr (&Sinternal_describe_syntax_value
);
3209 defsubr (&Sforward_word
);
3211 defsubr (&Sskip_chars_forward
);
3212 defsubr (&Sskip_chars_backward
);
3213 defsubr (&Sskip_syntax_forward
);
3214 defsubr (&Sskip_syntax_backward
);
3216 defsubr (&Sforward_comment
);
3217 defsubr (&Sscan_lists
);
3218 defsubr (&Sscan_sexps
);
3219 defsubr (&Sbackward_prefix_chars
);
3220 defsubr (&Sparse_partial_sexp
);
3223 /* arch-tag: 3e297b9f-088e-4b64-8f4c-fb0b3443e412
3224 (do not change this comment) */