(Vafter_change_functions, Vbefore_change_functions): Declared.
[bpt/emacs.git] / src / casefiddle.c
CommitLineData
dcfdbac7
JB
1/* GNU Emacs case conversion functions.
2 Copyright (C) 1985 Free Software Foundation, Inc.
3
4This file is part of GNU Emacs.
5
6GNU Emacs is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 1, or (at your option)
9any later version.
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs; see the file COPYING. If not, write to
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
18160b98 21#include <config.h>
dcfdbac7
JB
22#include "lisp.h"
23#include "buffer.h"
24#include "commands.h"
25#include "syntax.h"
26
27enum case_action {CASE_UP, CASE_DOWN, CASE_CAPITALIZE, CASE_CAPITALIZE_UP};
28\f
29Lisp_Object
30casify_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];
58 if (inword)
59 c = DOWNCASE (c);
60 else if (!UPPERCASEP (c))
61 c = UPCASE1 (c);
62 XSTRING (obj)->data[i] = c;
63 if (flag == CASE_CAPITALIZE)
64 inword = SYNTAX (c) == Sword;
65 }
66 return obj;
67 }
b37902c8 68 obj = wrong_type_argument (Qchar_or_string_p, obj);
dcfdbac7
JB
69 }
70}
71
72DEFUN ("upcase", Fupcase, Supcase, 1, 1, 0,
73 "Convert argument to upper case and return that.\n\
74The argument may be a character or string. The result has the same type.\n\
75The argument object is not altered. See also `capitalize'.")
76 (obj)
77 Lisp_Object obj;
78{
79 return casify_object (CASE_UP, obj);
80}
81
82DEFUN ("downcase", Fdowncase, Sdowncase, 1, 1, 0,
83 "Convert argument to lower case and return that.\n\
84The argument may be a character or string. The result has the same type.\n\
85The argument object is not altered.")
86 (obj)
87 Lisp_Object obj;
88{
89 return casify_object (CASE_DOWN, obj);
90}
91
92DEFUN ("capitalize", Fcapitalize, Scapitalize, 1, 1, 0,
93 "Convert argument to capitalized form and return that.\n\
94This means that each word's first character is upper case\n\
95and the rest is lower case.\n\
96The argument may be a character or string. The result has the same type.\n\
97The argument object is not altered.")
98 (obj)
99 Lisp_Object obj;
100{
101 return casify_object (CASE_CAPITALIZE, obj);
102}
103\f
104/* flag is CASE_UP, CASE_DOWN or CASE_CAPITALIZE or CASE_CAPITALIZE_UP.
105 b and e specify range of buffer to operate on. */
106
107casify_region (flag, b, e)
108 enum case_action flag;
109 Lisp_Object b, e;
110{
111 register int i;
112 register int c;
113 register int inword = flag == CASE_DOWN;
114
115 if (EQ (b, e))
116 /* Not modifying because nothing marked */
117 return;
118
119 validate_region (&b, &e);
04a759c8 120 modify_region (current_buffer, XFASTINT (b), XFASTINT (e));
a1929ae3 121 record_change (XFASTINT (b), XFASTINT (e) - XFASTINT (b));
dcfdbac7
JB
122
123 for (i = XFASTINT (b); i < XFASTINT (e); i++)
124 {
125 c = FETCH_CHAR (i);
126 if (inword && flag != CASE_CAPITALIZE_UP)
127 c = DOWNCASE (c);
128 else if (!UPPERCASEP (c)
129 && (!inword || flag != CASE_CAPITALIZE_UP))
130 c = UPCASE1 (c);
131 FETCH_CHAR (i) = c;
132 if ((int) flag >= (int) CASE_CAPITALIZE)
133 inword = SYNTAX (c) == Sword;
134 }
135
136 signal_after_change (XFASTINT (b),
137 XFASTINT (e) - XFASTINT (b),
138 XFASTINT (e) - XFASTINT (b));
139}
140
141DEFUN ("upcase-region", Fupcase_region, Supcase_region, 2, 2, "r",
142 "Convert the region to upper case. In programs, wants two arguments.\n\
143These arguments specify the starting and ending character numbers of\n\
144the region to operate on. When used as a command, the text between\n\
145point and the mark is operated on.\n\
146See also `capitalize-region'.")
147 (b, e)
148 Lisp_Object b, e;
149{
150 casify_region (CASE_UP, b, e);
151 return Qnil;
152}
153
154DEFUN ("downcase-region", Fdowncase_region, Sdowncase_region, 2, 2, "r",
155 "Convert the region to lower case. In programs, wants two arguments.\n\
156These arguments specify the starting and ending character numbers of\n\
157the region to operate on. When used as a command, the text between\n\
158point and the mark is operated on.")
159 (b, e)
160 Lisp_Object b, e;
161{
162 casify_region (CASE_DOWN, b, e);
163 return Qnil;
164}
165
166DEFUN ("capitalize-region", Fcapitalize_region, Scapitalize_region, 2, 2, "r",
167 "Convert the region to capitalized form.\n\
168Capitalized form means each word's first character is upper case\n\
169and the rest of it is lower case.\n\
170In programs, give two arguments, the starting and ending\n\
171character positions to operate on.")
172 (b, e)
173 Lisp_Object b, e;
174{
175 casify_region (CASE_CAPITALIZE, b, e);
176 return Qnil;
177}
178
179/* Like Fcapitalize but change only the initials. */
180
181Lisp_Object
182upcase_initials_region (b, e)
183 Lisp_Object b, e;
184{
185 casify_region (CASE_CAPITALIZE_UP, b, e);
186 return Qnil;
187}
188\f
189Lisp_Object
34628a90 190operate_on_word (arg, newpoint)
dcfdbac7 191 Lisp_Object arg;
34628a90 192 int *newpoint;
dcfdbac7 193{
39fb55ff 194 Lisp_Object val;
34628a90 195 int farend;
dcfdbac7
JB
196
197 CHECK_NUMBER (arg, 0);
198 farend = scan_words (point, XINT (arg));
199 if (!farend)
200 farend = XINT (arg) > 0 ? ZV : BEGV;
201
34628a90 202 *newpoint = point > farend ? point : farend;
dcfdbac7
JB
203 XFASTINT (val) = farend;
204
205 return val;
206}
207
208DEFUN ("upcase-word", Fupcase_word, Supcase_word, 1, 1, "p",
209 "Convert following word (or ARG words) to upper case, moving over.\n\
210With negative argument, convert previous words but do not move.\n\
211See also `capitalize-word'.")
212 (arg)
213 Lisp_Object arg;
214{
34628a90
RS
215 Lisp_Object beg, end;
216 int newpoint;
217 XFASTINT (beg) = point;
218 end = operate_on_word (arg, &newpoint);
219 casify_region (CASE_UP, beg, end);
220 SET_PT (newpoint);
dcfdbac7
JB
221 return Qnil;
222}
223
224DEFUN ("downcase-word", Fdowncase_word, Sdowncase_word, 1, 1, "p",
225 "Convert following word (or ARG words) to lower case, moving over.\n\
226With negative argument, convert previous words but do not move.")
227 (arg)
228 Lisp_Object arg;
229{
34628a90
RS
230 Lisp_Object beg, end;
231 int newpoint;
232 XFASTINT (beg) = point;
233 end = operate_on_word (arg, &newpoint);
234 casify_region (CASE_DOWN, beg, end);
235 SET_PT (newpoint);
dcfdbac7
JB
236 return Qnil;
237}
238
239DEFUN ("capitalize-word", Fcapitalize_word, Scapitalize_word, 1, 1, "p",
240 "Capitalize the following word (or ARG words), moving over.\n\
241This gives the word(s) a first character in upper case\n\
242and the rest lower case.\n\
243With negative argument, capitalize previous words but do not move.")
244 (arg)
245 Lisp_Object arg;
246{
34628a90
RS
247 Lisp_Object beg, end;
248 int newpoint;
249 XFASTINT (beg) = point;
250 end = operate_on_word (arg, &newpoint);
251 casify_region (CASE_CAPITALIZE, beg, end);
252 SET_PT (newpoint);
dcfdbac7
JB
253 return Qnil;
254}
255\f
256syms_of_casefiddle ()
257{
258 defsubr (&Supcase);
259 defsubr (&Sdowncase);
260 defsubr (&Scapitalize);
261 defsubr (&Supcase_region);
262 defsubr (&Sdowncase_region);
263 defsubr (&Scapitalize_region);
264 defsubr (&Supcase_word);
265 defsubr (&Sdowncase_word);
266 defsubr (&Scapitalize_word);
267}
268
269keys_of_casefiddle ()
270{
271 initial_define_key (control_x_map, Ctl('U'), "upcase-region");
d427b66a 272 Fput (intern ("upcase-region"), Qdisabled, Qt);
dcfdbac7 273 initial_define_key (control_x_map, Ctl('L'), "downcase-region");
d427b66a
JB
274 Fput (intern ("downcase-region"), Qdisabled, Qt);
275
dcfdbac7
JB
276 initial_define_key (meta_map, 'u', "upcase-word");
277 initial_define_key (meta_map, 'l', "downcase-word");
278 initial_define_key (meta_map, 'c', "capitalize-word");
279}