Mercurial > emacs
annotate lisp/progmodes/glasses.el @ 30789:68b63779f4df
typo fix
author | Sam Steingold <sds@gnu.org> |
---|---|
date | Mon, 14 Aug 2000 19:47:39 +0000 |
parents | 87bca20b7a83 |
children | 1b2e8bd62f76 |
rev | line source |
---|---|
27469 | 1 ;;; glasses.el --- make cantReadThis readable |
2 | |
28498
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
3 ;; Copyright (C) 1999, 2000 Free Software Foundation, Inc. |
27469 | 4 |
5 ;; Author: Milan Zamazal <pdm@freesoft.cz> | |
6 ;; Maintainer: Milan Zamazal <pdm@freesoft.cz> | |
7 ;; Keywords: tools | |
8 | |
9 ;; This file is part of GNU Emacs. | |
10 | |
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 2, or (at your option) | |
14 ;; any later version. | |
15 | |
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 | |
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
24 ;; Boston, MA 02111-1307, USA. | |
25 | |
26 ;;; Commentary: | |
27 | |
28 ;; This file defines a minor mode for making unreadableIdentifiersLikeThis | |
29 ;; readable. In some environments, for instance Java, it is common to use such | |
30 ;; unreadable identifiers. It is not good to use underscores in identifiers of | |
31 ;; your own project in such an environment to make your sources more readable, | |
32 ;; since it introduces undesirable confusion, which is worse than the | |
33 ;; unreadability. Fortunately, you use Emacs for the subproject, so the | |
34 ;; problem can be solved some way. | |
35 ;; | |
36 ;; This file defines the `glasses-mode' minor mode, which displays underscores | |
37 ;; between all the pairs of lower and upper English letters. (This only | |
38 ;; displays underscores, the text is not changed actually.) Alternatively, you | |
39 ;; can say you want the capitals in some given face (e.g. bold). | |
40 ;; | |
41 ;; The mode does something usable, though not perfect. Improvement suggestions | |
42 ;; from Emacs experts are welcome. | |
43 ;; | |
44 ;; If you like in-identifier separators different from underscores, change the | |
45 ;; value of the variable `glasses-separator' appropriately. See also the | |
46 ;; variables `glasses-face' and `glasses-convert-on-write-p'. You can also use | |
47 ;; the command `M-x customize-group RET glasses RET'. | |
48 ;; | |
49 ;; If you set any of the variables `glasses-separator' or `glasses-face' after | |
28498
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
50 ;; glasses.el is loaded in a different way than through customize, you |
27469 | 51 ;; should call the function `glasses-set-overlay-properties' afterwards. |
52 | |
53 ;;; Code: | |
54 | |
55 | |
56 (eval-when-compile | |
57 (require 'cl)) | |
58 | |
59 | |
60 ;;; User variables | |
61 | |
62 | |
63 (defgroup glasses nil | |
28498
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
64 "Make unreadable code likeThis(one) readable." |
27469 | 65 :group 'tools) |
66 | |
67 | |
68 (defcustom glasses-separator "_" | |
69 "*String to be displayed as a visual separator in unreadable identifiers." | |
70 :group 'glasses | |
71 :type 'string | |
72 :set 'glasses-custom-set | |
73 :initialize 'custom-initialize-default) | |
74 | |
75 | |
76 (defcustom glasses-face nil | |
77 "*Face to be put on capitals of an identifier looked through glasses. | |
78 If it is nil, no face is placed at the capitalized letter. | |
79 | |
80 For example, you can set `glasses-separator' to an empty string and | |
81 `glasses-face' to `bold'. Then unreadable identifiers will have no separators, | |
82 but will have their capitals in bold." | |
83 :group 'glasses | |
84 :type 'symbol | |
85 :set 'glasses-custom-set | |
86 :initialize 'custom-initialize-default) | |
87 | |
88 | |
28498
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
89 (defcustom glasses-separate-parentheses-p t |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
90 "*If non-nil, ensure space between an identifier and an opening parenthesis." |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
91 :group 'glasses |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
92 :type 'boolean) |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
93 |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
94 |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
95 (defcustom glasses-uncapitalize-p nil |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
96 "*If non-nil, downcase embedded capital letters in identifiers. |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
97 Only identifiers starting with lower case letters are affected, letters inside |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
98 other identifiers are unchanged." |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
99 :group 'glasses |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
100 :type 'boolean |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
101 :set 'glasses-custom-set |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
102 :initialize 'custom-initialize-default) |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
103 |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
104 |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
105 (defcustom glasses-uncapitalize-regexp "[a-z]" |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
106 "*Regexp matching beginnings of words to be uncapitalized. |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
107 Only words starting with this regexp are uncapitalized. |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
108 The regexp is case sensitive. |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
109 It has any effect only when `glasses-uncapitalize-p' is non-nil." |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
110 :group 'glasses |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
111 :type 'regexp |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
112 :set 'glasses-custom-set |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
113 :initialize 'custom-initialize-default) |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
114 |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
115 |
27469 | 116 (defcustom glasses-convert-on-write-p nil |
117 "*If non-nil, remove separators when writing glasses buffer to a file. | |
118 If you are confused by glasses so much, that you write the separators into code | |
119 during coding, set this variable to t. The separators will be removed on each | |
120 file write then. | |
121 | |
122 Note the removal action does not try to be much clever, so it can remove real | |
123 separators too." | |
124 :group 'glasses | |
125 :type 'boolean) | |
126 | |
127 | |
128 (defun glasses-custom-set (symbol value) | |
129 "Set value of the variable SYMBOL to VALUE and update overlay categories. | |
130 Used in :set parameter of some customized glasses variables." | |
131 (set symbol value) | |
132 (glasses-set-overlay-properties)) | |
133 | |
134 | |
135 ;;; Utility functions | |
136 | |
137 | |
138 (defun glasses-set-overlay-properties () | |
139 "Set properties of glasses overlays. | |
140 Consider current setting of user variables." | |
141 ;; In-identifier overlay | |
142 (put 'glasses 'evaporate t) | |
143 (put 'glasses 'before-string glasses-separator) | |
144 (put 'glasses 'face glasses-face) | |
145 ;; Beg-identifier overlay | |
146 (put 'glasses-init 'evaporate t) | |
28498
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
147 (put 'glasses-init 'face glasses-face) |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
148 ;; Parenthesis overlay |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
149 (put 'glasses-parenthesis 'evaporate t) |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
150 (put 'glasses-parenthesis 'before-string " ")) |
27469 | 151 |
152 (glasses-set-overlay-properties) | |
153 | |
154 | |
155 (defun glasses-overlay-p (overlay) | |
156 "Return whether OVERLAY is an overlay of glasses mode." | |
28498
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
157 (memq (overlay-get overlay 'category) |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
158 '(glasses glasses-init glasses-parenthesis))) |
27469 | 159 |
160 | |
28498
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
161 (defun glasses-make-overlay (beg end &optional category) |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
162 "Create and return readability overlay over the region from BEG to END. |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
163 CATEGORY is the overlay category. If it is nil, use the `glasses' category." |
27469 | 164 (let ((overlay (make-overlay beg end))) |
28498
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
165 (overlay-put overlay 'category (or category 'glasses)) |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
166 overlay)) |
27469 | 167 |
168 | |
169 (defun glasses-make-readable (beg end) | |
170 "Make identifiers in the region from BEG to END readable." | |
171 (let ((case-fold-search nil)) | |
172 (save-excursion | |
173 (save-match-data | |
174 ;; Face only | |
175 (goto-char beg) | |
176 (while (re-search-forward | |
177 "\\<\\([A-Z]\\)[a-zA-Z]*\\([a-z][A-Z]\\|[A-Z][a-z]\\)" | |
178 end t) | |
28498
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
179 (glasses-make-overlay (match-beginning 1) (match-end 1) |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
180 'glasses-init)) |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
181 ;; Face + separator |
27469 | 182 (goto-char beg) |
183 (while (re-search-forward "[a-z]\\([A-Z]\\)\\|[A-Z]\\([A-Z]\\)[a-z]" | |
184 end t) | |
28498
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
185 (let* ((n (if (match-string 1) 1 2)) |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
186 (o (glasses-make-overlay (match-beginning n) (match-end n)))) |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
187 (goto-char (match-beginning n)) |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
188 (when (and glasses-uncapitalize-p |
30523
87bca20b7a83
(glasses-make-readable): Fix uncapitalization of
Gerd Moellmann <gerd@gnu.org>
parents:
28810
diff
changeset
|
189 (save-match-data |
87bca20b7a83
(glasses-make-readable): Fix uncapitalization of
Gerd Moellmann <gerd@gnu.org>
parents:
28810
diff
changeset
|
190 (looking-at "[A-Z]\\($\\|[^A-Z]\\)")) |
28498
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
191 (save-excursion |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
192 (save-match-data |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
193 (re-search-backward "\\<.") |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
194 (looking-at glasses-uncapitalize-regexp)))) |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
195 (overlay-put o 'invisible t) |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
196 (overlay-put o 'after-string (downcase (match-string n)))))) |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
197 ;; Parentheses |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
198 (when glasses-separate-parentheses-p |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
199 (goto-char beg) |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
200 (while (re-search-forward "[a-zA-Z]\\(\(\\)" end t) |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
201 (glasses-make-overlay (match-beginning 1) (match-end 1) |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
202 'glasses-parenthesis))))))) |
27469 | 203 |
204 | |
205 (defun glasses-make-unreadable (beg end) | |
206 "Return identifiers in the region from BEG to END to their unreadable state." | |
207 (dolist (o (overlays-in beg end)) | |
208 (when (glasses-overlay-p o) | |
209 (delete-overlay o)))) | |
210 | |
211 | |
212 (defun glasses-convert-to-unreadable () | |
213 "Convert current buffer to unreadable identifiers and return nil. | |
214 This function modifies buffer contents, it removes all the separators, | |
215 recognized according to the current value of the variable `glasses-separator'." | |
216 (when (and glasses-convert-on-write-p | |
217 (not (string= glasses-separator ""))) | |
28810
4756047a4398
(glasses-convert-to-unreadable): Use
Gerd Moellmann <gerd@gnu.org>
parents:
28498
diff
changeset
|
218 (let ((case-fold-search nil) |
4756047a4398
(glasses-convert-to-unreadable): Use
Gerd Moellmann <gerd@gnu.org>
parents:
28498
diff
changeset
|
219 (separator (regexp-quote glasses-separator))) |
27469 | 220 (save-excursion |
221 (goto-char (point-min)) | |
222 (while (re-search-forward | |
28810
4756047a4398
(glasses-convert-to-unreadable): Use
Gerd Moellmann <gerd@gnu.org>
parents:
28498
diff
changeset
|
223 (format "[a-z]\\(%s\\)[A-Z]\\|[A-Z]\\(%s\\)[A-Z][a-z]" |
4756047a4398
(glasses-convert-to-unreadable): Use
Gerd Moellmann <gerd@gnu.org>
parents:
28498
diff
changeset
|
224 separator separator) |
4756047a4398
(glasses-convert-to-unreadable): Use
Gerd Moellmann <gerd@gnu.org>
parents:
28498
diff
changeset
|
225 nil t) |
27469 | 226 (let ((n (if (match-string 1) 1 2))) |
227 (replace-match "" t nil nil n) | |
28498
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
228 (goto-char (match-end n)))) |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
229 (when glasses-separate-parentheses-p |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
230 (goto-char (point-min)) |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
231 (while (re-search-forward "[a-zA-Z]\\( \\)\(" nil t) |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
232 (replace-match "" t nil nil 1)))))) |
27469 | 233 ;; nil must be returned to allow use in write file hooks |
234 nil) | |
235 | |
236 | |
237 (defun glasses-change (beg end old-len) | |
238 "After-change function updating glass overlays." | |
239 (let ((beg-line (save-excursion (goto-char beg) (line-beginning-position))) | |
240 (end-line (save-excursion (goto-char end) (line-end-position)))) | |
241 (glasses-make-unreadable beg-line end-line) | |
242 (glasses-make-readable beg-line end-line))) | |
243 | |
244 | |
245 ;;; Minor mode definition | |
246 | |
247 | |
248 (defvar glasses-mode nil | |
249 "Mode variable for `glasses-mode'.") | |
250 (make-variable-buffer-local 'glasses-mode) | |
251 | |
252 (add-to-list 'minor-mode-alist '(glasses-mode " o^o")) | |
253 | |
254 | |
255 ;;;###autoload | |
256 (defun glasses-mode (arg) | |
257 "Minor mode for making identifiers likeThis readable. | |
258 When this mode is active, it tries to add virtual separators (like underscores) | |
259 at places they belong to." | |
260 (interactive "P") | |
261 (let ((new-flag (if (null arg) | |
262 (not glasses-mode) | |
263 (> (prefix-numeric-value arg) 0)))) | |
264 (unless (eq new-flag glasses-mode) | |
265 (save-excursion | |
266 (save-restriction | |
267 (widen) | |
28810
4756047a4398
(glasses-convert-to-unreadable): Use
Gerd Moellmann <gerd@gnu.org>
parents:
28498
diff
changeset
|
268 ;; We erase the all overlays anyway, to avoid dual sight in some |
4756047a4398
(glasses-convert-to-unreadable): Use
Gerd Moellmann <gerd@gnu.org>
parents:
28498
diff
changeset
|
269 ;; circumstances |
4756047a4398
(glasses-convert-to-unreadable): Use
Gerd Moellmann <gerd@gnu.org>
parents:
28498
diff
changeset
|
270 (glasses-make-unreadable (point-min) (point-max)) |
27469 | 271 (if new-flag |
272 (progn | |
273 (glasses-make-readable (point-min) (point-max)) | |
274 (make-local-hook 'after-change-functions) | |
275 (add-hook 'after-change-functions 'glasses-change nil t) | |
276 (add-hook 'local-write-file-hooks | |
277 'glasses-convert-to-unreadable nil t)) | |
278 (remove-hook 'after-change-functions 'glasses-change t) | |
279 (remove-hook 'local-write-file-hooks | |
280 'glasses-convert-to-unreadable t)))) | |
281 (setq glasses-mode new-flag)))) | |
282 | |
283 | |
284 ;;; Announce | |
285 | |
286 (provide 'glasses) | |
287 | |
288 | |
289 ;;; glasses.el ends here |