Mercurial > emacs
annotate src/casefiddle.c @ 9052:6de22822cf72
(upcase_initials): New function.
(casify_object): Handle CASE_CAPITALIZE_UP.
| author | Richard M. Stallman <rms@gnu.org> |
|---|---|
| date | Sat, 24 Sep 1994 01:57:54 +0000 |
| parents | cd81dba38a49 |
| children | 4887fc1a2dda |
| rev | line source |
|---|---|
| 118 | 1 /* GNU Emacs case conversion functions. |
| 7307 | 2 Copyright (C) 1985, 1994 Free Software Foundation, Inc. |
| 118 | 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 1, 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, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
| 19 | |
| 20 | |
|
4696
1fc792473491
Include <config.h> instead of "config.h".
Roland McGrath <roland@gnu.org>
parents:
2822
diff
changeset
|
21 #include <config.h> |
| 118 | 22 #include "lisp.h" |
| 23 #include "buffer.h" | |
| 24 #include "commands.h" | |
| 25 #include "syntax.h" | |
| 26 | |
| 27 enum case_action {CASE_UP, CASE_DOWN, CASE_CAPITALIZE, CASE_CAPITALIZE_UP}; | |
| 28 | |
| 29 Lisp_Object | |
| 30 casify_object (flag, obj) | |
| 31 enum case_action flag; | |
| 32 Lisp_Object obj; | |
| 33 { | |
| 34 register int i, c, len; | |
| 35 register int inword = flag == CASE_DOWN; | |
| 36 | |
| 37 while (1) | |
| 38 { | |
| 39 if (XTYPE (obj) == Lisp_Int) | |
| 40 { | |
| 41 c = XINT (obj); | |
| 42 if (c >= 0 && c <= 0400) | |
| 43 { | |
| 44 if (inword) | |
| 45 XFASTINT (obj) = DOWNCASE (c); | |
| 46 else if (!UPPERCASEP (c)) | |
| 47 XFASTINT (obj) = UPCASE1 (c); | |
| 48 } | |
| 49 return obj; | |
| 50 } | |
| 51 if (XTYPE (obj) == Lisp_String) | |
| 52 { | |
| 53 obj = Fcopy_sequence (obj); | |
| 54 len = XSTRING (obj)->size; | |
| 55 for (i = 0; i < len; i++) | |
| 56 { | |
| 57 c = XSTRING (obj)->data[i]; | |
|
9052
6de22822cf72
(upcase_initials): New function.
Richard M. Stallman <rms@gnu.org>
parents:
7307
diff
changeset
|
58 if (inword && flag != CASE_CAPITALIZE_UP) |
| 118 | 59 c = DOWNCASE (c); |
|
9052
6de22822cf72
(upcase_initials): New function.
Richard M. Stallman <rms@gnu.org>
parents:
7307
diff
changeset
|
60 else if (!UPPERCASEP (c) |
|
6de22822cf72
(upcase_initials): New function.
Richard M. Stallman <rms@gnu.org>
parents:
7307
diff
changeset
|
61 && (!inword || flag != CASE_CAPITALIZE_UP)) |
| 118 | 62 c = UPCASE1 (c); |
| 63 XSTRING (obj)->data[i] = c; | |
|
9052
6de22822cf72
(upcase_initials): New function.
Richard M. Stallman <rms@gnu.org>
parents:
7307
diff
changeset
|
64 if ((int) flag >= (int) CASE_CAPITALIZE) |
| 118 | 65 inword = SYNTAX (c) == Sword; |
| 66 } | |
| 67 return obj; | |
| 68 } | |
|
1926
952f2a18f83d
* callint.c (Fcall_interactively): Pass the correct number of
Jim Blandy <jimb@redhat.com>
parents:
1505
diff
changeset
|
69 obj = wrong_type_argument (Qchar_or_string_p, obj); |
| 118 | 70 } |
| 71 } | |
| 72 | |
| 73 DEFUN ("upcase", Fupcase, Supcase, 1, 1, 0, | |
| 74 "Convert argument to upper case and return that.\n\ | |
| 75 The argument may be a character or string. The result has the same type.\n\ | |
| 76 The argument object is not altered. See also `capitalize'.") | |
| 77 (obj) | |
| 78 Lisp_Object obj; | |
| 79 { | |
| 80 return casify_object (CASE_UP, obj); | |
| 81 } | |
| 82 | |
| 83 DEFUN ("downcase", Fdowncase, Sdowncase, 1, 1, 0, | |
| 84 "Convert argument to lower case and return that.\n\ | |
| 85 The argument may be a character or string. The result has the same type.\n\ | |
| 86 The argument object is not altered.") | |
| 87 (obj) | |
| 88 Lisp_Object obj; | |
| 89 { | |
| 90 return casify_object (CASE_DOWN, obj); | |
| 91 } | |
| 92 | |
| 93 DEFUN ("capitalize", Fcapitalize, Scapitalize, 1, 1, 0, | |
| 94 "Convert argument to capitalized form and return that.\n\ | |
| 95 This means that each word's first character is upper case\n\ | |
| 96 and the rest is lower case.\n\ | |
| 97 The argument may be a character or string. The result has the same type.\n\ | |
| 98 The argument object is not altered.") | |
| 99 (obj) | |
| 100 Lisp_Object obj; | |
| 101 { | |
| 102 return casify_object (CASE_CAPITALIZE, obj); | |
| 103 } | |
|
9052
6de22822cf72
(upcase_initials): New function.
Richard M. Stallman <rms@gnu.org>
parents:
7307
diff
changeset
|
104 |
|
6de22822cf72
(upcase_initials): New function.
Richard M. Stallman <rms@gnu.org>
parents:
7307
diff
changeset
|
105 /* Like Fcapitalize but change only the initials. */ |
|
6de22822cf72
(upcase_initials): New function.
Richard M. Stallman <rms@gnu.org>
parents:
7307
diff
changeset
|
106 |
|
6de22822cf72
(upcase_initials): New function.
Richard M. Stallman <rms@gnu.org>
parents:
7307
diff
changeset
|
107 Lisp_Object |
|
6de22822cf72
(upcase_initials): New function.
Richard M. Stallman <rms@gnu.org>
parents:
7307
diff
changeset
|
108 upcase_initials (obj) |
|
6de22822cf72
(upcase_initials): New function.
Richard M. Stallman <rms@gnu.org>
parents:
7307
diff
changeset
|
109 Lisp_Object obj; |
|
6de22822cf72
(upcase_initials): New function.
Richard M. Stallman <rms@gnu.org>
parents:
7307
diff
changeset
|
110 { |
|
6de22822cf72
(upcase_initials): New function.
Richard M. Stallman <rms@gnu.org>
parents:
7307
diff
changeset
|
111 return casify_object (CASE_CAPITALIZE_UP, obj); |
|
6de22822cf72
(upcase_initials): New function.
Richard M. Stallman <rms@gnu.org>
parents:
7307
diff
changeset
|
112 } |
| 118 | 113 |
| 114 /* flag is CASE_UP, CASE_DOWN or CASE_CAPITALIZE or CASE_CAPITALIZE_UP. | |
| 115 b and e specify range of buffer to operate on. */ | |
| 116 | |
| 117 casify_region (flag, b, e) | |
| 118 enum case_action flag; | |
| 119 Lisp_Object b, e; | |
| 120 { | |
| 121 register int i; | |
| 122 register int c; | |
| 123 register int inword = flag == CASE_DOWN; | |
| 124 | |
| 125 if (EQ (b, e)) | |
| 126 /* Not modifying because nothing marked */ | |
| 127 return; | |
| 128 | |
| 129 validate_region (&b, &e); | |
|
2783
789c11177579
The text property routines can now modify buffers other
Jim Blandy <jimb@redhat.com>
parents:
1926
diff
changeset
|
130 modify_region (current_buffer, XFASTINT (b), XFASTINT (e)); |
|
2822
153a269b1315
(casify_region): Remove mistaken arg to record_change.
Richard M. Stallman <rms@gnu.org>
parents:
2783
diff
changeset
|
131 record_change (XFASTINT (b), XFASTINT (e) - XFASTINT (b)); |
| 118 | 132 |
| 133 for (i = XFASTINT (b); i < XFASTINT (e); i++) | |
| 134 { | |
| 135 c = FETCH_CHAR (i); | |
| 136 if (inword && flag != CASE_CAPITALIZE_UP) | |
| 137 c = DOWNCASE (c); | |
| 138 else if (!UPPERCASEP (c) | |
| 139 && (!inword || flag != CASE_CAPITALIZE_UP)) | |
| 140 c = UPCASE1 (c); | |
| 141 FETCH_CHAR (i) = c; | |
| 142 if ((int) flag >= (int) CASE_CAPITALIZE) | |
| 143 inword = SYNTAX (c) == Sword; | |
| 144 } | |
| 145 | |
| 146 signal_after_change (XFASTINT (b), | |
| 147 XFASTINT (e) - XFASTINT (b), | |
| 148 XFASTINT (e) - XFASTINT (b)); | |
| 149 } | |
| 150 | |
| 151 DEFUN ("upcase-region", Fupcase_region, Supcase_region, 2, 2, "r", | |
| 152 "Convert the region to upper case. In programs, wants two arguments.\n\ | |
| 153 These arguments specify the starting and ending character numbers of\n\ | |
| 154 the region to operate on. When used as a command, the text between\n\ | |
| 155 point and the mark is operated on.\n\ | |
| 156 See also `capitalize-region'.") | |
| 157 (b, e) | |
| 158 Lisp_Object b, e; | |
| 159 { | |
| 160 casify_region (CASE_UP, b, e); | |
| 161 return Qnil; | |
| 162 } | |
| 163 | |
| 164 DEFUN ("downcase-region", Fdowncase_region, Sdowncase_region, 2, 2, "r", | |
| 165 "Convert the region to lower case. In programs, wants two arguments.\n\ | |
| 166 These arguments specify the starting and ending character numbers of\n\ | |
| 167 the region to operate on. When used as a command, the text between\n\ | |
| 168 point and the mark is operated on.") | |
| 169 (b, e) | |
| 170 Lisp_Object b, e; | |
| 171 { | |
| 172 casify_region (CASE_DOWN, b, e); | |
| 173 return Qnil; | |
| 174 } | |
| 175 | |
| 176 DEFUN ("capitalize-region", Fcapitalize_region, Scapitalize_region, 2, 2, "r", | |
| 177 "Convert the region to capitalized form.\n\ | |
| 178 Capitalized form means each word's first character is upper case\n\ | |
| 179 and the rest of it is lower case.\n\ | |
| 180 In programs, give two arguments, the starting and ending\n\ | |
| 181 character positions to operate on.") | |
| 182 (b, e) | |
| 183 Lisp_Object b, e; | |
| 184 { | |
| 185 casify_region (CASE_CAPITALIZE, b, e); | |
| 186 return Qnil; | |
| 187 } | |
| 188 | |
|
9052
6de22822cf72
(upcase_initials): New function.
Richard M. Stallman <rms@gnu.org>
parents:
7307
diff
changeset
|
189 /* Like Fcapitalize_region but change only the initials. */ |
| 118 | 190 |
| 191 Lisp_Object | |
| 192 upcase_initials_region (b, e) | |
| 193 Lisp_Object b, e; | |
| 194 { | |
| 195 casify_region (CASE_CAPITALIZE_UP, b, e); | |
| 196 return Qnil; | |
| 197 } | |
| 198 | |
| 199 Lisp_Object | |
|
6221
c2d29681d218
(operate_on_word): Don't move point; store in *NEWPOINT.
Richard M. Stallman <rms@gnu.org>
parents:
4696
diff
changeset
|
200 operate_on_word (arg, newpoint) |
| 118 | 201 Lisp_Object arg; |
|
6221
c2d29681d218
(operate_on_word): Don't move point; store in *NEWPOINT.
Richard M. Stallman <rms@gnu.org>
parents:
4696
diff
changeset
|
202 int *newpoint; |
| 118 | 203 { |
|
1505
4f138b03e5ab
* casefiddle.c (operate_on_word): Declare end to be an int, not a
Jim Blandy <jimb@redhat.com>
parents:
484
diff
changeset
|
204 Lisp_Object val; |
|
6221
c2d29681d218
(operate_on_word): Don't move point; store in *NEWPOINT.
Richard M. Stallman <rms@gnu.org>
parents:
4696
diff
changeset
|
205 int farend; |
| 118 | 206 |
| 207 CHECK_NUMBER (arg, 0); | |
| 208 farend = scan_words (point, XINT (arg)); | |
| 209 if (!farend) | |
| 210 farend = XINT (arg) > 0 ? ZV : BEGV; | |
| 211 | |
|
6221
c2d29681d218
(operate_on_word): Don't move point; store in *NEWPOINT.
Richard M. Stallman <rms@gnu.org>
parents:
4696
diff
changeset
|
212 *newpoint = point > farend ? point : farend; |
| 118 | 213 XFASTINT (val) = farend; |
| 214 | |
| 215 return val; | |
| 216 } | |
| 217 | |
| 218 DEFUN ("upcase-word", Fupcase_word, Supcase_word, 1, 1, "p", | |
| 219 "Convert following word (or ARG words) to upper case, moving over.\n\ | |
| 220 With negative argument, convert previous words but do not move.\n\ | |
| 221 See also `capitalize-word'.") | |
| 222 (arg) | |
| 223 Lisp_Object arg; | |
| 224 { | |
|
6221
c2d29681d218
(operate_on_word): Don't move point; store in *NEWPOINT.
Richard M. Stallman <rms@gnu.org>
parents:
4696
diff
changeset
|
225 Lisp_Object beg, end; |
|
c2d29681d218
(operate_on_word): Don't move point; store in *NEWPOINT.
Richard M. Stallman <rms@gnu.org>
parents:
4696
diff
changeset
|
226 int newpoint; |
|
c2d29681d218
(operate_on_word): Don't move point; store in *NEWPOINT.
Richard M. Stallman <rms@gnu.org>
parents:
4696
diff
changeset
|
227 XFASTINT (beg) = point; |
|
c2d29681d218
(operate_on_word): Don't move point; store in *NEWPOINT.
Richard M. Stallman <rms@gnu.org>
parents:
4696
diff
changeset
|
228 end = operate_on_word (arg, &newpoint); |
|
c2d29681d218
(operate_on_word): Don't move point; store in *NEWPOINT.
Richard M. Stallman <rms@gnu.org>
parents:
4696
diff
changeset
|
229 casify_region (CASE_UP, beg, end); |
|
c2d29681d218
(operate_on_word): Don't move point; store in *NEWPOINT.
Richard M. Stallman <rms@gnu.org>
parents:
4696
diff
changeset
|
230 SET_PT (newpoint); |
| 118 | 231 return Qnil; |
| 232 } | |
| 233 | |
| 234 DEFUN ("downcase-word", Fdowncase_word, Sdowncase_word, 1, 1, "p", | |
| 235 "Convert following word (or ARG words) to lower case, moving over.\n\ | |
| 236 With negative argument, convert previous words but do not move.") | |
| 237 (arg) | |
| 238 Lisp_Object arg; | |
| 239 { | |
|
6221
c2d29681d218
(operate_on_word): Don't move point; store in *NEWPOINT.
Richard M. Stallman <rms@gnu.org>
parents:
4696
diff
changeset
|
240 Lisp_Object beg, end; |
|
c2d29681d218
(operate_on_word): Don't move point; store in *NEWPOINT.
Richard M. Stallman <rms@gnu.org>
parents:
4696
diff
changeset
|
241 int newpoint; |
|
c2d29681d218
(operate_on_word): Don't move point; store in *NEWPOINT.
Richard M. Stallman <rms@gnu.org>
parents:
4696
diff
changeset
|
242 XFASTINT (beg) = point; |
|
c2d29681d218
(operate_on_word): Don't move point; store in *NEWPOINT.
Richard M. Stallman <rms@gnu.org>
parents:
4696
diff
changeset
|
243 end = operate_on_word (arg, &newpoint); |
|
c2d29681d218
(operate_on_word): Don't move point; store in *NEWPOINT.
Richard M. Stallman <rms@gnu.org>
parents:
4696
diff
changeset
|
244 casify_region (CASE_DOWN, beg, end); |
|
c2d29681d218
(operate_on_word): Don't move point; store in *NEWPOINT.
Richard M. Stallman <rms@gnu.org>
parents:
4696
diff
changeset
|
245 SET_PT (newpoint); |
| 118 | 246 return Qnil; |
| 247 } | |
| 248 | |
| 249 DEFUN ("capitalize-word", Fcapitalize_word, Scapitalize_word, 1, 1, "p", | |
| 250 "Capitalize the following word (or ARG words), moving over.\n\ | |
| 251 This gives the word(s) a first character in upper case\n\ | |
| 252 and the rest lower case.\n\ | |
| 253 With negative argument, capitalize previous words but do not move.") | |
| 254 (arg) | |
| 255 Lisp_Object arg; | |
| 256 { | |
|
6221
c2d29681d218
(operate_on_word): Don't move point; store in *NEWPOINT.
Richard M. Stallman <rms@gnu.org>
parents:
4696
diff
changeset
|
257 Lisp_Object beg, end; |
|
c2d29681d218
(operate_on_word): Don't move point; store in *NEWPOINT.
Richard M. Stallman <rms@gnu.org>
parents:
4696
diff
changeset
|
258 int newpoint; |
|
c2d29681d218
(operate_on_word): Don't move point; store in *NEWPOINT.
Richard M. Stallman <rms@gnu.org>
parents:
4696
diff
changeset
|
259 XFASTINT (beg) = point; |
|
c2d29681d218
(operate_on_word): Don't move point; store in *NEWPOINT.
Richard M. Stallman <rms@gnu.org>
parents:
4696
diff
changeset
|
260 end = operate_on_word (arg, &newpoint); |
|
c2d29681d218
(operate_on_word): Don't move point; store in *NEWPOINT.
Richard M. Stallman <rms@gnu.org>
parents:
4696
diff
changeset
|
261 casify_region (CASE_CAPITALIZE, beg, end); |
|
c2d29681d218
(operate_on_word): Don't move point; store in *NEWPOINT.
Richard M. Stallman <rms@gnu.org>
parents:
4696
diff
changeset
|
262 SET_PT (newpoint); |
| 118 | 263 return Qnil; |
| 264 } | |
| 265 | |
| 266 syms_of_casefiddle () | |
| 267 { | |
| 268 defsubr (&Supcase); | |
| 269 defsubr (&Sdowncase); | |
| 270 defsubr (&Scapitalize); | |
| 271 defsubr (&Supcase_region); | |
| 272 defsubr (&Sdowncase_region); | |
| 273 defsubr (&Scapitalize_region); | |
| 274 defsubr (&Supcase_word); | |
| 275 defsubr (&Sdowncase_word); | |
| 276 defsubr (&Scapitalize_word); | |
| 277 } | |
| 278 | |
| 279 keys_of_casefiddle () | |
| 280 { | |
| 281 initial_define_key (control_x_map, Ctl('U'), "upcase-region"); | |
| 484 | 282 Fput (intern ("upcase-region"), Qdisabled, Qt); |
| 118 | 283 initial_define_key (control_x_map, Ctl('L'), "downcase-region"); |
| 484 | 284 Fput (intern ("downcase-region"), Qdisabled, Qt); |
| 285 | |
| 118 | 286 initial_define_key (meta_map, 'u', "upcase-word"); |
| 287 initial_define_key (meta_map, 'l', "downcase-word"); | |
| 288 initial_define_key (meta_map, 'c', "capitalize-word"); | |
| 289 } |
