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