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