Mercurial > emacs
annotate lisp/progmodes/glasses.el @ 105838:5a494d9d4e4f
(define-obsolete-variable-alias): Use dolist,
and only put a prop if it is non-nil.
author | Stefan Monnier <monnier@iro.umontreal.ca> |
---|---|
date | Tue, 03 Nov 2009 15:28:13 +0000 |
parents | a9dc0e7c3f2b |
children | 1d1d5d9bd884 |
rev | line source |
---|---|
27469 | 1 ;;; glasses.el --- make cantReadThis readable |
2 | |
100908 | 3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 |
74547 | 4 ;; Free Software Foundation, Inc. |
27469 | 5 |
38176
2a85f8119924
(glasses-make-readable): Consider
Gerd Moellmann <gerd@gnu.org>
parents:
34473
diff
changeset
|
6 ;; Author: Milan Zamazal <pdm@zamazal.org> |
2a85f8119924
(glasses-make-readable): Consider
Gerd Moellmann <gerd@gnu.org>
parents:
34473
diff
changeset
|
7 ;; Maintainer: Milan Zamazal <pdm@zamazal.org> |
27469 | 8 ;; Keywords: tools |
9 | |
10 ;; This file is part of GNU Emacs. | |
11 | |
94673
52b7a8c22af5
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
79717
diff
changeset
|
12 ;; GNU Emacs is free software: you can redistribute it and/or modify |
27469 | 13 ;; it under the terms of the GNU General Public License as published by |
94673
52b7a8c22af5
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
79717
diff
changeset
|
14 ;; the Free Software Foundation, either version 3 of the License, or |
52b7a8c22af5
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
79717
diff
changeset
|
15 ;; (at your option) any later version. |
27469 | 16 |
17 ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 ;; GNU General Public License for more details. | |
21 | |
22 ;; You should have received a copy of the GNU General Public License | |
94673
52b7a8c22af5
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
79717
diff
changeset
|
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
27469 | 24 |
25 ;;; Commentary: | |
26 | |
27 ;; This file defines a minor mode for making unreadableIdentifiersLikeThis | |
28 ;; readable. In some environments, for instance Java, it is common to use such | |
29 ;; unreadable identifiers. It is not good to use underscores in identifiers of | |
30 ;; your own project in such an environment to make your sources more readable, | |
31 ;; since it introduces undesirable confusion, which is worse than the | |
32 ;; unreadability. Fortunately, you use Emacs for the subproject, so the | |
33 ;; problem can be solved some way. | |
34 ;; | |
35 ;; This file defines the `glasses-mode' minor mode, which displays underscores | |
36 ;; between all the pairs of lower and upper English letters. (This only | |
37 ;; displays underscores, the text is not changed actually.) Alternatively, you | |
38 ;; can say you want the capitals in some given face (e.g. bold). | |
39 ;; | |
40 ;; The mode does something usable, though not perfect. Improvement suggestions | |
41 ;; from Emacs experts are welcome. | |
42 ;; | |
43 ;; If you like in-identifier separators different from underscores, change the | |
44 ;; value of the variable `glasses-separator' appropriately. See also the | |
45 ;; variables `glasses-face' and `glasses-convert-on-write-p'. You can also use | |
46 ;; the command `M-x customize-group RET glasses RET'. | |
47 ;; | |
48 ;; 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
|
49 ;; glasses.el is loaded in a different way than through customize, you |
27469 | 50 ;; should call the function `glasses-set-overlay-properties' afterwards. |
51 | |
52 ;;; Code: | |
53 | |
54 | |
55 (eval-when-compile | |
56 (require 'cl)) | |
57 | |
58 | |
59 ;;; User variables | |
60 | |
61 | |
62 (defgroup glasses nil | |
28498
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
63 "Make unreadable code likeThis(one) readable." |
30814
1b2e8bd62f76
(glasses) <defgroup>: Add :version.
Dave Love <fx@gnu.org>
parents:
30523
diff
changeset
|
64 :version "21.1" |
27469 | 65 :group 'tools) |
66 | |
67 | |
68 (defcustom glasses-separator "_" | |
69123
618d3bbab8be
Remove unnecessary leading * in docstrings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
68790
diff
changeset
|
69 "String to be displayed as a visual separator in identifiers. |
68790
29a4ce0514ee
(glasses-separator): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
68773
diff
changeset
|
70 It is used both for adding missing separators and for replacing separators |
29a4ce0514ee
(glasses-separator): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
68773
diff
changeset
|
71 defined by `glasses-original-separator'. If you don't want to add missing |
29a4ce0514ee
(glasses-separator): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
68773
diff
changeset
|
72 separators, set `glasses-separator' to an empty string. If you don't want to |
29a4ce0514ee
(glasses-separator): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
68773
diff
changeset
|
73 replace existent separators, set `glasses-original-separator' to an empty |
29a4ce0514ee
(glasses-separator): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
68773
diff
changeset
|
74 string." |
27469 | 75 :group 'glasses |
76 :type 'string | |
77 :set 'glasses-custom-set | |
78 :initialize 'custom-initialize-default) | |
79 | |
80 | |
68790
29a4ce0514ee
(glasses-separator): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
68773
diff
changeset
|
81 (defcustom glasses-original-separator "_" |
29a4ce0514ee
(glasses-separator): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
68773
diff
changeset
|
82 "*String to be displayed as `glasses-separator' in separator positions. |
29a4ce0514ee
(glasses-separator): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
68773
diff
changeset
|
83 For instance, if you set it to \"_\" and set `glasses-separator' to \"-\", |
29a4ce0514ee
(glasses-separator): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
68773
diff
changeset
|
84 underscore separators are displayed as hyphens. |
29a4ce0514ee
(glasses-separator): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
68773
diff
changeset
|
85 If `glasses-original-separator' is an empty string, no such display change is |
29a4ce0514ee
(glasses-separator): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
68773
diff
changeset
|
86 performed." |
29a4ce0514ee
(glasses-separator): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
68773
diff
changeset
|
87 :group 'glasses |
29a4ce0514ee
(glasses-separator): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
68773
diff
changeset
|
88 :type 'string |
29a4ce0514ee
(glasses-separator): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
68773
diff
changeset
|
89 :set 'glasses-custom-set |
29a4ce0514ee
(glasses-separator): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
68773
diff
changeset
|
90 :initialize 'custom-initialize-default |
29a4ce0514ee
(glasses-separator): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
68773
diff
changeset
|
91 :version "22.1") |
29a4ce0514ee
(glasses-separator): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
68773
diff
changeset
|
92 |
29a4ce0514ee
(glasses-separator): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
68773
diff
changeset
|
93 |
27469 | 94 (defcustom glasses-face nil |
69123
618d3bbab8be
Remove unnecessary leading * in docstrings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
68790
diff
changeset
|
95 "Face to be put on capitals of an identifier looked through glasses. |
27469 | 96 If it is nil, no face is placed at the capitalized letter. |
97 | |
98 For example, you can set `glasses-separator' to an empty string and | |
99 `glasses-face' to `bold'. Then unreadable identifiers will have no separators, | |
100 but will have their capitals in bold." | |
101 :group 'glasses | |
67912
24b085b97616
(glasses-face): Add tag "None" to const nil.
Juri Linkov <juri@jurta.org>
parents:
67862
diff
changeset
|
102 :type '(choice (const :tag "None" nil) face) |
27469 | 103 :set 'glasses-custom-set |
104 :initialize 'custom-initialize-default) | |
105 | |
106 | |
28498
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
107 (defcustom glasses-separate-parentheses-p t |
69123
618d3bbab8be
Remove unnecessary leading * in docstrings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
68790
diff
changeset
|
108 "If non-nil, ensure space between an identifier and an opening parenthesis." |
28498
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
109 :group 'glasses |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
110 :type 'boolean) |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
111 |
74079
71cb863df348
(glasses-separate-parentheses-exceptions): New. Exceptions to the rule "add
Juanma Barranquero <lekktu@gmail.com>
parents:
69123
diff
changeset
|
112 (defcustom glasses-separate-parentheses-exceptions |
71cb863df348
(glasses-separate-parentheses-exceptions): New. Exceptions to the rule "add
Juanma Barranquero <lekktu@gmail.com>
parents:
69123
diff
changeset
|
113 '("^#[\t ]*define[\t ]*[A-Za-z0-9_-]* ?($") |
71cb863df348
(glasses-separate-parentheses-exceptions): New. Exceptions to the rule "add
Juanma Barranquero <lekktu@gmail.com>
parents:
69123
diff
changeset
|
114 "List of regexp that are exceptions for `glasses-separate-parentheses-p'. |
71cb863df348
(glasses-separate-parentheses-exceptions): New. Exceptions to the rule "add
Juanma Barranquero <lekktu@gmail.com>
parents:
69123
diff
changeset
|
115 They are matched to the current line truncated to the point where the |
71cb863df348
(glasses-separate-parentheses-exceptions): New. Exceptions to the rule "add
Juanma Barranquero <lekktu@gmail.com>
parents:
69123
diff
changeset
|
116 parenthesis expression starts." |
71cb863df348
(glasses-separate-parentheses-exceptions): New. Exceptions to the rule "add
Juanma Barranquero <lekktu@gmail.com>
parents:
69123
diff
changeset
|
117 :group 'glasses |
71cb863df348
(glasses-separate-parentheses-exceptions): New. Exceptions to the rule "add
Juanma Barranquero <lekktu@gmail.com>
parents:
69123
diff
changeset
|
118 :type '(repeat regexp)) |
28498
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
119 |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
120 (defcustom glasses-uncapitalize-p nil |
69123
618d3bbab8be
Remove unnecessary leading * in docstrings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
68790
diff
changeset
|
121 "If non-nil, downcase embedded capital letters in identifiers. |
28498
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
122 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
|
123 other identifiers are unchanged." |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
124 :group 'glasses |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
125 :type 'boolean |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
126 :set 'glasses-custom-set |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
127 :initialize 'custom-initialize-default) |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
128 |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
129 |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
130 (defcustom glasses-uncapitalize-regexp "[a-z]" |
69123
618d3bbab8be
Remove unnecessary leading * in docstrings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
68790
diff
changeset
|
131 "Regexp matching beginnings of words to be uncapitalized. |
28498
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
132 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
|
133 The regexp is case sensitive. |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
134 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
|
135 :group 'glasses |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
136 :type 'regexp |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
137 :set 'glasses-custom-set |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
138 :initialize 'custom-initialize-default) |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
139 |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
140 |
27469 | 141 (defcustom glasses-convert-on-write-p nil |
69123
618d3bbab8be
Remove unnecessary leading * in docstrings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
68790
diff
changeset
|
142 "If non-nil, remove separators when writing glasses buffer to a file. |
27469 | 143 If you are confused by glasses so much, that you write the separators into code |
144 during coding, set this variable to t. The separators will be removed on each | |
145 file write then. | |
146 | |
147 Note the removal action does not try to be much clever, so it can remove real | |
148 separators too." | |
149 :group 'glasses | |
150 :type 'boolean) | |
151 | |
152 | |
153 (defun glasses-custom-set (symbol value) | |
154 "Set value of the variable SYMBOL to VALUE and update overlay categories. | |
155 Used in :set parameter of some customized glasses variables." | |
30814
1b2e8bd62f76
(glasses) <defgroup>: Add :version.
Dave Love <fx@gnu.org>
parents:
30523
diff
changeset
|
156 (set-default symbol value) |
27469 | 157 (glasses-set-overlay-properties)) |
158 | |
159 | |
160 ;;; Utility functions | |
161 | |
74079
71cb863df348
(glasses-separate-parentheses-exceptions): New. Exceptions to the rule "add
Juanma Barranquero <lekktu@gmail.com>
parents:
69123
diff
changeset
|
162 (defun glasses-parenthesis-exception-p (beg end) |
71cb863df348
(glasses-separate-parentheses-exceptions): New. Exceptions to the rule "add
Juanma Barranquero <lekktu@gmail.com>
parents:
69123
diff
changeset
|
163 "Tell if (BEG, END) is an exception to `glasses-separate-parentheses-p'. |
71cb863df348
(glasses-separate-parentheses-exceptions): New. Exceptions to the rule "add
Juanma Barranquero <lekktu@gmail.com>
parents:
69123
diff
changeset
|
164 See `glasses-separate-parentheses-exceptions'." |
71cb863df348
(glasses-separate-parentheses-exceptions): New. Exceptions to the rule "add
Juanma Barranquero <lekktu@gmail.com>
parents:
69123
diff
changeset
|
165 (save-match-data |
71cb863df348
(glasses-separate-parentheses-exceptions): New. Exceptions to the rule "add
Juanma Barranquero <lekktu@gmail.com>
parents:
69123
diff
changeset
|
166 (let ((str (buffer-substring beg end))) |
71cb863df348
(glasses-separate-parentheses-exceptions): New. Exceptions to the rule "add
Juanma Barranquero <lekktu@gmail.com>
parents:
69123
diff
changeset
|
167 (catch 'match |
71cb863df348
(glasses-separate-parentheses-exceptions): New. Exceptions to the rule "add
Juanma Barranquero <lekktu@gmail.com>
parents:
69123
diff
changeset
|
168 (dolist (re glasses-separate-parentheses-exceptions) |
71cb863df348
(glasses-separate-parentheses-exceptions): New. Exceptions to the rule "add
Juanma Barranquero <lekktu@gmail.com>
parents:
69123
diff
changeset
|
169 (and (string-match re str) (throw 'match t))))))) |
27469 | 170 |
171 (defun glasses-set-overlay-properties () | |
172 "Set properties of glasses overlays. | |
173 Consider current setting of user variables." | |
174 ;; In-identifier overlay | |
175 (put 'glasses 'evaporate t) | |
176 (put 'glasses 'before-string glasses-separator) | |
177 (put 'glasses 'face glasses-face) | |
178 ;; Beg-identifier overlay | |
179 (put 'glasses-init 'evaporate t) | |
28498
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
180 (put 'glasses-init 'face glasses-face) |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
181 ;; Parenthesis overlay |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
182 (put 'glasses-parenthesis 'evaporate t) |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
183 (put 'glasses-parenthesis 'before-string " ")) |
27469 | 184 |
185 (glasses-set-overlay-properties) | |
186 | |
187 | |
188 (defun glasses-overlay-p (overlay) | |
189 "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
|
190 (memq (overlay-get overlay 'category) |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
191 '(glasses glasses-init glasses-parenthesis))) |
27469 | 192 |
193 | |
28498
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
194 (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
|
195 "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
|
196 CATEGORY is the overlay category. If it is nil, use the `glasses' category." |
27469 | 197 (let ((overlay (make-overlay beg end))) |
28498
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
198 (overlay-put overlay 'category (or category 'glasses)) |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
199 overlay)) |
27469 | 200 |
201 | |
202 (defun glasses-make-readable (beg end) | |
203 "Make identifiers in the region from BEG to END readable." | |
204 (let ((case-fold-search nil)) | |
205 (save-excursion | |
206 (save-match-data | |
207 ;; Face only | |
208 (goto-char beg) | |
209 (while (re-search-forward | |
210 "\\<\\([A-Z]\\)[a-zA-Z]*\\([a-z][A-Z]\\|[A-Z][a-z]\\)" | |
211 end t) | |
28498
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
212 (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
|
213 'glasses-init)) |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
214 ;; Face + separator |
27469 | 215 (goto-char beg) |
216 (while (re-search-forward "[a-z]\\([A-Z]\\)\\|[A-Z]\\([A-Z]\\)[a-z]" | |
217 end t) | |
28498
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
218 (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
|
219 (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
|
220 (goto-char (match-beginning n)) |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
221 (when (and glasses-uncapitalize-p |
30523
87bca20b7a83
(glasses-make-readable): Fix uncapitalization of
Gerd Moellmann <gerd@gnu.org>
parents:
28810
diff
changeset
|
222 (save-match-data |
87bca20b7a83
(glasses-make-readable): Fix uncapitalization of
Gerd Moellmann <gerd@gnu.org>
parents:
28810
diff
changeset
|
223 (looking-at "[A-Z]\\($\\|[^A-Z]\\)")) |
28498
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
224 (save-excursion |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
225 (save-match-data |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
226 (re-search-backward "\\<.") |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
227 (looking-at glasses-uncapitalize-regexp)))) |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
228 (overlay-put o 'invisible t) |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
229 (overlay-put o 'after-string (downcase (match-string n)))))) |
65564
d84981e3d7d4
(glasses-make-readable): If glasses-separator differs from underscore,
Eli Zaretskii <eliz@gnu.org>
parents:
64085
diff
changeset
|
230 ;; Separator change |
68790
29a4ce0514ee
(glasses-separator): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
68773
diff
changeset
|
231 (when (and (not (string= glasses-original-separator glasses-separator)) |
29a4ce0514ee
(glasses-separator): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
68773
diff
changeset
|
232 (not (string= glasses-original-separator ""))) |
65564
d84981e3d7d4
(glasses-make-readable): If glasses-separator differs from underscore,
Eli Zaretskii <eliz@gnu.org>
parents:
64085
diff
changeset
|
233 (goto-char beg) |
68790
29a4ce0514ee
(glasses-separator): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
68773
diff
changeset
|
234 (let ((original-regexp (regexp-quote glasses-original-separator))) |
29a4ce0514ee
(glasses-separator): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
68773
diff
changeset
|
235 (while (re-search-forward |
29a4ce0514ee
(glasses-separator): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
68773
diff
changeset
|
236 (format "[a-zA-Z0-9]\\(\\(%s\\)+\\)[a-zA-Z0-9]" |
29a4ce0514ee
(glasses-separator): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
68773
diff
changeset
|
237 original-regexp) |
29a4ce0514ee
(glasses-separator): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
68773
diff
changeset
|
238 end t) |
29a4ce0514ee
(glasses-separator): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
68773
diff
changeset
|
239 (goto-char (match-beginning 1)) |
29a4ce0514ee
(glasses-separator): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
68773
diff
changeset
|
240 (while (looking-at original-regexp) |
29a4ce0514ee
(glasses-separator): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
68773
diff
changeset
|
241 (let ((o (glasses-make-overlay (point) (1+ (point))))) |
29a4ce0514ee
(glasses-separator): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
68773
diff
changeset
|
242 ;; `concat' ensures the character properties won't merge |
29a4ce0514ee
(glasses-separator): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
68773
diff
changeset
|
243 (overlay-put o 'display (concat glasses-separator))) |
29a4ce0514ee
(glasses-separator): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
68773
diff
changeset
|
244 (goto-char (match-end 0)))))) |
28498
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
245 ;; Parentheses |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
246 (when glasses-separate-parentheses-p |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
247 (goto-char beg) |
38176
2a85f8119924
(glasses-make-readable): Consider
Gerd Moellmann <gerd@gnu.org>
parents:
34473
diff
changeset
|
248 (while (re-search-forward "[a-zA-Z]_*\\(\(\\)" end t) |
74079
71cb863df348
(glasses-separate-parentheses-exceptions): New. Exceptions to the rule "add
Juanma Barranquero <lekktu@gmail.com>
parents:
69123
diff
changeset
|
249 (unless (glasses-parenthesis-exception-p (point-at-bol) (match-end 1)) |
71cb863df348
(glasses-separate-parentheses-exceptions): New. Exceptions to the rule "add
Juanma Barranquero <lekktu@gmail.com>
parents:
69123
diff
changeset
|
250 (glasses-make-overlay (match-beginning 1) (match-end 1) |
71cb863df348
(glasses-separate-parentheses-exceptions): New. Exceptions to the rule "add
Juanma Barranquero <lekktu@gmail.com>
parents:
69123
diff
changeset
|
251 'glasses-parenthesis)))))))) |
27469 | 252 |
253 | |
254 (defun glasses-make-unreadable (beg end) | |
255 "Return identifiers in the region from BEG to END to their unreadable state." | |
256 (dolist (o (overlays-in beg end)) | |
257 (when (glasses-overlay-p o) | |
258 (delete-overlay o)))) | |
259 | |
260 | |
261 (defun glasses-convert-to-unreadable () | |
262 "Convert current buffer to unreadable identifiers and return nil. | |
263 This function modifies buffer contents, it removes all the separators, | |
264 recognized according to the current value of the variable `glasses-separator'." | |
74079
71cb863df348
(glasses-separate-parentheses-exceptions): New. Exceptions to the rule "add
Juanma Barranquero <lekktu@gmail.com>
parents:
69123
diff
changeset
|
265 (when glasses-convert-on-write-p |
28810
4756047a4398
(glasses-convert-to-unreadable): Use
Gerd Moellmann <gerd@gnu.org>
parents:
28498
diff
changeset
|
266 (let ((case-fold-search nil) |
4756047a4398
(glasses-convert-to-unreadable): Use
Gerd Moellmann <gerd@gnu.org>
parents:
28498
diff
changeset
|
267 (separator (regexp-quote glasses-separator))) |
27469 | 268 (save-excursion |
74079
71cb863df348
(glasses-separate-parentheses-exceptions): New. Exceptions to the rule "add
Juanma Barranquero <lekktu@gmail.com>
parents:
69123
diff
changeset
|
269 (unless (string= glasses-separator "") |
68790
29a4ce0514ee
(glasses-separator): Doc fix.
Eli Zaretskii <eliz@gnu.org>
parents:
68773
diff
changeset
|
270 (goto-char (point-min)) |
74079
71cb863df348
(glasses-separate-parentheses-exceptions): New. Exceptions to the rule "add
Juanma Barranquero <lekktu@gmail.com>
parents:
69123
diff
changeset
|
271 (while (re-search-forward |
71cb863df348
(glasses-separate-parentheses-exceptions): New. Exceptions to the rule "add
Juanma Barranquero <lekktu@gmail.com>
parents:
69123
diff
changeset
|
272 (format "[a-z]\\(%s\\)[A-Z]\\|[A-Z]\\(%s\\)[A-Z][a-z]" |
71cb863df348
(glasses-separate-parentheses-exceptions): New. Exceptions to the rule "add
Juanma Barranquero <lekktu@gmail.com>
parents:
69123
diff
changeset
|
273 separator separator) |
71cb863df348
(glasses-separate-parentheses-exceptions): New. Exceptions to the rule "add
Juanma Barranquero <lekktu@gmail.com>
parents:
69123
diff
changeset
|
274 nil t) |
71cb863df348
(glasses-separate-parentheses-exceptions): New. Exceptions to the rule "add
Juanma Barranquero <lekktu@gmail.com>
parents:
69123
diff
changeset
|
275 (let ((n (if (match-string 1) 1 2))) |
71cb863df348
(glasses-separate-parentheses-exceptions): New. Exceptions to the rule "add
Juanma Barranquero <lekktu@gmail.com>
parents:
69123
diff
changeset
|
276 (replace-match "" t nil nil n) |
71cb863df348
(glasses-separate-parentheses-exceptions): New. Exceptions to the rule "add
Juanma Barranquero <lekktu@gmail.com>
parents:
69123
diff
changeset
|
277 (goto-char (match-end n)))) |
71cb863df348
(glasses-separate-parentheses-exceptions): New. Exceptions to the rule "add
Juanma Barranquero <lekktu@gmail.com>
parents:
69123
diff
changeset
|
278 (unless (string= glasses-separator glasses-original-separator) |
71cb863df348
(glasses-separate-parentheses-exceptions): New. Exceptions to the rule "add
Juanma Barranquero <lekktu@gmail.com>
parents:
69123
diff
changeset
|
279 (goto-char (point-min)) |
71cb863df348
(glasses-separate-parentheses-exceptions): New. Exceptions to the rule "add
Juanma Barranquero <lekktu@gmail.com>
parents:
69123
diff
changeset
|
280 (while (re-search-forward (format "[a-zA-Z0-9]\\(%s+\\)[a-zA-Z0-9]" |
71cb863df348
(glasses-separate-parentheses-exceptions): New. Exceptions to the rule "add
Juanma Barranquero <lekktu@gmail.com>
parents:
69123
diff
changeset
|
281 separator) |
71cb863df348
(glasses-separate-parentheses-exceptions): New. Exceptions to the rule "add
Juanma Barranquero <lekktu@gmail.com>
parents:
69123
diff
changeset
|
282 nil t) |
71cb863df348
(glasses-separate-parentheses-exceptions): New. Exceptions to the rule "add
Juanma Barranquero <lekktu@gmail.com>
parents:
69123
diff
changeset
|
283 (replace-match glasses-original-separator nil nil nil 1) |
71cb863df348
(glasses-separate-parentheses-exceptions): New. Exceptions to the rule "add
Juanma Barranquero <lekktu@gmail.com>
parents:
69123
diff
changeset
|
284 (goto-char (match-beginning 1))))) |
28498
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
285 (when glasses-separate-parentheses-p |
809168962c9b
Provide facilities for inserting space before left
Gerd Moellmann <gerd@gnu.org>
parents:
27469
diff
changeset
|
286 (goto-char (point-min)) |
39088
2fc21ed3beac
(glasses-convert-to-unreadable): Consider
Gerd Moellmann <gerd@gnu.org>
parents:
38176
diff
changeset
|
287 (while (re-search-forward "[a-zA-Z]_*\\( \\)\(" nil t) |
74079
71cb863df348
(glasses-separate-parentheses-exceptions): New. Exceptions to the rule "add
Juanma Barranquero <lekktu@gmail.com>
parents:
69123
diff
changeset
|
288 (unless (glasses-parenthesis-exception-p (point-at-bol) (1+ (match-end 1))) |
71cb863df348
(glasses-separate-parentheses-exceptions): New. Exceptions to the rule "add
Juanma Barranquero <lekktu@gmail.com>
parents:
69123
diff
changeset
|
289 (replace-match "" t nil nil 1))))))) |
27469 | 290 ;; nil must be returned to allow use in write file hooks |
291 nil) | |
292 | |
293 | |
34473
e859388caea4
Use `define-minor-mode' for the minor mode
Gerd Moellmann <gerd@gnu.org>
parents:
34338
diff
changeset
|
294 (defun glasses-change (beg end &optional old-len) |
27469 | 295 "After-change function updating glass overlays." |
296 (let ((beg-line (save-excursion (goto-char beg) (line-beginning-position))) | |
297 (end-line (save-excursion (goto-char end) (line-end-position)))) | |
298 (glasses-make-unreadable beg-line end-line) | |
299 (glasses-make-readable beg-line end-line))) | |
300 | |
301 | |
302 ;;; Minor mode definition | |
303 | |
304 | |
305 ;;;###autoload | |
34473
e859388caea4
Use `define-minor-mode' for the minor mode
Gerd Moellmann <gerd@gnu.org>
parents:
34338
diff
changeset
|
306 (define-minor-mode glasses-mode |
27469 | 307 "Minor mode for making identifiers likeThis readable. |
308 When this mode is active, it tries to add virtual separators (like underscores) | |
309 at places they belong to." | |
61285
bb8465da2472
(glasses-mode): Specify :group.
Lute Kamstra <lute@gnu.org>
parents:
52401
diff
changeset
|
310 :group 'glasses :lighter " o^o" |
34473
e859388caea4
Use `define-minor-mode' for the minor mode
Gerd Moellmann <gerd@gnu.org>
parents:
34338
diff
changeset
|
311 (save-excursion |
e859388caea4
Use `define-minor-mode' for the minor mode
Gerd Moellmann <gerd@gnu.org>
parents:
34338
diff
changeset
|
312 (save-restriction |
e859388caea4
Use `define-minor-mode' for the minor mode
Gerd Moellmann <gerd@gnu.org>
parents:
34338
diff
changeset
|
313 (widen) |
e859388caea4
Use `define-minor-mode' for the minor mode
Gerd Moellmann <gerd@gnu.org>
parents:
34338
diff
changeset
|
314 ;; We erase all the overlays anyway, to avoid dual sight in some |
e859388caea4
Use `define-minor-mode' for the minor mode
Gerd Moellmann <gerd@gnu.org>
parents:
34338
diff
changeset
|
315 ;; circumstances |
e859388caea4
Use `define-minor-mode' for the minor mode
Gerd Moellmann <gerd@gnu.org>
parents:
34338
diff
changeset
|
316 (glasses-make-unreadable (point-min) (point-max)) |
e859388caea4
Use `define-minor-mode' for the minor mode
Gerd Moellmann <gerd@gnu.org>
parents:
34338
diff
changeset
|
317 (if glasses-mode |
e859388caea4
Use `define-minor-mode' for the minor mode
Gerd Moellmann <gerd@gnu.org>
parents:
34338
diff
changeset
|
318 (progn |
e859388caea4
Use `define-minor-mode' for the minor mode
Gerd Moellmann <gerd@gnu.org>
parents:
34338
diff
changeset
|
319 (jit-lock-register 'glasses-change) |
e859388caea4
Use `define-minor-mode' for the minor mode
Gerd Moellmann <gerd@gnu.org>
parents:
34338
diff
changeset
|
320 (add-hook 'local-write-file-hooks |
e859388caea4
Use `define-minor-mode' for the minor mode
Gerd Moellmann <gerd@gnu.org>
parents:
34338
diff
changeset
|
321 'glasses-convert-to-unreadable nil t)) |
e859388caea4
Use `define-minor-mode' for the minor mode
Gerd Moellmann <gerd@gnu.org>
parents:
34338
diff
changeset
|
322 (jit-lock-unregister 'glasses-change) |
e859388caea4
Use `define-minor-mode' for the minor mode
Gerd Moellmann <gerd@gnu.org>
parents:
34338
diff
changeset
|
323 (remove-hook 'local-write-file-hooks |
e859388caea4
Use `define-minor-mode' for the minor mode
Gerd Moellmann <gerd@gnu.org>
parents:
34338
diff
changeset
|
324 'glasses-convert-to-unreadable t))))) |
27469 | 325 |
326 | |
327 ;;; Announce | |
328 | |
329 (provide 'glasses) | |
330 | |
331 | |
69123
618d3bbab8be
Remove unnecessary leading * in docstrings.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
68790
diff
changeset
|
332 ;; arch-tag: a3515167-c89e-484f-90a1-d85143e52b12 |
27469 | 333 ;;; glasses.el ends here |