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