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