0
|
1 /* XEmacs case conversion functions.
|
|
2 Copyright (C) 1985, 1992, 1993, 1994 Free Software Foundation, Inc.
|
|
3
|
|
4 This file is part of XEmacs.
|
|
5
|
|
6 XEmacs is free software; you can redistribute it and/or modify it
|
|
7 under the terms of the GNU General Public License as published by the
|
|
8 Free Software Foundation; either version 2, or (at your option) any
|
|
9 later version.
|
|
10
|
|
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 for more details.
|
|
15
|
|
16 You should have received a copy of the GNU General Public License
|
|
17 along with XEmacs; 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 /* Synched up with: FSF 19.30. */
|
|
22
|
|
23 #include <config.h>
|
|
24 #include "lisp.h"
|
|
25
|
|
26 #include "buffer.h"
|
|
27 #include "commands.h"
|
|
28 #include "insdel.h"
|
|
29 #include "syntax.h"
|
|
30
|
|
31 enum case_action {CASE_UP, CASE_DOWN, CASE_CAPITALIZE, CASE_CAPITALIZE_UP};
|
|
32
|
|
33 static Lisp_Object
|
|
34 casify_object (struct buffer *buf, enum case_action flag, Lisp_Object obj)
|
|
35 {
|
|
36 REGISTER Emchar c;
|
|
37 REGISTER Charcount i, len;
|
|
38 REGISTER int inword = flag == CASE_DOWN;
|
|
39 Lisp_Object syntax_table = buf->syntax_table;
|
|
40
|
|
41 while (1)
|
|
42 {
|
|
43 if (CHAR_OR_CHAR_INTP (obj))
|
|
44 {
|
|
45 CHECK_CHAR_COERCE_INT (obj);
|
|
46 c = XCHAR (obj);
|
|
47 if (IN_TRT_TABLE_DOMAIN (c))
|
|
48 {
|
|
49 if (inword)
|
|
50 obj = make_char (DOWNCASE (buf, c));
|
|
51 else if (!UPPERCASEP (buf, c))
|
|
52 obj = make_char (UPCASE1 (buf, c));
|
|
53 }
|
|
54 return obj;
|
|
55 }
|
|
56 if (STRINGP (obj))
|
|
57 {
|
|
58 obj = Fcopy_sequence (obj);
|
|
59 len = string_char_length (XSTRING (obj));
|
|
60 for (i = 0; i < len; i++)
|
|
61 {
|
|
62 c = string_char (XSTRING (obj), i);
|
|
63 if (inword)
|
|
64 c = DOWNCASE (buf, c);
|
|
65 else if (!UPPERCASEP (buf, c)
|
|
66 && (!inword || flag != CASE_CAPITALIZE_UP))
|
|
67 c = UPCASE1 (buf, c);
|
|
68 set_string_char (XSTRING (obj), i, c);
|
|
69 if ((int) flag >= (int) CASE_CAPITALIZE)
|
|
70 inword = WORD_SYNTAX_P (syntax_table, c);
|
|
71 }
|
|
72 return obj;
|
|
73 }
|
|
74 obj = wrong_type_argument (Qchar_or_string_p, obj);
|
|
75 }
|
|
76 }
|
|
77
|
20
|
78 DEFUN ("upcase", Fupcase, 1, 2, 0, /*
|
0
|
79 Convert argument to upper case and return that.
|
|
80 The argument may be a character or string. The result has the same type.
|
|
81 The argument object is not altered--the value is a copy.
|
|
82 See also `capitalize', `downcase' and `upcase-initials'.
|
|
83 Optional second arg BUFFER specifies which buffer's case tables to use,
|
|
84 and defaults to the current buffer.
|
20
|
85 */
|
|
86 (obj, buffer))
|
0
|
87 {
|
|
88 return casify_object (decode_buffer (buffer, 0), CASE_UP, obj);
|
|
89 }
|
|
90
|
20
|
91 DEFUN ("downcase", Fdowncase, 1, 2, 0, /*
|
0
|
92 Convert argument to lower case and return that.
|
|
93 The argument may be a character or string. The result has the same type.
|
|
94 The argument object is not altered--the value is a copy.
|
|
95 Optional second arg BUFFER specifies which buffer's case tables to use,
|
|
96 and defaults to the current buffer.
|
20
|
97 */
|
|
98 (obj, buffer))
|
0
|
99 {
|
|
100 return casify_object (decode_buffer (buffer, 0), CASE_DOWN, obj);
|
|
101 }
|
|
102
|
20
|
103 DEFUN ("capitalize", Fcapitalize, 1, 2, 0, /*
|
0
|
104 Convert argument to capitalized form and return that.
|
|
105 This means that each word's first character is upper case
|
|
106 and the rest is lower case.
|
|
107 The argument may be a character or string. The result has the same type.
|
|
108 The argument object is not altered--the value is a copy.
|
|
109 Optional second arg BUFFER specifies which buffer's case tables to use,
|
|
110 and defaults to the current buffer.
|
20
|
111 */
|
|
112 (obj, buffer))
|
0
|
113 {
|
|
114 return casify_object (decode_buffer (buffer, 0), CASE_CAPITALIZE, obj);
|
|
115 }
|
|
116
|
|
117 /* Like Fcapitalize but change only the initials. */
|
|
118
|
20
|
119 DEFUN ("upcase-initials", Fupcase_initials, 1, 2, 0, /*
|
0
|
120 Convert the initial of each word in the argument to upper case.
|
|
121 Do not change the other letters of each word.
|
|
122 The argument may be a character or string. The result has the same type.
|
|
123 The argument object is not altered--the value is a copy.
|
|
124 Optional second arg BUFFER specifies which buffer's case tables to use,
|
|
125 and defaults to the current buffer.
|
20
|
126 */
|
|
127 (obj, buffer))
|
0
|
128 {
|
|
129 return casify_object (decode_buffer (buffer, 0), CASE_CAPITALIZE_UP, obj);
|
|
130 }
|
|
131
|
|
132 /* flag is CASE_UP, CASE_DOWN or CASE_CAPITALIZE or CASE_CAPITALIZE_UP.
|
|
133 b and e specify range of buffer to operate on. */
|
|
134
|
|
135 static void
|
|
136 casify_region (struct buffer *buf, enum case_action flag, Lisp_Object b,
|
|
137 Lisp_Object e)
|
|
138 {
|
|
139 /* This function can GC */
|
|
140 REGISTER Bufpos i;
|
|
141 Bufpos start, end;
|
|
142 REGISTER Emchar c;
|
|
143 REGISTER int inword = flag == CASE_DOWN;
|
|
144 Lisp_Object syntax_table = buf->syntax_table;
|
|
145 int mccount;
|
|
146
|
|
147 if (EQ (b, e))
|
|
148 /* Not modifying because nothing marked */
|
|
149 return;
|
|
150
|
|
151 get_buffer_range_char (buf, b, e, &start, &end, 0);
|
|
152
|
|
153 mccount = begin_multiple_change (buf, start, end);
|
|
154 record_change (buf, start, end - start);
|
|
155 BUF_MODIFF (buf)++;
|
|
156
|
|
157 for (i = start; i < end; i++)
|
|
158 {
|
|
159 c = BUF_FETCH_CHAR (buf, i);
|
|
160 if (inword && flag != CASE_CAPITALIZE_UP)
|
|
161 c = DOWNCASE (buf, c);
|
|
162 else if (!UPPERCASEP (buf, c)
|
|
163 && (!inword || flag != CASE_CAPITALIZE_UP))
|
|
164 c = UPCASE1 (buf, c);
|
|
165
|
|
166 buffer_replace_char (buf, i, c, 1, (i == start));
|
|
167 /* !!#### need to revalidate the start and end pointers in case
|
|
168 the buffer was changed */
|
|
169 if ((int) flag >= (int) CASE_CAPITALIZE)
|
|
170 inword = WORD_SYNTAX_P (syntax_table, c);
|
|
171 }
|
|
172 end_multiple_change (buf, mccount);
|
|
173 }
|
|
174
|
20
|
175 DEFUN ("upcase-region", Fupcase_region, 2, 3, "r", /*
|
0
|
176 Convert the region to upper case. In programs, wants two arguments.
|
|
177 These arguments specify the starting and ending character numbers of
|
|
178 the region to operate on. When used as a command, the text between
|
|
179 point and the mark is operated on.
|
|
180 See also `capitalize-region'.
|
|
181 Optional third arg BUFFER defaults to the current buffer.
|
20
|
182 */
|
|
183 (b, e, buffer))
|
0
|
184 {
|
|
185 /* This function can GC */
|
|
186 casify_region (decode_buffer (buffer, 1), CASE_UP, b, e);
|
|
187 return Qnil;
|
|
188 }
|
|
189
|
20
|
190 DEFUN ("downcase-region", Fdowncase_region, 2, 3, "r", /*
|
0
|
191 Convert the region to lower case. In programs, wants two arguments.
|
|
192 These arguments specify the starting and ending character numbers of
|
|
193 the region to operate on. When used as a command, the text between
|
|
194 point and the mark is operated on.
|
|
195 Optional third arg BUFFER defaults to the current buffer.
|
20
|
196 */
|
|
197 (b, e, buffer))
|
0
|
198 {
|
|
199 /* This function can GC */
|
|
200 casify_region (decode_buffer (buffer, 1), CASE_DOWN, b, e);
|
|
201 return Qnil;
|
|
202 }
|
|
203
|
20
|
204 DEFUN ("capitalize-region", Fcapitalize_region, 2, 3, "r", /*
|
0
|
205 Convert the region to capitalized form.
|
|
206 Capitalized form means each word's first character is upper case
|
|
207 and the rest of it is lower case.
|
|
208 In programs, give two arguments, the starting and ending
|
|
209 character positions to operate on.
|
|
210 Optional third arg BUFFER defaults to the current buffer.
|
20
|
211 */
|
|
212 (b, e, buffer))
|
0
|
213 {
|
|
214 /* This function can GC */
|
|
215 casify_region (decode_buffer (buffer, 1), CASE_CAPITALIZE, b, e);
|
|
216 return Qnil;
|
|
217 }
|
|
218
|
|
219 /* Like Fcapitalize_region but change only the initials. */
|
|
220
|
20
|
221 DEFUN ("upcase-initials-region", Fupcase_initials_region, 2, 3, "r", /*
|
0
|
222 Upcase the initial of each word in the region.
|
|
223 Subsequent letters of each word are not changed.
|
|
224 In programs, give two arguments, the starting and ending
|
|
225 character positions to operate on.
|
|
226 Optional third arg BUFFER defaults to the current buffer.
|
20
|
227 */
|
|
228 (b, e, buffer))
|
0
|
229 {
|
|
230 casify_region (decode_buffer (buffer, 1), CASE_CAPITALIZE_UP, b, e);
|
|
231 return Qnil;
|
|
232 }
|
|
233
|
|
234
|
|
235 static Lisp_Object
|
|
236 operate_on_word (struct buffer *buf, Lisp_Object arg, int *newpoint)
|
|
237 {
|
|
238 Bufpos farend;
|
|
239
|
|
240 CHECK_INT (arg);
|
|
241 farend = scan_words (buf, BUF_PT (buf), XINT (arg));
|
|
242 if (!farend)
|
|
243 farend = XINT (arg) > 0 ? BUF_ZV (buf) : BUF_BEGV (buf);
|
|
244
|
|
245 *newpoint = ((BUF_PT (buf) > farend) ? BUF_PT (buf) : farend);
|
|
246 return (make_int (farend));
|
|
247 }
|
|
248
|
20
|
249 DEFUN ("upcase-word", Fupcase_word, 1, 2, "p", /*
|
0
|
250 Convert following word (or ARG words) to upper case, moving over.
|
|
251 With negative argument, convert previous words but do not move.
|
|
252 See also `capitalize-word'.
|
|
253 Optional second arg BUFFER defaults to the current buffer.
|
20
|
254 */
|
|
255 (arg, buffer))
|
0
|
256 {
|
|
257 /* This function can GC */
|
|
258 Lisp_Object beg, end;
|
|
259 Bufpos newpoint;
|
|
260 struct buffer *buf = decode_buffer (buffer, 1);
|
|
261
|
|
262 beg = make_int (BUF_PT (buf));
|
|
263 end = operate_on_word (buf, arg, &newpoint);
|
|
264 casify_region (buf, CASE_UP, beg, end);
|
|
265 BUF_SET_PT (buf, newpoint);
|
|
266 return Qnil;
|
|
267 }
|
|
268
|
20
|
269 DEFUN ("downcase-word", Fdowncase_word, 1, 2, "p", /*
|
0
|
270 Convert following word (or ARG words) to lower case, moving over.
|
|
271 With negative argument, convert previous words but do not move.
|
|
272 Optional second arg BUFFER defaults to the current buffer.
|
20
|
273 */
|
|
274 (arg, buffer))
|
0
|
275 {
|
|
276 /* This function can GC */
|
|
277 Lisp_Object beg, end;
|
|
278 Bufpos newpoint;
|
|
279 struct buffer *buf = decode_buffer (buffer, 1);
|
|
280
|
|
281 beg = make_int (BUF_PT (buf));
|
|
282 end = operate_on_word (buf, arg, &newpoint);
|
|
283 casify_region (buf, CASE_DOWN, beg, end);
|
|
284 BUF_SET_PT (buf, newpoint);
|
|
285 return Qnil;
|
|
286 }
|
|
287
|
20
|
288 DEFUN ("capitalize-word", Fcapitalize_word, 1, 2, "p", /*
|
0
|
289 Capitalize the following word (or ARG words), moving over.
|
|
290 This gives the word(s) a first character in upper case
|
|
291 and the rest lower case.
|
|
292 With negative argument, capitalize previous words but do not move.
|
|
293 Optional second arg BUFFER defaults to the current buffer.
|
20
|
294 */
|
|
295 (arg, buffer))
|
0
|
296 {
|
|
297 /* This function can GC */
|
|
298 Lisp_Object beg, end;
|
|
299 Bufpos newpoint;
|
|
300 struct buffer *buf = decode_buffer (buffer, 1);
|
|
301
|
|
302 beg = make_int (BUF_PT (buf));
|
|
303 end = operate_on_word (buf, arg, &newpoint);
|
|
304 casify_region (buf, CASE_CAPITALIZE, beg, end);
|
|
305 BUF_SET_PT (buf, newpoint);
|
|
306 return Qnil;
|
|
307 }
|
|
308
|
|
309
|
|
310 void
|
|
311 syms_of_casefiddle (void)
|
|
312 {
|
20
|
313 DEFSUBR (Fupcase);
|
|
314 DEFSUBR (Fdowncase);
|
|
315 DEFSUBR (Fcapitalize);
|
|
316 DEFSUBR (Fupcase_initials);
|
|
317 DEFSUBR (Fupcase_region);
|
|
318 DEFSUBR (Fdowncase_region);
|
|
319 DEFSUBR (Fcapitalize_region);
|
|
320 DEFSUBR (Fupcase_initials_region);
|
|
321 DEFSUBR (Fupcase_word);
|
|
322 DEFSUBR (Fdowncase_word);
|
|
323 DEFSUBR (Fcapitalize_word);
|
0
|
324 }
|