misc changes
[bpt/emacs.git] / src / syntax.h
CommitLineData
9889c728 1/* Declarations having to do with GNU Emacs syntax tables.
95df8112 2
ba318903 3Copyright (C) 1985, 1993-1994, 1997-1998, 2001-2014 Free Software
ab422c4d 4Foundation, Inc.
9889c728
JB
5
6This file is part of GNU Emacs.
7
b9b1cc14 8GNU Emacs is free software: you can redistribute it and/or modify
9889c728 9it under the terms of the GNU General Public License as published by
b9b1cc14
GM
10the Free Software Foundation, either version 3 of the License, or
11(at your option) any later version.
9889c728
JB
12
13GNU Emacs is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
b9b1cc14 19along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
9889c728 20
45b683a1 21INLINE_HEADER_BEGIN
9889c728 22
1fc71008 23extern void update_syntax_table (ptrdiff_t, EMACS_INT, bool, Lisp_Object);
9889c728
JB
24
25/* The standard syntax table is stored where it will automatically
26 be used in all new buffers. */
4b4deea2 27#define Vstandard_syntax_table BVAR (&buffer_defaults, syntax_table)
9889c728 28
e46c910e
RS
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.
9889c728 32
e46c910e 33 The low 8 bits of CODE+FLAGS is a code, as follows: */
9889c728
JB
34
35enum syntaxcode
36 {
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 " */
5eea1c5a 45 Smath, /* for delimiters like $ in Tex. */
9889c728
JB
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 */
c8cdcb16 50 Sinherit, /* use the standard syntax table for this character */
5eea1c5a 51 Scomment_fence, /* Starts/ends comment which is delimited on the
47ab3db5 52 other side by any char with the same syntaxcode. */
5eea1c5a 53 Sstring_fence, /* Starts/ends string which is delimited on the
47ab3db5 54 other side by any char with the same syntaxcode. */
9889c728
JB
55 Smax /* Upper bound on codes that are meaningful */
56 };
57
e0b8ff93 58
45b683a1
PE
59struct gl_state_s
60{
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;
81};
e0b8ff93 82
45b683a1 83extern struct gl_state_s gl_state;
9889c728 84
45b683a1 85/* Fetch the information from the entry for character C
c6cfd910
PE
86 in the current buffer's syntax table,
87 or (if VIA_PROPERTY) from globally kept data (gl_state).
5eea1c5a 88 Does inheritance. */
5eea1c5a 89
00382e8b 90INLINE Lisp_Object
c6cfd910 91syntax_property_entry (int c, bool via_property)
45b683a1 92{
c6cfd910
PE
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));
45b683a1 97 return CHAR_TABLE_REF (BVAR (current_buffer, syntax_table), c);
c6cfd910
PE
98}
99INLINE Lisp_Object
100SYNTAX_ENTRY (int c)
101{
96c06863 102 return syntax_property_entry (c, false);
45b683a1 103}
e0b8ff93 104
e46c910e 105/* Extract the information from the entry for character C
e0b8ff93 106 in the current syntax table. */
c8cdcb16 107
00382e8b 108INLINE int
c6cfd910 109syntax_property_with_flags (int c, bool via_property)
45b683a1 110{
c6cfd910 111 Lisp_Object ent = syntax_property_entry (c, via_property);
45b683a1
PE
112 return CONSP (ent) ? XINT (XCAR (ent)) : Swhitespace;
113}
c6cfd910
PE
114INLINE int
115SYNTAX_WITH_FLAGS (int c)
116{
96c06863 117 return syntax_property_with_flags (c, false);
c6cfd910 118}
45b683a1 119
c6cfd910
PE
120INLINE enum syntaxcode
121syntax_property (int c, bool via_property)
122{
123 return syntax_property_with_flags (c, via_property) & 0xff;
124}
00382e8b 125INLINE enum syntaxcode
45b683a1
PE
126SYNTAX (int c)
127{
96c06863 128 return syntax_property (c, false);
45b683a1 129}
9889c728 130
a306d6f1 131
c5683ceb 132/* Whether the syntax of the character C has the prefix flag set. */
1fc71008 133extern bool syntax_prefix_flag_p (int c);
c0364919 134
1fc71008
PE
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. */
9889c728 138
1fc71008 139extern unsigned char const syntax_spec_code[0400];
9889c728 140
5eea1c5a
RS
141/* Indexed by syntax code, give the letter that describes it. */
142
1fc71008 143extern char const syntax_code_spec[16];
5eea1c5a 144
c292db29 145/* Convert the byte offset BYTEPOS into a character position,
2f16e7fd
RS
146 for the object recorded in gl_state with SETUP_SYNTAX_TABLE_FOR_OBJECT.
147
45b683a1 148 The value is meant for use in code that does nothing when
96c06863
PE
149 parse_sexp_lookup_properties is false, so return 0 in that case,
150 for speed. */
45b683a1 151
00382e8b 152INLINE ptrdiff_t
45b683a1
PE
153SYNTAX_TABLE_BYTE_TO_CHAR (ptrdiff_t bytepos)
154{
155 return (! parse_sexp_lookup_properties
156 ? 0
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);
167}
c292db29 168
f79b4b7e
KH
169/* Make syntax table state (gl_state) good for CHARPOS, assuming it is
170 currently good for a position before CHARPOS. */
5eea1c5a 171
00382e8b 172INLINE void
45b683a1
PE
173UPDATE_SYNTAX_TABLE_FORWARD (ptrdiff_t charpos)
174{
175 if (parse_sexp_lookup_properties && charpos >= gl_state.e_property)
96c06863 176 update_syntax_table (charpos + gl_state.offset, 1, false, gl_state.object);
45b683a1 177}
5eea1c5a 178
f79b4b7e
KH
179/* Make syntax table state (gl_state) good for CHARPOS, assuming it is
180 currently good for a position after CHARPOS. */
5eea1c5a 181
00382e8b 182INLINE void
45b683a1
PE
183UPDATE_SYNTAX_TABLE_BACKWARD (ptrdiff_t charpos)
184{
185 if (parse_sexp_lookup_properties && charpos < gl_state.b_property)
96c06863 186 update_syntax_table (charpos + gl_state.offset, -1, false, gl_state.object);
45b683a1 187}
e2d8d746 188
f79b4b7e 189/* Make syntax table good for CHARPOS. */
e2d8d746 190
00382e8b 191INLINE void
45b683a1
PE
192UPDATE_SYNTAX_TABLE (ptrdiff_t charpos)
193{
194 UPDATE_SYNTAX_TABLE_BACKWARD (charpos);
195 UPDATE_SYNTAX_TABLE_FORWARD (charpos);
196}
197
198/* Set up the buffer-global syntax table. */
5eea1c5a 199
00382e8b 200INLINE void
45b683a1 201SETUP_BUFFER_SYNTAX_TABLE (void)
5eea1c5a 202{
96c06863 203 gl_state.use_global = false;
45b683a1
PE
204 gl_state.current_syntax_table = BVAR (current_buffer, syntax_table);
205}
5eea1c5a 206
d311d28c 207extern ptrdiff_t scan_words (ptrdiff_t, EMACS_INT);
45b683a1
PE
208extern void SETUP_SYNTAX_TABLE_FOR_OBJECT (Lisp_Object, ptrdiff_t, ptrdiff_t);
209
210INLINE_HEADER_END