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)
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. */
29 enum case_action
{CASE_UP
, CASE_DOWN
, CASE_CAPITALIZE
, CASE_CAPITALIZE_UP
};
31 Lisp_Object Qidentity
;
34 casify_object (flag
, obj
)
35 enum case_action flag
;
38 register int i
, c
, len
;
39 register int inword
= flag
== CASE_DOWN
;
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
);
50 c
= DOWNCASE (XFASTINT (obj
));
53 else if (c
== XFASTINT (obj
))
55 c
= UPCASE1 (XFASTINT (obj
));
63 int multibyte
= STRING_MULTIBYTE (obj
);
65 obj
= Fcopy_sequence (obj
);
66 len
= STRING_BYTES (XSTRING (obj
));
68 /* Scan all single-byte characters from start of string. */
71 c
= XSTRING (obj
)->data
[i
];
73 if (multibyte
&& c
>= 0x80)
74 /* A multibyte character can't be handled in this
77 if (inword
&& flag
!= CASE_CAPITALIZE_UP
)
79 else if (!UPPERCASEP (c
)
80 && (!inword
|| flag
!= CASE_CAPITALIZE_UP
))
82 /* If this char won't fit in a single-byte string.
83 fall out to the multibyte case. */
84 if (multibyte
? ! ASCII_BYTE_P (c
)
85 : ! SINGLE_BYTE_CHAR_P (c
))
88 XSTRING (obj
)->data
[i
] = c
;
89 if ((int) flag
>= (int) CASE_CAPITALIZE
)
90 inword
= SYNTAX (c
) == Sword
;
94 /* If we didn't do the whole string as single-byte,
95 scan the rest in a more complex way. */
98 /* The work is not yet finished because of a multibyte
99 character just encountered. */
100 int fromlen
, tolen
, j
= i
, j_byte
= i
;
102 = (char *) alloca ((len
- i
) * MAX_LENGTH_OF_MULTI_BYTE_FORM
104 unsigned char *str
, workbuf
[4];
106 /* Copy data already handled. */
107 bcopy (XSTRING (obj
)->data
, buf
, i
);
109 /* From now on, I counts bytes. */
112 c
= STRING_CHAR_AND_LENGTH (XSTRING (obj
)->data
+ i
,
114 if (inword
&& flag
!= CASE_CAPITALIZE_UP
)
116 else if (!UPPERCASEP (c
)
117 && (!inword
|| flag
!= CASE_CAPITALIZE_UP
))
119 tolen
= CHAR_STRING (c
, workbuf
, str
);
120 bcopy (str
, buf
+ j_byte
, tolen
);
124 if ((int) flag
>= (int) CASE_CAPITALIZE
)
125 inword
= SYNTAX (c
) == Sword
;
127 obj
= make_specified_string (buf
, j
, j_byte
,
128 STRING_MULTIBYTE (obj
));
132 obj
= wrong_type_argument (Qchar_or_string_p
, obj
);
136 DEFUN ("upcase", Fupcase
, Supcase
, 1, 1, 0,
137 "Convert argument to upper case and return that.\n\
138 The argument may be a character or string. The result has the same type.\n\
139 The argument object is not altered--the value is a copy.\n\
140 See also `capitalize', `downcase' and `upcase-initials'.")
144 return casify_object (CASE_UP
, obj
);
147 DEFUN ("downcase", Fdowncase
, Sdowncase
, 1, 1, 0,
148 "Convert argument to lower case and return that.\n\
149 The argument may be a character or string. The result has the same type.\n\
150 The argument object is not altered--the value is a copy.")
154 return casify_object (CASE_DOWN
, obj
);
157 DEFUN ("capitalize", Fcapitalize
, Scapitalize
, 1, 1, 0,
158 "Convert argument to capitalized form and return that.\n\
159 This means that each word's first character is upper case\n\
160 and the rest is lower case.\n\
161 The argument may be a character or string. The result has the same type.\n\
162 The argument object is not altered--the value is a copy.")
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 "Convert the initial of each word in the argument to upper case.\n\
173 Do not change the other letters of each word.\n\
174 The argument may be a character or string. The result has the same type.\n\
175 The argument object is not altered--the value is a copy.")
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 casify_region (flag
, b
, e
)
186 enum case_action flag
;
191 register int inword
= flag
== CASE_DOWN
;
192 register int multibyte
= !NILP (current_buffer
->enable_multibyte_characters
);
194 int start_byte
, end_byte
;
195 Lisp_Object ch
, downch
, val
;
198 /* Not modifying because nothing marked */
201 /* If the case table is flagged as modified, rescan it. */
202 if (NILP (XCHAR_TABLE (current_buffer
->downcase_table
)->extras
[1]))
203 Fset_case_table (current_buffer
->downcase_table
);
205 validate_region (&b
, &e
);
206 start
= XFASTINT (b
);
208 modify_region (current_buffer
, start
, end
);
209 record_change (start
, end
- start
);
210 start_byte
= CHAR_TO_BYTE (start
);
211 end_byte
= CHAR_TO_BYTE (end
);
213 for (i
= start_byte
; i
< end_byte
; i
++)
216 if (multibyte
&& c
>= 0x80)
217 /* A multibyte character can't be handled in this simple loop. */
219 if (inword
&& flag
!= CASE_CAPITALIZE_UP
)
221 else if (!UPPERCASEP (c
)
222 && (!inword
|| flag
!= CASE_CAPITALIZE_UP
))
225 if ((int) flag
>= (int) CASE_CAPITALIZE
)
226 inword
= SYNTAX (c
) == Sword
;
230 /* The work is not yet finished because of a multibyte character
233 int opoint_byte
= PT_BYTE
;
238 if ((c
= FETCH_BYTE (i
)) >= 0x80)
239 c
= FETCH_MULTIBYTE_CHAR (i
);
241 if (inword
&& flag
!= CASE_CAPITALIZE_UP
)
243 else if (!UPPERCASEP (c
)
244 && (!inword
|| flag
!= CASE_CAPITALIZE_UP
))
248 int fromlen
, tolen
, j
;
249 unsigned char workbuf
[4], *str
;
251 /* Handle the most likely case */
252 if (c
< 0400 && c2
< 0400)
254 else if (fromlen
= CHAR_STRING (c
, workbuf
, str
),
255 tolen
= CHAR_STRING (c2
, workbuf
, str
),
258 for (j
= 0; j
< tolen
; ++j
)
259 FETCH_BYTE (i
+ j
) = str
[j
];
263 error ("Can't casify letters that change length");
264 #if 0 /* This is approximately what we'd like to be able to do here */
266 del_range_1 (i
+ tolen
, i
+ fromlen
, 0);
267 else if (tolen
> fromlen
)
269 TEMP_SET_PT (i
+ fromlen
);
270 insert_1 (str
+ fromlen
, tolen
- fromlen
, 1, 0, 0);
275 if ((int) flag
>= (int) CASE_CAPITALIZE
)
276 inword
= SYNTAX (c2
) == Sword
;
279 TEMP_SET_PT_BOTH (opoint
, opoint_byte
);
282 signal_after_change (start
, end
- start
, end
- start
);
285 DEFUN ("upcase-region", Fupcase_region
, Supcase_region
, 2, 2, "r",
286 "Convert the region to upper case. In programs, wants two arguments.\n\
287 These arguments specify the starting and ending character numbers of\n\
288 the region to operate on. When used as a command, the text between\n\
289 point and the mark is operated on.\n\
290 See also `capitalize-region'.")
292 Lisp_Object beg
, end
;
294 casify_region (CASE_UP
, beg
, end
);
298 DEFUN ("downcase-region", Fdowncase_region
, Sdowncase_region
, 2, 2, "r",
299 "Convert the region to lower case. In programs, wants two arguments.\n\
300 These arguments specify the starting and ending character numbers of\n\
301 the region to operate on. When used as a command, the text between\n\
302 point and the mark is operated on.")
304 Lisp_Object beg
, end
;
306 casify_region (CASE_DOWN
, beg
, end
);
310 DEFUN ("capitalize-region", Fcapitalize_region
, Scapitalize_region
, 2, 2, "r",
311 "Convert the region to capitalized form.\n\
312 Capitalized form means each word's first character is upper case\n\
313 and the rest of it is lower case.\n\
314 In programs, give two arguments, the starting and ending\n\
315 character positions to operate on.")
317 Lisp_Object beg
, end
;
319 casify_region (CASE_CAPITALIZE
, beg
, end
);
323 /* Like Fcapitalize_region but change only the initials. */
325 DEFUN ("upcase-initials-region", Fupcase_initials_region
,
326 Supcase_initials_region
, 2, 2, "r",
327 "Upcase the initial of each word in the region.\n\
328 Subsequent letters of each word are not changed.\n\
329 In programs, give two arguments, the starting and ending\n\
330 character positions to operate on.")
332 Lisp_Object beg
, end
;
334 casify_region (CASE_CAPITALIZE_UP
, beg
, end
);
339 operate_on_word (arg
, newpoint
)
347 CHECK_NUMBER (arg
, 0);
349 farend
= scan_words (PT
, iarg
);
351 farend
= iarg
> 0 ? ZV
: BEGV
;
353 *newpoint
= PT
> farend
? PT
: farend
;
354 XSETFASTINT (val
, farend
);
359 DEFUN ("upcase-word", Fupcase_word
, Supcase_word
, 1, 1, "p",
360 "Convert following word (or ARG words) to upper case, moving over.\n\
361 With negative argument, convert previous words but do not move.\n\
362 See also `capitalize-word'.")
366 Lisp_Object beg
, end
;
368 XSETFASTINT (beg
, PT
);
369 end
= operate_on_word (arg
, &newpoint
);
370 casify_region (CASE_UP
, beg
, end
);
375 DEFUN ("downcase-word", Fdowncase_word
, Sdowncase_word
, 1, 1, "p",
376 "Convert following word (or ARG words) to lower case, moving over.\n\
377 With negative argument, convert previous words but do not move.")
381 Lisp_Object beg
, end
;
383 XSETFASTINT (beg
, PT
);
384 end
= operate_on_word (arg
, &newpoint
);
385 casify_region (CASE_DOWN
, beg
, end
);
390 DEFUN ("capitalize-word", Fcapitalize_word
, Scapitalize_word
, 1, 1, "p",
391 "Capitalize the following word (or ARG words), moving over.\n\
392 This gives the word(s) a first character in upper case\n\
393 and the rest lower case.\n\
394 With negative argument, capitalize previous words but do not move.")
398 Lisp_Object beg
, end
;
400 XSETFASTINT (beg
, PT
);
401 end
= operate_on_word (arg
, &newpoint
);
402 casify_region (CASE_CAPITALIZE
, beg
, end
);
407 syms_of_casefiddle ()
409 Qidentity
= intern ("identity");
410 staticpro (&Qidentity
);
412 defsubr (&Sdowncase
);
413 defsubr (&Scapitalize
);
414 defsubr (&Supcase_initials
);
415 defsubr (&Supcase_region
);
416 defsubr (&Sdowncase_region
);
417 defsubr (&Scapitalize_region
);
418 defsubr (&Supcase_initials_region
);
419 defsubr (&Supcase_word
);
420 defsubr (&Sdowncase_word
);
421 defsubr (&Scapitalize_word
);
424 keys_of_casefiddle ()
426 initial_define_key (control_x_map
, Ctl('U'), "upcase-region");
427 Fput (intern ("upcase-region"), Qdisabled
, Qt
);
428 initial_define_key (control_x_map
, Ctl('L'), "downcase-region");
429 Fput (intern ("downcase-region"), Qdisabled
, Qt
);
431 initial_define_key (meta_map
, 'u', "upcase-word");
432 initial_define_key (meta_map
, 'l', "downcase-word");
433 initial_define_key (meta_map
, 'c', "capitalize-word");