36 |
;; M-x hide-ifdefs or C-c @ h |
;; M-x hide-ifdefs or C-c @ h |
37 |
;; |
;; |
38 |
;; Hide-ifdef suppresses the display of code that the preprocessor wouldn't |
;; Hide-ifdef suppresses the display of code that the preprocessor wouldn't |
39 |
;; pass through. The support of constant expressions in #if lines is |
;; pass through. The support of constant expressions in #if lines is |
40 |
;; limited to identifiers, parens, and the operators: &&, ||, !, and |
;; limited to identifiers, parens, and the operators: &&, ||, !, and |
41 |
;; "defined". Please extend this. |
;; "defined". Please extend this. |
42 |
;; |
;; |
43 |
;; The hidden code is marked by ellipses (...). Be |
;; The hidden code is marked by ellipses (...). Be |
44 |
;; cautious when editing near ellipses, since the hidden text is |
;; 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 |
;; still in the buffer, and you can move the point into it and modify |
46 |
;; text unawares. If you don't want to see the ellipses, set |
;; text unawares. If you don't want to see the ellipses, set |
47 |
;; selective-display-ellipses to nil. But this can be dangerous. |
;; selective-display-ellipses to nil. But this can be dangerous. |
48 |
;; You can make your buffer read-only while hide-ifdef-hiding by setting |
;; You can make your buffer read-only while hide-ifdef-hiding by setting |
49 |
;; hide-ifdef-read-only to a non-nil value. You can toggle this |
;; hide-ifdef-read-only to a non-nil value. You can toggle this |
50 |
;; variable with hide-ifdef-toggle-read-only (C-c @ C-q). |
;; variable with hide-ifdef-toggle-read-only (C-c @ C-q). |
51 |
;; |
;; |
52 |
;; You can undo the effect of hide-ifdefs by typing |
;; You can undo the effect of hide-ifdefs by typing |
59 |
;; If you define or undefine a symbol while hide-ifdef-mode is in effect, |
;; If you define or undefine a symbol while hide-ifdef-mode is in effect, |
60 |
;; the display will be updated. Only the define list for the current |
;; the display will be updated. Only the define list for the current |
61 |
;; buffer will be affected. You can save changes to the local define |
;; buffer will be affected. You can save changes to the local define |
62 |
;; list with hide-ifdef-set-define-alist. This adds entries |
;; list with hide-ifdef-set-define-alist. This adds entries |
63 |
;; to hide-ifdef-define-alist. |
;; to hide-ifdef-define-alist. |
64 |
;; |
;; |
65 |
;; If you have defined a hide-ifdef-mode-hook, you can set |
;; If you have defined a hide-ifdef-mode-hook, you can set |
207 |
is used. |
is used. |
208 |
|
|
209 |
hide-ifdef-define-alist |
hide-ifdef-define-alist |
210 |
An association list of defined symbol lists. |
An association list of defined symbol lists. |
211 |
Use `hide-ifdef-set-define-alist' to save the current `hide-ifdef-env' |
Use `hide-ifdef-set-define-alist' to save the current `hide-ifdef-env' |
212 |
and `hide-ifdef-use-define-alist' to set the current `hide-ifdef-env' |
and `hide-ifdef-use-define-alist' to set the current `hide-ifdef-env' |
213 |
from one of the lists in `hide-ifdef-define-alist'. |
from one of the lists in `hide-ifdef-define-alist'. |
232 |
(if (null arg) |
(if (null arg) |
233 |
(not hide-ifdef-mode) |
(not hide-ifdef-mode) |
234 |
(> (prefix-numeric-value arg) 0))) |
(> (prefix-numeric-value arg) 0))) |
235 |
|
|
236 |
(force-mode-line-update) |
(force-mode-line-update) |
237 |
|
|
238 |
(if hide-ifdef-mode |
(if hide-ifdef-mode |
259 |
(show-ifdefs)) |
(show-ifdefs)) |
260 |
(message "Exit Hide-Ifdef mode") |
(message "Exit Hide-Ifdef mode") |
261 |
)) |
)) |
262 |
|
|
263 |
|
|
264 |
;; from outline.el with docstring fixed. |
;; from outline.el with docstring fixed. |
265 |
(defun hif-outline-flag-region (from to flag) |
(defun hif-outline-flag-region (from to flag) |
288 |
(add-hook 'after-revert-hook 'hif-before-revert-function) |
(add-hook 'after-revert-hook 'hif-before-revert-function) |
289 |
|
|
290 |
(defun hide-ifdef-region (start end) |
(defun hide-ifdef-region (start end) |
291 |
"START is the start of a #if or #else form. END is the ending part. |
"START is the start of an #if or #else form. END is the ending part. |
292 |
Everything including these lines is made invisible." |
Everything including these lines is made invisible." |
293 |
(hif-outline-flag-region start end ?\^M) |
(hif-outline-flag-region start end ?\^M) |
294 |
) |
) |
377 |
(unwind-protect |
(unwind-protect |
378 |
(progn |
(progn |
379 |
(set-syntax-table hide-ifdef-syntax-table) |
(set-syntax-table hide-ifdef-syntax-table) |
380 |
(while (< expr-start expr-length) |
(while (< expr-start expr-length) |
381 |
; (message "expr-start = %d" expr-start) (sit-for 1) |
; (message "expr-start = %d" expr-start) (sit-for 1) |
382 |
(cond |
(cond |
383 |
((string-match "^[ \t]+" expr-string expr-start) |
((string-match "^[ \t]+" expr-string expr-start) |
485 |
(hif-nexttoken) |
(hif-nexttoken) |
486 |
(setq result (list math-op result (hif-factor)))) |
(setq result (list math-op result (hif-factor)))) |
487 |
result)) |
result)) |
488 |
|
|
489 |
(defun hif-factor () |
(defun hif-factor () |
490 |
"Parse a factor: '!' factor | '(' expr ')' | 'defined(' id ')' | id." |
"Parse a factor: '!' factor | '(' expr ')' | 'defined(' id ')' | id." |
491 |
(cond |
(cond |
774 |
(while (hif-looking-at-ifX) ; Skip nested ifdef |
(while (hif-looking-at-ifX) ; Skip nested ifdef |
775 |
(hif-ifdef-to-endif) |
(hif-ifdef-to-endif) |
776 |
(hif-find-next-relevant)) |
(hif-find-next-relevant)) |
777 |
;; Found either a #else or an #endif. |
;; Found either an #else or an #endif. |
778 |
(cond ((hif-looking-at-else) |
(cond ((hif-looking-at-else) |
779 |
(setq else-p t) |
(setq else-p t) |
780 |
(setq else (point))) |
(setq else (point))) |
794 |
)) |
)) |
795 |
(hif-make-range else-p start end else)))) |
(hif-make-range else-p start end else)))) |
796 |
|
|
797 |
|
|
798 |
;;; A bit slimy. |
;;; A bit slimy. |
799 |
;;; NOTE: If there's an #ifdef at the beginning of the file, we can't |
;;; NOTE: If there's an #ifdef at the beginning of the file, we can't |
800 |
;;; hide it. There's no previous newline to replace. If we added |
;;; hide it. There's no previous newline to replace. If we added |
814 |
(set-buffer-modified-p modp)) |
(set-buffer-modified-p modp)) |
815 |
)) |
)) |
816 |
)) |
)) |
817 |
|
|
818 |
|
|
819 |
;;; Hif-Possibly-Hide |
;;; Hif-Possibly-Hide |
820 |
;;; There are four cases. The #ifX expression is "taken" if it |
;;; There are four cases. The #ifX expression is "taken" if it |
821 |
;;; the hide-ifdef-evaluator returns T. Presumably, this means the code |
;;; the hide-ifdef-evaluator returns T. Presumably, this means the code |
822 |
;;; inside the #ifdef would be included when the program was |
;;; inside the #ifdef would be included when the program was |
823 |
;;; compiled. |
;;; compiled. |
824 |
;;; |
;;; |
825 |
;;; Case 1: #ifX taken, and there's an #else. |
;;; Case 1: #ifX taken, and there's an #else. |
826 |
;;; The #else part must be hidden. The #if (then) part must be |
;;; The #else part must be hidden. The #if (then) part must be |
855 |
(let ((test (hif-canonicalize)) |
(let ((test (hif-canonicalize)) |
856 |
(range (hif-find-range))) |
(range (hif-find-range))) |
857 |
; (message "test = %s" test) (sit-for 1) |
; (message "test = %s" test) (sit-for 1) |
858 |
|
|
859 |
(hif-hide-line (hif-range-end range)) |
(hif-hide-line (hif-range-end range)) |
860 |
(if (funcall hide-ifdef-evaluator test) |
(if (funcall hide-ifdef-evaluator test) |
861 |
(cond ((hif-range-else-p range) ; case 1 |
(cond ((hif-range-else-p range) ; case 1 |
862 |
(hif-hide-line (hif-range-else range)) |
(hif-hide-line (hif-range-else range)) |
863 |
(hide-ifdef-region (hif-range-else range) |
(hide-ifdef-region (hif-range-else range) |
864 |
(1- (hif-range-end range))) |
(1- (hif-range-end range))) |
865 |
(hif-recurse-on (hif-range-start range) |
(hif-recurse-on (hif-range-start range) |
866 |
(hif-range-else range))) |
(hif-range-else range))) |
938 |
) |
) |
939 |
(force-mode-line-update)) |
(force-mode-line-update)) |
940 |
|
|
941 |
|
|
942 |
(defun hide-ifdef-define (var) |
(defun hide-ifdef-define (var) |
943 |
"Define a VAR so that #ifdef VAR would be included." |
"Define a VAR so that #ifdef VAR would be included." |
944 |
(interactive "SDefine what? ") |
(interactive "SDefine what? ") |
953 |
|
|
954 |
|
|
955 |
(defun hide-ifdefs (&optional nomsg) |
(defun hide-ifdefs (&optional nomsg) |
956 |
"Hide the contents of some #ifdefs. |
"Hide the contents of some #ifdefs. |
957 |
Assume that defined symbols have been added to `hide-ifdef-env'. |
Assume that defined symbols have been added to `hide-ifdef-env'. |
958 |
The text hidden is the text that would not be included by the C |
The text hidden is the text that would not be included by the C |
959 |
preprocessor if it were given the file with those symbols defined. |
preprocessor if it were given the file with those symbols defined. |
960 |
|
|