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