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