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