1 ;;; hide-ifdef-mode.el --- hides selected code within ifdef.
3 ;; Author: Dan LaLiberte <liberte@a.cs.uiuc.edu>
4 ;; Last-Modified: 06 Mar 1991
7 ;;; $Header: hide-ifdef-mode.el,v 1.7 88/02/16 03:12:58 liberte Exp $
9 ;;; Copyright (C) 1988 Brian Marick and Daniel LaLiberte
13 ;;; Written by Brian Marick, at Gould, Computer Systems Division, Urbana IL.
14 ;;; Extensively modified by Daniel LaLiberte (while at Gould).
16 ;;; You may freely modify and distribute this, but keep a record
17 ;;; of modifications and send comments to:
18 ;;; liberte@a.cs.uiuc.edu or ihnp4!uiucdcs!liberte
19 ;;; I will continue to upgrade hide-ifdef-mode
20 ;;; with your contributions.
22 ;;; To initialize, toggle the hide-ifdef minor mode with
24 ;;; M-x hide-ifdef-mode
26 ;;; This will set up key bindings and call hide-ifdef-mode-hook if it
27 ;;; has a value. To explicitly hide ifdefs using a buffer-local
28 ;;; define list (default empty), type
30 ;;; M-x hide-ifdefs or C-c h
32 ;;; Hide-ifdef suppresses the display of code that the preprocessor wouldn't
33 ;;; pass through. The support of constant expressions in #if lines is
34 ;;; limited to identifiers, parens, and the operators: &&, ||, !, and
35 ;;; "defined". Please extend this.
37 ;;; The hidden code is marked by ellipses (...). Be
38 ;;; cautious when editing near ellipses, since the hidden text is
39 ;;; still in the buffer, and you can move the point into it and modify
40 ;;; text unawares. If you don't want to see the ellipses, set
41 ;;; selective-display-ellipses to nil. But this can be dangerous.
42 ;;; You can make your buffer read-only while hide-ifdef-hiding by setting
43 ;;; hide-ifdef-read-only to a non-nil value. You can toggle this
44 ;;; variable with hide-ifdef-toggle-read-only (C-c C-q).
46 ;;; You can undo the effect of hide-ifdefs by typing
48 ;;; M-x show-ifdefs or C-c s
50 ;;; Use M-x hide-ifdef-define (C-c d) to define a symbol.
51 ;;; Use M-x hide-ifdef-undef (C-c u) to undefine a symbol.
53 ;;; If you define or undefine a symbol while hide-ifdef-mode is in effect,
54 ;;; the display will be updated. Only the define list for the current
55 ;;; buffer will be affected. You can save changes to the local define
56 ;;; list with hide-ifdef-set-define-alist. This adds entries
57 ;;; to hide-ifdef-define-alist.
59 ;;; If you have defined a hide-ifdef-mode-hook, you can set
60 ;;; up a list of symbols that may be used by hide-ifdefs as in the
61 ;;; following example:
63 ;;; (setq hide-ifdef-mode-hook
65 ;;; (if (not hide-ifdef-define-alist)
66 ;;; (setq hide-ifdef-define-alist
70 ;;; (hide-ifdef-use-define-alist 'list2) ; use list2 by default
73 ;;; You can call hide-ifdef-use-define-alist (C-c u) at any time to specify
74 ;;; another list to use.
76 ;;; To cause ifdefs to be hidden as soon as hide-ifdef-mode is called,
77 ;;; set hide-ifdef-initially to non-nil.
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
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.
89 ;;; You can move the point between ifdefs with forward-ifdef, backward-ifdef,
90 ;;; up-ifdef, down-ifdef, next-ifdef, and previous-ifdef.
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).
98 ;;; $Log: hide-ifdef-mode.el,v $
99 ;;; Revision 1.7 88/02/16 03:12:58 liberte
100 ;;; Fixed comments and doc strings.
101 ;;; Added optional prefix arg for ifdef motion commands.
103 ;;; Revision 1.6 88/02/05 00:36:18 liberte
105 ;;; 1. A multi-line comment that starts on an #ifdef line
106 ;;; now ends on that line.
107 ;;; 2. Fix bad function name: hide-hif-ifdef-toggle-read-only
108 ;;; 3. Make ifdef-block hiding work outside of ifdefs.
110 ;;; Revision 1.5 88/01/31 23:19:31 liberte
112 ;;; Prefix internal names with "hif-".
114 ;;; Revision 1.4 88/01/30 14:09:38 liberte
115 ;;; Add hide-ifdef-hiding and hide-ifdef-mode to minor-mode-alist.
117 ;;; Revision 1.3 88/01/29 00:38:19 liberte
119 ;;; 1. Function "defined" is just like lookup.
120 ;;; 2. Skip to newline or cr in case text is hidden.
121 ;;; 3. Use car of token list if just one symbol.
123 ;;; Revision 1.2 88/01/28 23:32:46 liberte
124 ;;; Use hide-ifdef-mode-prefix-key.
125 ;;; Copy current-local-map so other buffers do not get
126 ;;; hide-ifdef-mode bindings.
131 (defvar hide-ifdef-mode-map nil
132 "Keymap used with Hide-Ifdef mode")
134 (defconst hide-ifdef-mode-prefix-key "\C-c"
135 "Prefix key for all Hide-Ifdef mode commands.")
137 (defvar hide-ifdef-mode-map-before nil
138 "Buffer-local variable to store a copy of the local keymap
139 before `hide-ifdef-mode' modifies it.")
141 (defun define-hide-ifdef-mode-map ()
142 (if hide-ifdef-mode-map
143 () ; dont redefine it.
144 (setq hide-ifdef-mode-map (make-sparse-keymap))
145 (define-key hide-ifdef-mode-map "d" 'hide-ifdef-define)
146 (define-key hide-ifdef-mode-map "u" 'hide-ifdef-undef)
147 (define-key hide-ifdef-mode-map "D" 'hide-ifdef-set-define-alist)
148 (define-key hide-ifdef-mode-map "U" 'hide-ifdef-use-define-alist)
150 (define-key hide-ifdef-mode-map "h" 'hide-ifdefs)
151 (define-key hide-ifdef-mode-map "s" 'show-ifdefs)
152 (define-key hide-ifdef-mode-map "\C-h" 'hide-ifdef-block)
153 (define-key hide-ifdef-mode-map "\C-s" 'show-ifdef-block)
155 (define-key hide-ifdef-mode-map "\C-f" 'forward-ifdef)
156 (define-key hide-ifdef-mode-map "\C-b" 'backward-ifdef)
157 (define-key hide-ifdef-mode-map "\C-d" 'down-ifdef)
158 (define-key hide-ifdef-mode-map "\C-u" 'up-ifdef)
159 (define-key hide-ifdef-mode-map "\C-n" 'next-ifdef)
160 (define-key hide-ifdef-mode-map "\C-p" 'previous-ifdef)
161 (define-key hide-ifdef-mode-map "\C-q" 'hide-ifdef-toggle-read-only)
162 (define-key hide-ifdef-mode-map
163 (where-is-internal 'toggle-read-only nil nil t)
164 'hide-ifdef-toggle-outside-read-only)
166 (fset 'hide-ifdef-mode-map hide-ifdef-mode-map) ; the function is the map
169 (defun hif-update-mode-line ()
170 "Update mode-line by setting buffer-modified to itself."
171 (set-buffer-modified-p (buffer-modified-p)))
173 (defvar hide-ifdef-mode nil
174 "non-nil when hide-ifdef-mode is activated.")
176 (defvar hide-ifdef-hiding nil
177 "non-nil when text may be hidden.")
179 (or (assq 'hide-ifdef-hiding minor-mode-alist)
180 (setq minor-mode-alist
181 (cons '(hide-ifdef-hiding " Hiding")
184 (or (assq 'hide-ifdef-mode minor-mode-alist)
185 (setq minor-mode-alist
186 (cons '(hide-ifdef-mode " Ifdef")
189 (defun hide-ifdef-mode (arg)
190 "Toggle Hide-Ifdef mode. This is a minor mode, albeit a large one.
191 With ARG, turn Hide-Ifdef mode on iff arg is positive.
192 In Hide-Ifdef mode, code within #ifdef constructs that the C preprocessor
193 would eliminate may be hidden from view. Several variables affect
194 how the hiding is done:
197 An association list of defined and undefined symbols for the
198 current buffer. Initially, the global value of `hide-ifdef-env'
201 hide-ifdef-define-alist
202 An association list of defined symbol lists.
203 Use `hide-ifdef-set-define-alist' to save the current `hide-ifdef-env'
204 and `hide-ifdef-use-define-alist' to set the current `hide-ifdef-env'
205 from one of the lists in `hide-ifdef-define-alist'.
208 Set to non-nil to not show #if, #ifdef, #ifndef, #else, and
209 #endif lines when hiding.
212 Indicates whether `hide-ifdefs' should be called when Hide-Ifdef mode
216 Set to non-nil if you want to make buffers read only while hiding.
217 After `show-ifdefs', read-only status is restored to previous value.
219 \\{hide-ifdef-mode-map}"
222 (make-local-variable 'hide-ifdef-mode)
223 (setq hide-ifdef-mode
225 (not hide-ifdef-mode)
226 (> (prefix-numeric-value arg) 0)))
228 (hif-update-mode-line)
232 ; fix c-mode syntax table so we can recognize whole symbols.
233 (modify-syntax-entry ?_ "w")
234 (modify-syntax-entry ?& ".")
235 (modify-syntax-entry ?\| ".")
237 ; inherit global values
238 (make-local-variable 'hide-ifdef-env)
239 (setq hide-ifdef-env (default-value 'hide-ifdef-env))
241 (make-local-variable 'hide-ifdef-hiding)
242 (setq hide-ifdef-hiding (default-value 'hide-ifdef-hiding))
244 (make-local-variable 'hif-outside-read-only)
245 (setq hif-outside-read-only buffer-read-only)
247 (make-local-variable 'hide-ifdef-mode-map-before)
248 (setq hide-ifdef-mode-map-before (current-local-map))
249 (use-local-map (copy-keymap (current-local-map)))
250 (local-unset-key hide-ifdef-mode-prefix-key)
251 (local-set-key hide-ifdef-mode-prefix-key 'hide-ifdef-mode-map)
252 (define-hide-ifdef-mode-map)
254 (run-hooks 'hide-ifdef-mode-hook)
256 (if hide-ifdef-initially
259 (message "Enter hide-ifdef-mode.")
261 ; else end hide-ifdef-mode
262 (if hide-ifdef-hiding
264 (use-local-map hide-ifdef-mode-map-before)
265 (message "Exit hide-ifdef-mode.")
269 ;; from outline.el with docstring fixed.
270 (defun hif-outline-flag-region (from to flag)
271 "Hides or shows lines from FROM to TO, according to FLAG. If FLAG
272 is \\n (newline character) then text is shown, while if FLAG is \\^M
273 \(control-M) the text is hidden."
274 (let ((modp (buffer-modified-p)))
275 (unwind-protect (progn
276 (subst-char-in-region from to
277 (if (= flag ?\n) ?\^M ?\n)
279 (set-buffer-modified-p modp))
282 (defun hif-show-all ()
283 "Show all of the text in the current buffer."
285 (hif-outline-flag-region (point-min) (point-max) ?\n))
287 (defun hide-ifdef-region (start end)
288 "START is the start of a #if or #else form. END is the ending part.
289 Everything including these lines is made invisible."
290 (hif-outline-flag-region start end ?\^M)
293 (defun hif-show-ifdef-region (start end)
294 "Everything between START and END is made visible."
295 (hif-outline-flag-region start end ?\n)
300 ;===%%SF%% evaluation (Start) ===
302 (defvar hide-ifdef-evaluator 'eval
303 "The evaluator is given a canonical form and returns T if text under
304 that form should be displayed.")
306 (defvar hif-undefined-symbol nil
307 "...is by default considered to be false.")
309 (defvar hide-ifdef-env nil
310 "An alist of defined symbols and their values.")
313 (defun hif-set-var (var value)
314 "Prepend (var value) pair to hide-ifdef-env."
315 (setq hide-ifdef-env (cons (cons var value) hide-ifdef-env)))
318 (defun hif-lookup (var)
319 ; (message "hif-lookup %s" var)
320 (let ((val (assoc var hide-ifdef-env)))
323 hif-undefined-symbol)))
325 (defun hif-defined (var)
327 ; when #if expressions are fully supported, defined result should be 1
328 ; (if (assoc var hide-ifdef-env)
334 ;===%%SF%% evaluation (End) ===
338 ;===%%SF%% parsing (Start) ===
339 ;;; The code that understands what ifs and ifdef in files look like.
341 (defconst hif-cpp-prefix "\\(^\\|\r\\)[ \t]*#[ \t]*")
342 (defconst hif-ifndef-regexp (concat hif-cpp-prefix "ifndef"))
343 (defconst hif-ifx-regexp (concat hif-cpp-prefix "if\\(n?def\\)?[ \t]+"))
344 (defconst hif-else-regexp (concat hif-cpp-prefix "else"))
345 (defconst hif-endif-regexp (concat hif-cpp-prefix "endif"))
346 (defconst hif-ifx-else-endif-regexp
347 (concat hif-ifx-regexp "\\|" hif-else-regexp "\\|" hif-endif-regexp))
350 (defun hif-infix-to-prefix (token-list)
351 "Convert list of tokens in infix into prefix list"
352 ; (message "hif-infix-to-prefix: %s" token-list)
353 (if (= 1 (length token-list))
354 (` (hif-lookup (quote (, (car token-list)))))
355 (hif-parse-if-exp token-list))
358 ; pattern to match initial identifier, !, &&, ||, (, or ).
359 (defconst hif-token-regexp "^\\(!\\|&&\\|||\\|[()]\\|\\w+\\)")
360 (defconst hif-end-of-comment "\\*/")
363 (defun hif-tokenize (expr-string)
364 "Separate string into a list of tokens"
365 (let ((token-list nil)
367 (expr-length (length expr-string)))
369 (while (< expr-start expr-length)
370 ; (message "expr-start = %d" expr-start) (sit-for 1)
372 ((string-match "^[ \t]+" expr-string expr-start)
374 (setq expr-start (match-end 0))
375 ; stick newline in string so ^ matches on the next string-match
376 (aset expr-string (1- expr-start) ?\n)
379 ((string-match "^/\\*" expr-string expr-start)
380 (setq expr-start (match-end 0))
381 (aset expr-string (1- expr-start) ?\n)
383 (string-match hif-end-of-comment
384 expr-string expr-start) ; eat comment
385 (string-match "$" expr-string expr-start)) ; multi-line comment
386 (setq expr-start (match-end 0))
387 (aset expr-string (1- expr-start) ?\n)
390 ((string-match hif-token-regexp expr-string expr-start)
391 (let ((token (substring expr-string expr-start (match-end 0))))
392 (setq expr-start (match-end 0))
393 (aset expr-string (1- expr-start) ?\n)
394 ; (message "token: %s" token) (sit-for 1)
398 ((string-equal token "||") 'or)
399 ((string-equal token "&&") 'and)
400 ((string-equal token "!") 'not)
401 ((string-equal token "defined") 'hif-defined)
402 ((string-equal token "(") 'lparen)
403 ((string-equal token ")") 'rparen)
407 (t (error "Bad #if expression: %s" expr-string))
409 (nreverse token-list)
412 ;;;-----------------------------------------------------------------
413 ;;; Translate C preprocessor #if expressions using recursive descent.
414 ;;; This parser is limited to the operators &&, ||, !, and "defined".
416 (defun hif-parse-if-exp (token-list)
417 "Parse the TOKEN-LIST. Return translated list in prefix form."
421 (if token ; is there still a token?
422 (error "Error: unexpected token: %s" token))))
424 (defun hif-nexttoken ()
425 "Pop the next token from token-list into the let variable \"token\"."
426 (setq token (car token-list))
427 (setq token-list (cdr token-list))
431 "Parse and expression of the form
432 expr : term | expr '||' term."
433 (let ((result (hif-term)))
434 (while (eq token 'or)
436 (setq result (list 'or result (hif-term))))
440 "Parse a term of the form
441 term : factor | term '&&' factor."
442 (let ((result (hif-factor)))
443 (while (eq token 'and)
445 (setq result (list 'and result (hif-factor))))
449 "Parse a factor of the form
450 factor : '!' factor | '(' expr ')' | 'defined(' id ')' | id."
454 (list 'not (hif-factor)))
458 (let ((result (hif-expr)))
459 (if (not (eq token 'rparen))
460 (error "Bad token in parenthesized expression: %s" token)
464 ((eq token 'hif-defined)
466 (if (not (eq token 'lparen))
467 (error "Error: expected \"(\" after \"defined\""))
470 (if (memq token '(or and not hif-defined lparen rparen))
471 (error "Error: unexpected token: %s" token))
473 (if (not (eq token 'rparen))
474 (error "Error: expected \")\" after identifier"))
476 (` (hif-defined (quote (, ident))))
481 (if (memq ident '(or and))
482 (error "Error: missing identifier"))
484 (` (hif-lookup (quote (, ident))))
488 ;;;----------- end of parser -----------------------
491 (defun hif-canonicalize ()
492 "When at beginning of #ifX, returns a canonical (evaluatable)
493 form for the expression."
495 (let ((negate (looking-at hif-ifndef-regexp)))
496 (re-search-forward hif-ifx-regexp)
498 (buffer-substring (point)
499 (progn (skip-chars-forward "^\n\r") (point))))
500 (expr (hif-infix-to-prefix (hif-tokenize expr-string))))
501 ; (message "hif-canonicalized: %s" expr)
507 (defun hif-find-any-ifX ()
508 "Position at beginning of next #if, #ifdef, or #ifndef, including one on
510 ; (message "find ifX at %d" (point))
512 (re-search-forward hif-ifx-regexp (point-max) t)
513 (beginning-of-line)))
516 (defun hif-find-next-relevant ()
517 "Position at beginning of next #ifdef, #ifndef, #else, #endif,
518 NOT including one on this line."
519 ; (message "hif-find-next-relevant at %d" (point))
521 ; avoid infinite recursion by only going to beginning of line if match found
522 (if (re-search-forward hif-ifx-else-endif-regexp (point-max) t)
523 (beginning-of-line)))
525 (defun hif-find-previous-relevant ()
526 "Position at beginning of previous #ifdef, #ifndef, #else, #endif,
527 NOT including one on this line."
528 ; (message "hif-find-previous-relevant at %d" (point))
530 ; avoid infinite recursion by only going to beginning of line if match found
531 (if (re-search-backward hif-ifx-else-endif-regexp (point-min) t)
532 (beginning-of-line)))
535 (defun hif-looking-at-ifX () ;; Should eventually see #if
536 (looking-at hif-ifx-regexp))
537 (defun hif-looking-at-endif ()
538 (looking-at hif-endif-regexp))
539 (defun hif-looking-at-else ()
540 (looking-at hif-else-regexp))
544 (defun hif-ifdef-to-endif ()
545 "If positioned at #ifX or #else form, skip to corresponding #endif."
546 ; (message "hif-ifdef-to-endif at %d" (point)) (sit-for 1)
547 (hif-find-next-relevant)
548 (cond ((hif-looking-at-ifX)
549 (hif-ifdef-to-endif) ; find endif of nested if
550 (hif-ifdef-to-endif)) ; find outer endif or else
551 ((hif-looking-at-else)
552 (hif-ifdef-to-endif)) ; find endif following else
553 ((hif-looking-at-endif)
556 (error "Missmatched #ifdef #endif pair"))))
559 (defun hif-endif-to-ifdef ()
560 "If positioned at #endif form, skip backward to corresponding #ifX."
561 ; (message "hif-endif-to-ifdef at %d" (point))
562 (let ((start (point)))
563 (hif-find-previous-relevant)
564 (if (= start (point))
565 (error "Missmatched #ifdef #endif pair")))
566 (cond ((hif-looking-at-endif)
567 (hif-endif-to-ifdef) ; find beginning of nested if
568 (hif-endif-to-ifdef)) ; find beginning of outer if or else
569 ((hif-looking-at-else)
570 (hif-endif-to-ifdef))
571 ((hif-looking-at-ifX)
573 (t ; never gets here)))
576 (defun forward-ifdef (&optional arg)
577 "Move point to beginning of line of the next ifdef-endif.
578 With argument, do this that many times."
580 (or arg (setq arg 1))
582 (backward-ifdef (- arg)))
585 (let ((start (point)))
586 (if (not (hif-looking-at-ifX))
587 (hif-find-next-relevant))
588 (if (hif-looking-at-ifX)
591 (error "No following #ifdef")
595 (defun backward-ifdef (&optional arg)
596 "Move point to beginning of the previous ifdef-endif.
597 With argument, do this that many times."
599 (or arg (setq arg 1))
601 (forward-ifdef (- arg)))
605 (let ((start (point)))
606 (if (not (hif-looking-at-endif))
607 (hif-find-previous-relevant))
608 (if (hif-looking-at-endif)
611 (error "No previous #ifdef")))))
615 "Move point to beginning of nested ifdef or else-part."
617 (let ((start (point)))
618 (hif-find-next-relevant)
619 (if (or (hif-looking-at-ifX) (hif-looking-at-else))
622 (error "No following #ifdef"))))
626 "Move point to beginning of enclosing ifdef or else-part."
629 (let ((start (point)))
630 (if (not (hif-looking-at-endif))
631 (hif-find-previous-relevant))
632 (if (hif-looking-at-endif)
633 (hif-endif-to-ifdef))
634 (if (= start (point))
635 (error "No previous #ifdef"))))
637 (defun next-ifdef (&optional arg)
638 "Move to the beginning of the next #ifX, #else, or #endif.
639 With argument, do this that many times."
641 (or arg (setq arg 1))
643 (previous-ifdef (- arg)))
646 (hif-find-next-relevant)
650 (error "No following #ifdefs, #elses, or #endifs")))))
652 (defun previous-ifdef (&optional arg)
653 "Move to the beginning of the previous #ifX, #else, or #endif.
654 With argument, do this that many times."
656 (or arg (setq arg 1))
658 (next-ifdef (- arg)))
661 (let ((start (point)))
662 (hif-find-previous-relevant)
663 (if (= start (point))
664 (error "No previous #ifdefs, #elses, or #endifs")
668 ;===%%SF%% parsing (End) ===
671 ;===%%SF%% hide-ifdef-hiding (Start) ===
674 ;;; A range is a structure with four components:
675 ;;; ELSE-P True if there was an else clause for the ifdef.
676 ;;; START The start of the range. (beginning of line)
677 ;;; ELSE The else marker (beginning of line)
678 ;;; Only valid if ELSE-P is true.
679 ;;; END The end of the range. (beginning of line)
681 (defun hif-make-range (else-p start end &optional else)
682 (list else-p start else end))
684 (defun hif-range-else-p (range) (elt range 0))
685 (defun hif-range-start (range) (elt range 1))
686 (defun hif-range-else (range) (elt range 2))
687 (defun hif-range-end (range) (elt range 3))
692 ;;; The workhorse, it delimits the #if region. Reasonably simple:
693 ;;; Skip until an #else or #endif is found, remembering positions. If
694 ;;; an #else was found, skip some more, looking for the true #endif.
696 (defun hif-find-range ()
697 "Returns a Range structure describing the current #if region.
698 Point is left unchanged."
699 ; (message "hif-find-range at %d" (point))
702 (let ((start (point))
706 ;; Part one. Look for either #endif or #else.
707 ;; This loop-and-a-half dedicated to E. Dijkstra.
708 (hif-find-next-relevant)
709 (while (hif-looking-at-ifX) ; Skip nested ifdef
711 (hif-find-next-relevant))
712 ;; Found either a #else or an #endif.
713 (cond ((hif-looking-at-else)
717 (setq end (point)) ; (save-excursion (end-of-line) (point))
719 ;; If found #else, look for #endif.
722 (hif-find-next-relevant)
723 (while (hif-looking-at-ifX) ; Skip nested ifdef
725 (hif-find-next-relevant))
726 (if (hif-looking-at-else)
727 (error "Found two elses in a row? Broken!"))
728 (setq end (point)) ; (save-excursion (end-of-line) (point))
730 (hif-make-range else-p start end else))))
734 ;;; NOTE: If there's an #ifdef at the beginning of the file, we can't
735 ;;; hide it. There's no previous newline to replace. If we added
736 ;;; one, we'd throw off all the counts. Feh.
738 (defun hif-hide-line (point)
739 "Hide the line containing point. Does nothing if `hide-ifdef-lines' is nil."
743 (let ((modp (buffer-modified-p)))
747 (if (not (= (point) 1))
748 (hide-ifdef-region (1- (point)) (point))))
749 (set-buffer-modified-p modp))
754 ;;; Hif-Possibly-Hide
755 ;;; There are four cases. The #ifX expression is "taken" if it
756 ;;; the hide-ifdef-evaluator returns T. Presumably, this means the code
757 ;;; inside the #ifdef would be included when the program was
760 ;;; Case 1: #ifX taken, and there's an #else.
761 ;;; The #else part must be hidden. The #if (then) part must be
762 ;;; processed for nested #ifX's.
763 ;;; Case 2: #ifX taken, and there's no #else.
764 ;;; The #if part must be processed for nested #ifX's.
765 ;;; Case 3: #ifX not taken, and there's an #else.
766 ;;; The #if part must be hidden. The #else part must be processed
768 ;;; Case 4: #ifX not taken, and there's no #else.
769 ;;; The #ifX part must be hidden.
771 ;;; Further processing is done by narrowing to the relevant region
772 ;;; and just recursively calling hide-ifdef-guts.
774 ;;; When hif-possibly-hide returns, point is at the end of the
775 ;;; possibly-hidden range.
777 (defun hif-recurse-on (start end)
778 "Call `hide-ifdef-guts' after narrowing to end of START line and END line."
783 (narrow-to-region (point) end)
786 (defun hif-possibly-hide ()
787 "Called at #ifX expression, this hides those parts that should be
788 hidden, according to judgement of `hide-ifdef-evaluator'."
789 ; (message "hif-possibly-hide") (sit-for 1)
790 (let ((test (hif-canonicalize))
791 (range (hif-find-range)))
792 ; (message "test = %s" test) (sit-for 1)
794 (hif-hide-line (hif-range-end range))
795 (if (funcall hide-ifdef-evaluator test)
796 (cond ((hif-range-else-p range) ; case 1
797 (hif-hide-line (hif-range-else range))
798 (hide-ifdef-region (hif-range-else range)
799 (1- (hif-range-end range)))
800 (hif-recurse-on (hif-range-start range)
801 (hif-range-else range)))
803 (hif-recurse-on (hif-range-start range)
804 (hif-range-end range))))
805 (cond ((hif-range-else-p range) ; case 3
806 (hif-hide-line (hif-range-else range))
807 (hide-ifdef-region (hif-range-start range)
808 (1- (hif-range-else range)))
809 (hif-recurse-on (hif-range-else range)
810 (hif-range-end range)))
812 (hide-ifdef-region (point)
813 (1- (hif-range-end range))))
815 (hif-hide-line (hif-range-start range)) ; Always hide start.
816 (goto-char (hif-range-end range))
822 (defun hide-ifdef-guts ()
823 "Does the work of `hide-ifdefs', except for the work that's pointless
824 to redo on a recursive entry."
825 ; (message "hide-ifdef-guts")
827 (goto-char (point-min))
828 (while (hif-find-any-ifX)
829 (hif-possibly-hide))))
831 ;===%%SF%% hide-ifdef-hiding (End) ===
834 ;===%%SF%% exports (Start) ===
836 (defvar hide-ifdef-initially nil
837 "*Non-nil if `hide-ifdefs' should be called when Hide-Ifdef mode
838 is first activated.")
840 (defvar hide-ifdef-hiding nil
841 "Non-nil if text might be hidden.")
843 (defvar hide-ifdef-read-only nil
844 "*Set to non-nil if you want buffer to be read-only while hiding text.")
846 (defvar hif-outside-read-only nil
847 "Internal variable. Saves the value of `buffer-read-only' while hiding.")
849 (defvar hide-ifdef-lines nil
850 "*Set to t if you don't want to see the #ifX, #else, and #endif lines.")
852 (defun hide-ifdef-toggle-read-only ()
853 "Toggle hide-ifdef-read-only."
855 (setq hide-ifdef-read-only (not hide-ifdef-read-only))
856 (message "Hide-Read-Only %s"
857 (if hide-ifdef-read-only "ON" "OFF"))
858 (if hide-ifdef-hiding
859 (setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only)))
860 (hif-update-mode-line))
862 (defun hide-ifdef-toggle-outside-read-only ()
863 "Replacement for `toggle-read-only' within Hide-Ifdef mode."
865 (setq hif-outside-read-only (not hif-outside-read-only))
866 (message "Read only %s"
867 (if hif-outside-read-only "ON" "OFF"))
868 (setq buffer-read-only
869 (or (and hide-ifdef-hiding hide-ifdef-read-only)
870 hif-outside-read-only)
872 (hif-update-mode-line))
875 (defun hide-ifdef-define (var)
876 "Define a VAR so that #ifdef VAR would be included."
877 (interactive "SDefine what? ")
879 (if hide-ifdef-hiding (hide-ifdefs)))
881 (defun hide-ifdef-undef (var)
882 "Undefine a VAR so that #ifdef VAR would not be included."
883 (interactive "SUndefine what? ")
884 (hif-set-var var nil)
885 (if hide-ifdef-hiding (hide-ifdefs)))
888 (defun hide-ifdefs ()
889 "Hide the contents of some #ifdefs. Assume that defined symbols have
890 been added to `hide-ifdef-env'. The text hidden is the text that would not
891 be included by the C preprocessor if it were given the file with those
894 Turn off hiding by calling show-ifdef."
897 (message "Hiding...")
898 (if (not hide-ifdef-mode)
899 (hide-ifdef-mode 1)) ; turn on hide-ifdef-mode
900 (if hide-ifdef-hiding
901 (show-ifdefs)) ; Otherwise, deep confusion.
902 (if buffer-read-only (toggle-read-only)) ; make it writable temporarily
903 (setq selective-display t)
904 (setq hide-ifdef-hiding t)
906 (if (or hide-ifdef-read-only hif-outside-read-only)
907 (toggle-read-only)) ; make it read only
908 (message "Hiding done"))
911 (defun show-ifdefs ()
912 "Cancel the effects of hide-ifdef. The contents of all #ifdefs is shown."
914 (if buffer-read-only (toggle-read-only)) ; make it writable temporarily
915 (setq selective-display nil) ; defaults
917 (if hif-outside-read-only
918 (toggle-read-only)) ; make it read only
919 (setq hide-ifdef-hiding nil))
922 (defun hif-find-ifdef-block ()
923 "Utilitiy for hide and show ifdef-block. Set top and bottom of ifdef block."
927 (if (not (or (hif-looking-at-else) (hif-looking-at-ifX)))
931 (setq max-bottom (1- (point))))
934 (if (not (hif-looking-at-endif))
935 (hif-find-next-relevant))
936 (while (hif-looking-at-ifX)
938 (hif-find-next-relevant))
939 (setq bottom (min max-bottom (1- (point))))))
943 (defun hide-ifdef-block ()
944 "Hide the ifdef block (true or false part) enclosing or before the cursor."
946 (if (not hide-ifdef-mode)
948 (if buffer-read-only (toggle-read-only))
949 (setq selective-display t)
951 (hif-find-ifdef-block) ; set top and bottom - dynamic scoping
952 (hide-ifdef-region top bottom)
956 (hif-hide-line (1+ bottom))))
957 (setq hide-ifdef-hiding t))
958 (if (or hide-ifdef-read-only hif-outside-read-only)
962 (defun show-ifdef-block ()
963 "Show the ifdef block (true or false part) enclosing or before the cursor."
965 (let ((old-read-only buffer-read-only))
966 (if old-read-only (toggle-read-only))
970 (hif-show-ifdef-region (1- (point)) (progn (end-of-line) (point))))
973 (hif-find-ifdef-block)
974 (hif-show-ifdef-region (1- top) bottom))
977 ; restore read only status since we dont know if all is shown.
978 (if old-read-only (toggle-read-only))))
981 ;;; defininition alist support
983 (defvar hide-ifdef-define-alist nil
984 "A global assoc list of pre-defined symbol lists")
986 (defun hif-compress-define-list (env)
987 "Compress the define list ENV into a list of defined symbols only."
988 (let ((defs (mapcar '(lambda (arg)
989 (if (hif-lookup (car arg)) (car arg)))
994 (setq new-defs (cons (car defs) new-defs)))
995 (setq defs (cdr defs)))
998 (defun hide-ifdef-set-define-alist (name)
999 "Set the association for NAME to hide-ifdef-env."
1000 (interactive "SSet define list: ")
1001 (setq hide-ifdef-define-alist
1002 (cons (cons name (hif-compress-define-list hide-ifdef-env))
1003 hide-ifdef-define-alist)))
1005 (defun hide-ifdef-use-define-alist (name)
1006 "Set hide-ifdef-env to the define list specified by NAME."
1007 (interactive "SUse define list: ")
1008 (let ((define-list (assoc name hide-ifdef-define-alist)))
1010 (setq hide-ifdef-env
1011 (mapcar '(lambda (arg) (cons arg t))
1013 (error "No define list for %s" name))
1014 (if hide-ifdef-hiding (hide-ifdefs))))
1016 ;;; hideif.el ends here