Merge changes from emacs-23 branch
[emacs.git] / lisp / progmodes / glasses.el
bloba1dc19da1ed3dcd3a61c53afdf1b7b4b07691381
1 ;;; glasses.el --- make cantReadThis readable
3 ;; Copyright (C) 1999-2011 Free Software Foundation, Inc.
5 ;; Author: Milan Zamazal <pdm@zamazal.org>
6 ;; Maintainer: Milan Zamazal <pdm@zamazal.org>
7 ;; Keywords: tools
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
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.
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).
39 ;; The mode does something usable, though not perfect. Improvement suggestions
40 ;; from Emacs experts are welcome.
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'.
47 ;; If you set any of the variables `glasses-separator' or `glasses-face' after
48 ;; glasses.el is loaded in a different way than through customize, you
49 ;; should call the function `glasses-set-overlay-properties' afterwards.
51 ;;; Code:
54 (eval-when-compile
55 (require 'cl))
58 ;;; User variables
61 (defgroup glasses nil
62 "Make unreadable code likeThis(one) readable."
63 :version "21.1"
64 :group 'tools)
67 (defcustom glasses-separator "_"
68 "String to be displayed as a visual separator in identifiers.
69 It is used both for adding missing separators and for replacing separators
70 defined by `glasses-original-separator'. If you don't want to add missing
71 separators, set `glasses-separator' to an empty string. If you don't want to
72 replace existent separators, set `glasses-original-separator' to an empty
73 string."
74 :group 'glasses
75 :type 'string
76 :set 'glasses-custom-set
77 :initialize 'custom-initialize-default)
80 (defcustom glasses-original-separator "_"
81 "*String to be displayed as `glasses-separator' in separator positions.
82 For instance, if you set it to \"_\" and set `glasses-separator' to \"-\",
83 underscore separators are displayed as hyphens.
84 If `glasses-original-separator' is an empty string, no such display change is
85 performed."
86 :group 'glasses
87 :type 'string
88 :set 'glasses-custom-set
89 :initialize 'custom-initialize-default
90 :version "22.1")
93 (defcustom glasses-face nil
94 "Face to be put on capitals of an identifier looked through glasses.
95 If it is nil, no face is placed at the capitalized letter.
97 For example, you can set `glasses-separator' to an empty string and
98 `glasses-face' to `bold'. Then unreadable identifiers will have no separators,
99 but will have their capitals in bold."
100 :group 'glasses
101 :type '(choice (const :tag "None" nil) face)
102 :set 'glasses-custom-set
103 :initialize 'custom-initialize-default)
106 (defcustom glasses-separate-parentheses-p t
107 "If non-nil, ensure space between an identifier and an opening parenthesis."
108 :group 'glasses
109 :type 'boolean)
111 (defcustom glasses-separate-parentheses-exceptions
112 '("^#[\t ]*define[\t ]*[A-Za-z0-9_-]* ?($")
113 "List of regexp that are exceptions for `glasses-separate-parentheses-p'.
114 They are matched to the current line truncated to the point where the
115 parenthesis expression starts."
116 :group 'glasses
117 :type '(repeat regexp))
119 (defcustom glasses-uncapitalize-p nil
120 "If non-nil, downcase embedded capital letters in identifiers.
121 Only identifiers starting with lower case letters are affected, letters inside
122 other identifiers are unchanged."
123 :group 'glasses
124 :type 'boolean
125 :set 'glasses-custom-set
126 :initialize 'custom-initialize-default)
129 (defcustom glasses-uncapitalize-regexp "[a-z]"
130 "Regexp matching beginnings of words to be uncapitalized.
131 Only words starting with this regexp are uncapitalized.
132 The regexp is case sensitive.
133 It has any effect only when `glasses-uncapitalize-p' is non-nil."
134 :group 'glasses
135 :type 'regexp
136 :set 'glasses-custom-set
137 :initialize 'custom-initialize-default)
140 (defcustom glasses-convert-on-write-p nil
141 "If non-nil, remove separators when writing glasses buffer to a file.
142 If you are confused by glasses so much, that you write the separators into code
143 during coding, set this variable to t. The separators will be removed on each
144 file write then.
146 Note the removal action does not try to be much clever, so it can remove real
147 separators too."
148 :group 'glasses
149 :type 'boolean)
152 (defun glasses-custom-set (symbol value)
153 "Set value of the variable SYMBOL to VALUE and update overlay categories.
154 Used in :set parameter of some customized glasses variables."
155 (set-default symbol value)
156 (glasses-set-overlay-properties))
159 ;;; Utility functions
161 (defun glasses-parenthesis-exception-p (beg end)
162 "Tell if (BEG, END) is an exception to `glasses-separate-parentheses-p'.
163 See `glasses-separate-parentheses-exceptions'."
164 (save-match-data
165 (let ((str (buffer-substring beg end)))
166 (catch 'match
167 (dolist (re glasses-separate-parentheses-exceptions)
168 (and (string-match re str) (throw 'match t)))))))
170 (defun glasses-set-overlay-properties ()
171 "Set properties of glasses overlays.
172 Consider current setting of user variables."
173 ;; In-identifier overlay
174 (put 'glasses 'evaporate t)
175 (put 'glasses 'before-string glasses-separator)
176 (put 'glasses 'face glasses-face)
177 ;; Beg-identifier overlay
178 (put 'glasses-init 'evaporate t)
179 (put 'glasses-init 'face glasses-face)
180 ;; Parenthesis overlay
181 (put 'glasses-parenthesis 'evaporate t)
182 (put 'glasses-parenthesis 'before-string " "))
184 (glasses-set-overlay-properties)
187 (defun glasses-overlay-p (overlay)
188 "Return whether OVERLAY is an overlay of glasses mode."
189 (memq (overlay-get overlay 'category)
190 '(glasses glasses-init glasses-parenthesis)))
193 (defun glasses-make-overlay (beg end &optional category)
194 "Create and return readability overlay over the region from BEG to END.
195 CATEGORY is the overlay category. If it is nil, use the `glasses' category."
196 (let ((overlay (make-overlay beg end)))
197 (overlay-put overlay 'category (or category 'glasses))
198 overlay))
201 (defun glasses-make-readable (beg end)
202 "Make identifiers in the region from BEG to END readable."
203 (let ((case-fold-search nil))
204 (save-excursion
205 (save-match-data
206 ;; Face only
207 (goto-char beg)
208 (while (re-search-forward
209 "\\<\\([A-Z]\\)[a-zA-Z]*\\([a-z][A-Z]\\|[A-Z][a-z]\\)"
210 end t)
211 (glasses-make-overlay (match-beginning 1) (match-end 1)
212 'glasses-init))
213 ;; Face + separator
214 (goto-char beg)
215 (while (re-search-forward "[a-z]\\([A-Z]\\)\\|[A-Z]\\([A-Z]\\)[a-z]"
216 end t)
217 (let* ((n (if (match-string 1) 1 2))
218 (o (glasses-make-overlay (match-beginning n) (match-end n))))
219 (goto-char (match-beginning n))
220 (when (and glasses-uncapitalize-p
221 (save-match-data
222 (looking-at "[A-Z]\\($\\|[^A-Z]\\)"))
223 (save-excursion
224 (save-match-data
225 (re-search-backward "\\<.")
226 (looking-at glasses-uncapitalize-regexp))))
227 (overlay-put o 'invisible t)
228 (overlay-put o 'after-string (downcase (match-string n))))))
229 ;; Separator change
230 (when (and (not (string= glasses-original-separator glasses-separator))
231 (not (string= glasses-original-separator "")))
232 (goto-char beg)
233 (let ((original-regexp (regexp-quote glasses-original-separator)))
234 (while (re-search-forward
235 (format "[a-zA-Z0-9]\\(\\(%s\\)+\\)[a-zA-Z0-9]"
236 original-regexp)
237 end t)
238 (goto-char (match-beginning 1))
239 (while (looking-at original-regexp)
240 (let ((o (glasses-make-overlay (point) (1+ (point)))))
241 ;; `concat' ensures the character properties won't merge
242 (overlay-put o 'display (concat glasses-separator)))
243 (goto-char (match-end 0))))))
244 ;; Parentheses
245 (when glasses-separate-parentheses-p
246 (goto-char beg)
247 (while (re-search-forward "[a-zA-Z]_*\\(\(\\)" end t)
248 (unless (glasses-parenthesis-exception-p (point-at-bol) (match-end 1))
249 (glasses-make-overlay (match-beginning 1) (match-end 1)
250 'glasses-parenthesis))))))))
253 (defun glasses-make-unreadable (beg end)
254 "Return identifiers in the region from BEG to END to their unreadable state."
255 (dolist (o (overlays-in beg end))
256 (when (glasses-overlay-p o)
257 (delete-overlay o))))
260 (defun glasses-convert-to-unreadable ()
261 "Convert current buffer to unreadable identifiers and return nil.
262 This function modifies buffer contents, it removes all the separators,
263 recognized according to the current value of the variable `glasses-separator'."
264 (when glasses-convert-on-write-p
265 (let ((case-fold-search nil)
266 (separator (regexp-quote glasses-separator)))
267 (save-excursion
268 (unless (string= glasses-separator "")
269 (goto-char (point-min))
270 (while (re-search-forward
271 (format "[a-z]\\(%s\\)[A-Z]\\|[A-Z]\\(%s\\)[A-Z][a-z]"
272 separator separator)
273 nil t)
274 (let ((n (if (match-string 1) 1 2)))
275 (replace-match "" t nil nil n)
276 (goto-char (match-end n))))
277 (unless (string= glasses-separator glasses-original-separator)
278 (goto-char (point-min))
279 (while (re-search-forward (format "[a-zA-Z0-9]\\(%s+\\)[a-zA-Z0-9]"
280 separator)
281 nil t)
282 (replace-match glasses-original-separator nil nil nil 1)
283 (goto-char (match-beginning 1)))))
284 (when glasses-separate-parentheses-p
285 (goto-char (point-min))
286 (while (re-search-forward "[a-zA-Z]_*\\( \\)\(" nil t)
287 (unless (glasses-parenthesis-exception-p (point-at-bol) (1+ (match-end 1)))
288 (replace-match "" t nil nil 1)))))))
289 ;; nil must be returned to allow use in write file hooks
290 nil)
293 (defun glasses-change (beg end &optional old-len)
294 "After-change function updating glass overlays."
295 (let ((beg-line (save-excursion (goto-char beg) (line-beginning-position)))
296 (end-line (save-excursion (goto-char end) (line-end-position))))
297 (glasses-make-unreadable beg-line end-line)
298 (glasses-make-readable beg-line end-line)))
301 ;;; Minor mode definition
304 ;;;###autoload
305 (define-minor-mode glasses-mode
306 "Minor mode for making identifiers likeThis readable.
307 When this mode is active, it tries to add virtual separators (like underscores)
308 at places they belong to."
309 :group 'glasses :lighter " o^o"
310 (save-excursion
311 (save-restriction
312 (widen)
313 ;; We erase all the overlays anyway, to avoid dual sight in some
314 ;; circumstances
315 (glasses-make-unreadable (point-min) (point-max))
316 (if glasses-mode
317 (progn
318 (jit-lock-register 'glasses-change)
319 (add-hook 'local-write-file-hooks
320 'glasses-convert-to-unreadable nil t))
321 (jit-lock-unregister 'glasses-change)
322 (remove-hook 'local-write-file-hooks
323 'glasses-convert-to-unreadable t)))))
326 ;;; Announce
328 (provide 'glasses)
331 ;;; glasses.el ends here