Commit | Line | Data |
---|---|---|
55535639 | 1 | ;;; hideif.el --- hides selected code within ifdef |
fc68affa | 2 | |
4e643dd2 | 3 | ;; Copyright (C) 1988, 1994, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 |
034babe1 | 4 | ;; Free Software Foundation, Inc. |
3a801d0c | 5 | |
f367dfc1 | 6 | ;; Author: Daniel LaLiberte <liberte@holonexus.org> |
36f063e8 | 7 | ;; Maintainer: FSF |
612abcae | 8 | ;; Keywords: c, outlines |
fc68affa | 9 | |
c9ed5a47 RS |
10 | ;; This file is part of GNU Emacs. |
11 | ||
12 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
13 | ;; it under the terms of the GNU General Public License as published by | |
1a484753 | 14 | ;; the Free Software Foundation; either version 3, or (at your option) |
c9ed5a47 RS |
15 | ;; any later version. |
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 | |
b578f267 | 23 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
3a35cf56 LK |
24 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
25 | ;; Boston, MA 02110-1301, USA. | |
c9ed5a47 | 26 | |
fc68affa ER |
27 | ;;; Commentary: |
28 | ||
b578f267 EN |
29 | ;; To initialize, toggle the hide-ifdef minor mode with |
30 | ;; | |
31 | ;; M-x hide-ifdef-mode | |
32 | ;; | |
33 | ;; This will set up key bindings and call hide-ifdef-mode-hook if it | |
34 | ;; has a value. To explicitly hide ifdefs using a buffer-local | |
35 | ;; define list (default empty), type | |
36 | ;; | |
37 | ;; M-x hide-ifdefs or C-c @ h | |
38 | ;; | |
39 | ;; Hide-ifdef suppresses the display of code that the preprocessor wouldn't | |
a1506d29 | 40 | ;; pass through. The support of constant expressions in #if lines is |
b578f267 EN |
41 | ;; limited to identifiers, parens, and the operators: &&, ||, !, and |
42 | ;; "defined". Please extend this. | |
43 | ;; | |
44 | ;; The hidden code is marked by ellipses (...). Be | |
45 | ;; cautious when editing near ellipses, since the hidden text is | |
46 | ;; still in the buffer, and you can move the point into it and modify | |
067d92a1 | 47 | ;; text unawares. |
b578f267 | 48 | ;; You can make your buffer read-only while hide-ifdef-hiding by setting |
a1506d29 | 49 | ;; hide-ifdef-read-only to a non-nil value. You can toggle this |
b578f267 EN |
50 | ;; variable with hide-ifdef-toggle-read-only (C-c @ C-q). |
51 | ;; | |
52 | ;; You can undo the effect of hide-ifdefs by typing | |
53 | ;; | |
54 | ;; M-x show-ifdefs or C-c @ s | |
55 | ;; | |
56 | ;; Use M-x hide-ifdef-define (C-c @ d) to define a symbol. | |
57 | ;; Use M-x hide-ifdef-undef (C-c @ u) to undefine a symbol. | |
58 | ;; | |
59 | ;; If you define or undefine a symbol while hide-ifdef-mode is in effect, | |
60 | ;; the display will be updated. Only the define list for the current | |
61 | ;; buffer will be affected. You can save changes to the local define | |
a1506d29 | 62 | ;; list with hide-ifdef-set-define-alist. This adds entries |
b578f267 EN |
63 | ;; to hide-ifdef-define-alist. |
64 | ;; | |
65 | ;; If you have defined a hide-ifdef-mode-hook, you can set | |
66 | ;; up a list of symbols that may be used by hide-ifdefs as in the | |
67 | ;; following example: | |
68 | ;; | |
067d92a1 | 69 | ;; (add-hook 'hide-ifdef-mode-hook |
16fdbdce | 70 | ;; (lambda () |
067d92a1 SM |
71 | ;; (unless hide-ifdef-define-alist |
72 | ;; (setq hide-ifdef-define-alist | |
73 | ;; '((list1 ONE TWO) | |
74 | ;; (list2 TWO THREE)))) | |
75 | ;; (hide-ifdef-use-define-alist 'list2))) ; use list2 by default | |
b578f267 | 76 | ;; |
482bb01b | 77 | ;; You can call hide-ifdef-use-define-alist (C-c @ U) at any time to specify |
b578f267 EN |
78 | ;; another list to use. |
79 | ;; | |
80 | ;; To cause ifdefs to be hidden as soon as hide-ifdef-mode is called, | |
81 | ;; set hide-ifdef-initially to non-nil. | |
82 | ;; | |
83 | ;; If you set hide-ifdef-lines to t, hide-ifdefs hides all the #ifdef lines. | |
84 | ;; In the absence of highlighting, that might be a bad idea. If you set | |
85 | ;; hide-ifdef-lines to nil (the default), the surrounding preprocessor | |
86 | ;; lines will be displayed. That can be confusing in its own | |
87 | ;; right. Other variations on display are possible, but not much | |
88 | ;; better. | |
89 | ;; | |
90 | ;; You can explicitly hide or show individual ifdef blocks irrespective | |
91 | ;; of the define list by using hide-ifdef-block and show-ifdef-block. | |
92 | ;; | |
93 | ;; You can move the point between ifdefs with forward-ifdef, backward-ifdef, | |
94 | ;; up-ifdef, down-ifdef, next-ifdef, and previous-ifdef. | |
95 | ;; | |
96 | ;; If you have minor-mode-alist in your mode line (the default) two labels | |
97 | ;; may appear. "Ifdef" will appear when hide-ifdef-mode is active. "Hiding" | |
98 | ;; will appear when text may be hidden ("hide-ifdef-hiding" is non-nil). | |
99 | ;; | |
100 | ;; Written by Brian Marick, at Gould, Computer Systems Division, Urbana IL. | |
101 | ;; Extensively modified by Daniel LaLiberte (while at Gould). | |
102 | ;; | |
103 | ;; You may freely modify and distribute this, but keep a record | |
104 | ;; of modifications and send comments to: | |
105 | ;; liberte@a.cs.uiuc.edu or ihnp4!uiucdcs!liberte | |
106 | ;; I will continue to upgrade hide-ifdef-mode | |
107 | ;; with your contributions. | |
175ce218 | 108 | |
fc68affa | 109 | ;;; Code: |
175ce218 | 110 | |
048fb1b7 RS |
111 | (require 'cc-mode) |
112 | ||
28d16ed3 AS |
113 | (defgroup hide-ifdef nil |
114 | "Hide selected code within `ifdef'." | |
115 | :group 'c) | |
116 | ||
067d92a1 SM |
117 | (defvar hide-ifdef-mode-submap |
118 | ;; Set up the submap that goes after the prefix key. | |
119 | (let ((map (make-sparse-keymap))) | |
120 | (define-key map "d" 'hide-ifdef-define) | |
121 | (define-key map "u" 'hide-ifdef-undef) | |
122 | (define-key map "D" 'hide-ifdef-set-define-alist) | |
123 | (define-key map "U" 'hide-ifdef-use-define-alist) | |
124 | ||
125 | (define-key map "h" 'hide-ifdefs) | |
126 | (define-key map "s" 'show-ifdefs) | |
127 | (define-key map "\C-d" 'hide-ifdef-block) | |
128 | (define-key map "\C-s" 'show-ifdef-block) | |
129 | ||
130 | (define-key map "\C-q" 'hide-ifdef-toggle-read-only) | |
131 | (substitute-key-definition | |
132 | 'toggle-read-only 'hide-ifdef-toggle-outside-read-only map) | |
133 | map) | |
134 | "Keymap used by `hide-ifdef-mode' under `hide-ifdef-mode-prefix-key'.") | |
175ce218 | 135 | |
0f21f770 | 136 | (defconst hide-ifdef-mode-prefix-key "\C-c@" |
073c9531 | 137 | "Prefix key for all Hide-Ifdef mode commands.") |
175ce218 | 138 | |
067d92a1 SM |
139 | (defvar hide-ifdef-mode-map |
140 | ;; Set up the mode's main map, which leads via the prefix key to the submap. | |
141 | (let ((map (make-sparse-keymap))) | |
142 | (define-key map hide-ifdef-mode-prefix-key hide-ifdef-mode-submap) | |
143 | map) | |
144 | "Keymap used with `hide-ifdef-mode'.") | |
175ce218 | 145 | |
47aef245 NR |
146 | (easy-menu-define hide-ifdef-mode-menu hide-ifdef-mode-map |
147 | "Menu for `hide-ifdef-mode'." | |
148 | '("Hide-Ifdef" | |
149 | ["Hide some ifdefs" hide-ifdefs t] | |
150 | ["Show all ifdefs" show-ifdefs t] | |
151 | ["Hide ifdef block" hide-ifdef-block t] | |
152 | ["Show ifdef block" show-ifdef-block t] | |
153 | ["Define a variable" hide-ifdef-define t] | |
154 | ["Define an alist" hide-ifdef-set-define-alist t] | |
155 | ["Use an alist" hide-ifdef-use-define-alist t] | |
156 | ["Undefine a variable" hide-ifdef-undef t] | |
157 | ["Toggle read only" hide-ifdef-toggle-read-only | |
158 | :style toggle :selected hide-ifdef-read-only])) | |
159 | ||
175ce218 | 160 | (defvar hide-ifdef-hiding nil |
36f063e8 RS |
161 | "Non-nil when text may be hidden.") |
162 | ||
175ce218 RS |
163 | (or (assq 'hide-ifdef-hiding minor-mode-alist) |
164 | (setq minor-mode-alist | |
165 | (cons '(hide-ifdef-hiding " Hiding") | |
166 | minor-mode-alist))) | |
167 | ||
e945e87d KH |
168 | ;; fix c-mode syntax table so we can recognize whole symbols. |
169 | (defvar hide-ifdef-syntax-table | |
067d92a1 SM |
170 | (let ((st (copy-syntax-table c-mode-syntax-table))) |
171 | (modify-syntax-entry ?_ "w" st) | |
172 | (modify-syntax-entry ?& "." st) | |
173 | (modify-syntax-entry ?\| "." st) | |
174 | st) | |
e945e87d KH |
175 | "Syntax table used for tokenizing #if expressions.") |
176 | ||
4e391a67 AS |
177 | (defvar hide-ifdef-env nil |
178 | "An alist of defined symbols and their values.") | |
179 | ||
180 | (defvar hif-outside-read-only nil | |
181 | "Internal variable. Saves the value of `buffer-read-only' while hiding.") | |
182 | ||
fbfed6f0 | 183 | ;;;###autoload |
067d92a1 | 184 | (define-minor-mode hide-ifdef-mode |
073c9531 | 185 | "Toggle Hide-Ifdef mode. This is a minor mode, albeit a large one. |
46b93246 | 186 | With ARG, turn Hide-Ifdef mode on if arg is positive, off otherwise. |
073c9531 | 187 | In Hide-Ifdef mode, code within #ifdef constructs that the C preprocessor |
175ce218 RS |
188 | would eliminate may be hidden from view. Several variables affect |
189 | how the hiding is done: | |
190 | ||
067d92a1 | 191 | `hide-ifdef-env' |
175ce218 | 192 | An association list of defined and undefined symbols for the |
073c9531 JB |
193 | current buffer. Initially, the global value of `hide-ifdef-env' |
194 | is used. | |
175ce218 | 195 | |
067d92a1 | 196 | `hide-ifdef-define-alist' |
a1506d29 | 197 | An association list of defined symbol lists. |
073c9531 JB |
198 | Use `hide-ifdef-set-define-alist' to save the current `hide-ifdef-env' |
199 | and `hide-ifdef-use-define-alist' to set the current `hide-ifdef-env' | |
200 | from one of the lists in `hide-ifdef-define-alist'. | |
175ce218 | 201 | |
067d92a1 | 202 | `hide-ifdef-lines' |
175ce218 RS |
203 | Set to non-nil to not show #if, #ifdef, #ifndef, #else, and |
204 | #endif lines when hiding. | |
205 | ||
067d92a1 | 206 | `hide-ifdef-initially' |
073c9531 | 207 | Indicates whether `hide-ifdefs' should be called when Hide-Ifdef mode |
175ce218 RS |
208 | is activated. |
209 | ||
067d92a1 | 210 | `hide-ifdef-read-only' |
175ce218 | 211 | Set to non-nil if you want to make buffers read only while hiding. |
073c9531 | 212 | After `show-ifdefs', read-only status is restored to previous value. |
175ce218 RS |
213 | |
214 | \\{hide-ifdef-mode-map}" | |
0f06a4df | 215 | :group 'hide-ifdef :lighter " Ifdef" |
175ce218 RS |
216 | (if hide-ifdef-mode |
217 | (progn | |
067d92a1 SM |
218 | ;; inherit global values |
219 | (set (make-local-variable 'hide-ifdef-env) | |
220 | (default-value 'hide-ifdef-env)) | |
221 | (set (make-local-variable 'hide-ifdef-hiding) | |
222 | (default-value 'hide-ifdef-hiding)) | |
223 | (set (make-local-variable 'hif-outside-read-only) buffer-read-only) | |
722fa77f | 224 | (set (make-local-variable 'line-move-ignore-invisible) t) |
f71d927d SM |
225 | (add-hook 'change-major-mode-hook |
226 | (lambda () (hide-ifdef-mode -1)) nil t) | |
175ce218 | 227 | |
067d92a1 | 228 | (add-to-invisibility-spec '(hide-ifdef . t)) |
175ce218 RS |
229 | |
230 | (if hide-ifdef-initially | |
231 | (hide-ifdefs) | |
067d92a1 SM |
232 | (show-ifdefs))) |
233 | ;; else end hide-ifdef-mode | |
722fa77f | 234 | (kill-local-variable 'line-move-ignore-invisible) |
f71d927d | 235 | (remove-from-invisibility-spec '(hide-ifdef . t)) |
175ce218 | 236 | (if hide-ifdef-hiding |
067d92a1 | 237 | (show-ifdefs)))) |
a1506d29 | 238 | |
175ce218 | 239 | |
175ce218 RS |
240 | (defun hif-show-all () |
241 | "Show all of the text in the current buffer." | |
242 | (interactive) | |
067d92a1 | 243 | (hif-show-ifdef-region (point-min) (point-max))) |
175ce218 | 244 | |
f5356416 RS |
245 | ;; By putting this on after-revert-hook, we arrange that it only |
246 | ;; does anything when revert-buffer avoids turning off the mode. | |
247 | ;; (That can happen in VC.) | |
067d92a1 | 248 | (defun hif-after-revert-function () |
f5356416 RS |
249 | (and hide-ifdef-mode hide-ifdef-hiding |
250 | (hide-ifdefs t))) | |
067d92a1 | 251 | (add-hook 'after-revert-hook 'hif-after-revert-function) |
f5356416 | 252 | |
722fa77f SM |
253 | (defun hif-end-of-line () |
254 | (end-of-line) | |
255 | (while (= (logand 1 (skip-chars-backward "\\\\")) 1) | |
256 | (end-of-line 2))) | |
257 | ||
258 | (defun hide-ifdef-region-internal (start end) | |
259 | (remove-overlays start end 'invisible 'hide-ifdef) | |
260 | (let ((o (make-overlay start end))) | |
261 | (overlay-put o 'invisible 'hide-ifdef))) | |
262 | ||
175ce218 RS |
263 | (defun hide-ifdef-region (start end) |
264 | "START is the start of a #if or #else form. END is the ending part. | |
265 | Everything including these lines is made invisible." | |
067d92a1 | 266 | (save-excursion |
722fa77f SM |
267 | (goto-char start) (hif-end-of-line) (setq start (point)) |
268 | (goto-char end) (hif-end-of-line) (setq end (point)) | |
269 | (hide-ifdef-region-internal start end))) | |
175ce218 RS |
270 | |
271 | (defun hif-show-ifdef-region (start end) | |
272 | "Everything between START and END is made visible." | |
067d92a1 | 273 | (remove-overlays start end 'invisible 'hide-ifdef)) |
175ce218 RS |
274 | |
275 | ||
067d92a1 | 276 | ;;===%%SF%% evaluation (Start) === |
175ce218 | 277 | |
f5356416 RS |
278 | ;; It is not useful to set this to anything but `eval'. |
279 | ;; In fact, the variable might as well be eliminated. | |
175ce218 | 280 | (defvar hide-ifdef-evaluator 'eval |
f5356416 RS |
281 | "The function to use to evaluate a form. |
282 | The evaluator is given a canonical form and returns t if text under | |
175ce218 RS |
283 | that form should be displayed.") |
284 | ||
285 | (defvar hif-undefined-symbol nil | |
286 | "...is by default considered to be false.") | |
287 | ||
175ce218 RS |
288 | |
289 | (defun hif-set-var (var value) | |
290 | "Prepend (var value) pair to hide-ifdef-env." | |
291 | (setq hide-ifdef-env (cons (cons var value) hide-ifdef-env))) | |
292 | ||
293 | ||
294 | (defun hif-lookup (var) | |
067d92a1 | 295 | ;; (message "hif-lookup %s" var) |
175ce218 RS |
296 | (let ((val (assoc var hide-ifdef-env))) |
297 | (if val | |
298 | (cdr val) | |
299 | hif-undefined-symbol))) | |
300 | ||
301 | (defun hif-defined (var) | |
722fa77f | 302 | (if (assoc var hide-ifdef-env) 1 0)) |
175ce218 | 303 | |
067d92a1 | 304 | ;;===%%SF%% evaluation (End) === |
175ce218 RS |
305 | |
306 | ||
307 | ||
067d92a1 | 308 | ;;===%%SF%% parsing (Start) === |
175ce218 RS |
309 | ;;; The code that understands what ifs and ifdef in files look like. |
310 | ||
311 | (defconst hif-cpp-prefix "\\(^\\|\r\\)[ \t]*#[ \t]*") | |
312 | (defconst hif-ifndef-regexp (concat hif-cpp-prefix "ifndef")) | |
313 | (defconst hif-ifx-regexp (concat hif-cpp-prefix "if\\(n?def\\)?[ \t]+")) | |
314 | (defconst hif-else-regexp (concat hif-cpp-prefix "else")) | |
315 | (defconst hif-endif-regexp (concat hif-cpp-prefix "endif")) | |
316 | (defconst hif-ifx-else-endif-regexp | |
317 | (concat hif-ifx-regexp "\\|" hif-else-regexp "\\|" hif-endif-regexp)) | |
318 | ||
067d92a1 SM |
319 | ;; Used to store the current token and the whole token list during parsing. |
320 | ;; Only bound dynamically. | |
4e391a67 AS |
321 | (defvar hif-token) |
322 | (defvar hif-token-list) | |
175ce218 | 323 | |
067d92a1 SM |
324 | ;; pattern to match initial identifier, !, &&, ||, (, or ). |
325 | ;; Added ==, + and -: garyo@avs.com 8/9/94 | |
722fa77f | 326 | (defconst hif-token-regexp |
2dc2ec3d | 327 | "\\(&&\\|||\\|[!=]=\\|!\\|[()+?:-]\\|[<>]=?\\|\\w+\\)") |
175ce218 | 328 | |
722fa77f SM |
329 | (defun hif-tokenize (start end) |
330 | "Separate string between START and END into a list of tokens." | |
331 | (let ((token-list nil)) | |
067d92a1 | 332 | (with-syntax-table hide-ifdef-syntax-table |
722fa77f SM |
333 | (save-excursion |
334 | (goto-char start) | |
335 | (while (progn (forward-comment (point-max)) (< (point) end)) | |
336 | ;; (message "expr-start = %d" expr-start) (sit-for 1) | |
337 | (cond | |
338 | ((looking-at "\\\\\n") | |
339 | (forward-char 2)) | |
340 | ||
341 | ((looking-at hif-token-regexp) | |
342 | (let ((token (buffer-substring (point) (match-end 0)))) | |
343 | (goto-char (match-end 0)) | |
344 | ;; (message "token: %s" token) (sit-for 1) | |
345 | (push (cond | |
346 | ((string-equal token "||") 'or) | |
347 | ((string-equal token "&&") 'and) | |
348 | ((string-equal token "==") 'equal) | |
349 | ((string-equal token "!=") 'hif-notequal) | |
350 | ((string-equal token "!") 'not) | |
351 | ((string-equal token "defined") 'hif-defined) | |
352 | ((string-equal token "(") 'lparen) | |
353 | ((string-equal token ")") 'rparen) | |
354 | ((string-equal token ">") 'hif-greater) | |
355 | ((string-equal token "<") 'hif-less) | |
356 | ((string-equal token ">=") 'hif-greater-equal) | |
357 | ((string-equal token "<=") 'hif-less-equal) | |
358 | ((string-equal token "+") 'hif-plus) | |
359 | ((string-equal token "-") 'hif-minus) | |
2dc2ec3d AS |
360 | ((string-equal token "?") 'hif-conditional) |
361 | ((string-equal token ":") 'hif-colon) | |
722fa77f SM |
362 | ((string-match "\\`[0-9]*\\'" token) |
363 | (string-to-number token)) | |
364 | (t (intern token))) | |
365 | token-list))) | |
366 | (t (error "Bad #if expression: %s" (buffer-string))))))) | |
e945e87d | 367 | (nreverse token-list))) |
175ce218 RS |
368 | |
369 | ;;;----------------------------------------------------------------- | |
370 | ;;; Translate C preprocessor #if expressions using recursive descent. | |
371 | ;;; This parser is limited to the operators &&, ||, !, and "defined". | |
7bbe1dea | 372 | ;;; Added ==, !=, +, and -. Gary Oberbrunner, garyo@avs.com, 8/9/94 |
175ce218 | 373 | |
f1259a53 SM |
374 | (defsubst hif-nexttoken () |
375 | "Pop the next token from token-list into the let variable \"hif-token\"." | |
376 | (setq hif-token (pop hif-token-list))) | |
377 | ||
4e391a67 | 378 | (defun hif-parse-if-exp (hif-token-list) |
175ce218 RS |
379 | "Parse the TOKEN-LIST. Return translated list in prefix form." |
380 | (hif-nexttoken) | |
381 | (prog1 | |
382 | (hif-expr) | |
4e391a67 AS |
383 | (if hif-token ; is there still a token? |
384 | (error "Error: unexpected token: %s" hif-token)))) | |
175ce218 | 385 | |
175ce218 | 386 | (defun hif-expr () |
f5356416 | 387 | "Parse an expression as found in #if. |
2dc2ec3d AS |
388 | expr : or-expr | or-expr '?' expr ':' expr." |
389 | (let ((result (hif-or-expr)) | |
390 | middle) | |
391 | (while (eq hif-token 'hif-conditional) | |
392 | (hif-nexttoken) | |
393 | (setq middle (hif-expr)) | |
394 | (if (eq hif-token 'hif-colon) | |
395 | (progn | |
396 | (hif-nexttoken) | |
397 | (setq result (list 'hif-conditional result middle (hif-expr)))) | |
398 | (error "Error: unexpected token: %s" hif-token))) | |
399 | result)) | |
400 | ||
401 | (defun hif-or-expr () | |
402 | "Parse n or-expr : and-expr | or-expr '||' and-expr." | |
403 | (let ((result (hif-and-expr))) | |
4e391a67 | 404 | (while (eq hif-token 'or) |
175ce218 | 405 | (hif-nexttoken) |
2dc2ec3d | 406 | (setq result (list 'hif-or result (hif-and-expr)))) |
073c9531 | 407 | result)) |
175ce218 | 408 | |
2dc2ec3d AS |
409 | (defun hif-and-expr () |
410 | "Parse an and-expr : eq-expr | and-expr '&&' eq-expr." | |
7bbe1dea | 411 | (let ((result (hif-eq-expr))) |
4e391a67 | 412 | (while (eq hif-token 'and) |
175ce218 | 413 | (hif-nexttoken) |
722fa77f | 414 | (setq result (list 'hif-and result (hif-eq-expr)))) |
073c9531 | 415 | result)) |
175ce218 | 416 | |
7bbe1dea | 417 | (defun hif-eq-expr () |
958ef4fe | 418 | "Parse an eq-expr : math | eq-expr `=='|`!='|`<'|`>'|`>='|`<=' math." |
7bbe1dea RS |
419 | (let ((result (hif-math)) |
420 | (eq-token nil)) | |
4e391a67 AS |
421 | (while (memq hif-token '(equal hif-notequal hif-greater hif-less |
422 | hif-greater-equal hif-less-equal)) | |
423 | (setq eq-token hif-token) | |
7bbe1dea RS |
424 | (hif-nexttoken) |
425 | (setq result (list eq-token result (hif-math)))) | |
426 | result)) | |
427 | ||
428 | (defun hif-math () | |
f5356416 | 429 | "Parse an expression with + or - and simpler things. |
7bbe1dea RS |
430 | math : factor | math '+|-' factor." |
431 | (let ((result (hif-factor)) | |
432 | (math-op nil)) | |
722fa77f | 433 | (while (memq hif-token '(hif-plus hif-minus)) |
4e391a67 | 434 | (setq math-op hif-token) |
7bbe1dea RS |
435 | (hif-nexttoken) |
436 | (setq result (list math-op result (hif-factor)))) | |
437 | result)) | |
a1506d29 | 438 | |
175ce218 | 439 | (defun hif-factor () |
f5356416 | 440 | "Parse a factor: '!' factor | '(' expr ')' | 'defined(' id ')' | id." |
175ce218 | 441 | (cond |
d8f1319a GM |
442 | ((eq hif-token 'not) |
443 | (hif-nexttoken) | |
722fa77f | 444 | (list 'hif-not (hif-factor))) |
d8f1319a GM |
445 | |
446 | ((eq hif-token 'lparen) | |
447 | (hif-nexttoken) | |
448 | (let ((result (hif-expr))) | |
449 | (if (not (eq hif-token 'rparen)) | |
450 | (error "Bad token in parenthesized expression: %s" hif-token) | |
175ce218 | 451 | (hif-nexttoken) |
d8f1319a GM |
452 | result))) |
453 | ||
454 | ((eq hif-token 'hif-defined) | |
455 | (hif-nexttoken) | |
722fa77f SM |
456 | (let ((paren (when (eq hif-token 'lparen) (hif-nexttoken) t)) |
457 | (ident hif-token)) | |
d8f1319a GM |
458 | (if (memq hif-token '(or and not hif-defined lparen rparen)) |
459 | (error "Error: unexpected token: %s" hif-token)) | |
722fa77f SM |
460 | (when paren |
461 | (hif-nexttoken) | |
462 | (unless (eq hif-token 'rparen) | |
463 | (error "Error: expected \")\" after identifier"))) | |
d8f1319a | 464 | (hif-nexttoken) |
067d92a1 | 465 | `(hif-defined (quote ,ident)))) |
d8f1319a | 466 | |
722fa77f SM |
467 | ((numberp hif-token) |
468 | (prog1 hif-token (hif-nexttoken))) | |
469 | ||
d8f1319a GM |
470 | (t ; identifier |
471 | (let ((ident hif-token)) | |
472 | (if (memq ident '(or and)) | |
473 | (error "Error: missing identifier")) | |
474 | (hif-nexttoken) | |
067d92a1 | 475 | `(hif-lookup (quote ,ident)))))) |
175ce218 | 476 | |
7bbe1dea RS |
477 | (defun hif-mathify (val) |
478 | "Treat VAL as a number: if it's t or nil, use 1 or 0." | |
067d92a1 SM |
479 | (cond ((eq val t) 1) |
480 | ((null val) 0) | |
7bbe1dea RS |
481 | (t val))) |
482 | ||
2dc2ec3d AS |
483 | (defun hif-conditional (a b c) |
484 | (if (not (zerop (hif-mathify a))) (hif-mathify b) (hif-mathify c))) | |
722fa77f SM |
485 | (defun hif-and (a b) |
486 | (and (not (zerop (hif-mathify a))) (not (zerop (hif-mathify b))))) | |
487 | (defun hif-or (a b) | |
488 | (or (not (zerop (hif-mathify a))) (not (zerop (hif-mathify b))))) | |
489 | (defun hif-not (a) | |
490 | (zerop (hif-mathify a))) | |
7bbe1dea RS |
491 | (defun hif-plus (a b) |
492 | "Like ordinary plus but treat t and nil as 1 and 0." | |
493 | (+ (hif-mathify a) (hif-mathify b))) | |
494 | (defun hif-minus (a b) | |
495 | "Like ordinary minus but treat t and nil as 1 and 0." | |
496 | (- (hif-mathify a) (hif-mathify b))) | |
497 | (defun hif-notequal (a b) | |
498 | "Like (not (equal A B)) but as one symbol." | |
499 | (not (equal a b))) | |
958ef4fe RS |
500 | (defun hif-greater (a b) |
501 | "Simple comparison." | |
502 | (> (hif-mathify a) (hif-mathify b))) | |
503 | (defun hif-less (a b) | |
504 | "Simple comparison." | |
505 | (< (hif-mathify a) (hif-mathify b))) | |
506 | (defun hif-greater-equal (a b) | |
507 | "Simple comparison." | |
508 | (>= (hif-mathify a) (hif-mathify b))) | |
509 | (defun hif-less-equal (a b) | |
510 | "Simple comparison." | |
511 | (<= (hif-mathify a) (hif-mathify b))) | |
175ce218 RS |
512 | ;;;----------- end of parser ----------------------- |
513 | ||
514 | ||
515 | (defun hif-canonicalize () | |
067d92a1 | 516 | "When at beginning of #ifX, return a Lisp expression for its condition." |
175ce218 RS |
517 | (save-excursion |
518 | (let ((negate (looking-at hif-ifndef-regexp))) | |
519 | (re-search-forward hif-ifx-regexp) | |
722fa77f SM |
520 | (let* ((tokens (hif-tokenize (point) |
521 | (progn (hif-end-of-line) (point)))) | |
522 | (expr (hif-parse-if-exp tokens))) | |
067d92a1 | 523 | ;; (message "hif-canonicalized: %s" expr) |
175ce218 | 524 | (if negate |
722fa77f | 525 | (list 'hif-not expr) |
175ce218 RS |
526 | expr))))) |
527 | ||
528 | ||
529 | (defun hif-find-any-ifX () | |
f5356416 | 530 | "Move to next #if..., or #ifndef, at point or after." |
067d92a1 | 531 | ;; (message "find ifX at %d" (point)) |
175ce218 RS |
532 | (prog1 |
533 | (re-search-forward hif-ifx-regexp (point-max) t) | |
534 | (beginning-of-line))) | |
535 | ||
536 | ||
537 | (defun hif-find-next-relevant () | |
f5356416 | 538 | "Move to next #if..., #else, or #endif, after the current line." |
067d92a1 | 539 | ;; (message "hif-find-next-relevant at %d" (point)) |
175ce218 | 540 | (end-of-line) |
067d92a1 | 541 | ;; avoid infinite recursion by only going to beginning of line if match found |
175ce218 | 542 | (if (re-search-forward hif-ifx-else-endif-regexp (point-max) t) |
073c9531 | 543 | (beginning-of-line))) |
175ce218 RS |
544 | |
545 | (defun hif-find-previous-relevant () | |
f5356416 | 546 | "Move to previous #if..., #else, or #endif, before the current line." |
067d92a1 | 547 | ;; (message "hif-find-previous-relevant at %d" (point)) |
175ce218 | 548 | (beginning-of-line) |
067d92a1 | 549 | ;; avoid infinite recursion by only going to beginning of line if match found |
175ce218 | 550 | (if (re-search-backward hif-ifx-else-endif-regexp (point-min) t) |
073c9531 | 551 | (beginning-of-line))) |
175ce218 RS |
552 | |
553 | ||
554 | (defun hif-looking-at-ifX () ;; Should eventually see #if | |
555 | (looking-at hif-ifx-regexp)) | |
556 | (defun hif-looking-at-endif () | |
557 | (looking-at hif-endif-regexp)) | |
558 | (defun hif-looking-at-else () | |
559 | (looking-at hif-else-regexp)) | |
560 | ||
561 | ||
562 | ||
563 | (defun hif-ifdef-to-endif () | |
564 | "If positioned at #ifX or #else form, skip to corresponding #endif." | |
067d92a1 | 565 | ;; (message "hif-ifdef-to-endif at %d" (point)) (sit-for 1) |
175ce218 RS |
566 | (hif-find-next-relevant) |
567 | (cond ((hif-looking-at-ifX) | |
568 | (hif-ifdef-to-endif) ; find endif of nested if | |
569 | (hif-ifdef-to-endif)) ; find outer endif or else | |
570 | ((hif-looking-at-else) | |
571 | (hif-ifdef-to-endif)) ; find endif following else | |
572 | ((hif-looking-at-endif) | |
573 | 'done) | |
574 | (t | |
eb8c3be9 | 575 | (error "Mismatched #ifdef #endif pair")))) |
175ce218 RS |
576 | |
577 | ||
578 | (defun hif-endif-to-ifdef () | |
579 | "If positioned at #endif form, skip backward to corresponding #ifX." | |
067d92a1 | 580 | ;; (message "hif-endif-to-ifdef at %d" (point)) |
175ce218 RS |
581 | (let ((start (point))) |
582 | (hif-find-previous-relevant) | |
583 | (if (= start (point)) | |
eb8c3be9 | 584 | (error "Mismatched #ifdef #endif pair"))) |
175ce218 RS |
585 | (cond ((hif-looking-at-endif) |
586 | (hif-endif-to-ifdef) ; find beginning of nested if | |
587 | (hif-endif-to-ifdef)) ; find beginning of outer if or else | |
588 | ((hif-looking-at-else) | |
589 | (hif-endif-to-ifdef)) | |
590 | ((hif-looking-at-ifX) | |
591 | 'done) | |
0b030df7 | 592 | (t))) ; never gets here |
175ce218 RS |
593 | |
594 | ||
595 | (defun forward-ifdef (&optional arg) | |
596 | "Move point to beginning of line of the next ifdef-endif. | |
073c9531 | 597 | With argument, do this that many times." |
175ce218 RS |
598 | (interactive "p") |
599 | (or arg (setq arg 1)) | |
067d92a1 SM |
600 | (if (< arg 0) (backward-ifdef (- arg)) |
601 | (while (< 0 arg) | |
602 | (setq arg (- arg)) | |
603 | (let ((start (point))) | |
604 | (unless (hif-looking-at-ifX) | |
175ce218 | 605 | (hif-find-next-relevant)) |
067d92a1 SM |
606 | (if (hif-looking-at-ifX) |
607 | (hif-ifdef-to-endif) | |
608 | (goto-char start) | |
609 | (error "No following #ifdef")))))) | |
175ce218 RS |
610 | |
611 | ||
612 | (defun backward-ifdef (&optional arg) | |
613 | "Move point to beginning of the previous ifdef-endif. | |
073c9531 | 614 | With argument, do this that many times." |
175ce218 RS |
615 | (interactive "p") |
616 | (or arg (setq arg 1)) | |
067d92a1 SM |
617 | (if (< arg 0) (forward-ifdef (- arg)) |
618 | (while (< 0 arg) | |
619 | (setq arg (1- arg)) | |
620 | (beginning-of-line) | |
621 | (let ((start (point))) | |
622 | (unless (hif-looking-at-endif) | |
175ce218 | 623 | (hif-find-previous-relevant)) |
067d92a1 SM |
624 | (if (hif-looking-at-endif) |
625 | (hif-endif-to-ifdef) | |
626 | (goto-char start) | |
627 | (error "No previous #ifdef")))))) | |
175ce218 RS |
628 | |
629 | ||
630 | (defun down-ifdef () | |
631 | "Move point to beginning of nested ifdef or else-part." | |
632 | (interactive) | |
633 | (let ((start (point))) | |
634 | (hif-find-next-relevant) | |
635 | (if (or (hif-looking-at-ifX) (hif-looking-at-else)) | |
636 | () | |
637 | (goto-char start) | |
073c9531 | 638 | (error "No following #ifdef")))) |
175ce218 RS |
639 | |
640 | ||
641 | (defun up-ifdef () | |
642 | "Move point to beginning of enclosing ifdef or else-part." | |
643 | (interactive) | |
644 | (beginning-of-line) | |
645 | (let ((start (point))) | |
067d92a1 SM |
646 | (unless (hif-looking-at-endif) |
647 | (hif-find-previous-relevant)) | |
175ce218 RS |
648 | (if (hif-looking-at-endif) |
649 | (hif-endif-to-ifdef)) | |
650 | (if (= start (point)) | |
073c9531 | 651 | (error "No previous #ifdef")))) |
175ce218 RS |
652 | |
653 | (defun next-ifdef (&optional arg) | |
654 | "Move to the beginning of the next #ifX, #else, or #endif. | |
073c9531 | 655 | With argument, do this that many times." |
175ce218 RS |
656 | (interactive "p") |
657 | (or arg (setq arg 1)) | |
067d92a1 SM |
658 | (if (< arg 0) (previous-ifdef (- arg)) |
659 | (while (< 0 arg) | |
660 | (setq arg (1- arg)) | |
661 | (hif-find-next-relevant) | |
662 | (when (eolp) | |
663 | (beginning-of-line) | |
664 | (error "No following #ifdefs, #elses, or #endifs"))))) | |
175ce218 RS |
665 | |
666 | (defun previous-ifdef (&optional arg) | |
667 | "Move to the beginning of the previous #ifX, #else, or #endif. | |
073c9531 | 668 | With argument, do this that many times." |
175ce218 RS |
669 | (interactive "p") |
670 | (or arg (setq arg 1)) | |
067d92a1 SM |
671 | (if (< arg 0) (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")))))) | |
175ce218 RS |
678 | |
679 | ||
067d92a1 | 680 | ;;===%%SF%% parsing (End) === |
175ce218 RS |
681 | |
682 | ||
067d92a1 | 683 | ;;===%%SF%% hide-ifdef-hiding (Start) === |
175ce218 RS |
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 | ||
722fa77f | 693 | (defsubst hif-make-range (start end &optional else) |
067d92a1 | 694 | (list start else end)) |
175ce218 | 695 | |
722fa77f SM |
696 | (defsubst hif-range-start (range) (elt range 0)) |
697 | (defsubst hif-range-else (range) (elt range 1)) | |
698 | (defsubst hif-range-end (range) (elt range 2)) | |
175ce218 RS |
699 | |
700 | ||
701 | ||
702 | ;;; Find-Range | |
703 | ;;; The workhorse, it delimits the #if region. Reasonably simple: | |
704 | ;;; Skip until an #else or #endif is found, remembering positions. If | |
705 | ;;; an #else was found, skip some more, looking for the true #endif. | |
706 | ||
707 | (defun hif-find-range () | |
067d92a1 | 708 | "Return a Range structure describing the current #if region. |
175ce218 | 709 | Point is left unchanged." |
067d92a1 | 710 | ;; (message "hif-find-range at %d" (point)) |
175ce218 RS |
711 | (save-excursion |
712 | (beginning-of-line) | |
713 | (let ((start (point)) | |
175ce218 RS |
714 | (else nil) |
715 | (end nil)) | |
716 | ;; Part one. Look for either #endif or #else. | |
717 | ;; This loop-and-a-half dedicated to E. Dijkstra. | |
067d92a1 SM |
718 | (while (progn |
719 | (hif-find-next-relevant) | |
720 | (hif-looking-at-ifX)) ; Skip nested ifdef | |
721 | (hif-ifdef-to-endif)) | |
175ce218 RS |
722 | ;; Found either a #else or an #endif. |
723 | (cond ((hif-looking-at-else) | |
175ce218 RS |
724 | (setq else (point))) |
725 | (t | |
067d92a1 | 726 | (setq end (point)))) ; (save-excursion (end-of-line) (point)) |
175ce218 | 727 | ;; If found #else, look for #endif. |
067d92a1 SM |
728 | (when else |
729 | (while (progn | |
730 | (hif-find-next-relevant) | |
731 | (hif-looking-at-ifX)) ; Skip nested ifdef | |
732 | (hif-ifdef-to-endif)) | |
733 | (if (hif-looking-at-else) | |
734 | (error "Found two elses in a row? Broken!")) | |
735 | (setq end (point))) ; (save-excursion (end-of-line) (point)) | |
736 | (hif-make-range start end else)))) | |
175ce218 | 737 | |
a1506d29 | 738 | |
175ce218 | 739 | ;;; A bit slimy. |
175ce218 RS |
740 | |
741 | (defun hif-hide-line (point) | |
073c9531 | 742 | "Hide the line containing point. Does nothing if `hide-ifdef-lines' is nil." |
175ce218 RS |
743 | (if hide-ifdef-lines |
744 | (save-excursion | |
745 | (goto-char point) | |
722fa77f SM |
746 | (hide-ifdef-region-internal (line-beginning-position) |
747 | (progn (hif-end-of-line) (point)))))) | |
a1506d29 | 748 | |
175ce218 RS |
749 | |
750 | ;;; Hif-Possibly-Hide | |
751 | ;;; There are four cases. The #ifX expression is "taken" if it | |
752 | ;;; the hide-ifdef-evaluator returns T. Presumably, this means the code | |
753 | ;;; inside the #ifdef would be included when the program was | |
a1506d29 | 754 | ;;; compiled. |
175ce218 RS |
755 | ;;; |
756 | ;;; Case 1: #ifX taken, and there's an #else. | |
757 | ;;; The #else part must be hidden. The #if (then) part must be | |
758 | ;;; processed for nested #ifX's. | |
759 | ;;; Case 2: #ifX taken, and there's no #else. | |
760 | ;;; The #if part must be processed for nested #ifX's. | |
761 | ;;; Case 3: #ifX not taken, and there's an #else. | |
762 | ;;; The #if part must be hidden. The #else part must be processed | |
763 | ;;; for nested #ifs. | |
764 | ;;; Case 4: #ifX not taken, and there's no #else. | |
765 | ;;; The #ifX part must be hidden. | |
766 | ;;; | |
767 | ;;; Further processing is done by narrowing to the relevant region | |
768 | ;;; and just recursively calling hide-ifdef-guts. | |
769 | ;;; | |
770 | ;;; When hif-possibly-hide returns, point is at the end of the | |
771 | ;;; possibly-hidden range. | |
772 | ||
773 | (defun hif-recurse-on (start end) | |
073c9531 | 774 | "Call `hide-ifdef-guts' after narrowing to end of START line and END line." |
175ce218 RS |
775 | (save-excursion |
776 | (save-restriction | |
777 | (goto-char start) | |
778 | (end-of-line) | |
779 | (narrow-to-region (point) end) | |
780 | (hide-ifdef-guts)))) | |
781 | ||
782 | (defun hif-possibly-hide () | |
f5356416 RS |
783 | "Called at #ifX expression, this hides those parts that should be hidden. |
784 | It uses the judgement of `hide-ifdef-evaluator'." | |
067d92a1 SM |
785 | ;; (message "hif-possibly-hide") (sit-for 1) |
786 | (let ((test (hif-canonicalize)) | |
787 | (range (hif-find-range))) | |
788 | ;; (message "test = %s" test) (sit-for 1) | |
a1506d29 | 789 | |
067d92a1 | 790 | (hif-hide-line (hif-range-end range)) |
722fa77f SM |
791 | (if (not (hif-not (funcall hide-ifdef-evaluator test))) |
792 | (cond ((hif-range-else range) ; case 1 | |
175ce218 | 793 | (hif-hide-line (hif-range-else range)) |
722fa77f | 794 | (hide-ifdef-region (hif-range-else range) |
067d92a1 SM |
795 | (1- (hif-range-end range))) |
796 | (hif-recurse-on (hif-range-start range) | |
797 | (hif-range-else range))) | |
798 | (t ; case 2 | |
799 | (hif-recurse-on (hif-range-start range) | |
800 | (hif-range-end range)))) | |
801 | (cond ((hif-range-else range) ; case 3 | |
802 | (hif-hide-line (hif-range-else range)) | |
803 | (hide-ifdef-region (hif-range-start range) | |
804 | (1- (hif-range-else range))) | |
805 | (hif-recurse-on (hif-range-else range) | |
806 | (hif-range-end range))) | |
807 | (t ; case 4 | |
808 | (hide-ifdef-region (point) | |
809 | (1- (hif-range-end range)))))) | |
810 | (hif-hide-line (hif-range-start range)) ; Always hide start. | |
811 | (goto-char (hif-range-end range)) | |
812 | (end-of-line))) | |
175ce218 RS |
813 | |
814 | ||
815 | ||
816 | (defun hide-ifdef-guts () | |
f5356416 RS |
817 | "Does most of the work of `hide-ifdefs'. |
818 | It does not do the work that's pointless to redo on a recursive entry." | |
067d92a1 | 819 | ;; (message "hide-ifdef-guts") |
175ce218 RS |
820 | (save-excursion |
821 | (goto-char (point-min)) | |
822 | (while (hif-find-any-ifX) | |
823 | (hif-possibly-hide)))) | |
824 | ||
067d92a1 | 825 | ;;===%%SF%% hide-ifdef-hiding (End) === |
175ce218 RS |
826 | |
827 | ||
067d92a1 | 828 | ;;===%%SF%% exports (Start) === |
175ce218 | 829 | |
fbfed6f0 | 830 | ;;;###autoload |
28d16ed3 AS |
831 | (defcustom hide-ifdef-initially nil |
832 | "*Non-nil means call `hide-ifdefs' when Hide-Ifdef mode is first activated." | |
833 | :type 'boolean | |
834 | :group 'hide-ifdef) | |
175ce218 | 835 | |
fbfed6f0 | 836 | ;;;###autoload |
28d16ed3 AS |
837 | (defcustom hide-ifdef-read-only nil |
838 | "*Set to non-nil if you want buffer to be read-only while hiding text." | |
839 | :type 'boolean | |
840 | :group 'hide-ifdef) | |
175ce218 | 841 | |
fbfed6f0 | 842 | ;;;###autoload |
28d16ed3 AS |
843 | (defcustom hide-ifdef-lines nil |
844 | "*Non-nil means hide the #ifX, #else, and #endif lines." | |
845 | :type 'boolean | |
846 | :group 'hide-ifdef) | |
175ce218 RS |
847 | |
848 | (defun hide-ifdef-toggle-read-only () | |
067d92a1 | 849 | "Toggle `hide-ifdef-read-only'." |
175ce218 RS |
850 | (interactive) |
851 | (setq hide-ifdef-read-only (not hide-ifdef-read-only)) | |
852 | (message "Hide-Read-Only %s" | |
853 | (if hide-ifdef-read-only "ON" "OFF")) | |
854 | (if hide-ifdef-hiding | |
855 | (setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only))) | |
c63e0f9a | 856 | (force-mode-line-update)) |
175ce218 RS |
857 | |
858 | (defun hide-ifdef-toggle-outside-read-only () | |
f5356416 | 859 | "Replacement for `toggle-read-only' within Hide-Ifdef mode." |
175ce218 RS |
860 | (interactive) |
861 | (setq hif-outside-read-only (not hif-outside-read-only)) | |
862 | (message "Read only %s" | |
863 | (if hif-outside-read-only "ON" "OFF")) | |
864 | (setq buffer-read-only | |
865 | (or (and hide-ifdef-hiding hide-ifdef-read-only) | |
067d92a1 | 866 | hif-outside-read-only)) |
c63e0f9a | 867 | (force-mode-line-update)) |
175ce218 | 868 | |
a1506d29 | 869 | |
175ce218 RS |
870 | (defun hide-ifdef-define (var) |
871 | "Define a VAR so that #ifdef VAR would be included." | |
872 | (interactive "SDefine what? ") | |
7bbe1dea | 873 | (hif-set-var var 1) |
175ce218 RS |
874 | (if hide-ifdef-hiding (hide-ifdefs))) |
875 | ||
876 | (defun hide-ifdef-undef (var) | |
877 | "Undefine a VAR so that #ifdef VAR would not be included." | |
878 | (interactive "SUndefine what? ") | |
879 | (hif-set-var var nil) | |
880 | (if hide-ifdef-hiding (hide-ifdefs))) | |
881 | ||
882 | ||
f5356416 | 883 | (defun hide-ifdefs (&optional nomsg) |
a1506d29 JB |
884 | "Hide the contents of some #ifdefs. |
885 | Assume that defined symbols have been added to `hide-ifdef-env'. | |
579e495a CZ |
886 | The text hidden is the text that would not be included by the C |
887 | preprocessor if it were given the file with those symbols defined. | |
175ce218 | 888 | |
d1fa6aff | 889 | Turn off hiding by calling `show-ifdefs'." |
175ce218 RS |
890 | |
891 | (interactive) | |
892 | (message "Hiding...") | |
3edc14ae | 893 | (setq hif-outside-read-only buffer-read-only) |
067d92a1 | 894 | (unless hide-ifdef-mode (hide-ifdef-mode 1)) ; turn on hide-ifdef-mode |
175ce218 RS |
895 | (if hide-ifdef-hiding |
896 | (show-ifdefs)) ; Otherwise, deep confusion. | |
067d92a1 SM |
897 | (setq hide-ifdef-hiding t) |
898 | (hide-ifdef-guts) | |
36f063e8 | 899 | (setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only)) |
f5356416 RS |
900 | (or nomsg |
901 | (message "Hiding done"))) | |
175ce218 RS |
902 | |
903 | ||
904 | (defun show-ifdefs () | |
f5356416 | 905 | "Cancel the effects of `hide-ifdef': show the contents of all #ifdefs." |
175ce218 | 906 | (interactive) |
36f063e8 | 907 | (setq buffer-read-only hif-outside-read-only) |
067d92a1 | 908 | (hif-show-all) |
073c9531 | 909 | (setq hide-ifdef-hiding nil)) |
175ce218 RS |
910 | |
911 | ||
912 | (defun hif-find-ifdef-block () | |
eb8c3be9 | 913 | "Utility for hide and show `ifdef-block'. |
4e391a67 | 914 | Return as (TOP . BOTTOM) the extent of ifdef block." |
175ce218 | 915 | (let (max-bottom) |
4e391a67 AS |
916 | (cons (save-excursion |
917 | (beginning-of-line) | |
067d92a1 SM |
918 | (unless (or (hif-looking-at-else) (hif-looking-at-ifX)) |
919 | (up-ifdef)) | |
4e391a67 AS |
920 | (prog1 (point) |
921 | (hif-ifdef-to-endif) | |
922 | (setq max-bottom (1- (point))))) | |
923 | (save-excursion | |
924 | (beginning-of-line) | |
067d92a1 SM |
925 | (unless (hif-looking-at-endif) |
926 | (hif-find-next-relevant)) | |
4e391a67 AS |
927 | (while (hif-looking-at-ifX) |
928 | (hif-ifdef-to-endif) | |
929 | (hif-find-next-relevant)) | |
930 | (min max-bottom (1- (point))))))) | |
175ce218 RS |
931 | |
932 | ||
933 | (defun hide-ifdef-block () | |
934 | "Hide the ifdef block (true or false part) enclosing or before the cursor." | |
935 | (interactive) | |
067d92a1 SM |
936 | (unless hide-ifdef-mode (hide-ifdef-mode 1)) |
937 | (let ((top-bottom (hif-find-ifdef-block))) | |
4e391a67 | 938 | (hide-ifdef-region (car top-bottom) (cdr top-bottom)) |
067d92a1 SM |
939 | (when hide-ifdef-lines |
940 | (hif-hide-line (car top-bottom)) | |
941 | (hif-hide-line (1+ (cdr top-bottom)))) | |
073c9531 | 942 | (setq hide-ifdef-hiding t)) |
36f063e8 | 943 | (setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only))) |
175ce218 | 944 | |
175ce218 RS |
945 | (defun show-ifdef-block () |
946 | "Show the ifdef block (true or false part) enclosing or before the cursor." | |
947 | (interactive) | |
d689858f EZ |
948 | (let ((top-bottom (hif-find-ifdef-block))) |
949 | (if hide-ifdef-lines | |
950 | (hif-show-ifdef-region | |
951 | (save-excursion | |
952 | (goto-char (car top-bottom)) (line-beginning-position)) | |
953 | (save-excursion | |
954 | (goto-char (1+ (cdr top-bottom))) | |
955 | (hif-end-of-line) (point))) | |
067d92a1 | 956 | (hif-show-ifdef-region (1- (car top-bottom)) (cdr top-bottom))))) |
175ce218 RS |
957 | |
958 | ||
eb8c3be9 | 959 | ;;; definition alist support |
175ce218 RS |
960 | |
961 | (defvar hide-ifdef-define-alist nil | |
067d92a1 | 962 | "A global assoc list of pre-defined symbol lists.") |
175ce218 RS |
963 | |
964 | (defun hif-compress-define-list (env) | |
965 | "Compress the define list ENV into a list of defined symbols only." | |
067d92a1 SM |
966 | (let ((new-defs nil)) |
967 | (dolist (def env new-defs) | |
968 | (if (hif-lookup (car def)) (push (car env) new-defs))))) | |
175ce218 RS |
969 | |
970 | (defun hide-ifdef-set-define-alist (name) | |
579e495a | 971 | "Set the association for NAME to `hide-ifdef-env'." |
175ce218 | 972 | (interactive "SSet define list: ") |
067d92a1 SM |
973 | (push (cons name (hif-compress-define-list hide-ifdef-env)) |
974 | hide-ifdef-define-alist)) | |
175ce218 RS |
975 | |
976 | (defun hide-ifdef-use-define-alist (name) | |
579e495a | 977 | "Set `hide-ifdef-env' to the define list specified by NAME." |
48d66f99 KS |
978 | (interactive |
979 | (list (completing-read "Use define list: " | |
521b2748 SM |
980 | (mapcar (lambda (x) (symbol-name (car x))) |
981 | hide-ifdef-define-alist) | |
982 | nil t))) | |
48d66f99 | 983 | (if (stringp name) (setq name (intern name))) |
175ce218 RS |
984 | (let ((define-list (assoc name hide-ifdef-define-alist))) |
985 | (if define-list | |
986 | (setq hide-ifdef-env | |
16fdbdce | 987 | (mapcar (lambda (arg) (cons arg t)) |
175ce218 RS |
988 | (cdr define-list))) |
989 | (error "No define list for %s" name)) | |
073c9531 | 990 | (if hide-ifdef-hiding (hide-ifdefs)))) |
175ce218 | 991 | |
048fb1b7 RS |
992 | (provide 'hideif) |
993 | ||
521b2748 | 994 | ;; arch-tag: c6381d17-a59a-483a-b945-658f22277981 |
1a06eabd | 995 | ;;; hideif.el ends here |