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