1 ;;; subword.el --- Handling capitalized subwords in a nomenclature
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010 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.
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 "\\([[:alpha:]-]+\\)-word[s]?" name
)
90 (intern (concat "subword-" (match-string 1 name
))))))
91 (define-key map
(vector 'remap cmd
) othercmd
)))
93 "Keymap used in `subword-mode' minor mode.")
96 (define-minor-mode subword-mode
97 "Mode enabling subword movement and editing keys.
98 In spite of GNU Coding Standards, it is popular to name a symbol by
99 mixing uppercase and lowercase letters, e.g. \"GtkWidget\",
100 \"EmacsFrameClass\", \"NSGraphicsContext\", etc. Here we call these
101 mixed case symbols `nomenclatures'. Also, each capitalized (or
102 completely uppercase) part of a nomenclature is called a `subword'.
103 Here are some examples:
105 Nomenclature Subwords
106 ===========================================================
107 GtkWindow => \"Gtk\" and \"Window\"
108 EmacsFrameClass => \"Emacs\", \"Frame\" and \"Class\"
109 NSGraphicsContext => \"NS\", \"Graphics\" and \"Context\"
111 The subword oriented commands activated in this minor mode recognize
112 subwords in a nomenclature to move between subwords and to edit them
115 \\{subword-mode-map}"
120 (define-obsolete-function-alias 'c-subword-mode
'subword-mode
"23.2")
123 (define-global-minor-mode global-subword-mode subword-mode
124 (lambda () (subword-mode 1)))
126 (defun subword-forward (&optional arg
)
127 "Do the same as `forward-word' but on subwords.
128 See the command `subword-mode' for a description of subwords.
129 Optional argument ARG is the same as for `forward-word'."
131 (unless arg
(setq arg
1))
134 (dotimes (i arg
(point))
135 (subword-forward-internal)))
137 (dotimes (i (- arg
) (point))
138 (subword-backward-internal)))
142 (put 'subword-forward
'CUA
'move
)
144 (defun subword-backward (&optional arg
)
145 "Do the same as `backward-word' but on subwords.
146 See the command `subword-mode' for a description of subwords.
147 Optional argument ARG is the same as for `backward-word'."
149 (subword-forward (- (or arg
1))))
151 (defun subword-mark (arg)
152 "Do the same as `mark-word' but on subwords.
153 See the command `subword-mode' for a description of subwords.
154 Optional argument ARG is the same as for `mark-word'."
155 ;; This code is almost copied from `mark-word' in GNU Emacs.
157 (cond ((and (eq last-command this-command
) (mark t
))
161 (subword-forward arg
)
166 (subword-forward arg
)
170 (put 'subword-backward
'CUA
'move
)
172 (defun subword-kill (arg)
173 "Do the same as `kill-word' but on subwords.
174 See the command `subword-mode' for a description of subwords.
175 Optional argument ARG is the same as for `kill-word'."
177 (kill-region (point) (subword-forward arg
)))
179 (defun subword-backward-kill (arg)
180 "Do the same as `backward-kill-word' but on subwords.
181 See the command `subword-mode' for a description of subwords.
182 Optional argument ARG is the same as for `backward-kill-word'."
184 (subword-kill (- arg
)))
186 (defun subword-transpose (arg)
187 "Do the same as `transpose-words' but on subwords.
188 See the command `subword-mode' for a description of subwords.
189 Optional argument ARG is the same as for `transpose-words'."
191 (transpose-subr 'subword-forward arg
))
193 (defun subword-downcase (arg)
194 "Do the same as `downcase-word' but on subwords.
195 See the command `subword-mode' for a description of subwords.
196 Optional argument ARG is the same as for `downcase-word'."
198 (let ((start (point)))
199 (downcase-region (point) (subword-forward arg
))
203 (defun subword-upcase (arg)
204 "Do the same as `upcase-word' but on subwords.
205 See the command `subword-mode' for a description of subwords.
206 Optional argument ARG is the same as for `upcase-word'."
208 (let ((start (point)))
209 (upcase-region (point) (subword-forward arg
))
213 (defun subword-capitalize (arg)
214 "Do the same as `capitalize-word' but on subwords.
215 See the command `subword-mode' for a description of subwords.
216 Optional argument ARG is the same as for `capitalize-word'."
218 (let ((count (abs arg
))
220 (advance (if (< arg
0) nil t
)))
223 (progn (re-search-forward
224 (concat "[[:alpha:]]")
226 (goto-char (match-beginning 0)))
230 (np (subword-forward)))
232 (downcase-region pp np
)
233 (goto-char (if advance np p
))))
240 ;; Internal functions
242 (defun subword-forward-internal ()
245 (let ((case-fold-search nil
))
247 (concat "\\W*\\(\\([[:upper:]]*\\W?\\)[[:lower:][:digit:]]*\\)")
249 (> (match-end 0) (point)))
252 ((< 1 (- (match-end 2) (match-beginning 2)))
259 (defun subword-backward-internal ()
261 (let ((case-fold-search nil
))
264 "\\(\\(\\W\\|[[:lower:][:digit:]]\\)\\([[:upper:]]+\\W*\\)"
270 (< 1 (- (match-end 3) (match-beginning 3)))
271 (not (eq (point) (match-end 3))))
274 (1+ (match-beginning 0)))))
280 ;; arch-tag: b8a01202-8a52-4a71-ae0a-d753fafd67ef
281 ;;; subword.el ends here