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