Merge from emacs--rel--22
[emacs.git] / src / syntax.c
blobf282ed3a262eabb1b369d8b76f15cef3f5e66bd3
1 /* GNU Emacs routines to deal with syntax tables; also word and list parsing.
2 Copyright (C) 1985, 1987, 1993, 1994, 1995, 1997, 1998, 1999, 2001,
3 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
24 #include <config.h>
25 #include <ctype.h>
26 #include "lisp.h"
27 #include "commands.h"
28 #include "buffer.h"
29 #include "character.h"
30 #include "keymap.h"
31 #include "regex.h"
33 /* Make syntax table lookup grant data in gl_state. */
34 #define SYNTAX_ENTRY_VIA_PROPERTY
36 #include "syntax.h"
37 #include "intervals.h"
39 /* We use these constants in place for comment-style and
40 string-ender-char to distinguish comments/strings started by
41 comment_fence and string_fence codes. */
43 #define ST_COMMENT_STYLE (256 + 1)
44 #define ST_STRING_STYLE (256 + 2)
45 #include "category.h"
47 Lisp_Object Qsyntax_table_p, Qsyntax_table, Qscan_error;
49 int words_include_escapes;
50 int parse_sexp_lookup_properties;
52 /* Nonzero means `scan-sexps' treat all multibyte characters as symbol. */
53 int multibyte_syntax_as_symbol;
55 /* Used as a temporary in SYNTAX_ENTRY and other macros in syntax.h,
56 if not compiled with GCC. No need to mark it, since it is used
57 only very temporarily. */
58 Lisp_Object syntax_temp;
60 /* Non-zero means an open parenthesis in column 0 is always considered
61 to be the start of a defun. Zero means an open parenthesis in
62 column 0 has no special meaning. */
64 int open_paren_in_column_0_is_defun_start;
66 /* This is the internal form of the parse state used in parse-partial-sexp. */
68 struct lisp_parse_state
70 int depth; /* Depth at end of parsing. */
71 int instring; /* -1 if not within string, else desired terminator. */
72 int incomment; /* -1 if in unnestable comment else comment nesting */
73 int comstyle; /* comment style a=0, or b=1, or ST_COMMENT_STYLE. */
74 int quoted; /* Nonzero if just after an escape char at end of parsing */
75 int thislevelstart; /* Char number of most recent start-of-expression at current level */
76 int prevlevelstart; /* Char number of start of containing expression */
77 int location; /* Char number at which parsing stopped. */
78 int mindepth; /* Minimum depth seen while scanning. */
79 int comstr_start; /* Position just after last comment/string starter. */
80 Lisp_Object levelstarts; /* Char numbers of starts-of-expression
81 of levels (starting from outermost). */
84 /* These variables are a cache for finding the start of a defun.
85 find_start_pos is the place for which the defun start was found.
86 find_start_value is the defun start position found for it.
87 find_start_value_byte is the corresponding byte position.
88 find_start_buffer is the buffer it was found in.
89 find_start_begv is the BEGV value when it was found.
90 find_start_modiff is the value of MODIFF when it was found. */
92 static int find_start_pos;
93 static int find_start_value;
94 static int find_start_value_byte;
95 static struct buffer *find_start_buffer;
96 static int find_start_begv;
97 static int find_start_modiff;
100 static int find_defun_start P_ ((int, int));
101 static int back_comment P_ ((EMACS_INT, EMACS_INT, EMACS_INT, int, int,
102 EMACS_INT *, EMACS_INT *));
103 static int char_quoted P_ ((int, int));
104 static Lisp_Object skip_chars P_ ((int, Lisp_Object, Lisp_Object, int));
105 static Lisp_Object skip_syntaxes P_ ((int, Lisp_Object, Lisp_Object));
106 static Lisp_Object scan_lists P_ ((EMACS_INT, EMACS_INT, EMACS_INT, int));
107 static void scan_sexps_forward P_ ((struct lisp_parse_state *,
108 int, int, int, int,
109 int, Lisp_Object, int));
110 static int in_classes P_ ((int, Lisp_Object));
113 struct gl_state_s gl_state; /* Global state of syntax parser. */
115 INTERVAL interval_of ();
116 #define INTERVALS_AT_ONCE 10 /* 1 + max-number of intervals
117 to scan to property-change. */
119 /* Update gl_state to an appropriate interval which contains CHARPOS. The
120 sign of COUNT give the relative position of CHARPOS wrt the previously
121 valid interval. If INIT, only [be]_property fields of gl_state are
122 valid at start, the rest is filled basing on OBJECT.
124 `gl_state.*_i' are the intervals, and CHARPOS is further in the search
125 direction than the intervals - or in an interval. We update the
126 current syntax-table basing on the property of this interval, and
127 update the interval to start further than CHARPOS - or be
128 NULL_INTERVAL. We also update lim_property to be the next value of
129 charpos to call this subroutine again - or be before/after the
130 start/end of OBJECT. */
132 void
133 update_syntax_table (charpos, count, init, object)
134 int charpos, count, init;
135 Lisp_Object object;
137 Lisp_Object tmp_table;
138 int cnt = 0, invalidate = 1;
139 INTERVAL i;
141 if (init)
143 gl_state.old_prop = Qnil;
144 gl_state.start = gl_state.b_property;
145 gl_state.stop = gl_state.e_property;
146 i = interval_of (charpos, object);
147 gl_state.backward_i = gl_state.forward_i = i;
148 invalidate = 0;
149 if (NULL_INTERVAL_P (i))
150 return;
151 /* interval_of updates only ->position of the return value, so
152 update the parents manually to speed up update_interval. */
153 while (!NULL_PARENT (i))
155 if (AM_RIGHT_CHILD (i))
156 INTERVAL_PARENT (i)->position = i->position
157 - LEFT_TOTAL_LENGTH (i) + TOTAL_LENGTH (i) /* right end */
158 - TOTAL_LENGTH (INTERVAL_PARENT (i))
159 + LEFT_TOTAL_LENGTH (INTERVAL_PARENT (i));
160 else
161 INTERVAL_PARENT (i)->position = i->position - LEFT_TOTAL_LENGTH (i)
162 + TOTAL_LENGTH (i);
163 i = INTERVAL_PARENT (i);
165 i = gl_state.forward_i;
166 gl_state.b_property = i->position - gl_state.offset;
167 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
168 goto update;
170 i = count > 0 ? gl_state.forward_i : gl_state.backward_i;
172 /* We are guaranteed to be called with CHARPOS either in i,
173 or further off. */
174 if (NULL_INTERVAL_P (i))
175 error ("Error in syntax_table logic for to-the-end intervals");
176 else if (charpos < i->position) /* Move left. */
178 if (count > 0)
179 error ("Error in syntax_table logic for intervals <-");
180 /* Update the interval. */
181 i = update_interval (i, charpos);
182 if (INTERVAL_LAST_POS (i) != gl_state.b_property)
184 invalidate = 0;
185 gl_state.forward_i = i;
186 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
189 else if (charpos >= INTERVAL_LAST_POS (i)) /* Move right. */
191 if (count < 0)
192 error ("Error in syntax_table logic for intervals ->");
193 /* Update the interval. */
194 i = update_interval (i, charpos);
195 if (i->position != gl_state.e_property)
197 invalidate = 0;
198 gl_state.backward_i = i;
199 gl_state.b_property = i->position - gl_state.offset;
203 update:
204 tmp_table = textget (i->plist, Qsyntax_table);
206 if (invalidate)
207 invalidate = !EQ (tmp_table, gl_state.old_prop); /* Need to invalidate? */
209 if (invalidate) /* Did not get to adjacent interval. */
210 { /* with the same table => */
211 /* invalidate the old range. */
212 if (count > 0)
214 gl_state.backward_i = i;
215 gl_state.b_property = i->position - gl_state.offset;
217 else
219 gl_state.forward_i = i;
220 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
224 if (!EQ (tmp_table, gl_state.old_prop))
226 gl_state.current_syntax_table = tmp_table;
227 gl_state.old_prop = tmp_table;
228 if (EQ (Fsyntax_table_p (tmp_table), Qt))
230 gl_state.use_global = 0;
232 else if (CONSP (tmp_table))
234 gl_state.use_global = 1;
235 gl_state.global_code = tmp_table;
237 else
239 gl_state.use_global = 0;
240 gl_state.current_syntax_table = current_buffer->syntax_table;
244 while (!NULL_INTERVAL_P (i))
246 if (cnt && !EQ (tmp_table, textget (i->plist, Qsyntax_table)))
248 if (count > 0)
250 gl_state.e_property = i->position - gl_state.offset;
251 gl_state.forward_i = i;
253 else
255 gl_state.b_property
256 = i->position + LENGTH (i) - gl_state.offset;
257 gl_state.backward_i = i;
259 return;
261 else if (cnt == INTERVALS_AT_ONCE)
263 if (count > 0)
265 gl_state.e_property
266 = i->position + LENGTH (i) - gl_state.offset
267 /* e_property at EOB is not set to ZV but to ZV+1, so that
268 we can do INC(from);UPDATE_SYNTAX_TABLE_FORWARD without
269 having to check eob between the two. */
270 + (NULL_INTERVAL_P (next_interval (i)) ? 1 : 0);
271 gl_state.forward_i = i;
273 else
275 gl_state.b_property = i->position - gl_state.offset;
276 gl_state.backward_i = i;
278 return;
280 cnt++;
281 i = count > 0 ? next_interval (i) : previous_interval (i);
283 eassert (NULL_INTERVAL_P (i)); /* This property goes to the end. */
284 if (count > 0)
285 gl_state.e_property = gl_state.stop;
286 else
287 gl_state.b_property = gl_state.start;
290 /* Returns TRUE if char at CHARPOS is quoted.
291 Global syntax-table data should be set up already to be good at CHARPOS
292 or after. On return global syntax data is good for lookup at CHARPOS. */
294 static int
295 char_quoted (charpos, bytepos)
296 register int charpos, bytepos;
298 register enum syntaxcode code;
299 register int beg = BEGV;
300 register int quoted = 0;
301 int orig = charpos;
303 DEC_BOTH (charpos, bytepos);
305 while (charpos >= beg)
307 int c;
309 UPDATE_SYNTAX_TABLE_BACKWARD (charpos);
310 c = FETCH_CHAR_AS_MULTIBYTE (bytepos);
311 code = SYNTAX (c);
312 if (! (code == Scharquote || code == Sescape))
313 break;
315 DEC_BOTH (charpos, bytepos);
316 quoted = !quoted;
319 UPDATE_SYNTAX_TABLE (orig);
320 return quoted;
323 /* Return the bytepos one character after BYTEPOS.
324 We assume that BYTEPOS is not at the end of the buffer. */
326 INLINE int
327 inc_bytepos (bytepos)
328 int bytepos;
330 if (NILP (current_buffer->enable_multibyte_characters))
331 return bytepos + 1;
333 INC_POS (bytepos);
334 return bytepos;
337 /* Return the bytepos one character before BYTEPOS.
338 We assume that BYTEPOS is not at the start of the buffer. */
340 INLINE int
341 dec_bytepos (bytepos)
342 int bytepos;
344 if (NILP (current_buffer->enable_multibyte_characters))
345 return bytepos - 1;
347 DEC_POS (bytepos);
348 return bytepos;
351 /* Return a defun-start position before before POS and not too far before.
352 It should be the last one before POS, or nearly the last.
354 When open_paren_in_column_0_is_defun_start is nonzero,
355 only the beginning of the buffer is treated as a defun-start.
357 We record the information about where the scan started
358 and what its result was, so that another call in the same area
359 can return the same value very quickly.
361 There is no promise at which position the global syntax data is
362 valid on return from the subroutine, so the caller should explicitly
363 update the global data. */
365 static int
366 find_defun_start (pos, pos_byte)
367 int pos, pos_byte;
369 int opoint = PT, opoint_byte = PT_BYTE;
371 if (!open_paren_in_column_0_is_defun_start)
373 find_start_value_byte = BEGV_BYTE;
374 return BEGV;
377 /* Use previous finding, if it's valid and applies to this inquiry. */
378 if (current_buffer == find_start_buffer
379 /* Reuse the defun-start even if POS is a little farther on.
380 POS might be in the next defun, but that's ok.
381 Our value may not be the best possible, but will still be usable. */
382 && pos <= find_start_pos + 1000
383 && pos >= find_start_value
384 && BEGV == find_start_begv
385 && MODIFF == find_start_modiff)
386 return find_start_value;
388 /* Back up to start of line. */
389 scan_newline (pos, pos_byte, BEGV, BEGV_BYTE, -1, 1);
391 /* We optimize syntax-table lookup for rare updates. Thus we accept
392 only those `^\s(' which are good in global _and_ text-property
393 syntax-tables. */
394 gl_state.current_syntax_table = current_buffer->syntax_table;
395 gl_state.use_global = 0;
396 while (PT > BEGV)
398 int c;
400 /* Open-paren at start of line means we may have found our
401 defun-start. */
402 c = FETCH_CHAR_AS_MULTIBYTE (PT_BYTE);
403 if (SYNTAX (c) == Sopen)
405 SETUP_SYNTAX_TABLE (PT + 1, -1); /* Try again... */
406 c = FETCH_CHAR_AS_MULTIBYTE (PT_BYTE);
407 if (SYNTAX (c) == Sopen)
408 break;
409 /* Now fallback to the default value. */
410 gl_state.current_syntax_table = current_buffer->syntax_table;
411 gl_state.use_global = 0;
413 /* Move to beg of previous line. */
414 scan_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, -2, 1);
417 /* Record what we found, for the next try. */
418 find_start_value = PT;
419 find_start_value_byte = PT_BYTE;
420 find_start_buffer = current_buffer;
421 find_start_modiff = MODIFF;
422 find_start_begv = BEGV;
423 find_start_pos = pos;
425 TEMP_SET_PT_BOTH (opoint, opoint_byte);
427 return find_start_value;
430 /* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
432 static int
433 prev_char_comend_first (pos, pos_byte)
434 int pos, pos_byte;
436 int c, val;
438 DEC_BOTH (pos, pos_byte);
439 UPDATE_SYNTAX_TABLE_BACKWARD (pos);
440 c = FETCH_CHAR (pos_byte);
441 val = SYNTAX_COMEND_FIRST (c);
442 UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
443 return val;
446 /* Return the SYNTAX_COMSTART_FIRST of the character before POS, POS_BYTE. */
448 /* static int
449 * prev_char_comstart_first (pos, pos_byte)
450 * int pos, pos_byte;
452 * int c, val;
454 * DEC_BOTH (pos, pos_byte);
455 * UPDATE_SYNTAX_TABLE_BACKWARD (pos);
456 * c = FETCH_CHAR (pos_byte);
457 * val = SYNTAX_COMSTART_FIRST (c);
458 * UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
459 * return val;
460 * } */
462 /* Checks whether charpos FROM is at the end of a comment.
463 FROM_BYTE is the bytepos corresponding to FROM.
464 Do not move back before STOP.
466 Return a positive value if we find a comment ending at FROM/FROM_BYTE;
467 return -1 otherwise.
469 If successful, store the charpos of the comment's beginning
470 into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR.
472 Global syntax data remains valid for backward search starting at
473 the returned value (or at FROM, if the search was not successful). */
475 static int
476 back_comment (from, from_byte, stop, comnested, comstyle, charpos_ptr, bytepos_ptr)
477 EMACS_INT from, from_byte, stop;
478 int comnested, comstyle;
479 EMACS_INT *charpos_ptr, *bytepos_ptr;
481 /* Look back, counting the parity of string-quotes,
482 and recording the comment-starters seen.
483 When we reach a safe place, assume that's not in a string;
484 then step the main scan to the earliest comment-starter seen
485 an even number of string quotes away from the safe place.
487 OFROM[I] is position of the earliest comment-starter seen
488 which is I+2X quotes from the comment-end.
489 PARITY is current parity of quotes from the comment end. */
490 int string_style = -1; /* Presumed outside of any string. */
491 int string_lossage = 0;
492 /* Not a real lossage: indicates that we have passed a matching comment
493 starter plus a non-matching comment-ender, meaning that any matching
494 comment-starter we might see later could be a false positive (hidden
495 inside another comment).
496 Test case: { a (* b } c (* d *) */
497 int comment_lossage = 0;
498 int comment_end = from;
499 int comment_end_byte = from_byte;
500 int comstart_pos = 0;
501 int comstart_byte;
502 /* Place where the containing defun starts,
503 or 0 if we didn't come across it yet. */
504 int defun_start = 0;
505 int defun_start_byte = 0;
506 register enum syntaxcode code;
507 int nesting = 1; /* current comment nesting */
508 int c;
509 int syntax = 0;
511 /* FIXME: A }} comment-ender style leads to incorrect behavior
512 in the case of {{ c }}} because we ignore the last two chars which are
513 assumed to be comment-enders although they aren't. */
515 /* At beginning of range to scan, we're outside of strings;
516 that determines quote parity to the comment-end. */
517 while (from != stop)
519 int temp_byte, prev_syntax;
520 int com2start, com2end;
522 /* Move back and examine a character. */
523 DEC_BOTH (from, from_byte);
524 UPDATE_SYNTAX_TABLE_BACKWARD (from);
526 prev_syntax = syntax;
527 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
528 syntax = SYNTAX_WITH_FLAGS (c);
529 code = SYNTAX (c);
531 /* Check for 2-char comment markers. */
532 com2start = (SYNTAX_FLAGS_COMSTART_FIRST (syntax)
533 && SYNTAX_FLAGS_COMSTART_SECOND (prev_syntax)
534 && comstyle == SYNTAX_FLAGS_COMMENT_STYLE (prev_syntax)
535 && (SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax)
536 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested);
537 com2end = (SYNTAX_FLAGS_COMEND_FIRST (syntax)
538 && SYNTAX_FLAGS_COMEND_SECOND (prev_syntax));
540 /* Nasty cases with overlapping 2-char comment markers:
541 - snmp-mode: -- c -- foo -- c --
542 --- c --
543 ------ c --
544 - c-mode: *||*
545 |* *|* *|
546 |*| |* |*|
547 /// */
549 /* If a 2-char comment sequence partly overlaps with another,
550 we don't try to be clever. */
551 if (from > stop && (com2end || com2start))
553 int next = from, next_byte = from_byte, next_c, next_syntax;
554 DEC_BOTH (next, next_byte);
555 UPDATE_SYNTAX_TABLE_BACKWARD (next);
556 next_c = FETCH_CHAR_AS_MULTIBYTE (next_byte);
557 next_syntax = SYNTAX_WITH_FLAGS (next_c);
558 if (((com2start || comnested)
559 && SYNTAX_FLAGS_COMEND_SECOND (syntax)
560 && SYNTAX_FLAGS_COMEND_FIRST (next_syntax))
561 || ((com2end || comnested)
562 && SYNTAX_FLAGS_COMSTART_SECOND (syntax)
563 && comstyle == SYNTAX_FLAGS_COMMENT_STYLE (syntax)
564 && SYNTAX_FLAGS_COMSTART_FIRST (next_syntax)))
565 goto lossage;
566 /* UPDATE_SYNTAX_TABLE_FORWARD (next + 1); */
569 if (com2start && comstart_pos == 0)
570 /* We're looking at a comment starter. But it might be a comment
571 ender as well (see snmp-mode). The first time we see one, we
572 need to consider it as a comment starter,
573 and the subsequent times as a comment ender. */
574 com2end = 0;
576 /* Turn a 2-char comment sequences into the appropriate syntax. */
577 if (com2end)
578 code = Sendcomment;
579 else if (com2start)
580 code = Scomment;
581 /* Ignore comment starters of a different style. */
582 else if (code == Scomment
583 && (comstyle != SYNTAX_FLAGS_COMMENT_STYLE (syntax)
584 || SYNTAX_FLAGS_COMMENT_NESTED (syntax) != comnested))
585 continue;
587 /* Ignore escaped characters, except comment-enders. */
588 if (code != Sendcomment && char_quoted (from, from_byte))
589 continue;
591 switch (code)
593 case Sstring_fence:
594 case Scomment_fence:
595 c = (code == Sstring_fence ? ST_STRING_STYLE : ST_COMMENT_STYLE);
596 case Sstring:
597 /* Track parity of quotes. */
598 if (string_style == -1)
599 /* Entering a string. */
600 string_style = c;
601 else if (string_style == c)
602 /* Leaving the string. */
603 string_style = -1;
604 else
605 /* If we have two kinds of string delimiters.
606 There's no way to grok this scanning backwards. */
607 string_lossage = 1;
608 break;
610 case Scomment:
611 /* We've already checked that it is the relevant comstyle. */
612 if (string_style != -1 || comment_lossage || string_lossage)
613 /* There are odd string quotes involved, so let's be careful.
614 Test case in Pascal: " { " a { " } */
615 goto lossage;
617 if (!comnested)
619 /* Record best comment-starter so far. */
620 comstart_pos = from;
621 comstart_byte = from_byte;
623 else if (--nesting <= 0)
624 /* nested comments have to be balanced, so we don't need to
625 keep looking for earlier ones. We use here the same (slightly
626 incorrect) reasoning as below: since it is followed by uniform
627 paired string quotes, this comment-start has to be outside of
628 strings, else the comment-end itself would be inside a string. */
629 goto done;
630 break;
632 case Sendcomment:
633 if (SYNTAX_FLAGS_COMMENT_STYLE (syntax) == comstyle
634 && ((com2end && SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax))
635 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested)
636 /* This is the same style of comment ender as ours. */
638 if (comnested)
639 nesting++;
640 else
641 /* Anything before that can't count because it would match
642 this comment-ender rather than ours. */
643 from = stop; /* Break out of the loop. */
645 else if (comstart_pos != 0 || c != '\n')
646 /* We're mixing comment styles here, so we'd better be careful.
647 The (comstart_pos != 0 || c != '\n') check is not quite correct
648 (we should just always set comment_lossage), but removing it
649 would imply that any multiline comment in C would go through
650 lossage, which seems overkill.
651 The failure should only happen in the rare cases such as
652 { (* } *) */
653 comment_lossage = 1;
654 break;
656 case Sopen:
657 /* Assume a defun-start point is outside of strings. */
658 if (open_paren_in_column_0_is_defun_start
659 && (from == stop
660 || (temp_byte = dec_bytepos (from_byte),
661 FETCH_CHAR (temp_byte) == '\n')))
663 defun_start = from;
664 defun_start_byte = from_byte;
665 from = stop; /* Break out of the loop. */
667 break;
669 default:
670 break;
674 if (comstart_pos == 0)
676 from = comment_end;
677 from_byte = comment_end_byte;
678 UPDATE_SYNTAX_TABLE_FORWARD (comment_end - 1);
680 /* If comstart_pos is set and we get here (ie. didn't jump to `lossage'
681 or `done'), then we've found the beginning of the non-nested comment. */
682 else if (1) /* !comnested */
684 from = comstart_pos;
685 from_byte = comstart_byte;
686 UPDATE_SYNTAX_TABLE_FORWARD (from - 1);
688 else
690 struct lisp_parse_state state;
691 lossage:
692 /* We had two kinds of string delimiters mixed up
693 together. Decode this going forwards.
694 Scan fwd from a known safe place (beginning-of-defun)
695 to the one in question; this records where we
696 last passed a comment starter. */
697 /* If we did not already find the defun start, find it now. */
698 if (defun_start == 0)
700 defun_start = find_defun_start (comment_end, comment_end_byte);
701 defun_start_byte = find_start_value_byte;
705 scan_sexps_forward (&state,
706 defun_start, defun_start_byte,
707 comment_end, -10000, 0, Qnil, 0);
708 defun_start = comment_end;
709 if (state.incomment == (comnested ? 1 : -1)
710 && state.comstyle == comstyle)
711 from = state.comstr_start;
712 else
714 from = comment_end;
715 if (state.incomment)
716 /* If comment_end is inside some other comment, maybe ours
717 is nested, so we need to try again from within the
718 surrounding comment. Example: { a (* " *) */
720 /* FIXME: We should advance by one or two chars. */
721 defun_start = state.comstr_start + 2;
722 defun_start_byte = CHAR_TO_BYTE (defun_start);
725 } while (defun_start < comment_end);
727 from_byte = CHAR_TO_BYTE (from);
728 UPDATE_SYNTAX_TABLE_FORWARD (from - 1);
731 done:
732 *charpos_ptr = from;
733 *bytepos_ptr = from_byte;
735 return (from == comment_end) ? -1 : from;
738 DEFUN ("syntax-table-p", Fsyntax_table_p, Ssyntax_table_p, 1, 1, 0,
739 doc: /* Return t if OBJECT is a syntax table.
740 Currently, any char-table counts as a syntax table. */)
741 (object)
742 Lisp_Object object;
744 if (CHAR_TABLE_P (object)
745 && EQ (XCHAR_TABLE (object)->purpose, Qsyntax_table))
746 return Qt;
747 return Qnil;
750 static void
751 check_syntax_table (obj)
752 Lisp_Object obj;
754 CHECK_TYPE (CHAR_TABLE_P (obj) && EQ (XCHAR_TABLE (obj)->purpose, Qsyntax_table),
755 Qsyntax_table_p, obj);
758 DEFUN ("syntax-table", Fsyntax_table, Ssyntax_table, 0, 0, 0,
759 doc: /* Return the current syntax table.
760 This is the one specified by the current buffer. */)
763 return current_buffer->syntax_table;
766 DEFUN ("standard-syntax-table", Fstandard_syntax_table,
767 Sstandard_syntax_table, 0, 0, 0,
768 doc: /* Return the standard syntax table.
769 This is the one used for new buffers. */)
772 return Vstandard_syntax_table;
775 DEFUN ("copy-syntax-table", Fcopy_syntax_table, Scopy_syntax_table, 0, 1, 0,
776 doc: /* Construct a new syntax table and return it.
777 It is a copy of the TABLE, which defaults to the standard syntax table. */)
778 (table)
779 Lisp_Object table;
781 Lisp_Object copy;
783 if (!NILP (table))
784 check_syntax_table (table);
785 else
786 table = Vstandard_syntax_table;
788 copy = Fcopy_sequence (table);
790 /* Only the standard syntax table should have a default element.
791 Other syntax tables should inherit from parents instead. */
792 XCHAR_TABLE (copy)->defalt = Qnil;
794 /* Copied syntax tables should all have parents.
795 If we copied one with no parent, such as the standard syntax table,
796 use the standard syntax table as the copy's parent. */
797 if (NILP (XCHAR_TABLE (copy)->parent))
798 Fset_char_table_parent (copy, Vstandard_syntax_table);
799 return copy;
802 DEFUN ("set-syntax-table", Fset_syntax_table, Sset_syntax_table, 1, 1, 0,
803 doc: /* Select a new syntax table for the current buffer.
804 One argument, a syntax table. */)
805 (table)
806 Lisp_Object table;
808 int idx;
809 check_syntax_table (table);
810 current_buffer->syntax_table = table;
811 /* Indicate that this buffer now has a specified syntax table. */
812 idx = PER_BUFFER_VAR_IDX (syntax_table);
813 SET_PER_BUFFER_VALUE_P (current_buffer, idx, 1);
814 return table;
817 /* Convert a letter which signifies a syntax code
818 into the code it signifies.
819 This is used by modify-syntax-entry, and other things. */
821 unsigned char syntax_spec_code[0400] =
822 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
823 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
824 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
825 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
826 (char) Swhitespace, (char) Scomment_fence, (char) Sstring, 0377,
827 (char) Smath, 0377, 0377, (char) Squote,
828 (char) Sopen, (char) Sclose, 0377, 0377,
829 0377, (char) Swhitespace, (char) Spunct, (char) Scharquote,
830 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
831 0377, 0377, 0377, 0377,
832 (char) Scomment, 0377, (char) Sendcomment, 0377,
833 (char) Sinherit, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
834 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
835 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword,
836 0377, 0377, 0377, 0377, (char) Sescape, 0377, 0377, (char) Ssymbol,
837 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
838 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
839 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword,
840 0377, 0377, 0377, 0377, (char) Sstring_fence, 0377, 0377, 0377
843 /* Indexed by syntax code, give the letter that describes it. */
845 char syntax_code_spec[16] =
847 ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@',
848 '!', '|'
851 /* Indexed by syntax code, give the object (cons of syntax code and
852 nil) to be stored in syntax table. Since these objects can be
853 shared among syntax tables, we generate them in advance. By
854 sharing objects, the function `describe-syntax' can give a more
855 compact listing. */
856 static Lisp_Object Vsyntax_code_object;
859 DEFUN ("char-syntax", Fchar_syntax, Schar_syntax, 1, 1, 0,
860 doc: /* Return the syntax code of CHARACTER, described by a character.
861 For example, if CHARACTER is a word constituent,
862 the character `w' is returned.
863 The characters that correspond to various syntax codes
864 are listed in the documentation of `modify-syntax-entry'. */)
865 (character)
866 Lisp_Object character;
868 int char_int;
869 gl_state.current_syntax_table = current_buffer->syntax_table;
871 gl_state.use_global = 0;
872 CHECK_NUMBER (character);
873 char_int = XINT (character);
874 return make_number (syntax_code_spec[(int) SYNTAX (char_int)]);
877 DEFUN ("matching-paren", Fmatching_paren, Smatching_paren, 1, 1, 0,
878 doc: /* Return the matching parenthesis of CHARACTER, or nil if none. */)
879 (character)
880 Lisp_Object character;
882 int char_int, code;
883 gl_state.current_syntax_table = current_buffer->syntax_table;
884 gl_state.use_global = 0;
885 CHECK_NUMBER (character);
886 char_int = XINT (character);
887 code = SYNTAX (char_int);
888 if (code == Sopen || code == Sclose)
889 return SYNTAX_MATCH (char_int);
890 return Qnil;
893 DEFUN ("string-to-syntax", Fstring_to_syntax, Sstring_to_syntax, 1, 1, 0,
894 doc: /* Convert a syntax specification STRING into syntax cell form.
895 STRING should be a string as it is allowed as argument of
896 `modify-syntax-entry'. Value is the equivalent cons cell
897 \(CODE . MATCHING-CHAR) that can be used as value of a `syntax-table'
898 text property. */)
899 (string)
900 Lisp_Object string;
902 register const unsigned char *p;
903 register enum syntaxcode code;
904 int val;
905 Lisp_Object match;
907 CHECK_STRING (string);
909 p = SDATA (string);
910 code = (enum syntaxcode) syntax_spec_code[*p++];
911 if (((int) code & 0377) == 0377)
912 error ("Invalid syntax description letter: %c", p[-1]);
914 if (code == Sinherit)
915 return Qnil;
917 if (*p)
919 int len;
920 int character = (STRING_CHAR_AND_LENGTH
921 (p, SBYTES (string) - 1, len));
922 XSETINT (match, character);
923 if (XFASTINT (match) == ' ')
924 match = Qnil;
925 p += len;
927 else
928 match = Qnil;
930 val = (int) code;
931 while (*p)
932 switch (*p++)
934 case '1':
935 val |= 1 << 16;
936 break;
938 case '2':
939 val |= 1 << 17;
940 break;
942 case '3':
943 val |= 1 << 18;
944 break;
946 case '4':
947 val |= 1 << 19;
948 break;
950 case 'p':
951 val |= 1 << 20;
952 break;
954 case 'b':
955 val |= 1 << 21;
956 break;
958 case 'n':
959 val |= 1 << 22;
960 break;
963 if (val < XVECTOR (Vsyntax_code_object)->size && NILP (match))
964 return XVECTOR (Vsyntax_code_object)->contents[val];
965 else
966 /* Since we can't use a shared object, let's make a new one. */
967 return Fcons (make_number (val), match);
970 /* I really don't know why this is interactive
971 help-form should at least be made useful whilst reading the second arg. */
972 DEFUN ("modify-syntax-entry", Fmodify_syntax_entry, Smodify_syntax_entry, 2, 3,
973 "cSet syntax for character: \nsSet syntax for %s to: ",
974 doc: /* Set syntax for character CHAR according to string NEWENTRY.
975 The syntax is changed only for table SYNTAX-TABLE, which defaults to
976 the current buffer's syntax table.
977 CHAR may be a cons (MIN . MAX), in which case, syntaxes of all characters
978 in the range MIN and MAX are changed.
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 if (CONSP (c))
1017 CHECK_CHARACTER_CAR (c);
1018 CHECK_CHARACTER_CDR (c);
1020 else
1021 CHECK_CHARACTER (c);
1023 if (NILP (syntax_table))
1024 syntax_table = current_buffer->syntax_table;
1025 else
1026 check_syntax_table (syntax_table);
1028 newentry = Fstring_to_syntax (newentry);
1029 if (CONSP (c))
1030 SET_RAW_SYNTAX_ENTRY_RANGE (syntax_table, c, newentry);
1031 else
1032 SET_RAW_SYNTAX_ENTRY (syntax_table, XINT (c), newentry);
1034 /* We clear the regexp cache, since character classes can now have
1035 different values from those in the compiled regexps.*/
1036 clear_regexp_cache ();
1038 return Qnil;
1041 /* Dump syntax table to buffer in human-readable format */
1043 DEFUN ("internal-describe-syntax-value", Finternal_describe_syntax_value,
1044 Sinternal_describe_syntax_value, 1, 1, 0,
1045 doc: /* Insert a description of the internal syntax description SYNTAX at point. */)
1046 (syntax)
1047 Lisp_Object syntax;
1049 register enum syntaxcode code;
1050 char desc, start1, start2, end1, end2, prefix, comstyle, comnested;
1051 char str[2];
1052 Lisp_Object first, match_lisp, value = syntax;
1054 if (NILP (value))
1056 insert_string ("default");
1057 return syntax;
1060 if (CHAR_TABLE_P (value))
1062 insert_string ("deeper char-table ...");
1063 return syntax;
1066 if (!CONSP (value))
1068 insert_string ("invalid");
1069 return syntax;
1072 first = XCAR (value);
1073 match_lisp = XCDR (value);
1075 if (!INTEGERP (first) || !(NILP (match_lisp) || INTEGERP (match_lisp)))
1077 insert_string ("invalid");
1078 return syntax;
1081 code = (enum syntaxcode) (XINT (first) & 0377);
1082 start1 = (XINT (first) >> 16) & 1;
1083 start2 = (XINT (first) >> 17) & 1;
1084 end1 = (XINT (first) >> 18) & 1;
1085 end2 = (XINT (first) >> 19) & 1;
1086 prefix = (XINT (first) >> 20) & 1;
1087 comstyle = (XINT (first) >> 21) & 1;
1088 comnested = (XINT (first) >> 22) & 1;
1090 if ((int) code < 0 || (int) code >= (int) Smax)
1092 insert_string ("invalid");
1093 return syntax;
1095 desc = syntax_code_spec[(int) code];
1097 str[0] = desc, str[1] = 0;
1098 insert (str, 1);
1100 if (NILP (match_lisp))
1101 insert (" ", 1);
1102 else
1103 insert_char (XINT (match_lisp));
1105 if (start1)
1106 insert ("1", 1);
1107 if (start2)
1108 insert ("2", 1);
1110 if (end1)
1111 insert ("3", 1);
1112 if (end2)
1113 insert ("4", 1);
1115 if (prefix)
1116 insert ("p", 1);
1117 if (comstyle)
1118 insert ("b", 1);
1119 if (comnested)
1120 insert ("n", 1);
1122 insert_string ("\twhich means: ");
1124 switch (SWITCH_ENUM_CAST (code))
1126 case Swhitespace:
1127 insert_string ("whitespace"); break;
1128 case Spunct:
1129 insert_string ("punctuation"); break;
1130 case Sword:
1131 insert_string ("word"); break;
1132 case Ssymbol:
1133 insert_string ("symbol"); break;
1134 case Sopen:
1135 insert_string ("open"); break;
1136 case Sclose:
1137 insert_string ("close"); break;
1138 case Squote:
1139 insert_string ("prefix"); break;
1140 case Sstring:
1141 insert_string ("string"); break;
1142 case Smath:
1143 insert_string ("math"); break;
1144 case Sescape:
1145 insert_string ("escape"); break;
1146 case Scharquote:
1147 insert_string ("charquote"); break;
1148 case Scomment:
1149 insert_string ("comment"); break;
1150 case Sendcomment:
1151 insert_string ("endcomment"); break;
1152 case Sinherit:
1153 insert_string ("inherit"); break;
1154 case Scomment_fence:
1155 insert_string ("comment fence"); break;
1156 case Sstring_fence:
1157 insert_string ("string fence"); break;
1158 default:
1159 insert_string ("invalid");
1160 return syntax;
1163 if (!NILP (match_lisp))
1165 insert_string (", matches ");
1166 insert_char (XINT (match_lisp));
1169 if (start1)
1170 insert_string (",\n\t is the first character of a comment-start sequence");
1171 if (start2)
1172 insert_string (",\n\t is the second character of a comment-start sequence");
1174 if (end1)
1175 insert_string (",\n\t is the first character of a comment-end sequence");
1176 if (end2)
1177 insert_string (",\n\t is the second character of a comment-end sequence");
1178 if (comstyle)
1179 insert_string (" (comment style b)");
1180 if (comnested)
1181 insert_string (" (nestable)");
1183 if (prefix)
1184 insert_string (",\n\t is a prefix character for `backward-prefix-chars'");
1186 return syntax;
1189 int parse_sexp_ignore_comments;
1191 /* Char-table of functions that find the next or previous word
1192 boundary. */
1193 Lisp_Object Vfind_word_boundary_function_table;
1195 /* Return the position across COUNT words from FROM.
1196 If that many words cannot be found before the end of the buffer, return 0.
1197 COUNT negative means scan backward and stop at word beginning. */
1200 scan_words (from, count)
1201 register int from, count;
1203 register int beg = BEGV;
1204 register int end = ZV;
1205 register int from_byte = CHAR_TO_BYTE (from);
1206 register enum syntaxcode code;
1207 int ch0, ch1;
1208 Lisp_Object func, script, pos;
1210 immediate_quit = 1;
1211 QUIT;
1213 SETUP_SYNTAX_TABLE (from, count);
1215 while (count > 0)
1217 while (1)
1219 if (from == end)
1221 immediate_quit = 0;
1222 return 0;
1224 UPDATE_SYNTAX_TABLE_FORWARD (from);
1225 ch0 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1226 code = SYNTAX (ch0);
1227 INC_BOTH (from, from_byte);
1228 if (words_include_escapes
1229 && (code == Sescape || code == Scharquote))
1230 break;
1231 if (code == Sword)
1232 break;
1234 /* Now CH0 is a character which begins a word and FROM is the
1235 position of the next character. */
1236 func = CHAR_TABLE_REF (Vfind_word_boundary_function_table, ch0);
1237 if (! NILP (Ffboundp (func)))
1239 pos = call2 (func, make_number (from - 1), make_number (end));
1240 if (INTEGERP (pos) && XINT (pos) > from)
1242 from = XINT (pos);
1243 from_byte = CHAR_TO_BYTE (from);
1246 else
1248 script = CHAR_TABLE_REF (Vchar_script_table, ch0);
1249 while (1)
1251 if (from == end) break;
1252 UPDATE_SYNTAX_TABLE_FORWARD (from);
1253 ch1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1254 code = SYNTAX (ch1);
1255 if ((code != Sword
1256 && (! words_include_escapes
1257 || (code != Sescape && code != Scharquote)))
1258 || ! EQ (CHAR_TABLE_REF (Vchar_script_table, ch1), script))
1259 break;
1260 INC_BOTH (from, from_byte);
1261 ch0 = ch1;
1264 count--;
1266 while (count < 0)
1268 while (1)
1270 if (from == beg)
1272 immediate_quit = 0;
1273 return 0;
1275 DEC_BOTH (from, from_byte);
1276 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1277 ch1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1278 code = SYNTAX (ch1);
1279 if (words_include_escapes
1280 && (code == Sescape || code == Scharquote))
1281 break;
1282 if (code == Sword)
1283 break;
1285 /* Now CH1 is a character which ends a word and FROM is the
1286 position of it. */
1287 func = CHAR_TABLE_REF (Vfind_word_boundary_function_table, ch1);
1288 if (! NILP (Ffboundp (func)))
1290 pos = call2 (func, make_number (from), make_number (beg));
1291 if (INTEGERP (pos) && XINT (pos) < from)
1293 from = XINT (pos);
1294 from_byte = CHAR_TO_BYTE (from);
1297 else
1299 script = CHAR_TABLE_REF (Vchar_script_table, ch1);
1300 while (1)
1302 if (from == beg)
1303 break;
1304 DEC_BOTH (from, from_byte);
1305 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1306 ch0 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1307 code = SYNTAX (ch0);
1308 if ((code != Sword
1309 && (! words_include_escapes
1310 || (code != Sescape && code != Scharquote)))
1311 || ! EQ (CHAR_TABLE_REF (Vchar_script_table, ch0), script))
1313 INC_BOTH (from, from_byte);
1314 break;
1316 ch1 = ch0;
1319 count++;
1322 immediate_quit = 0;
1324 return from;
1327 DEFUN ("forward-word", Fforward_word, Sforward_word, 0, 1, "^p",
1328 doc: /* Move point forward ARG words (backward if ARG is negative).
1329 Normally returns t.
1330 If an edge of the buffer or a field boundary is reached, point is left there
1331 and the function returns nil. Field boundaries are not noticed if
1332 `inhibit-field-text-motion' is non-nil. */)
1333 (arg)
1334 Lisp_Object arg;
1336 Lisp_Object tmp;
1337 int orig_val, val;
1339 if (NILP (arg))
1340 XSETFASTINT (arg, 1);
1341 else
1342 CHECK_NUMBER (arg);
1344 val = orig_val = scan_words (PT, XINT (arg));
1345 if (! orig_val)
1346 val = XINT (arg) > 0 ? ZV : BEGV;
1348 /* Avoid jumping out of an input field. */
1349 tmp = Fconstrain_to_field (make_number (val), make_number (PT),
1350 Qt, Qnil, Qnil);
1351 val = XFASTINT (tmp);
1353 SET_PT (val);
1354 return val == orig_val ? Qt : Qnil;
1357 Lisp_Object skip_chars ();
1359 DEFUN ("skip-chars-forward", Fskip_chars_forward, Sskip_chars_forward, 1, 2, 0,
1360 doc: /* Move point forward, stopping before a char not in STRING, or at pos LIM.
1361 STRING is like the inside of a `[...]' in a regular expression
1362 except that `]' is never special and `\\' quotes `^', `-' or `\\'
1363 (but not as the end of a range; quoting is never needed there).
1364 Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
1365 With arg "^a-zA-Z", skips nonletters stopping before first letter.
1366 Char classes, e.g. `[:alpha:]', are supported.
1368 Returns the distance traveled, either zero or positive. */)
1369 (string, lim)
1370 Lisp_Object string, lim;
1372 return skip_chars (1, string, lim, 1);
1375 DEFUN ("skip-chars-backward", Fskip_chars_backward, Sskip_chars_backward, 1, 2, 0,
1376 doc: /* Move point backward, stopping after a char not in STRING, or at pos LIM.
1377 See `skip-chars-forward' for details.
1378 Returns the distance traveled, either zero or negative. */)
1379 (string, lim)
1380 Lisp_Object string, lim;
1382 return skip_chars (0, string, lim, 1);
1385 DEFUN ("skip-syntax-forward", Fskip_syntax_forward, Sskip_syntax_forward, 1, 2, 0,
1386 doc: /* Move point forward across chars in specified syntax classes.
1387 SYNTAX is a string of syntax code characters.
1388 Stop before a char whose syntax is not in SYNTAX, or at position LIM.
1389 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1390 This function returns the distance traveled, either zero or positive. */)
1391 (syntax, lim)
1392 Lisp_Object syntax, lim;
1394 return skip_syntaxes (1, syntax, lim);
1397 DEFUN ("skip-syntax-backward", Fskip_syntax_backward, Sskip_syntax_backward, 1, 2, 0,
1398 doc: /* Move point backward across chars in specified syntax classes.
1399 SYNTAX is a string of syntax code characters.
1400 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.
1401 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1402 This function returns the distance traveled, either zero or negative. */)
1403 (syntax, lim)
1404 Lisp_Object syntax, lim;
1406 return skip_syntaxes (0, syntax, lim);
1409 static Lisp_Object
1410 skip_chars (forwardp, string, lim, handle_iso_classes)
1411 int forwardp;
1412 Lisp_Object string, lim;
1413 int handle_iso_classes;
1415 register unsigned int c;
1416 unsigned char fastmap[0400];
1417 /* Store the ranges of non-ASCII characters. */
1418 int *char_ranges;
1419 int n_char_ranges = 0;
1420 int negate = 0;
1421 register int i, i_byte;
1422 /* Set to 1 if the current buffer is multibyte and the region
1423 contains non-ASCII chars. */
1424 int multibyte;
1425 /* Set to 1 if STRING is multibyte and it contains non-ASCII
1426 chars. */
1427 int string_multibyte;
1428 int size_byte;
1429 const unsigned char *str;
1430 int len;
1431 Lisp_Object iso_classes;
1433 CHECK_STRING (string);
1434 iso_classes = Qnil;
1436 if (NILP (lim))
1437 XSETINT (lim, forwardp ? ZV : BEGV);
1438 else
1439 CHECK_NUMBER_COERCE_MARKER (lim);
1441 /* In any case, don't allow scan outside bounds of buffer. */
1442 if (XINT (lim) > ZV)
1443 XSETFASTINT (lim, ZV);
1444 if (XINT (lim) < BEGV)
1445 XSETFASTINT (lim, BEGV);
1447 multibyte = (!NILP (current_buffer->enable_multibyte_characters)
1448 && (XINT (lim) - PT != CHAR_TO_BYTE (XINT (lim)) - PT_BYTE));
1449 string_multibyte = SBYTES (string) > SCHARS (string);
1451 bzero (fastmap, sizeof fastmap);
1453 str = SDATA (string);
1454 size_byte = SBYTES (string);
1456 i_byte = 0;
1457 if (i_byte < size_byte
1458 && SREF (string, 0) == '^')
1460 negate = 1; i_byte++;
1463 /* Find the characters specified and set their elements of fastmap.
1464 Handle backslashes and ranges specially.
1466 If STRING contains non-ASCII characters, setup char_ranges for
1467 them and use fastmap only for their leading codes. */
1469 if (! string_multibyte)
1471 int string_has_eight_bit = 0;
1473 /* At first setup fastmap. */
1474 while (i_byte < size_byte)
1476 c = str[i_byte++];
1478 if (handle_iso_classes && c == '['
1479 && i_byte < size_byte
1480 && str[i_byte] == ':')
1482 const unsigned char *class_beg = str + i_byte + 1;
1483 const unsigned char *class_end = class_beg;
1484 const unsigned char *class_limit = str + size_byte - 2;
1485 /* Leave room for the null. */
1486 unsigned char class_name[CHAR_CLASS_MAX_LENGTH + 1];
1487 re_wctype_t cc;
1489 if (class_limit - class_beg > CHAR_CLASS_MAX_LENGTH)
1490 class_limit = class_beg + CHAR_CLASS_MAX_LENGTH;
1492 while (class_end < class_limit
1493 && *class_end >= 'a' && *class_end <= 'z')
1494 class_end++;
1496 if (class_end == class_beg
1497 || *class_end != ':' || class_end[1] != ']')
1498 goto not_a_class_name;
1500 bcopy (class_beg, class_name, class_end - class_beg);
1501 class_name[class_end - class_beg] = 0;
1503 cc = re_wctype (class_name);
1504 if (cc == 0)
1505 error ("Invalid ISO C character class");
1507 iso_classes = Fcons (make_number (cc), iso_classes);
1509 i_byte = class_end + 2 - str;
1510 continue;
1513 not_a_class_name:
1514 if (c == '\\')
1516 if (i_byte == size_byte)
1517 break;
1519 c = str[i_byte++];
1521 /* Treat `-' as range character only if another character
1522 follows. */
1523 if (i_byte + 1 < size_byte
1524 && str[i_byte] == '-')
1526 unsigned int c2;
1528 /* Skip over the dash. */
1529 i_byte++;
1531 /* Get the end of the range. */
1532 c2 = str[i_byte++];
1533 if (c2 == '\\'
1534 && i_byte < size_byte)
1535 c2 = str[i_byte++];
1537 if (c <= c2)
1539 while (c <= c2)
1540 fastmap[c++] = 1;
1541 if (! ASCII_CHAR_P (c2))
1542 string_has_eight_bit = 1;
1545 else
1547 fastmap[c] = 1;
1548 if (! ASCII_CHAR_P (c))
1549 string_has_eight_bit = 1;
1553 /* If the current range is multibyte and STRING contains
1554 eight-bit chars, arrange fastmap and setup char_ranges for
1555 the corresponding multibyte chars. */
1556 if (multibyte && string_has_eight_bit)
1558 unsigned char fastmap2[0400];
1559 int range_start_byte, range_start_char;
1561 bcopy (fastmap2 + 0200, fastmap + 0200, 0200);
1562 bzero (fastmap + 0200, 0200);
1563 /* We are sure that this loop stops. */
1564 for (i = 0200; ! fastmap2[i]; i++);
1565 c = unibyte_char_to_multibyte (i);
1566 fastmap[CHAR_LEADING_CODE (c)] = 1;
1567 range_start_byte = i;
1568 range_start_char = c;
1569 char_ranges = (int *) alloca (sizeof (int) * 128 * 2);
1570 for (i = 129; i < 0400; i++)
1572 c = unibyte_char_to_multibyte (i);
1573 fastmap[CHAR_LEADING_CODE (c)] = 1;
1574 if (i - range_start_byte != c - range_start_char)
1576 char_ranges[n_char_ranges++] = range_start_char;
1577 char_ranges[n_char_ranges++] = ((i - 1 - range_start_byte)
1578 + range_start_char);
1579 range_start_byte = i;
1580 range_start_char = c;
1583 char_ranges[n_char_ranges++] = range_start_char;
1584 char_ranges[n_char_ranges++] = ((i - 1 - range_start_byte)
1585 + range_start_char);
1588 else /* STRING is multibyte */
1590 char_ranges = (int *) alloca (sizeof (int) * SCHARS (string) * 2);
1592 while (i_byte < size_byte)
1594 unsigned char leading_code;
1596 leading_code = str[i_byte];
1597 c = STRING_CHAR_AND_LENGTH (str + i_byte, size_byte-i_byte, len);
1598 i_byte += len;
1600 if (handle_iso_classes && c == '['
1601 && i_byte < size_byte
1602 && STRING_CHAR (str + i_byte, size_byte - i_byte) == ':')
1604 const unsigned char *class_beg = str + i_byte + 1;
1605 const unsigned char *class_end = class_beg;
1606 const unsigned char *class_limit = str + size_byte - 2;
1607 /* Leave room for the null. */
1608 unsigned char class_name[CHAR_CLASS_MAX_LENGTH + 1];
1609 re_wctype_t cc;
1611 if (class_limit - class_beg > CHAR_CLASS_MAX_LENGTH)
1612 class_limit = class_beg + CHAR_CLASS_MAX_LENGTH;
1614 while (class_end < class_limit
1615 && *class_end >= 'a' && *class_end <= 'z')
1616 class_end++;
1618 if (class_end == class_beg
1619 || *class_end != ':' || class_end[1] != ']')
1620 goto not_a_class_name_multibyte;
1622 bcopy (class_beg, class_name, class_end - class_beg);
1623 class_name[class_end - class_beg] = 0;
1625 cc = re_wctype (class_name);
1626 if (cc == 0)
1627 error ("Invalid ISO C character class");
1629 iso_classes = Fcons (make_number (cc), iso_classes);
1631 i_byte = class_end + 2 - str;
1632 continue;
1635 not_a_class_name_multibyte:
1636 if (c == '\\')
1638 if (i_byte == size_byte)
1639 break;
1641 leading_code = str[i_byte];
1642 c = STRING_CHAR_AND_LENGTH (str + i_byte,
1643 size_byte - i_byte, len);
1644 i_byte += len;
1646 /* Treat `-' as range character only if another character
1647 follows. */
1648 if (i_byte + 1 < size_byte
1649 && str[i_byte] == '-')
1651 unsigned int c2;
1652 unsigned char leading_code2;
1654 /* Skip over the dash. */
1655 i_byte++;
1657 /* Get the end of the range. */
1658 leading_code2 = str[i_byte];
1659 c2 = STRING_CHAR_AND_LENGTH (str + i_byte,
1660 size_byte - i_byte, len);
1661 i_byte += len;
1663 if (c2 == '\\'
1664 && i_byte < size_byte)
1666 leading_code2 = str[i_byte];
1667 c2 =STRING_CHAR_AND_LENGTH (str + i_byte, size_byte-i_byte, len);
1668 i_byte += len;
1671 if (c > c2)
1672 continue;
1673 if (ASCII_CHAR_P (c))
1675 while (c <= c2 && c < 0x80)
1676 fastmap[c++] = 1;
1677 leading_code = CHAR_LEADING_CODE (c);
1679 if (! ASCII_CHAR_P (c))
1681 while (leading_code <= leading_code2)
1682 fastmap[leading_code++] = 1;
1683 if (c <= c2)
1685 char_ranges[n_char_ranges++] = c;
1686 char_ranges[n_char_ranges++] = c2;
1690 else
1692 if (ASCII_CHAR_P (c))
1693 fastmap[c] = 1;
1694 else
1696 fastmap[leading_code] = 1;
1697 char_ranges[n_char_ranges++] = c;
1698 char_ranges[n_char_ranges++] = c;
1703 /* If the current range is unibyte and STRING contains non-ASCII
1704 chars, arrange fastmap for the corresponding unibyte
1705 chars. */
1707 if (! multibyte && n_char_ranges > 0)
1709 bzero (fastmap + 0200, 0200);
1710 for (i = 0; i < n_char_ranges; i += 2)
1712 int c1 = char_ranges[i];
1713 int c2 = char_ranges[i + 1];
1715 for (; c1 <= c2; c1++)
1716 fastmap[CHAR_TO_BYTE8 (c1)] = 1;
1721 /* If ^ was the first character, complement the fastmap. */
1722 if (negate)
1724 if (! multibyte)
1725 for (i = 0; i < sizeof fastmap; i++)
1726 fastmap[i] ^= 1;
1727 else
1729 for (i = 0; i < 0200; i++)
1730 fastmap[i] ^= 1;
1731 /* All non-ASCII chars possibly match. */
1732 for (; i < sizeof fastmap; i++)
1733 fastmap[i] = 1;
1738 int start_point = PT;
1739 int pos = PT;
1740 int pos_byte = PT_BYTE;
1741 unsigned char *p = PT_ADDR, *endp, *stop;
1743 if (forwardp)
1745 endp = (XINT (lim) == GPT) ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
1746 stop = (pos < GPT && GPT < XINT (lim)) ? GPT_ADDR : endp;
1748 else
1750 endp = CHAR_POS_ADDR (XINT (lim));
1751 stop = (pos >= GPT && GPT > XINT (lim)) ? GAP_END_ADDR : endp;
1754 immediate_quit = 1;
1755 if (forwardp)
1757 if (multibyte)
1758 while (1)
1760 int nbytes;
1762 if (p >= stop)
1764 if (p >= endp)
1765 break;
1766 p = GAP_END_ADDR;
1767 stop = endp;
1769 c = STRING_CHAR_AND_LENGTH (p, MAX_MULTIBYTE_LENGTH, nbytes);
1770 if (! NILP (iso_classes) && in_classes (c, iso_classes))
1772 if (negate)
1773 break;
1774 else
1775 goto fwd_ok;
1778 if (! fastmap[*p])
1779 break;
1780 if (! ASCII_CHAR_P (c))
1782 /* As we are looking at a multibyte character, we
1783 must look up the character in the table
1784 CHAR_RANGES. If there's no data in the table,
1785 that character is not what we want to skip. */
1787 /* The following code do the right thing even if
1788 n_char_ranges is zero (i.e. no data in
1789 CHAR_RANGES). */
1790 for (i = 0; i < n_char_ranges; i += 2)
1791 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
1792 break;
1793 if (!(negate ^ (i < n_char_ranges)))
1794 break;
1796 fwd_ok:
1797 p += nbytes, pos++, pos_byte += nbytes;
1799 else
1800 while (1)
1802 if (p >= stop)
1804 if (p >= endp)
1805 break;
1806 p = GAP_END_ADDR;
1807 stop = endp;
1810 if (!NILP (iso_classes) && in_classes (*p, iso_classes))
1812 if (negate)
1813 break;
1814 else
1815 goto fwd_unibyte_ok;
1818 if (!fastmap[*p])
1819 break;
1820 fwd_unibyte_ok:
1821 p++, pos++, pos_byte++;
1824 else
1826 if (multibyte)
1827 while (1)
1829 unsigned char *prev_p;
1831 if (p <= stop)
1833 if (p <= endp)
1834 break;
1835 p = GPT_ADDR;
1836 stop = endp;
1838 prev_p = p;
1839 while (--p >= stop && ! CHAR_HEAD_P (*p));
1840 c = STRING_CHAR (p, MAX_MULTIBYTE_LENGTH);
1842 if (! NILP (iso_classes) && in_classes (c, iso_classes))
1844 if (negate)
1845 break;
1846 else
1847 goto back_ok;
1850 if (! fastmap[*p])
1851 break;
1852 if (! ASCII_CHAR_P (c))
1854 /* See the comment in the previous similar code. */
1855 for (i = 0; i < n_char_ranges; i += 2)
1856 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
1857 break;
1858 if (!(negate ^ (i < n_char_ranges)))
1859 break;
1861 back_ok:
1862 pos--, pos_byte -= prev_p - p;
1864 else
1865 while (1)
1867 if (p <= stop)
1869 if (p <= endp)
1870 break;
1871 p = GPT_ADDR;
1872 stop = endp;
1875 if (! NILP (iso_classes) && in_classes (p[-1], iso_classes))
1877 if (negate)
1878 break;
1879 else
1880 goto back_unibyte_ok;
1883 if (!fastmap[p[-1]])
1884 break;
1885 back_unibyte_ok:
1886 p--, pos--, pos_byte--;
1890 SET_PT_BOTH (pos, pos_byte);
1891 immediate_quit = 0;
1893 return make_number (PT - start_point);
1898 static Lisp_Object
1899 skip_syntaxes (forwardp, string, lim)
1900 int forwardp;
1901 Lisp_Object string, lim;
1903 register unsigned int c;
1904 unsigned char fastmap[0400];
1905 int negate = 0;
1906 register int i, i_byte;
1907 int multibyte;
1908 int size_byte;
1909 unsigned char *str;
1911 CHECK_STRING (string);
1913 if (NILP (lim))
1914 XSETINT (lim, forwardp ? ZV : BEGV);
1915 else
1916 CHECK_NUMBER_COERCE_MARKER (lim);
1918 /* In any case, don't allow scan outside bounds of buffer. */
1919 if (XINT (lim) > ZV)
1920 XSETFASTINT (lim, ZV);
1921 if (XINT (lim) < BEGV)
1922 XSETFASTINT (lim, BEGV);
1924 if (forwardp ? (PT >= XFASTINT (lim)) : (PT <= XFASTINT (lim)))
1925 return make_number (0);
1927 multibyte = (!NILP (current_buffer->enable_multibyte_characters)
1928 && (XINT (lim) - PT != CHAR_TO_BYTE (XINT (lim)) - PT_BYTE));
1930 bzero (fastmap, sizeof fastmap);
1932 if (SBYTES (string) > SCHARS (string))
1933 /* As this is very rare case (syntax spec is ASCII only), don't
1934 consider efficiency. */
1935 string = string_make_unibyte (string);
1937 str = SDATA (string);
1938 size_byte = SBYTES (string);
1940 i_byte = 0;
1941 if (i_byte < size_byte
1942 && SREF (string, 0) == '^')
1944 negate = 1; i_byte++;
1947 /* Find the syntaxes specified and set their elements of fastmap. */
1949 while (i_byte < size_byte)
1951 c = str[i_byte++];
1952 fastmap[syntax_spec_code[c]] = 1;
1955 /* If ^ was the first character, complement the fastmap. */
1956 if (negate)
1957 for (i = 0; i < sizeof fastmap; i++)
1958 fastmap[i] ^= 1;
1961 int start_point = PT;
1962 int pos = PT;
1963 int pos_byte = PT_BYTE;
1964 unsigned char *p = PT_ADDR, *endp, *stop;
1966 if (forwardp)
1968 endp = (XINT (lim) == GPT) ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
1969 stop = (pos < GPT && GPT < XINT (lim)) ? GPT_ADDR : endp;
1971 else
1973 endp = CHAR_POS_ADDR (XINT (lim));
1974 stop = (pos >= GPT && GPT > XINT (lim)) ? GAP_END_ADDR : endp;
1977 immediate_quit = 1;
1978 SETUP_SYNTAX_TABLE (pos, forwardp ? 1 : -1);
1979 if (forwardp)
1981 if (multibyte)
1983 while (1)
1985 int nbytes;
1987 if (p >= stop)
1989 if (p >= endp)
1990 break;
1991 p = GAP_END_ADDR;
1992 stop = endp;
1994 c = STRING_CHAR_AND_LENGTH (p, MAX_MULTIBYTE_LENGTH, nbytes);
1995 if (! fastmap[(int) SYNTAX (c)])
1996 break;
1997 p += nbytes, pos++, pos_byte += nbytes;
1998 UPDATE_SYNTAX_TABLE_FORWARD (pos);
2001 else
2003 while (1)
2005 if (p >= stop)
2007 if (p >= endp)
2008 break;
2009 p = GAP_END_ADDR;
2010 stop = endp;
2012 if (! fastmap[(int) SYNTAX (*p)])
2013 break;
2014 p++, pos++, pos_byte++;
2015 UPDATE_SYNTAX_TABLE_FORWARD (pos);
2019 else
2021 if (multibyte)
2023 while (1)
2025 unsigned char *prev_p;
2027 if (p <= stop)
2029 if (p <= endp)
2030 break;
2031 p = GPT_ADDR;
2032 stop = endp;
2034 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
2035 prev_p = p;
2036 while (--p >= stop && ! CHAR_HEAD_P (*p));
2037 c = STRING_CHAR (p, MAX_MULTIBYTE_LENGTH);
2038 if (! fastmap[(int) SYNTAX (c)])
2039 break;
2040 pos--, pos_byte -= prev_p - p;
2043 else
2045 while (1)
2047 if (p <= stop)
2049 if (p <= endp)
2050 break;
2051 p = GPT_ADDR;
2052 stop = endp;
2054 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
2055 if (! fastmap[(int) SYNTAX (p[-1])])
2056 break;
2057 p--, pos--, pos_byte--;
2062 SET_PT_BOTH (pos, pos_byte);
2063 immediate_quit = 0;
2065 return make_number (PT - start_point);
2069 /* Return 1 if character C belongs to one of the ISO classes
2070 in the list ISO_CLASSES. Each class is represented by an
2071 integer which is its type according to re_wctype. */
2073 static int
2074 in_classes (c, iso_classes)
2075 int c;
2076 Lisp_Object iso_classes;
2078 int fits_class = 0;
2080 while (! NILP (iso_classes))
2082 Lisp_Object elt;
2083 elt = XCAR (iso_classes);
2084 iso_classes = XCDR (iso_classes);
2086 if (re_iswctype (c, XFASTINT (elt)))
2087 fits_class = 1;
2090 return fits_class;
2093 /* Jump over a comment, assuming we are at the beginning of one.
2094 FROM is the current position.
2095 FROM_BYTE is the bytepos corresponding to FROM.
2096 Do not move past STOP (a charpos).
2097 The comment over which we have to jump is of style STYLE
2098 (either SYNTAX_COMMENT_STYLE(foo) or ST_COMMENT_STYLE).
2099 NESTING should be positive to indicate the nesting at the beginning
2100 for nested comments and should be zero or negative else.
2101 ST_COMMENT_STYLE cannot be nested.
2102 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
2103 (or 0 If the search cannot start in the middle of a two-character).
2105 If successful, return 1 and store the charpos of the comment's end
2106 into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR.
2107 Else, return 0 and store the charpos STOP into *CHARPOS_PTR, the
2108 corresponding bytepos into *BYTEPOS_PTR and the current nesting
2109 (as defined for state.incomment) in *INCOMMENT_PTR.
2111 The comment end is the last character of the comment rather than the
2112 character just after the comment.
2114 Global syntax data is assumed to initially be valid for FROM and
2115 remains valid for forward search starting at the returned position. */
2117 static int
2118 forw_comment (from, from_byte, stop, nesting, style, prev_syntax,
2119 charpos_ptr, bytepos_ptr, incomment_ptr)
2120 EMACS_INT from, from_byte, stop;
2121 int nesting, style, prev_syntax;
2122 EMACS_INT *charpos_ptr, *bytepos_ptr;
2123 int *incomment_ptr;
2125 register int c, c1;
2126 register enum syntaxcode code;
2127 register int syntax;
2129 if (nesting <= 0) nesting = -1;
2131 /* Enter the loop in the middle so that we find
2132 a 2-char comment ender if we start in the middle of it. */
2133 syntax = prev_syntax;
2134 if (syntax != 0) goto forw_incomment;
2136 while (1)
2138 if (from == stop)
2140 *incomment_ptr = nesting;
2141 *charpos_ptr = from;
2142 *bytepos_ptr = from_byte;
2143 return 0;
2145 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2146 syntax = SYNTAX_WITH_FLAGS (c);
2147 code = syntax & 0xff;
2148 if (code == Sendcomment
2149 && SYNTAX_FLAGS_COMMENT_STYLE (syntax) == style
2150 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ?
2151 (nesting > 0 && --nesting == 0) : nesting < 0))
2152 /* we have encountered a comment end of the same style
2153 as the comment sequence which began this comment
2154 section */
2155 break;
2156 if (code == Scomment_fence
2157 && style == ST_COMMENT_STYLE)
2158 /* we have encountered a comment end of the same style
2159 as the comment sequence which began this comment
2160 section. */
2161 break;
2162 if (nesting > 0
2163 && code == Scomment
2164 && SYNTAX_FLAGS_COMMENT_NESTED (syntax)
2165 && SYNTAX_FLAGS_COMMENT_STYLE (syntax) == style)
2166 /* we have encountered a nested comment of the same style
2167 as the comment sequence which began this comment section */
2168 nesting++;
2169 INC_BOTH (from, from_byte);
2170 UPDATE_SYNTAX_TABLE_FORWARD (from);
2172 forw_incomment:
2173 if (from < stop && SYNTAX_FLAGS_COMEND_FIRST (syntax)
2174 && SYNTAX_FLAGS_COMMENT_STYLE (syntax) == style
2175 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2176 SYNTAX_COMEND_SECOND (c1))
2177 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
2178 SYNTAX_COMMENT_NESTED (c1)) ? nesting > 0 : nesting < 0))
2180 if (--nesting <= 0)
2181 /* we have encountered a comment end of the same style
2182 as the comment sequence which began this comment
2183 section */
2184 break;
2185 else
2187 INC_BOTH (from, from_byte);
2188 UPDATE_SYNTAX_TABLE_FORWARD (from);
2191 if (nesting > 0
2192 && from < stop
2193 && SYNTAX_FLAGS_COMSTART_FIRST (syntax)
2194 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2195 SYNTAX_COMMENT_STYLE (c1) == style
2196 && SYNTAX_COMSTART_SECOND (c1))
2197 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
2198 SYNTAX_COMMENT_NESTED (c1)))
2199 /* we have encountered a nested comment of the same style
2200 as the comment sequence which began this comment
2201 section */
2203 INC_BOTH (from, from_byte);
2204 UPDATE_SYNTAX_TABLE_FORWARD (from);
2205 nesting++;
2208 *charpos_ptr = from;
2209 *bytepos_ptr = from_byte;
2210 return 1;
2213 DEFUN ("forward-comment", Fforward_comment, Sforward_comment, 1, 1, 0,
2214 doc: /*
2215 Move forward across up to COUNT comments. If COUNT is negative, move backward.
2216 Stop scanning if we find something other than a comment or whitespace.
2217 Set point to where scanning stops.
2218 If COUNT comments are found as expected, with nothing except whitespace
2219 between them, return t; otherwise return nil. */)
2220 (count)
2221 Lisp_Object count;
2223 register EMACS_INT from;
2224 EMACS_INT from_byte;
2225 register EMACS_INT stop;
2226 register int c, c1;
2227 register enum syntaxcode code;
2228 int comstyle = 0; /* style of comment encountered */
2229 int comnested = 0; /* whether the comment is nestable or not */
2230 int found;
2231 EMACS_INT count1;
2232 EMACS_INT out_charpos, out_bytepos;
2233 int dummy;
2235 CHECK_NUMBER (count);
2236 count1 = XINT (count);
2237 stop = count1 > 0 ? ZV : BEGV;
2239 immediate_quit = 1;
2240 QUIT;
2242 from = PT;
2243 from_byte = PT_BYTE;
2245 SETUP_SYNTAX_TABLE (from, count1);
2246 while (count1 > 0)
2250 int comstart_first;
2252 if (from == stop)
2254 SET_PT_BOTH (from, from_byte);
2255 immediate_quit = 0;
2256 return Qnil;
2258 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2259 code = SYNTAX (c);
2260 comstart_first = SYNTAX_COMSTART_FIRST (c);
2261 comnested = SYNTAX_COMMENT_NESTED (c);
2262 comstyle = SYNTAX_COMMENT_STYLE (c);
2263 INC_BOTH (from, from_byte);
2264 UPDATE_SYNTAX_TABLE_FORWARD (from);
2265 if (from < stop && comstart_first
2266 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2267 SYNTAX_COMSTART_SECOND (c1)))
2269 /* We have encountered a comment start sequence and we
2270 are ignoring all text inside comments. We must record
2271 the comment style this sequence begins so that later,
2272 only a comment end of the same style actually ends
2273 the comment section. */
2274 code = Scomment;
2275 comstyle = SYNTAX_COMMENT_STYLE (c1);
2276 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
2277 INC_BOTH (from, from_byte);
2278 UPDATE_SYNTAX_TABLE_FORWARD (from);
2281 while (code == Swhitespace || (code == Sendcomment && c == '\n'));
2283 if (code == Scomment_fence)
2284 comstyle = ST_COMMENT_STYLE;
2285 else if (code != Scomment)
2287 immediate_quit = 0;
2288 DEC_BOTH (from, from_byte);
2289 SET_PT_BOTH (from, from_byte);
2290 return Qnil;
2292 /* We're at the start of a comment. */
2293 found = forw_comment (from, from_byte, stop, comnested, comstyle, 0,
2294 &out_charpos, &out_bytepos, &dummy);
2295 from = out_charpos; from_byte = out_bytepos;
2296 if (!found)
2298 immediate_quit = 0;
2299 SET_PT_BOTH (from, from_byte);
2300 return Qnil;
2302 INC_BOTH (from, from_byte);
2303 UPDATE_SYNTAX_TABLE_FORWARD (from);
2304 /* We have skipped one comment. */
2305 count1--;
2308 while (count1 < 0)
2310 while (1)
2312 int quoted;
2314 if (from <= stop)
2316 SET_PT_BOTH (BEGV, BEGV_BYTE);
2317 immediate_quit = 0;
2318 return Qnil;
2321 DEC_BOTH (from, from_byte);
2322 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
2323 quoted = char_quoted (from, from_byte);
2324 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2325 code = SYNTAX (c);
2326 comstyle = 0;
2327 comnested = SYNTAX_COMMENT_NESTED (c);
2328 if (code == Sendcomment)
2329 comstyle = SYNTAX_COMMENT_STYLE (c);
2330 if (from > stop && SYNTAX_COMEND_SECOND (c)
2331 && prev_char_comend_first (from, from_byte)
2332 && !char_quoted (from - 1, dec_bytepos (from_byte)))
2334 /* We must record the comment style encountered so that
2335 later, we can match only the proper comment begin
2336 sequence of the same style. */
2337 DEC_BOTH (from, from_byte);
2338 code = Sendcomment;
2339 /* Calling char_quoted, above, set up global syntax position
2340 at the new value of FROM. */
2341 c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2342 comstyle = SYNTAX_COMMENT_STYLE (c1);
2343 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
2346 if (code == Scomment_fence)
2348 /* Skip until first preceding unquoted comment_fence. */
2349 int found = 0, ini = from, ini_byte = from_byte;
2351 while (1)
2353 DEC_BOTH (from, from_byte);
2354 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2355 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2356 if (SYNTAX (c) == Scomment_fence
2357 && !char_quoted (from, from_byte))
2359 found = 1;
2360 break;
2362 else if (from == stop)
2363 break;
2365 if (found == 0)
2367 from = ini; /* Set point to ini + 1. */
2368 from_byte = ini_byte;
2369 goto leave;
2371 else
2372 /* We have skipped one comment. */
2373 break;
2375 else if (code == Sendcomment)
2377 found = back_comment (from, from_byte, stop, comnested, comstyle,
2378 &out_charpos, &out_bytepos);
2379 if (found == -1)
2381 if (c == '\n')
2382 /* This end-of-line is not an end-of-comment.
2383 Treat it like a whitespace.
2384 CC-mode (and maybe others) relies on this behavior. */
2386 else
2388 /* Failure: we should go back to the end of this
2389 not-quite-endcomment. */
2390 if (SYNTAX(c) != code)
2391 /* It was a two-char Sendcomment. */
2392 INC_BOTH (from, from_byte);
2393 goto leave;
2396 else
2398 /* We have skipped one comment. */
2399 from = out_charpos, from_byte = out_bytepos;
2400 break;
2403 else if (code != Swhitespace || quoted)
2405 leave:
2406 immediate_quit = 0;
2407 INC_BOTH (from, from_byte);
2408 SET_PT_BOTH (from, from_byte);
2409 return Qnil;
2413 count1++;
2416 SET_PT_BOTH (from, from_byte);
2417 immediate_quit = 0;
2418 return Qt;
2421 /* Return syntax code of character C if C is an ASCII character
2422 or `multibyte_symbol_p' is zero. Otherwise, return Ssymbol. */
2424 #define SYNTAX_WITH_MULTIBYTE_CHECK(c) \
2425 ((ASCII_CHAR_P (c) || !multibyte_symbol_p) \
2426 ? SYNTAX (c) : Ssymbol)
2428 static Lisp_Object
2429 scan_lists (from, count, depth, sexpflag)
2430 register EMACS_INT from;
2431 EMACS_INT count, depth;
2432 int sexpflag;
2434 Lisp_Object val;
2435 register EMACS_INT stop = count > 0 ? ZV : BEGV;
2436 register int c, c1;
2437 int stringterm;
2438 int quoted;
2439 int mathexit = 0;
2440 register enum syntaxcode code, temp_code;
2441 int min_depth = depth; /* Err out if depth gets less than this. */
2442 int comstyle = 0; /* style of comment encountered */
2443 int comnested = 0; /* whether the comment is nestable or not */
2444 EMACS_INT temp_pos;
2445 EMACS_INT last_good = from;
2446 int found;
2447 EMACS_INT from_byte;
2448 EMACS_INT out_bytepos, out_charpos;
2449 int temp, dummy;
2450 int multibyte_symbol_p = sexpflag && multibyte_syntax_as_symbol;
2452 if (depth > 0) min_depth = 0;
2454 if (from > ZV) from = ZV;
2455 if (from < BEGV) from = BEGV;
2457 from_byte = CHAR_TO_BYTE (from);
2459 immediate_quit = 1;
2460 QUIT;
2462 SETUP_SYNTAX_TABLE (from, count);
2463 while (count > 0)
2465 while (from < stop)
2467 int comstart_first, prefix;
2468 UPDATE_SYNTAX_TABLE_FORWARD (from);
2469 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2470 code = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2471 comstart_first = SYNTAX_COMSTART_FIRST (c);
2472 comnested = SYNTAX_COMMENT_NESTED (c);
2473 comstyle = SYNTAX_COMMENT_STYLE (c);
2474 prefix = SYNTAX_PREFIX (c);
2475 if (depth == min_depth)
2476 last_good = from;
2477 INC_BOTH (from, from_byte);
2478 UPDATE_SYNTAX_TABLE_FORWARD (from);
2479 if (from < stop && comstart_first
2480 && (c = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2481 SYNTAX_COMSTART_SECOND (c))
2482 && parse_sexp_ignore_comments)
2484 /* we have encountered a comment start sequence and we
2485 are ignoring all text inside comments. We must record
2486 the comment style this sequence begins so that later,
2487 only a comment end of the same style actually ends
2488 the comment section */
2489 code = Scomment;
2490 c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2491 comstyle = SYNTAX_COMMENT_STYLE (c1);
2492 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
2493 INC_BOTH (from, from_byte);
2494 UPDATE_SYNTAX_TABLE_FORWARD (from);
2497 if (prefix)
2498 continue;
2500 switch (SWITCH_ENUM_CAST (code))
2502 case Sescape:
2503 case Scharquote:
2504 if (from == stop)
2505 goto lose;
2506 INC_BOTH (from, from_byte);
2507 /* treat following character as a word constituent */
2508 case Sword:
2509 case Ssymbol:
2510 if (depth || !sexpflag) break;
2511 /* This word counts as a sexp; return at end of it. */
2512 while (from < stop)
2514 UPDATE_SYNTAX_TABLE_FORWARD (from);
2516 /* Some compilers can't handle this inside the switch. */
2517 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2518 temp = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2519 switch (temp)
2521 case Scharquote:
2522 case Sescape:
2523 INC_BOTH (from, from_byte);
2524 if (from == stop)
2525 goto lose;
2526 break;
2527 case Sword:
2528 case Ssymbol:
2529 case Squote:
2530 break;
2531 default:
2532 goto done;
2534 INC_BOTH (from, from_byte);
2536 goto done;
2538 case Scomment_fence:
2539 comstyle = ST_COMMENT_STYLE;
2540 /* FALLTHROUGH */
2541 case Scomment:
2542 if (!parse_sexp_ignore_comments) break;
2543 UPDATE_SYNTAX_TABLE_FORWARD (from);
2544 found = forw_comment (from, from_byte, stop,
2545 comnested, comstyle, 0,
2546 &out_charpos, &out_bytepos, &dummy);
2547 from = out_charpos, from_byte = out_bytepos;
2548 if (!found)
2550 if (depth == 0)
2551 goto done;
2552 goto lose;
2554 INC_BOTH (from, from_byte);
2555 UPDATE_SYNTAX_TABLE_FORWARD (from);
2556 break;
2558 case Smath:
2559 if (!sexpflag)
2560 break;
2561 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (from_byte))
2563 INC_BOTH (from, from_byte);
2565 if (mathexit)
2567 mathexit = 0;
2568 goto close1;
2570 mathexit = 1;
2572 case Sopen:
2573 if (!++depth) goto done;
2574 break;
2576 case Sclose:
2577 close1:
2578 if (!--depth) goto done;
2579 if (depth < min_depth)
2580 xsignal3 (Qscan_error,
2581 build_string ("Containing expression ends prematurely"),
2582 make_number (last_good), make_number (from));
2583 break;
2585 case Sstring:
2586 case Sstring_fence:
2587 temp_pos = dec_bytepos (from_byte);
2588 stringterm = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2589 while (1)
2591 if (from >= stop)
2592 goto lose;
2593 UPDATE_SYNTAX_TABLE_FORWARD (from);
2594 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2595 if (code == Sstring
2596 ? (c == stringterm
2597 && SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring)
2598 : SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring_fence)
2599 break;
2601 /* Some compilers can't handle this inside the switch. */
2602 temp = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2603 switch (temp)
2605 case Scharquote:
2606 case Sescape:
2607 INC_BOTH (from, from_byte);
2609 INC_BOTH (from, from_byte);
2611 INC_BOTH (from, from_byte);
2612 if (!depth && sexpflag) goto done;
2613 break;
2614 default:
2615 /* Ignore whitespace, punctuation, quote, endcomment. */
2616 break;
2620 /* Reached end of buffer. Error if within object, return nil if between */
2621 if (depth)
2622 goto lose;
2624 immediate_quit = 0;
2625 return Qnil;
2627 /* End of object reached */
2628 done:
2629 count--;
2632 while (count < 0)
2634 while (from > stop)
2636 DEC_BOTH (from, from_byte);
2637 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2638 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2639 code = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2640 if (depth == min_depth)
2641 last_good = from;
2642 comstyle = 0;
2643 comnested = SYNTAX_COMMENT_NESTED (c);
2644 if (code == Sendcomment)
2645 comstyle = SYNTAX_COMMENT_STYLE (c);
2646 if (from > stop && SYNTAX_COMEND_SECOND (c)
2647 && prev_char_comend_first (from, from_byte)
2648 && parse_sexp_ignore_comments)
2650 /* We must record the comment style encountered so that
2651 later, we can match only the proper comment begin
2652 sequence of the same style. */
2653 DEC_BOTH (from, from_byte);
2654 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2655 code = Sendcomment;
2656 c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2657 comstyle = SYNTAX_COMMENT_STYLE (c1);
2658 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
2661 /* Quoting turns anything except a comment-ender
2662 into a word character. Note that this cannot be true
2663 if we decremented FROM in the if-statement above. */
2664 if (code != Sendcomment && char_quoted (from, from_byte))
2666 DEC_BOTH (from, from_byte);
2667 code = Sword;
2669 else if (SYNTAX_PREFIX (c))
2670 continue;
2672 switch (SWITCH_ENUM_CAST (code))
2674 case Sword:
2675 case Ssymbol:
2676 case Sescape:
2677 case Scharquote:
2678 if (depth || !sexpflag) break;
2679 /* This word counts as a sexp; count object finished
2680 after passing it. */
2681 while (from > stop)
2683 temp_pos = from_byte;
2684 if (! NILP (current_buffer->enable_multibyte_characters))
2685 DEC_POS (temp_pos);
2686 else
2687 temp_pos--;
2688 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2689 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2690 temp_code = SYNTAX_WITH_MULTIBYTE_CHECK (c1);
2691 /* Don't allow comment-end to be quoted. */
2692 if (temp_code == Sendcomment)
2693 goto done2;
2694 quoted = char_quoted (from - 1, temp_pos);
2695 if (quoted)
2697 DEC_BOTH (from, from_byte);
2698 temp_pos = dec_bytepos (temp_pos);
2699 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2701 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2702 temp_code = SYNTAX_WITH_MULTIBYTE_CHECK (c1);
2703 if (! (quoted || temp_code == Sword
2704 || temp_code == Ssymbol
2705 || temp_code == Squote))
2706 goto done2;
2707 DEC_BOTH (from, from_byte);
2709 goto done2;
2711 case Smath:
2712 if (!sexpflag)
2713 break;
2714 temp_pos = dec_bytepos (from_byte);
2715 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2716 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (temp_pos))
2717 DEC_BOTH (from, from_byte);
2718 if (mathexit)
2720 mathexit = 0;
2721 goto open2;
2723 mathexit = 1;
2725 case Sclose:
2726 if (!++depth) goto done2;
2727 break;
2729 case Sopen:
2730 open2:
2731 if (!--depth) goto done2;
2732 if (depth < min_depth)
2733 xsignal3 (Qscan_error,
2734 build_string ("Containing expression ends prematurely"),
2735 make_number (last_good), make_number (from));
2736 break;
2738 case Sendcomment:
2739 if (!parse_sexp_ignore_comments)
2740 break;
2741 found = back_comment (from, from_byte, stop, comnested, comstyle,
2742 &out_charpos, &out_bytepos);
2743 /* FIXME: if found == -1, then it really wasn't a comment-end.
2744 For single-char Sendcomment, we can't do much about it apart
2745 from skipping the char.
2746 For 2-char endcomments, we could try again, taking both
2747 chars as separate entities, but it's a lot of trouble
2748 for very little gain, so we don't bother either. -sm */
2749 if (found != -1)
2750 from = out_charpos, from_byte = out_bytepos;
2751 break;
2753 case Scomment_fence:
2754 case Sstring_fence:
2755 while (1)
2757 if (from == stop)
2758 goto lose;
2759 DEC_BOTH (from, from_byte);
2760 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2761 if (!char_quoted (from, from_byte)
2762 && (c = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2763 SYNTAX_WITH_MULTIBYTE_CHECK (c) == code))
2764 break;
2766 if (code == Sstring_fence && !depth && sexpflag) goto done2;
2767 break;
2769 case Sstring:
2770 stringterm = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2771 while (1)
2773 if (from == stop)
2774 goto lose;
2775 DEC_BOTH (from, from_byte);
2776 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2777 if (!char_quoted (from, from_byte)
2778 && (stringterm
2779 == (c = FETCH_CHAR_AS_MULTIBYTE (from_byte)))
2780 && SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring)
2781 break;
2783 if (!depth && sexpflag) goto done2;
2784 break;
2785 default:
2786 /* Ignore whitespace, punctuation, quote, endcomment. */
2787 break;
2791 /* Reached start of buffer. Error if within object, return nil if between */
2792 if (depth)
2793 goto lose;
2795 immediate_quit = 0;
2796 return Qnil;
2798 done2:
2799 count++;
2803 immediate_quit = 0;
2804 XSETFASTINT (val, from);
2805 return val;
2807 lose:
2808 xsignal3 (Qscan_error,
2809 build_string ("Unbalanced parentheses"),
2810 make_number (last_good), make_number (from));
2813 DEFUN ("scan-lists", Fscan_lists, Sscan_lists, 3, 3, 0,
2814 doc: /* Scan from character number FROM by COUNT lists.
2815 Returns the character number of the position thus found.
2817 If DEPTH is nonzero, paren depth begins counting from that value,
2818 only places where the depth in parentheses becomes zero
2819 are candidates for stopping; COUNT such places are counted.
2820 Thus, a positive value for DEPTH means go out levels.
2822 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2824 If the beginning or end of (the accessible part of) the buffer is reached
2825 and the depth is wrong, an error is signaled.
2826 If the depth is right but the count is not used up, nil is returned. */)
2827 (from, count, depth)
2828 Lisp_Object from, count, depth;
2830 CHECK_NUMBER (from);
2831 CHECK_NUMBER (count);
2832 CHECK_NUMBER (depth);
2834 return scan_lists (XINT (from), XINT (count), XINT (depth), 0);
2837 DEFUN ("scan-sexps", Fscan_sexps, Sscan_sexps, 2, 2, 0,
2838 doc: /* Scan from character number FROM by COUNT balanced expressions.
2839 If COUNT is negative, scan backwards.
2840 Returns the character number of the position thus found.
2842 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2844 If the beginning or end of (the accessible part of) the buffer is reached
2845 in the middle of a parenthetical grouping, an error is signaled.
2846 If the beginning or end is reached between groupings
2847 but before count is used up, nil is returned. */)
2848 (from, count)
2849 Lisp_Object from, count;
2851 CHECK_NUMBER (from);
2852 CHECK_NUMBER (count);
2854 return scan_lists (XINT (from), XINT (count), 0, 1);
2857 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars, Sbackward_prefix_chars,
2858 0, 0, 0,
2859 doc: /* Move point backward over any number of chars with prefix syntax.
2860 This includes chars with "quote" or "prefix" syntax (' or p). */)
2863 int beg = BEGV;
2864 int opoint = PT;
2865 int opoint_byte = PT_BYTE;
2866 int pos = PT;
2867 int pos_byte = PT_BYTE;
2868 int c;
2870 if (pos <= beg)
2872 SET_PT_BOTH (opoint, opoint_byte);
2874 return Qnil;
2877 SETUP_SYNTAX_TABLE (pos, -1);
2879 DEC_BOTH (pos, pos_byte);
2881 while (!char_quoted (pos, pos_byte)
2882 /* Previous statement updates syntax table. */
2883 && ((c = FETCH_CHAR_AS_MULTIBYTE (pos_byte), SYNTAX (c) == Squote)
2884 || SYNTAX_PREFIX (c)))
2886 opoint = pos;
2887 opoint_byte = pos_byte;
2889 if (pos + 1 > beg)
2890 DEC_BOTH (pos, pos_byte);
2893 SET_PT_BOTH (opoint, opoint_byte);
2895 return Qnil;
2898 /* Parse forward from FROM / FROM_BYTE to END,
2899 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
2900 and return a description of the state of the parse at END.
2901 If STOPBEFORE is nonzero, stop at the start of an atom.
2902 If COMMENTSTOP is 1, stop at the start of a comment.
2903 If COMMENTSTOP is -1, stop at the start or end of a comment,
2904 after the beginning of a string, or after the end of a string. */
2906 static void
2907 scan_sexps_forward (stateptr, from, from_byte, end, targetdepth,
2908 stopbefore, oldstate, commentstop)
2909 struct lisp_parse_state *stateptr;
2910 register int from;
2911 int from_byte;
2912 int end, targetdepth, stopbefore;
2913 Lisp_Object oldstate;
2914 int commentstop;
2916 struct lisp_parse_state state;
2918 register enum syntaxcode code;
2919 int c1;
2920 int comnested;
2921 struct level { int last, prev; };
2922 struct level levelstart[100];
2923 register struct level *curlevel = levelstart;
2924 struct level *endlevel = levelstart + 100;
2925 register int depth; /* Paren depth of current scanning location.
2926 level - levelstart equals this except
2927 when the depth becomes negative. */
2928 int mindepth; /* Lowest DEPTH value seen. */
2929 int start_quoted = 0; /* Nonzero means starting after a char quote */
2930 Lisp_Object tem;
2931 int prev_from; /* Keep one character before FROM. */
2932 int prev_from_byte;
2933 int prev_from_syntax;
2934 int boundary_stop = commentstop == -1;
2935 int nofence;
2936 int found;
2937 EMACS_INT out_bytepos, out_charpos;
2938 int temp;
2940 prev_from = from;
2941 prev_from_byte = from_byte;
2942 if (from != BEGV)
2943 DEC_BOTH (prev_from, prev_from_byte);
2945 /* Use this macro instead of `from++'. */
2946 #define INC_FROM \
2947 do { prev_from = from; \
2948 prev_from_byte = from_byte; \
2949 temp = FETCH_CHAR_AS_MULTIBYTE (prev_from_byte); \
2950 prev_from_syntax = SYNTAX_WITH_FLAGS (temp); \
2951 INC_BOTH (from, from_byte); \
2952 if (from < end) \
2953 UPDATE_SYNTAX_TABLE_FORWARD (from); \
2954 } while (0)
2956 immediate_quit = 1;
2957 QUIT;
2959 if (NILP (oldstate))
2961 depth = 0;
2962 state.instring = -1;
2963 state.incomment = 0;
2964 state.comstyle = 0; /* comment style a by default. */
2965 state.comstr_start = -1; /* no comment/string seen. */
2967 else
2969 tem = Fcar (oldstate);
2970 if (!NILP (tem))
2971 depth = XINT (tem);
2972 else
2973 depth = 0;
2975 oldstate = Fcdr (oldstate);
2976 oldstate = Fcdr (oldstate);
2977 oldstate = Fcdr (oldstate);
2978 tem = Fcar (oldstate);
2979 /* Check whether we are inside string_fence-style string: */
2980 state.instring = (!NILP (tem)
2981 ? (INTEGERP (tem) ? XINT (tem) : ST_STRING_STYLE)
2982 : -1);
2984 oldstate = Fcdr (oldstate);
2985 tem = Fcar (oldstate);
2986 state.incomment = (!NILP (tem)
2987 ? (INTEGERP (tem) ? XINT (tem) : -1)
2988 : 0);
2990 oldstate = Fcdr (oldstate);
2991 tem = Fcar (oldstate);
2992 start_quoted = !NILP (tem);
2994 /* if the eighth element of the list is nil, we are in comment
2995 style a. If it is non-nil, we are in comment style b */
2996 oldstate = Fcdr (oldstate);
2997 oldstate = Fcdr (oldstate);
2998 tem = Fcar (oldstate);
2999 state.comstyle = NILP (tem) ? 0 : (EQ (tem, Qsyntax_table)
3000 ? ST_COMMENT_STYLE : 1);
3002 oldstate = Fcdr (oldstate);
3003 tem = Fcar (oldstate);
3004 state.comstr_start = NILP (tem) ? -1 : XINT (tem) ;
3005 oldstate = Fcdr (oldstate);
3006 tem = Fcar (oldstate);
3007 while (!NILP (tem)) /* >= second enclosing sexps. */
3009 /* curlevel++->last ran into compiler bug on Apollo */
3010 curlevel->last = XINT (Fcar (tem));
3011 if (++curlevel == endlevel)
3012 curlevel--; /* error ("Nesting too deep for parser"); */
3013 curlevel->prev = -1;
3014 curlevel->last = -1;
3015 tem = Fcdr (tem);
3018 state.quoted = 0;
3019 mindepth = depth;
3021 curlevel->prev = -1;
3022 curlevel->last = -1;
3024 SETUP_SYNTAX_TABLE (prev_from, 1);
3025 temp = FETCH_CHAR (prev_from_byte);
3026 prev_from_syntax = SYNTAX_WITH_FLAGS (temp);
3027 UPDATE_SYNTAX_TABLE_FORWARD (from);
3029 /* Enter the loop at a place appropriate for initial state. */
3031 if (state.incomment)
3032 goto startincomment;
3033 if (state.instring >= 0)
3035 nofence = state.instring != ST_STRING_STYLE;
3036 if (start_quoted)
3037 goto startquotedinstring;
3038 goto startinstring;
3040 else if (start_quoted)
3041 goto startquoted;
3043 while (from < end)
3045 INC_FROM;
3046 code = prev_from_syntax & 0xff;
3048 if (from < end
3049 && SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax)
3050 && (c1 = FETCH_CHAR (from_byte),
3051 SYNTAX_COMSTART_SECOND (c1)))
3052 /* Duplicate code to avoid a complex if-expression
3053 which causes trouble for the SGI compiler. */
3055 /* Record the comment style we have entered so that only
3056 the comment-end sequence of the same style actually
3057 terminates the comment section. */
3058 state.comstyle = SYNTAX_COMMENT_STYLE (c1);
3059 comnested = SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax);
3060 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
3061 state.incomment = comnested ? 1 : -1;
3062 state.comstr_start = prev_from;
3063 INC_FROM;
3064 code = Scomment;
3066 else if (code == Scomment_fence)
3068 /* Record the comment style we have entered so that only
3069 the comment-end sequence of the same style actually
3070 terminates the comment section. */
3071 state.comstyle = ST_COMMENT_STYLE;
3072 state.incomment = -1;
3073 state.comstr_start = prev_from;
3074 code = Scomment;
3076 else if (code == Scomment)
3078 state.comstyle = SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax);
3079 state.incomment = (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax) ?
3080 1 : -1);
3081 state.comstr_start = prev_from;
3084 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax))
3085 continue;
3086 switch (SWITCH_ENUM_CAST (code))
3088 case Sescape:
3089 case Scharquote:
3090 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3091 curlevel->last = prev_from;
3092 startquoted:
3093 if (from == end) goto endquoted;
3094 INC_FROM;
3095 goto symstarted;
3096 /* treat following character as a word constituent */
3097 case Sword:
3098 case Ssymbol:
3099 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3100 curlevel->last = prev_from;
3101 symstarted:
3102 while (from < end)
3104 /* Some compilers can't handle this inside the switch. */
3105 temp = FETCH_CHAR_AS_MULTIBYTE (from_byte);
3106 temp = SYNTAX (temp);
3107 switch (temp)
3109 case Scharquote:
3110 case Sescape:
3111 INC_FROM;
3112 if (from == end) goto endquoted;
3113 break;
3114 case Sword:
3115 case Ssymbol:
3116 case Squote:
3117 break;
3118 default:
3119 goto symdone;
3121 INC_FROM;
3123 symdone:
3124 curlevel->prev = curlevel->last;
3125 break;
3127 case Scomment_fence: /* Can't happen because it's handled above. */
3128 case Scomment:
3129 if (commentstop || boundary_stop) goto done;
3130 startincomment:
3131 /* The (from == BEGV) test was to enter the loop in the middle so
3132 that we find a 2-char comment ender even if we start in the
3133 middle of it. We don't want to do that if we're just at the
3134 beginning of the comment (think of (*) ... (*)). */
3135 found = forw_comment (from, from_byte, end,
3136 state.incomment, state.comstyle,
3137 (from == BEGV || from < state.comstr_start + 3)
3138 ? 0 : prev_from_syntax,
3139 &out_charpos, &out_bytepos, &state.incomment);
3140 from = out_charpos; from_byte = out_bytepos;
3141 /* Beware! prev_from and friends are invalid now.
3142 Luckily, the `done' doesn't use them and the INC_FROM
3143 sets them to a sane value without looking at them. */
3144 if (!found) goto done;
3145 INC_FROM;
3146 state.incomment = 0;
3147 state.comstyle = 0; /* reset the comment style */
3148 if (boundary_stop) goto done;
3149 break;
3151 case Sopen:
3152 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3153 depth++;
3154 /* curlevel++->last ran into compiler bug on Apollo */
3155 curlevel->last = prev_from;
3156 if (++curlevel == endlevel)
3157 curlevel--; /* error ("Nesting too deep for parser"); */
3158 curlevel->prev = -1;
3159 curlevel->last = -1;
3160 if (targetdepth == depth) goto done;
3161 break;
3163 case Sclose:
3164 depth--;
3165 if (depth < mindepth)
3166 mindepth = depth;
3167 if (curlevel != levelstart)
3168 curlevel--;
3169 curlevel->prev = curlevel->last;
3170 if (targetdepth == depth) goto done;
3171 break;
3173 case Sstring:
3174 case Sstring_fence:
3175 state.comstr_start = from - 1;
3176 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3177 curlevel->last = prev_from;
3178 state.instring = (code == Sstring
3179 ? (FETCH_CHAR_AS_MULTIBYTE (prev_from_byte))
3180 : ST_STRING_STYLE);
3181 if (boundary_stop) goto done;
3182 startinstring:
3184 nofence = state.instring != ST_STRING_STYLE;
3186 while (1)
3188 int c;
3190 if (from >= end) goto done;
3191 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
3192 /* Some compilers can't handle this inside the switch. */
3193 temp = SYNTAX (c);
3195 /* Check TEMP here so that if the char has
3196 a syntax-table property which says it is NOT
3197 a string character, it does not end the string. */
3198 if (nofence && c == state.instring && temp == Sstring)
3199 break;
3201 switch (temp)
3203 case Sstring_fence:
3204 if (!nofence) goto string_end;
3205 break;
3206 case Scharquote:
3207 case Sescape:
3208 INC_FROM;
3209 startquotedinstring:
3210 if (from >= end) goto endquoted;
3212 INC_FROM;
3215 string_end:
3216 state.instring = -1;
3217 curlevel->prev = curlevel->last;
3218 INC_FROM;
3219 if (boundary_stop) goto done;
3220 break;
3222 case Smath:
3223 /* FIXME: We should do something with it. */
3224 break;
3225 default:
3226 /* Ignore whitespace, punctuation, quote, endcomment. */
3227 break;
3230 goto done;
3232 stop: /* Here if stopping before start of sexp. */
3233 from = prev_from; /* We have just fetched the char that starts it; */
3234 goto done; /* but return the position before it. */
3236 endquoted:
3237 state.quoted = 1;
3238 done:
3239 state.depth = depth;
3240 state.mindepth = mindepth;
3241 state.thislevelstart = curlevel->prev;
3242 state.prevlevelstart
3243 = (curlevel == levelstart) ? -1 : (curlevel - 1)->last;
3244 state.location = from;
3245 state.levelstarts = Qnil;
3246 while (--curlevel >= levelstart)
3247 state.levelstarts = Fcons (make_number (curlevel->last),
3248 state.levelstarts);
3249 immediate_quit = 0;
3251 *stateptr = state;
3254 DEFUN ("parse-partial-sexp", Fparse_partial_sexp, Sparse_partial_sexp, 2, 6, 0,
3255 doc: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
3256 Parsing stops at TO or when certain criteria are met;
3257 point is set to where parsing stops.
3258 If fifth arg OLDSTATE is omitted or nil,
3259 parsing assumes that FROM is the beginning of a function.
3260 Value is a list of elements describing final state of parsing:
3261 0. depth in parens.
3262 1. character address of start of innermost containing list; nil if none.
3263 2. character address of start of last complete sexp terminated.
3264 3. non-nil if inside a string.
3265 (it is the character that will terminate the string,
3266 or t if the string should be terminated by a generic string delimiter.)
3267 4. nil if outside a comment, t if inside a non-nestable comment,
3268 else an integer (the current comment nesting).
3269 5. t if following a quote character.
3270 6. the minimum paren-depth encountered during this scan.
3271 7. t if in a comment of style b; symbol `syntax-table' if the comment
3272 should be terminated by a generic comment delimiter.
3273 8. character address of start of comment or string; nil if not in one.
3274 9. Intermediate data for continuation of parsing (subject to change).
3275 If third arg TARGETDEPTH is non-nil, parsing stops if the depth
3276 in parentheses becomes equal to TARGETDEPTH.
3277 Fourth arg STOPBEFORE non-nil means stop when come to
3278 any character that starts a sexp.
3279 Fifth arg OLDSTATE is a list like what this function returns.
3280 It is used to initialize the state of the parse. Elements number 1, 2, 6
3281 and 8 are ignored.
3282 Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.
3283 If it is symbol `syntax-table', stop after the start of a comment or a
3284 string, or after end of a comment or a string. */)
3285 (from, to, targetdepth, stopbefore, oldstate, commentstop)
3286 Lisp_Object from, to, targetdepth, stopbefore, oldstate, commentstop;
3288 struct lisp_parse_state state;
3289 int target;
3291 if (!NILP (targetdepth))
3293 CHECK_NUMBER (targetdepth);
3294 target = XINT (targetdepth);
3296 else
3297 target = -100000; /* We won't reach this depth */
3299 validate_region (&from, &to);
3300 scan_sexps_forward (&state, XINT (from), CHAR_TO_BYTE (XINT (from)),
3301 XINT (to),
3302 target, !NILP (stopbefore), oldstate,
3303 (NILP (commentstop)
3304 ? 0 : (EQ (commentstop, Qsyntax_table) ? -1 : 1)));
3306 SET_PT (state.location);
3308 return Fcons (make_number (state.depth),
3309 Fcons (state.prevlevelstart < 0 ? Qnil : make_number (state.prevlevelstart),
3310 Fcons (state.thislevelstart < 0 ? Qnil : make_number (state.thislevelstart),
3311 Fcons (state.instring >= 0
3312 ? (state.instring == ST_STRING_STYLE
3313 ? Qt : make_number (state.instring)) : Qnil,
3314 Fcons (state.incomment < 0 ? Qt :
3315 (state.incomment == 0 ? Qnil :
3316 make_number (state.incomment)),
3317 Fcons (state.quoted ? Qt : Qnil,
3318 Fcons (make_number (state.mindepth),
3319 Fcons ((state.comstyle
3320 ? (state.comstyle == ST_COMMENT_STYLE
3321 ? Qsyntax_table : Qt) :
3322 Qnil),
3323 Fcons (((state.incomment
3324 || (state.instring >= 0))
3325 ? make_number (state.comstr_start)
3326 : Qnil),
3327 Fcons (state.levelstarts, Qnil))))))))));
3330 void
3331 init_syntax_once ()
3333 register int i, c;
3334 Lisp_Object temp;
3336 /* This has to be done here, before we call Fmake_char_table. */
3337 Qsyntax_table = intern ("syntax-table");
3338 staticpro (&Qsyntax_table);
3340 /* Intern this now in case it isn't already done.
3341 Setting this variable twice is harmless.
3342 But don't staticpro it here--that is done in alloc.c. */
3343 Qchar_table_extra_slots = intern ("char-table-extra-slots");
3345 /* Create objects which can be shared among syntax tables. */
3346 Vsyntax_code_object = Fmake_vector (make_number (Smax), Qnil);
3347 for (i = 0; i < XVECTOR (Vsyntax_code_object)->size; i++)
3348 XVECTOR (Vsyntax_code_object)->contents[i]
3349 = Fcons (make_number (i), Qnil);
3351 /* Now we are ready to set up this property, so we can
3352 create syntax tables. */
3353 Fput (Qsyntax_table, Qchar_table_extra_slots, make_number (0));
3355 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Swhitespace];
3357 Vstandard_syntax_table = Fmake_char_table (Qsyntax_table, temp);
3359 /* Control characters should not be whitespace. */
3360 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Spunct];
3361 for (i = 0; i <= ' ' - 1; i++)
3362 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3363 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 0177, temp);
3365 /* Except that a few really are whitespace. */
3366 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Swhitespace];
3367 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ' ', temp);
3368 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\t', temp);
3369 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\n', temp);
3370 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 015, temp);
3371 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 014, temp);
3373 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Sword];
3374 for (i = 'a'; i <= 'z'; i++)
3375 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3376 for (i = 'A'; i <= 'Z'; i++)
3377 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3378 for (i = '0'; i <= '9'; i++)
3379 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3381 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '$', temp);
3382 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '%', temp);
3384 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '(',
3385 Fcons (make_number (Sopen), make_number (')')));
3386 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ')',
3387 Fcons (make_number (Sclose), make_number ('(')));
3388 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '[',
3389 Fcons (make_number (Sopen), make_number (']')));
3390 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ']',
3391 Fcons (make_number (Sclose), make_number ('[')));
3392 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '{',
3393 Fcons (make_number (Sopen), make_number ('}')));
3394 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '}',
3395 Fcons (make_number (Sclose), make_number ('{')));
3396 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '"',
3397 Fcons (make_number ((int) Sstring), Qnil));
3398 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\\',
3399 Fcons (make_number ((int) Sescape), Qnil));
3401 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Ssymbol];
3402 for (i = 0; i < 10; i++)
3404 c = "_-+*/&|<>="[i];
3405 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3408 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Spunct];
3409 for (i = 0; i < 12; i++)
3411 c = ".,;:?!#@~^'`"[i];
3412 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3415 /* All multibyte characters have syntax `word' by default. */
3416 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Sword];
3417 char_table_set_range (Vstandard_syntax_table, 0x80, MAX_CHAR, temp);
3420 void
3421 syms_of_syntax ()
3423 Qsyntax_table_p = intern ("syntax-table-p");
3424 staticpro (&Qsyntax_table_p);
3426 staticpro (&Vsyntax_code_object);
3428 staticpro (&gl_state.object);
3429 staticpro (&gl_state.global_code);
3430 staticpro (&gl_state.current_syntax_table);
3431 staticpro (&gl_state.old_prop);
3433 /* Defined in regex.c */
3434 staticpro (&re_match_object);
3436 Qscan_error = intern ("scan-error");
3437 staticpro (&Qscan_error);
3438 Fput (Qscan_error, Qerror_conditions,
3439 Fcons (Qscan_error, Fcons (Qerror, Qnil)));
3440 Fput (Qscan_error, Qerror_message,
3441 build_string ("Scan error"));
3443 DEFVAR_BOOL ("parse-sexp-ignore-comments", &parse_sexp_ignore_comments,
3444 doc: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
3446 DEFVAR_BOOL ("parse-sexp-lookup-properties", &parse_sexp_lookup_properties,
3447 doc: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3448 Otherwise, that text property is simply ignored.
3449 See the info node `(elisp)Syntax Properties' for a description of the
3450 `syntax-table' property. */);
3452 words_include_escapes = 0;
3453 DEFVAR_BOOL ("words-include-escapes", &words_include_escapes,
3454 doc: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
3456 DEFVAR_BOOL ("multibyte-syntax-as-symbol", &multibyte_syntax_as_symbol,
3457 doc: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
3458 multibyte_syntax_as_symbol = 0;
3460 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3461 &open_paren_in_column_0_is_defun_start,
3462 doc: /* *Non-nil means an open paren in column 0 denotes the start of a defun. */);
3463 open_paren_in_column_0_is_defun_start = 1;
3466 DEFVAR_LISP ("find-word-boundary-function-table",
3467 &Vfind_word_boundary_function_table,
3468 doc: /*
3469 Char table of functions to search for the word boundary.
3470 Each function is called with two arguments; POS and LIMIT.
3471 POS and LIMIT are character positions in the current buffer.
3473 If POS is less than LIMIT, POS is at the first character of a word,
3474 and the return value of a function is a position after the last
3475 character of that word.
3477 If POS is not less than LIMIT, POS is at the last character of a word,
3478 and the return value of a function is a position at the first
3479 character of that word.
3481 In both cases, LIMIT bounds the search. */);
3482 Vfind_word_boundary_function_table = Fmake_char_table (Qnil, Qnil);
3484 defsubr (&Ssyntax_table_p);
3485 defsubr (&Ssyntax_table);
3486 defsubr (&Sstandard_syntax_table);
3487 defsubr (&Scopy_syntax_table);
3488 defsubr (&Sset_syntax_table);
3489 defsubr (&Schar_syntax);
3490 defsubr (&Smatching_paren);
3491 defsubr (&Sstring_to_syntax);
3492 defsubr (&Smodify_syntax_entry);
3493 defsubr (&Sinternal_describe_syntax_value);
3495 defsubr (&Sforward_word);
3497 defsubr (&Sskip_chars_forward);
3498 defsubr (&Sskip_chars_backward);
3499 defsubr (&Sskip_syntax_forward);
3500 defsubr (&Sskip_syntax_backward);
3502 defsubr (&Sforward_comment);
3503 defsubr (&Sscan_lists);
3504 defsubr (&Sscan_sexps);
3505 defsubr (&Sbackward_prefix_chars);
3506 defsubr (&Sparse_partial_sexp);
3509 /* arch-tag: 3e297b9f-088e-4b64-8f4c-fb0b3443e412
3510 (do not change this comment) */