]> code.delx.au - gnu-emacs/blob - lisp/progmodes/hideif.el
ChangeLog fixes
[gnu-emacs] / lisp / progmodes / hideif.el
1 ;;; hideif.el --- hides selected code within ifdef
2
3 ;; Copyright (C) 1988, 1994, 2001-2014 Free Software Foundation, Inc.
4
5 ;; Author: Brian Marick
6 ;; Daniel LaLiberte <liberte@holonexus.org>
7 ;; Maintainer: emacs-devel@gnu.org
8 ;; Keywords: c, outlines
9
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 3 of the License, or
15 ;; (at your option) 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
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;; To initialize, toggle the hide-ifdef minor mode with
28 ;;
29 ;; M-x hide-ifdef-mode
30 ;;
31 ;; This will set up key bindings and call hide-ifdef-mode-hook if it
32 ;; has a value. To explicitly hide ifdefs using a buffer-local
33 ;; define list (default empty), type
34 ;;
35 ;; M-x hide-ifdefs or C-c @ h
36 ;;
37 ;; Hide-ifdef suppresses the display of code that the preprocessor wouldn't
38 ;; pass through. Support complete C/C++ expression and precedence.
39 ;;
40 ;; The hidden code is marked by ellipses (...). Be
41 ;; cautious when editing near ellipses, since the hidden text is
42 ;; still in the buffer, and you can move the point into it and modify
43 ;; text unawares.
44 ;; You can make your buffer read-only while hide-ifdef-hiding by setting
45 ;; hide-ifdef-read-only to a non-nil value. You can toggle this
46 ;; variable with hide-ifdef-toggle-read-only (C-c @ C-q).
47 ;;
48 ;; You can undo the effect of hide-ifdefs by typing
49 ;;
50 ;; M-x show-ifdefs or C-c @ s
51 ;;
52 ;; Use M-x hide-ifdef-define (C-c @ d) to define a symbol.
53 ;; Use M-x hide-ifdef-undef (C-c @ u) to undefine a symbol.
54 ;;
55 ;; If you define or undefine a symbol while hide-ifdef-mode is in effect,
56 ;; the display will be updated. Only the define list for the current
57 ;; buffer will be affected. You can save changes to the local define
58 ;; list with hide-ifdef-set-define-alist. This adds entries
59 ;; to hide-ifdef-define-alist.
60 ;;
61 ;; If you have defined a hide-ifdef-mode-hook, you can set
62 ;; up a list of symbols that may be used by hide-ifdefs as in the
63 ;; following example:
64 ;;
65 ;; (add-hook 'hide-ifdef-mode-hook
66 ;; (lambda ()
67 ;; (unless hide-ifdef-define-alist
68 ;; (setq hide-ifdef-define-alist
69 ;; '((list1 ONE TWO)
70 ;; (list2 TWO THREE))))
71 ;; (hide-ifdef-use-define-alist 'list2))) ; use list2 by default
72 ;;
73 ;; You can call hide-ifdef-use-define-alist (C-c @ U) at any time to specify
74 ;; another list to use.
75 ;;
76 ;; To cause ifdefs to be hidden as soon as hide-ifdef-mode is called,
77 ;; set hide-ifdef-initially to non-nil.
78 ;;
79 ;; If you set hide-ifdef-lines to t, hide-ifdefs hides all the #ifdef lines.
80 ;; In the absence of highlighting, that might be a bad idea. If you set
81 ;; hide-ifdef-lines to nil (the default), the surrounding preprocessor
82 ;; lines will be displayed. That can be confusing in its own
83 ;; right. Other variations on display are possible, but not much
84 ;; better.
85 ;;
86 ;; You can explicitly hide or show individual ifdef blocks irrespective
87 ;; of the define list by using hide-ifdef-block and show-ifdef-block.
88 ;;
89 ;; You can move the point between ifdefs with forward-ifdef, backward-ifdef,
90 ;; up-ifdef, down-ifdef, next-ifdef, and previous-ifdef.
91 ;;
92 ;; If you have minor-mode-alist in your mode line (the default) two labels
93 ;; may appear. "Ifdef" will appear when hide-ifdef-mode is active. "Hiding"
94 ;; will appear when text may be hidden ("hide-ifdef-hiding" is non-nil).
95 ;;
96 ;; Written by Brian Marick, at Gould, Computer Systems Division, Urbana IL.
97 ;; Extensively modified by Daniel LaLiberte (while at Gould).
98 ;;
99 ;; Extensively modified by Luke Lee in 2013 to support complete C expression
100 ;; evaluation.
101
102 ;;; Code:
103
104 (require 'cc-mode)
105
106 (defgroup hide-ifdef nil
107 "Hide selected code within `ifdef'."
108 :group 'c)
109
110 (defcustom hide-ifdef-initially nil
111 "Non-nil means call `hide-ifdefs' when Hide-Ifdef mode is first activated."
112 :type 'boolean
113 :group 'hide-ifdef)
114
115 (defcustom hide-ifdef-read-only nil
116 "Set to non-nil if you want buffer to be read-only while hiding text."
117 :type 'boolean
118 :group 'hide-ifdef)
119
120 (defcustom hide-ifdef-lines nil
121 "Non-nil means hide the #ifX, #else, and #endif lines."
122 :type 'boolean
123 :group 'hide-ifdef)
124
125 (defcustom hide-ifdef-shadow nil
126 "Non-nil means shadow text instead of hiding it."
127 :type 'boolean
128 :group 'hide-ifdef
129 :version "23.1")
130
131 (defface hide-ifdef-shadow '((t (:inherit shadow)))
132 "Face for shadowing ifdef blocks."
133 :group 'hide-ifdef
134 :version "23.1")
135
136
137 (defvar hide-ifdef-mode-submap
138 ;; Set up the submap that goes after the prefix key.
139 (let ((map (make-sparse-keymap)))
140 (define-key map "d" 'hide-ifdef-define)
141 (define-key map "u" 'hide-ifdef-undef)
142 (define-key map "D" 'hide-ifdef-set-define-alist)
143 (define-key map "U" 'hide-ifdef-use-define-alist)
144
145 (define-key map "h" 'hide-ifdefs)
146 (define-key map "s" 'show-ifdefs)
147 (define-key map "\C-d" 'hide-ifdef-block)
148 (define-key map "\C-s" 'show-ifdef-block)
149
150 (define-key map "\C-q" 'hide-ifdef-toggle-read-only)
151 (define-key map "\C-w" 'hide-ifdef-toggle-shadowing)
152 (substitute-key-definition
153 'read-only-mode 'hide-ifdef-toggle-outside-read-only map)
154 ;; `toggle-read-only' is obsoleted by `read-only-mode'.
155 (substitute-key-definition
156 'toggle-read-only 'hide-ifdef-toggle-outside-read-only map)
157 map)
158 "Keymap used by `hide-ifdef-mode' under `hide-ifdef-mode-prefix-key'.")
159
160 (defconst hide-ifdef-mode-prefix-key "\C-c@"
161 "Prefix key for all Hide-Ifdef mode commands.")
162
163 (defvar hide-ifdef-mode-map
164 ;; Set up the mode's main map, which leads via the prefix key to the submap.
165 (let ((map (make-sparse-keymap)))
166 (define-key map hide-ifdef-mode-prefix-key hide-ifdef-mode-submap)
167 map)
168 "Keymap used with `hide-ifdef-mode'.")
169
170 (easy-menu-define hide-ifdef-mode-menu hide-ifdef-mode-map
171 "Menu for `hide-ifdef-mode'."
172 '("Hide-Ifdef"
173 ["Hide some ifdefs" hide-ifdefs
174 :help "Hide the contents of some #ifdefs"]
175 ["Show all ifdefs" show-ifdefs
176 :help "Cancel the effects of `hide-ifdef': show the contents of all #ifdefs"]
177 ["Hide ifdef block" hide-ifdef-block
178 :help "Hide the ifdef block (true or false part) enclosing or before the cursor"]
179 ["Show ifdef block" show-ifdef-block
180 :help "Show the ifdef block (true or false part) enclosing or before the cursor"]
181 ["Define a variable..." hide-ifdef-define
182 :help "Define a VAR so that #ifdef VAR would be included"]
183 ["Undefine a variable..." hide-ifdef-undef
184 :help "Undefine a VAR so that #ifdef VAR would not be included"]
185 ["Define an alist..." hide-ifdef-set-define-alist
186 :help "Set the association for NAME to `hide-ifdef-env'"]
187 ["Use an alist..." hide-ifdef-use-define-alist
188 :help "Set `hide-ifdef-env' to the define list specified by NAME"]
189 ["Toggle read only" hide-ifdef-toggle-read-only
190 :style toggle :selected hide-ifdef-read-only
191 :help "Buffer should be read-only while hiding text"]
192 ["Toggle shadowing" hide-ifdef-toggle-shadowing
193 :style toggle :selected hide-ifdef-shadow
194 :help "Text should be shadowed instead of hidden"]))
195
196 (defvar hide-ifdef-hiding nil
197 "Non-nil when text may be hidden.")
198
199 (or (assq 'hide-ifdef-hiding minor-mode-alist)
200 (setq minor-mode-alist
201 (cons '(hide-ifdef-hiding " Hiding")
202 minor-mode-alist)))
203
204 ;; fix c-mode syntax table so we can recognize whole symbols.
205 (defvar hide-ifdef-syntax-table
206 (let ((st (copy-syntax-table c-mode-syntax-table)))
207 (modify-syntax-entry ?_ "w" st)
208 (modify-syntax-entry ?& "." st)
209 (modify-syntax-entry ?\| "." st)
210 st)
211 "Syntax table used for tokenizing #if expressions.")
212
213 (defvar hide-ifdef-env nil
214 "An alist of defined symbols and their values.")
215
216 (defvar hif-outside-read-only nil
217 "Internal variable. Saves the value of `buffer-read-only' while hiding.")
218
219 ;;;###autoload
220 (define-minor-mode hide-ifdef-mode
221 "Toggle features to hide/show #ifdef blocks (Hide-Ifdef mode).
222 With a prefix argument ARG, enable Hide-Ifdef mode if ARG is
223 positive, and disable it otherwise. If called from Lisp, enable
224 the mode if ARG is omitted or nil.
225
226 Hide-Ifdef mode is a buffer-local minor mode for use with C and
227 C-like major modes. When enabled, code within #ifdef constructs
228 that the C preprocessor would eliminate may be hidden from view.
229 Several variables affect how the hiding is done:
230
231 `hide-ifdef-env'
232 An association list of defined and undefined symbols for the
233 current buffer. Initially, the global value of `hide-ifdef-env'
234 is used.
235
236 `hide-ifdef-define-alist'
237 An association list of defined symbol lists.
238 Use `hide-ifdef-set-define-alist' to save the current `hide-ifdef-env'
239 and `hide-ifdef-use-define-alist' to set the current `hide-ifdef-env'
240 from one of the lists in `hide-ifdef-define-alist'.
241
242 `hide-ifdef-lines'
243 Set to non-nil to not show #if, #ifdef, #ifndef, #else, and
244 #endif lines when hiding.
245
246 `hide-ifdef-initially'
247 Indicates whether `hide-ifdefs' should be called when Hide-Ifdef mode
248 is activated.
249
250 `hide-ifdef-read-only'
251 Set to non-nil if you want to make buffers read only while hiding.
252 After `show-ifdefs', read-only status is restored to previous value.
253
254 \\{hide-ifdef-mode-map}"
255 :group 'hide-ifdef :lighter " Ifdef"
256 (if hide-ifdef-mode
257 (progn
258 ;; inherit global values
259 (set (make-local-variable 'hide-ifdef-env)
260 (default-value 'hide-ifdef-env))
261 (set (make-local-variable 'hide-ifdef-hiding)
262 (default-value 'hide-ifdef-hiding))
263 (set (make-local-variable 'hif-outside-read-only) buffer-read-only)
264 (set (make-local-variable 'line-move-ignore-invisible) t)
265 (add-hook 'change-major-mode-hook
266 (lambda () (hide-ifdef-mode -1)) nil t)
267
268 (add-to-invisibility-spec '(hide-ifdef . t))
269
270 (if hide-ifdef-initially
271 (hide-ifdefs)
272 (show-ifdefs)))
273 ;; else end hide-ifdef-mode
274 (kill-local-variable 'line-move-ignore-invisible)
275 (remove-from-invisibility-spec '(hide-ifdef . t))
276 (when hide-ifdef-hiding
277 (show-ifdefs))))
278
279
280 (defun hif-show-all ()
281 "Show all of the text in the current buffer."
282 (interactive)
283 (hif-show-ifdef-region (point-min) (point-max)))
284
285 ;; By putting this on after-revert-hook, we arrange that it only
286 ;; does anything when revert-buffer avoids turning off the mode.
287 ;; (That can happen in VC.)
288 (defun hif-after-revert-function ()
289 (and hide-ifdef-mode hide-ifdef-hiding
290 (hide-ifdefs t)))
291 (add-hook 'after-revert-hook 'hif-after-revert-function)
292
293 (defun hif-end-of-line ()
294 (end-of-line)
295 (while (= (logand 1 (skip-chars-backward "\\\\")) 1)
296 (end-of-line 2)))
297
298 (defun hide-ifdef-region-internal (start end)
299 (remove-overlays start end 'hide-ifdef t)
300 (let ((o (make-overlay start end)))
301 (overlay-put o 'hide-ifdef t)
302 (if hide-ifdef-shadow
303 (overlay-put o 'face 'hide-ifdef-shadow)
304 (overlay-put o 'invisible 'hide-ifdef))))
305
306 (defun hide-ifdef-region (start end)
307 "START is the start of a #if or #else form. END is the ending part.
308 Everything including these lines is made invisible."
309 (save-excursion
310 (goto-char start) (hif-end-of-line) (setq start (point))
311 (goto-char end) (hif-end-of-line) (setq end (point))
312 (hide-ifdef-region-internal start end)))
313
314 (defun hif-show-ifdef-region (start end)
315 "Everything between START and END is made visible."
316 (remove-overlays start end 'hide-ifdef t))
317
318
319 ;;===%%SF%% evaluation (Start) ===
320
321 ;; It is not useful to set this to anything but `eval'.
322 ;; In fact, the variable might as well be eliminated.
323 (defvar hide-ifdef-evaluator 'eval
324 "The function to use to evaluate a form.
325 The evaluator is given a canonical form and returns t if text under
326 that form should be displayed.")
327
328 (defvar hif-undefined-symbol nil
329 "...is by default considered to be false.")
330
331
332 (defun hif-set-var (var value)
333 "Prepend (var value) pair to `hide-ifdef-env'."
334 (setq hide-ifdef-env (cons (cons var value) hide-ifdef-env)))
335
336 (declare-function semantic-c-hideif-lookup "semantic/bovine/c" (var))
337 (declare-function semantic-c-hideif-defined "semantic/bovine/c" (var))
338
339 (defun hif-lookup (var)
340 (or (when (bound-and-true-p semantic-c-takeover-hideif)
341 (semantic-c-hideif-lookup var))
342 (let ((val (assoc var hide-ifdef-env)))
343 (if val
344 (cdr val)
345 hif-undefined-symbol))))
346
347 (defun hif-defined (var)
348 (cond
349 ((bound-and-true-p semantic-c-takeover-hideif)
350 (semantic-c-hideif-defined var))
351 ((assoc var hide-ifdef-env) 1)
352 (t 0)))
353
354 ;;===%%SF%% evaluation (End) ===
355
356
357
358 ;;===%%SF%% parsing (Start) ===
359 ;;; The code that understands what ifs and ifdef in files look like.
360
361 (defconst hif-cpp-prefix "\\(^\\|\r\\)[ \t]*#[ \t]*")
362 (defconst hif-ifndef-regexp (concat hif-cpp-prefix "ifndef"))
363 (defconst hif-ifx-regexp (concat hif-cpp-prefix "if\\(n?def\\)?[ \t]+"))
364 (defconst hif-else-regexp (concat hif-cpp-prefix "else"))
365 (defconst hif-endif-regexp (concat hif-cpp-prefix "endif"))
366 (defconst hif-ifx-else-endif-regexp
367 (concat hif-ifx-regexp "\\|" hif-else-regexp "\\|" hif-endif-regexp))
368
369 ;; Used to store the current token and the whole token list during parsing.
370 ;; Only bound dynamically.
371 (defvar hif-token)
372 (defvar hif-token-list)
373
374 (defconst hif-token-alist
375 '(("||" . hif-or)
376 ("&&" . hif-and)
377 ("|" . hif-logior)
378 ("^" . hif-logxor)
379 ("&" . hif-logand)
380 ("<<" . hif-shiftleft)
381 (">>" . hif-shiftright)
382 ("==" . hif-equal)
383 ;; Note: we include tokens like `=' which aren't supported by CPP's
384 ;; expression syntax, because they are still relevant for the tokenizer,
385 ;; especially in conjunction with ##.
386 ("=" . hif-assign)
387 ("!=" . hif-notequal)
388 ("##" . hif-token-concat)
389 ("!" . hif-not)
390 ("~" . hif-lognot)
391 ("(" . hif-lparen)
392 (")" . hif-rparen)
393 (">" . hif-greater)
394 ("<" . hif-less)
395 (">=" . hif-greater-equal)
396 ("<=" . hif-less-equal)
397 ("+" . hif-plus)
398 ("-" . hif-minus)
399 ("*" . hif-multiply)
400 ("/" . hif-divide)
401 ("%" . hif-modulo)
402 ("?" . hif-conditional)
403 (":" . hif-colon)))
404
405 (defconst hif-token-regexp
406 (concat (regexp-opt (mapcar 'car hif-token-alist))
407 "\\|0x[0-9a-fA-F]+\\.?[0-9a-fA-F]*"
408 "\\|[0-9]+\\.?[0-9]*" ;; decimal/octal
409 "\\|\\w+"))
410
411 (defconst hif-string-literal-regexp "\\(\"\\(?:[^\"\\]\\|\\\\.\\)*\"\\)")
412
413
414 (defun hif-tokenize (start end)
415 "Separate string between START and END into a list of tokens."
416 (let ((token-list nil))
417 (with-syntax-table hide-ifdef-syntax-table
418 (save-excursion
419 (goto-char start)
420 (while (progn (forward-comment (point-max)) (< (point) end))
421 ;; (message "expr-start = %d" expr-start) (sit-for 1)
422 (cond
423 ((looking-at "\\\\\n")
424 (forward-char 2))
425
426 ((looking-at hif-string-literal-regexp)
427 (push (substring-no-properties (match-string 1)) token-list)
428 (goto-char (match-end 0)))
429 ((looking-at hif-token-regexp)
430 (let ((token (buffer-substring (point) (match-end 0))))
431 (goto-char (match-end 0))
432 ;; (message "token: %s" token) (sit-for 1)
433 (push
434 (or (cdr (assoc token hif-token-alist))
435 (if (string-equal token "defined") 'hif-defined)
436 ;; TODO:
437 ;; 1. postfix 'l', 'll', 'ul' and 'ull'
438 ;; 2. floating number formats
439 ;; 3. hexadecimal/octal floats
440 ;; 4. 098 is interpreted as octal conversion error
441 ;; FIXME: string-to-number does not convert hex floats
442 (if (string-match "0x\\([0-9a-fA-F]+\\.?[0-9a-fA-F]*\\)"
443 token)
444 (string-to-number (match-string 1 token) 16)) ;; hex
445 ;; FIXME: string-to-number does not convert octal floats
446 (if (string-match "\\`0[0-9]+\\(\\.[0-9]+\\)?\\'" token)
447 (string-to-number token 8)) ;; octal
448 (if (string-match "\\`[1-9][0-9]*\\(\\.[0-9]+\\)?\\'"
449 token)
450 (string-to-number token)) ;; decimal
451 (intern token))
452 token-list)))
453 (t (error "Bad #if expression: %s" (buffer-string)))))))
454 (nreverse token-list)))
455
456 ;;------------------------------------------------------------------------
457 ;; Translate C preprocessor #if expressions using recursive descent.
458 ;; This parser was limited to the operators &&, ||, !, and "defined".
459 ;; Added ==, !=, +, and -. Gary Oberbrunner, garyo@avs.com, 8/9/94
460 ;;
461 ;; Implement the C language operator precedence table. Add all those
462 ;; missing operators that could be used in macros. Luke Lee 2013-09-04
463
464 ;; | Operator Type | Operator | Associativity |
465 ;; +----------------------+-----------------------------+---------------+
466 ;; | Primary Expression | () [] . -> expr++ expr-- | left-to-right |
467 ;; | Unary Operators | * & + - ! ~ ++expr --expr | right-to-left |
468 ;; | | (typecast) sizeof | |
469 ;; | Binary Operators | * / % | left-to-right |
470 ;; | | + - | |
471 ;; | | >> << | |
472 ;; | | < > <= >= | |
473 ;; | | == != | |
474 ;; | | & | |
475 ;; | | ^ | |
476 ;; | | | | |
477 ;; | | && | |
478 ;; | | || | |
479 ;; | Ternary Operator | ?: | right-to-left |
480 ;; x| Assignment Operators | = += -= *= /= %= >>= <<= &= | right-to-left |
481 ;; | | ^= = | |
482 ;; | Comma | , | left-to-right |
483
484 (defsubst hif-nexttoken ()
485 "Pop the next token from token-list into the let variable `hif-token'."
486 (setq hif-token (pop hif-token-list)))
487
488 (defun hif-parse-if-exp (token-list)
489 "Parse the TOKEN-LIST. Return translated list in prefix form."
490 (let ((hif-token-list token-list))
491 (hif-nexttoken)
492 (prog1
493 (and hif-token
494 (hif-exprlist))
495 (if hif-token ; is there still a token?
496 (error "Error: unexpected token: %s" hif-token)))))
497
498 (defun hif-exprlist ()
499 "Parse an exprlist: expr { ',' expr}"
500 (let ((result (hif-expr)))
501 (if (eq hif-token 'hif-comma)
502 (let ((temp (list result)))
503 (while
504 (progn
505 (hif-nexttoken)
506 (push (hif-expr) temp)
507 (eq hif-token 'hif-comma)))
508 (cons 'hif-comma (nreverse temp)))
509 result)))
510
511 (defun hif-expr ()
512 "Parse an expression as found in #if.
513 expr : or-expr | or-expr '?' expr ':' expr."
514 (let ((result (hif-or-expr))
515 middle)
516 (while (eq hif-token 'hif-conditional)
517 (hif-nexttoken)
518 (setq middle (hif-expr))
519 (if (eq hif-token 'hif-colon)
520 (progn
521 (hif-nexttoken)
522 (setq result (list 'hif-conditional result middle (hif-expr))))
523 (error "Error: unexpected token: %s" hif-token)))
524 result))
525
526 (defun hif-or-expr ()
527 "Parse an or-expr : and-expr | or-expr '||' and-expr."
528 (let ((result (hif-and-expr)))
529 (while (eq hif-token 'hif-or)
530 (hif-nexttoken)
531 (setq result (list 'hif-or result (hif-and-expr))))
532 result))
533
534 (defun hif-and-expr ()
535 "Parse an and-expr : logior-expr | and-expr '&&' logior-expr."
536 (let ((result (hif-logior-expr)))
537 (while (eq hif-token 'hif-and)
538 (hif-nexttoken)
539 (setq result (list 'hif-and result (hif-logior-expr))))
540 result))
541
542 (defun hif-logior-expr ()
543 "Parse a logor-expr : logxor-expr | logor-expr '|' logxor-expr."
544 (let ((result (hif-logxor-expr)))
545 (while (eq hif-token 'hif-logior)
546 (hif-nexttoken)
547 (setq result (list 'hif-logior result (hif-logxor-expr))))
548 result))
549
550 (defun hif-logxor-expr ()
551 "Parse a logxor-expr : logand-expr | logxor-expr '^' logand-expr."
552 (let ((result (hif-logand-expr)))
553 (while (eq hif-token 'hif-logxor)
554 (hif-nexttoken)
555 (setq result (list 'hif-logxor result (hif-logand-expr))))
556 result))
557
558 (defun hif-logand-expr ()
559 "Parse a logand-expr : eq-expr | logand-expr '&' eq-expr."
560 (let ((result (hif-eq-expr)))
561 (while (eq hif-token 'hif-logand)
562 (hif-nexttoken)
563 (setq result (list 'hif-logand result (hif-eq-expr))))
564 result))
565
566 (defun hif-eq-expr ()
567 "Parse an eq-expr : comp | eq-expr `=='|`!=' comp."
568 (let ((result (hif-comp-expr))
569 (eq-token nil))
570 (while (memq hif-token '(hif-equal hif-notequal))
571 (setq eq-token hif-token)
572 (hif-nexttoken)
573 (setq result (list eq-token result (hif-comp-expr))))
574 result))
575
576 (defun hif-comp-expr ()
577 "Parse a comp-expr : logshift | comp-expr `<'|`>'|`>='|`<=' logshift."
578 (let ((result (hif-logshift-expr))
579 (comp-token nil))
580 (while (memq hif-token '(hif-greater hif-less hif-greater-equal hif-less-equal))
581 (setq comp-token hif-token)
582 (hif-nexttoken)
583 (setq result (list comp-token result (hif-logshift-expr))))
584 result))
585
586 (defun hif-logshift-expr ()
587 "Parse a logshift : math | logshift `<<'|`>>' math."
588 (let ((result (hif-math))
589 (shift-token nil))
590 (while (memq hif-token '(hif-shiftleft hif-shiftright))
591 (setq shift-token hif-token)
592 (hif-nexttoken)
593 (setq result (list shift-token result (hif-math))))
594 result))
595
596 (defun hif-math ()
597 "Parse an expression with + or -.
598 math : muldiv | math '+|-' muldiv."
599 (let ((result (hif-muldiv-expr))
600 (math-op nil))
601 (while (memq hif-token '(hif-plus hif-minus))
602 (setq math-op hif-token)
603 (hif-nexttoken)
604 (setq result (list math-op result (hif-muldiv-expr))))
605 result))
606
607 (defun hif-muldiv-expr ()
608 "Parse an expression with *,/,%.
609 muldiv : factor | muldiv '*|/|%' factor."
610 (let ((result (hif-factor))
611 (math-op nil))
612 (while (memq hif-token '(hif-multiply hif-divide hif-modulo))
613 (setq math-op hif-token)
614 (hif-nexttoken)
615 (setq result (list math-op result (hif-factor))))
616 result))
617
618 (defun hif-factor ()
619 "Parse a factor: '!' factor | '~' factor | '(' expr ')' | 'defined(' id ')' | 'id(parmlist)' | strings | id."
620 (cond
621 ((eq hif-token 'hif-not)
622 (hif-nexttoken)
623 (list 'hif-not (hif-factor)))
624
625 ((eq hif-token 'hif-lognot)
626 (hif-nexttoken)
627 (list 'hif-lognot (hif-factor)))
628
629 ((eq hif-token 'hif-lparen)
630 (hif-nexttoken)
631 (let ((result (hif-exprlist)))
632 (if (not (eq hif-token 'hif-rparen))
633 (error "Bad token in parenthesized expression: %s" hif-token)
634 (hif-nexttoken)
635 result)))
636
637 ((eq hif-token 'hif-defined)
638 (hif-nexttoken)
639 (let ((paren (when (eq hif-token 'hif-lparen) (hif-nexttoken) t))
640 (ident hif-token))
641 (if (memq hif-token '(or and not hif-defined hif-lparen hif-rparen))
642 (error "Error: unexpected token: %s" hif-token))
643 (when paren
644 (hif-nexttoken)
645 (unless (eq hif-token 'hif-rparen)
646 (error "Error: expected \")\" after identifier")))
647 (hif-nexttoken)
648 `(hif-defined (quote ,ident))))
649
650 ((numberp hif-token)
651 (prog1 hif-token (hif-nexttoken)))
652
653 ;; Unary plus/minus.
654 ((memq hif-token '(hif-minus hif-plus))
655 (list (prog1 hif-token (hif-nexttoken)) 0 (hif-factor)))
656
657 (t ; identifier
658 (let ((ident hif-token))
659 (if (memq ident '(or and))
660 (error "Error: missing identifier"))
661 (hif-nexttoken)
662 `(hif-lookup (quote ,ident))))))
663
664 (defun hif-mathify (val)
665 "Treat VAL as a number: if it's t or nil, use 1 or 0."
666 (cond ((eq val t) 1)
667 ((null val) 0)
668 (t val)))
669
670 (defun hif-conditional (a b c)
671 (if (not (zerop (hif-mathify a))) (hif-mathify b) (hif-mathify c)))
672 (defun hif-and (a b)
673 (and (not (zerop (hif-mathify a))) (not (zerop (hif-mathify b)))))
674 (defun hif-or (a b)
675 (or (not (zerop (hif-mathify a))) (not (zerop (hif-mathify b)))))
676 (defun hif-not (a)
677 (zerop (hif-mathify a)))
678 (defun hif-lognot (a)
679 (lognot (hif-mathify a)))
680
681 (defmacro hif-mathify-binop (fun)
682 `(lambda (a b)
683 ,(format "Like `%s' but treat t and nil as 1 and 0." fun)
684 (,fun (hif-mathify a) (hif-mathify b))))
685
686 (defun hif-shiftleft (a b)
687 (setq a (hif-mathify a))
688 (setq b (hif-mathify b))
689 (if (< a 0)
690 (ash a b)
691 (lsh a b)))
692
693 (defun hif-shiftright (a b)
694 (setq a (hif-mathify a))
695 (setq b (hif-mathify b))
696 (if (< a 0)
697 (ash a (- b))
698 (lsh a (- b))))
699
700
701 (defalias 'hif-multiply (hif-mathify-binop *))
702 (defalias 'hif-divide (hif-mathify-binop /))
703 (defalias 'hif-modulo (hif-mathify-binop %))
704 (defalias 'hif-plus (hif-mathify-binop +))
705 (defalias 'hif-minus (hif-mathify-binop -))
706 (defalias 'hif-equal (hif-mathify-binop =))
707 (defalias 'hif-notequal (hif-mathify-binop /=))
708 (defalias 'hif-greater (hif-mathify-binop >))
709 (defalias 'hif-less (hif-mathify-binop <))
710 (defalias 'hif-greater-equal (hif-mathify-binop >=))
711 (defalias 'hif-less-equal (hif-mathify-binop <=))
712 (defalias 'hif-logior (hif-mathify-binop logior))
713 (defalias 'hif-logxor (hif-mathify-binop logxor))
714 (defalias 'hif-logand (hif-mathify-binop logand))
715
716
717 (defun hif-comma (&rest expr)
718 "Evaluate a list of expr, return the result of the last item."
719 (let ((result nil))
720 (dolist (e expr)
721 (ignore-errors
722 (setq result (funcall hide-ifdef-evaluator e))))
723 result))
724
725
726 ;;;----------- end of parser -----------------------
727
728
729 (defun hif-canonicalize ()
730 "When at beginning of #ifX, return a Lisp expression for its condition."
731 (save-excursion
732 (let ((negate (looking-at hif-ifndef-regexp)))
733 (re-search-forward hif-ifx-regexp)
734 (let* ((tokens (hif-tokenize (point)
735 (progn (hif-end-of-line) (point))))
736 (expr (hif-parse-if-exp tokens)))
737 ;; (message "hif-canonicalized: %s" expr)
738 (if negate
739 (list 'hif-not expr)
740 expr)))))
741
742
743 (defun hif-find-any-ifX ()
744 "Move to next #if..., or #ifndef, at point or after."
745 ;; (message "find ifX at %d" (point))
746 (prog1
747 (re-search-forward hif-ifx-regexp (point-max) t)
748 (beginning-of-line)))
749
750
751 (defun hif-find-next-relevant ()
752 "Move to next #if..., #else, or #endif, after the current line."
753 ;; (message "hif-find-next-relevant at %d" (point))
754 (end-of-line)
755 ;; avoid infinite recursion by only going to beginning of line if match found
756 (if (re-search-forward hif-ifx-else-endif-regexp (point-max) t)
757 (beginning-of-line)))
758
759 (defun hif-find-previous-relevant ()
760 "Move to previous #if..., #else, or #endif, before the current line."
761 ;; (message "hif-find-previous-relevant at %d" (point))
762 (beginning-of-line)
763 ;; avoid infinite recursion by only going to beginning of line if match found
764 (if (re-search-backward hif-ifx-else-endif-regexp (point-min) t)
765 (beginning-of-line)))
766
767
768 (defun hif-looking-at-ifX () ;; Should eventually see #if
769 (looking-at hif-ifx-regexp))
770 (defun hif-looking-at-endif ()
771 (looking-at hif-endif-regexp))
772 (defun hif-looking-at-else ()
773 (looking-at hif-else-regexp))
774
775
776
777 (defun hif-ifdef-to-endif ()
778 "If positioned at #ifX or #else form, skip to corresponding #endif."
779 ;; (message "hif-ifdef-to-endif at %d" (point)) (sit-for 1)
780 (hif-find-next-relevant)
781 (cond ((hif-looking-at-ifX)
782 (hif-ifdef-to-endif) ; find endif of nested if
783 (hif-ifdef-to-endif)) ; find outer endif or else
784 ((hif-looking-at-else)
785 (hif-ifdef-to-endif)) ; find endif following else
786 ((hif-looking-at-endif)
787 'done)
788 (t
789 (error "Mismatched #ifdef #endif pair"))))
790
791
792 (defun hif-endif-to-ifdef ()
793 "If positioned at #endif form, skip backward to corresponding #ifX."
794 ;; (message "hif-endif-to-ifdef at %d" (point))
795 (let ((start (point)))
796 (hif-find-previous-relevant)
797 (if (= start (point))
798 (error "Mismatched #ifdef #endif pair")))
799 (cond ((hif-looking-at-endif)
800 (hif-endif-to-ifdef) ; find beginning of nested if
801 (hif-endif-to-ifdef)) ; find beginning of outer if or else
802 ((hif-looking-at-else)
803 (hif-endif-to-ifdef))
804 ((hif-looking-at-ifX)
805 'done)
806 (t))) ; never gets here
807
808
809 (defun forward-ifdef (&optional arg)
810 "Move point to beginning of line of the next ifdef-endif.
811 With argument, do this that many times."
812 (interactive "p")
813 (or arg (setq arg 1))
814 (if (< arg 0) (backward-ifdef (- arg))
815 (while (< 0 arg)
816 (setq arg (- arg))
817 (let ((start (point)))
818 (unless (hif-looking-at-ifX)
819 (hif-find-next-relevant))
820 (if (hif-looking-at-ifX)
821 (hif-ifdef-to-endif)
822 (goto-char start)
823 (error "No following #ifdef"))))))
824
825
826 (defun backward-ifdef (&optional arg)
827 "Move point to beginning of the previous ifdef-endif.
828 With argument, do this that many times."
829 (interactive "p")
830 (or arg (setq arg 1))
831 (if (< arg 0) (forward-ifdef (- arg))
832 (while (< 0 arg)
833 (setq arg (1- arg))
834 (beginning-of-line)
835 (let ((start (point)))
836 (unless (hif-looking-at-endif)
837 (hif-find-previous-relevant))
838 (if (hif-looking-at-endif)
839 (hif-endif-to-ifdef)
840 (goto-char start)
841 (error "No previous #ifdef"))))))
842
843
844 (defun down-ifdef ()
845 "Move point to beginning of nested ifdef or else-part."
846 (interactive)
847 (let ((start (point)))
848 (hif-find-next-relevant)
849 (if (or (hif-looking-at-ifX) (hif-looking-at-else))
850 ()
851 (goto-char start)
852 (error "No following #ifdef"))))
853
854
855 (defun up-ifdef ()
856 "Move point to beginning of enclosing ifdef or else-part."
857 (interactive)
858 (beginning-of-line)
859 (let ((start (point)))
860 (unless (hif-looking-at-endif)
861 (hif-find-previous-relevant))
862 (if (hif-looking-at-endif)
863 (hif-endif-to-ifdef))
864 (if (= start (point))
865 (error "No previous #ifdef"))))
866
867 (defun next-ifdef (&optional arg)
868 "Move to the beginning of the next #ifX, #else, or #endif.
869 With argument, do this that many times."
870 (interactive "p")
871 (or arg (setq arg 1))
872 (if (< arg 0) (previous-ifdef (- arg))
873 (while (< 0 arg)
874 (setq arg (1- arg))
875 (hif-find-next-relevant)
876 (when (eolp)
877 (beginning-of-line)
878 (error "No following #ifdefs, #elses, or #endifs")))))
879
880 (defun previous-ifdef (&optional arg)
881 "Move to the beginning of the previous #ifX, #else, or #endif.
882 With argument, do this that many times."
883 (interactive "p")
884 (or arg (setq arg 1))
885 (if (< arg 0) (next-ifdef (- arg))
886 (while (< 0 arg)
887 (setq arg (1- arg))
888 (let ((start (point)))
889 (hif-find-previous-relevant)
890 (if (= start (point))
891 (error "No previous #ifdefs, #elses, or #endifs"))))))
892
893
894 ;;===%%SF%% parsing (End) ===
895
896
897 ;;===%%SF%% hide-ifdef-hiding (Start) ===
898
899
900 ;;; A range is a structure with four components:
901 ;;; ELSE-P True if there was an else clause for the ifdef.
902 ;;; START The start of the range. (beginning of line)
903 ;;; ELSE The else marker (beginning of line)
904 ;;; Only valid if ELSE-P is true.
905 ;;; END The end of the range. (beginning of line)
906
907 (defsubst hif-make-range (start end &optional else)
908 (list start else end))
909
910 (defsubst hif-range-start (range) (elt range 0))
911 (defsubst hif-range-else (range) (elt range 1))
912 (defsubst hif-range-end (range) (elt range 2))
913
914
915
916 ;;; Find-Range
917 ;;; The workhorse, it delimits the #if region. Reasonably simple:
918 ;;; Skip until an #else or #endif is found, remembering positions. If
919 ;;; an #else was found, skip some more, looking for the true #endif.
920
921 (defun hif-find-range ()
922 "Return a Range structure describing the current #if region.
923 Point is left unchanged."
924 ;; (message "hif-find-range at %d" (point))
925 (save-excursion
926 (beginning-of-line)
927 (let ((start (point))
928 (else nil)
929 (end nil))
930 ;; Part one. Look for either #endif or #else.
931 ;; This loop-and-a-half dedicated to E. Dijkstra.
932 (while (progn
933 (hif-find-next-relevant)
934 (hif-looking-at-ifX)) ; Skip nested ifdef
935 (hif-ifdef-to-endif))
936 ;; Found either a #else or an #endif.
937 (cond ((hif-looking-at-else)
938 (setq else (point)))
939 (t
940 (setq end (point)))) ; (line-end-position)
941 ;; If found #else, look for #endif.
942 (when else
943 (while (progn
944 (hif-find-next-relevant)
945 (hif-looking-at-ifX)) ; Skip nested ifdef
946 (hif-ifdef-to-endif))
947 (if (hif-looking-at-else)
948 (error "Found two elses in a row? Broken!"))
949 (setq end (point))) ; (line-end-position)
950 (hif-make-range start end else))))
951
952
953 ;;; A bit slimy.
954
955 (defun hif-hide-line (point)
956 "Hide the line containing point. Does nothing if `hide-ifdef-lines' is nil."
957 (when hide-ifdef-lines
958 (save-excursion
959 (goto-char point)
960 (hide-ifdef-region-internal
961 (line-beginning-position) (progn (hif-end-of-line) (point))))))
962
963
964 ;;; Hif-Possibly-Hide
965 ;;; There are four cases. The #ifX expression is "taken" if it
966 ;;; the hide-ifdef-evaluator returns T. Presumably, this means the code
967 ;;; inside the #ifdef would be included when the program was
968 ;;; compiled.
969 ;;;
970 ;;; Case 1: #ifX taken, and there's an #else.
971 ;;; The #else part must be hidden. The #if (then) part must be
972 ;;; processed for nested #ifX's.
973 ;;; Case 2: #ifX taken, and there's no #else.
974 ;;; The #if part must be processed for nested #ifX's.
975 ;;; Case 3: #ifX not taken, and there's an #else.
976 ;;; The #if part must be hidden. The #else part must be processed
977 ;;; for nested #ifs.
978 ;;; Case 4: #ifX not taken, and there's no #else.
979 ;;; The #ifX part must be hidden.
980 ;;;
981 ;;; Further processing is done by narrowing to the relevant region
982 ;;; and just recursively calling hide-ifdef-guts.
983 ;;;
984 ;;; When hif-possibly-hide returns, point is at the end of the
985 ;;; possibly-hidden range.
986
987 (defun hif-recurse-on (start end)
988 "Call `hide-ifdef-guts' after narrowing to end of START line and END line."
989 (save-excursion
990 (save-restriction
991 (goto-char start)
992 (end-of-line)
993 (narrow-to-region (point) end)
994 (hide-ifdef-guts))))
995
996 (defun hif-possibly-hide ()
997 "Called at #ifX expression, this hides those parts that should be hidden.
998 It uses the judgment of `hide-ifdef-evaluator'."
999 ;; (message "hif-possibly-hide") (sit-for 1)
1000 (let ((test (hif-canonicalize))
1001 (range (hif-find-range)))
1002 ;; (message "test = %s" test) (sit-for 1)
1003
1004 (hif-hide-line (hif-range-end range))
1005 (if (not (hif-not (funcall hide-ifdef-evaluator test)))
1006 (cond ((hif-range-else range) ; case 1
1007 (hif-hide-line (hif-range-else range))
1008 (hide-ifdef-region (hif-range-else range)
1009 (1- (hif-range-end range)))
1010 (hif-recurse-on (hif-range-start range)
1011 (hif-range-else range)))
1012 (t ; case 2
1013 (hif-recurse-on (hif-range-start range)
1014 (hif-range-end range))))
1015 (cond ((hif-range-else range) ; case 3
1016 (hif-hide-line (hif-range-else range))
1017 (hide-ifdef-region (hif-range-start range)
1018 (1- (hif-range-else range)))
1019 (hif-recurse-on (hif-range-else range)
1020 (hif-range-end range)))
1021 (t ; case 4
1022 (hide-ifdef-region (point)
1023 (1- (hif-range-end range))))))
1024 (hif-hide-line (hif-range-start range)) ; Always hide start.
1025 (goto-char (hif-range-end range))
1026 (end-of-line)))
1027
1028
1029
1030 (defun hide-ifdef-guts ()
1031 "Does most of the work of `hide-ifdefs'.
1032 It does not do the work that's pointless to redo on a recursive entry."
1033 ;; (message "hide-ifdef-guts")
1034 (save-excursion
1035 (goto-char (point-min))
1036 (while (hif-find-any-ifX)
1037 (hif-possibly-hide))))
1038
1039 ;;===%%SF%% hide-ifdef-hiding (End) ===
1040
1041
1042 ;;===%%SF%% exports (Start) ===
1043
1044 (defun hide-ifdef-toggle-read-only ()
1045 "Toggle `hide-ifdef-read-only'."
1046 (interactive)
1047 (setq hide-ifdef-read-only (not hide-ifdef-read-only))
1048 (message "Hide-Read-Only %s"
1049 (if hide-ifdef-read-only "ON" "OFF"))
1050 (if hide-ifdef-hiding
1051 (setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only)))
1052 (force-mode-line-update))
1053
1054 (defun hide-ifdef-toggle-outside-read-only ()
1055 "Replacement for `read-only-mode' within Hide-Ifdef mode."
1056 (interactive)
1057 (setq hif-outside-read-only (not hif-outside-read-only))
1058 (message "Read only %s"
1059 (if hif-outside-read-only "ON" "OFF"))
1060 (setq buffer-read-only
1061 (or (and hide-ifdef-hiding hide-ifdef-read-only)
1062 hif-outside-read-only))
1063 (force-mode-line-update))
1064
1065 (defun hide-ifdef-toggle-shadowing ()
1066 "Toggle shadowing."
1067 (interactive)
1068 (set (make-local-variable 'hide-ifdef-shadow) (not hide-ifdef-shadow))
1069 (message "Shadowing %s" (if hide-ifdef-shadow "ON" "OFF"))
1070 (save-restriction
1071 (widen)
1072 (dolist (overlay (overlays-in (point-min) (point-max)))
1073 (when (overlay-get overlay 'hide-ifdef)
1074 (if hide-ifdef-shadow
1075 (progn
1076 (overlay-put overlay 'invisible nil)
1077 (overlay-put overlay 'face 'hide-ifdef-shadow))
1078 (overlay-put overlay 'face nil)
1079 (overlay-put overlay 'invisible 'hide-ifdef))))))
1080
1081 (defun hide-ifdef-define (var)
1082 "Define a VAR so that #ifdef VAR would be included."
1083 (interactive "SDefine what? ")
1084 (hif-set-var var 1)
1085 (if hide-ifdef-hiding (hide-ifdefs)))
1086
1087 (defun hide-ifdef-undef (var)
1088 "Undefine a VAR so that #ifdef VAR would not be included."
1089 (interactive "SUndefine what? ")
1090 (hif-set-var var nil)
1091 (if hide-ifdef-hiding (hide-ifdefs)))
1092
1093
1094 (defun hide-ifdefs (&optional nomsg)
1095 "Hide the contents of some #ifdefs.
1096 Assume that defined symbols have been added to `hide-ifdef-env'.
1097 The text hidden is the text that would not be included by the C
1098 preprocessor if it were given the file with those symbols defined.
1099
1100 Turn off hiding by calling `show-ifdefs'."
1101
1102 (interactive)
1103 (message "Hiding...")
1104 (setq hif-outside-read-only buffer-read-only)
1105 (unless hide-ifdef-mode (hide-ifdef-mode 1)) ; turn on hide-ifdef-mode
1106 (if hide-ifdef-hiding
1107 (show-ifdefs)) ; Otherwise, deep confusion.
1108 (setq hide-ifdef-hiding t)
1109 (hide-ifdef-guts)
1110 (setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only))
1111 (or nomsg
1112 (message "Hiding done")))
1113
1114
1115 (defun show-ifdefs ()
1116 "Cancel the effects of `hide-ifdef': show the contents of all #ifdefs."
1117 (interactive)
1118 (setq buffer-read-only hif-outside-read-only)
1119 (hif-show-all)
1120 (setq hide-ifdef-hiding nil))
1121
1122
1123 (defun hif-find-ifdef-block ()
1124 "Utility to hide and show ifdef block.
1125 Return as (TOP . BOTTOM) the extent of ifdef block."
1126 (let (max-bottom)
1127 (cons (save-excursion
1128 (beginning-of-line)
1129 (unless (or (hif-looking-at-else) (hif-looking-at-ifX))
1130 (up-ifdef))
1131 (prog1 (point)
1132 (hif-ifdef-to-endif)
1133 (setq max-bottom (1- (point)))))
1134 (save-excursion
1135 (beginning-of-line)
1136 (unless (hif-looking-at-endif)
1137 (hif-find-next-relevant))
1138 (while (hif-looking-at-ifX)
1139 (hif-ifdef-to-endif)
1140 (hif-find-next-relevant))
1141 (min max-bottom (1- (point)))))))
1142
1143
1144 (defun hide-ifdef-block ()
1145 "Hide the ifdef block (true or false part) enclosing or before the cursor."
1146 (interactive)
1147 (unless hide-ifdef-mode (hide-ifdef-mode 1))
1148 (let ((top-bottom (hif-find-ifdef-block)))
1149 (hide-ifdef-region (car top-bottom) (cdr top-bottom))
1150 (when hide-ifdef-lines
1151 (hif-hide-line (car top-bottom))
1152 (hif-hide-line (1+ (cdr top-bottom))))
1153 (setq hide-ifdef-hiding t))
1154 (setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only)))
1155
1156 (defun show-ifdef-block ()
1157 "Show the ifdef block (true or false part) enclosing or before the cursor."
1158 (interactive)
1159 (let ((top-bottom (hif-find-ifdef-block)))
1160 (if hide-ifdef-lines
1161 (hif-show-ifdef-region
1162 (save-excursion
1163 (goto-char (car top-bottom)) (line-beginning-position))
1164 (save-excursion
1165 (goto-char (1+ (cdr top-bottom)))
1166 (hif-end-of-line) (point)))
1167 (hif-show-ifdef-region (1- (car top-bottom)) (cdr top-bottom)))))
1168
1169
1170 ;;; definition alist support
1171
1172 (defvar hide-ifdef-define-alist nil
1173 "A global assoc list of pre-defined symbol lists.")
1174
1175 (defun hif-compress-define-list (env)
1176 "Compress the define list ENV into a list of defined symbols only."
1177 (let ((new-defs nil))
1178 (dolist (def env new-defs)
1179 (if (hif-lookup (car def)) (push (car def) new-defs)))))
1180
1181 (defun hide-ifdef-set-define-alist (name)
1182 "Set the association for NAME to `hide-ifdef-env'."
1183 (interactive "SSet define list: ")
1184 (push (cons name (hif-compress-define-list hide-ifdef-env))
1185 hide-ifdef-define-alist))
1186
1187 (defun hide-ifdef-use-define-alist (name)
1188 "Set `hide-ifdef-env' to the define list specified by NAME."
1189 (interactive
1190 (list (completing-read "Use define list: "
1191 (mapcar (lambda (x) (symbol-name (car x)))
1192 hide-ifdef-define-alist)
1193 nil t)))
1194 (if (stringp name) (setq name (intern name)))
1195 (let ((define-list (assoc name hide-ifdef-define-alist)))
1196 (if define-list
1197 (setq hide-ifdef-env
1198 (mapcar (lambda (arg) (cons arg t))
1199 (cdr define-list)))
1200 (error "No define list for %s" name))
1201 (if hide-ifdef-hiding (hide-ifdefs))))
1202
1203 (provide 'hideif)
1204
1205 ;;; hideif.el ends here