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