PageRenderTime 27ms CodeModel.GetById 38ms RepoModel.GetById 0ms app.codeStats 0ms

/src/casefiddle.c

https://github.com/T-force/emacs
C | 445 lines | 342 code | 42 blank | 61 comment | 73 complexity | eebc5a915bb0427624e9625ffa14d8de MD5 | raw file
  1. /* GNU Emacs case conversion functions.
  2. Copyright (C) 1985, 1994, 1997-1999, 2001-2011 Free Software Foundation, Inc.
  3. This file is part of GNU Emacs.
  4. GNU Emacs is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. GNU Emacs is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
  14. #include <config.h>
  15. #include <setjmp.h>
  16. #include "lisp.h"
  17. #include "buffer.h"
  18. #include "character.h"
  19. #include "commands.h"
  20. #include "syntax.h"
  21. #include "composite.h"
  22. #include "keymap.h"
  23. enum case_action {CASE_UP, CASE_DOWN, CASE_CAPITALIZE, CASE_CAPITALIZE_UP};
  24. Lisp_Object Qidentity;
  25. static Lisp_Object
  26. casify_object (enum case_action flag, Lisp_Object obj)
  27. {
  28. register int c, c1;
  29. register int inword = flag == CASE_DOWN;
  30. /* If the case table is flagged as modified, rescan it. */
  31. if (NILP (XCHAR_TABLE (BVAR (current_buffer, downcase_table))->extras[1]))
  32. Fset_case_table (BVAR (current_buffer, downcase_table));
  33. if (INTEGERP (obj))
  34. {
  35. int flagbits = (CHAR_ALT | CHAR_SUPER | CHAR_HYPER
  36. | CHAR_SHIFT | CHAR_CTL | CHAR_META);
  37. int flags = XINT (obj) & flagbits;
  38. int multibyte = ! NILP (BVAR (current_buffer, enable_multibyte_characters));
  39. /* If the character has higher bits set
  40. above the flags, return it unchanged.
  41. It is not a real character. */
  42. if (UNSIGNED_CMP (XFASTINT (obj), >, flagbits))
  43. return obj;
  44. c1 = XFASTINT (obj) & ~flagbits;
  45. /* FIXME: Even if enable-multibyte-characters is nil, we may
  46. manipulate multibyte chars. This means we have a bug for latin-1
  47. chars since when we receive an int 128-255 we can't tell whether
  48. it's an eight-bit byte or a latin-1 char. */
  49. if (c1 >= 256)
  50. multibyte = 1;
  51. if (! multibyte)
  52. MAKE_CHAR_MULTIBYTE (c1);
  53. c = downcase (c1);
  54. if (inword)
  55. XSETFASTINT (obj, c | flags);
  56. else if (c == (XFASTINT (obj) & ~flagbits))
  57. {
  58. if (! inword)
  59. c = upcase1 (c1);
  60. if (! multibyte)
  61. MAKE_CHAR_UNIBYTE (c);
  62. XSETFASTINT (obj, c | flags);
  63. }
  64. return obj;
  65. }
  66. if (!STRINGP (obj))
  67. wrong_type_argument (Qchar_or_string_p, obj);
  68. else if (!STRING_MULTIBYTE (obj))
  69. {
  70. EMACS_INT i;
  71. EMACS_INT size = SCHARS (obj);
  72. obj = Fcopy_sequence (obj);
  73. for (i = 0; i < size; i++)
  74. {
  75. c = SREF (obj, i);
  76. MAKE_CHAR_MULTIBYTE (c);
  77. c1 = c;
  78. if (inword && flag != CASE_CAPITALIZE_UP)
  79. c = downcase (c);
  80. else if (!uppercasep (c)
  81. && (!inword || flag != CASE_CAPITALIZE_UP))
  82. c = upcase1 (c1);
  83. if ((int) flag >= (int) CASE_CAPITALIZE)
  84. inword = (SYNTAX (c) == Sword);
  85. if (c != c1)
  86. {
  87. MAKE_CHAR_UNIBYTE (c);
  88. /* If the char can't be converted to a valid byte, just don't
  89. change it. */
  90. if (c >= 0 && c < 256)
  91. SSET (obj, i, c);
  92. }
  93. }
  94. return obj;
  95. }
  96. else
  97. {
  98. EMACS_INT i, i_byte, size = SCHARS (obj);
  99. int len;
  100. USE_SAFE_ALLOCA;
  101. unsigned char *dst, *o;
  102. /* Over-allocate by 12%: this is a minor overhead, but should be
  103. sufficient in 99.999% of the cases to avoid a reallocation. */
  104. EMACS_INT o_size = SBYTES (obj) + SBYTES (obj) / 8 + MAX_MULTIBYTE_LENGTH;
  105. SAFE_ALLOCA (dst, void *, o_size);
  106. o = dst;
  107. for (i = i_byte = 0; i < size; i++, i_byte += len)
  108. {
  109. if ((o - dst) + MAX_MULTIBYTE_LENGTH > o_size)
  110. { /* Not enough space for the next char: grow the destination. */
  111. unsigned char *old_dst = dst;
  112. o_size += o_size; /* Probably overkill, but extremely rare. */
  113. SAFE_ALLOCA (dst, void *, o_size);
  114. memcpy (dst, old_dst, o - old_dst);
  115. o = dst + (o - old_dst);
  116. }
  117. c = STRING_CHAR_AND_LENGTH (SDATA (obj) + i_byte, len);
  118. if (inword && flag != CASE_CAPITALIZE_UP)
  119. c = downcase (c);
  120. else if (!uppercasep (c)
  121. && (!inword || flag != CASE_CAPITALIZE_UP))
  122. c = upcase1 (c);
  123. if ((int) flag >= (int) CASE_CAPITALIZE)
  124. inword = (SYNTAX (c) == Sword);
  125. o += CHAR_STRING (c, o);
  126. }
  127. eassert (o - dst <= o_size);
  128. obj = make_multibyte_string ((char *) dst, size, o - dst);
  129. SAFE_FREE ();
  130. return obj;
  131. }
  132. }
  133. DEFUN ("upcase", Fupcase, Supcase, 1, 1, 0,
  134. doc: /* Convert argument to upper case and return that.
  135. The argument may be a character or string. The result has the same type.
  136. The argument object is not altered--the value is a copy.
  137. See also `capitalize', `downcase' and `upcase-initials'. */)
  138. (Lisp_Object obj)
  139. {
  140. return casify_object (CASE_UP, obj);
  141. }
  142. DEFUN ("downcase", Fdowncase, Sdowncase, 1, 1, 0,
  143. doc: /* Convert argument to lower case and return that.
  144. The argument may be a character or string. The result has the same type.
  145. The argument object is not altered--the value is a copy. */)
  146. (Lisp_Object obj)
  147. {
  148. return casify_object (CASE_DOWN, obj);
  149. }
  150. DEFUN ("capitalize", Fcapitalize, Scapitalize, 1, 1, 0,
  151. doc: /* Convert argument to capitalized form and return that.
  152. This means that each word's first character is upper case
  153. and the rest is lower case.
  154. The argument may be a character or string. The result has the same type.
  155. The argument object is not altered--the value is a copy. */)
  156. (Lisp_Object obj)
  157. {
  158. return casify_object (CASE_CAPITALIZE, obj);
  159. }
  160. /* Like Fcapitalize but change only the initials. */
  161. DEFUN ("upcase-initials", Fupcase_initials, Supcase_initials, 1, 1, 0,
  162. doc: /* Convert the initial of each word in the argument to upper case.
  163. Do not change the other letters of each word.
  164. The argument may be a character or string. The result has the same type.
  165. The argument object is not altered--the value is a copy. */)
  166. (Lisp_Object obj)
  167. {
  168. return casify_object (CASE_CAPITALIZE_UP, obj);
  169. }
  170. /* flag is CASE_UP, CASE_DOWN or CASE_CAPITALIZE or CASE_CAPITALIZE_UP.
  171. b and e specify range of buffer to operate on. */
  172. static void
  173. casify_region (enum case_action flag, Lisp_Object b, Lisp_Object e)
  174. {
  175. register int c;
  176. register int inword = flag == CASE_DOWN;
  177. register int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
  178. EMACS_INT start, end;
  179. EMACS_INT start_byte;
  180. /* Position of first and last changes. */
  181. EMACS_INT first = -1, last IF_LINT (= 0);
  182. EMACS_INT opoint = PT;
  183. EMACS_INT opoint_byte = PT_BYTE;
  184. if (EQ (b, e))
  185. /* Not modifying because nothing marked */
  186. return;
  187. /* If the case table is flagged as modified, rescan it. */
  188. if (NILP (XCHAR_TABLE (BVAR (current_buffer, downcase_table))->extras[1]))
  189. Fset_case_table (BVAR (current_buffer, downcase_table));
  190. validate_region (&b, &e);
  191. start = XFASTINT (b);
  192. end = XFASTINT (e);
  193. modify_region (current_buffer, start, end, 0);
  194. record_change (start, end - start);
  195. start_byte = CHAR_TO_BYTE (start);
  196. SETUP_BUFFER_SYNTAX_TABLE(); /* For syntax_prefix_flag_p. */
  197. while (start < end)
  198. {
  199. int c2, len;
  200. if (multibyte)
  201. {
  202. c = FETCH_MULTIBYTE_CHAR (start_byte);
  203. len = CHAR_BYTES (c);
  204. }
  205. else
  206. {
  207. c = FETCH_BYTE (start_byte);
  208. MAKE_CHAR_MULTIBYTE (c);
  209. len = 1;
  210. }
  211. c2 = c;
  212. if (inword && flag != CASE_CAPITALIZE_UP)
  213. c = downcase (c);
  214. else if (!uppercasep (c)
  215. && (!inword || flag != CASE_CAPITALIZE_UP))
  216. c = upcase1 (c);
  217. if ((int) flag >= (int) CASE_CAPITALIZE)
  218. inword = ((SYNTAX (c) == Sword)
  219. && (inword || !syntax_prefix_flag_p (c)));
  220. if (c != c2)
  221. {
  222. last = start;
  223. if (first < 0)
  224. first = start;
  225. if (! multibyte)
  226. {
  227. MAKE_CHAR_UNIBYTE (c);
  228. FETCH_BYTE (start_byte) = c;
  229. }
  230. else if (ASCII_CHAR_P (c2) && ASCII_CHAR_P (c))
  231. FETCH_BYTE (start_byte) = c;
  232. else
  233. {
  234. int tolen = CHAR_BYTES (c);
  235. int j;
  236. unsigned char str[MAX_MULTIBYTE_LENGTH];
  237. CHAR_STRING (c, str);
  238. if (len == tolen)
  239. {
  240. /* Length is unchanged. */
  241. for (j = 0; j < len; ++j)
  242. FETCH_BYTE (start_byte + j) = str[j];
  243. }
  244. else
  245. {
  246. /* Replace one character with the other,
  247. keeping text properties the same. */
  248. replace_range_2 (start, start_byte,
  249. start + 1, start_byte + len,
  250. (char *) str, 1, tolen,
  251. 0);
  252. len = tolen;
  253. }
  254. }
  255. }
  256. start++;
  257. start_byte += len;
  258. }
  259. if (PT != opoint)
  260. TEMP_SET_PT_BOTH (opoint, opoint_byte);
  261. if (first >= 0)
  262. {
  263. signal_after_change (first, last + 1 - first, last + 1 - first);
  264. update_compositions (first, last + 1, CHECK_ALL);
  265. }
  266. }
  267. DEFUN ("upcase-region", Fupcase_region, Supcase_region, 2, 2, "r",
  268. doc: /* Convert the region to upper case. In programs, wants two arguments.
  269. These arguments specify the starting and ending character numbers of
  270. the region to operate on. When used as a command, the text between
  271. point and the mark is operated on.
  272. See also `capitalize-region'. */)
  273. (Lisp_Object beg, Lisp_Object end)
  274. {
  275. casify_region (CASE_UP, beg, end);
  276. return Qnil;
  277. }
  278. DEFUN ("downcase-region", Fdowncase_region, Sdowncase_region, 2, 2, "r",
  279. doc: /* Convert the region to lower case. In programs, wants two arguments.
  280. These arguments specify the starting and ending character numbers of
  281. the region to operate on. When used as a command, the text between
  282. point and the mark is operated on. */)
  283. (Lisp_Object beg, Lisp_Object end)
  284. {
  285. casify_region (CASE_DOWN, beg, end);
  286. return Qnil;
  287. }
  288. DEFUN ("capitalize-region", Fcapitalize_region, Scapitalize_region, 2, 2, "r",
  289. doc: /* Convert the region to capitalized form.
  290. Capitalized form means each word's first character is upper case
  291. and the rest of it is lower case.
  292. In programs, give two arguments, the starting and ending
  293. character positions to operate on. */)
  294. (Lisp_Object beg, Lisp_Object end)
  295. {
  296. casify_region (CASE_CAPITALIZE, beg, end);
  297. return Qnil;
  298. }
  299. /* Like Fcapitalize_region but change only the initials. */
  300. DEFUN ("upcase-initials-region", Fupcase_initials_region,
  301. Supcase_initials_region, 2, 2, "r",
  302. doc: /* Upcase the initial of each word in the region.
  303. Subsequent letters of each word are not changed.
  304. In programs, give two arguments, the starting and ending
  305. character positions to operate on. */)
  306. (Lisp_Object beg, Lisp_Object end)
  307. {
  308. casify_region (CASE_CAPITALIZE_UP, beg, end);
  309. return Qnil;
  310. }
  311. static Lisp_Object
  312. operate_on_word (Lisp_Object arg, EMACS_INT *newpoint)
  313. {
  314. Lisp_Object val;
  315. EMACS_INT farend;
  316. EMACS_INT iarg;
  317. CHECK_NUMBER (arg);
  318. iarg = XINT (arg);
  319. farend = scan_words (PT, iarg);
  320. if (!farend)
  321. farend = iarg > 0 ? ZV : BEGV;
  322. *newpoint = PT > farend ? PT : farend;
  323. XSETFASTINT (val, farend);
  324. return val;
  325. }
  326. DEFUN ("upcase-word", Fupcase_word, Supcase_word, 1, 1, "p",
  327. doc: /* Convert following word (or ARG words) to upper case, moving over.
  328. With negative argument, convert previous words but do not move.
  329. See also `capitalize-word'. */)
  330. (Lisp_Object arg)
  331. {
  332. Lisp_Object beg, end;
  333. EMACS_INT newpoint;
  334. XSETFASTINT (beg, PT);
  335. end = operate_on_word (arg, &newpoint);
  336. casify_region (CASE_UP, beg, end);
  337. SET_PT (newpoint);
  338. return Qnil;
  339. }
  340. DEFUN ("downcase-word", Fdowncase_word, Sdowncase_word, 1, 1, "p",
  341. doc: /* Convert following word (or ARG words) to lower case, moving over.
  342. With negative argument, convert previous words but do not move. */)
  343. (Lisp_Object arg)
  344. {
  345. Lisp_Object beg, end;
  346. EMACS_INT newpoint;
  347. XSETFASTINT (beg, PT);
  348. end = operate_on_word (arg, &newpoint);
  349. casify_region (CASE_DOWN, beg, end);
  350. SET_PT (newpoint);
  351. return Qnil;
  352. }
  353. DEFUN ("capitalize-word", Fcapitalize_word, Scapitalize_word, 1, 1, "p",
  354. doc: /* Capitalize the following word (or ARG words), moving over.
  355. This gives the word(s) a first character in upper case
  356. and the rest lower case.
  357. With negative argument, capitalize previous words but do not move. */)
  358. (Lisp_Object arg)
  359. {
  360. Lisp_Object beg, end;
  361. EMACS_INT newpoint;
  362. XSETFASTINT (beg, PT);
  363. end = operate_on_word (arg, &newpoint);
  364. casify_region (CASE_CAPITALIZE, beg, end);
  365. SET_PT (newpoint);
  366. return Qnil;
  367. }
  368. void
  369. syms_of_casefiddle (void)
  370. {
  371. DEFSYM (Qidentity, "identity");
  372. defsubr (&Supcase);
  373. defsubr (&Sdowncase);
  374. defsubr (&Scapitalize);
  375. defsubr (&Supcase_initials);
  376. defsubr (&Supcase_region);
  377. defsubr (&Sdowncase_region);
  378. defsubr (&Scapitalize_region);
  379. defsubr (&Supcase_initials_region);
  380. defsubr (&Supcase_word);
  381. defsubr (&Sdowncase_word);
  382. defsubr (&Scapitalize_word);
  383. }
  384. void
  385. keys_of_casefiddle (void)
  386. {
  387. initial_define_key (control_x_map, Ctl('U'), "upcase-region");
  388. Fput (intern ("upcase-region"), Qdisabled, Qt);
  389. initial_define_key (control_x_map, Ctl('L'), "downcase-region");
  390. Fput (intern ("downcase-region"), Qdisabled, Qt);
  391. initial_define_key (meta_map, 'u', "upcase-word");
  392. initial_define_key (meta_map, 'l', "downcase-word");
  393. initial_define_key (meta_map, 'c', "capitalize-word");
  394. }