Merge from emacs-24; up to 2012-12-06T01:39:03Z!monnier@iro.umontreal.ca
[bpt/emacs.git] / lisp / progmodes / cap-words.el
CommitLineData
4a6a3d18 1;;; cap-words.el --- minor mode for motion in CapitalizedWordIdentifiers
f7ab7a26 2
ab422c4d 3;; Copyright (C) 2002-2013 Free Software Foundation, Inc.
f7ab7a26
DL
4
5;; Author: Dave Love <fx@gnu.org>
6;; Keywords: languages
7
b1fc2b50
GM
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software: you can redistribute it and/or modify
f7ab7a26 11;; it under the terms of the GNU General Public License as published by
b1fc2b50
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
f7ab7a26 14
b1fc2b50 15;; GNU Emacs is distributed in the hope that it will be useful,
f7ab7a26
DL
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.
19
20;; You should have received a copy of the GNU General Public License
b1fc2b50 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
f7ab7a26
DL
22
23;;; Commentary:
24
25;; Provides Capitalized Words minor mode for word movement in
26;; identifiers CapitalizedLikeThis.
27
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.
31
32;; Fixme: This doesn't work properly for mouse double clicks.
33
34;;; Code:
35
d4b5f7d7
GM
36(defun capitalized-find-word-boundary (pos limit)
37 "Function for use in `find-word-boundary-function-table'.
f7ab7a26
DL
38Looks 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)))
52
d4b5f7d7
GM
53
54(defconst capitalized-find-word-boundary-function-table
f7ab7a26 55 (let ((tab (make-char-table nil)))
d4b5f7d7 56 (set-char-table-range tab t #'capitalized-find-word-boundary)
f7ab7a26 57 tab)
d4b5f7d7 58 "Assigned to `find-word-boundary-function-table' in Capitalized Words mode.")
f7ab7a26 59
6eff8ddf 60;;;###autoload
f7ab7a26 61(define-minor-mode capitalized-words-mode
f05cde18 62 "Toggle Capitalized Words mode.
ac6c8639
CY
63With a prefix argument ARG, enable Capitalized Words mode if ARG
64is positive, and disable it otherwise. If called from Lisp,
65enable the mode if ARG is omitted or nil.
f7ab7a26 66
ac6c8639
CY
67Capitalized Words mode is a buffer-local minor mode. When
68enabled, a word boundary occurs immediately before an uppercase
69letter in a symbol. This is in addition to all the normal
f7ab7a26
DL
70boundaries given by the syntax and category tables. There is no
71restriction to ASCII.
72
73E.g. the beginning of words in the following identifier are as marked:
74
75 capitalizedWorDD
76 ^ ^ ^^
77
78Note that these word boundaries only apply for word motion and
79marking commands such as \\[forward-word]. This mode does not affect word
f05cde18 80boundaries found by regexp matching (`\\>', `\\w' &c).
f7ab7a26
DL
81
82This style of identifiers is common in environments like Java ones,
83where underscores aren't trendy enough. Capitalization rules are
6eff8ddf
DL
84sometimes part of the language, e.g. Haskell, which may thus encourage
85such a style. It is appropriate to add `capitalized-words-mode' to
f05cde18 86the mode hook for programming language modes in which you encounter
6eff8ddf
DL
87variables like this, e.g. `java-mode-hook'. It's unlikely to cause
88trouble if such identifiers aren't used.
f7ab7a26
DL
89
90See also `glasses-mode' and `studlify-word'.
91Obsoletes `c-forward-into-nomenclature'."
92 nil " Caps" nil :group 'programming
d4b5f7d7
GM
93 (set (make-local-variable 'find-word-boundary-function-table)
94 capitalized-find-word-boundary-function-table))
f7ab7a26
DL
95
96(provide 'cap-words)
fbaf0946 97
f7ab7a26 98;;; cap-words.el ends here