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