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).
103 ;; You may freely modify and distribute this, but keep a record
104 ;; of modifications and send comments to:
105 ;; liberte@a.cs.uiuc.edu or ihnp4!uiucdcs!liberte
106 ;; I will continue to upgrade hide-ifdef-mode
107 ;; with your contributions.
113 (defgroup hide-ifdef nil
114 "Hide selected code within `ifdef'."
117 (defcustom hide-ifdef-initially nil
118 "Non-nil means call `hide-ifdefs' when Hide-Ifdef mode is first activated."
122 (defcustom hide-ifdef-read-only nil
123 "Set to non-nil if you want buffer to be read-only while hiding text."
127 (defcustom hide-ifdef-lines nil
128 "Non-nil means hide the #ifX, #else, and #endif lines."
132 (defcustom hide-ifdef-shadow nil
133 "Non-nil means shadow text instead of hiding it."
138 (defface hide-ifdef-shadow
'((t (:inherit shadow
)))
139 "Face for shadowing ifdef blocks."
144 (defvar hide-ifdef-mode-submap
145 ;; Set up the submap that goes after the prefix key.
146 (let ((map (make-sparse-keymap)))
147 (define-key map
"d" 'hide-ifdef-define
)
148 (define-key map
"u" 'hide-ifdef-undef
)
149 (define-key map
"D" 'hide-ifdef-set-define-alist
)
150 (define-key map
"U" 'hide-ifdef-use-define-alist
)
152 (define-key map
"h" 'hide-ifdefs
)
153 (define-key map
"s" 'show-ifdefs
)
154 (define-key map
"\C-d" 'hide-ifdef-block
)
155 (define-key map
"\C-s" 'show-ifdef-block
)
157 (define-key map
"\C-q" 'hide-ifdef-toggle-read-only
)
158 (define-key map
"\C-w" 'hide-ifdef-toggle-shadowing
)
159 (substitute-key-definition
160 'toggle-read-only
'hide-ifdef-toggle-outside-read-only map
)
162 "Keymap used by `hide-ifdef-mode' under `hide-ifdef-mode-prefix-key'.")
164 (defconst hide-ifdef-mode-prefix-key
"\C-c@"
165 "Prefix key for all Hide-Ifdef mode commands.")
167 (defvar hide-ifdef-mode-map
168 ;; Set up the mode's main map, which leads via the prefix key to the submap.
169 (let ((map (make-sparse-keymap)))
170 (define-key map hide-ifdef-mode-prefix-key hide-ifdef-mode-submap
)
172 "Keymap used with `hide-ifdef-mode'.")
174 (easy-menu-define hide-ifdef-mode-menu hide-ifdef-mode-map
175 "Menu for `hide-ifdef-mode'."
177 ["Hide some ifdefs" hide-ifdefs t
]
178 ["Show all ifdefs" show-ifdefs t
]
179 ["Hide ifdef block" hide-ifdef-block t
]
180 ["Show ifdef block" show-ifdef-block t
]
181 ["Define a variable" hide-ifdef-define t
]
182 ["Define an alist" hide-ifdef-set-define-alist t
]
183 ["Use an alist" hide-ifdef-use-define-alist t
]
184 ["Undefine a variable" hide-ifdef-undef t
]
185 ["Toggle read only" hide-ifdef-toggle-read-only
186 :style toggle
:selected hide-ifdef-read-only
]
187 ["Toggle shadowing" hide-ifdef-toggle-shadowing
188 :style toggle
:selected hide-ifdef-shadow
]))
190 (defvar hide-ifdef-hiding nil
191 "Non-nil when text may be hidden.")
193 (or (assq 'hide-ifdef-hiding minor-mode-alist
)
194 (setq minor-mode-alist
195 (cons '(hide-ifdef-hiding " Hiding")
198 ;; fix c-mode syntax table so we can recognize whole symbols.
199 (defvar hide-ifdef-syntax-table
200 (let ((st (copy-syntax-table c-mode-syntax-table
)))
201 (modify-syntax-entry ?_
"w" st
)
202 (modify-syntax-entry ?
& "." st
)
203 (modify-syntax-entry ?\|
"." st
)
205 "Syntax table used for tokenizing #if expressions.")
207 (defvar hide-ifdef-env nil
208 "An alist of defined symbols and their values.")
210 (defvar hif-outside-read-only nil
211 "Internal variable. Saves the value of `buffer-read-only' while hiding.")
214 (define-minor-mode hide-ifdef-mode
215 "Toggle Hide-Ifdef mode. This is a minor mode, albeit a large one.
216 With ARG, turn Hide-Ifdef mode on if arg is positive, off otherwise.
217 In Hide-Ifdef mode, code within #ifdef constructs that the C preprocessor
218 would eliminate may be hidden from view. Several variables affect
219 how the hiding is done:
222 An association list of defined and undefined symbols for the
223 current buffer. Initially, the global value of `hide-ifdef-env'
226 `hide-ifdef-define-alist'
227 An association list of defined symbol lists.
228 Use `hide-ifdef-set-define-alist' to save the current `hide-ifdef-env'
229 and `hide-ifdef-use-define-alist' to set the current `hide-ifdef-env'
230 from one of the lists in `hide-ifdef-define-alist'.
233 Set to non-nil to not show #if, #ifdef, #ifndef, #else, and
234 #endif lines when hiding.
236 `hide-ifdef-initially'
237 Indicates whether `hide-ifdefs' should be called when Hide-Ifdef mode
240 `hide-ifdef-read-only'
241 Set to non-nil if you want to make buffers read only while hiding.
242 After `show-ifdefs', read-only status is restored to previous value.
244 \\{hide-ifdef-mode-map}"
245 :group
'hide-ifdef
:lighter
" Ifdef"
248 ;; inherit global values
249 (set (make-local-variable 'hide-ifdef-env
)
250 (default-value 'hide-ifdef-env
))
251 (set (make-local-variable 'hide-ifdef-hiding
)
252 (default-value 'hide-ifdef-hiding
))
253 (set (make-local-variable 'hif-outside-read-only
) buffer-read-only
)
254 (set (make-local-variable 'line-move-ignore-invisible
) t
)
255 (add-hook 'change-major-mode-hook
256 (lambda () (hide-ifdef-mode -
1)) nil t
)
258 (add-to-invisibility-spec '(hide-ifdef . t
))
260 (if hide-ifdef-initially
263 ;; else end hide-ifdef-mode
264 (kill-local-variable 'line-move-ignore-invisible
)
265 (remove-from-invisibility-spec '(hide-ifdef . t
))
266 (if hide-ifdef-hiding
270 (defun hif-show-all ()
271 "Show all of the text in the current buffer."
273 (hif-show-ifdef-region (point-min) (point-max)))
275 ;; By putting this on after-revert-hook, we arrange that it only
276 ;; does anything when revert-buffer avoids turning off the mode.
277 ;; (That can happen in VC.)
278 (defun hif-after-revert-function ()
279 (and hide-ifdef-mode hide-ifdef-hiding
281 (add-hook 'after-revert-hook
'hif-after-revert-function
)
283 (defun hif-end-of-line ()
285 (while (= (logand 1 (skip-chars-backward "\\\\")) 1)
288 (defun hide-ifdef-region-internal (start end
)
289 (remove-overlays start end
'hide-ifdef t
)
290 (let ((o (make-overlay start end
)))
291 (overlay-put o
'hide-ifdef t
)
292 (if hide-ifdef-shadow
293 (overlay-put o
'face
'hide-ifdef-shadow
)
294 (overlay-put o
'invisible
'hide-ifdef
))))
296 (defun hide-ifdef-region (start end
)
297 "START is the start of a #if or #else form. END is the ending part.
298 Everything including these lines is made invisible."
300 (goto-char start
) (hif-end-of-line) (setq start
(point))
301 (goto-char end
) (hif-end-of-line) (setq end
(point))
302 (hide-ifdef-region-internal start end
)))
304 (defun hif-show-ifdef-region (start end
)
305 "Everything between START and END is made visible."
306 (remove-overlays start end
'hide-ifdef t
))
309 ;;===%%SF%% evaluation (Start) ===
311 ;; It is not useful to set this to anything but `eval'.
312 ;; In fact, the variable might as well be eliminated.
313 (defvar hide-ifdef-evaluator
'eval
314 "The function to use to evaluate a form.
315 The evaluator is given a canonical form and returns t if text under
316 that form should be displayed.")
318 (defvar hif-undefined-symbol nil
319 "...is by default considered to be false.")
322 (defun hif-set-var (var value
)
323 "Prepend (var value) pair to hide-ifdef-env."
324 (setq hide-ifdef-env
(cons (cons var value
) hide-ifdef-env
)))
327 (defun hif-lookup (var)
328 ;; (message "hif-lookup %s" var)
329 (let ((val (assoc var hide-ifdef-env
)))
332 hif-undefined-symbol
)))
334 (defun hif-defined (var)
335 (if (assoc var hide-ifdef-env
) 1 0))
337 ;;===%%SF%% evaluation (End) ===
341 ;;===%%SF%% parsing (Start) ===
342 ;;; The code that understands what ifs and ifdef in files look like.
344 (defconst hif-cpp-prefix
"\\(^\\|\r\\)[ \t]*#[ \t]*")
345 (defconst hif-ifndef-regexp
(concat hif-cpp-prefix
"ifndef"))
346 (defconst hif-ifx-regexp
(concat hif-cpp-prefix
"if\\(n?def\\)?[ \t]+"))
347 (defconst hif-else-regexp
(concat hif-cpp-prefix
"else"))
348 (defconst hif-endif-regexp
(concat hif-cpp-prefix
"endif"))
349 (defconst hif-ifx-else-endif-regexp
350 (concat hif-ifx-regexp
"\\|" hif-else-regexp
"\\|" hif-endif-regexp
))
352 ;; Used to store the current token and the whole token list during parsing.
353 ;; Only bound dynamically.
355 (defvar hif-token-list
)
357 ;; pattern to match initial identifier, !, &&, ||, (, or ).
358 ;; Added ==, + and -: garyo@avs.com 8/9/94
359 (defconst hif-token-regexp
360 "\\(&&\\|||\\|[!=]=\\|!\\|[()+?:-]\\|[<>]=?\\|\\w+\\)")
362 (defun hif-tokenize (start end
)
363 "Separate string between START and END into a list of tokens."
364 (let ((token-list nil
))
365 (with-syntax-table hide-ifdef-syntax-table
368 (while (progn (forward-comment (point-max)) (< (point) end
))
369 ;; (message "expr-start = %d" expr-start) (sit-for 1)
371 ((looking-at "\\\\\n")
374 ((looking-at hif-token-regexp
)
375 (let ((token (buffer-substring (point) (match-end 0))))
376 (goto-char (match-end 0))
377 ;; (message "token: %s" token) (sit-for 1)
379 ((string-equal token
"||") 'or
)
380 ((string-equal token
"&&") 'and
)
381 ((string-equal token
"==") 'equal
)
382 ((string-equal token
"!=") 'hif-notequal
)
383 ((string-equal token
"!") 'not
)
384 ((string-equal token
"defined") 'hif-defined
)
385 ((string-equal token
"(") 'lparen
)
386 ((string-equal token
")") 'rparen
)
387 ((string-equal token
">") 'hif-greater
)
388 ((string-equal token
"<") 'hif-less
)
389 ((string-equal token
">=") 'hif-greater-equal
)
390 ((string-equal token
"<=") 'hif-less-equal
)
391 ((string-equal token
"+") 'hif-plus
)
392 ((string-equal token
"-") 'hif-minus
)
393 ((string-equal token
"?") 'hif-conditional
)
394 ((string-equal token
":") 'hif-colon
)
395 ((string-match "\\`[0-9]*\\'" token
)
396 (string-to-number token
))
399 (t (error "Bad #if expression: %s" (buffer-string)))))))
400 (nreverse token-list
)))
402 ;;;-----------------------------------------------------------------
403 ;;; Translate C preprocessor #if expressions using recursive descent.
404 ;;; This parser is limited to the operators &&, ||, !, and "defined".
405 ;;; Added ==, !=, +, and -. Gary Oberbrunner, garyo@avs.com, 8/9/94
407 (defsubst hif-nexttoken
()
408 "Pop the next token from token-list into the let variable \"hif-token\"."
409 (setq hif-token
(pop hif-token-list
)))
411 (defun hif-parse-if-exp (hif-token-list)
412 "Parse the TOKEN-LIST. Return translated list in prefix form."
416 (if hif-token
; is there still a token?
417 (error "Error: unexpected token: %s" hif-token
))))
420 "Parse an expression as found in #if.
421 expr : or-expr | or-expr '?' expr ':' expr."
422 (let ((result (hif-or-expr))
424 (while (eq hif-token
'hif-conditional
)
426 (setq middle
(hif-expr))
427 (if (eq hif-token
'hif-colon
)
430 (setq result
(list 'hif-conditional result middle
(hif-expr))))
431 (error "Error: unexpected token: %s" hif-token
)))
434 (defun hif-or-expr ()
435 "Parse n or-expr : and-expr | or-expr '||' and-expr."
436 (let ((result (hif-and-expr)))
437 (while (eq hif-token
'or
)
439 (setq result
(list 'hif-or result
(hif-and-expr))))
442 (defun hif-and-expr ()
443 "Parse an and-expr : eq-expr | and-expr '&&' eq-expr."
444 (let ((result (hif-eq-expr)))
445 (while (eq hif-token
'and
)
447 (setq result
(list 'hif-and result
(hif-eq-expr))))
450 (defun hif-eq-expr ()
451 "Parse an eq-expr : math | eq-expr `=='|`!='|`<'|`>'|`>='|`<=' math."
452 (let ((result (hif-math))
454 (while (memq hif-token
'(equal hif-notequal hif-greater hif-less
455 hif-greater-equal hif-less-equal
))
456 (setq eq-token hif-token
)
458 (setq result
(list eq-token result
(hif-math))))
462 "Parse an expression with + or - and simpler things.
463 math : factor | math '+|-' factor."
464 (let ((result (hif-factor))
466 (while (memq hif-token
'(hif-plus hif-minus
))
467 (setq math-op hif-token
)
469 (setq result
(list math-op result
(hif-factor))))
473 "Parse a factor: '!' factor | '(' expr ')' | 'defined(' id ')' | id."
477 (list 'hif-not
(hif-factor)))
479 ((eq hif-token
'lparen
)
481 (let ((result (hif-expr)))
482 (if (not (eq hif-token
'rparen
))
483 (error "Bad token in parenthesized expression: %s" hif-token
)
487 ((eq hif-token
'hif-defined
)
489 (let ((paren (when (eq hif-token
'lparen
) (hif-nexttoken) t
))
491 (if (memq hif-token
'(or and not hif-defined lparen rparen
))
492 (error "Error: unexpected token: %s" hif-token
))
495 (unless (eq hif-token
'rparen
)
496 (error "Error: expected \")\" after identifier")))
498 `(hif-defined (quote ,ident
))))
501 (prog1 hif-token
(hif-nexttoken)))
504 (let ((ident hif-token
))
505 (if (memq ident
'(or and
))
506 (error "Error: missing identifier"))
508 `(hif-lookup (quote ,ident
))))))
510 (defun hif-mathify (val)
511 "Treat VAL as a number: if it's t or nil, use 1 or 0."
516 (defun hif-conditional (a b c
)
517 (if (not (zerop (hif-mathify a
))) (hif-mathify b
) (hif-mathify c
)))
519 (and (not (zerop (hif-mathify a
))) (not (zerop (hif-mathify b
)))))
521 (or (not (zerop (hif-mathify a
))) (not (zerop (hif-mathify b
)))))
523 (zerop (hif-mathify a
)))
524 (defun hif-plus (a b
)
525 "Like ordinary plus but treat t and nil as 1 and 0."
526 (+ (hif-mathify a
) (hif-mathify b
)))
527 (defun hif-minus (a b
)
528 "Like ordinary minus but treat t and nil as 1 and 0."
529 (- (hif-mathify a
) (hif-mathify b
)))
530 (defun hif-notequal (a b
)
531 "Like (not (equal A B)) but as one symbol."
533 (defun hif-greater (a b
)
535 (> (hif-mathify a
) (hif-mathify b
)))
536 (defun hif-less (a b
)
538 (< (hif-mathify a
) (hif-mathify b
)))
539 (defun hif-greater-equal (a b
)
541 (>= (hif-mathify a
) (hif-mathify b
)))
542 (defun hif-less-equal (a b
)
544 (<= (hif-mathify a
) (hif-mathify b
)))
545 ;;;----------- end of parser -----------------------
548 (defun hif-canonicalize ()
549 "When at beginning of #ifX, return a Lisp expression for its condition."
551 (let ((negate (looking-at hif-ifndef-regexp
)))
552 (re-search-forward hif-ifx-regexp
)
553 (let* ((tokens (hif-tokenize (point)
554 (progn (hif-end-of-line) (point))))
555 (expr (hif-parse-if-exp tokens
)))
556 ;; (message "hif-canonicalized: %s" expr)
562 (defun hif-find-any-ifX ()
563 "Move to next #if..., or #ifndef, at point or after."
564 ;; (message "find ifX at %d" (point))
566 (re-search-forward hif-ifx-regexp
(point-max) t
)
567 (beginning-of-line)))
570 (defun hif-find-next-relevant ()
571 "Move to next #if..., #else, or #endif, after the current line."
572 ;; (message "hif-find-next-relevant at %d" (point))
574 ;; avoid infinite recursion by only going to beginning of line if match found
575 (if (re-search-forward hif-ifx-else-endif-regexp
(point-max) t
)
576 (beginning-of-line)))
578 (defun hif-find-previous-relevant ()
579 "Move to previous #if..., #else, or #endif, before the current line."
580 ;; (message "hif-find-previous-relevant at %d" (point))
582 ;; avoid infinite recursion by only going to beginning of line if match found
583 (if (re-search-backward hif-ifx-else-endif-regexp
(point-min) t
)
584 (beginning-of-line)))
587 (defun hif-looking-at-ifX () ;; Should eventually see #if
588 (looking-at hif-ifx-regexp
))
589 (defun hif-looking-at-endif ()
590 (looking-at hif-endif-regexp
))
591 (defun hif-looking-at-else ()
592 (looking-at hif-else-regexp
))
596 (defun hif-ifdef-to-endif ()
597 "If positioned at #ifX or #else form, skip to corresponding #endif."
598 ;; (message "hif-ifdef-to-endif at %d" (point)) (sit-for 1)
599 (hif-find-next-relevant)
600 (cond ((hif-looking-at-ifX)
601 (hif-ifdef-to-endif) ; find endif of nested if
602 (hif-ifdef-to-endif)) ; find outer endif or else
603 ((hif-looking-at-else)
604 (hif-ifdef-to-endif)) ; find endif following else
605 ((hif-looking-at-endif)
608 (error "Mismatched #ifdef #endif pair"))))
611 (defun hif-endif-to-ifdef ()
612 "If positioned at #endif form, skip backward to corresponding #ifX."
613 ;; (message "hif-endif-to-ifdef at %d" (point))
614 (let ((start (point)))
615 (hif-find-previous-relevant)
616 (if (= start
(point))
617 (error "Mismatched #ifdef #endif pair")))
618 (cond ((hif-looking-at-endif)
619 (hif-endif-to-ifdef) ; find beginning of nested if
620 (hif-endif-to-ifdef)) ; find beginning of outer if or else
621 ((hif-looking-at-else)
622 (hif-endif-to-ifdef))
623 ((hif-looking-at-ifX)
625 (t))) ; never gets here
628 (defun forward-ifdef (&optional arg
)
629 "Move point to beginning of line of the next ifdef-endif.
630 With argument, do this that many times."
632 (or arg
(setq arg
1))
633 (if (< arg
0) (backward-ifdef (- arg
))
636 (let ((start (point)))
637 (unless (hif-looking-at-ifX)
638 (hif-find-next-relevant))
639 (if (hif-looking-at-ifX)
642 (error "No following #ifdef"))))))
645 (defun backward-ifdef (&optional arg
)
646 "Move point to beginning of the previous ifdef-endif.
647 With argument, do this that many times."
649 (or arg
(setq arg
1))
650 (if (< arg
0) (forward-ifdef (- arg
))
654 (let ((start (point)))
655 (unless (hif-looking-at-endif)
656 (hif-find-previous-relevant))
657 (if (hif-looking-at-endif)
660 (error "No previous #ifdef"))))))
664 "Move point to beginning of nested ifdef or else-part."
666 (let ((start (point)))
667 (hif-find-next-relevant)
668 (if (or (hif-looking-at-ifX) (hif-looking-at-else))
671 (error "No following #ifdef"))))
675 "Move point to beginning of enclosing ifdef or else-part."
678 (let ((start (point)))
679 (unless (hif-looking-at-endif)
680 (hif-find-previous-relevant))
681 (if (hif-looking-at-endif)
682 (hif-endif-to-ifdef))
683 (if (= start
(point))
684 (error "No previous #ifdef"))))
686 (defun next-ifdef (&optional arg
)
687 "Move to the beginning of the next #ifX, #else, or #endif.
688 With argument, do this that many times."
690 (or arg
(setq arg
1))
691 (if (< arg
0) (previous-ifdef (- arg
))
694 (hif-find-next-relevant)
697 (error "No following #ifdefs, #elses, or #endifs")))))
699 (defun previous-ifdef (&optional arg
)
700 "Move to the beginning of the previous #ifX, #else, or #endif.
701 With argument, do this that many times."
703 (or arg
(setq arg
1))
704 (if (< arg
0) (next-ifdef (- arg
))
707 (let ((start (point)))
708 (hif-find-previous-relevant)
709 (if (= start
(point))
710 (error "No previous #ifdefs, #elses, or #endifs"))))))
713 ;;===%%SF%% parsing (End) ===
716 ;;===%%SF%% hide-ifdef-hiding (Start) ===
719 ;;; A range is a structure with four components:
720 ;;; ELSE-P True if there was an else clause for the ifdef.
721 ;;; START The start of the range. (beginning of line)
722 ;;; ELSE The else marker (beginning of line)
723 ;;; Only valid if ELSE-P is true.
724 ;;; END The end of the range. (beginning of line)
726 (defsubst hif-make-range
(start end
&optional else
)
727 (list start else end
))
729 (defsubst hif-range-start
(range) (elt range
0))
730 (defsubst hif-range-else
(range) (elt range
1))
731 (defsubst hif-range-end
(range) (elt range
2))
736 ;;; The workhorse, it delimits the #if region. Reasonably simple:
737 ;;; Skip until an #else or #endif is found, remembering positions. If
738 ;;; an #else was found, skip some more, looking for the true #endif.
740 (defun hif-find-range ()
741 "Return a Range structure describing the current #if region.
742 Point is left unchanged."
743 ;; (message "hif-find-range at %d" (point))
746 (let ((start (point))
749 ;; Part one. Look for either #endif or #else.
750 ;; This loop-and-a-half dedicated to E. Dijkstra.
752 (hif-find-next-relevant)
753 (hif-looking-at-ifX)) ; Skip nested ifdef
754 (hif-ifdef-to-endif))
755 ;; Found either a #else or an #endif.
756 (cond ((hif-looking-at-else)
759 (setq end
(point)))) ; (save-excursion (end-of-line) (point))
760 ;; If found #else, look for #endif.
763 (hif-find-next-relevant)
764 (hif-looking-at-ifX)) ; Skip nested ifdef
765 (hif-ifdef-to-endif))
766 (if (hif-looking-at-else)
767 (error "Found two elses in a row? Broken!"))
768 (setq end
(point))) ; (save-excursion (end-of-line) (point))
769 (hif-make-range start end else
))))
774 (defun hif-hide-line (point)
775 "Hide the line containing point. Does nothing if `hide-ifdef-lines' is nil."
776 (when hide-ifdef-lines
779 (hide-ifdef-region-internal
780 (line-beginning-position) (progn (hif-end-of-line) (point))))))
783 ;;; Hif-Possibly-Hide
784 ;;; There are four cases. The #ifX expression is "taken" if it
785 ;;; the hide-ifdef-evaluator returns T. Presumably, this means the code
786 ;;; inside the #ifdef would be included when the program was
789 ;;; Case 1: #ifX taken, and there's an #else.
790 ;;; The #else part must be hidden. The #if (then) part must be
791 ;;; processed for nested #ifX's.
792 ;;; Case 2: #ifX taken, and there's no #else.
793 ;;; The #if part must be processed for nested #ifX's.
794 ;;; Case 3: #ifX not taken, and there's an #else.
795 ;;; The #if part must be hidden. The #else part must be processed
797 ;;; Case 4: #ifX not taken, and there's no #else.
798 ;;; The #ifX part must be hidden.
800 ;;; Further processing is done by narrowing to the relevant region
801 ;;; and just recursively calling hide-ifdef-guts.
803 ;;; When hif-possibly-hide returns, point is at the end of the
804 ;;; possibly-hidden range.
806 (defun hif-recurse-on (start end
)
807 "Call `hide-ifdef-guts' after narrowing to end of START line and END line."
812 (narrow-to-region (point) end
)
815 (defun hif-possibly-hide ()
816 "Called at #ifX expression, this hides those parts that should be hidden.
817 It uses the judgement of `hide-ifdef-evaluator'."
818 ;; (message "hif-possibly-hide") (sit-for 1)
819 (let ((test (hif-canonicalize))
820 (range (hif-find-range)))
821 ;; (message "test = %s" test) (sit-for 1)
823 (hif-hide-line (hif-range-end range
))
824 (if (not (hif-not (funcall hide-ifdef-evaluator test
)))
825 (cond ((hif-range-else range
) ; case 1
826 (hif-hide-line (hif-range-else range
))
827 (hide-ifdef-region (hif-range-else range
)
828 (1- (hif-range-end range
)))
829 (hif-recurse-on (hif-range-start range
)
830 (hif-range-else range
)))
832 (hif-recurse-on (hif-range-start range
)
833 (hif-range-end range
))))
834 (cond ((hif-range-else range
) ; case 3
835 (hif-hide-line (hif-range-else range
))
836 (hide-ifdef-region (hif-range-start range
)
837 (1- (hif-range-else range
)))
838 (hif-recurse-on (hif-range-else range
)
839 (hif-range-end range
)))
841 (hide-ifdef-region (point)
842 (1- (hif-range-end range
))))))
843 (hif-hide-line (hif-range-start range
)) ; Always hide start.
844 (goto-char (hif-range-end range
))
849 (defun hide-ifdef-guts ()
850 "Does most of the work of `hide-ifdefs'.
851 It does not do the work that's pointless to redo on a recursive entry."
852 ;; (message "hide-ifdef-guts")
854 (goto-char (point-min))
855 (while (hif-find-any-ifX)
856 (hif-possibly-hide))))
858 ;;===%%SF%% hide-ifdef-hiding (End) ===
861 ;;===%%SF%% exports (Start) ===
863 (defun hide-ifdef-toggle-read-only ()
864 "Toggle `hide-ifdef-read-only'."
866 (setq hide-ifdef-read-only
(not hide-ifdef-read-only
))
867 (message "Hide-Read-Only %s"
868 (if hide-ifdef-read-only
"ON" "OFF"))
869 (if hide-ifdef-hiding
870 (setq buffer-read-only
(or hide-ifdef-read-only hif-outside-read-only
)))
871 (force-mode-line-update))
873 (defun hide-ifdef-toggle-outside-read-only ()
874 "Replacement for `toggle-read-only' within Hide-Ifdef mode."
876 (setq hif-outside-read-only
(not hif-outside-read-only
))
877 (message "Read only %s"
878 (if hif-outside-read-only
"ON" "OFF"))
879 (setq buffer-read-only
880 (or (and hide-ifdef-hiding hide-ifdef-read-only
)
881 hif-outside-read-only
))
882 (force-mode-line-update))
884 (defun hide-ifdef-toggle-shadowing ()
887 (set (make-local-variable 'hide-ifdef-shadow
) (not hide-ifdef-shadow
))
888 (message "Shadowing %s" (if hide-ifdef-shadow
"ON" "OFF"))
891 (dolist (overlay (overlays-in (point-min) (point-max)))
892 (when (overlay-get overlay
'hide-ifdef
)
893 (if hide-ifdef-shadow
895 (overlay-put overlay
'invisible nil
)
896 (overlay-put overlay
'face
'hide-ifdef-shadow
))
897 (overlay-put overlay
'face nil
)
898 (overlay-put overlay
'invisible
'hide-ifdef
))))))
900 (defun hide-ifdef-define (var)
901 "Define a VAR so that #ifdef VAR would be included."
902 (interactive "SDefine what? ")
904 (if hide-ifdef-hiding
(hide-ifdefs)))
906 (defun hide-ifdef-undef (var)
907 "Undefine a VAR so that #ifdef VAR would not be included."
908 (interactive "SUndefine what? ")
909 (hif-set-var var nil
)
910 (if hide-ifdef-hiding
(hide-ifdefs)))
913 (defun hide-ifdefs (&optional nomsg
)
914 "Hide the contents of some #ifdefs.
915 Assume that defined symbols have been added to `hide-ifdef-env'.
916 The text hidden is the text that would not be included by the C
917 preprocessor if it were given the file with those symbols defined.
919 Turn off hiding by calling `show-ifdefs'."
922 (message "Hiding...")
923 (setq hif-outside-read-only buffer-read-only
)
924 (unless hide-ifdef-mode
(hide-ifdef-mode 1)) ; turn on hide-ifdef-mode
925 (if hide-ifdef-hiding
926 (show-ifdefs)) ; Otherwise, deep confusion.
927 (setq hide-ifdef-hiding t
)
929 (setq buffer-read-only
(or hide-ifdef-read-only hif-outside-read-only
))
931 (message "Hiding done")))
934 (defun show-ifdefs ()
935 "Cancel the effects of `hide-ifdef': show the contents of all #ifdefs."
937 (setq buffer-read-only hif-outside-read-only
)
939 (setq hide-ifdef-hiding nil
))
942 (defun hif-find-ifdef-block ()
943 "Utility for hide and show `ifdef-block'.
944 Return as (TOP . BOTTOM) the extent of ifdef block."
946 (cons (save-excursion
948 (unless (or (hif-looking-at-else) (hif-looking-at-ifX))
952 (setq max-bottom
(1- (point)))))
955 (unless (hif-looking-at-endif)
956 (hif-find-next-relevant))
957 (while (hif-looking-at-ifX)
959 (hif-find-next-relevant))
960 (min max-bottom
(1- (point)))))))
963 (defun hide-ifdef-block ()
964 "Hide the ifdef block (true or false part) enclosing or before the cursor."
966 (unless hide-ifdef-mode
(hide-ifdef-mode 1))
967 (let ((top-bottom (hif-find-ifdef-block)))
968 (hide-ifdef-region (car top-bottom
) (cdr top-bottom
))
969 (when hide-ifdef-lines
970 (hif-hide-line (car top-bottom
))
971 (hif-hide-line (1+ (cdr top-bottom
))))
972 (setq hide-ifdef-hiding t
))
973 (setq buffer-read-only
(or hide-ifdef-read-only hif-outside-read-only
)))
975 (defun show-ifdef-block ()
976 "Show the ifdef block (true or false part) enclosing or before the cursor."
978 (let ((top-bottom (hif-find-ifdef-block)))
980 (hif-show-ifdef-region
982 (goto-char (car top-bottom
)) (line-beginning-position))
984 (goto-char (1+ (cdr top-bottom
)))
985 (hif-end-of-line) (point)))
986 (hif-show-ifdef-region (1- (car top-bottom
)) (cdr top-bottom
)))))
989 ;;; definition alist support
991 (defvar hide-ifdef-define-alist nil
992 "A global assoc list of pre-defined symbol lists.")
994 (defun hif-compress-define-list (env)
995 "Compress the define list ENV into a list of defined symbols only."
996 (let ((new-defs nil
))
997 (dolist (def env new-defs
)
998 (if (hif-lookup (car def
)) (push (car env
) new-defs
)))))
1000 (defun hide-ifdef-set-define-alist (name)
1001 "Set the association for NAME to `hide-ifdef-env'."
1002 (interactive "SSet define list: ")
1003 (push (cons name
(hif-compress-define-list hide-ifdef-env
))
1004 hide-ifdef-define-alist
))
1006 (defun hide-ifdef-use-define-alist (name)
1007 "Set `hide-ifdef-env' to the define list specified by NAME."
1009 (list (completing-read "Use define list: "
1010 (mapcar (lambda (x) (symbol-name (car x
)))
1011 hide-ifdef-define-alist
)
1013 (if (stringp name
) (setq name
(intern name
)))
1014 (let ((define-list (assoc name hide-ifdef-define-alist
)))
1016 (setq hide-ifdef-env
1017 (mapcar (lambda (arg) (cons arg t
))
1019 (error "No define list for %s" name
))
1020 (if hide-ifdef-hiding
(hide-ifdefs))))
1024 ;; arch-tag: c6381d17-a59a-483a-b945-658f22277981
1025 ;;; hideif.el ends here