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