1 /* GNU Emacs routines to deal with syntax tables; also word and list parsing.
2 Copyright (C) 1985, 1987, 1993, 1994, 1995 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
28 Lisp_Object Qsyntax_table_p
, Qsyntax_table
;
30 static void scan_sexps_forward ();
31 static int char_quoted ();
33 int words_include_escapes
;
35 /* Used as a temporary in SYNTAX_ENTRY and other macros in syntax.h,
36 if not compiled with GCC. No need to mark it, since it is used
37 only very temporarily. */
38 Lisp_Object syntax_temp
;
40 /* This is the internal form of the parse state used in parse-partial-sexp. */
42 struct lisp_parse_state
44 int depth
; /* Depth at end of parsing */
45 int instring
; /* -1 if not within string, else desired terminator. */
46 int incomment
; /* Nonzero if within a comment at end of parsing */
47 int comstyle
; /* comment style a=0, or b=1 */
48 int quoted
; /* Nonzero if just after an escape char at end of parsing */
49 int thislevelstart
; /* Char number of most recent start-of-expression at current level */
50 int prevlevelstart
; /* Char number of start of containing expression */
51 int location
; /* Char number at which parsing stopped. */
52 int mindepth
; /* Minimum depth seen while scanning. */
53 int comstart
; /* Position just after last comment starter. */
56 /* These variables are a cache for finding the start of a defun.
57 find_start_pos is the place for which the defun start was found.
58 find_start_value is the defun start position found for it.
59 find_start_buffer is the buffer it was found in.
60 find_start_begv is the BEGV value when it was found.
61 find_start_modiff is the value of MODIFF when it was found. */
63 static int find_start_pos
;
64 static int find_start_value
;
65 static struct buffer
*find_start_buffer
;
66 static int find_start_begv
;
67 static int find_start_modiff
;
69 /* Find a defun-start that is the last one before POS (or nearly the last).
70 We record what we find, so that another call in the same area
71 can return the same value right away. */
74 find_defun_start (pos
)
80 /* Use previous finding, if it's valid and applies to this inquiry. */
81 if (current_buffer
== find_start_buffer
82 /* Reuse the defun-start even if POS is a little farther on.
83 POS might be in the next defun, but that's ok.
84 Our value may not be the best possible, but will still be usable. */
85 && pos
<= find_start_pos
+ 1000
86 && pos
>= find_start_value
87 && BEGV
== find_start_begv
88 && MODIFF
== find_start_modiff
)
89 return find_start_value
;
91 /* Back up to start of line. */
92 tem
= scan_buffer ('\n', pos
, BEGV
, -1, &shortage
, 1);
96 /* Open-paren at start of line means we found our defun-start. */
97 if (SYNTAX (FETCH_CHAR (tem
)) == Sopen
)
99 /* Move to beg of previous line. */
100 tem
= scan_buffer ('\n', tem
, BEGV
, -2, &shortage
, 1);
103 /* Record what we found, for the next try. */
104 find_start_value
= tem
;
105 find_start_buffer
= current_buffer
;
106 find_start_modiff
= MODIFF
;
107 find_start_begv
= BEGV
;
108 find_start_pos
= pos
;
110 return find_start_value
;
113 DEFUN ("syntax-table-p", Fsyntax_table_p
, Ssyntax_table_p
, 1, 1, 0,
114 "Return t if ARG is a syntax table.\n\
115 Currently, any char-table counts as a syntax table.")
119 if (CHAR_TABLE_P (obj
)
120 && XCHAR_TABLE (obj
)->purpose
== Qsyntax_table
)
126 check_syntax_table (obj
)
129 if (!(CHAR_TABLE_P (obj
)
130 && XCHAR_TABLE (obj
)->purpose
== Qsyntax_table
))
131 wrong_type_argument (Qsyntax_table_p
, obj
);
134 DEFUN ("syntax-table", Fsyntax_table
, Ssyntax_table
, 0, 0, 0,
135 "Return the current syntax table.\n\
136 This is the one specified by the current buffer.")
139 return current_buffer
->syntax_table
;
142 DEFUN ("standard-syntax-table", Fstandard_syntax_table
,
143 Sstandard_syntax_table
, 0, 0, 0,
144 "Return the standard syntax table.\n\
145 This is the one used for new buffers.")
148 return Vstandard_syntax_table
;
151 DEFUN ("copy-syntax-table", Fcopy_syntax_table
, Scopy_syntax_table
, 0, 1, 0,
152 "Construct a new syntax table and return it.\n\
153 It is a copy of the TABLE, which defaults to the standard syntax table.")
160 check_syntax_table (table
);
162 table
= Vstandard_syntax_table
;
164 copy
= Fcopy_sequence (table
);
165 Fset_char_table_parent (copy
, Vstandard_syntax_table
);
169 DEFUN ("set-syntax-table", Fset_syntax_table
, Sset_syntax_table
, 1, 1, 0,
170 "Select a new syntax table for the current buffer.\n\
171 One argument, a syntax table.")
175 check_syntax_table (table
);
176 current_buffer
->syntax_table
= table
;
177 /* Indicate that this buffer now has a specified syntax table. */
178 current_buffer
->local_var_flags
179 |= XFASTINT (buffer_local_flags
.syntax_table
);
183 /* Convert a letter which signifies a syntax code
184 into the code it signifies.
185 This is used by modify-syntax-entry, and other things. */
187 unsigned char syntax_spec_code
[0400] =
188 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
189 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
190 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
191 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
192 (char) Swhitespace
, 0377, (char) Sstring
, 0377,
193 (char) Smath
, 0377, 0377, (char) Squote
,
194 (char) Sopen
, (char) Sclose
, 0377, 0377,
195 0377, (char) Swhitespace
, (char) Spunct
, (char) Scharquote
,
196 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
197 0377, 0377, 0377, 0377,
198 (char) Scomment
, 0377, (char) Sendcomment
, 0377,
199 (char) Sinherit
, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
200 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
201 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword
,
202 0377, 0377, 0377, 0377, (char) Sescape
, 0377, 0377, (char) Ssymbol
,
203 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
204 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
205 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword
,
206 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377
209 /* Indexed by syntax code, give the letter that describes it. */
211 char syntax_code_spec
[14] =
213 ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@'
216 /* Look up the value for CHARACTER in syntax table TABLE's parent
217 and its parents. SYNTAX_ENTRY calls this, when TABLE itself has nil
218 for CHARACTER. It's actually used only when not compiled with GCC. */
221 syntax_parent_lookup (table
, character
)
229 table
= XCHAR_TABLE (table
)->parent
;
233 value
= XCHAR_TABLE (table
)->contents
[character
];
239 DEFUN ("char-syntax", Fchar_syntax
, Schar_syntax
, 1, 1, 0,
240 "Return the syntax code of CHAR, described by a character.\n\
241 For example, if CHAR is a word constituent, the character `?w' is returned.\n\
242 The characters that correspond to various syntax codes\n\
243 are listed in the documentation of `modify-syntax-entry'.")
248 CHECK_NUMBER (ch
, 0);
249 char_int
= XINT (ch
);
250 return make_number (syntax_code_spec
[(int) SYNTAX (char_int
)]);
253 DEFUN ("matching-paren", Fmatching_paren
, Smatching_paren
, 1, 1, 0,
254 "Return the matching parenthesis of CHAR, or nil if none.")
259 CHECK_NUMBER (ch
, 0);
260 char_int
= XINT (ch
);
261 code
= SYNTAX (char_int
);
262 if (code
== Sopen
|| code
== Sclose
)
263 return make_number (SYNTAX_MATCH (char_int
));
267 /* This comment supplies the doc string for modify-syntax-entry,
268 for make-docfile to see. We cannot put this in the real DEFUN
269 due to limits in the Unix cpp.
271 DEFUN ("modify-syntax-entry", foo, bar, 2, 3, 0,
272 "Set syntax for character CHAR according to string S.\n\
273 The syntax is changed only for table TABLE, which defaults to\n\
274 the current buffer's syntax table.\n\
275 The first character of S should be one of the following:\n\
276 Space or - whitespace syntax. w word constituent.\n\
277 _ symbol constituent. . punctuation.\n\
278 ( open-parenthesis. ) close-parenthesis.\n\
279 \" string quote. \\ escape.\n\
280 $ paired delimiter. ' expression quote or prefix operator.\n\
281 < comment starter. > comment ender.\n\
282 / character-quote. @ inherit from `standard-syntax-table'.\n\
284 Only single-character comment start and end sequences are represented thus.\n\
285 Two-character sequences are represented as described below.\n\
286 The second character of S is the matching parenthesis,\n\
287 used only if the first character is `(' or `)'.\n\
288 Any additional characters are flags.\n\
289 Defined flags are the characters 1, 2, 3, 4, b, and p.\n\
290 1 means C is the start of a two-char comment start sequence.\n\
291 2 means C is the second character of such a sequence.\n\
292 3 means C is the start of a two-char comment end sequence.\n\
293 4 means C is the second character of such a sequence.\n\
295 There can be up to two orthogonal comment sequences. This is to support\n\
296 language modes such as C++. By default, all comment sequences are of style\n\
297 a, but you can set the comment sequence style to b (on the second character\n\
298 of a comment-start, or the first character of a comment-end sequence) using\n\
300 b means C is part of comment sequence b.\n\
302 p means C is a prefix character for `backward-prefix-chars';\n\
303 such characters are treated as whitespace when they occur\n\
304 between expressions.")
308 DEFUN ("modify-syntax-entry", Fmodify_syntax_entry
, Smodify_syntax_entry
, 2, 3,
309 /* I really don't know why this is interactive
310 help-form should at least be made useful whilst reading the second arg
312 "cSet syntax for character: \nsSet syntax for %s to: ",
313 0 /* See immediately above */)
314 (c
, newentry
, syntax_table
)
315 Lisp_Object c
, newentry
, syntax_table
;
317 register unsigned char *p
;
318 register enum syntaxcode code
;
323 CHECK_STRING (newentry
, 1);
325 if (NILP (syntax_table
))
326 syntax_table
= current_buffer
->syntax_table
;
328 check_syntax_table (syntax_table
);
330 p
= XSTRING (newentry
)->data
;
331 code
= (enum syntaxcode
) syntax_spec_code
[*p
++];
332 if (((int) code
& 0377) == 0377)
333 error ("invalid syntax description letter: %c", c
);
335 if (code
== Sinherit
)
337 SET_RAW_SYNTAX_ENTRY (syntax_table
, c
, Qnil
);
343 XSETINT (match
, *p
++);
344 if (XFASTINT (match
) == ' ')
379 SET_RAW_SYNTAX_ENTRY (syntax_table
, c
,
380 Fcons (make_number (val
), match
));
385 /* Dump syntax table to buffer in human-readable format */
388 describe_syntax (value
)
391 register enum syntaxcode code
;
392 char desc
, match
, start1
, start2
, end1
, end2
, prefix
, comstyle
;
394 Lisp_Object first
, match_lisp
;
396 Findent_to (make_number (16), make_number (1));
400 insert_string ("inherit");
406 insert_string ("invalid");
410 first
= XCONS (value
)->car
;
411 match_lisp
= XCONS (value
)->cdr
;
413 if (!INTEGERP (first
) || !(NILP (match_lisp
) || INTEGERP (match_lisp
)))
415 insert_string ("invalid");
419 code
= (enum syntaxcode
) (first
& 0377);
420 start1
= (XINT (first
) >> 16) & 1;
421 start2
= (XINT (first
) >> 17) & 1;
422 end1
= (XINT (first
) >> 18) & 1;
423 end2
= (XINT (first
) >> 19) & 1;
424 prefix
= (XINT (first
) >> 20) & 1;
425 comstyle
= (XINT (first
) >> 21) & 1;
427 if ((int) code
< 0 || (int) code
>= (int) Smax
)
429 insert_string ("invalid");
432 desc
= syntax_code_spec
[(int) code
];
434 str
[0] = desc
, str
[1] = 0;
437 str
[0] = !NILP (match_lisp
) ? XINT (match_lisp
) : ' ';
455 insert_string ("\twhich means: ");
457 switch (SWITCH_ENUM_CAST (code
))
460 insert_string ("whitespace"); break;
462 insert_string ("punctuation"); break;
464 insert_string ("word"); break;
466 insert_string ("symbol"); break;
468 insert_string ("open"); break;
470 insert_string ("close"); break;
472 insert_string ("quote"); break;
474 insert_string ("string"); break;
476 insert_string ("math"); break;
478 insert_string ("escape"); break;
480 insert_string ("charquote"); break;
482 insert_string ("comment"); break;
484 insert_string ("endcomment"); break;
486 insert_string ("invalid");
490 if (!NILP (match_lisp
))
492 insert_string (", matches ");
493 insert_char (XINT (match_lisp
));
497 insert_string (",\n\t is the first character of a comment-start sequence");
499 insert_string (",\n\t is the second character of a comment-start sequence");
502 insert_string (",\n\t is the first character of a comment-end sequence");
504 insert_string (",\n\t is the second character of a comment-end sequence");
506 insert_string (" (comment style b)");
509 insert_string (",\n\t is a prefix character for `backward-prefix-chars'");
511 insert_string ("\n");
515 describe_syntax_1 (vector
)
518 struct buffer
*old
= current_buffer
;
519 set_buffer_internal (XBUFFER (Vstandard_output
));
520 describe_vector (vector
, Qnil
, describe_syntax
, 0, Qnil
, Qnil
);
521 call0 (intern ("help-mode"));
522 set_buffer_internal (old
);
526 DEFUN ("describe-syntax", Fdescribe_syntax
, Sdescribe_syntax
, 0, 0, "",
527 "Describe the syntax specifications in the syntax table.\n\
528 The descriptions are inserted in a buffer, which is then displayed.")
531 internal_with_output_to_temp_buffer
532 ("*Help*", describe_syntax_1
, current_buffer
->syntax_table
);
537 /* Return the position across COUNT words from FROM.
538 If that many words cannot be found before the end of the buffer, return 0.
539 COUNT negative means scan backward and stop at word beginning. */
541 scan_words (from
, count
)
542 register int from
, count
;
544 register int beg
= BEGV
;
545 register int end
= ZV
;
561 charcode
= FETCH_CHAR (from
);
562 code
= SYNTAX (charcode
);
563 if (words_include_escapes
564 && (code
== Sescape
|| code
== Scharquote
))
572 if (from
== end
) break;
573 charcode
= FETCH_CHAR (from
);
574 code
= SYNTAX (charcode
);
575 if (!(words_include_escapes
576 && (code
== Sescape
|| code
== Scharquote
)))
592 charcode
= FETCH_CHAR (from
- 1);
593 code
= SYNTAX (charcode
);
594 if (words_include_escapes
595 && (code
== Sescape
|| code
== Scharquote
))
603 if (from
== beg
) break;
604 charcode
= FETCH_CHAR (from
- 1);
605 code
= SYNTAX (charcode
);
606 if (!(words_include_escapes
607 && (code
== Sescape
|| code
== Scharquote
)))
620 DEFUN ("forward-word", Fforward_word
, Sforward_word
, 1, 1, "p",
621 "Move point forward ARG words (backward if ARG is negative).\n\
622 Normally returns t.\n\
623 If an edge of the buffer is reached, point is left there\n\
624 and nil is returned.")
629 CHECK_NUMBER (count
, 0);
631 if (!(val
= scan_words (point
, XINT (count
))))
633 SET_PT (XINT (count
) > 0 ? ZV
: BEGV
);
640 DEFUN ("forward-comment", Fforward_comment
, Sforward_comment
, 1, 1, 0,
641 "Move forward across up to N comments. If N is negative, move backward.\n\
642 Stop scanning if we find something other than a comment or whitespace.\n\
643 Set point to where scanning stops.\n\
644 If N comments are found as expected, with nothing except whitespace\n\
645 between them, return t; otherwise return nil.")
652 register enum syntaxcode code
;
653 int comstyle
= 0; /* style of comment encountered */
657 CHECK_NUMBER (count
, 0);
658 count1
= XINT (count
);
676 c
= FETCH_CHAR (from
);
680 if (from
< stop
&& SYNTAX_COMSTART_FIRST (c
)
681 && (c1
= FETCH_CHAR (from
),
682 SYNTAX_COMSTART_SECOND (c1
)))
684 /* We have encountered a comment start sequence and we
685 are ignoring all text inside comments. We must record
686 the comment style this sequence begins so that later,
687 only a comment end of the same style actually ends
688 the comment section. */
690 comstyle
= SYNTAX_COMMENT_STYLE (c1
);
694 while (code
== Swhitespace
|| code
== Sendcomment
);
695 if (code
!= Scomment
)
701 /* We're at the start of a comment. */
710 c
= FETCH_CHAR (from
);
712 if (SYNTAX (c
) == Sendcomment
713 && SYNTAX_COMMENT_STYLE (c
) == comstyle
)
714 /* we have encountered a comment end of the same style
715 as the comment sequence which began this comment
718 if (from
< stop
&& SYNTAX_COMEND_FIRST (c
)
719 && (c1
= FETCH_CHAR (from
),
720 SYNTAX_COMEND_SECOND (c1
))
721 && SYNTAX_COMMENT_STYLE (c
) == comstyle
)
722 /* we have encountered a comment end of the same style
723 as the comment sequence which began this comment
727 /* We have skipped one comment. */
739 quoted
= char_quoted (from
);
742 c
= FETCH_CHAR (from
);
745 if (code
== Sendcomment
)
746 comstyle
= SYNTAX_COMMENT_STYLE (c
);
747 if (from
> stop
&& SYNTAX_COMEND_SECOND (c
)
748 && (c1
= FETCH_CHAR (from
- 1),
749 SYNTAX_COMEND_FIRST (c1
))
750 && !char_quoted (from
- 1))
752 /* We must record the comment style encountered so that
753 later, we can match only the proper comment begin
754 sequence of the same style. */
756 comstyle
= SYNTAX_COMMENT_STYLE (c1
);
760 if (code
== Sendcomment
&& !quoted
)
763 if (code
!= SYNTAX (c
))
764 /* For a two-char comment ender, we can assume
765 it does end a comment. So scan back in a simple way. */
767 if (from
!= stop
) from
--;
770 if ((c
= FETCH_CHAR (from
),
771 SYNTAX (c
) == Scomment
)
772 && SYNTAX_COMMENT_STYLE (c
) == comstyle
)
781 if (SYNTAX_COMSTART_SECOND (c
)
782 && (c1
= FETCH_CHAR (from
),
783 SYNTAX_COMSTART_FIRST (c1
))
784 && SYNTAX_COMMENT_STYLE (c
) == comstyle
785 && !char_quoted (from
))
792 /* Look back, counting the parity of string-quotes,
793 and recording the comment-starters seen.
794 When we reach a safe place, assume that's not in a string;
795 then step the main scan to the earliest comment-starter seen
796 an even number of string quotes away from the safe place.
798 OFROM[I] is position of the earliest comment-starter seen
799 which is I+2X quotes from the comment-end.
800 PARITY is current parity of quotes from the comment end. */
803 char my_stringend
= 0;
804 int string_lossage
= 0;
805 int comment_end
= from
;
806 int comstart_pos
= 0;
807 int comstart_parity
= 0;
808 int scanstart
= from
- 1;
810 /* At beginning of range to scan, we're outside of strings;
811 that determines quote parity to the comment-end. */
814 /* Move back and examine a character. */
817 c
= FETCH_CHAR (from
);
820 /* If this char is the second of a 2-char comment sequence,
821 back up and give the pair the appropriate syntax. */
822 if (from
> stop
&& SYNTAX_COMEND_SECOND (c
)
823 && SYNTAX_COMEND_FIRST (FETCH_CHAR (from
- 1)))
827 c
= FETCH_CHAR (from
);
830 /* If this char starts a 2-char comment start sequence,
831 treat it like a 1-char comment starter. */
832 if (from
< scanstart
&& SYNTAX_COMSTART_FIRST (c
)
833 && SYNTAX_COMSTART_SECOND (FETCH_CHAR (from
+ 1))
834 && comstyle
== SYNTAX_COMMENT_STYLE (FETCH_CHAR (from
+ 1)))
837 /* Ignore escaped characters. */
838 if (char_quoted (from
))
841 /* Track parity of quotes. */
845 if (my_stringend
== 0)
847 /* If we have two kinds of string delimiters.
848 There's no way to grok this scanning backwards. */
849 else if (my_stringend
!= c
)
853 /* Record comment-starters according to that
854 quote-parity to the comment-end. */
855 if (code
== Scomment
)
857 comstart_parity
= parity
;
861 /* If we find another earlier comment-ender,
862 any comment-starts earlier than that don't count
863 (because they go with the earlier comment-ender). */
864 if (code
== Sendcomment
865 && SYNTAX_COMMENT_STYLE (FETCH_CHAR (from
)) == comstyle
)
868 /* Assume a defun-start point is outside of strings. */
870 && (from
== stop
|| FETCH_CHAR (from
- 1) == '\n'))
874 if (comstart_pos
== 0)
876 /* If the earliest comment starter
877 is followed by uniform paired string quotes or none,
878 we know it can't be inside a string
879 since if it were then the comment ender would be inside one.
880 So it does start a comment. Skip back to it. */
881 else if (comstart_parity
== 0 && !string_lossage
)
885 /* We had two kinds of string delimiters mixed up
886 together. Decode this going forwards.
887 Scan fwd from the previous comment ender
888 to the one in question; this records where we
889 last passed a comment starter. */
890 struct lisp_parse_state state
;
891 scan_sexps_forward (&state
, find_defun_start (comment_end
),
892 comment_end
- 1, -10000, 0, Qnil
, 0);
894 from
= state
.comstart
;
896 /* We can't grok this as a comment; scan it normally. */
900 /* We have skipped one comment. */
903 else if ((code
!= Swhitespace
&& code
!= Scomment
) || quoted
)
919 int parse_sexp_ignore_comments
;
922 scan_lists (from
, count
, depth
, sexpflag
)
924 int count
, depth
, sexpflag
;
929 unsigned char stringterm
;
932 register enum syntaxcode code
;
933 int min_depth
= depth
; /* Err out if depth gets less than this. */
934 int comstyle
= 0; /* style of comment encountered */
936 if (depth
> 0) min_depth
= 0;
946 c
= FETCH_CHAR (from
);
949 if (from
< stop
&& SYNTAX_COMSTART_FIRST (c
)
950 && SYNTAX_COMSTART_SECOND (FETCH_CHAR (from
))
951 && parse_sexp_ignore_comments
)
953 /* we have encountered a comment start sequence and we
954 are ignoring all text inside comments. we must record
955 the comment style this sequence begins so that later,
956 only a comment end of the same style actually ends
957 the comment section */
959 comstyle
= SYNTAX_COMMENT_STYLE (FETCH_CHAR (from
));
963 if (SYNTAX_PREFIX (c
))
966 switch (SWITCH_ENUM_CAST (code
))
970 if (from
== stop
) goto lose
;
972 /* treat following character as a word constituent */
975 if (depth
|| !sexpflag
) break;
976 /* This word counts as a sexp; return at end of it. */
979 switch (SWITCH_ENUM_CAST (SYNTAX (FETCH_CHAR (from
))))
984 if (from
== stop
) goto lose
;
998 if (!parse_sexp_ignore_comments
) break;
1007 c
= FETCH_CHAR (from
);
1008 if (SYNTAX (c
) == Sendcomment
1009 && SYNTAX_COMMENT_STYLE (c
) == comstyle
)
1010 /* we have encountered a comment end of the same style
1011 as the comment sequence which began this comment
1015 if (from
< stop
&& SYNTAX_COMEND_FIRST (c
)
1016 && SYNTAX_COMEND_SECOND (FETCH_CHAR (from
))
1017 && SYNTAX_COMMENT_STYLE (c
) == comstyle
)
1018 /* we have encountered a comment end of the same style
1019 as the comment sequence which began this comment
1028 if (from
!= stop
&& c
== FETCH_CHAR (from
))
1038 if (!++depth
) goto done
;
1043 if (!--depth
) goto done
;
1044 if (depth
< min_depth
)
1045 error ("Containing expression ends prematurely");
1049 stringterm
= FETCH_CHAR (from
- 1);
1052 if (from
>= stop
) goto lose
;
1053 if (FETCH_CHAR (from
) == stringterm
) break;
1054 switch (SWITCH_ENUM_CAST (SYNTAX (FETCH_CHAR (from
))))
1063 if (!depth
&& sexpflag
) goto done
;
1068 /* Reached end of buffer. Error if within object, return nil if between */
1069 if (depth
) goto lose
;
1074 /* End of object reached */
1085 if (quoted
= char_quoted (from
))
1087 c
= FETCH_CHAR (from
);
1090 if (code
== Sendcomment
)
1091 comstyle
= SYNTAX_COMMENT_STYLE (c
);
1092 if (from
> stop
&& SYNTAX_COMEND_SECOND (c
)
1093 && SYNTAX_COMEND_FIRST (FETCH_CHAR (from
- 1))
1094 && !char_quoted (from
- 1)
1095 && parse_sexp_ignore_comments
)
1097 /* we must record the comment style encountered so that
1098 later, we can match only the proper comment begin
1099 sequence of the same style */
1101 comstyle
= SYNTAX_COMMENT_STYLE (FETCH_CHAR (from
- 1));
1105 if (SYNTAX_PREFIX (c
))
1108 switch (SWITCH_ENUM_CAST (quoted
? Sword
: code
))
1112 if (depth
|| !sexpflag
) break;
1113 /* This word counts as a sexp; count object finished after passing it. */
1116 quoted
= char_quoted (from
- 1);
1119 if (! (quoted
|| SYNTAX (FETCH_CHAR (from
- 1)) == Sword
1120 || SYNTAX (FETCH_CHAR (from
- 1)) == Ssymbol
1121 || SYNTAX (FETCH_CHAR (from
- 1)) == Squote
))
1130 if (from
!= stop
&& c
== FETCH_CHAR (from
- 1))
1140 if (!++depth
) goto done2
;
1145 if (!--depth
) goto done2
;
1146 if (depth
< min_depth
)
1147 error ("Containing expression ends prematurely");
1151 if (!parse_sexp_ignore_comments
)
1154 if (code
!= SYNTAX (c
))
1155 /* For a two-char comment ender, we can assume
1156 it does end a comment. So scan back in a simple way. */
1158 if (from
!= stop
) from
--;
1161 if (SYNTAX (c
= FETCH_CHAR (from
)) == Scomment
1162 && SYNTAX_COMMENT_STYLE (c
) == comstyle
)
1171 if (SYNTAX_COMSTART_SECOND (c
)
1172 && SYNTAX_COMSTART_FIRST (FETCH_CHAR (from
))
1173 && SYNTAX_COMMENT_STYLE (c
) == comstyle
1174 && !char_quoted (from
))
1181 /* Look back, counting the parity of string-quotes,
1182 and recording the comment-starters seen.
1183 When we reach a safe place, assume that's not in a string;
1184 then step the main scan to the earliest comment-starter seen
1185 an even number of string quotes away from the safe place.
1187 OFROM[I] is position of the earliest comment-starter seen
1188 which is I+2X quotes from the comment-end.
1189 PARITY is current parity of quotes from the comment end. */
1192 char my_stringend
= 0;
1193 int string_lossage
= 0;
1194 int comment_end
= from
;
1195 int comstart_pos
= 0;
1196 int comstart_parity
= 0;
1197 int scanstart
= from
- 1;
1199 /* At beginning of range to scan, we're outside of strings;
1200 that determines quote parity to the comment-end. */
1201 while (from
!= stop
)
1203 /* Move back and examine a character. */
1206 c
= FETCH_CHAR (from
);
1209 /* If this char is the second of a 2-char comment sequence,
1210 back up and give the pair the appropriate syntax. */
1211 if (from
> stop
&& SYNTAX_COMEND_SECOND (c
)
1212 && SYNTAX_COMEND_FIRST (FETCH_CHAR (from
- 1)))
1216 c
= FETCH_CHAR (from
);
1219 /* If this char starts a 2-char comment start sequence,
1220 treat it like a 1-char comment starter. */
1221 if (from
< scanstart
&& SYNTAX_COMSTART_FIRST (c
)
1222 && SYNTAX_COMSTART_SECOND (FETCH_CHAR (from
+ 1))
1223 && comstyle
== SYNTAX_COMMENT_STYLE (FETCH_CHAR (from
+ 1)))
1226 /* Ignore escaped characters. */
1227 if (char_quoted (from
))
1230 /* Track parity of quotes. */
1231 if (code
== Sstring
)
1234 if (my_stringend
== 0)
1236 /* If we have two kinds of string delimiters.
1237 There's no way to grok this scanning backwards. */
1238 else if (my_stringend
!= c
)
1242 /* Record comment-starters according to that
1243 quote-parity to the comment-end. */
1244 if (code
== Scomment
)
1246 comstart_parity
= parity
;
1247 comstart_pos
= from
;
1250 /* If we find another earlier comment-ender,
1251 any comment-starts earlier than that don't count
1252 (because they go with the earlier comment-ender). */
1253 if (code
== Sendcomment
1254 && SYNTAX_COMMENT_STYLE (FETCH_CHAR (from
)) == comstyle
)
1257 /* Assume a defun-start point is outside of strings. */
1259 && (from
== stop
|| FETCH_CHAR (from
- 1) == '\n'))
1263 if (comstart_pos
== 0)
1265 /* If the earliest comment starter
1266 is followed by uniform paired string quotes or none,
1267 we know it can't be inside a string
1268 since if it were then the comment ender would be inside one.
1269 So it does start a comment. Skip back to it. */
1270 else if (comstart_parity
== 0 && !string_lossage
)
1271 from
= comstart_pos
;
1274 /* We had two kinds of string delimiters mixed up
1275 together. Decode this going forwards.
1276 Scan fwd from the previous comment ender
1277 to the one in question; this records where we
1278 last passed a comment starter. */
1279 struct lisp_parse_state state
;
1280 scan_sexps_forward (&state
, find_defun_start (comment_end
),
1281 comment_end
- 1, -10000, 0, Qnil
, 0);
1282 if (state
.incomment
)
1283 from
= state
.comstart
;
1285 /* We can't grok this as a comment; scan it normally. */
1292 stringterm
= FETCH_CHAR (from
);
1295 if (from
== stop
) goto lose
;
1296 if (!char_quoted (from
- 1)
1297 && stringterm
== FETCH_CHAR (from
- 1))
1302 if (!depth
&& sexpflag
) goto done2
;
1307 /* Reached start of buffer. Error if within object, return nil if between */
1308 if (depth
) goto lose
;
1319 XSETFASTINT (val
, from
);
1323 error ("Unbalanced parentheses");
1331 register enum syntaxcode code
;
1332 register int beg
= BEGV
;
1333 register int quoted
= 0;
1336 && ((code
= SYNTAX (FETCH_CHAR (pos
- 1))) == Scharquote
1337 || code
== Sescape
))
1338 pos
--, quoted
= !quoted
;
1342 DEFUN ("scan-lists", Fscan_lists
, Sscan_lists
, 3, 3, 0,
1343 "Scan from character number FROM by COUNT lists.\n\
1344 Returns the character number of the position thus found.\n\
1346 If DEPTH is nonzero, paren depth begins counting from that value,\n\
1347 only places where the depth in parentheses becomes zero\n\
1348 are candidates for stopping; COUNT such places are counted.\n\
1349 Thus, a positive value for DEPTH means go out levels.\n\
1351 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.\n\
1353 If the beginning or end of (the accessible part of) the buffer is reached\n\
1354 and the depth is wrong, an error is signaled.\n\
1355 If the depth is right but the count is not used up, nil is returned.")
1356 (from
, count
, depth
)
1357 Lisp_Object from
, count
, depth
;
1359 CHECK_NUMBER (from
, 0);
1360 CHECK_NUMBER (count
, 1);
1361 CHECK_NUMBER (depth
, 2);
1363 return scan_lists (XINT (from
), XINT (count
), XINT (depth
), 0);
1366 DEFUN ("scan-sexps", Fscan_sexps
, Sscan_sexps
, 2, 2, 0,
1367 "Scan from character number FROM by COUNT balanced expressions.\n\
1368 If COUNT is negative, scan backwards.\n\
1369 Returns the character number of the position thus found.\n\
1371 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.\n\
1373 If the beginning or end of (the accessible part of) the buffer is reached\n\
1374 in the middle of a parenthetical grouping, an error is signaled.\n\
1375 If the beginning or end is reached between groupings\n\
1376 but before count is used up, nil is returned.")
1378 Lisp_Object from
, count
;
1380 CHECK_NUMBER (from
, 0);
1381 CHECK_NUMBER (count
, 1);
1383 return scan_lists (XINT (from
), XINT (count
), 0, 1);
1386 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars
, Sbackward_prefix_chars
,
1388 "Move point backward over any number of chars with prefix syntax.\n\
1389 This includes chars with \"quote\" or \"prefix\" syntax (' or p).")
1395 while (pos
> beg
&& !char_quoted (pos
- 1)
1396 && (SYNTAX (FETCH_CHAR (pos
- 1)) == Squote
1397 || SYNTAX_PREFIX (FETCH_CHAR (pos
- 1))))
1405 /* Parse forward from FROM to END,
1406 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
1407 and return a description of the state of the parse at END.
1408 If STOPBEFORE is nonzero, stop at the start of an atom.
1409 If COMMENTSTOP is nonzero, stop at the start of a comment. */
1412 scan_sexps_forward (stateptr
, from
, end
, targetdepth
,
1413 stopbefore
, oldstate
, commentstop
)
1414 struct lisp_parse_state
*stateptr
;
1416 int end
, targetdepth
, stopbefore
;
1417 Lisp_Object oldstate
;
1420 struct lisp_parse_state state
;
1422 register enum syntaxcode code
;
1423 struct level
{ int last
, prev
; };
1424 struct level levelstart
[100];
1425 register struct level
*curlevel
= levelstart
;
1426 struct level
*endlevel
= levelstart
+ 100;
1428 register int depth
; /* Paren depth of current scanning location.
1429 level - levelstart equals this except
1430 when the depth becomes negative. */
1431 int mindepth
; /* Lowest DEPTH value seen. */
1432 int start_quoted
= 0; /* Nonzero means starting after a char quote */
1438 if (NILP (oldstate
))
1441 state
.instring
= -1;
1442 state
.incomment
= 0;
1443 state
.comstyle
= 0; /* comment style a by default */
1447 tem
= Fcar (oldstate
);
1453 oldstate
= Fcdr (oldstate
);
1454 oldstate
= Fcdr (oldstate
);
1455 oldstate
= Fcdr (oldstate
);
1456 tem
= Fcar (oldstate
);
1457 state
.instring
= !NILP (tem
) ? XINT (tem
) : -1;
1459 oldstate
= Fcdr (oldstate
);
1460 tem
= Fcar (oldstate
);
1461 state
.incomment
= !NILP (tem
);
1463 oldstate
= Fcdr (oldstate
);
1464 tem
= Fcar (oldstate
);
1465 start_quoted
= !NILP (tem
);
1467 /* if the eight element of the list is nil, we are in comment
1468 style a. if it is non-nil, we are in comment style b */
1469 oldstate
= Fcdr (oldstate
);
1470 oldstate
= Fcdr (oldstate
);
1471 tem
= Fcar (oldstate
);
1472 state
.comstyle
= !NILP (tem
);
1477 curlevel
->prev
= -1;
1478 curlevel
->last
= -1;
1480 /* Enter the loop at a place appropriate for initial state. */
1482 if (state
.incomment
) goto startincomment
;
1483 if (state
.instring
>= 0)
1485 if (start_quoted
) goto startquotedinstring
;
1488 if (start_quoted
) goto startquoted
;
1492 code
= SYNTAX (FETCH_CHAR (from
));
1494 if (code
== Scomment
)
1495 state
.comstart
= from
-1;
1497 else if (from
< end
&& SYNTAX_COMSTART_FIRST (FETCH_CHAR (from
- 1))
1498 && SYNTAX_COMSTART_SECOND (FETCH_CHAR (from
)))
1500 /* Record the comment style we have entered so that only
1501 the comment-end sequence of the same style actually
1502 terminates the comment section. */
1504 state
.comstyle
= SYNTAX_COMMENT_STYLE (FETCH_CHAR (from
));
1505 state
.comstart
= from
-1;
1509 if (SYNTAX_PREFIX (FETCH_CHAR (from
- 1)))
1511 switch (SWITCH_ENUM_CAST (code
))
1515 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
1516 curlevel
->last
= from
- 1;
1518 if (from
== end
) goto endquoted
;
1521 /* treat following character as a word constituent */
1524 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
1525 curlevel
->last
= from
- 1;
1529 switch (SWITCH_ENUM_CAST (SYNTAX (FETCH_CHAR (from
))))
1534 if (from
== end
) goto endquoted
;
1546 curlevel
->prev
= curlevel
->last
;
1554 /* Enter the loop in the middle so that we find
1555 a 2-char comment ender if we start in the middle of it. */
1556 prev
= FETCH_CHAR (from
- 1);
1557 goto startincomment_1
;
1559 /* At beginning of buffer, enter the loop the ordinary way. */
1562 state
.incomment
= 1;
1567 if (from
== end
) goto done
;
1568 prev
= FETCH_CHAR (from
);
1569 if (SYNTAX (prev
) == Sendcomment
1570 && SYNTAX_COMMENT_STYLE (prev
) == state
.comstyle
)
1571 /* Only terminate the comment section if the endcomment
1572 of the same style as the start sequence has been
1577 if (from
< end
&& SYNTAX_COMEND_FIRST (prev
)
1578 && SYNTAX_COMEND_SECOND (FETCH_CHAR (from
))
1579 && SYNTAX_COMMENT_STYLE (prev
) == state
.comstyle
)
1580 /* Only terminate the comment section if the end-comment
1581 sequence of the same style as the start sequence has
1582 been encountered. */
1585 state
.incomment
= 0;
1586 state
.comstyle
= 0; /* reset the comment style */
1590 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
1592 /* curlevel++->last ran into compiler bug on Apollo */
1593 curlevel
->last
= from
- 1;
1594 if (++curlevel
== endlevel
)
1595 error ("Nesting too deep for parser");
1596 curlevel
->prev
= -1;
1597 curlevel
->last
= -1;
1598 if (targetdepth
== depth
) goto done
;
1603 if (depth
< mindepth
)
1605 if (curlevel
!= levelstart
)
1607 curlevel
->prev
= curlevel
->last
;
1608 if (targetdepth
== depth
) goto done
;
1612 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
1613 curlevel
->last
= from
- 1;
1614 state
.instring
= FETCH_CHAR (from
- 1);
1618 if (from
>= end
) goto done
;
1619 if (FETCH_CHAR (from
) == state
.instring
) break;
1620 switch (SWITCH_ENUM_CAST (SYNTAX (FETCH_CHAR (from
))))
1625 startquotedinstring
:
1626 if (from
>= end
) goto endquoted
;
1630 state
.instring
= -1;
1631 curlevel
->prev
= curlevel
->last
;
1641 stop
: /* Here if stopping before start of sexp. */
1642 from
--; /* We have just fetched the char that starts it; */
1643 goto done
; /* but return the position before it. */
1648 state
.depth
= depth
;
1649 state
.mindepth
= mindepth
;
1650 state
.thislevelstart
= curlevel
->prev
;
1651 state
.prevlevelstart
1652 = (curlevel
== levelstart
) ? -1 : (curlevel
- 1)->last
;
1653 state
.location
= from
;
1659 /* This comment supplies the doc string for parse-partial-sexp,
1660 for make-docfile to see. We cannot put this in the real DEFUN
1661 due to limits in the Unix cpp.
1663 DEFUN ("parse-partial-sexp", Ffoo, Sfoo, 2, 6, 0,
1664 "Parse Lisp syntax starting at FROM until TO; return status of parse at TO.\n\
1665 Parsing stops at TO or when certain criteria are met;\n\
1666 point is set to where parsing stops.\n\
1667 If fifth arg STATE is omitted or nil,\n\
1668 parsing assumes that FROM is the beginning of a function.\n\
1669 Value is a list of eight elements describing final state of parsing:\n\
1670 0. depth in parens.\n\
1671 1. character address of start of innermost containing list; nil if none.\n\
1672 2. character address of start of last complete sexp terminated.\n\
1673 3. non-nil if inside a string.\n\
1674 (it is the character that will terminate the string.)\n\
1675 4. t if inside a comment.\n\
1676 5. t if following a quote character.\n\
1677 6. the minimum paren-depth encountered during this scan.\n\
1678 7. t if in a comment of style `b'.\n\
1679 If third arg TARGETDEPTH is non-nil, parsing stops if the depth\n\
1680 in parentheses becomes equal to TARGETDEPTH.\n\
1681 Fourth arg STOPBEFORE non-nil means stop when come to\n\
1682 any character that starts a sexp.\n\
1683 Fifth arg STATE is an eight-list like what this function returns.\n\
1684 It is used to initialize the state of the parse. Its second and third
1685 elements are ignored.
1686 Sixth args COMMENTSTOP non-nil means stop at the start of a comment.")
1687 (from, to, targetdepth, stopbefore, state, commentstop)
1690 DEFUN ("parse-partial-sexp", Fparse_partial_sexp
, Sparse_partial_sexp
, 2, 6, 0,
1691 0 /* See immediately above */)
1692 (from
, to
, targetdepth
, stopbefore
, oldstate
, commentstop
)
1693 Lisp_Object from
, to
, targetdepth
, stopbefore
, oldstate
, commentstop
;
1695 struct lisp_parse_state state
;
1698 if (!NILP (targetdepth
))
1700 CHECK_NUMBER (targetdepth
, 3);
1701 target
= XINT (targetdepth
);
1704 target
= -100000; /* We won't reach this depth */
1706 validate_region (&from
, &to
);
1707 scan_sexps_forward (&state
, XINT (from
), XINT (to
),
1708 target
, !NILP (stopbefore
), oldstate
,
1709 !NILP (commentstop
));
1711 SET_PT (state
.location
);
1713 return Fcons (make_number (state
.depth
),
1714 Fcons (state
.prevlevelstart
< 0 ? Qnil
: make_number (state
.prevlevelstart
),
1715 Fcons (state
.thislevelstart
< 0 ? Qnil
: make_number (state
.thislevelstart
),
1716 Fcons (state
.instring
>= 0 ? make_number (state
.instring
) : Qnil
,
1717 Fcons (state
.incomment
? Qt
: Qnil
,
1718 Fcons (state
.quoted
? Qt
: Qnil
,
1719 Fcons (make_number (state
.mindepth
),
1720 Fcons (state
.comstyle
? Qt
: Qnil
,
1729 /* This has to be done here, before we call Fmake_char_table. */
1730 Qsyntax_table
= intern ("syntax-table");
1731 staticpro (&Qsyntax_table
);
1733 /* Intern this now in case it isn't already done.
1734 Setting this variable twice is harmless.
1735 But don't staticpro it here--that is done in alloc.c. */
1736 Qchar_table_extra_slots
= intern ("char-table-extra-slots");
1738 /* Now we are ready to set up this property, so we can
1739 create syntax tables. */
1740 Fput (Qsyntax_table
, Qchar_table_extra_slots
, make_number (0));
1742 temp
= Fcons (make_number ((int) Swhitespace
), Qnil
);
1744 Vstandard_syntax_table
= Fmake_char_table (Qsyntax_table
, temp
);
1746 temp
= Fcons (make_number ((int) Sword
), Qnil
);
1747 for (i
= 'a'; i
<= 'z'; i
++)
1748 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
1749 for (i
= 'A'; i
<= 'Z'; i
++)
1750 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
1751 for (i
= '0'; i
<= '9'; i
++)
1752 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
1754 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '$', temp
);
1755 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '%', temp
);
1757 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '(',
1758 Fcons (make_number (Sopen
), make_number (')')));
1759 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ')',
1760 Fcons (make_number (Sclose
), make_number ('(')));
1761 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '[',
1762 Fcons (make_number (Sopen
), make_number (']')));
1763 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ']',
1764 Fcons (make_number (Sclose
), make_number ('[')));
1765 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '{',
1766 Fcons (make_number (Sopen
), make_number ('}')));
1767 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '}',
1768 Fcons (make_number (Sclose
), make_number ('{')));
1769 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '"',
1770 Fcons (make_number ((int) Sstring
), Qnil
));
1771 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\\',
1772 Fcons (make_number ((int) Sescape
), Qnil
));
1774 temp
= Fcons (make_number ((int) Ssymbol
), Qnil
);
1775 for (i
= 0; i
< 10; i
++)
1776 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, "_-+*/&|<>="[i
], temp
);
1778 temp
= Fcons (make_number ((int) Spunct
), Qnil
);
1779 for (i
= 0; i
< 12; i
++)
1780 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ".,;:?!#@~^'`"[i
], temp
);
1785 Qsyntax_table_p
= intern ("syntax-table-p");
1786 staticpro (&Qsyntax_table_p
);
1788 DEFVAR_BOOL ("parse-sexp-ignore-comments", &parse_sexp_ignore_comments
,
1789 "Non-nil means `forward-sexp', etc., should treat comments as whitespace.");
1791 words_include_escapes
= 0;
1792 DEFVAR_BOOL ("words-include-escapes", &words_include_escapes
,
1793 "Non-nil means `forward-word', etc., should treat escape chars part of words.");
1795 defsubr (&Ssyntax_table_p
);
1796 defsubr (&Ssyntax_table
);
1797 defsubr (&Sstandard_syntax_table
);
1798 defsubr (&Scopy_syntax_table
);
1799 defsubr (&Sset_syntax_table
);
1800 defsubr (&Schar_syntax
);
1801 defsubr (&Smatching_paren
);
1802 defsubr (&Smodify_syntax_entry
);
1803 defsubr (&Sdescribe_syntax
);
1805 defsubr (&Sforward_word
);
1807 defsubr (&Sforward_comment
);
1808 defsubr (&Sscan_lists
);
1809 defsubr (&Sscan_sexps
);
1810 defsubr (&Sbackward_prefix_chars
);
1811 defsubr (&Sparse_partial_sexp
);