(scan_sexps_forward):
[emacs.git] / src / syntax.c
blob656d567b1a5deb41fc2ea3b34677e59d771d23b8
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)
9 any later version.
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. */
22 #include <config.h>
23 #include <ctype.h>
24 #include "lisp.h"
25 #include "commands.h"
26 #include "buffer.h"
27 #include "charset.h"
29 /* Make syntax table lookup grant data in gl_state. */
30 #define SYNTAX_ENTRY_VIA_PROPERTY
32 #include "syntax.h"
33 #include "intervals.h"
35 /* We use these constants in place for comment-style and
36 string-ender-char to distinguish comments/strings started by
37 comment_fence and string_fence codes. */
39 #define ST_COMMENT_STYLE (256 + 1)
40 #define ST_STRING_STYLE (256 + 2)
41 #include "category.h"
43 Lisp_Object Qsyntax_table_p, Qsyntax_table, Qscan_error;
45 int words_include_escapes;
46 int parse_sexp_lookup_properties;
48 /* Nonzero means `scan-sexps' treat all multibyte characters as symbol. */
49 int multibyte_syntax_as_symbol;
51 /* Used as a temporary in SYNTAX_ENTRY and other macros in syntax.h,
52 if not compiled with GCC. No need to mark it, since it is used
53 only very temporarily. */
54 Lisp_Object syntax_temp;
56 /* Non-zero means an open parenthesis in column 0 is always considered
57 to be the start of a defun. Zero means an open parenthesis in
58 column 0 has no special meaning. */
60 int open_paren_in_column_0_is_defun_start;
62 /* This is the internal form of the parse state used in parse-partial-sexp. */
64 struct lisp_parse_state
66 int depth; /* Depth at end of parsing. */
67 int instring; /* -1 if not within string, else desired terminator. */
68 int incomment; /* -1 if in unnestable comment else comment nesting */
69 int comstyle; /* comment style a=0, or b=1, or ST_COMMENT_STYLE. */
70 int quoted; /* Nonzero if just after an escape char at end of parsing */
71 int thislevelstart; /* Char number of most recent start-of-expression at current level */
72 int prevlevelstart; /* Char number of start of containing expression */
73 int location; /* Char number at which parsing stopped. */
74 int mindepth; /* Minimum depth seen while scanning. */
75 int comstr_start; /* Position just after last comment/string starter. */
76 Lisp_Object levelstarts; /* Char numbers of starts-of-expression
77 of levels (starting from outermost). */
80 /* These variables are a cache for finding the start of a defun.
81 find_start_pos is the place for which the defun start was found.
82 find_start_value is the defun start position found for it.
83 find_start_value_byte is the corresponding byte position.
84 find_start_buffer is the buffer it was found in.
85 find_start_begv is the BEGV value when it was found.
86 find_start_modiff is the value of MODIFF when it was found. */
88 static int find_start_pos;
89 static int find_start_value;
90 static int find_start_value_byte;
91 static struct buffer *find_start_buffer;
92 static int find_start_begv;
93 static int find_start_modiff;
96 static int find_defun_start P_ ((int, int));
97 static int back_comment P_ ((int, int, int, int, int, int *, int *));
98 static int char_quoted P_ ((int, int));
99 static Lisp_Object skip_chars P_ ((int, int, Lisp_Object, Lisp_Object));
100 static Lisp_Object scan_lists P_ ((int, int, int, int));
101 static void scan_sexps_forward P_ ((struct lisp_parse_state *,
102 int, int, int, int,
103 int, Lisp_Object, int));
106 struct gl_state_s gl_state; /* Global state of syntax parser. */
108 INTERVAL interval_of ();
109 #define INTERVALS_AT_ONCE 10 /* 1 + max-number of intervals
110 to scan to property-change. */
112 /* Update gl_state to an appropriate interval which contains CHARPOS. The
113 sign of COUNT give the relative position of CHARPOS wrt the previously
114 valid interval. If INIT, only [be]_property fields of gl_state are
115 valid at start, the rest is filled basing on OBJECT.
117 `gl_state.*_i' are the intervals, and CHARPOS is further in the search
118 direction than the intervals - or in an interval. We update the
119 current syntax-table basing on the property of this interval, and
120 update the interval to start further than CHARPOS - or be
121 NULL_INTERVAL. We also update lim_property to be the next value of
122 charpos to call this subroutine again - or be before/after the
123 start/end of OBJECT. */
125 void
126 update_syntax_table (charpos, count, init, object)
127 int charpos, count, init;
128 Lisp_Object object;
130 Lisp_Object tmp_table;
131 int cnt = 0, invalidate = 1;
132 INTERVAL i, oldi;
134 if (init)
136 gl_state.start = gl_state.b_property;
137 gl_state.stop = gl_state.e_property;
138 gl_state.forward_i = interval_of (charpos, object);
139 i = gl_state.backward_i = gl_state.forward_i;
140 gl_state.left_ok = gl_state.right_ok = 1;
141 invalidate = 0;
142 if (NULL_INTERVAL_P (i))
143 return;
144 /* interval_of updates only ->position of the return value, so
145 update the parents manually to speed up update_interval. */
146 while (!NULL_PARENT (i))
148 if (AM_RIGHT_CHILD (i))
149 INTERVAL_PARENT (i)->position = i->position
150 - LEFT_TOTAL_LENGTH (i) + TOTAL_LENGTH (i) /* right end */
151 - TOTAL_LENGTH (INTERVAL_PARENT (i))
152 + LEFT_TOTAL_LENGTH (INTERVAL_PARENT (i));
153 else
154 INTERVAL_PARENT (i)->position = i->position - LEFT_TOTAL_LENGTH (i)
155 + TOTAL_LENGTH (i);
156 i = INTERVAL_PARENT (i);
158 i = gl_state.forward_i;
159 gl_state.b_property = i->position - 1 - gl_state.offset;
160 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
161 goto update;
163 oldi = i = count > 0 ? gl_state.forward_i : gl_state.backward_i;
165 /* We are guarantied to be called with CHARPOS either in i,
166 or further off. */
167 if (NULL_INTERVAL_P (i))
168 error ("Error in syntax_table logic for to-the-end intervals");
169 else if (charpos < i->position) /* Move left. */
171 if (count > 0)
172 error ("Error in syntax_table logic for intervals <-");
173 /* Update the interval. */
174 i = update_interval (i, charpos);
175 if (oldi->position != INTERVAL_LAST_POS (i))
177 invalidate = 0;
178 gl_state.right_ok = 1; /* Invalidate the other end. */
179 gl_state.forward_i = i;
180 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
183 else if (charpos >= INTERVAL_LAST_POS (i)) /* Move right. */
185 if (count < 0)
186 error ("Error in syntax_table logic for intervals ->");
187 /* Update the interval. */
188 i = update_interval (i, charpos);
189 if (i->position != INTERVAL_LAST_POS (oldi))
191 invalidate = 0;
192 gl_state.left_ok = 1; /* Invalidate the other end. */
193 gl_state.backward_i = i;
194 gl_state.b_property = i->position - 1 - gl_state.offset;
197 else if (count > 0 ? gl_state.right_ok : gl_state.left_ok)
199 /* We do not need to recalculate tmp_table. */
200 tmp_table = gl_state.old_prop;
203 update:
204 tmp_table = textget (i->plist, Qsyntax_table);
206 if (invalidate)
207 invalidate = !EQ (tmp_table, gl_state.old_prop); /* Need to invalidate? */
209 if (invalidate) /* Did not get to adjacent interval. */
210 { /* with the same table => */
211 /* invalidate the old range. */
212 if (count > 0)
214 gl_state.backward_i = i;
215 gl_state.left_ok = 1; /* Invalidate the other end. */
216 gl_state.b_property = i->position - 1 - gl_state.offset;
218 else
220 gl_state.forward_i = i;
221 gl_state.right_ok = 1; /* Invalidate the other end. */
222 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
226 gl_state.current_syntax_table = tmp_table;
227 gl_state.old_prop = tmp_table;
228 if (EQ (Fsyntax_table_p (tmp_table), Qt))
230 gl_state.use_global = 0;
232 else if (CONSP (tmp_table))
234 gl_state.use_global = 1;
235 gl_state.global_code = tmp_table;
237 else
239 gl_state.use_global = 0;
240 gl_state.current_syntax_table = current_buffer->syntax_table;
243 while (!NULL_INTERVAL_P (i))
245 if (cnt && !EQ (tmp_table, textget (i->plist, Qsyntax_table)))
247 if (count > 0)
248 gl_state.right_ok = 0;
249 else
250 gl_state.left_ok = 0;
251 break;
253 else if (cnt == INTERVALS_AT_ONCE)
255 if (count > 0)
256 gl_state.right_ok = 1;
257 else
258 gl_state.left_ok = 1;
259 break;
261 cnt++;
262 i = count > 0 ? next_interval (i) : previous_interval (i);
264 if (NULL_INTERVAL_P (i))
265 { /* This property goes to the end. */
266 if (count > 0)
267 gl_state.e_property = gl_state.stop;
268 else
269 gl_state.b_property = gl_state.start;
271 else
273 if (count > 0)
275 gl_state.e_property = i->position - gl_state.offset;
276 gl_state.forward_i = i;
278 else
280 gl_state.b_property = i->position + LENGTH (i) - 1 - gl_state.offset;
281 gl_state.backward_i = i;
286 /* Returns TRUE if char at CHARPOS is quoted.
287 Global syntax-table data should be set up already to be good at CHARPOS
288 or after. On return global syntax data is good for lookup at CHARPOS. */
290 static int
291 char_quoted (charpos, bytepos)
292 register int charpos, bytepos;
294 register enum syntaxcode code;
295 register int beg = BEGV;
296 register int quoted = 0;
297 int orig = charpos;
299 DEC_BOTH (charpos, bytepos);
301 while (bytepos >= beg)
303 UPDATE_SYNTAX_TABLE_BACKWARD (charpos);
304 code = SYNTAX (FETCH_CHAR (bytepos));
305 if (! (code == Scharquote || code == Sescape))
306 break;
308 DEC_BOTH (charpos, bytepos);
309 quoted = !quoted;
312 UPDATE_SYNTAX_TABLE (orig);
313 return quoted;
316 /* Return the bytepos one character after BYTEPOS.
317 We assume that BYTEPOS is not at the end of the buffer. */
319 INLINE int
320 inc_bytepos (bytepos)
321 int bytepos;
323 if (NILP (current_buffer->enable_multibyte_characters))
324 return bytepos + 1;
326 INC_POS (bytepos);
327 return bytepos;
330 /* Return the bytepos one character before BYTEPOS.
331 We assume that BYTEPOS is not at the start of the buffer. */
333 INLINE int
334 dec_bytepos (bytepos)
335 int bytepos;
337 if (NILP (current_buffer->enable_multibyte_characters))
338 return bytepos - 1;
340 DEC_POS (bytepos);
341 return bytepos;
344 /* Find a defun-start that is the last one before POS (or nearly the last).
345 We record what we find, so that another call in the same area
346 can return the same value right away.
348 There is no promise at which position the global syntax data is
349 valid on return from the subroutine, so the caller should explicitly
350 update the global data. */
352 static int
353 find_defun_start (pos, pos_byte)
354 int pos, pos_byte;
356 int opoint = PT, opoint_byte = PT_BYTE;
358 /* Use previous finding, if it's valid and applies to this inquiry. */
359 if (current_buffer == find_start_buffer
360 /* Reuse the defun-start even if POS is a little farther on.
361 POS might be in the next defun, but that's ok.
362 Our value may not be the best possible, but will still be usable. */
363 && pos <= find_start_pos + 1000
364 && pos >= find_start_value
365 && BEGV == find_start_begv
366 && MODIFF == find_start_modiff)
367 return find_start_value;
369 /* Back up to start of line. */
370 scan_newline (pos, pos_byte, BEGV, BEGV_BYTE, -1, 1);
372 /* We optimize syntax-table lookup for rare updates. Thus we accept
373 only those `^\s(' which are good in global _and_ text-property
374 syntax-tables. */
375 gl_state.current_syntax_table = current_buffer->syntax_table;
376 gl_state.use_global = 0;
377 if (open_paren_in_column_0_is_defun_start)
379 while (PT > BEGV)
381 /* Open-paren at start of line means we may have found our
382 defun-start. */
383 if (SYNTAX (FETCH_CHAR (PT_BYTE)) == Sopen)
385 SETUP_SYNTAX_TABLE (PT + 1, -1); /* Try again... */
386 if (SYNTAX (FETCH_CHAR (PT_BYTE)) == Sopen)
387 break;
388 /* Now fallback to the default value. */
389 gl_state.current_syntax_table = current_buffer->syntax_table;
390 gl_state.use_global = 0;
392 /* Move to beg of previous line. */
393 scan_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, -2, 1);
397 /* Record what we found, for the next try. */
398 find_start_value = PT;
399 find_start_value_byte = PT_BYTE;
400 find_start_buffer = current_buffer;
401 find_start_modiff = MODIFF;
402 find_start_begv = BEGV;
403 find_start_pos = pos;
405 TEMP_SET_PT_BOTH (opoint, opoint_byte);
407 return find_start_value;
410 /* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
412 static int
413 prev_char_comend_first (pos, pos_byte)
414 int pos, pos_byte;
416 int c, val;
418 DEC_BOTH (pos, pos_byte);
419 UPDATE_SYNTAX_TABLE_BACKWARD (pos);
420 c = FETCH_CHAR (pos_byte);
421 val = SYNTAX_COMEND_FIRST (c);
422 UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
423 return val;
426 /* Return the SYNTAX_COMSTART_FIRST of the character before POS, POS_BYTE. */
428 static int
429 prev_char_comstart_first (pos, pos_byte)
430 int pos, pos_byte;
432 int c, val;
434 DEC_BOTH (pos, pos_byte);
435 UPDATE_SYNTAX_TABLE_BACKWARD (pos);
436 c = FETCH_CHAR (pos_byte);
437 val = SYNTAX_COMSTART_FIRST (c);
438 UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
439 return val;
442 /* Checks whether charpos FROM is at the end of a comment.
443 FROM_BYTE is the bytepos corresponding to FROM.
444 Do not move back before STOP.
446 Return a positive value if we find a comment ending at FROM/FROM_BYTE;
447 return -1 otherwise.
449 If successful, store the charpos of the comment's beginning
450 into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR.
452 Global syntax data remains valid for backward search starting at
453 the returned value (or at FROM, if the search was not successful). */
455 static int
456 back_comment (from, from_byte, stop, comnested, comstyle, charpos_ptr, bytepos_ptr)
457 int from, from_byte, stop;
458 int comnested, comstyle;
459 int *charpos_ptr, *bytepos_ptr;
461 /* Look back, counting the parity of string-quotes,
462 and recording the comment-starters seen.
463 When we reach a safe place, assume that's not in a string;
464 then step the main scan to the earliest comment-starter seen
465 an even number of string quotes away from the safe place.
467 OFROM[I] is position of the earliest comment-starter seen
468 which is I+2X quotes from the comment-end.
469 PARITY is current parity of quotes from the comment end. */
470 int parity = 0;
471 int my_stringend = 0;
472 int string_lossage = 0;
473 int comment_end = from;
474 int comment_end_byte = from_byte;
475 int comstart_pos = 0;
476 int comstart_byte;
477 /* Value that PARITY had, when we reached the position
478 in COMSTART_POS. */
479 int comstart_parity = 0;
480 int scanstart = from - 1;
481 /* Place where the containing defun starts,
482 or 0 if we didn't come across it yet. */
483 int defun_start = 0;
484 int defun_start_byte = 0;
485 register enum syntaxcode code;
486 int nesting = 1; /* current comment nesting */
487 int c;
489 /* At beginning of range to scan, we're outside of strings;
490 that determines quote parity to the comment-end. */
491 while (from != stop)
493 int temp_byte;
495 /* Move back and examine a character. */
496 DEC_BOTH (from, from_byte);
497 UPDATE_SYNTAX_TABLE_BACKWARD (from);
499 c = FETCH_CHAR (from_byte);
500 code = SYNTAX (c);
502 /* If this char is the second of a 2-char comment end sequence,
503 back up and give the pair the appropriate syntax. */
504 if (from > stop && SYNTAX_COMEND_SECOND (c)
505 && prev_char_comend_first (from, from_byte))
507 code = Sendcomment;
508 DEC_BOTH (from, from_byte);
509 UPDATE_SYNTAX_TABLE_BACKWARD (from);
510 c = FETCH_CHAR (from_byte);
513 /* If this char starts a 2-char comment start sequence,
514 treat it like a 1-char comment starter. */
515 if (from < scanstart && SYNTAX_COMSTART_FIRST (c))
517 temp_byte = inc_bytepos (from_byte);
518 UPDATE_SYNTAX_TABLE_FORWARD (from + 1);
519 if (SYNTAX_COMSTART_SECOND (FETCH_CHAR (temp_byte))
520 && comstyle == SYNTAX_COMMENT_STYLE (FETCH_CHAR (temp_byte)))
521 code = Scomment;
522 UPDATE_SYNTAX_TABLE_BACKWARD (from);
524 else if (code == Scomment && comstyle != SYNTAX_COMMENT_STYLE (c))
525 /* Ignore comment starters of a different style. */
526 continue;
528 /* Ignore escaped characters, except comment-enders. */
529 if (code != Sendcomment && char_quoted (from, from_byte))
530 continue;
532 /* Track parity of quotes. */
533 if (code == Sstring)
535 parity ^= 1;
536 if (my_stringend == 0)
537 my_stringend = c;
538 /* If we have two kinds of string delimiters.
539 There's no way to grok this scanning backwards. */
540 else if (my_stringend != c)
541 string_lossage = 1;
544 if (code == Sstring_fence || code == Scomment_fence)
546 parity ^= 1;
547 if (my_stringend == 0)
548 my_stringend
549 = code == Sstring_fence ? ST_STRING_STYLE : ST_COMMENT_STYLE;
550 /* If we have two kinds of string delimiters.
551 There's no way to grok this scanning backwards. */
552 else if (my_stringend != (code == Sstring_fence
553 ? ST_STRING_STYLE : ST_COMMENT_STYLE))
554 string_lossage = 1;
557 if (code == Scomment)
558 /* We've already checked that it is the relevant comstyle. */
560 if (comnested && --nesting <= 0 && parity == 0 && !string_lossage)
561 /* nested comments have to be balanced, so we don't need to
562 keep looking for earlier ones. We use here the same (slightly
563 incorrect) reasoning as below: since it is followed by uniform
564 paired string quotes, this comment-start has to be outside of
565 strings, else the comment-end itself would be inside a string. */
566 goto done;
568 /* Record comment-starters according to that
569 quote-parity to the comment-end. */
570 comstart_parity = parity;
571 comstart_pos = from;
572 comstart_byte = from_byte;
575 /* If we find another earlier comment-ender,
576 any comment-starts earlier than that don't count
577 (because they go with the earlier comment-ender). */
578 if (code == Sendcomment
579 && SYNTAX_COMMENT_STYLE (FETCH_CHAR (from_byte)) == comstyle)
581 if (comnested)
582 nesting++;
583 else
584 break;
587 /* Assume a defun-start point is outside of strings. */
588 if (code == Sopen
589 && (from == stop
590 || (temp_byte = dec_bytepos (from_byte),
591 FETCH_CHAR (temp_byte) == '\n')))
593 defun_start = from;
594 defun_start_byte = from_byte;
595 break;
599 if (comstart_pos == 0)
601 from = comment_end;
602 from_byte = comment_end_byte;
603 UPDATE_SYNTAX_TABLE_FORWARD (comment_end - 1);
605 /* If the earliest comment starter
606 is followed by uniform paired string quotes or none,
607 we know it can't be inside a string
608 since if it were then the comment ender would be inside one.
609 So it does start a comment. Skip back to it. */
610 else if (!comnested && comstart_parity == 0 && !string_lossage)
612 from = comstart_pos;
613 from_byte = comstart_byte;
614 /* Globals are correct now. */
616 else
618 /* We had two kinds of string delimiters mixed up
619 together. Decode this going forwards.
620 Scan fwd from the previous comment ender
621 to the one in question; this records where we
622 last passed a comment starter. */
623 struct lisp_parse_state state;
624 /* If we did not already find the defun start, find it now. */
625 if (defun_start == 0)
627 defun_start = find_defun_start (comment_end, comment_end_byte);
628 defun_start_byte = find_start_value_byte;
630 scan_sexps_forward (&state,
631 defun_start, defun_start_byte,
632 comment_end - 1, -10000, 0, Qnil, 0);
633 if (state.incomment)
635 /* scan_sexps_forward changed the direction of search in
636 global variables, so we need to update it completely. */
638 from = state.comstr_start;
640 else
642 from = comment_end;
644 from_byte = CHAR_TO_BYTE (from);
645 UPDATE_SYNTAX_TABLE_FORWARD (from - 1);
648 done:
649 *charpos_ptr = from;
650 *bytepos_ptr = from_byte;
652 return (from == comment_end) ? -1 : from;
655 DEFUN ("syntax-table-p", Fsyntax_table_p, Ssyntax_table_p, 1, 1, 0,
656 "Return t if OBJECT is a syntax table.\n\
657 Currently, any char-table counts as a syntax table.")
658 (object)
659 Lisp_Object object;
661 if (CHAR_TABLE_P (object)
662 && EQ (XCHAR_TABLE (object)->purpose, Qsyntax_table))
663 return Qt;
664 return Qnil;
667 static void
668 check_syntax_table (obj)
669 Lisp_Object obj;
671 if (!(CHAR_TABLE_P (obj)
672 && EQ (XCHAR_TABLE (obj)->purpose, Qsyntax_table)))
673 wrong_type_argument (Qsyntax_table_p, obj);
676 DEFUN ("syntax-table", Fsyntax_table, Ssyntax_table, 0, 0, 0,
677 "Return the current syntax table.\n\
678 This is the one specified by the current buffer.")
681 return current_buffer->syntax_table;
684 DEFUN ("standard-syntax-table", Fstandard_syntax_table,
685 Sstandard_syntax_table, 0, 0, 0,
686 "Return the standard syntax table.\n\
687 This is the one used for new buffers.")
690 return Vstandard_syntax_table;
693 DEFUN ("copy-syntax-table", Fcopy_syntax_table, Scopy_syntax_table, 0, 1, 0,
694 "Construct a new syntax table and return it.\n\
695 It is a copy of the TABLE, which defaults to the standard syntax table.")
696 (table)
697 Lisp_Object table;
699 Lisp_Object copy;
701 if (!NILP (table))
702 check_syntax_table (table);
703 else
704 table = Vstandard_syntax_table;
706 copy = Fcopy_sequence (table);
708 /* Only the standard syntax table should have a default element.
709 Other syntax tables should inherit from parents instead. */
710 XCHAR_TABLE (copy)->defalt = Qnil;
712 /* Copied syntax tables should all have parents.
713 If we copied one with no parent, such as the standard syntax table,
714 use the standard syntax table as the copy's parent. */
715 if (NILP (XCHAR_TABLE (copy)->parent))
716 Fset_char_table_parent (copy, Vstandard_syntax_table);
717 return copy;
720 DEFUN ("set-syntax-table", Fset_syntax_table, Sset_syntax_table, 1, 1, 0,
721 "Select a new syntax table for the current buffer.\n\
722 One argument, a syntax table.")
723 (table)
724 Lisp_Object table;
726 int idx;
727 check_syntax_table (table);
728 current_buffer->syntax_table = table;
729 /* Indicate that this buffer now has a specified syntax table. */
730 idx = PER_BUFFER_VAR_IDX (syntax_table);
731 SET_PER_BUFFER_VALUE_P (current_buffer, idx, 1);
732 return table;
735 /* Convert a letter which signifies a syntax code
736 into the code it signifies.
737 This is used by modify-syntax-entry, and other things. */
739 unsigned char syntax_spec_code[0400] =
740 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
741 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
742 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
743 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
744 (char) Swhitespace, (char) Scomment_fence, (char) Sstring, 0377,
745 (char) Smath, 0377, 0377, (char) Squote,
746 (char) Sopen, (char) Sclose, 0377, 0377,
747 0377, (char) Swhitespace, (char) Spunct, (char) Scharquote,
748 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
749 0377, 0377, 0377, 0377,
750 (char) Scomment, 0377, (char) Sendcomment, 0377,
751 (char) Sinherit, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
752 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
753 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword,
754 0377, 0377, 0377, 0377, (char) Sescape, 0377, 0377, (char) Ssymbol,
755 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
756 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
757 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword,
758 0377, 0377, 0377, 0377, (char) Sstring_fence, 0377, 0377, 0377
761 /* Indexed by syntax code, give the letter that describes it. */
763 char syntax_code_spec[16] =
765 ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@',
766 '!', '|'
769 /* Indexed by syntax code, give the object (cons of syntax code and
770 nil) to be stored in syntax table. Since these objects can be
771 shared among syntax tables, we generate them in advance. By
772 sharing objects, the function `describe-syntax' can give a more
773 compact listing. */
774 static Lisp_Object Vsyntax_code_object;
777 /* Look up the value for CHARACTER in syntax table TABLE's parent
778 and its parents. SYNTAX_ENTRY calls this, when TABLE itself has nil
779 for CHARACTER. It's actually used only when not compiled with GCC. */
781 Lisp_Object
782 syntax_parent_lookup (table, character)
783 Lisp_Object table;
784 int character;
786 Lisp_Object value;
788 while (1)
790 table = XCHAR_TABLE (table)->parent;
791 if (NILP (table))
792 return Qnil;
794 value = XCHAR_TABLE (table)->contents[character];
795 if (!NILP (value))
796 return value;
800 DEFUN ("char-syntax", Fchar_syntax, Schar_syntax, 1, 1, 0,
801 "Return the syntax code of CHARACTER, described by a character.\n\
802 For example, if CHARACTER is a word constituent,\n\
803 the character `w' is returned.\n\
804 The characters that correspond to various syntax codes\n\
805 are listed in the documentation of `modify-syntax-entry'.")
806 (character)
807 Lisp_Object character;
809 int char_int;
810 gl_state.current_syntax_table = current_buffer->syntax_table;
812 gl_state.use_global = 0;
813 CHECK_NUMBER (character, 0);
814 char_int = XINT (character);
815 return make_number (syntax_code_spec[(int) SYNTAX (char_int)]);
818 DEFUN ("matching-paren", Fmatching_paren, Smatching_paren, 1, 1, 0,
819 "Return the matching parenthesis of CHARACTER, or nil if none.")
820 (character)
821 Lisp_Object character;
823 int char_int, code;
824 gl_state.current_syntax_table = current_buffer->syntax_table;
825 gl_state.use_global = 0;
826 CHECK_NUMBER (character, 0);
827 char_int = XINT (character);
828 code = SYNTAX (char_int);
829 if (code == Sopen || code == Sclose)
830 return SYNTAX_MATCH (char_int);
831 return Qnil;
834 /* This comment supplies the doc string for modify-syntax-entry,
835 for make-docfile to see. We cannot put this in the real DEFUN
836 due to limits in the Unix cpp.
838 DEFUN ("modify-syntax-entry", foo, bar, 2, 3, 0,
839 "Set syntax for character CHAR according to string S.\n\
840 The syntax is changed only for table TABLE, which defaults to\n\
841 the current buffer's syntax table.\n\
842 The first character of S should be one of the following:\n\
843 Space or - whitespace syntax. w word constituent.\n\
844 _ symbol constituent. . punctuation.\n\
845 ( open-parenthesis. ) close-parenthesis.\n\
846 \" string quote. \\ escape.\n\
847 $ paired delimiter. ' expression quote or prefix operator.\n\
848 < comment starter. > comment ender.\n\
849 / character-quote. @ inherit from `standard-syntax-table'.\n\
851 Only single-character comment start and end sequences are represented thus.\n\
852 Two-character sequences are represented as described below.\n\
853 The second character of S is the matching parenthesis,\n\
854 used only if the first character is `(' or `)'.\n\
855 Any additional characters are flags.\n\
856 Defined flags are the characters 1, 2, 3, 4, b, p, and n.\n\
857 1 means CHAR is the start of a two-char comment start sequence.\n\
858 2 means CHAR is the second character of such a sequence.\n\
859 3 means CHAR is the start of a two-char comment end sequence.\n\
860 4 means CHAR is the second character of such a sequence.\n\
862 There can be up to two orthogonal comment sequences. This is to support\n\
863 language modes such as C++. By default, all comment sequences are of style\n\
864 a, but you can set the comment sequence style to b (on the second character\n\
865 of a comment-start, or the first character of a comment-end sequence) using\n\
866 this flag:\n\
867 b means CHAR is part of comment sequence b.\n\
868 n means CHAR is part of a nestable comment sequence.\n\
870 p means CHAR is a prefix character for `backward-prefix-chars';\n\
871 such characters are treated as whitespace when they occur\n\
872 between expressions.")
873 (char, s, table)
876 DEFUN ("modify-syntax-entry", Fmodify_syntax_entry, Smodify_syntax_entry, 2, 3,
877 /* I really don't know why this is interactive
878 help-form should at least be made useful whilst reading the second arg
880 "cSet syntax for character: \nsSet syntax for %s to: ",
881 0 /* See immediately above */)
882 (c, newentry, syntax_table)
883 Lisp_Object c, newentry, syntax_table;
885 register unsigned char *p;
886 register enum syntaxcode code;
887 int val;
888 Lisp_Object match;
890 CHECK_NUMBER (c, 0);
891 CHECK_STRING (newentry, 1);
893 if (NILP (syntax_table))
894 syntax_table = current_buffer->syntax_table;
895 else
896 check_syntax_table (syntax_table);
898 p = XSTRING (newentry)->data;
899 code = (enum syntaxcode) syntax_spec_code[*p++];
900 if (((int) code & 0377) == 0377)
901 error ("invalid syntax description letter: %c", p[-1]);
903 if (code == Sinherit)
905 SET_RAW_SYNTAX_ENTRY (syntax_table, XINT (c), Qnil);
906 return Qnil;
909 if (*p)
911 int len;
912 int character = (STRING_CHAR_AND_LENGTH
913 (p, STRING_BYTES (XSTRING (newentry)) - 1, len));
914 XSETINT (match, character);
915 if (XFASTINT (match) == ' ')
916 match = Qnil;
917 p += len;
919 else
920 match = Qnil;
922 val = (int) code;
923 while (*p)
924 switch (*p++)
926 case '1':
927 val |= 1 << 16;
928 break;
930 case '2':
931 val |= 1 << 17;
932 break;
934 case '3':
935 val |= 1 << 18;
936 break;
938 case '4':
939 val |= 1 << 19;
940 break;
942 case 'p':
943 val |= 1 << 20;
944 break;
946 case 'b':
947 val |= 1 << 21;
948 break;
950 case 'n':
951 val |= 1 << 22;
952 break;
955 if (val < XVECTOR (Vsyntax_code_object)->size && NILP (match))
956 newentry = XVECTOR (Vsyntax_code_object)->contents[val];
957 else
958 /* Since we can't use a shared object, let's make a new one. */
959 newentry = Fcons (make_number (val), match);
961 SET_RAW_SYNTAX_ENTRY (syntax_table, XINT (c), newentry);
963 return Qnil;
966 /* Dump syntax table to buffer in human-readable format */
968 static void
969 describe_syntax (value)
970 Lisp_Object value;
972 register enum syntaxcode code;
973 char desc, start1, start2, end1, end2, prefix, comstyle, comnested;
974 char str[2];
975 Lisp_Object first, match_lisp;
977 Findent_to (make_number (16), make_number (1));
979 if (NILP (value))
981 insert_string ("default\n");
982 return;
985 if (CHAR_TABLE_P (value))
987 insert_string ("deeper char-table ...\n");
988 return;
991 if (!CONSP (value))
993 insert_string ("invalid\n");
994 return;
997 first = XCAR (value);
998 match_lisp = XCDR (value);
1000 if (!INTEGERP (first) || !(NILP (match_lisp) || INTEGERP (match_lisp)))
1002 insert_string ("invalid\n");
1003 return;
1006 code = (enum syntaxcode) (XINT (first) & 0377);
1007 start1 = (XINT (first) >> 16) & 1;
1008 start2 = (XINT (first) >> 17) & 1;
1009 end1 = (XINT (first) >> 18) & 1;
1010 end2 = (XINT (first) >> 19) & 1;
1011 prefix = (XINT (first) >> 20) & 1;
1012 comstyle = (XINT (first) >> 21) & 1;
1013 comnested = (XINT (first) >> 22) & 1;
1015 if ((int) code < 0 || (int) code >= (int) Smax)
1017 insert_string ("invalid");
1018 return;
1020 desc = syntax_code_spec[(int) code];
1022 str[0] = desc, str[1] = 0;
1023 insert (str, 1);
1025 if (NILP (match_lisp))
1026 insert (" ", 1);
1027 else
1028 insert_char (XINT (match_lisp));
1030 if (start1)
1031 insert ("1", 1);
1032 if (start2)
1033 insert ("2", 1);
1035 if (end1)
1036 insert ("3", 1);
1037 if (end2)
1038 insert ("4", 1);
1040 if (prefix)
1041 insert ("p", 1);
1042 if (comstyle)
1043 insert ("b", 1);
1044 if (comnested)
1045 insert ("n", 1);
1047 insert_string ("\twhich means: ");
1049 switch (SWITCH_ENUM_CAST (code))
1051 case Swhitespace:
1052 insert_string ("whitespace"); break;
1053 case Spunct:
1054 insert_string ("punctuation"); break;
1055 case Sword:
1056 insert_string ("word"); break;
1057 case Ssymbol:
1058 insert_string ("symbol"); break;
1059 case Sopen:
1060 insert_string ("open"); break;
1061 case Sclose:
1062 insert_string ("close"); break;
1063 case Squote:
1064 insert_string ("quote"); break;
1065 case Sstring:
1066 insert_string ("string"); break;
1067 case Smath:
1068 insert_string ("math"); break;
1069 case Sescape:
1070 insert_string ("escape"); break;
1071 case Scharquote:
1072 insert_string ("charquote"); break;
1073 case Scomment:
1074 insert_string ("comment"); break;
1075 case Sendcomment:
1076 insert_string ("endcomment"); break;
1077 default:
1078 insert_string ("invalid");
1079 return;
1082 if (!NILP (match_lisp))
1084 insert_string (", matches ");
1085 insert_char (XINT (match_lisp));
1088 if (start1)
1089 insert_string (",\n\t is the first character of a comment-start sequence");
1090 if (start2)
1091 insert_string (",\n\t is the second character of a comment-start sequence");
1093 if (end1)
1094 insert_string (",\n\t is the first character of a comment-end sequence");
1095 if (end2)
1096 insert_string (",\n\t is the second character of a comment-end sequence");
1097 if (comstyle)
1098 insert_string (" (comment style b)");
1099 if (comnested)
1100 insert_string (" (nestable)");
1102 if (prefix)
1103 insert_string (",\n\t is a prefix character for `backward-prefix-chars'");
1105 insert_string ("\n");
1108 static Lisp_Object
1109 describe_syntax_1 (vector)
1110 Lisp_Object vector;
1112 struct buffer *old = current_buffer;
1113 set_buffer_internal (XBUFFER (Vstandard_output));
1114 describe_vector (vector, Qnil, describe_syntax, 0, Qnil, Qnil, (int *) 0, 0);
1115 while (! NILP (XCHAR_TABLE (vector)->parent))
1117 vector = XCHAR_TABLE (vector)->parent;
1118 insert_string ("\nThe parent syntax table is:");
1119 describe_vector (vector, Qnil, describe_syntax, 0, Qnil, Qnil,
1120 (int *) 0, 0);
1123 call0 (intern ("help-mode"));
1124 set_buffer_internal (old);
1125 return Qnil;
1128 DEFUN ("describe-syntax", Fdescribe_syntax, Sdescribe_syntax, 0, 0, "",
1129 "Describe the syntax specifications in the syntax table.\n\
1130 The descriptions are inserted in a buffer, which is then displayed.")
1133 internal_with_output_to_temp_buffer
1134 ("*Help*", describe_syntax_1, current_buffer->syntax_table);
1136 return Qnil;
1139 int parse_sexp_ignore_comments;
1141 /* Return the position across COUNT words from FROM.
1142 If that many words cannot be found before the end of the buffer, return 0.
1143 COUNT negative means scan backward and stop at word beginning. */
1146 scan_words (from, count)
1147 register int from, count;
1149 register int beg = BEGV;
1150 register int end = ZV;
1151 register int from_byte = CHAR_TO_BYTE (from);
1152 register enum syntaxcode code;
1153 int ch0, ch1;
1155 immediate_quit = 1;
1156 QUIT;
1158 SETUP_SYNTAX_TABLE (from, count);
1160 while (count > 0)
1162 while (1)
1164 if (from == end)
1166 immediate_quit = 0;
1167 return 0;
1169 UPDATE_SYNTAX_TABLE_FORWARD (from);
1170 ch0 = FETCH_CHAR (from_byte);
1171 code = SYNTAX (ch0);
1172 INC_BOTH (from, from_byte);
1173 if (words_include_escapes
1174 && (code == Sescape || code == Scharquote))
1175 break;
1176 if (code == Sword)
1177 break;
1179 /* Now CH0 is a character which begins a word and FROM is the
1180 position of the next character. */
1181 while (1)
1183 if (from == end) break;
1184 UPDATE_SYNTAX_TABLE_FORWARD (from);
1185 ch1 = FETCH_CHAR (from_byte);
1186 code = SYNTAX (ch1);
1187 if (!(words_include_escapes
1188 && (code == Sescape || code == Scharquote)))
1189 if (code != Sword || WORD_BOUNDARY_P (ch0, ch1))
1190 break;
1191 INC_BOTH (from, from_byte);
1192 ch0 = ch1;
1194 count--;
1196 while (count < 0)
1198 while (1)
1200 if (from == beg)
1202 immediate_quit = 0;
1203 return 0;
1205 DEC_BOTH (from, from_byte);
1206 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1207 ch1 = FETCH_CHAR (from_byte);
1208 code = SYNTAX (ch1);
1209 if (words_include_escapes
1210 && (code == Sescape || code == Scharquote))
1211 break;
1212 if (code == Sword)
1213 break;
1215 /* Now CH1 is a character which ends a word and FROM is the
1216 position of it. */
1217 while (1)
1219 int temp_byte;
1221 if (from == beg)
1222 break;
1223 temp_byte = dec_bytepos (from_byte);
1224 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1225 ch0 = FETCH_CHAR (temp_byte);
1226 code = SYNTAX (ch0);
1227 if (!(words_include_escapes
1228 && (code == Sescape || code == Scharquote)))
1229 if (code != Sword || WORD_BOUNDARY_P (ch0, ch1))
1230 break;
1231 DEC_BOTH (from, from_byte);
1232 ch1 = ch0;
1234 count++;
1237 immediate_quit = 0;
1239 return from;
1242 DEFUN ("forward-word", Fforward_word, Sforward_word, 1, 1, "p",
1243 "Move point forward ARG words (backward if ARG is negative).\n\
1244 Normally returns t.\n\
1245 If an edge of the buffer or a field boundary is reached, point is left there\n\
1246 and the function returns nil. Field boundaries are not noticed if\n\
1247 `inhibit-field-text-motion' is non-nil.")
1248 (count)
1249 Lisp_Object count;
1251 int orig_val, val;
1252 CHECK_NUMBER (count, 0);
1254 val = orig_val = scan_words (PT, XINT (count));
1255 if (! orig_val)
1256 val = XINT (count) > 0 ? ZV : BEGV;
1258 /* Avoid jumping out of an input field. */
1259 val = XFASTINT (Fconstrain_to_field (make_number (val), make_number (PT),
1260 Qt, Qnil));
1262 SET_PT (val);
1263 return val == orig_val ? Qt : Qnil;
1266 Lisp_Object skip_chars ();
1268 DEFUN ("skip-chars-forward", Fskip_chars_forward, Sskip_chars_forward, 1, 2, 0,
1269 "Move point forward, stopping before a char not in STRING, or at pos LIM.\n\
1270 STRING is like the inside of a `[...]' in a regular expression\n\
1271 except that `]' is never special and `\\' quotes `^', `-' or `\\'\n\
1272 (but not as the end of a range; quoting is never needed there).\n\
1273 Thus, with arg \"a-zA-Z\", this skips letters stopping before first nonletter.\n\
1274 With arg \"^a-zA-Z\", skips nonletters stopping before first letter.\n\
1275 Returns the distance traveled, either zero or positive.")
1276 (string, lim)
1277 Lisp_Object string, lim;
1279 return skip_chars (1, 0, string, lim);
1282 DEFUN ("skip-chars-backward", Fskip_chars_backward, Sskip_chars_backward, 1, 2, 0,
1283 "Move point backward, stopping after a char not in STRING, or at pos LIM.\n\
1284 See `skip-chars-forward' for details.\n\
1285 Returns the distance traveled, either zero or negative.")
1286 (string, lim)
1287 Lisp_Object string, lim;
1289 return skip_chars (0, 0, string, lim);
1292 DEFUN ("skip-syntax-forward", Fskip_syntax_forward, Sskip_syntax_forward, 1, 2, 0,
1293 "Move point forward across chars in specified syntax classes.\n\
1294 SYNTAX is a string of syntax code characters.\n\
1295 Stop before a char whose syntax is not in SYNTAX, or at position LIM.\n\
1296 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.\n\
1297 This function returns the distance traveled, either zero or positive.")
1298 (syntax, lim)
1299 Lisp_Object syntax, lim;
1301 return skip_chars (1, 1, syntax, lim);
1304 DEFUN ("skip-syntax-backward", Fskip_syntax_backward, Sskip_syntax_backward, 1, 2, 0,
1305 "Move point backward across chars in specified syntax classes.\n\
1306 SYNTAX is a string of syntax code characters.\n\
1307 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.\n\
1308 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.\n\
1309 This function returns the distance traveled, either zero or negative.")
1310 (syntax, lim)
1311 Lisp_Object syntax, lim;
1313 return skip_chars (0, 1, syntax, lim);
1316 static Lisp_Object
1317 skip_chars (forwardp, syntaxp, string, lim)
1318 int forwardp, syntaxp;
1319 Lisp_Object string, lim;
1321 register unsigned int c;
1322 register int ch;
1323 unsigned char fastmap[0400];
1324 /* If SYNTAXP is 0, STRING may contain multi-byte form of characters
1325 of which codes don't fit in FASTMAP. In that case, we set the
1326 first byte of multibyte form (i.e. base leading-code) in FASTMAP
1327 and set the actual ranges of characters in CHAR_RANGES. In the
1328 form "X-Y" of STRING, both X and Y must belong to the same
1329 character set because a range striding across character sets is
1330 meaningless. */
1331 int *char_ranges;
1332 int n_char_ranges = 0;
1333 int negate = 0;
1334 register int i, i_byte;
1335 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
1336 int string_multibyte;
1337 int size_byte;
1339 CHECK_STRING (string, 0);
1340 char_ranges = (int *) alloca (XSTRING (string)->size * (sizeof (int)) * 2);
1341 string_multibyte = STRING_MULTIBYTE (string);
1342 size_byte = STRING_BYTES (XSTRING (string));
1344 if (NILP (lim))
1345 XSETINT (lim, forwardp ? ZV : BEGV);
1346 else
1347 CHECK_NUMBER_COERCE_MARKER (lim, 0);
1349 /* In any case, don't allow scan outside bounds of buffer. */
1350 if (XINT (lim) > ZV)
1351 XSETFASTINT (lim, ZV);
1352 if (XINT (lim) < BEGV)
1353 XSETFASTINT (lim, BEGV);
1355 bzero (fastmap, sizeof fastmap);
1357 i = 0, i_byte = 0;
1359 if (i_byte < size_byte
1360 && XSTRING (string)->data[0] == '^')
1362 negate = 1; i++, i_byte++;
1365 /* Find the characters specified and set their elements of fastmap.
1366 If syntaxp, each character counts as itself.
1367 Otherwise, handle backslashes and ranges specially. */
1369 while (i_byte < size_byte)
1371 int c_leading_code = XSTRING (string)->data[i_byte];
1373 FETCH_STRING_CHAR_ADVANCE (c, string, i, i_byte);
1375 /* Convert multibyteness between what the string has
1376 and what the buffer has. */
1377 if (multibyte)
1378 c = unibyte_char_to_multibyte (c);
1379 else
1380 c &= 0377;
1382 if (syntaxp)
1383 fastmap[syntax_spec_code[c & 0377]] = 1;
1384 else
1386 if (c == '\\')
1388 if (i_byte == size_byte)
1389 break;
1391 c_leading_code = XSTRING (string)->data[i_byte];
1392 FETCH_STRING_CHAR_ADVANCE (c, string, i, i_byte);
1394 if (i_byte < size_byte
1395 && XSTRING (string)->data[i_byte] == '-')
1397 unsigned int c2, c2_leading_code;
1399 /* Skip over the dash. */
1400 i++, i_byte++;
1402 if (i_byte == size_byte)
1403 break;
1405 /* Get the end of the range. */
1406 c2_leading_code = XSTRING (string)->data[i_byte];
1407 FETCH_STRING_CHAR_ADVANCE (c2, string, i, i_byte);
1409 if (SINGLE_BYTE_CHAR_P (c))
1411 if (! SINGLE_BYTE_CHAR_P (c2))
1412 error ("Invalid charcter range: %s",
1413 XSTRING (string)->data);
1414 while (c <= c2)
1416 fastmap[c] = 1;
1417 c++;
1420 else
1422 if (c_leading_code != c2_leading_code)
1423 error ("Invalid charcter range: %s",
1424 XSTRING (string)->data);
1425 fastmap[c_leading_code] = 1;
1426 if (c <= c2)
1428 char_ranges[n_char_ranges++] = c;
1429 char_ranges[n_char_ranges++] = c2;
1433 else
1435 fastmap[c_leading_code] = 1;
1436 if (!SINGLE_BYTE_CHAR_P (c))
1438 char_ranges[n_char_ranges++] = c;
1439 char_ranges[n_char_ranges++] = c;
1445 /* If ^ was the first character, complement the fastmap. In
1446 addition, as all multibyte characters have possibility of
1447 matching, set all entries for base leading codes, which is
1448 harmless even if SYNTAXP is 1. */
1450 if (negate)
1451 for (i = 0; i < sizeof fastmap; i++)
1453 if (!multibyte || !BASE_LEADING_CODE_P (i))
1454 fastmap[i] ^= 1;
1455 else
1456 fastmap[i] = 1;
1460 int start_point = PT;
1461 int pos = PT;
1462 int pos_byte = PT_BYTE;
1464 immediate_quit = 1;
1465 if (syntaxp)
1467 SETUP_SYNTAX_TABLE (pos, forwardp ? 1 : -1);
1468 if (forwardp)
1470 if (multibyte)
1472 if (pos < XINT (lim))
1473 while (fastmap[(int) SYNTAX (FETCH_CHAR (pos_byte))])
1475 /* Since we already checked for multibyteness,
1476 avoid using INC_BOTH which checks again. */
1477 INC_POS (pos_byte);
1478 pos++;
1479 if (pos >= XINT (lim))
1480 break;
1481 UPDATE_SYNTAX_TABLE_FORWARD (pos);
1484 else
1486 while (pos < XINT (lim)
1487 && fastmap[(int) SYNTAX (FETCH_BYTE (pos))])
1489 pos++;
1490 UPDATE_SYNTAX_TABLE_FORWARD (pos);
1494 else
1496 if (multibyte)
1498 while (pos > XINT (lim))
1500 int savepos = pos_byte;
1501 /* Since we already checked for multibyteness,
1502 avoid using DEC_BOTH which checks again. */
1503 pos--;
1504 DEC_POS (pos_byte);
1505 UPDATE_SYNTAX_TABLE_BACKWARD (pos);
1506 if (!fastmap[(int) SYNTAX (FETCH_CHAR (pos_byte))])
1508 pos++;
1509 pos_byte = savepos;
1510 break;
1514 else
1516 if (pos > XINT (lim))
1517 while (fastmap[(int) SYNTAX (FETCH_BYTE (pos - 1))])
1519 pos--;
1520 if (pos <= XINT (lim))
1521 break;
1522 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
1527 else
1529 if (forwardp)
1531 if (multibyte)
1532 while (pos < XINT (lim) && fastmap[(c = FETCH_BYTE (pos_byte))])
1534 if (!BASE_LEADING_CODE_P (c))
1535 INC_BOTH (pos, pos_byte);
1536 else if (n_char_ranges)
1538 /* We much check CHAR_RANGES for a multibyte
1539 character. */
1540 ch = FETCH_MULTIBYTE_CHAR (pos_byte);
1541 for (i = 0; i < n_char_ranges; i += 2)
1542 if ((ch >= char_ranges[i] && ch <= char_ranges[i + 1]))
1543 break;
1544 if (!(negate ^ (i < n_char_ranges)))
1545 break;
1547 INC_BOTH (pos, pos_byte);
1549 else
1551 if (!negate) break;
1552 INC_BOTH (pos, pos_byte);
1555 else
1556 while (pos < XINT (lim) && fastmap[FETCH_BYTE (pos)])
1557 pos++;
1559 else
1561 if (multibyte)
1562 while (pos > XINT (lim))
1564 int savepos = pos_byte;
1565 DEC_BOTH (pos, pos_byte);
1566 if (fastmap[(c = FETCH_BYTE (pos_byte))])
1568 if (!BASE_LEADING_CODE_P (c))
1570 else if (n_char_ranges)
1572 /* We much check CHAR_RANGES for a multibyte
1573 character. */
1574 ch = FETCH_MULTIBYTE_CHAR (pos_byte);
1575 for (i = 0; i < n_char_ranges; i += 2)
1576 if (ch >= char_ranges[i] && ch <= char_ranges[i + 1])
1577 break;
1578 if (!(negate ^ (i < n_char_ranges)))
1580 pos++;
1581 pos_byte = savepos;
1582 break;
1585 else
1586 if (!negate)
1588 pos++;
1589 pos_byte = savepos;
1590 break;
1593 else
1595 pos++;
1596 pos_byte = savepos;
1597 break;
1600 else
1601 while (pos > XINT (lim) && fastmap[FETCH_BYTE (pos - 1)])
1602 pos--;
1606 #if 0 /* Not needed now that a position in mid-character
1607 cannot be specified in Lisp. */
1608 if (multibyte
1609 /* INC_POS or DEC_POS might have moved POS over LIM. */
1610 && (forwardp ? (pos > XINT (lim)) : (pos < XINT (lim))))
1611 pos = XINT (lim);
1612 #endif
1614 if (! multibyte)
1615 pos_byte = pos;
1617 SET_PT_BOTH (pos, pos_byte);
1618 immediate_quit = 0;
1620 return make_number (PT - start_point);
1624 /* Jump over a comment, assuming we are at the beginning of one.
1625 FROM is the current position.
1626 FROM_BYTE is the bytepos corresponding to FROM.
1627 Do not move past STOP (a charpos).
1628 The comment over which we have to jump is of style STYLE
1629 (either SYNTAX_COMMENT_STYLE(foo) or ST_COMMENT_STYLE).
1630 NESTING should be positive to indicate the nesting at the beginning
1631 for nested comments and should be zero or negative else.
1632 ST_COMMENT_STYLE cannot be nested.
1633 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
1634 (or 0 If the search cannot start in the middle of a two-character).
1636 If successful, return 1 and store the charpos of the comment's end
1637 into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR.
1638 Else, return 0 and store the charpos STOP into *CHARPOS_PTR, the
1639 corresponding bytepos into *BYTEPOS_PTR and the current nesting
1640 (as defined for state.incomment) in *INCOMMENT_PTR.
1642 The comment end is the last character of the comment rather than the
1643 character just after the comment.
1645 Global syntax data is assumed to initially be valid for FROM and
1646 remains valid for forward search starting at the returned position. */
1648 static int
1649 forw_comment (from, from_byte, stop, nesting, style, prev_syntax,
1650 charpos_ptr, bytepos_ptr, incomment_ptr)
1651 int from, from_byte, stop;
1652 int nesting, style, prev_syntax;
1653 int *charpos_ptr, *bytepos_ptr, *incomment_ptr;
1655 register int c, c1;
1656 register enum syntaxcode code;
1657 register int syntax;
1659 if (nesting <= 0) nesting = -1;
1661 /* Enter the loop in the middle so that we find
1662 a 2-char comment ender if we start in the middle of it. */
1663 syntax = prev_syntax;
1664 if (syntax != 0) goto forw_incomment;
1666 while (1)
1668 if (from == stop)
1670 *incomment_ptr = nesting;
1671 *charpos_ptr = from;
1672 *bytepos_ptr = from_byte;
1673 return 0;
1675 c = FETCH_CHAR (from_byte);
1676 syntax = SYNTAX_WITH_FLAGS (c);
1677 code = syntax & 0xff;
1678 if (code == Sendcomment
1679 && SYNTAX_FLAGS_COMMENT_STYLE (syntax) == style
1680 && --nesting <= 0)
1681 /* we have encountered a comment end of the same style
1682 as the comment sequence which began this comment
1683 section */
1684 break;
1685 if (code == Scomment_fence
1686 && style == ST_COMMENT_STYLE)
1687 /* we have encountered a comment end of the same style
1688 as the comment sequence which began this comment
1689 section. */
1690 break;
1691 if (nesting > 0
1692 && code == Scomment
1693 && SYNTAX_FLAGS_COMMENT_STYLE (syntax) == style)
1694 /* we have encountered a nested comment of the same style
1695 as the comment sequence which began this comment section */
1696 nesting++;
1697 INC_BOTH (from, from_byte);
1698 UPDATE_SYNTAX_TABLE_FORWARD (from);
1700 forw_incomment:
1701 if (from < stop && SYNTAX_FLAGS_COMEND_FIRST (syntax)
1702 && SYNTAX_FLAGS_COMMENT_STYLE (syntax) == style
1703 && (c1 = FETCH_CHAR (from_byte),
1704 SYNTAX_COMEND_SECOND (c1)))
1706 if (--nesting <= 0)
1707 /* we have encountered a comment end of the same style
1708 as the comment sequence which began this comment
1709 section */
1710 break;
1711 else
1713 INC_BOTH (from, from_byte);
1714 UPDATE_SYNTAX_TABLE_FORWARD (from);
1717 if (nesting > 0
1718 && from < stop
1719 && SYNTAX_FLAGS_COMSTART_FIRST (syntax)
1720 && (c1 = FETCH_CHAR (from_byte),
1721 SYNTAX_COMMENT_STYLE (c1) == style
1722 && SYNTAX_COMSTART_SECOND (c1)))
1723 /* we have encountered a nested comment of the same style
1724 as the comment sequence which began this comment
1725 section */
1727 INC_BOTH (from, from_byte);
1728 UPDATE_SYNTAX_TABLE_FORWARD (from);
1729 nesting++;
1732 *charpos_ptr = from;
1733 *bytepos_ptr = from_byte;
1734 return 1;
1737 DEFUN ("forward-comment", Fforward_comment, Sforward_comment, 1, 1, 0,
1738 "Move forward across up to N comments. If N is negative, move backward.\n\
1739 Stop scanning if we find something other than a comment or whitespace.\n\
1740 Set point to where scanning stops.\n\
1741 If N comments are found as expected, with nothing except whitespace\n\
1742 between them, return t; otherwise return nil.")
1743 (count)
1744 Lisp_Object count;
1746 register int from;
1747 int from_byte;
1748 register int stop;
1749 register int c, c1;
1750 register enum syntaxcode code;
1751 int comstyle = 0; /* style of comment encountered */
1752 int comnested = 0; /* whether the comment is nestable or not */
1753 int found;
1754 int count1;
1755 int out_charpos, out_bytepos;
1756 int dummy;
1758 CHECK_NUMBER (count, 0);
1759 count1 = XINT (count);
1760 stop = count1 > 0 ? ZV : BEGV;
1762 immediate_quit = 1;
1763 QUIT;
1765 from = PT;
1766 from_byte = PT_BYTE;
1768 SETUP_SYNTAX_TABLE (from, count1);
1769 while (count1 > 0)
1773 int comstart_first;
1775 if (from == stop)
1777 SET_PT_BOTH (from, from_byte);
1778 immediate_quit = 0;
1779 return Qnil;
1781 c = FETCH_CHAR (from_byte);
1782 code = SYNTAX (c);
1783 comstart_first = SYNTAX_COMSTART_FIRST (c);
1784 comnested = SYNTAX_COMMENT_NESTED (c);
1785 comstyle = SYNTAX_COMMENT_STYLE (c);
1786 INC_BOTH (from, from_byte);
1787 UPDATE_SYNTAX_TABLE_FORWARD (from);
1788 if (from < stop && comstart_first
1789 && (c1 = FETCH_CHAR (from_byte),
1790 SYNTAX_COMSTART_SECOND (c1)))
1792 /* We have encountered a comment start sequence and we
1793 are ignoring all text inside comments. We must record
1794 the comment style this sequence begins so that later,
1795 only a comment end of the same style actually ends
1796 the comment section. */
1797 code = Scomment;
1798 comstyle = SYNTAX_COMMENT_STYLE (c1);
1799 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
1800 INC_BOTH (from, from_byte);
1801 UPDATE_SYNTAX_TABLE_FORWARD (from);
1803 /* FIXME: here we ignore 2-char endcomments while we don't
1804 when going backwards. */
1806 while (code == Swhitespace || code == Sendcomment);
1808 if (code == Scomment_fence)
1809 comstyle = ST_COMMENT_STYLE;
1810 else if (code != Scomment)
1812 immediate_quit = 0;
1813 DEC_BOTH (from, from_byte);
1814 SET_PT_BOTH (from, from_byte);
1815 return Qnil;
1817 /* We're at the start of a comment. */
1818 found = forw_comment (from, from_byte, stop, comnested, comstyle, 0,
1819 &out_charpos, &out_bytepos, &dummy);
1820 from = out_charpos; from_byte = out_bytepos;
1821 if (!found)
1823 immediate_quit = 0;
1824 SET_PT_BOTH (from, from_byte);
1825 return Qnil;
1827 INC_BOTH (from, from_byte);
1828 UPDATE_SYNTAX_TABLE_FORWARD (from);
1829 /* We have skipped one comment. */
1830 count1--;
1833 while (count1 < 0)
1835 while (1)
1837 int quoted, comstart_second;
1839 if (from <= stop)
1841 SET_PT_BOTH (BEGV, BEGV_BYTE);
1842 immediate_quit = 0;
1843 return Qnil;
1846 DEC_BOTH (from, from_byte);
1847 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
1848 quoted = char_quoted (from, from_byte);
1849 if (quoted)
1851 DEC_BOTH (from, from_byte);
1852 goto leave;
1854 c = FETCH_CHAR (from_byte);
1855 code = SYNTAX (c);
1856 comstyle = 0;
1857 comnested = SYNTAX_COMMENT_NESTED (c);
1858 if (code == Sendcomment)
1859 comstyle = SYNTAX_COMMENT_STYLE (c);
1860 comstart_second = SYNTAX_COMSTART_SECOND (c);
1861 if (from > stop && SYNTAX_COMEND_SECOND (c)
1862 && prev_char_comend_first (from, from_byte)
1863 && !char_quoted (from - 1, dec_bytepos (from_byte)))
1865 /* We must record the comment style encountered so that
1866 later, we can match only the proper comment begin
1867 sequence of the same style. */
1868 DEC_BOTH (from, from_byte);
1869 code = Sendcomment;
1870 /* Calling char_quoted, above, set up global syntax position
1871 at the new value of FROM. */
1872 c1 = FETCH_CHAR (from_byte);
1873 comstyle = SYNTAX_COMMENT_STYLE (c1);
1874 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
1876 if (from > stop && comstart_second
1877 && prev_char_comstart_first (from, from_byte)
1878 && !char_quoted (from - 1, dec_bytepos (from_byte)))
1880 code = Scomment;
1881 DEC_BOTH (from, from_byte);
1884 if (code == Scomment_fence)
1886 /* Skip until first preceding unquoted comment_fence. */
1887 int found = 0, ini = from, ini_byte = from_byte;
1889 while (1)
1891 DEC_BOTH (from, from_byte);
1892 if (from == stop)
1893 break;
1894 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1895 c = FETCH_CHAR (from_byte);
1896 if (SYNTAX (c) == Scomment_fence
1897 && !char_quoted (from, from_byte))
1899 found = 1;
1900 break;
1903 if (found == 0)
1905 from = ini; /* Set point to ini + 1. */
1906 from_byte = ini_byte;
1907 goto leave;
1910 else if (code == Sendcomment)
1912 found = back_comment (from, from_byte, stop, comnested, comstyle,
1913 &out_charpos, &out_bytepos);
1914 if (found == -1)
1916 #if 0 /* cc-mode (and maybe others) relies on the bogus behavior. */
1917 /* Failure: we should go back to the end of this
1918 not-quite-endcomment. */
1919 if (SYNTAX(c) != code)
1920 /* It was a two-char Sendcomment. */
1921 INC_BOTH (from, from_byte);
1922 goto leave;
1923 #endif
1925 else
1926 /* We have skipped one comment. */
1927 from = out_charpos, from_byte = out_bytepos;
1928 break;
1930 else if (code != Swhitespace && code != Scomment)
1932 leave:
1933 immediate_quit = 0;
1934 INC_BOTH (from, from_byte);
1935 SET_PT_BOTH (from, from_byte);
1936 return Qnil;
1940 count1++;
1943 SET_PT_BOTH (from, from_byte);
1944 immediate_quit = 0;
1945 return Qt;
1948 /* Return syntax code of character C if C is a single byte character
1949 or `multibyte_symbol_p' is zero. Otherwise, retrun Ssymbol. */
1951 #define SYNTAX_WITH_MULTIBYTE_CHECK(c) \
1952 ((SINGLE_BYTE_CHAR_P (c) || !multibyte_symbol_p) \
1953 ? SYNTAX (c) : Ssymbol)
1955 static Lisp_Object
1956 scan_lists (from, count, depth, sexpflag)
1957 register int from;
1958 int count, depth, sexpflag;
1960 Lisp_Object val;
1961 register int stop = count > 0 ? ZV : BEGV;
1962 register int c, c1;
1963 int stringterm;
1964 int quoted;
1965 int mathexit = 0;
1966 register enum syntaxcode code, temp_code;
1967 int min_depth = depth; /* Err out if depth gets less than this. */
1968 int comstyle = 0; /* style of comment encountered */
1969 int comnested = 0; /* whether the comment is nestable or not */
1970 int temp_pos;
1971 int last_good = from;
1972 int found;
1973 int from_byte;
1974 int out_bytepos, out_charpos;
1975 int temp, dummy;
1976 int multibyte_symbol_p = sexpflag && multibyte_syntax_as_symbol;
1978 if (depth > 0) min_depth = 0;
1980 if (from > ZV) from = ZV;
1981 if (from < BEGV) from = BEGV;
1983 from_byte = CHAR_TO_BYTE (from);
1985 immediate_quit = 1;
1986 QUIT;
1988 SETUP_SYNTAX_TABLE (from, count);
1989 while (count > 0)
1991 while (from < stop)
1993 int comstart_first, prefix;
1994 UPDATE_SYNTAX_TABLE_FORWARD (from);
1995 c = FETCH_CHAR (from_byte);
1996 code = SYNTAX_WITH_MULTIBYTE_CHECK (c);
1997 comstart_first = SYNTAX_COMSTART_FIRST (c);
1998 comnested = SYNTAX_COMMENT_NESTED (c);
1999 comstyle = SYNTAX_COMMENT_STYLE (c);
2000 prefix = SYNTAX_PREFIX (c);
2001 if (depth == min_depth)
2002 last_good = from;
2003 INC_BOTH (from, from_byte);
2004 UPDATE_SYNTAX_TABLE_FORWARD (from);
2005 if (from < stop && comstart_first
2006 && SYNTAX_COMSTART_SECOND (FETCH_CHAR (from_byte))
2007 && parse_sexp_ignore_comments)
2009 /* we have encountered a comment start sequence and we
2010 are ignoring all text inside comments. We must record
2011 the comment style this sequence begins so that later,
2012 only a comment end of the same style actually ends
2013 the comment section */
2014 code = Scomment;
2015 c1 = FETCH_CHAR (from_byte);
2016 comstyle = SYNTAX_COMMENT_STYLE (c1);
2017 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
2018 INC_BOTH (from, from_byte);
2019 UPDATE_SYNTAX_TABLE_FORWARD (from);
2022 if (prefix)
2023 continue;
2025 switch (SWITCH_ENUM_CAST (code))
2027 case Sescape:
2028 case Scharquote:
2029 if (from == stop) goto lose;
2030 INC_BOTH (from, from_byte);
2031 /* treat following character as a word constituent */
2032 case Sword:
2033 case Ssymbol:
2034 if (depth || !sexpflag) break;
2035 /* This word counts as a sexp; return at end of it. */
2036 while (from < stop)
2038 UPDATE_SYNTAX_TABLE_FORWARD (from);
2040 /* Some compilers can't handle this inside the switch. */
2041 c = FETCH_CHAR (from_byte);
2042 temp = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2043 switch (temp)
2045 case Scharquote:
2046 case Sescape:
2047 INC_BOTH (from, from_byte);
2048 if (from == stop) goto lose;
2049 break;
2050 case Sword:
2051 case Ssymbol:
2052 case Squote:
2053 break;
2054 default:
2055 goto done;
2057 INC_BOTH (from, from_byte);
2059 goto done;
2061 case Scomment_fence:
2062 comstyle = ST_COMMENT_STYLE;
2063 /* FALLTHROUGH */
2064 case Scomment:
2065 if (!parse_sexp_ignore_comments) break;
2066 UPDATE_SYNTAX_TABLE_FORWARD (from);
2067 found = forw_comment (from, from_byte, stop,
2068 comnested, comstyle, 0,
2069 &out_charpos, &out_bytepos, &dummy);
2070 from = out_charpos, from_byte = out_bytepos;
2071 if (!found)
2073 if (depth == 0)
2074 goto done;
2075 goto lose;
2077 INC_BOTH (from, from_byte);
2078 UPDATE_SYNTAX_TABLE_FORWARD (from);
2079 break;
2081 case Smath:
2082 if (!sexpflag)
2083 break;
2084 if (from != stop && c == FETCH_CHAR (from_byte))
2086 INC_BOTH (from, from_byte);
2088 if (mathexit)
2090 mathexit = 0;
2091 goto close1;
2093 mathexit = 1;
2095 case Sopen:
2096 if (!++depth) goto done;
2097 break;
2099 case Sclose:
2100 close1:
2101 if (!--depth) goto done;
2102 if (depth < min_depth)
2103 Fsignal (Qscan_error,
2104 Fcons (build_string ("Containing expression ends prematurely"),
2105 Fcons (make_number (last_good),
2106 Fcons (make_number (from), Qnil))));
2107 break;
2109 case Sstring:
2110 case Sstring_fence:
2111 temp_pos = dec_bytepos (from_byte);
2112 stringterm = FETCH_CHAR (temp_pos);
2113 while (1)
2115 if (from >= stop) goto lose;
2116 UPDATE_SYNTAX_TABLE_FORWARD (from);
2117 c = FETCH_CHAR (from_byte);
2118 if (code == Sstring
2119 ? c == stringterm
2120 : SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring_fence)
2121 break;
2123 /* Some compilers can't handle this inside the switch. */
2124 temp = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2125 switch (temp)
2127 case Scharquote:
2128 case Sescape:
2129 INC_BOTH (from, from_byte);
2131 INC_BOTH (from, from_byte);
2133 INC_BOTH (from, from_byte);
2134 if (!depth && sexpflag) goto done;
2135 break;
2139 /* Reached end of buffer. Error if within object, return nil if between */
2140 if (depth) goto lose;
2142 immediate_quit = 0;
2143 return Qnil;
2145 /* End of object reached */
2146 done:
2147 count--;
2150 while (count < 0)
2152 while (from > stop)
2154 DEC_BOTH (from, from_byte);
2155 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2156 c = FETCH_CHAR (from_byte);
2157 code = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2158 if (depth == min_depth)
2159 last_good = from;
2160 comstyle = 0;
2161 comnested = SYNTAX_COMMENT_NESTED (c);
2162 if (code == Sendcomment)
2163 comstyle = SYNTAX_COMMENT_STYLE (c);
2164 if (from > stop && SYNTAX_COMEND_SECOND (c)
2165 && prev_char_comend_first (from, from_byte)
2166 && parse_sexp_ignore_comments)
2168 /* We must record the comment style encountered so that
2169 later, we can match only the proper comment begin
2170 sequence of the same style. */
2171 DEC_BOTH (from, from_byte);
2172 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2173 code = Sendcomment;
2174 c1 = FETCH_CHAR (from_byte);
2175 comstyle = SYNTAX_COMMENT_STYLE (c1);
2176 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
2179 /* Quoting turns anything except a comment-ender
2180 into a word character. Note that this cannot be true
2181 if we decremented FROM in the if-statement above. */
2182 if (code != Sendcomment && char_quoted (from, from_byte))
2183 code = Sword;
2184 else if (SYNTAX_PREFIX (c))
2185 continue;
2187 switch (SWITCH_ENUM_CAST (code))
2189 case Sword:
2190 case Ssymbol:
2191 case Sescape:
2192 case Scharquote:
2193 if (depth || !sexpflag) break;
2194 /* This word counts as a sexp; count object finished
2195 after passing it. */
2196 while (from > stop)
2198 temp_pos = from_byte;
2199 if (! NILP (current_buffer->enable_multibyte_characters))
2200 DEC_POS (temp_pos);
2201 else
2202 temp_pos--;
2203 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2204 c1 = FETCH_CHAR (temp_pos);
2205 temp_code = SYNTAX_WITH_MULTIBYTE_CHECK (c1);
2206 /* Don't allow comment-end to be quoted. */
2207 if (temp_code == Sendcomment)
2208 goto done2;
2209 quoted = char_quoted (from - 1, temp_pos);
2210 if (quoted)
2212 DEC_BOTH (from, from_byte);
2213 temp_pos = dec_bytepos (temp_pos);
2214 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2216 c1 = FETCH_CHAR (temp_pos);
2217 temp_code = SYNTAX_WITH_MULTIBYTE_CHECK (c1);
2218 if (! (quoted || temp_code == Sword
2219 || temp_code == Ssymbol
2220 || temp_code == Squote))
2221 goto done2;
2222 DEC_BOTH (from, from_byte);
2224 goto done2;
2226 case Smath:
2227 if (!sexpflag)
2228 break;
2229 temp_pos = dec_bytepos (from_byte);
2230 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2231 if (from != stop && c == FETCH_CHAR (temp_pos))
2232 DEC_BOTH (from, from_byte);
2233 if (mathexit)
2235 mathexit = 0;
2236 goto open2;
2238 mathexit = 1;
2240 case Sclose:
2241 if (!++depth) goto done2;
2242 break;
2244 case Sopen:
2245 open2:
2246 if (!--depth) goto done2;
2247 if (depth < min_depth)
2248 Fsignal (Qscan_error,
2249 Fcons (build_string ("Containing expression ends prematurely"),
2250 Fcons (make_number (last_good),
2251 Fcons (make_number (from), Qnil))));
2252 break;
2254 case Sendcomment:
2255 if (!parse_sexp_ignore_comments)
2256 break;
2257 found = back_comment (from, from_byte, stop, comnested, comstyle,
2258 &out_charpos, &out_bytepos);
2259 /* FIXME: if found == -1, then it really wasn't a comment-end.
2260 For single-char Sendcomment, we can't do much about it apart
2261 from skipping the char.
2262 For 2-char endcomments, we could try again, taking both
2263 chars as separate entities, but it's a lot of trouble
2264 for very little gain, so we don't bother either. -sm */
2265 if (found != -1)
2266 from = out_charpos, from_byte = out_bytepos;
2267 break;
2269 case Scomment_fence:
2270 case Sstring_fence:
2271 while (1)
2273 DEC_BOTH (from, from_byte);
2274 if (from == stop) goto lose;
2275 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2276 if (!char_quoted (from, from_byte)
2277 && (c = FETCH_CHAR (from_byte),
2278 SYNTAX_WITH_MULTIBYTE_CHECK (c) == code))
2279 break;
2281 if (code == Sstring_fence && !depth && sexpflag) goto done2;
2282 break;
2284 case Sstring:
2285 stringterm = FETCH_CHAR (from_byte);
2286 while (1)
2288 if (from == stop) goto lose;
2289 temp_pos = from_byte;
2290 if (! NILP (current_buffer->enable_multibyte_characters))
2291 DEC_POS (temp_pos);
2292 else
2293 temp_pos--;
2294 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2295 if (!char_quoted (from - 1, temp_pos)
2296 && stringterm == FETCH_CHAR (temp_pos))
2297 break;
2298 DEC_BOTH (from, from_byte);
2300 DEC_BOTH (from, from_byte);
2301 if (!depth && sexpflag) goto done2;
2302 break;
2306 /* Reached start of buffer. Error if within object, return nil if between */
2307 if (depth) goto lose;
2309 immediate_quit = 0;
2310 return Qnil;
2312 done2:
2313 count++;
2317 immediate_quit = 0;
2318 XSETFASTINT (val, from);
2319 return val;
2321 lose:
2322 Fsignal (Qscan_error,
2323 Fcons (build_string ("Unbalanced parentheses"),
2324 Fcons (make_number (last_good),
2325 Fcons (make_number (from), Qnil))));
2327 /* NOTREACHED */
2330 DEFUN ("scan-lists", Fscan_lists, Sscan_lists, 3, 3, 0,
2331 "Scan from character number FROM by COUNT lists.\n\
2332 Returns the character number of the position thus found.\n\
2334 If DEPTH is nonzero, paren depth begins counting from that value,\n\
2335 only places where the depth in parentheses becomes zero\n\
2336 are candidates for stopping; COUNT such places are counted.\n\
2337 Thus, a positive value for DEPTH means go out levels.\n\
2339 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.\n\
2341 If the beginning or end of (the accessible part of) the buffer is reached\n\
2342 and the depth is wrong, an error is signaled.\n\
2343 If the depth is right but the count is not used up, nil is returned.")
2344 (from, count, depth)
2345 Lisp_Object from, count, depth;
2347 CHECK_NUMBER (from, 0);
2348 CHECK_NUMBER (count, 1);
2349 CHECK_NUMBER (depth, 2);
2351 return scan_lists (XINT (from), XINT (count), XINT (depth), 0);
2354 DEFUN ("scan-sexps", Fscan_sexps, Sscan_sexps, 2, 2, 0,
2355 "Scan from character number FROM by COUNT balanced expressions.\n\
2356 If COUNT is negative, scan backwards.\n\
2357 Returns the character number of the position thus found.\n\
2359 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.\n\
2361 If the beginning or end of (the accessible part of) the buffer is reached\n\
2362 in the middle of a parenthetical grouping, an error is signaled.\n\
2363 If the beginning or end is reached between groupings\n\
2364 but before count is used up, nil is returned.")
2365 (from, count)
2366 Lisp_Object from, count;
2368 CHECK_NUMBER (from, 0);
2369 CHECK_NUMBER (count, 1);
2371 return scan_lists (XINT (from), XINT (count), 0, 1);
2374 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars, Sbackward_prefix_chars,
2375 0, 0, 0,
2376 "Move point backward over any number of chars with prefix syntax.\n\
2377 This includes chars with \"quote\" or \"prefix\" syntax (' or p).")
2380 int beg = BEGV;
2381 int opoint = PT;
2382 int opoint_byte = PT_BYTE;
2383 int pos = PT;
2384 int pos_byte = PT_BYTE;
2385 int c;
2387 if (pos <= beg)
2389 SET_PT_BOTH (opoint, opoint_byte);
2391 return Qnil;
2394 SETUP_SYNTAX_TABLE (pos, -1);
2396 DEC_BOTH (pos, pos_byte);
2398 while (!char_quoted (pos, pos_byte)
2399 /* Previous statement updates syntax table. */
2400 && ((c = FETCH_CHAR (pos_byte), SYNTAX (c) == Squote)
2401 || SYNTAX_PREFIX (c)))
2403 opoint = pos;
2404 opoint_byte = pos_byte;
2406 if (pos + 1 > beg)
2407 DEC_BOTH (pos, pos_byte);
2410 SET_PT_BOTH (opoint, opoint_byte);
2412 return Qnil;
2415 /* Parse forward from FROM / FROM_BYTE to END,
2416 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
2417 and return a description of the state of the parse at END.
2418 If STOPBEFORE is nonzero, stop at the start of an atom.
2419 If COMMENTSTOP is 1, stop at the start of a comment.
2420 If COMMENTSTOP is -1, stop at the start or end of a comment,
2421 after the beginning of a string, or after the end of a string. */
2423 static void
2424 scan_sexps_forward (stateptr, from, from_byte, end, targetdepth,
2425 stopbefore, oldstate, commentstop)
2426 struct lisp_parse_state *stateptr;
2427 register int from;
2428 int end, targetdepth, stopbefore;
2429 Lisp_Object oldstate;
2430 int commentstop;
2432 struct lisp_parse_state state;
2434 register enum syntaxcode code;
2435 int c1;
2436 int comnested;
2437 struct level { int last, prev; };
2438 struct level levelstart[100];
2439 register struct level *curlevel = levelstart;
2440 struct level *endlevel = levelstart + 100;
2441 register int depth; /* Paren depth of current scanning location.
2442 level - levelstart equals this except
2443 when the depth becomes negative. */
2444 int mindepth; /* Lowest DEPTH value seen. */
2445 int start_quoted = 0; /* Nonzero means starting after a char quote */
2446 Lisp_Object tem;
2447 int prev_from; /* Keep one character before FROM. */
2448 int prev_from_byte;
2449 int prev_from_syntax;
2450 int boundary_stop = commentstop == -1;
2451 int nofence;
2452 int found;
2453 int out_bytepos, out_charpos;
2454 int temp;
2456 prev_from = from;
2457 prev_from_byte = from_byte;
2458 if (from != BEGV)
2459 DEC_BOTH (prev_from, prev_from_byte);
2461 /* Use this macro instead of `from++'. */
2462 #define INC_FROM \
2463 do { prev_from = from; \
2464 prev_from_byte = from_byte; \
2465 prev_from_syntax \
2466 = SYNTAX_WITH_FLAGS (FETCH_CHAR (prev_from_byte)); \
2467 INC_BOTH (from, from_byte); \
2468 UPDATE_SYNTAX_TABLE_FORWARD (from); \
2469 } while (0)
2471 immediate_quit = 1;
2472 QUIT;
2474 if (NILP (oldstate))
2476 depth = 0;
2477 state.instring = -1;
2478 state.incomment = 0;
2479 state.comstyle = 0; /* comment style a by default. */
2480 state.comstr_start = -1; /* no comment/string seen. */
2482 else
2484 tem = Fcar (oldstate);
2485 if (!NILP (tem))
2486 depth = XINT (tem);
2487 else
2488 depth = 0;
2490 oldstate = Fcdr (oldstate);
2491 oldstate = Fcdr (oldstate);
2492 oldstate = Fcdr (oldstate);
2493 tem = Fcar (oldstate);
2494 /* Check whether we are inside string_fence-style string: */
2495 state.instring = (!NILP (tem)
2496 ? (INTEGERP (tem) ? XINT (tem) : ST_STRING_STYLE)
2497 : -1);
2499 oldstate = Fcdr (oldstate);
2500 tem = Fcar (oldstate);
2501 state.incomment = (!NILP (tem)
2502 ? (INTEGERP (tem) ? XINT (tem) : -1)
2503 : 0);
2505 oldstate = Fcdr (oldstate);
2506 tem = Fcar (oldstate);
2507 start_quoted = !NILP (tem);
2509 /* if the eighth element of the list is nil, we are in comment
2510 style a. If it is non-nil, we are in comment style b */
2511 oldstate = Fcdr (oldstate);
2512 oldstate = Fcdr (oldstate);
2513 tem = Fcar (oldstate);
2514 state.comstyle = NILP (tem) ? 0 : (EQ (tem, Qsyntax_table)
2515 ? ST_COMMENT_STYLE : 1);
2517 oldstate = Fcdr (oldstate);
2518 tem = Fcar (oldstate);
2519 state.comstr_start = NILP (tem) ? -1 : XINT (tem) ;
2520 oldstate = Fcdr (oldstate);
2521 tem = Fcar (oldstate);
2522 while (!NILP (tem)) /* >= second enclosing sexps. */
2524 /* curlevel++->last ran into compiler bug on Apollo */
2525 curlevel->last = XINT (Fcar (tem));
2526 if (++curlevel == endlevel)
2527 error ("Nesting too deep for parser");
2528 curlevel->prev = -1;
2529 curlevel->last = -1;
2530 tem = Fcdr (tem);
2533 state.quoted = 0;
2534 mindepth = depth;
2536 curlevel->prev = -1;
2537 curlevel->last = -1;
2539 SETUP_SYNTAX_TABLE (prev_from, 1);
2540 prev_from_syntax = SYNTAX_WITH_FLAGS (FETCH_CHAR (prev_from_byte));
2541 UPDATE_SYNTAX_TABLE_FORWARD (from);
2543 /* Enter the loop at a place appropriate for initial state. */
2545 if (state.incomment)
2546 goto startincomment;
2547 if (state.instring >= 0)
2549 nofence = state.instring != ST_STRING_STYLE;
2550 if (start_quoted)
2551 goto startquotedinstring;
2552 goto startinstring;
2554 else if (start_quoted)
2555 goto startquoted;
2557 #if 0 /* This seems to be redundant with the identical code above. */
2558 SETUP_SYNTAX_TABLE (prev_from, 1);
2559 prev_from_syntax = SYNTAX_WITH_FLAGS (FETCH_CHAR (prev_from_byte));
2560 UPDATE_SYNTAX_TABLE_FORWARD (from);
2561 #endif
2563 while (from < end)
2565 INC_FROM;
2566 code = prev_from_syntax & 0xff;
2568 if (code == Scomment)
2570 state.comstyle = SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax);
2571 state.incomment = (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax) ?
2572 1 : -1);
2573 state.comstr_start = prev_from;
2575 else if (code == Scomment_fence)
2577 /* Record the comment style we have entered so that only
2578 the comment-end sequence of the same style actually
2579 terminates the comment section. */
2580 state.comstyle = ST_COMMENT_STYLE;
2581 state.incomment = -1;
2582 state.comstr_start = prev_from;
2583 code = Scomment;
2585 else if (from < end)
2586 if (SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax))
2587 if (c1 = FETCH_CHAR (from_byte),
2588 SYNTAX_COMSTART_SECOND (c1))
2589 /* Duplicate code to avoid a complex if-expression
2590 which causes trouble for the SGI compiler. */
2592 /* Record the comment style we have entered so that only
2593 the comment-end sequence of the same style actually
2594 terminates the comment section. */
2595 state.comstyle = SYNTAX_COMMENT_STYLE (FETCH_CHAR (from_byte));
2596 comnested = SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax);
2597 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
2598 state.incomment = comnested ? 1 : -1;
2599 state.comstr_start = prev_from;
2600 INC_FROM;
2601 code = Scomment;
2604 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax))
2605 continue;
2606 switch (SWITCH_ENUM_CAST (code))
2608 case Sescape:
2609 case Scharquote:
2610 if (stopbefore) goto stop; /* this arg means stop at sexp start */
2611 curlevel->last = prev_from;
2612 startquoted:
2613 if (from == end) goto endquoted;
2614 INC_FROM;
2615 goto symstarted;
2616 /* treat following character as a word constituent */
2617 case Sword:
2618 case Ssymbol:
2619 if (stopbefore) goto stop; /* this arg means stop at sexp start */
2620 curlevel->last = prev_from;
2621 symstarted:
2622 while (from < end)
2624 /* Some compilers can't handle this inside the switch. */
2625 temp = SYNTAX (FETCH_CHAR (from_byte));
2626 switch (temp)
2628 case Scharquote:
2629 case Sescape:
2630 INC_FROM;
2631 if (from == end) goto endquoted;
2632 break;
2633 case Sword:
2634 case Ssymbol:
2635 case Squote:
2636 break;
2637 default:
2638 goto symdone;
2640 INC_FROM;
2642 symdone:
2643 curlevel->prev = curlevel->last;
2644 break;
2646 case Scomment:
2647 if (commentstop || boundary_stop) goto done;
2648 startincomment:
2649 /* The (from == BEGV) test was to enter the loop in the middle so
2650 that we find a 2-char comment ender even if we start in the
2651 middle of it. We don't want to do that if we're just at the
2652 beginning of the comment (think of (*) ... (*)). */
2653 found = forw_comment (from, from_byte, end,
2654 state.incomment, state.comstyle,
2655 (from == BEGV || from < state.comstr_start + 3)
2656 ? 0 : prev_from_syntax,
2657 &out_charpos, &out_bytepos, &state.incomment);
2658 from = out_charpos; from_byte = out_bytepos;
2659 /* Beware! prev_from and friends are invalid now.
2660 Luckily, the `done' doesn't use them and the INC_FROM
2661 sets them to a sane value without looking at them. */
2662 if (!found) goto done;
2663 INC_FROM;
2664 state.incomment = 0;
2665 state.comstyle = 0; /* reset the comment style */
2666 if (boundary_stop) goto done;
2667 break;
2669 case Sopen:
2670 if (stopbefore) goto stop; /* this arg means stop at sexp start */
2671 depth++;
2672 /* curlevel++->last ran into compiler bug on Apollo */
2673 curlevel->last = prev_from;
2674 if (++curlevel == endlevel)
2675 error ("Nesting too deep for parser");
2676 curlevel->prev = -1;
2677 curlevel->last = -1;
2678 if (targetdepth == depth) goto done;
2679 break;
2681 case Sclose:
2682 depth--;
2683 if (depth < mindepth)
2684 mindepth = depth;
2685 if (curlevel != levelstart)
2686 curlevel--;
2687 curlevel->prev = curlevel->last;
2688 if (targetdepth == depth) goto done;
2689 break;
2691 case Sstring:
2692 case Sstring_fence:
2693 state.comstr_start = from - 1;
2694 if (stopbefore) goto stop; /* this arg means stop at sexp start */
2695 curlevel->last = prev_from;
2696 state.instring = (code == Sstring
2697 ? (FETCH_CHAR (prev_from_byte))
2698 : ST_STRING_STYLE);
2699 if (boundary_stop) goto done;
2700 startinstring:
2702 nofence = state.instring != ST_STRING_STYLE;
2704 while (1)
2706 int c;
2708 if (from >= end) goto done;
2709 c = FETCH_CHAR (from_byte);
2710 /* Some compilers can't handle this inside the switch. */
2711 temp = SYNTAX (c);
2713 /* Check TEMP here so that if the char has
2714 a syntax-table property which says it is NOT
2715 a string character, it does not end the string. */
2716 if (nofence && c == state.instring && temp == Sstring)
2717 break;
2719 switch (temp)
2721 case Sstring_fence:
2722 if (!nofence) goto string_end;
2723 break;
2724 case Scharquote:
2725 case Sescape:
2726 INC_FROM;
2727 startquotedinstring:
2728 if (from >= end) goto endquoted;
2730 INC_FROM;
2733 string_end:
2734 state.instring = -1;
2735 curlevel->prev = curlevel->last;
2736 INC_FROM;
2737 if (boundary_stop) goto done;
2738 break;
2740 case Smath:
2741 break;
2744 goto done;
2746 stop: /* Here if stopping before start of sexp. */
2747 from = prev_from; /* We have just fetched the char that starts it; */
2748 goto done; /* but return the position before it. */
2750 endquoted:
2751 state.quoted = 1;
2752 done:
2753 state.depth = depth;
2754 state.mindepth = mindepth;
2755 state.thislevelstart = curlevel->prev;
2756 state.prevlevelstart
2757 = (curlevel == levelstart) ? -1 : (curlevel - 1)->last;
2758 state.location = from;
2759 state.levelstarts = Qnil;
2760 while (--curlevel >= levelstart)
2761 state.levelstarts = Fcons (make_number (curlevel->last),
2762 state.levelstarts);
2763 immediate_quit = 0;
2765 *stateptr = state;
2768 /* This comment supplies the doc string for parse-partial-sexp,
2769 for make-docfile to see. We cannot put this in the real DEFUN
2770 due to limits in the Unix cpp.
2772 DEFUN ("parse-partial-sexp", Ffoo, Sfoo, 2, 6, 0,
2773 "Parse Lisp syntax starting at FROM until TO; return status of parse at TO.\n\
2774 Parsing stops at TO or when certain criteria are met;\n\
2775 point is set to where parsing stops.\n\
2776 If fifth arg STATE is omitted or nil,\n\
2777 parsing assumes that FROM is the beginning of a function.\n\
2778 Value is a list of ten elements describing final state of parsing:\n\
2779 0. depth in parens.\n\
2780 1. character address of start of innermost containing list; nil if none.\n\
2781 2. character address of start of last complete sexp terminated.\n\
2782 3. non-nil if inside a string.\n\
2783 (it is the character that will terminate the string,\n\
2784 or t if the string should be terminated by a generic string delimiter.)\n\
2785 4. nil if outside a comment, t if inside a non-nestable comment, \n\
2786 else an integer (the current comment nesting).\n\
2787 5. t if following a quote character.\n\
2788 6. the minimum paren-depth encountered during this scan.\n\
2789 7. t if in a comment of style b; symbol `syntax-table' if the comment\n\
2790 should be terminated by a generic comment delimiter.\n\
2791 8. character address of start of comment or string; nil if not in one.\n\
2792 9. Intermediate data for continuation of parsing (subject to change).\n\
2793 If third arg TARGETDEPTH is non-nil, parsing stops if the depth\n\
2794 in parentheses becomes equal to TARGETDEPTH.\n\
2795 Fourth arg STOPBEFORE non-nil means stop when come to\n\
2796 any character that starts a sexp.\n\
2797 Fifth arg STATE is a nine-element list like what this function returns.\n\
2798 It is used to initialize the state of the parse. Elements number 1, 2, 6\n\
2799 and 8 are ignored; you can leave off element 8 (the last) entirely.\n\
2800 Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.\n\
2801 If it is symbol `syntax-table', stop after the start of a comment or a\n\
2802 string, or after end of a comment or a string.")
2803 (from, to, targetdepth, stopbefore, state, commentstop)
2806 DEFUN ("parse-partial-sexp", Fparse_partial_sexp, Sparse_partial_sexp, 2, 6, 0,
2807 0 /* See immediately above */)
2808 (from, to, targetdepth, stopbefore, oldstate, commentstop)
2809 Lisp_Object from, to, targetdepth, stopbefore, oldstate, commentstop;
2811 struct lisp_parse_state state;
2812 int target;
2814 if (!NILP (targetdepth))
2816 CHECK_NUMBER (targetdepth, 3);
2817 target = XINT (targetdepth);
2819 else
2820 target = -100000; /* We won't reach this depth */
2822 validate_region (&from, &to);
2823 scan_sexps_forward (&state, XINT (from), CHAR_TO_BYTE (XINT (from)),
2824 XINT (to),
2825 target, !NILP (stopbefore), oldstate,
2826 (NILP (commentstop)
2827 ? 0 : (EQ (commentstop, Qsyntax_table) ? -1 : 1)));
2829 SET_PT (state.location);
2831 return Fcons (make_number (state.depth),
2832 Fcons (state.prevlevelstart < 0 ? Qnil : make_number (state.prevlevelstart),
2833 Fcons (state.thislevelstart < 0 ? Qnil : make_number (state.thislevelstart),
2834 Fcons (state.instring >= 0
2835 ? (state.instring == ST_STRING_STYLE
2836 ? Qt : make_number (state.instring)) : Qnil,
2837 Fcons (state.incomment < 0 ? Qt :
2838 (state.incomment == 0 ? Qnil :
2839 make_number (state.incomment)),
2840 Fcons (state.quoted ? Qt : Qnil,
2841 Fcons (make_number (state.mindepth),
2842 Fcons ((state.comstyle
2843 ? (state.comstyle == ST_COMMENT_STYLE
2844 ? Qsyntax_table : Qt) :
2845 Qnil),
2846 Fcons (((state.incomment
2847 || (state.instring >= 0))
2848 ? make_number (state.comstr_start)
2849 : Qnil),
2850 Fcons (state.levelstarts, Qnil))))))))));
2853 void
2854 init_syntax_once ()
2856 register int i, c;
2857 Lisp_Object temp;
2859 /* This has to be done here, before we call Fmake_char_table. */
2860 Qsyntax_table = intern ("syntax-table");
2861 staticpro (&Qsyntax_table);
2863 /* Intern this now in case it isn't already done.
2864 Setting this variable twice is harmless.
2865 But don't staticpro it here--that is done in alloc.c. */
2866 Qchar_table_extra_slots = intern ("char-table-extra-slots");
2868 /* Create objects which can be shared among syntax tables. */
2869 Vsyntax_code_object = Fmake_vector (make_number (13), Qnil);
2870 for (i = 0; i < XVECTOR (Vsyntax_code_object)->size; i++)
2871 XVECTOR (Vsyntax_code_object)->contents[i]
2872 = Fcons (make_number (i), Qnil);
2874 /* Now we are ready to set up this property, so we can
2875 create syntax tables. */
2876 Fput (Qsyntax_table, Qchar_table_extra_slots, make_number (0));
2878 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Swhitespace];
2880 Vstandard_syntax_table = Fmake_char_table (Qsyntax_table, temp);
2882 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Sword];
2883 for (i = 'a'; i <= 'z'; i++)
2884 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
2885 for (i = 'A'; i <= 'Z'; i++)
2886 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
2887 for (i = '0'; i <= '9'; i++)
2888 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
2890 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '$', temp);
2891 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '%', temp);
2893 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '(',
2894 Fcons (make_number (Sopen), make_number (')')));
2895 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ')',
2896 Fcons (make_number (Sclose), make_number ('(')));
2897 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '[',
2898 Fcons (make_number (Sopen), make_number (']')));
2899 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ']',
2900 Fcons (make_number (Sclose), make_number ('[')));
2901 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '{',
2902 Fcons (make_number (Sopen), make_number ('}')));
2903 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '}',
2904 Fcons (make_number (Sclose), make_number ('{')));
2905 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '"',
2906 Fcons (make_number ((int) Sstring), Qnil));
2907 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\\',
2908 Fcons (make_number ((int) Sescape), Qnil));
2910 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Ssymbol];
2911 for (i = 0; i < 10; i++)
2913 c = "_-+*/&|<>="[i];
2914 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
2917 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Spunct];
2918 for (i = 0; i < 12; i++)
2920 c = ".,;:?!#@~^'`"[i];
2921 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
2924 /* All multibyte characters have syntax `word' by default. */
2925 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Sword];
2926 for (i = CHAR_TABLE_SINGLE_BYTE_SLOTS; i < CHAR_TABLE_ORDINARY_SLOTS; i++)
2927 XCHAR_TABLE (Vstandard_syntax_table)->contents[i] = temp;
2930 void
2931 syms_of_syntax ()
2933 Qsyntax_table_p = intern ("syntax-table-p");
2934 staticpro (&Qsyntax_table_p);
2936 staticpro (&Vsyntax_code_object);
2938 Qscan_error = intern ("scan-error");
2939 staticpro (&Qscan_error);
2940 Fput (Qscan_error, Qerror_conditions,
2941 Fcons (Qscan_error, Fcons (Qerror, Qnil)));
2942 Fput (Qscan_error, Qerror_message,
2943 build_string ("Scan error"));
2945 DEFVAR_BOOL ("parse-sexp-ignore-comments", &parse_sexp_ignore_comments,
2946 "Non-nil means `forward-sexp', etc., should treat comments as whitespace.");
2948 DEFVAR_BOOL ("parse-sexp-lookup-properties", &parse_sexp_lookup_properties,
2949 "Non-nil means `forward-sexp', etc., grant `syntax-table' property.\n\
2950 The value of this property should be either a syntax table, or a cons\n\
2951 of the form (SYNTAXCODE . MATCHCHAR), SYNTAXCODE being the numeric\n\
2952 syntax code, MATCHCHAR being nil or the character to match (which is\n\
2953 relevant only for open/close type.");
2955 words_include_escapes = 0;
2956 DEFVAR_BOOL ("words-include-escapes", &words_include_escapes,
2957 "Non-nil means `forward-word', etc., should treat escape chars part of words.");
2959 DEFVAR_BOOL ("multibyte-syntax-as-symbol", &multibyte_syntax_as_symbol,
2960 "Non-nil means `scan-sexps' treats all multibyte characters as symbol.");
2961 multibyte_syntax_as_symbol = 0;
2963 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
2964 &open_paren_in_column_0_is_defun_start,
2965 "Non-nil means an open paren in column 0 denotes the start of a defun.");
2966 open_paren_in_column_0_is_defun_start = 1;
2968 defsubr (&Ssyntax_table_p);
2969 defsubr (&Ssyntax_table);
2970 defsubr (&Sstandard_syntax_table);
2971 defsubr (&Scopy_syntax_table);
2972 defsubr (&Sset_syntax_table);
2973 defsubr (&Schar_syntax);
2974 defsubr (&Smatching_paren);
2975 defsubr (&Smodify_syntax_entry);
2976 defsubr (&Sdescribe_syntax);
2978 defsubr (&Sforward_word);
2980 defsubr (&Sskip_chars_forward);
2981 defsubr (&Sskip_chars_backward);
2982 defsubr (&Sskip_syntax_forward);
2983 defsubr (&Sskip_syntax_backward);
2985 defsubr (&Sforward_comment);
2986 defsubr (&Sscan_lists);
2987 defsubr (&Sscan_sexps);
2988 defsubr (&Sbackward_prefix_chars);
2989 defsubr (&Sparse_partial_sexp);