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. */
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
.start
= gl_state
.b_property
;
138 gl_state
.stop
= gl_state
.e_property
;
139 gl_state
.forward_i
= interval_of (charpos
, object
);
140 i
= gl_state
.backward_i
= gl_state
.forward_i
;
141 gl_state
.left_ok
= gl_state
.right_ok
= 1;
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
- 1 - 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 (!gl_state
.left_ok
|| oldi
->position
!= INTERVAL_LAST_POS (i
))
179 gl_state
.right_ok
= 1; /* Invalidate the other end. */
180 gl_state
.forward_i
= i
;
181 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
184 else if (charpos
>= INTERVAL_LAST_POS (i
)) /* Move right. */
187 error ("Error in syntax_table logic for intervals ->");
188 /* Update the interval. */
189 i
= update_interval (i
, charpos
);
190 if (!gl_state
.right_ok
|| i
->position
!= INTERVAL_LAST_POS (oldi
))
193 gl_state
.left_ok
= 1; /* Invalidate the other end. */
194 gl_state
.backward_i
= i
;
195 gl_state
.b_property
= i
->position
- 1 - gl_state
.offset
;
198 else if (count
> 0 ? gl_state
.right_ok
: gl_state
.left_ok
)
200 /* We do not need to recalculate tmp_table. */
201 tmp_table
= gl_state
.old_prop
;
205 tmp_table
= textget (i
->plist
, Qsyntax_table
);
208 invalidate
= !EQ (tmp_table
, gl_state
.old_prop
); /* Need to invalidate? */
210 if (invalidate
) /* Did not get to adjacent interval. */
211 { /* with the same table => */
212 /* invalidate the old range. */
215 gl_state
.backward_i
= i
;
216 gl_state
.left_ok
= 1; /* Invalidate the other end. */
217 gl_state
.b_property
= i
->position
- 1 - gl_state
.offset
;
221 gl_state
.forward_i
= i
;
222 gl_state
.right_ok
= 1; /* Invalidate the other end. */
223 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
227 gl_state
.current_syntax_table
= tmp_table
;
228 gl_state
.old_prop
= tmp_table
;
229 if (EQ (Fsyntax_table_p (tmp_table
), Qt
))
231 gl_state
.use_global
= 0;
233 else if (CONSP (tmp_table
))
235 gl_state
.use_global
= 1;
236 gl_state
.global_code
= tmp_table
;
240 gl_state
.use_global
= 0;
241 gl_state
.current_syntax_table
= current_buffer
->syntax_table
;
244 while (!NULL_INTERVAL_P (i
))
246 if (cnt
&& !EQ (tmp_table
, textget (i
->plist
, Qsyntax_table
)))
249 gl_state
.right_ok
= 0;
251 gl_state
.left_ok
= 0;
254 else if (cnt
== INTERVALS_AT_ONCE
)
257 gl_state
.right_ok
= 1;
259 gl_state
.left_ok
= 1;
263 i
= count
> 0 ? next_interval (i
) : previous_interval (i
);
265 if (NULL_INTERVAL_P (i
))
266 { /* This property goes to the end. */
268 gl_state
.e_property
= gl_state
.stop
;
270 gl_state
.b_property
= gl_state
.start
;
276 gl_state
.e_property
= i
->position
- gl_state
.offset
;
277 gl_state
.forward_i
= i
;
281 gl_state
.b_property
= i
->position
+ LENGTH (i
) - 1 - gl_state
.offset
;
282 gl_state
.backward_i
= i
;
287 /* Returns TRUE if char at CHARPOS is quoted.
288 Global syntax-table data should be set up already to be good at CHARPOS
289 or after. On return global syntax data is good for lookup at CHARPOS. */
292 char_quoted (charpos
, bytepos
)
293 register int charpos
, bytepos
;
295 register enum syntaxcode code
;
296 register int beg
= BEGV
;
297 register int quoted
= 0;
300 DEC_BOTH (charpos
, bytepos
);
302 while (bytepos
>= beg
)
304 UPDATE_SYNTAX_TABLE_BACKWARD (charpos
);
305 code
= SYNTAX (FETCH_CHAR (bytepos
));
306 if (! (code
== Scharquote
|| code
== Sescape
))
309 DEC_BOTH (charpos
, bytepos
);
313 UPDATE_SYNTAX_TABLE (orig
);
317 /* Return the bytepos one character after BYTEPOS.
318 We assume that BYTEPOS is not at the end of the buffer. */
321 inc_bytepos (bytepos
)
324 if (NILP (current_buffer
->enable_multibyte_characters
))
331 /* Return the bytepos one character before BYTEPOS.
332 We assume that BYTEPOS is not at the start of the buffer. */
335 dec_bytepos (bytepos
)
338 if (NILP (current_buffer
->enable_multibyte_characters
))
345 /* Return a defun-start position before before POS and not too far before.
346 It should be the last one before POS, or nearly the last.
348 When open_paren_in_column_0_is_defun_start is nonzero,
349 the beginning of every line is treated as a defun-start.
351 We record the information about where the scan started
352 and what its result was, so that another call in the same area
353 can return the same value very quickly.
355 There is no promise at which position the global syntax data is
356 valid on return from the subroutine, so the caller should explicitly
357 update the global data. */
360 find_defun_start (pos
, pos_byte
)
363 int opoint
= PT
, opoint_byte
= PT_BYTE
;
365 /* Use previous finding, if it's valid and applies to this inquiry. */
366 if (current_buffer
== find_start_buffer
367 /* Reuse the defun-start even if POS is a little farther on.
368 POS might be in the next defun, but that's ok.
369 Our value may not be the best possible, but will still be usable. */
370 && pos
<= find_start_pos
+ 1000
371 && pos
>= find_start_value
372 && BEGV
== find_start_begv
373 && MODIFF
== find_start_modiff
)
374 return find_start_value
;
376 /* Back up to start of line. */
377 scan_newline (pos
, pos_byte
, BEGV
, BEGV_BYTE
, -1, 1);
379 /* We optimize syntax-table lookup for rare updates. Thus we accept
380 only those `^\s(' which are good in global _and_ text-property
382 gl_state
.current_syntax_table
= current_buffer
->syntax_table
;
383 gl_state
.use_global
= 0;
384 if (open_paren_in_column_0_is_defun_start
)
388 /* Open-paren at start of line means we may have found our
390 if (SYNTAX (FETCH_CHAR (PT_BYTE
)) == Sopen
)
392 SETUP_SYNTAX_TABLE (PT
+ 1, -1); /* Try again... */
393 if (SYNTAX (FETCH_CHAR (PT_BYTE
)) == Sopen
)
395 /* Now fallback to the default value. */
396 gl_state
.current_syntax_table
= current_buffer
->syntax_table
;
397 gl_state
.use_global
= 0;
399 /* Move to beg of previous line. */
400 scan_newline (PT
, PT_BYTE
, BEGV
, BEGV_BYTE
, -2, 1);
404 /* Record what we found, for the next try. */
405 find_start_value
= PT
;
406 find_start_value_byte
= PT_BYTE
;
407 find_start_buffer
= current_buffer
;
408 find_start_modiff
= MODIFF
;
409 find_start_begv
= BEGV
;
410 find_start_pos
= pos
;
412 TEMP_SET_PT_BOTH (opoint
, opoint_byte
);
414 return find_start_value
;
417 /* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
420 prev_char_comend_first (pos
, pos_byte
)
425 DEC_BOTH (pos
, pos_byte
);
426 UPDATE_SYNTAX_TABLE_BACKWARD (pos
);
427 c
= FETCH_CHAR (pos_byte
);
428 val
= SYNTAX_COMEND_FIRST (c
);
429 UPDATE_SYNTAX_TABLE_FORWARD (pos
+ 1);
433 /* Return the SYNTAX_COMSTART_FIRST of the character before POS, POS_BYTE. */
436 * prev_char_comstart_first (pos, pos_byte)
441 * DEC_BOTH (pos, pos_byte);
442 * UPDATE_SYNTAX_TABLE_BACKWARD (pos);
443 * c = FETCH_CHAR (pos_byte);
444 * val = SYNTAX_COMSTART_FIRST (c);
445 * UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
449 /* Checks whether charpos FROM is at the end of a comment.
450 FROM_BYTE is the bytepos corresponding to FROM.
451 Do not move back before STOP.
453 Return a positive value if we find a comment ending at FROM/FROM_BYTE;
456 If successful, store the charpos of the comment's beginning
457 into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR.
459 Global syntax data remains valid for backward search starting at
460 the returned value (or at FROM, if the search was not successful). */
463 back_comment (from
, from_byte
, stop
, comnested
, comstyle
, charpos_ptr
, bytepos_ptr
)
464 int from
, from_byte
, stop
;
465 int comnested
, comstyle
;
466 int *charpos_ptr
, *bytepos_ptr
;
468 /* Look back, counting the parity of string-quotes,
469 and recording the comment-starters seen.
470 When we reach a safe place, assume that's not in a string;
471 then step the main scan to the earliest comment-starter seen
472 an even number of string quotes away from the safe place.
474 OFROM[I] is position of the earliest comment-starter seen
475 which is I+2X quotes from the comment-end.
476 PARITY is current parity of quotes from the comment end. */
477 int string_style
= -1; /* Presumed outside of any string. */
478 int string_lossage
= 0;
479 /* Not a real lossage: indicates that we have passed a matching comment
480 starter plus an non-matching comment-ender, meaning that any matching
481 comment-starter we might see later could be a false positive (hidden
482 inside another comment).
483 Test case: { a (* b } c (* d *) */
484 int comment_lossage
= 0;
485 int comment_end
= from
;
486 int comment_end_byte
= from_byte
;
487 int comstart_pos
= 0;
489 /* Place where the containing defun starts,
490 or 0 if we didn't come across it yet. */
492 int defun_start_byte
= 0;
493 register enum syntaxcode code
;
494 int nesting
= 1; /* current comment nesting */
498 /* FIXME: A }} comment-ender style leads to incorrect behavior
499 in the case of {{ c }}} because we ignore the last two chars which are
500 assumed to be comment-enders although they aren't. */
502 /* At beginning of range to scan, we're outside of strings;
503 that determines quote parity to the comment-end. */
506 int temp_byte
, prev_syntax
;
507 int com2start
, com2end
;
509 /* Move back and examine a character. */
510 DEC_BOTH (from
, from_byte
);
511 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
513 prev_syntax
= syntax
;
514 c
= FETCH_CHAR (from_byte
);
515 syntax
= SYNTAX_WITH_FLAGS (c
);
518 /* Check for 2-char comment markers. */
519 com2start
= (SYNTAX_FLAGS_COMSTART_FIRST (syntax
)
520 && SYNTAX_FLAGS_COMSTART_SECOND (prev_syntax
)
521 && comstyle
== SYNTAX_FLAGS_COMMENT_STYLE (prev_syntax
)
522 && (SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax
)
523 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
)) == comnested
);
524 com2end
= (SYNTAX_FLAGS_COMEND_FIRST (syntax
)
525 && SYNTAX_FLAGS_COMEND_SECOND (prev_syntax
));
527 /* Nasty cases with overlapping 2-char comment markers:
528 - snmp-mode: -- c -- foo -- c --
536 /* If a 2-char comment sequence partly overlaps with another,
537 we don't try to be clever. */
538 if (from
> stop
&& (com2end
|| com2start
))
540 int next
= from
, next_byte
= from_byte
, next_c
, next_syntax
;
541 DEC_BOTH (next
, next_byte
);
542 UPDATE_SYNTAX_TABLE_BACKWARD (next
);
543 next_c
= FETCH_CHAR (next_byte
);
544 next_syntax
= SYNTAX_WITH_FLAGS (next_c
);
545 if (((com2start
|| comnested
)
546 && SYNTAX_FLAGS_COMEND_SECOND (syntax
)
547 && SYNTAX_FLAGS_COMEND_FIRST (next_syntax
))
548 || ((com2end
|| comnested
)
549 && SYNTAX_FLAGS_COMSTART_SECOND (syntax
)
550 && comstyle
== SYNTAX_FLAGS_COMMENT_STYLE (syntax
)
551 && SYNTAX_FLAGS_COMSTART_FIRST (next_syntax
)))
553 /* UPDATE_SYNTAX_TABLE_FORWARD (next + 1); */
556 if (com2start
&& comstart_pos
== 0)
557 /* We're looking at a comment starter. But it might be a comment
558 ender as well (see snmp-mode). The first time we see one, we
559 need to consider it as a comment starter,
560 and the subsequent times as a comment ender. */
563 /* Turn a 2-char comment sequences into the appropriate syntax. */
568 /* Ignore comment starters of a different style. */
569 else if (code
== Scomment
570 && (comstyle
!= SYNTAX_FLAGS_COMMENT_STYLE (syntax
)
571 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
) != comnested
))
574 /* Ignore escaped characters, except comment-enders. */
575 if (code
!= Sendcomment
&& char_quoted (from
, from_byte
))
582 c
= (code
== Sstring_fence
? ST_STRING_STYLE
: ST_COMMENT_STYLE
);
584 /* Track parity of quotes. */
585 if (string_style
== -1)
586 /* Entering a string. */
588 else if (string_style
== c
)
589 /* Leaving the string. */
592 /* If we have two kinds of string delimiters.
593 There's no way to grok this scanning backwards. */
598 /* We've already checked that it is the relevant comstyle. */
599 if (string_style
!= -1 || comment_lossage
|| string_lossage
)
600 /* There are odd string quotes involved, so let's be careful.
601 Test case in Pascal: " { " a { " } */
606 /* Record best comment-starter so far. */
608 comstart_byte
= from_byte
;
610 else if (--nesting
<= 0)
611 /* nested comments have to be balanced, so we don't need to
612 keep looking for earlier ones. We use here the same (slightly
613 incorrect) reasoning as below: since it is followed by uniform
614 paired string quotes, this comment-start has to be outside of
615 strings, else the comment-end itself would be inside a string. */
620 if (SYNTAX_FLAGS_COMMENT_STYLE (syntax
) == comstyle
621 && (SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax
)
622 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
)) == comnested
)
623 /* This is the same style of comment ender as ours. */
628 /* Anything before that can't count because it would match
629 this comment-ender rather than ours. */
630 from
= stop
; /* Break out of the loop. */
632 else if (comstart_pos
!= 0 || c
!= '\n')
633 /* We're mixing comment styles here, so we'd better be careful.
634 The (comstart_pos != 0 || c != '\n') check is not quite correct
635 (we should just always set comment_lossage), but removing it
636 would imply that any multiline comment in C would go through
637 lossage, which seems overkill.
638 The failure should only happen in the rare cases such as
644 /* Assume a defun-start point is outside of strings. */
645 if (open_paren_in_column_0_is_defun_start
647 || (temp_byte
= dec_bytepos (from_byte
),
648 FETCH_CHAR (temp_byte
) == '\n')))
651 defun_start_byte
= from_byte
;
652 from
= stop
; /* Break out of the loop. */
661 if (comstart_pos
== 0)
664 from_byte
= comment_end_byte
;
665 UPDATE_SYNTAX_TABLE_FORWARD (comment_end
- 1);
667 /* If comstart_pos is set and we get here (ie. didn't jump to `lossage'
668 or `done'), then we've found the beginning of the non-nested comment. */
669 else if (1) /* !comnested */
672 from_byte
= comstart_byte
;
673 /* Globals are correct now. */
677 struct lisp_parse_state state
;
679 /* We had two kinds of string delimiters mixed up
680 together. Decode this going forwards.
681 Scan fwd from a known safe place (beginning-of-defun)
682 to the one in question; this records where we
683 last passed a comment starter. */
684 /* If we did not already find the defun start, find it now. */
685 if (defun_start
== 0)
687 defun_start
= find_defun_start (comment_end
, comment_end_byte
);
688 defun_start_byte
= find_start_value_byte
;
692 scan_sexps_forward (&state
,
693 defun_start
, defun_start_byte
,
694 comment_end
, -10000, 0, Qnil
, 0);
695 defun_start
= comment_end
;
696 if (state
.incomment
== (comnested
? 1 : -1)
697 && state
.comstyle
== comstyle
)
698 from
= state
.comstr_start
;
703 /* If comment_end is inside some other comment, maybe ours
704 is nested, so we need to try again from within the
705 surrounding comment. Example: { a (* " *) */
707 /* FIXME: We should advance by one or two chars. */
708 defun_start
= state
.comstr_start
+ 2;
709 defun_start_byte
= CHAR_TO_BYTE (defun_start
);
712 } while (defun_start
< comment_end
);
714 from_byte
= CHAR_TO_BYTE (from
);
715 UPDATE_SYNTAX_TABLE_FORWARD (from
- 1);
720 *bytepos_ptr
= from_byte
;
722 return (from
== comment_end
) ? -1 : from
;
725 DEFUN ("syntax-table-p", Fsyntax_table_p
, Ssyntax_table_p
, 1, 1, 0,
726 "Return t if OBJECT is a syntax table.\n\
727 Currently, any char-table counts as a syntax table.")
731 if (CHAR_TABLE_P (object
)
732 && EQ (XCHAR_TABLE (object
)->purpose
, Qsyntax_table
))
738 check_syntax_table (obj
)
741 if (!(CHAR_TABLE_P (obj
)
742 && EQ (XCHAR_TABLE (obj
)->purpose
, Qsyntax_table
)))
743 wrong_type_argument (Qsyntax_table_p
, obj
);
746 DEFUN ("syntax-table", Fsyntax_table
, Ssyntax_table
, 0, 0, 0,
747 "Return the current syntax table.\n\
748 This is the one specified by the current buffer.")
751 return current_buffer
->syntax_table
;
754 DEFUN ("standard-syntax-table", Fstandard_syntax_table
,
755 Sstandard_syntax_table
, 0, 0, 0,
756 "Return the standard syntax table.\n\
757 This is the one used for new buffers.")
760 return Vstandard_syntax_table
;
763 DEFUN ("copy-syntax-table", Fcopy_syntax_table
, Scopy_syntax_table
, 0, 1, 0,
764 "Construct a new syntax table and return it.\n\
765 It is a copy of the TABLE, which defaults to the standard syntax table.")
772 check_syntax_table (table
);
774 table
= Vstandard_syntax_table
;
776 copy
= Fcopy_sequence (table
);
778 /* Only the standard syntax table should have a default element.
779 Other syntax tables should inherit from parents instead. */
780 XCHAR_TABLE (copy
)->defalt
= Qnil
;
782 /* Copied syntax tables should all have parents.
783 If we copied one with no parent, such as the standard syntax table,
784 use the standard syntax table as the copy's parent. */
785 if (NILP (XCHAR_TABLE (copy
)->parent
))
786 Fset_char_table_parent (copy
, Vstandard_syntax_table
);
790 DEFUN ("set-syntax-table", Fset_syntax_table
, Sset_syntax_table
, 1, 1, 0,
791 "Select a new syntax table for the current buffer.\n\
792 One argument, a syntax table.")
797 check_syntax_table (table
);
798 current_buffer
->syntax_table
= table
;
799 /* Indicate that this buffer now has a specified syntax table. */
800 idx
= PER_BUFFER_VAR_IDX (syntax_table
);
801 SET_PER_BUFFER_VALUE_P (current_buffer
, idx
, 1);
805 /* Convert a letter which signifies a syntax code
806 into the code it signifies.
807 This is used by modify-syntax-entry, and other things. */
809 unsigned char syntax_spec_code
[0400] =
810 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
811 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
812 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
813 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
814 (char) Swhitespace
, (char) Scomment_fence
, (char) Sstring
, 0377,
815 (char) Smath
, 0377, 0377, (char) Squote
,
816 (char) Sopen
, (char) Sclose
, 0377, 0377,
817 0377, (char) Swhitespace
, (char) Spunct
, (char) Scharquote
,
818 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
819 0377, 0377, 0377, 0377,
820 (char) Scomment
, 0377, (char) Sendcomment
, 0377,
821 (char) Sinherit
, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
822 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
823 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword
,
824 0377, 0377, 0377, 0377, (char) Sescape
, 0377, 0377, (char) Ssymbol
,
825 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
826 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
827 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword
,
828 0377, 0377, 0377, 0377, (char) Sstring_fence
, 0377, 0377, 0377
831 /* Indexed by syntax code, give the letter that describes it. */
833 char syntax_code_spec
[16] =
835 ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@',
839 /* Indexed by syntax code, give the object (cons of syntax code and
840 nil) to be stored in syntax table. Since these objects can be
841 shared among syntax tables, we generate them in advance. By
842 sharing objects, the function `describe-syntax' can give a more
844 static Lisp_Object Vsyntax_code_object
;
847 /* Look up the value for CHARACTER in syntax table TABLE's parent
848 and its parents. SYNTAX_ENTRY calls this, when TABLE itself has nil
849 for CHARACTER. It's actually used only when not compiled with GCC. */
852 syntax_parent_lookup (table
, character
)
860 table
= XCHAR_TABLE (table
)->parent
;
864 value
= XCHAR_TABLE (table
)->contents
[character
];
870 DEFUN ("char-syntax", Fchar_syntax
, Schar_syntax
, 1, 1, 0,
871 "Return the syntax code of CHARACTER, described by a character.\n\
872 For example, if CHARACTER is a word constituent,\n\
873 the character `w' is returned.\n\
874 The characters that correspond to various syntax codes\n\
875 are listed in the documentation of `modify-syntax-entry'.")
877 Lisp_Object character
;
880 gl_state
.current_syntax_table
= current_buffer
->syntax_table
;
882 gl_state
.use_global
= 0;
883 CHECK_NUMBER (character
, 0);
884 char_int
= XINT (character
);
885 return make_number (syntax_code_spec
[(int) SYNTAX (char_int
)]);
888 DEFUN ("matching-paren", Fmatching_paren
, Smatching_paren
, 1, 1, 0,
889 "Return the matching parenthesis of CHARACTER, or nil if none.")
891 Lisp_Object character
;
894 gl_state
.current_syntax_table
= current_buffer
->syntax_table
;
895 gl_state
.use_global
= 0;
896 CHECK_NUMBER (character
, 0);
897 char_int
= XINT (character
);
898 code
= SYNTAX (char_int
);
899 if (code
== Sopen
|| code
== Sclose
)
900 return SYNTAX_MATCH (char_int
);
904 DEFUN ("string-to-syntax", Fstring_to_syntax
, Sstring_to_syntax
, 1, 1, 0,
905 "Convert a syntax specification STRING into syntax cell form.\n\
906 STRING should be a string as it is allowed as argument of\n\
907 `modify-syntax-entry'. Value is the equivalent cons cell\n\
908 \(CODE . MATCHING-CHAR) that can be used as value of a `syntax-table'\n\
913 register unsigned char *p
;
914 register enum syntaxcode code
;
918 CHECK_STRING (string
, 0);
920 p
= XSTRING (string
)->data
;
921 code
= (enum syntaxcode
) syntax_spec_code
[*p
++];
922 if (((int) code
& 0377) == 0377)
923 error ("invalid syntax description letter: %c", p
[-1]);
925 if (code
== Sinherit
)
931 int character
= (STRING_CHAR_AND_LENGTH
932 (p
, STRING_BYTES (XSTRING (string
)) - 1, len
));
933 XSETINT (match
, character
);
934 if (XFASTINT (match
) == ' ')
974 if (val
< XVECTOR (Vsyntax_code_object
)->size
&& NILP (match
))
975 return XVECTOR (Vsyntax_code_object
)->contents
[val
];
977 /* Since we can't use a shared object, let's make a new one. */
978 return Fcons (make_number (val
), match
);
981 /* This comment supplies the doc string for modify-syntax-entry,
982 for make-docfile to see. We cannot put this in the real DEFUN
983 due to limits in the Unix cpp.
985 DEFUN ("modify-syntax-entry", foo, bar, 2, 3, 0,
986 "Set syntax for character CHAR according to string S.\n\
987 The syntax is changed only for table TABLE, which defaults to\n\
988 the current buffer's syntax table.\n\
989 The first character of S should be one of the following:\n\
990 Space or - whitespace syntax. w word constituent.\n\
991 _ symbol constituent. . punctuation.\n\
992 ( open-parenthesis. ) close-parenthesis.\n\
993 \" string quote. \\ escape.\n\
994 $ paired delimiter. ' expression quote or prefix operator.\n\
995 < comment starter. > comment ender.\n\
996 / character-quote. @ inherit from `standard-syntax-table'.\n\
997 | generic string fence. ! generic comment fence.\n\
999 Only single-character comment start and end sequences are represented thus.\n\
1000 Two-character sequences are represented as described below.\n\
1001 The second character of S is the matching parenthesis,\n\
1002 used only if the first character is `(' or `)'.\n\
1003 Any additional characters are flags.\n\
1004 Defined flags are the characters 1, 2, 3, 4, b, p, and n.\n\
1005 1 means CHAR is the start of a two-char comment start sequence.\n\
1006 2 means CHAR is the second character of such a sequence.\n\
1007 3 means CHAR is the start of a two-char comment end sequence.\n\
1008 4 means CHAR is the second character of such a sequence.\n\
1010 There can be up to two orthogonal comment sequences. This is to support\n\
1011 language modes such as C++. By default, all comment sequences are of style\n\
1012 a, but you can set the comment sequence style to b (on the second character\n\
1013 of a comment-start, or the first character of a comment-end sequence) using\n\
1015 b means CHAR is part of comment sequence b.\n\
1016 n means CHAR is part of a nestable comment sequence.\n\
1018 p means CHAR is a prefix character for `backward-prefix-chars';\n\
1019 such characters are treated as whitespace when they occur\n\
1020 between expressions.")
1024 DEFUN ("modify-syntax-entry", Fmodify_syntax_entry
, Smodify_syntax_entry
, 2, 3,
1025 /* I really don't know why this is interactive
1026 help-form should at least be made useful whilst reading the second arg
1028 "cSet syntax for character: \nsSet syntax for %s to: ",
1029 0 /* See immediately above */)
1030 (c
, newentry
, syntax_table
)
1031 Lisp_Object c
, newentry
, syntax_table
;
1033 CHECK_NUMBER (c
, 0);
1035 if (NILP (syntax_table
))
1036 syntax_table
= current_buffer
->syntax_table
;
1038 check_syntax_table (syntax_table
);
1040 SET_RAW_SYNTAX_ENTRY (syntax_table
, XINT (c
), Fstring_to_syntax (newentry
));
1044 /* Dump syntax table to buffer in human-readable format */
1047 describe_syntax (value
)
1050 register enum syntaxcode code
;
1051 char desc
, start1
, start2
, end1
, end2
, prefix
, comstyle
, comnested
;
1053 Lisp_Object first
, match_lisp
;
1055 Findent_to (make_number (16), make_number (1));
1059 insert_string ("default\n");
1063 if (CHAR_TABLE_P (value
))
1065 insert_string ("deeper char-table ...\n");
1071 insert_string ("invalid\n");
1075 first
= XCAR (value
);
1076 match_lisp
= XCDR (value
);
1078 if (!INTEGERP (first
) || !(NILP (match_lisp
) || INTEGERP (match_lisp
)))
1080 insert_string ("invalid\n");
1084 code
= (enum syntaxcode
) (XINT (first
) & 0377);
1085 start1
= (XINT (first
) >> 16) & 1;
1086 start2
= (XINT (first
) >> 17) & 1;
1087 end1
= (XINT (first
) >> 18) & 1;
1088 end2
= (XINT (first
) >> 19) & 1;
1089 prefix
= (XINT (first
) >> 20) & 1;
1090 comstyle
= (XINT (first
) >> 21) & 1;
1091 comnested
= (XINT (first
) >> 22) & 1;
1093 if ((int) code
< 0 || (int) code
>= (int) Smax
)
1095 insert_string ("invalid");
1098 desc
= syntax_code_spec
[(int) code
];
1100 str
[0] = desc
, str
[1] = 0;
1103 if (NILP (match_lisp
))
1106 insert_char (XINT (match_lisp
));
1125 insert_string ("\twhich means: ");
1127 switch (SWITCH_ENUM_CAST (code
))
1130 insert_string ("whitespace"); break;
1132 insert_string ("punctuation"); break;
1134 insert_string ("word"); break;
1136 insert_string ("symbol"); break;
1138 insert_string ("open"); break;
1140 insert_string ("close"); break;
1142 insert_string ("prefix"); break;
1144 insert_string ("string"); break;
1146 insert_string ("math"); break;
1148 insert_string ("escape"); break;
1150 insert_string ("charquote"); break;
1152 insert_string ("comment"); break;
1154 insert_string ("endcomment"); break;
1156 insert_string ("inherit"); break;
1157 case Scomment_fence
:
1158 insert_string ("comment fence"); break;
1160 insert_string ("string fence"); break;
1162 insert_string ("invalid");
1166 if (!NILP (match_lisp
))
1168 insert_string (", matches ");
1169 insert_char (XINT (match_lisp
));
1173 insert_string (",\n\t is the first character of a comment-start sequence");
1175 insert_string (",\n\t is the second character of a comment-start sequence");
1178 insert_string (",\n\t is the first character of a comment-end sequence");
1180 insert_string (",\n\t is the second character of a comment-end sequence");
1182 insert_string (" (comment style b)");
1184 insert_string (" (nestable)");
1187 insert_string (",\n\t is a prefix character for `backward-prefix-chars'");
1189 insert_string ("\n");
1193 describe_syntax_1 (vector
)
1196 struct buffer
*old
= current_buffer
;
1197 set_buffer_internal (XBUFFER (Vstandard_output
));
1198 describe_vector (vector
, Qnil
, describe_syntax
, 0, Qnil
, Qnil
, (int *) 0, 0);
1199 while (! NILP (XCHAR_TABLE (vector
)->parent
))
1201 vector
= XCHAR_TABLE (vector
)->parent
;
1202 insert_string ("\nThe parent syntax table is:");
1203 describe_vector (vector
, Qnil
, describe_syntax
, 0, Qnil
, Qnil
,
1207 call0 (intern ("help-mode"));
1208 set_buffer_internal (old
);
1212 DEFUN ("describe-syntax", Fdescribe_syntax
, Sdescribe_syntax
, 0, 0, "",
1213 "Describe the syntax specifications in the syntax table.\n\
1214 The descriptions are inserted in a buffer, which is then displayed.")
1217 internal_with_output_to_temp_buffer
1218 ("*Help*", describe_syntax_1
, current_buffer
->syntax_table
);
1223 int parse_sexp_ignore_comments
;
1225 /* Return the position across COUNT words from FROM.
1226 If that many words cannot be found before the end of the buffer, return 0.
1227 COUNT negative means scan backward and stop at word beginning. */
1230 scan_words (from
, count
)
1231 register int from
, count
;
1233 register int beg
= BEGV
;
1234 register int end
= ZV
;
1235 register int from_byte
= CHAR_TO_BYTE (from
);
1236 register enum syntaxcode code
;
1242 SETUP_SYNTAX_TABLE (from
, count
);
1253 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1254 ch0
= FETCH_CHAR (from_byte
);
1255 code
= SYNTAX (ch0
);
1256 INC_BOTH (from
, from_byte
);
1257 if (words_include_escapes
1258 && (code
== Sescape
|| code
== Scharquote
))
1263 /* Now CH0 is a character which begins a word and FROM is the
1264 position of the next character. */
1267 if (from
== end
) break;
1268 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1269 ch1
= FETCH_CHAR (from_byte
);
1270 code
= SYNTAX (ch1
);
1271 if (!(words_include_escapes
1272 && (code
== Sescape
|| code
== Scharquote
)))
1273 if (code
!= Sword
|| WORD_BOUNDARY_P (ch0
, ch1
))
1275 INC_BOTH (from
, from_byte
);
1289 DEC_BOTH (from
, from_byte
);
1290 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
1291 ch1
= FETCH_CHAR (from_byte
);
1292 code
= SYNTAX (ch1
);
1293 if (words_include_escapes
1294 && (code
== Sescape
|| code
== Scharquote
))
1299 /* Now CH1 is a character which ends a word and FROM is the
1307 temp_byte
= dec_bytepos (from_byte
);
1308 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
1309 ch0
= FETCH_CHAR (temp_byte
);
1310 code
= SYNTAX (ch0
);
1311 if (!(words_include_escapes
1312 && (code
== Sescape
|| code
== Scharquote
)))
1313 if (code
!= Sword
|| WORD_BOUNDARY_P (ch0
, ch1
))
1315 DEC_BOTH (from
, from_byte
);
1326 DEFUN ("forward-word", Fforward_word
, Sforward_word
, 1, 1, "p",
1327 "Move point forward ARG words (backward if ARG is negative).\n\
1328 Normally returns t.\n\
1329 If an edge of the buffer or a field boundary is reached, point is left there\n\
1330 and the function returns nil. Field boundaries are not noticed if\n\
1331 `inhibit-field-text-motion' is non-nil.")
1336 CHECK_NUMBER (count
, 0);
1338 val
= orig_val
= scan_words (PT
, XINT (count
));
1340 val
= XINT (count
) > 0 ? ZV
: BEGV
;
1342 /* Avoid jumping out of an input field. */
1343 val
= XFASTINT (Fconstrain_to_field (make_number (val
), make_number (PT
),
1347 return val
== orig_val
? Qt
: Qnil
;
1350 Lisp_Object
skip_chars ();
1352 DEFUN ("skip-chars-forward", Fskip_chars_forward
, Sskip_chars_forward
, 1, 2, 0,
1353 "Move point forward, stopping before a char not in STRING, or at pos LIM.\n\
1354 STRING is like the inside of a `[...]' in a regular expression\n\
1355 except that `]' is never special and `\\' quotes `^', `-' or `\\'\n\
1356 (but not as the end of a range; quoting is never needed there).\n\
1357 Thus, with arg \"a-zA-Z\", this skips letters stopping before first nonletter.\n\
1358 With arg \"^a-zA-Z\", skips nonletters stopping before first letter.\n\
1359 Returns the distance traveled, either zero or positive.")
1361 Lisp_Object string
, lim
;
1363 return skip_chars (1, 0, string
, lim
);
1366 DEFUN ("skip-chars-backward", Fskip_chars_backward
, Sskip_chars_backward
, 1, 2, 0,
1367 "Move point backward, stopping after a char not in STRING, or at pos LIM.\n\
1368 See `skip-chars-forward' for details.\n\
1369 Returns the distance traveled, either zero or negative.")
1371 Lisp_Object string
, lim
;
1373 return skip_chars (0, 0, string
, lim
);
1376 DEFUN ("skip-syntax-forward", Fskip_syntax_forward
, Sskip_syntax_forward
, 1, 2, 0,
1377 "Move point forward across chars in specified syntax classes.\n\
1378 SYNTAX is a string of syntax code characters.\n\
1379 Stop before a char whose syntax is not in SYNTAX, or at position LIM.\n\
1380 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.\n\
1381 This function returns the distance traveled, either zero or positive.")
1383 Lisp_Object syntax
, lim
;
1385 return skip_chars (1, 1, syntax
, lim
);
1388 DEFUN ("skip-syntax-backward", Fskip_syntax_backward
, Sskip_syntax_backward
, 1, 2, 0,
1389 "Move point backward across chars in specified syntax classes.\n\
1390 SYNTAX is a string of syntax code characters.\n\
1391 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.\n\
1392 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.\n\
1393 This function returns the distance traveled, either zero or negative.")
1395 Lisp_Object syntax
, lim
;
1397 return skip_chars (0, 1, syntax
, lim
);
1401 skip_chars (forwardp
, syntaxp
, string
, lim
)
1402 int forwardp
, syntaxp
;
1403 Lisp_Object string
, lim
;
1405 register unsigned int c
;
1406 unsigned char fastmap
[0400];
1407 /* If SYNTAXP is 0, STRING may contain multi-byte form of characters
1408 of which codes don't fit in FASTMAP. In that case, set the
1409 ranges of characters in CHAR_RANGES. */
1411 int n_char_ranges
= 0;
1413 register int i
, i_byte
;
1414 int multibyte
= !NILP (current_buffer
->enable_multibyte_characters
);
1415 int string_multibyte
;
1420 CHECK_STRING (string
, 0);
1421 char_ranges
= (int *) alloca (XSTRING (string
)->size
* (sizeof (int)) * 2);
1422 string_multibyte
= STRING_MULTIBYTE (string
);
1423 str
= XSTRING (string
)->data
;
1424 size_byte
= STRING_BYTES (XSTRING (string
));
1426 /* Adjust the multibyteness of the string to that of the buffer. */
1427 if (multibyte
!= string_multibyte
)
1432 nbytes
= count_size_as_multibyte (XSTRING (string
)->data
,
1433 XSTRING (string
)->size
);
1435 nbytes
= XSTRING (string
)->size
;
1436 if (nbytes
!= size_byte
)
1438 str
= (unsigned char *) alloca (nbytes
);
1439 copy_text (XSTRING (string
)->data
, str
, size_byte
,
1440 string_multibyte
, multibyte
);
1446 XSETINT (lim
, forwardp
? ZV
: BEGV
);
1448 CHECK_NUMBER_COERCE_MARKER (lim
, 0);
1450 /* In any case, don't allow scan outside bounds of buffer. */
1451 if (XINT (lim
) > ZV
)
1452 XSETFASTINT (lim
, ZV
);
1453 if (XINT (lim
) < BEGV
)
1454 XSETFASTINT (lim
, BEGV
);
1456 bzero (fastmap
, sizeof fastmap
);
1460 if (i_byte
< size_byte
1461 && XSTRING (string
)->data
[0] == '^')
1463 negate
= 1; i_byte
++;
1466 /* Find the characters specified and set their elements of fastmap.
1467 If syntaxp, each character counts as itself.
1468 Otherwise, handle backslashes and ranges specially. */
1470 while (i_byte
< size_byte
)
1472 c
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, size_byte
- i_byte
, len
);
1476 fastmap
[syntax_spec_code
[c
& 0377]] = 1;
1481 if (i_byte
== size_byte
)
1484 c
= STRING_CHAR_AND_LENGTH (str
+i_byte
, size_byte
-i_byte
, len
);
1487 if (i_byte
< size_byte
1488 && str
[i_byte
] == '-')
1492 /* Skip over the dash. */
1495 if (i_byte
== size_byte
)
1498 /* Get the end of the range. */
1499 c2
=STRING_CHAR_AND_LENGTH (str
+i_byte
, size_byte
-i_byte
, len
);
1502 if (SINGLE_BYTE_CHAR_P (c
))
1504 if (! SINGLE_BYTE_CHAR_P (c2
))
1506 /* Handle a range starting with a character of
1507 less than 256, and ending with a character of
1508 not less than 256. Split that into two
1509 ranges, the low one ending at 0377, and the
1510 high one starting at the smallest character
1511 in the charset of C2 and ending at C2. */
1512 int charset
= CHAR_CHARSET (c2
);
1513 int c1
= MAKE_CHAR (charset
, 0, 0);
1515 char_ranges
[n_char_ranges
++] = c1
;
1516 char_ranges
[n_char_ranges
++] = c2
;
1525 else if (c
<= c2
) /* Both C and C2 are multibyte char. */
1527 char_ranges
[n_char_ranges
++] = c
;
1528 char_ranges
[n_char_ranges
++] = c2
;
1533 if (SINGLE_BYTE_CHAR_P (c
))
1537 char_ranges
[n_char_ranges
++] = c
;
1538 char_ranges
[n_char_ranges
++] = c
;
1544 /* If ^ was the first character, complement the fastmap. */
1546 for (i
= 0; i
< sizeof fastmap
; i
++)
1550 int start_point
= PT
;
1552 int pos_byte
= PT_BYTE
;
1557 SETUP_SYNTAX_TABLE (pos
, forwardp
? 1 : -1);
1562 if (pos
< XINT (lim
))
1563 while (fastmap
[(int) SYNTAX (FETCH_CHAR (pos_byte
))])
1565 /* Since we already checked for multibyteness,
1566 avoid using INC_BOTH which checks again. */
1569 if (pos
>= XINT (lim
))
1571 UPDATE_SYNTAX_TABLE_FORWARD (pos
);
1576 while (pos
< XINT (lim
)
1577 && fastmap
[(int) SYNTAX (FETCH_BYTE (pos
))])
1580 UPDATE_SYNTAX_TABLE_FORWARD (pos
);
1588 while (pos
> XINT (lim
))
1590 int savepos
= pos_byte
;
1591 /* Since we already checked for multibyteness,
1592 avoid using DEC_BOTH which checks again. */
1595 UPDATE_SYNTAX_TABLE_BACKWARD (pos
);
1596 if (!fastmap
[(int) SYNTAX (FETCH_CHAR (pos_byte
))])
1606 if (pos
> XINT (lim
))
1607 while (fastmap
[(int) SYNTAX (FETCH_BYTE (pos
- 1))])
1610 if (pos
<= XINT (lim
))
1612 UPDATE_SYNTAX_TABLE_BACKWARD (pos
- 1);
1622 while (pos
< XINT (lim
))
1624 c
= FETCH_MULTIBYTE_CHAR (pos_byte
);
1625 if (SINGLE_BYTE_CHAR_P (c
))
1632 /* If we are looking at a multibyte character,
1633 we must look up the character in the table
1634 CHAR_RANGES. If there's no data in the
1635 table, that character is not what we want to
1638 /* The following code do the right thing even if
1639 n_char_ranges is zero (i.e. no data in
1641 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1642 if (c
>= char_ranges
[i
] && c
<= char_ranges
[i
+ 1])
1644 if (!(negate
^ (i
< n_char_ranges
)))
1647 INC_BOTH (pos
, pos_byte
);
1650 while (pos
< XINT (lim
) && fastmap
[FETCH_BYTE (pos
)])
1656 while (pos
> XINT (lim
))
1658 int prev_pos_byte
= pos_byte
;
1660 DEC_POS (prev_pos_byte
);
1661 c
= FETCH_MULTIBYTE_CHAR (prev_pos_byte
);
1662 if (SINGLE_BYTE_CHAR_P (c
))
1669 /* See the comment in the previous similar code. */
1670 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1671 if (c
>= char_ranges
[i
] && c
<= char_ranges
[i
+ 1])
1673 if (!(negate
^ (i
< n_char_ranges
)))
1677 pos_byte
= prev_pos_byte
;
1680 while (pos
> XINT (lim
) && fastmap
[FETCH_BYTE (pos
- 1)])
1685 #if 0 /* Not needed now that a position in mid-character
1686 cannot be specified in Lisp. */
1688 /* INC_POS or DEC_POS might have moved POS over LIM. */
1689 && (forwardp
? (pos
> XINT (lim
)) : (pos
< XINT (lim
))))
1696 SET_PT_BOTH (pos
, pos_byte
);
1699 return make_number (PT
- start_point
);
1703 /* Jump over a comment, assuming we are at the beginning of one.
1704 FROM is the current position.
1705 FROM_BYTE is the bytepos corresponding to FROM.
1706 Do not move past STOP (a charpos).
1707 The comment over which we have to jump is of style STYLE
1708 (either SYNTAX_COMMENT_STYLE(foo) or ST_COMMENT_STYLE).
1709 NESTING should be positive to indicate the nesting at the beginning
1710 for nested comments and should be zero or negative else.
1711 ST_COMMENT_STYLE cannot be nested.
1712 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
1713 (or 0 If the search cannot start in the middle of a two-character).
1715 If successful, return 1 and store the charpos of the comment's end
1716 into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR.
1717 Else, return 0 and store the charpos STOP into *CHARPOS_PTR, the
1718 corresponding bytepos into *BYTEPOS_PTR and the current nesting
1719 (as defined for state.incomment) in *INCOMMENT_PTR.
1721 The comment end is the last character of the comment rather than the
1722 character just after the comment.
1724 Global syntax data is assumed to initially be valid for FROM and
1725 remains valid for forward search starting at the returned position. */
1728 forw_comment (from
, from_byte
, stop
, nesting
, style
, prev_syntax
,
1729 charpos_ptr
, bytepos_ptr
, incomment_ptr
)
1730 int from
, from_byte
, stop
;
1731 int nesting
, style
, prev_syntax
;
1732 int *charpos_ptr
, *bytepos_ptr
, *incomment_ptr
;
1735 register enum syntaxcode code
;
1736 register int syntax
;
1738 if (nesting
<= 0) nesting
= -1;
1740 /* Enter the loop in the middle so that we find
1741 a 2-char comment ender if we start in the middle of it. */
1742 syntax
= prev_syntax
;
1743 if (syntax
!= 0) goto forw_incomment
;
1749 *incomment_ptr
= nesting
;
1750 *charpos_ptr
= from
;
1751 *bytepos_ptr
= from_byte
;
1754 c
= FETCH_CHAR (from_byte
);
1755 syntax
= SYNTAX_WITH_FLAGS (c
);
1756 code
= syntax
& 0xff;
1757 if (code
== Sendcomment
1758 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
) == style
1759 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ?
1760 (nesting
> 0 && --nesting
== 0) : nesting
< 0))
1761 /* we have encountered a comment end of the same style
1762 as the comment sequence which began this comment
1765 if (code
== Scomment_fence
1766 && style
== ST_COMMENT_STYLE
)
1767 /* we have encountered a comment end of the same style
1768 as the comment sequence which began this comment
1773 && SYNTAX_FLAGS_COMMENT_NESTED (syntax
)
1774 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
) == style
)
1775 /* we have encountered a nested comment of the same style
1776 as the comment sequence which began this comment section */
1778 INC_BOTH (from
, from_byte
);
1779 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1782 if (from
< stop
&& SYNTAX_FLAGS_COMEND_FIRST (syntax
)
1783 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
) == style
1784 && (c1
= FETCH_CHAR (from_byte
),
1785 SYNTAX_COMEND_SECOND (c1
))
1786 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ||
1787 SYNTAX_COMMENT_NESTED (c1
)) ? nesting
> 0 : nesting
< 0))
1790 /* we have encountered a comment end of the same style
1791 as the comment sequence which began this comment
1796 INC_BOTH (from
, from_byte
);
1797 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1802 && SYNTAX_FLAGS_COMSTART_FIRST (syntax
)
1803 && (c1
= FETCH_CHAR (from_byte
),
1804 SYNTAX_COMMENT_STYLE (c1
) == style
1805 && SYNTAX_COMSTART_SECOND (c1
))
1806 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ||
1807 SYNTAX_COMMENT_NESTED (c1
)))
1808 /* we have encountered a nested comment of the same style
1809 as the comment sequence which began this comment
1812 INC_BOTH (from
, from_byte
);
1813 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1817 *charpos_ptr
= from
;
1818 *bytepos_ptr
= from_byte
;
1822 DEFUN ("forward-comment", Fforward_comment
, Sforward_comment
, 1, 1, 0,
1823 "Move forward across up to N comments. If N is negative, move backward.\n\
1824 Stop scanning if we find something other than a comment or whitespace.\n\
1825 Set point to where scanning stops.\n\
1826 If N comments are found as expected, with nothing except whitespace\n\
1827 between them, return t; otherwise return nil.")
1835 register enum syntaxcode code
;
1836 int comstyle
= 0; /* style of comment encountered */
1837 int comnested
= 0; /* whether the comment is nestable or not */
1840 int out_charpos
, out_bytepos
;
1843 CHECK_NUMBER (count
, 0);
1844 count1
= XINT (count
);
1845 stop
= count1
> 0 ? ZV
: BEGV
;
1851 from_byte
= PT_BYTE
;
1853 SETUP_SYNTAX_TABLE (from
, count1
);
1862 SET_PT_BOTH (from
, from_byte
);
1866 c
= FETCH_CHAR (from_byte
);
1868 comstart_first
= SYNTAX_COMSTART_FIRST (c
);
1869 comnested
= SYNTAX_COMMENT_NESTED (c
);
1870 comstyle
= SYNTAX_COMMENT_STYLE (c
);
1871 INC_BOTH (from
, from_byte
);
1872 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1873 if (from
< stop
&& comstart_first
1874 && (c1
= FETCH_CHAR (from_byte
),
1875 SYNTAX_COMSTART_SECOND (c1
)))
1877 /* We have encountered a comment start sequence and we
1878 are ignoring all text inside comments. We must record
1879 the comment style this sequence begins so that later,
1880 only a comment end of the same style actually ends
1881 the comment section. */
1883 comstyle
= SYNTAX_COMMENT_STYLE (c1
);
1884 comnested
= comnested
|| SYNTAX_COMMENT_NESTED (c1
);
1885 INC_BOTH (from
, from_byte
);
1886 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1889 while (code
== Swhitespace
|| (code
== Sendcomment
&& c
== '\n'));
1891 if (code
== Scomment_fence
)
1892 comstyle
= ST_COMMENT_STYLE
;
1893 else if (code
!= Scomment
)
1896 DEC_BOTH (from
, from_byte
);
1897 SET_PT_BOTH (from
, from_byte
);
1900 /* We're at the start of a comment. */
1901 found
= forw_comment (from
, from_byte
, stop
, comnested
, comstyle
, 0,
1902 &out_charpos
, &out_bytepos
, &dummy
);
1903 from
= out_charpos
; from_byte
= out_bytepos
;
1907 SET_PT_BOTH (from
, from_byte
);
1910 INC_BOTH (from
, from_byte
);
1911 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1912 /* We have skipped one comment. */
1924 SET_PT_BOTH (BEGV
, BEGV_BYTE
);
1929 DEC_BOTH (from
, from_byte
);
1930 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
1931 quoted
= char_quoted (from
, from_byte
);
1934 DEC_BOTH (from
, from_byte
);
1937 c
= FETCH_CHAR (from_byte
);
1940 comnested
= SYNTAX_COMMENT_NESTED (c
);
1941 if (code
== Sendcomment
)
1942 comstyle
= SYNTAX_COMMENT_STYLE (c
);
1943 if (from
> stop
&& SYNTAX_COMEND_SECOND (c
)
1944 && prev_char_comend_first (from
, from_byte
)
1945 && !char_quoted (from
- 1, dec_bytepos (from_byte
)))
1947 /* We must record the comment style encountered so that
1948 later, we can match only the proper comment begin
1949 sequence of the same style. */
1950 DEC_BOTH (from
, from_byte
);
1952 /* Calling char_quoted, above, set up global syntax position
1953 at the new value of FROM. */
1954 c1
= FETCH_CHAR (from_byte
);
1955 comstyle
= SYNTAX_COMMENT_STYLE (c1
);
1956 comnested
= comnested
|| SYNTAX_COMMENT_NESTED (c1
);
1959 if (code
== Scomment_fence
)
1961 /* Skip until first preceding unquoted comment_fence. */
1962 int found
= 0, ini
= from
, ini_byte
= from_byte
;
1966 DEC_BOTH (from
, from_byte
);
1969 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
1970 c
= FETCH_CHAR (from_byte
);
1971 if (SYNTAX (c
) == Scomment_fence
1972 && !char_quoted (from
, from_byte
))
1980 from
= ini
; /* Set point to ini + 1. */
1981 from_byte
= ini_byte
;
1985 else if (code
== Sendcomment
)
1987 found
= back_comment (from
, from_byte
, stop
, comnested
, comstyle
,
1988 &out_charpos
, &out_bytepos
);
1992 /* This end-of-line is not an end-of-comment.
1993 Treat it like a whitespace.
1994 CC-mode (and maybe others) relies on this behavior. */
1998 /* Failure: we should go back to the end of this
1999 not-quite-endcomment. */
2000 if (SYNTAX(c
) != code
)
2001 /* It was a two-char Sendcomment. */
2002 INC_BOTH (from
, from_byte
);
2008 /* We have skipped one comment. */
2009 from
= out_charpos
, from_byte
= out_bytepos
;
2013 else if (code
!= Swhitespace
)
2017 INC_BOTH (from
, from_byte
);
2018 SET_PT_BOTH (from
, from_byte
);
2026 SET_PT_BOTH (from
, from_byte
);
2031 /* Return syntax code of character C if C is a single byte character
2032 or `multibyte_symbol_p' is zero. Otherwise, return Ssymbol. */
2034 #define SYNTAX_WITH_MULTIBYTE_CHECK(c) \
2035 ((SINGLE_BYTE_CHAR_P (c) || !multibyte_symbol_p) \
2036 ? SYNTAX (c) : Ssymbol)
2039 scan_lists (from
, count
, depth
, sexpflag
)
2041 int count
, depth
, sexpflag
;
2044 register int stop
= count
> 0 ? ZV
: BEGV
;
2049 register enum syntaxcode code
, temp_code
;
2050 int min_depth
= depth
; /* Err out if depth gets less than this. */
2051 int comstyle
= 0; /* style of comment encountered */
2052 int comnested
= 0; /* whether the comment is nestable or not */
2054 int last_good
= from
;
2057 int out_bytepos
, out_charpos
;
2059 int multibyte_symbol_p
= sexpflag
&& multibyte_syntax_as_symbol
;
2061 if (depth
> 0) min_depth
= 0;
2063 if (from
> ZV
) from
= ZV
;
2064 if (from
< BEGV
) from
= BEGV
;
2066 from_byte
= CHAR_TO_BYTE (from
);
2071 SETUP_SYNTAX_TABLE (from
, count
);
2076 int comstart_first
, prefix
;
2077 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2078 c
= FETCH_CHAR (from_byte
);
2079 code
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2080 comstart_first
= SYNTAX_COMSTART_FIRST (c
);
2081 comnested
= SYNTAX_COMMENT_NESTED (c
);
2082 comstyle
= SYNTAX_COMMENT_STYLE (c
);
2083 prefix
= SYNTAX_PREFIX (c
);
2084 if (depth
== min_depth
)
2086 INC_BOTH (from
, from_byte
);
2087 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2088 if (from
< stop
&& comstart_first
2089 && SYNTAX_COMSTART_SECOND (FETCH_CHAR (from_byte
))
2090 && parse_sexp_ignore_comments
)
2092 /* we have encountered a comment start sequence and we
2093 are ignoring all text inside comments. We must record
2094 the comment style this sequence begins so that later,
2095 only a comment end of the same style actually ends
2096 the comment section */
2098 c1
= FETCH_CHAR (from_byte
);
2099 comstyle
= SYNTAX_COMMENT_STYLE (c1
);
2100 comnested
= comnested
|| SYNTAX_COMMENT_NESTED (c1
);
2101 INC_BOTH (from
, from_byte
);
2102 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2108 switch (SWITCH_ENUM_CAST (code
))
2112 if (from
== stop
) goto lose
;
2113 INC_BOTH (from
, from_byte
);
2114 /* treat following character as a word constituent */
2117 if (depth
|| !sexpflag
) break;
2118 /* This word counts as a sexp; return at end of it. */
2121 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2123 /* Some compilers can't handle this inside the switch. */
2124 c
= FETCH_CHAR (from_byte
);
2125 temp
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2130 INC_BOTH (from
, from_byte
);
2131 if (from
== stop
) goto lose
;
2140 INC_BOTH (from
, from_byte
);
2144 case Scomment_fence
:
2145 comstyle
= ST_COMMENT_STYLE
;
2148 if (!parse_sexp_ignore_comments
) break;
2149 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2150 found
= forw_comment (from
, from_byte
, stop
,
2151 comnested
, comstyle
, 0,
2152 &out_charpos
, &out_bytepos
, &dummy
);
2153 from
= out_charpos
, from_byte
= out_bytepos
;
2160 INC_BOTH (from
, from_byte
);
2161 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2167 if (from
!= stop
&& c
== FETCH_CHAR (from_byte
))
2169 INC_BOTH (from
, from_byte
);
2179 if (!++depth
) goto done
;
2184 if (!--depth
) goto done
;
2185 if (depth
< min_depth
)
2186 Fsignal (Qscan_error
,
2187 Fcons (build_string ("Containing expression ends prematurely"),
2188 Fcons (make_number (last_good
),
2189 Fcons (make_number (from
), Qnil
))));
2194 temp_pos
= dec_bytepos (from_byte
);
2195 stringterm
= FETCH_CHAR (temp_pos
);
2198 if (from
>= stop
) goto lose
;
2199 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2200 c
= FETCH_CHAR (from_byte
);
2203 && SYNTAX_WITH_MULTIBYTE_CHECK (c
) == Sstring
)
2204 : SYNTAX_WITH_MULTIBYTE_CHECK (c
) == Sstring_fence
)
2207 /* Some compilers can't handle this inside the switch. */
2208 temp
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2213 INC_BOTH (from
, from_byte
);
2215 INC_BOTH (from
, from_byte
);
2217 INC_BOTH (from
, from_byte
);
2218 if (!depth
&& sexpflag
) goto done
;
2223 /* Reached end of buffer. Error if within object, return nil if between */
2224 if (depth
) goto lose
;
2229 /* End of object reached */
2238 DEC_BOTH (from
, from_byte
);
2239 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2240 c
= FETCH_CHAR (from_byte
);
2241 code
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2242 if (depth
== min_depth
)
2245 comnested
= SYNTAX_COMMENT_NESTED (c
);
2246 if (code
== Sendcomment
)
2247 comstyle
= SYNTAX_COMMENT_STYLE (c
);
2248 if (from
> stop
&& SYNTAX_COMEND_SECOND (c
)
2249 && prev_char_comend_first (from
, from_byte
)
2250 && parse_sexp_ignore_comments
)
2252 /* We must record the comment style encountered so that
2253 later, we can match only the proper comment begin
2254 sequence of the same style. */
2255 DEC_BOTH (from
, from_byte
);
2256 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2258 c1
= FETCH_CHAR (from_byte
);
2259 comstyle
= SYNTAX_COMMENT_STYLE (c1
);
2260 comnested
= comnested
|| SYNTAX_COMMENT_NESTED (c1
);
2263 /* Quoting turns anything except a comment-ender
2264 into a word character. Note that this cannot be true
2265 if we decremented FROM in the if-statement above. */
2266 if (code
!= Sendcomment
&& char_quoted (from
, from_byte
))
2268 else if (SYNTAX_PREFIX (c
))
2271 switch (SWITCH_ENUM_CAST (code
))
2277 if (depth
|| !sexpflag
) break;
2278 /* This word counts as a sexp; count object finished
2279 after passing it. */
2282 temp_pos
= from_byte
;
2283 if (! NILP (current_buffer
->enable_multibyte_characters
))
2287 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2288 c1
= FETCH_CHAR (temp_pos
);
2289 temp_code
= SYNTAX_WITH_MULTIBYTE_CHECK (c1
);
2290 /* Don't allow comment-end to be quoted. */
2291 if (temp_code
== Sendcomment
)
2293 quoted
= char_quoted (from
- 1, temp_pos
);
2296 DEC_BOTH (from
, from_byte
);
2297 temp_pos
= dec_bytepos (temp_pos
);
2298 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2300 c1
= FETCH_CHAR (temp_pos
);
2301 temp_code
= SYNTAX_WITH_MULTIBYTE_CHECK (c1
);
2302 if (! (quoted
|| temp_code
== Sword
2303 || temp_code
== Ssymbol
2304 || temp_code
== Squote
))
2306 DEC_BOTH (from
, from_byte
);
2313 temp_pos
= dec_bytepos (from_byte
);
2314 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2315 if (from
!= stop
&& c
== FETCH_CHAR (temp_pos
))
2316 DEC_BOTH (from
, from_byte
);
2325 if (!++depth
) goto done2
;
2330 if (!--depth
) goto done2
;
2331 if (depth
< min_depth
)
2332 Fsignal (Qscan_error
,
2333 Fcons (build_string ("Containing expression ends prematurely"),
2334 Fcons (make_number (last_good
),
2335 Fcons (make_number (from
), Qnil
))));
2339 if (!parse_sexp_ignore_comments
)
2341 found
= back_comment (from
, from_byte
, stop
, comnested
, comstyle
,
2342 &out_charpos
, &out_bytepos
);
2343 /* FIXME: if found == -1, then it really wasn't a comment-end.
2344 For single-char Sendcomment, we can't do much about it apart
2345 from skipping the char.
2346 For 2-char endcomments, we could try again, taking both
2347 chars as separate entities, but it's a lot of trouble
2348 for very little gain, so we don't bother either. -sm */
2350 from
= out_charpos
, from_byte
= out_bytepos
;
2353 case Scomment_fence
:
2357 DEC_BOTH (from
, from_byte
);
2358 if (from
== stop
) goto lose
;
2359 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2360 if (!char_quoted (from
, from_byte
)
2361 && (c
= FETCH_CHAR (from_byte
),
2362 SYNTAX_WITH_MULTIBYTE_CHECK (c
) == code
))
2365 if (code
== Sstring_fence
&& !depth
&& sexpflag
) goto done2
;
2369 stringterm
= FETCH_CHAR (from_byte
);
2372 if (from
== stop
) goto lose
;
2373 temp_pos
= from_byte
;
2374 if (! NILP (current_buffer
->enable_multibyte_characters
))
2378 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2379 if (!char_quoted (from
- 1, temp_pos
)
2380 && stringterm
== (c
= FETCH_CHAR (temp_pos
))
2381 && SYNTAX_WITH_MULTIBYTE_CHECK (c
) == Sstring
)
2383 DEC_BOTH (from
, from_byte
);
2385 DEC_BOTH (from
, from_byte
);
2386 if (!depth
&& sexpflag
) goto done2
;
2391 /* Reached start of buffer. Error if within object, return nil if between */
2392 if (depth
) goto lose
;
2403 XSETFASTINT (val
, from
);
2407 Fsignal (Qscan_error
,
2408 Fcons (build_string ("Unbalanced parentheses"),
2409 Fcons (make_number (last_good
),
2410 Fcons (make_number (from
), Qnil
))));
2415 DEFUN ("scan-lists", Fscan_lists
, Sscan_lists
, 3, 3, 0,
2416 "Scan from character number FROM by COUNT lists.\n\
2417 Returns the character number of the position thus found.\n\
2419 If DEPTH is nonzero, paren depth begins counting from that value,\n\
2420 only places where the depth in parentheses becomes zero\n\
2421 are candidates for stopping; COUNT such places are counted.\n\
2422 Thus, a positive value for DEPTH means go out levels.\n\
2424 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.\n\
2426 If the beginning or end of (the accessible part of) the buffer is reached\n\
2427 and the depth is wrong, an error is signaled.\n\
2428 If the depth is right but the count is not used up, nil is returned.")
2429 (from
, count
, depth
)
2430 Lisp_Object from
, count
, depth
;
2432 CHECK_NUMBER (from
, 0);
2433 CHECK_NUMBER (count
, 1);
2434 CHECK_NUMBER (depth
, 2);
2436 return scan_lists (XINT (from
), XINT (count
), XINT (depth
), 0);
2439 DEFUN ("scan-sexps", Fscan_sexps
, Sscan_sexps
, 2, 2, 0,
2440 "Scan from character number FROM by COUNT balanced expressions.\n\
2441 If COUNT is negative, scan backwards.\n\
2442 Returns the character number of the position thus found.\n\
2444 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.\n\
2446 If the beginning or end of (the accessible part of) the buffer is reached\n\
2447 in the middle of a parenthetical grouping, an error is signaled.\n\
2448 If the beginning or end is reached between groupings\n\
2449 but before count is used up, nil is returned.")
2451 Lisp_Object from
, count
;
2453 CHECK_NUMBER (from
, 0);
2454 CHECK_NUMBER (count
, 1);
2456 return scan_lists (XINT (from
), XINT (count
), 0, 1);
2459 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars
, Sbackward_prefix_chars
,
2461 "Move point backward over any number of chars with prefix syntax.\n\
2462 This includes chars with \"quote\" or \"prefix\" syntax (' or p).")
2467 int opoint_byte
= PT_BYTE
;
2469 int pos_byte
= PT_BYTE
;
2474 SET_PT_BOTH (opoint
, opoint_byte
);
2479 SETUP_SYNTAX_TABLE (pos
, -1);
2481 DEC_BOTH (pos
, pos_byte
);
2483 while (!char_quoted (pos
, pos_byte
)
2484 /* Previous statement updates syntax table. */
2485 && ((c
= FETCH_CHAR (pos_byte
), SYNTAX (c
) == Squote
)
2486 || SYNTAX_PREFIX (c
)))
2489 opoint_byte
= pos_byte
;
2492 DEC_BOTH (pos
, pos_byte
);
2495 SET_PT_BOTH (opoint
, opoint_byte
);
2500 /* Parse forward from FROM / FROM_BYTE to END,
2501 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
2502 and return a description of the state of the parse at END.
2503 If STOPBEFORE is nonzero, stop at the start of an atom.
2504 If COMMENTSTOP is 1, stop at the start of a comment.
2505 If COMMENTSTOP is -1, stop at the start or end of a comment,
2506 after the beginning of a string, or after the end of a string. */
2509 scan_sexps_forward (stateptr
, from
, from_byte
, end
, targetdepth
,
2510 stopbefore
, oldstate
, commentstop
)
2511 struct lisp_parse_state
*stateptr
;
2513 int end
, targetdepth
, stopbefore
;
2514 Lisp_Object oldstate
;
2517 struct lisp_parse_state state
;
2519 register enum syntaxcode code
;
2522 struct level
{ int last
, prev
; };
2523 struct level levelstart
[100];
2524 register struct level
*curlevel
= levelstart
;
2525 struct level
*endlevel
= levelstart
+ 100;
2526 register int depth
; /* Paren depth of current scanning location.
2527 level - levelstart equals this except
2528 when the depth becomes negative. */
2529 int mindepth
; /* Lowest DEPTH value seen. */
2530 int start_quoted
= 0; /* Nonzero means starting after a char quote */
2532 int prev_from
; /* Keep one character before FROM. */
2534 int prev_from_syntax
;
2535 int boundary_stop
= commentstop
== -1;
2538 int out_bytepos
, out_charpos
;
2542 prev_from_byte
= from_byte
;
2544 DEC_BOTH (prev_from
, prev_from_byte
);
2546 /* Use this macro instead of `from++'. */
2548 do { prev_from = from; \
2549 prev_from_byte = from_byte; \
2551 = SYNTAX_WITH_FLAGS (FETCH_CHAR (prev_from_byte)); \
2552 INC_BOTH (from, from_byte); \
2553 UPDATE_SYNTAX_TABLE_FORWARD (from); \
2559 if (NILP (oldstate
))
2562 state
.instring
= -1;
2563 state
.incomment
= 0;
2564 state
.comstyle
= 0; /* comment style a by default. */
2565 state
.comstr_start
= -1; /* no comment/string seen. */
2569 tem
= Fcar (oldstate
);
2575 oldstate
= Fcdr (oldstate
);
2576 oldstate
= Fcdr (oldstate
);
2577 oldstate
= Fcdr (oldstate
);
2578 tem
= Fcar (oldstate
);
2579 /* Check whether we are inside string_fence-style string: */
2580 state
.instring
= (!NILP (tem
)
2581 ? (INTEGERP (tem
) ? XINT (tem
) : ST_STRING_STYLE
)
2584 oldstate
= Fcdr (oldstate
);
2585 tem
= Fcar (oldstate
);
2586 state
.incomment
= (!NILP (tem
)
2587 ? (INTEGERP (tem
) ? XINT (tem
) : -1)
2590 oldstate
= Fcdr (oldstate
);
2591 tem
= Fcar (oldstate
);
2592 start_quoted
= !NILP (tem
);
2594 /* if the eighth element of the list is nil, we are in comment
2595 style a. If it is non-nil, we are in comment style b */
2596 oldstate
= Fcdr (oldstate
);
2597 oldstate
= Fcdr (oldstate
);
2598 tem
= Fcar (oldstate
);
2599 state
.comstyle
= NILP (tem
) ? 0 : (EQ (tem
, Qsyntax_table
)
2600 ? ST_COMMENT_STYLE
: 1);
2602 oldstate
= Fcdr (oldstate
);
2603 tem
= Fcar (oldstate
);
2604 state
.comstr_start
= NILP (tem
) ? -1 : XINT (tem
) ;
2605 oldstate
= Fcdr (oldstate
);
2606 tem
= Fcar (oldstate
);
2607 while (!NILP (tem
)) /* >= second enclosing sexps. */
2609 /* curlevel++->last ran into compiler bug on Apollo */
2610 curlevel
->last
= XINT (Fcar (tem
));
2611 if (++curlevel
== endlevel
)
2612 curlevel
--; /* error ("Nesting too deep for parser"); */
2613 curlevel
->prev
= -1;
2614 curlevel
->last
= -1;
2621 curlevel
->prev
= -1;
2622 curlevel
->last
= -1;
2624 SETUP_SYNTAX_TABLE (prev_from
, 1);
2625 prev_from_syntax
= SYNTAX_WITH_FLAGS (FETCH_CHAR (prev_from_byte
));
2626 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2628 /* Enter the loop at a place appropriate for initial state. */
2630 if (state
.incomment
)
2631 goto startincomment
;
2632 if (state
.instring
>= 0)
2634 nofence
= state
.instring
!= ST_STRING_STYLE
;
2636 goto startquotedinstring
;
2639 else if (start_quoted
)
2642 #if 0 /* This seems to be redundant with the identical code above. */
2643 SETUP_SYNTAX_TABLE (prev_from
, 1);
2644 prev_from_syntax
= SYNTAX_WITH_FLAGS (FETCH_CHAR (prev_from_byte
));
2645 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2651 code
= prev_from_syntax
& 0xff;
2653 if (code
== Scomment
)
2655 state
.comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax
);
2656 state
.incomment
= (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax
) ?
2658 state
.comstr_start
= prev_from
;
2660 else if (code
== Scomment_fence
)
2662 /* Record the comment style we have entered so that only
2663 the comment-end sequence of the same style actually
2664 terminates the comment section. */
2665 state
.comstyle
= ST_COMMENT_STYLE
;
2666 state
.incomment
= -1;
2667 state
.comstr_start
= prev_from
;
2670 else if (from
< end
)
2671 if (SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax
))
2672 if (c1
= FETCH_CHAR (from_byte
),
2673 SYNTAX_COMSTART_SECOND (c1
))
2674 /* Duplicate code to avoid a complex if-expression
2675 which causes trouble for the SGI compiler. */
2677 /* Record the comment style we have entered so that only
2678 the comment-end sequence of the same style actually
2679 terminates the comment section. */
2680 state
.comstyle
= SYNTAX_COMMENT_STYLE (FETCH_CHAR (from_byte
));
2681 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax
);
2682 comnested
= comnested
|| SYNTAX_COMMENT_NESTED (c1
);
2683 state
.incomment
= comnested
? 1 : -1;
2684 state
.comstr_start
= prev_from
;
2689 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax
))
2691 switch (SWITCH_ENUM_CAST (code
))
2695 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
2696 curlevel
->last
= prev_from
;
2698 if (from
== end
) goto endquoted
;
2701 /* treat following character as a word constituent */
2704 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
2705 curlevel
->last
= prev_from
;
2709 /* Some compilers can't handle this inside the switch. */
2710 temp
= SYNTAX (FETCH_CHAR (from_byte
));
2716 if (from
== end
) goto endquoted
;
2728 curlevel
->prev
= curlevel
->last
;
2732 if (commentstop
|| boundary_stop
) goto done
;
2734 /* The (from == BEGV) test was to enter the loop in the middle so
2735 that we find a 2-char comment ender even if we start in the
2736 middle of it. We don't want to do that if we're just at the
2737 beginning of the comment (think of (*) ... (*)). */
2738 found
= forw_comment (from
, from_byte
, end
,
2739 state
.incomment
, state
.comstyle
,
2740 (from
== BEGV
|| from
< state
.comstr_start
+ 3)
2741 ? 0 : prev_from_syntax
,
2742 &out_charpos
, &out_bytepos
, &state
.incomment
);
2743 from
= out_charpos
; from_byte
= out_bytepos
;
2744 /* Beware! prev_from and friends are invalid now.
2745 Luckily, the `done' doesn't use them and the INC_FROM
2746 sets them to a sane value without looking at them. */
2747 if (!found
) goto done
;
2749 state
.incomment
= 0;
2750 state
.comstyle
= 0; /* reset the comment style */
2751 if (boundary_stop
) goto done
;
2755 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
2757 /* curlevel++->last ran into compiler bug on Apollo */
2758 curlevel
->last
= prev_from
;
2759 if (++curlevel
== endlevel
)
2760 curlevel
--; /* error ("Nesting too deep for parser"); */
2761 curlevel
->prev
= -1;
2762 curlevel
->last
= -1;
2763 if (targetdepth
== depth
) goto done
;
2768 if (depth
< mindepth
)
2770 if (curlevel
!= levelstart
)
2772 curlevel
->prev
= curlevel
->last
;
2773 if (targetdepth
== depth
) goto done
;
2778 state
.comstr_start
= from
- 1;
2779 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
2780 curlevel
->last
= prev_from
;
2781 state
.instring
= (code
== Sstring
2782 ? (FETCH_CHAR (prev_from_byte
))
2784 if (boundary_stop
) goto done
;
2787 nofence
= state
.instring
!= ST_STRING_STYLE
;
2793 if (from
>= end
) goto done
;
2794 c
= FETCH_CHAR (from_byte
);
2795 /* Some compilers can't handle this inside the switch. */
2798 /* Check TEMP here so that if the char has
2799 a syntax-table property which says it is NOT
2800 a string character, it does not end the string. */
2801 if (nofence
&& c
== state
.instring
&& temp
== Sstring
)
2807 if (!nofence
) goto string_end
;
2812 startquotedinstring
:
2813 if (from
>= end
) goto endquoted
;
2819 state
.instring
= -1;
2820 curlevel
->prev
= curlevel
->last
;
2822 if (boundary_stop
) goto done
;
2831 stop
: /* Here if stopping before start of sexp. */
2832 from
= prev_from
; /* We have just fetched the char that starts it; */
2833 goto done
; /* but return the position before it. */
2838 state
.depth
= depth
;
2839 state
.mindepth
= mindepth
;
2840 state
.thislevelstart
= curlevel
->prev
;
2841 state
.prevlevelstart
2842 = (curlevel
== levelstart
) ? -1 : (curlevel
- 1)->last
;
2843 state
.location
= from
;
2844 state
.levelstarts
= Qnil
;
2845 while (--curlevel
>= levelstart
)
2846 state
.levelstarts
= Fcons (make_number (curlevel
->last
),
2853 /* This comment supplies the doc string for parse-partial-sexp,
2854 for make-docfile to see. We cannot put this in the real DEFUN
2855 due to limits in the Unix cpp.
2857 DEFUN ("parse-partial-sexp", Ffoo, Sfoo, 2, 6, 0,
2858 "Parse Lisp syntax starting at FROM until TO; return status of parse at TO.\n\
2859 Parsing stops at TO or when certain criteria are met;\n\
2860 point is set to where parsing stops.\n\
2861 If fifth arg STATE is omitted or nil,\n\
2862 parsing assumes that FROM is the beginning of a function.\n\
2863 Value is a list of ten elements describing final state of parsing:\n\
2864 0. depth in parens.\n\
2865 1. character address of start of innermost containing list; nil if none.\n\
2866 2. character address of start of last complete sexp terminated.\n\
2867 3. non-nil if inside a string.\n\
2868 (it is the character that will terminate the string,\n\
2869 or t if the string should be terminated by a generic string delimiter.)\n\
2870 4. nil if outside a comment, t if inside a non-nestable comment, \n\
2871 else an integer (the current comment nesting).\n\
2872 5. t if following a quote character.\n\
2873 6. the minimum paren-depth encountered during this scan.\n\
2874 7. t if in a comment of style b; symbol `syntax-table' if the comment\n\
2875 should be terminated by a generic comment delimiter.\n\
2876 8. character address of start of comment or string; nil if not in one.\n\
2877 9. Intermediate data for continuation of parsing (subject to change).\n\
2878 If third arg TARGETDEPTH is non-nil, parsing stops if the depth\n\
2879 in parentheses becomes equal to TARGETDEPTH.\n\
2880 Fourth arg STOPBEFORE non-nil means stop when come to\n\
2881 any character that starts a sexp.\n\
2882 Fifth arg STATE is a nine-element list like what this function returns.\n\
2883 It is used to initialize the state of the parse. Elements number 1, 2, 6\n\
2884 and 8 are ignored; you can leave off element 8 (the last) entirely.\n\
2885 Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.\n\
2886 If it is symbol `syntax-table', stop after the start of a comment or a\n\
2887 string, or after end of a comment or a string.")
2888 (from, to, targetdepth, stopbefore, state, commentstop)
2891 DEFUN ("parse-partial-sexp", Fparse_partial_sexp
, Sparse_partial_sexp
, 2, 6, 0,
2892 0 /* See immediately above */)
2893 (from
, to
, targetdepth
, stopbefore
, oldstate
, commentstop
)
2894 Lisp_Object from
, to
, targetdepth
, stopbefore
, oldstate
, commentstop
;
2896 struct lisp_parse_state state
;
2899 if (!NILP (targetdepth
))
2901 CHECK_NUMBER (targetdepth
, 3);
2902 target
= XINT (targetdepth
);
2905 target
= -100000; /* We won't reach this depth */
2907 validate_region (&from
, &to
);
2908 scan_sexps_forward (&state
, XINT (from
), CHAR_TO_BYTE (XINT (from
)),
2910 target
, !NILP (stopbefore
), oldstate
,
2912 ? 0 : (EQ (commentstop
, Qsyntax_table
) ? -1 : 1)));
2914 SET_PT (state
.location
);
2916 return Fcons (make_number (state
.depth
),
2917 Fcons (state
.prevlevelstart
< 0 ? Qnil
: make_number (state
.prevlevelstart
),
2918 Fcons (state
.thislevelstart
< 0 ? Qnil
: make_number (state
.thislevelstart
),
2919 Fcons (state
.instring
>= 0
2920 ? (state
.instring
== ST_STRING_STYLE
2921 ? Qt
: make_number (state
.instring
)) : Qnil
,
2922 Fcons (state
.incomment
< 0 ? Qt
:
2923 (state
.incomment
== 0 ? Qnil
:
2924 make_number (state
.incomment
)),
2925 Fcons (state
.quoted
? Qt
: Qnil
,
2926 Fcons (make_number (state
.mindepth
),
2927 Fcons ((state
.comstyle
2928 ? (state
.comstyle
== ST_COMMENT_STYLE
2929 ? Qsyntax_table
: Qt
) :
2931 Fcons (((state
.incomment
2932 || (state
.instring
>= 0))
2933 ? make_number (state
.comstr_start
)
2935 Fcons (state
.levelstarts
, Qnil
))))))))));
2944 /* This has to be done here, before we call Fmake_char_table. */
2945 Qsyntax_table
= intern ("syntax-table");
2946 staticpro (&Qsyntax_table
);
2948 /* Intern this now in case it isn't already done.
2949 Setting this variable twice is harmless.
2950 But don't staticpro it here--that is done in alloc.c. */
2951 Qchar_table_extra_slots
= intern ("char-table-extra-slots");
2953 /* Create objects which can be shared among syntax tables. */
2954 Vsyntax_code_object
= Fmake_vector (make_number (Smax
), Qnil
);
2955 for (i
= 0; i
< XVECTOR (Vsyntax_code_object
)->size
; i
++)
2956 XVECTOR (Vsyntax_code_object
)->contents
[i
]
2957 = Fcons (make_number (i
), Qnil
);
2959 /* Now we are ready to set up this property, so we can
2960 create syntax tables. */
2961 Fput (Qsyntax_table
, Qchar_table_extra_slots
, make_number (0));
2963 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Swhitespace
];
2965 Vstandard_syntax_table
= Fmake_char_table (Qsyntax_table
, temp
);
2967 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Sword
];
2968 for (i
= 'a'; i
<= 'z'; i
++)
2969 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
2970 for (i
= 'A'; i
<= 'Z'; i
++)
2971 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
2972 for (i
= '0'; i
<= '9'; i
++)
2973 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
2975 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '$', temp
);
2976 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '%', temp
);
2978 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '(',
2979 Fcons (make_number (Sopen
), make_number (')')));
2980 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ')',
2981 Fcons (make_number (Sclose
), make_number ('(')));
2982 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '[',
2983 Fcons (make_number (Sopen
), make_number (']')));
2984 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ']',
2985 Fcons (make_number (Sclose
), make_number ('[')));
2986 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '{',
2987 Fcons (make_number (Sopen
), make_number ('}')));
2988 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '}',
2989 Fcons (make_number (Sclose
), make_number ('{')));
2990 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '"',
2991 Fcons (make_number ((int) Sstring
), Qnil
));
2992 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\\',
2993 Fcons (make_number ((int) Sescape
), Qnil
));
2995 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Ssymbol
];
2996 for (i
= 0; i
< 10; i
++)
2998 c
= "_-+*/&|<>="[i
];
2999 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, c
, temp
);
3002 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Spunct
];
3003 for (i
= 0; i
< 12; i
++)
3005 c
= ".,;:?!#@~^'`"[i
];
3006 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, c
, temp
);
3009 /* All multibyte characters have syntax `word' by default. */
3010 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Sword
];
3011 for (i
= CHAR_TABLE_SINGLE_BYTE_SLOTS
; i
< CHAR_TABLE_ORDINARY_SLOTS
; i
++)
3012 XCHAR_TABLE (Vstandard_syntax_table
)->contents
[i
] = temp
;
3018 Qsyntax_table_p
= intern ("syntax-table-p");
3019 staticpro (&Qsyntax_table_p
);
3021 staticpro (&Vsyntax_code_object
);
3023 Qscan_error
= intern ("scan-error");
3024 staticpro (&Qscan_error
);
3025 Fput (Qscan_error
, Qerror_conditions
,
3026 Fcons (Qscan_error
, Fcons (Qerror
, Qnil
)));
3027 Fput (Qscan_error
, Qerror_message
,
3028 build_string ("Scan error"));
3030 DEFVAR_BOOL ("parse-sexp-ignore-comments", &parse_sexp_ignore_comments
,
3031 "Non-nil means `forward-sexp', etc., should treat comments as whitespace.");
3033 DEFVAR_BOOL ("parse-sexp-lookup-properties", &parse_sexp_lookup_properties
,
3034 "Non-nil means `forward-sexp', etc., grant `syntax-table' property.\n\
3035 The value of this property should be either a syntax table, or a cons\n\
3036 of the form (SYNTAXCODE . MATCHCHAR), SYNTAXCODE being the numeric\n\
3037 syntax code, MATCHCHAR being nil or the character to match (which is\n\
3038 relevant only for open/close type.");
3040 words_include_escapes
= 0;
3041 DEFVAR_BOOL ("words-include-escapes", &words_include_escapes
,
3042 "Non-nil means `forward-word', etc., should treat escape chars part of words.");
3044 DEFVAR_BOOL ("multibyte-syntax-as-symbol", &multibyte_syntax_as_symbol
,
3045 "Non-nil means `scan-sexps' treats all multibyte characters as symbol.");
3046 multibyte_syntax_as_symbol
= 0;
3048 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3049 &open_paren_in_column_0_is_defun_start
,
3050 "Non-nil means an open paren in column 0 denotes the start of a defun.");
3051 open_paren_in_column_0_is_defun_start
= 1;
3053 defsubr (&Ssyntax_table_p
);
3054 defsubr (&Ssyntax_table
);
3055 defsubr (&Sstandard_syntax_table
);
3056 defsubr (&Scopy_syntax_table
);
3057 defsubr (&Sset_syntax_table
);
3058 defsubr (&Schar_syntax
);
3059 defsubr (&Smatching_paren
);
3060 defsubr (&Sstring_to_syntax
);
3061 defsubr (&Smodify_syntax_entry
);
3062 defsubr (&Sdescribe_syntax
);
3064 defsubr (&Sforward_word
);
3066 defsubr (&Sskip_chars_forward
);
3067 defsubr (&Sskip_chars_backward
);
3068 defsubr (&Sskip_syntax_forward
);
3069 defsubr (&Sskip_syntax_backward
);
3071 defsubr (&Sforward_comment
);
3072 defsubr (&Sscan_lists
);
3073 defsubr (&Sscan_sexps
);
3074 defsubr (&Sbackward_prefix_chars
);
3075 defsubr (&Sparse_partial_sexp
);