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