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