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