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