Fix more minor mode docstrings.
[emacs.git] / lisp / progmodes / hideif.el
blob374a45acec65538b9c20d113b2c7e1151a695ec5
1 ;;; hideif.el --- hides selected code within ifdef
3 ;; Copyright (C) 1988, 1994, 2001-2011 Free Software Foundation, Inc.
5 ;; Author: Brian Marick
6 ;; Daniel LaLiberte <liberte@holonexus.org>
7 ;; Maintainer: FSF
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 of the License, or
15 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; To initialize, toggle the hide-ifdef minor mode with
29 ;; M-x hide-ifdef-mode
31 ;; This will set up key bindings and call hide-ifdef-mode-hook if it
32 ;; has a value. To explicitly hide ifdefs using a buffer-local
33 ;; define list (default empty), type
35 ;; M-x hide-ifdefs or C-c @ h
37 ;; Hide-ifdef suppresses the display of code that the preprocessor wouldn't
38 ;; pass through. The support of constant expressions in #if lines is
39 ;; limited to identifiers, parens, and the operators: &&, ||, !, and
40 ;; "defined". Please extend this.
42 ;; The hidden code is marked by ellipses (...). Be
43 ;; cautious when editing near ellipses, since the hidden text is
44 ;; still in the buffer, and you can move the point into it and modify
45 ;; text unawares.
46 ;; You can make your buffer read-only while hide-ifdef-hiding by setting
47 ;; hide-ifdef-read-only to a non-nil value. You can toggle this
48 ;; variable with hide-ifdef-toggle-read-only (C-c @ C-q).
50 ;; You can undo the effect of hide-ifdefs by typing
52 ;; M-x show-ifdefs or C-c @ s
54 ;; Use M-x hide-ifdef-define (C-c @ d) to define a symbol.
55 ;; Use M-x hide-ifdef-undef (C-c @ u) to undefine a symbol.
57 ;; If you define or undefine a symbol while hide-ifdef-mode is in effect,
58 ;; the display will be updated. Only the define list for the current
59 ;; buffer will be affected. You can save changes to the local define
60 ;; list with hide-ifdef-set-define-alist. This adds entries
61 ;; to hide-ifdef-define-alist.
63 ;; If you have defined a hide-ifdef-mode-hook, you can set
64 ;; up a list of symbols that may be used by hide-ifdefs as in the
65 ;; following example:
67 ;; (add-hook 'hide-ifdef-mode-hook
68 ;; (lambda ()
69 ;; (unless hide-ifdef-define-alist
70 ;; (setq hide-ifdef-define-alist
71 ;; '((list1 ONE TWO)
72 ;; (list2 TWO THREE))))
73 ;; (hide-ifdef-use-define-alist 'list2))) ; use list2 by default
75 ;; You can call hide-ifdef-use-define-alist (C-c @ U) at any time to specify
76 ;; another list to use.
78 ;; To cause ifdefs to be hidden as soon as hide-ifdef-mode is called,
79 ;; set hide-ifdef-initially to non-nil.
81 ;; If you set hide-ifdef-lines to t, hide-ifdefs hides all the #ifdef lines.
82 ;; In the absence of highlighting, that might be a bad idea. If you set
83 ;; hide-ifdef-lines to nil (the default), the surrounding preprocessor
84 ;; lines will be displayed. That can be confusing in its own
85 ;; right. Other variations on display are possible, but not much
86 ;; better.
88 ;; You can explicitly hide or show individual ifdef blocks irrespective
89 ;; of the define list by using hide-ifdef-block and show-ifdef-block.
91 ;; You can move the point between ifdefs with forward-ifdef, backward-ifdef,
92 ;; up-ifdef, down-ifdef, next-ifdef, and previous-ifdef.
94 ;; If you have minor-mode-alist in your mode line (the default) two labels
95 ;; may appear. "Ifdef" will appear when hide-ifdef-mode is active. "Hiding"
96 ;; will appear when text may be hidden ("hide-ifdef-hiding" is non-nil).
98 ;; Written by Brian Marick, at Gould, Computer Systems Division, Urbana IL.
99 ;; Extensively modified by Daniel LaLiberte (while at Gould).
101 ;;; Code:
103 (require 'cc-mode)
105 (defgroup hide-ifdef nil
106 "Hide selected code within `ifdef'."
107 :group 'c)
109 (defcustom hide-ifdef-initially nil
110 "Non-nil means call `hide-ifdefs' when Hide-Ifdef mode is first activated."
111 :type 'boolean
112 :group 'hide-ifdef)
114 (defcustom hide-ifdef-read-only nil
115 "Set to non-nil if you want buffer to be read-only while hiding text."
116 :type 'boolean
117 :group 'hide-ifdef)
119 (defcustom hide-ifdef-lines nil
120 "Non-nil means hide the #ifX, #else, and #endif lines."
121 :type 'boolean
122 :group 'hide-ifdef)
124 (defcustom hide-ifdef-shadow nil
125 "Non-nil means shadow text instead of hiding it."
126 :type 'boolean
127 :group 'hide-ifdef
128 :version "23.1")
130 (defface hide-ifdef-shadow '((t (:inherit shadow)))
131 "Face for shadowing ifdef blocks."
132 :group 'hide-ifdef
133 :version "23.1")
136 (defvar hide-ifdef-mode-submap
137 ;; Set up the submap that goes after the prefix key.
138 (let ((map (make-sparse-keymap)))
139 (define-key map "d" 'hide-ifdef-define)
140 (define-key map "u" 'hide-ifdef-undef)
141 (define-key map "D" 'hide-ifdef-set-define-alist)
142 (define-key map "U" 'hide-ifdef-use-define-alist)
144 (define-key map "h" 'hide-ifdefs)
145 (define-key map "s" 'show-ifdefs)
146 (define-key map "\C-d" 'hide-ifdef-block)
147 (define-key map "\C-s" 'show-ifdef-block)
149 (define-key map "\C-q" 'hide-ifdef-toggle-read-only)
150 (define-key map "\C-w" 'hide-ifdef-toggle-shadowing)
151 (substitute-key-definition
152 'toggle-read-only 'hide-ifdef-toggle-outside-read-only map)
153 map)
154 "Keymap used by `hide-ifdef-mode' under `hide-ifdef-mode-prefix-key'.")
156 (defconst hide-ifdef-mode-prefix-key "\C-c@"
157 "Prefix key for all Hide-Ifdef mode commands.")
159 (defvar hide-ifdef-mode-map
160 ;; Set up the mode's main map, which leads via the prefix key to the submap.
161 (let ((map (make-sparse-keymap)))
162 (define-key map hide-ifdef-mode-prefix-key hide-ifdef-mode-submap)
163 map)
164 "Keymap used with `hide-ifdef-mode'.")
166 (easy-menu-define hide-ifdef-mode-menu hide-ifdef-mode-map
167 "Menu for `hide-ifdef-mode'."
168 '("Hide-Ifdef"
169 ["Hide some ifdefs" hide-ifdefs
170 :help "Hide the contents of some #ifdefs"]
171 ["Show all ifdefs" show-ifdefs
172 :help "Cancel the effects of `hide-ifdef': show the contents of all #ifdefs"]
173 ["Hide ifdef block" hide-ifdef-block
174 :help "Hide the ifdef block (true or false part) enclosing or before the cursor"]
175 ["Show ifdef block" show-ifdef-block
176 :help "Show the ifdef block (true or false part) enclosing or before the cursor"]
177 ["Define a variable..." hide-ifdef-define
178 :help "Define a VAR so that #ifdef VAR would be included"]
179 ["Undefine a variable..." hide-ifdef-undef
180 :help "Undefine a VAR so that #ifdef VAR would not be included"]
181 ["Define an alist..." hide-ifdef-set-define-alist
182 :help "Set the association for NAME to `hide-ifdef-env'"]
183 ["Use an alist..." hide-ifdef-use-define-alist
184 :help "Set `hide-ifdef-env' to the define list specified by NAME"]
185 ["Toggle read only" hide-ifdef-toggle-read-only
186 :style toggle :selected hide-ifdef-read-only
187 :help "Buffer should be read-only while hiding text"]
188 ["Toggle shadowing" hide-ifdef-toggle-shadowing
189 :style toggle :selected hide-ifdef-shadow
190 :help "Text should be shadowed instead of hidden"]))
192 (defvar hide-ifdef-hiding nil
193 "Non-nil when text may be hidden.")
195 (or (assq 'hide-ifdef-hiding minor-mode-alist)
196 (setq minor-mode-alist
197 (cons '(hide-ifdef-hiding " Hiding")
198 minor-mode-alist)))
200 ;; fix c-mode syntax table so we can recognize whole symbols.
201 (defvar hide-ifdef-syntax-table
202 (let ((st (copy-syntax-table c-mode-syntax-table)))
203 (modify-syntax-entry ?_ "w" st)
204 (modify-syntax-entry ?& "." st)
205 (modify-syntax-entry ?\| "." st)
207 "Syntax table used for tokenizing #if expressions.")
209 (defvar hide-ifdef-env nil
210 "An alist of defined symbols and their values.")
212 (defvar hif-outside-read-only nil
213 "Internal variable. Saves the value of `buffer-read-only' while hiding.")
215 ;;;###autoload
216 (define-minor-mode hide-ifdef-mode
217 "Toggle features to hide/show #ifdef blocks (Hide-Ifdef mode).
218 With a prefix argument ARG, enable Hide-Ifdef mode if ARG is
219 positive, and disable it otherwise. If called from Lisp, enable
220 the mode if ARG is omitted or nil.
222 Hide-Ifdef mode is a buffer-local minor mode for use with C and
223 C-like major modes. When enabled, code within #ifdef constructs
224 that the C preprocessor would eliminate may be hidden from view.
225 Several variables affect how the hiding is done:
227 `hide-ifdef-env'
228 An association list of defined and undefined symbols for the
229 current buffer. Initially, the global value of `hide-ifdef-env'
230 is used.
232 `hide-ifdef-define-alist'
233 An association list of defined symbol lists.
234 Use `hide-ifdef-set-define-alist' to save the current `hide-ifdef-env'
235 and `hide-ifdef-use-define-alist' to set the current `hide-ifdef-env'
236 from one of the lists in `hide-ifdef-define-alist'.
238 `hide-ifdef-lines'
239 Set to non-nil to not show #if, #ifdef, #ifndef, #else, and
240 #endif lines when hiding.
242 `hide-ifdef-initially'
243 Indicates whether `hide-ifdefs' should be called when Hide-Ifdef mode
244 is activated.
246 `hide-ifdef-read-only'
247 Set to non-nil if you want to make buffers read only while hiding.
248 After `show-ifdefs', read-only status is restored to previous value.
250 \\{hide-ifdef-mode-map}"
251 :group 'hide-ifdef :lighter " Ifdef"
252 (if hide-ifdef-mode
253 (progn
254 ;; inherit global values
255 (set (make-local-variable 'hide-ifdef-env)
256 (default-value 'hide-ifdef-env))
257 (set (make-local-variable 'hide-ifdef-hiding)
258 (default-value 'hide-ifdef-hiding))
259 (set (make-local-variable 'hif-outside-read-only) buffer-read-only)
260 (set (make-local-variable 'line-move-ignore-invisible) t)
261 (add-hook 'change-major-mode-hook
262 (lambda () (hide-ifdef-mode -1)) nil t)
264 (add-to-invisibility-spec '(hide-ifdef . t))
266 (if hide-ifdef-initially
267 (hide-ifdefs)
268 (show-ifdefs)))
269 ;; else end hide-ifdef-mode
270 (kill-local-variable 'line-move-ignore-invisible)
271 (remove-from-invisibility-spec '(hide-ifdef . t))
272 (when hide-ifdef-hiding
273 (show-ifdefs))))
276 (defun hif-show-all ()
277 "Show all of the text in the current buffer."
278 (interactive)
279 (hif-show-ifdef-region (point-min) (point-max)))
281 ;; By putting this on after-revert-hook, we arrange that it only
282 ;; does anything when revert-buffer avoids turning off the mode.
283 ;; (That can happen in VC.)
284 (defun hif-after-revert-function ()
285 (and hide-ifdef-mode hide-ifdef-hiding
286 (hide-ifdefs t)))
287 (add-hook 'after-revert-hook 'hif-after-revert-function)
289 (defun hif-end-of-line ()
290 (end-of-line)
291 (while (= (logand 1 (skip-chars-backward "\\\\")) 1)
292 (end-of-line 2)))
294 (defun hide-ifdef-region-internal (start end)
295 (remove-overlays start end 'hide-ifdef t)
296 (let ((o (make-overlay start end)))
297 (overlay-put o 'hide-ifdef t)
298 (if hide-ifdef-shadow
299 (overlay-put o 'face 'hide-ifdef-shadow)
300 (overlay-put o 'invisible 'hide-ifdef))))
302 (defun hide-ifdef-region (start end)
303 "START is the start of a #if or #else form. END is the ending part.
304 Everything including these lines is made invisible."
305 (save-excursion
306 (goto-char start) (hif-end-of-line) (setq start (point))
307 (goto-char end) (hif-end-of-line) (setq end (point))
308 (hide-ifdef-region-internal start end)))
310 (defun hif-show-ifdef-region (start end)
311 "Everything between START and END is made visible."
312 (remove-overlays start end 'hide-ifdef t))
315 ;;===%%SF%% evaluation (Start) ===
317 ;; It is not useful to set this to anything but `eval'.
318 ;; In fact, the variable might as well be eliminated.
319 (defvar hide-ifdef-evaluator 'eval
320 "The function to use to evaluate a form.
321 The evaluator is given a canonical form and returns t if text under
322 that form should be displayed.")
324 (defvar hif-undefined-symbol nil
325 "...is by default considered to be false.")
328 (defun hif-set-var (var value)
329 "Prepend (var value) pair to hide-ifdef-env."
330 (setq hide-ifdef-env (cons (cons var value) hide-ifdef-env)))
333 (defun hif-lookup (var)
334 ;; (message "hif-lookup %s" var)
335 (let ((val (assoc var hide-ifdef-env)))
336 (if val
337 (cdr val)
338 hif-undefined-symbol)))
340 (defun hif-defined (var)
341 (if (assoc var hide-ifdef-env) 1 0))
343 ;;===%%SF%% evaluation (End) ===
347 ;;===%%SF%% parsing (Start) ===
348 ;;; The code that understands what ifs and ifdef in files look like.
350 (defconst hif-cpp-prefix "\\(^\\|\r\\)[ \t]*#[ \t]*")
351 (defconst hif-ifndef-regexp (concat hif-cpp-prefix "ifndef"))
352 (defconst hif-ifx-regexp (concat hif-cpp-prefix "if\\(n?def\\)?[ \t]+"))
353 (defconst hif-else-regexp (concat hif-cpp-prefix "else"))
354 (defconst hif-endif-regexp (concat hif-cpp-prefix "endif"))
355 (defconst hif-ifx-else-endif-regexp
356 (concat hif-ifx-regexp "\\|" hif-else-regexp "\\|" hif-endif-regexp))
358 ;; Used to store the current token and the whole token list during parsing.
359 ;; Only bound dynamically.
360 (defvar hif-token)
361 (defvar hif-token-list)
363 (defconst hif-token-alist
364 '(("||" . or)
365 ("&&" . and)
366 ("|" . hif-logior)
367 ("&" . hif-logand)
368 ("==" . equal)
369 ("!=" . hif-notequal)
370 ("!" . not)
371 ("(" . lparen)
372 (")" . rparen)
373 (">" . hif-greater)
374 ("<" . hif-less)
375 (">=" . hif-greater-equal)
376 ("<=" . hif-less-equal)
377 ("+" . hif-plus)
378 ("-" . hif-minus)
379 ("?" . hif-conditional)
380 (":" . hif-colon)))
382 (defconst hif-token-regexp
383 (concat (regexp-opt (mapcar 'car hif-token-alist)) "\\|\\w+"))
385 (defun hif-tokenize (start end)
386 "Separate string between START and END into a list of tokens."
387 (let ((token-list nil))
388 (with-syntax-table hide-ifdef-syntax-table
389 (save-excursion
390 (goto-char start)
391 (while (progn (forward-comment (point-max)) (< (point) end))
392 ;; (message "expr-start = %d" expr-start) (sit-for 1)
393 (cond
394 ((looking-at "\\\\\n")
395 (forward-char 2))
397 ((looking-at hif-token-regexp)
398 (let ((token (buffer-substring (point) (match-end 0))))
399 (goto-char (match-end 0))
400 ;; (message "token: %s" token) (sit-for 1)
401 (push (or (cdr (assoc token hif-token-alist))
402 (if (string-equal token "defined") 'hif-defined)
403 (if (string-match "\\`[0-9]*\\'" token)
404 (string-to-number token))
405 (intern token))
406 token-list)))
407 (t (error "Bad #if expression: %s" (buffer-string)))))))
408 (nreverse token-list)))
410 ;;;-----------------------------------------------------------------
411 ;;; Translate C preprocessor #if expressions using recursive descent.
412 ;;; This parser is limited to the operators &&, ||, !, and "defined".
413 ;;; Added ==, !=, +, and -. Gary Oberbrunner, garyo@avs.com, 8/9/94
415 (defsubst hif-nexttoken ()
416 "Pop the next token from token-list into the let variable \"hif-token\"."
417 (setq hif-token (pop hif-token-list)))
419 (defun hif-parse-if-exp (token-list)
420 "Parse the TOKEN-LIST. Return translated list in prefix form."
421 (let ((hif-token-list token-list))
422 (hif-nexttoken)
423 (prog1
424 (hif-expr)
425 (if hif-token ; is there still a token?
426 (error "Error: unexpected token: %s" hif-token)))))
428 (defun hif-expr ()
429 "Parse an expression as found in #if.
430 expr : or-expr | or-expr '?' expr ':' expr."
431 (let ((result (hif-or-expr))
432 middle)
433 (while (eq hif-token 'hif-conditional)
434 (hif-nexttoken)
435 (setq middle (hif-expr))
436 (if (eq hif-token 'hif-colon)
437 (progn
438 (hif-nexttoken)
439 (setq result (list 'hif-conditional result middle (hif-expr))))
440 (error "Error: unexpected token: %s" hif-token)))
441 result))
443 (defun hif-or-expr ()
444 "Parse n or-expr : and-expr | or-expr '||' and-expr."
445 (let ((result (hif-and-expr)))
446 (while (eq hif-token 'or)
447 (hif-nexttoken)
448 (setq result (list 'hif-or result (hif-and-expr))))
449 result))
451 (defun hif-and-expr ()
452 "Parse an and-expr : eq-expr | and-expr '&&' eq-expr."
453 (let ((result (hif-eq-expr)))
454 (while (eq hif-token 'and)
455 (hif-nexttoken)
456 (setq result (list 'hif-and result (hif-eq-expr))))
457 result))
459 (defun hif-eq-expr ()
460 "Parse an eq-expr : math | eq-expr `=='|`!='|`<'|`>'|`>='|`<=' math."
461 (let ((result (hif-math))
462 (eq-token nil))
463 (while (memq hif-token '(equal hif-notequal hif-greater hif-less
464 hif-greater-equal hif-less-equal))
465 (setq eq-token hif-token)
466 (hif-nexttoken)
467 (setq result (list eq-token result (hif-math))))
468 result))
470 (defun hif-math ()
471 "Parse an expression with + or - and simpler things.
472 math : factor | math '+|-' factor."
473 (let ((result (hif-factor))
474 (math-op nil))
475 (while (memq hif-token '(hif-plus hif-minus hif-logior hif-logand))
476 (setq math-op hif-token)
477 (hif-nexttoken)
478 (setq result (list math-op result (hif-factor))))
479 result))
481 (defun hif-factor ()
482 "Parse a factor: '!' factor | '(' expr ')' | 'defined(' id ')' | id."
483 (cond
484 ((eq hif-token 'not)
485 (hif-nexttoken)
486 (list 'hif-not (hif-factor)))
488 ((eq hif-token 'lparen)
489 (hif-nexttoken)
490 (let ((result (hif-expr)))
491 (if (not (eq hif-token 'rparen))
492 (error "Bad token in parenthesized expression: %s" hif-token)
493 (hif-nexttoken)
494 result)))
496 ((eq hif-token 'hif-defined)
497 (hif-nexttoken)
498 (let ((paren (when (eq hif-token 'lparen) (hif-nexttoken) t))
499 (ident hif-token))
500 (if (memq hif-token '(or and not hif-defined lparen rparen))
501 (error "Error: unexpected token: %s" hif-token))
502 (when paren
503 (hif-nexttoken)
504 (unless (eq hif-token 'rparen)
505 (error "Error: expected \")\" after identifier")))
506 (hif-nexttoken)
507 `(hif-defined (quote ,ident))))
509 ((numberp hif-token)
510 (prog1 hif-token (hif-nexttoken)))
512 ;; Unary plus/minus.
513 ((memq hif-token '(hif-minus hif-plus))
514 (list (prog1 hif-token (hif-nexttoken)) 0 (hif-factor)))
516 (t ; identifier
517 (let ((ident hif-token))
518 (if (memq ident '(or and))
519 (error "Error: missing identifier"))
520 (hif-nexttoken)
521 `(hif-lookup (quote ,ident))))))
523 (defun hif-mathify (val)
524 "Treat VAL as a number: if it's t or nil, use 1 or 0."
525 (cond ((eq val t) 1)
526 ((null val) 0)
527 (t val)))
529 (defun hif-conditional (a b c)
530 (if (not (zerop (hif-mathify a))) (hif-mathify b) (hif-mathify c)))
531 (defun hif-and (a b)
532 (and (not (zerop (hif-mathify a))) (not (zerop (hif-mathify b)))))
533 (defun hif-or (a b)
534 (or (not (zerop (hif-mathify a))) (not (zerop (hif-mathify b)))))
535 (defun hif-not (a)
536 (zerop (hif-mathify a)))
538 (defmacro hif-mathify-binop (fun)
539 `(lambda (a b)
540 ,(format "Like `%s' but treat t and nil as 1 and 0." fun)
541 (,fun (hif-mathify a) (hif-mathify b))))
543 (defalias 'hif-plus (hif-mathify-binop +))
544 (defalias 'hif-minus (hif-mathify-binop -))
545 (defalias 'hif-notequal (hif-mathify-binop /=))
546 (defalias 'hif-greater (hif-mathify-binop >))
547 (defalias 'hif-less (hif-mathify-binop <))
548 (defalias 'hif-greater-equal (hif-mathify-binop >=))
549 (defalias 'hif-less-equal (hif-mathify-binop <=))
550 (defalias 'hif-logior (hif-mathify-binop logior))
551 (defalias 'hif-logand (hif-mathify-binop logand))
553 ;;;----------- end of parser -----------------------
556 (defun hif-canonicalize ()
557 "When at beginning of #ifX, return a Lisp expression for its condition."
558 (save-excursion
559 (let ((negate (looking-at hif-ifndef-regexp)))
560 (re-search-forward hif-ifx-regexp)
561 (let* ((tokens (hif-tokenize (point)
562 (progn (hif-end-of-line) (point))))
563 (expr (hif-parse-if-exp tokens)))
564 ;; (message "hif-canonicalized: %s" expr)
565 (if negate
566 (list 'hif-not expr)
567 expr)))))
570 (defun hif-find-any-ifX ()
571 "Move to next #if..., or #ifndef, at point or after."
572 ;; (message "find ifX at %d" (point))
573 (prog1
574 (re-search-forward hif-ifx-regexp (point-max) t)
575 (beginning-of-line)))
578 (defun hif-find-next-relevant ()
579 "Move to next #if..., #else, or #endif, after the current line."
580 ;; (message "hif-find-next-relevant at %d" (point))
581 (end-of-line)
582 ;; avoid infinite recursion by only going to beginning of line if match found
583 (if (re-search-forward hif-ifx-else-endif-regexp (point-max) t)
584 (beginning-of-line)))
586 (defun hif-find-previous-relevant ()
587 "Move to previous #if..., #else, or #endif, before the current line."
588 ;; (message "hif-find-previous-relevant at %d" (point))
589 (beginning-of-line)
590 ;; avoid infinite recursion by only going to beginning of line if match found
591 (if (re-search-backward hif-ifx-else-endif-regexp (point-min) t)
592 (beginning-of-line)))
595 (defun hif-looking-at-ifX () ;; Should eventually see #if
596 (looking-at hif-ifx-regexp))
597 (defun hif-looking-at-endif ()
598 (looking-at hif-endif-regexp))
599 (defun hif-looking-at-else ()
600 (looking-at hif-else-regexp))
604 (defun hif-ifdef-to-endif ()
605 "If positioned at #ifX or #else form, skip to corresponding #endif."
606 ;; (message "hif-ifdef-to-endif at %d" (point)) (sit-for 1)
607 (hif-find-next-relevant)
608 (cond ((hif-looking-at-ifX)
609 (hif-ifdef-to-endif) ; find endif of nested if
610 (hif-ifdef-to-endif)) ; find outer endif or else
611 ((hif-looking-at-else)
612 (hif-ifdef-to-endif)) ; find endif following else
613 ((hif-looking-at-endif)
614 'done)
616 (error "Mismatched #ifdef #endif pair"))))
619 (defun hif-endif-to-ifdef ()
620 "If positioned at #endif form, skip backward to corresponding #ifX."
621 ;; (message "hif-endif-to-ifdef at %d" (point))
622 (let ((start (point)))
623 (hif-find-previous-relevant)
624 (if (= start (point))
625 (error "Mismatched #ifdef #endif pair")))
626 (cond ((hif-looking-at-endif)
627 (hif-endif-to-ifdef) ; find beginning of nested if
628 (hif-endif-to-ifdef)) ; find beginning of outer if or else
629 ((hif-looking-at-else)
630 (hif-endif-to-ifdef))
631 ((hif-looking-at-ifX)
632 'done)
633 (t))) ; never gets here
636 (defun forward-ifdef (&optional arg)
637 "Move point to beginning of line of the next ifdef-endif.
638 With argument, do this that many times."
639 (interactive "p")
640 (or arg (setq arg 1))
641 (if (< arg 0) (backward-ifdef (- arg))
642 (while (< 0 arg)
643 (setq arg (- arg))
644 (let ((start (point)))
645 (unless (hif-looking-at-ifX)
646 (hif-find-next-relevant))
647 (if (hif-looking-at-ifX)
648 (hif-ifdef-to-endif)
649 (goto-char start)
650 (error "No following #ifdef"))))))
653 (defun backward-ifdef (&optional arg)
654 "Move point to beginning of the previous ifdef-endif.
655 With argument, do this that many times."
656 (interactive "p")
657 (or arg (setq arg 1))
658 (if (< arg 0) (forward-ifdef (- arg))
659 (while (< 0 arg)
660 (setq arg (1- arg))
661 (beginning-of-line)
662 (let ((start (point)))
663 (unless (hif-looking-at-endif)
664 (hif-find-previous-relevant))
665 (if (hif-looking-at-endif)
666 (hif-endif-to-ifdef)
667 (goto-char start)
668 (error "No previous #ifdef"))))))
671 (defun down-ifdef ()
672 "Move point to beginning of nested ifdef or else-part."
673 (interactive)
674 (let ((start (point)))
675 (hif-find-next-relevant)
676 (if (or (hif-looking-at-ifX) (hif-looking-at-else))
678 (goto-char start)
679 (error "No following #ifdef"))))
682 (defun up-ifdef ()
683 "Move point to beginning of enclosing ifdef or else-part."
684 (interactive)
685 (beginning-of-line)
686 (let ((start (point)))
687 (unless (hif-looking-at-endif)
688 (hif-find-previous-relevant))
689 (if (hif-looking-at-endif)
690 (hif-endif-to-ifdef))
691 (if (= start (point))
692 (error "No previous #ifdef"))))
694 (defun next-ifdef (&optional arg)
695 "Move to the beginning of the next #ifX, #else, or #endif.
696 With argument, do this that many times."
697 (interactive "p")
698 (or arg (setq arg 1))
699 (if (< arg 0) (previous-ifdef (- arg))
700 (while (< 0 arg)
701 (setq arg (1- arg))
702 (hif-find-next-relevant)
703 (when (eolp)
704 (beginning-of-line)
705 (error "No following #ifdefs, #elses, or #endifs")))))
707 (defun previous-ifdef (&optional arg)
708 "Move to the beginning of the previous #ifX, #else, or #endif.
709 With argument, do this that many times."
710 (interactive "p")
711 (or arg (setq arg 1))
712 (if (< arg 0) (next-ifdef (- arg))
713 (while (< 0 arg)
714 (setq arg (1- arg))
715 (let ((start (point)))
716 (hif-find-previous-relevant)
717 (if (= start (point))
718 (error "No previous #ifdefs, #elses, or #endifs"))))))
721 ;;===%%SF%% parsing (End) ===
724 ;;===%%SF%% hide-ifdef-hiding (Start) ===
727 ;;; A range is a structure with four components:
728 ;;; ELSE-P True if there was an else clause for the ifdef.
729 ;;; START The start of the range. (beginning of line)
730 ;;; ELSE The else marker (beginning of line)
731 ;;; Only valid if ELSE-P is true.
732 ;;; END The end of the range. (beginning of line)
734 (defsubst hif-make-range (start end &optional else)
735 (list start else end))
737 (defsubst hif-range-start (range) (elt range 0))
738 (defsubst hif-range-else (range) (elt range 1))
739 (defsubst hif-range-end (range) (elt range 2))
743 ;;; Find-Range
744 ;;; The workhorse, it delimits the #if region. Reasonably simple:
745 ;;; Skip until an #else or #endif is found, remembering positions. If
746 ;;; an #else was found, skip some more, looking for the true #endif.
748 (defun hif-find-range ()
749 "Return a Range structure describing the current #if region.
750 Point is left unchanged."
751 ;; (message "hif-find-range at %d" (point))
752 (save-excursion
753 (beginning-of-line)
754 (let ((start (point))
755 (else nil)
756 (end nil))
757 ;; Part one. Look for either #endif or #else.
758 ;; This loop-and-a-half dedicated to E. Dijkstra.
759 (while (progn
760 (hif-find-next-relevant)
761 (hif-looking-at-ifX)) ; Skip nested ifdef
762 (hif-ifdef-to-endif))
763 ;; Found either a #else or an #endif.
764 (cond ((hif-looking-at-else)
765 (setq else (point)))
767 (setq end (point)))) ; (line-end-position)
768 ;; If found #else, look for #endif.
769 (when else
770 (while (progn
771 (hif-find-next-relevant)
772 (hif-looking-at-ifX)) ; Skip nested ifdef
773 (hif-ifdef-to-endif))
774 (if (hif-looking-at-else)
775 (error "Found two elses in a row? Broken!"))
776 (setq end (point))) ; (line-end-position)
777 (hif-make-range start end else))))
780 ;;; A bit slimy.
782 (defun hif-hide-line (point)
783 "Hide the line containing point. Does nothing if `hide-ifdef-lines' is nil."
784 (when hide-ifdef-lines
785 (save-excursion
786 (goto-char point)
787 (hide-ifdef-region-internal
788 (line-beginning-position) (progn (hif-end-of-line) (point))))))
791 ;;; Hif-Possibly-Hide
792 ;;; There are four cases. The #ifX expression is "taken" if it
793 ;;; the hide-ifdef-evaluator returns T. Presumably, this means the code
794 ;;; inside the #ifdef would be included when the program was
795 ;;; compiled.
797 ;;; Case 1: #ifX taken, and there's an #else.
798 ;;; The #else part must be hidden. The #if (then) part must be
799 ;;; processed for nested #ifX's.
800 ;;; Case 2: #ifX taken, and there's no #else.
801 ;;; The #if part must be processed for nested #ifX's.
802 ;;; Case 3: #ifX not taken, and there's an #else.
803 ;;; The #if part must be hidden. The #else part must be processed
804 ;;; for nested #ifs.
805 ;;; Case 4: #ifX not taken, and there's no #else.
806 ;;; The #ifX part must be hidden.
808 ;;; Further processing is done by narrowing to the relevant region
809 ;;; and just recursively calling hide-ifdef-guts.
811 ;;; When hif-possibly-hide returns, point is at the end of the
812 ;;; possibly-hidden range.
814 (defun hif-recurse-on (start end)
815 "Call `hide-ifdef-guts' after narrowing to end of START line and END line."
816 (save-excursion
817 (save-restriction
818 (goto-char start)
819 (end-of-line)
820 (narrow-to-region (point) end)
821 (hide-ifdef-guts))))
823 (defun hif-possibly-hide ()
824 "Called at #ifX expression, this hides those parts that should be hidden.
825 It uses the judgement of `hide-ifdef-evaluator'."
826 ;; (message "hif-possibly-hide") (sit-for 1)
827 (let ((test (hif-canonicalize))
828 (range (hif-find-range)))
829 ;; (message "test = %s" test) (sit-for 1)
831 (hif-hide-line (hif-range-end range))
832 (if (not (hif-not (funcall hide-ifdef-evaluator test)))
833 (cond ((hif-range-else range) ; case 1
834 (hif-hide-line (hif-range-else range))
835 (hide-ifdef-region (hif-range-else range)
836 (1- (hif-range-end range)))
837 (hif-recurse-on (hif-range-start range)
838 (hif-range-else range)))
839 (t ; case 2
840 (hif-recurse-on (hif-range-start range)
841 (hif-range-end range))))
842 (cond ((hif-range-else range) ; case 3
843 (hif-hide-line (hif-range-else range))
844 (hide-ifdef-region (hif-range-start range)
845 (1- (hif-range-else range)))
846 (hif-recurse-on (hif-range-else range)
847 (hif-range-end range)))
848 (t ; case 4
849 (hide-ifdef-region (point)
850 (1- (hif-range-end range))))))
851 (hif-hide-line (hif-range-start range)) ; Always hide start.
852 (goto-char (hif-range-end range))
853 (end-of-line)))
857 (defun hide-ifdef-guts ()
858 "Does most of the work of `hide-ifdefs'.
859 It does not do the work that's pointless to redo on a recursive entry."
860 ;; (message "hide-ifdef-guts")
861 (save-excursion
862 (goto-char (point-min))
863 (while (hif-find-any-ifX)
864 (hif-possibly-hide))))
866 ;;===%%SF%% hide-ifdef-hiding (End) ===
869 ;;===%%SF%% exports (Start) ===
871 (defun hide-ifdef-toggle-read-only ()
872 "Toggle `hide-ifdef-read-only'."
873 (interactive)
874 (setq hide-ifdef-read-only (not hide-ifdef-read-only))
875 (message "Hide-Read-Only %s"
876 (if hide-ifdef-read-only "ON" "OFF"))
877 (if hide-ifdef-hiding
878 (setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only)))
879 (force-mode-line-update))
881 (defun hide-ifdef-toggle-outside-read-only ()
882 "Replacement for `toggle-read-only' within Hide-Ifdef mode."
883 (interactive)
884 (setq hif-outside-read-only (not hif-outside-read-only))
885 (message "Read only %s"
886 (if hif-outside-read-only "ON" "OFF"))
887 (setq buffer-read-only
888 (or (and hide-ifdef-hiding hide-ifdef-read-only)
889 hif-outside-read-only))
890 (force-mode-line-update))
892 (defun hide-ifdef-toggle-shadowing ()
893 "Toggle shadowing."
894 (interactive)
895 (set (make-local-variable 'hide-ifdef-shadow) (not hide-ifdef-shadow))
896 (message "Shadowing %s" (if hide-ifdef-shadow "ON" "OFF"))
897 (save-restriction
898 (widen)
899 (dolist (overlay (overlays-in (point-min) (point-max)))
900 (when (overlay-get overlay 'hide-ifdef)
901 (if hide-ifdef-shadow
902 (progn
903 (overlay-put overlay 'invisible nil)
904 (overlay-put overlay 'face 'hide-ifdef-shadow))
905 (overlay-put overlay 'face nil)
906 (overlay-put overlay 'invisible 'hide-ifdef))))))
908 (defun hide-ifdef-define (var)
909 "Define a VAR so that #ifdef VAR would be included."
910 (interactive "SDefine what? ")
911 (hif-set-var var 1)
912 (if hide-ifdef-hiding (hide-ifdefs)))
914 (defun hide-ifdef-undef (var)
915 "Undefine a VAR so that #ifdef VAR would not be included."
916 (interactive "SUndefine what? ")
917 (hif-set-var var nil)
918 (if hide-ifdef-hiding (hide-ifdefs)))
921 (defun hide-ifdefs (&optional nomsg)
922 "Hide the contents of some #ifdefs.
923 Assume that defined symbols have been added to `hide-ifdef-env'.
924 The text hidden is the text that would not be included by the C
925 preprocessor if it were given the file with those symbols defined.
927 Turn off hiding by calling `show-ifdefs'."
929 (interactive)
930 (message "Hiding...")
931 (setq hif-outside-read-only buffer-read-only)
932 (unless hide-ifdef-mode (hide-ifdef-mode 1)) ; turn on hide-ifdef-mode
933 (if hide-ifdef-hiding
934 (show-ifdefs)) ; Otherwise, deep confusion.
935 (setq hide-ifdef-hiding t)
936 (hide-ifdef-guts)
937 (setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only))
938 (or nomsg
939 (message "Hiding done")))
942 (defun show-ifdefs ()
943 "Cancel the effects of `hide-ifdef': show the contents of all #ifdefs."
944 (interactive)
945 (setq buffer-read-only hif-outside-read-only)
946 (hif-show-all)
947 (setq hide-ifdef-hiding nil))
950 (defun hif-find-ifdef-block ()
951 "Utility for hide and show `ifdef-block'.
952 Return as (TOP . BOTTOM) the extent of ifdef block."
953 (let (max-bottom)
954 (cons (save-excursion
955 (beginning-of-line)
956 (unless (or (hif-looking-at-else) (hif-looking-at-ifX))
957 (up-ifdef))
958 (prog1 (point)
959 (hif-ifdef-to-endif)
960 (setq max-bottom (1- (point)))))
961 (save-excursion
962 (beginning-of-line)
963 (unless (hif-looking-at-endif)
964 (hif-find-next-relevant))
965 (while (hif-looking-at-ifX)
966 (hif-ifdef-to-endif)
967 (hif-find-next-relevant))
968 (min max-bottom (1- (point)))))))
971 (defun hide-ifdef-block ()
972 "Hide the ifdef block (true or false part) enclosing or before the cursor."
973 (interactive)
974 (unless hide-ifdef-mode (hide-ifdef-mode 1))
975 (let ((top-bottom (hif-find-ifdef-block)))
976 (hide-ifdef-region (car top-bottom) (cdr top-bottom))
977 (when hide-ifdef-lines
978 (hif-hide-line (car top-bottom))
979 (hif-hide-line (1+ (cdr top-bottom))))
980 (setq hide-ifdef-hiding t))
981 (setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only)))
983 (defun show-ifdef-block ()
984 "Show the ifdef block (true or false part) enclosing or before the cursor."
985 (interactive)
986 (let ((top-bottom (hif-find-ifdef-block)))
987 (if hide-ifdef-lines
988 (hif-show-ifdef-region
989 (save-excursion
990 (goto-char (car top-bottom)) (line-beginning-position))
991 (save-excursion
992 (goto-char (1+ (cdr top-bottom)))
993 (hif-end-of-line) (point)))
994 (hif-show-ifdef-region (1- (car top-bottom)) (cdr top-bottom)))))
997 ;;; definition alist support
999 (defvar hide-ifdef-define-alist nil
1000 "A global assoc list of pre-defined symbol lists.")
1002 (defun hif-compress-define-list (env)
1003 "Compress the define list ENV into a list of defined symbols only."
1004 (let ((new-defs nil))
1005 (dolist (def env new-defs)
1006 (if (hif-lookup (car def)) (push (car env) new-defs)))))
1008 (defun hide-ifdef-set-define-alist (name)
1009 "Set the association for NAME to `hide-ifdef-env'."
1010 (interactive "SSet define list: ")
1011 (push (cons name (hif-compress-define-list hide-ifdef-env))
1012 hide-ifdef-define-alist))
1014 (defun hide-ifdef-use-define-alist (name)
1015 "Set `hide-ifdef-env' to the define list specified by NAME."
1016 (interactive
1017 (list (completing-read "Use define list: "
1018 (mapcar (lambda (x) (symbol-name (car x)))
1019 hide-ifdef-define-alist)
1020 nil t)))
1021 (if (stringp name) (setq name (intern name)))
1022 (let ((define-list (assoc name hide-ifdef-define-alist)))
1023 (if define-list
1024 (setq hide-ifdef-env
1025 (mapcar (lambda (arg) (cons arg t))
1026 (cdr define-list)))
1027 (error "No define list for %s" name))
1028 (if hide-ifdef-hiding (hide-ifdefs))))
1030 (provide 'hideif)
1032 ;;; hideif.el ends here