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