Rename comment-depth (etc.) to literal-cache (etc.). Enable it by default
[emacs.git] / src / syntax.h
blobcc277272daf9dff78a353d7b90822d7f113f1645
1 /* Declarations having to do with GNU Emacs syntax tables.
3 Copyright (C) 1985, 1993-1994, 1997-1998, 2001-2016 Free Software
4 Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #ifndef EMACS_SYNTAX_H
22 #define EMACS_SYNTAX_H
24 #include "buffer.h"
25 #include "lisp.h"
27 INLINE_HEADER_BEGIN
29 extern void update_syntax_table (ptrdiff_t, EMACS_INT, bool, Lisp_Object);
30 extern void update_syntax_table_forward (ptrdiff_t, bool, Lisp_Object);
31 extern void check_literal_cache_hwm_for_prop (ptrdiff_t, Lisp_Object,
32 Lisp_Object, Lisp_Object);
34 /* The standard syntax table is stored where it will automatically
35 be used in all new buffers. */
36 #define Vstandard_syntax_table BVAR (&buffer_defaults, syntax_table)
38 /* A syntax table is a chartable whose elements are cons cells
39 (CODE+FLAGS . MATCHING-CHAR). MATCHING-CHAR can be nil if the char
40 is not a kind of parenthesis.
42 The low 8 bits of CODE+FLAGS is a code, as follows: */
44 enum syntaxcode
46 Swhitespace, /* for a whitespace character */
47 Spunct, /* for random punctuation characters */
48 Sword, /* for a word constituent */
49 Ssymbol, /* symbol constituent but not word constituent */
50 Sopen, /* for a beginning delimiter */
51 Sclose, /* for an ending delimiter */
52 Squote, /* for a prefix character like Lisp ' */
53 Sstring, /* for a string-grouping character like Lisp " */
54 Smath, /* for delimiters like $ in Tex. */
55 Sescape, /* for a character that begins a C-style escape */
56 Scharquote, /* for a character that quotes the following character */
57 Scomment, /* for a comment-starting character */
58 Sendcomment, /* for a comment-ending character */
59 Sinherit, /* use the standard syntax table for this character */
60 Scomment_fence, /* Starts/ends comment which is delimited on the
61 other side by any char with the same syntaxcode. */
62 Sstring_fence, /* Starts/ends string which is delimited on the
63 other side by any char with the same syntaxcode. */
64 Smax /* Upper bound on codes that are meaningful. */
68 struct gl_state_s
70 Lisp_Object object; /* The object we are scanning. */
71 ptrdiff_t start; /* Where to stop. */
72 ptrdiff_t stop; /* Where to stop. */
73 bool use_global; /* Whether to use global_code
74 or c_s_t. */
75 Lisp_Object global_code; /* Syntax code of current char. */
76 Lisp_Object current_syntax_table; /* Syntax table for current pos. */
77 Lisp_Object old_prop; /* Syntax-table prop at prev pos. */
78 ptrdiff_t b_property; /* First index where c_s_t is valid. */
79 ptrdiff_t e_property; /* First index where c_s_t is
80 not valid. */
81 bool e_property_truncated; /* true if e_property if was truncated
82 by parse_sexp_propertize_done. */
83 INTERVAL forward_i; /* Where to start lookup on forward. */
84 INTERVAL backward_i; /* or backward movement. The
85 data in c_s_t is valid
86 between these intervals,
87 and possibly at the
88 intervals too, depending
89 on: */
90 /* Offset for positions specified to UPDATE_SYNTAX_TABLE. */
91 ptrdiff_t offset;
94 extern struct gl_state_s gl_state;
96 /* Fetch the information from the entry for character C
97 in the current buffer's syntax table,
98 or (if VIA_PROPERTY) from globally kept data (gl_state).
99 Does inheritance. */
101 INLINE Lisp_Object
102 syntax_property_entry (int c, bool via_property)
104 if (via_property)
105 return (gl_state.use_global
106 ? gl_state.global_code
107 : CHAR_TABLE_REF (gl_state.current_syntax_table, c));
108 return CHAR_TABLE_REF (BVAR (current_buffer, syntax_table), c);
110 INLINE Lisp_Object
111 SYNTAX_ENTRY (int c)
113 return syntax_property_entry (c, false);
116 /* Extract the information from the entry for character C
117 in the current syntax table. */
119 INLINE int
120 syntax_property_with_flags (int c, bool via_property)
122 Lisp_Object ent = syntax_property_entry (c, via_property);
123 return CONSP (ent) ? XINT (XCAR (ent)) : Swhitespace;
125 INLINE int
126 SYNTAX_WITH_FLAGS (int c)
128 return syntax_property_with_flags (c, false);
131 INLINE enum syntaxcode
132 syntax_property (int c, bool via_property)
134 return syntax_property_with_flags (c, via_property) & 0xff;
136 INLINE enum syntaxcode
137 SYNTAX (int c)
139 return syntax_property (c, false);
143 /* Whether the syntax of the character C has the prefix flag set. */
144 extern bool syntax_prefix_flag_p (int c);
146 /* This array, indexed by a character less than 256, contains the
147 syntax code which that character signifies (as an unsigned char).
148 For example, syntax_spec_code['w'] == Sword. */
150 extern unsigned char const syntax_spec_code[0400];
152 /* Indexed by syntax code, give the letter that describes it. */
154 extern char const syntax_code_spec[16];
156 /* Convert the byte offset BYTEPOS into a character position,
157 for the object recorded in gl_state with SETUP_SYNTAX_TABLE_FOR_OBJECT.
159 The value is meant for use in code that does nothing when
160 parse_sexp_lookup_properties is false, so return 0 in that case,
161 for speed. */
163 INLINE ptrdiff_t
164 SYNTAX_TABLE_BYTE_TO_CHAR (ptrdiff_t bytepos)
166 return (! parse_sexp_lookup_properties
168 : STRINGP (gl_state.object)
169 ? string_byte_to_char (gl_state.object, bytepos)
170 : BUFFERP (gl_state.object)
171 ? ((buf_bytepos_to_charpos
172 (XBUFFER (gl_state.object),
173 (bytepos + BUF_BEGV_BYTE (XBUFFER (gl_state.object)) - 1)))
174 - BUF_BEGV (XBUFFER (gl_state.object)) + 1)
175 : NILP (gl_state.object)
176 ? BYTE_TO_CHAR (bytepos + BEGV_BYTE - 1) - BEGV + 1
177 : bytepos);
180 /* Make syntax table state (gl_state) good for CHARPOS, assuming it is
181 currently good for a position before CHARPOS. */
183 INLINE void
184 UPDATE_SYNTAX_TABLE_FORWARD (ptrdiff_t charpos)
185 { /* Performs just-in-time syntax-propertization. */
186 if (parse_sexp_lookup_properties && charpos >= gl_state.e_property)
187 update_syntax_table_forward (charpos + gl_state.offset,
188 false, gl_state.object);
191 INLINE void
192 UPDATE_SYNTAX_TABLE_FORWARD_FAST (ptrdiff_t charpos)
194 if (parse_sexp_lookup_properties && charpos >= gl_state.e_property)
195 update_syntax_table (charpos + gl_state.offset, 1, false, gl_state.object);
198 /* Make syntax table state (gl_state) good for CHARPOS, assuming it is
199 currently good for a position after CHARPOS. */
201 INLINE void
202 UPDATE_SYNTAX_TABLE_BACKWARD (ptrdiff_t charpos)
204 if (parse_sexp_lookup_properties && charpos < gl_state.b_property)
205 update_syntax_table (charpos + gl_state.offset, -1, false, gl_state.object);
208 /* Make syntax table good for CHARPOS. */
210 INLINE void
211 UPDATE_SYNTAX_TABLE (ptrdiff_t charpos)
213 UPDATE_SYNTAX_TABLE_BACKWARD (charpos);
214 UPDATE_SYNTAX_TABLE_FORWARD (charpos);
217 INLINE void
218 UPDATE_SYNTAX_TABLE_FAST (ptrdiff_t charpos)
220 UPDATE_SYNTAX_TABLE_BACKWARD (charpos);
221 UPDATE_SYNTAX_TABLE_FORWARD_FAST (charpos);
224 /* Set up the buffer-global syntax table. */
226 INLINE void
227 SETUP_BUFFER_SYNTAX_TABLE (void)
229 gl_state.use_global = false;
230 gl_state.e_property_truncated = false;
231 gl_state.current_syntax_table = BVAR (current_buffer, syntax_table);
234 extern ptrdiff_t scan_words (ptrdiff_t, EMACS_INT);
235 extern void SETUP_SYNTAX_TABLE_FOR_OBJECT (Lisp_Object, ptrdiff_t, ptrdiff_t);
237 INLINE_HEADER_END
239 #endif /* EMACS_SYNTAX_H */