subword.el 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. ;;; subword.el --- Handling capitalized subwords in a nomenclature
  2. ;; Copyright (C) 2004-2012 Free Software Foundation, Inc.
  3. ;; Author: Masatake YAMATO
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;; This package was cc-submode.el before it was recognized being
  17. ;; useful in general and not tied to C and c-mode at all.
  18. ;; This package provides `subword' oriented commands and a minor mode
  19. ;; (`subword-mode') that substitutes the common word handling
  20. ;; functions with them.
  21. ;; In spite of GNU Coding Standards, it is popular to name a symbol by
  22. ;; mixing uppercase and lowercase letters, e.g. "GtkWidget",
  23. ;; "EmacsFrameClass", "NSGraphicsContext", etc. Here we call these
  24. ;; mixed case symbols `nomenclatures'. Also, each capitalized (or
  25. ;; completely uppercase) part of a nomenclature is called a `subword'.
  26. ;; Here are some examples:
  27. ;; Nomenclature Subwords
  28. ;; ===========================================================
  29. ;; GtkWindow => "Gtk" and "Window"
  30. ;; EmacsFrameClass => "Emacs", "Frame" and "Class"
  31. ;; NSGraphicsContext => "NS", "Graphics" and "Context"
  32. ;; The subword oriented commands defined in this package recognize
  33. ;; subwords in a nomenclature to move between them and to edit them as
  34. ;; words.
  35. ;; In the minor mode, all common key bindings for word oriented
  36. ;; commands are overridden by the subword oriented commands:
  37. ;; Key Word oriented command Subword oriented command
  38. ;; ============================================================
  39. ;; M-f `forward-word' `subword-forward'
  40. ;; M-b `backward-word' `subword-backward'
  41. ;; M-@ `mark-word' `subword-mark'
  42. ;; M-d `kill-word' `subword-kill'
  43. ;; M-DEL `backward-kill-word' `subword-backward-kill'
  44. ;; M-t `transpose-words' `subword-transpose'
  45. ;; M-c `capitalize-word' `subword-capitalize'
  46. ;; M-u `upcase-word' `subword-upcase'
  47. ;; M-l `downcase-word' `subword-downcase'
  48. ;;
  49. ;; Note: If you have changed the key bindings for the word oriented
  50. ;; commands in your .emacs or a similar place, the keys you've changed
  51. ;; to are also used for the corresponding subword oriented commands.
  52. ;; To make the mode turn on automatically, put the following code in
  53. ;; your .emacs:
  54. ;;
  55. ;; (add-hook 'c-mode-common-hook
  56. ;; (lambda () (subword-mode 1)))
  57. ;;
  58. ;; Acknowledgment:
  59. ;; The regular expressions to detect subwords are mostly based on
  60. ;; the old `c-forward-into-nomenclature' originally contributed by
  61. ;; Terry_Glanfield dot Southern at rxuk dot xerox dot com.
  62. ;; TODO: ispell-word.
  63. ;;; Code:
  64. (defvar subword-mode-map
  65. (let ((map (make-sparse-keymap)))
  66. (dolist (cmd '(forward-word backward-word mark-word kill-word
  67. backward-kill-word transpose-words
  68. capitalize-word upcase-word downcase-word))
  69. (let ((othercmd (let ((name (symbol-name cmd)))
  70. (string-match "\\([[:alpha:]-]+\\)-word[s]?" name)
  71. (intern (concat "subword-" (match-string 1 name))))))
  72. (define-key map (vector 'remap cmd) othercmd)))
  73. map)
  74. "Keymap used in `subword-mode' minor mode.")
  75. ;;;###autoload
  76. (define-minor-mode subword-mode
  77. "Toggle subword movement and editing (Subword mode).
  78. With a prefix argument ARG, enable Subword mode if ARG is
  79. positive, and disable it otherwise. If called from Lisp, enable
  80. the mode if ARG is omitted or nil.
  81. Subword mode is a buffer-local minor mode. Enabling it remaps
  82. word-based editing commands to subword-based commands that handle
  83. symbols with mixed uppercase and lowercase letters,
  84. e.g. \"GtkWidget\", \"EmacsFrameClass\", \"NSGraphicsContext\".
  85. Here we call these mixed case symbols `nomenclatures'. Each
  86. capitalized (or completely uppercase) part of a nomenclature is
  87. called a `subword'. Here are some examples:
  88. Nomenclature Subwords
  89. ===========================================================
  90. GtkWindow => \"Gtk\" and \"Window\"
  91. EmacsFrameClass => \"Emacs\", \"Frame\" and \"Class\"
  92. NSGraphicsContext => \"NS\", \"Graphics\" and \"Context\"
  93. The subword oriented commands activated in this minor mode recognize
  94. subwords in a nomenclature to move between subwords and to edit them
  95. as words.
  96. \\{subword-mode-map}"
  97. nil
  98. nil
  99. subword-mode-map)
  100. (define-obsolete-function-alias 'c-subword-mode 'subword-mode "23.2")
  101. ;;;###autoload
  102. (define-global-minor-mode global-subword-mode subword-mode
  103. (lambda () (subword-mode 1)))
  104. (defun subword-forward (&optional arg)
  105. "Do the same as `forward-word' but on subwords.
  106. See the command `subword-mode' for a description of subwords.
  107. Optional argument ARG is the same as for `forward-word'."
  108. (interactive "p")
  109. (unless arg (setq arg 1))
  110. (cond
  111. ((< 0 arg)
  112. (dotimes (i arg (point))
  113. (subword-forward-internal)))
  114. ((> 0 arg)
  115. (dotimes (i (- arg) (point))
  116. (subword-backward-internal)))
  117. (t
  118. (point))))
  119. (put 'subword-forward 'CUA 'move)
  120. (defun subword-backward (&optional arg)
  121. "Do the same as `backward-word' but on subwords.
  122. See the command `subword-mode' for a description of subwords.
  123. Optional argument ARG is the same as for `backward-word'."
  124. (interactive "p")
  125. (subword-forward (- (or arg 1))))
  126. (defun subword-mark (arg)
  127. "Do the same as `mark-word' but on subwords.
  128. See the command `subword-mode' for a description of subwords.
  129. Optional argument ARG is the same as for `mark-word'."
  130. ;; This code is almost copied from `mark-word' in GNU Emacs.
  131. (interactive "p")
  132. (cond ((and (eq last-command this-command) (mark t))
  133. (set-mark
  134. (save-excursion
  135. (goto-char (mark))
  136. (subword-forward arg)
  137. (point))))
  138. (t
  139. (push-mark
  140. (save-excursion
  141. (subword-forward arg)
  142. (point))
  143. nil t))))
  144. (put 'subword-backward 'CUA 'move)
  145. (defun subword-kill (arg)
  146. "Do the same as `kill-word' but on subwords.
  147. See the command `subword-mode' for a description of subwords.
  148. Optional argument ARG is the same as for `kill-word'."
  149. (interactive "p")
  150. (kill-region (point) (subword-forward arg)))
  151. (defun subword-backward-kill (arg)
  152. "Do the same as `backward-kill-word' but on subwords.
  153. See the command `subword-mode' for a description of subwords.
  154. Optional argument ARG is the same as for `backward-kill-word'."
  155. (interactive "p")
  156. (subword-kill (- arg)))
  157. (defun subword-transpose (arg)
  158. "Do the same as `transpose-words' but on subwords.
  159. See the command `subword-mode' for a description of subwords.
  160. Optional argument ARG is the same as for `transpose-words'."
  161. (interactive "*p")
  162. (transpose-subr 'subword-forward arg))
  163. (defun subword-downcase (arg)
  164. "Do the same as `downcase-word' but on subwords.
  165. See the command `subword-mode' for a description of subwords.
  166. Optional argument ARG is the same as for `downcase-word'."
  167. (interactive "p")
  168. (let ((start (point)))
  169. (downcase-region (point) (subword-forward arg))
  170. (when (< arg 0)
  171. (goto-char start))))
  172. (defun subword-upcase (arg)
  173. "Do the same as `upcase-word' but on subwords.
  174. See the command `subword-mode' for a description of subwords.
  175. Optional argument ARG is the same as for `upcase-word'."
  176. (interactive "p")
  177. (let ((start (point)))
  178. (upcase-region (point) (subword-forward arg))
  179. (when (< arg 0)
  180. (goto-char start))))
  181. (defun subword-capitalize (arg)
  182. "Do the same as `capitalize-word' but on subwords.
  183. See the command `subword-mode' for a description of subwords.
  184. Optional argument ARG is the same as for `capitalize-word'."
  185. (interactive "p")
  186. (let ((count (abs arg))
  187. (start (point))
  188. (advance (if (< arg 0) nil t)))
  189. (dotimes (i count)
  190. (if advance
  191. (progn (re-search-forward
  192. (concat "[[:alpha:]]")
  193. nil t)
  194. (goto-char (match-beginning 0)))
  195. (subword-backward))
  196. (let* ((p (point))
  197. (pp (1+ p))
  198. (np (subword-forward)))
  199. (upcase-region p pp)
  200. (downcase-region pp np)
  201. (goto-char (if advance np p))))
  202. (unless advance
  203. (goto-char start))))
  204. ;;
  205. ;; Internal functions
  206. ;;
  207. (defun subword-forward-internal ()
  208. (if (and
  209. (save-excursion
  210. (let ((case-fold-search nil))
  211. (re-search-forward
  212. (concat "\\W*\\(\\([[:upper:]]*\\W?\\)[[:lower:][:digit:]]*\\)")
  213. nil t)))
  214. (> (match-end 0) (point)))
  215. (goto-char
  216. (cond
  217. ((< 1 (- (match-end 2) (match-beginning 2)))
  218. (1- (match-end 2)))
  219. (t
  220. (match-end 0))))
  221. (forward-word 1)))
  222. (defun subword-backward-internal ()
  223. (if (save-excursion
  224. (let ((case-fold-search nil))
  225. (re-search-backward
  226. (concat
  227. "\\(\\(\\W\\|[[:lower:][:digit:]]\\)\\([[:upper:]]+\\W*\\)"
  228. "\\|\\W\\w+\\)")
  229. nil t)))
  230. (goto-char
  231. (cond
  232. ((and (match-end 3)
  233. (< 1 (- (match-end 3) (match-beginning 3)))
  234. (not (eq (point) (match-end 3))))
  235. (1- (match-end 3)))
  236. (t
  237. (1+ (match-beginning 0)))))
  238. (backward-word 1)))
  239. (provide 'subword)
  240. ;;; subword.el ends here