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