When calling put-charset-property, ignore errors.
[bpt/emacs.git] / src / casetab.c
CommitLineData
dcfdbac7 1/* GNU Emacs routines to deal with case tables.
3a22ee35 2 Copyright (C) 1993, 1994 Free Software Foundation, Inc.
dcfdbac7
JB
3
4This file is part of GNU Emacs.
5
6GNU Emacs is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
7c938215 8the Free Software Foundation; either version 2, or (at your option)
dcfdbac7
JB
9any later version.
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
18the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
dcfdbac7
JB
20
21/* Written by Howard Gayle. See chartab.c for details. */
22
18160b98 23#include <config.h>
dcfdbac7
JB
24#include "lisp.h"
25#include "buffer.h"
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
7f7fef04 31static void compute_trt_inverse ();
dcfdbac7
JB
32
33DEFUN ("case-table-p", Fcase_table_p, Scase_table_p, 1, 1, 0,
2858a1c1 34 "Return t iff OBJECT is a case table.\n\
dcfdbac7 35See `set-case-table' for more information on these data structures.")
2858a1c1
EN
36 (object)
37 Lisp_Object object;
dcfdbac7 38{
4b3bd052 39 Lisp_Object up, canon, eqv;
dcfdbac7 40
2858a1c1 41 if (! CHAR_TABLE_P (object))
7f7fef04 42 return Qnil;
2858a1c1 43 if (! EQ (XCHAR_TABLE (object)->purpose, Qcase_table))
7f7fef04 44 return Qnil;
dcfdbac7 45
2858a1c1
EN
46 up = XCHAR_TABLE (object)->extras[0];
47 canon = XCHAR_TABLE (object)->extras[1];
48 eqv = XCHAR_TABLE (object)->extras[2];
7f7fef04
RS
49
50 return ((NILP (up) || CHAR_TABLE_P (up))
d427b66a 51 && ((NILP (canon) && NILP (eqv))
7f7fef04
RS
52 || (CHAR_TABLE_P (canon)
53 && (NILP (eqv) || CHAR_TABLE_P (eqv))))
dcfdbac7
JB
54 ? Qt : Qnil);
55}
56
57static Lisp_Object
58check_case_table (obj)
59 Lisp_Object obj;
60{
61 register Lisp_Object tem;
62
d427b66a 63 while (tem = Fcase_table_p (obj), NILP (tem))
b37902c8 64 obj = wrong_type_argument (Qcase_table_p, obj);
dcfdbac7
JB
65 return (obj);
66}
67
68DEFUN ("current-case-table", Fcurrent_case_table, Scurrent_case_table, 0, 0, 0,
69 "Return the case table of the current buffer.")
70 ()
71{
7f7fef04 72 return current_buffer->downcase_table;
dcfdbac7
JB
73}
74
a1de6b4b 75DEFUN ("standard-case-table", Fstandard_case_table, Sstandard_case_table, 0, 0, 0,
dcfdbac7
JB
76 "Return the standard case table.\n\
77This is the one used for new buffers.")
78 ()
79{
7f7fef04 80 return Vascii_downcase_table;
dcfdbac7
JB
81}
82
d9da9451
JB
83static Lisp_Object set_case_table ();
84
dcfdbac7
JB
85DEFUN ("set-case-table", Fset_case_table, Sset_case_table, 1, 1, 0,
86 "Select a new case table for the current buffer.\n\
c7608f45
RS
87A case table is a char-table which maps characters\n\
88to their lower-case equivalents. It also has three \"extra\" slots\n\
89which may be additional char-tables or nil.\n\
7f7fef04 90These slots are called UPCASE, CANONICALIZE and EQUIVALENCES.\n\
dcfdbac7
JB
91UPCASE maps each character to its upper-case equivalent;\n\
92 if lower and upper case characters are in 1-1 correspondence,\n\
93 you may use nil and the upcase table will be deduced from DOWNCASE.\n\
94CANONICALIZE maps each character to a canonical equivalent;\n\
95 any two characters that are related by case-conversion have the same\n\
5a0fd72f
RS
96 canonical equivalent character; it may be nil, in which case it is\n\
97 deduced from DOWNCASE and UPCASE.\n\
dcfdbac7 98EQUIVALENCES is a map that cyclicly permutes each equivalence class\n\
5a0fd72f
RS
99 (of characters with the same canonical equivalent); it may be nil,\n\
100 in which case it is deduced from CANONICALIZE.")
dcfdbac7
JB
101 (table)
102 Lisp_Object table;
103{
d9da9451 104 return set_case_table (table, 0);
dcfdbac7
JB
105}
106
a1de6b4b 107DEFUN ("set-standard-case-table", Fset_standard_case_table, Sset_standard_case_table, 1, 1, 0,
dcfdbac7
JB
108 "Select a new standard case table for new buffers.\n\
109See `set-case-table' for more info on case tables.")
110 (table)
111 Lisp_Object table;
112{
d9da9451 113 return set_case_table (table, 1);
dcfdbac7
JB
114}
115
d9da9451 116static Lisp_Object
dcfdbac7
JB
117set_case_table (table, standard)
118 Lisp_Object table;
119 int standard;
120{
4b3bd052 121 Lisp_Object up, canon, eqv;
dcfdbac7
JB
122
123 check_case_table (table);
124
7f7fef04
RS
125 up = XCHAR_TABLE (table)->extras[0];
126 canon = XCHAR_TABLE (table)->extras[1];
127 eqv = XCHAR_TABLE (table)->extras[2];
dcfdbac7 128
d427b66a 129 if (NILP (up))
dcfdbac7 130 {
7f7fef04 131 up = Fmake_char_table (Qcase_table, Qnil);
4b3bd052 132 compute_trt_inverse (XCHAR_TABLE (table), XCHAR_TABLE (up));
7f7fef04 133 XCHAR_TABLE (table)->extras[0] = up;
dcfdbac7
JB
134 }
135
d427b66a 136 if (NILP (canon))
dcfdbac7
JB
137 {
138 register int i;
7f7fef04 139 Lisp_Object *upvec = XCHAR_TABLE (up)->contents;
4b3bd052 140 Lisp_Object *downvec = XCHAR_TABLE (table)->contents;
dcfdbac7 141
4b3bd052 142 canon = Fmake_char_table (Qcase_table, Qnil);
dcfdbac7
JB
143
144 /* Set up the CANON vector; for each character,
145 this sequence of upcasing and downcasing ought to
146 get the "preferred" lowercase equivalent. */
147 for (i = 0; i < 256; i++)
7f7fef04
RS
148 XCHAR_TABLE (canon)->contents[i] = downvec[upvec[downvec[i]]];
149 XCHAR_TABLE (table)->extras[1] = canon;
5a0fd72f
RS
150 }
151
152 if (NILP (eqv))
153 {
7f7fef04
RS
154 eqv = Fmake_char_table (Qcase_table, Qnil);
155 compute_trt_inverse (XCHAR_TABLE (canon), XCHAR_TABLE (eqv));
4b3bd052 156 XCHAR_TABLE (table)->extras[2] = eqv;
dcfdbac7
JB
157 }
158
159 if (standard)
4b3bd052 160 Vascii_downcase_table = table;
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
172/* Given a translate table TRT, store the inverse mapping into INVERSE.
173 Since TRT is not one-to-one, INVERSE is not a simple mapping.
174 Instead, it divides the space of characters into equivalence classes.
175 All characters in a given class form one circular list, chained through
176 the elements of INVERSE. */
177
7f7fef04 178static void
dcfdbac7 179compute_trt_inverse (trt, inverse)
7f7fef04 180 struct Lisp_Char_Table *trt, *inverse;
dcfdbac7
JB
181{
182 register int i = 0400;
183 register unsigned char c, q;
184
185 while (i--)
7f7fef04 186 inverse->contents[i] = i;
dcfdbac7
JB
187 i = 0400;
188 while (i--)
189 {
7f7fef04 190 if ((q = trt->contents[i]) != (unsigned char) i)
dcfdbac7 191 {
7f7fef04
RS
192 c = inverse->contents[q];
193 inverse->contents[q] = i;
194 inverse->contents[i] = c;
dcfdbac7
JB
195 }
196 }
197}
198\f
199init_casetab_once ()
200{
201 register int i;
7f7fef04
RS
202 Lisp_Object down, up;
203 Qcase_table = intern ("case-table");
204 staticpro (&Qcase_table);
205
206 /* Intern this now in case it isn't already done.
207 Setting this variable twice is harmless.
208 But don't staticpro it here--that is done in alloc.c. */
209 Qchar_table_extra_slots = intern ("char-table-extra-slots");
dcfdbac7 210
7f7fef04
RS
211 /* Now we are ready to set up this property, so we can
212 create char tables. */
4b3bd052 213 Fput (Qcase_table, Qchar_table_extra_slots, make_number (3));
7f7fef04
RS
214
215 down = Fmake_char_table (Qcase_table, Qnil);
216 Vascii_downcase_table = down;
dcfdbac7
JB
217
218 for (i = 0; i < 256; i++)
7f7fef04
RS
219 XCHAR_TABLE (down)->contents[i] = (i >= 'A' && i <= 'Z') ? i + 040 : i;
220
221 XCHAR_TABLE (down)->extras[1] = Fcopy_sequence (down);
dcfdbac7 222
7f7fef04
RS
223 up = Fmake_char_table (Qcase_table, Qnil);
224 XCHAR_TABLE (down)->extras[0] = up;
dcfdbac7
JB
225
226 for (i = 0; i < 256; i++)
7f7fef04 227 XCHAR_TABLE (up)->contents[i]
dcfdbac7
JB
228 = ((i >= 'A' && i <= 'Z')
229 ? i + ('a' - 'A')
230 : ((i >= 'a' && i <= 'z')
231 ? i + ('A' - 'a')
232 : i));
7f7fef04
RS
233
234 XCHAR_TABLE (down)->extras[2] = Fcopy_sequence (up);
dcfdbac7
JB
235}
236
237syms_of_casetab ()
238{
239 Qcase_table_p = intern ("case-table-p");
240 staticpro (&Qcase_table_p);
7f7fef04 241
8f84b1a1 242 staticpro (&Vascii_canon_table);
dcfdbac7 243 staticpro (&Vascii_downcase_table);
8f84b1a1
EN
244 staticpro (&Vascii_eqv_table);
245 staticpro (&Vascii_upcase_table);
dcfdbac7
JB
246
247 defsubr (&Scase_table_p);
248 defsubr (&Scurrent_case_table);
249 defsubr (&Sstandard_case_table);
250 defsubr (&Sset_case_table);
251 defsubr (&Sset_standard_case_table);
dcfdbac7 252}