Mercurial > hg > xemacs-beta
annotate src/casefiddle.c @ 5001:714d854d00e9
Automatic merge
author | Ben Wing <ben@xemacs.org> |
---|---|
date | Sun, 07 Feb 2010 05:23:07 -0600 |
parents | 6bc1f3f6cf0d |
children | 308d34e9f07d |
rev | line source |
---|---|
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 | |
4910
6bc1f3f6cf0d
Make canoncase visible to Lisp; use it with chars in internal_equalp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
31 enum case_action {CASE_UP, CASE_DOWN, CASE_CAPITALIZE, CASE_CAPITALIZE_UP, |
6bc1f3f6cf0d
Make canoncase visible to Lisp; use it with chars in internal_equalp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
32 CASE_CANONICALIZE}; |
428 | 33 |
34 static Lisp_Object | |
444 | 35 casify_object (enum case_action flag, Lisp_Object string_or_char, |
36 Lisp_Object buffer) | |
428 | 37 { |
38 struct buffer *buf = decode_buffer (buffer, 0); | |
39 | |
40 retry: | |
41 | |
444 | 42 if (CHAR_OR_CHAR_INTP (string_or_char)) |
428 | 43 { |
867 | 44 Ichar c; |
444 | 45 CHECK_CHAR_COERCE_INT (string_or_char); |
46 c = XCHAR (string_or_char); | |
4910
6bc1f3f6cf0d
Make canoncase visible to Lisp; use it with chars in internal_equalp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
47 if (flag == CASE_DOWN) |
6bc1f3f6cf0d
Make canoncase visible to Lisp; use it with chars in internal_equalp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
48 { |
6bc1f3f6cf0d
Make canoncase visible to Lisp; use it with chars in internal_equalp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
49 c = DOWNCASE (buf, c); |
6bc1f3f6cf0d
Make canoncase visible to Lisp; use it with chars in internal_equalp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
50 } |
6bc1f3f6cf0d
Make canoncase visible to Lisp; use it with chars in internal_equalp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
51 else if (flag == CASE_UP) |
6bc1f3f6cf0d
Make canoncase visible to Lisp; use it with chars in internal_equalp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
52 { |
6bc1f3f6cf0d
Make canoncase visible to Lisp; use it with chars in internal_equalp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
53 c = UPCASE (buf, c); |
6bc1f3f6cf0d
Make canoncase visible to Lisp; use it with chars in internal_equalp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
54 } |
6bc1f3f6cf0d
Make canoncase visible to Lisp; use it with chars in internal_equalp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
55 else |
6bc1f3f6cf0d
Make canoncase visible to Lisp; use it with chars in internal_equalp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
56 { |
6bc1f3f6cf0d
Make canoncase visible to Lisp; use it with chars in internal_equalp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
57 c = CANONCASE (buf, c); |
6bc1f3f6cf0d
Make canoncase visible to Lisp; use it with chars in internal_equalp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
58 } |
6bc1f3f6cf0d
Make canoncase visible to Lisp; use it with chars in internal_equalp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
59 |
428 | 60 return make_char (c); |
61 } | |
62 | |
444 | 63 if (STRINGP (string_or_char)) |
428 | 64 { |
826 | 65 Lisp_Object syntax_table = buf->mirror_syntax_table; |
867 | 66 Ibyte *storage = |
2367 | 67 alloca_ibytes (XSTRING_LENGTH (string_or_char) * MAX_ICHAR_LEN); |
867 | 68 Ibyte *newp = storage; |
69 Ibyte *oldp = XSTRING_DATA (string_or_char); | |
70 Ibyte *endp = oldp + XSTRING_LENGTH (string_or_char); | |
428 | 71 int wordp = 0, wordp_prev; |
72 | |
771 | 73 while (oldp < endp) |
428 | 74 { |
867 | 75 Ichar c = itext_ichar (oldp); |
428 | 76 switch (flag) |
77 { | |
78 case CASE_UP: | |
79 c = UPCASE (buf, c); | |
80 break; | |
81 case CASE_DOWN: | |
82 c = DOWNCASE (buf, c); | |
83 break; | |
4910
6bc1f3f6cf0d
Make canoncase visible to Lisp; use it with chars in internal_equalp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
84 case CASE_CANONICALIZE: |
6bc1f3f6cf0d
Make canoncase visible to Lisp; use it with chars in internal_equalp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
85 c = CANONCASE (buf, c); |
6bc1f3f6cf0d
Make canoncase visible to Lisp; use it with chars in internal_equalp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
86 break; |
428 | 87 case CASE_CAPITALIZE: |
88 case CASE_CAPITALIZE_UP: | |
89 wordp_prev = wordp; | |
90 wordp = WORD_SYNTAX_P (syntax_table, c); | |
91 if (!wordp) break; | |
92 if (wordp_prev) | |
93 { | |
94 if (flag == CASE_CAPITALIZE) | |
95 c = DOWNCASE (buf, c); | |
96 } | |
97 else | |
98 c = UPCASE (buf, c); | |
99 break; | |
100 } | |
101 | |
867 | 102 newp += set_itext_ichar (newp, c); |
103 INC_IBYTEPTR (oldp); | |
428 | 104 } |
105 | |
106 return make_string (storage, newp - storage); | |
107 } | |
108 | |
444 | 109 string_or_char = wrong_type_argument (Qchar_or_string_p, string_or_char); |
428 | 110 goto retry; |
111 } | |
112 | |
113 DEFUN ("upcase", Fupcase, 1, 2, 0, /* | |
444 | 114 Convert STRING-OR-CHAR to upper case and return that. |
115 STRING-OR-CHAR may be a character or string. The result has the same type. | |
116 STRING-OR-CHAR is not altered--the value is a copy. | |
428 | 117 See also `capitalize', `downcase' and `upcase-initials'. |
118 Optional second arg BUFFER specifies which buffer's case tables to use, | |
119 and defaults to the current buffer. | |
120 */ | |
444 | 121 (string_or_char, buffer)) |
428 | 122 { |
444 | 123 return casify_object (CASE_UP, string_or_char, buffer); |
428 | 124 } |
125 | |
126 DEFUN ("downcase", Fdowncase, 1, 2, 0, /* | |
444 | 127 Convert STRING-OR-CHAR to lower case and return that. |
128 STRING-OR-CHAR may be a character or string. The result has the same type. | |
129 STRING-OR-CHAR is not altered--the value is a copy. | |
428 | 130 Optional second arg BUFFER specifies which buffer's case tables to use, |
131 and defaults to the current buffer. | |
132 */ | |
444 | 133 (string_or_char, buffer)) |
428 | 134 { |
444 | 135 return casify_object (CASE_DOWN, string_or_char, buffer); |
428 | 136 } |
137 | |
4910
6bc1f3f6cf0d
Make canoncase visible to Lisp; use it with chars in internal_equalp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
138 DEFUN ("canoncase", Fcanoncase, 1, 2, 0, /* |
6bc1f3f6cf0d
Make canoncase visible to Lisp; use it with chars in internal_equalp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
139 Convert STRING-OR-CHAR to its canonical lowercase form and return that. |
6bc1f3f6cf0d
Make canoncase visible to Lisp; use it with chars in internal_equalp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
140 |
6bc1f3f6cf0d
Make canoncase visible to Lisp; use it with chars in internal_equalp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
141 STRING-OR-CHAR may be a character or string. The result has the same type. |
6bc1f3f6cf0d
Make canoncase visible to Lisp; use it with chars in internal_equalp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
142 STRING-OR-CHAR is not altered--the value is a copy. |
6bc1f3f6cf0d
Make canoncase visible to Lisp; use it with chars in internal_equalp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
143 |
6bc1f3f6cf0d
Make canoncase visible to Lisp; use it with chars in internal_equalp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
144 Optional second arg BUFFER specifies which buffer's case tables to use, |
6bc1f3f6cf0d
Make canoncase visible to Lisp; use it with chars in internal_equalp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
145 and defaults to the current buffer. |
6bc1f3f6cf0d
Make canoncase visible to Lisp; use it with chars in internal_equalp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
146 |
6bc1f3f6cf0d
Make canoncase visible to Lisp; use it with chars in internal_equalp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
147 For any N characters that are equivalent in case-insensitive searching, |
6bc1f3f6cf0d
Make canoncase visible to Lisp; use it with chars in internal_equalp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
148 their canonical lowercase character will be the same. |
6bc1f3f6cf0d
Make canoncase visible to Lisp; use it with chars in internal_equalp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
149 */ |
6bc1f3f6cf0d
Make canoncase visible to Lisp; use it with chars in internal_equalp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
150 (string_or_char, buffer)) |
6bc1f3f6cf0d
Make canoncase visible to Lisp; use it with chars in internal_equalp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
151 { |
6bc1f3f6cf0d
Make canoncase visible to Lisp; use it with chars in internal_equalp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
152 return casify_object (CASE_CANONICALIZE, string_or_char, buffer); |
6bc1f3f6cf0d
Make canoncase visible to Lisp; use it with chars in internal_equalp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
153 } |
6bc1f3f6cf0d
Make canoncase visible to Lisp; use it with chars in internal_equalp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
154 |
428 | 155 DEFUN ("capitalize", Fcapitalize, 1, 2, 0, /* |
444 | 156 Convert STRING-OR-CHAR to capitalized form and return that. |
428 | 157 This means that each word's first character is upper case |
158 and the rest is lower case. | |
444 | 159 STRING-OR-CHAR may be a character or string. The result has the same type. |
160 STRING-OR-CHAR is not altered--the value is a copy. | |
428 | 161 Optional second arg BUFFER specifies which buffer's case tables to use, |
162 and defaults to the current buffer. | |
163 */ | |
444 | 164 (string_or_char, buffer)) |
428 | 165 { |
444 | 166 return casify_object (CASE_CAPITALIZE, string_or_char, buffer); |
428 | 167 } |
168 | |
169 /* Like Fcapitalize but change only the initial characters. */ | |
170 | |
171 DEFUN ("upcase-initials", Fupcase_initials, 1, 2, 0, /* | |
444 | 172 Convert the initial of each word in STRING-OR-CHAR to upper case. |
428 | 173 Do not change the other letters of each word. |
444 | 174 STRING-OR-CHAR may be a character or string. The result has the same type. |
175 STRING-OR-CHAR is not altered--the value is a copy. | |
428 | 176 Optional second arg BUFFER specifies which buffer's case tables to use, |
177 and defaults to the current buffer. | |
178 */ | |
444 | 179 (string_or_char, buffer)) |
428 | 180 { |
444 | 181 return casify_object (CASE_CAPITALIZE_UP, string_or_char, buffer); |
428 | 182 } |
183 | |
184 /* flag is CASE_UP, CASE_DOWN or CASE_CAPITALIZE or CASE_CAPITALIZE_UP. | |
444 | 185 START and END specify range of buffer to operate on. */ |
428 | 186 |
187 static void | |
444 | 188 casify_region_internal (enum case_action flag, Lisp_Object start, |
189 Lisp_Object end, struct buffer *buf) | |
428 | 190 { |
191 /* This function can GC */ | |
665 | 192 Charbpos pos, s, e; |
826 | 193 Lisp_Object syntax_table = buf->mirror_syntax_table; |
428 | 194 int mccount; |
195 int wordp = 0, wordp_prev; | |
196 | |
444 | 197 if (EQ (start, end)) |
428 | 198 /* Not modifying because nothing marked */ |
199 return; | |
200 | |
444 | 201 get_buffer_range_char (buf, start, end, &s, &e, 0); |
202 | |
203 mccount = begin_multiple_change (buf, s, e); | |
204 record_change (buf, s, e - s); | |
428 | 205 |
444 | 206 for (pos = s; pos < e; pos++) |
428 | 207 { |
867 | 208 Ichar oldc = BUF_FETCH_CHAR (buf, pos); |
209 Ichar c = oldc; | |
428 | 210 |
211 switch (flag) | |
212 { | |
213 case CASE_UP: | |
214 c = UPCASE (buf, oldc); | |
215 break; | |
216 case CASE_DOWN: | |
217 c = DOWNCASE (buf, oldc); | |
218 break; | |
219 case CASE_CAPITALIZE: | |
220 case CASE_CAPITALIZE_UP: | |
221 /* !!#### need to revalidate the start and end pointers in case | |
222 the buffer was changed */ | |
223 wordp_prev = wordp; | |
224 wordp = WORD_SYNTAX_P (syntax_table, c); | |
225 if (!wordp) continue; | |
226 if (wordp_prev) | |
227 { | |
228 if (flag == CASE_CAPITALIZE) | |
229 c = DOWNCASE (buf, c); | |
230 } | |
231 else | |
232 c = UPCASE (buf, c); | |
233 break; | |
234 } | |
235 | |
236 if (oldc == c) continue; | |
444 | 237 buffer_replace_char (buf, pos, c, 1, (pos == s)); |
428 | 238 BUF_MODIFF (buf)++; |
239 } | |
240 | |
241 end_multiple_change (buf, mccount); | |
242 } | |
243 | |
244 static Lisp_Object | |
444 | 245 casify_region (enum case_action flag, Lisp_Object start, Lisp_Object end, |
428 | 246 Lisp_Object buffer) |
247 { | |
444 | 248 casify_region_internal (flag, start, end, decode_buffer (buffer, 1)); |
428 | 249 return Qnil; |
250 } | |
251 | |
252 DEFUN ("upcase-region", Fupcase_region, 2, 3, "r", /* | |
253 Convert the region to upper case. In programs, wants two arguments. | |
254 These arguments specify the starting and ending character numbers of | |
255 the region to operate on. When used as a command, the text between | |
256 point and the mark is operated on. | |
257 See also `capitalize-region'. | |
258 Optional third arg BUFFER defaults to the current buffer. | |
259 */ | |
444 | 260 (start, end, buffer)) |
428 | 261 { |
262 /* This function can GC */ | |
444 | 263 return casify_region (CASE_UP, start, end, buffer); |
428 | 264 } |
265 | |
266 DEFUN ("downcase-region", Fdowncase_region, 2, 3, "r", /* | |
267 Convert the region to lower case. In programs, wants two arguments. | |
268 These arguments specify the starting and ending character numbers of | |
269 the region to operate on. When used as a command, the text between | |
270 point and the mark is operated on. | |
271 Optional third arg BUFFER defaults to the current buffer. | |
272 */ | |
444 | 273 (start, end, buffer)) |
428 | 274 { |
275 /* This function can GC */ | |
444 | 276 return casify_region (CASE_DOWN, start, end, buffer); |
428 | 277 } |
278 | |
279 DEFUN ("capitalize-region", Fcapitalize_region, 2, 3, "r", /* | |
280 Convert the region to capitalized form. | |
281 Capitalized form means each word's first character is upper case | |
282 and the rest of it is lower case. | |
283 In programs, give two arguments, the starting and ending | |
284 character positions to operate on. | |
285 Optional third arg BUFFER defaults to the current buffer. | |
286 */ | |
444 | 287 (start, end, buffer)) |
428 | 288 { |
289 /* This function can GC */ | |
444 | 290 return casify_region (CASE_CAPITALIZE, start, end, buffer); |
428 | 291 } |
292 | |
293 /* Like Fcapitalize_region but change only the initials. */ | |
294 | |
295 DEFUN ("upcase-initials-region", Fupcase_initials_region, 2, 3, "r", /* | |
296 Upcase the initial of each word in the region. | |
297 Subsequent letters of each word are not changed. | |
298 In programs, give two arguments, the starting and ending | |
299 character positions to operate on. | |
300 Optional third arg BUFFER defaults to the current buffer. | |
301 */ | |
444 | 302 (start, end, buffer)) |
428 | 303 { |
444 | 304 return casify_region (CASE_CAPITALIZE_UP, start, end, buffer); |
428 | 305 } |
306 | |
307 | |
308 static Lisp_Object | |
309 casify_word (enum case_action flag, Lisp_Object arg, Lisp_Object buffer) | |
310 { | |
665 | 311 Charbpos farend; |
428 | 312 struct buffer *buf = decode_buffer (buffer, 1); |
313 | |
314 CHECK_INT (arg); | |
315 | |
316 farend = scan_words (buf, BUF_PT (buf), XINT (arg)); | |
317 if (!farend) | |
318 farend = XINT (arg) > 0 ? BUF_ZV (buf) : BUF_BEGV (buf); | |
319 | |
320 casify_region_internal (flag, make_int (BUF_PT (buf)), make_int (farend), buf); | |
321 BUF_SET_PT (buf, max (BUF_PT (buf), farend)); | |
322 return Qnil; | |
323 } | |
324 | |
325 DEFUN ("upcase-word", Fupcase_word, 1, 2, "p", /* | |
444 | 326 Convert following word (or COUNT words) to upper case, moving over. |
428 | 327 With negative argument, convert previous words but do not move. |
328 See also `capitalize-word'. | |
329 Optional second arg BUFFER defaults to the current buffer. | |
330 */ | |
444 | 331 (count, buffer)) |
428 | 332 { |
333 /* This function can GC */ | |
444 | 334 return casify_word (CASE_UP, count, buffer); |
428 | 335 } |
336 | |
337 DEFUN ("downcase-word", Fdowncase_word, 1, 2, "p", /* | |
444 | 338 Convert following word (or COUNT words) to lower case, moving over. |
428 | 339 With negative argument, convert previous words but do not move. |
340 Optional second arg BUFFER defaults to the current buffer. | |
341 */ | |
444 | 342 (count, buffer)) |
428 | 343 { |
344 /* This function can GC */ | |
444 | 345 return casify_word (CASE_DOWN, count, buffer); |
428 | 346 } |
347 | |
348 DEFUN ("capitalize-word", Fcapitalize_word, 1, 2, "p", /* | |
444 | 349 Capitalize the following word (or COUNT words), moving over. |
428 | 350 This gives the word(s) a first character in upper case |
351 and the rest lower case. | |
352 With negative argument, capitalize previous words but do not move. | |
353 Optional second arg BUFFER defaults to the current buffer. | |
354 */ | |
444 | 355 (count, buffer)) |
428 | 356 { |
357 /* This function can GC */ | |
444 | 358 return casify_word (CASE_CAPITALIZE, count, buffer); |
428 | 359 } |
360 | |
361 | |
362 void | |
363 syms_of_casefiddle (void) | |
364 { | |
365 DEFSUBR (Fupcase); | |
366 DEFSUBR (Fdowncase); | |
4910
6bc1f3f6cf0d
Make canoncase visible to Lisp; use it with chars in internal_equalp.
Aidan Kehoe <kehoea@parhasard.net>
parents:
2367
diff
changeset
|
367 DEFSUBR (Fcanoncase); |
428 | 368 DEFSUBR (Fcapitalize); |
369 DEFSUBR (Fupcase_initials); | |
370 DEFSUBR (Fupcase_region); | |
371 DEFSUBR (Fdowncase_region); | |
372 DEFSUBR (Fcapitalize_region); | |
373 DEFSUBR (Fupcase_initials_region); | |
374 DEFSUBR (Fupcase_word); | |
375 DEFSUBR (Fdowncase_word); | |
376 DEFSUBR (Fcapitalize_word); | |
377 } |