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