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