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. */
27 #include "character.h"
30 /* Make syntax table lookup grant data in gl_state. */
31 #define SYNTAX_ENTRY_VIA_PROPERTY
34 #include "intervals.h"
36 /* We use these constants in place for comment-style and
37 string-ender-char to distinguish comments/strings started by
38 comment_fence and string_fence codes. */
40 #define ST_COMMENT_STYLE (256 + 1)
41 #define ST_STRING_STYLE (256 + 2)
44 Lisp_Object Qsyntax_table_p
, Qsyntax_table
, Qscan_error
;
46 int words_include_escapes
;
47 int parse_sexp_lookup_properties
;
49 /* Nonzero means `scan-sexps' treat all multibyte characters as symbol. */
50 int multibyte_syntax_as_symbol
;
52 /* Used as a temporary in SYNTAX_ENTRY and other macros in syntax.h,
53 if not compiled with GCC. No need to mark it, since it is used
54 only very temporarily. */
55 Lisp_Object syntax_temp
;
57 /* Non-zero means an open parenthesis in column 0 is always considered
58 to be the start of a defun. Zero means an open parenthesis in
59 column 0 has no special meaning. */
61 int open_paren_in_column_0_is_defun_start
;
63 /* This is the internal form of the parse state used in parse-partial-sexp. */
65 struct lisp_parse_state
67 int depth
; /* Depth at end of parsing. */
68 int instring
; /* -1 if not within string, else desired terminator. */
69 int incomment
; /* -1 if in unnestable comment else comment nesting */
70 int comstyle
; /* comment style a=0, or b=1, or ST_COMMENT_STYLE. */
71 int quoted
; /* Nonzero if just after an escape char at end of parsing */
72 int thislevelstart
; /* Char number of most recent start-of-expression at current level */
73 int prevlevelstart
; /* Char number of start of containing expression */
74 int location
; /* Char number at which parsing stopped. */
75 int mindepth
; /* Minimum depth seen while scanning. */
76 int comstr_start
; /* Position just after last comment/string starter. */
77 Lisp_Object levelstarts
; /* Char numbers of starts-of-expression
78 of levels (starting from outermost). */
81 /* These variables are a cache for finding the start of a defun.
82 find_start_pos is the place for which the defun start was found.
83 find_start_value is the defun start position found for it.
84 find_start_value_byte is the corresponding byte position.
85 find_start_buffer is the buffer it was found in.
86 find_start_begv is the BEGV value when it was found.
87 find_start_modiff is the value of MODIFF when it was found. */
89 static int find_start_pos
;
90 static int find_start_value
;
91 static int find_start_value_byte
;
92 static struct buffer
*find_start_buffer
;
93 static int find_start_begv
;
94 static int find_start_modiff
;
97 static int find_defun_start
P_ ((int, int));
98 static int back_comment
P_ ((int, int, int, int, int, int *, int *));
99 static int char_quoted
P_ ((int, int));
100 static Lisp_Object skip_chars
P_ ((int, int, Lisp_Object
, Lisp_Object
));
101 static Lisp_Object scan_lists
P_ ((int, int, int, int));
102 static void scan_sexps_forward
P_ ((struct lisp_parse_state
*,
104 int, Lisp_Object
, int));
107 struct gl_state_s gl_state
; /* Global state of syntax parser. */
109 INTERVAL
interval_of ();
110 #define INTERVALS_AT_ONCE 10 /* 1 + max-number of intervals
111 to scan to property-change. */
113 /* Update gl_state to an appropriate interval which contains CHARPOS. The
114 sign of COUNT give the relative position of CHARPOS wrt the previously
115 valid interval. If INIT, only [be]_property fields of gl_state are
116 valid at start, the rest is filled basing on OBJECT.
118 `gl_state.*_i' are the intervals, and CHARPOS is further in the search
119 direction than the intervals - or in an interval. We update the
120 current syntax-table basing on the property of this interval, and
121 update the interval to start further than CHARPOS - or be
122 NULL_INTERVAL. We also update lim_property to be the next value of
123 charpos to call this subroutine again - or be before/after the
124 start/end of OBJECT. */
127 update_syntax_table (charpos
, count
, init
, object
)
128 int charpos
, count
, init
;
131 Lisp_Object tmp_table
;
132 int cnt
= 0, invalidate
= 1;
137 gl_state
.old_prop
= Qnil
;
138 gl_state
.start
= gl_state
.b_property
;
139 gl_state
.stop
= gl_state
.e_property
;
140 i
= interval_of (charpos
, object
);
141 gl_state
.backward_i
= gl_state
.forward_i
= i
;
143 if (NULL_INTERVAL_P (i
))
145 /* interval_of updates only ->position of the return value, so
146 update the parents manually to speed up update_interval. */
147 while (!NULL_PARENT (i
))
149 if (AM_RIGHT_CHILD (i
))
150 INTERVAL_PARENT (i
)->position
= i
->position
151 - LEFT_TOTAL_LENGTH (i
) + TOTAL_LENGTH (i
) /* right end */
152 - TOTAL_LENGTH (INTERVAL_PARENT (i
))
153 + LEFT_TOTAL_LENGTH (INTERVAL_PARENT (i
));
155 INTERVAL_PARENT (i
)->position
= i
->position
- LEFT_TOTAL_LENGTH (i
)
157 i
= INTERVAL_PARENT (i
);
159 i
= gl_state
.forward_i
;
160 gl_state
.b_property
= i
->position
- gl_state
.offset
;
161 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
164 oldi
= i
= count
> 0 ? gl_state
.forward_i
: gl_state
.backward_i
;
166 /* We are guaranteed to be called with CHARPOS either in i,
168 if (NULL_INTERVAL_P (i
))
169 error ("Error in syntax_table logic for to-the-end intervals");
170 else if (charpos
< i
->position
) /* Move left. */
173 error ("Error in syntax_table logic for intervals <-");
174 /* Update the interval. */
175 i
= update_interval (i
, charpos
);
176 if (INTERVAL_LAST_POS (i
) != gl_state
.b_property
)
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
!= gl_state
.e_property
)
192 gl_state
.backward_i
= i
;
193 gl_state
.b_property
= i
->position
- gl_state
.offset
;
198 tmp_table
= textget (i
->plist
, Qsyntax_table
);
201 invalidate
= !EQ (tmp_table
, gl_state
.old_prop
); /* Need to invalidate? */
203 if (invalidate
) /* Did not get to adjacent interval. */
204 { /* with the same table => */
205 /* invalidate the old range. */
208 gl_state
.backward_i
= i
;
209 gl_state
.b_property
= i
->position
- gl_state
.offset
;
213 gl_state
.forward_i
= i
;
214 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
218 if (!EQ (tmp_table
, gl_state
.old_prop
))
220 gl_state
.current_syntax_table
= tmp_table
;
221 gl_state
.old_prop
= tmp_table
;
222 if (EQ (Fsyntax_table_p (tmp_table
), Qt
))
224 gl_state
.use_global
= 0;
226 else if (CONSP (tmp_table
))
228 gl_state
.use_global
= 1;
229 gl_state
.global_code
= tmp_table
;
233 gl_state
.use_global
= 0;
234 gl_state
.current_syntax_table
= current_buffer
->syntax_table
;
238 while (!NULL_INTERVAL_P (i
))
240 if (cnt
&& !EQ (tmp_table
, textget (i
->plist
, Qsyntax_table
)))
244 gl_state
.e_property
= i
->position
- gl_state
.offset
;
245 gl_state
.forward_i
= i
;
249 gl_state
.b_property
= i
->position
+ LENGTH (i
) - gl_state
.offset
;
250 gl_state
.backward_i
= i
;
254 else if (cnt
== INTERVALS_AT_ONCE
)
258 gl_state
.e_property
= i
->position
+ LENGTH (i
) - gl_state
.offset
;
259 gl_state
.forward_i
= i
;
263 gl_state
.b_property
= i
->position
- gl_state
.offset
;
264 gl_state
.backward_i
= i
;
269 i
= count
> 0 ? next_interval (i
) : previous_interval (i
);
271 eassert (NULL_INTERVAL_P (i
)); /* This property goes to the end. */
273 gl_state
.e_property
= gl_state
.stop
;
275 gl_state
.b_property
= gl_state
.start
;
278 /* Returns TRUE if char at CHARPOS is quoted.
279 Global syntax-table data should be set up already to be good at CHARPOS
280 or after. On return global syntax data is good for lookup at CHARPOS. */
283 char_quoted (charpos
, bytepos
)
284 register int charpos
, bytepos
;
286 register enum syntaxcode code
;
287 register int beg
= BEGV
;
288 register int quoted
= 0;
291 DEC_BOTH (charpos
, bytepos
);
293 while (bytepos
>= beg
)
295 UPDATE_SYNTAX_TABLE_BACKWARD (charpos
);
296 code
= SYNTAX (FETCH_CHAR (bytepos
));
297 if (! (code
== Scharquote
|| code
== Sescape
))
300 DEC_BOTH (charpos
, bytepos
);
304 UPDATE_SYNTAX_TABLE (orig
);
308 /* Return the bytepos one character after BYTEPOS.
309 We assume that BYTEPOS is not at the end of the buffer. */
312 inc_bytepos (bytepos
)
315 if (NILP (current_buffer
->enable_multibyte_characters
))
322 /* Return the bytepos one character before BYTEPOS.
323 We assume that BYTEPOS is not at the start of the buffer. */
326 dec_bytepos (bytepos
)
329 if (NILP (current_buffer
->enable_multibyte_characters
))
336 /* Return a defun-start position before before POS and not too far before.
337 It should be the last one before POS, or nearly the last.
339 When open_paren_in_column_0_is_defun_start is nonzero,
340 the beginning of every line is treated as a defun-start.
342 We record the information about where the scan started
343 and what its result was, so that another call in the same area
344 can return the same value very quickly.
346 There is no promise at which position the global syntax data is
347 valid on return from the subroutine, so the caller should explicitly
348 update the global data. */
351 find_defun_start (pos
, pos_byte
)
354 int opoint
= PT
, opoint_byte
= PT_BYTE
;
356 /* Use previous finding, if it's valid and applies to this inquiry. */
357 if (current_buffer
== find_start_buffer
358 /* Reuse the defun-start even if POS is a little farther on.
359 POS might be in the next defun, but that's ok.
360 Our value may not be the best possible, but will still be usable. */
361 && pos
<= find_start_pos
+ 1000
362 && pos
>= find_start_value
363 && BEGV
== find_start_begv
364 && MODIFF
== find_start_modiff
)
365 return find_start_value
;
367 /* Back up to start of line. */
368 scan_newline (pos
, pos_byte
, BEGV
, BEGV_BYTE
, -1, 1);
370 /* We optimize syntax-table lookup for rare updates. Thus we accept
371 only those `^\s(' which are good in global _and_ text-property
373 gl_state
.current_syntax_table
= current_buffer
->syntax_table
;
374 gl_state
.use_global
= 0;
375 if (open_paren_in_column_0_is_defun_start
)
379 /* Open-paren at start of line means we may have found our
381 if (SYNTAX (FETCH_CHAR (PT_BYTE
)) == Sopen
)
383 SETUP_SYNTAX_TABLE (PT
+ 1, -1); /* Try again... */
384 if (SYNTAX (FETCH_CHAR (PT_BYTE
)) == Sopen
)
386 /* Now fallback to the default value. */
387 gl_state
.current_syntax_table
= current_buffer
->syntax_table
;
388 gl_state
.use_global
= 0;
390 /* Move to beg of previous line. */
391 scan_newline (PT
, PT_BYTE
, BEGV
, BEGV_BYTE
, -2, 1);
395 /* Record what we found, for the next try. */
396 find_start_value
= PT
;
397 find_start_value_byte
= PT_BYTE
;
398 find_start_buffer
= current_buffer
;
399 find_start_modiff
= MODIFF
;
400 find_start_begv
= BEGV
;
401 find_start_pos
= pos
;
403 TEMP_SET_PT_BOTH (opoint
, opoint_byte
);
405 return find_start_value
;
408 /* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
411 prev_char_comend_first (pos
, pos_byte
)
416 DEC_BOTH (pos
, pos_byte
);
417 UPDATE_SYNTAX_TABLE_BACKWARD (pos
);
418 c
= FETCH_CHAR (pos_byte
);
419 val
= SYNTAX_COMEND_FIRST (c
);
420 UPDATE_SYNTAX_TABLE_FORWARD (pos
+ 1);
424 /* Return the SYNTAX_COMSTART_FIRST of the character before POS, POS_BYTE. */
427 * prev_char_comstart_first (pos, pos_byte)
432 * DEC_BOTH (pos, pos_byte);
433 * UPDATE_SYNTAX_TABLE_BACKWARD (pos);
434 * c = FETCH_CHAR (pos_byte);
435 * val = SYNTAX_COMSTART_FIRST (c);
436 * UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
440 /* Checks whether charpos FROM is at the end of a comment.
441 FROM_BYTE is the bytepos corresponding to FROM.
442 Do not move back before STOP.
444 Return a positive value if we find a comment ending at FROM/FROM_BYTE;
447 If successful, store the charpos of the comment's beginning
448 into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR.
450 Global syntax data remains valid for backward search starting at
451 the returned value (or at FROM, if the search was not successful). */
454 back_comment (from
, from_byte
, stop
, comnested
, comstyle
, charpos_ptr
, bytepos_ptr
)
455 int from
, from_byte
, stop
;
456 int comnested
, comstyle
;
457 int *charpos_ptr
, *bytepos_ptr
;
459 /* Look back, counting the parity of string-quotes,
460 and recording the comment-starters seen.
461 When we reach a safe place, assume that's not in a string;
462 then step the main scan to the earliest comment-starter seen
463 an even number of string quotes away from the safe place.
465 OFROM[I] is position of the earliest comment-starter seen
466 which is I+2X quotes from the comment-end.
467 PARITY is current parity of quotes from the comment end. */
468 int string_style
= -1; /* Presumed outside of any string. */
469 int string_lossage
= 0;
470 /* Not a real lossage: indicates that we have passed a matching comment
471 starter plus an non-matching comment-ender, meaning that any matching
472 comment-starter we might see later could be a false positive (hidden
473 inside another comment).
474 Test case: { a (* b } c (* d *) */
475 int comment_lossage
= 0;
476 int comment_end
= from
;
477 int comment_end_byte
= from_byte
;
478 int comstart_pos
= 0;
480 /* Place where the containing defun starts,
481 or 0 if we didn't come across it yet. */
483 int defun_start_byte
= 0;
484 register enum syntaxcode code
;
485 int nesting
= 1; /* current comment nesting */
489 /* FIXME: A }} comment-ender style leads to incorrect behavior
490 in the case of {{ c }}} because we ignore the last two chars which are
491 assumed to be comment-enders although they aren't. */
493 /* At beginning of range to scan, we're outside of strings;
494 that determines quote parity to the comment-end. */
497 int temp_byte
, prev_syntax
;
498 int com2start
, com2end
;
500 /* Move back and examine a character. */
501 DEC_BOTH (from
, from_byte
);
502 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
504 prev_syntax
= syntax
;
505 c
= FETCH_CHAR (from_byte
);
506 syntax
= SYNTAX_WITH_FLAGS (c
);
509 /* Check for 2-char comment markers. */
510 com2start
= (SYNTAX_FLAGS_COMSTART_FIRST (syntax
)
511 && SYNTAX_FLAGS_COMSTART_SECOND (prev_syntax
)
512 && comstyle
== SYNTAX_FLAGS_COMMENT_STYLE (prev_syntax
)
513 && (SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax
)
514 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
)) == comnested
);
515 com2end
= (SYNTAX_FLAGS_COMEND_FIRST (syntax
)
516 && SYNTAX_FLAGS_COMEND_SECOND (prev_syntax
));
518 /* Nasty cases with overlapping 2-char comment markers:
519 - snmp-mode: -- c -- foo -- c --
527 /* If a 2-char comment sequence partly overlaps with another,
528 we don't try to be clever. */
529 if (from
> stop
&& (com2end
|| com2start
))
531 int next
= from
, next_byte
= from_byte
, next_c
, next_syntax
;
532 DEC_BOTH (next
, next_byte
);
533 UPDATE_SYNTAX_TABLE_BACKWARD (next
);
534 next_c
= FETCH_CHAR (next_byte
);
535 next_syntax
= SYNTAX_WITH_FLAGS (next_c
);
536 if (((com2start
|| comnested
)
537 && SYNTAX_FLAGS_COMEND_SECOND (syntax
)
538 && SYNTAX_FLAGS_COMEND_FIRST (next_syntax
))
539 || ((com2end
|| comnested
)
540 && SYNTAX_FLAGS_COMSTART_SECOND (syntax
)
541 && comstyle
== SYNTAX_FLAGS_COMMENT_STYLE (syntax
)
542 && SYNTAX_FLAGS_COMSTART_FIRST (next_syntax
)))
544 /* UPDATE_SYNTAX_TABLE_FORWARD (next + 1); */
547 if (com2start
&& comstart_pos
== 0)
548 /* We're looking at a comment starter. But it might be a comment
549 ender as well (see snmp-mode). The first time we see one, we
550 need to consider it as a comment starter,
551 and the subsequent times as a comment ender. */
554 /* Turn a 2-char comment sequences into the appropriate syntax. */
559 /* Ignore comment starters of a different style. */
560 else if (code
== Scomment
561 && (comstyle
!= SYNTAX_FLAGS_COMMENT_STYLE (syntax
)
562 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
) != comnested
))
565 /* Ignore escaped characters, except comment-enders. */
566 if (code
!= Sendcomment
&& char_quoted (from
, from_byte
))
573 c
= (code
== Sstring_fence
? ST_STRING_STYLE
: ST_COMMENT_STYLE
);
575 /* Track parity of quotes. */
576 if (string_style
== -1)
577 /* Entering a string. */
579 else if (string_style
== c
)
580 /* Leaving the string. */
583 /* If we have two kinds of string delimiters.
584 There's no way to grok this scanning backwards. */
589 /* We've already checked that it is the relevant comstyle. */
590 if (string_style
!= -1 || comment_lossage
|| string_lossage
)
591 /* There are odd string quotes involved, so let's be careful.
592 Test case in Pascal: " { " a { " } */
597 /* Record best comment-starter so far. */
599 comstart_byte
= from_byte
;
601 else if (--nesting
<= 0)
602 /* nested comments have to be balanced, so we don't need to
603 keep looking for earlier ones. We use here the same (slightly
604 incorrect) reasoning as below: since it is followed by uniform
605 paired string quotes, this comment-start has to be outside of
606 strings, else the comment-end itself would be inside a string. */
611 if (SYNTAX_FLAGS_COMMENT_STYLE (syntax
) == comstyle
612 && (SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax
)
613 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
)) == comnested
)
614 /* This is the same style of comment ender as ours. */
619 /* Anything before that can't count because it would match
620 this comment-ender rather than ours. */
621 from
= stop
; /* Break out of the loop. */
623 else if (comstart_pos
!= 0 || c
!= '\n')
624 /* We're mixing comment styles here, so we'd better be careful.
625 The (comstart_pos != 0 || c != '\n') check is not quite correct
626 (we should just always set comment_lossage), but removing it
627 would imply that any multiline comment in C would go through
628 lossage, which seems overkill.
629 The failure should only happen in the rare cases such as
635 /* Assume a defun-start point is outside of strings. */
636 if (open_paren_in_column_0_is_defun_start
638 || (temp_byte
= dec_bytepos (from_byte
),
639 FETCH_CHAR (temp_byte
) == '\n')))
642 defun_start_byte
= from_byte
;
643 from
= stop
; /* Break out of the loop. */
652 if (comstart_pos
== 0)
655 from_byte
= comment_end_byte
;
656 UPDATE_SYNTAX_TABLE_FORWARD (comment_end
- 1);
658 /* If comstart_pos is set and we get here (ie. didn't jump to `lossage'
659 or `done'), then we've found the beginning of the non-nested comment. */
660 else if (1) /* !comnested */
663 from_byte
= comstart_byte
;
664 /* Globals are correct now. */
668 struct lisp_parse_state state
;
670 /* We had two kinds of string delimiters mixed up
671 together. Decode this going forwards.
672 Scan fwd from a known safe place (beginning-of-defun)
673 to the one in question; this records where we
674 last passed a comment starter. */
675 /* If we did not already find the defun start, find it now. */
676 if (defun_start
== 0)
678 defun_start
= find_defun_start (comment_end
, comment_end_byte
);
679 defun_start_byte
= find_start_value_byte
;
683 scan_sexps_forward (&state
,
684 defun_start
, defun_start_byte
,
685 comment_end
, -10000, 0, Qnil
, 0);
686 defun_start
= comment_end
;
687 if (state
.incomment
== (comnested
? 1 : -1)
688 && state
.comstyle
== comstyle
)
689 from
= state
.comstr_start
;
694 /* If comment_end is inside some other comment, maybe ours
695 is nested, so we need to try again from within the
696 surrounding comment. Example: { a (* " *) */
698 /* FIXME: We should advance by one or two chars. */
699 defun_start
= state
.comstr_start
+ 2;
700 defun_start_byte
= CHAR_TO_BYTE (defun_start
);
703 } while (defun_start
< comment_end
);
705 from_byte
= CHAR_TO_BYTE (from
);
706 UPDATE_SYNTAX_TABLE_FORWARD (from
- 1);
711 *bytepos_ptr
= from_byte
;
713 return (from
== comment_end
) ? -1 : from
;
716 DEFUN ("syntax-table-p", Fsyntax_table_p
, Ssyntax_table_p
, 1, 1, 0,
717 doc
: /* Return t if OBJECT is a syntax table.
718 Currently, any char-table counts as a syntax table. */)
722 if (CHAR_TABLE_P (object
)
723 && EQ (XCHAR_TABLE (object
)->purpose
, Qsyntax_table
))
729 check_syntax_table (obj
)
732 if (!(CHAR_TABLE_P (obj
)
733 && EQ (XCHAR_TABLE (obj
)->purpose
, Qsyntax_table
)))
734 wrong_type_argument (Qsyntax_table_p
, obj
);
737 DEFUN ("syntax-table", Fsyntax_table
, Ssyntax_table
, 0, 0, 0,
738 doc
: /* Return the current syntax table.
739 This is the one specified by the current buffer. */)
742 return current_buffer
->syntax_table
;
745 DEFUN ("standard-syntax-table", Fstandard_syntax_table
,
746 Sstandard_syntax_table
, 0, 0, 0,
747 doc
: /* Return the standard syntax table.
748 This is the one used for new buffers. */)
751 return Vstandard_syntax_table
;
754 DEFUN ("copy-syntax-table", Fcopy_syntax_table
, Scopy_syntax_table
, 0, 1, 0,
755 doc
: /* Construct a new syntax table and return it.
756 It is a copy of the TABLE, which defaults to the standard syntax table. */)
763 check_syntax_table (table
);
765 table
= Vstandard_syntax_table
;
767 copy
= Fcopy_sequence (table
);
769 /* Only the standard syntax table should have a default element.
770 Other syntax tables should inherit from parents instead. */
771 XCHAR_TABLE (copy
)->defalt
= Qnil
;
773 /* Copied syntax tables should all have parents.
774 If we copied one with no parent, such as the standard syntax table,
775 use the standard syntax table as the copy's parent. */
776 if (NILP (XCHAR_TABLE (copy
)->parent
))
777 Fset_char_table_parent (copy
, Vstandard_syntax_table
);
781 DEFUN ("set-syntax-table", Fset_syntax_table
, Sset_syntax_table
, 1, 1, 0,
782 doc
: /* Select a new syntax table for the current buffer.
783 One argument, a syntax table. */)
788 check_syntax_table (table
);
789 current_buffer
->syntax_table
= table
;
790 /* Indicate that this buffer now has a specified syntax table. */
791 idx
= PER_BUFFER_VAR_IDX (syntax_table
);
792 SET_PER_BUFFER_VALUE_P (current_buffer
, idx
, 1);
796 /* Convert a letter which signifies a syntax code
797 into the code it signifies.
798 This is used by modify-syntax-entry, and other things. */
800 unsigned char syntax_spec_code
[0400] =
801 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
802 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
803 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
804 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
805 (char) Swhitespace
, (char) Scomment_fence
, (char) Sstring
, 0377,
806 (char) Smath
, 0377, 0377, (char) Squote
,
807 (char) Sopen
, (char) Sclose
, 0377, 0377,
808 0377, (char) Swhitespace
, (char) Spunct
, (char) Scharquote
,
809 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
810 0377, 0377, 0377, 0377,
811 (char) Scomment
, 0377, (char) Sendcomment
, 0377,
812 (char) Sinherit
, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
813 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
814 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword
,
815 0377, 0377, 0377, 0377, (char) Sescape
, 0377, 0377, (char) Ssymbol
,
816 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
817 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
818 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword
,
819 0377, 0377, 0377, 0377, (char) Sstring_fence
, 0377, 0377, 0377
822 /* Indexed by syntax code, give the letter that describes it. */
824 char syntax_code_spec
[16] =
826 ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@',
830 /* Indexed by syntax code, give the object (cons of syntax code and
831 nil) to be stored in syntax table. Since these objects can be
832 shared among syntax tables, we generate them in advance. By
833 sharing objects, the function `describe-syntax' can give a more
835 static Lisp_Object Vsyntax_code_object
;
838 DEFUN ("char-syntax", Fchar_syntax
, Schar_syntax
, 1, 1, 0,
839 doc
: /* Return the syntax code of CHARACTER, described by a character.
840 For example, if CHARACTER is a word constituent,
841 the character `w' is returned.
842 The characters that correspond to various syntax codes
843 are listed in the documentation of `modify-syntax-entry'. */)
845 Lisp_Object character
;
848 gl_state
.current_syntax_table
= current_buffer
->syntax_table
;
850 gl_state
.use_global
= 0;
851 CHECK_NUMBER (character
);
852 char_int
= XINT (character
);
853 return make_number (syntax_code_spec
[(int) SYNTAX (char_int
)]);
856 DEFUN ("matching-paren", Fmatching_paren
, Smatching_paren
, 1, 1, 0,
857 doc
: /* Return the matching parenthesis of CHARACTER, or nil if none. */)
859 Lisp_Object character
;
862 gl_state
.current_syntax_table
= current_buffer
->syntax_table
;
863 gl_state
.use_global
= 0;
864 CHECK_NUMBER (character
);
865 char_int
= XINT (character
);
866 code
= SYNTAX (char_int
);
867 if (code
== Sopen
|| code
== Sclose
)
868 return SYNTAX_MATCH (char_int
);
872 DEFUN ("string-to-syntax", Fstring_to_syntax
, Sstring_to_syntax
, 1, 1, 0,
873 doc
: /* Convert a syntax specification STRING into syntax cell form.
874 STRING should be a string as it is allowed as argument of
875 `modify-syntax-entry'. Value is the equivalent cons cell
876 (CODE . MATCHING-CHAR) that can be used as value of a `syntax-table'
881 register unsigned char *p
;
882 register enum syntaxcode code
;
886 CHECK_STRING (string
);
888 p
= XSTRING (string
)->data
;
889 code
= (enum syntaxcode
) syntax_spec_code
[*p
++];
890 if (((int) code
& 0377) == 0377)
891 error ("invalid syntax description letter: %c", p
[-1]);
893 if (code
== Sinherit
)
899 int character
= (STRING_CHAR_AND_LENGTH
900 (p
, STRING_BYTES (XSTRING (string
)) - 1, len
));
901 XSETINT (match
, character
);
902 if (XFASTINT (match
) == ' ')
942 if (val
< XVECTOR (Vsyntax_code_object
)->size
&& NILP (match
))
943 return XVECTOR (Vsyntax_code_object
)->contents
[val
];
945 /* Since we can't use a shared object, let's make a new one. */
946 return Fcons (make_number (val
), match
);
949 /* I really don't know why this is interactive
950 help-form should at least be made useful whilst reading the second arg. */
951 DEFUN ("modify-syntax-entry", Fmodify_syntax_entry
, Smodify_syntax_entry
, 2, 3,
952 "cSet syntax for character: \nsSet syntax for %s to: ",
953 doc
: /* Set syntax for character CHAR according to string NEWENTRY.
954 The syntax is changed only for table SYNTAX_TABLE, which defaults to
955 the current buffer's syntax table.
956 CHAR may be a cons (MIN . MAX), in which case, syntaxes of all characters
957 in the range MIN and MAX are changed.
958 The first character of NEWENTRY should be one of the following:
959 Space or - whitespace syntax. w word constituent.
960 _ symbol constituent. . punctuation.
961 ( open-parenthesis. ) close-parenthesis.
962 " string quote. \\ escape.
963 $ paired delimiter. ' expression quote or prefix operator.
964 < comment starter. > comment ender.
965 / character-quote. @ inherit from `standard-syntax-table'.
966 | generic string fence. ! generic comment fence.
968 Only single-character comment start and end sequences are represented thus.
969 Two-character sequences are represented as described below.
970 The second character of NEWENTRY is the matching parenthesis,
971 used only if the first character is `(' or `)'.
972 Any additional characters are flags.
973 Defined flags are the characters 1, 2, 3, 4, b, p, and n.
974 1 means CHAR is the start of a two-char comment start sequence.
975 2 means CHAR is the second character of such a sequence.
976 3 means CHAR is the start of a two-char comment end sequence.
977 4 means CHAR is the second character of such a sequence.
979 There can be up to two orthogonal comment sequences. This is to support
980 language modes such as C++. By default, all comment sequences are of style
981 a, but you can set the comment sequence style to b (on the second character
982 of a comment-start, or the first character of a comment-end sequence) using
984 b means CHAR is part of comment sequence b.
985 n means CHAR is part of a nestable comment sequence.
987 p means CHAR is a prefix character for `backward-prefix-chars';
988 such characters are treated as whitespace when they occur
990 usage: (modify-syntax-entry CHAR NEWENTRY &optional SYNTAX-TABLE) */)
991 (c
, newentry
, syntax_table
)
992 Lisp_Object c
, newentry
, syntax_table
;
996 CHECK_CHARACTER (XCAR (c
));
997 CHECK_CHARACTER (XCDR (c
));
1000 CHECK_CHARACTER (c
);
1002 if (NILP (syntax_table
))
1003 syntax_table
= current_buffer
->syntax_table
;
1005 check_syntax_table (syntax_table
);
1007 newentry
= Fstring_to_syntax (newentry
);
1009 SET_RAW_SYNTAX_ENTRY_RANGE (syntax_table
, c
, newentry
);
1011 SET_RAW_SYNTAX_ENTRY (syntax_table
, XINT (c
), newentry
);
1015 /* Dump syntax table to buffer in human-readable format */
1017 DEFUN ("internal-describe-syntax-value", Finternal_describe_syntax_value
,
1018 Sinternal_describe_syntax_value
, 1, 1, 0,
1019 doc
: /* Insert a description of the internal syntax description SYNTAX at point. */)
1023 register enum syntaxcode code
;
1024 char desc
, start1
, start2
, end1
, end2
, prefix
, comstyle
, comnested
;
1026 Lisp_Object first
, match_lisp
, value
= syntax
;
1030 insert_string ("default");
1034 if (CHAR_TABLE_P (value
))
1036 insert_string ("deeper char-table ...");
1042 insert_string ("invalid");
1046 first
= XCAR (value
);
1047 match_lisp
= XCDR (value
);
1049 if (!INTEGERP (first
) || !(NILP (match_lisp
) || INTEGERP (match_lisp
)))
1051 insert_string ("invalid");
1055 code
= (enum syntaxcode
) (XINT (first
) & 0377);
1056 start1
= (XINT (first
) >> 16) & 1;
1057 start2
= (XINT (first
) >> 17) & 1;
1058 end1
= (XINT (first
) >> 18) & 1;
1059 end2
= (XINT (first
) >> 19) & 1;
1060 prefix
= (XINT (first
) >> 20) & 1;
1061 comstyle
= (XINT (first
) >> 21) & 1;
1062 comnested
= (XINT (first
) >> 22) & 1;
1064 if ((int) code
< 0 || (int) code
>= (int) Smax
)
1066 insert_string ("invalid");
1069 desc
= syntax_code_spec
[(int) code
];
1071 str
[0] = desc
, str
[1] = 0;
1074 if (NILP (match_lisp
))
1077 insert_char (XINT (match_lisp
));
1096 insert_string ("\twhich means: ");
1098 switch (SWITCH_ENUM_CAST (code
))
1101 insert_string ("whitespace"); break;
1103 insert_string ("punctuation"); break;
1105 insert_string ("word"); break;
1107 insert_string ("symbol"); break;
1109 insert_string ("open"); break;
1111 insert_string ("close"); break;
1113 insert_string ("prefix"); break;
1115 insert_string ("string"); break;
1117 insert_string ("math"); break;
1119 insert_string ("escape"); break;
1121 insert_string ("charquote"); break;
1123 insert_string ("comment"); break;
1125 insert_string ("endcomment"); break;
1127 insert_string ("inherit"); break;
1128 case Scomment_fence
:
1129 insert_string ("comment fence"); break;
1131 insert_string ("string fence"); break;
1133 insert_string ("invalid");
1137 if (!NILP (match_lisp
))
1139 insert_string (", matches ");
1140 insert_char (XINT (match_lisp
));
1144 insert_string (",\n\t is the first character of a comment-start sequence");
1146 insert_string (",\n\t is the second character of a comment-start sequence");
1149 insert_string (",\n\t is the first character of a comment-end sequence");
1151 insert_string (",\n\t is the second character of a comment-end sequence");
1153 insert_string (" (comment style b)");
1155 insert_string (" (nestable)");
1158 insert_string (",\n\t is a prefix character for `backward-prefix-chars'");
1163 int parse_sexp_ignore_comments
;
1165 Lisp_Object Vnext_word_boundary_function_table
;
1167 /* Return the position across COUNT words from FROM.
1168 If that many words cannot be found before the end of the buffer, return 0.
1169 COUNT negative means scan backward and stop at word beginning. */
1172 scan_words (from
, count
)
1173 register int from
, count
;
1175 register int beg
= BEGV
;
1176 register int end
= ZV
;
1177 register int from_byte
= CHAR_TO_BYTE (from
);
1178 register enum syntaxcode code
;
1184 SETUP_SYNTAX_TABLE (from
, count
);
1197 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1198 ch0
= FETCH_CHAR (from_byte
);
1199 code
= SYNTAX (ch0
);
1200 INC_BOTH (from
, from_byte
);
1201 if (words_include_escapes
1202 && (code
== Sescape
|| code
== Scharquote
))
1207 /* Now CH0 is a character which begins a word and FROM is the
1208 position of the next character. */
1209 func
= CHAR_TABLE_REF (Vnext_word_boundary_function_table
, ch0
);
1210 if (! NILP (Ffboundp (func
)))
1214 pos
= call2 (func
, make_number (from
- 1), make_number (end
));
1216 from_byte
= CHAR_TO_BYTE (from
);
1222 script
= CHAR_TABLE_REF (Vchar_script_table
, ch0
);
1225 if (from
== end
) break;
1226 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1227 ch1
= FETCH_CHAR (from_byte
);
1228 code
= SYNTAX (ch1
);
1230 && (! words_include_escapes
1231 || (code
!= Sescape
&& code
!= Scharquote
)))
1232 || ! EQ (CHAR_TABLE_REF (Vchar_script_table
, ch1
), script
))
1234 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
1264 func
= CHAR_TABLE_REF (Vnext_word_boundary_function_table
, ch1
);
1265 if (! NILP (Ffboundp (func
)))
1269 pos
= call2 (func
, make_number (from
), make_number (beg
));
1271 from_byte
= CHAR_TO_BYTE (from
);
1277 script
= CHAR_TABLE_REF (Vchar_script_table
, ch1
);
1284 temp_byte
= dec_bytepos (from_byte
);
1285 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
1286 ch0
= FETCH_CHAR (temp_byte
);
1287 code
= SYNTAX (ch0
);
1289 && (! words_include_escapes
1290 || (code
!= Sescape
&& code
!= Scharquote
)))
1291 || ! EQ (CHAR_TABLE_REF (Vchar_script_table
, ch0
), script
))
1293 DEC_BOTH (from
, from_byte
);
1305 DEFUN ("forward-word", Fforward_word
, Sforward_word
, 1, 1, "p",
1306 doc
: /* Move point forward ARG words (backward if ARG is negative).
1308 If an edge of the buffer or a field boundary is reached, point is left there
1309 and the function returns nil. Field boundaries are not noticed if
1310 `inhibit-field-text-motion' is non-nil. */)
1315 CHECK_NUMBER (count
);
1317 val
= orig_val
= scan_words (PT
, XINT (count
));
1319 val
= XINT (count
) > 0 ? ZV
: BEGV
;
1321 /* Avoid jumping out of an input field. */
1322 val
= XFASTINT (Fconstrain_to_field (make_number (val
), make_number (PT
),
1326 return val
== orig_val
? Qt
: Qnil
;
1329 Lisp_Object
skip_chars ();
1331 DEFUN ("skip-chars-forward", Fskip_chars_forward
, Sskip_chars_forward
, 1, 2, 0,
1332 doc
: /* Move point forward, stopping before a char not in STRING, or at pos LIM.
1333 STRING is like the inside of a `[...]' in a regular expression
1334 except that `]' is never special and `\\' quotes `^', `-' or `\\'
1335 (but not as the end of a range; quoting is never needed there).
1336 Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
1337 With arg "^a-zA-Z", skips nonletters stopping before first letter.
1338 Returns the distance traveled, either zero or positive. */)
1340 Lisp_Object string
, lim
;
1342 return skip_chars (1, 0, string
, lim
);
1345 DEFUN ("skip-chars-backward", Fskip_chars_backward
, Sskip_chars_backward
, 1, 2, 0,
1346 doc
: /* Move point backward, stopping after a char not in STRING, or at pos LIM.
1347 See `skip-chars-forward' for details.
1348 Returns the distance traveled, either zero or negative. */)
1350 Lisp_Object string
, lim
;
1352 return skip_chars (0, 0, string
, lim
);
1355 DEFUN ("skip-syntax-forward", Fskip_syntax_forward
, Sskip_syntax_forward
, 1, 2, 0,
1356 doc
: /* Move point forward across chars in specified syntax classes.
1357 SYNTAX is a string of syntax code characters.
1358 Stop before a char whose syntax is not in SYNTAX, or at position LIM.
1359 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1360 This function returns the distance traveled, either zero or positive. */)
1362 Lisp_Object syntax
, lim
;
1364 return skip_chars (1, 1, syntax
, lim
);
1367 DEFUN ("skip-syntax-backward", Fskip_syntax_backward
, Sskip_syntax_backward
, 1, 2, 0,
1368 doc
: /* Move point backward across chars in specified syntax classes.
1369 SYNTAX is a string of syntax code characters.
1370 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.
1371 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1372 This function returns the distance traveled, either zero or negative. */)
1374 Lisp_Object syntax
, lim
;
1376 return skip_chars (0, 1, syntax
, lim
);
1380 skip_chars (forwardp
, syntaxp
, string
, lim
)
1381 int forwardp
, syntaxp
;
1382 Lisp_Object string
, lim
;
1384 register unsigned int c
;
1385 unsigned char fastmap
[0400];
1386 /* If SYNTAXP is 0, STRING may contain multi-byte form of characters
1387 of which codes don't fit in FASTMAP. In that case, set the
1388 ranges of characters in CHAR_RANGES. */
1390 int n_char_ranges
= 0;
1392 register int i
, i_byte
;
1393 int multibyte
= !NILP (current_buffer
->enable_multibyte_characters
);
1394 int string_multibyte
;
1399 CHECK_STRING (string
);
1400 char_ranges
= (int *) alloca (XSTRING (string
)->size
* (sizeof (int)) * 2);
1401 string_multibyte
= STRING_MULTIBYTE (string
);
1402 str
= XSTRING (string
)->data
;
1403 size_byte
= STRING_BYTES (XSTRING (string
));
1405 /* Adjust the multibyteness of the string to that of the buffer. */
1406 if (multibyte
!= string_multibyte
)
1411 nbytes
= count_size_as_multibyte (XSTRING (string
)->data
,
1412 XSTRING (string
)->size
);
1414 nbytes
= XSTRING (string
)->size
;
1415 if (nbytes
!= size_byte
)
1417 str
= (unsigned char *) alloca (nbytes
);
1418 copy_text (XSTRING (string
)->data
, str
, size_byte
,
1419 string_multibyte
, multibyte
);
1425 XSETINT (lim
, forwardp
? ZV
: BEGV
);
1427 CHECK_NUMBER_COERCE_MARKER (lim
);
1429 /* In any case, don't allow scan outside bounds of buffer. */
1430 if (XINT (lim
) > ZV
)
1431 XSETFASTINT (lim
, ZV
);
1432 if (XINT (lim
) < BEGV
)
1433 XSETFASTINT (lim
, BEGV
);
1435 bzero (fastmap
, sizeof fastmap
);
1439 if (i_byte
< size_byte
1440 && XSTRING (string
)->data
[0] == '^')
1442 negate
= 1; i_byte
++;
1445 /* Find the characters specified and set their elements of fastmap.
1446 If syntaxp, each character counts as itself.
1447 Otherwise, handle backslashes and ranges specially. */
1449 if (size_byte
== XSTRING (string
)->size
)
1450 while (i_byte
< size_byte
)
1455 fastmap
[syntax_spec_code
[c
]] = 1;
1460 if (i_byte
== size_byte
)
1465 if (i_byte
< size_byte
1466 && str
[i_byte
] == '-')
1470 /* Skip over the dash. */
1473 if (i_byte
== size_byte
)
1476 /* Get the end of the range. */
1486 while (i_byte
< size_byte
)
1488 c
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, size_byte
-i_byte
, len
);
1492 fastmap
[syntax_spec_code
[c
& 0377]] = 1;
1497 if (i_byte
== size_byte
)
1500 c
= STRING_CHAR_AND_LENGTH (str
+i_byte
, size_byte
-i_byte
, len
);
1503 if (i_byte
< size_byte
1504 && str
[i_byte
] == '-')
1508 /* Skip over the dash. */
1511 if (i_byte
== size_byte
)
1514 /* Get the end of the range. */
1515 c2
=STRING_CHAR_AND_LENGTH (str
+ i_byte
, size_byte
-i_byte
, len
);
1518 if (ASCII_CHAR_P (c
))
1519 while (c
<= c2
&& c
< 0x80)
1523 char_ranges
[n_char_ranges
++] = c
;
1524 char_ranges
[n_char_ranges
++] = c2
;
1529 if (ASCII_CHAR_P (c
))
1533 char_ranges
[n_char_ranges
++] = c
;
1534 char_ranges
[n_char_ranges
++] = c
;
1540 /* If ^ was the first character, complement the fastmap. */
1542 for (i
= 0; i
< sizeof fastmap
; i
++)
1546 int start_point
= PT
;
1548 int pos_byte
= PT_BYTE
;
1553 SETUP_SYNTAX_TABLE (pos
, forwardp
? 1 : -1);
1558 if (pos
< XINT (lim
))
1559 while (fastmap
[(int) SYNTAX (FETCH_CHAR (pos_byte
))])
1561 /* Since we already checked for multibyteness,
1562 avoid using INC_BOTH which checks again. */
1565 if (pos
>= XINT (lim
))
1567 UPDATE_SYNTAX_TABLE_FORWARD (pos
);
1572 while (pos
< XINT (lim
)
1573 && fastmap
[(int) SYNTAX (FETCH_BYTE (pos
))])
1576 UPDATE_SYNTAX_TABLE_FORWARD (pos
);
1584 while (pos
> XINT (lim
))
1586 int savepos
= pos_byte
;
1587 /* Since we already checked for multibyteness,
1588 avoid using DEC_BOTH which checks again. */
1591 UPDATE_SYNTAX_TABLE_BACKWARD (pos
);
1592 if (!fastmap
[(int) SYNTAX (FETCH_CHAR (pos_byte
))])
1602 if (pos
> XINT (lim
))
1603 while (fastmap
[(int) SYNTAX (FETCH_BYTE (pos
- 1))])
1606 if (pos
<= XINT (lim
))
1608 UPDATE_SYNTAX_TABLE_BACKWARD (pos
- 1);
1618 while (pos
< XINT (lim
))
1620 c
= FETCH_MULTIBYTE_CHAR (pos_byte
);
1621 if (ASCII_CHAR_P (c
))
1628 /* If we are looking at a multibyte character,
1629 we must look up the character in the table
1630 CHAR_RANGES. If there's no data in the
1631 table, that character is not what we want to
1634 /* The following code do the right thing even if
1635 n_char_ranges is zero (i.e. no data in
1637 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1638 if (c
>= char_ranges
[i
] && c
<= char_ranges
[i
+ 1])
1640 if (!(negate
^ (i
< n_char_ranges
)))
1643 INC_BOTH (pos
, pos_byte
);
1646 while (pos
< XINT (lim
) && fastmap
[FETCH_BYTE (pos
)])
1652 while (pos
> XINT (lim
))
1654 int prev_pos_byte
= pos_byte
;
1656 DEC_POS (prev_pos_byte
);
1657 c
= FETCH_MULTIBYTE_CHAR (prev_pos_byte
);
1658 if (SINGLE_BYTE_CHAR_P (c
))
1665 /* See the comment in the previous similar code. */
1666 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1667 if (c
>= char_ranges
[i
] && c
<= char_ranges
[i
+ 1])
1669 if (!(negate
^ (i
< n_char_ranges
)))
1673 pos_byte
= prev_pos_byte
;
1676 while (pos
> XINT (lim
) && fastmap
[FETCH_BYTE (pos
- 1)])
1684 SET_PT_BOTH (pos
, pos_byte
);
1687 return make_number (PT
- start_point
);
1691 /* Jump over a comment, assuming we are at the beginning of one.
1692 FROM is the current position.
1693 FROM_BYTE is the bytepos corresponding to FROM.
1694 Do not move past STOP (a charpos).
1695 The comment over which we have to jump is of style STYLE
1696 (either SYNTAX_COMMENT_STYLE(foo) or ST_COMMENT_STYLE).
1697 NESTING should be positive to indicate the nesting at the beginning
1698 for nested comments and should be zero or negative else.
1699 ST_COMMENT_STYLE cannot be nested.
1700 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
1701 (or 0 If the search cannot start in the middle of a two-character).
1703 If successful, return 1 and store the charpos of the comment's end
1704 into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR.
1705 Else, return 0 and store the charpos STOP into *CHARPOS_PTR, the
1706 corresponding bytepos into *BYTEPOS_PTR and the current nesting
1707 (as defined for state.incomment) in *INCOMMENT_PTR.
1709 The comment end is the last character of the comment rather than the
1710 character just after the comment.
1712 Global syntax data is assumed to initially be valid for FROM and
1713 remains valid for forward search starting at the returned position. */
1716 forw_comment (from
, from_byte
, stop
, nesting
, style
, prev_syntax
,
1717 charpos_ptr
, bytepos_ptr
, incomment_ptr
)
1718 int from
, from_byte
, stop
;
1719 int nesting
, style
, prev_syntax
;
1720 int *charpos_ptr
, *bytepos_ptr
, *incomment_ptr
;
1723 register enum syntaxcode code
;
1724 register int syntax
;
1726 if (nesting
<= 0) nesting
= -1;
1728 /* Enter the loop in the middle so that we find
1729 a 2-char comment ender if we start in the middle of it. */
1730 syntax
= prev_syntax
;
1731 if (syntax
!= 0) goto forw_incomment
;
1737 *incomment_ptr
= nesting
;
1738 *charpos_ptr
= from
;
1739 *bytepos_ptr
= from_byte
;
1742 c
= FETCH_CHAR (from_byte
);
1743 syntax
= SYNTAX_WITH_FLAGS (c
);
1744 code
= syntax
& 0xff;
1745 if (code
== Sendcomment
1746 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
) == style
1747 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ?
1748 (nesting
> 0 && --nesting
== 0) : nesting
< 0))
1749 /* we have encountered a comment end of the same style
1750 as the comment sequence which began this comment
1753 if (code
== Scomment_fence
1754 && style
== ST_COMMENT_STYLE
)
1755 /* we have encountered a comment end of the same style
1756 as the comment sequence which began this comment
1761 && SYNTAX_FLAGS_COMMENT_NESTED (syntax
)
1762 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
) == style
)
1763 /* we have encountered a nested comment of the same style
1764 as the comment sequence which began this comment section */
1766 INC_BOTH (from
, from_byte
);
1767 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1770 if (from
< stop
&& SYNTAX_FLAGS_COMEND_FIRST (syntax
)
1771 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
) == style
1772 && (c1
= FETCH_CHAR (from_byte
),
1773 SYNTAX_COMEND_SECOND (c1
))
1774 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ||
1775 SYNTAX_COMMENT_NESTED (c1
)) ? nesting
> 0 : nesting
< 0))
1778 /* we have encountered a comment end of the same style
1779 as the comment sequence which began this comment
1784 INC_BOTH (from
, from_byte
);
1785 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1790 && SYNTAX_FLAGS_COMSTART_FIRST (syntax
)
1791 && (c1
= FETCH_CHAR (from_byte
),
1792 SYNTAX_COMMENT_STYLE (c1
) == style
1793 && SYNTAX_COMSTART_SECOND (c1
))
1794 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ||
1795 SYNTAX_COMMENT_NESTED (c1
)))
1796 /* we have encountered a nested comment of the same style
1797 as the comment sequence which began this comment
1800 INC_BOTH (from
, from_byte
);
1801 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1805 *charpos_ptr
= from
;
1806 *bytepos_ptr
= from_byte
;
1810 DEFUN ("forward-comment", Fforward_comment
, Sforward_comment
, 1, 1, 0,
1811 doc
: /* Move forward across up to N comments. If N is negative, move backward.
1812 Stop scanning if we find something other than a comment or whitespace.
1813 Set point to where scanning stops.
1814 If N comments are found as expected, with nothing except whitespace
1815 between them, return t; otherwise return nil. */)
1823 register enum syntaxcode code
;
1824 int comstyle
= 0; /* style of comment encountered */
1825 int comnested
= 0; /* whether the comment is nestable or not */
1828 int out_charpos
, out_bytepos
;
1831 CHECK_NUMBER (count
);
1832 count1
= XINT (count
);
1833 stop
= count1
> 0 ? ZV
: BEGV
;
1839 from_byte
= PT_BYTE
;
1841 SETUP_SYNTAX_TABLE (from
, count1
);
1850 SET_PT_BOTH (from
, from_byte
);
1854 c
= FETCH_CHAR (from_byte
);
1856 comstart_first
= SYNTAX_COMSTART_FIRST (c
);
1857 comnested
= SYNTAX_COMMENT_NESTED (c
);
1858 comstyle
= SYNTAX_COMMENT_STYLE (c
);
1859 INC_BOTH (from
, from_byte
);
1860 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1861 if (from
< stop
&& comstart_first
1862 && (c1
= FETCH_CHAR (from_byte
),
1863 SYNTAX_COMSTART_SECOND (c1
)))
1865 /* We have encountered a comment start sequence and we
1866 are ignoring all text inside comments. We must record
1867 the comment style this sequence begins so that later,
1868 only a comment end of the same style actually ends
1869 the comment section. */
1871 comstyle
= SYNTAX_COMMENT_STYLE (c1
);
1872 comnested
= comnested
|| SYNTAX_COMMENT_NESTED (c1
);
1873 INC_BOTH (from
, from_byte
);
1874 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1877 while (code
== Swhitespace
|| (code
== Sendcomment
&& c
== '\n'));
1879 if (code
== Scomment_fence
)
1880 comstyle
= ST_COMMENT_STYLE
;
1881 else if (code
!= Scomment
)
1884 DEC_BOTH (from
, from_byte
);
1885 SET_PT_BOTH (from
, from_byte
);
1888 /* We're at the start of a comment. */
1889 found
= forw_comment (from
, from_byte
, stop
, comnested
, comstyle
, 0,
1890 &out_charpos
, &out_bytepos
, &dummy
);
1891 from
= out_charpos
; from_byte
= out_bytepos
;
1895 SET_PT_BOTH (from
, from_byte
);
1898 INC_BOTH (from
, from_byte
);
1899 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1900 /* We have skipped one comment. */
1912 SET_PT_BOTH (BEGV
, BEGV_BYTE
);
1917 DEC_BOTH (from
, from_byte
);
1918 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
1919 quoted
= char_quoted (from
, from_byte
);
1920 c
= FETCH_CHAR (from_byte
);
1923 comnested
= SYNTAX_COMMENT_NESTED (c
);
1924 if (code
== Sendcomment
)
1925 comstyle
= SYNTAX_COMMENT_STYLE (c
);
1926 if (from
> stop
&& SYNTAX_COMEND_SECOND (c
)
1927 && prev_char_comend_first (from
, from_byte
)
1928 && !char_quoted (from
- 1, dec_bytepos (from_byte
)))
1930 /* We must record the comment style encountered so that
1931 later, we can match only the proper comment begin
1932 sequence of the same style. */
1933 DEC_BOTH (from
, from_byte
);
1935 /* Calling char_quoted, above, set up global syntax position
1936 at the new value of FROM. */
1937 c1
= FETCH_CHAR (from_byte
);
1938 comstyle
= SYNTAX_COMMENT_STYLE (c1
);
1939 comnested
= comnested
|| SYNTAX_COMMENT_NESTED (c1
);
1942 if (code
== Scomment_fence
)
1944 /* Skip until first preceding unquoted comment_fence. */
1945 int found
= 0, ini
= from
, ini_byte
= from_byte
;
1949 DEC_BOTH (from
, from_byte
);
1952 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
1953 c
= FETCH_CHAR (from_byte
);
1954 if (SYNTAX (c
) == Scomment_fence
1955 && !char_quoted (from
, from_byte
))
1963 from
= ini
; /* Set point to ini + 1. */
1964 from_byte
= ini_byte
;
1968 else if (code
== Sendcomment
)
1970 found
= back_comment (from
, from_byte
, stop
, comnested
, comstyle
,
1971 &out_charpos
, &out_bytepos
);
1975 /* This end-of-line is not an end-of-comment.
1976 Treat it like a whitespace.
1977 CC-mode (and maybe others) relies on this behavior. */
1981 /* Failure: we should go back to the end of this
1982 not-quite-endcomment. */
1983 if (SYNTAX(c
) != code
)
1984 /* It was a two-char Sendcomment. */
1985 INC_BOTH (from
, from_byte
);
1991 /* We have skipped one comment. */
1992 from
= out_charpos
, from_byte
= out_bytepos
;
1996 else if (code
!= Swhitespace
|| quoted
)
2000 INC_BOTH (from
, from_byte
);
2001 SET_PT_BOTH (from
, from_byte
);
2009 SET_PT_BOTH (from
, from_byte
);
2014 /* Return syntax code of character C if C is a single byte character
2015 or `multibyte_symbol_p' is zero. Otherwise, return Ssymbol. */
2017 #define SYNTAX_WITH_MULTIBYTE_CHECK(c) \
2018 ((SINGLE_BYTE_CHAR_P (c) || !multibyte_symbol_p) \
2019 ? SYNTAX (c) : Ssymbol)
2022 scan_lists (from
, count
, depth
, sexpflag
)
2024 int count
, depth
, sexpflag
;
2027 register int stop
= count
> 0 ? ZV
: BEGV
;
2032 register enum syntaxcode code
, temp_code
;
2033 int min_depth
= depth
; /* Err out if depth gets less than this. */
2034 int comstyle
= 0; /* style of comment encountered */
2035 int comnested
= 0; /* whether the comment is nestable or not */
2037 int last_good
= from
;
2040 int out_bytepos
, out_charpos
;
2042 int multibyte_symbol_p
= sexpflag
&& multibyte_syntax_as_symbol
;
2044 if (depth
> 0) min_depth
= 0;
2046 if (from
> ZV
) from
= ZV
;
2047 if (from
< BEGV
) from
= BEGV
;
2049 from_byte
= CHAR_TO_BYTE (from
);
2054 SETUP_SYNTAX_TABLE (from
, count
);
2059 int comstart_first
, prefix
;
2060 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2061 c
= FETCH_CHAR (from_byte
);
2062 code
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2063 comstart_first
= SYNTAX_COMSTART_FIRST (c
);
2064 comnested
= SYNTAX_COMMENT_NESTED (c
);
2065 comstyle
= SYNTAX_COMMENT_STYLE (c
);
2066 prefix
= SYNTAX_PREFIX (c
);
2067 if (depth
== min_depth
)
2069 INC_BOTH (from
, from_byte
);
2070 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2071 if (from
< stop
&& comstart_first
2072 && SYNTAX_COMSTART_SECOND (FETCH_CHAR (from_byte
))
2073 && parse_sexp_ignore_comments
)
2075 /* we have encountered a comment start sequence and we
2076 are ignoring all text inside comments. We must record
2077 the comment style this sequence begins so that later,
2078 only a comment end of the same style actually ends
2079 the comment section */
2081 c1
= FETCH_CHAR (from_byte
);
2082 comstyle
= SYNTAX_COMMENT_STYLE (c1
);
2083 comnested
= comnested
|| SYNTAX_COMMENT_NESTED (c1
);
2084 INC_BOTH (from
, from_byte
);
2085 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2091 switch (SWITCH_ENUM_CAST (code
))
2095 if (from
== stop
) goto lose
;
2096 INC_BOTH (from
, from_byte
);
2097 /* treat following character as a word constituent */
2100 if (depth
|| !sexpflag
) break;
2101 /* This word counts as a sexp; return at end of it. */
2104 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2106 /* Some compilers can't handle this inside the switch. */
2107 c
= FETCH_CHAR (from_byte
);
2108 temp
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2113 INC_BOTH (from
, from_byte
);
2114 if (from
== stop
) goto lose
;
2123 INC_BOTH (from
, from_byte
);
2127 case Scomment_fence
:
2128 comstyle
= ST_COMMENT_STYLE
;
2131 if (!parse_sexp_ignore_comments
) break;
2132 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2133 found
= forw_comment (from
, from_byte
, stop
,
2134 comnested
, comstyle
, 0,
2135 &out_charpos
, &out_bytepos
, &dummy
);
2136 from
= out_charpos
, from_byte
= out_bytepos
;
2143 INC_BOTH (from
, from_byte
);
2144 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2150 if (from
!= stop
&& c
== FETCH_CHAR (from_byte
))
2152 INC_BOTH (from
, from_byte
);
2162 if (!++depth
) goto done
;
2167 if (!--depth
) goto done
;
2168 if (depth
< min_depth
)
2169 Fsignal (Qscan_error
,
2170 Fcons (build_string ("Containing expression ends prematurely"),
2171 Fcons (make_number (last_good
),
2172 Fcons (make_number (from
), Qnil
))));
2177 temp_pos
= dec_bytepos (from_byte
);
2178 stringterm
= FETCH_CHAR (temp_pos
);
2181 if (from
>= stop
) goto lose
;
2182 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2183 c
= FETCH_CHAR (from_byte
);
2186 && SYNTAX_WITH_MULTIBYTE_CHECK (c
) == Sstring
)
2187 : SYNTAX_WITH_MULTIBYTE_CHECK (c
) == Sstring_fence
)
2190 /* Some compilers can't handle this inside the switch. */
2191 temp
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2196 INC_BOTH (from
, from_byte
);
2198 INC_BOTH (from
, from_byte
);
2200 INC_BOTH (from
, from_byte
);
2201 if (!depth
&& sexpflag
) goto done
;
2206 /* Reached end of buffer. Error if within object, return nil if between */
2207 if (depth
) goto lose
;
2212 /* End of object reached */
2221 DEC_BOTH (from
, from_byte
);
2222 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2223 c
= FETCH_CHAR (from_byte
);
2224 code
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2225 if (depth
== min_depth
)
2228 comnested
= SYNTAX_COMMENT_NESTED (c
);
2229 if (code
== Sendcomment
)
2230 comstyle
= SYNTAX_COMMENT_STYLE (c
);
2231 if (from
> stop
&& SYNTAX_COMEND_SECOND (c
)
2232 && prev_char_comend_first (from
, from_byte
)
2233 && parse_sexp_ignore_comments
)
2235 /* We must record the comment style encountered so that
2236 later, we can match only the proper comment begin
2237 sequence of the same style. */
2238 DEC_BOTH (from
, from_byte
);
2239 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2241 c1
= FETCH_CHAR (from_byte
);
2242 comstyle
= SYNTAX_COMMENT_STYLE (c1
);
2243 comnested
= comnested
|| SYNTAX_COMMENT_NESTED (c1
);
2246 /* Quoting turns anything except a comment-ender
2247 into a word character. Note that this cannot be true
2248 if we decremented FROM in the if-statement above. */
2249 if (code
!= Sendcomment
&& char_quoted (from
, from_byte
))
2251 else if (SYNTAX_PREFIX (c
))
2254 switch (SWITCH_ENUM_CAST (code
))
2260 if (depth
|| !sexpflag
) break;
2261 /* This word counts as a sexp; count object finished
2262 after passing it. */
2265 temp_pos
= from_byte
;
2266 if (! NILP (current_buffer
->enable_multibyte_characters
))
2270 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2271 c1
= FETCH_CHAR (temp_pos
);
2272 temp_code
= SYNTAX_WITH_MULTIBYTE_CHECK (c1
);
2273 /* Don't allow comment-end to be quoted. */
2274 if (temp_code
== Sendcomment
)
2276 quoted
= char_quoted (from
- 1, temp_pos
);
2279 DEC_BOTH (from
, from_byte
);
2280 temp_pos
= dec_bytepos (temp_pos
);
2281 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2283 c1
= FETCH_CHAR (temp_pos
);
2284 temp_code
= SYNTAX_WITH_MULTIBYTE_CHECK (c1
);
2285 if (! (quoted
|| temp_code
== Sword
2286 || temp_code
== Ssymbol
2287 || temp_code
== Squote
))
2289 DEC_BOTH (from
, from_byte
);
2296 temp_pos
= dec_bytepos (from_byte
);
2297 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2298 if (from
!= stop
&& c
== FETCH_CHAR (temp_pos
))
2299 DEC_BOTH (from
, from_byte
);
2308 if (!++depth
) goto done2
;
2313 if (!--depth
) goto done2
;
2314 if (depth
< min_depth
)
2315 Fsignal (Qscan_error
,
2316 Fcons (build_string ("Containing expression ends prematurely"),
2317 Fcons (make_number (last_good
),
2318 Fcons (make_number (from
), Qnil
))));
2322 if (!parse_sexp_ignore_comments
)
2324 found
= back_comment (from
, from_byte
, stop
, comnested
, comstyle
,
2325 &out_charpos
, &out_bytepos
);
2326 /* FIXME: if found == -1, then it really wasn't a comment-end.
2327 For single-char Sendcomment, we can't do much about it apart
2328 from skipping the char.
2329 For 2-char endcomments, we could try again, taking both
2330 chars as separate entities, but it's a lot of trouble
2331 for very little gain, so we don't bother either. -sm */
2333 from
= out_charpos
, from_byte
= out_bytepos
;
2336 case Scomment_fence
:
2340 DEC_BOTH (from
, from_byte
);
2341 if (from
== stop
) goto lose
;
2342 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2343 if (!char_quoted (from
, from_byte
)
2344 && (c
= FETCH_CHAR (from_byte
),
2345 SYNTAX_WITH_MULTIBYTE_CHECK (c
) == code
))
2348 if (code
== Sstring_fence
&& !depth
&& sexpflag
) goto done2
;
2352 stringterm
= FETCH_CHAR (from_byte
);
2355 if (from
== stop
) goto lose
;
2356 temp_pos
= from_byte
;
2357 if (! NILP (current_buffer
->enable_multibyte_characters
))
2361 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2362 if (!char_quoted (from
- 1, temp_pos
)
2363 && stringterm
== (c
= FETCH_CHAR (temp_pos
))
2364 && SYNTAX_WITH_MULTIBYTE_CHECK (c
) == Sstring
)
2366 DEC_BOTH (from
, from_byte
);
2368 DEC_BOTH (from
, from_byte
);
2369 if (!depth
&& sexpflag
) goto done2
;
2374 /* Reached start of buffer. Error if within object, return nil if between */
2375 if (depth
) goto lose
;
2386 XSETFASTINT (val
, from
);
2390 Fsignal (Qscan_error
,
2391 Fcons (build_string ("Unbalanced parentheses"),
2392 Fcons (make_number (last_good
),
2393 Fcons (make_number (from
), Qnil
))));
2398 DEFUN ("scan-lists", Fscan_lists
, Sscan_lists
, 3, 3, 0,
2399 doc
: /* Scan from character number FROM by COUNT lists.
2400 Returns the character number of the position thus found.
2402 If DEPTH is nonzero, paren depth begins counting from that value,
2403 only places where the depth in parentheses becomes zero
2404 are candidates for stopping; COUNT such places are counted.
2405 Thus, a positive value for DEPTH means go out levels.
2407 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2409 If the beginning or end of (the accessible part of) the buffer is reached
2410 and the depth is wrong, an error is signaled.
2411 If the depth is right but the count is not used up, nil is returned. */)
2412 (from
, count
, depth
)
2413 Lisp_Object from
, count
, depth
;
2415 CHECK_NUMBER (from
);
2416 CHECK_NUMBER (count
);
2417 CHECK_NUMBER (depth
);
2419 return scan_lists (XINT (from
), XINT (count
), XINT (depth
), 0);
2422 DEFUN ("scan-sexps", Fscan_sexps
, Sscan_sexps
, 2, 2, 0,
2423 doc
: /* Scan from character number FROM by COUNT balanced expressions.
2424 If COUNT is negative, scan backwards.
2425 Returns the character number of the position thus found.
2427 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2429 If the beginning or end of (the accessible part of) the buffer is reached
2430 in the middle of a parenthetical grouping, an error is signaled.
2431 If the beginning or end is reached between groupings
2432 but before count is used up, nil is returned. */)
2434 Lisp_Object from
, count
;
2436 CHECK_NUMBER (from
);
2437 CHECK_NUMBER (count
);
2439 return scan_lists (XINT (from
), XINT (count
), 0, 1);
2442 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars
, Sbackward_prefix_chars
,
2444 doc
: /* Move point backward over any number of chars with prefix syntax.
2445 This includes chars with "quote" or "prefix" syntax (' or p). */)
2450 int opoint_byte
= PT_BYTE
;
2452 int pos_byte
= PT_BYTE
;
2457 SET_PT_BOTH (opoint
, opoint_byte
);
2462 SETUP_SYNTAX_TABLE (pos
, -1);
2464 DEC_BOTH (pos
, pos_byte
);
2466 while (!char_quoted (pos
, pos_byte
)
2467 /* Previous statement updates syntax table. */
2468 && ((c
= FETCH_CHAR (pos_byte
), SYNTAX (c
) == Squote
)
2469 || SYNTAX_PREFIX (c
)))
2472 opoint_byte
= pos_byte
;
2475 DEC_BOTH (pos
, pos_byte
);
2478 SET_PT_BOTH (opoint
, opoint_byte
);
2483 /* Parse forward from FROM / FROM_BYTE to END,
2484 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
2485 and return a description of the state of the parse at END.
2486 If STOPBEFORE is nonzero, stop at the start of an atom.
2487 If COMMENTSTOP is 1, stop at the start of a comment.
2488 If COMMENTSTOP is -1, stop at the start or end of a comment,
2489 after the beginning of a string, or after the end of a string. */
2492 scan_sexps_forward (stateptr
, from
, from_byte
, end
, targetdepth
,
2493 stopbefore
, oldstate
, commentstop
)
2494 struct lisp_parse_state
*stateptr
;
2497 int end
, targetdepth
, stopbefore
;
2498 Lisp_Object oldstate
;
2501 struct lisp_parse_state state
;
2503 register enum syntaxcode code
;
2506 struct level
{ int last
, prev
; };
2507 struct level levelstart
[100];
2508 register struct level
*curlevel
= levelstart
;
2509 struct level
*endlevel
= levelstart
+ 100;
2510 register int depth
; /* Paren depth of current scanning location.
2511 level - levelstart equals this except
2512 when the depth becomes negative. */
2513 int mindepth
; /* Lowest DEPTH value seen. */
2514 int start_quoted
= 0; /* Nonzero means starting after a char quote */
2516 int prev_from
; /* Keep one character before FROM. */
2518 int prev_from_syntax
;
2519 int boundary_stop
= commentstop
== -1;
2522 int out_bytepos
, out_charpos
;
2526 prev_from_byte
= from_byte
;
2528 DEC_BOTH (prev_from
, prev_from_byte
);
2530 /* Use this macro instead of `from++'. */
2532 do { prev_from = from; \
2533 prev_from_byte = from_byte; \
2535 = SYNTAX_WITH_FLAGS (FETCH_CHAR (prev_from_byte)); \
2536 INC_BOTH (from, from_byte); \
2537 UPDATE_SYNTAX_TABLE_FORWARD (from); \
2543 if (NILP (oldstate
))
2546 state
.instring
= -1;
2547 state
.incomment
= 0;
2548 state
.comstyle
= 0; /* comment style a by default. */
2549 state
.comstr_start
= -1; /* no comment/string seen. */
2553 tem
= Fcar (oldstate
);
2559 oldstate
= Fcdr (oldstate
);
2560 oldstate
= Fcdr (oldstate
);
2561 oldstate
= Fcdr (oldstate
);
2562 tem
= Fcar (oldstate
);
2563 /* Check whether we are inside string_fence-style string: */
2564 state
.instring
= (!NILP (tem
)
2565 ? (INTEGERP (tem
) ? XINT (tem
) : ST_STRING_STYLE
)
2568 oldstate
= Fcdr (oldstate
);
2569 tem
= Fcar (oldstate
);
2570 state
.incomment
= (!NILP (tem
)
2571 ? (INTEGERP (tem
) ? XINT (tem
) : -1)
2574 oldstate
= Fcdr (oldstate
);
2575 tem
= Fcar (oldstate
);
2576 start_quoted
= !NILP (tem
);
2578 /* if the eighth element of the list is nil, we are in comment
2579 style a. If it is non-nil, we are in comment style b */
2580 oldstate
= Fcdr (oldstate
);
2581 oldstate
= Fcdr (oldstate
);
2582 tem
= Fcar (oldstate
);
2583 state
.comstyle
= NILP (tem
) ? 0 : (EQ (tem
, Qsyntax_table
)
2584 ? ST_COMMENT_STYLE
: 1);
2586 oldstate
= Fcdr (oldstate
);
2587 tem
= Fcar (oldstate
);
2588 state
.comstr_start
= NILP (tem
) ? -1 : XINT (tem
) ;
2589 oldstate
= Fcdr (oldstate
);
2590 tem
= Fcar (oldstate
);
2591 while (!NILP (tem
)) /* >= second enclosing sexps. */
2593 /* curlevel++->last ran into compiler bug on Apollo */
2594 curlevel
->last
= XINT (Fcar (tem
));
2595 if (++curlevel
== endlevel
)
2596 curlevel
--; /* error ("Nesting too deep for parser"); */
2597 curlevel
->prev
= -1;
2598 curlevel
->last
= -1;
2605 curlevel
->prev
= -1;
2606 curlevel
->last
= -1;
2608 SETUP_SYNTAX_TABLE (prev_from
, 1);
2609 prev_from_syntax
= SYNTAX_WITH_FLAGS (FETCH_CHAR (prev_from_byte
));
2610 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2612 /* Enter the loop at a place appropriate for initial state. */
2614 if (state
.incomment
)
2615 goto startincomment
;
2616 if (state
.instring
>= 0)
2618 nofence
= state
.instring
!= ST_STRING_STYLE
;
2620 goto startquotedinstring
;
2623 else if (start_quoted
)
2626 #if 0 /* This seems to be redundant with the identical code above. */
2627 SETUP_SYNTAX_TABLE (prev_from
, 1);
2628 prev_from_syntax
= SYNTAX_WITH_FLAGS (FETCH_CHAR (prev_from_byte
));
2629 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2635 code
= prev_from_syntax
& 0xff;
2637 if (code
== Scomment
)
2639 state
.comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax
);
2640 state
.incomment
= (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax
) ?
2642 state
.comstr_start
= prev_from
;
2644 else if (code
== Scomment_fence
)
2646 /* Record the comment style we have entered so that only
2647 the comment-end sequence of the same style actually
2648 terminates the comment section. */
2649 state
.comstyle
= ST_COMMENT_STYLE
;
2650 state
.incomment
= -1;
2651 state
.comstr_start
= prev_from
;
2654 else if (from
< end
)
2655 if (SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax
))
2656 if (c1
= FETCH_CHAR (from_byte
),
2657 SYNTAX_COMSTART_SECOND (c1
))
2658 /* Duplicate code to avoid a complex if-expression
2659 which causes trouble for the SGI compiler. */
2661 /* Record the comment style we have entered so that only
2662 the comment-end sequence of the same style actually
2663 terminates the comment section. */
2664 state
.comstyle
= SYNTAX_COMMENT_STYLE (FETCH_CHAR (from_byte
));
2665 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax
);
2666 comnested
= comnested
|| SYNTAX_COMMENT_NESTED (c1
);
2667 state
.incomment
= comnested
? 1 : -1;
2668 state
.comstr_start
= prev_from
;
2673 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax
))
2675 switch (SWITCH_ENUM_CAST (code
))
2679 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
2680 curlevel
->last
= prev_from
;
2682 if (from
== end
) goto endquoted
;
2685 /* treat following character as a word constituent */
2688 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
2689 curlevel
->last
= prev_from
;
2693 /* Some compilers can't handle this inside the switch. */
2694 temp
= SYNTAX (FETCH_CHAR (from_byte
));
2700 if (from
== end
) goto endquoted
;
2712 curlevel
->prev
= curlevel
->last
;
2716 if (commentstop
|| boundary_stop
) goto done
;
2718 /* The (from == BEGV) test was to enter the loop in the middle so
2719 that we find a 2-char comment ender even if we start in the
2720 middle of it. We don't want to do that if we're just at the
2721 beginning of the comment (think of (*) ... (*)). */
2722 found
= forw_comment (from
, from_byte
, end
,
2723 state
.incomment
, state
.comstyle
,
2724 (from
== BEGV
|| from
< state
.comstr_start
+ 3)
2725 ? 0 : prev_from_syntax
,
2726 &out_charpos
, &out_bytepos
, &state
.incomment
);
2727 from
= out_charpos
; from_byte
= out_bytepos
;
2728 /* Beware! prev_from and friends are invalid now.
2729 Luckily, the `done' doesn't use them and the INC_FROM
2730 sets them to a sane value without looking at them. */
2731 if (!found
) goto done
;
2733 state
.incomment
= 0;
2734 state
.comstyle
= 0; /* reset the comment style */
2735 if (boundary_stop
) goto done
;
2739 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
2741 /* curlevel++->last ran into compiler bug on Apollo */
2742 curlevel
->last
= prev_from
;
2743 if (++curlevel
== endlevel
)
2744 curlevel
--; /* error ("Nesting too deep for parser"); */
2745 curlevel
->prev
= -1;
2746 curlevel
->last
= -1;
2747 if (targetdepth
== depth
) goto done
;
2752 if (depth
< mindepth
)
2754 if (curlevel
!= levelstart
)
2756 curlevel
->prev
= curlevel
->last
;
2757 if (targetdepth
== depth
) goto done
;
2762 state
.comstr_start
= from
- 1;
2763 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
2764 curlevel
->last
= prev_from
;
2765 state
.instring
= (code
== Sstring
2766 ? (FETCH_CHAR (prev_from_byte
))
2768 if (boundary_stop
) goto done
;
2771 nofence
= state
.instring
!= ST_STRING_STYLE
;
2777 if (from
>= end
) goto done
;
2778 c
= FETCH_CHAR (from_byte
);
2779 /* Some compilers can't handle this inside the switch. */
2782 /* Check TEMP here so that if the char has
2783 a syntax-table property which says it is NOT
2784 a string character, it does not end the string. */
2785 if (nofence
&& c
== state
.instring
&& temp
== Sstring
)
2791 if (!nofence
) goto string_end
;
2796 startquotedinstring
:
2797 if (from
>= end
) goto endquoted
;
2803 state
.instring
= -1;
2804 curlevel
->prev
= curlevel
->last
;
2806 if (boundary_stop
) goto done
;
2815 stop
: /* Here if stopping before start of sexp. */
2816 from
= prev_from
; /* We have just fetched the char that starts it; */
2817 goto done
; /* but return the position before it. */
2822 state
.depth
= depth
;
2823 state
.mindepth
= mindepth
;
2824 state
.thislevelstart
= curlevel
->prev
;
2825 state
.prevlevelstart
2826 = (curlevel
== levelstart
) ? -1 : (curlevel
- 1)->last
;
2827 state
.location
= from
;
2828 state
.levelstarts
= Qnil
;
2829 while (--curlevel
>= levelstart
)
2830 state
.levelstarts
= Fcons (make_number (curlevel
->last
),
2837 DEFUN ("parse-partial-sexp", Fparse_partial_sexp
, Sparse_partial_sexp
, 2, 6, 0,
2838 doc
: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
2839 Parsing stops at TO or when certain criteria are met;
2840 point is set to where parsing stops.
2841 If fifth arg OLDSTATE is omitted or nil,
2842 parsing assumes that FROM is the beginning of a function.
2843 Value is a list of ten elements describing final state of parsing:
2845 1. character address of start of innermost containing list; nil if none.
2846 2. character address of start of last complete sexp terminated.
2847 3. non-nil if inside a string.
2848 (it is the character that will terminate the string,
2849 or t if the string should be terminated by a generic string delimiter.)
2850 4. nil if outside a comment, t if inside a non-nestable comment,
2851 else an integer (the current comment nesting).
2852 5. t if following a quote character.
2853 6. the minimum paren-depth encountered during this scan.
2854 7. t if in a comment of style b; symbol `syntax-table' if the comment
2855 should be terminated by a generic comment delimiter.
2856 8. character address of start of comment or string; nil if not in one.
2857 9. Intermediate data for continuation of parsing (subject to change).
2858 If third arg TARGETDEPTH is non-nil, parsing stops if the depth
2859 in parentheses becomes equal to TARGETDEPTH.
2860 Fourth arg STOPBEFORE non-nil means stop when come to
2861 any character that starts a sexp.
2862 Fifth arg OLDSTATE is a nine-element list like what this function returns.
2863 It is used to initialize the state of the parse. Elements number 1, 2, 6
2864 and 8 are ignored; you can leave off element 8 (the last) entirely.
2865 Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.
2866 If it is symbol `syntax-table', stop after the start of a comment or a
2867 string, or after end of a comment or a string. */)
2868 (from
, to
, targetdepth
, stopbefore
, oldstate
, commentstop
)
2869 Lisp_Object from
, to
, targetdepth
, stopbefore
, oldstate
, commentstop
;
2871 struct lisp_parse_state state
;
2874 if (!NILP (targetdepth
))
2876 CHECK_NUMBER (targetdepth
);
2877 target
= XINT (targetdepth
);
2880 target
= -100000; /* We won't reach this depth */
2882 validate_region (&from
, &to
);
2883 scan_sexps_forward (&state
, XINT (from
), CHAR_TO_BYTE (XINT (from
)),
2885 target
, !NILP (stopbefore
), oldstate
,
2887 ? 0 : (EQ (commentstop
, Qsyntax_table
) ? -1 : 1)));
2889 SET_PT (state
.location
);
2891 return Fcons (make_number (state
.depth
),
2892 Fcons (state
.prevlevelstart
< 0 ? Qnil
: make_number (state
.prevlevelstart
),
2893 Fcons (state
.thislevelstart
< 0 ? Qnil
: make_number (state
.thislevelstart
),
2894 Fcons (state
.instring
>= 0
2895 ? (state
.instring
== ST_STRING_STYLE
2896 ? Qt
: make_number (state
.instring
)) : Qnil
,
2897 Fcons (state
.incomment
< 0 ? Qt
:
2898 (state
.incomment
== 0 ? Qnil
:
2899 make_number (state
.incomment
)),
2900 Fcons (state
.quoted
? Qt
: Qnil
,
2901 Fcons (make_number (state
.mindepth
),
2902 Fcons ((state
.comstyle
2903 ? (state
.comstyle
== ST_COMMENT_STYLE
2904 ? Qsyntax_table
: Qt
) :
2906 Fcons (((state
.incomment
2907 || (state
.instring
>= 0))
2908 ? make_number (state
.comstr_start
)
2910 Fcons (state
.levelstarts
, Qnil
))))))))));
2919 /* This has to be done here, before we call Fmake_char_table. */
2920 Qsyntax_table
= intern ("syntax-table");
2921 staticpro (&Qsyntax_table
);
2923 /* Intern this now in case it isn't already done.
2924 Setting this variable twice is harmless.
2925 But don't staticpro it here--that is done in alloc.c. */
2926 Qchar_table_extra_slots
= intern ("char-table-extra-slots");
2928 /* Create objects which can be shared among syntax tables. */
2929 Vsyntax_code_object
= Fmake_vector (make_number (Smax
), Qnil
);
2930 for (i
= 0; i
< XVECTOR (Vsyntax_code_object
)->size
; i
++)
2931 XVECTOR (Vsyntax_code_object
)->contents
[i
]
2932 = Fcons (make_number (i
), Qnil
);
2934 /* Now we are ready to set up this property, so we can
2935 create syntax tables. */
2936 Fput (Qsyntax_table
, Qchar_table_extra_slots
, make_number (0));
2938 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Swhitespace
];
2940 Vstandard_syntax_table
= Fmake_char_table (Qsyntax_table
, temp
);
2942 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Sword
];
2943 for (i
= 'a'; i
<= 'z'; i
++)
2944 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
2945 for (i
= 'A'; i
<= 'Z'; i
++)
2946 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
2947 for (i
= '0'; i
<= '9'; i
++)
2948 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
2950 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '$', temp
);
2951 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '%', temp
);
2953 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '(',
2954 Fcons (make_number (Sopen
), make_number (')')));
2955 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ')',
2956 Fcons (make_number (Sclose
), make_number ('(')));
2957 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '[',
2958 Fcons (make_number (Sopen
), make_number (']')));
2959 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ']',
2960 Fcons (make_number (Sclose
), make_number ('[')));
2961 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '{',
2962 Fcons (make_number (Sopen
), make_number ('}')));
2963 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '}',
2964 Fcons (make_number (Sclose
), make_number ('{')));
2965 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '"',
2966 Fcons (make_number ((int) Sstring
), Qnil
));
2967 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\\',
2968 Fcons (make_number ((int) Sescape
), Qnil
));
2970 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Ssymbol
];
2971 for (i
= 0; i
< 10; i
++)
2973 c
= "_-+*/&|<>="[i
];
2974 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, c
, temp
);
2977 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Spunct
];
2978 for (i
= 0; i
< 12; i
++)
2980 c
= ".,;:?!#@~^'`"[i
];
2981 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, c
, temp
);
2984 /* All multibyte characters have syntax `word' by default. */
2985 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Sword
];
2986 char_table_set_range (Vstandard_syntax_table
, 0x80, MAX_CHAR
, temp
);
2992 Qsyntax_table_p
= intern ("syntax-table-p");
2993 staticpro (&Qsyntax_table_p
);
2995 staticpro (&Vsyntax_code_object
);
2997 Qscan_error
= intern ("scan-error");
2998 staticpro (&Qscan_error
);
2999 Fput (Qscan_error
, Qerror_conditions
,
3000 Fcons (Qscan_error
, Fcons (Qerror
, Qnil
)));
3001 Fput (Qscan_error
, Qerror_message
,
3002 build_string ("Scan error"));
3004 DEFVAR_BOOL ("parse-sexp-ignore-comments", &parse_sexp_ignore_comments
,
3005 doc
: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
3007 DEFVAR_BOOL ("parse-sexp-lookup-properties", &parse_sexp_lookup_properties
,
3008 doc
: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3009 Otherwise, that text property is simply ignored.
3010 See the info node `(elisp)Syntax Properties' for a description of the
3011 `syntax-table' property. */);
3013 words_include_escapes
= 0;
3014 DEFVAR_BOOL ("words-include-escapes", &words_include_escapes
,
3015 doc
: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
3017 DEFVAR_BOOL ("multibyte-syntax-as-symbol", &multibyte_syntax_as_symbol
,
3018 doc
: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
3019 multibyte_syntax_as_symbol
= 0;
3021 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3022 &open_paren_in_column_0_is_defun_start
,
3023 doc
: /* Non-nil means an open paren in column 0 denotes the start of a defun. */);
3024 open_paren_in_column_0_is_defun_start
= 1;
3026 DEFVAR_LISP ("next-word-boundary-function-table",
3027 &Vnext_word_boundary_function_table
,
3029 Char table of functions to search for the next word boundary.
3030 Each function is called with two arguments; POS and LIMIT.
3031 POS and LIMIT are character positions in the current buffer.
3033 If POS is less than LIMIT, POS is at the first character of a word,
3034 and the return value of a function is a position after the last
3035 character of that word.
3037 If POS is not less than LIMIT, POS is at the last character of a word,
3038 and the return value of a function is a position at the first
3039 character of that word.
3041 In both cases, LIMIT bounds the search. */);
3042 Vnext_word_boundary_function_table
= Fmake_char_table (Qnil
, Qnil
);
3044 defsubr (&Ssyntax_table_p
);
3045 defsubr (&Ssyntax_table
);
3046 defsubr (&Sstandard_syntax_table
);
3047 defsubr (&Scopy_syntax_table
);
3048 defsubr (&Sset_syntax_table
);
3049 defsubr (&Schar_syntax
);
3050 defsubr (&Smatching_paren
);
3051 defsubr (&Sstring_to_syntax
);
3052 defsubr (&Smodify_syntax_entry
);
3053 defsubr (&Sinternal_describe_syntax_value
);
3055 defsubr (&Sforward_word
);
3057 defsubr (&Sskip_chars_forward
);
3058 defsubr (&Sskip_chars_backward
);
3059 defsubr (&Sskip_syntax_forward
);
3060 defsubr (&Sskip_syntax_backward
);
3062 defsubr (&Sforward_comment
);
3063 defsubr (&Sscan_lists
);
3064 defsubr (&Sscan_sexps
);
3065 defsubr (&Sbackward_prefix_chars
);
3066 defsubr (&Sparse_partial_sexp
);