1 ;;; hideif.el --- hides selected code within ifdef
3 ;; Copyright (C) 1988, 1994, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 ;; 2008 Free Software Foundation, Inc.
6 ;; Author: Daniel LaLiberte <liberte@holonexus.org>
8 ;; Keywords: c, outlines
10 ;; This file is part of GNU Emacs.
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, or (at your option)
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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; To initialize, toggle the hide-ifdef minor mode with
31 ;; M-x hide-ifdef-mode
33 ;; This will set up key bindings and call hide-ifdef-mode-hook if it
34 ;; has a value. To explicitly hide ifdefs using a buffer-local
35 ;; define list (default empty), type
37 ;; M-x hide-ifdefs or C-c @ h
39 ;; Hide-ifdef suppresses the display of code that the preprocessor wouldn't
40 ;; pass through. The support of constant expressions in #if lines is
41 ;; limited to identifiers, parens, and the operators: &&, ||, !, and
42 ;; "defined". Please extend this.
44 ;; The hidden code is marked by ellipses (...). Be
45 ;; cautious when editing near ellipses, since the hidden text is
46 ;; still in the buffer, and you can move the point into it and modify
48 ;; You can make your buffer read-only while hide-ifdef-hiding by setting
49 ;; hide-ifdef-read-only to a non-nil value. You can toggle this
50 ;; variable with hide-ifdef-toggle-read-only (C-c @ C-q).
52 ;; You can undo the effect of hide-ifdefs by typing
54 ;; M-x show-ifdefs or C-c @ s
56 ;; Use M-x hide-ifdef-define (C-c @ d) to define a symbol.
57 ;; Use M-x hide-ifdef-undef (C-c @ u) to undefine a symbol.
59 ;; If you define or undefine a symbol while hide-ifdef-mode is in effect,
60 ;; the display will be updated. Only the define list for the current
61 ;; buffer will be affected. You can save changes to the local define
62 ;; list with hide-ifdef-set-define-alist. This adds entries
63 ;; to hide-ifdef-define-alist.
65 ;; If you have defined a hide-ifdef-mode-hook, you can set
66 ;; up a list of symbols that may be used by hide-ifdefs as in the
69 ;; (add-hook 'hide-ifdef-mode-hook
71 ;; (unless hide-ifdef-define-alist
72 ;; (setq hide-ifdef-define-alist
74 ;; (list2 TWO THREE))))
75 ;; (hide-ifdef-use-define-alist 'list2))) ; use list2 by default
77 ;; You can call hide-ifdef-use-define-alist (C-c @ U) at any time to specify
78 ;; another list to use.
80 ;; To cause ifdefs to be hidden as soon as hide-ifdef-mode is called,
81 ;; set hide-ifdef-initially to non-nil.
83 ;; If you set hide-ifdef-lines to t, hide-ifdefs hides all the #ifdef lines.
84 ;; In the absence of highlighting, that might be a bad idea. If you set
85 ;; hide-ifdef-lines to nil (the default), the surrounding preprocessor
86 ;; lines will be displayed. That can be confusing in its own
87 ;; right. Other variations on display are possible, but not much
90 ;; You can explicitly hide or show individual ifdef blocks irrespective
91 ;; of the define list by using hide-ifdef-block and show-ifdef-block.
93 ;; You can move the point between ifdefs with forward-ifdef, backward-ifdef,
94 ;; up-ifdef, down-ifdef, next-ifdef, and previous-ifdef.
96 ;; If you have minor-mode-alist in your mode line (the default) two labels
97 ;; may appear. "Ifdef" will appear when hide-ifdef-mode is active. "Hiding"
98 ;; will appear when text may be hidden ("hide-ifdef-hiding" is non-nil).
100 ;; Written by Brian Marick, at Gould, Computer Systems Division, Urbana IL.
101 ;; Extensively modified by Daniel LaLiberte (while at Gould).
107 (defgroup hide-ifdef nil
108 "Hide selected code within `ifdef'."
111 (defcustom hide-ifdef-initially nil
112 "Non-nil means call `hide-ifdefs' when Hide-Ifdef mode is first activated."
116 (defcustom hide-ifdef-read-only nil
117 "Set to non-nil if you want buffer to be read-only while hiding text."
121 (defcustom hide-ifdef-lines nil
122 "Non-nil means hide the #ifX, #else, and #endif lines."
126 (defcustom hide-ifdef-shadow nil
127 "Non-nil means shadow text instead of hiding it."
132 (defface hide-ifdef-shadow
'((t (:inherit shadow
)))
133 "Face for shadowing ifdef blocks."
138 (defvar hide-ifdef-mode-submap
139 ;; Set up the submap that goes after the prefix key.
140 (let ((map (make-sparse-keymap)))
141 (define-key map
"d" 'hide-ifdef-define
)
142 (define-key map
"u" 'hide-ifdef-undef
)
143 (define-key map
"D" 'hide-ifdef-set-define-alist
)
144 (define-key map
"U" 'hide-ifdef-use-define-alist
)
146 (define-key map
"h" 'hide-ifdefs
)
147 (define-key map
"s" 'show-ifdefs
)
148 (define-key map
"\C-d" 'hide-ifdef-block
)
149 (define-key map
"\C-s" 'show-ifdef-block
)
151 (define-key map
"\C-q" 'hide-ifdef-toggle-read-only
)
152 (define-key map
"\C-w" 'hide-ifdef-toggle-shadowing
)
153 (substitute-key-definition
154 'toggle-read-only
'hide-ifdef-toggle-outside-read-only map
)
156 "Keymap used by `hide-ifdef-mode' under `hide-ifdef-mode-prefix-key'.")
158 (defconst hide-ifdef-mode-prefix-key
"\C-c@"
159 "Prefix key for all Hide-Ifdef mode commands.")
161 (defvar hide-ifdef-mode-map
162 ;; Set up the mode's main map, which leads via the prefix key to the submap.
163 (let ((map (make-sparse-keymap)))
164 (define-key map hide-ifdef-mode-prefix-key hide-ifdef-mode-submap
)
166 "Keymap used with `hide-ifdef-mode'.")
168 (easy-menu-define hide-ifdef-mode-menu hide-ifdef-mode-map
169 "Menu for `hide-ifdef-mode'."
171 ["Hide some ifdefs" hide-ifdefs t
]
172 ["Show all ifdefs" show-ifdefs t
]
173 ["Hide ifdef block" hide-ifdef-block t
]
174 ["Show ifdef block" show-ifdef-block t
]
175 ["Define a variable" hide-ifdef-define t
]
176 ["Define an alist" hide-ifdef-set-define-alist t
]
177 ["Use an alist" hide-ifdef-use-define-alist t
]
178 ["Undefine a variable" hide-ifdef-undef t
]
179 ["Toggle read only" hide-ifdef-toggle-read-only
180 :style toggle
:selected hide-ifdef-read-only
]
181 ["Toggle shadowing" hide-ifdef-toggle-shadowing
182 :style toggle
:selected hide-ifdef-shadow
]))
184 (defvar hide-ifdef-hiding nil
185 "Non-nil when text may be hidden.")
187 (or (assq 'hide-ifdef-hiding minor-mode-alist
)
188 (setq minor-mode-alist
189 (cons '(hide-ifdef-hiding " Hiding")
192 ;; fix c-mode syntax table so we can recognize whole symbols.
193 (defvar hide-ifdef-syntax-table
194 (let ((st (copy-syntax-table c-mode-syntax-table
)))
195 (modify-syntax-entry ?_
"w" st
)
196 (modify-syntax-entry ?
& "." st
)
197 (modify-syntax-entry ?\|
"." st
)
199 "Syntax table used for tokenizing #if expressions.")
201 (defvar hide-ifdef-env nil
202 "An alist of defined symbols and their values.")
204 (defvar hif-outside-read-only nil
205 "Internal variable. Saves the value of `buffer-read-only' while hiding.")
208 (define-minor-mode hide-ifdef-mode
209 "Toggle Hide-Ifdef mode. This is a minor mode, albeit a large one.
210 With ARG, turn Hide-Ifdef mode on if arg is positive, off otherwise.
211 In Hide-Ifdef mode, code within #ifdef constructs that the C preprocessor
212 would eliminate may be hidden from view. Several variables affect
213 how the hiding is done:
216 An association list of defined and undefined symbols for the
217 current buffer. Initially, the global value of `hide-ifdef-env'
220 `hide-ifdef-define-alist'
221 An association list of defined symbol lists.
222 Use `hide-ifdef-set-define-alist' to save the current `hide-ifdef-env'
223 and `hide-ifdef-use-define-alist' to set the current `hide-ifdef-env'
224 from one of the lists in `hide-ifdef-define-alist'.
227 Set to non-nil to not show #if, #ifdef, #ifndef, #else, and
228 #endif lines when hiding.
230 `hide-ifdef-initially'
231 Indicates whether `hide-ifdefs' should be called when Hide-Ifdef mode
234 `hide-ifdef-read-only'
235 Set to non-nil if you want to make buffers read only while hiding.
236 After `show-ifdefs', read-only status is restored to previous value.
238 \\{hide-ifdef-mode-map}"
239 :group
'hide-ifdef
:lighter
" Ifdef"
242 ;; inherit global values
243 (set (make-local-variable 'hide-ifdef-env
)
244 (default-value 'hide-ifdef-env
))
245 (set (make-local-variable 'hide-ifdef-hiding
)
246 (default-value 'hide-ifdef-hiding
))
247 (set (make-local-variable 'hif-outside-read-only
) buffer-read-only
)
248 (set (make-local-variable 'line-move-ignore-invisible
) t
)
249 (add-hook 'change-major-mode-hook
250 (lambda () (hide-ifdef-mode -
1)) nil t
)
252 (add-to-invisibility-spec '(hide-ifdef . t
))
254 (if hide-ifdef-initially
257 ;; else end hide-ifdef-mode
258 (kill-local-variable 'line-move-ignore-invisible
)
259 (remove-from-invisibility-spec '(hide-ifdef . t
))
260 (if hide-ifdef-hiding
264 (defun hif-show-all ()
265 "Show all of the text in the current buffer."
267 (hif-show-ifdef-region (point-min) (point-max)))
269 ;; By putting this on after-revert-hook, we arrange that it only
270 ;; does anything when revert-buffer avoids turning off the mode.
271 ;; (That can happen in VC.)
272 (defun hif-after-revert-function ()
273 (and hide-ifdef-mode hide-ifdef-hiding
275 (add-hook 'after-revert-hook
'hif-after-revert-function
)
277 (defun hif-end-of-line ()
279 (while (= (logand 1 (skip-chars-backward "\\\\")) 1)
282 (defun hide-ifdef-region-internal (start end
)
283 (remove-overlays start end
'hide-ifdef t
)
284 (let ((o (make-overlay start end
)))
285 (overlay-put o
'hide-ifdef t
)
286 (if hide-ifdef-shadow
287 (overlay-put o
'face
'hide-ifdef-shadow
)
288 (overlay-put o
'invisible
'hide-ifdef
))))
290 (defun hide-ifdef-region (start end
)
291 "START is the start of a #if or #else form. END is the ending part.
292 Everything including these lines is made invisible."
294 (goto-char start
) (hif-end-of-line) (setq start
(point))
295 (goto-char end
) (hif-end-of-line) (setq end
(point))
296 (hide-ifdef-region-internal start end
)))
298 (defun hif-show-ifdef-region (start end
)
299 "Everything between START and END is made visible."
300 (remove-overlays start end
'hide-ifdef t
))
303 ;;===%%SF%% evaluation (Start) ===
305 ;; It is not useful to set this to anything but `eval'.
306 ;; In fact, the variable might as well be eliminated.
307 (defvar hide-ifdef-evaluator
'eval
308 "The function to use to evaluate a form.
309 The evaluator is given a canonical form and returns t if text under
310 that form should be displayed.")
312 (defvar hif-undefined-symbol nil
313 "...is by default considered to be false.")
316 (defun hif-set-var (var value
)
317 "Prepend (var value) pair to hide-ifdef-env."
318 (setq hide-ifdef-env
(cons (cons var value
) hide-ifdef-env
)))
321 (defun hif-lookup (var)
322 ;; (message "hif-lookup %s" var)
323 (let ((val (assoc var hide-ifdef-env
)))
326 hif-undefined-symbol
)))
328 (defun hif-defined (var)
329 (if (assoc var hide-ifdef-env
) 1 0))
331 ;;===%%SF%% evaluation (End) ===
335 ;;===%%SF%% parsing (Start) ===
336 ;;; The code that understands what ifs and ifdef in files look like.
338 (defconst hif-cpp-prefix
"\\(^\\|\r\\)[ \t]*#[ \t]*")
339 (defconst hif-ifndef-regexp
(concat hif-cpp-prefix
"ifndef"))
340 (defconst hif-ifx-regexp
(concat hif-cpp-prefix
"if\\(n?def\\)?[ \t]+"))
341 (defconst hif-else-regexp
(concat hif-cpp-prefix
"else"))
342 (defconst hif-endif-regexp
(concat hif-cpp-prefix
"endif"))
343 (defconst hif-ifx-else-endif-regexp
344 (concat hif-ifx-regexp
"\\|" hif-else-regexp
"\\|" hif-endif-regexp
))
346 ;; Used to store the current token and the whole token list during parsing.
347 ;; Only bound dynamically.
349 (defvar hif-token-list
)
351 ;; pattern to match initial identifier, !, &&, ||, (, or ).
352 ;; Added ==, + and -: garyo@avs.com 8/9/94
353 (defconst hif-token-regexp
354 "\\(&&\\|||\\|[!=]=\\|!\\|[()+?:-]\\|[<>]=?\\|\\w+\\)")
356 (defun hif-tokenize (start end
)
357 "Separate string between START and END into a list of tokens."
358 (let ((token-list nil
))
359 (with-syntax-table hide-ifdef-syntax-table
362 (while (progn (forward-comment (point-max)) (< (point) end
))
363 ;; (message "expr-start = %d" expr-start) (sit-for 1)
365 ((looking-at "\\\\\n")
368 ((looking-at hif-token-regexp
)
369 (let ((token (buffer-substring (point) (match-end 0))))
370 (goto-char (match-end 0))
371 ;; (message "token: %s" token) (sit-for 1)
373 ((string-equal token
"||") 'or
)
374 ((string-equal token
"&&") 'and
)
375 ((string-equal token
"==") 'equal
)
376 ((string-equal token
"!=") 'hif-notequal
)
377 ((string-equal token
"!") 'not
)
378 ((string-equal token
"defined") 'hif-defined
)
379 ((string-equal token
"(") 'lparen
)
380 ((string-equal token
")") 'rparen
)
381 ((string-equal token
">") 'hif-greater
)
382 ((string-equal token
"<") 'hif-less
)
383 ((string-equal token
">=") 'hif-greater-equal
)
384 ((string-equal token
"<=") 'hif-less-equal
)
385 ((string-equal token
"+") 'hif-plus
)
386 ((string-equal token
"-") 'hif-minus
)
387 ((string-equal token
"?") 'hif-conditional
)
388 ((string-equal token
":") 'hif-colon
)
389 ((string-match "\\`[0-9]*\\'" token
)
390 (string-to-number token
))
393 (t (error "Bad #if expression: %s" (buffer-string)))))))
394 (nreverse token-list
)))
396 ;;;-----------------------------------------------------------------
397 ;;; Translate C preprocessor #if expressions using recursive descent.
398 ;;; This parser is limited to the operators &&, ||, !, and "defined".
399 ;;; Added ==, !=, +, and -. Gary Oberbrunner, garyo@avs.com, 8/9/94
401 (defsubst hif-nexttoken
()
402 "Pop the next token from token-list into the let variable \"hif-token\"."
403 (setq hif-token
(pop hif-token-list
)))
405 (defun hif-parse-if-exp (hif-token-list)
406 "Parse the TOKEN-LIST. Return translated list in prefix form."
410 (if hif-token
; is there still a token?
411 (error "Error: unexpected token: %s" hif-token
))))
414 "Parse an expression as found in #if.
415 expr : or-expr | or-expr '?' expr ':' expr."
416 (let ((result (hif-or-expr))
418 (while (eq hif-token
'hif-conditional
)
420 (setq middle
(hif-expr))
421 (if (eq hif-token
'hif-colon
)
424 (setq result
(list 'hif-conditional result middle
(hif-expr))))
425 (error "Error: unexpected token: %s" hif-token
)))
428 (defun hif-or-expr ()
429 "Parse n or-expr : and-expr | or-expr '||' and-expr."
430 (let ((result (hif-and-expr)))
431 (while (eq hif-token
'or
)
433 (setq result
(list 'hif-or result
(hif-and-expr))))
436 (defun hif-and-expr ()
437 "Parse an and-expr : eq-expr | and-expr '&&' eq-expr."
438 (let ((result (hif-eq-expr)))
439 (while (eq hif-token
'and
)
441 (setq result
(list 'hif-and result
(hif-eq-expr))))
444 (defun hif-eq-expr ()
445 "Parse an eq-expr : math | eq-expr `=='|`!='|`<'|`>'|`>='|`<=' math."
446 (let ((result (hif-math))
448 (while (memq hif-token
'(equal hif-notequal hif-greater hif-less
449 hif-greater-equal hif-less-equal
))
450 (setq eq-token hif-token
)
452 (setq result
(list eq-token result
(hif-math))))
456 "Parse an expression with + or - and simpler things.
457 math : factor | math '+|-' factor."
458 (let ((result (hif-factor))
460 (while (memq hif-token
'(hif-plus hif-minus
))
461 (setq math-op hif-token
)
463 (setq result
(list math-op result
(hif-factor))))
467 "Parse a factor: '!' factor | '(' expr ')' | 'defined(' id ')' | id."
471 (list 'hif-not
(hif-factor)))
473 ((eq hif-token
'lparen
)
475 (let ((result (hif-expr)))
476 (if (not (eq hif-token
'rparen
))
477 (error "Bad token in parenthesized expression: %s" hif-token
)
481 ((eq hif-token
'hif-defined
)
483 (let ((paren (when (eq hif-token
'lparen
) (hif-nexttoken) t
))
485 (if (memq hif-token
'(or and not hif-defined lparen rparen
))
486 (error "Error: unexpected token: %s" hif-token
))
489 (unless (eq hif-token
'rparen
)
490 (error "Error: expected \")\" after identifier")))
492 `(hif-defined (quote ,ident
))))
495 (prog1 hif-token
(hif-nexttoken)))
498 (let ((ident hif-token
))
499 (if (memq ident
'(or and
))
500 (error "Error: missing identifier"))
502 `(hif-lookup (quote ,ident
))))))
504 (defun hif-mathify (val)
505 "Treat VAL as a number: if it's t or nil, use 1 or 0."
510 (defun hif-conditional (a b c
)
511 (if (not (zerop (hif-mathify a
))) (hif-mathify b
) (hif-mathify c
)))
513 (and (not (zerop (hif-mathify a
))) (not (zerop (hif-mathify b
)))))
515 (or (not (zerop (hif-mathify a
))) (not (zerop (hif-mathify b
)))))
517 (zerop (hif-mathify a
)))
518 (defun hif-plus (a b
)
519 "Like ordinary plus but treat t and nil as 1 and 0."
520 (+ (hif-mathify a
) (hif-mathify b
)))
521 (defun hif-minus (a b
)
522 "Like ordinary minus but treat t and nil as 1 and 0."
523 (- (hif-mathify a
) (hif-mathify b
)))
524 (defun hif-notequal (a b
)
525 "Like (not (equal A B)) but as one symbol."
527 (defun hif-greater (a b
)
529 (> (hif-mathify a
) (hif-mathify b
)))
530 (defun hif-less (a b
)
532 (< (hif-mathify a
) (hif-mathify b
)))
533 (defun hif-greater-equal (a b
)
535 (>= (hif-mathify a
) (hif-mathify b
)))
536 (defun hif-less-equal (a b
)
538 (<= (hif-mathify a
) (hif-mathify b
)))
539 ;;;----------- end of parser -----------------------
542 (defun hif-canonicalize ()
543 "When at beginning of #ifX, return a Lisp expression for its condition."
545 (let ((negate (looking-at hif-ifndef-regexp
)))
546 (re-search-forward hif-ifx-regexp
)
547 (let* ((tokens (hif-tokenize (point)
548 (progn (hif-end-of-line) (point))))
549 (expr (hif-parse-if-exp tokens
)))
550 ;; (message "hif-canonicalized: %s" expr)
556 (defun hif-find-any-ifX ()
557 "Move to next #if..., or #ifndef, at point or after."
558 ;; (message "find ifX at %d" (point))
560 (re-search-forward hif-ifx-regexp
(point-max) t
)
561 (beginning-of-line)))
564 (defun hif-find-next-relevant ()
565 "Move to next #if..., #else, or #endif, after the current line."
566 ;; (message "hif-find-next-relevant at %d" (point))
568 ;; avoid infinite recursion by only going to beginning of line if match found
569 (if (re-search-forward hif-ifx-else-endif-regexp
(point-max) t
)
570 (beginning-of-line)))
572 (defun hif-find-previous-relevant ()
573 "Move to previous #if..., #else, or #endif, before the current line."
574 ;; (message "hif-find-previous-relevant at %d" (point))
576 ;; avoid infinite recursion by only going to beginning of line if match found
577 (if (re-search-backward hif-ifx-else-endif-regexp
(point-min) t
)
578 (beginning-of-line)))
581 (defun hif-looking-at-ifX () ;; Should eventually see #if
582 (looking-at hif-ifx-regexp
))
583 (defun hif-looking-at-endif ()
584 (looking-at hif-endif-regexp
))
585 (defun hif-looking-at-else ()
586 (looking-at hif-else-regexp
))
590 (defun hif-ifdef-to-endif ()
591 "If positioned at #ifX or #else form, skip to corresponding #endif."
592 ;; (message "hif-ifdef-to-endif at %d" (point)) (sit-for 1)
593 (hif-find-next-relevant)
594 (cond ((hif-looking-at-ifX)
595 (hif-ifdef-to-endif) ; find endif of nested if
596 (hif-ifdef-to-endif)) ; find outer endif or else
597 ((hif-looking-at-else)
598 (hif-ifdef-to-endif)) ; find endif following else
599 ((hif-looking-at-endif)
602 (error "Mismatched #ifdef #endif pair"))))
605 (defun hif-endif-to-ifdef ()
606 "If positioned at #endif form, skip backward to corresponding #ifX."
607 ;; (message "hif-endif-to-ifdef at %d" (point))
608 (let ((start (point)))
609 (hif-find-previous-relevant)
610 (if (= start
(point))
611 (error "Mismatched #ifdef #endif pair")))
612 (cond ((hif-looking-at-endif)
613 (hif-endif-to-ifdef) ; find beginning of nested if
614 (hif-endif-to-ifdef)) ; find beginning of outer if or else
615 ((hif-looking-at-else)
616 (hif-endif-to-ifdef))
617 ((hif-looking-at-ifX)
619 (t))) ; never gets here
622 (defun forward-ifdef (&optional arg
)
623 "Move point to beginning of line of the next ifdef-endif.
624 With argument, do this that many times."
626 (or arg
(setq arg
1))
627 (if (< arg
0) (backward-ifdef (- arg
))
630 (let ((start (point)))
631 (unless (hif-looking-at-ifX)
632 (hif-find-next-relevant))
633 (if (hif-looking-at-ifX)
636 (error "No following #ifdef"))))))
639 (defun backward-ifdef (&optional arg
)
640 "Move point to beginning of the previous ifdef-endif.
641 With argument, do this that many times."
643 (or arg
(setq arg
1))
644 (if (< arg
0) (forward-ifdef (- arg
))
648 (let ((start (point)))
649 (unless (hif-looking-at-endif)
650 (hif-find-previous-relevant))
651 (if (hif-looking-at-endif)
654 (error "No previous #ifdef"))))))
658 "Move point to beginning of nested ifdef or else-part."
660 (let ((start (point)))
661 (hif-find-next-relevant)
662 (if (or (hif-looking-at-ifX) (hif-looking-at-else))
665 (error "No following #ifdef"))))
669 "Move point to beginning of enclosing ifdef or else-part."
672 (let ((start (point)))
673 (unless (hif-looking-at-endif)
674 (hif-find-previous-relevant))
675 (if (hif-looking-at-endif)
676 (hif-endif-to-ifdef))
677 (if (= start
(point))
678 (error "No previous #ifdef"))))
680 (defun next-ifdef (&optional arg
)
681 "Move to the beginning of the next #ifX, #else, or #endif.
682 With argument, do this that many times."
684 (or arg
(setq arg
1))
685 (if (< arg
0) (previous-ifdef (- arg
))
688 (hif-find-next-relevant)
691 (error "No following #ifdefs, #elses, or #endifs")))))
693 (defun previous-ifdef (&optional arg
)
694 "Move to the beginning of the previous #ifX, #else, or #endif.
695 With argument, do this that many times."
697 (or arg
(setq arg
1))
698 (if (< arg
0) (next-ifdef (- arg
))
701 (let ((start (point)))
702 (hif-find-previous-relevant)
703 (if (= start
(point))
704 (error "No previous #ifdefs, #elses, or #endifs"))))))
707 ;;===%%SF%% parsing (End) ===
710 ;;===%%SF%% hide-ifdef-hiding (Start) ===
713 ;;; A range is a structure with four components:
714 ;;; ELSE-P True if there was an else clause for the ifdef.
715 ;;; START The start of the range. (beginning of line)
716 ;;; ELSE The else marker (beginning of line)
717 ;;; Only valid if ELSE-P is true.
718 ;;; END The end of the range. (beginning of line)
720 (defsubst hif-make-range
(start end
&optional else
)
721 (list start else end
))
723 (defsubst hif-range-start
(range) (elt range
0))
724 (defsubst hif-range-else
(range) (elt range
1))
725 (defsubst hif-range-end
(range) (elt range
2))
730 ;;; The workhorse, it delimits the #if region. Reasonably simple:
731 ;;; Skip until an #else or #endif is found, remembering positions. If
732 ;;; an #else was found, skip some more, looking for the true #endif.
734 (defun hif-find-range ()
735 "Return a Range structure describing the current #if region.
736 Point is left unchanged."
737 ;; (message "hif-find-range at %d" (point))
740 (let ((start (point))
743 ;; Part one. Look for either #endif or #else.
744 ;; This loop-and-a-half dedicated to E. Dijkstra.
746 (hif-find-next-relevant)
747 (hif-looking-at-ifX)) ; Skip nested ifdef
748 (hif-ifdef-to-endif))
749 ;; Found either a #else or an #endif.
750 (cond ((hif-looking-at-else)
753 (setq end
(point)))) ; (save-excursion (end-of-line) (point))
754 ;; If found #else, look for #endif.
757 (hif-find-next-relevant)
758 (hif-looking-at-ifX)) ; Skip nested ifdef
759 (hif-ifdef-to-endif))
760 (if (hif-looking-at-else)
761 (error "Found two elses in a row? Broken!"))
762 (setq end
(point))) ; (save-excursion (end-of-line) (point))
763 (hif-make-range start end else
))))
768 (defun hif-hide-line (point)
769 "Hide the line containing point. Does nothing if `hide-ifdef-lines' is nil."
770 (when hide-ifdef-lines
773 (hide-ifdef-region-internal
774 (line-beginning-position) (progn (hif-end-of-line) (point))))))
777 ;;; Hif-Possibly-Hide
778 ;;; There are four cases. The #ifX expression is "taken" if it
779 ;;; the hide-ifdef-evaluator returns T. Presumably, this means the code
780 ;;; inside the #ifdef would be included when the program was
783 ;;; Case 1: #ifX taken, and there's an #else.
784 ;;; The #else part must be hidden. The #if (then) part must be
785 ;;; processed for nested #ifX's.
786 ;;; Case 2: #ifX taken, and there's no #else.
787 ;;; The #if part must be processed for nested #ifX's.
788 ;;; Case 3: #ifX not taken, and there's an #else.
789 ;;; The #if part must be hidden. The #else part must be processed
791 ;;; Case 4: #ifX not taken, and there's no #else.
792 ;;; The #ifX part must be hidden.
794 ;;; Further processing is done by narrowing to the relevant region
795 ;;; and just recursively calling hide-ifdef-guts.
797 ;;; When hif-possibly-hide returns, point is at the end of the
798 ;;; possibly-hidden range.
800 (defun hif-recurse-on (start end
)
801 "Call `hide-ifdef-guts' after narrowing to end of START line and END line."
806 (narrow-to-region (point) end
)
809 (defun hif-possibly-hide ()
810 "Called at #ifX expression, this hides those parts that should be hidden.
811 It uses the judgement of `hide-ifdef-evaluator'."
812 ;; (message "hif-possibly-hide") (sit-for 1)
813 (let ((test (hif-canonicalize))
814 (range (hif-find-range)))
815 ;; (message "test = %s" test) (sit-for 1)
817 (hif-hide-line (hif-range-end range
))
818 (if (not (hif-not (funcall hide-ifdef-evaluator test
)))
819 (cond ((hif-range-else range
) ; case 1
820 (hif-hide-line (hif-range-else range
))
821 (hide-ifdef-region (hif-range-else range
)
822 (1- (hif-range-end range
)))
823 (hif-recurse-on (hif-range-start range
)
824 (hif-range-else range
)))
826 (hif-recurse-on (hif-range-start range
)
827 (hif-range-end range
))))
828 (cond ((hif-range-else range
) ; case 3
829 (hif-hide-line (hif-range-else range
))
830 (hide-ifdef-region (hif-range-start range
)
831 (1- (hif-range-else range
)))
832 (hif-recurse-on (hif-range-else range
)
833 (hif-range-end range
)))
835 (hide-ifdef-region (point)
836 (1- (hif-range-end range
))))))
837 (hif-hide-line (hif-range-start range
)) ; Always hide start.
838 (goto-char (hif-range-end range
))
843 (defun hide-ifdef-guts ()
844 "Does most of the work of `hide-ifdefs'.
845 It does not do the work that's pointless to redo on a recursive entry."
846 ;; (message "hide-ifdef-guts")
848 (goto-char (point-min))
849 (while (hif-find-any-ifX)
850 (hif-possibly-hide))))
852 ;;===%%SF%% hide-ifdef-hiding (End) ===
855 ;;===%%SF%% exports (Start) ===
857 (defun hide-ifdef-toggle-read-only ()
858 "Toggle `hide-ifdef-read-only'."
860 (setq hide-ifdef-read-only
(not hide-ifdef-read-only
))
861 (message "Hide-Read-Only %s"
862 (if hide-ifdef-read-only
"ON" "OFF"))
863 (if hide-ifdef-hiding
864 (setq buffer-read-only
(or hide-ifdef-read-only hif-outside-read-only
)))
865 (force-mode-line-update))
867 (defun hide-ifdef-toggle-outside-read-only ()
868 "Replacement for `toggle-read-only' within Hide-Ifdef mode."
870 (setq hif-outside-read-only
(not hif-outside-read-only
))
871 (message "Read only %s"
872 (if hif-outside-read-only
"ON" "OFF"))
873 (setq buffer-read-only
874 (or (and hide-ifdef-hiding hide-ifdef-read-only
)
875 hif-outside-read-only
))
876 (force-mode-line-update))
878 (defun hide-ifdef-toggle-shadowing ()
881 (set (make-local-variable 'hide-ifdef-shadow
) (not hide-ifdef-shadow
))
882 (message "Shadowing %s" (if hide-ifdef-shadow
"ON" "OFF"))
885 (dolist (overlay (overlays-in (point-min) (point-max)))
886 (when (overlay-get overlay
'hide-ifdef
)
887 (if hide-ifdef-shadow
889 (overlay-put overlay
'invisible nil
)
890 (overlay-put overlay
'face
'hide-ifdef-shadow
))
891 (overlay-put overlay
'face nil
)
892 (overlay-put overlay
'invisible
'hide-ifdef
))))))
894 (defun hide-ifdef-define (var)
895 "Define a VAR so that #ifdef VAR would be included."
896 (interactive "SDefine what? ")
898 (if hide-ifdef-hiding
(hide-ifdefs)))
900 (defun hide-ifdef-undef (var)
901 "Undefine a VAR so that #ifdef VAR would not be included."
902 (interactive "SUndefine what? ")
903 (hif-set-var var nil
)
904 (if hide-ifdef-hiding
(hide-ifdefs)))
907 (defun hide-ifdefs (&optional nomsg
)
908 "Hide the contents of some #ifdefs.
909 Assume that defined symbols have been added to `hide-ifdef-env'.
910 The text hidden is the text that would not be included by the C
911 preprocessor if it were given the file with those symbols defined.
913 Turn off hiding by calling `show-ifdefs'."
916 (message "Hiding...")
917 (setq hif-outside-read-only buffer-read-only
)
918 (unless hide-ifdef-mode
(hide-ifdef-mode 1)) ; turn on hide-ifdef-mode
919 (if hide-ifdef-hiding
920 (show-ifdefs)) ; Otherwise, deep confusion.
921 (setq hide-ifdef-hiding t
)
923 (setq buffer-read-only
(or hide-ifdef-read-only hif-outside-read-only
))
925 (message "Hiding done")))
928 (defun show-ifdefs ()
929 "Cancel the effects of `hide-ifdef': show the contents of all #ifdefs."
931 (setq buffer-read-only hif-outside-read-only
)
933 (setq hide-ifdef-hiding nil
))
936 (defun hif-find-ifdef-block ()
937 "Utility for hide and show `ifdef-block'.
938 Return as (TOP . BOTTOM) the extent of ifdef block."
940 (cons (save-excursion
942 (unless (or (hif-looking-at-else) (hif-looking-at-ifX))
946 (setq max-bottom
(1- (point)))))
949 (unless (hif-looking-at-endif)
950 (hif-find-next-relevant))
951 (while (hif-looking-at-ifX)
953 (hif-find-next-relevant))
954 (min max-bottom
(1- (point)))))))
957 (defun hide-ifdef-block ()
958 "Hide the ifdef block (true or false part) enclosing or before the cursor."
960 (unless hide-ifdef-mode
(hide-ifdef-mode 1))
961 (let ((top-bottom (hif-find-ifdef-block)))
962 (hide-ifdef-region (car top-bottom
) (cdr top-bottom
))
963 (when hide-ifdef-lines
964 (hif-hide-line (car top-bottom
))
965 (hif-hide-line (1+ (cdr top-bottom
))))
966 (setq hide-ifdef-hiding t
))
967 (setq buffer-read-only
(or hide-ifdef-read-only hif-outside-read-only
)))
969 (defun show-ifdef-block ()
970 "Show the ifdef block (true or false part) enclosing or before the cursor."
972 (let ((top-bottom (hif-find-ifdef-block)))
974 (hif-show-ifdef-region
976 (goto-char (car top-bottom
)) (line-beginning-position))
978 (goto-char (1+ (cdr top-bottom
)))
979 (hif-end-of-line) (point)))
980 (hif-show-ifdef-region (1- (car top-bottom
)) (cdr top-bottom
)))))
983 ;;; definition alist support
985 (defvar hide-ifdef-define-alist nil
986 "A global assoc list of pre-defined symbol lists.")
988 (defun hif-compress-define-list (env)
989 "Compress the define list ENV into a list of defined symbols only."
990 (let ((new-defs nil
))
991 (dolist (def env new-defs
)
992 (if (hif-lookup (car def
)) (push (car env
) new-defs
)))))
994 (defun hide-ifdef-set-define-alist (name)
995 "Set the association for NAME to `hide-ifdef-env'."
996 (interactive "SSet define list: ")
997 (push (cons name
(hif-compress-define-list hide-ifdef-env
))
998 hide-ifdef-define-alist
))
1000 (defun hide-ifdef-use-define-alist (name)
1001 "Set `hide-ifdef-env' to the define list specified by NAME."
1003 (list (completing-read "Use define list: "
1004 (mapcar (lambda (x) (symbol-name (car x
)))
1005 hide-ifdef-define-alist
)
1007 (if (stringp name
) (setq name
(intern name
)))
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))))
1018 ;; arch-tag: c6381d17-a59a-483a-b945-658f22277981
1019 ;;; hideif.el ends here