; * src/doc.c: Add a 'coding' cookie.
[emacs.git] / src / syntax.h
blobbfcb87168ba87a6115c1ca4b4d803055cf07a652
1 /* Declarations having to do with GNU Emacs syntax tables.
3 Copyright (C) 1985, 1993-1994, 1997-1998, 2001-2015 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 INLINE_HEADER_BEGIN
23 extern void update_syntax_table (ptrdiff_t, EMACS_INT, bool, Lisp_Object);
25 /* The standard syntax table is stored where it will automatically
26 be used in all new buffers. */
27 #define Vstandard_syntax_table BVAR (&buffer_defaults, syntax_table)
29 /* A syntax table is a chartable whose elements are cons cells
30 (CODE+FLAGS . MATCHING-CHAR). MATCHING-CHAR can be nil if the char
31 is not a kind of parenthesis.
33 The low 8 bits of CODE+FLAGS is a code, as follows: */
35 enum syntaxcode
37 Swhitespace, /* for a whitespace character */
38 Spunct, /* for random punctuation characters */
39 Sword, /* for a word constituent */
40 Ssymbol, /* symbol constituent but not word constituent */
41 Sopen, /* for a beginning delimiter */
42 Sclose, /* for an ending delimiter */
43 Squote, /* for a prefix character like Lisp ' */
44 Sstring, /* for a string-grouping character like Lisp " */
45 Smath, /* for delimiters like $ in Tex. */
46 Sescape, /* for a character that begins a C-style escape */
47 Scharquote, /* for a character that quotes the following character */
48 Scomment, /* for a comment-starting character */
49 Sendcomment, /* for a comment-ending character */
50 Sinherit, /* use the standard syntax table for this character */
51 Scomment_fence, /* Starts/ends comment which is delimited on the
52 other side by any char with the same syntaxcode. */
53 Sstring_fence, /* Starts/ends string which is delimited on the
54 other side by any char with the same syntaxcode. */
55 Smax /* Upper bound on codes that are meaningful */
59 struct gl_state_s
61 Lisp_Object object; /* The object we are scanning. */
62 ptrdiff_t start; /* Where to stop. */
63 ptrdiff_t stop; /* Where to stop. */
64 bool use_global; /* Whether to use global_code
65 or c_s_t. */
66 Lisp_Object global_code; /* Syntax code of current char. */
67 Lisp_Object current_syntax_table; /* Syntax table for current pos. */
68 Lisp_Object old_prop; /* Syntax-table prop at prev pos. */
69 ptrdiff_t b_property; /* First index where c_s_t is valid. */
70 ptrdiff_t e_property; /* First index where c_s_t is
71 not valid. */
72 INTERVAL forward_i; /* Where to start lookup on forward */
73 INTERVAL backward_i; /* or backward movement. The
74 data in c_s_t is valid
75 between these intervals,
76 and possibly at the
77 intervals too, depending
78 on: */
79 /* Offset for positions specified to UPDATE_SYNTAX_TABLE. */
80 ptrdiff_t offset;
83 extern struct gl_state_s gl_state;
85 /* Fetch the information from the entry for character C
86 in the current buffer's syntax table,
87 or (if VIA_PROPERTY) from globally kept data (gl_state).
88 Does inheritance. */
90 INLINE Lisp_Object
91 syntax_property_entry (int c, bool via_property)
93 if (via_property)
94 return (gl_state.use_global
95 ? gl_state.global_code
96 : CHAR_TABLE_REF (gl_state.current_syntax_table, c));
97 return CHAR_TABLE_REF (BVAR (current_buffer, syntax_table), c);
99 INLINE Lisp_Object
100 SYNTAX_ENTRY (int c)
102 return syntax_property_entry (c, false);
105 /* Extract the information from the entry for character C
106 in the current syntax table. */
108 INLINE int
109 syntax_property_with_flags (int c, bool via_property)
111 Lisp_Object ent = syntax_property_entry (c, via_property);
112 return CONSP (ent) ? XINT (XCAR (ent)) : Swhitespace;
114 INLINE int
115 SYNTAX_WITH_FLAGS (int c)
117 return syntax_property_with_flags (c, false);
120 INLINE enum syntaxcode
121 syntax_property (int c, bool via_property)
123 return syntax_property_with_flags (c, via_property) & 0xff;
125 INLINE enum syntaxcode
126 SYNTAX (int c)
128 return syntax_property (c, false);
132 /* Whether the syntax of the character C has the prefix flag set. */
133 extern bool syntax_prefix_flag_p (int c);
135 /* This array, indexed by a character less than 256, contains the
136 syntax code which that character signifies (as an unsigned char).
137 For example, syntax_spec_code['w'] == Sword. */
139 extern unsigned char const syntax_spec_code[0400];
141 /* Indexed by syntax code, give the letter that describes it. */
143 extern char const syntax_code_spec[16];
145 /* Convert the byte offset BYTEPOS into a character position,
146 for the object recorded in gl_state with SETUP_SYNTAX_TABLE_FOR_OBJECT.
148 The value is meant for use in code that does nothing when
149 parse_sexp_lookup_properties is false, so return 0 in that case,
150 for speed. */
152 INLINE ptrdiff_t
153 SYNTAX_TABLE_BYTE_TO_CHAR (ptrdiff_t bytepos)
155 return (! parse_sexp_lookup_properties
157 : STRINGP (gl_state.object)
158 ? string_byte_to_char (gl_state.object, bytepos)
159 : BUFFERP (gl_state.object)
160 ? ((buf_bytepos_to_charpos
161 (XBUFFER (gl_state.object),
162 (bytepos + BUF_BEGV_BYTE (XBUFFER (gl_state.object)) - 1)))
163 - BUF_BEGV (XBUFFER (gl_state.object)) + 1)
164 : NILP (gl_state.object)
165 ? BYTE_TO_CHAR (bytepos + BEGV_BYTE - 1) - BEGV + 1
166 : bytepos);
169 /* Make syntax table state (gl_state) good for CHARPOS, assuming it is
170 currently good for a position before CHARPOS. */
172 INLINE void
173 UPDATE_SYNTAX_TABLE_FORWARD (ptrdiff_t charpos)
175 if (parse_sexp_lookup_properties && charpos >= gl_state.e_property)
176 update_syntax_table (charpos + gl_state.offset, 1, false, gl_state.object);
179 /* Make syntax table state (gl_state) good for CHARPOS, assuming it is
180 currently good for a position after CHARPOS. */
182 INLINE void
183 UPDATE_SYNTAX_TABLE_BACKWARD (ptrdiff_t charpos)
185 if (parse_sexp_lookup_properties && charpos < gl_state.b_property)
186 update_syntax_table (charpos + gl_state.offset, -1, false, gl_state.object);
189 /* Make syntax table good for CHARPOS. */
191 INLINE void
192 UPDATE_SYNTAX_TABLE (ptrdiff_t charpos)
194 UPDATE_SYNTAX_TABLE_BACKWARD (charpos);
195 UPDATE_SYNTAX_TABLE_FORWARD (charpos);
198 /* Set up the buffer-global syntax table. */
200 INLINE void
201 SETUP_BUFFER_SYNTAX_TABLE (void)
203 gl_state.use_global = false;
204 gl_state.current_syntax_table = BVAR (current_buffer, syntax_table);
207 extern ptrdiff_t scan_words (ptrdiff_t, EMACS_INT);
208 extern void SETUP_SYNTAX_TABLE_FOR_OBJECT (Lisp_Object, ptrdiff_t, ptrdiff_t);
210 INLINE_HEADER_END