* lisp/cedet/semantic/grammar.el (eldoc-function-argstring)
[emacs.git] / lisp / progmodes / glasses.el
Commit [+]AuthorDateLineData
efd68b8a
GM
Gerd Moellmann2000-01-28 12:48:41 +00001;;; glasses.el --- make cantReadThis readable
2
7e09ef09 Paul Eggert2015-01-01 14:26:41 -08003;; Copyright (C) 1999-2015 Free Software Foundation, Inc.
efd68b8a Gerd Moellmann2000-01-28 12:48:41 +00004
7aee34d3
GM
Gerd Moellmann2001-06-25 10:04:27 +00005;; Author: Milan Zamazal <pdm@zamazal.org>
6;; Maintainer: Milan Zamazal <pdm@zamazal.org>
efd68b8a
GM
Gerd Moellmann2000-01-28 12:48:41 +00007;; Keywords: tools
8
9;; This file is part of GNU Emacs.
10
b1fc2b50 Glenn Morris2008-05-06 07:25:26 +000011;; GNU Emacs is free software: you can redistribute it and/or modify
efd68b8a Gerd Moellmann2000-01-28 12:48:41 +000012;; it under the terms of the GNU General Public License as published by
b1fc2b50
GM
Glenn Morris2008-05-06 07:25:26 +000013;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
efd68b8a
GM
Gerd Moellmann2000-01-28 12:48:41 +000015
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
b1fc2b50 Glenn Morris2008-05-06 07:25:26 +000022;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
efd68b8a
GM
Gerd Moellmann2000-01-28 12:48:41 +000023
24;;; Commentary:
25
26;; This file defines a minor mode for making unreadableIdentifiersLikeThis
27;; readable. In some environments, for instance Java, it is common to use such
28;; unreadable identifiers. It is not good to use underscores in identifiers of
29;; your own project in such an environment to make your sources more readable,
30;; since it introduces undesirable confusion, which is worse than the
31;; unreadability. Fortunately, you use Emacs for the subproject, so the
32;; problem can be solved some way.
33;;
34;; This file defines the `glasses-mode' minor mode, which displays underscores
35;; between all the pairs of lower and upper English letters. (This only
36;; displays underscores, the text is not changed actually.) Alternatively, you
37;; can say you want the capitals in some given face (e.g. bold).
38;;
39;; The mode does something usable, though not perfect. Improvement suggestions
40;; from Emacs experts are welcome.
41;;
42;; If you like in-identifier separators different from underscores, change the
43;; value of the variable `glasses-separator' appropriately. See also the
44;; variables `glasses-face' and `glasses-convert-on-write-p'. You can also use
45;; the command `M-x customize-group RET glasses RET'.
46;;
47;; If you set any of the variables `glasses-separator' or `glasses-face' after
170c1b26 Gerd Moellmann2000-04-04 15:01:53 +000048;; glasses.el is loaded in a different way than through customize, you
efd68b8a
GM
Gerd Moellmann2000-01-28 12:48:41 +000049;; should call the function `glasses-set-overlay-properties' afterwards.
50
51;;; Code:
52
53
efd68b8a
GM
Gerd Moellmann2000-01-28 12:48:41 +000054;;; User variables
55
56
57(defgroup glasses nil
170c1b26 Gerd Moellmann2000-04-04 15:01:53 +000058 "Make unreadable code likeThis(one) readable."
c17c99ad Dave Love2000-08-15 14:04:13 +000059 :version "21.1"
efd68b8a
GM
Gerd Moellmann2000-01-28 12:48:41 +000060 :group 'tools)
61
62
63(defcustom glasses-separator "_"
c57a573f Stefan Monnier2006-02-23 16:35:41 +000064 "String to be displayed as a visual separator in identifiers.
08108577
EZ
Eli Zaretskii2006-02-11 11:46:15 +000065It is used both for adding missing separators and for replacing separators
66defined by `glasses-original-separator'. If you don't want to add missing
67separators, set `glasses-separator' to an empty string. If you don't want to
68replace existent separators, set `glasses-original-separator' to an empty
69string."
efd68b8a
GM
Gerd Moellmann2000-01-28 12:48:41 +000070 :group 'glasses
71 :type 'string
72 :set 'glasses-custom-set
73 :initialize 'custom-initialize-default)
74
75
08108577 Eli Zaretskii2006-02-11 11:46:15 +000076(defcustom glasses-original-separator "_"
fb7ada5f Chong Yidong2012-04-09 21:05:48 +080077 "String to be displayed as `glasses-separator' in separator positions.
08108577
EZ
Eli Zaretskii2006-02-11 11:46:15 +000078For instance, if you set it to \"_\" and set `glasses-separator' to \"-\",
79underscore separators are displayed as hyphens.
80If `glasses-original-separator' is an empty string, no such display change is
81performed."
82 :group 'glasses
83 :type 'string
84 :set 'glasses-custom-set
85 :initialize 'custom-initialize-default
86 :version "22.1")
87
88
efd68b8a Gerd Moellmann2000-01-28 12:48:41 +000089(defcustom glasses-face nil
c57a573f Stefan Monnier2006-02-23 16:35:41 +000090 "Face to be put on capitals of an identifier looked through glasses.
efd68b8a
GM
Gerd Moellmann2000-01-28 12:48:41 +000091If it is nil, no face is placed at the capitalized letter.
92
93For example, you can set `glasses-separator' to an empty string and
94`glasses-face' to `bold'. Then unreadable identifiers will have no separators,
95but will have their capitals in bold."
96 :group 'glasses
deefca64 Juri Linkov2005-12-30 05:56:39 +000097 :type '(choice (const :tag "None" nil) face)
efd68b8a
GM
Gerd Moellmann2000-01-28 12:48:41 +000098 :set 'glasses-custom-set
99 :initialize 'custom-initialize-default)
100
101
170c1b26 Gerd Moellmann2000-04-04 15:01:53 +0000102(defcustom glasses-separate-parentheses-p t
c57a573f Stefan Monnier2006-02-23 16:35:41 +0000103 "If non-nil, ensure space between an identifier and an opening parenthesis."
170c1b26
GM
Gerd Moellmann2000-04-04 15:01:53 +0000104 :group 'glasses
105 :type 'boolean)
106
014d32b1
JB
Juanma Barranquero2006-11-19 16:56:09 +0000107(defcustom glasses-separate-parentheses-exceptions
108 '("^#[\t ]*define[\t ]*[A-Za-z0-9_-]* ?($")
109 "List of regexp that are exceptions for `glasses-separate-parentheses-p'.
110They are matched to the current line truncated to the point where the
111parenthesis expression starts."
112 :group 'glasses
113 :type '(repeat regexp))
170c1b26 Gerd Moellmann2000-04-04 15:01:53 +0000114
9c1d5ac5
MZ
Milan Zamazal2011-05-04 20:26:55 -0700115(defcustom glasses-separate-capital-groups t
116 "If non-nil, try to separate groups of capital letters.
117When the value is non-nil, HTMLSomething and IPv6 are displayed
118as HTML_Something and I_Pv6 respectively. Set the value to nil
119if you prefer to display them unchanged."
120 :group 'glasses
121 :type 'boolean
122 :version "24.1")
123
170c1b26 Gerd Moellmann2000-04-04 15:01:53 +0000124(defcustom glasses-uncapitalize-p nil
c57a573f Stefan Monnier2006-02-23 16:35:41 +0000125 "If non-nil, downcase embedded capital letters in identifiers.
170c1b26
GM
Gerd Moellmann2000-04-04 15:01:53 +0000126Only identifiers starting with lower case letters are affected, letters inside
127other identifiers are unchanged."
128 :group 'glasses
129 :type 'boolean
130 :set 'glasses-custom-set
131 :initialize 'custom-initialize-default)
132
133
134(defcustom glasses-uncapitalize-regexp "[a-z]"
c57a573f Stefan Monnier2006-02-23 16:35:41 +0000135 "Regexp matching beginnings of words to be uncapitalized.
170c1b26
GM
Gerd Moellmann2000-04-04 15:01:53 +0000136Only words starting with this regexp are uncapitalized.
137The regexp is case sensitive.
138It has any effect only when `glasses-uncapitalize-p' is non-nil."
139 :group 'glasses
140 :type 'regexp
141 :set 'glasses-custom-set
142 :initialize 'custom-initialize-default)
143
144
efd68b8a Gerd Moellmann2000-01-28 12:48:41 +0000145(defcustom glasses-convert-on-write-p nil
c57a573f Stefan Monnier2006-02-23 16:35:41 +0000146 "If non-nil, remove separators when writing glasses buffer to a file.
efd68b8a
GM
Gerd Moellmann2000-01-28 12:48:41 +0000147If you are confused by glasses so much, that you write the separators into code
148during coding, set this variable to t. The separators will be removed on each
149file write then.
150
151Note the removal action does not try to be much clever, so it can remove real
152separators too."
153 :group 'glasses
154 :type 'boolean)
155
156
157(defun glasses-custom-set (symbol value)
158 "Set value of the variable SYMBOL to VALUE and update overlay categories.
159Used in :set parameter of some customized glasses variables."
c17c99ad Dave Love2000-08-15 14:04:13 +0000160 (set-default symbol value)
efd68b8a
GM
Gerd Moellmann2000-01-28 12:48:41 +0000161 (glasses-set-overlay-properties))
162
163
164;;; Utility functions
165
014d32b1
JB
Juanma Barranquero2006-11-19 16:56:09 +0000166(defun glasses-parenthesis-exception-p (beg end)
167 "Tell if (BEG, END) is an exception to `glasses-separate-parentheses-p'.
168See `glasses-separate-parentheses-exceptions'."
169 (save-match-data
170 (let ((str (buffer-substring beg end)))
171 (catch 'match
172 (dolist (re glasses-separate-parentheses-exceptions)
173 (and (string-match re str) (throw 'match t)))))))
efd68b8a
GM
Gerd Moellmann2000-01-28 12:48:41 +0000174
175(defun glasses-set-overlay-properties ()
176 "Set properties of glasses overlays.
177Consider current setting of user variables."
178 ;; In-identifier overlay
179 (put 'glasses 'evaporate t)
180 (put 'glasses 'before-string glasses-separator)
181 (put 'glasses 'face glasses-face)
182 ;; Beg-identifier overlay
183 (put 'glasses-init 'evaporate t)
170c1b26
GM
Gerd Moellmann2000-04-04 15:01:53 +0000184 (put 'glasses-init 'face glasses-face)
185 ;; Parenthesis overlay
186 (put 'glasses-parenthesis 'evaporate t)
187 (put 'glasses-parenthesis 'before-string " "))
efd68b8a
GM
Gerd Moellmann2000-01-28 12:48:41 +0000188
189(glasses-set-overlay-properties)
190
191
192(defun glasses-overlay-p (overlay)
193 "Return whether OVERLAY is an overlay of glasses mode."
170c1b26
GM
Gerd Moellmann2000-04-04 15:01:53 +0000194 (memq (overlay-get overlay 'category)
195 '(glasses glasses-init glasses-parenthesis)))
efd68b8a
GM
Gerd Moellmann2000-01-28 12:48:41 +0000196
197
170c1b26
GM
Gerd Moellmann2000-04-04 15:01:53 +0000198(defun glasses-make-overlay (beg end &optional category)
199 "Create and return readability overlay over the region from BEG to END.
200CATEGORY is the overlay category. If it is nil, use the `glasses' category."
efd68b8a Gerd Moellmann2000-01-28 12:48:41 +0000201 (let ((overlay (make-overlay beg end)))
170c1b26
GM
Gerd Moellmann2000-04-04 15:01:53 +0000202 (overlay-put overlay 'category (or category 'glasses))
203 overlay))
efd68b8a
GM
Gerd Moellmann2000-01-28 12:48:41 +0000204
205
206(defun glasses-make-readable (beg end)
207 "Make identifiers in the region from BEG to END readable."
208 (let ((case-fold-search nil))
209 (save-excursion
210 (save-match-data
211 ;; Face only
212 (goto-char beg)
213 (while (re-search-forward
214 "\\<\\([A-Z]\\)[a-zA-Z]*\\([a-z][A-Z]\\|[A-Z][a-z]\\)"
215 end t)
170c1b26
GM
Gerd Moellmann2000-04-04 15:01:53 +0000216 (glasses-make-overlay (match-beginning 1) (match-end 1)
217 'glasses-init))
efd68b8a Gerd Moellmann2000-01-28 12:48:41 +0000218 ;; Face + separator
170c1b26 Gerd Moellmann2000-04-04 15:01:53 +0000219 (goto-char beg)
9c1d5ac5
MZ
Milan Zamazal2011-05-04 20:26:55 -0700220 (while (re-search-forward
221 (if glasses-separate-capital-groups
222 "[a-z]\\([A-Z]\\)\\|[A-Z]\\([A-Z]\\)[a-z]"
223 "[a-z]\\([A-Z]\\)")
224 end t)
170c1b26
GM
Gerd Moellmann2000-04-04 15:01:53 +0000225 (let* ((n (if (match-string 1) 1 2))
226 (o (glasses-make-overlay (match-beginning n) (match-end n))))
227 (goto-char (match-beginning n))
228 (when (and glasses-uncapitalize-p
15cca6c4
GM
Gerd Moellmann2000-07-30 11:49:57 +0000229 (save-match-data
230 (looking-at "[A-Z]\\($\\|[^A-Z]\\)"))
170c1b26
GM
Gerd Moellmann2000-04-04 15:01:53 +0000231 (save-excursion
232 (save-match-data
233 (re-search-backward "\\<.")
234 (looking-at glasses-uncapitalize-regexp))))
235 (overlay-put o 'invisible t)
236 (overlay-put o 'after-string (downcase (match-string n))))))
debe6451 Eli Zaretskii2005-09-17 11:02:11 +0000237 ;; Separator change
08108577
EZ
Eli Zaretskii2006-02-11 11:46:15 +0000238 (when (and (not (string= glasses-original-separator glasses-separator))
239 (not (string= glasses-original-separator "")))
debe6451 Eli Zaretskii2005-09-17 11:02:11 +0000240 (goto-char beg)
08108577
EZ
Eli Zaretskii2006-02-11 11:46:15 +0000241 (let ((original-regexp (regexp-quote glasses-original-separator)))
242 (while (re-search-forward
243 (format "[a-zA-Z0-9]\\(\\(%s\\)+\\)[a-zA-Z0-9]"
244 original-regexp)
245 end t)
246 (goto-char (match-beginning 1))
247 (while (looking-at original-regexp)
248 (let ((o (glasses-make-overlay (point) (1+ (point)))))
249 ;; `concat' ensures the character properties won't merge
250 (overlay-put o 'display (concat glasses-separator)))
251 (goto-char (match-end 0))))))
170c1b26
GM
Gerd Moellmann2000-04-04 15:01:53 +0000252 ;; Parentheses
253 (when glasses-separate-parentheses-p
254 (goto-char beg)
7aee34d3 Gerd Moellmann2001-06-25 10:04:27 +0000255 (while (re-search-forward "[a-zA-Z]_*\\(\(\\)" end t)
014d32b1
JB
Juanma Barranquero2006-11-19 16:56:09 +0000256 (unless (glasses-parenthesis-exception-p (point-at-bol) (match-end 1))
257 (glasses-make-overlay (match-beginning 1) (match-end 1)
258 'glasses-parenthesis))))))))
efd68b8a
GM
Gerd Moellmann2000-01-28 12:48:41 +0000259
260
261(defun glasses-make-unreadable (beg end)
262 "Return identifiers in the region from BEG to END to their unreadable state."
263 (dolist (o (overlays-in beg end))
264 (when (glasses-overlay-p o)
265 (delete-overlay o))))
266
267
268(defun glasses-convert-to-unreadable ()
269 "Convert current buffer to unreadable identifiers and return nil.
270This function modifies buffer contents, it removes all the separators,
271recognized according to the current value of the variable `glasses-separator'."
014d32b1 Juanma Barranquero2006-11-19 16:56:09 +0000272 (when glasses-convert-on-write-p
971c5b28
GM
Gerd Moellmann2000-05-04 20:17:59 +0000273 (let ((case-fold-search nil)
274 (separator (regexp-quote glasses-separator)))
efd68b8a Gerd Moellmann2000-01-28 12:48:41 +0000275 (save-excursion
014d32b1 Juanma Barranquero2006-11-19 16:56:09 +0000276 (unless (string= glasses-separator "")
08108577 Eli Zaretskii2006-02-11 11:46:15 +0000277 (goto-char (point-min))
014d32b1
JB
Juanma Barranquero2006-11-19 16:56:09 +0000278 (while (re-search-forward
279 (format "[a-z]\\(%s\\)[A-Z]\\|[A-Z]\\(%s\\)[A-Z][a-z]"
280 separator separator)
281 nil t)
282 (let ((n (if (match-string 1) 1 2)))
283 (replace-match "" t nil nil n)
284 (goto-char (match-end n))))
285 (unless (string= glasses-separator glasses-original-separator)
286 (goto-char (point-min))
287 (while (re-search-forward (format "[a-zA-Z0-9]\\(%s+\\)[a-zA-Z0-9]"
288 separator)
289 nil t)
290 (replace-match glasses-original-separator nil nil nil 1)
291 (goto-char (match-beginning 1)))))
170c1b26
GM
Gerd Moellmann2000-04-04 15:01:53 +0000292 (when glasses-separate-parentheses-p
293 (goto-char (point-min))
a21822f8 Gerd Moellmann2001-09-03 08:20:54 +0000294 (while (re-search-forward "[a-zA-Z]_*\\( \\)\(" nil t)
014d32b1
JB
Juanma Barranquero2006-11-19 16:56:09 +0000295 (unless (glasses-parenthesis-exception-p (point-at-bol) (1+ (match-end 1)))
296 (replace-match "" t nil nil 1)))))))
efd68b8a
GM
Gerd Moellmann2000-01-28 12:48:41 +0000297 ;; nil must be returned to allow use in write file hooks
298 nil)
299
300
e02f48d7 Juanma Barranquero2011-04-22 20:44:26 +0200301(defun glasses-change (beg end &optional _old-len)
efd68b8a
GM
Gerd Moellmann2000-01-28 12:48:41 +0000302 "After-change function updating glass overlays."
303 (let ((beg-line (save-excursion (goto-char beg) (line-beginning-position)))
304 (end-line (save-excursion (goto-char end) (line-end-position))))
305 (glasses-make-unreadable beg-line end-line)
306 (glasses-make-readable beg-line end-line)))
307
308
309;;; Minor mode definition
310
311
efd68b8a Gerd Moellmann2000-01-28 12:48:41 +0000312;;;###autoload
5610cefd Gerd Moellmann2000-12-12 10:20:52 +0000313(define-minor-mode glasses-mode
efd68b8a Gerd Moellmann2000-01-28 12:48:41 +0000314 "Minor mode for making identifiers likeThis readable.
e1ac4066
GM
Glenn Morris2012-02-07 21:12:24 -0500315With a prefix argument ARG, enable the mode if ARG is positive,
316and disable it otherwise. If called from Lisp, enable the mode
317if ARG is omitted or nil. When this mode is active, it tries to
318add virtual separators (like underscores) at places they belong to."
3b614b81 Lute Kamstra2005-04-04 09:23:59 +0000319 :group 'glasses :lighter " o^o"
5610cefd
GM
Gerd Moellmann2000-12-12 10:20:52 +0000320 (save-excursion
321 (save-restriction
322 (widen)
323 ;; We erase all the overlays anyway, to avoid dual sight in some
324 ;; circumstances
325 (glasses-make-unreadable (point-min) (point-max))
326 (if glasses-mode
327 (progn
328 (jit-lock-register 'glasses-change)
329 (add-hook 'local-write-file-hooks
330 'glasses-convert-to-unreadable nil t))
331 (jit-lock-unregister 'glasses-change)
332 (remove-hook 'local-write-file-hooks
333 'glasses-convert-to-unreadable t)))))
efd68b8a
GM
Gerd Moellmann2000-01-28 12:48:41 +0000334
335
336;;; Announce
337
338(provide 'glasses)
339
340
341;;; glasses.el ends here