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