Mercurial > emacs
annotate lisp/progmodes/hideif.el @ 994:5b2a1922c4d5
*** empty log message ***
author | Eric S. Raymond <esr@snark.thyrsus.com> |
---|---|
date | Fri, 14 Aug 1992 22:36:42 +0000 |
parents | 52cd80cb5be1 |
children | 61c6983219ff |
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 | |
201 (defun hide-ifdef-mode (arg) | |
210 | 202 "Toggle Hide-Ifdef mode. This is a minor mode, albeit a large one. |
203 With ARG, turn Hide-Ifdef mode on iff arg is positive. | |
204 In Hide-Ifdef mode, code within #ifdef constructs that the C preprocessor | |
31 | 205 would eliminate may be hidden from view. Several variables affect |
206 how the hiding is done: | |
207 | |
208 hide-ifdef-env | |
209 An association list of defined and undefined symbols for the | |
210 | 210 current buffer. Initially, the global value of `hide-ifdef-env' |
211 is used. | |
31 | 212 |
213 hide-ifdef-define-alist | |
214 An association list of defined symbol lists. | |
210 | 215 Use `hide-ifdef-set-define-alist' to save the current `hide-ifdef-env' |
216 and `hide-ifdef-use-define-alist' to set the current `hide-ifdef-env' | |
217 from one of the lists in `hide-ifdef-define-alist'. | |
31 | 218 |
219 hide-ifdef-lines | |
220 Set to non-nil to not show #if, #ifdef, #ifndef, #else, and | |
221 #endif lines when hiding. | |
222 | |
223 hide-ifdef-initially | |
210 | 224 Indicates whether `hide-ifdefs' should be called when Hide-Ifdef mode |
31 | 225 is activated. |
226 | |
227 hide-ifdef-read-only | |
228 Set to non-nil if you want to make buffers read only while hiding. | |
210 | 229 After `show-ifdefs', read-only status is restored to previous value. |
31 | 230 |
231 \\{hide-ifdef-mode-map}" | |
232 | |
233 (interactive "P") | |
234 (make-local-variable 'hide-ifdef-mode) | |
235 (setq hide-ifdef-mode | |
236 (if (null arg) | |
237 (not hide-ifdef-mode) | |
238 (> (prefix-numeric-value arg) 0))) | |
239 | |
240 (hif-update-mode-line) | |
241 | |
242 (if hide-ifdef-mode | |
243 (progn | |
244 ; fix c-mode syntax table so we can recognize whole symbols. | |
245 (modify-syntax-entry ?_ "w") | |
246 (modify-syntax-entry ?& ".") | |
247 (modify-syntax-entry ?\| ".") | |
248 | |
249 ; inherit global values | |
250 (make-local-variable 'hide-ifdef-env) | |
251 (setq hide-ifdef-env (default-value 'hide-ifdef-env)) | |
252 | |
253 (make-local-variable 'hide-ifdef-hiding) | |
254 (setq hide-ifdef-hiding (default-value 'hide-ifdef-hiding)) | |
255 | |
256 (make-local-variable 'hif-outside-read-only) | |
257 (setq hif-outside-read-only buffer-read-only) | |
258 | |
259 (make-local-variable 'hide-ifdef-mode-map-before) | |
260 (setq hide-ifdef-mode-map-before (current-local-map)) | |
261 (use-local-map (copy-keymap (current-local-map))) | |
262 (local-unset-key hide-ifdef-mode-prefix-key) | |
263 (local-set-key hide-ifdef-mode-prefix-key 'hide-ifdef-mode-map) | |
264 (define-hide-ifdef-mode-map) | |
265 | |
266 (run-hooks 'hide-ifdef-mode-hook) | |
267 | |
268 (if hide-ifdef-initially | |
269 (hide-ifdefs) | |
270 (show-ifdefs)) | |
271 (message "Enter hide-ifdef-mode.") | |
272 ) | |
273 ; else end hide-ifdef-mode | |
274 (if hide-ifdef-hiding | |
275 (show-ifdefs)) | |
276 (use-local-map hide-ifdef-mode-map-before) | |
277 (message "Exit hide-ifdef-mode.") | |
278 )) | |
279 | |
280 | |
281 ;; from outline.el with docstring fixed. | |
282 (defun hif-outline-flag-region (from to flag) | |
283 "Hides or shows lines from FROM to TO, according to FLAG. If FLAG | |
284 is \\n (newline character) then text is shown, while if FLAG is \\^M | |
285 \(control-M) the text is hidden." | |
286 (let ((modp (buffer-modified-p))) | |
287 (unwind-protect (progn | |
288 (subst-char-in-region from to | |
289 (if (= flag ?\n) ?\^M ?\n) | |
290 flag t) ) | |
291 (set-buffer-modified-p modp)) | |
292 )) | |
293 | |
294 (defun hif-show-all () | |
295 "Show all of the text in the current buffer." | |
296 (interactive) | |
297 (hif-outline-flag-region (point-min) (point-max) ?\n)) | |
298 | |
299 (defun hide-ifdef-region (start end) | |
300 "START is the start of a #if or #else form. END is the ending part. | |
301 Everything including these lines is made invisible." | |
302 (hif-outline-flag-region start end ?\^M) | |
303 ) | |
304 | |
305 (defun hif-show-ifdef-region (start end) | |
306 "Everything between START and END is made visible." | |
307 (hif-outline-flag-region start end ?\n) | |
308 ) | |
309 | |
310 | |
311 | |
312 ;===%%SF%% evaluation (Start) === | |
313 | |
314 (defvar hide-ifdef-evaluator 'eval | |
315 "The evaluator is given a canonical form and returns T if text under | |
316 that form should be displayed.") | |
317 | |
318 (defvar hif-undefined-symbol nil | |
319 "...is by default considered to be false.") | |
320 | |
321 (defvar hide-ifdef-env nil | |
322 "An alist of defined symbols and their values.") | |
323 | |
324 | |
325 (defun hif-set-var (var value) | |
326 "Prepend (var value) pair to hide-ifdef-env." | |
327 (setq hide-ifdef-env (cons (cons var value) hide-ifdef-env))) | |
328 | |
329 | |
330 (defun hif-lookup (var) | |
331 ; (message "hif-lookup %s" var) | |
332 (let ((val (assoc var hide-ifdef-env))) | |
333 (if val | |
334 (cdr val) | |
335 hif-undefined-symbol))) | |
336 | |
337 (defun hif-defined (var) | |
338 (hif-lookup var) | |
339 ; when #if expressions are fully supported, defined result should be 1 | |
340 ; (if (assoc var hide-ifdef-env) | |
341 ; 1 | |
342 ; nil) | |
343 ) | |
344 | |
345 | |
346 ;===%%SF%% evaluation (End) === | |
347 | |
348 | |
349 | |
350 ;===%%SF%% parsing (Start) === | |
351 ;;; The code that understands what ifs and ifdef in files look like. | |
352 | |
353 (defconst hif-cpp-prefix "\\(^\\|\r\\)[ \t]*#[ \t]*") | |
354 (defconst hif-ifndef-regexp (concat hif-cpp-prefix "ifndef")) | |
355 (defconst hif-ifx-regexp (concat hif-cpp-prefix "if\\(n?def\\)?[ \t]+")) | |
356 (defconst hif-else-regexp (concat hif-cpp-prefix "else")) | |
357 (defconst hif-endif-regexp (concat hif-cpp-prefix "endif")) | |
358 (defconst hif-ifx-else-endif-regexp | |
359 (concat hif-ifx-regexp "\\|" hif-else-regexp "\\|" hif-endif-regexp)) | |
360 | |
361 | |
362 (defun hif-infix-to-prefix (token-list) | |
363 "Convert list of tokens in infix into prefix list" | |
364 ; (message "hif-infix-to-prefix: %s" token-list) | |
365 (if (= 1 (length token-list)) | |
366 (` (hif-lookup (quote (, (car token-list))))) | |
367 (hif-parse-if-exp token-list)) | |
368 ) | |
369 | |
370 ; pattern to match initial identifier, !, &&, ||, (, or ). | |
371 (defconst hif-token-regexp "^\\(!\\|&&\\|||\\|[()]\\|\\w+\\)") | |
372 (defconst hif-end-of-comment "\\*/") | |
373 | |
374 | |
375 (defun hif-tokenize (expr-string) | |
376 "Separate string into a list of tokens" | |
377 (let ((token-list nil) | |
378 (expr-start 0) | |
379 (expr-length (length expr-string))) | |
380 | |
381 (while (< expr-start expr-length) | |
382 ; (message "expr-start = %d" expr-start) (sit-for 1) | |
383 (cond | |
384 ((string-match "^[ \t]+" expr-string expr-start) | |
385 ; skip whitespace | |
386 (setq expr-start (match-end 0)) | |
387 ; stick newline in string so ^ matches on the next string-match | |
388 (aset expr-string (1- expr-start) ?\n) | |
389 ) | |
390 | |
391 ((string-match "^/\\*" expr-string expr-start) | |
392 (setq expr-start (match-end 0)) | |
393 (aset expr-string (1- expr-start) ?\n) | |
394 (or | |
395 (string-match hif-end-of-comment | |
396 expr-string expr-start) ; eat comment | |
397 (string-match "$" expr-string expr-start)) ; multi-line comment | |
398 (setq expr-start (match-end 0)) | |
399 (aset expr-string (1- expr-start) ?\n) | |
400 ) | |
401 | |
402 ((string-match hif-token-regexp expr-string expr-start) | |
403 (let ((token (substring expr-string expr-start (match-end 0)))) | |
404 (setq expr-start (match-end 0)) | |
405 (aset expr-string (1- expr-start) ?\n) | |
406 ; (message "token: %s" token) (sit-for 1) | |
407 (setq token-list | |
408 (cons | |
409 (cond | |
410 ((string-equal token "||") 'or) | |
411 ((string-equal token "&&") 'and) | |
412 ((string-equal token "!") 'not) | |
413 ((string-equal token "defined") 'hif-defined) | |
414 ((string-equal token "(") 'lparen) | |
415 ((string-equal token ")") 'rparen) | |
416 (t (intern token))) | |
417 token-list)) | |
418 )) | |
419 (t (error "Bad #if expression: %s" expr-string)) | |
420 )) | |
421 (nreverse token-list) | |
422 )) | |
423 | |
424 ;;;----------------------------------------------------------------- | |
425 ;;; Translate C preprocessor #if expressions using recursive descent. | |
426 ;;; This parser is limited to the operators &&, ||, !, and "defined". | |
427 | |
428 (defun hif-parse-if-exp (token-list) | |
429 "Parse the TOKEN-LIST. Return translated list in prefix form." | |
430 (hif-nexttoken) | |
431 (prog1 | |
432 (hif-expr) | |
433 (if token ; is there still a token? | |
210 | 434 (error "Error: unexpected token: %s" token)))) |
31 | 435 |
436 (defun hif-nexttoken () | |
437 "Pop the next token from token-list into the let variable \"token\"." | |
438 (setq token (car token-list)) | |
439 (setq token-list (cdr token-list)) | |
210 | 440 token) |
31 | 441 |
442 (defun hif-expr () | |
443 "Parse and expression of the form | |
444 expr : term | expr '||' term." | |
445 (let ((result (hif-term))) | |
446 (while (eq token 'or) | |
447 (hif-nexttoken) | |
448 (setq result (list 'or result (hif-term)))) | |
210 | 449 result)) |
31 | 450 |
451 (defun hif-term () | |
452 "Parse a term of the form | |
453 term : factor | term '&&' factor." | |
454 (let ((result (hif-factor))) | |
455 (while (eq token 'and) | |
456 (hif-nexttoken) | |
457 (setq result (list 'and result (hif-factor)))) | |
210 | 458 result)) |
31 | 459 |
460 (defun hif-factor () | |
461 "Parse a factor of the form | |
462 factor : '!' factor | '(' expr ')' | 'defined(' id ')' | id." | |
463 (cond | |
464 ((eq token 'not) | |
465 (hif-nexttoken) | |
466 (list 'not (hif-factor))) | |
467 | |
468 ((eq token 'lparen) | |
469 (hif-nexttoken) | |
470 (let ((result (hif-expr))) | |
471 (if (not (eq token 'rparen)) | |
472 (error "Bad token in parenthesized expression: %s" token) | |
473 (hif-nexttoken) | |
474 result))) | |
475 | |
476 ((eq token 'hif-defined) | |
477 (hif-nexttoken) | |
478 (if (not (eq token 'lparen)) | |
479 (error "Error: expected \"(\" after \"defined\"")) | |
480 (hif-nexttoken) | |
481 (let ((ident token)) | |
482 (if (memq token '(or and not hif-defined lparen rparen)) | |
483 (error "Error: unexpected token: %s" token)) | |
484 (hif-nexttoken) | |
485 (if (not (eq token 'rparen)) | |
486 (error "Error: expected \")\" after identifier")) | |
487 (hif-nexttoken) | |
488 (` (hif-defined (quote (, ident)))) | |
489 )) | |
490 | |
491 (t ; identifier | |
492 (let ((ident token)) | |
493 (if (memq ident '(or and)) | |
494 (error "Error: missing identifier")) | |
495 (hif-nexttoken) | |
496 (` (hif-lookup (quote (, ident)))) | |
497 )) | |
498 )) | |
499 | |
500 ;;;----------- end of parser ----------------------- | |
501 | |
502 | |
503 (defun hif-canonicalize () | |
504 "When at beginning of #ifX, returns a canonical (evaluatable) | |
505 form for the expression." | |
506 (save-excursion | |
507 (let ((negate (looking-at hif-ifndef-regexp))) | |
508 (re-search-forward hif-ifx-regexp) | |
509 (let* ((expr-string | |
510 (buffer-substring (point) | |
511 (progn (skip-chars-forward "^\n\r") (point)))) | |
512 (expr (hif-infix-to-prefix (hif-tokenize expr-string)))) | |
513 ; (message "hif-canonicalized: %s" expr) | |
514 (if negate | |
515 (list 'not expr) | |
516 expr))))) | |
517 | |
518 | |
519 (defun hif-find-any-ifX () | |
520 "Position at beginning of next #if, #ifdef, or #ifndef, including one on | |
521 this line." | |
522 ; (message "find ifX at %d" (point)) | |
523 (prog1 | |
524 (re-search-forward hif-ifx-regexp (point-max) t) | |
525 (beginning-of-line))) | |
526 | |
527 | |
528 (defun hif-find-next-relevant () | |
529 "Position at beginning of next #ifdef, #ifndef, #else, #endif, | |
530 NOT including one on this line." | |
531 ; (message "hif-find-next-relevant at %d" (point)) | |
532 (end-of-line) | |
533 ; avoid infinite recursion by only going to beginning of line if match found | |
534 (if (re-search-forward hif-ifx-else-endif-regexp (point-max) t) | |
210 | 535 (beginning-of-line))) |
31 | 536 |
537 (defun hif-find-previous-relevant () | |
538 "Position at beginning of previous #ifdef, #ifndef, #else, #endif, | |
539 NOT including one on this line." | |
540 ; (message "hif-find-previous-relevant at %d" (point)) | |
541 (beginning-of-line) | |
542 ; avoid infinite recursion by only going to beginning of line if match found | |
543 (if (re-search-backward hif-ifx-else-endif-regexp (point-min) t) | |
210 | 544 (beginning-of-line))) |
31 | 545 |
546 | |
547 (defun hif-looking-at-ifX () ;; Should eventually see #if | |
548 (looking-at hif-ifx-regexp)) | |
549 (defun hif-looking-at-endif () | |
550 (looking-at hif-endif-regexp)) | |
551 (defun hif-looking-at-else () | |
552 (looking-at hif-else-regexp)) | |
553 | |
554 | |
555 | |
556 (defun hif-ifdef-to-endif () | |
557 "If positioned at #ifX or #else form, skip to corresponding #endif." | |
558 ; (message "hif-ifdef-to-endif at %d" (point)) (sit-for 1) | |
559 (hif-find-next-relevant) | |
560 (cond ((hif-looking-at-ifX) | |
561 (hif-ifdef-to-endif) ; find endif of nested if | |
562 (hif-ifdef-to-endif)) ; find outer endif or else | |
563 ((hif-looking-at-else) | |
564 (hif-ifdef-to-endif)) ; find endif following else | |
565 ((hif-looking-at-endif) | |
566 'done) | |
567 (t | |
210 | 568 (error "Missmatched #ifdef #endif pair")))) |
31 | 569 |
570 | |
571 (defun hif-endif-to-ifdef () | |
572 "If positioned at #endif form, skip backward to corresponding #ifX." | |
573 ; (message "hif-endif-to-ifdef at %d" (point)) | |
574 (let ((start (point))) | |
575 (hif-find-previous-relevant) | |
576 (if (= start (point)) | |
577 (error "Missmatched #ifdef #endif pair"))) | |
578 (cond ((hif-looking-at-endif) | |
579 (hif-endif-to-ifdef) ; find beginning of nested if | |
580 (hif-endif-to-ifdef)) ; find beginning of outer if or else | |
581 ((hif-looking-at-else) | |
582 (hif-endif-to-ifdef)) | |
583 ((hif-looking-at-ifX) | |
584 'done) | |
922 | 585 (t))) ; never gets here |
31 | 586 |
587 | |
588 (defun forward-ifdef (&optional arg) | |
589 "Move point to beginning of line of the next ifdef-endif. | |
210 | 590 With argument, do this that many times." |
31 | 591 (interactive "p") |
592 (or arg (setq arg 1)) | |
593 (if (< arg 0) | |
594 (backward-ifdef (- arg))) | |
595 (while (< 0 arg) | |
596 (setq arg (- arg)) | |
597 (let ((start (point))) | |
598 (if (not (hif-looking-at-ifX)) | |
599 (hif-find-next-relevant)) | |
600 (if (hif-looking-at-ifX) | |
601 (hif-ifdef-to-endif) | |
602 (goto-char start) | |
603 (error "No following #ifdef") | |
604 )))) | |
605 | |
606 | |
607 (defun backward-ifdef (&optional arg) | |
608 "Move point to beginning of the previous ifdef-endif. | |
210 | 609 With argument, do this that many times." |
31 | 610 (interactive "p") |
611 (or arg (setq arg 1)) | |
612 (if (< arg 0) | |
613 (forward-ifdef (- arg))) | |
614 (while (< 0 arg) | |
615 (setq arg (1- arg)) | |
616 (beginning-of-line) | |
617 (let ((start (point))) | |
618 (if (not (hif-looking-at-endif)) | |
619 (hif-find-previous-relevant)) | |
620 (if (hif-looking-at-endif) | |
621 (hif-endif-to-ifdef) | |
622 (goto-char start) | |
210 | 623 (error "No previous #ifdef"))))) |
31 | 624 |
625 | |
626 (defun down-ifdef () | |
627 "Move point to beginning of nested ifdef or else-part." | |
628 (interactive) | |
629 (let ((start (point))) | |
630 (hif-find-next-relevant) | |
631 (if (or (hif-looking-at-ifX) (hif-looking-at-else)) | |
632 () | |
633 (goto-char start) | |
210 | 634 (error "No following #ifdef")))) |
31 | 635 |
636 | |
637 (defun up-ifdef () | |
638 "Move point to beginning of enclosing ifdef or else-part." | |
639 (interactive) | |
640 (beginning-of-line) | |
641 (let ((start (point))) | |
642 (if (not (hif-looking-at-endif)) | |
643 (hif-find-previous-relevant)) | |
644 (if (hif-looking-at-endif) | |
645 (hif-endif-to-ifdef)) | |
646 (if (= start (point)) | |
210 | 647 (error "No previous #ifdef")))) |
31 | 648 |
649 (defun next-ifdef (&optional arg) | |
650 "Move to the beginning of the next #ifX, #else, or #endif. | |
210 | 651 With argument, do this that many times." |
31 | 652 (interactive "p") |
653 (or arg (setq arg 1)) | |
654 (if (< arg 0) | |
655 (previous-ifdef (- arg))) | |
656 (while (< 0 arg) | |
657 (setq arg (1- arg)) | |
658 (hif-find-next-relevant) | |
659 (if (eolp) | |
660 (progn | |
661 (beginning-of-line) | |
210 | 662 (error "No following #ifdefs, #elses, or #endifs"))))) |
31 | 663 |
664 (defun previous-ifdef (&optional arg) | |
665 "Move to the beginning of the previous #ifX, #else, or #endif. | |
210 | 666 With argument, do this that many times." |
31 | 667 (interactive "p") |
668 (or arg (setq arg 1)) | |
669 (if (< arg 0) | |
670 (next-ifdef (- arg))) | |
671 (while (< 0 arg) | |
672 (setq arg (1- arg)) | |
673 (let ((start (point))) | |
674 (hif-find-previous-relevant) | |
675 (if (= start (point)) | |
676 (error "No previous #ifdefs, #elses, or #endifs") | |
677 )))) | |
678 | |
679 | |
680 ;===%%SF%% parsing (End) === | |
681 | |
682 | |
683 ;===%%SF%% hide-ifdef-hiding (Start) === | |
684 | |
685 | |
686 ;;; A range is a structure with four components: | |
687 ;;; ELSE-P True if there was an else clause for the ifdef. | |
688 ;;; START The start of the range. (beginning of line) | |
689 ;;; ELSE The else marker (beginning of line) | |
690 ;;; Only valid if ELSE-P is true. | |
691 ;;; END The end of the range. (beginning of line) | |
692 | |
693 (defun hif-make-range (else-p start end &optional else) | |
694 (list else-p start else end)) | |
695 | |
696 (defun hif-range-else-p (range) (elt range 0)) | |
697 (defun hif-range-start (range) (elt range 1)) | |
698 (defun hif-range-else (range) (elt range 2)) | |
699 (defun hif-range-end (range) (elt range 3)) | |
700 | |
701 | |
702 | |
703 ;;; Find-Range | |
704 ;;; The workhorse, it delimits the #if region. Reasonably simple: | |
705 ;;; Skip until an #else or #endif is found, remembering positions. If | |
706 ;;; an #else was found, skip some more, looking for the true #endif. | |
707 | |
708 (defun hif-find-range () | |
709 "Returns a Range structure describing the current #if region. | |
710 Point is left unchanged." | |
711 ; (message "hif-find-range at %d" (point)) | |
712 (save-excursion | |
713 (beginning-of-line) | |
714 (let ((start (point)) | |
715 (else-p nil) | |
716 (else nil) | |
717 (end nil)) | |
718 ;; Part one. Look for either #endif or #else. | |
719 ;; This loop-and-a-half dedicated to E. Dijkstra. | |
720 (hif-find-next-relevant) | |
721 (while (hif-looking-at-ifX) ; Skip nested ifdef | |
722 (hif-ifdef-to-endif) | |
723 (hif-find-next-relevant)) | |
724 ;; Found either a #else or an #endif. | |
725 (cond ((hif-looking-at-else) | |
726 (setq else-p t) | |
727 (setq else (point))) | |
728 (t | |
729 (setq end (point)) ; (save-excursion (end-of-line) (point)) | |
730 )) | |
731 ;; If found #else, look for #endif. | |
732 (if else-p | |
733 (progn | |
734 (hif-find-next-relevant) | |
735 (while (hif-looking-at-ifX) ; Skip nested ifdef | |
736 (hif-ifdef-to-endif) | |
737 (hif-find-next-relevant)) | |
738 (if (hif-looking-at-else) | |
739 (error "Found two elses in a row? Broken!")) | |
740 (setq end (point)) ; (save-excursion (end-of-line) (point)) | |
741 )) | |
742 (hif-make-range else-p start end else)))) | |
743 | |
744 | |
745 ;;; A bit slimy. | |
746 ;;; NOTE: If there's an #ifdef at the beginning of the file, we can't | |
747 ;;; hide it. There's no previous newline to replace. If we added | |
748 ;;; one, we'd throw off all the counts. Feh. | |
749 | |
750 (defun hif-hide-line (point) | |
210 | 751 "Hide the line containing point. Does nothing if `hide-ifdef-lines' is nil." |
31 | 752 (if hide-ifdef-lines |
753 (save-excursion | |
754 (goto-char point) | |
755 (let ((modp (buffer-modified-p))) | |
756 (unwind-protect | |
757 (progn | |
758 (beginning-of-line) | |
759 (if (not (= (point) 1)) | |
760 (hide-ifdef-region (1- (point)) (point)))) | |
761 (set-buffer-modified-p modp)) | |
762 )) | |
763 )) | |
764 | |
765 | |
766 ;;; Hif-Possibly-Hide | |
767 ;;; There are four cases. The #ifX expression is "taken" if it | |
768 ;;; the hide-ifdef-evaluator returns T. Presumably, this means the code | |
769 ;;; inside the #ifdef would be included when the program was | |
770 ;;; compiled. | |
771 ;;; | |
772 ;;; Case 1: #ifX taken, and there's an #else. | |
773 ;;; The #else part must be hidden. The #if (then) part must be | |
774 ;;; processed for nested #ifX's. | |
775 ;;; Case 2: #ifX taken, and there's no #else. | |
776 ;;; The #if part must be processed for nested #ifX's. | |
777 ;;; Case 3: #ifX not taken, and there's an #else. | |
778 ;;; The #if part must be hidden. The #else part must be processed | |
779 ;;; for nested #ifs. | |
780 ;;; Case 4: #ifX not taken, and there's no #else. | |
781 ;;; The #ifX part must be hidden. | |
782 ;;; | |
783 ;;; Further processing is done by narrowing to the relevant region | |
784 ;;; and just recursively calling hide-ifdef-guts. | |
785 ;;; | |
786 ;;; When hif-possibly-hide returns, point is at the end of the | |
787 ;;; possibly-hidden range. | |
788 | |
789 (defun hif-recurse-on (start end) | |
210 | 790 "Call `hide-ifdef-guts' after narrowing to end of START line and END line." |
31 | 791 (save-excursion |
792 (save-restriction | |
793 (goto-char start) | |
794 (end-of-line) | |
795 (narrow-to-region (point) end) | |
796 (hide-ifdef-guts)))) | |
797 | |
798 (defun hif-possibly-hide () | |
799 "Called at #ifX expression, this hides those parts that should be | |
210 | 800 hidden, according to judgement of `hide-ifdef-evaluator'." |
31 | 801 ; (message "hif-possibly-hide") (sit-for 1) |
802 (let ((test (hif-canonicalize)) | |
803 (range (hif-find-range))) | |
804 ; (message "test = %s" test) (sit-for 1) | |
805 | |
806 (hif-hide-line (hif-range-end range)) | |
807 (if (funcall hide-ifdef-evaluator test) | |
808 (cond ((hif-range-else-p range) ; case 1 | |
809 (hif-hide-line (hif-range-else range)) | |
810 (hide-ifdef-region (hif-range-else range) | |
811 (1- (hif-range-end range))) | |
812 (hif-recurse-on (hif-range-start range) | |
813 (hif-range-else range))) | |
814 (t ; case 2 | |
815 (hif-recurse-on (hif-range-start range) | |
816 (hif-range-end range)))) | |
817 (cond ((hif-range-else-p range) ; case 3 | |
818 (hif-hide-line (hif-range-else range)) | |
819 (hide-ifdef-region (hif-range-start range) | |
820 (1- (hif-range-else range))) | |
821 (hif-recurse-on (hif-range-else range) | |
822 (hif-range-end range))) | |
823 (t ; case 4 | |
824 (hide-ifdef-region (point) | |
825 (1- (hif-range-end range)))) | |
826 )) | |
827 (hif-hide-line (hif-range-start range)) ; Always hide start. | |
828 (goto-char (hif-range-end range)) | |
829 (end-of-line) | |
830 )) | |
831 | |
832 | |
833 | |
834 (defun hide-ifdef-guts () | |
210 | 835 "Does the work of `hide-ifdefs', except for the work that's pointless |
31 | 836 to redo on a recursive entry." |
837 ; (message "hide-ifdef-guts") | |
838 (save-excursion | |
839 (goto-char (point-min)) | |
840 (while (hif-find-any-ifX) | |
841 (hif-possibly-hide)))) | |
842 | |
843 ;===%%SF%% hide-ifdef-hiding (End) === | |
844 | |
845 | |
846 ;===%%SF%% exports (Start) === | |
847 | |
848 (defvar hide-ifdef-initially nil | |
210 | 849 "*Non-nil if `hide-ifdefs' should be called when Hide-Ifdef mode |
850 is first activated.") | |
31 | 851 |
852 (defvar hide-ifdef-hiding nil | |
853 "Non-nil if text might be hidden.") | |
854 | |
855 (defvar hide-ifdef-read-only nil | |
856 "*Set to non-nil if you want buffer to be read-only while hiding text.") | |
857 | |
858 (defvar hif-outside-read-only nil | |
210 | 859 "Internal variable. Saves the value of `buffer-read-only' while hiding.") |
31 | 860 |
861 (defvar hide-ifdef-lines nil | |
862 "*Set to t if you don't want to see the #ifX, #else, and #endif lines.") | |
863 | |
864 (defun hide-ifdef-toggle-read-only () | |
865 "Toggle hide-ifdef-read-only." | |
866 (interactive) | |
867 (setq hide-ifdef-read-only (not hide-ifdef-read-only)) | |
868 (message "Hide-Read-Only %s" | |
869 (if hide-ifdef-read-only "ON" "OFF")) | |
870 (if hide-ifdef-hiding | |
871 (setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only))) | |
210 | 872 (hif-update-mode-line)) |
31 | 873 |
874 (defun hide-ifdef-toggle-outside-read-only () | |
210 | 875 "Replacement for `toggle-read-only' within Hide-Ifdef mode." |
31 | 876 (interactive) |
877 (setq hif-outside-read-only (not hif-outside-read-only)) | |
878 (message "Read only %s" | |
879 (if hif-outside-read-only "ON" "OFF")) | |
880 (setq buffer-read-only | |
881 (or (and hide-ifdef-hiding hide-ifdef-read-only) | |
882 hif-outside-read-only) | |
883 ) | |
210 | 884 (hif-update-mode-line)) |
31 | 885 |
886 | |
887 (defun hide-ifdef-define (var) | |
888 "Define a VAR so that #ifdef VAR would be included." | |
889 (interactive "SDefine what? ") | |
890 (hif-set-var var t) | |
891 (if hide-ifdef-hiding (hide-ifdefs))) | |
892 | |
893 (defun hide-ifdef-undef (var) | |
894 "Undefine a VAR so that #ifdef VAR would not be included." | |
895 (interactive "SUndefine what? ") | |
896 (hif-set-var var nil) | |
897 (if hide-ifdef-hiding (hide-ifdefs))) | |
898 | |
899 | |
900 (defun hide-ifdefs () | |
901 "Hide the contents of some #ifdefs. Assume that defined symbols have | |
210 | 902 been added to `hide-ifdef-env'. The text hidden is the text that would not |
31 | 903 be included by the C preprocessor if it were given the file with those |
904 symbols defined. | |
905 | |
906 Turn off hiding by calling show-ifdef." | |
907 | |
908 (interactive) | |
909 (message "Hiding...") | |
910 (if (not hide-ifdef-mode) | |
911 (hide-ifdef-mode 1)) ; turn on hide-ifdef-mode | |
912 (if hide-ifdef-hiding | |
913 (show-ifdefs)) ; Otherwise, deep confusion. | |
914 (if buffer-read-only (toggle-read-only)) ; make it writable temporarily | |
915 (setq selective-display t) | |
916 (setq hide-ifdef-hiding t) | |
917 (hide-ifdef-guts) | |
918 (if (or hide-ifdef-read-only hif-outside-read-only) | |
210 | 919 (toggle-read-only)) ; make it read only |
920 (message "Hiding done")) | |
31 | 921 |
922 | |
923 (defun show-ifdefs () | |
924 "Cancel the effects of hide-ifdef. The contents of all #ifdefs is shown." | |
925 (interactive) | |
926 (if buffer-read-only (toggle-read-only)) ; make it writable temporarily | |
927 (setq selective-display nil) ; defaults | |
928 (hif-show-all) | |
929 (if hif-outside-read-only | |
930 (toggle-read-only)) ; make it read only | |
210 | 931 (setq hide-ifdef-hiding nil)) |
31 | 932 |
933 | |
934 (defun hif-find-ifdef-block () | |
935 "Utilitiy for hide and show ifdef-block. Set top and bottom of ifdef block." | |
936 (let (max-bottom) | |
937 (save-excursion | |
938 (beginning-of-line) | |
939 (if (not (or (hif-looking-at-else) (hif-looking-at-ifX))) | |
940 (up-ifdef)) | |
941 (setq top (point)) | |
942 (hif-ifdef-to-endif) | |
210 | 943 (setq max-bottom (1- (point)))) |
31 | 944 (save-excursion |
945 (beginning-of-line) | |
946 (if (not (hif-looking-at-endif)) | |
947 (hif-find-next-relevant)) | |
948 (while (hif-looking-at-ifX) | |
949 (hif-ifdef-to-endif) | |
210 | 950 (hif-find-next-relevant)) |
951 (setq bottom (min max-bottom (1- (point)))))) | |
31 | 952 ) |
953 | |
954 | |
955 (defun hide-ifdef-block () | |
956 "Hide the ifdef block (true or false part) enclosing or before the cursor." | |
957 (interactive) | |
958 (if (not hide-ifdef-mode) | |
959 (hide-ifdef-mode 1)) | |
960 (if buffer-read-only (toggle-read-only)) | |
961 (setq selective-display t) | |
962 (let (top bottom) | |
963 (hif-find-ifdef-block) ; set top and bottom - dynamic scoping | |
964 (hide-ifdef-region top bottom) | |
965 (if hide-ifdef-lines | |
966 (progn | |
967 (hif-hide-line top) | |
968 (hif-hide-line (1+ bottom)))) | |
210 | 969 (setq hide-ifdef-hiding t)) |
31 | 970 (if (or hide-ifdef-read-only hif-outside-read-only) |
210 | 971 (toggle-read-only))) |
31 | 972 |
973 | |
974 (defun show-ifdef-block () | |
975 "Show the ifdef block (true or false part) enclosing or before the cursor." | |
976 (interactive) | |
977 (let ((old-read-only buffer-read-only)) | |
978 (if old-read-only (toggle-read-only)) | |
979 (if hide-ifdef-lines | |
980 (save-excursion | |
981 (beginning-of-line) | |
982 (hif-show-ifdef-region (1- (point)) (progn (end-of-line) (point)))) | |
983 | |
984 (let (top bottom) | |
985 (hif-find-ifdef-block) | |
986 (hif-show-ifdef-region (1- top) bottom)) | |
987 ) | |
988 | |
989 ; restore read only status since we dont know if all is shown. | |
210 | 990 (if old-read-only (toggle-read-only)))) |
31 | 991 |
992 | |
993 ;;; defininition alist support | |
994 | |
995 (defvar hide-ifdef-define-alist nil | |
996 "A global assoc list of pre-defined symbol lists") | |
997 | |
998 (defun hif-compress-define-list (env) | |
999 "Compress the define list ENV into a list of defined symbols only." | |
1000 (let ((defs (mapcar '(lambda (arg) | |
1001 (if (hif-lookup (car arg)) (car arg))) | |
1002 env)) | |
1003 (new-defs nil)) | |
1004 (while defs | |
1005 (if (car defs) | |
1006 (setq new-defs (cons (car defs) new-defs))) | |
1007 (setq defs (cdr defs))) | |
210 | 1008 new-defs)) |
31 | 1009 |
1010 (defun hide-ifdef-set-define-alist (name) | |
1011 "Set the association for NAME to hide-ifdef-env." | |
1012 (interactive "SSet define list: ") | |
1013 (setq hide-ifdef-define-alist | |
1014 (cons (cons name (hif-compress-define-list hide-ifdef-env)) | |
210 | 1015 hide-ifdef-define-alist))) |
31 | 1016 |
1017 (defun hide-ifdef-use-define-alist (name) | |
1018 "Set hide-ifdef-env to the define list specified by NAME." | |
1019 (interactive "SUse define list: ") | |
1020 (let ((define-list (assoc name hide-ifdef-define-alist))) | |
1021 (if define-list | |
1022 (setq hide-ifdef-env | |
1023 (mapcar '(lambda (arg) (cons arg t)) | |
1024 (cdr define-list))) | |
1025 (error "No define list for %s" name)) | |
210 | 1026 (if hide-ifdef-hiding (hide-ifdefs)))) |
31 | 1027 |
660
08eb386dd0f3
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
210
diff
changeset
|
1028 ;;; hideif.el ends here |
08eb386dd0f3
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
210
diff
changeset
|
1029 |