Fix a race condition diagnosed by gcc -Wsequence-point (Bug#8254).
[bpt/emacs.git] / src / casetab.c
1 /* GNU Emacs routines to deal with case tables.
2 Copyright (C) 1993-1994, 2001-2011 Free Software Foundation, Inc.
3
4 Author: Howard Gayle
5
6 This file is part of GNU Emacs.
7
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.
12
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.
17
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/>. */
20
21 #include <config.h>
22 #include <setjmp.h>
23 #include "lisp.h"
24 #include "buffer.h"
25 #include "character.h"
26
27 Lisp_Object Qcase_table_p, Qcase_table;
28 Lisp_Object Vascii_downcase_table, Vascii_upcase_table;
29 Lisp_Object Vascii_canon_table, Vascii_eqv_table;
30
31 static void set_canon (Lisp_Object case_table, Lisp_Object range, Lisp_Object elt);
32 static void set_identity (Lisp_Object table, Lisp_Object c, Lisp_Object elt);
33 static void shuffle (Lisp_Object table, Lisp_Object c, Lisp_Object elt);
34
35 DEFUN ("case-table-p", Fcase_table_p, Scase_table_p, 1, 1, 0,
36 doc: /* Return t if OBJECT is a case table.
37 See `set-case-table' for more information on these data structures. */)
38 (Lisp_Object object)
39 {
40 Lisp_Object up, canon, eqv;
41
42 if (! CHAR_TABLE_P (object))
43 return Qnil;
44 if (! EQ (XCHAR_TABLE (object)->purpose, Qcase_table))
45 return Qnil;
46
47 up = XCHAR_TABLE (object)->extras[0];
48 canon = XCHAR_TABLE (object)->extras[1];
49 eqv = XCHAR_TABLE (object)->extras[2];
50
51 return ((NILP (up) || CHAR_TABLE_P (up))
52 && ((NILP (canon) && NILP (eqv))
53 || (CHAR_TABLE_P (canon)
54 && (NILP (eqv) || CHAR_TABLE_P (eqv))))
55 ? Qt : Qnil);
56 }
57
58 static Lisp_Object
59 check_case_table (Lisp_Object obj)
60 {
61 CHECK_TYPE (!NILP (Fcase_table_p (obj)), Qcase_table_p, obj);
62 return (obj);
63 }
64
65 DEFUN ("current-case-table", Fcurrent_case_table, Scurrent_case_table, 0, 0, 0,
66 doc: /* Return the case table of the current buffer. */)
67 (void)
68 {
69 return BVAR (current_buffer, downcase_table);
70 }
71
72 DEFUN ("standard-case-table", Fstandard_case_table, Sstandard_case_table, 0, 0, 0,
73 doc: /* Return the standard case table.
74 This is the one used for new buffers. */)
75 (void)
76 {
77 return Vascii_downcase_table;
78 }
79
80 static Lisp_Object set_case_table (Lisp_Object table, int standard);
81
82 DEFUN ("set-case-table", Fset_case_table, Sset_case_table, 1, 1, 0,
83 doc: /* Select a new case table for the current buffer.
84 A case table is a char-table which maps characters
85 to their lower-case equivalents. It also has three \"extra\" slots
86 which may be additional char-tables or nil.
87 These slots are called UPCASE, CANONICALIZE and EQUIVALENCES.
88 UPCASE maps each non-upper-case character to its upper-case equivalent.
89 (The value in UPCASE for an upper-case character is never used.)
90 If lower and upper case characters are in 1-1 correspondence,
91 you may use nil and the upcase table will be deduced from DOWNCASE.
92 CANONICALIZE maps each character to a canonical equivalent;
93 any two characters that are related by case-conversion have the same
94 canonical equivalent character; it may be nil, in which case it is
95 deduced from DOWNCASE and UPCASE.
96 EQUIVALENCES is a map that cyclicly permutes each equivalence class
97 (of characters with the same canonical equivalent); it may be nil,
98 in which case it is deduced from CANONICALIZE. */)
99 (Lisp_Object table)
100 {
101 return set_case_table (table, 0);
102 }
103
104 DEFUN ("set-standard-case-table", Fset_standard_case_table, Sset_standard_case_table, 1, 1, 0,
105 doc: /* Select a new standard case table for new buffers.
106 See `set-case-table' for more info on case tables. */)
107 (Lisp_Object table)
108 {
109 return set_case_table (table, 1);
110 }
111
112 static Lisp_Object
113 set_case_table (Lisp_Object table, int standard)
114 {
115 Lisp_Object up, canon, eqv;
116
117 check_case_table (table);
118
119 up = XCHAR_TABLE (table)->extras[0];
120 canon = XCHAR_TABLE (table)->extras[1];
121 eqv = XCHAR_TABLE (table)->extras[2];
122
123 if (NILP (up))
124 {
125 up = Fmake_char_table (Qcase_table, Qnil);
126 map_char_table (set_identity, Qnil, table, up);
127 map_char_table (shuffle, Qnil, table, up);
128 XCHAR_TABLE (table)->extras[0] = up;
129 }
130
131 if (NILP (canon))
132 {
133 canon = Fmake_char_table (Qcase_table, Qnil);
134 XCHAR_TABLE (table)->extras[1] = canon;
135 map_char_table (set_canon, Qnil, table, table);
136 }
137
138 if (NILP (eqv))
139 {
140 eqv = Fmake_char_table (Qcase_table, Qnil);
141 map_char_table (set_identity, Qnil, canon, eqv);
142 map_char_table (shuffle, Qnil, canon, eqv);
143 XCHAR_TABLE (table)->extras[2] = eqv;
144 }
145
146 /* This is so set_image_of_range_1 in regex.c can find the EQV table. */
147 XCHAR_TABLE (canon)->extras[2] = eqv;
148
149 if (standard)
150 {
151 Vascii_downcase_table = table;
152 Vascii_upcase_table = up;
153 Vascii_canon_table = canon;
154 Vascii_eqv_table = eqv;
155 }
156 else
157 {
158 BVAR (current_buffer, downcase_table) = table;
159 BVAR (current_buffer, upcase_table) = up;
160 BVAR (current_buffer, case_canon_table) = canon;
161 BVAR (current_buffer, case_eqv_table) = eqv;
162 }
163
164 return table;
165 }
166 \f
167 /* The following functions are called in map_char_table. */
168
169 /* Set CANON char-table element for characters in RANGE to a
170 translated ELT by UP and DOWN char-tables. This is done only when
171 ELT is a character. The char-tables CANON, UP, and DOWN are in
172 CASE_TABLE. */
173
174 static void
175 set_canon (Lisp_Object case_table, Lisp_Object range, Lisp_Object elt)
176 {
177 Lisp_Object up = XCHAR_TABLE (case_table)->extras[0];
178 Lisp_Object canon = XCHAR_TABLE (case_table)->extras[1];
179
180 if (NATNUMP (elt))
181 Fset_char_table_range (canon, range, Faref (case_table, Faref (up, elt)));
182 }
183
184 /* Set elements of char-table TABLE for C to C itself. C may be a
185 cons specifying a character range. In that case, set characters in
186 that range to themselves. This is done only when ELT is a
187 character. This is called in map_char_table. */
188
189 static void
190 set_identity (Lisp_Object table, Lisp_Object c, Lisp_Object elt)
191 {
192 if (NATNUMP (elt))
193 {
194 int from, to;
195
196 if (CONSP (c))
197 {
198 from = XINT (XCAR (c));
199 to = XINT (XCDR (c));
200 }
201 else
202 from = to = XINT (c);
203 for (; from <= to; from++)
204 CHAR_TABLE_SET (table, from, make_number (from));
205 }
206 }
207
208 /* Permute the elements of TABLE (which is initially an identity
209 mapping) so that it has one cycle for each equivalence class
210 induced by the translation table on which map_char_table is
211 operated. */
212
213 static void
214 shuffle (Lisp_Object table, Lisp_Object c, Lisp_Object elt)
215 {
216 if (NATNUMP (elt))
217 {
218 int from, to;
219
220 if (CONSP (c))
221 {
222 from = XINT (XCAR (c));
223 to = XINT (XCDR (c));
224 }
225 else
226 from = to = XINT (c);
227
228 for (; from <= to; from++)
229 {
230 Lisp_Object tem = Faref (table, elt);
231 Faset (table, elt, make_number (from));
232 Faset (table, make_number (from), tem);
233 }
234 }
235 }
236 \f
237 void
238 init_casetab_once (void)
239 {
240 register int i;
241 Lisp_Object down, up;
242 Qcase_table = intern_c_string ("case-table");
243 staticpro (&Qcase_table);
244
245 /* Intern this now in case it isn't already done.
246 Setting this variable twice is harmless.
247 But don't staticpro it here--that is done in alloc.c. */
248 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
249
250 /* Now we are ready to set up this property, so we can
251 create char tables. */
252 Fput (Qcase_table, Qchar_table_extra_slots, make_number (3));
253
254 down = Fmake_char_table (Qcase_table, Qnil);
255 Vascii_downcase_table = down;
256 XCHAR_TABLE (down)->purpose = Qcase_table;
257
258 for (i = 0; i < 128; i++)
259 {
260 int c = (i >= 'A' && i <= 'Z') ? i + ('a' - 'A') : i;
261 CHAR_TABLE_SET (down, i, make_number (c));
262 }
263
264 XCHAR_TABLE (down)->extras[1] = Fcopy_sequence (down);
265
266 up = Fmake_char_table (Qcase_table, Qnil);
267 XCHAR_TABLE (down)->extras[0] = up;
268
269 for (i = 0; i < 128; i++)
270 {
271 int c = ((i >= 'A' && i <= 'Z') ? i + ('a' - 'A')
272 : ((i >= 'a' && i <= 'z') ? i + ('A' - 'a')
273 : i));
274 CHAR_TABLE_SET (up, i, make_number (c));
275 }
276
277 XCHAR_TABLE (down)->extras[2] = Fcopy_sequence (up);
278
279 /* Fill in what isn't filled in. */
280 set_case_table (down, 1);
281 }
282
283 void
284 syms_of_casetab (void)
285 {
286 Qcase_table_p = intern_c_string ("case-table-p");
287 staticpro (&Qcase_table_p);
288
289 staticpro (&Vascii_canon_table);
290 staticpro (&Vascii_downcase_table);
291 staticpro (&Vascii_eqv_table);
292 staticpro (&Vascii_upcase_table);
293
294 defsubr (&Scase_table_p);
295 defsubr (&Scurrent_case_table);
296 defsubr (&Sstandard_case_table);
297 defsubr (&Sset_case_table);
298 defsubr (&Sset_standard_case_table);
299 }