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