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