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