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