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