(mouse-buffer-menu-mode-groups): Added "Version Control" group.
[emacs.git] / src / syntax.c
blobc2a7c733734275640828a3b48c1a43d817912746
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 an non-matching comment-ender, meaning that any matching
472 comment-starter we might see later could be a false positive (hidden
473 inside another comment).
474 Test case: { a (* b } c (* d *) */
475 int comment_lossage = 0;
476 int comment_end = from;
477 int comment_end_byte = from_byte;
478 int comstart_pos = 0;
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 && (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, 0);
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, 0);
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 unsigned char *p;
905 register enum syntaxcode code;
906 int val;
907 Lisp_Object match;
909 CHECK_STRING (string, 0);
911 p = XSTRING (string)->data;
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, STRING_BYTES (XSTRING (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
975 DEFUN ("modify-syntax-entry", Fmodify_syntax_entry, Smodify_syntax_entry, 2, 3,
976 "cSet syntax for character: \nsSet syntax for %s to: ",
977 doc: /* Set syntax for character CHAR according to string NEWENTRY.
978 The syntax is changed only for table SYNTAX_TABLE, which defaults to
979 the current buffer's syntax table.
980 The first character of NEWENTRY should be one of the following:
981 Space or - whitespace syntax. w word constituent.
982 _ symbol constituent. . punctuation.
983 ( open-parenthesis. ) close-parenthesis.
984 " string quote. \\ escape.
985 $ paired delimiter. ' expression quote or prefix operator.
986 < comment starter. > comment ender.
987 / character-quote. @ inherit from `standard-syntax-table'.
988 | generic string fence. ! generic comment fence.
990 Only single-character comment start and end sequences are represented thus.
991 Two-character sequences are represented as described below.
992 The second character of NEWENTRY is the matching parenthesis,
993 used only if the first character is `(' or `)'.
994 Any additional characters are flags.
995 Defined flags are the characters 1, 2, 3, 4, b, p, and n.
996 1 means CHAR is the start of a two-char comment start sequence.
997 2 means CHAR is the second character of such a sequence.
998 3 means CHAR is the start of a two-char comment end sequence.
999 4 means CHAR is the second character of such a sequence.
1001 There can be up to two orthogonal comment sequences. This is to support
1002 language modes such as C++. By default, all comment sequences are of style
1003 a, but you can set the comment sequence style to b (on the second character
1004 of a comment-start, or the first character of a comment-end sequence) using
1005 this flag:
1006 b means CHAR is part of comment sequence b.
1007 n means CHAR is part of a nestable comment sequence.
1009 p means CHAR is a prefix character for `backward-prefix-chars';
1010 such characters are treated as whitespace when they occur
1011 between expressions.
1012 usage: (modify-syntax-entry CHAR NEWENTRY &optional SYNTAX-TABLE) */)
1013 (c, newentry, syntax_table)
1014 Lisp_Object c, newentry, syntax_table;
1016 CHECK_NUMBER (c, 0);
1018 if (NILP (syntax_table))
1019 syntax_table = current_buffer->syntax_table;
1020 else
1021 check_syntax_table (syntax_table);
1023 SET_RAW_SYNTAX_ENTRY (syntax_table, XINT (c), Fstring_to_syntax (newentry));
1024 return Qnil;
1027 /* Dump syntax table to buffer in human-readable format */
1029 static void
1030 describe_syntax (value)
1031 Lisp_Object value;
1033 register enum syntaxcode code;
1034 char desc, start1, start2, end1, end2, prefix, comstyle, comnested;
1035 char str[2];
1036 Lisp_Object first, match_lisp;
1038 Findent_to (make_number (16), make_number (1));
1040 if (NILP (value))
1042 insert_string ("default\n");
1043 return;
1046 if (CHAR_TABLE_P (value))
1048 insert_string ("deeper char-table ...\n");
1049 return;
1052 if (!CONSP (value))
1054 insert_string ("invalid\n");
1055 return;
1058 first = XCAR (value);
1059 match_lisp = XCDR (value);
1061 if (!INTEGERP (first) || !(NILP (match_lisp) || INTEGERP (match_lisp)))
1063 insert_string ("invalid\n");
1064 return;
1067 code = (enum syntaxcode) (XINT (first) & 0377);
1068 start1 = (XINT (first) >> 16) & 1;
1069 start2 = (XINT (first) >> 17) & 1;
1070 end1 = (XINT (first) >> 18) & 1;
1071 end2 = (XINT (first) >> 19) & 1;
1072 prefix = (XINT (first) >> 20) & 1;
1073 comstyle = (XINT (first) >> 21) & 1;
1074 comnested = (XINT (first) >> 22) & 1;
1076 if ((int) code < 0 || (int) code >= (int) Smax)
1078 insert_string ("invalid");
1079 return;
1081 desc = syntax_code_spec[(int) code];
1083 str[0] = desc, str[1] = 0;
1084 insert (str, 1);
1086 if (NILP (match_lisp))
1087 insert (" ", 1);
1088 else
1089 insert_char (XINT (match_lisp));
1091 if (start1)
1092 insert ("1", 1);
1093 if (start2)
1094 insert ("2", 1);
1096 if (end1)
1097 insert ("3", 1);
1098 if (end2)
1099 insert ("4", 1);
1101 if (prefix)
1102 insert ("p", 1);
1103 if (comstyle)
1104 insert ("b", 1);
1105 if (comnested)
1106 insert ("n", 1);
1108 insert_string ("\twhich means: ");
1110 switch (SWITCH_ENUM_CAST (code))
1112 case Swhitespace:
1113 insert_string ("whitespace"); break;
1114 case Spunct:
1115 insert_string ("punctuation"); break;
1116 case Sword:
1117 insert_string ("word"); break;
1118 case Ssymbol:
1119 insert_string ("symbol"); break;
1120 case Sopen:
1121 insert_string ("open"); break;
1122 case Sclose:
1123 insert_string ("close"); break;
1124 case Squote:
1125 insert_string ("prefix"); break;
1126 case Sstring:
1127 insert_string ("string"); break;
1128 case Smath:
1129 insert_string ("math"); break;
1130 case Sescape:
1131 insert_string ("escape"); break;
1132 case Scharquote:
1133 insert_string ("charquote"); break;
1134 case Scomment:
1135 insert_string ("comment"); break;
1136 case Sendcomment:
1137 insert_string ("endcomment"); break;
1138 case Sinherit:
1139 insert_string ("inherit"); break;
1140 case Scomment_fence:
1141 insert_string ("comment fence"); break;
1142 case Sstring_fence:
1143 insert_string ("string fence"); break;
1144 default:
1145 insert_string ("invalid");
1146 return;
1149 if (!NILP (match_lisp))
1151 insert_string (", matches ");
1152 insert_char (XINT (match_lisp));
1155 if (start1)
1156 insert_string (",\n\t is the first character of a comment-start sequence");
1157 if (start2)
1158 insert_string (",\n\t is the second character of a comment-start sequence");
1160 if (end1)
1161 insert_string (",\n\t is the first character of a comment-end sequence");
1162 if (end2)
1163 insert_string (",\n\t is the second character of a comment-end sequence");
1164 if (comstyle)
1165 insert_string (" (comment style b)");
1166 if (comnested)
1167 insert_string (" (nestable)");
1169 if (prefix)
1170 insert_string (",\n\t is a prefix character for `backward-prefix-chars'");
1172 insert_string ("\n");
1175 static Lisp_Object
1176 describe_syntax_1 (vector)
1177 Lisp_Object vector;
1179 struct buffer *old = current_buffer;
1180 set_buffer_internal (XBUFFER (Vstandard_output));
1181 describe_vector (vector, Qnil, describe_syntax, 0, Qnil, Qnil, (int *) 0, 0);
1182 while (! NILP (XCHAR_TABLE (vector)->parent))
1184 vector = XCHAR_TABLE (vector)->parent;
1185 insert_string ("\nThe parent syntax table is:");
1186 describe_vector (vector, Qnil, describe_syntax, 0, Qnil, Qnil,
1187 (int *) 0, 0);
1190 call0 (intern ("help-mode"));
1191 set_buffer_internal (old);
1192 return Qnil;
1195 DEFUN ("describe-syntax", Fdescribe_syntax, Sdescribe_syntax, 0, 0, "",
1196 doc: /* Describe the syntax specifications in the syntax table.
1197 The descriptions are inserted in a buffer, which is then displayed. */)
1200 internal_with_output_to_temp_buffer
1201 ("*Help*", describe_syntax_1, current_buffer->syntax_table);
1203 return Qnil;
1206 int parse_sexp_ignore_comments;
1208 /* Return the position across COUNT words from FROM.
1209 If that many words cannot be found before the end of the buffer, return 0.
1210 COUNT negative means scan backward and stop at word beginning. */
1213 scan_words (from, count)
1214 register int from, count;
1216 register int beg = BEGV;
1217 register int end = ZV;
1218 register int from_byte = CHAR_TO_BYTE (from);
1219 register enum syntaxcode code;
1220 int ch0, ch1;
1222 immediate_quit = 1;
1223 QUIT;
1225 SETUP_SYNTAX_TABLE (from, count);
1227 while (count > 0)
1229 while (1)
1231 if (from == end)
1233 immediate_quit = 0;
1234 return 0;
1236 UPDATE_SYNTAX_TABLE_FORWARD (from);
1237 ch0 = FETCH_CHAR (from_byte);
1238 code = SYNTAX (ch0);
1239 INC_BOTH (from, from_byte);
1240 if (words_include_escapes
1241 && (code == Sescape || code == Scharquote))
1242 break;
1243 if (code == Sword)
1244 break;
1246 /* Now CH0 is a character which begins a word and FROM is the
1247 position of the next character. */
1248 while (1)
1250 if (from == end) break;
1251 UPDATE_SYNTAX_TABLE_FORWARD (from);
1252 ch1 = FETCH_CHAR (from_byte);
1253 code = SYNTAX (ch1);
1254 if (!(words_include_escapes
1255 && (code == Sescape || code == Scharquote)))
1256 if (code != Sword || WORD_BOUNDARY_P (ch0, ch1))
1257 break;
1258 INC_BOTH (from, from_byte);
1259 ch0 = ch1;
1261 count--;
1263 while (count < 0)
1265 while (1)
1267 if (from == beg)
1269 immediate_quit = 0;
1270 return 0;
1272 DEC_BOTH (from, from_byte);
1273 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1274 ch1 = FETCH_CHAR (from_byte);
1275 code = SYNTAX (ch1);
1276 if (words_include_escapes
1277 && (code == Sescape || code == Scharquote))
1278 break;
1279 if (code == Sword)
1280 break;
1282 /* Now CH1 is a character which ends a word and FROM is the
1283 position of it. */
1284 while (1)
1286 int temp_byte;
1288 if (from == beg)
1289 break;
1290 temp_byte = dec_bytepos (from_byte);
1291 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1292 ch0 = FETCH_CHAR (temp_byte);
1293 code = SYNTAX (ch0);
1294 if (!(words_include_escapes
1295 && (code == Sescape || code == Scharquote)))
1296 if (code != Sword || WORD_BOUNDARY_P (ch0, ch1))
1297 break;
1298 DEC_BOTH (from, from_byte);
1299 ch1 = ch0;
1301 count++;
1304 immediate_quit = 0;
1306 return from;
1309 DEFUN ("forward-word", Fforward_word, Sforward_word, 1, 1, "p",
1310 doc: /* Move point forward ARG words (backward if ARG is negative).
1311 Normally returns t.
1312 If an edge of the buffer or a field boundary is reached, point is left there
1313 and the function returns nil. Field boundaries are not noticed if
1314 `inhibit-field-text-motion' is non-nil. */)
1315 (count)
1316 Lisp_Object count;
1318 int orig_val, val;
1319 CHECK_NUMBER (count, 0);
1321 val = orig_val = scan_words (PT, XINT (count));
1322 if (! orig_val)
1323 val = XINT (count) > 0 ? ZV : BEGV;
1325 /* Avoid jumping out of an input field. */
1326 val = XFASTINT (Fconstrain_to_field (make_number (val), make_number (PT),
1327 Qt, Qnil, Qnil));
1329 SET_PT (val);
1330 return val == orig_val ? Qt : Qnil;
1333 Lisp_Object skip_chars ();
1335 DEFUN ("skip-chars-forward", Fskip_chars_forward, Sskip_chars_forward, 1, 2, 0,
1336 doc: /* Move point forward, stopping before a char not in STRING, or at pos LIM.
1337 STRING is like the inside of a `[...]' in a regular expression
1338 except that `]' is never special and `\\' quotes `^', `-' or `\\'
1339 (but not as the end of a range; quoting is never needed there).
1340 Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
1341 With arg "^a-zA-Z", skips nonletters stopping before first letter.
1342 Returns the distance traveled, either zero or positive. */)
1343 (string, lim)
1344 Lisp_Object string, lim;
1346 return skip_chars (1, 0, string, lim);
1349 DEFUN ("skip-chars-backward", Fskip_chars_backward, Sskip_chars_backward, 1, 2, 0,
1350 doc: /* Move point backward, stopping after a char not in STRING, or at pos LIM.
1351 See `skip-chars-forward' for details.
1352 Returns the distance traveled, either zero or negative. */)
1353 (string, lim)
1354 Lisp_Object string, lim;
1356 return skip_chars (0, 0, string, lim);
1359 DEFUN ("skip-syntax-forward", Fskip_syntax_forward, Sskip_syntax_forward, 1, 2, 0,
1360 doc: /* Move point forward across chars in specified syntax classes.
1361 SYNTAX is a string of syntax code characters.
1362 Stop before a char whose syntax is not in SYNTAX, or at position LIM.
1363 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1364 This function returns the distance traveled, either zero or positive. */)
1365 (syntax, lim)
1366 Lisp_Object syntax, lim;
1368 return skip_chars (1, 1, syntax, lim);
1371 DEFUN ("skip-syntax-backward", Fskip_syntax_backward, Sskip_syntax_backward, 1, 2, 0,
1372 doc: /* Move point backward across chars in specified syntax classes.
1373 SYNTAX is a string of syntax code characters.
1374 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.
1375 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1376 This function returns the distance traveled, either zero or negative. */)
1377 (syntax, lim)
1378 Lisp_Object syntax, lim;
1380 return skip_chars (0, 1, syntax, lim);
1383 static Lisp_Object
1384 skip_chars (forwardp, syntaxp, string, lim)
1385 int forwardp, syntaxp;
1386 Lisp_Object string, lim;
1388 register unsigned int c;
1389 unsigned char fastmap[0400];
1390 /* If SYNTAXP is 0, STRING may contain multi-byte form of characters
1391 of which codes don't fit in FASTMAP. In that case, set the
1392 ranges of characters in CHAR_RANGES. */
1393 int *char_ranges;
1394 int n_char_ranges = 0;
1395 int negate = 0;
1396 register int i, i_byte;
1397 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
1398 int string_multibyte;
1399 int size_byte;
1400 unsigned char *str;
1401 int len;
1403 CHECK_STRING (string, 0);
1404 char_ranges = (int *) alloca (XSTRING (string)->size * (sizeof (int)) * 2);
1405 string_multibyte = STRING_MULTIBYTE (string);
1406 str = XSTRING (string)->data;
1407 size_byte = STRING_BYTES (XSTRING (string));
1409 /* Adjust the multibyteness of the string to that of the buffer. */
1410 if (multibyte != string_multibyte)
1412 int nbytes;
1414 if (multibyte)
1415 nbytes = count_size_as_multibyte (XSTRING (string)->data,
1416 XSTRING (string)->size);
1417 else
1418 nbytes = XSTRING (string)->size;
1419 if (nbytes != size_byte)
1421 str = (unsigned char *) alloca (nbytes);
1422 copy_text (XSTRING (string)->data, str, size_byte,
1423 string_multibyte, multibyte);
1424 size_byte = nbytes;
1428 if (NILP (lim))
1429 XSETINT (lim, forwardp ? ZV : BEGV);
1430 else
1431 CHECK_NUMBER_COERCE_MARKER (lim, 0);
1433 /* In any case, don't allow scan outside bounds of buffer. */
1434 if (XINT (lim) > ZV)
1435 XSETFASTINT (lim, ZV);
1436 if (XINT (lim) < BEGV)
1437 XSETFASTINT (lim, BEGV);
1439 bzero (fastmap, sizeof fastmap);
1441 i_byte = 0;
1443 if (i_byte < size_byte
1444 && XSTRING (string)->data[0] == '^')
1446 negate = 1; i_byte++;
1449 /* Find the characters specified and set their elements of fastmap.
1450 If syntaxp, each character counts as itself.
1451 Otherwise, handle backslashes and ranges specially. */
1453 while (i_byte < size_byte)
1455 c = STRING_CHAR_AND_LENGTH (str + i_byte, size_byte - i_byte, len);
1456 i_byte += len;
1458 if (syntaxp)
1459 fastmap[syntax_spec_code[c & 0377]] = 1;
1460 else
1462 if (c == '\\')
1464 if (i_byte == size_byte)
1465 break;
1467 c = STRING_CHAR_AND_LENGTH (str+i_byte, size_byte-i_byte, len);
1468 i_byte += len;
1470 if (i_byte < size_byte
1471 && str[i_byte] == '-')
1473 unsigned int c2;
1475 /* Skip over the dash. */
1476 i_byte++;
1478 if (i_byte == size_byte)
1479 break;
1481 /* Get the end of the range. */
1482 c2 =STRING_CHAR_AND_LENGTH (str+i_byte, size_byte-i_byte, len);
1483 i_byte += len;
1485 if (SINGLE_BYTE_CHAR_P (c))
1487 if (! SINGLE_BYTE_CHAR_P (c2))
1489 /* Handle a range starting with a character of
1490 less than 256, and ending with a character of
1491 not less than 256. Split that into two
1492 ranges, the low one ending at 0377, and the
1493 high one starting at the smallest character
1494 in the charset of C2 and ending at C2. */
1495 int charset = CHAR_CHARSET (c2);
1496 int c1 = MAKE_CHAR (charset, 0, 0);
1498 char_ranges[n_char_ranges++] = c1;
1499 char_ranges[n_char_ranges++] = c2;
1500 c2 = 0377;
1502 while (c <= c2)
1504 fastmap[c] = 1;
1505 c++;
1508 else if (c <= c2) /* Both C and C2 are multibyte char. */
1510 char_ranges[n_char_ranges++] = c;
1511 char_ranges[n_char_ranges++] = c2;
1514 else
1516 if (SINGLE_BYTE_CHAR_P (c))
1517 fastmap[c] = 1;
1518 else
1520 char_ranges[n_char_ranges++] = c;
1521 char_ranges[n_char_ranges++] = c;
1527 /* If ^ was the first character, complement the fastmap. */
1528 if (negate)
1529 for (i = 0; i < sizeof fastmap; i++)
1530 fastmap[i] ^= 1;
1533 int start_point = PT;
1534 int pos = PT;
1535 int pos_byte = PT_BYTE;
1537 immediate_quit = 1;
1538 if (syntaxp)
1540 SETUP_SYNTAX_TABLE (pos, forwardp ? 1 : -1);
1541 if (forwardp)
1543 if (multibyte)
1545 if (pos < XINT (lim))
1546 while (fastmap[(int) SYNTAX (FETCH_CHAR (pos_byte))])
1548 /* Since we already checked for multibyteness,
1549 avoid using INC_BOTH which checks again. */
1550 INC_POS (pos_byte);
1551 pos++;
1552 if (pos >= XINT (lim))
1553 break;
1554 UPDATE_SYNTAX_TABLE_FORWARD (pos);
1557 else
1559 while (pos < XINT (lim)
1560 && fastmap[(int) SYNTAX (FETCH_BYTE (pos))])
1562 pos++;
1563 UPDATE_SYNTAX_TABLE_FORWARD (pos);
1567 else
1569 if (multibyte)
1571 while (pos > XINT (lim))
1573 int savepos = pos_byte;
1574 /* Since we already checked for multibyteness,
1575 avoid using DEC_BOTH which checks again. */
1576 pos--;
1577 DEC_POS (pos_byte);
1578 UPDATE_SYNTAX_TABLE_BACKWARD (pos);
1579 if (!fastmap[(int) SYNTAX (FETCH_CHAR (pos_byte))])
1581 pos++;
1582 pos_byte = savepos;
1583 break;
1587 else
1589 if (pos > XINT (lim))
1590 while (fastmap[(int) SYNTAX (FETCH_BYTE (pos - 1))])
1592 pos--;
1593 if (pos <= XINT (lim))
1594 break;
1595 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
1600 else
1602 if (forwardp)
1604 if (multibyte)
1605 while (pos < XINT (lim))
1607 c = FETCH_MULTIBYTE_CHAR (pos_byte);
1608 if (SINGLE_BYTE_CHAR_P (c))
1610 if (!fastmap[c])
1611 break;
1613 else
1615 /* If we are looking at a multibyte character,
1616 we must look up the character in the table
1617 CHAR_RANGES. If there's no data in the
1618 table, that character is not what we want to
1619 skip. */
1621 /* The following code do the right thing even if
1622 n_char_ranges is zero (i.e. no data in
1623 CHAR_RANGES). */
1624 for (i = 0; i < n_char_ranges; i += 2)
1625 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
1626 break;
1627 if (!(negate ^ (i < n_char_ranges)))
1628 break;
1630 INC_BOTH (pos, pos_byte);
1632 else
1633 while (pos < XINT (lim) && fastmap[FETCH_BYTE (pos)])
1634 pos++;
1636 else
1638 if (multibyte)
1639 while (pos > XINT (lim))
1641 int prev_pos_byte = pos_byte;
1643 DEC_POS (prev_pos_byte);
1644 c = FETCH_MULTIBYTE_CHAR (prev_pos_byte);
1645 if (SINGLE_BYTE_CHAR_P (c))
1647 if (!fastmap[c])
1648 break;
1650 else
1652 /* See the comment in the previous similar code. */
1653 for (i = 0; i < n_char_ranges; i += 2)
1654 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
1655 break;
1656 if (!(negate ^ (i < n_char_ranges)))
1657 break;
1659 pos--;
1660 pos_byte = prev_pos_byte;
1662 else
1663 while (pos > XINT (lim) && fastmap[FETCH_BYTE (pos - 1)])
1664 pos--;
1668 #if 0 /* Not needed now that a position in mid-character
1669 cannot be specified in Lisp. */
1670 if (multibyte
1671 /* INC_POS or DEC_POS might have moved POS over LIM. */
1672 && (forwardp ? (pos > XINT (lim)) : (pos < XINT (lim))))
1673 pos = XINT (lim);
1674 #endif
1676 if (! multibyte)
1677 pos_byte = pos;
1679 SET_PT_BOTH (pos, pos_byte);
1680 immediate_quit = 0;
1682 return make_number (PT - start_point);
1686 /* Jump over a comment, assuming we are at the beginning of one.
1687 FROM is the current position.
1688 FROM_BYTE is the bytepos corresponding to FROM.
1689 Do not move past STOP (a charpos).
1690 The comment over which we have to jump is of style STYLE
1691 (either SYNTAX_COMMENT_STYLE(foo) or ST_COMMENT_STYLE).
1692 NESTING should be positive to indicate the nesting at the beginning
1693 for nested comments and should be zero or negative else.
1694 ST_COMMENT_STYLE cannot be nested.
1695 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
1696 (or 0 If the search cannot start in the middle of a two-character).
1698 If successful, return 1 and store the charpos of the comment's end
1699 into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR.
1700 Else, return 0 and store the charpos STOP into *CHARPOS_PTR, the
1701 corresponding bytepos into *BYTEPOS_PTR and the current nesting
1702 (as defined for state.incomment) in *INCOMMENT_PTR.
1704 The comment end is the last character of the comment rather than the
1705 character just after the comment.
1707 Global syntax data is assumed to initially be valid for FROM and
1708 remains valid for forward search starting at the returned position. */
1710 static int
1711 forw_comment (from, from_byte, stop, nesting, style, prev_syntax,
1712 charpos_ptr, bytepos_ptr, incomment_ptr)
1713 int from, from_byte, stop;
1714 int nesting, style, prev_syntax;
1715 int *charpos_ptr, *bytepos_ptr, *incomment_ptr;
1717 register int c, c1;
1718 register enum syntaxcode code;
1719 register int syntax;
1721 if (nesting <= 0) nesting = -1;
1723 /* Enter the loop in the middle so that we find
1724 a 2-char comment ender if we start in the middle of it. */
1725 syntax = prev_syntax;
1726 if (syntax != 0) goto forw_incomment;
1728 while (1)
1730 if (from == stop)
1732 *incomment_ptr = nesting;
1733 *charpos_ptr = from;
1734 *bytepos_ptr = from_byte;
1735 return 0;
1737 c = FETCH_CHAR (from_byte);
1738 syntax = SYNTAX_WITH_FLAGS (c);
1739 code = syntax & 0xff;
1740 if (code == Sendcomment
1741 && SYNTAX_FLAGS_COMMENT_STYLE (syntax) == style
1742 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ?
1743 (nesting > 0 && --nesting == 0) : nesting < 0))
1744 /* we have encountered a comment end of the same style
1745 as the comment sequence which began this comment
1746 section */
1747 break;
1748 if (code == Scomment_fence
1749 && style == ST_COMMENT_STYLE)
1750 /* we have encountered a comment end of the same style
1751 as the comment sequence which began this comment
1752 section. */
1753 break;
1754 if (nesting > 0
1755 && code == Scomment
1756 && SYNTAX_FLAGS_COMMENT_NESTED (syntax)
1757 && SYNTAX_FLAGS_COMMENT_STYLE (syntax) == style)
1758 /* we have encountered a nested comment of the same style
1759 as the comment sequence which began this comment section */
1760 nesting++;
1761 INC_BOTH (from, from_byte);
1762 UPDATE_SYNTAX_TABLE_FORWARD (from);
1764 forw_incomment:
1765 if (from < stop && SYNTAX_FLAGS_COMEND_FIRST (syntax)
1766 && SYNTAX_FLAGS_COMMENT_STYLE (syntax) == style
1767 && (c1 = FETCH_CHAR (from_byte),
1768 SYNTAX_COMEND_SECOND (c1))
1769 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
1770 SYNTAX_COMMENT_NESTED (c1)) ? nesting > 0 : nesting < 0))
1772 if (--nesting <= 0)
1773 /* we have encountered a comment end of the same style
1774 as the comment sequence which began this comment
1775 section */
1776 break;
1777 else
1779 INC_BOTH (from, from_byte);
1780 UPDATE_SYNTAX_TABLE_FORWARD (from);
1783 if (nesting > 0
1784 && from < stop
1785 && SYNTAX_FLAGS_COMSTART_FIRST (syntax)
1786 && (c1 = FETCH_CHAR (from_byte),
1787 SYNTAX_COMMENT_STYLE (c1) == style
1788 && SYNTAX_COMSTART_SECOND (c1))
1789 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
1790 SYNTAX_COMMENT_NESTED (c1)))
1791 /* we have encountered a nested comment of the same style
1792 as the comment sequence which began this comment
1793 section */
1795 INC_BOTH (from, from_byte);
1796 UPDATE_SYNTAX_TABLE_FORWARD (from);
1797 nesting++;
1800 *charpos_ptr = from;
1801 *bytepos_ptr = from_byte;
1802 return 1;
1805 DEFUN ("forward-comment", Fforward_comment, Sforward_comment, 1, 1, 0,
1806 doc: /* Move forward across up to N comments. If N is negative, move backward.
1807 Stop scanning if we find something other than a comment or whitespace.
1808 Set point to where scanning stops.
1809 If N comments are found as expected, with nothing except whitespace
1810 between them, return t; otherwise return nil. */)
1811 (count)
1812 Lisp_Object count;
1814 register int from;
1815 int from_byte;
1816 register int stop;
1817 register int c, c1;
1818 register enum syntaxcode code;
1819 int comstyle = 0; /* style of comment encountered */
1820 int comnested = 0; /* whether the comment is nestable or not */
1821 int found;
1822 int count1;
1823 int out_charpos, out_bytepos;
1824 int dummy;
1826 CHECK_NUMBER (count, 0);
1827 count1 = XINT (count);
1828 stop = count1 > 0 ? ZV : BEGV;
1830 immediate_quit = 1;
1831 QUIT;
1833 from = PT;
1834 from_byte = PT_BYTE;
1836 SETUP_SYNTAX_TABLE (from, count1);
1837 while (count1 > 0)
1841 int comstart_first;
1843 if (from == stop)
1845 SET_PT_BOTH (from, from_byte);
1846 immediate_quit = 0;
1847 return Qnil;
1849 c = FETCH_CHAR (from_byte);
1850 code = SYNTAX (c);
1851 comstart_first = SYNTAX_COMSTART_FIRST (c);
1852 comnested = SYNTAX_COMMENT_NESTED (c);
1853 comstyle = SYNTAX_COMMENT_STYLE (c);
1854 INC_BOTH (from, from_byte);
1855 UPDATE_SYNTAX_TABLE_FORWARD (from);
1856 if (from < stop && comstart_first
1857 && (c1 = FETCH_CHAR (from_byte),
1858 SYNTAX_COMSTART_SECOND (c1)))
1860 /* We have encountered a comment start sequence and we
1861 are ignoring all text inside comments. We must record
1862 the comment style this sequence begins so that later,
1863 only a comment end of the same style actually ends
1864 the comment section. */
1865 code = Scomment;
1866 comstyle = SYNTAX_COMMENT_STYLE (c1);
1867 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
1868 INC_BOTH (from, from_byte);
1869 UPDATE_SYNTAX_TABLE_FORWARD (from);
1872 while (code == Swhitespace || (code == Sendcomment && c == '\n'));
1874 if (code == Scomment_fence)
1875 comstyle = ST_COMMENT_STYLE;
1876 else if (code != Scomment)
1878 immediate_quit = 0;
1879 DEC_BOTH (from, from_byte);
1880 SET_PT_BOTH (from, from_byte);
1881 return Qnil;
1883 /* We're at the start of a comment. */
1884 found = forw_comment (from, from_byte, stop, comnested, comstyle, 0,
1885 &out_charpos, &out_bytepos, &dummy);
1886 from = out_charpos; from_byte = out_bytepos;
1887 if (!found)
1889 immediate_quit = 0;
1890 SET_PT_BOTH (from, from_byte);
1891 return Qnil;
1893 INC_BOTH (from, from_byte);
1894 UPDATE_SYNTAX_TABLE_FORWARD (from);
1895 /* We have skipped one comment. */
1896 count1--;
1899 while (count1 < 0)
1901 while (1)
1903 int quoted;
1905 if (from <= stop)
1907 SET_PT_BOTH (BEGV, BEGV_BYTE);
1908 immediate_quit = 0;
1909 return Qnil;
1912 DEC_BOTH (from, from_byte);
1913 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
1914 quoted = char_quoted (from, from_byte);
1915 c = FETCH_CHAR (from_byte);
1916 code = SYNTAX (c);
1917 comstyle = 0;
1918 comnested = SYNTAX_COMMENT_NESTED (c);
1919 if (code == Sendcomment)
1920 comstyle = SYNTAX_COMMENT_STYLE (c);
1921 if (from > stop && SYNTAX_COMEND_SECOND (c)
1922 && prev_char_comend_first (from, from_byte)
1923 && !char_quoted (from - 1, dec_bytepos (from_byte)))
1925 /* We must record the comment style encountered so that
1926 later, we can match only the proper comment begin
1927 sequence of the same style. */
1928 DEC_BOTH (from, from_byte);
1929 code = Sendcomment;
1930 /* Calling char_quoted, above, set up global syntax position
1931 at the new value of FROM. */
1932 c1 = FETCH_CHAR (from_byte);
1933 comstyle = SYNTAX_COMMENT_STYLE (c1);
1934 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
1937 if (code == Scomment_fence)
1939 /* Skip until first preceding unquoted comment_fence. */
1940 int found = 0, ini = from, ini_byte = from_byte;
1942 while (1)
1944 DEC_BOTH (from, from_byte);
1945 if (from == stop)
1946 break;
1947 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1948 c = FETCH_CHAR (from_byte);
1949 if (SYNTAX (c) == Scomment_fence
1950 && !char_quoted (from, from_byte))
1952 found = 1;
1953 break;
1956 if (found == 0)
1958 from = ini; /* Set point to ini + 1. */
1959 from_byte = ini_byte;
1960 goto leave;
1963 else if (code == Sendcomment)
1965 found = back_comment (from, from_byte, stop, comnested, comstyle,
1966 &out_charpos, &out_bytepos);
1967 if (found == -1)
1969 if (c == '\n')
1970 /* This end-of-line is not an end-of-comment.
1971 Treat it like a whitespace.
1972 CC-mode (and maybe others) relies on this behavior. */
1974 else
1976 /* Failure: we should go back to the end of this
1977 not-quite-endcomment. */
1978 if (SYNTAX(c) != code)
1979 /* It was a two-char Sendcomment. */
1980 INC_BOTH (from, from_byte);
1981 goto leave;
1984 else
1986 /* We have skipped one comment. */
1987 from = out_charpos, from_byte = out_bytepos;
1988 break;
1991 else if (code != Swhitespace || quoted)
1993 leave:
1994 immediate_quit = 0;
1995 INC_BOTH (from, from_byte);
1996 SET_PT_BOTH (from, from_byte);
1997 return Qnil;
2001 count1++;
2004 SET_PT_BOTH (from, from_byte);
2005 immediate_quit = 0;
2006 return Qt;
2009 /* Return syntax code of character C if C is a single byte character
2010 or `multibyte_symbol_p' is zero. Otherwise, return Ssymbol. */
2012 #define SYNTAX_WITH_MULTIBYTE_CHECK(c) \
2013 ((SINGLE_BYTE_CHAR_P (c) || !multibyte_symbol_p) \
2014 ? SYNTAX (c) : Ssymbol)
2016 static Lisp_Object
2017 scan_lists (from, count, depth, sexpflag)
2018 register int from;
2019 int count, depth, sexpflag;
2021 Lisp_Object val;
2022 register int stop = count > 0 ? ZV : BEGV;
2023 register int c, c1;
2024 int stringterm;
2025 int quoted;
2026 int mathexit = 0;
2027 register enum syntaxcode code, temp_code;
2028 int min_depth = depth; /* Err out if depth gets less than this. */
2029 int comstyle = 0; /* style of comment encountered */
2030 int comnested = 0; /* whether the comment is nestable or not */
2031 int temp_pos;
2032 int last_good = from;
2033 int found;
2034 int from_byte;
2035 int out_bytepos, out_charpos;
2036 int temp, dummy;
2037 int multibyte_symbol_p = sexpflag && multibyte_syntax_as_symbol;
2039 if (depth > 0) min_depth = 0;
2041 if (from > ZV) from = ZV;
2042 if (from < BEGV) from = BEGV;
2044 from_byte = CHAR_TO_BYTE (from);
2046 immediate_quit = 1;
2047 QUIT;
2049 SETUP_SYNTAX_TABLE (from, count);
2050 while (count > 0)
2052 while (from < stop)
2054 int comstart_first, prefix;
2055 UPDATE_SYNTAX_TABLE_FORWARD (from);
2056 c = FETCH_CHAR (from_byte);
2057 code = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2058 comstart_first = SYNTAX_COMSTART_FIRST (c);
2059 comnested = SYNTAX_COMMENT_NESTED (c);
2060 comstyle = SYNTAX_COMMENT_STYLE (c);
2061 prefix = SYNTAX_PREFIX (c);
2062 if (depth == min_depth)
2063 last_good = from;
2064 INC_BOTH (from, from_byte);
2065 UPDATE_SYNTAX_TABLE_FORWARD (from);
2066 if (from < stop && comstart_first
2067 && SYNTAX_COMSTART_SECOND (FETCH_CHAR (from_byte))
2068 && parse_sexp_ignore_comments)
2070 /* we have encountered a comment start sequence and we
2071 are ignoring all text inside comments. We must record
2072 the comment style this sequence begins so that later,
2073 only a comment end of the same style actually ends
2074 the comment section */
2075 code = Scomment;
2076 c1 = FETCH_CHAR (from_byte);
2077 comstyle = SYNTAX_COMMENT_STYLE (c1);
2078 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
2079 INC_BOTH (from, from_byte);
2080 UPDATE_SYNTAX_TABLE_FORWARD (from);
2083 if (prefix)
2084 continue;
2086 switch (SWITCH_ENUM_CAST (code))
2088 case Sescape:
2089 case Scharquote:
2090 if (from == stop) goto lose;
2091 INC_BOTH (from, from_byte);
2092 /* treat following character as a word constituent */
2093 case Sword:
2094 case Ssymbol:
2095 if (depth || !sexpflag) break;
2096 /* This word counts as a sexp; return at end of it. */
2097 while (from < stop)
2099 UPDATE_SYNTAX_TABLE_FORWARD (from);
2101 /* Some compilers can't handle this inside the switch. */
2102 c = FETCH_CHAR (from_byte);
2103 temp = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2104 switch (temp)
2106 case Scharquote:
2107 case Sescape:
2108 INC_BOTH (from, from_byte);
2109 if (from == stop) goto lose;
2110 break;
2111 case Sword:
2112 case Ssymbol:
2113 case Squote:
2114 break;
2115 default:
2116 goto done;
2118 INC_BOTH (from, from_byte);
2120 goto done;
2122 case Scomment_fence:
2123 comstyle = ST_COMMENT_STYLE;
2124 /* FALLTHROUGH */
2125 case Scomment:
2126 if (!parse_sexp_ignore_comments) break;
2127 UPDATE_SYNTAX_TABLE_FORWARD (from);
2128 found = forw_comment (from, from_byte, stop,
2129 comnested, comstyle, 0,
2130 &out_charpos, &out_bytepos, &dummy);
2131 from = out_charpos, from_byte = out_bytepos;
2132 if (!found)
2134 if (depth == 0)
2135 goto done;
2136 goto lose;
2138 INC_BOTH (from, from_byte);
2139 UPDATE_SYNTAX_TABLE_FORWARD (from);
2140 break;
2142 case Smath:
2143 if (!sexpflag)
2144 break;
2145 if (from != stop && c == FETCH_CHAR (from_byte))
2147 INC_BOTH (from, from_byte);
2149 if (mathexit)
2151 mathexit = 0;
2152 goto close1;
2154 mathexit = 1;
2156 case Sopen:
2157 if (!++depth) goto done;
2158 break;
2160 case Sclose:
2161 close1:
2162 if (!--depth) goto done;
2163 if (depth < min_depth)
2164 Fsignal (Qscan_error,
2165 Fcons (build_string ("Containing expression ends prematurely"),
2166 Fcons (make_number (last_good),
2167 Fcons (make_number (from), Qnil))));
2168 break;
2170 case Sstring:
2171 case Sstring_fence:
2172 temp_pos = dec_bytepos (from_byte);
2173 stringterm = FETCH_CHAR (temp_pos);
2174 while (1)
2176 if (from >= stop) goto lose;
2177 UPDATE_SYNTAX_TABLE_FORWARD (from);
2178 c = FETCH_CHAR (from_byte);
2179 if (code == Sstring
2180 ? (c == stringterm
2181 && SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring)
2182 : SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring_fence)
2183 break;
2185 /* Some compilers can't handle this inside the switch. */
2186 temp = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2187 switch (temp)
2189 case Scharquote:
2190 case Sescape:
2191 INC_BOTH (from, from_byte);
2193 INC_BOTH (from, from_byte);
2195 INC_BOTH (from, from_byte);
2196 if (!depth && sexpflag) goto done;
2197 break;
2201 /* Reached end of buffer. Error if within object, return nil if between */
2202 if (depth) goto lose;
2204 immediate_quit = 0;
2205 return Qnil;
2207 /* End of object reached */
2208 done:
2209 count--;
2212 while (count < 0)
2214 while (from > stop)
2216 DEC_BOTH (from, from_byte);
2217 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2218 c = FETCH_CHAR (from_byte);
2219 code = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2220 if (depth == min_depth)
2221 last_good = from;
2222 comstyle = 0;
2223 comnested = SYNTAX_COMMENT_NESTED (c);
2224 if (code == Sendcomment)
2225 comstyle = SYNTAX_COMMENT_STYLE (c);
2226 if (from > stop && SYNTAX_COMEND_SECOND (c)
2227 && prev_char_comend_first (from, from_byte)
2228 && parse_sexp_ignore_comments)
2230 /* We must record the comment style encountered so that
2231 later, we can match only the proper comment begin
2232 sequence of the same style. */
2233 DEC_BOTH (from, from_byte);
2234 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2235 code = Sendcomment;
2236 c1 = FETCH_CHAR (from_byte);
2237 comstyle = SYNTAX_COMMENT_STYLE (c1);
2238 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
2241 /* Quoting turns anything except a comment-ender
2242 into a word character. Note that this cannot be true
2243 if we decremented FROM in the if-statement above. */
2244 if (code != Sendcomment && char_quoted (from, from_byte))
2245 code = Sword;
2246 else if (SYNTAX_PREFIX (c))
2247 continue;
2249 switch (SWITCH_ENUM_CAST (code))
2251 case Sword:
2252 case Ssymbol:
2253 case Sescape:
2254 case Scharquote:
2255 if (depth || !sexpflag) break;
2256 /* This word counts as a sexp; count object finished
2257 after passing it. */
2258 while (from > stop)
2260 temp_pos = from_byte;
2261 if (! NILP (current_buffer->enable_multibyte_characters))
2262 DEC_POS (temp_pos);
2263 else
2264 temp_pos--;
2265 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2266 c1 = FETCH_CHAR (temp_pos);
2267 temp_code = SYNTAX_WITH_MULTIBYTE_CHECK (c1);
2268 /* Don't allow comment-end to be quoted. */
2269 if (temp_code == Sendcomment)
2270 goto done2;
2271 quoted = char_quoted (from - 1, temp_pos);
2272 if (quoted)
2274 DEC_BOTH (from, from_byte);
2275 temp_pos = dec_bytepos (temp_pos);
2276 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2278 c1 = FETCH_CHAR (temp_pos);
2279 temp_code = SYNTAX_WITH_MULTIBYTE_CHECK (c1);
2280 if (! (quoted || temp_code == Sword
2281 || temp_code == Ssymbol
2282 || temp_code == Squote))
2283 goto done2;
2284 DEC_BOTH (from, from_byte);
2286 goto done2;
2288 case Smath:
2289 if (!sexpflag)
2290 break;
2291 temp_pos = dec_bytepos (from_byte);
2292 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2293 if (from != stop && c == FETCH_CHAR (temp_pos))
2294 DEC_BOTH (from, from_byte);
2295 if (mathexit)
2297 mathexit = 0;
2298 goto open2;
2300 mathexit = 1;
2302 case Sclose:
2303 if (!++depth) goto done2;
2304 break;
2306 case Sopen:
2307 open2:
2308 if (!--depth) goto done2;
2309 if (depth < min_depth)
2310 Fsignal (Qscan_error,
2311 Fcons (build_string ("Containing expression ends prematurely"),
2312 Fcons (make_number (last_good),
2313 Fcons (make_number (from), Qnil))));
2314 break;
2316 case Sendcomment:
2317 if (!parse_sexp_ignore_comments)
2318 break;
2319 found = back_comment (from, from_byte, stop, comnested, comstyle,
2320 &out_charpos, &out_bytepos);
2321 /* FIXME: if found == -1, then it really wasn't a comment-end.
2322 For single-char Sendcomment, we can't do much about it apart
2323 from skipping the char.
2324 For 2-char endcomments, we could try again, taking both
2325 chars as separate entities, but it's a lot of trouble
2326 for very little gain, so we don't bother either. -sm */
2327 if (found != -1)
2328 from = out_charpos, from_byte = out_bytepos;
2329 break;
2331 case Scomment_fence:
2332 case Sstring_fence:
2333 while (1)
2335 DEC_BOTH (from, from_byte);
2336 if (from == stop) goto lose;
2337 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2338 if (!char_quoted (from, from_byte)
2339 && (c = FETCH_CHAR (from_byte),
2340 SYNTAX_WITH_MULTIBYTE_CHECK (c) == code))
2341 break;
2343 if (code == Sstring_fence && !depth && sexpflag) goto done2;
2344 break;
2346 case Sstring:
2347 stringterm = FETCH_CHAR (from_byte);
2348 while (1)
2350 if (from == stop) goto lose;
2351 temp_pos = from_byte;
2352 if (! NILP (current_buffer->enable_multibyte_characters))
2353 DEC_POS (temp_pos);
2354 else
2355 temp_pos--;
2356 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2357 if (!char_quoted (from - 1, temp_pos)
2358 && stringterm == (c = FETCH_CHAR (temp_pos))
2359 && SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring)
2360 break;
2361 DEC_BOTH (from, from_byte);
2363 DEC_BOTH (from, from_byte);
2364 if (!depth && sexpflag) goto done2;
2365 break;
2369 /* Reached start of buffer. Error if within object, return nil if between */
2370 if (depth) goto lose;
2372 immediate_quit = 0;
2373 return Qnil;
2375 done2:
2376 count++;
2380 immediate_quit = 0;
2381 XSETFASTINT (val, from);
2382 return val;
2384 lose:
2385 Fsignal (Qscan_error,
2386 Fcons (build_string ("Unbalanced parentheses"),
2387 Fcons (make_number (last_good),
2388 Fcons (make_number (from), Qnil))));
2390 /* NOTREACHED */
2393 DEFUN ("scan-lists", Fscan_lists, Sscan_lists, 3, 3, 0,
2394 doc: /* Scan from character number FROM by COUNT lists.
2395 Returns the character number of the position thus found.
2397 If DEPTH is nonzero, paren depth begins counting from that value,
2398 only places where the depth in parentheses becomes zero
2399 are candidates for stopping; COUNT such places are counted.
2400 Thus, a positive value for DEPTH means go out levels.
2402 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2404 If the beginning or end of (the accessible part of) the buffer is reached
2405 and the depth is wrong, an error is signaled.
2406 If the depth is right but the count is not used up, nil is returned. */)
2407 (from, count, depth)
2408 Lisp_Object from, count, depth;
2410 CHECK_NUMBER (from, 0);
2411 CHECK_NUMBER (count, 1);
2412 CHECK_NUMBER (depth, 2);
2414 return scan_lists (XINT (from), XINT (count), XINT (depth), 0);
2417 DEFUN ("scan-sexps", Fscan_sexps, Sscan_sexps, 2, 2, 0,
2418 doc: /* Scan from character number FROM by COUNT balanced expressions.
2419 If COUNT is negative, scan backwards.
2420 Returns the character number of the position thus found.
2422 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2424 If the beginning or end of (the accessible part of) the buffer is reached
2425 in the middle of a parenthetical grouping, an error is signaled.
2426 If the beginning or end is reached between groupings
2427 but before count is used up, nil is returned. */)
2428 (from, count)
2429 Lisp_Object from, count;
2431 CHECK_NUMBER (from, 0);
2432 CHECK_NUMBER (count, 1);
2434 return scan_lists (XINT (from), XINT (count), 0, 1);
2437 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars, Sbackward_prefix_chars,
2438 0, 0, 0,
2439 doc: /* Move point backward over any number of chars with prefix syntax.
2440 This includes chars with "quote" or "prefix" syntax (' or p). */)
2443 int beg = BEGV;
2444 int opoint = PT;
2445 int opoint_byte = PT_BYTE;
2446 int pos = PT;
2447 int pos_byte = PT_BYTE;
2448 int c;
2450 if (pos <= beg)
2452 SET_PT_BOTH (opoint, opoint_byte);
2454 return Qnil;
2457 SETUP_SYNTAX_TABLE (pos, -1);
2459 DEC_BOTH (pos, pos_byte);
2461 while (!char_quoted (pos, pos_byte)
2462 /* Previous statement updates syntax table. */
2463 && ((c = FETCH_CHAR (pos_byte), SYNTAX (c) == Squote)
2464 || SYNTAX_PREFIX (c)))
2466 opoint = pos;
2467 opoint_byte = pos_byte;
2469 if (pos + 1 > beg)
2470 DEC_BOTH (pos, pos_byte);
2473 SET_PT_BOTH (opoint, opoint_byte);
2475 return Qnil;
2478 /* Parse forward from FROM / FROM_BYTE to END,
2479 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
2480 and return a description of the state of the parse at END.
2481 If STOPBEFORE is nonzero, stop at the start of an atom.
2482 If COMMENTSTOP is 1, stop at the start of a comment.
2483 If COMMENTSTOP is -1, stop at the start or end of a comment,
2484 after the beginning of a string, or after the end of a string. */
2486 static void
2487 scan_sexps_forward (stateptr, from, from_byte, end, targetdepth,
2488 stopbefore, oldstate, commentstop)
2489 struct lisp_parse_state *stateptr;
2490 register int from;
2491 int end, targetdepth, stopbefore;
2492 Lisp_Object oldstate;
2493 int commentstop;
2495 struct lisp_parse_state state;
2497 register enum syntaxcode code;
2498 int c1;
2499 int comnested;
2500 struct level { int last, prev; };
2501 struct level levelstart[100];
2502 register struct level *curlevel = levelstart;
2503 struct level *endlevel = levelstart + 100;
2504 register int depth; /* Paren depth of current scanning location.
2505 level - levelstart equals this except
2506 when the depth becomes negative. */
2507 int mindepth; /* Lowest DEPTH value seen. */
2508 int start_quoted = 0; /* Nonzero means starting after a char quote */
2509 Lisp_Object tem;
2510 int prev_from; /* Keep one character before FROM. */
2511 int prev_from_byte;
2512 int prev_from_syntax;
2513 int boundary_stop = commentstop == -1;
2514 int nofence;
2515 int found;
2516 int out_bytepos, out_charpos;
2517 int temp;
2519 prev_from = from;
2520 prev_from_byte = from_byte;
2521 if (from != BEGV)
2522 DEC_BOTH (prev_from, prev_from_byte);
2524 /* Use this macro instead of `from++'. */
2525 #define INC_FROM \
2526 do { prev_from = from; \
2527 prev_from_byte = from_byte; \
2528 prev_from_syntax \
2529 = SYNTAX_WITH_FLAGS (FETCH_CHAR (prev_from_byte)); \
2530 INC_BOTH (from, from_byte); \
2531 UPDATE_SYNTAX_TABLE_FORWARD (from); \
2532 } while (0)
2534 immediate_quit = 1;
2535 QUIT;
2537 if (NILP (oldstate))
2539 depth = 0;
2540 state.instring = -1;
2541 state.incomment = 0;
2542 state.comstyle = 0; /* comment style a by default. */
2543 state.comstr_start = -1; /* no comment/string seen. */
2545 else
2547 tem = Fcar (oldstate);
2548 if (!NILP (tem))
2549 depth = XINT (tem);
2550 else
2551 depth = 0;
2553 oldstate = Fcdr (oldstate);
2554 oldstate = Fcdr (oldstate);
2555 oldstate = Fcdr (oldstate);
2556 tem = Fcar (oldstate);
2557 /* Check whether we are inside string_fence-style string: */
2558 state.instring = (!NILP (tem)
2559 ? (INTEGERP (tem) ? XINT (tem) : ST_STRING_STYLE)
2560 : -1);
2562 oldstate = Fcdr (oldstate);
2563 tem = Fcar (oldstate);
2564 state.incomment = (!NILP (tem)
2565 ? (INTEGERP (tem) ? XINT (tem) : -1)
2566 : 0);
2568 oldstate = Fcdr (oldstate);
2569 tem = Fcar (oldstate);
2570 start_quoted = !NILP (tem);
2572 /* if the eighth element of the list is nil, we are in comment
2573 style a. If it is non-nil, we are in comment style b */
2574 oldstate = Fcdr (oldstate);
2575 oldstate = Fcdr (oldstate);
2576 tem = Fcar (oldstate);
2577 state.comstyle = NILP (tem) ? 0 : (EQ (tem, Qsyntax_table)
2578 ? ST_COMMENT_STYLE : 1);
2580 oldstate = Fcdr (oldstate);
2581 tem = Fcar (oldstate);
2582 state.comstr_start = NILP (tem) ? -1 : XINT (tem) ;
2583 oldstate = Fcdr (oldstate);
2584 tem = Fcar (oldstate);
2585 while (!NILP (tem)) /* >= second enclosing sexps. */
2587 /* curlevel++->last ran into compiler bug on Apollo */
2588 curlevel->last = XINT (Fcar (tem));
2589 if (++curlevel == endlevel)
2590 curlevel--; /* error ("Nesting too deep for parser"); */
2591 curlevel->prev = -1;
2592 curlevel->last = -1;
2593 tem = Fcdr (tem);
2596 state.quoted = 0;
2597 mindepth = depth;
2599 curlevel->prev = -1;
2600 curlevel->last = -1;
2602 SETUP_SYNTAX_TABLE (prev_from, 1);
2603 prev_from_syntax = SYNTAX_WITH_FLAGS (FETCH_CHAR (prev_from_byte));
2604 UPDATE_SYNTAX_TABLE_FORWARD (from);
2606 /* Enter the loop at a place appropriate for initial state. */
2608 if (state.incomment)
2609 goto startincomment;
2610 if (state.instring >= 0)
2612 nofence = state.instring != ST_STRING_STYLE;
2613 if (start_quoted)
2614 goto startquotedinstring;
2615 goto startinstring;
2617 else if (start_quoted)
2618 goto startquoted;
2620 #if 0 /* This seems to be redundant with the identical code above. */
2621 SETUP_SYNTAX_TABLE (prev_from, 1);
2622 prev_from_syntax = SYNTAX_WITH_FLAGS (FETCH_CHAR (prev_from_byte));
2623 UPDATE_SYNTAX_TABLE_FORWARD (from);
2624 #endif
2626 while (from < end)
2628 INC_FROM;
2629 code = prev_from_syntax & 0xff;
2631 if (code == Scomment)
2633 state.comstyle = SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax);
2634 state.incomment = (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax) ?
2635 1 : -1);
2636 state.comstr_start = prev_from;
2638 else if (code == Scomment_fence)
2640 /* Record the comment style we have entered so that only
2641 the comment-end sequence of the same style actually
2642 terminates the comment section. */
2643 state.comstyle = ST_COMMENT_STYLE;
2644 state.incomment = -1;
2645 state.comstr_start = prev_from;
2646 code = Scomment;
2648 else if (from < end)
2649 if (SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax))
2650 if (c1 = FETCH_CHAR (from_byte),
2651 SYNTAX_COMSTART_SECOND (c1))
2652 /* Duplicate code to avoid a complex if-expression
2653 which causes trouble for the SGI compiler. */
2655 /* Record the comment style we have entered so that only
2656 the comment-end sequence of the same style actually
2657 terminates the comment section. */
2658 state.comstyle = SYNTAX_COMMENT_STYLE (FETCH_CHAR (from_byte));
2659 comnested = SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax);
2660 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
2661 state.incomment = comnested ? 1 : -1;
2662 state.comstr_start = prev_from;
2663 INC_FROM;
2664 code = Scomment;
2667 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax))
2668 continue;
2669 switch (SWITCH_ENUM_CAST (code))
2671 case Sescape:
2672 case Scharquote:
2673 if (stopbefore) goto stop; /* this arg means stop at sexp start */
2674 curlevel->last = prev_from;
2675 startquoted:
2676 if (from == end) goto endquoted;
2677 INC_FROM;
2678 goto symstarted;
2679 /* treat following character as a word constituent */
2680 case Sword:
2681 case Ssymbol:
2682 if (stopbefore) goto stop; /* this arg means stop at sexp start */
2683 curlevel->last = prev_from;
2684 symstarted:
2685 while (from < end)
2687 /* Some compilers can't handle this inside the switch. */
2688 temp = SYNTAX (FETCH_CHAR (from_byte));
2689 switch (temp)
2691 case Scharquote:
2692 case Sescape:
2693 INC_FROM;
2694 if (from == end) goto endquoted;
2695 break;
2696 case Sword:
2697 case Ssymbol:
2698 case Squote:
2699 break;
2700 default:
2701 goto symdone;
2703 INC_FROM;
2705 symdone:
2706 curlevel->prev = curlevel->last;
2707 break;
2709 case Scomment:
2710 if (commentstop || boundary_stop) goto done;
2711 startincomment:
2712 /* The (from == BEGV) test was to enter the loop in the middle so
2713 that we find a 2-char comment ender even if we start in the
2714 middle of it. We don't want to do that if we're just at the
2715 beginning of the comment (think of (*) ... (*)). */
2716 found = forw_comment (from, from_byte, end,
2717 state.incomment, state.comstyle,
2718 (from == BEGV || from < state.comstr_start + 3)
2719 ? 0 : prev_from_syntax,
2720 &out_charpos, &out_bytepos, &state.incomment);
2721 from = out_charpos; from_byte = out_bytepos;
2722 /* Beware! prev_from and friends are invalid now.
2723 Luckily, the `done' doesn't use them and the INC_FROM
2724 sets them to a sane value without looking at them. */
2725 if (!found) goto done;
2726 INC_FROM;
2727 state.incomment = 0;
2728 state.comstyle = 0; /* reset the comment style */
2729 if (boundary_stop) goto done;
2730 break;
2732 case Sopen:
2733 if (stopbefore) goto stop; /* this arg means stop at sexp start */
2734 depth++;
2735 /* curlevel++->last ran into compiler bug on Apollo */
2736 curlevel->last = prev_from;
2737 if (++curlevel == endlevel)
2738 curlevel--; /* error ("Nesting too deep for parser"); */
2739 curlevel->prev = -1;
2740 curlevel->last = -1;
2741 if (targetdepth == depth) goto done;
2742 break;
2744 case Sclose:
2745 depth--;
2746 if (depth < mindepth)
2747 mindepth = depth;
2748 if (curlevel != levelstart)
2749 curlevel--;
2750 curlevel->prev = curlevel->last;
2751 if (targetdepth == depth) goto done;
2752 break;
2754 case Sstring:
2755 case Sstring_fence:
2756 state.comstr_start = from - 1;
2757 if (stopbefore) goto stop; /* this arg means stop at sexp start */
2758 curlevel->last = prev_from;
2759 state.instring = (code == Sstring
2760 ? (FETCH_CHAR (prev_from_byte))
2761 : ST_STRING_STYLE);
2762 if (boundary_stop) goto done;
2763 startinstring:
2765 nofence = state.instring != ST_STRING_STYLE;
2767 while (1)
2769 int c;
2771 if (from >= end) goto done;
2772 c = FETCH_CHAR (from_byte);
2773 /* Some compilers can't handle this inside the switch. */
2774 temp = SYNTAX (c);
2776 /* Check TEMP here so that if the char has
2777 a syntax-table property which says it is NOT
2778 a string character, it does not end the string. */
2779 if (nofence && c == state.instring && temp == Sstring)
2780 break;
2782 switch (temp)
2784 case Sstring_fence:
2785 if (!nofence) goto string_end;
2786 break;
2787 case Scharquote:
2788 case Sescape:
2789 INC_FROM;
2790 startquotedinstring:
2791 if (from >= end) goto endquoted;
2793 INC_FROM;
2796 string_end:
2797 state.instring = -1;
2798 curlevel->prev = curlevel->last;
2799 INC_FROM;
2800 if (boundary_stop) goto done;
2801 break;
2803 case Smath:
2804 break;
2807 goto done;
2809 stop: /* Here if stopping before start of sexp. */
2810 from = prev_from; /* We have just fetched the char that starts it; */
2811 goto done; /* but return the position before it. */
2813 endquoted:
2814 state.quoted = 1;
2815 done:
2816 state.depth = depth;
2817 state.mindepth = mindepth;
2818 state.thislevelstart = curlevel->prev;
2819 state.prevlevelstart
2820 = (curlevel == levelstart) ? -1 : (curlevel - 1)->last;
2821 state.location = from;
2822 state.levelstarts = Qnil;
2823 while (--curlevel >= levelstart)
2824 state.levelstarts = Fcons (make_number (curlevel->last),
2825 state.levelstarts);
2826 immediate_quit = 0;
2828 *stateptr = state;
2831 DEFUN ("parse-partial-sexp", Fparse_partial_sexp, Sparse_partial_sexp, 2, 6, 0,
2832 doc: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
2833 Parsing stops at TO or when certain criteria are met;
2834 point is set to where parsing stops.
2835 If fifth arg OLDSTATE is omitted or nil,
2836 parsing assumes that FROM is the beginning of a function.
2837 Value is a list of ten elements describing final state of parsing:
2838 0. depth in parens.
2839 1. character address of start of innermost containing list; nil if none.
2840 2. character address of start of last complete sexp terminated.
2841 3. non-nil if inside a string.
2842 (it is the character that will terminate the string,
2843 or t if the string should be terminated by a generic string delimiter.)
2844 4. nil if outside a comment, t if inside a non-nestable comment,
2845 else an integer (the current comment nesting).
2846 5. t if following a quote character.
2847 6. the minimum paren-depth encountered during this scan.
2848 7. t if in a comment of style b; symbol `syntax-table' if the comment
2849 should be terminated by a generic comment delimiter.
2850 8. character address of start of comment or string; nil if not in one.
2851 9. Intermediate data for continuation of parsing (subject to change).
2852 If third arg TARGETDEPTH is non-nil, parsing stops if the depth
2853 in parentheses becomes equal to TARGETDEPTH.
2854 Fourth arg STOPBEFORE non-nil means stop when come to
2855 any character that starts a sexp.
2856 Fifth arg OLDSTATE is a nine-element list like what this function returns.
2857 It is used to initialize the state of the parse. Elements number 1, 2, 6
2858 and 8 are ignored; you can leave off element 8 (the last) entirely.
2859 Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.
2860 If it is symbol `syntax-table', stop after the start of a comment or a
2861 string, or after end of a comment or a string. */)
2862 (from, to, targetdepth, stopbefore, oldstate, commentstop)
2863 Lisp_Object from, to, targetdepth, stopbefore, oldstate, commentstop;
2865 struct lisp_parse_state state;
2866 int target;
2868 if (!NILP (targetdepth))
2870 CHECK_NUMBER (targetdepth, 3);
2871 target = XINT (targetdepth);
2873 else
2874 target = -100000; /* We won't reach this depth */
2876 validate_region (&from, &to);
2877 scan_sexps_forward (&state, XINT (from), CHAR_TO_BYTE (XINT (from)),
2878 XINT (to),
2879 target, !NILP (stopbefore), oldstate,
2880 (NILP (commentstop)
2881 ? 0 : (EQ (commentstop, Qsyntax_table) ? -1 : 1)));
2883 SET_PT (state.location);
2885 return Fcons (make_number (state.depth),
2886 Fcons (state.prevlevelstart < 0 ? Qnil : make_number (state.prevlevelstart),
2887 Fcons (state.thislevelstart < 0 ? Qnil : make_number (state.thislevelstart),
2888 Fcons (state.instring >= 0
2889 ? (state.instring == ST_STRING_STYLE
2890 ? Qt : make_number (state.instring)) : Qnil,
2891 Fcons (state.incomment < 0 ? Qt :
2892 (state.incomment == 0 ? Qnil :
2893 make_number (state.incomment)),
2894 Fcons (state.quoted ? Qt : Qnil,
2895 Fcons (make_number (state.mindepth),
2896 Fcons ((state.comstyle
2897 ? (state.comstyle == ST_COMMENT_STYLE
2898 ? Qsyntax_table : Qt) :
2899 Qnil),
2900 Fcons (((state.incomment
2901 || (state.instring >= 0))
2902 ? make_number (state.comstr_start)
2903 : Qnil),
2904 Fcons (state.levelstarts, Qnil))))))))));
2907 void
2908 init_syntax_once ()
2910 register int i, c;
2911 Lisp_Object temp;
2913 /* This has to be done here, before we call Fmake_char_table. */
2914 Qsyntax_table = intern ("syntax-table");
2915 staticpro (&Qsyntax_table);
2917 /* Intern this now in case it isn't already done.
2918 Setting this variable twice is harmless.
2919 But don't staticpro it here--that is done in alloc.c. */
2920 Qchar_table_extra_slots = intern ("char-table-extra-slots");
2922 /* Create objects which can be shared among syntax tables. */
2923 Vsyntax_code_object = Fmake_vector (make_number (Smax), Qnil);
2924 for (i = 0; i < XVECTOR (Vsyntax_code_object)->size; i++)
2925 XVECTOR (Vsyntax_code_object)->contents[i]
2926 = Fcons (make_number (i), Qnil);
2928 /* Now we are ready to set up this property, so we can
2929 create syntax tables. */
2930 Fput (Qsyntax_table, Qchar_table_extra_slots, make_number (0));
2932 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Swhitespace];
2934 Vstandard_syntax_table = Fmake_char_table (Qsyntax_table, temp);
2936 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Sword];
2937 for (i = 'a'; i <= 'z'; i++)
2938 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
2939 for (i = 'A'; i <= 'Z'; i++)
2940 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
2941 for (i = '0'; i <= '9'; i++)
2942 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
2944 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '$', temp);
2945 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '%', temp);
2947 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '(',
2948 Fcons (make_number (Sopen), make_number (')')));
2949 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ')',
2950 Fcons (make_number (Sclose), make_number ('(')));
2951 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '[',
2952 Fcons (make_number (Sopen), make_number (']')));
2953 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ']',
2954 Fcons (make_number (Sclose), make_number ('[')));
2955 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '{',
2956 Fcons (make_number (Sopen), make_number ('}')));
2957 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '}',
2958 Fcons (make_number (Sclose), make_number ('{')));
2959 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '"',
2960 Fcons (make_number ((int) Sstring), Qnil));
2961 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\\',
2962 Fcons (make_number ((int) Sescape), Qnil));
2964 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Ssymbol];
2965 for (i = 0; i < 10; i++)
2967 c = "_-+*/&|<>="[i];
2968 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
2971 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Spunct];
2972 for (i = 0; i < 12; i++)
2974 c = ".,;:?!#@~^'`"[i];
2975 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
2978 /* All multibyte characters have syntax `word' by default. */
2979 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Sword];
2980 for (i = CHAR_TABLE_SINGLE_BYTE_SLOTS; i < CHAR_TABLE_ORDINARY_SLOTS; i++)
2981 XCHAR_TABLE (Vstandard_syntax_table)->contents[i] = temp;
2984 void
2985 syms_of_syntax ()
2987 Qsyntax_table_p = intern ("syntax-table-p");
2988 staticpro (&Qsyntax_table_p);
2990 staticpro (&Vsyntax_code_object);
2992 Qscan_error = intern ("scan-error");
2993 staticpro (&Qscan_error);
2994 Fput (Qscan_error, Qerror_conditions,
2995 Fcons (Qscan_error, Fcons (Qerror, Qnil)));
2996 Fput (Qscan_error, Qerror_message,
2997 build_string ("Scan error"));
2999 DEFVAR_BOOL ("parse-sexp-ignore-comments", &parse_sexp_ignore_comments,
3000 doc: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
3002 DEFVAR_BOOL ("parse-sexp-lookup-properties", &parse_sexp_lookup_properties,
3003 doc: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3004 Otherwise, that text property is simply ignored.
3005 See the info node `(elisp)Syntax Properties' for a description of the
3006 `syntax-table' property. */);
3008 words_include_escapes = 0;
3009 DEFVAR_BOOL ("words-include-escapes", &words_include_escapes,
3010 doc: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
3012 DEFVAR_BOOL ("multibyte-syntax-as-symbol", &multibyte_syntax_as_symbol,
3013 doc: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
3014 multibyte_syntax_as_symbol = 0;
3016 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3017 &open_paren_in_column_0_is_defun_start,
3018 doc: /* Non-nil means an open paren in column 0 denotes the start of a defun. */);
3019 open_paren_in_column_0_is_defun_start = 1;
3021 defsubr (&Ssyntax_table_p);
3022 defsubr (&Ssyntax_table);
3023 defsubr (&Sstandard_syntax_table);
3024 defsubr (&Scopy_syntax_table);
3025 defsubr (&Sset_syntax_table);
3026 defsubr (&Schar_syntax);
3027 defsubr (&Smatching_paren);
3028 defsubr (&Sstring_to_syntax);
3029 defsubr (&Smodify_syntax_entry);
3030 defsubr (&Sdescribe_syntax);
3032 defsubr (&Sforward_word);
3034 defsubr (&Sskip_chars_forward);
3035 defsubr (&Sskip_chars_backward);
3036 defsubr (&Sskip_syntax_forward);
3037 defsubr (&Sskip_syntax_backward);
3039 defsubr (&Sforward_comment);
3040 defsubr (&Sscan_lists);
3041 defsubr (&Sscan_sexps);
3042 defsubr (&Sbackward_prefix_chars);
3043 defsubr (&Sparse_partial_sexp);