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