Revision: miles@gnu.org--gnu-2004/emacs--unicode--0--patch-57
[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., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21
22 #include <config.h>
23 #include <ctype.h>
24 #include "lisp.h"
25 #include "commands.h"
26 #include "buffer.h"
27 #include "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 int orig_val, val;
1324
1325 if (NILP (arg))
1326 XSETFASTINT (arg, 1);
1327 else
1328 CHECK_NUMBER (arg);
1329
1330 val = orig_val = scan_words (PT, XINT (arg));
1331 if (! orig_val)
1332 val = XINT (arg) > 0 ? ZV : BEGV;
1333
1334 /* Avoid jumping out of an input field. */
1335 val = XFASTINT (Fconstrain_to_field (make_number (val), make_number (PT),
1336 Qt, Qnil, Qnil));
1337
1338 SET_PT (val);
1339 return val == orig_val ? Qt : Qnil;
1340 }
1341 \f
1342 Lisp_Object skip_chars ();
1343
1344 DEFUN ("skip-chars-forward", Fskip_chars_forward, Sskip_chars_forward, 1, 2, 0,
1345 doc: /* Move point forward, stopping before a char not in STRING, or at pos LIM.
1346 STRING is like the inside of a `[...]' in a regular expression
1347 except that `]' is never special and `\\' quotes `^', `-' or `\\'
1348 (but not as the end of a range; quoting is never needed there).
1349 Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
1350 With arg "^a-zA-Z", skips nonletters stopping before first letter.
1351 Char classes, e.g. `[:alpha:]', are supported.
1352
1353 Returns the distance traveled, either zero or positive. */)
1354 (string, lim)
1355 Lisp_Object string, lim;
1356 {
1357 return skip_chars (1, string, lim, 1);
1358 }
1359
1360 DEFUN ("skip-chars-backward", Fskip_chars_backward, Sskip_chars_backward, 1, 2, 0,
1361 doc: /* Move point backward, stopping after a char not in STRING, or at pos LIM.
1362 See `skip-chars-forward' for details.
1363 Returns the distance traveled, either zero or negative. */)
1364 (string, lim)
1365 Lisp_Object string, lim;
1366 {
1367 return skip_chars (0, string, lim, 1);
1368 }
1369
1370 DEFUN ("skip-syntax-forward", Fskip_syntax_forward, Sskip_syntax_forward, 1, 2, 0,
1371 doc: /* Move point forward across chars in specified syntax classes.
1372 SYNTAX is a string of syntax code characters.
1373 Stop before a char whose syntax is not in SYNTAX, or at position LIM.
1374 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1375 This function returns the distance traveled, either zero or positive. */)
1376 (syntax, lim)
1377 Lisp_Object syntax, lim;
1378 {
1379 return skip_syntaxes (1, syntax, lim);
1380 }
1381
1382 DEFUN ("skip-syntax-backward", Fskip_syntax_backward, Sskip_syntax_backward, 1, 2, 0,
1383 doc: /* Move point backward across chars in specified syntax classes.
1384 SYNTAX is a string of syntax code characters.
1385 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.
1386 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1387 This function returns the distance traveled, either zero or negative. */)
1388 (syntax, lim)
1389 Lisp_Object syntax, lim;
1390 {
1391 return skip_syntaxes (0, syntax, lim);
1392 }
1393
1394 static Lisp_Object
1395 skip_chars (forwardp, string, lim, handle_iso_classes)
1396 int forwardp;
1397 Lisp_Object string, lim;
1398 int handle_iso_classes;
1399 {
1400 register unsigned int c;
1401 unsigned char fastmap[0400];
1402 /* Store the ranges of non-ASCII characters. */
1403 int *char_ranges;
1404 int n_char_ranges = 0;
1405 int negate = 0;
1406 register int i, i_byte;
1407 /* Set to 1 if the current buffer is multibyte and the region
1408 contains non-ASCII chars. */
1409 int multibyte;
1410 /* Set to 1 if STRING is multibyte and it contains non-ASCII
1411 chars. */
1412 int string_multibyte;
1413 int size_byte;
1414 const unsigned char *str;
1415 int len;
1416 Lisp_Object iso_classes;
1417
1418 CHECK_STRING (string);
1419 iso_classes = Qnil;
1420
1421 if (NILP (lim))
1422 XSETINT (lim, forwardp ? ZV : BEGV);
1423 else
1424 CHECK_NUMBER_COERCE_MARKER (lim);
1425
1426 /* In any case, don't allow scan outside bounds of buffer. */
1427 if (XINT (lim) > ZV)
1428 XSETFASTINT (lim, ZV);
1429 if (XINT (lim) < BEGV)
1430 XSETFASTINT (lim, BEGV);
1431
1432 multibyte = (!NILP (current_buffer->enable_multibyte_characters)
1433 && (XINT (lim) - PT != CHAR_TO_BYTE (XINT (lim)) - PT_BYTE));
1434 string_multibyte = SBYTES (string) > SCHARS (string);
1435
1436 bzero (fastmap, sizeof fastmap);
1437
1438 str = SDATA (string);
1439 size_byte = SBYTES (string);
1440
1441 i_byte = 0;
1442 if (i_byte < size_byte
1443 && SREF (string, 0) == '^')
1444 {
1445 negate = 1; i_byte++;
1446 }
1447
1448 /* Find the characters specified and set their elements of fastmap.
1449 Handle backslashes and ranges specially.
1450
1451 If STRING contains non-ASCII characters, setup char_ranges for
1452 them and use fastmap only for their leading codes. */
1453
1454 if (! string_multibyte)
1455 {
1456 int string_has_eight_bit = 0;
1457
1458 /* At first setup fastmap. */
1459 while (i_byte < size_byte)
1460 {
1461 c = str[i_byte++];
1462
1463 if (handle_iso_classes && c == '['
1464 && i_byte < size_byte
1465 && str[i_byte] == ':')
1466 {
1467 const unsigned char *class_beg = str + i_byte + 1;
1468 const unsigned char *class_end = class_beg;
1469 const unsigned char *class_limit = str + size_byte - 2;
1470 /* Leave room for the null. */
1471 unsigned char class_name[CHAR_CLASS_MAX_LENGTH + 1];
1472 re_wctype_t cc;
1473
1474 if (class_limit - class_beg > CHAR_CLASS_MAX_LENGTH)
1475 class_limit = class_beg + CHAR_CLASS_MAX_LENGTH;
1476
1477 while (class_end < class_limit
1478 && *class_end >= 'a' && *class_end <= 'z')
1479 class_end++;
1480
1481 if (class_end == class_beg
1482 || *class_end != ':' || class_end[1] != ']')
1483 goto not_a_class_name;
1484
1485 bcopy (class_beg, class_name, class_end - class_beg);
1486 class_name[class_end - class_beg] = 0;
1487
1488 cc = re_wctype (class_name);
1489 if (cc == 0)
1490 error ("Invalid ISO C character class");
1491
1492 iso_classes = Fcons (make_number (cc), iso_classes);
1493
1494 i_byte = class_end + 2 - str;
1495 continue;
1496 }
1497
1498 not_a_class_name:
1499 if (c == '\\')
1500 {
1501 if (i_byte == size_byte)
1502 break;
1503
1504 c = str[i_byte++];
1505 }
1506 /* Treat `-' as range character only if another character
1507 follows. */
1508 if (i_byte + 1 < size_byte
1509 && str[i_byte] == '-')
1510 {
1511 unsigned int c2;
1512
1513 /* Skip over the dash. */
1514 i_byte++;
1515
1516 /* Get the end of the range. */
1517 c2 = str[i_byte++];
1518 if (c2 == '\\'
1519 && i_byte < size_byte)
1520 c2 = str[i_byte++];
1521
1522 if (c <= c2)
1523 {
1524 while (c <= c2)
1525 fastmap[c++] = 1;
1526 if (! ASCII_CHAR_P (c2))
1527 string_has_eight_bit = 1;
1528 }
1529 }
1530 else
1531 {
1532 fastmap[c] = 1;
1533 if (! ASCII_CHAR_P (c))
1534 string_has_eight_bit = 1;
1535 }
1536 }
1537
1538 /* If the current range is multibyte and STRING contains
1539 eight-bit chars, arrange fastmap and setup char_ranges for
1540 the corresponding multibyte chars. */
1541 if (multibyte && string_has_eight_bit)
1542 {
1543 unsigned char fastmap2[0400];
1544 int range_start_byte, range_start_char;
1545
1546 bcopy (fastmap2 + 0200, fastmap + 0200, 0200);
1547 bzero (fastmap + 0200, 0200);
1548 /* We are sure that this loop stops. */
1549 for (i = 0200; ! fastmap2[i]; i++);
1550 c = unibyte_char_to_multibyte (i);
1551 fastmap[CHAR_LEADING_CODE (c)] = 1;
1552 range_start_byte = i;
1553 range_start_char = c;
1554 char_ranges = (int *) alloca (sizeof (int) * 128 * 2);
1555 for (i = 129; i < 0400; i++)
1556 {
1557 c = unibyte_char_to_multibyte (i);
1558 fastmap[CHAR_LEADING_CODE (c)] = 1;
1559 if (i - range_start_byte != c - range_start_char)
1560 {
1561 char_ranges[n_char_ranges++] = range_start_char;
1562 char_ranges[n_char_ranges++] = ((i - 1 - range_start_byte)
1563 + range_start_char);
1564 range_start_byte = i;
1565 range_start_char = c;
1566 }
1567 }
1568 char_ranges[n_char_ranges++] = range_start_char;
1569 char_ranges[n_char_ranges++] = ((i - 1 - range_start_byte)
1570 + range_start_char);
1571 }
1572 }
1573 else /* STRING is multibyte */
1574 {
1575 char_ranges = (int *) alloca (sizeof (int) * SCHARS (string) * 2);
1576
1577 while (i_byte < size_byte)
1578 {
1579 unsigned char leading_code;
1580
1581 leading_code = str[i_byte];
1582 c = STRING_CHAR_AND_LENGTH (str + i_byte, size_byte-i_byte, len);
1583 i_byte += len;
1584
1585 if (handle_iso_classes && c == '['
1586 && i_byte < size_byte
1587 && STRING_CHAR (str + i_byte, size_byte - i_byte) == ':')
1588 {
1589 const unsigned char *class_beg = str + i_byte + 1;
1590 const unsigned char *class_end = class_beg;
1591 const unsigned char *class_limit = str + size_byte - 2;
1592 /* Leave room for the null. */
1593 unsigned char class_name[CHAR_CLASS_MAX_LENGTH + 1];
1594 re_wctype_t cc;
1595
1596 if (class_limit - class_beg > CHAR_CLASS_MAX_LENGTH)
1597 class_limit = class_beg + CHAR_CLASS_MAX_LENGTH;
1598
1599 while (class_end < class_limit
1600 && *class_end >= 'a' && *class_end <= 'z')
1601 class_end++;
1602
1603 if (class_end == class_beg
1604 || *class_end != ':' || class_end[1] != ']')
1605 goto not_a_class_name_multibyte;
1606
1607 bcopy (class_beg, class_name, class_end - class_beg);
1608 class_name[class_end - class_beg] = 0;
1609
1610 cc = re_wctype (class_name);
1611 if (cc == 0)
1612 error ("Invalid ISO C character class");
1613
1614 iso_classes = Fcons (make_number (cc), iso_classes);
1615
1616 i_byte = class_end + 2 - str;
1617 continue;
1618 }
1619
1620 not_a_class_name_multibyte:
1621 if (c == '\\')
1622 {
1623 if (i_byte == size_byte)
1624 break;
1625
1626 leading_code = str[i_byte];
1627 c = STRING_CHAR_AND_LENGTH (str + i_byte,
1628 size_byte - i_byte, len);
1629 i_byte += len;
1630 }
1631 /* Treat `-' as range character only if another character
1632 follows. */
1633 if (i_byte + 1 < size_byte
1634 && str[i_byte] == '-')
1635 {
1636 unsigned int c2;
1637 unsigned char leading_code2;
1638
1639 /* Skip over the dash. */
1640 i_byte++;
1641
1642 /* Get the end of the range. */
1643 leading_code2 = str[i_byte];
1644 c2 = STRING_CHAR_AND_LENGTH (str + i_byte,
1645 size_byte - i_byte, len);
1646 i_byte += len;
1647
1648 if (c2 == '\\'
1649 && i_byte < size_byte)
1650 {
1651 leading_code2 = str[i_byte];
1652 c2 =STRING_CHAR_AND_LENGTH (str + i_byte, size_byte-i_byte, len);
1653 i_byte += len;
1654 }
1655
1656 if (c > c2)
1657 continue;
1658 if (ASCII_CHAR_P (c))
1659 {
1660 while (c <= c2 && c < 0x80)
1661 fastmap[c++] = 1;
1662 leading_code = CHAR_LEADING_CODE (c);
1663 }
1664 if (! ASCII_CHAR_P (c))
1665 {
1666 while (leading_code <= leading_code2)
1667 fastmap[leading_code++] = 1;
1668 if (c <= c2)
1669 {
1670 char_ranges[n_char_ranges++] = c;
1671 char_ranges[n_char_ranges++] = c2;
1672 }
1673 }
1674 }
1675 else
1676 {
1677 if (ASCII_CHAR_P (c))
1678 fastmap[c] = 1;
1679 else
1680 {
1681 fastmap[leading_code] = 1;
1682 char_ranges[n_char_ranges++] = c;
1683 char_ranges[n_char_ranges++] = c;
1684 }
1685 }
1686 }
1687
1688 /* If the current range is unibyte and STRING contains non-ASCII
1689 chars, arrange fastmap for the corresponding unibyte
1690 chars. */
1691
1692 if (! multibyte && n_char_ranges > 0)
1693 {
1694 bzero (fastmap + 0200, 0200);
1695 for (i = 0; i < n_char_ranges; i += 2)
1696 {
1697 int c1 = char_ranges[i];
1698 int c2 = char_ranges[i + 1];
1699
1700 for (; c1 <= c2; c1++)
1701 fastmap[CHAR_TO_BYTE8 (c1)] = 1;
1702 }
1703 }
1704 }
1705
1706 /* If ^ was the first character, complement the fastmap. */
1707 if (negate)
1708 {
1709 if (! multibyte)
1710 for (i = 0; i < sizeof fastmap; i++)
1711 fastmap[i] ^= 1;
1712 else
1713 {
1714 for (i = 0; i < 0200; i++)
1715 fastmap[i] ^= 1;
1716 /* All non-ASCII chars possibly match. */
1717 for (; i < sizeof fastmap; i++)
1718 fastmap[i] = 1;
1719 }
1720 }
1721
1722 {
1723 int start_point = PT;
1724 int pos = PT;
1725 int pos_byte = PT_BYTE;
1726 unsigned char *p = PT_ADDR, *endp, *stop;
1727
1728 if (forwardp)
1729 {
1730 endp = (XINT (lim) == GPT) ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
1731 stop = (pos < GPT && GPT < XINT (lim)) ? GPT_ADDR : endp;
1732 }
1733 else
1734 {
1735 endp = CHAR_POS_ADDR (XINT (lim));
1736 stop = (pos >= GPT && GPT > XINT (lim)) ? GAP_END_ADDR : endp;
1737 }
1738
1739 immediate_quit = 1;
1740 if (forwardp)
1741 {
1742 if (multibyte)
1743 while (1)
1744 {
1745 int nbytes;
1746
1747 if (p >= stop)
1748 {
1749 if (p >= endp)
1750 break;
1751 p = GAP_END_ADDR;
1752 stop = endp;
1753 }
1754 c = STRING_CHAR_AND_LENGTH (p, MAX_MULTIBYTE_LENGTH, nbytes);
1755 if (! NILP (iso_classes) && in_classes (c, iso_classes))
1756 {
1757 if (negate)
1758 break;
1759 else
1760 goto fwd_ok;
1761 }
1762
1763 if (! fastmap[*p])
1764 break;
1765 if (! ASCII_CHAR_P (c))
1766 {
1767 /* As we are looking at a multibyte character, we
1768 must look up the character in the table
1769 CHAR_RANGES. If there's no data in the table,
1770 that character is not what we want to skip. */
1771
1772 /* The following code do the right thing even if
1773 n_char_ranges is zero (i.e. no data in
1774 CHAR_RANGES). */
1775 for (i = 0; i < n_char_ranges; i += 2)
1776 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
1777 break;
1778 if (!(negate ^ (i < n_char_ranges)))
1779 break;
1780 }
1781 fwd_ok:
1782 p += nbytes, pos++, pos_byte += nbytes;
1783 }
1784 else
1785 while (1)
1786 {
1787 if (p >= stop)
1788 {
1789 if (p >= endp)
1790 break;
1791 p = GAP_END_ADDR;
1792 stop = endp;
1793 }
1794
1795 if (!NILP (iso_classes) && in_classes (*p, iso_classes))
1796 {
1797 if (negate)
1798 break;
1799 else
1800 goto fwd_unibyte_ok;
1801 }
1802
1803 if (!fastmap[*p])
1804 break;
1805 fwd_unibyte_ok:
1806 p++, pos++, pos_byte++;
1807 }
1808 }
1809 else
1810 {
1811 if (multibyte)
1812 while (1)
1813 {
1814 unsigned char *prev_p;
1815
1816 if (p <= stop)
1817 {
1818 if (p <= endp)
1819 break;
1820 p = GPT_ADDR;
1821 stop = endp;
1822 }
1823 prev_p = p;
1824 while (--p >= stop && ! CHAR_HEAD_P (*p));
1825 c = STRING_CHAR (p, MAX_MULTIBYTE_LENGTH);
1826
1827 if (! NILP (iso_classes) && in_classes (c, iso_classes))
1828 {
1829 if (negate)
1830 break;
1831 else
1832 goto back_ok;
1833 }
1834
1835 if (! fastmap[*p])
1836 break;
1837 if (! ASCII_CHAR_P (c))
1838 {
1839 /* See the comment in the previous similar code. */
1840 for (i = 0; i < n_char_ranges; i += 2)
1841 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
1842 break;
1843 if (!(negate ^ (i < n_char_ranges)))
1844 break;
1845 }
1846 back_ok:
1847 pos--, pos_byte -= prev_p - p;
1848 }
1849 else
1850 while (1)
1851 {
1852 if (p <= stop)
1853 {
1854 if (p <= endp)
1855 break;
1856 p = GPT_ADDR;
1857 stop = endp;
1858 }
1859
1860 if (! NILP (iso_classes) && in_classes (p[-1], iso_classes))
1861 {
1862 if (negate)
1863 break;
1864 else
1865 goto back_unibyte_ok;
1866 }
1867
1868 if (!fastmap[p[-1]])
1869 break;
1870 back_unibyte_ok:
1871 p--, pos--, pos_byte--;
1872 }
1873 }
1874
1875 SET_PT_BOTH (pos, pos_byte);
1876 immediate_quit = 0;
1877
1878 return make_number (PT - start_point);
1879 }
1880 }
1881
1882
1883 static Lisp_Object
1884 skip_syntaxes (forwardp, string, lim)
1885 int forwardp;
1886 Lisp_Object string, lim;
1887 {
1888 register unsigned int c;
1889 unsigned char fastmap[0400];
1890 int negate = 0;
1891 register int i, i_byte;
1892 int multibyte;
1893 int size_byte;
1894 unsigned char *str;
1895
1896 CHECK_STRING (string);
1897
1898 if (NILP (lim))
1899 XSETINT (lim, forwardp ? ZV : BEGV);
1900 else
1901 CHECK_NUMBER_COERCE_MARKER (lim);
1902
1903 /* In any case, don't allow scan outside bounds of buffer. */
1904 if (XINT (lim) > ZV)
1905 XSETFASTINT (lim, ZV);
1906 if (XINT (lim) < BEGV)
1907 XSETFASTINT (lim, BEGV);
1908
1909 if (forwardp ? (PT >= XFASTINT (lim)) : (PT <= XFASTINT (lim)))
1910 return Qnil;
1911
1912 multibyte = (!NILP (current_buffer->enable_multibyte_characters)
1913 && (XINT (lim) - PT != CHAR_TO_BYTE (XINT (lim)) - PT_BYTE));
1914
1915 bzero (fastmap, sizeof fastmap);
1916
1917 if (SBYTES (string) > SCHARS (string))
1918 /* As this is very rare case (syntax spec is ASCII only), don't
1919 consider efficiency. */
1920 string = string_make_unibyte (string);
1921
1922 str = SDATA (string);
1923 size_byte = SBYTES (string);
1924
1925 i_byte = 0;
1926 if (i_byte < size_byte
1927 && SREF (string, 0) == '^')
1928 {
1929 negate = 1; i_byte++;
1930 }
1931
1932 /* Find the syntaxes specified and set their elements of fastmap. */
1933
1934 while (i_byte < size_byte)
1935 {
1936 c = str[i_byte++];
1937 fastmap[syntax_spec_code[c]] = 1;
1938 }
1939
1940 /* If ^ was the first character, complement the fastmap. */
1941 if (negate)
1942 for (i = 0; i < sizeof fastmap; i++)
1943 fastmap[i] ^= 1;
1944
1945 {
1946 int start_point = PT;
1947 int pos = PT;
1948 int pos_byte = PT_BYTE;
1949 unsigned char *p = PT_ADDR, *endp, *stop;
1950
1951 if (forwardp)
1952 {
1953 endp = (XINT (lim) == GPT) ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
1954 stop = (pos < GPT && GPT < XINT (lim)) ? GPT_ADDR : endp;
1955 }
1956 else
1957 {
1958 endp = CHAR_POS_ADDR (XINT (lim));
1959 stop = (pos >= GPT && GPT > XINT (lim)) ? GAP_END_ADDR : endp;
1960 }
1961
1962 immediate_quit = 1;
1963 SETUP_SYNTAX_TABLE (pos, forwardp ? 1 : -1);
1964 if (forwardp)
1965 {
1966 if (multibyte)
1967 {
1968 while (1)
1969 {
1970 int nbytes;
1971
1972 if (p >= stop)
1973 {
1974 if (p >= endp)
1975 break;
1976 p = GAP_END_ADDR;
1977 stop = endp;
1978 }
1979 c = STRING_CHAR_AND_LENGTH (p, MAX_MULTIBYTE_LENGTH, nbytes);
1980 if (! fastmap[(int) SYNTAX (c)])
1981 break;
1982 p += nbytes, pos++, pos_byte += nbytes;
1983 UPDATE_SYNTAX_TABLE_FORWARD (pos);
1984 }
1985 }
1986 else
1987 {
1988 while (1)
1989 {
1990 if (p >= stop)
1991 {
1992 if (p >= endp)
1993 break;
1994 p = GAP_END_ADDR;
1995 stop = endp;
1996 }
1997 if (! fastmap[(int) SYNTAX (*p)])
1998 break;
1999 p++, pos++, pos_byte++;
2000 UPDATE_SYNTAX_TABLE_FORWARD (pos);
2001 }
2002 }
2003 }
2004 else
2005 {
2006 if (multibyte)
2007 {
2008 while (1)
2009 {
2010 unsigned char *prev_p;
2011
2012 if (p <= stop)
2013 {
2014 if (p <= endp)
2015 break;
2016 p = GPT_ADDR;
2017 stop = endp;
2018 }
2019 prev_p = p;
2020 while (--p >= stop && ! CHAR_HEAD_P (*p));
2021 c = STRING_CHAR (p, MAX_MULTIBYTE_LENGTH);
2022 if (! fastmap[(int) SYNTAX (c)])
2023 break;
2024 pos--, pos_byte -= prev_p - p;
2025 UPDATE_SYNTAX_TABLE_BACKWARD (pos);
2026 }
2027 }
2028 else
2029 {
2030 while (1)
2031 {
2032 if (p <= stop)
2033 {
2034 if (p <= endp)
2035 break;
2036 p = GPT_ADDR;
2037 stop = endp;
2038 }
2039 if (! fastmap[(int) SYNTAX (p[-1])])
2040 break;
2041 p--, pos--, pos_byte--;
2042 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
2043 }
2044 }
2045 }
2046
2047 SET_PT_BOTH (pos, pos_byte);
2048 immediate_quit = 0;
2049
2050 return make_number (PT - start_point);
2051 }
2052 }
2053
2054 /* Return 1 if character C belongs to one of the ISO classes
2055 in the list ISO_CLASSES. Each class is represented by an
2056 integer which is its type according to re_wctype. */
2057
2058 static int
2059 in_classes (c, iso_classes)
2060 int c;
2061 Lisp_Object iso_classes;
2062 {
2063 int fits_class = 0;
2064
2065 while (! NILP (iso_classes))
2066 {
2067 Lisp_Object elt;
2068 elt = XCAR (iso_classes);
2069 iso_classes = XCDR (iso_classes);
2070
2071 if (re_iswctype (c, XFASTINT (elt)))
2072 fits_class = 1;
2073 }
2074
2075 return fits_class;
2076 }
2077 \f
2078 /* Jump over a comment, assuming we are at the beginning of one.
2079 FROM is the current position.
2080 FROM_BYTE is the bytepos corresponding to FROM.
2081 Do not move past STOP (a charpos).
2082 The comment over which we have to jump is of style STYLE
2083 (either SYNTAX_COMMENT_STYLE(foo) or ST_COMMENT_STYLE).
2084 NESTING should be positive to indicate the nesting at the beginning
2085 for nested comments and should be zero or negative else.
2086 ST_COMMENT_STYLE cannot be nested.
2087 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
2088 (or 0 If the search cannot start in the middle of a two-character).
2089
2090 If successful, return 1 and store the charpos of the comment's end
2091 into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR.
2092 Else, return 0 and store the charpos STOP into *CHARPOS_PTR, the
2093 corresponding bytepos into *BYTEPOS_PTR and the current nesting
2094 (as defined for state.incomment) in *INCOMMENT_PTR.
2095
2096 The comment end is the last character of the comment rather than the
2097 character just after the comment.
2098
2099 Global syntax data is assumed to initially be valid for FROM and
2100 remains valid for forward search starting at the returned position. */
2101
2102 static int
2103 forw_comment (from, from_byte, stop, nesting, style, prev_syntax,
2104 charpos_ptr, bytepos_ptr, incomment_ptr)
2105 int from, from_byte, stop;
2106 int nesting, style, prev_syntax;
2107 int *charpos_ptr, *bytepos_ptr, *incomment_ptr;
2108 {
2109 register int c, c1;
2110 register enum syntaxcode code;
2111 register int syntax;
2112
2113 if (nesting <= 0) nesting = -1;
2114
2115 /* Enter the loop in the middle so that we find
2116 a 2-char comment ender if we start in the middle of it. */
2117 syntax = prev_syntax;
2118 if (syntax != 0) goto forw_incomment;
2119
2120 while (1)
2121 {
2122 if (from == stop)
2123 {
2124 *incomment_ptr = nesting;
2125 *charpos_ptr = from;
2126 *bytepos_ptr = from_byte;
2127 return 0;
2128 }
2129 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2130 syntax = SYNTAX_WITH_FLAGS (c);
2131 code = syntax & 0xff;
2132 if (code == Sendcomment
2133 && SYNTAX_FLAGS_COMMENT_STYLE (syntax) == style
2134 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ?
2135 (nesting > 0 && --nesting == 0) : nesting < 0))
2136 /* we have encountered a comment end of the same style
2137 as the comment sequence which began this comment
2138 section */
2139 break;
2140 if (code == Scomment_fence
2141 && style == ST_COMMENT_STYLE)
2142 /* we have encountered a comment end of the same style
2143 as the comment sequence which began this comment
2144 section. */
2145 break;
2146 if (nesting > 0
2147 && code == Scomment
2148 && SYNTAX_FLAGS_COMMENT_NESTED (syntax)
2149 && SYNTAX_FLAGS_COMMENT_STYLE (syntax) == style)
2150 /* we have encountered a nested comment of the same style
2151 as the comment sequence which began this comment section */
2152 nesting++;
2153 INC_BOTH (from, from_byte);
2154 UPDATE_SYNTAX_TABLE_FORWARD (from);
2155
2156 forw_incomment:
2157 if (from < stop && SYNTAX_FLAGS_COMEND_FIRST (syntax)
2158 && SYNTAX_FLAGS_COMMENT_STYLE (syntax) == style
2159 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2160 SYNTAX_COMEND_SECOND (c1))
2161 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
2162 SYNTAX_COMMENT_NESTED (c1)) ? nesting > 0 : nesting < 0))
2163 {
2164 if (--nesting <= 0)
2165 /* we have encountered a comment end of the same style
2166 as the comment sequence which began this comment
2167 section */
2168 break;
2169 else
2170 {
2171 INC_BOTH (from, from_byte);
2172 UPDATE_SYNTAX_TABLE_FORWARD (from);
2173 }
2174 }
2175 if (nesting > 0
2176 && from < stop
2177 && SYNTAX_FLAGS_COMSTART_FIRST (syntax)
2178 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2179 SYNTAX_COMMENT_STYLE (c1) == style
2180 && SYNTAX_COMSTART_SECOND (c1))
2181 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
2182 SYNTAX_COMMENT_NESTED (c1)))
2183 /* we have encountered a nested comment of the same style
2184 as the comment sequence which began this comment
2185 section */
2186 {
2187 INC_BOTH (from, from_byte);
2188 UPDATE_SYNTAX_TABLE_FORWARD (from);
2189 nesting++;
2190 }
2191 }
2192 *charpos_ptr = from;
2193 *bytepos_ptr = from_byte;
2194 return 1;
2195 }
2196
2197 DEFUN ("forward-comment", Fforward_comment, Sforward_comment, 1, 1, 0,
2198 doc: /*
2199 Move forward across up to COUNT comments. If COUNT is negative, move backward.
2200 Stop scanning if we find something other than a comment or whitespace.
2201 Set point to where scanning stops.
2202 If COUNT comments are found as expected, with nothing except whitespace
2203 between them, return t; otherwise return nil. */)
2204 (count)
2205 Lisp_Object count;
2206 {
2207 register int from;
2208 int from_byte;
2209 register int stop;
2210 register int c, c1;
2211 register enum syntaxcode code;
2212 int comstyle = 0; /* style of comment encountered */
2213 int comnested = 0; /* whether the comment is nestable or not */
2214 int found;
2215 int count1;
2216 int out_charpos, out_bytepos;
2217 int dummy;
2218
2219 CHECK_NUMBER (count);
2220 count1 = XINT (count);
2221 stop = count1 > 0 ? ZV : BEGV;
2222
2223 immediate_quit = 1;
2224 QUIT;
2225
2226 from = PT;
2227 from_byte = PT_BYTE;
2228
2229 SETUP_SYNTAX_TABLE (from, count1);
2230 while (count1 > 0)
2231 {
2232 do
2233 {
2234 int comstart_first;
2235
2236 if (from == stop)
2237 {
2238 SET_PT_BOTH (from, from_byte);
2239 immediate_quit = 0;
2240 return Qnil;
2241 }
2242 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2243 code = SYNTAX (c);
2244 comstart_first = SYNTAX_COMSTART_FIRST (c);
2245 comnested = SYNTAX_COMMENT_NESTED (c);
2246 comstyle = SYNTAX_COMMENT_STYLE (c);
2247 INC_BOTH (from, from_byte);
2248 UPDATE_SYNTAX_TABLE_FORWARD (from);
2249 if (from < stop && comstart_first
2250 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2251 SYNTAX_COMSTART_SECOND (c1)))
2252 {
2253 /* We have encountered a comment start sequence and we
2254 are ignoring all text inside comments. We must record
2255 the comment style this sequence begins so that later,
2256 only a comment end of the same style actually ends
2257 the comment section. */
2258 code = Scomment;
2259 comstyle = SYNTAX_COMMENT_STYLE (c1);
2260 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
2261 INC_BOTH (from, from_byte);
2262 UPDATE_SYNTAX_TABLE_FORWARD (from);
2263 }
2264 }
2265 while (code == Swhitespace || (code == Sendcomment && c == '\n'));
2266
2267 if (code == Scomment_fence)
2268 comstyle = ST_COMMENT_STYLE;
2269 else if (code != Scomment)
2270 {
2271 immediate_quit = 0;
2272 DEC_BOTH (from, from_byte);
2273 SET_PT_BOTH (from, from_byte);
2274 return Qnil;
2275 }
2276 /* We're at the start of a comment. */
2277 found = forw_comment (from, from_byte, stop, comnested, comstyle, 0,
2278 &out_charpos, &out_bytepos, &dummy);
2279 from = out_charpos; from_byte = out_bytepos;
2280 if (!found)
2281 {
2282 immediate_quit = 0;
2283 SET_PT_BOTH (from, from_byte);
2284 return Qnil;
2285 }
2286 INC_BOTH (from, from_byte);
2287 UPDATE_SYNTAX_TABLE_FORWARD (from);
2288 /* We have skipped one comment. */
2289 count1--;
2290 }
2291
2292 while (count1 < 0)
2293 {
2294 while (1)
2295 {
2296 int quoted;
2297
2298 if (from <= stop)
2299 {
2300 SET_PT_BOTH (BEGV, BEGV_BYTE);
2301 immediate_quit = 0;
2302 return Qnil;
2303 }
2304
2305 DEC_BOTH (from, from_byte);
2306 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
2307 quoted = char_quoted (from, from_byte);
2308 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2309 code = SYNTAX (c);
2310 comstyle = 0;
2311 comnested = SYNTAX_COMMENT_NESTED (c);
2312 if (code == Sendcomment)
2313 comstyle = SYNTAX_COMMENT_STYLE (c);
2314 if (from > stop && SYNTAX_COMEND_SECOND (c)
2315 && prev_char_comend_first (from, from_byte)
2316 && !char_quoted (from - 1, dec_bytepos (from_byte)))
2317 {
2318 /* We must record the comment style encountered so that
2319 later, we can match only the proper comment begin
2320 sequence of the same style. */
2321 DEC_BOTH (from, from_byte);
2322 code = Sendcomment;
2323 /* Calling char_quoted, above, set up global syntax position
2324 at the new value of FROM. */
2325 c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2326 comstyle = SYNTAX_COMMENT_STYLE (c1);
2327 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
2328 }
2329
2330 if (code == Scomment_fence)
2331 {
2332 /* Skip until first preceding unquoted comment_fence. */
2333 int found = 0, ini = from, ini_byte = from_byte;
2334
2335 while (1)
2336 {
2337 DEC_BOTH (from, from_byte);
2338 if (from == stop)
2339 break;
2340 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2341 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2342 if (SYNTAX (c) == Scomment_fence
2343 && !char_quoted (from, from_byte))
2344 {
2345 found = 1;
2346 break;
2347 }
2348 }
2349 if (found == 0)
2350 {
2351 from = ini; /* Set point to ini + 1. */
2352 from_byte = ini_byte;
2353 goto leave;
2354 }
2355 }
2356 else if (code == Sendcomment)
2357 {
2358 found = back_comment (from, from_byte, stop, comnested, comstyle,
2359 &out_charpos, &out_bytepos);
2360 if (found == -1)
2361 {
2362 if (c == '\n')
2363 /* This end-of-line is not an end-of-comment.
2364 Treat it like a whitespace.
2365 CC-mode (and maybe others) relies on this behavior. */
2366 ;
2367 else
2368 {
2369 /* Failure: we should go back to the end of this
2370 not-quite-endcomment. */
2371 if (SYNTAX(c) != code)
2372 /* It was a two-char Sendcomment. */
2373 INC_BOTH (from, from_byte);
2374 goto leave;
2375 }
2376 }
2377 else
2378 {
2379 /* We have skipped one comment. */
2380 from = out_charpos, from_byte = out_bytepos;
2381 break;
2382 }
2383 }
2384 else if (code != Swhitespace || quoted)
2385 {
2386 leave:
2387 immediate_quit = 0;
2388 INC_BOTH (from, from_byte);
2389 SET_PT_BOTH (from, from_byte);
2390 return Qnil;
2391 }
2392 }
2393
2394 count1++;
2395 }
2396
2397 SET_PT_BOTH (from, from_byte);
2398 immediate_quit = 0;
2399 return Qt;
2400 }
2401 \f
2402 /* Return syntax code of character C if C is an ASCII character
2403 or `multibyte_symbol_p' is zero. Otherwise, return Ssymbol. */
2404
2405 #define SYNTAX_WITH_MULTIBYTE_CHECK(c) \
2406 ((ASCII_CHAR_P (c) || !multibyte_symbol_p) \
2407 ? SYNTAX (c) : Ssymbol)
2408
2409 static Lisp_Object
2410 scan_lists (from, count, depth, sexpflag)
2411 register int from;
2412 int count, depth, sexpflag;
2413 {
2414 Lisp_Object val;
2415 register int stop = count > 0 ? ZV : BEGV;
2416 register int c, c1;
2417 int stringterm;
2418 int quoted;
2419 int mathexit = 0;
2420 register enum syntaxcode code, temp_code;
2421 int min_depth = depth; /* Err out if depth gets less than this. */
2422 int comstyle = 0; /* style of comment encountered */
2423 int comnested = 0; /* whether the comment is nestable or not */
2424 int temp_pos;
2425 int last_good = from;
2426 int found;
2427 int from_byte;
2428 int out_bytepos, out_charpos;
2429 int temp, dummy;
2430 int multibyte_symbol_p = sexpflag && multibyte_syntax_as_symbol;
2431
2432 if (depth > 0) min_depth = 0;
2433
2434 if (from > ZV) from = ZV;
2435 if (from < BEGV) from = BEGV;
2436
2437 from_byte = CHAR_TO_BYTE (from);
2438
2439 immediate_quit = 1;
2440 QUIT;
2441
2442 SETUP_SYNTAX_TABLE (from, count);
2443 while (count > 0)
2444 {
2445 while (from < stop)
2446 {
2447 int comstart_first, prefix;
2448 UPDATE_SYNTAX_TABLE_FORWARD (from);
2449 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2450 code = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2451 comstart_first = SYNTAX_COMSTART_FIRST (c);
2452 comnested = SYNTAX_COMMENT_NESTED (c);
2453 comstyle = SYNTAX_COMMENT_STYLE (c);
2454 prefix = SYNTAX_PREFIX (c);
2455 if (depth == min_depth)
2456 last_good = from;
2457 INC_BOTH (from, from_byte);
2458 UPDATE_SYNTAX_TABLE_FORWARD (from);
2459 if (from < stop && comstart_first
2460 && (c = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2461 SYNTAX_COMSTART_SECOND (c))
2462 && parse_sexp_ignore_comments)
2463 {
2464 /* we have encountered a comment start sequence and we
2465 are ignoring all text inside comments. We must record
2466 the comment style this sequence begins so that later,
2467 only a comment end of the same style actually ends
2468 the comment section */
2469 code = Scomment;
2470 c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2471 comstyle = SYNTAX_COMMENT_STYLE (c1);
2472 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
2473 INC_BOTH (from, from_byte);
2474 UPDATE_SYNTAX_TABLE_FORWARD (from);
2475 }
2476
2477 if (prefix)
2478 continue;
2479
2480 switch (SWITCH_ENUM_CAST (code))
2481 {
2482 case Sescape:
2483 case Scharquote:
2484 if (from == stop) goto lose;
2485 INC_BOTH (from, from_byte);
2486 /* treat following character as a word constituent */
2487 case Sword:
2488 case Ssymbol:
2489 if (depth || !sexpflag) break;
2490 /* This word counts as a sexp; return at end of it. */
2491 while (from < stop)
2492 {
2493 UPDATE_SYNTAX_TABLE_FORWARD (from);
2494
2495 /* Some compilers can't handle this inside the switch. */
2496 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2497 temp = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2498 switch (temp)
2499 {
2500 case Scharquote:
2501 case Sescape:
2502 INC_BOTH (from, from_byte);
2503 if (from == stop) goto lose;
2504 break;
2505 case Sword:
2506 case Ssymbol:
2507 case Squote:
2508 break;
2509 default:
2510 goto done;
2511 }
2512 INC_BOTH (from, from_byte);
2513 }
2514 goto done;
2515
2516 case Scomment_fence:
2517 comstyle = ST_COMMENT_STYLE;
2518 /* FALLTHROUGH */
2519 case Scomment:
2520 if (!parse_sexp_ignore_comments) break;
2521 UPDATE_SYNTAX_TABLE_FORWARD (from);
2522 found = forw_comment (from, from_byte, stop,
2523 comnested, comstyle, 0,
2524 &out_charpos, &out_bytepos, &dummy);
2525 from = out_charpos, from_byte = out_bytepos;
2526 if (!found)
2527 {
2528 if (depth == 0)
2529 goto done;
2530 goto lose;
2531 }
2532 INC_BOTH (from, from_byte);
2533 UPDATE_SYNTAX_TABLE_FORWARD (from);
2534 break;
2535
2536 case Smath:
2537 if (!sexpflag)
2538 break;
2539 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (from_byte))
2540 {
2541 INC_BOTH (from, from_byte);
2542 }
2543 if (mathexit)
2544 {
2545 mathexit = 0;
2546 goto close1;
2547 }
2548 mathexit = 1;
2549
2550 case Sopen:
2551 if (!++depth) goto done;
2552 break;
2553
2554 case Sclose:
2555 close1:
2556 if (!--depth) goto done;
2557 if (depth < min_depth)
2558 Fsignal (Qscan_error,
2559 Fcons (build_string ("Containing expression ends prematurely"),
2560 Fcons (make_number (last_good),
2561 Fcons (make_number (from), Qnil))));
2562 break;
2563
2564 case Sstring:
2565 case Sstring_fence:
2566 temp_pos = dec_bytepos (from_byte);
2567 stringterm = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2568 while (1)
2569 {
2570 if (from >= stop) goto lose;
2571 UPDATE_SYNTAX_TABLE_FORWARD (from);
2572 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2573 if (code == Sstring
2574 ? (c == stringterm
2575 && SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring)
2576 : SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring_fence)
2577 break;
2578
2579 /* Some compilers can't handle this inside the switch. */
2580 temp = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2581 switch (temp)
2582 {
2583 case Scharquote:
2584 case Sescape:
2585 INC_BOTH (from, from_byte);
2586 }
2587 INC_BOTH (from, from_byte);
2588 }
2589 INC_BOTH (from, from_byte);
2590 if (!depth && sexpflag) goto done;
2591 break;
2592 default:
2593 /* Ignore whitespace, punctuation, quote, endcomment. */
2594 break;
2595 }
2596 }
2597
2598 /* Reached end of buffer. Error if within object, return nil if between */
2599 if (depth) goto lose;
2600
2601 immediate_quit = 0;
2602 return Qnil;
2603
2604 /* End of object reached */
2605 done:
2606 count--;
2607 }
2608
2609 while (count < 0)
2610 {
2611 while (from > stop)
2612 {
2613 DEC_BOTH (from, from_byte);
2614 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2615 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2616 code = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2617 if (depth == min_depth)
2618 last_good = from;
2619 comstyle = 0;
2620 comnested = SYNTAX_COMMENT_NESTED (c);
2621 if (code == Sendcomment)
2622 comstyle = SYNTAX_COMMENT_STYLE (c);
2623 if (from > stop && SYNTAX_COMEND_SECOND (c)
2624 && prev_char_comend_first (from, from_byte)
2625 && parse_sexp_ignore_comments)
2626 {
2627 /* We must record the comment style encountered so that
2628 later, we can match only the proper comment begin
2629 sequence of the same style. */
2630 DEC_BOTH (from, from_byte);
2631 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2632 code = Sendcomment;
2633 c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2634 comstyle = SYNTAX_COMMENT_STYLE (c1);
2635 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
2636 }
2637
2638 /* Quoting turns anything except a comment-ender
2639 into a word character. Note that this cannot be true
2640 if we decremented FROM in the if-statement above. */
2641 if (code != Sendcomment && char_quoted (from, from_byte))
2642 {
2643 DEC_BOTH (from, from_byte);
2644 code = Sword;
2645 }
2646 else if (SYNTAX_PREFIX (c))
2647 continue;
2648
2649 switch (SWITCH_ENUM_CAST (code))
2650 {
2651 case Sword:
2652 case Ssymbol:
2653 case Sescape:
2654 case Scharquote:
2655 if (depth || !sexpflag) break;
2656 /* This word counts as a sexp; count object finished
2657 after passing it. */
2658 while (from > stop)
2659 {
2660 temp_pos = from_byte;
2661 if (! NILP (current_buffer->enable_multibyte_characters))
2662 DEC_POS (temp_pos);
2663 else
2664 temp_pos--;
2665 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2666 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2667 temp_code = SYNTAX_WITH_MULTIBYTE_CHECK (c1);
2668 /* Don't allow comment-end to be quoted. */
2669 if (temp_code == Sendcomment)
2670 goto done2;
2671 quoted = char_quoted (from - 1, temp_pos);
2672 if (quoted)
2673 {
2674 DEC_BOTH (from, from_byte);
2675 temp_pos = dec_bytepos (temp_pos);
2676 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2677 }
2678 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2679 temp_code = SYNTAX_WITH_MULTIBYTE_CHECK (c1);
2680 if (! (quoted || temp_code == Sword
2681 || temp_code == Ssymbol
2682 || temp_code == Squote))
2683 goto done2;
2684 DEC_BOTH (from, from_byte);
2685 }
2686 goto done2;
2687
2688 case Smath:
2689 if (!sexpflag)
2690 break;
2691 temp_pos = dec_bytepos (from_byte);
2692 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2693 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (temp_pos))
2694 DEC_BOTH (from, from_byte);
2695 if (mathexit)
2696 {
2697 mathexit = 0;
2698 goto open2;
2699 }
2700 mathexit = 1;
2701
2702 case Sclose:
2703 if (!++depth) goto done2;
2704 break;
2705
2706 case Sopen:
2707 open2:
2708 if (!--depth) goto done2;
2709 if (depth < min_depth)
2710 Fsignal (Qscan_error,
2711 Fcons (build_string ("Containing expression ends prematurely"),
2712 Fcons (make_number (last_good),
2713 Fcons (make_number (from), Qnil))));
2714 break;
2715
2716 case Sendcomment:
2717 if (!parse_sexp_ignore_comments)
2718 break;
2719 found = back_comment (from, from_byte, stop, comnested, comstyle,
2720 &out_charpos, &out_bytepos);
2721 /* FIXME: if found == -1, then it really wasn't a comment-end.
2722 For single-char Sendcomment, we can't do much about it apart
2723 from skipping the char.
2724 For 2-char endcomments, we could try again, taking both
2725 chars as separate entities, but it's a lot of trouble
2726 for very little gain, so we don't bother either. -sm */
2727 if (found != -1)
2728 from = out_charpos, from_byte = out_bytepos;
2729 break;
2730
2731 case Scomment_fence:
2732 case Sstring_fence:
2733 while (1)
2734 {
2735 if (from == stop) goto lose;
2736 DEC_BOTH (from, from_byte);
2737 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2738 if (!char_quoted (from, from_byte)
2739 && (c = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2740 SYNTAX_WITH_MULTIBYTE_CHECK (c) == code))
2741 break;
2742 }
2743 if (code == Sstring_fence && !depth && sexpflag) goto done2;
2744 break;
2745
2746 case Sstring:
2747 stringterm = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2748 while (1)
2749 {
2750 if (from == stop) goto lose;
2751 DEC_BOTH (from, from_byte);
2752 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2753 if (!char_quoted (from, from_byte)
2754 && (stringterm
2755 == (c = FETCH_CHAR_AS_MULTIBYTE (from_byte)))
2756 && SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring)
2757 break;
2758 }
2759 if (!depth && sexpflag) goto done2;
2760 break;
2761 default:
2762 /* Ignore whitespace, punctuation, quote, endcomment. */
2763 break;
2764 }
2765 }
2766
2767 /* Reached start of buffer. Error if within object, return nil if between */
2768 if (depth) goto lose;
2769
2770 immediate_quit = 0;
2771 return Qnil;
2772
2773 done2:
2774 count++;
2775 }
2776
2777
2778 immediate_quit = 0;
2779 XSETFASTINT (val, from);
2780 return val;
2781
2782 lose:
2783 Fsignal (Qscan_error,
2784 Fcons (build_string ("Unbalanced parentheses"),
2785 Fcons (make_number (last_good),
2786 Fcons (make_number (from), Qnil))));
2787 abort ();
2788 /* NOTREACHED */
2789 }
2790
2791 DEFUN ("scan-lists", Fscan_lists, Sscan_lists, 3, 3, 0,
2792 doc: /* Scan from character number FROM by COUNT lists.
2793 Returns the character number of the position thus found.
2794
2795 If DEPTH is nonzero, paren depth begins counting from that value,
2796 only places where the depth in parentheses becomes zero
2797 are candidates for stopping; COUNT such places are counted.
2798 Thus, a positive value for DEPTH means go out levels.
2799
2800 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2801
2802 If the beginning or end of (the accessible part of) the buffer is reached
2803 and the depth is wrong, an error is signaled.
2804 If the depth is right but the count is not used up, nil is returned. */)
2805 (from, count, depth)
2806 Lisp_Object from, count, depth;
2807 {
2808 CHECK_NUMBER (from);
2809 CHECK_NUMBER (count);
2810 CHECK_NUMBER (depth);
2811
2812 return scan_lists (XINT (from), XINT (count), XINT (depth), 0);
2813 }
2814
2815 DEFUN ("scan-sexps", Fscan_sexps, Sscan_sexps, 2, 2, 0,
2816 doc: /* Scan from character number FROM by COUNT balanced expressions.
2817 If COUNT is negative, scan backwards.
2818 Returns the character number of the position thus found.
2819
2820 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2821
2822 If the beginning or end of (the accessible part of) the buffer is reached
2823 in the middle of a parenthetical grouping, an error is signaled.
2824 If the beginning or end is reached between groupings
2825 but before count is used up, nil is returned. */)
2826 (from, count)
2827 Lisp_Object from, count;
2828 {
2829 CHECK_NUMBER (from);
2830 CHECK_NUMBER (count);
2831
2832 return scan_lists (XINT (from), XINT (count), 0, 1);
2833 }
2834
2835 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars, Sbackward_prefix_chars,
2836 0, 0, 0,
2837 doc: /* Move point backward over any number of chars with prefix syntax.
2838 This includes chars with "quote" or "prefix" syntax (' or p). */)
2839 ()
2840 {
2841 int beg = BEGV;
2842 int opoint = PT;
2843 int opoint_byte = PT_BYTE;
2844 int pos = PT;
2845 int pos_byte = PT_BYTE;
2846 int c;
2847
2848 if (pos <= beg)
2849 {
2850 SET_PT_BOTH (opoint, opoint_byte);
2851
2852 return Qnil;
2853 }
2854
2855 SETUP_SYNTAX_TABLE (pos, -1);
2856
2857 DEC_BOTH (pos, pos_byte);
2858
2859 while (!char_quoted (pos, pos_byte)
2860 /* Previous statement updates syntax table. */
2861 && ((c = FETCH_CHAR_AS_MULTIBYTE (pos_byte), SYNTAX (c) == Squote)
2862 || SYNTAX_PREFIX (c)))
2863 {
2864 opoint = pos;
2865 opoint_byte = pos_byte;
2866
2867 if (pos + 1 > beg)
2868 DEC_BOTH (pos, pos_byte);
2869 }
2870
2871 SET_PT_BOTH (opoint, opoint_byte);
2872
2873 return Qnil;
2874 }
2875 \f
2876 /* Parse forward from FROM / FROM_BYTE to END,
2877 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
2878 and return a description of the state of the parse at END.
2879 If STOPBEFORE is nonzero, stop at the start of an atom.
2880 If COMMENTSTOP is 1, stop at the start of a comment.
2881 If COMMENTSTOP is -1, stop at the start or end of a comment,
2882 after the beginning of a string, or after the end of a string. */
2883
2884 static void
2885 scan_sexps_forward (stateptr, from, from_byte, end, targetdepth,
2886 stopbefore, oldstate, commentstop)
2887 struct lisp_parse_state *stateptr;
2888 register int from;
2889 int from_byte;
2890 int end, targetdepth, stopbefore;
2891 Lisp_Object oldstate;
2892 int commentstop;
2893 {
2894 struct lisp_parse_state state;
2895
2896 register enum syntaxcode code;
2897 int c1;
2898 int comnested;
2899 struct level { int last, prev; };
2900 struct level levelstart[100];
2901 register struct level *curlevel = levelstart;
2902 struct level *endlevel = levelstart + 100;
2903 register int depth; /* Paren depth of current scanning location.
2904 level - levelstart equals this except
2905 when the depth becomes negative. */
2906 int mindepth; /* Lowest DEPTH value seen. */
2907 int start_quoted = 0; /* Nonzero means starting after a char quote */
2908 Lisp_Object tem;
2909 int prev_from; /* Keep one character before FROM. */
2910 int prev_from_byte;
2911 int prev_from_syntax;
2912 int boundary_stop = commentstop == -1;
2913 int nofence;
2914 int found;
2915 int out_bytepos, out_charpos;
2916 int temp;
2917
2918 prev_from = from;
2919 prev_from_byte = from_byte;
2920 if (from != BEGV)
2921 DEC_BOTH (prev_from, prev_from_byte);
2922
2923 /* Use this macro instead of `from++'. */
2924 #define INC_FROM \
2925 do { prev_from = from; \
2926 prev_from_byte = from_byte; \
2927 temp = FETCH_CHAR_AS_MULTIBYTE (prev_from_byte); \
2928 prev_from_syntax = SYNTAX_WITH_FLAGS (temp); \
2929 INC_BOTH (from, from_byte); \
2930 if (from < end) \
2931 UPDATE_SYNTAX_TABLE_FORWARD (from); \
2932 } while (0)
2933
2934 immediate_quit = 1;
2935 QUIT;
2936
2937 if (NILP (oldstate))
2938 {
2939 depth = 0;
2940 state.instring = -1;
2941 state.incomment = 0;
2942 state.comstyle = 0; /* comment style a by default. */
2943 state.comstr_start = -1; /* no comment/string seen. */
2944 }
2945 else
2946 {
2947 tem = Fcar (oldstate);
2948 if (!NILP (tem))
2949 depth = XINT (tem);
2950 else
2951 depth = 0;
2952
2953 oldstate = Fcdr (oldstate);
2954 oldstate = Fcdr (oldstate);
2955 oldstate = Fcdr (oldstate);
2956 tem = Fcar (oldstate);
2957 /* Check whether we are inside string_fence-style string: */
2958 state.instring = (!NILP (tem)
2959 ? (INTEGERP (tem) ? XINT (tem) : ST_STRING_STYLE)
2960 : -1);
2961
2962 oldstate = Fcdr (oldstate);
2963 tem = Fcar (oldstate);
2964 state.incomment = (!NILP (tem)
2965 ? (INTEGERP (tem) ? XINT (tem) : -1)
2966 : 0);
2967
2968 oldstate = Fcdr (oldstate);
2969 tem = Fcar (oldstate);
2970 start_quoted = !NILP (tem);
2971
2972 /* if the eighth element of the list is nil, we are in comment
2973 style a. If it is non-nil, we are in comment style b */
2974 oldstate = Fcdr (oldstate);
2975 oldstate = Fcdr (oldstate);
2976 tem = Fcar (oldstate);
2977 state.comstyle = NILP (tem) ? 0 : (EQ (tem, Qsyntax_table)
2978 ? ST_COMMENT_STYLE : 1);
2979
2980 oldstate = Fcdr (oldstate);
2981 tem = Fcar (oldstate);
2982 state.comstr_start = NILP (tem) ? -1 : XINT (tem) ;
2983 oldstate = Fcdr (oldstate);
2984 tem = Fcar (oldstate);
2985 while (!NILP (tem)) /* >= second enclosing sexps. */
2986 {
2987 /* curlevel++->last ran into compiler bug on Apollo */
2988 curlevel->last = XINT (Fcar (tem));
2989 if (++curlevel == endlevel)
2990 curlevel--; /* error ("Nesting too deep for parser"); */
2991 curlevel->prev = -1;
2992 curlevel->last = -1;
2993 tem = Fcdr (tem);
2994 }
2995 }
2996 state.quoted = 0;
2997 mindepth = depth;
2998
2999 curlevel->prev = -1;
3000 curlevel->last = -1;
3001
3002 SETUP_SYNTAX_TABLE (prev_from, 1);
3003 temp = FETCH_CHAR (prev_from_byte);
3004 prev_from_syntax = SYNTAX_WITH_FLAGS (temp);
3005 UPDATE_SYNTAX_TABLE_FORWARD (from);
3006
3007 /* Enter the loop at a place appropriate for initial state. */
3008
3009 if (state.incomment)
3010 goto startincomment;
3011 if (state.instring >= 0)
3012 {
3013 nofence = state.instring != ST_STRING_STYLE;
3014 if (start_quoted)
3015 goto startquotedinstring;
3016 goto startinstring;
3017 }
3018 else if (start_quoted)
3019 goto startquoted;
3020
3021 while (from < end)
3022 {
3023 INC_FROM;
3024 code = prev_from_syntax & 0xff;
3025
3026 if (code == Scomment)
3027 {
3028 state.comstyle = SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax);
3029 state.incomment = (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax) ?
3030 1 : -1);
3031 state.comstr_start = prev_from;
3032 }
3033 else if (code == Scomment_fence)
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 = ST_COMMENT_STYLE;
3039 state.incomment = -1;
3040 state.comstr_start = prev_from;
3041 code = Scomment;
3042 }
3043 else if (from < end)
3044 if (SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax))
3045 if (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
3046 SYNTAX_COMSTART_SECOND (c1))
3047 /* Duplicate code to avoid a complex if-expression
3048 which causes trouble for the SGI compiler. */
3049 {
3050 /* Record the comment style we have entered so that only
3051 the comment-end sequence of the same style actually
3052 terminates the comment section. */
3053 state.comstyle = SYNTAX_COMMENT_STYLE (c1);
3054 comnested = SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax);
3055 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
3056 state.incomment = comnested ? 1 : -1;
3057 state.comstr_start = prev_from;
3058 INC_FROM;
3059 code = Scomment;
3060 }
3061
3062 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax))
3063 continue;
3064 switch (SWITCH_ENUM_CAST (code))
3065 {
3066 case Sescape:
3067 case Scharquote:
3068 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3069 curlevel->last = prev_from;
3070 startquoted:
3071 if (from == end) goto endquoted;
3072 INC_FROM;
3073 goto symstarted;
3074 /* treat following character as a word constituent */
3075 case Sword:
3076 case Ssymbol:
3077 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3078 curlevel->last = prev_from;
3079 symstarted:
3080 while (from < end)
3081 {
3082 /* Some compilers can't handle this inside the switch. */
3083 temp = FETCH_CHAR_AS_MULTIBYTE (from_byte);
3084 temp = SYNTAX (temp);
3085 switch (temp)
3086 {
3087 case Scharquote:
3088 case Sescape:
3089 INC_FROM;
3090 if (from == end) goto endquoted;
3091 break;
3092 case Sword:
3093 case Ssymbol:
3094 case Squote:
3095 break;
3096 default:
3097 goto symdone;
3098 }
3099 INC_FROM;
3100 }
3101 symdone:
3102 curlevel->prev = curlevel->last;
3103 break;
3104
3105 case Scomment_fence: /* Can't happen because it's handled above. */
3106 case Scomment:
3107 if (commentstop || boundary_stop) goto done;
3108 startincomment:
3109 /* The (from == BEGV) test was to enter the loop in the middle so
3110 that we find a 2-char comment ender even if we start in the
3111 middle of it. We don't want to do that if we're just at the
3112 beginning of the comment (think of (*) ... (*)). */
3113 found = forw_comment (from, from_byte, end,
3114 state.incomment, state.comstyle,
3115 (from == BEGV || from < state.comstr_start + 3)
3116 ? 0 : prev_from_syntax,
3117 &out_charpos, &out_bytepos, &state.incomment);
3118 from = out_charpos; from_byte = out_bytepos;
3119 /* Beware! prev_from and friends are invalid now.
3120 Luckily, the `done' doesn't use them and the INC_FROM
3121 sets them to a sane value without looking at them. */
3122 if (!found) goto done;
3123 INC_FROM;
3124 state.incomment = 0;
3125 state.comstyle = 0; /* reset the comment style */
3126 if (boundary_stop) goto done;
3127 break;
3128
3129 case Sopen:
3130 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3131 depth++;
3132 /* curlevel++->last ran into compiler bug on Apollo */
3133 curlevel->last = prev_from;
3134 if (++curlevel == endlevel)
3135 curlevel--; /* error ("Nesting too deep for parser"); */
3136 curlevel->prev = -1;
3137 curlevel->last = -1;
3138 if (targetdepth == depth) goto done;
3139 break;
3140
3141 case Sclose:
3142 depth--;
3143 if (depth < mindepth)
3144 mindepth = depth;
3145 if (curlevel != levelstart)
3146 curlevel--;
3147 curlevel->prev = curlevel->last;
3148 if (targetdepth == depth) goto done;
3149 break;
3150
3151 case Sstring:
3152 case Sstring_fence:
3153 state.comstr_start = from - 1;
3154 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3155 curlevel->last = prev_from;
3156 state.instring = (code == Sstring
3157 ? (FETCH_CHAR_AS_MULTIBYTE (prev_from_byte))
3158 : ST_STRING_STYLE);
3159 if (boundary_stop) goto done;
3160 startinstring:
3161 {
3162 nofence = state.instring != ST_STRING_STYLE;
3163
3164 while (1)
3165 {
3166 int c;
3167
3168 if (from >= end) goto done;
3169 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
3170 /* Some compilers can't handle this inside the switch. */
3171 temp = SYNTAX (c);
3172
3173 /* Check TEMP here so that if the char has
3174 a syntax-table property which says it is NOT
3175 a string character, it does not end the string. */
3176 if (nofence && c == state.instring && temp == Sstring)
3177 break;
3178
3179 switch (temp)
3180 {
3181 case Sstring_fence:
3182 if (!nofence) goto string_end;
3183 break;
3184 case Scharquote:
3185 case Sescape:
3186 INC_FROM;
3187 startquotedinstring:
3188 if (from >= end) goto endquoted;
3189 }
3190 INC_FROM;
3191 }
3192 }
3193 string_end:
3194 state.instring = -1;
3195 curlevel->prev = curlevel->last;
3196 INC_FROM;
3197 if (boundary_stop) goto done;
3198 break;
3199
3200 case Smath:
3201 /* FIXME: We should do something with it. */
3202 break;
3203 default:
3204 /* Ignore whitespace, punctuation, quote, endcomment. */
3205 break;
3206 }
3207 }
3208 goto done;
3209
3210 stop: /* Here if stopping before start of sexp. */
3211 from = prev_from; /* We have just fetched the char that starts it; */
3212 goto done; /* but return the position before it. */
3213
3214 endquoted:
3215 state.quoted = 1;
3216 done:
3217 state.depth = depth;
3218 state.mindepth = mindepth;
3219 state.thislevelstart = curlevel->prev;
3220 state.prevlevelstart
3221 = (curlevel == levelstart) ? -1 : (curlevel - 1)->last;
3222 state.location = from;
3223 state.levelstarts = Qnil;
3224 while (--curlevel >= levelstart)
3225 state.levelstarts = Fcons (make_number (curlevel->last),
3226 state.levelstarts);
3227 immediate_quit = 0;
3228
3229 *stateptr = state;
3230 }
3231
3232 DEFUN ("parse-partial-sexp", Fparse_partial_sexp, Sparse_partial_sexp, 2, 6, 0,
3233 doc: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
3234 Parsing stops at TO or when certain criteria are met;
3235 point is set to where parsing stops.
3236 If fifth arg OLDSTATE is omitted or nil,
3237 parsing assumes that FROM is the beginning of a function.
3238 Value is a list of ten elements describing final state of parsing:
3239 0. depth in parens.
3240 1. character address of start of innermost containing list; nil if none.
3241 2. character address of start of last complete sexp terminated.
3242 3. non-nil if inside a string.
3243 (it is the character that will terminate the string,
3244 or t if the string should be terminated by a generic string delimiter.)
3245 4. nil if outside a comment, t if inside a non-nestable comment,
3246 else an integer (the current comment nesting).
3247 5. t if following a quote character.
3248 6. the minimum paren-depth encountered during this scan.
3249 7. t if in a comment of style b; symbol `syntax-table' if the comment
3250 should be terminated by a generic comment delimiter.
3251 8. character address of start of comment or string; nil if not in one.
3252 9. Intermediate data for continuation of parsing (subject to change).
3253 If third arg TARGETDEPTH is non-nil, parsing stops if the depth
3254 in parentheses becomes equal to TARGETDEPTH.
3255 Fourth arg STOPBEFORE non-nil means stop when come to
3256 any character that starts a sexp.
3257 Fifth arg OLDSTATE is a nine-element list like what this function returns.
3258 It is used to initialize the state of the parse. Elements number 1, 2, 6
3259 and 8 are ignored; you can leave off element 8 (the last) entirely.
3260 Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.
3261 If it is symbol `syntax-table', stop after the start of a comment or a
3262 string, or after end of a comment or a string. */)
3263 (from, to, targetdepth, stopbefore, oldstate, commentstop)
3264 Lisp_Object from, to, targetdepth, stopbefore, oldstate, commentstop;
3265 {
3266 struct lisp_parse_state state;
3267 int target;
3268
3269 if (!NILP (targetdepth))
3270 {
3271 CHECK_NUMBER (targetdepth);
3272 target = XINT (targetdepth);
3273 }
3274 else
3275 target = -100000; /* We won't reach this depth */
3276
3277 validate_region (&from, &to);
3278 scan_sexps_forward (&state, XINT (from), CHAR_TO_BYTE (XINT (from)),
3279 XINT (to),
3280 target, !NILP (stopbefore), oldstate,
3281 (NILP (commentstop)
3282 ? 0 : (EQ (commentstop, Qsyntax_table) ? -1 : 1)));
3283
3284 SET_PT (state.location);
3285
3286 return Fcons (make_number (state.depth),
3287 Fcons (state.prevlevelstart < 0 ? Qnil : make_number (state.prevlevelstart),
3288 Fcons (state.thislevelstart < 0 ? Qnil : make_number (state.thislevelstart),
3289 Fcons (state.instring >= 0
3290 ? (state.instring == ST_STRING_STYLE
3291 ? Qt : make_number (state.instring)) : Qnil,
3292 Fcons (state.incomment < 0 ? Qt :
3293 (state.incomment == 0 ? Qnil :
3294 make_number (state.incomment)),
3295 Fcons (state.quoted ? Qt : Qnil,
3296 Fcons (make_number (state.mindepth),
3297 Fcons ((state.comstyle
3298 ? (state.comstyle == ST_COMMENT_STYLE
3299 ? Qsyntax_table : Qt) :
3300 Qnil),
3301 Fcons (((state.incomment
3302 || (state.instring >= 0))
3303 ? make_number (state.comstr_start)
3304 : Qnil),
3305 Fcons (state.levelstarts, Qnil))))))))));
3306 }
3307 \f
3308 void
3309 init_syntax_once ()
3310 {
3311 register int i, c;
3312 Lisp_Object temp;
3313
3314 /* This has to be done here, before we call Fmake_char_table. */
3315 Qsyntax_table = intern ("syntax-table");
3316 staticpro (&Qsyntax_table);
3317
3318 /* Intern this now in case it isn't already done.
3319 Setting this variable twice is harmless.
3320 But don't staticpro it here--that is done in alloc.c. */
3321 Qchar_table_extra_slots = intern ("char-table-extra-slots");
3322
3323 /* Create objects which can be shared among syntax tables. */
3324 Vsyntax_code_object = Fmake_vector (make_number (Smax), Qnil);
3325 for (i = 0; i < XVECTOR (Vsyntax_code_object)->size; i++)
3326 XVECTOR (Vsyntax_code_object)->contents[i]
3327 = Fcons (make_number (i), Qnil);
3328
3329 /* Now we are ready to set up this property, so we can
3330 create syntax tables. */
3331 Fput (Qsyntax_table, Qchar_table_extra_slots, make_number (0));
3332
3333 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Swhitespace];
3334
3335 Vstandard_syntax_table = Fmake_char_table (Qsyntax_table, temp);
3336
3337 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Sword];
3338 for (i = 'a'; i <= 'z'; i++)
3339 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3340 for (i = 'A'; i <= 'Z'; i++)
3341 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3342 for (i = '0'; i <= '9'; i++)
3343 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3344
3345 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '$', temp);
3346 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '%', temp);
3347
3348 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '(',
3349 Fcons (make_number (Sopen), make_number (')')));
3350 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ')',
3351 Fcons (make_number (Sclose), make_number ('(')));
3352 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '[',
3353 Fcons (make_number (Sopen), make_number (']')));
3354 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ']',
3355 Fcons (make_number (Sclose), make_number ('[')));
3356 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '{',
3357 Fcons (make_number (Sopen), make_number ('}')));
3358 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '}',
3359 Fcons (make_number (Sclose), make_number ('{')));
3360 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '"',
3361 Fcons (make_number ((int) Sstring), Qnil));
3362 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\\',
3363 Fcons (make_number ((int) Sescape), Qnil));
3364
3365 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Ssymbol];
3366 for (i = 0; i < 10; i++)
3367 {
3368 c = "_-+*/&|<>="[i];
3369 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3370 }
3371
3372 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Spunct];
3373 for (i = 0; i < 12; i++)
3374 {
3375 c = ".,;:?!#@~^'`"[i];
3376 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3377 }
3378
3379 /* All multibyte characters have syntax `word' by default. */
3380 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Sword];
3381 char_table_set_range (Vstandard_syntax_table, 0x80, MAX_CHAR, temp);
3382 }
3383
3384 void
3385 syms_of_syntax ()
3386 {
3387 Qsyntax_table_p = intern ("syntax-table-p");
3388 staticpro (&Qsyntax_table_p);
3389
3390 staticpro (&Vsyntax_code_object);
3391
3392 Qscan_error = intern ("scan-error");
3393 staticpro (&Qscan_error);
3394 Fput (Qscan_error, Qerror_conditions,
3395 Fcons (Qscan_error, Fcons (Qerror, Qnil)));
3396 Fput (Qscan_error, Qerror_message,
3397 build_string ("Scan error"));
3398
3399 DEFVAR_BOOL ("parse-sexp-ignore-comments", &parse_sexp_ignore_comments,
3400 doc: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
3401
3402 DEFVAR_BOOL ("parse-sexp-lookup-properties", &parse_sexp_lookup_properties,
3403 doc: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3404 Otherwise, that text property is simply ignored.
3405 See the info node `(elisp)Syntax Properties' for a description of the
3406 `syntax-table' property. */);
3407
3408 words_include_escapes = 0;
3409 DEFVAR_BOOL ("words-include-escapes", &words_include_escapes,
3410 doc: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
3411
3412 DEFVAR_BOOL ("multibyte-syntax-as-symbol", &multibyte_syntax_as_symbol,
3413 doc: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
3414 multibyte_syntax_as_symbol = 0;
3415
3416 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3417 &open_paren_in_column_0_is_defun_start,
3418 doc: /* *Non-nil means an open paren in column 0 denotes the start of a defun. */);
3419 open_paren_in_column_0_is_defun_start = 1;
3420
3421
3422 DEFVAR_LISP ("find-word-boundary-function-table",
3423 &Vfind_word_boundary_function_table,
3424 doc: /*
3425 Char table of functions to search for the word boundary.
3426 Each function is called with two arguments; POS and LIMIT.
3427 POS and LIMIT are character positions in the current buffer.
3428
3429 If POS is less than LIMIT, POS is at the first character of a word,
3430 and the return value of a function is a position after the last
3431 character of that word.
3432
3433 If POS is not less than LIMIT, POS is at the last character of a word,
3434 and the return value of a function is a position at the first
3435 character of that word.
3436
3437 In both cases, LIMIT bounds the search. */);
3438 Vfind_word_boundary_function_table = Fmake_char_table (Qnil, Qnil);
3439
3440 defsubr (&Ssyntax_table_p);
3441 defsubr (&Ssyntax_table);
3442 defsubr (&Sstandard_syntax_table);
3443 defsubr (&Scopy_syntax_table);
3444 defsubr (&Sset_syntax_table);
3445 defsubr (&Schar_syntax);
3446 defsubr (&Smatching_paren);
3447 defsubr (&Sstring_to_syntax);
3448 defsubr (&Smodify_syntax_entry);
3449 defsubr (&Sinternal_describe_syntax_value);
3450
3451 defsubr (&Sforward_word);
3452
3453 defsubr (&Sskip_chars_forward);
3454 defsubr (&Sskip_chars_backward);
3455 defsubr (&Sskip_syntax_forward);
3456 defsubr (&Sskip_syntax_backward);
3457
3458 defsubr (&Sforward_comment);
3459 defsubr (&Sscan_lists);
3460 defsubr (&Sscan_sexps);
3461 defsubr (&Sbackward_prefix_chars);
3462 defsubr (&Sparse_partial_sexp);
3463 }
3464
3465 /* arch-tag: 3e297b9f-088e-4b64-8f4c-fb0b3443e412
3466 (do not change this comment) */