]> code.delx.au - gnu-emacs/blob - src/casefiddle.c
(casify_object): Use make_string instead of
[gnu-emacs] / src / casefiddle.c
1 /* GNU Emacs case conversion functions.
2 Copyright (C) 1985, 1994, 1997 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
22 #include <config.h>
23 #include "lisp.h"
24 #include "buffer.h"
25 #include "charset.h"
26 #include "commands.h"
27 #include "syntax.h"
28
29 enum case_action {CASE_UP, CASE_DOWN, CASE_CAPITALIZE, CASE_CAPITALIZE_UP};
30
31 Lisp_Object Qidentity;
32 \f
33 Lisp_Object
34 casify_object (flag, obj)
35 enum case_action flag;
36 Lisp_Object obj;
37 {
38 register int i, c, len;
39 register int inword = flag == CASE_DOWN;
40 Lisp_Object tem;
41
42 /* If the case table is flagged as modified, rescan it. */
43 if (NILP (XCHAR_TABLE (current_buffer->downcase_table)->extras[1]))
44 Fset_case_table (current_buffer->downcase_table);
45
46 while (1)
47 {
48 if (INTEGERP (obj))
49 {
50 int flagbits = (CHAR_ALT | CHAR_SUPER | CHAR_HYPER
51 | CHAR_SHIFT | CHAR_CTL | CHAR_META);
52 int flags = XINT (obj) & flagbits;
53
54 c = DOWNCASE (XFASTINT (obj) & ~flagbits);
55 if (inword)
56 XSETFASTINT (obj, c | flags);
57 else if (c == (XFASTINT (obj) & ~flagbits))
58 {
59 c = UPCASE1 ((XFASTINT (obj) & ~flagbits));
60 XSETFASTINT (obj, c | flags);
61 }
62 return obj;
63 }
64
65 if (STRINGP (obj))
66 {
67 int multibyte = STRING_MULTIBYTE (obj);
68
69 obj = Fcopy_sequence (obj);
70 len = STRING_BYTES (XSTRING (obj));
71
72 /* Scan all single-byte characters from start of string. */
73 for (i = 0; i < len;)
74 {
75 c = XSTRING (obj)->data[i];
76
77 if (multibyte && c >= 0x80)
78 /* A multibyte character can't be handled in this
79 simple loop. */
80 break;
81 if (inword && flag != CASE_CAPITALIZE_UP)
82 c = DOWNCASE (c);
83 else if (!UPPERCASEP (c)
84 && (!inword || flag != CASE_CAPITALIZE_UP))
85 c = UPCASE1 (c);
86 /* If this char won't fit in a single-byte string.
87 fall out to the multibyte case. */
88 if (multibyte ? ! ASCII_BYTE_P (c)
89 : ! SINGLE_BYTE_CHAR_P (c))
90 break;
91
92 XSTRING (obj)->data[i] = c;
93 if ((int) flag >= (int) CASE_CAPITALIZE)
94 inword = SYNTAX (c) == Sword;
95 i++;
96 }
97
98 /* If we didn't do the whole string as single-byte,
99 scan the rest in a more complex way. */
100 if (i < len)
101 {
102 /* The work is not yet finished because of a multibyte
103 character just encountered. */
104 int fromlen, tolen, j_byte = i;
105 char *buf
106 = (char *) alloca ((len - i) * MAX_LENGTH_OF_MULTI_BYTE_FORM
107 + i);
108 unsigned char *str, workbuf[4];
109
110 /* Copy data already handled. */
111 bcopy (XSTRING (obj)->data, buf, i);
112
113 /* From now on, I counts bytes. */
114 while (i < len)
115 {
116 c = STRING_CHAR_AND_LENGTH (XSTRING (obj)->data + i,
117 len - i, fromlen);
118 if (inword && flag != CASE_CAPITALIZE_UP)
119 c = DOWNCASE (c);
120 else if (!UPPERCASEP (c)
121 && (!inword || flag != CASE_CAPITALIZE_UP))
122 c = UPCASE1 (c);
123 tolen = CHAR_STRING (c, workbuf, str);
124 bcopy (str, buf + j_byte, tolen);
125 i += fromlen;
126 j_byte += tolen;
127 if ((int) flag >= (int) CASE_CAPITALIZE)
128 inword = SYNTAX (c) == Sword;
129 }
130 obj = make_string (buf, j_byte);
131 }
132 return obj;
133 }
134 obj = wrong_type_argument (Qchar_or_string_p, obj);
135 }
136 }
137
138 DEFUN ("upcase", Fupcase, Supcase, 1, 1, 0,
139 "Convert argument to upper case and return that.\n\
140 The argument may be a character or string. The result has the same type.\n\
141 The argument object is not altered--the value is a copy.\n\
142 See also `capitalize', `downcase' and `upcase-initials'.")
143 (obj)
144 Lisp_Object obj;
145 {
146 return casify_object (CASE_UP, obj);
147 }
148
149 DEFUN ("downcase", Fdowncase, Sdowncase, 1, 1, 0,
150 "Convert argument to lower case and return that.\n\
151 The argument may be a character or string. The result has the same type.\n\
152 The argument object is not altered--the value is a copy.")
153 (obj)
154 Lisp_Object obj;
155 {
156 return casify_object (CASE_DOWN, obj);
157 }
158
159 DEFUN ("capitalize", Fcapitalize, Scapitalize, 1, 1, 0,
160 "Convert argument to capitalized form and return that.\n\
161 This means that each word's first character is upper case\n\
162 and the rest is lower case.\n\
163 The argument may be a character or string. The result has the same type.\n\
164 The argument object is not altered--the value is a copy.")
165 (obj)
166 Lisp_Object obj;
167 {
168 return casify_object (CASE_CAPITALIZE, obj);
169 }
170
171 /* Like Fcapitalize but change only the initials. */
172
173 DEFUN ("upcase-initials", Fupcase_initials, Supcase_initials, 1, 1, 0,
174 "Convert the initial of each word in the argument to upper case.\n\
175 Do not change the other letters of each word.\n\
176 The argument may be a character or string. The result has the same type.\n\
177 The argument object is not altered--the value is a copy.")
178 (obj)
179 Lisp_Object obj;
180 {
181 return casify_object (CASE_CAPITALIZE_UP, obj);
182 }
183 \f
184 /* flag is CASE_UP, CASE_DOWN or CASE_CAPITALIZE or CASE_CAPITALIZE_UP.
185 b and e specify range of buffer to operate on. */
186
187 void
188 casify_region (flag, b, e)
189 enum case_action flag;
190 Lisp_Object b, e;
191 {
192 register int i;
193 register int c;
194 register int inword = flag == CASE_DOWN;
195 register int multibyte = !NILP (current_buffer->enable_multibyte_characters);
196 int start, end;
197 int start_byte, end_byte;
198 Lisp_Object ch, downch, val;
199
200 if (EQ (b, e))
201 /* Not modifying because nothing marked */
202 return;
203
204 /* If the case table is flagged as modified, rescan it. */
205 if (NILP (XCHAR_TABLE (current_buffer->downcase_table)->extras[1]))
206 Fset_case_table (current_buffer->downcase_table);
207
208 validate_region (&b, &e);
209 start = XFASTINT (b);
210 end = XFASTINT (e);
211 modify_region (current_buffer, start, end);
212 record_change (start, end - start);
213 start_byte = CHAR_TO_BYTE (start);
214 end_byte = CHAR_TO_BYTE (end);
215
216 for (i = start_byte; i < end_byte; i++)
217 {
218 c = FETCH_BYTE (i);
219 if (multibyte && c >= 0x80)
220 /* A multibyte character can't be handled in this simple loop. */
221 break;
222 if (inword && flag != CASE_CAPITALIZE_UP)
223 c = DOWNCASE (c);
224 else if (!UPPERCASEP (c)
225 && (!inword || flag != CASE_CAPITALIZE_UP))
226 c = UPCASE1 (c);
227 FETCH_BYTE (i) = c;
228 if ((int) flag >= (int) CASE_CAPITALIZE)
229 inword = SYNTAX (c) == Sword;
230 }
231 if (i < end_byte)
232 {
233 /* The work is not yet finished because of a multibyte character
234 just encountered. */
235 int opoint = PT;
236 int opoint_byte = PT_BYTE;
237 int c2;
238
239 while (i < end_byte)
240 {
241 if ((c = FETCH_BYTE (i)) >= 0x80)
242 c = FETCH_MULTIBYTE_CHAR (i);
243 c2 = c;
244 if (inword && flag != CASE_CAPITALIZE_UP)
245 c2 = DOWNCASE (c);
246 else if (!UPPERCASEP (c)
247 && (!inword || flag != CASE_CAPITALIZE_UP))
248 c2 = UPCASE1 (c);
249 if (c != c2)
250 {
251 int fromlen, tolen, j;
252 unsigned char workbuf[4], *str;
253
254 /* Handle the most likely case */
255 if (c < 0400 && c2 < 0400)
256 FETCH_BYTE (i) = c2;
257 else if (fromlen = CHAR_STRING (c, workbuf, str),
258 tolen = CHAR_STRING (c2, workbuf, str),
259 fromlen == tolen)
260 {
261 for (j = 0; j < tolen; ++j)
262 FETCH_BYTE (i + j) = str[j];
263 }
264 else
265 {
266 error ("Can't casify letters that change length");
267 #if 0 /* This is approximately what we'd like to be able to do here */
268 if (tolen < fromlen)
269 del_range_1 (i + tolen, i + fromlen, 0);
270 else if (tolen > fromlen)
271 {
272 TEMP_SET_PT (i + fromlen);
273 insert_1 (str + fromlen, tolen - fromlen, 1, 0, 0);
274 }
275 #endif
276 }
277 }
278 if ((int) flag >= (int) CASE_CAPITALIZE)
279 inword = SYNTAX (c2) == Sword;
280 INC_POS (i);
281 }
282 TEMP_SET_PT_BOTH (opoint, opoint_byte);
283 }
284
285 signal_after_change (start, end - start, end - start);
286 }
287
288 DEFUN ("upcase-region", Fupcase_region, Supcase_region, 2, 2, "r",
289 "Convert the region to upper case. In programs, wants two arguments.\n\
290 These arguments specify the starting and ending character numbers of\n\
291 the region to operate on. When used as a command, the text between\n\
292 point and the mark is operated on.\n\
293 See also `capitalize-region'.")
294 (beg, end)
295 Lisp_Object beg, end;
296 {
297 casify_region (CASE_UP, beg, end);
298 return Qnil;
299 }
300
301 DEFUN ("downcase-region", Fdowncase_region, Sdowncase_region, 2, 2, "r",
302 "Convert the region to lower case. In programs, wants two arguments.\n\
303 These arguments specify the starting and ending character numbers of\n\
304 the region to operate on. When used as a command, the text between\n\
305 point and the mark is operated on.")
306 (beg, end)
307 Lisp_Object beg, end;
308 {
309 casify_region (CASE_DOWN, beg, end);
310 return Qnil;
311 }
312
313 DEFUN ("capitalize-region", Fcapitalize_region, Scapitalize_region, 2, 2, "r",
314 "Convert the region to capitalized form.\n\
315 Capitalized form means each word's first character is upper case\n\
316 and the rest of it is lower case.\n\
317 In programs, give two arguments, the starting and ending\n\
318 character positions to operate on.")
319 (beg, end)
320 Lisp_Object beg, end;
321 {
322 casify_region (CASE_CAPITALIZE, beg, end);
323 return Qnil;
324 }
325
326 /* Like Fcapitalize_region but change only the initials. */
327
328 DEFUN ("upcase-initials-region", Fupcase_initials_region,
329 Supcase_initials_region, 2, 2, "r",
330 "Upcase the initial of each word in the region.\n\
331 Subsequent letters of each word are not changed.\n\
332 In programs, give two arguments, the starting and ending\n\
333 character positions to operate on.")
334 (beg, end)
335 Lisp_Object beg, end;
336 {
337 casify_region (CASE_CAPITALIZE_UP, beg, end);
338 return Qnil;
339 }
340 \f
341 Lisp_Object
342 operate_on_word (arg, newpoint)
343 Lisp_Object arg;
344 int *newpoint;
345 {
346 Lisp_Object val;
347 int farend;
348 int iarg;
349
350 CHECK_NUMBER (arg, 0);
351 iarg = XINT (arg);
352 farend = scan_words (PT, iarg);
353 if (!farend)
354 farend = iarg > 0 ? ZV : BEGV;
355
356 *newpoint = PT > farend ? PT : farend;
357 XSETFASTINT (val, farend);
358
359 return val;
360 }
361
362 DEFUN ("upcase-word", Fupcase_word, Supcase_word, 1, 1, "p",
363 "Convert following word (or ARG words) to upper case, moving over.\n\
364 With negative argument, convert previous words but do not move.\n\
365 See also `capitalize-word'.")
366 (arg)
367 Lisp_Object arg;
368 {
369 Lisp_Object beg, end;
370 int newpoint;
371 XSETFASTINT (beg, PT);
372 end = operate_on_word (arg, &newpoint);
373 casify_region (CASE_UP, beg, end);
374 SET_PT (newpoint);
375 return Qnil;
376 }
377
378 DEFUN ("downcase-word", Fdowncase_word, Sdowncase_word, 1, 1, "p",
379 "Convert following word (or ARG words) to lower case, moving over.\n\
380 With negative argument, convert previous words but do not move.")
381 (arg)
382 Lisp_Object arg;
383 {
384 Lisp_Object beg, end;
385 int newpoint;
386 XSETFASTINT (beg, PT);
387 end = operate_on_word (arg, &newpoint);
388 casify_region (CASE_DOWN, beg, end);
389 SET_PT (newpoint);
390 return Qnil;
391 }
392
393 DEFUN ("capitalize-word", Fcapitalize_word, Scapitalize_word, 1, 1, "p",
394 "Capitalize the following word (or ARG words), moving over.\n\
395 This gives the word(s) a first character in upper case\n\
396 and the rest lower case.\n\
397 With negative argument, capitalize previous words but do not move.")
398 (arg)
399 Lisp_Object arg;
400 {
401 Lisp_Object beg, end;
402 int newpoint;
403 XSETFASTINT (beg, PT);
404 end = operate_on_word (arg, &newpoint);
405 casify_region (CASE_CAPITALIZE, beg, end);
406 SET_PT (newpoint);
407 return Qnil;
408 }
409 \f
410 void
411 syms_of_casefiddle ()
412 {
413 Qidentity = intern ("identity");
414 staticpro (&Qidentity);
415 defsubr (&Supcase);
416 defsubr (&Sdowncase);
417 defsubr (&Scapitalize);
418 defsubr (&Supcase_initials);
419 defsubr (&Supcase_region);
420 defsubr (&Sdowncase_region);
421 defsubr (&Scapitalize_region);
422 defsubr (&Supcase_initials_region);
423 defsubr (&Supcase_word);
424 defsubr (&Sdowncase_word);
425 defsubr (&Scapitalize_word);
426 }
427
428 void
429 keys_of_casefiddle ()
430 {
431 initial_define_key (control_x_map, Ctl('U'), "upcase-region");
432 Fput (intern ("upcase-region"), Qdisabled, Qt);
433 initial_define_key (control_x_map, Ctl('L'), "downcase-region");
434 Fput (intern ("downcase-region"), Qdisabled, Qt);
435
436 initial_define_key (meta_map, 'u', "upcase-word");
437 initial_define_key (meta_map, 'l', "downcase-word");
438 initial_define_key (meta_map, 'c', "capitalize-word");
439 }