1 /* GNU Emacs case conversion functions.
2 Copyright (C) 1985 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 1, or (at your option)
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
27 enum case_action
{CASE_UP
, CASE_DOWN
, CASE_CAPITALIZE
, CASE_CAPITALIZE_UP
};
30 casify_object (flag
, obj
)
31 enum case_action flag
;
34 register int i
, c
, len
;
35 register int inword
= flag
== CASE_DOWN
;
39 if (XTYPE (obj
) == Lisp_Int
)
42 if (c
>= 0 && c
<= 0400)
45 XFASTINT (obj
) = DOWNCASE (c
);
46 else if (!UPPERCASEP (c
))
47 XFASTINT (obj
) = UPCASE1 (c
);
51 if (XTYPE (obj
) == Lisp_String
)
53 obj
= Fcopy_sequence (obj
);
54 len
= XSTRING (obj
)->size
;
55 for (i
= 0; i
< len
; i
++)
57 c
= XSTRING (obj
)->data
[i
];
60 else if (!UPPERCASEP (c
))
62 XSTRING (obj
)->data
[i
] = c
;
63 if (flag
== CASE_CAPITALIZE
)
64 inword
= SYNTAX (c
) == Sword
;
68 obj
= wrong_type_argument (Qchar_or_string_p
, obj
);
72 DEFUN ("upcase", Fupcase
, Supcase
, 1, 1, 0,
73 "Convert argument to upper case and return that.\n\
74 The argument may be a character or string. The result has the same type.\n\
75 The argument object is not altered. See also `capitalize'.")
79 return casify_object (CASE_UP
, obj
);
82 DEFUN ("downcase", Fdowncase
, Sdowncase
, 1, 1, 0,
83 "Convert argument to lower case and return that.\n\
84 The argument may be a character or string. The result has the same type.\n\
85 The argument object is not altered.")
89 return casify_object (CASE_DOWN
, obj
);
92 DEFUN ("capitalize", Fcapitalize
, Scapitalize
, 1, 1, 0,
93 "Convert argument to capitalized form and return that.\n\
94 This means that each word's first character is upper case\n\
95 and the rest is lower case.\n\
96 The argument may be a character or string. The result has the same type.\n\
97 The argument object is not altered.")
101 return casify_object (CASE_CAPITALIZE
, obj
);
104 /* flag is CASE_UP, CASE_DOWN or CASE_CAPITALIZE or CASE_CAPITALIZE_UP.
105 b and e specify range of buffer to operate on. */
107 casify_region (flag
, b
, e
)
108 enum case_action flag
;
113 register int inword
= flag
== CASE_DOWN
;
116 /* Not modifying because nothing marked */
119 validate_region (&b
, &e
);
120 modify_region (current_buffer
, XFASTINT (b
), XFASTINT (e
));
121 record_change (XFASTINT (b
), XFASTINT (e
) - XFASTINT (b
));
123 for (i
= XFASTINT (b
); i
< XFASTINT (e
); i
++)
126 if (inword
&& flag
!= CASE_CAPITALIZE_UP
)
128 else if (!UPPERCASEP (c
)
129 && (!inword
|| flag
!= CASE_CAPITALIZE_UP
))
132 if ((int) flag
>= (int) CASE_CAPITALIZE
)
133 inword
= SYNTAX (c
) == Sword
;
136 signal_after_change (XFASTINT (b
),
137 XFASTINT (e
) - XFASTINT (b
),
138 XFASTINT (e
) - XFASTINT (b
));
141 DEFUN ("upcase-region", Fupcase_region
, Supcase_region
, 2, 2, "r",
142 "Convert the region to upper case. In programs, wants two arguments.\n\
143 These arguments specify the starting and ending character numbers of\n\
144 the region to operate on. When used as a command, the text between\n\
145 point and the mark is operated on.\n\
146 See also `capitalize-region'.")
150 casify_region (CASE_UP
, b
, e
);
154 DEFUN ("downcase-region", Fdowncase_region
, Sdowncase_region
, 2, 2, "r",
155 "Convert the region to lower case. In programs, wants two arguments.\n\
156 These arguments specify the starting and ending character numbers of\n\
157 the region to operate on. When used as a command, the text between\n\
158 point and the mark is operated on.")
162 casify_region (CASE_DOWN
, b
, e
);
166 DEFUN ("capitalize-region", Fcapitalize_region
, Scapitalize_region
, 2, 2, "r",
167 "Convert the region to capitalized form.\n\
168 Capitalized form means each word's first character is upper case\n\
169 and the rest of it is lower case.\n\
170 In programs, give two arguments, the starting and ending\n\
171 character positions to operate on.")
175 casify_region (CASE_CAPITALIZE
, b
, e
);
179 /* Like Fcapitalize but change only the initials. */
182 upcase_initials_region (b
, e
)
185 casify_region (CASE_CAPITALIZE_UP
, b
, e
);
190 operate_on_word (arg
, newpoint
)
197 CHECK_NUMBER (arg
, 0);
198 farend
= scan_words (point
, XINT (arg
));
200 farend
= XINT (arg
) > 0 ? ZV
: BEGV
;
202 *newpoint
= point
> farend
? point
: farend
;
203 XFASTINT (val
) = farend
;
208 DEFUN ("upcase-word", Fupcase_word
, Supcase_word
, 1, 1, "p",
209 "Convert following word (or ARG words) to upper case, moving over.\n\
210 With negative argument, convert previous words but do not move.\n\
211 See also `capitalize-word'.")
215 Lisp_Object beg
, end
;
217 XFASTINT (beg
) = point
;
218 end
= operate_on_word (arg
, &newpoint
);
219 casify_region (CASE_UP
, beg
, end
);
224 DEFUN ("downcase-word", Fdowncase_word
, Sdowncase_word
, 1, 1, "p",
225 "Convert following word (or ARG words) to lower case, moving over.\n\
226 With negative argument, convert previous words but do not move.")
230 Lisp_Object beg
, end
;
232 XFASTINT (beg
) = point
;
233 end
= operate_on_word (arg
, &newpoint
);
234 casify_region (CASE_DOWN
, beg
, end
);
239 DEFUN ("capitalize-word", Fcapitalize_word
, Scapitalize_word
, 1, 1, "p",
240 "Capitalize the following word (or ARG words), moving over.\n\
241 This gives the word(s) a first character in upper case\n\
242 and the rest lower case.\n\
243 With negative argument, capitalize previous words but do not move.")
247 Lisp_Object beg
, end
;
249 XFASTINT (beg
) = point
;
250 end
= operate_on_word (arg
, &newpoint
);
251 casify_region (CASE_CAPITALIZE
, beg
, end
);
256 syms_of_casefiddle ()
259 defsubr (&Sdowncase
);
260 defsubr (&Scapitalize
);
261 defsubr (&Supcase_region
);
262 defsubr (&Sdowncase_region
);
263 defsubr (&Scapitalize_region
);
264 defsubr (&Supcase_word
);
265 defsubr (&Sdowncase_word
);
266 defsubr (&Scapitalize_word
);
269 keys_of_casefiddle ()
271 initial_define_key (control_x_map
, Ctl('U'), "upcase-region");
272 Fput (intern ("upcase-region"), Qdisabled
, Qt
);
273 initial_define_key (control_x_map
, Ctl('L'), "downcase-region");
274 Fput (intern ("downcase-region"), Qdisabled
, Qt
);
276 initial_define_key (meta_map
, 'u', "upcase-word");
277 initial_define_key (meta_map
, 'l', "downcase-word");
278 initial_define_key (meta_map
, 'c', "capitalize-word");