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