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