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