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