1 ;;; subword.el --- Handling capitalized subwords in a nomenclature
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
5 ;; Author: Masatake YAMATO
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;; This package was cc-submode.el before it was recognized being
25 ;; useful in general and not tied to C and c-mode at all.
27 ;; This package provides `subword' oriented commands and a minor mode
28 ;; (`subword-mode') that substitutes the common word handling
29 ;; functions with them.
31 ;; In spite of GNU Coding Standards, it is popular to name a symbol by
32 ;; mixing uppercase and lowercase letters, e.g. "GtkWidget",
33 ;; "EmacsFrameClass", "NSGraphicsContext", etc. Here we call these
34 ;; mixed case symbols `nomenclatures'. Also, each capitalized (or
35 ;; completely uppercase) part of a nomenclature is called a `subword'.
36 ;; Here are some examples:
38 ;; Nomenclature Subwords
39 ;; ===========================================================
40 ;; GtkWindow => "Gtk" and "Window"
41 ;; EmacsFrameClass => "Emacs", "Frame" and "Class"
42 ;; NSGraphicsContext => "NS", "Graphics" and "Context"
44 ;; The subword oriented commands defined in this package recognize
45 ;; subwords in a nomenclature to move between them and to edit them as
48 ;; In the minor mode, all common key bindings for word oriented
49 ;; commands are overridden by the subword oriented commands:
51 ;; Key Word oriented command Subword oriented command
52 ;; ============================================================
53 ;; M-f `forward-word' `subword-forward'
54 ;; M-b `backward-word' `subword-backward'
55 ;; M-@ `mark-word' `subword-mark'
56 ;; M-d `kill-word' `subword-kill'
57 ;; M-DEL `backward-kill-word' `subword-backward-kill'
58 ;; M-t `transpose-words' `subword-transpose'
59 ;; M-c `capitalize-word' `subword-capitalize'
60 ;; M-u `upcase-word' `subword-upcase'
61 ;; M-l `downcase-word' `subword-downcase'
63 ;; Note: If you have changed the key bindings for the word oriented
64 ;; commands in your .emacs or a similar place, the keys you've changed
65 ;; to are also used for the corresponding subword oriented commands.
67 ;; To make the mode turn on automatically, put the following code in
70 ;; (add-hook 'c-mode-common-hook
71 ;; (lambda () (subword-mode 1)))
75 ;; The regular expressions to detect subwords are mostly based on
76 ;; the old `c-forward-into-nomenclature' originally contributed by
77 ;; Terry_Glanfield dot Southern at rxuk dot xerox dot com.
79 ;; TODO: ispell-word and subword oriented C-w in isearch.
83 (defvar subword-mode-map
84 (let ((map (make-sparse-keymap)))
85 (dolist (cmd '(forward-word backward-word mark-word kill-word
86 backward-kill-word transpose-words
87 capitalize-word upcase-word downcase-word
))
88 (let ((othercmd (let ((name (symbol-name cmd
)))
89 (string-match "\\(.*-\\)\\(word.*\\)" name
)
90 (intern (concat (match-string 1 name
)
92 (match-string 2 name
))))))
93 (define-key map
(vector 'remap cmd
) othercmd
)))
95 "Keymap used in `subword-mode' minor mode.")
98 (define-minor-mode subword-mode
99 "Mode enabling subword movement and editing keys.
100 In spite of GNU Coding Standards, it is popular to name a symbol by
101 mixing uppercase and lowercase letters, e.g. \"GtkWidget\",
102 \"EmacsFrameClass\", \"NSGraphicsContext\", etc. Here we call these
103 mixed case symbols `nomenclatures'. Also, each capitalized (or
104 completely uppercase) part of a nomenclature is called a `subword'.
105 Here are some examples:
107 Nomenclature Subwords
108 ===========================================================
109 GtkWindow => \"Gtk\" and \"Window\"
110 EmacsFrameClass => \"Emacs\", \"Frame\" and \"Class\"
111 NSGraphicsContext => \"NS\", \"Graphics\" and \"Context\"
113 The subword oriented commands activated in this minor mode recognize
114 subwords in a nomenclature to move between subwords and to edit them
117 \\{subword-mode-map}"
122 (define-obsolete-function-alias 'c-subword-mode
'subword-mode
"23.2")
125 (define-global-minor-mode global-subword-mode subword-mode
126 (lambda () (subword-mode 1)))
128 (defun subword-forward (&optional arg
)
129 "Do the same as `forward-word' but on subwords.
130 See the command `subword-mode' for a description of subwords.
131 Optional argument ARG is the same as for `forward-word'."
133 (unless arg
(setq arg
1))
136 (dotimes (i arg
(point))
137 (subword-forward-internal)))
139 (dotimes (i (- arg
) (point))
140 (subword-backward-internal)))
144 (put 'subword-forward
'CUA
'move
)
146 (defun subword-backward (&optional arg
)
147 "Do the same as `backward-word' but on subwords.
148 See the command `subword-mode' for a description of subwords.
149 Optional argument ARG is the same as for `backward-word'."
151 (subword-forward (- (or arg
1))))
153 (defun subword-mark (arg)
154 "Do the same as `mark-word' but on subwords.
155 See the command `subword-mode' for a description of subwords.
156 Optional argument ARG is the same as for `mark-word'."
157 ;; This code is almost copied from `mark-word' in GNU Emacs.
159 (cond ((and (eq last-command this-command
) (mark t
))
163 (subword-forward arg
)
168 (subword-forward arg
)
172 (put 'subword-backward
'CUA
'move
)
174 (defun subword-kill (arg)
175 "Do the same as `kill-word' but on subwords.
176 See the command `subword-mode' for a description of subwords.
177 Optional argument ARG is the same as for `kill-word'."
179 (kill-region (point) (subword-forward arg
)))
181 (defun subword-backward-kill (arg)
182 "Do the same as `backward-kill-word' but on subwords.
183 See the command `subword-mode' for a description of subwords.
184 Optional argument ARG is the same as for `backward-kill-word'."
186 (subword-kill (- arg
)))
188 (defun subword-transpose (arg)
189 "Do the same as `transpose-words' but on subwords.
190 See the command `subword-mode' for a description of subwords.
191 Optional argument ARG is the same as for `transpose-words'."
193 (transpose-subr 'subword-forward arg
))
195 (defun subword-downcase (arg)
196 "Do the same as `downcase-word' but on subwords.
197 See the command `subword-mode' for a description of subwords.
198 Optional argument ARG is the same as for `downcase-word'."
200 (let ((start (point)))
201 (downcase-region (point) (subword-forward arg
))
205 (defun subword-upcase (arg)
206 "Do the same as `upcase-word' but on subwords.
207 See the command `subword-mode' for a description of subwords.
208 Optional argument ARG is the same as for `upcase-word'."
210 (let ((start (point)))
211 (upcase-region (point) (subword-forward arg
))
215 (defun subword-capitalize (arg)
216 "Do the same as `capitalize-word' but on subwords.
217 See the command `subword-mode' for a description of subwords.
218 Optional argument ARG is the same as for `capitalize-word'."
220 (let ((count (abs arg
))
222 (advance (if (< arg
0) nil t
)))
225 (progn (re-search-forward
226 (concat "[[:alpha:]]")
228 (goto-char (match-beginning 0)))
232 (np (subword-forward)))
234 (downcase-region pp np
)
235 (goto-char (if advance np p
))))
242 ;; Internal functions
244 (defun subword-forward-internal ()
247 (let ((case-fold-search nil
))
249 (concat "\\W*\\(\\([[:upper:]]*\\W?\\)[[:lower:][:digit:]]*\\)")
251 (> (match-end 0) (point)))
254 ((< 1 (- (match-end 2) (match-beginning 2)))
261 (defun subword-backward-internal ()
263 (let ((case-fold-search nil
))
266 "\\(\\(\\W\\|[[:lower:][:digit:]]\\)\\([[:upper:]]+\\W*\\)"
272 (< 1 (- (match-end 3) (match-beginning 3)))
273 (not (eq (point) (match-end 3))))
276 (1+ (match-beginning 0)))))
282 ;;; subword.el ends here