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