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