(Fprimitive_undo): Check veracity of delta,start,end.
[bpt/emacs.git] / src / syntax.c
1 /* GNU Emacs routines to deal with syntax tables; also word and list parsing.
2 Copyright (C) 1985, 87, 93, 94, 95, 97, 1998, 1999, 2004 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21
22 #include <config.h>
23 #include <ctype.h>
24 #include "lisp.h"
25 #include "commands.h"
26 #include "buffer.h"
27 #include "charset.h"
28 #include "keymap.h"
29 #include "regex.h"
30
31 /* Make syntax table lookup grant data in gl_state. */
32 #define SYNTAX_ENTRY_VIA_PROPERTY
33
34 #include "syntax.h"
35 #include "intervals.h"
36
37 /* We use these constants in place for comment-style and
38 string-ender-char to distinguish comments/strings started by
39 comment_fence and string_fence codes. */
40
41 #define ST_COMMENT_STYLE (256 + 1)
42 #define ST_STRING_STYLE (256 + 2)
43 #include "category.h"
44
45 Lisp_Object Qsyntax_table_p, Qsyntax_table, Qscan_error;
46
47 int words_include_escapes;
48 int parse_sexp_lookup_properties;
49
50 /* Nonzero means `scan-sexps' treat all multibyte characters as symbol. */
51 int multibyte_syntax_as_symbol;
52
53 /* Used as a temporary in SYNTAX_ENTRY and other macros in syntax.h,
54 if not compiled with GCC. No need to mark it, since it is used
55 only very temporarily. */
56 Lisp_Object syntax_temp;
57
58 /* Non-zero means an open parenthesis in column 0 is always considered
59 to be the start of a defun. Zero means an open parenthesis in
60 column 0 has no special meaning. */
61
62 int open_paren_in_column_0_is_defun_start;
63
64 /* This is the internal form of the parse state used in parse-partial-sexp. */
65
66 struct lisp_parse_state
67 {
68 int depth; /* Depth at end of parsing. */
69 int instring; /* -1 if not within string, else desired terminator. */
70 int incomment; /* -1 if in unnestable comment else comment nesting */
71 int comstyle; /* comment style a=0, or b=1, or ST_COMMENT_STYLE. */
72 int quoted; /* Nonzero if just after an escape char at end of parsing */
73 int thislevelstart; /* Char number of most recent start-of-expression at current level */
74 int prevlevelstart; /* Char number of start of containing expression */
75 int location; /* Char number at which parsing stopped. */
76 int mindepth; /* Minimum depth seen while scanning. */
77 int comstr_start; /* Position just after last comment/string starter. */
78 Lisp_Object levelstarts; /* Char numbers of starts-of-expression
79 of levels (starting from outermost). */
80 };
81 \f
82 /* These variables are a cache for finding the start of a defun.
83 find_start_pos is the place for which the defun start was found.
84 find_start_value is the defun start position found for it.
85 find_start_value_byte is the corresponding byte position.
86 find_start_buffer is the buffer it was found in.
87 find_start_begv is the BEGV value when it was found.
88 find_start_modiff is the value of MODIFF when it was found. */
89
90 static int find_start_pos;
91 static int find_start_value;
92 static int find_start_value_byte;
93 static struct buffer *find_start_buffer;
94 static int find_start_begv;
95 static int find_start_modiff;
96
97
98 static int find_defun_start P_ ((int, int));
99 static int back_comment P_ ((int, int, int, int, int, int *, int *));
100 static int char_quoted P_ ((int, int));
101 static Lisp_Object skip_chars P_ ((int, int, Lisp_Object, Lisp_Object, int));
102 static Lisp_Object scan_lists P_ ((int, int, int, int));
103 static void scan_sexps_forward P_ ((struct lisp_parse_state *,
104 int, int, int, int,
105 int, Lisp_Object, int));
106 static int in_classes P_ ((int, Lisp_Object));
107 \f
108
109 struct gl_state_s gl_state; /* Global state of syntax parser. */
110
111 INTERVAL interval_of ();
112 #define INTERVALS_AT_ONCE 10 /* 1 + max-number of intervals
113 to scan to property-change. */
114
115 /* Update gl_state to an appropriate interval which contains CHARPOS. The
116 sign of COUNT give the relative position of CHARPOS wrt the previously
117 valid interval. If INIT, only [be]_property fields of gl_state are
118 valid at start, the rest is filled basing on OBJECT.
119
120 `gl_state.*_i' are the intervals, and CHARPOS is further in the search
121 direction than the intervals - or in an interval. We update the
122 current syntax-table basing on the property of this interval, and
123 update the interval to start further than CHARPOS - or be
124 NULL_INTERVAL. We also update lim_property to be the next value of
125 charpos to call this subroutine again - or be before/after the
126 start/end of OBJECT. */
127
128 void
129 update_syntax_table (charpos, count, init, object)
130 int charpos, count, init;
131 Lisp_Object object;
132 {
133 Lisp_Object tmp_table;
134 int cnt = 0, invalidate = 1;
135 INTERVAL i, oldi;
136
137 if (init)
138 {
139 gl_state.old_prop = Qnil;
140 gl_state.start = gl_state.b_property;
141 gl_state.stop = gl_state.e_property;
142 i = interval_of (charpos, object);
143 gl_state.backward_i = gl_state.forward_i = i;
144 invalidate = 0;
145 if (NULL_INTERVAL_P (i))
146 return;
147 /* interval_of updates only ->position of the return value, so
148 update the parents manually to speed up update_interval. */
149 while (!NULL_PARENT (i))
150 {
151 if (AM_RIGHT_CHILD (i))
152 INTERVAL_PARENT (i)->position = i->position
153 - LEFT_TOTAL_LENGTH (i) + TOTAL_LENGTH (i) /* right end */
154 - TOTAL_LENGTH (INTERVAL_PARENT (i))
155 + LEFT_TOTAL_LENGTH (INTERVAL_PARENT (i));
156 else
157 INTERVAL_PARENT (i)->position = i->position - LEFT_TOTAL_LENGTH (i)
158 + TOTAL_LENGTH (i);
159 i = INTERVAL_PARENT (i);
160 }
161 i = gl_state.forward_i;
162 gl_state.b_property = i->position - gl_state.offset;
163 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
164 goto update;
165 }
166 oldi = i = count > 0 ? gl_state.forward_i : gl_state.backward_i;
167
168 /* We are guaranteed to be called with CHARPOS either in i,
169 or further off. */
170 if (NULL_INTERVAL_P (i))
171 error ("Error in syntax_table logic for to-the-end intervals");
172 else if (charpos < i->position) /* Move left. */
173 {
174 if (count > 0)
175 error ("Error in syntax_table logic for intervals <-");
176 /* Update the interval. */
177 i = update_interval (i, charpos);
178 if (INTERVAL_LAST_POS (i) != gl_state.b_property)
179 {
180 invalidate = 0;
181 gl_state.forward_i = i;
182 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
183 }
184 }
185 else if (charpos >= INTERVAL_LAST_POS (i)) /* Move right. */
186 {
187 if (count < 0)
188 error ("Error in syntax_table logic for intervals ->");
189 /* Update the interval. */
190 i = update_interval (i, charpos);
191 if (i->position != gl_state.e_property)
192 {
193 invalidate = 0;
194 gl_state.backward_i = i;
195 gl_state.b_property = i->position - gl_state.offset;
196 }
197 }
198
199 update:
200 tmp_table = textget (i->plist, Qsyntax_table);
201
202 if (invalidate)
203 invalidate = !EQ (tmp_table, gl_state.old_prop); /* Need to invalidate? */
204
205 if (invalidate) /* Did not get to adjacent interval. */
206 { /* with the same table => */
207 /* invalidate the old range. */
208 if (count > 0)
209 {
210 gl_state.backward_i = i;
211 gl_state.b_property = i->position - gl_state.offset;
212 }
213 else
214 {
215 gl_state.forward_i = i;
216 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
217 }
218 }
219
220 if (!EQ (tmp_table, gl_state.old_prop))
221 {
222 gl_state.current_syntax_table = tmp_table;
223 gl_state.old_prop = tmp_table;
224 if (EQ (Fsyntax_table_p (tmp_table), Qt))
225 {
226 gl_state.use_global = 0;
227 }
228 else if (CONSP (tmp_table))
229 {
230 gl_state.use_global = 1;
231 gl_state.global_code = tmp_table;
232 }
233 else
234 {
235 gl_state.use_global = 0;
236 gl_state.current_syntax_table = current_buffer->syntax_table;
237 }
238 }
239
240 while (!NULL_INTERVAL_P (i))
241 {
242 if (cnt && !EQ (tmp_table, textget (i->plist, Qsyntax_table)))
243 {
244 if (count > 0)
245 {
246 gl_state.e_property = i->position - gl_state.offset;
247 gl_state.forward_i = i;
248 }
249 else
250 {
251 gl_state.b_property = i->position + LENGTH (i) - gl_state.offset;
252 gl_state.backward_i = i;
253 }
254 return;
255 }
256 else if (cnt == INTERVALS_AT_ONCE)
257 {
258 if (count > 0)
259 {
260 gl_state.e_property = i->position + LENGTH (i) - gl_state.offset;
261 gl_state.forward_i = i;
262 }
263 else
264 {
265 gl_state.b_property = i->position - gl_state.offset;
266 gl_state.backward_i = i;
267 }
268 return;
269 }
270 cnt++;
271 i = count > 0 ? next_interval (i) : previous_interval (i);
272 }
273 eassert (NULL_INTERVAL_P (i)); /* This property goes to the end. */
274 if (count > 0)
275 gl_state.e_property = gl_state.stop;
276 else
277 gl_state.b_property = gl_state.start;
278 }
279 \f
280 /* Returns TRUE if char at CHARPOS is quoted.
281 Global syntax-table data should be set up already to be good at CHARPOS
282 or after. On return global syntax data is good for lookup at CHARPOS. */
283
284 static int
285 char_quoted (charpos, bytepos)
286 register int charpos, bytepos;
287 {
288 register enum syntaxcode code;
289 register int beg = BEGV;
290 register int quoted = 0;
291 int orig = charpos;
292
293 DEC_BOTH (charpos, bytepos);
294
295 while (charpos >= beg)
296 {
297 int c;
298
299 UPDATE_SYNTAX_TABLE_BACKWARD (charpos);
300 c = FETCH_CHAR (bytepos);
301 code = SYNTAX (c);
302 if (! (code == Scharquote || code == Sescape))
303 break;
304
305 DEC_BOTH (charpos, bytepos);
306 quoted = !quoted;
307 }
308
309 UPDATE_SYNTAX_TABLE (orig);
310 return quoted;
311 }
312
313 /* Return the bytepos one character after BYTEPOS.
314 We assume that BYTEPOS is not at the end of the buffer. */
315
316 INLINE int
317 inc_bytepos (bytepos)
318 int bytepos;
319 {
320 if (NILP (current_buffer->enable_multibyte_characters))
321 return bytepos + 1;
322
323 INC_POS (bytepos);
324 return bytepos;
325 }
326
327 /* Return the bytepos one character before BYTEPOS.
328 We assume that BYTEPOS is not at the start of the buffer. */
329
330 INLINE int
331 dec_bytepos (bytepos)
332 int bytepos;
333 {
334 if (NILP (current_buffer->enable_multibyte_characters))
335 return bytepos - 1;
336
337 DEC_POS (bytepos);
338 return bytepos;
339 }
340 \f
341 /* Return a defun-start position before before POS and not too far before.
342 It should be the last one before POS, or nearly the last.
343
344 When open_paren_in_column_0_is_defun_start is nonzero,
345 only the beginning of the buffer is treated as a defun-start.
346
347 We record the information about where the scan started
348 and what its result was, so that another call in the same area
349 can return the same value very quickly.
350
351 There is no promise at which position the global syntax data is
352 valid on return from the subroutine, so the caller should explicitly
353 update the global data. */
354
355 static int
356 find_defun_start (pos, pos_byte)
357 int pos, pos_byte;
358 {
359 int opoint = PT, opoint_byte = PT_BYTE;
360
361 if (!open_paren_in_column_0_is_defun_start)
362 {
363 find_start_value_byte = BEGV_BYTE;
364 return BEGV;
365 }
366
367 /* Use previous finding, if it's valid and applies to this inquiry. */
368 if (current_buffer == find_start_buffer
369 /* Reuse the defun-start even if POS is a little farther on.
370 POS might be in the next defun, but that's ok.
371 Our value may not be the best possible, but will still be usable. */
372 && pos <= find_start_pos + 1000
373 && pos >= find_start_value
374 && BEGV == find_start_begv
375 && MODIFF == find_start_modiff)
376 return find_start_value;
377
378 /* Back up to start of line. */
379 scan_newline (pos, pos_byte, BEGV, BEGV_BYTE, -1, 1);
380
381 /* We optimize syntax-table lookup for rare updates. Thus we accept
382 only those `^\s(' which are good in global _and_ text-property
383 syntax-tables. */
384 gl_state.current_syntax_table = current_buffer->syntax_table;
385 gl_state.use_global = 0;
386 while (PT > BEGV)
387 {
388 int c;
389
390 /* Open-paren at start of line means we may have found our
391 defun-start. */
392 c = FETCH_CHAR (PT_BYTE);
393 if (SYNTAX (c) == Sopen)
394 {
395 SETUP_SYNTAX_TABLE (PT + 1, -1); /* Try again... */
396 c = FETCH_CHAR (PT_BYTE);
397 if (SYNTAX (c) == Sopen)
398 break;
399 /* Now fallback to the default value. */
400 gl_state.current_syntax_table = current_buffer->syntax_table;
401 gl_state.use_global = 0;
402 }
403 /* Move to beg of previous line. */
404 scan_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, -2, 1);
405 }
406
407 /* Record what we found, for the next try. */
408 find_start_value = PT;
409 find_start_value_byte = PT_BYTE;
410 find_start_buffer = current_buffer;
411 find_start_modiff = MODIFF;
412 find_start_begv = BEGV;
413 find_start_pos = pos;
414
415 TEMP_SET_PT_BOTH (opoint, opoint_byte);
416
417 return find_start_value;
418 }
419 \f
420 /* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
421
422 static int
423 prev_char_comend_first (pos, pos_byte)
424 int pos, pos_byte;
425 {
426 int c, val;
427
428 DEC_BOTH (pos, pos_byte);
429 UPDATE_SYNTAX_TABLE_BACKWARD (pos);
430 c = FETCH_CHAR (pos_byte);
431 val = SYNTAX_COMEND_FIRST (c);
432 UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
433 return val;
434 }
435
436 /* Return the SYNTAX_COMSTART_FIRST of the character before POS, POS_BYTE. */
437
438 /* static int
439 * prev_char_comstart_first (pos, pos_byte)
440 * int pos, pos_byte;
441 * {
442 * int c, val;
443 *
444 * DEC_BOTH (pos, pos_byte);
445 * UPDATE_SYNTAX_TABLE_BACKWARD (pos);
446 * c = FETCH_CHAR (pos_byte);
447 * val = SYNTAX_COMSTART_FIRST (c);
448 * UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
449 * return val;
450 * } */
451
452 /* Checks whether charpos FROM is at the end of a comment.
453 FROM_BYTE is the bytepos corresponding to FROM.
454 Do not move back before STOP.
455
456 Return a positive value if we find a comment ending at FROM/FROM_BYTE;
457 return -1 otherwise.
458
459 If successful, store the charpos of the comment's beginning
460 into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR.
461
462 Global syntax data remains valid for backward search starting at
463 the returned value (or at FROM, if the search was not successful). */
464
465 static int
466 back_comment (from, from_byte, stop, comnested, comstyle, charpos_ptr, bytepos_ptr)
467 int from, from_byte, stop;
468 int comnested, comstyle;
469 int *charpos_ptr, *bytepos_ptr;
470 {
471 /* Look back, counting the parity of string-quotes,
472 and recording the comment-starters seen.
473 When we reach a safe place, assume that's not in a string;
474 then step the main scan to the earliest comment-starter seen
475 an even number of string quotes away from the safe place.
476
477 OFROM[I] is position of the earliest comment-starter seen
478 which is I+2X quotes from the comment-end.
479 PARITY is current parity of quotes from the comment end. */
480 int string_style = -1; /* Presumed outside of any string. */
481 int string_lossage = 0;
482 /* Not a real lossage: indicates that we have passed a matching comment
483 starter plus a non-matching comment-ender, meaning that any matching
484 comment-starter we might see later could be a false positive (hidden
485 inside another comment).
486 Test case: { a (* b } c (* d *) */
487 int comment_lossage = 0;
488 int comment_end = from;
489 int comment_end_byte = from_byte;
490 int comstart_pos = 0;
491 int comstart_byte;
492 /* Place where the containing defun starts,
493 or 0 if we didn't come across it yet. */
494 int defun_start = 0;
495 int defun_start_byte = 0;
496 register enum syntaxcode code;
497 int nesting = 1; /* current comment nesting */
498 int c;
499 int syntax = 0;
500
501 /* FIXME: A }} comment-ender style leads to incorrect behavior
502 in the case of {{ c }}} because we ignore the last two chars which are
503 assumed to be comment-enders although they aren't. */
504
505 /* At beginning of range to scan, we're outside of strings;
506 that determines quote parity to the comment-end. */
507 while (from != stop)
508 {
509 int temp_byte, prev_syntax;
510 int com2start, com2end;
511
512 /* Move back and examine a character. */
513 DEC_BOTH (from, from_byte);
514 UPDATE_SYNTAX_TABLE_BACKWARD (from);
515
516 prev_syntax = syntax;
517 c = FETCH_CHAR (from_byte);
518 syntax = SYNTAX_WITH_FLAGS (c);
519 code = SYNTAX (c);
520
521 /* Check for 2-char comment markers. */
522 com2start = (SYNTAX_FLAGS_COMSTART_FIRST (syntax)
523 && SYNTAX_FLAGS_COMSTART_SECOND (prev_syntax)
524 && comstyle == SYNTAX_FLAGS_COMMENT_STYLE (prev_syntax)
525 && (SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax)
526 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested);
527 com2end = (SYNTAX_FLAGS_COMEND_FIRST (syntax)
528 && SYNTAX_FLAGS_COMEND_SECOND (prev_syntax));
529
530 /* Nasty cases with overlapping 2-char comment markers:
531 - snmp-mode: -- c -- foo -- c --
532 --- c --
533 ------ c --
534 - c-mode: *||*
535 |* *|* *|
536 |*| |* |*|
537 /// */
538
539 /* If a 2-char comment sequence partly overlaps with another,
540 we don't try to be clever. */
541 if (from > stop && (com2end || com2start))
542 {
543 int next = from, next_byte = from_byte, next_c, next_syntax;
544 DEC_BOTH (next, next_byte);
545 UPDATE_SYNTAX_TABLE_BACKWARD (next);
546 next_c = FETCH_CHAR (next_byte);
547 next_syntax = SYNTAX_WITH_FLAGS (next_c);
548 if (((com2start || comnested)
549 && SYNTAX_FLAGS_COMEND_SECOND (syntax)
550 && SYNTAX_FLAGS_COMEND_FIRST (next_syntax))
551 || ((com2end || comnested)
552 && SYNTAX_FLAGS_COMSTART_SECOND (syntax)
553 && comstyle == SYNTAX_FLAGS_COMMENT_STYLE (syntax)
554 && SYNTAX_FLAGS_COMSTART_FIRST (next_syntax)))
555 goto lossage;
556 /* UPDATE_SYNTAX_TABLE_FORWARD (next + 1); */
557 }
558
559 if (com2start && comstart_pos == 0)
560 /* We're looking at a comment starter. But it might be a comment
561 ender as well (see snmp-mode). The first time we see one, we
562 need to consider it as a comment starter,
563 and the subsequent times as a comment ender. */
564 com2end = 0;
565
566 /* Turn a 2-char comment sequences into the appropriate syntax. */
567 if (com2end)
568 code = Sendcomment;
569 else if (com2start)
570 code = Scomment;
571 /* Ignore comment starters of a different style. */
572 else if (code == Scomment
573 && (comstyle != SYNTAX_FLAGS_COMMENT_STYLE (syntax)
574 || SYNTAX_FLAGS_COMMENT_NESTED (syntax) != comnested))
575 continue;
576
577 /* Ignore escaped characters, except comment-enders. */
578 if (code != Sendcomment && char_quoted (from, from_byte))
579 continue;
580
581 switch (code)
582 {
583 case Sstring_fence:
584 case Scomment_fence:
585 c = (code == Sstring_fence ? ST_STRING_STYLE : ST_COMMENT_STYLE);
586 case Sstring:
587 /* Track parity of quotes. */
588 if (string_style == -1)
589 /* Entering a string. */
590 string_style = c;
591 else if (string_style == c)
592 /* Leaving the string. */
593 string_style = -1;
594 else
595 /* If we have two kinds of string delimiters.
596 There's no way to grok this scanning backwards. */
597 string_lossage = 1;
598 break;
599
600 case Scomment:
601 /* We've already checked that it is the relevant comstyle. */
602 if (string_style != -1 || comment_lossage || string_lossage)
603 /* There are odd string quotes involved, so let's be careful.
604 Test case in Pascal: " { " a { " } */
605 goto lossage;
606
607 if (!comnested)
608 {
609 /* Record best comment-starter so far. */
610 comstart_pos = from;
611 comstart_byte = from_byte;
612 }
613 else if (--nesting <= 0)
614 /* nested comments have to be balanced, so we don't need to
615 keep looking for earlier ones. We use here the same (slightly
616 incorrect) reasoning as below: since it is followed by uniform
617 paired string quotes, this comment-start has to be outside of
618 strings, else the comment-end itself would be inside a string. */
619 goto done;
620 break;
621
622 case Sendcomment:
623 if (SYNTAX_FLAGS_COMMENT_STYLE (syntax) == comstyle
624 && ((com2end && SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax))
625 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested)
626 /* This is the same style of comment ender as ours. */
627 {
628 if (comnested)
629 nesting++;
630 else
631 /* Anything before that can't count because it would match
632 this comment-ender rather than ours. */
633 from = stop; /* Break out of the loop. */
634 }
635 else if (comstart_pos != 0 || c != '\n')
636 /* We're mixing comment styles here, so we'd better be careful.
637 The (comstart_pos != 0 || c != '\n') check is not quite correct
638 (we should just always set comment_lossage), but removing it
639 would imply that any multiline comment in C would go through
640 lossage, which seems overkill.
641 The failure should only happen in the rare cases such as
642 { (* } *) */
643 comment_lossage = 1;
644 break;
645
646 case Sopen:
647 /* Assume a defun-start point is outside of strings. */
648 if (open_paren_in_column_0_is_defun_start
649 && (from == stop
650 || (temp_byte = dec_bytepos (from_byte),
651 FETCH_CHAR (temp_byte) == '\n')))
652 {
653 defun_start = from;
654 defun_start_byte = from_byte;
655 from = stop; /* Break out of the loop. */
656 }
657 break;
658
659 default:
660 break;
661 }
662 }
663
664 if (comstart_pos == 0)
665 {
666 from = comment_end;
667 from_byte = comment_end_byte;
668 UPDATE_SYNTAX_TABLE_FORWARD (comment_end - 1);
669 }
670 /* If comstart_pos is set and we get here (ie. didn't jump to `lossage'
671 or `done'), then we've found the beginning of the non-nested comment. */
672 else if (1) /* !comnested */
673 {
674 from = comstart_pos;
675 from_byte = comstart_byte;
676 UPDATE_SYNTAX_TABLE_FORWARD (from - 1);
677 }
678 else
679 {
680 struct lisp_parse_state state;
681 lossage:
682 /* We had two kinds of string delimiters mixed up
683 together. Decode this going forwards.
684 Scan fwd from a known safe place (beginning-of-defun)
685 to the one in question; this records where we
686 last passed a comment starter. */
687 /* If we did not already find the defun start, find it now. */
688 if (defun_start == 0)
689 {
690 defun_start = find_defun_start (comment_end, comment_end_byte);
691 defun_start_byte = find_start_value_byte;
692 }
693 do
694 {
695 scan_sexps_forward (&state,
696 defun_start, defun_start_byte,
697 comment_end, -10000, 0, Qnil, 0);
698 defun_start = comment_end;
699 if (state.incomment == (comnested ? 1 : -1)
700 && state.comstyle == comstyle)
701 from = state.comstr_start;
702 else
703 {
704 from = comment_end;
705 if (state.incomment)
706 /* If comment_end is inside some other comment, maybe ours
707 is nested, so we need to try again from within the
708 surrounding comment. Example: { a (* " *) */
709 {
710 /* FIXME: We should advance by one or two chars. */
711 defun_start = state.comstr_start + 2;
712 defun_start_byte = CHAR_TO_BYTE (defun_start);
713 }
714 }
715 } while (defun_start < comment_end);
716
717 from_byte = CHAR_TO_BYTE (from);
718 UPDATE_SYNTAX_TABLE_FORWARD (from - 1);
719 }
720
721 done:
722 *charpos_ptr = from;
723 *bytepos_ptr = from_byte;
724
725 return (from == comment_end) ? -1 : from;
726 }
727 \f
728 DEFUN ("syntax-table-p", Fsyntax_table_p, Ssyntax_table_p, 1, 1, 0,
729 doc: /* Return t if OBJECT is a syntax table.
730 Currently, any char-table counts as a syntax table. */)
731 (object)
732 Lisp_Object object;
733 {
734 if (CHAR_TABLE_P (object)
735 && EQ (XCHAR_TABLE (object)->purpose, Qsyntax_table))
736 return Qt;
737 return Qnil;
738 }
739
740 static void
741 check_syntax_table (obj)
742 Lisp_Object obj;
743 {
744 if (!(CHAR_TABLE_P (obj)
745 && EQ (XCHAR_TABLE (obj)->purpose, Qsyntax_table)))
746 wrong_type_argument (Qsyntax_table_p, obj);
747 }
748
749 DEFUN ("syntax-table", Fsyntax_table, Ssyntax_table, 0, 0, 0,
750 doc: /* Return the current syntax table.
751 This is the one specified by the current buffer. */)
752 ()
753 {
754 return current_buffer->syntax_table;
755 }
756
757 DEFUN ("standard-syntax-table", Fstandard_syntax_table,
758 Sstandard_syntax_table, 0, 0, 0,
759 doc: /* Return the standard syntax table.
760 This is the one used for new buffers. */)
761 ()
762 {
763 return Vstandard_syntax_table;
764 }
765
766 DEFUN ("copy-syntax-table", Fcopy_syntax_table, Scopy_syntax_table, 0, 1, 0,
767 doc: /* Construct a new syntax table and return it.
768 It is a copy of the TABLE, which defaults to the standard syntax table. */)
769 (table)
770 Lisp_Object table;
771 {
772 Lisp_Object copy;
773
774 if (!NILP (table))
775 check_syntax_table (table);
776 else
777 table = Vstandard_syntax_table;
778
779 copy = Fcopy_sequence (table);
780
781 /* Only the standard syntax table should have a default element.
782 Other syntax tables should inherit from parents instead. */
783 XCHAR_TABLE (copy)->defalt = Qnil;
784
785 /* Copied syntax tables should all have parents.
786 If we copied one with no parent, such as the standard syntax table,
787 use the standard syntax table as the copy's parent. */
788 if (NILP (XCHAR_TABLE (copy)->parent))
789 Fset_char_table_parent (copy, Vstandard_syntax_table);
790 return copy;
791 }
792
793 DEFUN ("set-syntax-table", Fset_syntax_table, Sset_syntax_table, 1, 1, 0,
794 doc: /* Select a new syntax table for the current buffer.
795 One argument, a syntax table. */)
796 (table)
797 Lisp_Object table;
798 {
799 int idx;
800 check_syntax_table (table);
801 current_buffer->syntax_table = table;
802 /* Indicate that this buffer now has a specified syntax table. */
803 idx = PER_BUFFER_VAR_IDX (syntax_table);
804 SET_PER_BUFFER_VALUE_P (current_buffer, idx, 1);
805 return table;
806 }
807 \f
808 /* Convert a letter which signifies a syntax code
809 into the code it signifies.
810 This is used by modify-syntax-entry, and other things. */
811
812 unsigned char syntax_spec_code[0400] =
813 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
814 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
815 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
816 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
817 (char) Swhitespace, (char) Scomment_fence, (char) Sstring, 0377,
818 (char) Smath, 0377, 0377, (char) Squote,
819 (char) Sopen, (char) Sclose, 0377, 0377,
820 0377, (char) Swhitespace, (char) Spunct, (char) Scharquote,
821 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
822 0377, 0377, 0377, 0377,
823 (char) Scomment, 0377, (char) Sendcomment, 0377,
824 (char) Sinherit, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
825 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
826 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword,
827 0377, 0377, 0377, 0377, (char) Sescape, 0377, 0377, (char) Ssymbol,
828 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
829 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
830 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword,
831 0377, 0377, 0377, 0377, (char) Sstring_fence, 0377, 0377, 0377
832 };
833
834 /* Indexed by syntax code, give the letter that describes it. */
835
836 char syntax_code_spec[16] =
837 {
838 ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@',
839 '!', '|'
840 };
841
842 /* Indexed by syntax code, give the object (cons of syntax code and
843 nil) to be stored in syntax table. Since these objects can be
844 shared among syntax tables, we generate them in advance. By
845 sharing objects, the function `describe-syntax' can give a more
846 compact listing. */
847 static Lisp_Object Vsyntax_code_object;
848
849 \f
850 /* Look up the value for CHARACTER in syntax table TABLE's parent
851 and its parents. SYNTAX_ENTRY calls this, when TABLE itself has nil
852 for CHARACTER. It's actually used only when not compiled with GCC. */
853
854 Lisp_Object
855 syntax_parent_lookup (table, character)
856 Lisp_Object table;
857 int character;
858 {
859 Lisp_Object value;
860
861 while (1)
862 {
863 table = XCHAR_TABLE (table)->parent;
864 if (NILP (table))
865 return Qnil;
866
867 value = XCHAR_TABLE (table)->contents[character];
868 if (!NILP (value))
869 return value;
870 }
871 }
872
873 DEFUN ("char-syntax", Fchar_syntax, Schar_syntax, 1, 1, 0,
874 doc: /* Return the syntax code of CHARACTER, described by a character.
875 For example, if CHARACTER is a word constituent,
876 the character `w' is returned.
877 The characters that correspond to various syntax codes
878 are listed in the documentation of `modify-syntax-entry'. */)
879 (character)
880 Lisp_Object character;
881 {
882 int char_int;
883 gl_state.current_syntax_table = current_buffer->syntax_table;
884
885 gl_state.use_global = 0;
886 CHECK_NUMBER (character);
887 char_int = XINT (character);
888 return make_number (syntax_code_spec[(int) SYNTAX (char_int)]);
889 }
890
891 DEFUN ("matching-paren", Fmatching_paren, Smatching_paren, 1, 1, 0,
892 doc: /* Return the matching parenthesis of CHARACTER, or nil if none. */)
893 (character)
894 Lisp_Object character;
895 {
896 int char_int, code;
897 gl_state.current_syntax_table = current_buffer->syntax_table;
898 gl_state.use_global = 0;
899 CHECK_NUMBER (character);
900 char_int = XINT (character);
901 code = SYNTAX (char_int);
902 if (code == Sopen || code == Sclose)
903 return SYNTAX_MATCH (char_int);
904 return Qnil;
905 }
906
907 DEFUN ("string-to-syntax", Fstring_to_syntax, Sstring_to_syntax, 1, 1, 0,
908 doc: /* Convert a syntax specification STRING into syntax cell form.
909 STRING should be a string as it is allowed as argument of
910 `modify-syntax-entry'. Value is the equivalent cons cell
911 (CODE . MATCHING-CHAR) that can be used as value of a `syntax-table'
912 text property. */)
913 (string)
914 Lisp_Object string;
915 {
916 register const unsigned char *p;
917 register enum syntaxcode code;
918 int val;
919 Lisp_Object match;
920
921 CHECK_STRING (string);
922
923 p = SDATA (string);
924 code = (enum syntaxcode) syntax_spec_code[*p++];
925 if (((int) code & 0377) == 0377)
926 error ("invalid syntax description letter: %c", p[-1]);
927
928 if (code == Sinherit)
929 return Qnil;
930
931 if (*p)
932 {
933 int len;
934 int character = (STRING_CHAR_AND_LENGTH
935 (p, SBYTES (string) - 1, len));
936 XSETINT (match, character);
937 if (XFASTINT (match) == ' ')
938 match = Qnil;
939 p += len;
940 }
941 else
942 match = Qnil;
943
944 val = (int) code;
945 while (*p)
946 switch (*p++)
947 {
948 case '1':
949 val |= 1 << 16;
950 break;
951
952 case '2':
953 val |= 1 << 17;
954 break;
955
956 case '3':
957 val |= 1 << 18;
958 break;
959
960 case '4':
961 val |= 1 << 19;
962 break;
963
964 case 'p':
965 val |= 1 << 20;
966 break;
967
968 case 'b':
969 val |= 1 << 21;
970 break;
971
972 case 'n':
973 val |= 1 << 22;
974 break;
975 }
976
977 if (val < XVECTOR (Vsyntax_code_object)->size && NILP (match))
978 return XVECTOR (Vsyntax_code_object)->contents[val];
979 else
980 /* Since we can't use a shared object, let's make a new one. */
981 return Fcons (make_number (val), match);
982 }
983
984 /* I really don't know why this is interactive
985 help-form should at least be made useful whilst reading the second arg. */
986 DEFUN ("modify-syntax-entry", Fmodify_syntax_entry, Smodify_syntax_entry, 2, 3,
987 "cSet syntax for character: \nsSet syntax for %s to: ",
988 doc: /* Set syntax for character CHAR according to string NEWENTRY.
989 The syntax is changed only for table SYNTAX-TABLE, which defaults to
990 the current buffer's syntax table.
991 The first character of NEWENTRY should be one of the following:
992 Space or - whitespace syntax. w word constituent.
993 _ symbol constituent. . punctuation.
994 ( open-parenthesis. ) close-parenthesis.
995 " string quote. \\ escape.
996 $ paired delimiter. ' expression quote or prefix operator.
997 < comment starter. > comment ender.
998 / character-quote. @ inherit from `standard-syntax-table'.
999 | generic string fence. ! generic comment fence.
1000
1001 Only single-character comment start and end sequences are represented thus.
1002 Two-character sequences are represented as described below.
1003 The second character of NEWENTRY is the matching parenthesis,
1004 used only if the first character is `(' or `)'.
1005 Any additional characters are flags.
1006 Defined flags are the characters 1, 2, 3, 4, b, p, and n.
1007 1 means CHAR is the start of a two-char comment start sequence.
1008 2 means CHAR is the second character of such a sequence.
1009 3 means CHAR is the start of a two-char comment end sequence.
1010 4 means CHAR is the second character of such a sequence.
1011
1012 There can be up to two orthogonal comment sequences. This is to support
1013 language modes such as C++. By default, all comment sequences are of style
1014 a, but you can set the comment sequence style to b (on the second character
1015 of a comment-start, or the first character of a comment-end sequence) using
1016 this flag:
1017 b means CHAR is part of comment sequence b.
1018 n means CHAR is part of a nestable comment sequence.
1019
1020 p means CHAR is a prefix character for `backward-prefix-chars';
1021 such characters are treated as whitespace when they occur
1022 between expressions.
1023 usage: (modify-syntax-entry CHAR NEWENTRY &optional SYNTAX-TABLE) */)
1024 (c, newentry, syntax_table)
1025 Lisp_Object c, newentry, syntax_table;
1026 {
1027 CHECK_NUMBER (c);
1028
1029 if (NILP (syntax_table))
1030 syntax_table = current_buffer->syntax_table;
1031 else
1032 check_syntax_table (syntax_table);
1033
1034 SET_RAW_SYNTAX_ENTRY (syntax_table, XINT (c), Fstring_to_syntax (newentry));
1035 return Qnil;
1036 }
1037 \f
1038 /* Dump syntax table to buffer in human-readable format */
1039
1040 DEFUN ("internal-describe-syntax-value", Finternal_describe_syntax_value,
1041 Sinternal_describe_syntax_value, 1, 1, 0,
1042 doc: /* Insert a description of the internal syntax description SYNTAX at point. */)
1043 (syntax)
1044 Lisp_Object syntax;
1045 {
1046 register enum syntaxcode code;
1047 char desc, start1, start2, end1, end2, prefix, comstyle, comnested;
1048 char str[2];
1049 Lisp_Object first, match_lisp, value = syntax;
1050
1051 if (NILP (value))
1052 {
1053 insert_string ("default");
1054 return syntax;
1055 }
1056
1057 if (CHAR_TABLE_P (value))
1058 {
1059 insert_string ("deeper char-table ...");
1060 return syntax;
1061 }
1062
1063 if (!CONSP (value))
1064 {
1065 insert_string ("invalid");
1066 return syntax;
1067 }
1068
1069 first = XCAR (value);
1070 match_lisp = XCDR (value);
1071
1072 if (!INTEGERP (first) || !(NILP (match_lisp) || INTEGERP (match_lisp)))
1073 {
1074 insert_string ("invalid");
1075 return syntax;
1076 }
1077
1078 code = (enum syntaxcode) (XINT (first) & 0377);
1079 start1 = (XINT (first) >> 16) & 1;
1080 start2 = (XINT (first) >> 17) & 1;
1081 end1 = (XINT (first) >> 18) & 1;
1082 end2 = (XINT (first) >> 19) & 1;
1083 prefix = (XINT (first) >> 20) & 1;
1084 comstyle = (XINT (first) >> 21) & 1;
1085 comnested = (XINT (first) >> 22) & 1;
1086
1087 if ((int) code < 0 || (int) code >= (int) Smax)
1088 {
1089 insert_string ("invalid");
1090 return syntax;
1091 }
1092 desc = syntax_code_spec[(int) code];
1093
1094 str[0] = desc, str[1] = 0;
1095 insert (str, 1);
1096
1097 if (NILP (match_lisp))
1098 insert (" ", 1);
1099 else
1100 insert_char (XINT (match_lisp));
1101
1102 if (start1)
1103 insert ("1", 1);
1104 if (start2)
1105 insert ("2", 1);
1106
1107 if (end1)
1108 insert ("3", 1);
1109 if (end2)
1110 insert ("4", 1);
1111
1112 if (prefix)
1113 insert ("p", 1);
1114 if (comstyle)
1115 insert ("b", 1);
1116 if (comnested)
1117 insert ("n", 1);
1118
1119 insert_string ("\twhich means: ");
1120
1121 switch (SWITCH_ENUM_CAST (code))
1122 {
1123 case Swhitespace:
1124 insert_string ("whitespace"); break;
1125 case Spunct:
1126 insert_string ("punctuation"); break;
1127 case Sword:
1128 insert_string ("word"); break;
1129 case Ssymbol:
1130 insert_string ("symbol"); break;
1131 case Sopen:
1132 insert_string ("open"); break;
1133 case Sclose:
1134 insert_string ("close"); break;
1135 case Squote:
1136 insert_string ("prefix"); break;
1137 case Sstring:
1138 insert_string ("string"); break;
1139 case Smath:
1140 insert_string ("math"); break;
1141 case Sescape:
1142 insert_string ("escape"); break;
1143 case Scharquote:
1144 insert_string ("charquote"); break;
1145 case Scomment:
1146 insert_string ("comment"); break;
1147 case Sendcomment:
1148 insert_string ("endcomment"); break;
1149 case Sinherit:
1150 insert_string ("inherit"); break;
1151 case Scomment_fence:
1152 insert_string ("comment fence"); break;
1153 case Sstring_fence:
1154 insert_string ("string fence"); break;
1155 default:
1156 insert_string ("invalid");
1157 return syntax;
1158 }
1159
1160 if (!NILP (match_lisp))
1161 {
1162 insert_string (", matches ");
1163 insert_char (XINT (match_lisp));
1164 }
1165
1166 if (start1)
1167 insert_string (",\n\t is the first character of a comment-start sequence");
1168 if (start2)
1169 insert_string (",\n\t is the second character of a comment-start sequence");
1170
1171 if (end1)
1172 insert_string (",\n\t is the first character of a comment-end sequence");
1173 if (end2)
1174 insert_string (",\n\t is the second character of a comment-end sequence");
1175 if (comstyle)
1176 insert_string (" (comment style b)");
1177 if (comnested)
1178 insert_string (" (nestable)");
1179
1180 if (prefix)
1181 insert_string (",\n\t is a prefix character for `backward-prefix-chars'");
1182
1183 return syntax;
1184 }
1185 \f
1186 int parse_sexp_ignore_comments;
1187
1188 /* Return the position across COUNT words from FROM.
1189 If that many words cannot be found before the end of the buffer, return 0.
1190 COUNT negative means scan backward and stop at word beginning. */
1191
1192 int
1193 scan_words (from, count)
1194 register int from, count;
1195 {
1196 register int beg = BEGV;
1197 register int end = ZV;
1198 register int from_byte = CHAR_TO_BYTE (from);
1199 register enum syntaxcode code;
1200 int ch0, ch1;
1201
1202 immediate_quit = 1;
1203 QUIT;
1204
1205 SETUP_SYNTAX_TABLE (from, count);
1206
1207 while (count > 0)
1208 {
1209 while (1)
1210 {
1211 if (from == end)
1212 {
1213 immediate_quit = 0;
1214 return 0;
1215 }
1216 UPDATE_SYNTAX_TABLE_FORWARD (from);
1217 ch0 = FETCH_CHAR (from_byte);
1218 code = SYNTAX (ch0);
1219 INC_BOTH (from, from_byte);
1220 if (words_include_escapes
1221 && (code == Sescape || code == Scharquote))
1222 break;
1223 if (code == Sword)
1224 break;
1225 }
1226 /* Now CH0 is a character which begins a word and FROM is the
1227 position of the next character. */
1228 while (1)
1229 {
1230 if (from == end) break;
1231 UPDATE_SYNTAX_TABLE_FORWARD (from);
1232 ch1 = FETCH_CHAR (from_byte);
1233 code = SYNTAX (ch1);
1234 if (!(words_include_escapes
1235 && (code == Sescape || code == Scharquote)))
1236 if (code != Sword || WORD_BOUNDARY_P (ch0, ch1))
1237 break;
1238 INC_BOTH (from, from_byte);
1239 ch0 = ch1;
1240 }
1241 count--;
1242 }
1243 while (count < 0)
1244 {
1245 while (1)
1246 {
1247 if (from == beg)
1248 {
1249 immediate_quit = 0;
1250 return 0;
1251 }
1252 DEC_BOTH (from, from_byte);
1253 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1254 ch1 = FETCH_CHAR (from_byte);
1255 code = SYNTAX (ch1);
1256 if (words_include_escapes
1257 && (code == Sescape || code == Scharquote))
1258 break;
1259 if (code == Sword)
1260 break;
1261 }
1262 /* Now CH1 is a character which ends a word and FROM is the
1263 position of it. */
1264 while (1)
1265 {
1266 int temp_byte;
1267
1268 if (from == beg)
1269 break;
1270 temp_byte = dec_bytepos (from_byte);
1271 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1272 ch0 = FETCH_CHAR (temp_byte);
1273 code = SYNTAX (ch0);
1274 if (!(words_include_escapes
1275 && (code == Sescape || code == Scharquote)))
1276 if (code != Sword || WORD_BOUNDARY_P (ch0, ch1))
1277 break;
1278 DEC_BOTH (from, from_byte);
1279 ch1 = ch0;
1280 }
1281 count++;
1282 }
1283
1284 immediate_quit = 0;
1285
1286 return from;
1287 }
1288
1289 DEFUN ("forward-word", Fforward_word, Sforward_word, 0, 1, "p",
1290 doc: /* Move point forward ARG words (backward if ARG is negative).
1291 Normally returns t.
1292 If an edge of the buffer or a field boundary is reached, point is left there
1293 and the function returns nil. Field boundaries are not noticed if
1294 `inhibit-field-text-motion' is non-nil. */)
1295 (arg)
1296 Lisp_Object arg;
1297 {
1298 Lisp_Object tmp;
1299 int orig_val, val;
1300
1301 if (NILP (arg))
1302 XSETFASTINT (arg, 1);
1303 else
1304 CHECK_NUMBER (arg);
1305
1306 val = orig_val = scan_words (PT, XINT (arg));
1307 if (! orig_val)
1308 val = XINT (arg) > 0 ? ZV : BEGV;
1309
1310 /* Avoid jumping out of an input field. */
1311 tmp = Fconstrain_to_field (make_number (val), make_number (PT),
1312 Qt, Qnil, Qnil);
1313 val = XFASTINT (tmp);
1314
1315 SET_PT (val);
1316 return val == orig_val ? Qt : Qnil;
1317 }
1318 \f
1319 Lisp_Object skip_chars ();
1320
1321 DEFUN ("skip-chars-forward", Fskip_chars_forward, Sskip_chars_forward, 1, 2, 0,
1322 doc: /* Move point forward, stopping before a char not in STRING, or at pos LIM.
1323 STRING is like the inside of a `[...]' in a regular expression
1324 except that `]' is never special and `\\' quotes `^', `-' or `\\'
1325 (but not as the end of a range; quoting is never needed there).
1326 Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
1327 With arg "^a-zA-Z", skips nonletters stopping before first letter.
1328 Char classes, e.g. `[:alpha:]', are supported.
1329
1330 Returns the distance traveled, either zero or positive. */)
1331 (string, lim)
1332 Lisp_Object string, lim;
1333 {
1334 return skip_chars (1, 0, string, lim, 1);
1335 }
1336
1337 DEFUN ("skip-chars-backward", Fskip_chars_backward, Sskip_chars_backward, 1, 2, 0,
1338 doc: /* Move point backward, stopping after a char not in STRING, or at pos LIM.
1339 See `skip-chars-forward' for details.
1340 Returns the distance traveled, either zero or negative. */)
1341 (string, lim)
1342 Lisp_Object string, lim;
1343 {
1344 return skip_chars (0, 0, string, lim, 1);
1345 }
1346
1347 DEFUN ("skip-syntax-forward", Fskip_syntax_forward, Sskip_syntax_forward, 1, 2, 0,
1348 doc: /* Move point forward across chars in specified syntax classes.
1349 SYNTAX is a string of syntax code characters.
1350 Stop before a char whose syntax is not in SYNTAX, or at position LIM.
1351 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1352 This function returns the distance traveled, either zero or positive. */)
1353 (syntax, lim)
1354 Lisp_Object syntax, lim;
1355 {
1356 return skip_chars (1, 1, syntax, lim, 0);
1357 }
1358
1359 DEFUN ("skip-syntax-backward", Fskip_syntax_backward, Sskip_syntax_backward, 1, 2, 0,
1360 doc: /* Move point backward across chars in specified syntax classes.
1361 SYNTAX is a string of syntax code characters.
1362 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.
1363 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1364 This function returns the distance traveled, either zero or negative. */)
1365 (syntax, lim)
1366 Lisp_Object syntax, lim;
1367 {
1368 return skip_chars (0, 1, syntax, lim, 0);
1369 }
1370
1371 static Lisp_Object
1372 skip_chars (forwardp, syntaxp, string, lim, handle_iso_classes)
1373 int forwardp, syntaxp;
1374 Lisp_Object string, lim;
1375 int handle_iso_classes;
1376 {
1377 register unsigned int c;
1378 unsigned char fastmap[0400];
1379 /* If SYNTAXP is 0, STRING may contain multi-byte form of characters
1380 of which codes don't fit in FASTMAP. In that case, set the
1381 ranges of characters in CHAR_RANGES. */
1382 int *char_ranges;
1383 int n_char_ranges = 0;
1384 int negate = 0;
1385 register int i, i_byte;
1386 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
1387 int string_multibyte;
1388 int size_byte;
1389 const unsigned char *str;
1390 int len;
1391 Lisp_Object iso_classes;
1392
1393 CHECK_STRING (string);
1394 char_ranges = (int *) alloca (SCHARS (string) * (sizeof (int)) * 2);
1395 string_multibyte = STRING_MULTIBYTE (string);
1396 str = SDATA (string);
1397 size_byte = SBYTES (string);
1398 iso_classes = Qnil;
1399
1400 /* Adjust the multibyteness of the string to that of the buffer. */
1401 if (multibyte != string_multibyte)
1402 {
1403 int nbytes;
1404
1405 if (multibyte)
1406 nbytes = count_size_as_multibyte (SDATA (string),
1407 SCHARS (string));
1408 else
1409 nbytes = SCHARS (string);
1410 if (nbytes != size_byte)
1411 {
1412 unsigned char *tmp = (unsigned char *) alloca (nbytes);
1413 copy_text (SDATA (string), tmp, size_byte,
1414 string_multibyte, multibyte);
1415 size_byte = nbytes;
1416 str = tmp;
1417 }
1418 }
1419
1420 if (NILP (lim))
1421 XSETINT (lim, forwardp ? ZV : BEGV);
1422 else
1423 CHECK_NUMBER_COERCE_MARKER (lim);
1424
1425 /* In any case, don't allow scan outside bounds of buffer. */
1426 if (XINT (lim) > ZV)
1427 XSETFASTINT (lim, ZV);
1428 if (XINT (lim) < BEGV)
1429 XSETFASTINT (lim, BEGV);
1430
1431 bzero (fastmap, sizeof fastmap);
1432
1433 i_byte = 0;
1434
1435 if (i_byte < size_byte
1436 && SREF (string, 0) == '^')
1437 {
1438 negate = 1; i_byte++;
1439 }
1440
1441 /* Find the characters specified and set their elements of fastmap.
1442 If syntaxp, each character counts as itself.
1443 Otherwise, handle backslashes and ranges specially. */
1444
1445 while (i_byte < size_byte)
1446 {
1447 c = STRING_CHAR_AND_LENGTH (str + i_byte, size_byte - i_byte, len);
1448 i_byte += len;
1449
1450 if (syntaxp)
1451 fastmap[syntax_spec_code[c & 0377]] = 1;
1452 else
1453 {
1454 if (handle_iso_classes && c == '['
1455 && i_byte < size_byte
1456 && STRING_CHAR (str + i_byte, size_byte - i_byte) == ':')
1457 {
1458 const unsigned char *class_beg = str + i_byte + 1;
1459 const unsigned char *class_end = class_beg;
1460 const unsigned char *class_limit = str + size_byte - 2;
1461 /* Leave room for the null. */
1462 unsigned char class_name[CHAR_CLASS_MAX_LENGTH + 1];
1463 re_wctype_t cc;
1464
1465 if (class_limit - class_beg > CHAR_CLASS_MAX_LENGTH)
1466 class_limit = class_beg + CHAR_CLASS_MAX_LENGTH;
1467
1468 while (class_end < class_limit
1469 && *class_end >= 'a' && *class_end <= 'z')
1470 class_end++;
1471
1472 if (class_end == class_beg
1473 || *class_end != ':' || class_end[1] != ']')
1474 goto not_a_class_name;
1475
1476 bcopy (class_beg, class_name, class_end - class_beg);
1477 class_name[class_end - class_beg] = 0;
1478
1479 cc = re_wctype (class_name);
1480 if (cc == 0)
1481 error ("Invalid ISO C character class");
1482
1483 iso_classes = Fcons (make_number (cc), iso_classes);
1484
1485 i_byte = class_end + 2 - str;
1486 continue;
1487 }
1488
1489 not_a_class_name:
1490 if (c == '\\')
1491 {
1492 if (i_byte == size_byte)
1493 break;
1494
1495 c = STRING_CHAR_AND_LENGTH (str + i_byte,
1496 size_byte - i_byte, len);
1497 i_byte += len;
1498 }
1499 /* Treat `-' as range character only if another character
1500 follows. */
1501 if (i_byte + 1 < size_byte
1502 && str[i_byte] == '-')
1503 {
1504 unsigned int c2;
1505
1506 /* Skip over the dash. */
1507 i_byte++;
1508
1509 /* Get the end of the range. */
1510 c2 = STRING_CHAR_AND_LENGTH (str + i_byte,
1511 size_byte - i_byte, len);
1512 i_byte += len;
1513
1514 if (SINGLE_BYTE_CHAR_P (c))
1515 {
1516 if (! SINGLE_BYTE_CHAR_P (c2))
1517 {
1518 /* Handle a range starting with a character of
1519 less than 256, and ending with a character of
1520 not less than 256. Split that into two
1521 ranges, the low one ending at 0377, and the
1522 high one starting at the smallest character
1523 in the charset of C2 and ending at C2. */
1524 int charset = CHAR_CHARSET (c2);
1525 int c1 = MAKE_CHAR (charset, 0, 0);
1526
1527 char_ranges[n_char_ranges++] = c1;
1528 char_ranges[n_char_ranges++] = c2;
1529 c2 = 0377;
1530 }
1531 while (c <= c2)
1532 {
1533 fastmap[c] = 1;
1534 c++;
1535 }
1536 }
1537 else if (c <= c2) /* Both C and C2 are multibyte char. */
1538 {
1539 char_ranges[n_char_ranges++] = c;
1540 char_ranges[n_char_ranges++] = c2;
1541 }
1542 }
1543 else
1544 {
1545 if (SINGLE_BYTE_CHAR_P (c))
1546 fastmap[c] = 1;
1547 else
1548 {
1549 char_ranges[n_char_ranges++] = c;
1550 char_ranges[n_char_ranges++] = c;
1551 }
1552 }
1553 }
1554 }
1555
1556 /* If ^ was the first character, complement the fastmap. */
1557 if (negate)
1558 for (i = 0; i < sizeof fastmap; i++)
1559 fastmap[i] ^= 1;
1560
1561 {
1562 int start_point = PT;
1563 int pos = PT;
1564 int pos_byte = PT_BYTE;
1565 unsigned char *p = PT_ADDR, *endp, *stop;
1566
1567 if (forwardp)
1568 {
1569 endp = (XINT (lim) == GPT) ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
1570 stop = (pos < GPT && GPT < XINT (lim)) ? GPT_ADDR : endp;
1571 }
1572 else
1573 {
1574 endp = CHAR_POS_ADDR (XINT (lim));
1575 stop = (pos >= GPT && GPT > XINT (lim)) ? GAP_END_ADDR : endp;
1576 }
1577
1578 immediate_quit = 1;
1579 if (syntaxp)
1580 {
1581 SETUP_SYNTAX_TABLE (pos, forwardp ? 1 : -1);
1582 if (forwardp)
1583 {
1584 if (multibyte)
1585 while (1)
1586 {
1587 int nbytes;
1588
1589 if (p >= stop)
1590 {
1591 if (p >= endp)
1592 break;
1593 p = GAP_END_ADDR;
1594 stop = endp;
1595 }
1596 c = STRING_CHAR_AND_LENGTH (p, MAX_MULTIBYTE_LENGTH, nbytes);
1597 if (! fastmap[(int) SYNTAX (c)])
1598 break;
1599 p += nbytes, pos++, pos_byte += nbytes;
1600 UPDATE_SYNTAX_TABLE_FORWARD (pos);
1601 }
1602 else
1603 while (1)
1604 {
1605 if (p >= stop)
1606 {
1607 if (p >= endp)
1608 break;
1609 p = GAP_END_ADDR;
1610 stop = endp;
1611 }
1612 if (! fastmap[(int) SYNTAX (*p)])
1613 break;
1614 p++, pos++;
1615 UPDATE_SYNTAX_TABLE_FORWARD (pos);
1616 }
1617 }
1618 else
1619 {
1620 if (multibyte)
1621 while (1)
1622 {
1623 unsigned char *prev_p;
1624 int nbytes;
1625
1626 if (p <= stop)
1627 {
1628 if (p <= endp)
1629 break;
1630 p = GPT_ADDR;
1631 stop = endp;
1632 }
1633 prev_p = p;
1634 while (--p >= stop && ! CHAR_HEAD_P (*p));
1635 PARSE_MULTIBYTE_SEQ (p, MAX_MULTIBYTE_LENGTH, nbytes);
1636 if (prev_p - p > nbytes)
1637 p = prev_p - 1, c = *p, nbytes = 1;
1638 else
1639 c = STRING_CHAR (p, MAX_MULTIBYTE_LENGTH);
1640 pos--, pos_byte -= nbytes;
1641 UPDATE_SYNTAX_TABLE_BACKWARD (pos);
1642 if (! fastmap[(int) SYNTAX (c)])
1643 {
1644 pos++;
1645 pos_byte += nbytes;
1646 break;
1647 }
1648 }
1649 else
1650 while (1)
1651 {
1652 if (p <= stop)
1653 {
1654 if (p <= endp)
1655 break;
1656 p = GPT_ADDR;
1657 stop = endp;
1658 }
1659 if (! fastmap[(int) SYNTAX (p[-1])])
1660 break;
1661 p--, pos--;
1662 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
1663 }
1664 }
1665 }
1666 else
1667 {
1668 if (forwardp)
1669 {
1670 if (multibyte)
1671 while (1)
1672 {
1673 int nbytes;
1674
1675 if (p >= stop)
1676 {
1677 if (p >= endp)
1678 break;
1679 p = GAP_END_ADDR;
1680 stop = endp;
1681 }
1682 c = STRING_CHAR_AND_LENGTH (p, MAX_MULTIBYTE_LENGTH, nbytes);
1683
1684 if (! NILP (iso_classes) && in_classes (c, iso_classes))
1685 {
1686 if (negate)
1687 break;
1688 else
1689 goto fwd_ok;
1690 }
1691
1692 if (SINGLE_BYTE_CHAR_P (c))
1693 {
1694 if (!fastmap[c])
1695 break;
1696 }
1697 else
1698 {
1699 /* If we are looking at a multibyte character,
1700 we must look up the character in the table
1701 CHAR_RANGES. If there's no data in the
1702 table, that character is not what we want to
1703 skip. */
1704
1705 /* The following code do the right thing even if
1706 n_char_ranges is zero (i.e. no data in
1707 CHAR_RANGES). */
1708 for (i = 0; i < n_char_ranges; i += 2)
1709 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
1710 break;
1711 if (!(negate ^ (i < n_char_ranges)))
1712 break;
1713 }
1714 fwd_ok:
1715 p += nbytes, pos++, pos_byte += nbytes;
1716 }
1717 else
1718 while (1)
1719 {
1720 if (p >= stop)
1721 {
1722 if (p >= endp)
1723 break;
1724 p = GAP_END_ADDR;
1725 stop = endp;
1726 }
1727
1728 if (!NILP (iso_classes) && in_classes (*p, iso_classes))
1729 {
1730 if (negate)
1731 break;
1732 else
1733 goto fwd_unibyte_ok;
1734 }
1735
1736 if (!fastmap[*p])
1737 break;
1738 fwd_unibyte_ok:
1739 p++, pos++;
1740 }
1741 }
1742 else
1743 {
1744 if (multibyte)
1745 while (1)
1746 {
1747 unsigned char *prev_p;
1748 int nbytes;
1749
1750 if (p <= stop)
1751 {
1752 if (p <= endp)
1753 break;
1754 p = GPT_ADDR;
1755 stop = endp;
1756 }
1757 prev_p = p;
1758 while (--p >= stop && ! CHAR_HEAD_P (*p));
1759 PARSE_MULTIBYTE_SEQ (p, MAX_MULTIBYTE_LENGTH, nbytes);
1760 if (prev_p - p > nbytes)
1761 p = prev_p - 1, c = *p, nbytes = 1;
1762 else
1763 c = STRING_CHAR (p, MAX_MULTIBYTE_LENGTH);
1764
1765 if (! NILP (iso_classes) && in_classes (c, iso_classes))
1766 {
1767 if (negate)
1768 break;
1769 else
1770 goto back_ok;
1771 }
1772
1773 if (SINGLE_BYTE_CHAR_P (c))
1774 {
1775 if (!fastmap[c])
1776 break;
1777 }
1778 else
1779 {
1780 /* See the comment in the previous similar code. */
1781 for (i = 0; i < n_char_ranges; i += 2)
1782 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
1783 break;
1784 if (!(negate ^ (i < n_char_ranges)))
1785 break;
1786 }
1787 back_ok:
1788 pos--, pos_byte -= nbytes;
1789 }
1790 else
1791 while (1)
1792 {
1793 if (p <= stop)
1794 {
1795 if (p <= endp)
1796 break;
1797 p = GPT_ADDR;
1798 stop = endp;
1799 }
1800
1801 if (! NILP (iso_classes) && in_classes (p[-1], iso_classes))
1802 {
1803 if (negate)
1804 break;
1805 else
1806 goto back_unibyte_ok;
1807 }
1808
1809 if (!fastmap[p[-1]])
1810 break;
1811 back_unibyte_ok:
1812 p--, pos--;
1813 }
1814 }
1815 }
1816
1817 #if 0 /* Not needed now that a position in mid-character
1818 cannot be specified in Lisp. */
1819 if (multibyte
1820 /* INC_POS or DEC_POS might have moved POS over LIM. */
1821 && (forwardp ? (pos > XINT (lim)) : (pos < XINT (lim))))
1822 pos = XINT (lim);
1823 #endif
1824
1825 if (! multibyte)
1826 pos_byte = pos;
1827
1828 SET_PT_BOTH (pos, pos_byte);
1829 immediate_quit = 0;
1830
1831 return make_number (PT - start_point);
1832 }
1833 }
1834
1835 /* Return 1 if character C belongs to one of the ISO classes
1836 in the list ISO_CLASSES. Each class is represented by an
1837 integer which is its type according to re_wctype. */
1838
1839 static int
1840 in_classes (c, iso_classes)
1841 int c;
1842 Lisp_Object iso_classes;
1843 {
1844 int fits_class = 0;
1845
1846 while (! NILP (iso_classes))
1847 {
1848 Lisp_Object elt;
1849 elt = XCAR (iso_classes);
1850 iso_classes = XCDR (iso_classes);
1851
1852 if (re_iswctype (c, XFASTINT (elt)))
1853 fits_class = 1;
1854 }
1855
1856 return fits_class;
1857 }
1858 \f
1859 /* Jump over a comment, assuming we are at the beginning of one.
1860 FROM is the current position.
1861 FROM_BYTE is the bytepos corresponding to FROM.
1862 Do not move past STOP (a charpos).
1863 The comment over which we have to jump is of style STYLE
1864 (either SYNTAX_COMMENT_STYLE(foo) or ST_COMMENT_STYLE).
1865 NESTING should be positive to indicate the nesting at the beginning
1866 for nested comments and should be zero or negative else.
1867 ST_COMMENT_STYLE cannot be nested.
1868 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
1869 (or 0 If the search cannot start in the middle of a two-character).
1870
1871 If successful, return 1 and store the charpos of the comment's end
1872 into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR.
1873 Else, return 0 and store the charpos STOP into *CHARPOS_PTR, the
1874 corresponding bytepos into *BYTEPOS_PTR and the current nesting
1875 (as defined for state.incomment) in *INCOMMENT_PTR.
1876
1877 The comment end is the last character of the comment rather than the
1878 character just after the comment.
1879
1880 Global syntax data is assumed to initially be valid for FROM and
1881 remains valid for forward search starting at the returned position. */
1882
1883 static int
1884 forw_comment (from, from_byte, stop, nesting, style, prev_syntax,
1885 charpos_ptr, bytepos_ptr, incomment_ptr)
1886 int from, from_byte, stop;
1887 int nesting, style, prev_syntax;
1888 int *charpos_ptr, *bytepos_ptr, *incomment_ptr;
1889 {
1890 register int c, c1;
1891 register enum syntaxcode code;
1892 register int syntax;
1893
1894 if (nesting <= 0) nesting = -1;
1895
1896 /* Enter the loop in the middle so that we find
1897 a 2-char comment ender if we start in the middle of it. */
1898 syntax = prev_syntax;
1899 if (syntax != 0) goto forw_incomment;
1900
1901 while (1)
1902 {
1903 if (from == stop)
1904 {
1905 *incomment_ptr = nesting;
1906 *charpos_ptr = from;
1907 *bytepos_ptr = from_byte;
1908 return 0;
1909 }
1910 c = FETCH_CHAR (from_byte);
1911 syntax = SYNTAX_WITH_FLAGS (c);
1912 code = syntax & 0xff;
1913 if (code == Sendcomment
1914 && SYNTAX_FLAGS_COMMENT_STYLE (syntax) == style
1915 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ?
1916 (nesting > 0 && --nesting == 0) : nesting < 0))
1917 /* we have encountered a comment end of the same style
1918 as the comment sequence which began this comment
1919 section */
1920 break;
1921 if (code == Scomment_fence
1922 && style == ST_COMMENT_STYLE)
1923 /* we have encountered a comment end of the same style
1924 as the comment sequence which began this comment
1925 section. */
1926 break;
1927 if (nesting > 0
1928 && code == Scomment
1929 && SYNTAX_FLAGS_COMMENT_NESTED (syntax)
1930 && SYNTAX_FLAGS_COMMENT_STYLE (syntax) == style)
1931 /* we have encountered a nested comment of the same style
1932 as the comment sequence which began this comment section */
1933 nesting++;
1934 INC_BOTH (from, from_byte);
1935 UPDATE_SYNTAX_TABLE_FORWARD (from);
1936
1937 forw_incomment:
1938 if (from < stop && SYNTAX_FLAGS_COMEND_FIRST (syntax)
1939 && SYNTAX_FLAGS_COMMENT_STYLE (syntax) == style
1940 && (c1 = FETCH_CHAR (from_byte),
1941 SYNTAX_COMEND_SECOND (c1))
1942 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
1943 SYNTAX_COMMENT_NESTED (c1)) ? nesting > 0 : nesting < 0))
1944 {
1945 if (--nesting <= 0)
1946 /* we have encountered a comment end of the same style
1947 as the comment sequence which began this comment
1948 section */
1949 break;
1950 else
1951 {
1952 INC_BOTH (from, from_byte);
1953 UPDATE_SYNTAX_TABLE_FORWARD (from);
1954 }
1955 }
1956 if (nesting > 0
1957 && from < stop
1958 && SYNTAX_FLAGS_COMSTART_FIRST (syntax)
1959 && (c1 = FETCH_CHAR (from_byte),
1960 SYNTAX_COMMENT_STYLE (c1) == style
1961 && SYNTAX_COMSTART_SECOND (c1))
1962 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
1963 SYNTAX_COMMENT_NESTED (c1)))
1964 /* we have encountered a nested comment of the same style
1965 as the comment sequence which began this comment
1966 section */
1967 {
1968 INC_BOTH (from, from_byte);
1969 UPDATE_SYNTAX_TABLE_FORWARD (from);
1970 nesting++;
1971 }
1972 }
1973 *charpos_ptr = from;
1974 *bytepos_ptr = from_byte;
1975 return 1;
1976 }
1977
1978 DEFUN ("forward-comment", Fforward_comment, Sforward_comment, 1, 1, 0,
1979 doc: /*
1980 Move forward across up to COUNT comments. If COUNT is negative, move backward.
1981 Stop scanning if we find something other than a comment or whitespace.
1982 Set point to where scanning stops.
1983 If COUNT comments are found as expected, with nothing except whitespace
1984 between them, return t; otherwise return nil. */)
1985 (count)
1986 Lisp_Object count;
1987 {
1988 register int from;
1989 int from_byte;
1990 register int stop;
1991 register int c, c1;
1992 register enum syntaxcode code;
1993 int comstyle = 0; /* style of comment encountered */
1994 int comnested = 0; /* whether the comment is nestable or not */
1995 int found;
1996 int count1;
1997 int out_charpos, out_bytepos;
1998 int dummy;
1999
2000 CHECK_NUMBER (count);
2001 count1 = XINT (count);
2002 stop = count1 > 0 ? ZV : BEGV;
2003
2004 immediate_quit = 1;
2005 QUIT;
2006
2007 from = PT;
2008 from_byte = PT_BYTE;
2009
2010 SETUP_SYNTAX_TABLE (from, count1);
2011 while (count1 > 0)
2012 {
2013 do
2014 {
2015 int comstart_first;
2016
2017 if (from == stop)
2018 {
2019 SET_PT_BOTH (from, from_byte);
2020 immediate_quit = 0;
2021 return Qnil;
2022 }
2023 c = FETCH_CHAR (from_byte);
2024 code = SYNTAX (c);
2025 comstart_first = SYNTAX_COMSTART_FIRST (c);
2026 comnested = SYNTAX_COMMENT_NESTED (c);
2027 comstyle = SYNTAX_COMMENT_STYLE (c);
2028 INC_BOTH (from, from_byte);
2029 UPDATE_SYNTAX_TABLE_FORWARD (from);
2030 if (from < stop && comstart_first
2031 && (c1 = FETCH_CHAR (from_byte),
2032 SYNTAX_COMSTART_SECOND (c1)))
2033 {
2034 /* We have encountered a comment start sequence and we
2035 are ignoring all text inside comments. We must record
2036 the comment style this sequence begins so that later,
2037 only a comment end of the same style actually ends
2038 the comment section. */
2039 code = Scomment;
2040 comstyle = SYNTAX_COMMENT_STYLE (c1);
2041 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
2042 INC_BOTH (from, from_byte);
2043 UPDATE_SYNTAX_TABLE_FORWARD (from);
2044 }
2045 }
2046 while (code == Swhitespace || (code == Sendcomment && c == '\n'));
2047
2048 if (code == Scomment_fence)
2049 comstyle = ST_COMMENT_STYLE;
2050 else if (code != Scomment)
2051 {
2052 immediate_quit = 0;
2053 DEC_BOTH (from, from_byte);
2054 SET_PT_BOTH (from, from_byte);
2055 return Qnil;
2056 }
2057 /* We're at the start of a comment. */
2058 found = forw_comment (from, from_byte, stop, comnested, comstyle, 0,
2059 &out_charpos, &out_bytepos, &dummy);
2060 from = out_charpos; from_byte = out_bytepos;
2061 if (!found)
2062 {
2063 immediate_quit = 0;
2064 SET_PT_BOTH (from, from_byte);
2065 return Qnil;
2066 }
2067 INC_BOTH (from, from_byte);
2068 UPDATE_SYNTAX_TABLE_FORWARD (from);
2069 /* We have skipped one comment. */
2070 count1--;
2071 }
2072
2073 while (count1 < 0)
2074 {
2075 while (1)
2076 {
2077 int quoted;
2078
2079 if (from <= stop)
2080 {
2081 SET_PT_BOTH (BEGV, BEGV_BYTE);
2082 immediate_quit = 0;
2083 return Qnil;
2084 }
2085
2086 DEC_BOTH (from, from_byte);
2087 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
2088 quoted = char_quoted (from, from_byte);
2089 c = FETCH_CHAR (from_byte);
2090 code = SYNTAX (c);
2091 comstyle = 0;
2092 comnested = SYNTAX_COMMENT_NESTED (c);
2093 if (code == Sendcomment)
2094 comstyle = SYNTAX_COMMENT_STYLE (c);
2095 if (from > stop && SYNTAX_COMEND_SECOND (c)
2096 && prev_char_comend_first (from, from_byte)
2097 && !char_quoted (from - 1, dec_bytepos (from_byte)))
2098 {
2099 /* We must record the comment style encountered so that
2100 later, we can match only the proper comment begin
2101 sequence of the same style. */
2102 DEC_BOTH (from, from_byte);
2103 code = Sendcomment;
2104 /* Calling char_quoted, above, set up global syntax position
2105 at the new value of FROM. */
2106 c1 = FETCH_CHAR (from_byte);
2107 comstyle = SYNTAX_COMMENT_STYLE (c1);
2108 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
2109 }
2110
2111 if (code == Scomment_fence)
2112 {
2113 /* Skip until first preceding unquoted comment_fence. */
2114 int found = 0, ini = from, ini_byte = from_byte;
2115
2116 while (1)
2117 {
2118 DEC_BOTH (from, from_byte);
2119 if (from == stop)
2120 break;
2121 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2122 c = FETCH_CHAR (from_byte);
2123 if (SYNTAX (c) == Scomment_fence
2124 && !char_quoted (from, from_byte))
2125 {
2126 found = 1;
2127 break;
2128 }
2129 }
2130 if (found == 0)
2131 {
2132 from = ini; /* Set point to ini + 1. */
2133 from_byte = ini_byte;
2134 goto leave;
2135 }
2136 }
2137 else if (code == Sendcomment)
2138 {
2139 found = back_comment (from, from_byte, stop, comnested, comstyle,
2140 &out_charpos, &out_bytepos);
2141 if (found == -1)
2142 {
2143 if (c == '\n')
2144 /* This end-of-line is not an end-of-comment.
2145 Treat it like a whitespace.
2146 CC-mode (and maybe others) relies on this behavior. */
2147 ;
2148 else
2149 {
2150 /* Failure: we should go back to the end of this
2151 not-quite-endcomment. */
2152 if (SYNTAX(c) != code)
2153 /* It was a two-char Sendcomment. */
2154 INC_BOTH (from, from_byte);
2155 goto leave;
2156 }
2157 }
2158 else
2159 {
2160 /* We have skipped one comment. */
2161 from = out_charpos, from_byte = out_bytepos;
2162 break;
2163 }
2164 }
2165 else if (code != Swhitespace || quoted)
2166 {
2167 leave:
2168 immediate_quit = 0;
2169 INC_BOTH (from, from_byte);
2170 SET_PT_BOTH (from, from_byte);
2171 return Qnil;
2172 }
2173 }
2174
2175 count1++;
2176 }
2177
2178 SET_PT_BOTH (from, from_byte);
2179 immediate_quit = 0;
2180 return Qt;
2181 }
2182 \f
2183 /* Return syntax code of character C if C is a single byte character
2184 or `multibyte_symbol_p' is zero. Otherwise, return Ssymbol. */
2185
2186 #define SYNTAX_WITH_MULTIBYTE_CHECK(c) \
2187 ((SINGLE_BYTE_CHAR_P (c) || !multibyte_symbol_p) \
2188 ? SYNTAX (c) : Ssymbol)
2189
2190 static Lisp_Object
2191 scan_lists (from, count, depth, sexpflag)
2192 register int from;
2193 int count, depth, sexpflag;
2194 {
2195 Lisp_Object val;
2196 register int stop = count > 0 ? ZV : BEGV;
2197 register int c, c1;
2198 int stringterm;
2199 int quoted;
2200 int mathexit = 0;
2201 register enum syntaxcode code, temp_code;
2202 int min_depth = depth; /* Err out if depth gets less than this. */
2203 int comstyle = 0; /* style of comment encountered */
2204 int comnested = 0; /* whether the comment is nestable or not */
2205 int temp_pos;
2206 int last_good = from;
2207 int found;
2208 int from_byte;
2209 int out_bytepos, out_charpos;
2210 int temp, dummy;
2211 int multibyte_symbol_p = sexpflag && multibyte_syntax_as_symbol;
2212
2213 if (depth > 0) min_depth = 0;
2214
2215 if (from > ZV) from = ZV;
2216 if (from < BEGV) from = BEGV;
2217
2218 from_byte = CHAR_TO_BYTE (from);
2219
2220 immediate_quit = 1;
2221 QUIT;
2222
2223 SETUP_SYNTAX_TABLE (from, count);
2224 while (count > 0)
2225 {
2226 while (from < stop)
2227 {
2228 int comstart_first, prefix;
2229 UPDATE_SYNTAX_TABLE_FORWARD (from);
2230 c = FETCH_CHAR (from_byte);
2231 code = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2232 comstart_first = SYNTAX_COMSTART_FIRST (c);
2233 comnested = SYNTAX_COMMENT_NESTED (c);
2234 comstyle = SYNTAX_COMMENT_STYLE (c);
2235 prefix = SYNTAX_PREFIX (c);
2236 if (depth == min_depth)
2237 last_good = from;
2238 INC_BOTH (from, from_byte);
2239 UPDATE_SYNTAX_TABLE_FORWARD (from);
2240 if (from < stop && comstart_first
2241 && (c = FETCH_CHAR (from_byte), SYNTAX_COMSTART_SECOND (c))
2242 && parse_sexp_ignore_comments)
2243 {
2244 /* we have encountered a comment start sequence and we
2245 are ignoring all text inside comments. We must record
2246 the comment style this sequence begins so that later,
2247 only a comment end of the same style actually ends
2248 the comment section */
2249 code = Scomment;
2250 c1 = FETCH_CHAR (from_byte);
2251 comstyle = SYNTAX_COMMENT_STYLE (c1);
2252 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
2253 INC_BOTH (from, from_byte);
2254 UPDATE_SYNTAX_TABLE_FORWARD (from);
2255 }
2256
2257 if (prefix)
2258 continue;
2259
2260 switch (SWITCH_ENUM_CAST (code))
2261 {
2262 case Sescape:
2263 case Scharquote:
2264 if (from == stop) goto lose;
2265 INC_BOTH (from, from_byte);
2266 /* treat following character as a word constituent */
2267 case Sword:
2268 case Ssymbol:
2269 if (depth || !sexpflag) break;
2270 /* This word counts as a sexp; return at end of it. */
2271 while (from < stop)
2272 {
2273 UPDATE_SYNTAX_TABLE_FORWARD (from);
2274
2275 /* Some compilers can't handle this inside the switch. */
2276 c = FETCH_CHAR (from_byte);
2277 temp = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2278 switch (temp)
2279 {
2280 case Scharquote:
2281 case Sescape:
2282 INC_BOTH (from, from_byte);
2283 if (from == stop) goto lose;
2284 break;
2285 case Sword:
2286 case Ssymbol:
2287 case Squote:
2288 break;
2289 default:
2290 goto done;
2291 }
2292 INC_BOTH (from, from_byte);
2293 }
2294 goto done;
2295
2296 case Scomment_fence:
2297 comstyle = ST_COMMENT_STYLE;
2298 /* FALLTHROUGH */
2299 case Scomment:
2300 if (!parse_sexp_ignore_comments) break;
2301 UPDATE_SYNTAX_TABLE_FORWARD (from);
2302 found = forw_comment (from, from_byte, stop,
2303 comnested, comstyle, 0,
2304 &out_charpos, &out_bytepos, &dummy);
2305 from = out_charpos, from_byte = out_bytepos;
2306 if (!found)
2307 {
2308 if (depth == 0)
2309 goto done;
2310 goto lose;
2311 }
2312 INC_BOTH (from, from_byte);
2313 UPDATE_SYNTAX_TABLE_FORWARD (from);
2314 break;
2315
2316 case Smath:
2317 if (!sexpflag)
2318 break;
2319 if (from != stop && c == FETCH_CHAR (from_byte))
2320 {
2321 INC_BOTH (from, from_byte);
2322 }
2323 if (mathexit)
2324 {
2325 mathexit = 0;
2326 goto close1;
2327 }
2328 mathexit = 1;
2329
2330 case Sopen:
2331 if (!++depth) goto done;
2332 break;
2333
2334 case Sclose:
2335 close1:
2336 if (!--depth) goto done;
2337 if (depth < min_depth)
2338 Fsignal (Qscan_error,
2339 Fcons (build_string ("Containing expression ends prematurely"),
2340 Fcons (make_number (last_good),
2341 Fcons (make_number (from), Qnil))));
2342 break;
2343
2344 case Sstring:
2345 case Sstring_fence:
2346 temp_pos = dec_bytepos (from_byte);
2347 stringterm = FETCH_CHAR (temp_pos);
2348 while (1)
2349 {
2350 if (from >= stop) goto lose;
2351 UPDATE_SYNTAX_TABLE_FORWARD (from);
2352 c = FETCH_CHAR (from_byte);
2353 if (code == Sstring
2354 ? (c == stringterm
2355 && SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring)
2356 : SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring_fence)
2357 break;
2358
2359 /* Some compilers can't handle this inside the switch. */
2360 temp = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2361 switch (temp)
2362 {
2363 case Scharquote:
2364 case Sescape:
2365 INC_BOTH (from, from_byte);
2366 }
2367 INC_BOTH (from, from_byte);
2368 }
2369 INC_BOTH (from, from_byte);
2370 if (!depth && sexpflag) goto done;
2371 break;
2372 default:
2373 /* Ignore whitespace, punctuation, quote, endcomment. */
2374 break;
2375 }
2376 }
2377
2378 /* Reached end of buffer. Error if within object, return nil if between */
2379 if (depth) goto lose;
2380
2381 immediate_quit = 0;
2382 return Qnil;
2383
2384 /* End of object reached */
2385 done:
2386 count--;
2387 }
2388
2389 while (count < 0)
2390 {
2391 while (from > stop)
2392 {
2393 DEC_BOTH (from, from_byte);
2394 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2395 c = FETCH_CHAR (from_byte);
2396 code = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2397 if (depth == min_depth)
2398 last_good = from;
2399 comstyle = 0;
2400 comnested = SYNTAX_COMMENT_NESTED (c);
2401 if (code == Sendcomment)
2402 comstyle = SYNTAX_COMMENT_STYLE (c);
2403 if (from > stop && SYNTAX_COMEND_SECOND (c)
2404 && prev_char_comend_first (from, from_byte)
2405 && parse_sexp_ignore_comments)
2406 {
2407 /* We must record the comment style encountered so that
2408 later, we can match only the proper comment begin
2409 sequence of the same style. */
2410 DEC_BOTH (from, from_byte);
2411 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2412 code = Sendcomment;
2413 c1 = FETCH_CHAR (from_byte);
2414 comstyle = SYNTAX_COMMENT_STYLE (c1);
2415 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
2416 }
2417
2418 /* Quoting turns anything except a comment-ender
2419 into a word character. Note that this cannot be true
2420 if we decremented FROM in the if-statement above. */
2421 if (code != Sendcomment && char_quoted (from, from_byte))
2422 {
2423 DEC_BOTH (from, from_byte);
2424 code = Sword;
2425 }
2426 else if (SYNTAX_PREFIX (c))
2427 continue;
2428
2429 switch (SWITCH_ENUM_CAST (code))
2430 {
2431 case Sword:
2432 case Ssymbol:
2433 case Sescape:
2434 case Scharquote:
2435 if (depth || !sexpflag) break;
2436 /* This word counts as a sexp; count object finished
2437 after passing it. */
2438 while (from > stop)
2439 {
2440 temp_pos = from_byte;
2441 if (! NILP (current_buffer->enable_multibyte_characters))
2442 DEC_POS (temp_pos);
2443 else
2444 temp_pos--;
2445 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2446 c1 = FETCH_CHAR (temp_pos);
2447 temp_code = SYNTAX_WITH_MULTIBYTE_CHECK (c1);
2448 /* Don't allow comment-end to be quoted. */
2449 if (temp_code == Sendcomment)
2450 goto done2;
2451 quoted = char_quoted (from - 1, temp_pos);
2452 if (quoted)
2453 {
2454 DEC_BOTH (from, from_byte);
2455 temp_pos = dec_bytepos (temp_pos);
2456 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2457 }
2458 c1 = FETCH_CHAR (temp_pos);
2459 temp_code = SYNTAX_WITH_MULTIBYTE_CHECK (c1);
2460 if (! (quoted || temp_code == Sword
2461 || temp_code == Ssymbol
2462 || temp_code == Squote))
2463 goto done2;
2464 DEC_BOTH (from, from_byte);
2465 }
2466 goto done2;
2467
2468 case Smath:
2469 if (!sexpflag)
2470 break;
2471 temp_pos = dec_bytepos (from_byte);
2472 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2473 if (from != stop && c == FETCH_CHAR (temp_pos))
2474 DEC_BOTH (from, from_byte);
2475 if (mathexit)
2476 {
2477 mathexit = 0;
2478 goto open2;
2479 }
2480 mathexit = 1;
2481
2482 case Sclose:
2483 if (!++depth) goto done2;
2484 break;
2485
2486 case Sopen:
2487 open2:
2488 if (!--depth) goto done2;
2489 if (depth < min_depth)
2490 Fsignal (Qscan_error,
2491 Fcons (build_string ("Containing expression ends prematurely"),
2492 Fcons (make_number (last_good),
2493 Fcons (make_number (from), Qnil))));
2494 break;
2495
2496 case Sendcomment:
2497 if (!parse_sexp_ignore_comments)
2498 break;
2499 found = back_comment (from, from_byte, stop, comnested, comstyle,
2500 &out_charpos, &out_bytepos);
2501 /* FIXME: if found == -1, then it really wasn't a comment-end.
2502 For single-char Sendcomment, we can't do much about it apart
2503 from skipping the char.
2504 For 2-char endcomments, we could try again, taking both
2505 chars as separate entities, but it's a lot of trouble
2506 for very little gain, so we don't bother either. -sm */
2507 if (found != -1)
2508 from = out_charpos, from_byte = out_bytepos;
2509 break;
2510
2511 case Scomment_fence:
2512 case Sstring_fence:
2513 while (1)
2514 {
2515 if (from == stop) goto lose;
2516 DEC_BOTH (from, from_byte);
2517 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2518 if (!char_quoted (from, from_byte)
2519 && (c = FETCH_CHAR (from_byte),
2520 SYNTAX_WITH_MULTIBYTE_CHECK (c) == code))
2521 break;
2522 }
2523 if (code == Sstring_fence && !depth && sexpflag) goto done2;
2524 break;
2525
2526 case Sstring:
2527 stringterm = FETCH_CHAR (from_byte);
2528 while (1)
2529 {
2530 if (from == stop) goto lose;
2531 DEC_BOTH (from, from_byte);
2532 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2533 if (!char_quoted (from, from_byte)
2534 && stringterm == (c = FETCH_CHAR (from_byte))
2535 && SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring)
2536 break;
2537 }
2538 if (!depth && sexpflag) goto done2;
2539 break;
2540 default:
2541 /* Ignore whitespace, punctuation, quote, endcomment. */
2542 break;
2543 }
2544 }
2545
2546 /* Reached start of buffer. Error if within object, return nil if between */
2547 if (depth) goto lose;
2548
2549 immediate_quit = 0;
2550 return Qnil;
2551
2552 done2:
2553 count++;
2554 }
2555
2556
2557 immediate_quit = 0;
2558 XSETFASTINT (val, from);
2559 return val;
2560
2561 lose:
2562 Fsignal (Qscan_error,
2563 Fcons (build_string ("Unbalanced parentheses"),
2564 Fcons (make_number (last_good),
2565 Fcons (make_number (from), Qnil))));
2566 abort ();
2567 /* NOTREACHED */
2568 }
2569
2570 DEFUN ("scan-lists", Fscan_lists, Sscan_lists, 3, 3, 0,
2571 doc: /* Scan from character number FROM by COUNT lists.
2572 Returns the character number of the position thus found.
2573
2574 If DEPTH is nonzero, paren depth begins counting from that value,
2575 only places where the depth in parentheses becomes zero
2576 are candidates for stopping; COUNT such places are counted.
2577 Thus, a positive value for DEPTH means go out levels.
2578
2579 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2580
2581 If the beginning or end of (the accessible part of) the buffer is reached
2582 and the depth is wrong, an error is signaled.
2583 If the depth is right but the count is not used up, nil is returned. */)
2584 (from, count, depth)
2585 Lisp_Object from, count, depth;
2586 {
2587 CHECK_NUMBER (from);
2588 CHECK_NUMBER (count);
2589 CHECK_NUMBER (depth);
2590
2591 return scan_lists (XINT (from), XINT (count), XINT (depth), 0);
2592 }
2593
2594 DEFUN ("scan-sexps", Fscan_sexps, Sscan_sexps, 2, 2, 0,
2595 doc: /* Scan from character number FROM by COUNT balanced expressions.
2596 If COUNT is negative, scan backwards.
2597 Returns the character number of the position thus found.
2598
2599 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2600
2601 If the beginning or end of (the accessible part of) the buffer is reached
2602 in the middle of a parenthetical grouping, an error is signaled.
2603 If the beginning or end is reached between groupings
2604 but before count is used up, nil is returned. */)
2605 (from, count)
2606 Lisp_Object from, count;
2607 {
2608 CHECK_NUMBER (from);
2609 CHECK_NUMBER (count);
2610
2611 return scan_lists (XINT (from), XINT (count), 0, 1);
2612 }
2613
2614 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars, Sbackward_prefix_chars,
2615 0, 0, 0,
2616 doc: /* Move point backward over any number of chars with prefix syntax.
2617 This includes chars with "quote" or "prefix" syntax (' or p). */)
2618 ()
2619 {
2620 int beg = BEGV;
2621 int opoint = PT;
2622 int opoint_byte = PT_BYTE;
2623 int pos = PT;
2624 int pos_byte = PT_BYTE;
2625 int c;
2626
2627 if (pos <= beg)
2628 {
2629 SET_PT_BOTH (opoint, opoint_byte);
2630
2631 return Qnil;
2632 }
2633
2634 SETUP_SYNTAX_TABLE (pos, -1);
2635
2636 DEC_BOTH (pos, pos_byte);
2637
2638 while (!char_quoted (pos, pos_byte)
2639 /* Previous statement updates syntax table. */
2640 && ((c = FETCH_CHAR (pos_byte), SYNTAX (c) == Squote)
2641 || SYNTAX_PREFIX (c)))
2642 {
2643 opoint = pos;
2644 opoint_byte = pos_byte;
2645
2646 if (pos + 1 > beg)
2647 DEC_BOTH (pos, pos_byte);
2648 }
2649
2650 SET_PT_BOTH (opoint, opoint_byte);
2651
2652 return Qnil;
2653 }
2654 \f
2655 /* Parse forward from FROM / FROM_BYTE to END,
2656 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
2657 and return a description of the state of the parse at END.
2658 If STOPBEFORE is nonzero, stop at the start of an atom.
2659 If COMMENTSTOP is 1, stop at the start of a comment.
2660 If COMMENTSTOP is -1, stop at the start or end of a comment,
2661 after the beginning of a string, or after the end of a string. */
2662
2663 static void
2664 scan_sexps_forward (stateptr, from, from_byte, end, targetdepth,
2665 stopbefore, oldstate, commentstop)
2666 struct lisp_parse_state *stateptr;
2667 register int from;
2668 int end, targetdepth, stopbefore, from_byte;
2669 Lisp_Object oldstate;
2670 int commentstop;
2671 {
2672 struct lisp_parse_state state;
2673
2674 register enum syntaxcode code;
2675 int c1;
2676 int comnested;
2677 struct level { int last, prev; };
2678 struct level levelstart[100];
2679 register struct level *curlevel = levelstart;
2680 struct level *endlevel = levelstart + 100;
2681 register int depth; /* Paren depth of current scanning location.
2682 level - levelstart equals this except
2683 when the depth becomes negative. */
2684 int mindepth; /* Lowest DEPTH value seen. */
2685 int start_quoted = 0; /* Nonzero means starting after a char quote */
2686 Lisp_Object tem;
2687 int prev_from; /* Keep one character before FROM. */
2688 int prev_from_byte;
2689 int prev_from_syntax;
2690 int boundary_stop = commentstop == -1;
2691 int nofence;
2692 int found;
2693 int out_bytepos, out_charpos;
2694 int temp;
2695
2696 prev_from = from;
2697 prev_from_byte = from_byte;
2698 if (from != BEGV)
2699 DEC_BOTH (prev_from, prev_from_byte);
2700
2701 /* Use this macro instead of `from++'. */
2702 #define INC_FROM \
2703 do { prev_from = from; \
2704 prev_from_byte = from_byte; \
2705 temp = FETCH_CHAR (prev_from_byte); \
2706 prev_from_syntax = SYNTAX_WITH_FLAGS (temp); \
2707 INC_BOTH (from, from_byte); \
2708 if (from < end) \
2709 UPDATE_SYNTAX_TABLE_FORWARD (from); \
2710 } while (0)
2711
2712 immediate_quit = 1;
2713 QUIT;
2714
2715 if (NILP (oldstate))
2716 {
2717 depth = 0;
2718 state.instring = -1;
2719 state.incomment = 0;
2720 state.comstyle = 0; /* comment style a by default. */
2721 state.comstr_start = -1; /* no comment/string seen. */
2722 }
2723 else
2724 {
2725 tem = Fcar (oldstate);
2726 if (!NILP (tem))
2727 depth = XINT (tem);
2728 else
2729 depth = 0;
2730
2731 oldstate = Fcdr (oldstate);
2732 oldstate = Fcdr (oldstate);
2733 oldstate = Fcdr (oldstate);
2734 tem = Fcar (oldstate);
2735 /* Check whether we are inside string_fence-style string: */
2736 state.instring = (!NILP (tem)
2737 ? (INTEGERP (tem) ? XINT (tem) : ST_STRING_STYLE)
2738 : -1);
2739
2740 oldstate = Fcdr (oldstate);
2741 tem = Fcar (oldstate);
2742 state.incomment = (!NILP (tem)
2743 ? (INTEGERP (tem) ? XINT (tem) : -1)
2744 : 0);
2745
2746 oldstate = Fcdr (oldstate);
2747 tem = Fcar (oldstate);
2748 start_quoted = !NILP (tem);
2749
2750 /* if the eighth element of the list is nil, we are in comment
2751 style a. If it is non-nil, we are in comment style b */
2752 oldstate = Fcdr (oldstate);
2753 oldstate = Fcdr (oldstate);
2754 tem = Fcar (oldstate);
2755 state.comstyle = NILP (tem) ? 0 : (EQ (tem, Qsyntax_table)
2756 ? ST_COMMENT_STYLE : 1);
2757
2758 oldstate = Fcdr (oldstate);
2759 tem = Fcar (oldstate);
2760 state.comstr_start = NILP (tem) ? -1 : XINT (tem) ;
2761 oldstate = Fcdr (oldstate);
2762 tem = Fcar (oldstate);
2763 while (!NILP (tem)) /* >= second enclosing sexps. */
2764 {
2765 /* curlevel++->last ran into compiler bug on Apollo */
2766 curlevel->last = XINT (Fcar (tem));
2767 if (++curlevel == endlevel)
2768 curlevel--; /* error ("Nesting too deep for parser"); */
2769 curlevel->prev = -1;
2770 curlevel->last = -1;
2771 tem = Fcdr (tem);
2772 }
2773 }
2774 state.quoted = 0;
2775 mindepth = depth;
2776
2777 curlevel->prev = -1;
2778 curlevel->last = -1;
2779
2780 SETUP_SYNTAX_TABLE (prev_from, 1);
2781 temp = FETCH_CHAR (prev_from_byte);
2782 prev_from_syntax = SYNTAX_WITH_FLAGS (temp);
2783 UPDATE_SYNTAX_TABLE_FORWARD (from);
2784
2785 /* Enter the loop at a place appropriate for initial state. */
2786
2787 if (state.incomment)
2788 goto startincomment;
2789 if (state.instring >= 0)
2790 {
2791 nofence = state.instring != ST_STRING_STYLE;
2792 if (start_quoted)
2793 goto startquotedinstring;
2794 goto startinstring;
2795 }
2796 else if (start_quoted)
2797 goto startquoted;
2798
2799 while (from < end)
2800 {
2801 INC_FROM;
2802 code = prev_from_syntax & 0xff;
2803
2804 if (from < end
2805 && SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax)
2806 && (c1 = FETCH_CHAR (from_byte),
2807 SYNTAX_COMSTART_SECOND (c1)))
2808 /* Duplicate code to avoid a complex if-expression
2809 which causes trouble for the SGI compiler. */
2810 {
2811 /* Record the comment style we have entered so that only
2812 the comment-end sequence of the same style actually
2813 terminates the comment section. */
2814 state.comstyle = SYNTAX_COMMENT_STYLE (c1);
2815 comnested = SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax);
2816 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
2817 state.incomment = comnested ? 1 : -1;
2818 state.comstr_start = prev_from;
2819 INC_FROM;
2820 code = Scomment;
2821 }
2822 else if (code == Scomment_fence)
2823 {
2824 /* Record the comment style we have entered so that only
2825 the comment-end sequence of the same style actually
2826 terminates the comment section. */
2827 state.comstyle = ST_COMMENT_STYLE;
2828 state.incomment = -1;
2829 state.comstr_start = prev_from;
2830 code = Scomment;
2831 }
2832 else if (code == Scomment)
2833 {
2834 state.comstyle = SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax);
2835 state.incomment = (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax) ?
2836 1 : -1);
2837 state.comstr_start = prev_from;
2838 }
2839
2840 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax))
2841 continue;
2842 switch (SWITCH_ENUM_CAST (code))
2843 {
2844 case Sescape:
2845 case Scharquote:
2846 if (stopbefore) goto stop; /* this arg means stop at sexp start */
2847 curlevel->last = prev_from;
2848 startquoted:
2849 if (from == end) goto endquoted;
2850 INC_FROM;
2851 goto symstarted;
2852 /* treat following character as a word constituent */
2853 case Sword:
2854 case Ssymbol:
2855 if (stopbefore) goto stop; /* this arg means stop at sexp start */
2856 curlevel->last = prev_from;
2857 symstarted:
2858 while (from < end)
2859 {
2860 /* Some compilers can't handle this inside the switch. */
2861 temp = FETCH_CHAR (from_byte);
2862 temp = SYNTAX (temp);
2863 switch (temp)
2864 {
2865 case Scharquote:
2866 case Sescape:
2867 INC_FROM;
2868 if (from == end) goto endquoted;
2869 break;
2870 case Sword:
2871 case Ssymbol:
2872 case Squote:
2873 break;
2874 default:
2875 goto symdone;
2876 }
2877 INC_FROM;
2878 }
2879 symdone:
2880 curlevel->prev = curlevel->last;
2881 break;
2882
2883 case Scomment_fence: /* Can't happen because it's handled above. */
2884 case Scomment:
2885 if (commentstop || boundary_stop) goto done;
2886 startincomment:
2887 /* The (from == BEGV) test was to enter the loop in the middle so
2888 that we find a 2-char comment ender even if we start in the
2889 middle of it. We don't want to do that if we're just at the
2890 beginning of the comment (think of (*) ... (*)). */
2891 found = forw_comment (from, from_byte, end,
2892 state.incomment, state.comstyle,
2893 (from == BEGV || from < state.comstr_start + 3)
2894 ? 0 : prev_from_syntax,
2895 &out_charpos, &out_bytepos, &state.incomment);
2896 from = out_charpos; from_byte = out_bytepos;
2897 /* Beware! prev_from and friends are invalid now.
2898 Luckily, the `done' doesn't use them and the INC_FROM
2899 sets them to a sane value without looking at them. */
2900 if (!found) goto done;
2901 INC_FROM;
2902 state.incomment = 0;
2903 state.comstyle = 0; /* reset the comment style */
2904 if (boundary_stop) goto done;
2905 break;
2906
2907 case Sopen:
2908 if (stopbefore) goto stop; /* this arg means stop at sexp start */
2909 depth++;
2910 /* curlevel++->last ran into compiler bug on Apollo */
2911 curlevel->last = prev_from;
2912 if (++curlevel == endlevel)
2913 curlevel--; /* error ("Nesting too deep for parser"); */
2914 curlevel->prev = -1;
2915 curlevel->last = -1;
2916 if (targetdepth == depth) goto done;
2917 break;
2918
2919 case Sclose:
2920 depth--;
2921 if (depth < mindepth)
2922 mindepth = depth;
2923 if (curlevel != levelstart)
2924 curlevel--;
2925 curlevel->prev = curlevel->last;
2926 if (targetdepth == depth) goto done;
2927 break;
2928
2929 case Sstring:
2930 case Sstring_fence:
2931 state.comstr_start = from - 1;
2932 if (stopbefore) goto stop; /* this arg means stop at sexp start */
2933 curlevel->last = prev_from;
2934 state.instring = (code == Sstring
2935 ? (FETCH_CHAR (prev_from_byte))
2936 : ST_STRING_STYLE);
2937 if (boundary_stop) goto done;
2938 startinstring:
2939 {
2940 nofence = state.instring != ST_STRING_STYLE;
2941
2942 while (1)
2943 {
2944 int c;
2945
2946 if (from >= end) goto done;
2947 c = FETCH_CHAR (from_byte);
2948 /* Some compilers can't handle this inside the switch. */
2949 temp = SYNTAX (c);
2950
2951 /* Check TEMP here so that if the char has
2952 a syntax-table property which says it is NOT
2953 a string character, it does not end the string. */
2954 if (nofence && c == state.instring && temp == Sstring)
2955 break;
2956
2957 switch (temp)
2958 {
2959 case Sstring_fence:
2960 if (!nofence) goto string_end;
2961 break;
2962 case Scharquote:
2963 case Sescape:
2964 INC_FROM;
2965 startquotedinstring:
2966 if (from >= end) goto endquoted;
2967 }
2968 INC_FROM;
2969 }
2970 }
2971 string_end:
2972 state.instring = -1;
2973 curlevel->prev = curlevel->last;
2974 INC_FROM;
2975 if (boundary_stop) goto done;
2976 break;
2977
2978 case Smath:
2979 /* FIXME: We should do something with it. */
2980 break;
2981 default:
2982 /* Ignore whitespace, punctuation, quote, endcomment. */
2983 break;
2984 }
2985 }
2986 goto done;
2987
2988 stop: /* Here if stopping before start of sexp. */
2989 from = prev_from; /* We have just fetched the char that starts it; */
2990 goto done; /* but return the position before it. */
2991
2992 endquoted:
2993 state.quoted = 1;
2994 done:
2995 state.depth = depth;
2996 state.mindepth = mindepth;
2997 state.thislevelstart = curlevel->prev;
2998 state.prevlevelstart
2999 = (curlevel == levelstart) ? -1 : (curlevel - 1)->last;
3000 state.location = from;
3001 state.levelstarts = Qnil;
3002 while (--curlevel >= levelstart)
3003 state.levelstarts = Fcons (make_number (curlevel->last),
3004 state.levelstarts);
3005 immediate_quit = 0;
3006
3007 *stateptr = state;
3008 }
3009
3010 DEFUN ("parse-partial-sexp", Fparse_partial_sexp, Sparse_partial_sexp, 2, 6, 0,
3011 doc: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
3012 Parsing stops at TO or when certain criteria are met;
3013 point is set to where parsing stops.
3014 If fifth arg OLDSTATE is omitted or nil,
3015 parsing assumes that FROM is the beginning of a function.
3016 Value is a list of ten elements describing final state of parsing:
3017 0. depth in parens.
3018 1. character address of start of innermost containing list; nil if none.
3019 2. character address of start of last complete sexp terminated.
3020 3. non-nil if inside a string.
3021 (it is the character that will terminate the string,
3022 or t if the string should be terminated by a generic string delimiter.)
3023 4. nil if outside a comment, t if inside a non-nestable comment,
3024 else an integer (the current comment nesting).
3025 5. t if following a quote character.
3026 6. the minimum paren-depth encountered during this scan.
3027 7. t if in a comment of style b; symbol `syntax-table' if the comment
3028 should be terminated by a generic comment delimiter.
3029 8. character address of start of comment or string; nil if not in one.
3030 9. Intermediate data for continuation of parsing (subject to change).
3031 If third arg TARGETDEPTH is non-nil, parsing stops if the depth
3032 in parentheses becomes equal to TARGETDEPTH.
3033 Fourth arg STOPBEFORE non-nil means stop when come to
3034 any character that starts a sexp.
3035 Fifth arg OLDSTATE is a nine-element list like what this function returns.
3036 It is used to initialize the state of the parse. Elements number 1, 2, 6
3037 and 8 are ignored; you can leave off element 8 (the last) entirely.
3038 Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.
3039 If it is symbol `syntax-table', stop after the start of a comment or a
3040 string, or after end of a comment or a string. */)
3041 (from, to, targetdepth, stopbefore, oldstate, commentstop)
3042 Lisp_Object from, to, targetdepth, stopbefore, oldstate, commentstop;
3043 {
3044 struct lisp_parse_state state;
3045 int target;
3046
3047 if (!NILP (targetdepth))
3048 {
3049 CHECK_NUMBER (targetdepth);
3050 target = XINT (targetdepth);
3051 }
3052 else
3053 target = -100000; /* We won't reach this depth */
3054
3055 validate_region (&from, &to);
3056 scan_sexps_forward (&state, XINT (from), CHAR_TO_BYTE (XINT (from)),
3057 XINT (to),
3058 target, !NILP (stopbefore), oldstate,
3059 (NILP (commentstop)
3060 ? 0 : (EQ (commentstop, Qsyntax_table) ? -1 : 1)));
3061
3062 SET_PT (state.location);
3063
3064 return Fcons (make_number (state.depth),
3065 Fcons (state.prevlevelstart < 0 ? Qnil : make_number (state.prevlevelstart),
3066 Fcons (state.thislevelstart < 0 ? Qnil : make_number (state.thislevelstart),
3067 Fcons (state.instring >= 0
3068 ? (state.instring == ST_STRING_STYLE
3069 ? Qt : make_number (state.instring)) : Qnil,
3070 Fcons (state.incomment < 0 ? Qt :
3071 (state.incomment == 0 ? Qnil :
3072 make_number (state.incomment)),
3073 Fcons (state.quoted ? Qt : Qnil,
3074 Fcons (make_number (state.mindepth),
3075 Fcons ((state.comstyle
3076 ? (state.comstyle == ST_COMMENT_STYLE
3077 ? Qsyntax_table : Qt) :
3078 Qnil),
3079 Fcons (((state.incomment
3080 || (state.instring >= 0))
3081 ? make_number (state.comstr_start)
3082 : Qnil),
3083 Fcons (state.levelstarts, Qnil))))))))));
3084 }
3085 \f
3086 void
3087 init_syntax_once ()
3088 {
3089 register int i, c;
3090 Lisp_Object temp;
3091
3092 /* This has to be done here, before we call Fmake_char_table. */
3093 Qsyntax_table = intern ("syntax-table");
3094 staticpro (&Qsyntax_table);
3095
3096 /* Intern this now in case it isn't already done.
3097 Setting this variable twice is harmless.
3098 But don't staticpro it here--that is done in alloc.c. */
3099 Qchar_table_extra_slots = intern ("char-table-extra-slots");
3100
3101 /* Create objects which can be shared among syntax tables. */
3102 Vsyntax_code_object = Fmake_vector (make_number (Smax), Qnil);
3103 for (i = 0; i < XVECTOR (Vsyntax_code_object)->size; i++)
3104 XVECTOR (Vsyntax_code_object)->contents[i]
3105 = Fcons (make_number (i), Qnil);
3106
3107 /* Now we are ready to set up this property, so we can
3108 create syntax tables. */
3109 Fput (Qsyntax_table, Qchar_table_extra_slots, make_number (0));
3110
3111 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Swhitespace];
3112
3113 Vstandard_syntax_table = Fmake_char_table (Qsyntax_table, temp);
3114
3115 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Sword];
3116 for (i = 'a'; i <= 'z'; i++)
3117 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3118 for (i = 'A'; i <= 'Z'; i++)
3119 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3120 for (i = '0'; i <= '9'; i++)
3121 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3122
3123 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '$', temp);
3124 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '%', temp);
3125
3126 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '(',
3127 Fcons (make_number (Sopen), make_number (')')));
3128 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ')',
3129 Fcons (make_number (Sclose), make_number ('(')));
3130 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '[',
3131 Fcons (make_number (Sopen), make_number (']')));
3132 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ']',
3133 Fcons (make_number (Sclose), make_number ('[')));
3134 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '{',
3135 Fcons (make_number (Sopen), make_number ('}')));
3136 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '}',
3137 Fcons (make_number (Sclose), make_number ('{')));
3138 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '"',
3139 Fcons (make_number ((int) Sstring), Qnil));
3140 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\\',
3141 Fcons (make_number ((int) Sescape), Qnil));
3142
3143 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Ssymbol];
3144 for (i = 0; i < 10; i++)
3145 {
3146 c = "_-+*/&|<>="[i];
3147 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3148 }
3149
3150 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Spunct];
3151 for (i = 0; i < 12; i++)
3152 {
3153 c = ".,;:?!#@~^'`"[i];
3154 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3155 }
3156
3157 /* All multibyte characters have syntax `word' by default. */
3158 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Sword];
3159 for (i = CHAR_TABLE_SINGLE_BYTE_SLOTS; i < CHAR_TABLE_ORDINARY_SLOTS; i++)
3160 XCHAR_TABLE (Vstandard_syntax_table)->contents[i] = temp;
3161 }
3162
3163 void
3164 syms_of_syntax ()
3165 {
3166 Qsyntax_table_p = intern ("syntax-table-p");
3167 staticpro (&Qsyntax_table_p);
3168
3169 staticpro (&Vsyntax_code_object);
3170
3171 Qscan_error = intern ("scan-error");
3172 staticpro (&Qscan_error);
3173 Fput (Qscan_error, Qerror_conditions,
3174 Fcons (Qscan_error, Fcons (Qerror, Qnil)));
3175 Fput (Qscan_error, Qerror_message,
3176 build_string ("Scan error"));
3177
3178 DEFVAR_BOOL ("parse-sexp-ignore-comments", &parse_sexp_ignore_comments,
3179 doc: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
3180
3181 DEFVAR_BOOL ("parse-sexp-lookup-properties", &parse_sexp_lookup_properties,
3182 doc: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3183 Otherwise, that text property is simply ignored.
3184 See the info node `(elisp)Syntax Properties' for a description of the
3185 `syntax-table' property. */);
3186
3187 words_include_escapes = 0;
3188 DEFVAR_BOOL ("words-include-escapes", &words_include_escapes,
3189 doc: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
3190
3191 DEFVAR_BOOL ("multibyte-syntax-as-symbol", &multibyte_syntax_as_symbol,
3192 doc: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
3193 multibyte_syntax_as_symbol = 0;
3194
3195 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3196 &open_paren_in_column_0_is_defun_start,
3197 doc: /* *Non-nil means an open paren in column 0 denotes the start of a defun. */);
3198 open_paren_in_column_0_is_defun_start = 1;
3199
3200 defsubr (&Ssyntax_table_p);
3201 defsubr (&Ssyntax_table);
3202 defsubr (&Sstandard_syntax_table);
3203 defsubr (&Scopy_syntax_table);
3204 defsubr (&Sset_syntax_table);
3205 defsubr (&Schar_syntax);
3206 defsubr (&Smatching_paren);
3207 defsubr (&Sstring_to_syntax);
3208 defsubr (&Smodify_syntax_entry);
3209 defsubr (&Sinternal_describe_syntax_value);
3210
3211 defsubr (&Sforward_word);
3212
3213 defsubr (&Sskip_chars_forward);
3214 defsubr (&Sskip_chars_backward);
3215 defsubr (&Sskip_syntax_forward);
3216 defsubr (&Sskip_syntax_backward);
3217
3218 defsubr (&Sforward_comment);
3219 defsubr (&Sscan_lists);
3220 defsubr (&Sscan_sexps);
3221 defsubr (&Sbackward_prefix_chars);
3222 defsubr (&Sparse_partial_sexp);
3223 }
3224
3225 /* arch-tag: 3e297b9f-088e-4b64-8f4c-fb0b3443e412
3226 (do not change this comment) */