*** empty log message ***
[emacs.git] / src / syntax.c
blobda28d6eed6d7b550a7f8747e3b6d3248cdedfba8
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"
28 #include <assert.h>
30 /* Make syntax table lookup grant data in gl_state. */
31 #define SYNTAX_ENTRY_VIA_PROPERTY
33 #include "syntax.h"
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)
42 #include "category.h"
44 Lisp_Object Qsyntax_table_p, Qsyntax_table, Qscan_error;
46 int words_include_escapes;
47 int parse_sexp_lookup_properties;
49 /* Used as a temporary in SYNTAX_ENTRY and other macros in syntax.h,
50 if not compiled with GCC. No need to mark it, since it is used
51 only very temporarily. */
52 Lisp_Object syntax_temp;
54 /* This is the internal form of the parse state used in parse-partial-sexp. */
56 struct lisp_parse_state
58 int depth; /* Depth at end of parsing. */
59 int instring; /* -1 if not within string, else desired terminator. */
60 int incomment; /* -1 if in unnestable comment else comment nesting */
61 int comstyle; /* comment style a=0, or b=1, or ST_COMMENT_STYLE. */
62 int quoted; /* Nonzero if just after an escape char at end of parsing */
63 int thislevelstart; /* Char number of most recent start-of-expression at current level */
64 int prevlevelstart; /* Char number of start of containing expression */
65 int location; /* Char number at which parsing stopped. */
66 int mindepth; /* Minimum depth seen while scanning. */
67 int comstr_start; /* Position just after last comment/string starter. */
68 Lisp_Object levelstarts; /* Char numbers of starts-of-expression
69 of levels (starting from outermost). */
72 /* These variables are a cache for finding the start of a defun.
73 find_start_pos is the place for which the defun start was found.
74 find_start_value is the defun start position found for it.
75 find_start_value_byte is the corresponding byte position.
76 find_start_buffer is the buffer it was found in.
77 find_start_begv is the BEGV value when it was found.
78 find_start_modiff is the value of MODIFF when it was found. */
80 static int find_start_pos;
81 static int find_start_value;
82 static int find_start_value_byte;
83 static struct buffer *find_start_buffer;
84 static int find_start_begv;
85 static int find_start_modiff;
88 static int find_defun_start P_ ((int, int));
89 static int back_comment P_ ((int, int, int, int, int, int *, int *));
90 static int char_quoted P_ ((int, int));
91 static Lisp_Object skip_chars P_ ((int, int, Lisp_Object, Lisp_Object));
92 static Lisp_Object scan_lists P_ ((int, int, int, int));
93 static void scan_sexps_forward P_ ((struct lisp_parse_state *,
94 int, int, int, int,
95 int, Lisp_Object, int));
98 struct gl_state_s gl_state; /* Global state of syntax parser. */
100 INTERVAL interval_of ();
101 #define INTERVALS_AT_ONCE 10 /* 1 + max-number of intervals
102 to scan to property-change. */
104 /* Update gl_state to an appropriate interval which contains CHARPOS. The
105 sign of COUNT give the relative position of CHARPOS wrt the previously
106 valid interval. If INIT, only [be]_property fields of gl_state are
107 valid at start, the rest is filled basing on OBJECT.
109 `gl_state.*_i' are the intervals, and CHARPOS is further in the search
110 direction than the intervals - or in an interval. We update the
111 current syntax-table basing on the property of this interval, and
112 update the interval to start further than CHARPOS - or be
113 NULL_INTERVAL. We also update lim_property to be the next value of
114 charpos to call this subroutine again - or be before/after the
115 start/end of OBJECT. */
117 void
118 update_syntax_table (charpos, count, init, object)
119 int charpos, count, init;
120 Lisp_Object object;
122 Lisp_Object tmp_table;
123 int cnt = 0, invalidate = 1;
124 INTERVAL i, oldi;
126 if (init)
128 gl_state.start = gl_state.b_property;
129 gl_state.stop = gl_state.e_property;
130 gl_state.forward_i = interval_of (charpos, object);
131 i = gl_state.backward_i = gl_state.forward_i;
132 gl_state.left_ok = gl_state.right_ok = 1;
133 invalidate = 0;
134 if (NULL_INTERVAL_P (i))
135 return;
136 /* interval_of updates only ->position of the return value, so
137 update the parents manually to speed up update_interval. */
138 while (!NULL_PARENT (i))
140 if (AM_RIGHT_CHILD (i))
141 i->parent->position = i->position
142 - LEFT_TOTAL_LENGTH (i) + TOTAL_LENGTH (i) /* right end */
143 - TOTAL_LENGTH (i->parent)
144 + LEFT_TOTAL_LENGTH (i->parent);
145 else
146 i->parent->position = i->position - LEFT_TOTAL_LENGTH (i)
147 + TOTAL_LENGTH (i);
148 i = i->parent;
150 i = gl_state.forward_i;
151 gl_state.b_property = i->position - 1 - gl_state.offset;
152 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
153 goto update;
155 oldi = i = count > 0 ? gl_state.forward_i : gl_state.backward_i;
157 /* We are guarantied to be called with CHARPOS either in i,
158 or further off. */
159 if (NULL_INTERVAL_P (i))
160 error ("Error in syntax_table logic for to-the-end intervals");
161 else if (charpos < i->position) /* Move left. */
163 if (count > 0)
164 error ("Error in syntax_table logic for intervals <-");
165 /* Update the interval. */
166 i = update_interval (i, charpos);
167 if (oldi->position != INTERVAL_LAST_POS (i))
169 invalidate = 0;
170 gl_state.right_ok = 1; /* Invalidate the other end. */
171 gl_state.forward_i = i;
172 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
175 else if (charpos >= INTERVAL_LAST_POS (i)) /* Move right. */
177 if (count < 0)
178 error ("Error in syntax_table logic for intervals ->");
179 /* Update the interval. */
180 i = update_interval (i, charpos);
181 if (i->position != INTERVAL_LAST_POS (oldi))
183 invalidate = 0;
184 gl_state.left_ok = 1; /* Invalidate the other end. */
185 gl_state.backward_i = i;
186 gl_state.b_property = i->position - 1 - gl_state.offset;
189 else if (count > 0 ? gl_state.right_ok : gl_state.left_ok)
191 /* We do not need to recalculate tmp_table. */
192 tmp_table = gl_state.old_prop;
195 update:
196 tmp_table = textget (i->plist, Qsyntax_table);
198 if (invalidate)
199 invalidate = !EQ (tmp_table, gl_state.old_prop); /* Need to invalidate? */
201 if (invalidate) /* Did not get to adjacent interval. */
202 { /* with the same table => */
203 /* invalidate the old range. */
204 if (count > 0)
206 gl_state.backward_i = i;
207 gl_state.left_ok = 1; /* Invalidate the other end. */
208 gl_state.b_property = i->position - 1 - gl_state.offset;
210 else
212 gl_state.forward_i = i;
213 gl_state.right_ok = 1; /* Invalidate the other end. */
214 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
218 gl_state.current_syntax_table = tmp_table;
219 gl_state.old_prop = tmp_table;
220 if (EQ (Fsyntax_table_p (tmp_table), Qt))
222 gl_state.use_global = 0;
224 else if (CONSP (tmp_table))
226 gl_state.use_global = 1;
227 gl_state.global_code = tmp_table;
229 else
231 gl_state.use_global = 0;
232 gl_state.current_syntax_table = current_buffer->syntax_table;
235 while (!NULL_INTERVAL_P (i))
237 if (cnt && !EQ (tmp_table, textget (i->plist, Qsyntax_table)))
239 if (count > 0)
240 gl_state.right_ok = 0;
241 else
242 gl_state.left_ok = 0;
243 break;
245 else if (cnt == INTERVALS_AT_ONCE)
247 if (count > 0)
248 gl_state.right_ok = 1;
249 else
250 gl_state.left_ok = 1;
251 break;
253 cnt++;
254 i = count > 0 ? next_interval (i) : previous_interval (i);
256 if (NULL_INTERVAL_P (i))
257 { /* This property goes to the end. */
258 if (count > 0)
259 gl_state.e_property = gl_state.stop;
260 else
261 gl_state.b_property = gl_state.start;
263 else
265 if (count > 0)
267 gl_state.e_property = i->position - gl_state.offset;
268 gl_state.forward_i = i;
270 else
272 gl_state.b_property = i->position + LENGTH (i) - 1 - gl_state.offset;
273 gl_state.backward_i = i;
278 /* Returns TRUE if char at CHARPOS is quoted.
279 Global syntax-table data should be set up already to be good at CHARPOS
280 or after. On return global syntax data is good for lookup at CHARPOS. */
282 static int
283 char_quoted (charpos, bytepos)
284 register int charpos, bytepos;
286 register enum syntaxcode code;
287 register int beg = BEGV;
288 register int quoted = 0;
289 int orig = charpos;
291 DEC_BOTH (charpos, bytepos);
293 while (bytepos >= beg)
295 UPDATE_SYNTAX_TABLE_BACKWARD (charpos);
296 code = SYNTAX (FETCH_CHAR (bytepos));
297 if (! (code == Scharquote || code == Sescape))
298 break;
300 DEC_BOTH (charpos, bytepos);
301 quoted = !quoted;
304 UPDATE_SYNTAX_TABLE (orig);
305 return quoted;
308 /* Return the bytepos one character after BYTEPOS.
309 We assume that BYTEPOS is not at the end of the buffer. */
311 INLINE int
312 inc_bytepos (bytepos)
313 int bytepos;
315 if (NILP (current_buffer->enable_multibyte_characters))
316 return bytepos + 1;
318 INC_POS (bytepos);
319 return bytepos;
322 /* Return the bytepos one character before BYTEPOS.
323 We assume that BYTEPOS is not at the start of the buffer. */
325 INLINE int
326 dec_bytepos (bytepos)
327 int bytepos;
329 if (NILP (current_buffer->enable_multibyte_characters))
330 return bytepos - 1;
332 DEC_POS (bytepos);
333 return bytepos;
336 /* Find a defun-start that is the last one before POS (or nearly the last).
337 We record what we find, so that another call in the same area
338 can return the same value right away.
340 There is no promise at which position the global syntax data is
341 valid on return from the subroutine, so the caller should explicitly
342 update the global data. */
344 static int
345 find_defun_start (pos, pos_byte)
346 int pos, pos_byte;
348 int opoint = PT, opoint_byte = PT_BYTE;
350 /* Use previous finding, if it's valid and applies to this inquiry. */
351 if (current_buffer == find_start_buffer
352 /* Reuse the defun-start even if POS is a little farther on.
353 POS might be in the next defun, but that's ok.
354 Our value may not be the best possible, but will still be usable. */
355 && pos <= find_start_pos + 1000
356 && pos >= find_start_value
357 && BEGV == find_start_begv
358 && MODIFF == find_start_modiff)
359 return find_start_value;
361 /* Back up to start of line. */
362 scan_newline (pos, pos_byte, BEGV, BEGV_BYTE, -1, 1);
364 /* We optimize syntax-table lookup for rare updates. Thus we accept
365 only those `^\s(' which are good in global _and_ text-property
366 syntax-tables. */
367 gl_state.current_syntax_table = current_buffer->syntax_table;
368 gl_state.use_global = 0;
369 while (PT > BEGV)
371 /* Open-paren at start of line means we found our defun-start. */
372 if (SYNTAX (FETCH_CHAR (PT_BYTE)) == Sopen)
374 SETUP_SYNTAX_TABLE (PT + 1, -1); /* Try again... */
375 if (SYNTAX (FETCH_CHAR (PT_BYTE)) == Sopen)
376 break;
377 /* Now fallback to the default value. */
378 gl_state.current_syntax_table = current_buffer->syntax_table;
379 gl_state.use_global = 0;
381 /* Move to beg of previous line. */
382 scan_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, -2, 1);
385 /* Record what we found, for the next try. */
386 find_start_value = PT;
387 find_start_value_byte = PT_BYTE;
388 find_start_buffer = current_buffer;
389 find_start_modiff = MODIFF;
390 find_start_begv = BEGV;
391 find_start_pos = pos;
393 TEMP_SET_PT_BOTH (opoint, opoint_byte);
395 return find_start_value;
398 /* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
400 static int
401 prev_char_comend_first (pos, pos_byte)
402 int pos, pos_byte;
404 int c, val;
406 DEC_BOTH (pos, pos_byte);
407 UPDATE_SYNTAX_TABLE_BACKWARD (pos);
408 c = FETCH_CHAR (pos_byte);
409 val = SYNTAX_COMEND_FIRST (c);
410 UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
411 return val;
414 /* Return the SYNTAX_COMSTART_FIRST of the character before POS, POS_BYTE. */
416 static int
417 prev_char_comstart_first (pos, pos_byte)
418 int pos, pos_byte;
420 int c, val;
422 DEC_BOTH (pos, pos_byte);
423 UPDATE_SYNTAX_TABLE_BACKWARD (pos);
424 c = FETCH_CHAR (pos_byte);
425 val = SYNTAX_COMSTART_FIRST (c);
426 UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
427 return val;
430 /* Checks whether charpos FROM is at the end of a comment.
431 FROM_BYTE is the bytepos corresponding to FROM.
432 Do not move back before STOP.
434 Return a positive value if we find a comment ending at FROM/FROM_BYTE;
435 return -1 otherwise.
437 If successful, store the charpos of the comment's beginning
438 into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR.
440 Global syntax data remains valid for backward search starting at
441 the returned value (or at FROM, if the search was not successful). */
443 static int
444 back_comment (from, from_byte, stop, comnested, comstyle, charpos_ptr, bytepos_ptr)
445 int from, from_byte, stop;
446 int comnested, comstyle;
447 int *charpos_ptr, *bytepos_ptr;
449 /* Look back, counting the parity of string-quotes,
450 and recording the comment-starters seen.
451 When we reach a safe place, assume that's not in a string;
452 then step the main scan to the earliest comment-starter seen
453 an even number of string quotes away from the safe place.
455 OFROM[I] is position of the earliest comment-starter seen
456 which is I+2X quotes from the comment-end.
457 PARITY is current parity of quotes from the comment end. */
458 int parity = 0;
459 int my_stringend = 0;
460 int string_lossage = 0;
461 int comment_end = from;
462 int comment_end_byte = from_byte;
463 int comstart_pos = 0;
464 int comstart_byte;
465 /* Value that PARITY had, when we reached the position
466 in COMSTART_POS. */
467 int comstart_parity = 0;
468 int scanstart = from - 1;
469 /* Place where the containing defun starts,
470 or 0 if we didn't come across it yet. */
471 int defun_start = 0;
472 int defun_start_byte = 0;
473 register enum syntaxcode code;
474 int nesting = 1; /* current comment nesting */
475 int c;
477 /* At beginning of range to scan, we're outside of strings;
478 that determines quote parity to the comment-end. */
479 while (from != stop)
481 int temp_byte;
483 /* Move back and examine a character. */
484 DEC_BOTH (from, from_byte);
485 UPDATE_SYNTAX_TABLE_BACKWARD (from);
487 c = FETCH_CHAR (from_byte);
488 code = SYNTAX (c);
490 /* If this char is the second of a 2-char comment end sequence,
491 back up and give the pair the appropriate syntax. */
492 if (from > stop && SYNTAX_COMEND_SECOND (c)
493 && prev_char_comend_first (from, from_byte))
495 code = Sendcomment;
496 DEC_BOTH (from, from_byte);
497 UPDATE_SYNTAX_TABLE_BACKWARD (from);
498 c = FETCH_CHAR (from_byte);
501 /* If this char starts a 2-char comment start sequence,
502 treat it like a 1-char comment starter. */
503 if (from < scanstart && SYNTAX_COMSTART_FIRST (c))
505 temp_byte = inc_bytepos (from_byte);
506 UPDATE_SYNTAX_TABLE_FORWARD (from + 1);
507 if (SYNTAX_COMSTART_SECOND (FETCH_CHAR (temp_byte))
508 && comstyle == SYNTAX_COMMENT_STYLE (FETCH_CHAR (temp_byte)))
509 code = Scomment;
510 UPDATE_SYNTAX_TABLE_BACKWARD (from);
513 /* Ignore escaped characters, except comment-enders. */
514 if (code != Sendcomment && char_quoted (from, from_byte))
515 continue;
517 /* Track parity of quotes. */
518 if (code == Sstring)
520 parity ^= 1;
521 if (my_stringend == 0)
522 my_stringend = c;
523 /* If we have two kinds of string delimiters.
524 There's no way to grok this scanning backwards. */
525 else if (my_stringend != c)
526 string_lossage = 1;
529 if (code == Sstring_fence || code == Scomment_fence)
531 parity ^= 1;
532 if (my_stringend == 0)
533 my_stringend
534 = code == Sstring_fence ? ST_STRING_STYLE : ST_COMMENT_STYLE;
535 /* If we have two kinds of string delimiters.
536 There's no way to grok this scanning backwards. */
537 else if (my_stringend != (code == Sstring_fence
538 ? ST_STRING_STYLE : ST_COMMENT_STYLE))
539 string_lossage = 1;
542 if (code == Scomment)
543 /* FIXME: we should also check that the comstyle is correct
544 if the Scomment is a single-char. */
546 if (comnested && --nesting <= 0 && parity == 0 && !string_lossage)
547 /* nested comments have to be balanced, so we don't need to
548 keep looking for earlier ones. We use here the same (slightly
549 incorrect) reasoning as below: since it is followed by uniform
550 paired string quotes, this comment-start has to be outside of
551 strings, else the comment-end itself would be inside a string. */
552 goto done;
554 /* Record comment-starters according to that
555 quote-parity to the comment-end. */
556 comstart_parity = parity;
557 comstart_pos = from;
558 comstart_byte = from_byte;
561 /* If we find another earlier comment-ender,
562 any comment-starts earlier than that don't count
563 (because they go with the earlier comment-ender). */
564 if (code == Sendcomment
565 && SYNTAX_COMMENT_STYLE (FETCH_CHAR (from_byte)) == comstyle)
567 if (comnested)
568 nesting++;
569 else
570 break;
573 /* Assume a defun-start point is outside of strings. */
574 if (code == Sopen
575 && (from == stop
576 || (temp_byte = dec_bytepos (from_byte),
577 FETCH_CHAR (temp_byte) == '\n')))
579 defun_start = from;
580 defun_start_byte = from_byte;
581 break;
585 if (comstart_pos == 0)
587 from = comment_end;
588 from_byte = comment_end_byte;
589 UPDATE_SYNTAX_TABLE_FORWARD (comment_end - 1);
591 /* If the earliest comment starter
592 is followed by uniform paired string quotes or none,
593 we know it can't be inside a string
594 since if it were then the comment ender would be inside one.
595 So it does start a comment. Skip back to it. */
596 else if (!comnested && comstart_parity == 0 && !string_lossage)
598 from = comstart_pos;
599 from_byte = comstart_byte;
600 /* Globals are correct now. */
602 else
604 /* We had two kinds of string delimiters mixed up
605 together. Decode this going forwards.
606 Scan fwd from the previous comment ender
607 to the one in question; this records where we
608 last passed a comment starter. */
609 struct lisp_parse_state state;
610 /* If we did not already find the defun start, find it now. */
611 if (defun_start == 0)
613 defun_start = find_defun_start (comment_end, comment_end_byte);
614 defun_start_byte = find_start_value_byte;
616 scan_sexps_forward (&state,
617 defun_start, defun_start_byte,
618 comment_end - 1, -10000, 0, Qnil, 0);
619 if (state.incomment)
621 /* scan_sexps_forward changed the direction of search in
622 global variables, so we need to update it completely. */
624 from = state.comstr_start;
626 else
628 from = comment_end;
630 from_byte = CHAR_TO_BYTE (from);
631 UPDATE_SYNTAX_TABLE_FORWARD (from - 1);
634 done:
635 *charpos_ptr = from;
636 *bytepos_ptr = from_byte;
638 return from;
641 DEFUN ("syntax-table-p", Fsyntax_table_p, Ssyntax_table_p, 1, 1, 0,
642 "Return t if OBJECT is a syntax table.\n\
643 Currently, any char-table counts as a syntax table.")
644 (object)
645 Lisp_Object object;
647 if (CHAR_TABLE_P (object)
648 && EQ (XCHAR_TABLE (object)->purpose, Qsyntax_table))
649 return Qt;
650 return Qnil;
653 static void
654 check_syntax_table (obj)
655 Lisp_Object obj;
657 if (!(CHAR_TABLE_P (obj)
658 && EQ (XCHAR_TABLE (obj)->purpose, Qsyntax_table)))
659 wrong_type_argument (Qsyntax_table_p, obj);
662 DEFUN ("syntax-table", Fsyntax_table, Ssyntax_table, 0, 0, 0,
663 "Return the current syntax table.\n\
664 This is the one specified by the current buffer.")
667 return current_buffer->syntax_table;
670 DEFUN ("standard-syntax-table", Fstandard_syntax_table,
671 Sstandard_syntax_table, 0, 0, 0,
672 "Return the standard syntax table.\n\
673 This is the one used for new buffers.")
676 return Vstandard_syntax_table;
679 DEFUN ("copy-syntax-table", Fcopy_syntax_table, Scopy_syntax_table, 0, 1, 0,
680 "Construct a new syntax table and return it.\n\
681 It is a copy of the TABLE, which defaults to the standard syntax table.")
682 (table)
683 Lisp_Object table;
685 Lisp_Object copy;
687 if (!NILP (table))
688 check_syntax_table (table);
689 else
690 table = Vstandard_syntax_table;
692 copy = Fcopy_sequence (table);
694 /* Only the standard syntax table should have a default element.
695 Other syntax tables should inherit from parents instead. */
696 XCHAR_TABLE (copy)->defalt = Qnil;
698 /* Copied syntax tables should all have parents.
699 If we copied one with no parent, such as the standard syntax table,
700 use the standard syntax table as the copy's parent. */
701 if (NILP (XCHAR_TABLE (copy)->parent))
702 Fset_char_table_parent (copy, Vstandard_syntax_table);
703 return copy;
706 DEFUN ("set-syntax-table", Fset_syntax_table, Sset_syntax_table, 1, 1, 0,
707 "Select a new syntax table for the current buffer.\n\
708 One argument, a syntax table.")
709 (table)
710 Lisp_Object table;
712 check_syntax_table (table);
713 current_buffer->syntax_table = table;
714 /* Indicate that this buffer now has a specified syntax table. */
715 current_buffer->local_var_flags
716 |= XFASTINT (buffer_local_flags.syntax_table);
717 return table;
720 /* Convert a letter which signifies a syntax code
721 into the code it signifies.
722 This is used by modify-syntax-entry, and other things. */
724 unsigned char syntax_spec_code[0400] =
725 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
726 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
727 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
728 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
729 (char) Swhitespace, (char) Scomment_fence, (char) Sstring, 0377,
730 (char) Smath, 0377, 0377, (char) Squote,
731 (char) Sopen, (char) Sclose, 0377, 0377,
732 0377, (char) Swhitespace, (char) Spunct, (char) Scharquote,
733 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
734 0377, 0377, 0377, 0377,
735 (char) Scomment, 0377, (char) Sendcomment, 0377,
736 (char) Sinherit, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
737 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
738 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword,
739 0377, 0377, 0377, 0377, (char) Sescape, 0377, 0377, (char) Ssymbol,
740 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
741 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
742 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword,
743 0377, 0377, 0377, 0377, (char) Sstring_fence, 0377, 0377, 0377
746 /* Indexed by syntax code, give the letter that describes it. */
748 char syntax_code_spec[16] =
750 ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@',
751 '!', '|'
754 /* Indexed by syntax code, give the object (cons of syntax code and
755 nil) to be stored in syntax table. Since these objects can be
756 shared among syntax tables, we generate them in advance. By
757 sharing objects, the function `describe-syntax' can give a more
758 compact listing. */
759 static Lisp_Object Vsyntax_code_object;
762 /* Look up the value for CHARACTER in syntax table TABLE's parent
763 and its parents. SYNTAX_ENTRY calls this, when TABLE itself has nil
764 for CHARACTER. It's actually used only when not compiled with GCC. */
766 Lisp_Object
767 syntax_parent_lookup (table, character)
768 Lisp_Object table;
769 int character;
771 Lisp_Object value;
773 while (1)
775 table = XCHAR_TABLE (table)->parent;
776 if (NILP (table))
777 return Qnil;
779 value = XCHAR_TABLE (table)->contents[character];
780 if (!NILP (value))
781 return value;
785 DEFUN ("char-syntax", Fchar_syntax, Schar_syntax, 1, 1, 0,
786 "Return the syntax code of CHARACTER, described by a character.\n\
787 For example, if CHARACTER is a word constituent,\n\
788 the character `w' is returned.\n\
789 The characters that correspond to various syntax codes\n\
790 are listed in the documentation of `modify-syntax-entry'.")
791 (character)
792 Lisp_Object character;
794 int char_int;
795 gl_state.current_syntax_table = current_buffer->syntax_table;
797 gl_state.use_global = 0;
798 CHECK_NUMBER (character, 0);
799 char_int = XINT (character);
800 return make_number (syntax_code_spec[(int) SYNTAX (char_int)]);
803 DEFUN ("matching-paren", Fmatching_paren, Smatching_paren, 1, 1, 0,
804 "Return the matching parenthesis of CHARACTER, or nil if none.")
805 (character)
806 Lisp_Object character;
808 int char_int, code;
809 gl_state.current_syntax_table = current_buffer->syntax_table;
810 gl_state.use_global = 0;
811 CHECK_NUMBER (character, 0);
812 char_int = XINT (character);
813 code = SYNTAX (char_int);
814 if (code == Sopen || code == Sclose)
815 return SYNTAX_MATCH (char_int);
816 return Qnil;
819 /* This comment supplies the doc string for modify-syntax-entry,
820 for make-docfile to see. We cannot put this in the real DEFUN
821 due to limits in the Unix cpp.
823 DEFUN ("modify-syntax-entry", foo, bar, 2, 3, 0,
824 "Set syntax for character CHAR according to string S.\n\
825 The syntax is changed only for table TABLE, which defaults to\n\
826 the current buffer's syntax table.\n\
827 The first character of S should be one of the following:\n\
828 Space or - whitespace syntax. w word constituent.\n\
829 _ symbol constituent. . punctuation.\n\
830 ( open-parenthesis. ) close-parenthesis.\n\
831 \" string quote. \\ escape.\n\
832 $ paired delimiter. ' expression quote or prefix operator.\n\
833 < comment starter. > comment ender.\n\
834 / character-quote. @ inherit from `standard-syntax-table'.\n\
836 Only single-character comment start and end sequences are represented thus.\n\
837 Two-character sequences are represented as described below.\n\
838 The second character of S is the matching parenthesis,\n\
839 used only if the first character is `(' or `)'.\n\
840 Any additional characters are flags.\n\
841 Defined flags are the characters 1, 2, 3, 4, b, p, and n.\n\
842 1 means CHAR is the start of a two-char comment start sequence.\n\
843 2 means CHAR is the second character of such a sequence.\n\
844 3 means CHAR is the start of a two-char comment end sequence.\n\
845 4 means CHAR is the second character of such a sequence.\n\
847 There can be up to two orthogonal comment sequences. This is to support\n\
848 language modes such as C++. By default, all comment sequences are of style\n\
849 a, but you can set the comment sequence style to b (on the second character\n\
850 of a comment-start, or the first character of a comment-end sequence) using\n\
851 this flag:\n\
852 b means CHAR is part of comment sequence b.\n\
853 n means CHAR is part of a nestable comment sequence.\n\
855 p means CHAR is a prefix character for `backward-prefix-chars';\n\
856 such characters are treated as whitespace when they occur\n\
857 between expressions.")
858 (char, s, table)
861 DEFUN ("modify-syntax-entry", Fmodify_syntax_entry, Smodify_syntax_entry, 2, 3,
862 /* I really don't know why this is interactive
863 help-form should at least be made useful whilst reading the second arg
865 "cSet syntax for character: \nsSet syntax for %s to: ",
866 0 /* See immediately above */)
867 (c, newentry, syntax_table)
868 Lisp_Object c, newentry, syntax_table;
870 register unsigned char *p;
871 register enum syntaxcode code;
872 int val;
873 Lisp_Object match;
875 CHECK_NUMBER (c, 0);
876 CHECK_STRING (newentry, 1);
878 if (NILP (syntax_table))
879 syntax_table = current_buffer->syntax_table;
880 else
881 check_syntax_table (syntax_table);
883 p = XSTRING (newentry)->data;
884 code = (enum syntaxcode) syntax_spec_code[*p++];
885 if (((int) code & 0377) == 0377)
886 error ("invalid syntax description letter: %c", p[-1]);
888 if (code == Sinherit)
890 SET_RAW_SYNTAX_ENTRY (syntax_table, XINT (c), Qnil);
891 return Qnil;
894 if (*p)
896 int len;
897 int character = (STRING_CHAR_AND_LENGTH
898 (p, STRING_BYTES (XSTRING (newentry)) - 1, len));
899 XSETINT (match, character);
900 if (XFASTINT (match) == ' ')
901 match = Qnil;
902 p += len;
904 else
905 match = Qnil;
907 val = (int) code;
908 while (*p)
909 switch (*p++)
911 case '1':
912 val |= 1 << 16;
913 break;
915 case '2':
916 val |= 1 << 17;
917 break;
919 case '3':
920 val |= 1 << 18;
921 break;
923 case '4':
924 val |= 1 << 19;
925 break;
927 case 'p':
928 val |= 1 << 20;
929 break;
931 case 'b':
932 val |= 1 << 21;
933 break;
935 case 'n':
936 val |= 1 << 22;
937 break;
940 if (val < XVECTOR (Vsyntax_code_object)->size && NILP (match))
941 newentry = XVECTOR (Vsyntax_code_object)->contents[val];
942 else
943 /* Since we can't use a shared object, let's make a new one. */
944 newentry = Fcons (make_number (val), match);
946 SET_RAW_SYNTAX_ENTRY (syntax_table, XINT (c), newentry);
948 return Qnil;
951 /* Dump syntax table to buffer in human-readable format */
953 static void
954 describe_syntax (value)
955 Lisp_Object value;
957 register enum syntaxcode code;
958 char desc, start1, start2, end1, end2, prefix, comstyle;
959 char str[2];
960 Lisp_Object first, match_lisp;
962 Findent_to (make_number (16), make_number (1));
964 if (NILP (value))
966 insert_string ("default\n");
967 return;
970 if (CHAR_TABLE_P (value))
972 insert_string ("deeper char-table ...\n");
973 return;
976 if (!CONSP (value))
978 insert_string ("invalid\n");
979 return;
982 first = XCAR (value);
983 match_lisp = XCDR (value);
985 if (!INTEGERP (first) || !(NILP (match_lisp) || INTEGERP (match_lisp)))
987 insert_string ("invalid\n");
988 return;
991 code = (enum syntaxcode) (XINT (first) & 0377);
992 start1 = (XINT (first) >> 16) & 1;
993 start2 = (XINT (first) >> 17) & 1;
994 end1 = (XINT (first) >> 18) & 1;
995 end2 = (XINT (first) >> 19) & 1;
996 prefix = (XINT (first) >> 20) & 1;
997 comstyle = (XINT (first) >> 21) & 1;
999 if ((int) code < 0 || (int) code >= (int) Smax)
1001 insert_string ("invalid");
1002 return;
1004 desc = syntax_code_spec[(int) code];
1006 str[0] = desc, str[1] = 0;
1007 insert (str, 1);
1009 if (NILP (match_lisp))
1010 insert (" ", 1);
1011 else
1012 insert_char (XINT (match_lisp));
1014 if (start1)
1015 insert ("1", 1);
1016 if (start2)
1017 insert ("2", 1);
1019 if (end1)
1020 insert ("3", 1);
1021 if (end2)
1022 insert ("4", 1);
1024 if (prefix)
1025 insert ("p", 1);
1026 if (comstyle)
1027 insert ("b", 1);
1029 insert_string ("\twhich means: ");
1031 switch (SWITCH_ENUM_CAST (code))
1033 case Swhitespace:
1034 insert_string ("whitespace"); break;
1035 case Spunct:
1036 insert_string ("punctuation"); break;
1037 case Sword:
1038 insert_string ("word"); break;
1039 case Ssymbol:
1040 insert_string ("symbol"); break;
1041 case Sopen:
1042 insert_string ("open"); break;
1043 case Sclose:
1044 insert_string ("close"); break;
1045 case Squote:
1046 insert_string ("quote"); break;
1047 case Sstring:
1048 insert_string ("string"); break;
1049 case Smath:
1050 insert_string ("math"); break;
1051 case Sescape:
1052 insert_string ("escape"); break;
1053 case Scharquote:
1054 insert_string ("charquote"); break;
1055 case Scomment:
1056 insert_string ("comment"); break;
1057 case Sendcomment:
1058 insert_string ("endcomment"); break;
1059 default:
1060 insert_string ("invalid");
1061 return;
1064 if (!NILP (match_lisp))
1066 insert_string (", matches ");
1067 insert_char (XINT (match_lisp));
1070 if (start1)
1071 insert_string (",\n\t is the first character of a comment-start sequence");
1072 if (start2)
1073 insert_string (",\n\t is the second character of a comment-start sequence");
1075 if (end1)
1076 insert_string (",\n\t is the first character of a comment-end sequence");
1077 if (end2)
1078 insert_string (",\n\t is the second character of a comment-end sequence");
1079 if (comstyle)
1080 insert_string (" (comment style b)");
1082 if (prefix)
1083 insert_string (",\n\t is a prefix character for `backward-prefix-chars'");
1085 insert_string ("\n");
1088 static Lisp_Object
1089 describe_syntax_1 (vector)
1090 Lisp_Object vector;
1092 struct buffer *old = current_buffer;
1093 set_buffer_internal (XBUFFER (Vstandard_output));
1094 describe_vector (vector, Qnil, describe_syntax, 0, Qnil, Qnil, (int *) 0, 0);
1095 while (! NILP (XCHAR_TABLE (vector)->parent))
1097 vector = XCHAR_TABLE (vector)->parent;
1098 insert_string ("\nThe parent syntax table is:");
1099 describe_vector (vector, Qnil, describe_syntax, 0, Qnil, Qnil,
1100 (int *) 0, 0);
1103 call0 (intern ("help-mode"));
1104 set_buffer_internal (old);
1105 return Qnil;
1108 DEFUN ("describe-syntax", Fdescribe_syntax, Sdescribe_syntax, 0, 0, "",
1109 "Describe the syntax specifications in the syntax table.\n\
1110 The descriptions are inserted in a buffer, which is then displayed.")
1113 internal_with_output_to_temp_buffer
1114 ("*Help*", describe_syntax_1, current_buffer->syntax_table);
1116 return Qnil;
1119 int parse_sexp_ignore_comments;
1121 /* Return the position across COUNT words from FROM.
1122 If that many words cannot be found before the end of the buffer, return 0.
1123 COUNT negative means scan backward and stop at word beginning. */
1126 scan_words (from, count)
1127 register int from, count;
1129 register int beg = BEGV;
1130 register int end = ZV;
1131 register int from_byte = CHAR_TO_BYTE (from);
1132 register enum syntaxcode code;
1133 int ch0, ch1;
1135 immediate_quit = 1;
1136 QUIT;
1138 SETUP_SYNTAX_TABLE (from, count);
1140 while (count > 0)
1142 while (1)
1144 if (from == end)
1146 immediate_quit = 0;
1147 return 0;
1149 UPDATE_SYNTAX_TABLE_FORWARD (from);
1150 ch0 = FETCH_CHAR (from_byte);
1151 code = SYNTAX (ch0);
1152 INC_BOTH (from, from_byte);
1153 if (words_include_escapes
1154 && (code == Sescape || code == Scharquote))
1155 break;
1156 if (code == Sword)
1157 break;
1159 /* Now CH0 is a character which begins a word and FROM is the
1160 position of the next character. */
1161 while (1)
1163 if (from == end) break;
1164 UPDATE_SYNTAX_TABLE_FORWARD (from);
1165 ch1 = FETCH_CHAR (from_byte);
1166 code = SYNTAX (ch1);
1167 if (!(words_include_escapes
1168 && (code == Sescape || code == Scharquote)))
1169 if (code != Sword || WORD_BOUNDARY_P (ch0, ch1))
1170 break;
1171 INC_BOTH (from, from_byte);
1172 ch0 = ch1;
1174 count--;
1176 while (count < 0)
1178 while (1)
1180 if (from == beg)
1182 immediate_quit = 0;
1183 return 0;
1185 DEC_BOTH (from, from_byte);
1186 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1187 ch1 = FETCH_CHAR (from_byte);
1188 code = SYNTAX (ch1);
1189 if (words_include_escapes
1190 && (code == Sescape || code == Scharquote))
1191 break;
1192 if (code == Sword)
1193 break;
1195 /* Now CH1 is a character which ends a word and FROM is the
1196 position of it. */
1197 while (1)
1199 int temp_byte;
1201 if (from == beg)
1202 break;
1203 temp_byte = dec_bytepos (from_byte);
1204 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1205 ch0 = FETCH_CHAR (temp_byte);
1206 code = SYNTAX (ch0);
1207 if (!(words_include_escapes
1208 && (code == Sescape || code == Scharquote)))
1209 if (code != Sword || WORD_BOUNDARY_P (ch0, ch1))
1210 break;
1211 DEC_BOTH (from, from_byte);
1212 ch1 = ch0;
1214 count++;
1217 immediate_quit = 0;
1219 return from;
1222 DEFUN ("forward-word", Fforward_word, Sforward_word, 1, 1, "p",
1223 "Move point forward ARG words (backward if ARG is negative).\n\
1224 Normally returns t.\n\
1225 If an edge of the buffer or a field boundary is reached, point is left there\n\
1226 and the function returns nil.")
1227 (count)
1228 Lisp_Object count;
1230 int orig_val, val;
1231 CHECK_NUMBER (count, 0);
1233 val = orig_val = scan_words (PT, XINT (count));
1234 if (! orig_val)
1235 val = XINT (count) > 0 ? ZV : BEGV;
1237 /* Avoid jumping out of an input field. */
1238 val = XFASTINT (Fconstrain_to_field (make_number (val), make_number (PT),
1239 Qt, Qnil));
1241 SET_PT (val);
1242 return (val == orig_val ? Qt : Qnil);
1245 Lisp_Object skip_chars ();
1247 DEFUN ("skip-chars-forward", Fskip_chars_forward, Sskip_chars_forward, 1, 2, 0,
1248 "Move point forward, stopping before a char not in STRING, or at pos LIM.\n\
1249 STRING is like the inside of a `[...]' in a regular expression\n\
1250 except that `]' is never special and `\\' quotes `^', `-' or `\\'\n\
1251 (but not as the end of a range; quoting is never needed there).\n\
1252 Thus, with arg \"a-zA-Z\", this skips letters stopping before first nonletter.\n\
1253 With arg \"^a-zA-Z\", skips nonletters stopping before first letter.\n\
1254 Returns the distance traveled, either zero or positive.")
1255 (string, lim)
1256 Lisp_Object string, lim;
1258 return skip_chars (1, 0, string, lim);
1261 DEFUN ("skip-chars-backward", Fskip_chars_backward, Sskip_chars_backward, 1, 2, 0,
1262 "Move point backward, stopping after a char not in STRING, or at pos LIM.\n\
1263 See `skip-chars-forward' for details.\n\
1264 Returns the distance traveled, either zero or negative.")
1265 (string, lim)
1266 Lisp_Object string, lim;
1268 return skip_chars (0, 0, string, lim);
1271 DEFUN ("skip-syntax-forward", Fskip_syntax_forward, Sskip_syntax_forward, 1, 2, 0,
1272 "Move point forward across chars in specified syntax classes.\n\
1273 SYNTAX is a string of syntax code characters.\n\
1274 Stop before a char whose syntax is not in SYNTAX, or at position LIM.\n\
1275 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.\n\
1276 This function returns the distance traveled, either zero or positive.")
1277 (syntax, lim)
1278 Lisp_Object syntax, lim;
1280 return skip_chars (1, 1, syntax, lim);
1283 DEFUN ("skip-syntax-backward", Fskip_syntax_backward, Sskip_syntax_backward, 1, 2, 0,
1284 "Move point backward across chars in specified syntax classes.\n\
1285 SYNTAX is a string of syntax code characters.\n\
1286 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.\n\
1287 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.\n\
1288 This function returns the distance traveled, either zero or negative.")
1289 (syntax, lim)
1290 Lisp_Object syntax, lim;
1292 return skip_chars (0, 1, syntax, lim);
1295 static Lisp_Object
1296 skip_chars (forwardp, syntaxp, string, lim)
1297 int forwardp, syntaxp;
1298 Lisp_Object string, lim;
1300 register unsigned int c;
1301 register int ch;
1302 unsigned char fastmap[0400];
1303 /* If SYNTAXP is 0, STRING may contain multi-byte form of characters
1304 of which codes don't fit in FASTMAP. In that case, we set the
1305 first byte of multibyte form (i.e. base leading-code) in FASTMAP
1306 and set the actual ranges of characters in CHAR_RANGES. In the
1307 form "X-Y" of STRING, both X and Y must belong to the same
1308 character set because a range striding across character sets is
1309 meaningless. */
1310 int *char_ranges;
1311 int n_char_ranges = 0;
1312 int negate = 0;
1313 register int i, i_byte;
1314 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
1315 int string_multibyte;
1316 int size_byte;
1318 CHECK_STRING (string, 0);
1319 char_ranges = (int *) alloca (XSTRING (string)->size * (sizeof (int)) * 2);
1320 string_multibyte = STRING_MULTIBYTE (string);
1321 size_byte = STRING_BYTES (XSTRING (string));
1323 if (NILP (lim))
1324 XSETINT (lim, forwardp ? ZV : BEGV);
1325 else
1326 CHECK_NUMBER_COERCE_MARKER (lim, 0);
1328 /* In any case, don't allow scan outside bounds of buffer. */
1329 if (XINT (lim) > ZV)
1330 XSETFASTINT (lim, ZV);
1331 if (XINT (lim) < BEGV)
1332 XSETFASTINT (lim, BEGV);
1334 bzero (fastmap, sizeof fastmap);
1336 i = 0, i_byte = 0;
1338 if (i_byte < size_byte
1339 && XSTRING (string)->data[0] == '^')
1341 negate = 1; i++, i_byte++;
1344 /* Find the characters specified and set their elements of fastmap.
1345 If syntaxp, each character counts as itself.
1346 Otherwise, handle backslashes and ranges specially. */
1348 while (i_byte < size_byte)
1350 int c_leading_code;
1352 if (string_multibyte)
1354 c_leading_code = XSTRING (string)->data[i_byte];
1355 FETCH_STRING_CHAR_ADVANCE (c, string, i, i_byte);
1357 else
1358 c = c_leading_code = XSTRING (string)->data[i_byte++];
1360 /* Convert multibyteness between what the string has
1361 and what the buffer has. */
1362 if (multibyte)
1363 c = unibyte_char_to_multibyte (c);
1364 else
1365 c &= 0377;
1367 if (syntaxp)
1368 fastmap[syntax_spec_code[c & 0377]] = 1;
1369 else
1371 if (c == '\\')
1373 if (i_byte == size_byte)
1374 break;
1376 if (string_multibyte)
1378 c_leading_code = XSTRING (string)->data[i_byte];
1379 FETCH_STRING_CHAR_ADVANCE (c, string, i, i_byte);
1381 else
1382 c = c_leading_code = XSTRING (string)->data[i_byte++];
1384 if (i_byte < size_byte
1385 && XSTRING (string)->data[i_byte] == '-')
1387 unsigned int c2, c2_leading_code;
1389 /* Skip over the dash. */
1390 i++, i_byte++;
1392 if (i_byte == size_byte)
1393 break;
1395 /* Get the end of the range. */
1396 if (string_multibyte)
1398 c2_leading_code = XSTRING (string)->data[i_byte];
1399 FETCH_STRING_CHAR_ADVANCE (c2, string, i, i_byte);
1401 else
1402 c2 = XSTRING (string)->data[i_byte++];
1404 if (SINGLE_BYTE_CHAR_P (c))
1406 if (! SINGLE_BYTE_CHAR_P (c2))
1407 error ("Invalid charcter range: %s",
1408 XSTRING (string)->data);
1409 while (c <= c2)
1411 fastmap[c] = 1;
1412 c++;
1415 else
1417 if (c_leading_code != c2_leading_code)
1418 error ("Invalid charcter range: %s",
1419 XSTRING (string)->data);
1420 fastmap[c_leading_code] = 1;
1421 if (c <= c2)
1423 char_ranges[n_char_ranges++] = c;
1424 char_ranges[n_char_ranges++] = c2;
1428 else
1430 fastmap[c_leading_code] = 1;
1431 if (!SINGLE_BYTE_CHAR_P (c))
1433 char_ranges[n_char_ranges++] = c;
1434 char_ranges[n_char_ranges++] = c;
1440 /* If ^ was the first character, complement the fastmap. In
1441 addition, as all multibyte characters have possibility of
1442 matching, set all entries for base leading codes, which is
1443 harmless even if SYNTAXP is 1. */
1445 if (negate)
1446 for (i = 0; i < sizeof fastmap; i++)
1448 if (!multibyte || !BASE_LEADING_CODE_P (i))
1449 fastmap[i] ^= 1;
1450 else
1451 fastmap[i] = 1;
1455 int start_point = PT;
1456 int pos = PT;
1457 int pos_byte = PT_BYTE;
1459 immediate_quit = 1;
1460 if (syntaxp)
1462 SETUP_SYNTAX_TABLE (pos, forwardp ? 1 : -1);
1463 if (forwardp)
1465 if (multibyte)
1467 if (pos < XINT (lim))
1468 while (fastmap[(int) SYNTAX (FETCH_CHAR (pos_byte))])
1470 /* Since we already checked for multibyteness,
1471 avoid using INC_BOTH which checks again. */
1472 INC_POS (pos_byte);
1473 pos++;
1474 if (pos >= XINT (lim))
1475 break;
1476 UPDATE_SYNTAX_TABLE_FORWARD (pos);
1479 else
1481 while (pos < XINT (lim)
1482 && fastmap[(int) SYNTAX (FETCH_BYTE (pos))])
1484 pos++;
1485 UPDATE_SYNTAX_TABLE_FORWARD (pos);
1489 else
1491 if (multibyte)
1493 while (pos > XINT (lim))
1495 int savepos = pos_byte;
1496 /* Since we already checked for multibyteness,
1497 avoid using DEC_BOTH which checks again. */
1498 pos--;
1499 DEC_POS (pos_byte);
1500 UPDATE_SYNTAX_TABLE_BACKWARD (pos);
1501 if (!fastmap[(int) SYNTAX (FETCH_CHAR (pos_byte))])
1503 pos++;
1504 pos_byte = savepos;
1505 break;
1509 else
1511 if (pos > XINT (lim))
1512 while (fastmap[(int) SYNTAX (FETCH_BYTE (pos - 1))])
1514 pos--;
1515 if (pos <= XINT (lim))
1516 break;
1517 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
1522 else
1524 if (forwardp)
1526 if (multibyte)
1527 while (pos < XINT (lim) && fastmap[(c = FETCH_BYTE (pos_byte))])
1529 if (!BASE_LEADING_CODE_P (c))
1530 INC_BOTH (pos, pos_byte);
1531 else if (n_char_ranges)
1533 /* We much check CHAR_RANGES for a multibyte
1534 character. */
1535 ch = FETCH_MULTIBYTE_CHAR (pos_byte);
1536 for (i = 0; i < n_char_ranges; i += 2)
1537 if ((ch >= char_ranges[i] && ch <= char_ranges[i + 1]))
1538 break;
1539 if (!(negate ^ (i < n_char_ranges)))
1540 break;
1542 INC_BOTH (pos, pos_byte);
1544 else
1546 if (!negate) break;
1547 INC_BOTH (pos, pos_byte);
1550 else
1551 while (pos < XINT (lim) && fastmap[FETCH_BYTE (pos)])
1552 pos++;
1554 else
1556 if (multibyte)
1557 while (pos > XINT (lim))
1559 int savepos = pos_byte;
1560 DEC_BOTH (pos, pos_byte);
1561 if (fastmap[(c = FETCH_BYTE (pos_byte))])
1563 if (!BASE_LEADING_CODE_P (c))
1565 else if (n_char_ranges)
1567 /* We much check CHAR_RANGES for a multibyte
1568 character. */
1569 ch = FETCH_MULTIBYTE_CHAR (pos_byte);
1570 for (i = 0; i < n_char_ranges; i += 2)
1571 if (ch >= char_ranges[i] && ch <= char_ranges[i + 1])
1572 break;
1573 if (!(negate ^ (i < n_char_ranges)))
1575 pos++;
1576 pos_byte = savepos;
1577 break;
1580 else
1581 if (!negate)
1583 pos++;
1584 pos_byte = savepos;
1585 break;
1588 else
1590 pos++;
1591 pos_byte = savepos;
1592 break;
1595 else
1596 while (pos > XINT (lim) && fastmap[FETCH_BYTE (pos - 1)])
1597 pos--;
1601 #if 0 /* Not needed now that a position in mid-character
1602 cannot be specified in Lisp. */
1603 if (multibyte
1604 /* INC_POS or DEC_POS might have moved POS over LIM. */
1605 && (forwardp ? (pos > XINT (lim)) : (pos < XINT (lim))))
1606 pos = XINT (lim);
1607 #endif
1609 if (! multibyte)
1610 pos_byte = pos;
1612 SET_PT_BOTH (pos, pos_byte);
1613 immediate_quit = 0;
1615 return make_number (PT - start_point);
1619 /* Jump over a comment, assuming we are at the beginning of one.
1620 FROM is the current position.
1621 FROM_BYTE is the bytepos corresponding to FROM.
1622 Do not move past STOP (a charpos).
1623 The comment over which we have to jump is of style STYLE
1624 (either SYNTAX_COMMENT_STYLE(foo) or ST_COMMENT_STYLE).
1625 NESTING should be positive to indicate the nesting at the beginning
1626 for nested comments and should be zero or negative else.
1627 ST_COMMENT_STYLE cannot be nested.
1628 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
1629 (or 0 If the search cannot start in the middle of a two-character).
1631 If successful, return 1 and store the charpos of the comment's end
1632 into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR.
1633 Else, return 0 and store the charpos STOP into *CHARPOS_PTR, the
1634 corresponding bytepos into *BYTEPOS_PTR and the current nesting
1635 (as defined for state.incomment) in *INCOMMENT_PTR.
1637 The comment end is the last character of the comment rather than the
1638 character just after the comment.
1640 Global syntax data is assumed to initially be valid for FROM and
1641 remains valid for forward search starting at the returned position. */
1643 static int
1644 forw_comment (from, from_byte, stop, nesting, style, prev_syntax,
1645 charpos_ptr, bytepos_ptr, incomment_ptr)
1646 int from, from_byte, stop;
1647 int nesting, style, prev_syntax;
1648 int *charpos_ptr, *bytepos_ptr, *incomment_ptr;
1650 register int c, c1;
1651 register enum syntaxcode code;
1652 register int syntax;
1654 if (nesting <= 0) nesting = -1;
1656 /* Enter the loop in the middle so that we find
1657 a 2-char comment ender if we start in the middle of it. */
1658 syntax = prev_syntax;
1659 if (syntax != 0) goto forw_incomment;
1661 while (1)
1663 if (from == stop)
1665 *incomment_ptr = nesting;
1666 *charpos_ptr = from;
1667 *bytepos_ptr = from_byte;
1668 return 0;
1670 c = FETCH_CHAR (from_byte);
1671 syntax = SYNTAX_WITH_FLAGS (c);
1672 code = syntax & 0xff;
1673 if (code == Sendcomment
1674 && SYNTAX_FLAGS_COMMENT_STYLE (syntax) == style
1675 && --nesting <= 0)
1676 /* we have encountered a comment end of the same style
1677 as the comment sequence which began this comment
1678 section */
1679 break;
1680 if (code == Scomment_fence
1681 && style == ST_COMMENT_STYLE)
1682 /* we have encountered a comment end of the same style
1683 as the comment sequence which began this comment
1684 section. */
1685 break;
1686 if (nesting > 0
1687 && code == Scomment
1688 && SYNTAX_FLAGS_COMMENT_STYLE (syntax) == style)
1689 /* we have encountered a nested comment of the same style
1690 as the comment sequence which began this comment section */
1691 nesting++;
1692 INC_BOTH (from, from_byte);
1693 UPDATE_SYNTAX_TABLE_FORWARD (from);
1695 forw_incomment:
1696 if (from < stop && SYNTAX_FLAGS_COMEND_FIRST (syntax)
1697 && SYNTAX_FLAGS_COMMENT_STYLE (syntax) == style
1698 && (c1 = FETCH_CHAR (from_byte),
1699 SYNTAX_COMEND_SECOND (c1)))
1701 if (--nesting <= 0)
1702 /* we have encountered a comment end of the same style
1703 as the comment sequence which began this comment
1704 section */
1705 break;
1706 else
1708 INC_BOTH (from, from_byte);
1709 UPDATE_SYNTAX_TABLE_FORWARD (from);
1712 if (nesting > 0
1713 && from < stop
1714 && SYNTAX_FLAGS_COMSTART_FIRST (syntax)
1715 && (c1 = FETCH_CHAR (from_byte),
1716 SYNTAX_COMMENT_STYLE (c1) == style
1717 && SYNTAX_COMSTART_SECOND (c1)))
1718 /* we have encountered a nested comment of the same style
1719 as the comment sequence which began this comment
1720 section */
1722 INC_BOTH (from, from_byte);
1723 UPDATE_SYNTAX_TABLE_FORWARD (from);
1724 nesting++;
1727 *charpos_ptr = from;
1728 *bytepos_ptr = from_byte;
1729 return 1;
1732 DEFUN ("forward-comment", Fforward_comment, Sforward_comment, 1, 1, 0,
1733 "Move forward across up to N comments. If N is negative, move backward.\n\
1734 Stop scanning if we find something other than a comment or whitespace.\n\
1735 Set point to where scanning stops.\n\
1736 If N comments are found as expected, with nothing except whitespace\n\
1737 between them, return t; otherwise return nil.")
1738 (count)
1739 Lisp_Object count;
1741 register int from;
1742 int from_byte;
1743 register int stop;
1744 register int c, c1;
1745 register enum syntaxcode code;
1746 int comstyle = 0; /* style of comment encountered */
1747 int comnested = 0; /* whether the comment is nestable or not */
1748 int found;
1749 int count1;
1750 int out_charpos, out_bytepos;
1751 int dummy;
1753 CHECK_NUMBER (count, 0);
1754 count1 = XINT (count);
1755 stop = count1 > 0 ? ZV : BEGV;
1757 immediate_quit = 1;
1758 QUIT;
1760 from = PT;
1761 from_byte = PT_BYTE;
1763 SETUP_SYNTAX_TABLE (from, count1);
1764 while (count1 > 0)
1768 int comstart_first;
1770 if (from == stop)
1772 SET_PT_BOTH (from, from_byte);
1773 immediate_quit = 0;
1774 return Qnil;
1776 c = FETCH_CHAR (from_byte);
1777 code = SYNTAX (c);
1778 comstart_first = SYNTAX_COMSTART_FIRST (c);
1779 comnested = SYNTAX_COMMENT_NESTED (c);
1780 INC_BOTH (from, from_byte);
1781 UPDATE_SYNTAX_TABLE_FORWARD (from);
1782 comstyle = 0;
1783 if (from < stop && comstart_first
1784 && (c1 = FETCH_CHAR (from_byte),
1785 SYNTAX_COMSTART_SECOND (c1)))
1787 /* We have encountered a comment start sequence and we
1788 are ignoring all text inside comments. We must record
1789 the comment style this sequence begins so that later,
1790 only a comment end of the same style actually ends
1791 the comment section. */
1792 code = Scomment;
1793 comstyle = SYNTAX_COMMENT_STYLE (c1);
1794 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
1795 INC_BOTH (from, from_byte);
1796 UPDATE_SYNTAX_TABLE_FORWARD (from);
1799 while (code == Swhitespace || code == Sendcomment);
1801 if (code == Scomment_fence)
1802 comstyle = ST_COMMENT_STYLE;
1803 else if (code != Scomment)
1805 immediate_quit = 0;
1806 DEC_BOTH (from, from_byte);
1807 SET_PT_BOTH (from, from_byte);
1808 return Qnil;
1810 /* We're at the start of a comment. */
1811 found = forw_comment (from, from_byte, stop, comnested, comstyle, 0,
1812 &out_charpos, &out_bytepos, &dummy);
1813 from = out_charpos; from_byte = out_bytepos;
1814 if (!found)
1816 immediate_quit = 0;
1817 SET_PT_BOTH (from, from_byte);
1818 return Qnil;
1820 INC_BOTH (from, from_byte);
1821 UPDATE_SYNTAX_TABLE_FORWARD (from);
1822 /* We have skipped one comment. */
1823 count1--;
1826 while (count1 < 0)
1828 while (1)
1830 int quoted, comstart_second;
1832 if (from <= stop)
1834 SET_PT_BOTH (BEGV, BEGV_BYTE);
1835 immediate_quit = 0;
1836 return Qnil;
1839 DEC_BOTH (from, from_byte);
1840 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
1841 quoted = char_quoted (from, from_byte);
1842 if (quoted)
1844 DEC_BOTH (from, from_byte);
1845 goto leave;
1847 c = FETCH_CHAR (from_byte);
1848 code = SYNTAX (c);
1849 comstyle = 0;
1850 comnested = SYNTAX_COMMENT_NESTED (c);
1851 if (code == Sendcomment)
1852 comstyle = SYNTAX_COMMENT_STYLE (c);
1853 comstart_second = SYNTAX_COMSTART_SECOND (c);
1854 if (from > stop && SYNTAX_COMEND_SECOND (c)
1855 && prev_char_comend_first (from, from_byte)
1856 && !char_quoted (from - 1, dec_bytepos (from_byte)))
1858 /* We must record the comment style encountered so that
1859 later, we can match only the proper comment begin
1860 sequence of the same style. */
1861 DEC_BOTH (from, from_byte);
1862 code = Sendcomment;
1863 /* Calling char_quoted, above, set up global syntax position
1864 at the new value of FROM. */
1865 c1 = FETCH_CHAR (from_byte);
1866 comstyle = SYNTAX_COMMENT_STYLE (c1);
1867 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
1869 if (from > stop && comstart_second
1870 && prev_char_comstart_first (from, from_byte)
1871 && !char_quoted (from - 1, dec_bytepos (from_byte)))
1873 code = Scomment;
1874 DEC_BOTH (from, from_byte);
1877 if (code == Scomment_fence)
1879 /* Skip until first preceding unquoted comment_fence. */
1880 int found = 0, ini = from, ini_byte = from_byte;
1882 while (1)
1884 DEC_BOTH (from, from_byte);
1885 if (from == stop)
1886 break;
1887 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1888 c = FETCH_CHAR (from_byte);
1889 if (SYNTAX (c) == Scomment_fence
1890 && !char_quoted (from, from_byte))
1892 found = 1;
1893 break;
1896 if (found == 0)
1898 from = ini; /* Set point to ini + 1. */
1899 from_byte = ini_byte;
1900 goto leave;
1903 else if (code == Sendcomment)
1905 found = back_comment (from, from_byte, stop, comnested, comstyle,
1906 &out_charpos, &out_bytepos);
1907 if (found != -1)
1908 from = out_charpos, from_byte = out_bytepos;
1909 /* We have skipped one comment. */
1910 break;
1912 else if (code != Swhitespace && code != Scomment)
1914 leave:
1915 immediate_quit = 0;
1916 INC_BOTH (from, from_byte);
1917 SET_PT_BOTH (from, from_byte);
1918 return Qnil;
1922 count1++;
1925 SET_PT_BOTH (from, from_byte);
1926 immediate_quit = 0;
1927 return Qt;
1930 static Lisp_Object
1931 scan_lists (from, count, depth, sexpflag)
1932 register int from;
1933 int count, depth, sexpflag;
1935 Lisp_Object val;
1936 register int stop = count > 0 ? ZV : BEGV;
1937 register int c, c1;
1938 int stringterm;
1939 int quoted;
1940 int mathexit = 0;
1941 register enum syntaxcode code, temp_code;
1942 int min_depth = depth; /* Err out if depth gets less than this. */
1943 int comstyle = 0; /* style of comment encountered */
1944 int comnested = 0; /* whether the comment is nestable or not */
1945 int temp_pos;
1946 int last_good = from;
1947 int found;
1948 int from_byte;
1949 int out_bytepos, out_charpos;
1950 int temp, dummy;
1952 if (depth > 0) min_depth = 0;
1954 if (from > ZV) from = ZV;
1955 if (from < BEGV) from = BEGV;
1957 from_byte = CHAR_TO_BYTE (from);
1959 immediate_quit = 1;
1960 QUIT;
1962 SETUP_SYNTAX_TABLE (from, count);
1963 while (count > 0)
1965 while (from < stop)
1967 int comstart_first, prefix;
1968 UPDATE_SYNTAX_TABLE_FORWARD (from);
1969 c = FETCH_CHAR (from_byte);
1970 code = SYNTAX (c);
1971 comstart_first = SYNTAX_COMSTART_FIRST (c);
1972 comnested = SYNTAX_COMMENT_NESTED (c);
1973 prefix = SYNTAX_PREFIX (c);
1974 if (depth == min_depth)
1975 last_good = from;
1976 INC_BOTH (from, from_byte);
1977 UPDATE_SYNTAX_TABLE_FORWARD (from);
1978 if (from < stop && comstart_first
1979 && SYNTAX_COMSTART_SECOND (FETCH_CHAR (from_byte))
1980 && parse_sexp_ignore_comments)
1982 /* we have encountered a comment start sequence and we
1983 are ignoring all text inside comments. We must record
1984 the comment style this sequence begins so that later,
1985 only a comment end of the same style actually ends
1986 the comment section */
1987 code = Scomment;
1988 c1 = FETCH_CHAR (from_byte);
1989 comstyle = SYNTAX_COMMENT_STYLE (c1);
1990 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
1991 INC_BOTH (from, from_byte);
1992 UPDATE_SYNTAX_TABLE_FORWARD (from);
1995 if (prefix)
1996 continue;
1998 switch (SWITCH_ENUM_CAST (code))
2000 case Sescape:
2001 case Scharquote:
2002 if (from == stop) goto lose;
2003 INC_BOTH (from, from_byte);
2004 /* treat following character as a word constituent */
2005 case Sword:
2006 case Ssymbol:
2007 if (depth || !sexpflag) break;
2008 /* This word counts as a sexp; return at end of it. */
2009 while (from < stop)
2011 UPDATE_SYNTAX_TABLE_FORWARD (from);
2013 /* Some compilers can't handle this inside the switch. */
2014 temp = SYNTAX (FETCH_CHAR (from_byte));
2015 switch (temp)
2017 case Scharquote:
2018 case Sescape:
2019 INC_BOTH (from, from_byte);
2020 if (from == stop) goto lose;
2021 break;
2022 case Sword:
2023 case Ssymbol:
2024 case Squote:
2025 break;
2026 default:
2027 goto done;
2029 INC_BOTH (from, from_byte);
2031 goto done;
2033 case Scomment_fence:
2034 comstyle = ST_COMMENT_STYLE;
2035 /* FALLTHROUGH */
2036 case Scomment:
2037 if (!parse_sexp_ignore_comments) break;
2038 UPDATE_SYNTAX_TABLE_FORWARD (from);
2039 found = forw_comment (from, from_byte, stop,
2040 comnested, comstyle, 0,
2041 &out_charpos, &out_bytepos, &dummy);
2042 from = out_charpos, from_byte = out_bytepos;
2043 if (!found)
2045 if (depth == 0)
2046 goto done;
2047 goto lose;
2049 INC_BOTH (from, from_byte);
2050 UPDATE_SYNTAX_TABLE_FORWARD (from);
2051 break;
2053 case Smath:
2054 if (!sexpflag)
2055 break;
2056 if (from != stop && c == FETCH_CHAR (from_byte))
2058 INC_BOTH (from, from_byte);
2060 if (mathexit)
2062 mathexit = 0;
2063 goto close1;
2065 mathexit = 1;
2067 case Sopen:
2068 if (!++depth) goto done;
2069 break;
2071 case Sclose:
2072 close1:
2073 if (!--depth) goto done;
2074 if (depth < min_depth)
2075 Fsignal (Qscan_error,
2076 Fcons (build_string ("Containing expression ends prematurely"),
2077 Fcons (make_number (last_good),
2078 Fcons (make_number (from), Qnil))));
2079 break;
2081 case Sstring:
2082 case Sstring_fence:
2083 temp_pos = dec_bytepos (from_byte);
2084 stringterm = FETCH_CHAR (temp_pos);
2085 while (1)
2087 if (from >= stop) goto lose;
2088 UPDATE_SYNTAX_TABLE_FORWARD (from);
2089 if (code == Sstring
2090 ? (FETCH_CHAR (from_byte) == stringterm)
2091 : SYNTAX (FETCH_CHAR (from_byte)) == Sstring_fence)
2092 break;
2094 /* Some compilers can't handle this inside the switch. */
2095 temp = SYNTAX (FETCH_CHAR (from_byte));
2096 switch (temp)
2098 case Scharquote:
2099 case Sescape:
2100 INC_BOTH (from, from_byte);
2102 INC_BOTH (from, from_byte);
2104 INC_BOTH (from, from_byte);
2105 if (!depth && sexpflag) goto done;
2106 break;
2110 /* Reached end of buffer. Error if within object, return nil if between */
2111 if (depth) goto lose;
2113 immediate_quit = 0;
2114 return Qnil;
2116 /* End of object reached */
2117 done:
2118 count--;
2121 while (count < 0)
2123 while (from > stop)
2125 DEC_BOTH (from, from_byte);
2126 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2127 c = FETCH_CHAR (from_byte);
2128 code = SYNTAX (c);
2129 if (depth == min_depth)
2130 last_good = from;
2131 comstyle = 0;
2132 comnested = SYNTAX_COMMENT_NESTED (c);
2133 if (code == Sendcomment)
2134 comstyle = SYNTAX_COMMENT_STYLE (c);
2135 if (from > stop && SYNTAX_COMEND_SECOND (c)
2136 && prev_char_comend_first (from, from_byte)
2137 && parse_sexp_ignore_comments)
2139 /* We must record the comment style encountered so that
2140 later, we can match only the proper comment begin
2141 sequence of the same style. */
2142 DEC_BOTH (from, from_byte);
2143 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2144 code = Sendcomment;
2145 c1 = FETCH_CHAR (from_byte);
2146 comstyle = SYNTAX_COMMENT_STYLE (c1);
2147 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
2150 /* Quoting turns anything except a comment-ender
2151 into a word character. Note that this if cannot be true
2152 if we decremented FROM in the if-statement above. */
2153 if (code != Sendcomment && char_quoted (from, from_byte))
2154 code = Sword;
2155 else if (SYNTAX_PREFIX (c))
2156 continue;
2158 switch (SWITCH_ENUM_CAST (code))
2160 case Sword:
2161 case Ssymbol:
2162 case Sescape:
2163 case Scharquote:
2164 if (depth || !sexpflag) break;
2165 /* This word counts as a sexp; count object finished
2166 after passing it. */
2167 while (from > stop)
2169 temp_pos = from_byte;
2170 if (! NILP (current_buffer->enable_multibyte_characters))
2171 DEC_POS (temp_pos);
2172 else
2173 temp_pos--;
2174 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2175 c1 = FETCH_CHAR (temp_pos);
2176 temp_code = SYNTAX (c1);
2177 /* Don't allow comment-end to be quoted. */
2178 if (temp_code == Sendcomment)
2179 goto done2;
2180 quoted = char_quoted (from - 1, temp_pos);
2181 if (quoted)
2183 DEC_BOTH (from, from_byte);
2184 temp_pos = dec_bytepos (temp_pos);
2185 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2187 c1 = FETCH_CHAR (temp_pos);
2188 temp_code = SYNTAX (c1);
2189 if (! (quoted || temp_code == Sword
2190 || temp_code == Ssymbol
2191 || temp_code == Squote))
2192 goto done2;
2193 DEC_BOTH (from, from_byte);
2195 goto done2;
2197 case Smath:
2198 if (!sexpflag)
2199 break;
2200 temp_pos = dec_bytepos (from_byte);
2201 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2202 if (from != stop && c == FETCH_CHAR (temp_pos))
2203 DEC_BOTH (from, from_byte);
2204 if (mathexit)
2206 mathexit = 0;
2207 goto open2;
2209 mathexit = 1;
2211 case Sclose:
2212 if (!++depth) goto done2;
2213 break;
2215 case Sopen:
2216 open2:
2217 if (!--depth) goto done2;
2218 if (depth < min_depth)
2219 Fsignal (Qscan_error,
2220 Fcons (build_string ("Containing expression ends prematurely"),
2221 Fcons (make_number (last_good),
2222 Fcons (make_number (from), Qnil))));
2223 break;
2225 case Sendcomment:
2226 if (!parse_sexp_ignore_comments)
2227 break;
2228 found = back_comment (from, from_byte, stop, comnested, comstyle,
2229 &out_charpos, &out_bytepos);
2230 if (found != -1)
2231 from = out_charpos, from_byte = out_bytepos;
2232 break;
2234 case Scomment_fence:
2235 case Sstring_fence:
2236 while (1)
2238 DEC_BOTH (from, from_byte);
2239 if (from == stop) goto lose;
2240 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2241 if (!char_quoted (from, from_byte)
2242 && SYNTAX (FETCH_CHAR (from_byte)) == code)
2243 break;
2245 if (code == Sstring_fence && !depth && sexpflag) goto done2;
2246 break;
2248 case Sstring:
2249 stringterm = FETCH_CHAR (from_byte);
2250 while (1)
2252 if (from == stop) goto lose;
2253 temp_pos = from_byte;
2254 if (! NILP (current_buffer->enable_multibyte_characters))
2255 DEC_POS (temp_pos);
2256 else
2257 temp_pos--;
2258 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2259 if (!char_quoted (from - 1, temp_pos)
2260 && stringterm == FETCH_CHAR (temp_pos))
2261 break;
2262 DEC_BOTH (from, from_byte);
2264 DEC_BOTH (from, from_byte);
2265 if (!depth && sexpflag) goto done2;
2266 break;
2270 /* Reached start of buffer. Error if within object, return nil if between */
2271 if (depth) goto lose;
2273 immediate_quit = 0;
2274 return Qnil;
2276 done2:
2277 count++;
2281 immediate_quit = 0;
2282 XSETFASTINT (val, from);
2283 return val;
2285 lose:
2286 Fsignal (Qscan_error,
2287 Fcons (build_string ("Unbalanced parentheses"),
2288 Fcons (make_number (last_good),
2289 Fcons (make_number (from), Qnil))));
2291 /* NOTREACHED */
2294 DEFUN ("scan-lists", Fscan_lists, Sscan_lists, 3, 3, 0,
2295 "Scan from character number FROM by COUNT lists.\n\
2296 Returns the character number of the position thus found.\n\
2298 If DEPTH is nonzero, paren depth begins counting from that value,\n\
2299 only places where the depth in parentheses becomes zero\n\
2300 are candidates for stopping; COUNT such places are counted.\n\
2301 Thus, a positive value for DEPTH means go out levels.\n\
2303 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.\n\
2305 If the beginning or end of (the accessible part of) the buffer is reached\n\
2306 and the depth is wrong, an error is signaled.\n\
2307 If the depth is right but the count is not used up, nil is returned.")
2308 (from, count, depth)
2309 Lisp_Object from, count, depth;
2311 CHECK_NUMBER (from, 0);
2312 CHECK_NUMBER (count, 1);
2313 CHECK_NUMBER (depth, 2);
2315 return scan_lists (XINT (from), XINT (count), XINT (depth), 0);
2318 DEFUN ("scan-sexps", Fscan_sexps, Sscan_sexps, 2, 2, 0,
2319 "Scan from character number FROM by COUNT balanced expressions.\n\
2320 If COUNT is negative, scan backwards.\n\
2321 Returns the character number of the position thus found.\n\
2323 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.\n\
2325 If the beginning or end of (the accessible part of) the buffer is reached\n\
2326 in the middle of a parenthetical grouping, an error is signaled.\n\
2327 If the beginning or end is reached between groupings\n\
2328 but before count is used up, nil is returned.")
2329 (from, count)
2330 Lisp_Object from, count;
2332 CHECK_NUMBER (from, 0);
2333 CHECK_NUMBER (count, 1);
2335 return scan_lists (XINT (from), XINT (count), 0, 1);
2338 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars, Sbackward_prefix_chars,
2339 0, 0, 0,
2340 "Move point backward over any number of chars with prefix syntax.\n\
2341 This includes chars with \"quote\" or \"prefix\" syntax (' or p).")
2344 int beg = BEGV;
2345 int opoint = PT;
2346 int opoint_byte = PT_BYTE;
2347 int pos = PT;
2348 int pos_byte = PT_BYTE;
2349 int c;
2351 if (pos <= beg)
2353 SET_PT_BOTH (opoint, opoint_byte);
2355 return Qnil;
2358 SETUP_SYNTAX_TABLE (pos, -1);
2360 DEC_BOTH (pos, pos_byte);
2362 while (!char_quoted (pos, pos_byte)
2363 /* Previous statement updates syntax table. */
2364 && ((c = FETCH_CHAR (pos_byte), SYNTAX (c) == Squote)
2365 || SYNTAX_PREFIX (c)))
2367 opoint = pos;
2368 opoint_byte = pos_byte;
2370 if (pos + 1 > beg)
2371 DEC_BOTH (pos, pos_byte);
2374 SET_PT_BOTH (opoint, opoint_byte);
2376 return Qnil;
2379 /* Parse forward from FROM / FROM_BYTE to END,
2380 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
2381 and return a description of the state of the parse at END.
2382 If STOPBEFORE is nonzero, stop at the start of an atom.
2383 If COMMENTSTOP is 1, stop at the start of a comment.
2384 If COMMENTSTOP is -1, stop at the start or end of a comment,
2385 after the beginning of a string, or after the end of a string. */
2387 static void
2388 scan_sexps_forward (stateptr, from, from_byte, end, targetdepth,
2389 stopbefore, oldstate, commentstop)
2390 struct lisp_parse_state *stateptr;
2391 register int from;
2392 int end, targetdepth, stopbefore;
2393 Lisp_Object oldstate;
2394 int commentstop;
2396 struct lisp_parse_state state;
2398 register enum syntaxcode code;
2399 int c1;
2400 int comnested;
2401 struct level { int last, prev; };
2402 struct level levelstart[100];
2403 register struct level *curlevel = levelstart;
2404 struct level *endlevel = levelstart + 100;
2405 register int depth; /* Paren depth of current scanning location.
2406 level - levelstart equals this except
2407 when the depth becomes negative. */
2408 int mindepth; /* Lowest DEPTH value seen. */
2409 int start_quoted = 0; /* Nonzero means starting after a char quote */
2410 Lisp_Object tem;
2411 int prev_from; /* Keep one character before FROM. */
2412 int prev_from_byte;
2413 int prev_from_syntax;
2414 int boundary_stop = commentstop == -1;
2415 int nofence;
2416 int found;
2417 int out_bytepos, out_charpos;
2418 int temp;
2420 prev_from = from;
2421 prev_from_byte = from_byte;
2422 if (from != BEGV)
2423 DEC_BOTH (prev_from, prev_from_byte);
2425 /* Use this macro instead of `from++'. */
2426 #define INC_FROM \
2427 do { prev_from = from; \
2428 prev_from_byte = from_byte; \
2429 prev_from_syntax \
2430 = SYNTAX_WITH_FLAGS (FETCH_CHAR (prev_from_byte)); \
2431 INC_BOTH (from, from_byte); \
2432 UPDATE_SYNTAX_TABLE_FORWARD (from); \
2433 } while (0)
2435 immediate_quit = 1;
2436 QUIT;
2438 if (NILP (oldstate))
2440 depth = 0;
2441 state.instring = -1;
2442 state.incomment = 0;
2443 state.comstyle = 0; /* comment style a by default. */
2444 state.comstr_start = -1; /* no comment/string seen. */
2446 else
2448 tem = Fcar (oldstate);
2449 if (!NILP (tem))
2450 depth = XINT (tem);
2451 else
2452 depth = 0;
2454 oldstate = Fcdr (oldstate);
2455 oldstate = Fcdr (oldstate);
2456 oldstate = Fcdr (oldstate);
2457 tem = Fcar (oldstate);
2458 /* Check whether we are inside string_fence-style string: */
2459 state.instring = (!NILP (tem)
2460 ? (INTEGERP (tem) ? XINT (tem) : ST_STRING_STYLE)
2461 : -1);
2463 oldstate = Fcdr (oldstate);
2464 tem = Fcar (oldstate);
2465 state.incomment = (!NILP (tem)
2466 ? (INTEGERP (tem) ? XINT (tem) : -1)
2467 : 0);
2469 oldstate = Fcdr (oldstate);
2470 tem = Fcar (oldstate);
2471 start_quoted = !NILP (tem);
2473 /* if the eighth element of the list is nil, we are in comment
2474 style a. If it is non-nil, we are in comment style b */
2475 oldstate = Fcdr (oldstate);
2476 oldstate = Fcdr (oldstate);
2477 tem = Fcar (oldstate);
2478 state.comstyle = NILP (tem) ? 0 : (EQ (tem, Qsyntax_table)
2479 ? ST_COMMENT_STYLE : 1);
2481 oldstate = Fcdr (oldstate);
2482 tem = Fcar (oldstate);
2483 state.comstr_start = NILP (tem) ? -1 : XINT (tem) ;
2484 oldstate = Fcdr (oldstate);
2485 tem = Fcar (oldstate);
2486 while (!NILP (tem)) /* >= second enclosing sexps. */
2488 /* curlevel++->last ran into compiler bug on Apollo */
2489 curlevel->last = XINT (Fcar (tem));
2490 if (++curlevel == endlevel)
2491 error ("Nesting too deep for parser");
2492 curlevel->prev = -1;
2493 curlevel->last = -1;
2494 tem = Fcdr (tem);
2497 state.quoted = 0;
2498 mindepth = depth;
2500 curlevel->prev = -1;
2501 curlevel->last = -1;
2503 SETUP_SYNTAX_TABLE (prev_from, 1);
2504 prev_from_syntax = SYNTAX_WITH_FLAGS (FETCH_CHAR (prev_from_byte));
2505 UPDATE_SYNTAX_TABLE_FORWARD (from);
2507 /* Enter the loop at a place appropriate for initial state. */
2509 if (state.incomment)
2510 goto startincomment;
2511 if (state.instring >= 0)
2513 nofence = state.instring != ST_STRING_STYLE;
2514 if (start_quoted)
2515 goto startquotedinstring;
2516 goto startinstring;
2518 else if (start_quoted)
2519 goto startquoted;
2521 #if 0 /* This seems to be redundant with the identical code above. */
2522 SETUP_SYNTAX_TABLE (prev_from, 1);
2523 prev_from_syntax = SYNTAX_WITH_FLAGS (FETCH_CHAR (prev_from_byte));
2524 UPDATE_SYNTAX_TABLE_FORWARD (from);
2525 #endif
2527 while (from < end)
2529 INC_FROM;
2530 code = prev_from_syntax & 0xff;
2532 if (code == Scomment)
2534 state.incomment = (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax) ?
2535 1 : -1);
2536 state.comstr_start = prev_from;
2538 else if (code == Scomment_fence)
2540 /* Record the comment style we have entered so that only
2541 the comment-end sequence of the same style actually
2542 terminates the comment section. */
2543 state.comstyle = ST_COMMENT_STYLE;
2544 state.incomment = -1;
2545 state.comstr_start = prev_from;
2546 code = Scomment;
2548 else if (from < end)
2549 if (SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax))
2550 if (c1 = FETCH_CHAR (from_byte),
2551 SYNTAX_COMSTART_SECOND (c1))
2552 /* Duplicate code to avoid a complex if-expression
2553 which causes trouble for the SGI compiler. */
2555 /* Record the comment style we have entered so that only
2556 the comment-end sequence of the same style actually
2557 terminates the comment section. */
2558 state.comstyle = SYNTAX_COMMENT_STYLE (FETCH_CHAR (from_byte));
2559 comnested = SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax);
2560 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
2561 state.incomment = comnested ? 1 : -1;
2562 state.comstr_start = prev_from;
2563 INC_FROM;
2564 code = Scomment;
2567 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax))
2568 continue;
2569 switch (SWITCH_ENUM_CAST (code))
2571 case Sescape:
2572 case Scharquote:
2573 if (stopbefore) goto stop; /* this arg means stop at sexp start */
2574 curlevel->last = prev_from;
2575 startquoted:
2576 if (from == end) goto endquoted;
2577 INC_FROM;
2578 goto symstarted;
2579 /* treat following character as a word constituent */
2580 case Sword:
2581 case Ssymbol:
2582 if (stopbefore) goto stop; /* this arg means stop at sexp start */
2583 curlevel->last = prev_from;
2584 symstarted:
2585 while (from < end)
2587 /* Some compilers can't handle this inside the switch. */
2588 temp = SYNTAX (FETCH_CHAR (from_byte));
2589 switch (temp)
2591 case Scharquote:
2592 case Sescape:
2593 INC_FROM;
2594 if (from == end) goto endquoted;
2595 break;
2596 case Sword:
2597 case Ssymbol:
2598 case Squote:
2599 break;
2600 default:
2601 goto symdone;
2603 INC_FROM;
2605 symdone:
2606 curlevel->prev = curlevel->last;
2607 break;
2609 startincomment:
2610 if (commentstop == 1)
2611 goto done;
2612 goto commentloop;
2614 case Scomment:
2615 assert (state.incomment != 0); /* state.incomment = -1; */
2616 if (commentstop || boundary_stop) goto done;
2617 commentloop:
2618 /* The (from == BEGV) test is to enter the loop in the middle so
2619 that we find a 2-char comment ender even if we start in the
2620 middle of it. */
2621 found = forw_comment (from, from_byte, end,
2622 state.incomment, state.comstyle,
2623 (from == BEGV) ? 0 : prev_from_syntax,
2624 &out_charpos, &out_bytepos, &state.incomment);
2625 from = out_charpos; from_byte = out_bytepos;
2626 /* Beware! prev_from and friends are invalid now.
2627 Luckily, the `done' doesn't use them and the INC_FROM
2628 sets them to a sane value without looking at them. */
2629 if (!found) goto done;
2630 INC_FROM;
2631 state.incomment = 0;
2632 state.comstyle = 0; /* reset the comment style */
2633 if (boundary_stop) goto done;
2634 break;
2636 case Sopen:
2637 if (stopbefore) goto stop; /* this arg means stop at sexp start */
2638 depth++;
2639 /* curlevel++->last ran into compiler bug on Apollo */
2640 curlevel->last = prev_from;
2641 if (++curlevel == endlevel)
2642 error ("Nesting too deep for parser");
2643 curlevel->prev = -1;
2644 curlevel->last = -1;
2645 if (targetdepth == depth) goto done;
2646 break;
2648 case Sclose:
2649 depth--;
2650 if (depth < mindepth)
2651 mindepth = depth;
2652 if (curlevel != levelstart)
2653 curlevel--;
2654 curlevel->prev = curlevel->last;
2655 if (targetdepth == depth) goto done;
2656 break;
2658 case Sstring:
2659 case Sstring_fence:
2660 state.comstr_start = from - 1;
2661 if (stopbefore) goto stop; /* this arg means stop at sexp start */
2662 curlevel->last = prev_from;
2663 state.instring = (code == Sstring
2664 ? (FETCH_CHAR (prev_from_byte))
2665 : ST_STRING_STYLE);
2666 if (boundary_stop) goto done;
2667 startinstring:
2669 nofence = state.instring != ST_STRING_STYLE;
2671 while (1)
2673 int c;
2675 if (from >= end) goto done;
2676 c = FETCH_CHAR (from_byte);
2677 /* Some compilers can't handle this inside the switch. */
2678 temp = SYNTAX (c);
2680 /* Check TEMP here so that if the char has
2681 a syntax-table property which says it is NOT
2682 a string character, it does not end the string. */
2683 if (nofence && c == state.instring && temp == Sstring)
2684 break;
2686 switch (temp)
2688 case Sstring_fence:
2689 if (!nofence) goto string_end;
2690 break;
2691 case Scharquote:
2692 case Sescape:
2693 INC_FROM;
2694 startquotedinstring:
2695 if (from >= end) goto endquoted;
2697 INC_FROM;
2700 string_end:
2701 state.instring = -1;
2702 curlevel->prev = curlevel->last;
2703 INC_FROM;
2704 if (boundary_stop) goto done;
2705 break;
2707 case Smath:
2708 break;
2711 goto done;
2713 stop: /* Here if stopping before start of sexp. */
2714 from = prev_from; /* We have just fetched the char that starts it; */
2715 goto done; /* but return the position before it. */
2717 endquoted:
2718 state.quoted = 1;
2719 done:
2720 state.depth = depth;
2721 state.mindepth = mindepth;
2722 state.thislevelstart = curlevel->prev;
2723 state.prevlevelstart
2724 = (curlevel == levelstart) ? -1 : (curlevel - 1)->last;
2725 state.location = from;
2726 state.levelstarts = Qnil;
2727 while (--curlevel >= levelstart)
2728 state.levelstarts = Fcons (make_number (curlevel->last),
2729 state.levelstarts);
2730 immediate_quit = 0;
2732 *stateptr = state;
2735 /* This comment supplies the doc string for parse-partial-sexp,
2736 for make-docfile to see. We cannot put this in the real DEFUN
2737 due to limits in the Unix cpp.
2739 DEFUN ("parse-partial-sexp", Ffoo, Sfoo, 2, 6, 0,
2740 "Parse Lisp syntax starting at FROM until TO; return status of parse at TO.\n\
2741 Parsing stops at TO or when certain criteria are met;\n\
2742 point is set to where parsing stops.\n\
2743 If fifth arg STATE is omitted or nil,\n\
2744 parsing assumes that FROM is the beginning of a function.\n\
2745 Value is a list of ten elements describing final state of parsing:\n\
2746 0. depth in parens.\n\
2747 1. character address of start of innermost containing list; nil if none.\n\
2748 2. character address of start of last complete sexp terminated.\n\
2749 3. non-nil if inside a string.\n\
2750 (it is the character that will terminate the string,\n\
2751 or t if the string should be terminated by a generic string delimiter.)\n\
2752 4. nil if outside a comment, t if inside a non-nestable comment, \n\
2753 else an integer (the current comment nesting).\n\
2754 5. t if following a quote character.\n\
2755 6. the minimum paren-depth encountered during this scan.\n\
2756 7. t if in a comment of style b; `syntax-table' if the comment\n\
2757 should be terminated by a generic comment delimiter.\n\
2758 8. character address of start of comment or string; nil if not in one.\n\
2759 9. Intermediate data for continuation of parsing (subject to change).\n\
2760 If third arg TARGETDEPTH is non-nil, parsing stops if the depth\n\
2761 in parentheses becomes equal to TARGETDEPTH.\n\
2762 Fourth arg STOPBEFORE non-nil means stop when come to\n\
2763 any character that starts a sexp.\n\
2764 Fifth arg STATE is a nine-element list like what this function returns.\n\
2765 It is used to initialize the state of the parse. Elements number 1, 2, 6\n\
2766 and 8 are ignored; you can leave off element 8 (the last) entirely.\n\
2767 Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.\n\
2768 If it is `syntax-table', stop after the start of a comment or a string,\n\
2769 or after end of a comment or a string.")
2770 (from, to, targetdepth, stopbefore, state, commentstop)
2773 DEFUN ("parse-partial-sexp", Fparse_partial_sexp, Sparse_partial_sexp, 2, 6, 0,
2774 0 /* See immediately above */)
2775 (from, to, targetdepth, stopbefore, oldstate, commentstop)
2776 Lisp_Object from, to, targetdepth, stopbefore, oldstate, commentstop;
2778 struct lisp_parse_state state;
2779 int target;
2781 if (!NILP (targetdepth))
2783 CHECK_NUMBER (targetdepth, 3);
2784 target = XINT (targetdepth);
2786 else
2787 target = -100000; /* We won't reach this depth */
2789 validate_region (&from, &to);
2790 scan_sexps_forward (&state, XINT (from), CHAR_TO_BYTE (XINT (from)),
2791 XINT (to),
2792 target, !NILP (stopbefore), oldstate,
2793 (NILP (commentstop)
2794 ? 0 : (EQ (commentstop, Qsyntax_table) ? -1 : 1)));
2796 SET_PT (state.location);
2798 return Fcons (make_number (state.depth),
2799 Fcons (state.prevlevelstart < 0 ? Qnil : make_number (state.prevlevelstart),
2800 Fcons (state.thislevelstart < 0 ? Qnil : make_number (state.thislevelstart),
2801 Fcons (state.instring >= 0
2802 ? (state.instring == ST_STRING_STYLE
2803 ? Qt : make_number (state.instring)) : Qnil,
2804 Fcons (state.incomment < 0 ? Qt :
2805 (state.incomment == 0 ? Qnil :
2806 make_number (state.incomment)),
2807 Fcons (state.quoted ? Qt : Qnil,
2808 Fcons (make_number (state.mindepth),
2809 Fcons ((state.comstyle
2810 ? (state.comstyle == ST_COMMENT_STYLE
2811 ? Qsyntax_table : Qt) :
2812 Qnil),
2813 Fcons (((state.incomment
2814 || (state.instring >= 0))
2815 ? make_number (state.comstr_start)
2816 : Qnil),
2817 Fcons (state.levelstarts, Qnil))))))))));
2820 void
2821 init_syntax_once ()
2823 register int i, c;
2824 Lisp_Object temp;
2826 /* This has to be done here, before we call Fmake_char_table. */
2827 Qsyntax_table = intern ("syntax-table");
2828 staticpro (&Qsyntax_table);
2830 /* Intern this now in case it isn't already done.
2831 Setting this variable twice is harmless.
2832 But don't staticpro it here--that is done in alloc.c. */
2833 Qchar_table_extra_slots = intern ("char-table-extra-slots");
2835 /* Create objects which can be shared among syntax tables. */
2836 Vsyntax_code_object = Fmake_vector (make_number (13), Qnil);
2837 for (i = 0; i < XVECTOR (Vsyntax_code_object)->size; i++)
2838 XVECTOR (Vsyntax_code_object)->contents[i]
2839 = Fcons (make_number (i), Qnil);
2841 /* Now we are ready to set up this property, so we can
2842 create syntax tables. */
2843 Fput (Qsyntax_table, Qchar_table_extra_slots, make_number (0));
2845 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Swhitespace];
2847 Vstandard_syntax_table = Fmake_char_table (Qsyntax_table, temp);
2849 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Sword];
2850 for (i = 'a'; i <= 'z'; i++)
2851 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
2852 for (i = 'A'; i <= 'Z'; i++)
2853 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
2854 for (i = '0'; i <= '9'; i++)
2855 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
2857 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '$', temp);
2858 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '%', temp);
2860 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '(',
2861 Fcons (make_number (Sopen), make_number (')')));
2862 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ')',
2863 Fcons (make_number (Sclose), make_number ('(')));
2864 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '[',
2865 Fcons (make_number (Sopen), make_number (']')));
2866 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ']',
2867 Fcons (make_number (Sclose), make_number ('[')));
2868 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '{',
2869 Fcons (make_number (Sopen), make_number ('}')));
2870 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '}',
2871 Fcons (make_number (Sclose), make_number ('{')));
2872 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '"',
2873 Fcons (make_number ((int) Sstring), Qnil));
2874 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\\',
2875 Fcons (make_number ((int) Sescape), Qnil));
2877 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Ssymbol];
2878 for (i = 0; i < 10; i++)
2880 c = "_-+*/&|<>="[i];
2881 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
2884 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Spunct];
2885 for (i = 0; i < 12; i++)
2887 c = ".,;:?!#@~^'`"[i];
2888 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
2892 void
2893 syms_of_syntax ()
2895 Qsyntax_table_p = intern ("syntax-table-p");
2896 staticpro (&Qsyntax_table_p);
2898 staticpro (&Vsyntax_code_object);
2900 Qscan_error = intern ("scan-error");
2901 staticpro (&Qscan_error);
2902 Fput (Qscan_error, Qerror_conditions,
2903 Fcons (Qscan_error, Fcons (Qerror, Qnil)));
2904 Fput (Qscan_error, Qerror_message,
2905 build_string ("Scan error"));
2907 DEFVAR_BOOL ("parse-sexp-ignore-comments", &parse_sexp_ignore_comments,
2908 "Non-nil means `forward-sexp', etc., should treat comments as whitespace.");
2910 DEFVAR_BOOL ("parse-sexp-lookup-properties", &parse_sexp_lookup_properties,
2911 "Non-nil means `forward-sexp', etc., grant `syntax-table' property.\n\
2912 The value of this property should be either a syntax table, or a cons\n\
2913 of the form (SYNTAXCODE . MATCHCHAR), SYNTAXCODE being the numeric\n\
2914 syntax code, MATCHCHAR being nil or the character to match (which is\n\
2915 relevant only for open/close type.");
2917 words_include_escapes = 0;
2918 DEFVAR_BOOL ("words-include-escapes", &words_include_escapes,
2919 "Non-nil means `forward-word', etc., should treat escape chars part of words.");
2921 defsubr (&Ssyntax_table_p);
2922 defsubr (&Ssyntax_table);
2923 defsubr (&Sstandard_syntax_table);
2924 defsubr (&Scopy_syntax_table);
2925 defsubr (&Sset_syntax_table);
2926 defsubr (&Schar_syntax);
2927 defsubr (&Smatching_paren);
2928 defsubr (&Smodify_syntax_entry);
2929 defsubr (&Sdescribe_syntax);
2931 defsubr (&Sforward_word);
2933 defsubr (&Sskip_chars_forward);
2934 defsubr (&Sskip_chars_backward);
2935 defsubr (&Sskip_syntax_forward);
2936 defsubr (&Sskip_syntax_backward);
2938 defsubr (&Sforward_comment);
2939 defsubr (&Sscan_lists);
2940 defsubr (&Sscan_sexps);
2941 defsubr (&Sbackward_prefix_chars);
2942 defsubr (&Sparse_partial_sexp);