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