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