(batch-update-autoloads): Call `update-directory-autoloads'.
[emacs.git] / src / syntax.c
blobd25c4394c6efdcfbeef49f1e1a9679a68a8e7de7
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 "keymap.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 /* Nonzero means `scan-sexps' treat all multibyte characters as symbol. */
50 int multibyte_syntax_as_symbol;
52 /* Used as a temporary in SYNTAX_ENTRY and other macros in syntax.h,
53 if not compiled with GCC. No need to mark it, since it is used
54 only very temporarily. */
55 Lisp_Object syntax_temp;
57 /* Non-zero means an open parenthesis in column 0 is always considered
58 to be the start of a defun. Zero means an open parenthesis in
59 column 0 has no special meaning. */
61 int open_paren_in_column_0_is_defun_start;
63 /* This is the internal form of the parse state used in parse-partial-sexp. */
65 struct lisp_parse_state
67 int depth; /* Depth at end of parsing. */
68 int instring; /* -1 if not within string, else desired terminator. */
69 int incomment; /* -1 if in unnestable comment else comment nesting */
70 int comstyle; /* comment style a=0, or b=1, or ST_COMMENT_STYLE. */
71 int quoted; /* Nonzero if just after an escape char at end of parsing */
72 int thislevelstart; /* Char number of most recent start-of-expression at current level */
73 int prevlevelstart; /* Char number of start of containing expression */
74 int location; /* Char number at which parsing stopped. */
75 int mindepth; /* Minimum depth seen while scanning. */
76 int comstr_start; /* Position just after last comment/string starter. */
77 Lisp_Object levelstarts; /* Char numbers of starts-of-expression
78 of levels (starting from outermost). */
81 /* These variables are a cache for finding the start of a defun.
82 find_start_pos is the place for which the defun start was found.
83 find_start_value is the defun start position found for it.
84 find_start_value_byte is the corresponding byte position.
85 find_start_buffer is the buffer it was found in.
86 find_start_begv is the BEGV value when it was found.
87 find_start_modiff is the value of MODIFF when it was found. */
89 static int find_start_pos;
90 static int find_start_value;
91 static int find_start_value_byte;
92 static struct buffer *find_start_buffer;
93 static int find_start_begv;
94 static int find_start_modiff;
97 static int find_defun_start P_ ((int, int));
98 static int back_comment P_ ((int, int, int, int, int, int *, int *));
99 static int char_quoted P_ ((int, int));
100 static Lisp_Object skip_chars P_ ((int, int, Lisp_Object, Lisp_Object));
101 static Lisp_Object scan_lists P_ ((int, int, int, int));
102 static void scan_sexps_forward P_ ((struct lisp_parse_state *,
103 int, int, int, int,
104 int, Lisp_Object, int));
107 struct gl_state_s gl_state; /* Global state of syntax parser. */
109 INTERVAL interval_of ();
110 #define INTERVALS_AT_ONCE 10 /* 1 + max-number of intervals
111 to scan to property-change. */
113 /* Update gl_state to an appropriate interval which contains CHARPOS. The
114 sign of COUNT give the relative position of CHARPOS wrt the previously
115 valid interval. If INIT, only [be]_property fields of gl_state are
116 valid at start, the rest is filled basing on OBJECT.
118 `gl_state.*_i' are the intervals, and CHARPOS is further in the search
119 direction than the intervals - or in an interval. We update the
120 current syntax-table basing on the property of this interval, and
121 update the interval to start further than CHARPOS - or be
122 NULL_INTERVAL. We also update lim_property to be the next value of
123 charpos to call this subroutine again - or be before/after the
124 start/end of OBJECT. */
126 void
127 update_syntax_table (charpos, count, init, object)
128 int charpos, count, init;
129 Lisp_Object object;
131 Lisp_Object tmp_table;
132 int cnt = 0, invalidate = 1;
133 INTERVAL i, oldi;
135 if (init)
137 gl_state.old_prop = Qnil;
138 gl_state.start = gl_state.b_property;
139 gl_state.stop = gl_state.e_property;
140 i = interval_of (charpos, object);
141 gl_state.backward_i = gl_state.forward_i = i;
142 invalidate = 0;
143 if (NULL_INTERVAL_P (i))
144 return;
145 /* interval_of updates only ->position of the return value, so
146 update the parents manually to speed up update_interval. */
147 while (!NULL_PARENT (i))
149 if (AM_RIGHT_CHILD (i))
150 INTERVAL_PARENT (i)->position = i->position
151 - LEFT_TOTAL_LENGTH (i) + TOTAL_LENGTH (i) /* right end */
152 - TOTAL_LENGTH (INTERVAL_PARENT (i))
153 + LEFT_TOTAL_LENGTH (INTERVAL_PARENT (i));
154 else
155 INTERVAL_PARENT (i)->position = i->position - LEFT_TOTAL_LENGTH (i)
156 + TOTAL_LENGTH (i);
157 i = INTERVAL_PARENT (i);
159 i = gl_state.forward_i;
160 gl_state.b_property = i->position - gl_state.offset;
161 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
162 goto update;
164 oldi = i = count > 0 ? gl_state.forward_i : gl_state.backward_i;
166 /* We are guaranteed to be called with CHARPOS either in i,
167 or further off. */
168 if (NULL_INTERVAL_P (i))
169 error ("Error in syntax_table logic for to-the-end intervals");
170 else if (charpos < i->position) /* Move left. */
172 if (count > 0)
173 error ("Error in syntax_table logic for intervals <-");
174 /* Update the interval. */
175 i = update_interval (i, charpos);
176 if (INTERVAL_LAST_POS (i) != gl_state.b_property)
178 invalidate = 0;
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 != gl_state.e_property)
191 invalidate = 0;
192 gl_state.backward_i = i;
193 gl_state.b_property = i->position - gl_state.offset;
197 update:
198 tmp_table = textget (i->plist, Qsyntax_table);
200 if (invalidate)
201 invalidate = !EQ (tmp_table, gl_state.old_prop); /* Need to invalidate? */
203 if (invalidate) /* Did not get to adjacent interval. */
204 { /* with the same table => */
205 /* invalidate the old range. */
206 if (count > 0)
208 gl_state.backward_i = i;
209 gl_state.b_property = i->position - gl_state.offset;
211 else
213 gl_state.forward_i = i;
214 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
218 if (!EQ (tmp_table, gl_state.old_prop))
220 gl_state.current_syntax_table = tmp_table;
221 gl_state.old_prop = tmp_table;
222 if (EQ (Fsyntax_table_p (tmp_table), Qt))
224 gl_state.use_global = 0;
226 else if (CONSP (tmp_table))
228 gl_state.use_global = 1;
229 gl_state.global_code = tmp_table;
231 else
233 gl_state.use_global = 0;
234 gl_state.current_syntax_table = current_buffer->syntax_table;
238 while (!NULL_INTERVAL_P (i))
240 if (cnt && !EQ (tmp_table, textget (i->plist, Qsyntax_table)))
242 if (count > 0)
244 gl_state.e_property = i->position - gl_state.offset;
245 gl_state.forward_i = i;
247 else
249 gl_state.b_property = i->position + LENGTH (i) - gl_state.offset;
250 gl_state.backward_i = i;
252 return;
254 else if (cnt == INTERVALS_AT_ONCE)
256 if (count > 0)
258 gl_state.e_property = i->position + LENGTH (i) - gl_state.offset;
259 gl_state.forward_i = i;
261 else
263 gl_state.b_property = i->position - gl_state.offset;
264 gl_state.backward_i = i;
266 return;
268 cnt++;
269 i = count > 0 ? next_interval (i) : previous_interval (i);
271 eassert (NULL_INTERVAL_P (i)); /* This property goes to the end. */
272 if (count > 0)
273 gl_state.e_property = gl_state.stop;
274 else
275 gl_state.b_property = gl_state.start;
278 /* Returns TRUE if char at CHARPOS is quoted.
279 Global syntax-table data should be set up already to be good at CHARPOS
280 or after. On return global syntax data is good for lookup at CHARPOS. */
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 /* Return a defun-start position before before POS and not too far before.
337 It should be the last one before POS, or nearly the last.
339 When open_paren_in_column_0_is_defun_start is nonzero,
340 the beginning of every line is treated as a defun-start.
342 We record the information about where the scan started
343 and what its result was, so that another call in the same area
344 can return the same value very quickly.
346 There is no promise at which position the global syntax data is
347 valid on return from the subroutine, so the caller should explicitly
348 update the global data. */
350 static int
351 find_defun_start (pos, pos_byte)
352 int pos, pos_byte;
354 int opoint = PT, opoint_byte = PT_BYTE;
356 /* Use previous finding, if it's valid and applies to this inquiry. */
357 if (current_buffer == find_start_buffer
358 /* Reuse the defun-start even if POS is a little farther on.
359 POS might be in the next defun, but that's ok.
360 Our value may not be the best possible, but will still be usable. */
361 && pos <= find_start_pos + 1000
362 && pos >= find_start_value
363 && BEGV == find_start_begv
364 && MODIFF == find_start_modiff)
365 return find_start_value;
367 /* Back up to start of line. */
368 scan_newline (pos, pos_byte, BEGV, BEGV_BYTE, -1, 1);
370 /* We optimize syntax-table lookup for rare updates. Thus we accept
371 only those `^\s(' which are good in global _and_ text-property
372 syntax-tables. */
373 gl_state.current_syntax_table = current_buffer->syntax_table;
374 gl_state.use_global = 0;
375 if (open_paren_in_column_0_is_defun_start)
377 while (PT > BEGV)
379 /* Open-paren at start of line means we may have found our
380 defun-start. */
381 if (SYNTAX (FETCH_CHAR (PT_BYTE)) == Sopen)
383 SETUP_SYNTAX_TABLE (PT + 1, -1); /* Try again... */
384 if (SYNTAX (FETCH_CHAR (PT_BYTE)) == Sopen)
385 break;
386 /* Now fallback to the default value. */
387 gl_state.current_syntax_table = current_buffer->syntax_table;
388 gl_state.use_global = 0;
390 /* Move to beg of previous line. */
391 scan_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, -2, 1);
395 /* Record what we found, for the next try. */
396 find_start_value = PT;
397 find_start_value_byte = PT_BYTE;
398 find_start_buffer = current_buffer;
399 find_start_modiff = MODIFF;
400 find_start_begv = BEGV;
401 find_start_pos = pos;
403 TEMP_SET_PT_BOTH (opoint, opoint_byte);
405 return find_start_value;
408 /* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
410 static int
411 prev_char_comend_first (pos, pos_byte)
412 int pos, pos_byte;
414 int c, val;
416 DEC_BOTH (pos, pos_byte);
417 UPDATE_SYNTAX_TABLE_BACKWARD (pos);
418 c = FETCH_CHAR (pos_byte);
419 val = SYNTAX_COMEND_FIRST (c);
420 UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
421 return val;
424 /* Return the SYNTAX_COMSTART_FIRST of the character before POS, POS_BYTE. */
426 /* static int
427 * prev_char_comstart_first (pos, pos_byte)
428 * int pos, pos_byte;
430 * int c, val;
432 * DEC_BOTH (pos, pos_byte);
433 * UPDATE_SYNTAX_TABLE_BACKWARD (pos);
434 * c = FETCH_CHAR (pos_byte);
435 * val = SYNTAX_COMSTART_FIRST (c);
436 * UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
437 * return val;
438 * } */
440 /* Checks whether charpos FROM is at the end of a comment.
441 FROM_BYTE is the bytepos corresponding to FROM.
442 Do not move back before STOP.
444 Return a positive value if we find a comment ending at FROM/FROM_BYTE;
445 return -1 otherwise.
447 If successful, store the charpos of the comment's beginning
448 into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR.
450 Global syntax data remains valid for backward search starting at
451 the returned value (or at FROM, if the search was not successful). */
453 static int
454 back_comment (from, from_byte, stop, comnested, comstyle, charpos_ptr, bytepos_ptr)
455 int from, from_byte, stop;
456 int comnested, comstyle;
457 int *charpos_ptr, *bytepos_ptr;
459 /* Look back, counting the parity of string-quotes,
460 and recording the comment-starters seen.
461 When we reach a safe place, assume that's not in a string;
462 then step the main scan to the earliest comment-starter seen
463 an even number of string quotes away from the safe place.
465 OFROM[I] is position of the earliest comment-starter seen
466 which is I+2X quotes from the comment-end.
467 PARITY is current parity of quotes from the comment end. */
468 int string_style = -1; /* Presumed outside of any string. */
469 int string_lossage = 0;
470 /* Not a real lossage: indicates that we have passed a matching comment
471 starter plus a non-matching comment-ender, meaning that any matching
472 comment-starter we might see later could be a false positive (hidden
473 inside another comment).
474 Test case: { a (* b } c (* d *) */
475 int comment_lossage = 0;
476 int comment_end = from;
477 int comment_end_byte = from_byte;
478 int comstart_pos = 0;
479 int comstart_byte;
480 /* Place where the containing defun starts,
481 or 0 if we didn't come across it yet. */
482 int defun_start = 0;
483 int defun_start_byte = 0;
484 register enum syntaxcode code;
485 int nesting = 1; /* current comment nesting */
486 int c;
487 int syntax = 0;
489 /* FIXME: A }} comment-ender style leads to incorrect behavior
490 in the case of {{ c }}} because we ignore the last two chars which are
491 assumed to be comment-enders although they aren't. */
493 /* At beginning of range to scan, we're outside of strings;
494 that determines quote parity to the comment-end. */
495 while (from != stop)
497 int temp_byte, prev_syntax;
498 int com2start, com2end;
500 /* Move back and examine a character. */
501 DEC_BOTH (from, from_byte);
502 UPDATE_SYNTAX_TABLE_BACKWARD (from);
504 prev_syntax = syntax;
505 c = FETCH_CHAR (from_byte);
506 syntax = SYNTAX_WITH_FLAGS (c);
507 code = SYNTAX (c);
509 /* Check for 2-char comment markers. */
510 com2start = (SYNTAX_FLAGS_COMSTART_FIRST (syntax)
511 && SYNTAX_FLAGS_COMSTART_SECOND (prev_syntax)
512 && comstyle == SYNTAX_FLAGS_COMMENT_STYLE (prev_syntax)
513 && (SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax)
514 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested);
515 com2end = (SYNTAX_FLAGS_COMEND_FIRST (syntax)
516 && SYNTAX_FLAGS_COMEND_SECOND (prev_syntax));
518 /* Nasty cases with overlapping 2-char comment markers:
519 - snmp-mode: -- c -- foo -- c --
520 --- c --
521 ------ c --
522 - c-mode: *||*
523 |* *|* *|
524 |*| |* |*|
525 /// */
527 /* If a 2-char comment sequence partly overlaps with another,
528 we don't try to be clever. */
529 if (from > stop && (com2end || com2start))
531 int next = from, next_byte = from_byte, next_c, next_syntax;
532 DEC_BOTH (next, next_byte);
533 UPDATE_SYNTAX_TABLE_BACKWARD (next);
534 next_c = FETCH_CHAR (next_byte);
535 next_syntax = SYNTAX_WITH_FLAGS (next_c);
536 if (((com2start || comnested)
537 && SYNTAX_FLAGS_COMEND_SECOND (syntax)
538 && SYNTAX_FLAGS_COMEND_FIRST (next_syntax))
539 || ((com2end || comnested)
540 && SYNTAX_FLAGS_COMSTART_SECOND (syntax)
541 && comstyle == SYNTAX_FLAGS_COMMENT_STYLE (syntax)
542 && SYNTAX_FLAGS_COMSTART_FIRST (next_syntax)))
543 goto lossage;
544 /* UPDATE_SYNTAX_TABLE_FORWARD (next + 1); */
547 if (com2start && comstart_pos == 0)
548 /* We're looking at a comment starter. But it might be a comment
549 ender as well (see snmp-mode). The first time we see one, we
550 need to consider it as a comment starter,
551 and the subsequent times as a comment ender. */
552 com2end = 0;
554 /* Turn a 2-char comment sequences into the appropriate syntax. */
555 if (com2end)
556 code = Sendcomment;
557 else if (com2start)
558 code = Scomment;
559 /* Ignore comment starters of a different style. */
560 else if (code == Scomment
561 && (comstyle != SYNTAX_FLAGS_COMMENT_STYLE (syntax)
562 || SYNTAX_FLAGS_COMMENT_NESTED (syntax) != comnested))
563 continue;
565 /* Ignore escaped characters, except comment-enders. */
566 if (code != Sendcomment && char_quoted (from, from_byte))
567 continue;
569 switch (code)
571 case Sstring_fence:
572 case Scomment_fence:
573 c = (code == Sstring_fence ? ST_STRING_STYLE : ST_COMMENT_STYLE);
574 case Sstring:
575 /* Track parity of quotes. */
576 if (string_style == -1)
577 /* Entering a string. */
578 string_style = c;
579 else if (string_style == c)
580 /* Leaving the string. */
581 string_style = -1;
582 else
583 /* If we have two kinds of string delimiters.
584 There's no way to grok this scanning backwards. */
585 string_lossage = 1;
586 break;
588 case Scomment:
589 /* We've already checked that it is the relevant comstyle. */
590 if (string_style != -1 || comment_lossage || string_lossage)
591 /* There are odd string quotes involved, so let's be careful.
592 Test case in Pascal: " { " a { " } */
593 goto lossage;
595 if (!comnested)
597 /* Record best comment-starter so far. */
598 comstart_pos = from;
599 comstart_byte = from_byte;
601 else if (--nesting <= 0)
602 /* nested comments have to be balanced, so we don't need to
603 keep looking for earlier ones. We use here the same (slightly
604 incorrect) reasoning as below: since it is followed by uniform
605 paired string quotes, this comment-start has to be outside of
606 strings, else the comment-end itself would be inside a string. */
607 goto done;
608 break;
610 case Sendcomment:
611 if (SYNTAX_FLAGS_COMMENT_STYLE (syntax) == comstyle
612 && ((com2end && SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax))
613 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested)
614 /* This is the same style of comment ender as ours. */
616 if (comnested)
617 nesting++;
618 else
619 /* Anything before that can't count because it would match
620 this comment-ender rather than ours. */
621 from = stop; /* Break out of the loop. */
623 else if (comstart_pos != 0 || c != '\n')
624 /* We're mixing comment styles here, so we'd better be careful.
625 The (comstart_pos != 0 || c != '\n') check is not quite correct
626 (we should just always set comment_lossage), but removing it
627 would imply that any multiline comment in C would go through
628 lossage, which seems overkill.
629 The failure should only happen in the rare cases such as
630 { (* } *) */
631 comment_lossage = 1;
632 break;
634 case Sopen:
635 /* Assume a defun-start point is outside of strings. */
636 if (open_paren_in_column_0_is_defun_start
637 && (from == stop
638 || (temp_byte = dec_bytepos (from_byte),
639 FETCH_CHAR (temp_byte) == '\n')))
641 defun_start = from;
642 defun_start_byte = from_byte;
643 from = stop; /* Break out of the loop. */
645 break;
647 default:
648 break;
652 if (comstart_pos == 0)
654 from = comment_end;
655 from_byte = comment_end_byte;
656 UPDATE_SYNTAX_TABLE_FORWARD (comment_end - 1);
658 /* If comstart_pos is set and we get here (ie. didn't jump to `lossage'
659 or `done'), then we've found the beginning of the non-nested comment. */
660 else if (1) /* !comnested */
662 from = comstart_pos;
663 from_byte = comstart_byte;
664 /* Globals are correct now. */
666 else
668 struct lisp_parse_state state;
669 lossage:
670 /* We had two kinds of string delimiters mixed up
671 together. Decode this going forwards.
672 Scan fwd from a known safe place (beginning-of-defun)
673 to the one in question; this records where we
674 last passed a comment starter. */
675 /* If we did not already find the defun start, find it now. */
676 if (defun_start == 0)
678 defun_start = find_defun_start (comment_end, comment_end_byte);
679 defun_start_byte = find_start_value_byte;
683 scan_sexps_forward (&state,
684 defun_start, defun_start_byte,
685 comment_end, -10000, 0, Qnil, 0);
686 defun_start = comment_end;
687 if (state.incomment == (comnested ? 1 : -1)
688 && state.comstyle == comstyle)
689 from = state.comstr_start;
690 else
692 from = comment_end;
693 if (state.incomment)
694 /* If comment_end is inside some other comment, maybe ours
695 is nested, so we need to try again from within the
696 surrounding comment. Example: { a (* " *) */
698 /* FIXME: We should advance by one or two chars. */
699 defun_start = state.comstr_start + 2;
700 defun_start_byte = CHAR_TO_BYTE (defun_start);
703 } while (defun_start < comment_end);
705 from_byte = CHAR_TO_BYTE (from);
706 UPDATE_SYNTAX_TABLE_FORWARD (from - 1);
709 done:
710 *charpos_ptr = from;
711 *bytepos_ptr = from_byte;
713 return (from == comment_end) ? -1 : from;
716 DEFUN ("syntax-table-p", Fsyntax_table_p, Ssyntax_table_p, 1, 1, 0,
717 doc: /* Return t if OBJECT is a syntax table.
718 Currently, any char-table counts as a syntax table. */)
719 (object)
720 Lisp_Object object;
722 if (CHAR_TABLE_P (object)
723 && EQ (XCHAR_TABLE (object)->purpose, Qsyntax_table))
724 return Qt;
725 return Qnil;
728 static void
729 check_syntax_table (obj)
730 Lisp_Object obj;
732 if (!(CHAR_TABLE_P (obj)
733 && EQ (XCHAR_TABLE (obj)->purpose, Qsyntax_table)))
734 wrong_type_argument (Qsyntax_table_p, obj);
737 DEFUN ("syntax-table", Fsyntax_table, Ssyntax_table, 0, 0, 0,
738 doc: /* Return the current syntax table.
739 This is the one specified by the current buffer. */)
742 return current_buffer->syntax_table;
745 DEFUN ("standard-syntax-table", Fstandard_syntax_table,
746 Sstandard_syntax_table, 0, 0, 0,
747 doc: /* Return the standard syntax table.
748 This is the one used for new buffers. */)
751 return Vstandard_syntax_table;
754 DEFUN ("copy-syntax-table", Fcopy_syntax_table, Scopy_syntax_table, 0, 1, 0,
755 doc: /* Construct a new syntax table and return it.
756 It is a copy of the TABLE, which defaults to the standard syntax table. */)
757 (table)
758 Lisp_Object table;
760 Lisp_Object copy;
762 if (!NILP (table))
763 check_syntax_table (table);
764 else
765 table = Vstandard_syntax_table;
767 copy = Fcopy_sequence (table);
769 /* Only the standard syntax table should have a default element.
770 Other syntax tables should inherit from parents instead. */
771 XCHAR_TABLE (copy)->defalt = Qnil;
773 /* Copied syntax tables should all have parents.
774 If we copied one with no parent, such as the standard syntax table,
775 use the standard syntax table as the copy's parent. */
776 if (NILP (XCHAR_TABLE (copy)->parent))
777 Fset_char_table_parent (copy, Vstandard_syntax_table);
778 return copy;
781 DEFUN ("set-syntax-table", Fset_syntax_table, Sset_syntax_table, 1, 1, 0,
782 doc: /* Select a new syntax table for the current buffer.
783 One argument, a syntax table. */)
784 (table)
785 Lisp_Object table;
787 int idx;
788 check_syntax_table (table);
789 current_buffer->syntax_table = table;
790 /* Indicate that this buffer now has a specified syntax table. */
791 idx = PER_BUFFER_VAR_IDX (syntax_table);
792 SET_PER_BUFFER_VALUE_P (current_buffer, idx, 1);
793 return table;
796 /* Convert a letter which signifies a syntax code
797 into the code it signifies.
798 This is used by modify-syntax-entry, and other things. */
800 unsigned char syntax_spec_code[0400] =
801 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
802 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
803 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
804 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
805 (char) Swhitespace, (char) Scomment_fence, (char) Sstring, 0377,
806 (char) Smath, 0377, 0377, (char) Squote,
807 (char) Sopen, (char) Sclose, 0377, 0377,
808 0377, (char) Swhitespace, (char) Spunct, (char) Scharquote,
809 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
810 0377, 0377, 0377, 0377,
811 (char) Scomment, 0377, (char) Sendcomment, 0377,
812 (char) Sinherit, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
813 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
814 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword,
815 0377, 0377, 0377, 0377, (char) Sescape, 0377, 0377, (char) Ssymbol,
816 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
817 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
818 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword,
819 0377, 0377, 0377, 0377, (char) Sstring_fence, 0377, 0377, 0377
822 /* Indexed by syntax code, give the letter that describes it. */
824 char syntax_code_spec[16] =
826 ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@',
827 '!', '|'
830 /* Indexed by syntax code, give the object (cons of syntax code and
831 nil) to be stored in syntax table. Since these objects can be
832 shared among syntax tables, we generate them in advance. By
833 sharing objects, the function `describe-syntax' can give a more
834 compact listing. */
835 static Lisp_Object Vsyntax_code_object;
838 /* Look up the value for CHARACTER in syntax table TABLE's parent
839 and its parents. SYNTAX_ENTRY calls this, when TABLE itself has nil
840 for CHARACTER. It's actually used only when not compiled with GCC. */
842 Lisp_Object
843 syntax_parent_lookup (table, character)
844 Lisp_Object table;
845 int character;
847 Lisp_Object value;
849 while (1)
851 table = XCHAR_TABLE (table)->parent;
852 if (NILP (table))
853 return Qnil;
855 value = XCHAR_TABLE (table)->contents[character];
856 if (!NILP (value))
857 return value;
861 DEFUN ("char-syntax", Fchar_syntax, Schar_syntax, 1, 1, 0,
862 doc: /* Return the syntax code of CHARACTER, described by a character.
863 For example, if CHARACTER is a word constituent,
864 the character `w' is returned.
865 The characters that correspond to various syntax codes
866 are listed in the documentation of `modify-syntax-entry'. */)
867 (character)
868 Lisp_Object character;
870 int char_int;
871 gl_state.current_syntax_table = current_buffer->syntax_table;
873 gl_state.use_global = 0;
874 CHECK_NUMBER (character);
875 char_int = XINT (character);
876 return make_number (syntax_code_spec[(int) SYNTAX (char_int)]);
879 DEFUN ("matching-paren", Fmatching_paren, Smatching_paren, 1, 1, 0,
880 doc: /* Return the matching parenthesis of CHARACTER, or nil if none. */)
881 (character)
882 Lisp_Object character;
884 int char_int, code;
885 gl_state.current_syntax_table = current_buffer->syntax_table;
886 gl_state.use_global = 0;
887 CHECK_NUMBER (character);
888 char_int = XINT (character);
889 code = SYNTAX (char_int);
890 if (code == Sopen || code == Sclose)
891 return SYNTAX_MATCH (char_int);
892 return Qnil;
895 DEFUN ("string-to-syntax", Fstring_to_syntax, Sstring_to_syntax, 1, 1, 0,
896 doc: /* Convert a syntax specification STRING into syntax cell form.
897 STRING should be a string as it is allowed as argument of
898 `modify-syntax-entry'. Value is the equivalent cons cell
899 (CODE . MATCHING-CHAR) that can be used as value of a `syntax-table'
900 text property. */)
901 (string)
902 Lisp_Object string;
904 register const unsigned char *p;
905 register enum syntaxcode code;
906 int val;
907 Lisp_Object match;
909 CHECK_STRING (string);
911 p = SDATA (string);
912 code = (enum syntaxcode) syntax_spec_code[*p++];
913 if (((int) code & 0377) == 0377)
914 error ("invalid syntax description letter: %c", p[-1]);
916 if (code == Sinherit)
917 return Qnil;
919 if (*p)
921 int len;
922 int character = (STRING_CHAR_AND_LENGTH
923 (p, SBYTES (string) - 1, len));
924 XSETINT (match, character);
925 if (XFASTINT (match) == ' ')
926 match = Qnil;
927 p += len;
929 else
930 match = Qnil;
932 val = (int) code;
933 while (*p)
934 switch (*p++)
936 case '1':
937 val |= 1 << 16;
938 break;
940 case '2':
941 val |= 1 << 17;
942 break;
944 case '3':
945 val |= 1 << 18;
946 break;
948 case '4':
949 val |= 1 << 19;
950 break;
952 case 'p':
953 val |= 1 << 20;
954 break;
956 case 'b':
957 val |= 1 << 21;
958 break;
960 case 'n':
961 val |= 1 << 22;
962 break;
965 if (val < XVECTOR (Vsyntax_code_object)->size && NILP (match))
966 return XVECTOR (Vsyntax_code_object)->contents[val];
967 else
968 /* Since we can't use a shared object, let's make a new one. */
969 return Fcons (make_number (val), match);
972 /* I really don't know why this is interactive
973 help-form should at least be made useful whilst reading the second arg. */
974 DEFUN ("modify-syntax-entry", Fmodify_syntax_entry, Smodify_syntax_entry, 2, 3,
975 "cSet syntax for character: \nsSet syntax for %s to: ",
976 doc: /* Set syntax for character CHAR according to string NEWENTRY.
977 The syntax is changed only for table SYNTAX_TABLE, which defaults to
978 the current buffer's syntax table.
979 The first character of NEWENTRY should be one of the following:
980 Space or - whitespace syntax. w word constituent.
981 _ symbol constituent. . punctuation.
982 ( open-parenthesis. ) close-parenthesis.
983 " string quote. \\ escape.
984 $ paired delimiter. ' expression quote or prefix operator.
985 < comment starter. > comment ender.
986 / character-quote. @ inherit from `standard-syntax-table'.
987 | generic string fence. ! generic comment fence.
989 Only single-character comment start and end sequences are represented thus.
990 Two-character sequences are represented as described below.
991 The second character of NEWENTRY is the matching parenthesis,
992 used only if the first character is `(' or `)'.
993 Any additional characters are flags.
994 Defined flags are the characters 1, 2, 3, 4, b, p, and n.
995 1 means CHAR is the start of a two-char comment start sequence.
996 2 means CHAR is the second character of such a sequence.
997 3 means CHAR is the start of a two-char comment end sequence.
998 4 means CHAR is the second character of such a sequence.
1000 There can be up to two orthogonal comment sequences. This is to support
1001 language modes such as C++. By default, all comment sequences are of style
1002 a, but you can set the comment sequence style to b (on the second character
1003 of a comment-start, or the first character of a comment-end sequence) using
1004 this flag:
1005 b means CHAR is part of comment sequence b.
1006 n means CHAR is part of a nestable comment sequence.
1008 p means CHAR is a prefix character for `backward-prefix-chars';
1009 such characters are treated as whitespace when they occur
1010 between expressions.
1011 usage: (modify-syntax-entry CHAR NEWENTRY &optional SYNTAX-TABLE) */)
1012 (c, newentry, syntax_table)
1013 Lisp_Object c, newentry, syntax_table;
1015 CHECK_NUMBER (c);
1017 if (NILP (syntax_table))
1018 syntax_table = current_buffer->syntax_table;
1019 else
1020 check_syntax_table (syntax_table);
1022 SET_RAW_SYNTAX_ENTRY (syntax_table, XINT (c), Fstring_to_syntax (newentry));
1023 return Qnil;
1026 /* Dump syntax table to buffer in human-readable format */
1028 DEFUN ("internal-describe-syntax-value", Finternal_describe_syntax_value,
1029 Sinternal_describe_syntax_value, 1, 1, 0,
1030 doc: /* Insert a description of the internal syntax description SYNTAX at point. */)
1031 (syntax)
1032 Lisp_Object syntax;
1034 register enum syntaxcode code;
1035 char desc, start1, start2, end1, end2, prefix, comstyle, comnested;
1036 char str[2];
1037 Lisp_Object first, match_lisp, value = syntax;
1039 if (NILP (value))
1041 insert_string ("default");
1042 return syntax;
1045 if (CHAR_TABLE_P (value))
1047 insert_string ("deeper char-table ...");
1048 return syntax;
1051 if (!CONSP (value))
1053 insert_string ("invalid");
1054 return syntax;
1057 first = XCAR (value);
1058 match_lisp = XCDR (value);
1060 if (!INTEGERP (first) || !(NILP (match_lisp) || INTEGERP (match_lisp)))
1062 insert_string ("invalid");
1063 return syntax;
1066 code = (enum syntaxcode) (XINT (first) & 0377);
1067 start1 = (XINT (first) >> 16) & 1;
1068 start2 = (XINT (first) >> 17) & 1;
1069 end1 = (XINT (first) >> 18) & 1;
1070 end2 = (XINT (first) >> 19) & 1;
1071 prefix = (XINT (first) >> 20) & 1;
1072 comstyle = (XINT (first) >> 21) & 1;
1073 comnested = (XINT (first) >> 22) & 1;
1075 if ((int) code < 0 || (int) code >= (int) Smax)
1077 insert_string ("invalid");
1078 return syntax;
1080 desc = syntax_code_spec[(int) code];
1082 str[0] = desc, str[1] = 0;
1083 insert (str, 1);
1085 if (NILP (match_lisp))
1086 insert (" ", 1);
1087 else
1088 insert_char (XINT (match_lisp));
1090 if (start1)
1091 insert ("1", 1);
1092 if (start2)
1093 insert ("2", 1);
1095 if (end1)
1096 insert ("3", 1);
1097 if (end2)
1098 insert ("4", 1);
1100 if (prefix)
1101 insert ("p", 1);
1102 if (comstyle)
1103 insert ("b", 1);
1104 if (comnested)
1105 insert ("n", 1);
1107 insert_string ("\twhich means: ");
1109 switch (SWITCH_ENUM_CAST (code))
1111 case Swhitespace:
1112 insert_string ("whitespace"); break;
1113 case Spunct:
1114 insert_string ("punctuation"); break;
1115 case Sword:
1116 insert_string ("word"); break;
1117 case Ssymbol:
1118 insert_string ("symbol"); break;
1119 case Sopen:
1120 insert_string ("open"); break;
1121 case Sclose:
1122 insert_string ("close"); break;
1123 case Squote:
1124 insert_string ("prefix"); break;
1125 case Sstring:
1126 insert_string ("string"); break;
1127 case Smath:
1128 insert_string ("math"); break;
1129 case Sescape:
1130 insert_string ("escape"); break;
1131 case Scharquote:
1132 insert_string ("charquote"); break;
1133 case Scomment:
1134 insert_string ("comment"); break;
1135 case Sendcomment:
1136 insert_string ("endcomment"); break;
1137 case Sinherit:
1138 insert_string ("inherit"); break;
1139 case Scomment_fence:
1140 insert_string ("comment fence"); break;
1141 case Sstring_fence:
1142 insert_string ("string fence"); break;
1143 default:
1144 insert_string ("invalid");
1145 return syntax;
1148 if (!NILP (match_lisp))
1150 insert_string (", matches ");
1151 insert_char (XINT (match_lisp));
1154 if (start1)
1155 insert_string (",\n\t is the first character of a comment-start sequence");
1156 if (start2)
1157 insert_string (",\n\t is the second character of a comment-start sequence");
1159 if (end1)
1160 insert_string (",\n\t is the first character of a comment-end sequence");
1161 if (end2)
1162 insert_string (",\n\t is the second character of a comment-end sequence");
1163 if (comstyle)
1164 insert_string (" (comment style b)");
1165 if (comnested)
1166 insert_string (" (nestable)");
1168 if (prefix)
1169 insert_string (",\n\t is a prefix character for `backward-prefix-chars'");
1171 return syntax;
1174 int parse_sexp_ignore_comments;
1176 /* Return the position across COUNT words from FROM.
1177 If that many words cannot be found before the end of the buffer, return 0.
1178 COUNT negative means scan backward and stop at word beginning. */
1181 scan_words (from, count)
1182 register int from, count;
1184 register int beg = BEGV;
1185 register int end = ZV;
1186 register int from_byte = CHAR_TO_BYTE (from);
1187 register enum syntaxcode code;
1188 int ch0, ch1;
1190 immediate_quit = 1;
1191 QUIT;
1193 SETUP_SYNTAX_TABLE (from, count);
1195 while (count > 0)
1197 while (1)
1199 if (from == end)
1201 immediate_quit = 0;
1202 return 0;
1204 UPDATE_SYNTAX_TABLE_FORWARD (from);
1205 ch0 = FETCH_CHAR (from_byte);
1206 code = SYNTAX (ch0);
1207 INC_BOTH (from, from_byte);
1208 if (words_include_escapes
1209 && (code == Sescape || code == Scharquote))
1210 break;
1211 if (code == Sword)
1212 break;
1214 /* Now CH0 is a character which begins a word and FROM is the
1215 position of the next character. */
1216 while (1)
1218 if (from == end) break;
1219 UPDATE_SYNTAX_TABLE_FORWARD (from);
1220 ch1 = FETCH_CHAR (from_byte);
1221 code = SYNTAX (ch1);
1222 if (!(words_include_escapes
1223 && (code == Sescape || code == Scharquote)))
1224 if (code != Sword || WORD_BOUNDARY_P (ch0, ch1))
1225 break;
1226 INC_BOTH (from, from_byte);
1227 ch0 = ch1;
1229 count--;
1231 while (count < 0)
1233 while (1)
1235 if (from == beg)
1237 immediate_quit = 0;
1238 return 0;
1240 DEC_BOTH (from, from_byte);
1241 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1242 ch1 = FETCH_CHAR (from_byte);
1243 code = SYNTAX (ch1);
1244 if (words_include_escapes
1245 && (code == Sescape || code == Scharquote))
1246 break;
1247 if (code == Sword)
1248 break;
1250 /* Now CH1 is a character which ends a word and FROM is the
1251 position of it. */
1252 while (1)
1254 int temp_byte;
1256 if (from == beg)
1257 break;
1258 temp_byte = dec_bytepos (from_byte);
1259 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1260 ch0 = FETCH_CHAR (temp_byte);
1261 code = SYNTAX (ch0);
1262 if (!(words_include_escapes
1263 && (code == Sescape || code == Scharquote)))
1264 if (code != Sword || WORD_BOUNDARY_P (ch0, ch1))
1265 break;
1266 DEC_BOTH (from, from_byte);
1267 ch1 = ch0;
1269 count++;
1272 immediate_quit = 0;
1274 return from;
1277 DEFUN ("forward-word", Fforward_word, Sforward_word, 1, 1, "p",
1278 doc: /* Move point forward ARG words (backward if ARG is negative).
1279 Normally returns t.
1280 If an edge of the buffer or a field boundary is reached, point is left there
1281 and the function returns nil. Field boundaries are not noticed if
1282 `inhibit-field-text-motion' is non-nil. */)
1283 (count)
1284 Lisp_Object count;
1286 int orig_val, val;
1287 CHECK_NUMBER (count);
1289 val = orig_val = scan_words (PT, XINT (count));
1290 if (! orig_val)
1291 val = XINT (count) > 0 ? ZV : BEGV;
1293 /* Avoid jumping out of an input field. */
1294 val = XFASTINT (Fconstrain_to_field (make_number (val), make_number (PT),
1295 Qt, Qnil, Qnil));
1297 SET_PT (val);
1298 return val == orig_val ? Qt : Qnil;
1301 Lisp_Object skip_chars ();
1303 DEFUN ("skip-chars-forward", Fskip_chars_forward, Sskip_chars_forward, 1, 2, 0,
1304 doc: /* Move point forward, stopping before a char not in STRING, or at pos LIM.
1305 STRING is like the inside of a `[...]' in a regular expression
1306 except that `]' is never special and `\\' quotes `^', `-' or `\\'
1307 (but not as the end of a range; quoting is never needed there).
1308 Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
1309 With arg "^a-zA-Z", skips nonletters stopping before first letter.
1310 Returns the distance traveled, either zero or positive.
1311 Note that char classes, e.g. `[:alpha:]', are not currently supported;
1312 they will be treated as literals. */)
1313 (string, lim)
1314 Lisp_Object string, lim;
1316 return skip_chars (1, 0, string, lim);
1319 DEFUN ("skip-chars-backward", Fskip_chars_backward, Sskip_chars_backward, 1, 2, 0,
1320 doc: /* Move point backward, stopping after a char not in STRING, or at pos LIM.
1321 See `skip-chars-forward' for details.
1322 Returns the distance traveled, either zero or negative. */)
1323 (string, lim)
1324 Lisp_Object string, lim;
1326 return skip_chars (0, 0, string, lim);
1329 DEFUN ("skip-syntax-forward", Fskip_syntax_forward, Sskip_syntax_forward, 1, 2, 0,
1330 doc: /* Move point forward across chars in specified syntax classes.
1331 SYNTAX is a string of syntax code characters.
1332 Stop before a char whose syntax is not in SYNTAX, or at position LIM.
1333 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1334 This function returns the distance traveled, either zero or positive. */)
1335 (syntax, lim)
1336 Lisp_Object syntax, lim;
1338 return skip_chars (1, 1, syntax, lim);
1341 DEFUN ("skip-syntax-backward", Fskip_syntax_backward, Sskip_syntax_backward, 1, 2, 0,
1342 doc: /* Move point backward across chars in specified syntax classes.
1343 SYNTAX is a string of syntax code characters.
1344 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.
1345 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1346 This function returns the distance traveled, either zero or negative. */)
1347 (syntax, lim)
1348 Lisp_Object syntax, lim;
1350 return skip_chars (0, 1, syntax, lim);
1353 static Lisp_Object
1354 skip_chars (forwardp, syntaxp, string, lim)
1355 int forwardp, syntaxp;
1356 Lisp_Object string, lim;
1358 register unsigned int c;
1359 unsigned char fastmap[0400];
1360 /* If SYNTAXP is 0, STRING may contain multi-byte form of characters
1361 of which codes don't fit in FASTMAP. In that case, set the
1362 ranges of characters in CHAR_RANGES. */
1363 int *char_ranges;
1364 int n_char_ranges = 0;
1365 int negate = 0;
1366 register int i, i_byte;
1367 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
1368 int string_multibyte;
1369 int size_byte;
1370 const unsigned char *str;
1371 int len;
1373 CHECK_STRING (string);
1374 char_ranges = (int *) alloca (SCHARS (string) * (sizeof (int)) * 2);
1375 string_multibyte = STRING_MULTIBYTE (string);
1376 str = SDATA (string);
1377 size_byte = SBYTES (string);
1379 /* Adjust the multibyteness of the string to that of the buffer. */
1380 if (multibyte != string_multibyte)
1382 int nbytes;
1384 if (multibyte)
1385 nbytes = count_size_as_multibyte (SDATA (string),
1386 SCHARS (string));
1387 else
1388 nbytes = SCHARS (string);
1389 if (nbytes != size_byte)
1391 unsigned char *tmp = (unsigned char *) alloca (nbytes);
1392 copy_text (SDATA (string), tmp, size_byte,
1393 string_multibyte, multibyte);
1394 size_byte = nbytes;
1395 str = tmp;
1399 if (NILP (lim))
1400 XSETINT (lim, forwardp ? ZV : BEGV);
1401 else
1402 CHECK_NUMBER_COERCE_MARKER (lim);
1404 /* In any case, don't allow scan outside bounds of buffer. */
1405 if (XINT (lim) > ZV)
1406 XSETFASTINT (lim, ZV);
1407 if (XINT (lim) < BEGV)
1408 XSETFASTINT (lim, BEGV);
1410 bzero (fastmap, sizeof fastmap);
1412 i_byte = 0;
1414 if (i_byte < size_byte
1415 && SREF (string, 0) == '^')
1417 negate = 1; i_byte++;
1420 /* Find the characters specified and set their elements of fastmap.
1421 If syntaxp, each character counts as itself.
1422 Otherwise, handle backslashes and ranges specially. */
1424 while (i_byte < size_byte)
1426 c = STRING_CHAR_AND_LENGTH (str + i_byte, size_byte - i_byte, len);
1427 i_byte += len;
1429 if (syntaxp)
1430 fastmap[syntax_spec_code[c & 0377]] = 1;
1431 else
1433 if (c == '\\')
1435 if (i_byte == size_byte)
1436 break;
1438 c = STRING_CHAR_AND_LENGTH (str+i_byte, size_byte-i_byte, len);
1439 i_byte += len;
1441 if (i_byte < size_byte
1442 && str[i_byte] == '-')
1444 unsigned int c2;
1446 /* Skip over the dash. */
1447 i_byte++;
1449 if (i_byte == size_byte)
1450 break;
1452 /* Get the end of the range. */
1453 c2 =STRING_CHAR_AND_LENGTH (str+i_byte, size_byte-i_byte, len);
1454 i_byte += len;
1456 if (SINGLE_BYTE_CHAR_P (c))
1458 if (! SINGLE_BYTE_CHAR_P (c2))
1460 /* Handle a range starting with a character of
1461 less than 256, and ending with a character of
1462 not less than 256. Split that into two
1463 ranges, the low one ending at 0377, and the
1464 high one starting at the smallest character
1465 in the charset of C2 and ending at C2. */
1466 int charset = CHAR_CHARSET (c2);
1467 int c1 = MAKE_CHAR (charset, 0, 0);
1469 char_ranges[n_char_ranges++] = c1;
1470 char_ranges[n_char_ranges++] = c2;
1471 c2 = 0377;
1473 while (c <= c2)
1475 fastmap[c] = 1;
1476 c++;
1479 else if (c <= c2) /* Both C and C2 are multibyte char. */
1481 char_ranges[n_char_ranges++] = c;
1482 char_ranges[n_char_ranges++] = c2;
1485 else
1487 if (SINGLE_BYTE_CHAR_P (c))
1488 fastmap[c] = 1;
1489 else
1491 char_ranges[n_char_ranges++] = c;
1492 char_ranges[n_char_ranges++] = c;
1498 /* If ^ was the first character, complement the fastmap. */
1499 if (negate)
1500 for (i = 0; i < sizeof fastmap; i++)
1501 fastmap[i] ^= 1;
1504 int start_point = PT;
1505 int pos = PT;
1506 int pos_byte = PT_BYTE;
1507 unsigned char *p = PT_ADDR, *endp, *stop;
1509 if (forwardp)
1511 endp = (XINT (lim) == GPT) ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
1512 stop = (pos < GPT && GPT < XINT (lim)) ? GPT_ADDR : endp;
1514 else
1516 endp = CHAR_POS_ADDR (XINT (lim));
1517 stop = (pos >= GPT && GPT > XINT (lim)) ? GAP_END_ADDR : endp;
1520 immediate_quit = 1;
1521 if (syntaxp)
1523 SETUP_SYNTAX_TABLE (pos, forwardp ? 1 : -1);
1524 if (forwardp)
1526 if (multibyte)
1527 while (1)
1529 int nbytes;
1531 if (p >= stop)
1533 if (p >= endp)
1534 break;
1535 p = GAP_END_ADDR;
1536 stop = endp;
1538 c = STRING_CHAR_AND_LENGTH (p, MAX_MULTIBYTE_LENGTH, nbytes);
1539 if (! fastmap[(int) SYNTAX (c)])
1540 break;
1541 p += nbytes, pos++, pos_byte += nbytes;
1542 UPDATE_SYNTAX_TABLE_FORWARD (pos);
1544 else
1545 while (1)
1547 if (p >= stop)
1549 if (p >= endp)
1550 break;
1551 p = GAP_END_ADDR;
1552 stop = endp;
1554 if (! fastmap[(int) SYNTAX (*p)])
1555 break;
1556 p++, pos++;
1557 UPDATE_SYNTAX_TABLE_FORWARD (pos);
1560 else
1562 if (multibyte)
1563 while (1)
1565 unsigned char *prev_p;
1566 int nbytes;
1568 if (p <= stop)
1570 if (p <= endp)
1571 break;
1572 p = GPT_ADDR;
1573 stop = endp;
1575 prev_p = p;
1576 while (--p >= stop && ! CHAR_HEAD_P (*p));
1577 PARSE_MULTIBYTE_SEQ (p, MAX_MULTIBYTE_LENGTH, nbytes);
1578 if (prev_p - p > nbytes)
1579 p = prev_p - 1, c = *p, nbytes = 1;
1580 else
1581 c = STRING_CHAR (p, MAX_MULTIBYTE_LENGTH);
1582 pos--, pos_byte -= nbytes;
1583 UPDATE_SYNTAX_TABLE_BACKWARD (pos);
1584 if (! fastmap[(int) SYNTAX (c)])
1586 pos++;
1587 pos_byte += nbytes;
1588 break;
1591 else
1592 while (1)
1594 if (p <= stop)
1596 if (p <= endp)
1597 break;
1598 p = GPT_ADDR;
1599 stop = endp;
1601 if (! fastmap[(int) SYNTAX (p[-1])])
1602 break;
1603 p--, pos--;
1604 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
1608 else
1610 if (forwardp)
1612 if (multibyte)
1613 while (1)
1615 int nbytes;
1617 if (p >= stop)
1619 if (p >= endp)
1620 break;
1621 p = GAP_END_ADDR;
1622 stop = endp;
1624 c = STRING_CHAR_AND_LENGTH (p, MAX_MULTIBYTE_LENGTH, nbytes);
1625 if (SINGLE_BYTE_CHAR_P (c))
1627 if (!fastmap[c])
1628 break;
1630 else
1632 /* If we are looking at a multibyte character,
1633 we must look up the character in the table
1634 CHAR_RANGES. If there's no data in the
1635 table, that character is not what we want to
1636 skip. */
1638 /* The following code do the right thing even if
1639 n_char_ranges is zero (i.e. no data in
1640 CHAR_RANGES). */
1641 for (i = 0; i < n_char_ranges; i += 2)
1642 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
1643 break;
1644 if (!(negate ^ (i < n_char_ranges)))
1645 break;
1647 p += nbytes, pos++, pos_byte += nbytes;
1649 else
1650 while (1)
1652 if (p >= stop)
1654 if (p >= endp)
1655 break;
1656 p = GAP_END_ADDR;
1657 stop = endp;
1659 if (!fastmap[*p])
1660 break;
1661 p++, pos++;
1664 else
1666 if (multibyte)
1667 while (1)
1669 unsigned char *prev_p;
1670 int nbytes;
1672 if (p <= stop)
1674 if (p <= endp)
1675 break;
1676 p = GPT_ADDR;
1677 stop = endp;
1679 prev_p = p;
1680 while (--p >= stop && ! CHAR_HEAD_P (*p));
1681 PARSE_MULTIBYTE_SEQ (p, MAX_MULTIBYTE_LENGTH, nbytes);
1682 if (prev_p - p > nbytes)
1683 p = prev_p - 1, c = *p, nbytes = 1;
1684 else
1685 c = STRING_CHAR (p, MAX_MULTIBYTE_LENGTH);
1686 if (SINGLE_BYTE_CHAR_P (c))
1688 if (!fastmap[c])
1689 break;
1691 else
1693 /* See the comment in the previous similar code. */
1694 for (i = 0; i < n_char_ranges; i += 2)
1695 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
1696 break;
1697 if (!(negate ^ (i < n_char_ranges)))
1698 break;
1700 pos--, pos_byte -= nbytes;
1702 else
1703 while (1)
1705 if (p <= stop)
1707 if (p <= endp)
1708 break;
1709 p = GPT_ADDR;
1710 stop = endp;
1712 if (!fastmap[p[-1]])
1713 break;
1714 p--, pos--;
1719 #if 0 /* Not needed now that a position in mid-character
1720 cannot be specified in Lisp. */
1721 if (multibyte
1722 /* INC_POS or DEC_POS might have moved POS over LIM. */
1723 && (forwardp ? (pos > XINT (lim)) : (pos < XINT (lim))))
1724 pos = XINT (lim);
1725 #endif
1727 if (! multibyte)
1728 pos_byte = pos;
1730 SET_PT_BOTH (pos, pos_byte);
1731 immediate_quit = 0;
1733 return make_number (PT - start_point);
1737 /* Jump over a comment, assuming we are at the beginning of one.
1738 FROM is the current position.
1739 FROM_BYTE is the bytepos corresponding to FROM.
1740 Do not move past STOP (a charpos).
1741 The comment over which we have to jump is of style STYLE
1742 (either SYNTAX_COMMENT_STYLE(foo) or ST_COMMENT_STYLE).
1743 NESTING should be positive to indicate the nesting at the beginning
1744 for nested comments and should be zero or negative else.
1745 ST_COMMENT_STYLE cannot be nested.
1746 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
1747 (or 0 If the search cannot start in the middle of a two-character).
1749 If successful, return 1 and store the charpos of the comment's end
1750 into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR.
1751 Else, return 0 and store the charpos STOP into *CHARPOS_PTR, the
1752 corresponding bytepos into *BYTEPOS_PTR and the current nesting
1753 (as defined for state.incomment) in *INCOMMENT_PTR.
1755 The comment end is the last character of the comment rather than the
1756 character just after the comment.
1758 Global syntax data is assumed to initially be valid for FROM and
1759 remains valid for forward search starting at the returned position. */
1761 static int
1762 forw_comment (from, from_byte, stop, nesting, style, prev_syntax,
1763 charpos_ptr, bytepos_ptr, incomment_ptr)
1764 int from, from_byte, stop;
1765 int nesting, style, prev_syntax;
1766 int *charpos_ptr, *bytepos_ptr, *incomment_ptr;
1768 register int c, c1;
1769 register enum syntaxcode code;
1770 register int syntax;
1772 if (nesting <= 0) nesting = -1;
1774 /* Enter the loop in the middle so that we find
1775 a 2-char comment ender if we start in the middle of it. */
1776 syntax = prev_syntax;
1777 if (syntax != 0) goto forw_incomment;
1779 while (1)
1781 if (from == stop)
1783 *incomment_ptr = nesting;
1784 *charpos_ptr = from;
1785 *bytepos_ptr = from_byte;
1786 return 0;
1788 c = FETCH_CHAR (from_byte);
1789 syntax = SYNTAX_WITH_FLAGS (c);
1790 code = syntax & 0xff;
1791 if (code == Sendcomment
1792 && SYNTAX_FLAGS_COMMENT_STYLE (syntax) == style
1793 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ?
1794 (nesting > 0 && --nesting == 0) : nesting < 0))
1795 /* we have encountered a comment end of the same style
1796 as the comment sequence which began this comment
1797 section */
1798 break;
1799 if (code == Scomment_fence
1800 && style == ST_COMMENT_STYLE)
1801 /* we have encountered a comment end of the same style
1802 as the comment sequence which began this comment
1803 section. */
1804 break;
1805 if (nesting > 0
1806 && code == Scomment
1807 && SYNTAX_FLAGS_COMMENT_NESTED (syntax)
1808 && SYNTAX_FLAGS_COMMENT_STYLE (syntax) == style)
1809 /* we have encountered a nested comment of the same style
1810 as the comment sequence which began this comment section */
1811 nesting++;
1812 INC_BOTH (from, from_byte);
1813 UPDATE_SYNTAX_TABLE_FORWARD (from);
1815 forw_incomment:
1816 if (from < stop && SYNTAX_FLAGS_COMEND_FIRST (syntax)
1817 && SYNTAX_FLAGS_COMMENT_STYLE (syntax) == style
1818 && (c1 = FETCH_CHAR (from_byte),
1819 SYNTAX_COMEND_SECOND (c1))
1820 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
1821 SYNTAX_COMMENT_NESTED (c1)) ? nesting > 0 : nesting < 0))
1823 if (--nesting <= 0)
1824 /* we have encountered a comment end of the same style
1825 as the comment sequence which began this comment
1826 section */
1827 break;
1828 else
1830 INC_BOTH (from, from_byte);
1831 UPDATE_SYNTAX_TABLE_FORWARD (from);
1834 if (nesting > 0
1835 && from < stop
1836 && SYNTAX_FLAGS_COMSTART_FIRST (syntax)
1837 && (c1 = FETCH_CHAR (from_byte),
1838 SYNTAX_COMMENT_STYLE (c1) == style
1839 && SYNTAX_COMSTART_SECOND (c1))
1840 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
1841 SYNTAX_COMMENT_NESTED (c1)))
1842 /* we have encountered a nested comment of the same style
1843 as the comment sequence which began this comment
1844 section */
1846 INC_BOTH (from, from_byte);
1847 UPDATE_SYNTAX_TABLE_FORWARD (from);
1848 nesting++;
1851 *charpos_ptr = from;
1852 *bytepos_ptr = from_byte;
1853 return 1;
1856 DEFUN ("forward-comment", Fforward_comment, Sforward_comment, 1, 1, 0,
1857 doc: /*
1858 Move forward across up to COUNT comments. If COUNT is negative, move backward.
1859 Stop scanning if we find something other than a comment or whitespace.
1860 Set point to where scanning stops.
1861 If COUNT comments are found as expected, with nothing except whitespace
1862 between them, return t; otherwise return nil. */)
1863 (count)
1864 Lisp_Object count;
1866 register int from;
1867 int from_byte;
1868 register int stop;
1869 register int c, c1;
1870 register enum syntaxcode code;
1871 int comstyle = 0; /* style of comment encountered */
1872 int comnested = 0; /* whether the comment is nestable or not */
1873 int found;
1874 int count1;
1875 int out_charpos, out_bytepos;
1876 int dummy;
1878 CHECK_NUMBER (count);
1879 count1 = XINT (count);
1880 stop = count1 > 0 ? ZV : BEGV;
1882 immediate_quit = 1;
1883 QUIT;
1885 from = PT;
1886 from_byte = PT_BYTE;
1888 SETUP_SYNTAX_TABLE (from, count1);
1889 while (count1 > 0)
1893 int comstart_first;
1895 if (from == stop)
1897 SET_PT_BOTH (from, from_byte);
1898 immediate_quit = 0;
1899 return Qnil;
1901 c = FETCH_CHAR (from_byte);
1902 code = SYNTAX (c);
1903 comstart_first = SYNTAX_COMSTART_FIRST (c);
1904 comnested = SYNTAX_COMMENT_NESTED (c);
1905 comstyle = SYNTAX_COMMENT_STYLE (c);
1906 INC_BOTH (from, from_byte);
1907 UPDATE_SYNTAX_TABLE_FORWARD (from);
1908 if (from < stop && comstart_first
1909 && (c1 = FETCH_CHAR (from_byte),
1910 SYNTAX_COMSTART_SECOND (c1)))
1912 /* We have encountered a comment start sequence and we
1913 are ignoring all text inside comments. We must record
1914 the comment style this sequence begins so that later,
1915 only a comment end of the same style actually ends
1916 the comment section. */
1917 code = Scomment;
1918 comstyle = SYNTAX_COMMENT_STYLE (c1);
1919 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
1920 INC_BOTH (from, from_byte);
1921 UPDATE_SYNTAX_TABLE_FORWARD (from);
1924 while (code == Swhitespace || (code == Sendcomment && c == '\n'));
1926 if (code == Scomment_fence)
1927 comstyle = ST_COMMENT_STYLE;
1928 else if (code != Scomment)
1930 immediate_quit = 0;
1931 DEC_BOTH (from, from_byte);
1932 SET_PT_BOTH (from, from_byte);
1933 return Qnil;
1935 /* We're at the start of a comment. */
1936 found = forw_comment (from, from_byte, stop, comnested, comstyle, 0,
1937 &out_charpos, &out_bytepos, &dummy);
1938 from = out_charpos; from_byte = out_bytepos;
1939 if (!found)
1941 immediate_quit = 0;
1942 SET_PT_BOTH (from, from_byte);
1943 return Qnil;
1945 INC_BOTH (from, from_byte);
1946 UPDATE_SYNTAX_TABLE_FORWARD (from);
1947 /* We have skipped one comment. */
1948 count1--;
1951 while (count1 < 0)
1953 while (1)
1955 int quoted;
1957 if (from <= stop)
1959 SET_PT_BOTH (BEGV, BEGV_BYTE);
1960 immediate_quit = 0;
1961 return Qnil;
1964 DEC_BOTH (from, from_byte);
1965 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
1966 quoted = char_quoted (from, from_byte);
1967 c = FETCH_CHAR (from_byte);
1968 code = SYNTAX (c);
1969 comstyle = 0;
1970 comnested = SYNTAX_COMMENT_NESTED (c);
1971 if (code == Sendcomment)
1972 comstyle = SYNTAX_COMMENT_STYLE (c);
1973 if (from > stop && SYNTAX_COMEND_SECOND (c)
1974 && prev_char_comend_first (from, from_byte)
1975 && !char_quoted (from - 1, dec_bytepos (from_byte)))
1977 /* We must record the comment style encountered so that
1978 later, we can match only the proper comment begin
1979 sequence of the same style. */
1980 DEC_BOTH (from, from_byte);
1981 code = Sendcomment;
1982 /* Calling char_quoted, above, set up global syntax position
1983 at the new value of FROM. */
1984 c1 = FETCH_CHAR (from_byte);
1985 comstyle = SYNTAX_COMMENT_STYLE (c1);
1986 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
1989 if (code == Scomment_fence)
1991 /* Skip until first preceding unquoted comment_fence. */
1992 int found = 0, ini = from, ini_byte = from_byte;
1994 while (1)
1996 DEC_BOTH (from, from_byte);
1997 if (from == stop)
1998 break;
1999 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2000 c = FETCH_CHAR (from_byte);
2001 if (SYNTAX (c) == Scomment_fence
2002 && !char_quoted (from, from_byte))
2004 found = 1;
2005 break;
2008 if (found == 0)
2010 from = ini; /* Set point to ini + 1. */
2011 from_byte = ini_byte;
2012 goto leave;
2015 else if (code == Sendcomment)
2017 found = back_comment (from, from_byte, stop, comnested, comstyle,
2018 &out_charpos, &out_bytepos);
2019 if (found == -1)
2021 if (c == '\n')
2022 /* This end-of-line is not an end-of-comment.
2023 Treat it like a whitespace.
2024 CC-mode (and maybe others) relies on this behavior. */
2026 else
2028 /* Failure: we should go back to the end of this
2029 not-quite-endcomment. */
2030 if (SYNTAX(c) != code)
2031 /* It was a two-char Sendcomment. */
2032 INC_BOTH (from, from_byte);
2033 goto leave;
2036 else
2038 /* We have skipped one comment. */
2039 from = out_charpos, from_byte = out_bytepos;
2040 break;
2043 else if (code != Swhitespace || quoted)
2045 leave:
2046 immediate_quit = 0;
2047 INC_BOTH (from, from_byte);
2048 SET_PT_BOTH (from, from_byte);
2049 return Qnil;
2053 count1++;
2056 SET_PT_BOTH (from, from_byte);
2057 immediate_quit = 0;
2058 return Qt;
2061 /* Return syntax code of character C if C is a single byte character
2062 or `multibyte_symbol_p' is zero. Otherwise, return Ssymbol. */
2064 #define SYNTAX_WITH_MULTIBYTE_CHECK(c) \
2065 ((SINGLE_BYTE_CHAR_P (c) || !multibyte_symbol_p) \
2066 ? SYNTAX (c) : Ssymbol)
2068 static Lisp_Object
2069 scan_lists (from, count, depth, sexpflag)
2070 register int from;
2071 int count, depth, sexpflag;
2073 Lisp_Object val;
2074 register int stop = count > 0 ? ZV : BEGV;
2075 register int c, c1;
2076 int stringterm;
2077 int quoted;
2078 int mathexit = 0;
2079 register enum syntaxcode code, temp_code;
2080 int min_depth = depth; /* Err out if depth gets less than this. */
2081 int comstyle = 0; /* style of comment encountered */
2082 int comnested = 0; /* whether the comment is nestable or not */
2083 int temp_pos;
2084 int last_good = from;
2085 int found;
2086 int from_byte;
2087 int out_bytepos, out_charpos;
2088 int temp, dummy;
2089 int multibyte_symbol_p = sexpflag && multibyte_syntax_as_symbol;
2091 if (depth > 0) min_depth = 0;
2093 if (from > ZV) from = ZV;
2094 if (from < BEGV) from = BEGV;
2096 from_byte = CHAR_TO_BYTE (from);
2098 immediate_quit = 1;
2099 QUIT;
2101 SETUP_SYNTAX_TABLE (from, count);
2102 while (count > 0)
2104 while (from < stop)
2106 int comstart_first, prefix;
2107 UPDATE_SYNTAX_TABLE_FORWARD (from);
2108 c = FETCH_CHAR (from_byte);
2109 code = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2110 comstart_first = SYNTAX_COMSTART_FIRST (c);
2111 comnested = SYNTAX_COMMENT_NESTED (c);
2112 comstyle = SYNTAX_COMMENT_STYLE (c);
2113 prefix = SYNTAX_PREFIX (c);
2114 if (depth == min_depth)
2115 last_good = from;
2116 INC_BOTH (from, from_byte);
2117 UPDATE_SYNTAX_TABLE_FORWARD (from);
2118 if (from < stop && comstart_first
2119 && SYNTAX_COMSTART_SECOND (FETCH_CHAR (from_byte))
2120 && parse_sexp_ignore_comments)
2122 /* we have encountered a comment start sequence and we
2123 are ignoring all text inside comments. We must record
2124 the comment style this sequence begins so that later,
2125 only a comment end of the same style actually ends
2126 the comment section */
2127 code = Scomment;
2128 c1 = FETCH_CHAR (from_byte);
2129 comstyle = SYNTAX_COMMENT_STYLE (c1);
2130 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
2131 INC_BOTH (from, from_byte);
2132 UPDATE_SYNTAX_TABLE_FORWARD (from);
2135 if (prefix)
2136 continue;
2138 switch (SWITCH_ENUM_CAST (code))
2140 case Sescape:
2141 case Scharquote:
2142 if (from == stop) goto lose;
2143 INC_BOTH (from, from_byte);
2144 /* treat following character as a word constituent */
2145 case Sword:
2146 case Ssymbol:
2147 if (depth || !sexpflag) break;
2148 /* This word counts as a sexp; return at end of it. */
2149 while (from < stop)
2151 UPDATE_SYNTAX_TABLE_FORWARD (from);
2153 /* Some compilers can't handle this inside the switch. */
2154 c = FETCH_CHAR (from_byte);
2155 temp = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2156 switch (temp)
2158 case Scharquote:
2159 case Sescape:
2160 INC_BOTH (from, from_byte);
2161 if (from == stop) goto lose;
2162 break;
2163 case Sword:
2164 case Ssymbol:
2165 case Squote:
2166 break;
2167 default:
2168 goto done;
2170 INC_BOTH (from, from_byte);
2172 goto done;
2174 case Scomment_fence:
2175 comstyle = ST_COMMENT_STYLE;
2176 /* FALLTHROUGH */
2177 case Scomment:
2178 if (!parse_sexp_ignore_comments) break;
2179 UPDATE_SYNTAX_TABLE_FORWARD (from);
2180 found = forw_comment (from, from_byte, stop,
2181 comnested, comstyle, 0,
2182 &out_charpos, &out_bytepos, &dummy);
2183 from = out_charpos, from_byte = out_bytepos;
2184 if (!found)
2186 if (depth == 0)
2187 goto done;
2188 goto lose;
2190 INC_BOTH (from, from_byte);
2191 UPDATE_SYNTAX_TABLE_FORWARD (from);
2192 break;
2194 case Smath:
2195 if (!sexpflag)
2196 break;
2197 if (from != stop && c == FETCH_CHAR (from_byte))
2199 INC_BOTH (from, from_byte);
2201 if (mathexit)
2203 mathexit = 0;
2204 goto close1;
2206 mathexit = 1;
2208 case Sopen:
2209 if (!++depth) goto done;
2210 break;
2212 case Sclose:
2213 close1:
2214 if (!--depth) goto done;
2215 if (depth < min_depth)
2216 Fsignal (Qscan_error,
2217 Fcons (build_string ("Containing expression ends prematurely"),
2218 Fcons (make_number (last_good),
2219 Fcons (make_number (from), Qnil))));
2220 break;
2222 case Sstring:
2223 case Sstring_fence:
2224 temp_pos = dec_bytepos (from_byte);
2225 stringterm = FETCH_CHAR (temp_pos);
2226 while (1)
2228 if (from >= stop) goto lose;
2229 UPDATE_SYNTAX_TABLE_FORWARD (from);
2230 c = FETCH_CHAR (from_byte);
2231 if (code == Sstring
2232 ? (c == stringterm
2233 && SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring)
2234 : SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring_fence)
2235 break;
2237 /* Some compilers can't handle this inside the switch. */
2238 temp = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2239 switch (temp)
2241 case Scharquote:
2242 case Sescape:
2243 INC_BOTH (from, from_byte);
2245 INC_BOTH (from, from_byte);
2247 INC_BOTH (from, from_byte);
2248 if (!depth && sexpflag) goto done;
2249 break;
2250 default:
2251 /* Ignore whitespace, punctuation, quote, endcomment. */
2252 break;
2256 /* Reached end of buffer. Error if within object, return nil if between */
2257 if (depth) goto lose;
2259 immediate_quit = 0;
2260 return Qnil;
2262 /* End of object reached */
2263 done:
2264 count--;
2267 while (count < 0)
2269 while (from > stop)
2271 DEC_BOTH (from, from_byte);
2272 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2273 c = FETCH_CHAR (from_byte);
2274 code = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2275 if (depth == min_depth)
2276 last_good = from;
2277 comstyle = 0;
2278 comnested = SYNTAX_COMMENT_NESTED (c);
2279 if (code == Sendcomment)
2280 comstyle = SYNTAX_COMMENT_STYLE (c);
2281 if (from > stop && SYNTAX_COMEND_SECOND (c)
2282 && prev_char_comend_first (from, from_byte)
2283 && parse_sexp_ignore_comments)
2285 /* We must record the comment style encountered so that
2286 later, we can match only the proper comment begin
2287 sequence of the same style. */
2288 DEC_BOTH (from, from_byte);
2289 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2290 code = Sendcomment;
2291 c1 = FETCH_CHAR (from_byte);
2292 comstyle = SYNTAX_COMMENT_STYLE (c1);
2293 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
2296 /* Quoting turns anything except a comment-ender
2297 into a word character. Note that this cannot be true
2298 if we decremented FROM in the if-statement above. */
2299 if (code != Sendcomment && char_quoted (from, from_byte))
2301 DEC_BOTH (from, from_byte);
2302 code = Sword;
2304 else if (SYNTAX_PREFIX (c))
2305 continue;
2307 switch (SWITCH_ENUM_CAST (code))
2309 case Sword:
2310 case Ssymbol:
2311 case Sescape:
2312 case Scharquote:
2313 if (depth || !sexpflag) break;
2314 /* This word counts as a sexp; count object finished
2315 after passing it. */
2316 while (from > stop)
2318 temp_pos = from_byte;
2319 if (! NILP (current_buffer->enable_multibyte_characters))
2320 DEC_POS (temp_pos);
2321 else
2322 temp_pos--;
2323 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2324 c1 = FETCH_CHAR (temp_pos);
2325 temp_code = SYNTAX_WITH_MULTIBYTE_CHECK (c1);
2326 /* Don't allow comment-end to be quoted. */
2327 if (temp_code == Sendcomment)
2328 goto done2;
2329 quoted = char_quoted (from - 1, temp_pos);
2330 if (quoted)
2332 DEC_BOTH (from, from_byte);
2333 temp_pos = dec_bytepos (temp_pos);
2334 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2336 c1 = FETCH_CHAR (temp_pos);
2337 temp_code = SYNTAX_WITH_MULTIBYTE_CHECK (c1);
2338 if (! (quoted || temp_code == Sword
2339 || temp_code == Ssymbol
2340 || temp_code == Squote))
2341 goto done2;
2342 DEC_BOTH (from, from_byte);
2344 goto done2;
2346 case Smath:
2347 if (!sexpflag)
2348 break;
2349 temp_pos = dec_bytepos (from_byte);
2350 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2351 if (from != stop && c == FETCH_CHAR (temp_pos))
2352 DEC_BOTH (from, from_byte);
2353 if (mathexit)
2355 mathexit = 0;
2356 goto open2;
2358 mathexit = 1;
2360 case Sclose:
2361 if (!++depth) goto done2;
2362 break;
2364 case Sopen:
2365 open2:
2366 if (!--depth) goto done2;
2367 if (depth < min_depth)
2368 Fsignal (Qscan_error,
2369 Fcons (build_string ("Containing expression ends prematurely"),
2370 Fcons (make_number (last_good),
2371 Fcons (make_number (from), Qnil))));
2372 break;
2374 case Sendcomment:
2375 if (!parse_sexp_ignore_comments)
2376 break;
2377 found = back_comment (from, from_byte, stop, comnested, comstyle,
2378 &out_charpos, &out_bytepos);
2379 /* FIXME: if found == -1, then it really wasn't a comment-end.
2380 For single-char Sendcomment, we can't do much about it apart
2381 from skipping the char.
2382 For 2-char endcomments, we could try again, taking both
2383 chars as separate entities, but it's a lot of trouble
2384 for very little gain, so we don't bother either. -sm */
2385 if (found != -1)
2386 from = out_charpos, from_byte = out_bytepos;
2387 break;
2389 case Scomment_fence:
2390 case Sstring_fence:
2391 while (1)
2393 DEC_BOTH (from, from_byte);
2394 if (from == stop) goto lose;
2395 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2396 if (!char_quoted (from, from_byte)
2397 && (c = FETCH_CHAR (from_byte),
2398 SYNTAX_WITH_MULTIBYTE_CHECK (c) == code))
2399 break;
2401 if (code == Sstring_fence && !depth && sexpflag) goto done2;
2402 break;
2404 case Sstring:
2405 stringterm = FETCH_CHAR (from_byte);
2406 while (1)
2408 if (from == stop) goto lose;
2409 temp_pos = from_byte;
2410 if (! NILP (current_buffer->enable_multibyte_characters))
2411 DEC_POS (temp_pos);
2412 else
2413 temp_pos--;
2414 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2415 if (!char_quoted (from - 1, temp_pos)
2416 && stringterm == (c = FETCH_CHAR (temp_pos))
2417 && SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring)
2418 break;
2419 DEC_BOTH (from, from_byte);
2421 DEC_BOTH (from, from_byte);
2422 if (!depth && sexpflag) goto done2;
2423 break;
2424 default:
2425 /* Ignore whitespace, punctuation, quote, endcomment. */
2426 break;
2430 /* Reached start of buffer. Error if within object, return nil if between */
2431 if (depth) goto lose;
2433 immediate_quit = 0;
2434 return Qnil;
2436 done2:
2437 count++;
2441 immediate_quit = 0;
2442 XSETFASTINT (val, from);
2443 return val;
2445 lose:
2446 Fsignal (Qscan_error,
2447 Fcons (build_string ("Unbalanced parentheses"),
2448 Fcons (make_number (last_good),
2449 Fcons (make_number (from), Qnil))));
2451 /* NOTREACHED */
2454 DEFUN ("scan-lists", Fscan_lists, Sscan_lists, 3, 3, 0,
2455 doc: /* Scan from character number FROM by COUNT lists.
2456 Returns the character number of the position thus found.
2458 If DEPTH is nonzero, paren depth begins counting from that value,
2459 only places where the depth in parentheses becomes zero
2460 are candidates for stopping; COUNT such places are counted.
2461 Thus, a positive value for DEPTH means go out levels.
2463 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2465 If the beginning or end of (the accessible part of) the buffer is reached
2466 and the depth is wrong, an error is signaled.
2467 If the depth is right but the count is not used up, nil is returned. */)
2468 (from, count, depth)
2469 Lisp_Object from, count, depth;
2471 CHECK_NUMBER (from);
2472 CHECK_NUMBER (count);
2473 CHECK_NUMBER (depth);
2475 return scan_lists (XINT (from), XINT (count), XINT (depth), 0);
2478 DEFUN ("scan-sexps", Fscan_sexps, Sscan_sexps, 2, 2, 0,
2479 doc: /* Scan from character number FROM by COUNT balanced expressions.
2480 If COUNT is negative, scan backwards.
2481 Returns the character number of the position thus found.
2483 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2485 If the beginning or end of (the accessible part of) the buffer is reached
2486 in the middle of a parenthetical grouping, an error is signaled.
2487 If the beginning or end is reached between groupings
2488 but before count is used up, nil is returned. */)
2489 (from, count)
2490 Lisp_Object from, count;
2492 CHECK_NUMBER (from);
2493 CHECK_NUMBER (count);
2495 return scan_lists (XINT (from), XINT (count), 0, 1);
2498 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars, Sbackward_prefix_chars,
2499 0, 0, 0,
2500 doc: /* Move point backward over any number of chars with prefix syntax.
2501 This includes chars with "quote" or "prefix" syntax (' or p). */)
2504 int beg = BEGV;
2505 int opoint = PT;
2506 int opoint_byte = PT_BYTE;
2507 int pos = PT;
2508 int pos_byte = PT_BYTE;
2509 int c;
2511 if (pos <= beg)
2513 SET_PT_BOTH (opoint, opoint_byte);
2515 return Qnil;
2518 SETUP_SYNTAX_TABLE (pos, -1);
2520 DEC_BOTH (pos, pos_byte);
2522 while (!char_quoted (pos, pos_byte)
2523 /* Previous statement updates syntax table. */
2524 && ((c = FETCH_CHAR (pos_byte), SYNTAX (c) == Squote)
2525 || SYNTAX_PREFIX (c)))
2527 opoint = pos;
2528 opoint_byte = pos_byte;
2530 if (pos + 1 > beg)
2531 DEC_BOTH (pos, pos_byte);
2534 SET_PT_BOTH (opoint, opoint_byte);
2536 return Qnil;
2539 /* Parse forward from FROM / FROM_BYTE to END,
2540 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
2541 and return a description of the state of the parse at END.
2542 If STOPBEFORE is nonzero, stop at the start of an atom.
2543 If COMMENTSTOP is 1, stop at the start of a comment.
2544 If COMMENTSTOP is -1, stop at the start or end of a comment,
2545 after the beginning of a string, or after the end of a string. */
2547 static void
2548 scan_sexps_forward (stateptr, from, from_byte, end, targetdepth,
2549 stopbefore, oldstate, commentstop)
2550 struct lisp_parse_state *stateptr;
2551 register int from;
2552 int end, targetdepth, stopbefore, from_byte;
2553 Lisp_Object oldstate;
2554 int commentstop;
2556 struct lisp_parse_state state;
2558 register enum syntaxcode code;
2559 int c1;
2560 int comnested;
2561 struct level { int last, prev; };
2562 struct level levelstart[100];
2563 register struct level *curlevel = levelstart;
2564 struct level *endlevel = levelstart + 100;
2565 register int depth; /* Paren depth of current scanning location.
2566 level - levelstart equals this except
2567 when the depth becomes negative. */
2568 int mindepth; /* Lowest DEPTH value seen. */
2569 int start_quoted = 0; /* Nonzero means starting after a char quote */
2570 Lisp_Object tem;
2571 int prev_from; /* Keep one character before FROM. */
2572 int prev_from_byte;
2573 int prev_from_syntax;
2574 int boundary_stop = commentstop == -1;
2575 int nofence;
2576 int found;
2577 int out_bytepos, out_charpos;
2578 int temp;
2580 prev_from = from;
2581 prev_from_byte = from_byte;
2582 if (from != BEGV)
2583 DEC_BOTH (prev_from, prev_from_byte);
2585 /* Use this macro instead of `from++'. */
2586 #define INC_FROM \
2587 do { prev_from = from; \
2588 prev_from_byte = from_byte; \
2589 prev_from_syntax \
2590 = SYNTAX_WITH_FLAGS (FETCH_CHAR (prev_from_byte)); \
2591 INC_BOTH (from, from_byte); \
2592 if (from < end) \
2593 UPDATE_SYNTAX_TABLE_FORWARD (from); \
2594 } while (0)
2596 immediate_quit = 1;
2597 QUIT;
2599 if (NILP (oldstate))
2601 depth = 0;
2602 state.instring = -1;
2603 state.incomment = 0;
2604 state.comstyle = 0; /* comment style a by default. */
2605 state.comstr_start = -1; /* no comment/string seen. */
2607 else
2609 tem = Fcar (oldstate);
2610 if (!NILP (tem))
2611 depth = XINT (tem);
2612 else
2613 depth = 0;
2615 oldstate = Fcdr (oldstate);
2616 oldstate = Fcdr (oldstate);
2617 oldstate = Fcdr (oldstate);
2618 tem = Fcar (oldstate);
2619 /* Check whether we are inside string_fence-style string: */
2620 state.instring = (!NILP (tem)
2621 ? (INTEGERP (tem) ? XINT (tem) : ST_STRING_STYLE)
2622 : -1);
2624 oldstate = Fcdr (oldstate);
2625 tem = Fcar (oldstate);
2626 state.incomment = (!NILP (tem)
2627 ? (INTEGERP (tem) ? XINT (tem) : -1)
2628 : 0);
2630 oldstate = Fcdr (oldstate);
2631 tem = Fcar (oldstate);
2632 start_quoted = !NILP (tem);
2634 /* if the eighth element of the list is nil, we are in comment
2635 style a. If it is non-nil, we are in comment style b */
2636 oldstate = Fcdr (oldstate);
2637 oldstate = Fcdr (oldstate);
2638 tem = Fcar (oldstate);
2639 state.comstyle = NILP (tem) ? 0 : (EQ (tem, Qsyntax_table)
2640 ? ST_COMMENT_STYLE : 1);
2642 oldstate = Fcdr (oldstate);
2643 tem = Fcar (oldstate);
2644 state.comstr_start = NILP (tem) ? -1 : XINT (tem) ;
2645 oldstate = Fcdr (oldstate);
2646 tem = Fcar (oldstate);
2647 while (!NILP (tem)) /* >= second enclosing sexps. */
2649 /* curlevel++->last ran into compiler bug on Apollo */
2650 curlevel->last = XINT (Fcar (tem));
2651 if (++curlevel == endlevel)
2652 curlevel--; /* error ("Nesting too deep for parser"); */
2653 curlevel->prev = -1;
2654 curlevel->last = -1;
2655 tem = Fcdr (tem);
2658 state.quoted = 0;
2659 mindepth = depth;
2661 curlevel->prev = -1;
2662 curlevel->last = -1;
2664 SETUP_SYNTAX_TABLE (prev_from, 1);
2665 prev_from_syntax = SYNTAX_WITH_FLAGS (FETCH_CHAR (prev_from_byte));
2666 UPDATE_SYNTAX_TABLE_FORWARD (from);
2668 /* Enter the loop at a place appropriate for initial state. */
2670 if (state.incomment)
2671 goto startincomment;
2672 if (state.instring >= 0)
2674 nofence = state.instring != ST_STRING_STYLE;
2675 if (start_quoted)
2676 goto startquotedinstring;
2677 goto startinstring;
2679 else if (start_quoted)
2680 goto startquoted;
2682 while (from < end)
2684 INC_FROM;
2685 code = prev_from_syntax & 0xff;
2687 if (code == Scomment)
2689 state.comstyle = SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax);
2690 state.incomment = (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax) ?
2691 1 : -1);
2692 state.comstr_start = prev_from;
2694 else if (code == Scomment_fence)
2696 /* Record the comment style we have entered so that only
2697 the comment-end sequence of the same style actually
2698 terminates the comment section. */
2699 state.comstyle = ST_COMMENT_STYLE;
2700 state.incomment = -1;
2701 state.comstr_start = prev_from;
2702 code = Scomment;
2704 else if (from < end)
2705 if (SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax))
2706 if (c1 = FETCH_CHAR (from_byte),
2707 SYNTAX_COMSTART_SECOND (c1))
2708 /* Duplicate code to avoid a complex if-expression
2709 which causes trouble for the SGI compiler. */
2711 /* Record the comment style we have entered so that only
2712 the comment-end sequence of the same style actually
2713 terminates the comment section. */
2714 state.comstyle = SYNTAX_COMMENT_STYLE (c1);
2715 comnested = SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax);
2716 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
2717 state.incomment = comnested ? 1 : -1;
2718 state.comstr_start = prev_from;
2719 INC_FROM;
2720 code = Scomment;
2723 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax))
2724 continue;
2725 switch (SWITCH_ENUM_CAST (code))
2727 case Sescape:
2728 case Scharquote:
2729 if (stopbefore) goto stop; /* this arg means stop at sexp start */
2730 curlevel->last = prev_from;
2731 startquoted:
2732 if (from == end) goto endquoted;
2733 INC_FROM;
2734 goto symstarted;
2735 /* treat following character as a word constituent */
2736 case Sword:
2737 case Ssymbol:
2738 if (stopbefore) goto stop; /* this arg means stop at sexp start */
2739 curlevel->last = prev_from;
2740 symstarted:
2741 while (from < end)
2743 /* Some compilers can't handle this inside the switch. */
2744 temp = SYNTAX (FETCH_CHAR (from_byte));
2745 switch (temp)
2747 case Scharquote:
2748 case Sescape:
2749 INC_FROM;
2750 if (from == end) goto endquoted;
2751 break;
2752 case Sword:
2753 case Ssymbol:
2754 case Squote:
2755 break;
2756 default:
2757 goto symdone;
2759 INC_FROM;
2761 symdone:
2762 curlevel->prev = curlevel->last;
2763 break;
2765 case Scomment_fence: /* Can't happen because it's handled above. */
2766 case Scomment:
2767 if (commentstop || boundary_stop) goto done;
2768 startincomment:
2769 /* The (from == BEGV) test was to enter the loop in the middle so
2770 that we find a 2-char comment ender even if we start in the
2771 middle of it. We don't want to do that if we're just at the
2772 beginning of the comment (think of (*) ... (*)). */
2773 found = forw_comment (from, from_byte, end,
2774 state.incomment, state.comstyle,
2775 (from == BEGV || from < state.comstr_start + 3)
2776 ? 0 : prev_from_syntax,
2777 &out_charpos, &out_bytepos, &state.incomment);
2778 from = out_charpos; from_byte = out_bytepos;
2779 /* Beware! prev_from and friends are invalid now.
2780 Luckily, the `done' doesn't use them and the INC_FROM
2781 sets them to a sane value without looking at them. */
2782 if (!found) goto done;
2783 INC_FROM;
2784 state.incomment = 0;
2785 state.comstyle = 0; /* reset the comment style */
2786 if (boundary_stop) goto done;
2787 break;
2789 case Sopen:
2790 if (stopbefore) goto stop; /* this arg means stop at sexp start */
2791 depth++;
2792 /* curlevel++->last ran into compiler bug on Apollo */
2793 curlevel->last = prev_from;
2794 if (++curlevel == endlevel)
2795 curlevel--; /* error ("Nesting too deep for parser"); */
2796 curlevel->prev = -1;
2797 curlevel->last = -1;
2798 if (targetdepth == depth) goto done;
2799 break;
2801 case Sclose:
2802 depth--;
2803 if (depth < mindepth)
2804 mindepth = depth;
2805 if (curlevel != levelstart)
2806 curlevel--;
2807 curlevel->prev = curlevel->last;
2808 if (targetdepth == depth) goto done;
2809 break;
2811 case Sstring:
2812 case Sstring_fence:
2813 state.comstr_start = from - 1;
2814 if (stopbefore) goto stop; /* this arg means stop at sexp start */
2815 curlevel->last = prev_from;
2816 state.instring = (code == Sstring
2817 ? (FETCH_CHAR (prev_from_byte))
2818 : ST_STRING_STYLE);
2819 if (boundary_stop) goto done;
2820 startinstring:
2822 nofence = state.instring != ST_STRING_STYLE;
2824 while (1)
2826 int c;
2828 if (from >= end) goto done;
2829 c = FETCH_CHAR (from_byte);
2830 /* Some compilers can't handle this inside the switch. */
2831 temp = SYNTAX (c);
2833 /* Check TEMP here so that if the char has
2834 a syntax-table property which says it is NOT
2835 a string character, it does not end the string. */
2836 if (nofence && c == state.instring && temp == Sstring)
2837 break;
2839 switch (temp)
2841 case Sstring_fence:
2842 if (!nofence) goto string_end;
2843 break;
2844 case Scharquote:
2845 case Sescape:
2846 INC_FROM;
2847 startquotedinstring:
2848 if (from >= end) goto endquoted;
2850 INC_FROM;
2853 string_end:
2854 state.instring = -1;
2855 curlevel->prev = curlevel->last;
2856 INC_FROM;
2857 if (boundary_stop) goto done;
2858 break;
2860 case Smath:
2861 /* FIXME: We should do something with it. */
2862 break;
2863 default:
2864 /* Ignore whitespace, punctuation, quote, endcomment. */
2865 break;
2868 goto done;
2870 stop: /* Here if stopping before start of sexp. */
2871 from = prev_from; /* We have just fetched the char that starts it; */
2872 goto done; /* but return the position before it. */
2874 endquoted:
2875 state.quoted = 1;
2876 done:
2877 state.depth = depth;
2878 state.mindepth = mindepth;
2879 state.thislevelstart = curlevel->prev;
2880 state.prevlevelstart
2881 = (curlevel == levelstart) ? -1 : (curlevel - 1)->last;
2882 state.location = from;
2883 state.levelstarts = Qnil;
2884 while (--curlevel >= levelstart)
2885 state.levelstarts = Fcons (make_number (curlevel->last),
2886 state.levelstarts);
2887 immediate_quit = 0;
2889 *stateptr = state;
2892 DEFUN ("parse-partial-sexp", Fparse_partial_sexp, Sparse_partial_sexp, 2, 6, 0,
2893 doc: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
2894 Parsing stops at TO or when certain criteria are met;
2895 point is set to where parsing stops.
2896 If fifth arg OLDSTATE is omitted or nil,
2897 parsing assumes that FROM is the beginning of a function.
2898 Value is a list of ten elements describing final state of parsing:
2899 0. depth in parens.
2900 1. character address of start of innermost containing list; nil if none.
2901 2. character address of start of last complete sexp terminated.
2902 3. non-nil if inside a string.
2903 (it is the character that will terminate the string,
2904 or t if the string should be terminated by a generic string delimiter.)
2905 4. nil if outside a comment, t if inside a non-nestable comment,
2906 else an integer (the current comment nesting).
2907 5. t if following a quote character.
2908 6. the minimum paren-depth encountered during this scan.
2909 7. t if in a comment of style b; symbol `syntax-table' if the comment
2910 should be terminated by a generic comment delimiter.
2911 8. character address of start of comment or string; nil if not in one.
2912 9. Intermediate data for continuation of parsing (subject to change).
2913 If third arg TARGETDEPTH is non-nil, parsing stops if the depth
2914 in parentheses becomes equal to TARGETDEPTH.
2915 Fourth arg STOPBEFORE non-nil means stop when come to
2916 any character that starts a sexp.
2917 Fifth arg OLDSTATE is a nine-element list like what this function returns.
2918 It is used to initialize the state of the parse. Elements number 1, 2, 6
2919 and 8 are ignored; you can leave off element 8 (the last) entirely.
2920 Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.
2921 If it is symbol `syntax-table', stop after the start of a comment or a
2922 string, or after end of a comment or a string. */)
2923 (from, to, targetdepth, stopbefore, oldstate, commentstop)
2924 Lisp_Object from, to, targetdepth, stopbefore, oldstate, commentstop;
2926 struct lisp_parse_state state;
2927 int target;
2929 if (!NILP (targetdepth))
2931 CHECK_NUMBER (targetdepth);
2932 target = XINT (targetdepth);
2934 else
2935 target = -100000; /* We won't reach this depth */
2937 validate_region (&from, &to);
2938 scan_sexps_forward (&state, XINT (from), CHAR_TO_BYTE (XINT (from)),
2939 XINT (to),
2940 target, !NILP (stopbefore), oldstate,
2941 (NILP (commentstop)
2942 ? 0 : (EQ (commentstop, Qsyntax_table) ? -1 : 1)));
2944 SET_PT (state.location);
2946 return Fcons (make_number (state.depth),
2947 Fcons (state.prevlevelstart < 0 ? Qnil : make_number (state.prevlevelstart),
2948 Fcons (state.thislevelstart < 0 ? Qnil : make_number (state.thislevelstart),
2949 Fcons (state.instring >= 0
2950 ? (state.instring == ST_STRING_STYLE
2951 ? Qt : make_number (state.instring)) : Qnil,
2952 Fcons (state.incomment < 0 ? Qt :
2953 (state.incomment == 0 ? Qnil :
2954 make_number (state.incomment)),
2955 Fcons (state.quoted ? Qt : Qnil,
2956 Fcons (make_number (state.mindepth),
2957 Fcons ((state.comstyle
2958 ? (state.comstyle == ST_COMMENT_STYLE
2959 ? Qsyntax_table : Qt) :
2960 Qnil),
2961 Fcons (((state.incomment
2962 || (state.instring >= 0))
2963 ? make_number (state.comstr_start)
2964 : Qnil),
2965 Fcons (state.levelstarts, Qnil))))))))));
2968 void
2969 init_syntax_once ()
2971 register int i, c;
2972 Lisp_Object temp;
2974 /* This has to be done here, before we call Fmake_char_table. */
2975 Qsyntax_table = intern ("syntax-table");
2976 staticpro (&Qsyntax_table);
2978 /* Intern this now in case it isn't already done.
2979 Setting this variable twice is harmless.
2980 But don't staticpro it here--that is done in alloc.c. */
2981 Qchar_table_extra_slots = intern ("char-table-extra-slots");
2983 /* Create objects which can be shared among syntax tables. */
2984 Vsyntax_code_object = Fmake_vector (make_number (Smax), Qnil);
2985 for (i = 0; i < XVECTOR (Vsyntax_code_object)->size; i++)
2986 XVECTOR (Vsyntax_code_object)->contents[i]
2987 = Fcons (make_number (i), Qnil);
2989 /* Now we are ready to set up this property, so we can
2990 create syntax tables. */
2991 Fput (Qsyntax_table, Qchar_table_extra_slots, make_number (0));
2993 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Swhitespace];
2995 Vstandard_syntax_table = Fmake_char_table (Qsyntax_table, temp);
2997 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Sword];
2998 for (i = 'a'; i <= 'z'; i++)
2999 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3000 for (i = 'A'; i <= 'Z'; i++)
3001 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3002 for (i = '0'; i <= '9'; i++)
3003 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3005 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '$', temp);
3006 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '%', temp);
3008 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '(',
3009 Fcons (make_number (Sopen), make_number (')')));
3010 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ')',
3011 Fcons (make_number (Sclose), make_number ('(')));
3012 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '[',
3013 Fcons (make_number (Sopen), make_number (']')));
3014 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ']',
3015 Fcons (make_number (Sclose), make_number ('[')));
3016 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '{',
3017 Fcons (make_number (Sopen), make_number ('}')));
3018 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '}',
3019 Fcons (make_number (Sclose), make_number ('{')));
3020 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '"',
3021 Fcons (make_number ((int) Sstring), Qnil));
3022 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\\',
3023 Fcons (make_number ((int) Sescape), Qnil));
3025 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Ssymbol];
3026 for (i = 0; i < 10; i++)
3028 c = "_-+*/&|<>="[i];
3029 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3032 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Spunct];
3033 for (i = 0; i < 12; i++)
3035 c = ".,;:?!#@~^'`"[i];
3036 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3039 /* All multibyte characters have syntax `word' by default. */
3040 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Sword];
3041 for (i = CHAR_TABLE_SINGLE_BYTE_SLOTS; i < CHAR_TABLE_ORDINARY_SLOTS; i++)
3042 XCHAR_TABLE (Vstandard_syntax_table)->contents[i] = temp;
3045 void
3046 syms_of_syntax ()
3048 Qsyntax_table_p = intern ("syntax-table-p");
3049 staticpro (&Qsyntax_table_p);
3051 staticpro (&Vsyntax_code_object);
3053 Qscan_error = intern ("scan-error");
3054 staticpro (&Qscan_error);
3055 Fput (Qscan_error, Qerror_conditions,
3056 Fcons (Qscan_error, Fcons (Qerror, Qnil)));
3057 Fput (Qscan_error, Qerror_message,
3058 build_string ("Scan error"));
3060 DEFVAR_BOOL ("parse-sexp-ignore-comments", &parse_sexp_ignore_comments,
3061 doc: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
3063 DEFVAR_BOOL ("parse-sexp-lookup-properties", &parse_sexp_lookup_properties,
3064 doc: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3065 Otherwise, that text property is simply ignored.
3066 See the info node `(elisp)Syntax Properties' for a description of the
3067 `syntax-table' property. */);
3069 words_include_escapes = 0;
3070 DEFVAR_BOOL ("words-include-escapes", &words_include_escapes,
3071 doc: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
3073 DEFVAR_BOOL ("multibyte-syntax-as-symbol", &multibyte_syntax_as_symbol,
3074 doc: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
3075 multibyte_syntax_as_symbol = 0;
3077 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3078 &open_paren_in_column_0_is_defun_start,
3079 doc: /* *Non-nil means an open paren in column 0 denotes the start of a defun. */);
3080 open_paren_in_column_0_is_defun_start = 1;
3082 defsubr (&Ssyntax_table_p);
3083 defsubr (&Ssyntax_table);
3084 defsubr (&Sstandard_syntax_table);
3085 defsubr (&Scopy_syntax_table);
3086 defsubr (&Sset_syntax_table);
3087 defsubr (&Schar_syntax);
3088 defsubr (&Smatching_paren);
3089 defsubr (&Sstring_to_syntax);
3090 defsubr (&Smodify_syntax_entry);
3091 defsubr (&Sinternal_describe_syntax_value);
3093 defsubr (&Sforward_word);
3095 defsubr (&Sskip_chars_forward);
3096 defsubr (&Sskip_chars_backward);
3097 defsubr (&Sskip_syntax_forward);
3098 defsubr (&Sskip_syntax_backward);
3100 defsubr (&Sforward_comment);
3101 defsubr (&Sscan_lists);
3102 defsubr (&Sscan_sexps);
3103 defsubr (&Sbackward_prefix_chars);
3104 defsubr (&Sparse_partial_sexp);