1 ;;; hideif.el --- hides selected code within ifdef
3 ;; Copyright (C) 1988, 1994, 2001-2014 Free Software Foundation, Inc.
5 ;; Author: Brian Marick
6 ;; Daniel LaLiberte <liberte@holonexus.org>
7 ;; Maintainer: emacs-devel@gnu.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 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/>.
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. Support complete C/C++ expression and precedence.
40 ;; The hidden code is marked by ellipses (...). Be
41 ;; cautious when editing near ellipses, since the hidden text is
42 ;; still in the buffer, and you can move the point into it and modify
44 ;; You can make your buffer read-only while hide-ifdef-hiding by setting
45 ;; hide-ifdef-read-only to a non-nil value. You can toggle this
46 ;; variable with hide-ifdef-toggle-read-only (C-c @ C-q).
48 ;; You can undo the effect of hide-ifdefs by typing
50 ;; M-x show-ifdefs or C-c @ s
52 ;; Use M-x hide-ifdef-define (C-c @ d) to define a symbol.
53 ;; Use M-x hide-ifdef-undef (C-c @ u) to undefine a symbol.
55 ;; If you define or undefine a symbol while hide-ifdef-mode is in effect,
56 ;; the display will be updated. Only the define list for the current
57 ;; buffer will be affected. You can save changes to the local define
58 ;; list with hide-ifdef-set-define-alist. This adds entries
59 ;; to hide-ifdef-define-alist.
61 ;; If you have defined a hide-ifdef-mode-hook, you can set
62 ;; up a list of symbols that may be used by hide-ifdefs as in the
65 ;; (add-hook 'hide-ifdef-mode-hook
67 ;; (unless hide-ifdef-define-alist
68 ;; (setq hide-ifdef-define-alist
70 ;; (list2 TWO THREE))))
71 ;; (hide-ifdef-use-define-alist 'list2))) ; use list2 by default
73 ;; You can call hide-ifdef-use-define-alist (C-c @ U) at any time to specify
74 ;; another list to use.
76 ;; To cause ifdefs to be hidden as soon as hide-ifdef-mode is called,
77 ;; set hide-ifdef-initially to non-nil.
79 ;; If you set hide-ifdef-lines to t, hide-ifdefs hides all the #ifdef lines.
80 ;; In the absence of highlighting, that might be a bad idea. If you set
81 ;; hide-ifdef-lines to nil (the default), the surrounding preprocessor
82 ;; lines will be displayed. That can be confusing in its own
83 ;; right. Other variations on display are possible, but not much
86 ;; You can explicitly hide or show individual ifdef blocks irrespective
87 ;; of the define list by using hide-ifdef-block and show-ifdef-block.
89 ;; You can move the point between ifdefs with forward-ifdef, backward-ifdef,
90 ;; up-ifdef, down-ifdef, next-ifdef, and previous-ifdef.
92 ;; If you have minor-mode-alist in your mode line (the default) two labels
93 ;; may appear. "Ifdef" will appear when hide-ifdef-mode is active. "Hiding"
94 ;; will appear when text may be hidden ("hide-ifdef-hiding" is non-nil).
96 ;; Written by Brian Marick, at Gould, Computer Systems Division, Urbana IL.
97 ;; Extensively modified by Daniel LaLiberte (while at Gould).
99 ;; Extensively modified by Luke Lee in 2013 to support complete C expression
106 (defgroup hide-ifdef nil
107 "Hide selected code within `ifdef'."
110 (defcustom hide-ifdef-initially nil
111 "Non-nil means call `hide-ifdefs' when Hide-Ifdef mode is first activated."
115 (defcustom hide-ifdef-read-only nil
116 "Set to non-nil if you want buffer to be read-only while hiding text."
120 (defcustom hide-ifdef-lines nil
121 "Non-nil means hide the #ifX, #else, and #endif lines."
125 (defcustom hide-ifdef-shadow nil
126 "Non-nil means shadow text instead of hiding it."
131 (defface hide-ifdef-shadow
'((t (:inherit shadow
)))
132 "Face for shadowing ifdef blocks."
137 (defvar hide-ifdef-mode-submap
138 ;; Set up the submap that goes after the prefix key.
139 (let ((map (make-sparse-keymap)))
140 (define-key map
"d" 'hide-ifdef-define
)
141 (define-key map
"u" 'hide-ifdef-undef
)
142 (define-key map
"D" 'hide-ifdef-set-define-alist
)
143 (define-key map
"U" 'hide-ifdef-use-define-alist
)
145 (define-key map
"h" 'hide-ifdefs
)
146 (define-key map
"s" 'show-ifdefs
)
147 (define-key map
"\C-d" 'hide-ifdef-block
)
148 (define-key map
"\C-s" 'show-ifdef-block
)
150 (define-key map
"\C-q" 'hide-ifdef-toggle-read-only
)
151 (define-key map
"\C-w" 'hide-ifdef-toggle-shadowing
)
152 (substitute-key-definition
153 'toggle-read-only
'hide-ifdef-toggle-outside-read-only map
)
155 "Keymap used by `hide-ifdef-mode' under `hide-ifdef-mode-prefix-key'.")
157 (defconst hide-ifdef-mode-prefix-key
"\C-c@"
158 "Prefix key for all Hide-Ifdef mode commands.")
160 (defvar hide-ifdef-mode-map
161 ;; Set up the mode's main map, which leads via the prefix key to the submap.
162 (let ((map (make-sparse-keymap)))
163 (define-key map hide-ifdef-mode-prefix-key hide-ifdef-mode-submap
)
165 "Keymap used with `hide-ifdef-mode'.")
167 (easy-menu-define hide-ifdef-mode-menu hide-ifdef-mode-map
168 "Menu for `hide-ifdef-mode'."
170 ["Hide some ifdefs" hide-ifdefs
171 :help
"Hide the contents of some #ifdefs"]
172 ["Show all ifdefs" show-ifdefs
173 :help
"Cancel the effects of `hide-ifdef': show the contents of all #ifdefs"]
174 ["Hide ifdef block" hide-ifdef-block
175 :help
"Hide the ifdef block (true or false part) enclosing or before the cursor"]
176 ["Show ifdef block" show-ifdef-block
177 :help
"Show the ifdef block (true or false part) enclosing or before the cursor"]
178 ["Define a variable..." hide-ifdef-define
179 :help
"Define a VAR so that #ifdef VAR would be included"]
180 ["Undefine a variable..." hide-ifdef-undef
181 :help
"Undefine a VAR so that #ifdef VAR would not be included"]
182 ["Define an alist..." hide-ifdef-set-define-alist
183 :help
"Set the association for NAME to `hide-ifdef-env'"]
184 ["Use an alist..." hide-ifdef-use-define-alist
185 :help
"Set `hide-ifdef-env' to the define list specified by NAME"]
186 ["Toggle read only" hide-ifdef-toggle-read-only
187 :style toggle
:selected hide-ifdef-read-only
188 :help
"Buffer should be read-only while hiding text"]
189 ["Toggle shadowing" hide-ifdef-toggle-shadowing
190 :style toggle
:selected hide-ifdef-shadow
191 :help
"Text should be shadowed instead of hidden"]))
193 (defvar hide-ifdef-hiding nil
194 "Non-nil when text may be hidden.")
196 (or (assq 'hide-ifdef-hiding minor-mode-alist
)
197 (setq minor-mode-alist
198 (cons '(hide-ifdef-hiding " Hiding")
201 ;; fix c-mode syntax table so we can recognize whole symbols.
202 (defvar hide-ifdef-syntax-table
203 (let ((st (copy-syntax-table c-mode-syntax-table
)))
204 (modify-syntax-entry ?_
"w" st
)
205 (modify-syntax-entry ?
& "." st
)
206 (modify-syntax-entry ?\|
"." st
)
208 "Syntax table used for tokenizing #if expressions.")
210 (defvar hide-ifdef-env nil
211 "An alist of defined symbols and their values.")
213 (defvar hif-outside-read-only nil
214 "Internal variable. Saves the value of `buffer-read-only' while hiding.")
217 (define-minor-mode hide-ifdef-mode
218 "Toggle features to hide/show #ifdef blocks (Hide-Ifdef mode).
219 With a prefix argument ARG, enable Hide-Ifdef mode if ARG is
220 positive, and disable it otherwise. If called from Lisp, enable
221 the mode if ARG is omitted or nil.
223 Hide-Ifdef mode is a buffer-local minor mode for use with C and
224 C-like major modes. When enabled, code within #ifdef constructs
225 that the C preprocessor would eliminate may be hidden from view.
226 Several variables affect how the hiding is done:
229 An association list of defined and undefined symbols for the
230 current buffer. Initially, the global value of `hide-ifdef-env'
233 `hide-ifdef-define-alist'
234 An association list of defined symbol lists.
235 Use `hide-ifdef-set-define-alist' to save the current `hide-ifdef-env'
236 and `hide-ifdef-use-define-alist' to set the current `hide-ifdef-env'
237 from one of the lists in `hide-ifdef-define-alist'.
240 Set to non-nil to not show #if, #ifdef, #ifndef, #else, and
241 #endif lines when hiding.
243 `hide-ifdef-initially'
244 Indicates whether `hide-ifdefs' should be called when Hide-Ifdef mode
247 `hide-ifdef-read-only'
248 Set to non-nil if you want to make buffers read only while hiding.
249 After `show-ifdefs', read-only status is restored to previous value.
251 \\{hide-ifdef-mode-map}"
252 :group
'hide-ifdef
:lighter
" Ifdef"
255 ;; inherit global values
256 (set (make-local-variable 'hide-ifdef-env
)
257 (default-value 'hide-ifdef-env
))
258 (set (make-local-variable 'hide-ifdef-hiding
)
259 (default-value 'hide-ifdef-hiding
))
260 (set (make-local-variable 'hif-outside-read-only
) buffer-read-only
)
261 (set (make-local-variable 'line-move-ignore-invisible
) t
)
262 (add-hook 'change-major-mode-hook
263 (lambda () (hide-ifdef-mode -
1)) nil t
)
265 (add-to-invisibility-spec '(hide-ifdef . t
))
267 (if hide-ifdef-initially
270 ;; else end hide-ifdef-mode
271 (kill-local-variable 'line-move-ignore-invisible
)
272 (remove-from-invisibility-spec '(hide-ifdef . t
))
273 (when hide-ifdef-hiding
277 (defun hif-show-all ()
278 "Show all of the text in the current buffer."
280 (hif-show-ifdef-region (point-min) (point-max)))
282 ;; By putting this on after-revert-hook, we arrange that it only
283 ;; does anything when revert-buffer avoids turning off the mode.
284 ;; (That can happen in VC.)
285 (defun hif-after-revert-function ()
286 (and hide-ifdef-mode hide-ifdef-hiding
288 (add-hook 'after-revert-hook
'hif-after-revert-function
)
290 (defun hif-end-of-line ()
292 (while (= (logand 1 (skip-chars-backward "\\\\")) 1)
295 (defun hide-ifdef-region-internal (start end
)
296 (remove-overlays start end
'hide-ifdef t
)
297 (let ((o (make-overlay start end
)))
298 (overlay-put o
'hide-ifdef t
)
299 (if hide-ifdef-shadow
300 (overlay-put o
'face
'hide-ifdef-shadow
)
301 (overlay-put o
'invisible
'hide-ifdef
))))
303 (defun hide-ifdef-region (start end
)
304 "START is the start of a #if or #else form. END is the ending part.
305 Everything including these lines is made invisible."
307 (goto-char start
) (hif-end-of-line) (setq start
(point))
308 (goto-char end
) (hif-end-of-line) (setq end
(point))
309 (hide-ifdef-region-internal start end
)))
311 (defun hif-show-ifdef-region (start end
)
312 "Everything between START and END is made visible."
313 (remove-overlays start end
'hide-ifdef t
))
316 ;;===%%SF%% evaluation (Start) ===
318 ;; It is not useful to set this to anything but `eval'.
319 ;; In fact, the variable might as well be eliminated.
320 (defvar hide-ifdef-evaluator
'eval
321 "The function to use to evaluate a form.
322 The evaluator is given a canonical form and returns t if text under
323 that form should be displayed.")
325 (defvar hif-undefined-symbol nil
326 "...is by default considered to be false.")
329 (defun hif-set-var (var value
)
330 "Prepend (var value) pair to `hide-ifdef-env'."
331 (setq hide-ifdef-env
(cons (cons var value
) hide-ifdef-env
)))
333 (declare-function semantic-c-hideif-lookup
"semantic/bovine/c" (var))
334 (declare-function semantic-c-hideif-defined
"semantic/bovine/c" (var))
336 (defun hif-lookup (var)
337 (or (when (bound-and-true-p semantic-c-takeover-hideif
)
338 (semantic-c-hideif-lookup var
))
339 (let ((val (assoc var hide-ifdef-env
)))
342 hif-undefined-symbol
))))
344 (defun hif-defined (var)
346 ((bound-and-true-p semantic-c-takeover-hideif
)
347 (semantic-c-hideif-defined var
))
348 ((assoc var hide-ifdef-env
) 1)
351 ;;===%%SF%% evaluation (End) ===
355 ;;===%%SF%% parsing (Start) ===
356 ;;; The code that understands what ifs and ifdef in files look like.
358 (defconst hif-cpp-prefix
"\\(^\\|\r\\)[ \t]*#[ \t]*")
359 (defconst hif-ifndef-regexp
(concat hif-cpp-prefix
"ifndef"))
360 (defconst hif-ifx-regexp
(concat hif-cpp-prefix
"if\\(n?def\\)?[ \t]+"))
361 (defconst hif-else-regexp
(concat hif-cpp-prefix
"else"))
362 (defconst hif-endif-regexp
(concat hif-cpp-prefix
"endif"))
363 (defconst hif-ifx-else-endif-regexp
364 (concat hif-ifx-regexp
"\\|" hif-else-regexp
"\\|" hif-endif-regexp
))
366 ;; Used to store the current token and the whole token list during parsing.
367 ;; Only bound dynamically.
369 (defvar hif-token-list
)
371 (defconst hif-token-alist
377 ("<<" . hif-shiftleft
)
378 (">>" . hif-shiftright
)
380 ;; Note: we include tokens like `=' which aren't supported by CPP's
381 ;; expression syntax, because they are still relevant for the tokenizer,
382 ;; especially in conjunction with ##.
384 ("!=" . hif-notequal
)
385 ("##" . hif-token-concat
)
392 (">=" . hif-greater-equal
)
393 ("<=" . hif-less-equal
)
399 ("?" . hif-conditional
)
402 (defconst hif-token-regexp
403 (concat (regexp-opt (mapcar 'car hif-token-alist
))
404 "\\|0x[0-9a-fA-F]+\\.?[0-9a-fA-F]*"
405 "\\|[0-9]+\\.?[0-9]*" ;; decimal/octal
408 (defconst hif-string-literal-regexp
"\\(\"\\(?:[^\"\\]\\|\\\\.\\)*\"\\)")
410 (defun hif-string-to-number (string &optional base
)
411 "Like `string-to-number', but it understands non-decimal floats."
412 (if (or (not base
) (= base
10))
413 (string-to-number string base
)
414 (let* ((parts (split-string string
"\\." t
"[ \t]+"))
416 (quot (expt (* base
1.0) (length frac
))))
417 (/ (string-to-number (concat (car parts
) frac
) base
) quot
))))
419 (defun hif-tokenize (start end
)
420 "Separate string between START and END into a list of tokens."
421 (let ((token-list nil
))
422 (with-syntax-table hide-ifdef-syntax-table
425 (while (progn (forward-comment (point-max)) (< (point) end
))
426 ;; (message "expr-start = %d" expr-start) (sit-for 1)
428 ((looking-at "\\\\\n")
431 ((looking-at hif-string-literal-regexp
)
432 (push (substring-no-properties (match-string 1)) token-list
)
433 (goto-char (match-end 0)))
434 ((looking-at hif-token-regexp
)
435 (let ((token (buffer-substring (point) (match-end 0))))
436 (goto-char (match-end 0))
437 ;; (message "token: %s" token) (sit-for 1)
439 (or (cdr (assoc token hif-token-alist
))
440 (if (string-equal token
"defined") 'hif-defined
)
442 ;; 1. postfix 'l', 'll', 'ul' and 'ull'
443 ;; 2. floating number formats
444 ;; 3. 098 is interpreted as octal conversion error
445 (if (string-match "0x\\([0-9a-fA-F]+\\.?[0-9a-fA-F]*\\)"
447 (hif-string-to-number (match-string 1 token
) 16)) ;; hex
448 (if (string-match "\\`0[0-9]+\\(\\.[0-9]+\\)?\\'" token
)
449 (hif-string-to-number token
8)) ;; octal
450 (if (string-match "\\`[1-9][0-9]*\\(\\.[0-9]+\\)?\\'"
452 (string-to-number token
)) ;; decimal
455 (t (error "Bad #if expression: %s" (buffer-string)))))))
456 (nreverse token-list
)))
458 ;;------------------------------------------------------------------------
459 ;; Translate C preprocessor #if expressions using recursive descent.
460 ;; This parser was limited to the operators &&, ||, !, and "defined".
461 ;; Added ==, !=, +, and -. Gary Oberbrunner, garyo@avs.com, 8/9/94
463 ;; Implement the C language operator precedence table. Add all those
464 ;; missing operators that could be used in macros. Luke Lee 2013-09-04
466 ;; | Operator Type | Operator | Associativity |
467 ;; +----------------------+-----------------------------+---------------+
468 ;; | Primary Expression | () [] . -> expr++ expr-- | left-to-right |
469 ;; | Unary Operators | * & + - ! ~ ++expr --expr | right-to-left |
470 ;; | | (typecast) sizeof | |
471 ;; | Binary Operators | * / % | left-to-right |
481 ;; | Ternary Operator | ?: | right-to-left |
482 ;; x| Assignment Operators | = += -= *= /= %= >>= <<= &= | right-to-left |
484 ;; | Comma | , | left-to-right |
486 (defsubst hif-nexttoken
()
487 "Pop the next token from token-list into the let variable `hif-token'."
488 (setq hif-token
(pop hif-token-list
)))
490 (defun hif-parse-if-exp (token-list)
491 "Parse the TOKEN-LIST. Return translated list in prefix form."
492 (let ((hif-token-list token-list
))
497 (if hif-token
; is there still a token?
498 (error "Error: unexpected token: %s" hif-token
)))))
500 (defun hif-exprlist ()
501 "Parse an exprlist: expr { ',' expr}"
502 (let ((result (hif-expr)))
503 (if (eq hif-token
'hif-comma
)
504 (let ((temp (list result
)))
508 (push (hif-expr) temp
)
509 (eq hif-token
'hif-comma
)))
510 (cons 'hif-comma
(nreverse temp
)))
514 "Parse an expression as found in #if.
515 expr : or-expr | or-expr '?' expr ':' expr."
516 (let ((result (hif-or-expr))
518 (while (eq hif-token
'hif-conditional
)
520 (setq middle
(hif-expr))
521 (if (eq hif-token
'hif-colon
)
524 (setq result
(list 'hif-conditional result middle
(hif-expr))))
525 (error "Error: unexpected token: %s" hif-token
)))
528 (defun hif-or-expr ()
529 "Parse an or-expr : and-expr | or-expr '||' and-expr."
530 (let ((result (hif-and-expr)))
531 (while (eq hif-token
'hif-or
)
533 (setq result
(list 'hif-or result
(hif-and-expr))))
536 (defun hif-and-expr ()
537 "Parse an and-expr : logior-expr | and-expr '&&' logior-expr."
538 (let ((result (hif-logior-expr)))
539 (while (eq hif-token
'hif-and
)
541 (setq result
(list 'hif-and result
(hif-logior-expr))))
544 (defun hif-logior-expr ()
545 "Parse a logor-expr : logxor-expr | logor-expr '|' logxor-expr."
546 (let ((result (hif-logxor-expr)))
547 (while (eq hif-token
'hif-logior
)
549 (setq result
(list 'hif-logior result
(hif-logxor-expr))))
552 (defun hif-logxor-expr ()
553 "Parse a logxor-expr : logand-expr | logxor-expr '^' logand-expr."
554 (let ((result (hif-logand-expr)))
555 (while (eq hif-token
'hif-logxor
)
557 (setq result
(list 'hif-logxor result
(hif-logand-expr))))
560 (defun hif-logand-expr ()
561 "Parse a logand-expr : eq-expr | logand-expr '&' eq-expr."
562 (let ((result (hif-eq-expr)))
563 (while (eq hif-token
'hif-logand
)
565 (setq result
(list 'hif-logand result
(hif-eq-expr))))
568 (defun hif-eq-expr ()
569 "Parse an eq-expr : comp | eq-expr `=='|`!=' comp."
570 (let ((result (hif-comp-expr))
572 (while (memq hif-token
'(hif-equal hif-notequal
))
573 (setq eq-token hif-token
)
575 (setq result
(list eq-token result
(hif-comp-expr))))
578 (defun hif-comp-expr ()
579 "Parse a comp-expr : logshift | comp-expr `<'|`>'|`>='|`<=' logshift."
580 (let ((result (hif-logshift-expr))
582 (while (memq hif-token
'(hif-greater hif-less hif-greater-equal hif-less-equal
))
583 (setq comp-token hif-token
)
585 (setq result
(list comp-token result
(hif-logshift-expr))))
588 (defun hif-logshift-expr ()
589 "Parse a logshift : math | logshift `<<'|`>>' math."
590 (let ((result (hif-math))
592 (while (memq hif-token
'(hif-shiftleft hif-shiftright
))
593 (setq shift-token hif-token
)
595 (setq result
(list shift-token result
(hif-math))))
599 "Parse an expression with + or -.
600 math : muldiv | math '+|-' muldiv."
601 (let ((result (hif-muldiv-expr))
603 (while (memq hif-token
'(hif-plus hif-minus
))
604 (setq math-op hif-token
)
606 (setq result
(list math-op result
(hif-muldiv-expr))))
609 (defun hif-muldiv-expr ()
610 "Parse an expression with *,/,%.
611 muldiv : factor | muldiv '*|/|%' factor."
612 (let ((result (hif-factor))
614 (while (memq hif-token
'(hif-multiply hif-divide hif-modulo
))
615 (setq math-op hif-token
)
617 (setq result
(list math-op result
(hif-factor))))
621 "Parse a factor: '!' factor | '~' factor | '(' expr ')' | 'defined(' id ')' | 'id(parmlist)' | strings | id."
623 ((eq hif-token
'hif-not
)
625 (list 'hif-not
(hif-factor)))
627 ((eq hif-token
'hif-lognot
)
629 (list 'hif-lognot
(hif-factor)))
631 ((eq hif-token
'hif-lparen
)
633 (let ((result (hif-exprlist)))
634 (if (not (eq hif-token
'hif-rparen
))
635 (error "Bad token in parenthesized expression: %s" hif-token
)
639 ((eq hif-token
'hif-defined
)
641 (let ((paren (when (eq hif-token
'hif-lparen
) (hif-nexttoken) t
))
643 (if (memq hif-token
'(or and not hif-defined hif-lparen hif-rparen
))
644 (error "Error: unexpected token: %s" hif-token
))
647 (unless (eq hif-token
'hif-rparen
)
648 (error "Error: expected \")\" after identifier")))
650 `(hif-defined (quote ,ident
))))
653 (prog1 hif-token
(hif-nexttoken)))
656 ((memq hif-token
'(hif-minus hif-plus
))
657 (list (prog1 hif-token
(hif-nexttoken)) 0 (hif-factor)))
660 (let ((ident hif-token
))
661 (if (memq ident
'(or and
))
662 (error "Error: missing identifier"))
664 `(hif-lookup (quote ,ident
))))))
666 (defun hif-mathify (val)
667 "Treat VAL as a number: if it's t or nil, use 1 or 0."
672 (defun hif-conditional (a b c
)
673 (if (not (zerop (hif-mathify a
))) (hif-mathify b
) (hif-mathify c
)))
675 (and (not (zerop (hif-mathify a
))) (not (zerop (hif-mathify b
)))))
677 (or (not (zerop (hif-mathify a
))) (not (zerop (hif-mathify b
)))))
679 (zerop (hif-mathify a
)))
680 (defun hif-lognot (a)
681 (lognot (hif-mathify a
)))
683 (defmacro hif-mathify-binop
(fun)
685 ,(format "Like `%s' but treat t and nil as 1 and 0." fun
)
686 (,fun
(hif-mathify a
) (hif-mathify b
))))
688 (defun hif-shiftleft (a b
)
689 (setq a
(hif-mathify a
))
690 (setq b
(hif-mathify b
))
695 (defun hif-shiftright (a b
)
696 (setq a
(hif-mathify a
))
697 (setq b
(hif-mathify b
))
703 (defalias 'hif-multiply
(hif-mathify-binop *))
704 (defalias 'hif-divide
(hif-mathify-binop /))
705 (defalias 'hif-modulo
(hif-mathify-binop %
))
706 (defalias 'hif-plus
(hif-mathify-binop +))
707 (defalias 'hif-minus
(hif-mathify-binop -
))
708 (defalias 'hif-equal
(hif-mathify-binop =))
709 (defalias 'hif-notequal
(hif-mathify-binop /=))
710 (defalias 'hif-greater
(hif-mathify-binop >))
711 (defalias 'hif-less
(hif-mathify-binop <))
712 (defalias 'hif-greater-equal
(hif-mathify-binop >=))
713 (defalias 'hif-less-equal
(hif-mathify-binop <=))
714 (defalias 'hif-logior
(hif-mathify-binop logior
))
715 (defalias 'hif-logxor
(hif-mathify-binop logxor
))
716 (defalias 'hif-logand
(hif-mathify-binop logand
))
719 (defun hif-comma (&rest expr
)
720 "Evaluate a list of expr, return the result of the last item."
724 (setq result
(funcall hide-ifdef-evaluator e
))))
728 ;;;----------- end of parser -----------------------
731 (defun hif-canonicalize ()
732 "When at beginning of #ifX, return a Lisp expression for its condition."
734 (let ((negate (looking-at hif-ifndef-regexp
)))
735 (re-search-forward hif-ifx-regexp
)
736 (let* ((tokens (hif-tokenize (point)
737 (progn (hif-end-of-line) (point))))
738 (expr (hif-parse-if-exp tokens
)))
739 ;; (message "hif-canonicalized: %s" expr)
745 (defun hif-find-any-ifX ()
746 "Move to next #if..., or #ifndef, at point or after."
747 ;; (message "find ifX at %d" (point))
749 (re-search-forward hif-ifx-regexp
(point-max) t
)
750 (beginning-of-line)))
753 (defun hif-find-next-relevant ()
754 "Move to next #if..., #else, or #endif, after the current line."
755 ;; (message "hif-find-next-relevant at %d" (point))
757 ;; avoid infinite recursion by only going to beginning of line if match found
758 (if (re-search-forward hif-ifx-else-endif-regexp
(point-max) t
)
759 (beginning-of-line)))
761 (defun hif-find-previous-relevant ()
762 "Move to previous #if..., #else, or #endif, before the current line."
763 ;; (message "hif-find-previous-relevant at %d" (point))
765 ;; avoid infinite recursion by only going to beginning of line if match found
766 (if (re-search-backward hif-ifx-else-endif-regexp
(point-min) t
)
767 (beginning-of-line)))
770 (defun hif-looking-at-ifX () ;; Should eventually see #if
771 (looking-at hif-ifx-regexp
))
772 (defun hif-looking-at-endif ()
773 (looking-at hif-endif-regexp
))
774 (defun hif-looking-at-else ()
775 (looking-at hif-else-regexp
))
779 (defun hif-ifdef-to-endif ()
780 "If positioned at #ifX or #else form, skip to corresponding #endif."
781 ;; (message "hif-ifdef-to-endif at %d" (point)) (sit-for 1)
782 (hif-find-next-relevant)
783 (cond ((hif-looking-at-ifX)
784 (hif-ifdef-to-endif) ; find endif of nested if
785 (hif-ifdef-to-endif)) ; find outer endif or else
786 ((hif-looking-at-else)
787 (hif-ifdef-to-endif)) ; find endif following else
788 ((hif-looking-at-endif)
791 (error "Mismatched #ifdef #endif pair"))))
794 (defun hif-endif-to-ifdef ()
795 "If positioned at #endif form, skip backward to corresponding #ifX."
796 ;; (message "hif-endif-to-ifdef at %d" (point))
797 (let ((start (point)))
798 (hif-find-previous-relevant)
799 (if (= start
(point))
800 (error "Mismatched #ifdef #endif pair")))
801 (cond ((hif-looking-at-endif)
802 (hif-endif-to-ifdef) ; find beginning of nested if
803 (hif-endif-to-ifdef)) ; find beginning of outer if or else
804 ((hif-looking-at-else)
805 (hif-endif-to-ifdef))
806 ((hif-looking-at-ifX)
808 (t))) ; never gets here
811 (defun forward-ifdef (&optional arg
)
812 "Move point to beginning of line of the next ifdef-endif.
813 With argument, do this that many times."
815 (or arg
(setq arg
1))
816 (if (< arg
0) (backward-ifdef (- arg
))
819 (let ((start (point)))
820 (unless (hif-looking-at-ifX)
821 (hif-find-next-relevant))
822 (if (hif-looking-at-ifX)
825 (error "No following #ifdef"))))))
828 (defun backward-ifdef (&optional arg
)
829 "Move point to beginning of the previous ifdef-endif.
830 With argument, do this that many times."
832 (or arg
(setq arg
1))
833 (if (< arg
0) (forward-ifdef (- arg
))
837 (let ((start (point)))
838 (unless (hif-looking-at-endif)
839 (hif-find-previous-relevant))
840 (if (hif-looking-at-endif)
843 (error "No previous #ifdef"))))))
847 "Move point to beginning of nested ifdef or else-part."
849 (let ((start (point)))
850 (hif-find-next-relevant)
851 (if (or (hif-looking-at-ifX) (hif-looking-at-else))
854 (error "No following #ifdef"))))
858 "Move point to beginning of enclosing ifdef or else-part."
861 (let ((start (point)))
862 (unless (hif-looking-at-endif)
863 (hif-find-previous-relevant))
864 (if (hif-looking-at-endif)
865 (hif-endif-to-ifdef))
866 (if (= start
(point))
867 (error "No previous #ifdef"))))
869 (defun next-ifdef (&optional arg
)
870 "Move to the beginning of the next #ifX, #else, or #endif.
871 With argument, do this that many times."
873 (or arg
(setq arg
1))
874 (if (< arg
0) (previous-ifdef (- arg
))
877 (hif-find-next-relevant)
880 (error "No following #ifdefs, #elses, or #endifs")))))
882 (defun previous-ifdef (&optional arg
)
883 "Move to the beginning of the previous #ifX, #else, or #endif.
884 With argument, do this that many times."
886 (or arg
(setq arg
1))
887 (if (< arg
0) (next-ifdef (- arg
))
890 (let ((start (point)))
891 (hif-find-previous-relevant)
892 (if (= start
(point))
893 (error "No previous #ifdefs, #elses, or #endifs"))))))
896 ;;===%%SF%% parsing (End) ===
899 ;;===%%SF%% hide-ifdef-hiding (Start) ===
902 ;;; A range is a structure with four components:
903 ;;; ELSE-P True if there was an else clause for the ifdef.
904 ;;; START The start of the range. (beginning of line)
905 ;;; ELSE The else marker (beginning of line)
906 ;;; Only valid if ELSE-P is true.
907 ;;; END The end of the range. (beginning of line)
909 (defsubst hif-make-range
(start end
&optional else
)
910 (list start else end
))
912 (defsubst hif-range-start
(range) (elt range
0))
913 (defsubst hif-range-else
(range) (elt range
1))
914 (defsubst hif-range-end
(range) (elt range
2))
919 ;;; The workhorse, it delimits the #if region. Reasonably simple:
920 ;;; Skip until an #else or #endif is found, remembering positions. If
921 ;;; an #else was found, skip some more, looking for the true #endif.
923 (defun hif-find-range ()
924 "Return a Range structure describing the current #if region.
925 Point is left unchanged."
926 ;; (message "hif-find-range at %d" (point))
929 (let ((start (point))
932 ;; Part one. Look for either #endif or #else.
933 ;; This loop-and-a-half dedicated to E. Dijkstra.
935 (hif-find-next-relevant)
936 (hif-looking-at-ifX)) ; Skip nested ifdef
937 (hif-ifdef-to-endif))
938 ;; Found either a #else or an #endif.
939 (cond ((hif-looking-at-else)
942 (setq end
(point)))) ; (line-end-position)
943 ;; If found #else, look for #endif.
946 (hif-find-next-relevant)
947 (hif-looking-at-ifX)) ; Skip nested ifdef
948 (hif-ifdef-to-endif))
949 (if (hif-looking-at-else)
950 (error "Found two elses in a row? Broken!"))
951 (setq end
(point))) ; (line-end-position)
952 (hif-make-range start end else
))))
957 (defun hif-hide-line (point)
958 "Hide the line containing point. Does nothing if `hide-ifdef-lines' is nil."
959 (when hide-ifdef-lines
962 (hide-ifdef-region-internal
963 (line-beginning-position) (progn (hif-end-of-line) (point))))))
966 ;;; Hif-Possibly-Hide
967 ;;; There are four cases. The #ifX expression is "taken" if it
968 ;;; the hide-ifdef-evaluator returns T. Presumably, this means the code
969 ;;; inside the #ifdef would be included when the program was
972 ;;; Case 1: #ifX taken, and there's an #else.
973 ;;; The #else part must be hidden. The #if (then) part must be
974 ;;; processed for nested #ifX's.
975 ;;; Case 2: #ifX taken, and there's no #else.
976 ;;; The #if part must be processed for nested #ifX's.
977 ;;; Case 3: #ifX not taken, and there's an #else.
978 ;;; The #if part must be hidden. The #else part must be processed
980 ;;; Case 4: #ifX not taken, and there's no #else.
981 ;;; The #ifX part must be hidden.
983 ;;; Further processing is done by narrowing to the relevant region
984 ;;; and just recursively calling hide-ifdef-guts.
986 ;;; When hif-possibly-hide returns, point is at the end of the
987 ;;; possibly-hidden range.
989 (defun hif-recurse-on (start end
)
990 "Call `hide-ifdef-guts' after narrowing to end of START line and END line."
995 (narrow-to-region (point) end
)
998 (defun hif-possibly-hide ()
999 "Called at #ifX expression, this hides those parts that should be hidden.
1000 It uses the judgment of `hide-ifdef-evaluator'."
1001 ;; (message "hif-possibly-hide") (sit-for 1)
1002 (let ((test (hif-canonicalize))
1003 (range (hif-find-range)))
1004 ;; (message "test = %s" test) (sit-for 1)
1006 (hif-hide-line (hif-range-end range
))
1007 (if (not (hif-not (funcall hide-ifdef-evaluator test
)))
1008 (cond ((hif-range-else range
) ; case 1
1009 (hif-hide-line (hif-range-else range
))
1010 (hide-ifdef-region (hif-range-else range
)
1011 (1- (hif-range-end range
)))
1012 (hif-recurse-on (hif-range-start range
)
1013 (hif-range-else range
)))
1015 (hif-recurse-on (hif-range-start range
)
1016 (hif-range-end range
))))
1017 (cond ((hif-range-else range
) ; case 3
1018 (hif-hide-line (hif-range-else range
))
1019 (hide-ifdef-region (hif-range-start range
)
1020 (1- (hif-range-else range
)))
1021 (hif-recurse-on (hif-range-else range
)
1022 (hif-range-end range
)))
1024 (hide-ifdef-region (point)
1025 (1- (hif-range-end range
))))))
1026 (hif-hide-line (hif-range-start range
)) ; Always hide start.
1027 (goto-char (hif-range-end range
))
1032 (defun hide-ifdef-guts ()
1033 "Does most of the work of `hide-ifdefs'.
1034 It does not do the work that's pointless to redo on a recursive entry."
1035 ;; (message "hide-ifdef-guts")
1037 (goto-char (point-min))
1038 (while (hif-find-any-ifX)
1039 (hif-possibly-hide))))
1041 ;;===%%SF%% hide-ifdef-hiding (End) ===
1044 ;;===%%SF%% exports (Start) ===
1046 (defun hide-ifdef-toggle-read-only ()
1047 "Toggle `hide-ifdef-read-only'."
1049 (setq hide-ifdef-read-only
(not hide-ifdef-read-only
))
1050 (message "Hide-Read-Only %s"
1051 (if hide-ifdef-read-only
"ON" "OFF"))
1052 (if hide-ifdef-hiding
1053 (setq buffer-read-only
(or hide-ifdef-read-only hif-outside-read-only
)))
1054 (force-mode-line-update))
1056 (defun hide-ifdef-toggle-outside-read-only ()
1057 "Replacement for `toggle-read-only' within Hide-Ifdef mode."
1059 (setq hif-outside-read-only
(not hif-outside-read-only
))
1060 (message "Read only %s"
1061 (if hif-outside-read-only
"ON" "OFF"))
1062 (setq buffer-read-only
1063 (or (and hide-ifdef-hiding hide-ifdef-read-only
)
1064 hif-outside-read-only
))
1065 (force-mode-line-update))
1067 (defun hide-ifdef-toggle-shadowing ()
1070 (set (make-local-variable 'hide-ifdef-shadow
) (not hide-ifdef-shadow
))
1071 (message "Shadowing %s" (if hide-ifdef-shadow
"ON" "OFF"))
1074 (dolist (overlay (overlays-in (point-min) (point-max)))
1075 (when (overlay-get overlay
'hide-ifdef
)
1076 (if hide-ifdef-shadow
1078 (overlay-put overlay
'invisible nil
)
1079 (overlay-put overlay
'face
'hide-ifdef-shadow
))
1080 (overlay-put overlay
'face nil
)
1081 (overlay-put overlay
'invisible
'hide-ifdef
))))))
1083 (defun hide-ifdef-define (var)
1084 "Define a VAR so that #ifdef VAR would be included."
1085 (interactive "SDefine what? ")
1087 (if hide-ifdef-hiding
(hide-ifdefs)))
1089 (defun hide-ifdef-undef (var)
1090 "Undefine a VAR so that #ifdef VAR would not be included."
1091 (interactive "SUndefine what? ")
1092 (hif-set-var var nil
)
1093 (if hide-ifdef-hiding
(hide-ifdefs)))
1096 (defun hide-ifdefs (&optional nomsg
)
1097 "Hide the contents of some #ifdefs.
1098 Assume that defined symbols have been added to `hide-ifdef-env'.
1099 The text hidden is the text that would not be included by the C
1100 preprocessor if it were given the file with those symbols defined.
1102 Turn off hiding by calling `show-ifdefs'."
1105 (message "Hiding...")
1106 (setq hif-outside-read-only buffer-read-only
)
1107 (unless hide-ifdef-mode
(hide-ifdef-mode 1)) ; turn on hide-ifdef-mode
1108 (if hide-ifdef-hiding
1109 (show-ifdefs)) ; Otherwise, deep confusion.
1110 (setq hide-ifdef-hiding t
)
1112 (setq buffer-read-only
(or hide-ifdef-read-only hif-outside-read-only
))
1114 (message "Hiding done")))
1117 (defun show-ifdefs ()
1118 "Cancel the effects of `hide-ifdef': show the contents of all #ifdefs."
1120 (setq buffer-read-only hif-outside-read-only
)
1122 (setq hide-ifdef-hiding nil
))
1125 (defun hif-find-ifdef-block ()
1126 "Utility to hide and show ifdef block.
1127 Return as (TOP . BOTTOM) the extent of ifdef block."
1129 (cons (save-excursion
1131 (unless (or (hif-looking-at-else) (hif-looking-at-ifX))
1134 (hif-ifdef-to-endif)
1135 (setq max-bottom
(1- (point)))))
1138 (unless (hif-looking-at-endif)
1139 (hif-find-next-relevant))
1140 (while (hif-looking-at-ifX)
1141 (hif-ifdef-to-endif)
1142 (hif-find-next-relevant))
1143 (min max-bottom
(1- (point)))))))
1146 (defun hide-ifdef-block ()
1147 "Hide the ifdef block (true or false part) enclosing or before the cursor."
1149 (unless hide-ifdef-mode
(hide-ifdef-mode 1))
1150 (let ((top-bottom (hif-find-ifdef-block)))
1151 (hide-ifdef-region (car top-bottom
) (cdr top-bottom
))
1152 (when hide-ifdef-lines
1153 (hif-hide-line (car top-bottom
))
1154 (hif-hide-line (1+ (cdr top-bottom
))))
1155 (setq hide-ifdef-hiding t
))
1156 (setq buffer-read-only
(or hide-ifdef-read-only hif-outside-read-only
)))
1158 (defun show-ifdef-block ()
1159 "Show the ifdef block (true or false part) enclosing or before the cursor."
1161 (let ((top-bottom (hif-find-ifdef-block)))
1162 (if hide-ifdef-lines
1163 (hif-show-ifdef-region
1165 (goto-char (car top-bottom
)) (line-beginning-position))
1167 (goto-char (1+ (cdr top-bottom
)))
1168 (hif-end-of-line) (point)))
1169 (hif-show-ifdef-region (1- (car top-bottom
)) (cdr top-bottom
)))))
1172 ;;; definition alist support
1174 (defvar hide-ifdef-define-alist nil
1175 "A global assoc list of pre-defined symbol lists.")
1177 (defun hif-compress-define-list (env)
1178 "Compress the define list ENV into a list of defined symbols only."
1179 (let ((new-defs nil
))
1180 (dolist (def env new-defs
)
1181 (if (hif-lookup (car def
)) (push (car def
) new-defs
)))))
1183 (defun hide-ifdef-set-define-alist (name)
1184 "Set the association for NAME to `hide-ifdef-env'."
1185 (interactive "SSet define list: ")
1186 (push (cons name
(hif-compress-define-list hide-ifdef-env
))
1187 hide-ifdef-define-alist
))
1189 (defun hide-ifdef-use-define-alist (name)
1190 "Set `hide-ifdef-env' to the define list specified by NAME."
1192 (list (completing-read "Use define list: "
1193 (mapcar (lambda (x) (symbol-name (car x
)))
1194 hide-ifdef-define-alist
)
1196 (if (stringp name
) (setq name
(intern name
)))
1197 (let ((define-list (assoc name hide-ifdef-define-alist
)))
1199 (setq hide-ifdef-env
1200 (mapcar (lambda (arg) (cons arg t
))
1202 (error "No define list for %s" name
))
1203 (if hide-ifdef-hiding
(hide-ifdefs))))
1207 ;;; hideif.el ends here