More CL cleanups and reduction of use of cl.el.
[emacs.git] / lisp / progmodes / cap-words.el
blob6d4d9f0544dd2d16ba7ab59f390381912cf9abba
1 ;;; cap-words.el --- minor mode for motion in CapitalizedWordIdentifiers
3 ;; Copyright (C) 2002-2012 Free Software Foundation, Inc.
5 ;; Author: Dave Love <fx@gnu.org>
6 ;; Keywords: languages
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; Provides Capitalized Words minor mode for word movement in
26 ;; identifiers CapitalizedLikeThis.
28 ;; Note that the same effect could be obtained by frobbing the
29 ;; category of upper case characters to produce word boundaries, but
30 ;; the necessary processing isn't done for ASCII characters.
32 ;; Fixme: This doesn't work properly for mouse double clicks.
34 ;;; Code:
36 (defun capitalized-find-word-boundary (pos limit)
37 "Function for use in `find-word-boundary-function-table'.
38 Looks for word boundaries before capitals."
39 (save-excursion
40 (goto-char pos)
41 (let (case-fold-search)
42 (if (<= pos limit)
43 ;; Fixme: Are these regexps the best?
44 (or (and (re-search-forward "\\=.\\w*[[:upper:]]"
45 limit t)
46 (progn (backward-char)
47 t))
48 (re-search-forward "\\>" limit t))
49 (or (re-search-backward "[[:upper:]]\\w*\\=" limit t)
50 (re-search-backward "\\<" limit t))))
51 (point)))
54 (defconst capitalized-find-word-boundary-function-table
55 (let ((tab (make-char-table nil)))
56 (set-char-table-range tab t #'capitalized-find-word-boundary)
57 tab)
58 "Assigned to `find-word-boundary-function-table' in Capitalized Words mode.")
60 ;;;###autoload
61 (define-minor-mode capitalized-words-mode
62 "Toggle Capitalized Words mode.
63 With a prefix argument ARG, enable Capitalized Words mode if ARG
64 is positive, and disable it otherwise. If called from Lisp,
65 enable the mode if ARG is omitted or nil.
67 Capitalized Words mode is a buffer-local minor mode. When
68 enabled, a word boundary occurs immediately before an uppercase
69 letter in a symbol. This is in addition to all the normal
70 boundaries given by the syntax and category tables. There is no
71 restriction to ASCII.
73 E.g. the beginning of words in the following identifier are as marked:
75 capitalizedWorDD
76 ^ ^ ^^
78 Note that these word boundaries only apply for word motion and
79 marking commands such as \\[forward-word]. This mode does not affect word
80 boundaries found by regexp matching (`\\>', `\\w' &c).
82 This style of identifiers is common in environments like Java ones,
83 where underscores aren't trendy enough. Capitalization rules are
84 sometimes part of the language, e.g. Haskell, which may thus encourage
85 such a style. It is appropriate to add `capitalized-words-mode' to
86 the mode hook for programming language modes in which you encounter
87 variables like this, e.g. `java-mode-hook'. It's unlikely to cause
88 trouble if such identifiers aren't used.
90 See also `glasses-mode' and `studlify-word'.
91 Obsoletes `c-forward-into-nomenclature'."
92 nil " Caps" nil :group 'programming
93 (set (make-local-variable 'find-word-boundary-function-table)
94 capitalized-find-word-boundary-function-table))
96 (provide 'cap-words)
98 ;;; cap-words.el ends here