Mercurial > emacs
annotate lisp/progmodes/hideif.el @ 1821:04fb1d3d6992
JimB's changes since January 18th
author | Jim Blandy <jimb@redhat.com> |
---|---|
date | Tue, 26 Jan 1993 01:58:16 +0000 |
parents | 10c59683079d |
children | 10e417efb12a |
rev | line source |
---|---|
793
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
1 ;;; hide-ifdef-mode.el --- hides selected code within ifdef. |
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
2 |
895
81e6b85a9261
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
853
diff
changeset
|
3 ;;; Copyright (C) 1988 Free Software Foundation, Inc. |
846
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
811
diff
changeset
|
4 |
811
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
793
diff
changeset
|
5 ;; Author: Dan LaLiberte <liberte@a.cs.uiuc.edu> |
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
793
diff
changeset
|
6 ;; Keywords: c |
793
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
7 |
895
81e6b85a9261
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
853
diff
changeset
|
8 ;; This file is part of GNU Emacs. |
81e6b85a9261
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
853
diff
changeset
|
9 |
81e6b85a9261
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
853
diff
changeset
|
10 ;; GNU Emacs is free software; you can redistribute it and/or modify |
81e6b85a9261
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
853
diff
changeset
|
11 ;; it under the terms of the GNU General Public License as published by |
81e6b85a9261
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
853
diff
changeset
|
12 ;; the Free Software Foundation; either version 2, or (at your option) |
81e6b85a9261
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
853
diff
changeset
|
13 ;; any later version. |
81e6b85a9261
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
853
diff
changeset
|
14 |
81e6b85a9261
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
853
diff
changeset
|
15 ;; GNU Emacs is distributed in the hope that it will be useful, |
81e6b85a9261
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
853
diff
changeset
|
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
81e6b85a9261
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
853
diff
changeset
|
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
81e6b85a9261
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
853
diff
changeset
|
18 ;; GNU General Public License for more details. |
81e6b85a9261
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
853
diff
changeset
|
19 |
81e6b85a9261
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
853
diff
changeset
|
20 ;; You should have received a copy of the GNU General Public License |
81e6b85a9261
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
853
diff
changeset
|
21 ;; along with GNU Emacs; see the file COPYING. If not, write to |
81e6b85a9261
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
853
diff
changeset
|
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
81e6b85a9261
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
853
diff
changeset
|
23 |
793
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
24 ;;; Commentary: |
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
25 |
31 | 26 ;;; Written by Brian Marick, at Gould, Computer Systems Division, Urbana IL. |
27 ;;; Extensively modified by Daniel LaLiberte (while at Gould). | |
28 ;;; | |
29 ;;; You may freely modify and distribute this, but keep a record | |
30 ;;; of modifications and send comments to: | |
31 ;;; liberte@a.cs.uiuc.edu or ihnp4!uiucdcs!liberte | |
32 ;;; I will continue to upgrade hide-ifdef-mode | |
793
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
33 ;;; with your contributions. |
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
34 |
31 | 35 ;;; To initialize, toggle the hide-ifdef minor mode with |
36 ;;; | |
37 ;;; M-x hide-ifdef-mode | |
38 ;;; | |
39 ;;; This will set up key bindings and call hide-ifdef-mode-hook if it | |
40 ;;; has a value. To explicitly hide ifdefs using a buffer-local | |
41 ;;; define list (default empty), type | |
42 ;;; | |
43 ;;; M-x hide-ifdefs or C-c h | |
44 ;;; | |
45 ;;; Hide-ifdef suppresses the display of code that the preprocessor wouldn't | |
46 ;;; pass through. The support of constant expressions in #if lines is | |
47 ;;; limited to identifiers, parens, and the operators: &&, ||, !, and | |
48 ;;; "defined". Please extend this. | |
49 ;;; | |
50 ;;; The hidden code is marked by ellipses (...). Be | |
51 ;;; cautious when editing near ellipses, since the hidden text is | |
52 ;;; still in the buffer, and you can move the point into it and modify | |
53 ;;; text unawares. If you don't want to see the ellipses, set | |
54 ;;; selective-display-ellipses to nil. But this can be dangerous. | |
55 ;;; You can make your buffer read-only while hide-ifdef-hiding by setting | |
56 ;;; hide-ifdef-read-only to a non-nil value. You can toggle this | |
57 ;;; variable with hide-ifdef-toggle-read-only (C-c C-q). | |
58 ;;; | |
59 ;;; You can undo the effect of hide-ifdefs by typing | |
60 ;;; | |
61 ;;; M-x show-ifdefs or C-c s | |
62 ;;; | |
63 ;;; Use M-x hide-ifdef-define (C-c d) to define a symbol. | |
64 ;;; Use M-x hide-ifdef-undef (C-c u) to undefine a symbol. | |
65 ;;; | |
66 ;;; If you define or undefine a symbol while hide-ifdef-mode is in effect, | |
67 ;;; the display will be updated. Only the define list for the current | |
68 ;;; buffer will be affected. You can save changes to the local define | |
69 ;;; list with hide-ifdef-set-define-alist. This adds entries | |
70 ;;; to hide-ifdef-define-alist. | |
71 ;;; | |
72 ;;; If you have defined a hide-ifdef-mode-hook, you can set | |
73 ;;; up a list of symbols that may be used by hide-ifdefs as in the | |
74 ;;; following example: | |
75 ;;; | |
76 ;;; (setq hide-ifdef-mode-hook | |
77 ;;; '(lambda () | |
78 ;;; (if (not hide-ifdef-define-alist) | |
79 ;;; (setq hide-ifdef-define-alist | |
80 ;;; '((list1 ONE TWO) | |
81 ;;; (list2 TWO THREE) | |
82 ;;; ))) | |
83 ;;; (hide-ifdef-use-define-alist 'list2) ; use list2 by default | |
84 ;;; )) | |
85 ;;; | |
86 ;;; You can call hide-ifdef-use-define-alist (C-c u) at any time to specify | |
87 ;;; another list to use. | |
88 ;;; | |
89 ;;; To cause ifdefs to be hidden as soon as hide-ifdef-mode is called, | |
90 ;;; set hide-ifdef-initially to non-nil. | |
91 ;;; | |
92 ;;; If you set hide-ifdef-lines to t, hide-ifdefs hides all the #ifdef lines. | |
93 ;;; In the absence of highlighting, that might be a bad idea. If you set | |
94 ;;; hide-ifdef-lines to nil (the default), the surrounding preprocessor | |
95 ;;; lines will be displayed. That can be confusing in its own | |
96 ;;; right. Other variations on display are possible, but not much | |
97 ;;; better. | |
98 ;;; | |
99 ;;; You can explicitly hide or show individual ifdef blocks irrespective | |
100 ;;; of the define list by using hide-ifdef-block and show-ifdef-block. | |
101 ;;; | |
102 ;;; You can move the point between ifdefs with forward-ifdef, backward-ifdef, | |
103 ;;; up-ifdef, down-ifdef, next-ifdef, and previous-ifdef. | |
104 ;;; | |
105 ;;; If you have minor-mode-alist in your mode line (the default) two labels | |
106 ;;; may appear. "Ifdef" will appear when hide-ifdef-mode is active. "Hiding" | |
107 ;;; will appear when text may be hidden ("hide-ifdef-hiding" is non-nil). | |
108 | |
793
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
109 ;;; Change Log: |
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
110 ;;; |
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
111 ;;; Revision 1.7 88/02/16 03:12:58 liberte |
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
112 ;;; Fixed comments and doc strings. |
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
113 ;;; Added optional prefix arg for ifdef motion commands. |
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
114 ;;; |
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
115 ;;; Revision 1.6 88/02/05 00:36:18 liberte |
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
116 ;;; Bug fixes. |
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
117 ;;; 1. A multi-line comment that starts on an #ifdef line |
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
118 ;;; now ends on that line. |
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
119 ;;; 2. Fix bad function name: hide-hif-ifdef-toggle-read-only |
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
120 ;;; 3. Make ifdef-block hiding work outside of ifdefs. |
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
121 ;;; |
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
122 ;;; Revision 1.5 88/01/31 23:19:31 liberte |
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
123 ;;; Major clean up. |
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
124 ;;; Prefix internal names with "hif-". |
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
125 ;;; |
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
126 ;;; Revision 1.4 88/01/30 14:09:38 liberte |
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
127 ;;; Add hide-ifdef-hiding and hide-ifdef-mode to minor-mode-alist. |
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
128 ;;; |
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
129 ;;; Revision 1.3 88/01/29 00:38:19 liberte |
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
130 ;;; Fix three bugs. |
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
131 ;;; 1. Function "defined" is just like lookup. |
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
132 ;;; 2. Skip to newline or cr in case text is hidden. |
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
133 ;;; 3. Use car of token list if just one symbol. |
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
134 ;;; |
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
135 ;;; Revision 1.2 88/01/28 23:32:46 liberte |
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
136 ;;; Use hide-ifdef-mode-prefix-key. |
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
137 ;;; Copy current-local-map so other buffers do not get |
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
138 ;;; hide-ifdef-mode bindings. |
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
139 ;;; |
31 | 140 |
793
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
141 ;;; Code: |
31 | 142 |
143 (defvar hide-ifdef-mode-map nil | |
210 | 144 "Keymap used with Hide-Ifdef mode") |
31 | 145 |
146 (defconst hide-ifdef-mode-prefix-key "\C-c" | |
210 | 147 "Prefix key for all Hide-Ifdef mode commands.") |
31 | 148 |
149 (defvar hide-ifdef-mode-map-before nil | |
150 "Buffer-local variable to store a copy of the local keymap | |
210 | 151 before `hide-ifdef-mode' modifies it.") |
31 | 152 |
153 (defun define-hide-ifdef-mode-map () | |
154 (if hide-ifdef-mode-map | |
155 () ; dont redefine it. | |
156 (setq hide-ifdef-mode-map (make-sparse-keymap)) | |
157 (define-key hide-ifdef-mode-map "d" 'hide-ifdef-define) | |
158 (define-key hide-ifdef-mode-map "u" 'hide-ifdef-undef) | |
159 (define-key hide-ifdef-mode-map "D" 'hide-ifdef-set-define-alist) | |
160 (define-key hide-ifdef-mode-map "U" 'hide-ifdef-use-define-alist) | |
161 | |
162 (define-key hide-ifdef-mode-map "h" 'hide-ifdefs) | |
163 (define-key hide-ifdef-mode-map "s" 'show-ifdefs) | |
164 (define-key hide-ifdef-mode-map "\C-h" 'hide-ifdef-block) | |
165 (define-key hide-ifdef-mode-map "\C-s" 'show-ifdef-block) | |
166 | |
167 (define-key hide-ifdef-mode-map "\C-f" 'forward-ifdef) | |
168 (define-key hide-ifdef-mode-map "\C-b" 'backward-ifdef) | |
169 (define-key hide-ifdef-mode-map "\C-d" 'down-ifdef) | |
170 (define-key hide-ifdef-mode-map "\C-u" 'up-ifdef) | |
171 (define-key hide-ifdef-mode-map "\C-n" 'next-ifdef) | |
172 (define-key hide-ifdef-mode-map "\C-p" 'previous-ifdef) | |
173 (define-key hide-ifdef-mode-map "\C-q" 'hide-ifdef-toggle-read-only) | |
174 (define-key hide-ifdef-mode-map | |
175 (where-is-internal 'toggle-read-only nil nil t) | |
176 'hide-ifdef-toggle-outside-read-only) | |
177 ) | |
178 (fset 'hide-ifdef-mode-map hide-ifdef-mode-map) ; the function is the map | |
179 ) | |
180 | |
181 (defun hif-update-mode-line () | |
182 "Update mode-line by setting buffer-modified to itself." | |
183 (set-buffer-modified-p (buffer-modified-p))) | |
184 | |
185 (defvar hide-ifdef-mode nil | |
186 "non-nil when hide-ifdef-mode is activated.") | |
187 | |
188 (defvar hide-ifdef-hiding nil | |
189 "non-nil when text may be hidden.") | |
190 | |
191 (or (assq 'hide-ifdef-hiding minor-mode-alist) | |
192 (setq minor-mode-alist | |
193 (cons '(hide-ifdef-hiding " Hiding") | |
194 minor-mode-alist))) | |
195 | |
196 (or (assq 'hide-ifdef-mode minor-mode-alist) | |
197 (setq minor-mode-alist | |
198 (cons '(hide-ifdef-mode " Ifdef") | |
199 minor-mode-alist))) | |
200 | |
998 | 201 ;;;###autoload |
31 | 202 (defun hide-ifdef-mode (arg) |
210 | 203 "Toggle Hide-Ifdef mode. This is a minor mode, albeit a large one. |
204 With ARG, turn Hide-Ifdef mode on iff arg is positive. | |
205 In Hide-Ifdef mode, code within #ifdef constructs that the C preprocessor | |
31 | 206 would eliminate may be hidden from view. Several variables affect |
207 how the hiding is done: | |
208 | |
209 hide-ifdef-env | |
210 An association list of defined and undefined symbols for the | |
210 | 211 current buffer. Initially, the global value of `hide-ifdef-env' |
212 is used. | |
31 | 213 |
214 hide-ifdef-define-alist | |
215 An association list of defined symbol lists. | |
210 | 216 Use `hide-ifdef-set-define-alist' to save the current `hide-ifdef-env' |
217 and `hide-ifdef-use-define-alist' to set the current `hide-ifdef-env' | |
218 from one of the lists in `hide-ifdef-define-alist'. | |
31 | 219 |
220 hide-ifdef-lines | |
221 Set to non-nil to not show #if, #ifdef, #ifndef, #else, and | |
222 #endif lines when hiding. | |
223 | |
224 hide-ifdef-initially | |
210 | 225 Indicates whether `hide-ifdefs' should be called when Hide-Ifdef mode |
31 | 226 is activated. |
227 | |
228 hide-ifdef-read-only | |
229 Set to non-nil if you want to make buffers read only while hiding. | |
210 | 230 After `show-ifdefs', read-only status is restored to previous value. |
31 | 231 |
232 \\{hide-ifdef-mode-map}" | |
233 | |
234 (interactive "P") | |
235 (make-local-variable 'hide-ifdef-mode) | |
236 (setq hide-ifdef-mode | |
237 (if (null arg) | |
238 (not hide-ifdef-mode) | |
239 (> (prefix-numeric-value arg) 0))) | |
240 | |
241 (hif-update-mode-line) | |
242 | |
243 (if hide-ifdef-mode | |
244 (progn | |
245 ; fix c-mode syntax table so we can recognize whole symbols. | |
246 (modify-syntax-entry ?_ "w") | |
247 (modify-syntax-entry ?& ".") | |
248 (modify-syntax-entry ?\| ".") | |
249 | |
250 ; inherit global values | |
251 (make-local-variable 'hide-ifdef-env) | |
252 (setq hide-ifdef-env (default-value 'hide-ifdef-env)) | |
253 | |
254 (make-local-variable 'hide-ifdef-hiding) | |
255 (setq hide-ifdef-hiding (default-value 'hide-ifdef-hiding)) | |
256 | |
257 (make-local-variable 'hif-outside-read-only) | |
258 (setq hif-outside-read-only buffer-read-only) | |
259 | |
260 (make-local-variable 'hide-ifdef-mode-map-before) | |
261 (setq hide-ifdef-mode-map-before (current-local-map)) | |
262 (use-local-map (copy-keymap (current-local-map))) | |
263 (local-unset-key hide-ifdef-mode-prefix-key) | |
264 (local-set-key hide-ifdef-mode-prefix-key 'hide-ifdef-mode-map) | |
265 (define-hide-ifdef-mode-map) | |
266 | |
267 (run-hooks 'hide-ifdef-mode-hook) | |
268 | |
269 (if hide-ifdef-initially | |
270 (hide-ifdefs) | |
271 (show-ifdefs)) | |
272 (message "Enter hide-ifdef-mode.") | |
273 ) | |
274 ; else end hide-ifdef-mode | |
275 (if hide-ifdef-hiding | |
276 (show-ifdefs)) | |
277 (use-local-map hide-ifdef-mode-map-before) | |
278 (message "Exit hide-ifdef-mode.") | |
279 )) | |
280 | |
281 | |
282 ;; from outline.el with docstring fixed. | |
283 (defun hif-outline-flag-region (from to flag) | |
284 "Hides or shows lines from FROM to TO, according to FLAG. If FLAG | |
285 is \\n (newline character) then text is shown, while if FLAG is \\^M | |
286 \(control-M) the text is hidden." | |
287 (let ((modp (buffer-modified-p))) | |
288 (unwind-protect (progn | |
289 (subst-char-in-region from to | |
290 (if (= flag ?\n) ?\^M ?\n) | |
291 flag t) ) | |
292 (set-buffer-modified-p modp)) | |
293 )) | |
294 | |
295 (defun hif-show-all () | |
296 "Show all of the text in the current buffer." | |
297 (interactive) | |
298 (hif-outline-flag-region (point-min) (point-max) ?\n)) | |
299 | |
300 (defun hide-ifdef-region (start end) | |
301 "START is the start of a #if or #else form. END is the ending part. | |
302 Everything including these lines is made invisible." | |
303 (hif-outline-flag-region start end ?\^M) | |
304 ) | |
305 | |
306 (defun hif-show-ifdef-region (start end) | |
307 "Everything between START and END is made visible." | |
308 (hif-outline-flag-region start end ?\n) | |
309 ) | |
310 | |
311 | |
312 | |
313 ;===%%SF%% evaluation (Start) === | |
314 | |
315 (defvar hide-ifdef-evaluator 'eval | |
316 "The evaluator is given a canonical form and returns T if text under | |
317 that form should be displayed.") | |
318 | |
319 (defvar hif-undefined-symbol nil | |
320 "...is by default considered to be false.") | |
321 | |
322 (defvar hide-ifdef-env nil | |
323 "An alist of defined symbols and their values.") | |
324 | |
325 | |
326 (defun hif-set-var (var value) | |
327 "Prepend (var value) pair to hide-ifdef-env." | |
328 (setq hide-ifdef-env (cons (cons var value) hide-ifdef-env))) | |
329 | |
330 | |
331 (defun hif-lookup (var) | |
332 ; (message "hif-lookup %s" var) | |
333 (let ((val (assoc var hide-ifdef-env))) | |
334 (if val | |
335 (cdr val) | |
336 hif-undefined-symbol))) | |
337 | |
338 (defun hif-defined (var) | |
339 (hif-lookup var) | |
340 ; when #if expressions are fully supported, defined result should be 1 | |
341 ; (if (assoc var hide-ifdef-env) | |
342 ; 1 | |
343 ; nil) | |
344 ) | |
345 | |
346 | |
347 ;===%%SF%% evaluation (End) === | |
348 | |
349 | |
350 | |
351 ;===%%SF%% parsing (Start) === | |
352 ;;; The code that understands what ifs and ifdef in files look like. | |
353 | |
354 (defconst hif-cpp-prefix "\\(^\\|\r\\)[ \t]*#[ \t]*") | |
355 (defconst hif-ifndef-regexp (concat hif-cpp-prefix "ifndef")) | |
356 (defconst hif-ifx-regexp (concat hif-cpp-prefix "if\\(n?def\\)?[ \t]+")) | |
357 (defconst hif-else-regexp (concat hif-cpp-prefix "else")) | |
358 (defconst hif-endif-regexp (concat hif-cpp-prefix "endif")) | |
359 (defconst hif-ifx-else-endif-regexp | |
360 (concat hif-ifx-regexp "\\|" hif-else-regexp "\\|" hif-endif-regexp)) | |
361 | |
362 | |
363 (defun hif-infix-to-prefix (token-list) | |
364 "Convert list of tokens in infix into prefix list" | |
365 ; (message "hif-infix-to-prefix: %s" token-list) | |
366 (if (= 1 (length token-list)) | |
367 (` (hif-lookup (quote (, (car token-list))))) | |
368 (hif-parse-if-exp token-list)) | |
369 ) | |
370 | |
371 ; pattern to match initial identifier, !, &&, ||, (, or ). | |
372 (defconst hif-token-regexp "^\\(!\\|&&\\|||\\|[()]\\|\\w+\\)") | |
373 (defconst hif-end-of-comment "\\*/") | |
374 | |
375 | |
376 (defun hif-tokenize (expr-string) | |
377 "Separate string into a list of tokens" | |
378 (let ((token-list nil) | |
379 (expr-start 0) | |
380 (expr-length (length expr-string))) | |
381 | |
382 (while (< expr-start expr-length) | |
383 ; (message "expr-start = %d" expr-start) (sit-for 1) | |
384 (cond | |
385 ((string-match "^[ \t]+" expr-string expr-start) | |
386 ; skip whitespace | |
387 (setq expr-start (match-end 0)) | |
388 ; stick newline in string so ^ matches on the next string-match | |
389 (aset expr-string (1- expr-start) ?\n) | |
390 ) | |
391 | |
392 ((string-match "^/\\*" expr-string expr-start) | |
393 (setq expr-start (match-end 0)) | |
394 (aset expr-string (1- expr-start) ?\n) | |
395 (or | |
396 (string-match hif-end-of-comment | |
397 expr-string expr-start) ; eat comment | |
398 (string-match "$" expr-string expr-start)) ; multi-line comment | |
399 (setq expr-start (match-end 0)) | |
400 (aset expr-string (1- expr-start) ?\n) | |
401 ) | |
402 | |
403 ((string-match hif-token-regexp expr-string expr-start) | |
404 (let ((token (substring expr-string expr-start (match-end 0)))) | |
405 (setq expr-start (match-end 0)) | |
406 (aset expr-string (1- expr-start) ?\n) | |
407 ; (message "token: %s" token) (sit-for 1) | |
408 (setq token-list | |
409 (cons | |
410 (cond | |
411 ((string-equal token "||") 'or) | |
412 ((string-equal token "&&") 'and) | |
413 ((string-equal token "!") 'not) | |
414 ((string-equal token "defined") 'hif-defined) | |
415 ((string-equal token "(") 'lparen) | |
416 ((string-equal token ")") 'rparen) | |
417 (t (intern token))) | |
418 token-list)) | |
419 )) | |
420 (t (error "Bad #if expression: %s" expr-string)) | |
421 )) | |
422 (nreverse token-list) | |
423 )) | |
424 | |
425 ;;;----------------------------------------------------------------- | |
426 ;;; Translate C preprocessor #if expressions using recursive descent. | |
427 ;;; This parser is limited to the operators &&, ||, !, and "defined". | |
428 | |
429 (defun hif-parse-if-exp (token-list) | |
430 "Parse the TOKEN-LIST. Return translated list in prefix form." | |
431 (hif-nexttoken) | |
432 (prog1 | |
433 (hif-expr) | |
434 (if token ; is there still a token? | |
210 | 435 (error "Error: unexpected token: %s" token)))) |
31 | 436 |
437 (defun hif-nexttoken () | |
438 "Pop the next token from token-list into the let variable \"token\"." | |
439 (setq token (car token-list)) | |
440 (setq token-list (cdr token-list)) | |
210 | 441 token) |
31 | 442 |
443 (defun hif-expr () | |
444 "Parse and expression of the form | |
445 expr : term | expr '||' term." | |
446 (let ((result (hif-term))) | |
447 (while (eq token 'or) | |
448 (hif-nexttoken) | |
449 (setq result (list 'or result (hif-term)))) | |
210 | 450 result)) |
31 | 451 |
452 (defun hif-term () | |
453 "Parse a term of the form | |
454 term : factor | term '&&' factor." | |
455 (let ((result (hif-factor))) | |
456 (while (eq token 'and) | |
457 (hif-nexttoken) | |
458 (setq result (list 'and result (hif-factor)))) | |
210 | 459 result)) |
31 | 460 |
461 (defun hif-factor () | |
462 "Parse a factor of the form | |
463 factor : '!' factor | '(' expr ')' | 'defined(' id ')' | id." | |
464 (cond | |
465 ((eq token 'not) | |
466 (hif-nexttoken) | |
467 (list 'not (hif-factor))) | |
468 | |
469 ((eq token 'lparen) | |
470 (hif-nexttoken) | |
471 (let ((result (hif-expr))) | |
472 (if (not (eq token 'rparen)) | |
473 (error "Bad token in parenthesized expression: %s" token) | |
474 (hif-nexttoken) | |
475 result))) | |
476 | |
477 ((eq token 'hif-defined) | |
478 (hif-nexttoken) | |
479 (if (not (eq token 'lparen)) | |
480 (error "Error: expected \"(\" after \"defined\"")) | |
481 (hif-nexttoken) | |
482 (let ((ident token)) | |
483 (if (memq token '(or and not hif-defined lparen rparen)) | |
484 (error "Error: unexpected token: %s" token)) | |
485 (hif-nexttoken) | |
486 (if (not (eq token 'rparen)) | |
487 (error "Error: expected \")\" after identifier")) | |
488 (hif-nexttoken) | |
489 (` (hif-defined (quote (, ident)))) | |
490 )) | |
491 | |
492 (t ; identifier | |
493 (let ((ident token)) | |
494 (if (memq ident '(or and)) | |
495 (error "Error: missing identifier")) | |
496 (hif-nexttoken) | |
497 (` (hif-lookup (quote (, ident)))) | |
498 )) | |
499 )) | |
500 | |
501 ;;;----------- end of parser ----------------------- | |
502 | |
503 | |
504 (defun hif-canonicalize () | |
505 "When at beginning of #ifX, returns a canonical (evaluatable) | |
506 form for the expression." | |
507 (save-excursion | |
508 (let ((negate (looking-at hif-ifndef-regexp))) | |
509 (re-search-forward hif-ifx-regexp) | |
510 (let* ((expr-string | |
511 (buffer-substring (point) | |
512 (progn (skip-chars-forward "^\n\r") (point)))) | |
513 (expr (hif-infix-to-prefix (hif-tokenize expr-string)))) | |
514 ; (message "hif-canonicalized: %s" expr) | |
515 (if negate | |
516 (list 'not expr) | |
517 expr))))) | |
518 | |
519 | |
520 (defun hif-find-any-ifX () | |
521 "Position at beginning of next #if, #ifdef, or #ifndef, including one on | |
522 this line." | |
523 ; (message "find ifX at %d" (point)) | |
524 (prog1 | |
525 (re-search-forward hif-ifx-regexp (point-max) t) | |
526 (beginning-of-line))) | |
527 | |
528 | |
529 (defun hif-find-next-relevant () | |
530 "Position at beginning of next #ifdef, #ifndef, #else, #endif, | |
531 NOT including one on this line." | |
532 ; (message "hif-find-next-relevant at %d" (point)) | |
533 (end-of-line) | |
534 ; avoid infinite recursion by only going to beginning of line if match found | |
535 (if (re-search-forward hif-ifx-else-endif-regexp (point-max) t) | |
210 | 536 (beginning-of-line))) |
31 | 537 |
538 (defun hif-find-previous-relevant () | |
539 "Position at beginning of previous #ifdef, #ifndef, #else, #endif, | |
540 NOT including one on this line." | |
541 ; (message "hif-find-previous-relevant at %d" (point)) | |
542 (beginning-of-line) | |
543 ; avoid infinite recursion by only going to beginning of line if match found | |
544 (if (re-search-backward hif-ifx-else-endif-regexp (point-min) t) | |
210 | 545 (beginning-of-line))) |
31 | 546 |
547 | |
548 (defun hif-looking-at-ifX () ;; Should eventually see #if | |
549 (looking-at hif-ifx-regexp)) | |
550 (defun hif-looking-at-endif () | |
551 (looking-at hif-endif-regexp)) | |
552 (defun hif-looking-at-else () | |
553 (looking-at hif-else-regexp)) | |
554 | |
555 | |
556 | |
557 (defun hif-ifdef-to-endif () | |
558 "If positioned at #ifX or #else form, skip to corresponding #endif." | |
559 ; (message "hif-ifdef-to-endif at %d" (point)) (sit-for 1) | |
560 (hif-find-next-relevant) | |
561 (cond ((hif-looking-at-ifX) | |
562 (hif-ifdef-to-endif) ; find endif of nested if | |
563 (hif-ifdef-to-endif)) ; find outer endif or else | |
564 ((hif-looking-at-else) | |
565 (hif-ifdef-to-endif)) ; find endif following else | |
566 ((hif-looking-at-endif) | |
567 'done) | |
568 (t | |
210 | 569 (error "Missmatched #ifdef #endif pair")))) |
31 | 570 |
571 | |
572 (defun hif-endif-to-ifdef () | |
573 "If positioned at #endif form, skip backward to corresponding #ifX." | |
574 ; (message "hif-endif-to-ifdef at %d" (point)) | |
575 (let ((start (point))) | |
576 (hif-find-previous-relevant) | |
577 (if (= start (point)) | |
578 (error "Missmatched #ifdef #endif pair"))) | |
579 (cond ((hif-looking-at-endif) | |
580 (hif-endif-to-ifdef) ; find beginning of nested if | |
581 (hif-endif-to-ifdef)) ; find beginning of outer if or else | |
582 ((hif-looking-at-else) | |
583 (hif-endif-to-ifdef)) | |
584 ((hif-looking-at-ifX) | |
585 'done) | |
922 | 586 (t))) ; never gets here |
31 | 587 |
588 | |
589 (defun forward-ifdef (&optional arg) | |
590 "Move point to beginning of line of the next ifdef-endif. | |
210 | 591 With argument, do this that many times." |
31 | 592 (interactive "p") |
593 (or arg (setq arg 1)) | |
594 (if (< arg 0) | |
595 (backward-ifdef (- arg))) | |
596 (while (< 0 arg) | |
597 (setq arg (- arg)) | |
598 (let ((start (point))) | |
599 (if (not (hif-looking-at-ifX)) | |
600 (hif-find-next-relevant)) | |
601 (if (hif-looking-at-ifX) | |
602 (hif-ifdef-to-endif) | |
603 (goto-char start) | |
604 (error "No following #ifdef") | |
605 )))) | |
606 | |
607 | |
608 (defun backward-ifdef (&optional arg) | |
609 "Move point to beginning of the previous ifdef-endif. | |
210 | 610 With argument, do this that many times." |
31 | 611 (interactive "p") |
612 (or arg (setq arg 1)) | |
613 (if (< arg 0) | |
614 (forward-ifdef (- arg))) | |
615 (while (< 0 arg) | |
616 (setq arg (1- arg)) | |
617 (beginning-of-line) | |
618 (let ((start (point))) | |
619 (if (not (hif-looking-at-endif)) | |
620 (hif-find-previous-relevant)) | |
621 (if (hif-looking-at-endif) | |
622 (hif-endif-to-ifdef) | |
623 (goto-char start) | |
210 | 624 (error "No previous #ifdef"))))) |
31 | 625 |
626 | |
627 (defun down-ifdef () | |
628 "Move point to beginning of nested ifdef or else-part." | |
629 (interactive) | |
630 (let ((start (point))) | |
631 (hif-find-next-relevant) | |
632 (if (or (hif-looking-at-ifX) (hif-looking-at-else)) | |
633 () | |
634 (goto-char start) | |
210 | 635 (error "No following #ifdef")))) |
31 | 636 |
637 | |
638 (defun up-ifdef () | |
639 "Move point to beginning of enclosing ifdef or else-part." | |
640 (interactive) | |
641 (beginning-of-line) | |
642 (let ((start (point))) | |
643 (if (not (hif-looking-at-endif)) | |
644 (hif-find-previous-relevant)) | |
645 (if (hif-looking-at-endif) | |
646 (hif-endif-to-ifdef)) | |
647 (if (= start (point)) | |
210 | 648 (error "No previous #ifdef")))) |
31 | 649 |
650 (defun next-ifdef (&optional arg) | |
651 "Move to the beginning of the next #ifX, #else, or #endif. | |
210 | 652 With argument, do this that many times." |
31 | 653 (interactive "p") |
654 (or arg (setq arg 1)) | |
655 (if (< arg 0) | |
656 (previous-ifdef (- arg))) | |
657 (while (< 0 arg) | |
658 (setq arg (1- arg)) | |
659 (hif-find-next-relevant) | |
660 (if (eolp) | |
661 (progn | |
662 (beginning-of-line) | |
210 | 663 (error "No following #ifdefs, #elses, or #endifs"))))) |
31 | 664 |
665 (defun previous-ifdef (&optional arg) | |
666 "Move to the beginning of the previous #ifX, #else, or #endif. | |
210 | 667 With argument, do this that many times." |
31 | 668 (interactive "p") |
669 (or arg (setq arg 1)) | |
670 (if (< arg 0) | |
671 (next-ifdef (- arg))) | |
672 (while (< 0 arg) | |
673 (setq arg (1- arg)) | |
674 (let ((start (point))) | |
675 (hif-find-previous-relevant) | |
676 (if (= start (point)) | |
677 (error "No previous #ifdefs, #elses, or #endifs") | |
678 )))) | |
679 | |
680 | |
681 ;===%%SF%% parsing (End) === | |
682 | |
683 | |
684 ;===%%SF%% hide-ifdef-hiding (Start) === | |
685 | |
686 | |
687 ;;; A range is a structure with four components: | |
688 ;;; ELSE-P True if there was an else clause for the ifdef. | |
689 ;;; START The start of the range. (beginning of line) | |
690 ;;; ELSE The else marker (beginning of line) | |
691 ;;; Only valid if ELSE-P is true. | |
692 ;;; END The end of the range. (beginning of line) | |
693 | |
694 (defun hif-make-range (else-p start end &optional else) | |
695 (list else-p start else end)) | |
696 | |
697 (defun hif-range-else-p (range) (elt range 0)) | |
698 (defun hif-range-start (range) (elt range 1)) | |
699 (defun hif-range-else (range) (elt range 2)) | |
700 (defun hif-range-end (range) (elt range 3)) | |
701 | |
702 | |
703 | |
704 ;;; Find-Range | |
705 ;;; The workhorse, it delimits the #if region. Reasonably simple: | |
706 ;;; Skip until an #else or #endif is found, remembering positions. If | |
707 ;;; an #else was found, skip some more, looking for the true #endif. | |
708 | |
709 (defun hif-find-range () | |
710 "Returns a Range structure describing the current #if region. | |
711 Point is left unchanged." | |
712 ; (message "hif-find-range at %d" (point)) | |
713 (save-excursion | |
714 (beginning-of-line) | |
715 (let ((start (point)) | |
716 (else-p nil) | |
717 (else nil) | |
718 (end nil)) | |
719 ;; Part one. Look for either #endif or #else. | |
720 ;; This loop-and-a-half dedicated to E. Dijkstra. | |
721 (hif-find-next-relevant) | |
722 (while (hif-looking-at-ifX) ; Skip nested ifdef | |
723 (hif-ifdef-to-endif) | |
724 (hif-find-next-relevant)) | |
725 ;; Found either a #else or an #endif. | |
726 (cond ((hif-looking-at-else) | |
727 (setq else-p t) | |
728 (setq else (point))) | |
729 (t | |
730 (setq end (point)) ; (save-excursion (end-of-line) (point)) | |
731 )) | |
732 ;; If found #else, look for #endif. | |
733 (if else-p | |
734 (progn | |
735 (hif-find-next-relevant) | |
736 (while (hif-looking-at-ifX) ; Skip nested ifdef | |
737 (hif-ifdef-to-endif) | |
738 (hif-find-next-relevant)) | |
739 (if (hif-looking-at-else) | |
740 (error "Found two elses in a row? Broken!")) | |
741 (setq end (point)) ; (save-excursion (end-of-line) (point)) | |
742 )) | |
743 (hif-make-range else-p start end else)))) | |
744 | |
745 | |
746 ;;; A bit slimy. | |
747 ;;; NOTE: If there's an #ifdef at the beginning of the file, we can't | |
748 ;;; hide it. There's no previous newline to replace. If we added | |
749 ;;; one, we'd throw off all the counts. Feh. | |
750 | |
751 (defun hif-hide-line (point) | |
210 | 752 "Hide the line containing point. Does nothing if `hide-ifdef-lines' is nil." |
31 | 753 (if hide-ifdef-lines |
754 (save-excursion | |
755 (goto-char point) | |
756 (let ((modp (buffer-modified-p))) | |
757 (unwind-protect | |
758 (progn | |
759 (beginning-of-line) | |
760 (if (not (= (point) 1)) | |
761 (hide-ifdef-region (1- (point)) (point)))) | |
762 (set-buffer-modified-p modp)) | |
763 )) | |
764 )) | |
765 | |
766 | |
767 ;;; Hif-Possibly-Hide | |
768 ;;; There are four cases. The #ifX expression is "taken" if it | |
769 ;;; the hide-ifdef-evaluator returns T. Presumably, this means the code | |
770 ;;; inside the #ifdef would be included when the program was | |
771 ;;; compiled. | |
772 ;;; | |
773 ;;; Case 1: #ifX taken, and there's an #else. | |
774 ;;; The #else part must be hidden. The #if (then) part must be | |
775 ;;; processed for nested #ifX's. | |
776 ;;; Case 2: #ifX taken, and there's no #else. | |
777 ;;; The #if part must be processed for nested #ifX's. | |
778 ;;; Case 3: #ifX not taken, and there's an #else. | |
779 ;;; The #if part must be hidden. The #else part must be processed | |
780 ;;; for nested #ifs. | |
781 ;;; Case 4: #ifX not taken, and there's no #else. | |
782 ;;; The #ifX part must be hidden. | |
783 ;;; | |
784 ;;; Further processing is done by narrowing to the relevant region | |
785 ;;; and just recursively calling hide-ifdef-guts. | |
786 ;;; | |
787 ;;; When hif-possibly-hide returns, point is at the end of the | |
788 ;;; possibly-hidden range. | |
789 | |
790 (defun hif-recurse-on (start end) | |
210 | 791 "Call `hide-ifdef-guts' after narrowing to end of START line and END line." |
31 | 792 (save-excursion |
793 (save-restriction | |
794 (goto-char start) | |
795 (end-of-line) | |
796 (narrow-to-region (point) end) | |
797 (hide-ifdef-guts)))) | |
798 | |
799 (defun hif-possibly-hide () | |
800 "Called at #ifX expression, this hides those parts that should be | |
210 | 801 hidden, according to judgement of `hide-ifdef-evaluator'." |
31 | 802 ; (message "hif-possibly-hide") (sit-for 1) |
803 (let ((test (hif-canonicalize)) | |
804 (range (hif-find-range))) | |
805 ; (message "test = %s" test) (sit-for 1) | |
806 | |
807 (hif-hide-line (hif-range-end range)) | |
808 (if (funcall hide-ifdef-evaluator test) | |
809 (cond ((hif-range-else-p range) ; case 1 | |
810 (hif-hide-line (hif-range-else range)) | |
811 (hide-ifdef-region (hif-range-else range) | |
812 (1- (hif-range-end range))) | |
813 (hif-recurse-on (hif-range-start range) | |
814 (hif-range-else range))) | |
815 (t ; case 2 | |
816 (hif-recurse-on (hif-range-start range) | |
817 (hif-range-end range)))) | |
818 (cond ((hif-range-else-p range) ; case 3 | |
819 (hif-hide-line (hif-range-else range)) | |
820 (hide-ifdef-region (hif-range-start range) | |
821 (1- (hif-range-else range))) | |
822 (hif-recurse-on (hif-range-else range) | |
823 (hif-range-end range))) | |
824 (t ; case 4 | |
825 (hide-ifdef-region (point) | |
826 (1- (hif-range-end range)))) | |
827 )) | |
828 (hif-hide-line (hif-range-start range)) ; Always hide start. | |
829 (goto-char (hif-range-end range)) | |
830 (end-of-line) | |
831 )) | |
832 | |
833 | |
834 | |
835 (defun hide-ifdef-guts () | |
210 | 836 "Does the work of `hide-ifdefs', except for the work that's pointless |
31 | 837 to redo on a recursive entry." |
838 ; (message "hide-ifdef-guts") | |
839 (save-excursion | |
840 (goto-char (point-min)) | |
841 (while (hif-find-any-ifX) | |
842 (hif-possibly-hide)))) | |
843 | |
844 ;===%%SF%% hide-ifdef-hiding (End) === | |
845 | |
846 | |
847 ;===%%SF%% exports (Start) === | |
848 | |
998 | 849 ;;;###autoload |
31 | 850 (defvar hide-ifdef-initially nil |
210 | 851 "*Non-nil if `hide-ifdefs' should be called when Hide-Ifdef mode |
852 is first activated.") | |
31 | 853 |
854 (defvar hide-ifdef-hiding nil | |
855 "Non-nil if text might be hidden.") | |
856 | |
998 | 857 ;;;###autoload |
31 | 858 (defvar hide-ifdef-read-only nil |
859 "*Set to non-nil if you want buffer to be read-only while hiding text.") | |
860 | |
861 (defvar hif-outside-read-only nil | |
210 | 862 "Internal variable. Saves the value of `buffer-read-only' while hiding.") |
31 | 863 |
998 | 864 ;;;###autoload |
31 | 865 (defvar hide-ifdef-lines nil |
866 "*Set to t if you don't want to see the #ifX, #else, and #endif lines.") | |
867 | |
868 (defun hide-ifdef-toggle-read-only () | |
869 "Toggle hide-ifdef-read-only." | |
870 (interactive) | |
871 (setq hide-ifdef-read-only (not hide-ifdef-read-only)) | |
872 (message "Hide-Read-Only %s" | |
873 (if hide-ifdef-read-only "ON" "OFF")) | |
874 (if hide-ifdef-hiding | |
875 (setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only))) | |
210 | 876 (hif-update-mode-line)) |
31 | 877 |
878 (defun hide-ifdef-toggle-outside-read-only () | |
1476 | 879 "Replacement for `toggle-read-only' within Hide Ifdef mode." |
31 | 880 (interactive) |
881 (setq hif-outside-read-only (not hif-outside-read-only)) | |
882 (message "Read only %s" | |
883 (if hif-outside-read-only "ON" "OFF")) | |
884 (setq buffer-read-only | |
885 (or (and hide-ifdef-hiding hide-ifdef-read-only) | |
886 hif-outside-read-only) | |
887 ) | |
210 | 888 (hif-update-mode-line)) |
31 | 889 |
890 | |
891 (defun hide-ifdef-define (var) | |
892 "Define a VAR so that #ifdef VAR would be included." | |
893 (interactive "SDefine what? ") | |
894 (hif-set-var var t) | |
895 (if hide-ifdef-hiding (hide-ifdefs))) | |
896 | |
897 (defun hide-ifdef-undef (var) | |
898 "Undefine a VAR so that #ifdef VAR would not be included." | |
899 (interactive "SUndefine what? ") | |
900 (hif-set-var var nil) | |
901 (if hide-ifdef-hiding (hide-ifdefs))) | |
902 | |
903 | |
904 (defun hide-ifdefs () | |
1476 | 905 "Hide the contents of some #ifdefs. |
906 Assume that defined symbols have been added to `hide-ifdef-env'. | |
907 The text hidden is the text that would not be included by the C | |
908 preprocessor if it were given the file with those symbols defined. | |
31 | 909 |
1476 | 910 Turn off hiding by calling `show-ifdef'." |
31 | 911 |
912 (interactive) | |
913 (message "Hiding...") | |
914 (if (not hide-ifdef-mode) | |
915 (hide-ifdef-mode 1)) ; turn on hide-ifdef-mode | |
916 (if hide-ifdef-hiding | |
917 (show-ifdefs)) ; Otherwise, deep confusion. | |
918 (if buffer-read-only (toggle-read-only)) ; make it writable temporarily | |
919 (setq selective-display t) | |
920 (setq hide-ifdef-hiding t) | |
921 (hide-ifdef-guts) | |
922 (if (or hide-ifdef-read-only hif-outside-read-only) | |
210 | 923 (toggle-read-only)) ; make it read only |
924 (message "Hiding done")) | |
31 | 925 |
926 | |
927 (defun show-ifdefs () | |
1476 | 928 "Cancel the effects of `hide-ifdef'. The contents of all #ifdefs is shown." |
31 | 929 (interactive) |
930 (if buffer-read-only (toggle-read-only)) ; make it writable temporarily | |
931 (setq selective-display nil) ; defaults | |
932 (hif-show-all) | |
933 (if hif-outside-read-only | |
934 (toggle-read-only)) ; make it read only | |
210 | 935 (setq hide-ifdef-hiding nil)) |
31 | 936 |
937 | |
938 (defun hif-find-ifdef-block () | |
1476 | 939 "Utilitiy for hide and show `ifdef-block'. |
940 Set top and bottom of ifdef block." | |
31 | 941 (let (max-bottom) |
942 (save-excursion | |
943 (beginning-of-line) | |
944 (if (not (or (hif-looking-at-else) (hif-looking-at-ifX))) | |
945 (up-ifdef)) | |
946 (setq top (point)) | |
947 (hif-ifdef-to-endif) | |
210 | 948 (setq max-bottom (1- (point)))) |
31 | 949 (save-excursion |
950 (beginning-of-line) | |
951 (if (not (hif-looking-at-endif)) | |
952 (hif-find-next-relevant)) | |
953 (while (hif-looking-at-ifX) | |
954 (hif-ifdef-to-endif) | |
210 | 955 (hif-find-next-relevant)) |
956 (setq bottom (min max-bottom (1- (point)))))) | |
31 | 957 ) |
958 | |
959 | |
960 (defun hide-ifdef-block () | |
961 "Hide the ifdef block (true or false part) enclosing or before the cursor." | |
962 (interactive) | |
963 (if (not hide-ifdef-mode) | |
964 (hide-ifdef-mode 1)) | |
965 (if buffer-read-only (toggle-read-only)) | |
966 (setq selective-display t) | |
967 (let (top bottom) | |
968 (hif-find-ifdef-block) ; set top and bottom - dynamic scoping | |
969 (hide-ifdef-region top bottom) | |
970 (if hide-ifdef-lines | |
971 (progn | |
972 (hif-hide-line top) | |
973 (hif-hide-line (1+ bottom)))) | |
210 | 974 (setq hide-ifdef-hiding t)) |
31 | 975 (if (or hide-ifdef-read-only hif-outside-read-only) |
210 | 976 (toggle-read-only))) |
31 | 977 |
978 | |
979 (defun show-ifdef-block () | |
980 "Show the ifdef block (true or false part) enclosing or before the cursor." | |
981 (interactive) | |
982 (let ((old-read-only buffer-read-only)) | |
983 (if old-read-only (toggle-read-only)) | |
984 (if hide-ifdef-lines | |
985 (save-excursion | |
986 (beginning-of-line) | |
987 (hif-show-ifdef-region (1- (point)) (progn (end-of-line) (point)))) | |
988 | |
989 (let (top bottom) | |
990 (hif-find-ifdef-block) | |
991 (hif-show-ifdef-region (1- top) bottom)) | |
992 ) | |
993 | |
994 ; restore read only status since we dont know if all is shown. | |
210 | 995 (if old-read-only (toggle-read-only)))) |
31 | 996 |
997 | |
998 ;;; defininition alist support | |
999 | |
1000 (defvar hide-ifdef-define-alist nil | |
1001 "A global assoc list of pre-defined symbol lists") | |
1002 | |
1003 (defun hif-compress-define-list (env) | |
1004 "Compress the define list ENV into a list of defined symbols only." | |
1005 (let ((defs (mapcar '(lambda (arg) | |
1006 (if (hif-lookup (car arg)) (car arg))) | |
1007 env)) | |
1008 (new-defs nil)) | |
1009 (while defs | |
1010 (if (car defs) | |
1011 (setq new-defs (cons (car defs) new-defs))) | |
1012 (setq defs (cdr defs))) | |
210 | 1013 new-defs)) |
31 | 1014 |
1015 (defun hide-ifdef-set-define-alist (name) | |
1476 | 1016 "Set the association for NAME to `hide-ifdef-env'." |
31 | 1017 (interactive "SSet define list: ") |
1018 (setq hide-ifdef-define-alist | |
1019 (cons (cons name (hif-compress-define-list hide-ifdef-env)) | |
210 | 1020 hide-ifdef-define-alist))) |
31 | 1021 |
1022 (defun hide-ifdef-use-define-alist (name) | |
1476 | 1023 "Set `hide-ifdef-env' to the define list specified by NAME." |
31 | 1024 (interactive "SUse define list: ") |
1025 (let ((define-list (assoc name hide-ifdef-define-alist))) | |
1026 (if define-list | |
1027 (setq hide-ifdef-env | |
1028 (mapcar '(lambda (arg) (cons arg t)) | |
1029 (cdr define-list))) | |
1030 (error "No define list for %s" name)) | |
210 | 1031 (if hide-ifdef-hiding (hide-ifdefs)))) |
31 | 1032 |
660
08eb386dd0f3
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
210
diff
changeset
|
1033 ;;; hideif.el ends here |
08eb386dd0f3
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
210
diff
changeset
|
1034 |