Mercurial > emacs
annotate lisp/progmodes/hideif.el @ 32566:f7ed7c88b2ed
(info-header-node): Remove unneded tty-specific definition.
author | Eli Zaretskii <eliz@gnu.org> |
---|---|
date | Tue, 17 Oct 2000 07:47:27 +0000 |
parents | 3cfd3dc474b8 |
children | 253f761ad37b |
rev | line source |
---|---|
793
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
1 ;;; hide-ifdef-mode.el --- hides selected code within ifdef. |
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
2 |
14169 | 3 ;; Copyright (C) 1988, 1994 Free Software Foundation, Inc. |
846
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
811
diff
changeset
|
4 |
30805
3cfd3dc474b8
Change authors' mail address.
Gerd Moellmann <gerd@gnu.org>
parents:
30327
diff
changeset
|
5 ;; Author: Daniel LaLiberte <liberte@holonexus.org> |
7324
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
6 ;; Maintainer: FSF |
11455 | 7 ;; Keywords: c, outlines |
793
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
8 |
895
81e6b85a9261
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
853
diff
changeset
|
9 ;; This file is part of GNU Emacs. |
81e6b85a9261
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
853
diff
changeset
|
10 |
81e6b85a9261
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
853
diff
changeset
|
11 ;; GNU Emacs is free software; you can redistribute it and/or modify |
81e6b85a9261
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
853
diff
changeset
|
12 ;; it under the terms of the GNU General Public License as published by |
81e6b85a9261
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
853
diff
changeset
|
13 ;; the Free Software Foundation; either version 2, or (at your option) |
81e6b85a9261
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
853
diff
changeset
|
14 ;; any later version. |
81e6b85a9261
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
853
diff
changeset
|
15 |
81e6b85a9261
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
853
diff
changeset
|
16 ;; GNU Emacs is distributed in the hope that it will be useful, |
81e6b85a9261
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
853
diff
changeset
|
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
81e6b85a9261
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
853
diff
changeset
|
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
81e6b85a9261
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
853
diff
changeset
|
19 ;; GNU General Public License for more details. |
81e6b85a9261
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
853
diff
changeset
|
20 |
81e6b85a9261
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
853
diff
changeset
|
21 ;; You should have received a copy of the GNU General Public License |
14169 | 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. | |
895
81e6b85a9261
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
853
diff
changeset
|
25 |
793
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
26 ;;; Commentary: |
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
27 |
14169 | 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. If you don't want to see the ellipses, set | |
47 ;; selective-display-ellipses to nil. But this can be dangerous. | |
48 ;; You can make your buffer read-only while hide-ifdef-hiding by setting | |
49 ;; hide-ifdef-read-only to a non-nil value. You can toggle this | |
50 ;; variable with hide-ifdef-toggle-read-only (C-c @ C-q). | |
51 ;; | |
52 ;; You can undo the effect of hide-ifdefs by typing | |
53 ;; | |
54 ;; M-x show-ifdefs or C-c @ s | |
55 ;; | |
56 ;; Use M-x hide-ifdef-define (C-c @ d) to define a symbol. | |
57 ;; Use M-x hide-ifdef-undef (C-c @ u) to undefine a symbol. | |
58 ;; | |
59 ;; If you define or undefine a symbol while hide-ifdef-mode is in effect, | |
60 ;; the display will be updated. Only the define list for the current | |
61 ;; buffer will be affected. You can save changes to the local define | |
62 ;; list with hide-ifdef-set-define-alist. This adds entries | |
63 ;; to hide-ifdef-define-alist. | |
64 ;; | |
65 ;; If you have defined a hide-ifdef-mode-hook, you can set | |
66 ;; up a list of symbols that may be used by hide-ifdefs as in the | |
67 ;; following example: | |
68 ;; | |
69 ;; (setq hide-ifdef-mode-hook | |
29588
0cf06902af64
(hif-compress-define-list)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26519
diff
changeset
|
70 ;; (lambda () |
14169 | 71 ;; (if (not hide-ifdef-define-alist) |
72 ;; (setq hide-ifdef-define-alist | |
73 ;; '((list1 ONE TWO) | |
74 ;; (list2 TWO THREE) | |
75 ;; ))) | |
76 ;; (hide-ifdef-use-define-alist 'list2) ; use list2 by default | |
77 ;; )) | |
78 ;; | |
30327
09b01c9914a6
Correct author's email address. Fix typo in comment.
Gerd Moellmann <gerd@gnu.org>
parents:
29588
diff
changeset
|
79 ;; You can call hide-ifdef-use-define-alist (C-c @ U) at any time to specify |
14169 | 80 ;; another list to use. |
81 ;; | |
82 ;; To cause ifdefs to be hidden as soon as hide-ifdef-mode is called, | |
83 ;; set hide-ifdef-initially to non-nil. | |
84 ;; | |
85 ;; If you set hide-ifdef-lines to t, hide-ifdefs hides all the #ifdef lines. | |
86 ;; In the absence of highlighting, that might be a bad idea. If you set | |
87 ;; hide-ifdef-lines to nil (the default), the surrounding preprocessor | |
88 ;; lines will be displayed. That can be confusing in its own | |
89 ;; right. Other variations on display are possible, but not much | |
90 ;; better. | |
91 ;; | |
92 ;; You can explicitly hide or show individual ifdef blocks irrespective | |
93 ;; of the define list by using hide-ifdef-block and show-ifdef-block. | |
94 ;; | |
95 ;; You can move the point between ifdefs with forward-ifdef, backward-ifdef, | |
96 ;; up-ifdef, down-ifdef, next-ifdef, and previous-ifdef. | |
97 ;; | |
98 ;; If you have minor-mode-alist in your mode line (the default) two labels | |
99 ;; may appear. "Ifdef" will appear when hide-ifdef-mode is active. "Hiding" | |
100 ;; will appear when text may be hidden ("hide-ifdef-hiding" is non-nil). | |
101 ;; | |
102 ;; Written by Brian Marick, at Gould, Computer Systems Division, Urbana IL. | |
103 ;; Extensively modified by Daniel LaLiberte (while at Gould). | |
104 ;; | |
105 ;; You may freely modify and distribute this, but keep a record | |
106 ;; of modifications and send comments to: | |
107 ;; liberte@a.cs.uiuc.edu or ihnp4!uiucdcs!liberte | |
108 ;; I will continue to upgrade hide-ifdef-mode | |
109 ;; with your contributions. | |
31 | 110 |
793
6fb68a1460a6
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
660
diff
changeset
|
111 ;;; Code: |
31 | 112 |
13297
9699410bf8bf
Provide hideif; require cc-mode.
Richard M. Stallman <rms@gnu.org>
parents:
12954
diff
changeset
|
113 (require 'cc-mode) |
9699410bf8bf
Provide hideif; require cc-mode.
Richard M. Stallman <rms@gnu.org>
parents:
12954
diff
changeset
|
114 |
20781 | 115 (defgroup hide-ifdef nil |
116 "Hide selected code within `ifdef'." | |
117 :group 'c) | |
118 | |
7324
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
119 (defvar hide-ifdef-mode-submap nil |
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
120 "Keymap used with Hide-Ifdef mode.") |
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
121 |
31 | 122 (defvar hide-ifdef-mode-map nil |
7324
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
123 "Keymap used with Hide-Ifdef mode.") |
31 | 124 |
11652
1162093d885c
(hide-ifdef-mode-prefix-key): Change to C-c @.
Richard M. Stallman <rms@gnu.org>
parents:
11581
diff
changeset
|
125 (defconst hide-ifdef-mode-prefix-key "\C-c@" |
210 | 126 "Prefix key for all Hide-Ifdef mode commands.") |
31 | 127 |
7324
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
128 ;; Set up the submap that goes after the prefix key. |
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
129 (if hide-ifdef-mode-submap |
14017 | 130 () ; Don't redefine it. |
7324
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
131 (setq hide-ifdef-mode-submap (make-sparse-keymap)) |
11652
1162093d885c
(hide-ifdef-mode-prefix-key): Change to C-c @.
Richard M. Stallman <rms@gnu.org>
parents:
11581
diff
changeset
|
132 (define-key hide-ifdef-mode-submap "d" 'hide-ifdef-define) |
1162093d885c
(hide-ifdef-mode-prefix-key): Change to C-c @.
Richard M. Stallman <rms@gnu.org>
parents:
11581
diff
changeset
|
133 (define-key hide-ifdef-mode-submap "u" 'hide-ifdef-undef) |
1162093d885c
(hide-ifdef-mode-prefix-key): Change to C-c @.
Richard M. Stallman <rms@gnu.org>
parents:
11581
diff
changeset
|
134 (define-key hide-ifdef-mode-submap "D" 'hide-ifdef-set-define-alist) |
1162093d885c
(hide-ifdef-mode-prefix-key): Change to C-c @.
Richard M. Stallman <rms@gnu.org>
parents:
11581
diff
changeset
|
135 (define-key hide-ifdef-mode-submap "U" 'hide-ifdef-use-define-alist) |
7324
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
136 |
11652
1162093d885c
(hide-ifdef-mode-prefix-key): Change to C-c @.
Richard M. Stallman <rms@gnu.org>
parents:
11581
diff
changeset
|
137 (define-key hide-ifdef-mode-submap "h" 'hide-ifdefs) |
1162093d885c
(hide-ifdef-mode-prefix-key): Change to C-c @.
Richard M. Stallman <rms@gnu.org>
parents:
11581
diff
changeset
|
138 (define-key hide-ifdef-mode-submap "s" 'show-ifdefs) |
7324
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
139 (define-key hide-ifdef-mode-submap "\C-d" 'hide-ifdef-block) |
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
140 (define-key hide-ifdef-mode-submap "\C-s" 'show-ifdef-block) |
31 | 141 |
7324
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
142 (define-key hide-ifdef-mode-submap "\C-q" 'hide-ifdef-toggle-read-only) |
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
143 (let ((where (where-is-internal 'toggle-read-only '(keymap) t))) |
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
144 (if where |
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
145 (define-key hide-ifdef-mode-submap |
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
146 where |
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
147 'hide-ifdef-toggle-outside-read-only))) |
31 | 148 ) |
149 | |
7324
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
150 ;; Set up the mode's main map, which leads via the prefix key to the submap. |
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
151 (if hide-ifdef-mode-map |
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
152 () |
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
153 (setq hide-ifdef-mode-map (make-sparse-keymap)) |
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
154 (define-key hide-ifdef-mode-map hide-ifdef-mode-prefix-key |
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
155 hide-ifdef-mode-submap)) |
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
156 |
25698
c8067b54bd55
(hide-ifdef-mode): Add autoload cookie.
Dave Love <fx@gnu.org>
parents:
23440
diff
changeset
|
157 ;; Autoload for the benefit of `make-mode-line-mouse-sensitive'. |
c8067b54bd55
(hide-ifdef-mode): Add autoload cookie.
Dave Love <fx@gnu.org>
parents:
23440
diff
changeset
|
158 ;;;###autoload |
31 | 159 (defvar hide-ifdef-mode nil |
7324
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
160 "Non-nil when hide-ifdef-mode is activated.") |
31 | 161 |
162 (defvar hide-ifdef-hiding nil | |
7324
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
163 "Non-nil when text may be hidden.") |
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
164 |
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
165 ;; Arrange to use the mode's map when the mode is enabled. |
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
166 (or (assq 'hide-ifdef-mode minor-mode-map-alist) |
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
167 (setq minor-mode-map-alist |
8095
8d24be305959
(minor-mode-map-alist): Add missing quote.
Richard M. Stallman <rms@gnu.org>
parents:
7379
diff
changeset
|
168 (cons (cons 'hide-ifdef-mode hide-ifdef-mode-map) |
7324
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
169 minor-mode-map-alist))) |
31 | 170 |
171 (or (assq 'hide-ifdef-hiding minor-mode-alist) | |
172 (setq minor-mode-alist | |
173 (cons '(hide-ifdef-hiding " Hiding") | |
174 minor-mode-alist))) | |
175 | |
176 (or (assq 'hide-ifdef-mode minor-mode-alist) | |
177 (setq minor-mode-alist | |
178 (cons '(hide-ifdef-mode " Ifdef") | |
179 minor-mode-alist))) | |
180 | |
7379
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
181 ;; fix c-mode syntax table so we can recognize whole symbols. |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
182 (defvar hide-ifdef-syntax-table |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
183 (copy-syntax-table c-mode-syntax-table) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
184 "Syntax table used for tokenizing #if expressions.") |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
185 |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
186 (modify-syntax-entry ?_ "w" hide-ifdef-syntax-table) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
187 (modify-syntax-entry ?& "." hide-ifdef-syntax-table) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
188 (modify-syntax-entry ?\| "." hide-ifdef-syntax-table) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
189 |
23440
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
190 (defvar hide-ifdef-env nil |
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
191 "An alist of defined symbols and their values.") |
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
192 |
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
193 (defvar hif-outside-read-only nil |
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
194 "Internal variable. Saves the value of `buffer-read-only' while hiding.") |
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
195 |
998 | 196 ;;;###autoload |
31 | 197 (defun hide-ifdef-mode (arg) |
210 | 198 "Toggle Hide-Ifdef mode. This is a minor mode, albeit a large one. |
15166
83258eb2ede5
(hide-ifdef-mode): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
199 With ARG, turn Hide-Ifdef mode on if arg is positive, off otherwise. |
210 | 200 In Hide-Ifdef mode, code within #ifdef constructs that the C preprocessor |
31 | 201 would eliminate may be hidden from view. Several variables affect |
202 how the hiding is done: | |
203 | |
204 hide-ifdef-env | |
205 An association list of defined and undefined symbols for the | |
210 | 206 current buffer. Initially, the global value of `hide-ifdef-env' |
207 is used. | |
31 | 208 |
209 hide-ifdef-define-alist | |
210 An association list of defined symbol lists. | |
210 | 211 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' | |
213 from one of the lists in `hide-ifdef-define-alist'. | |
31 | 214 |
215 hide-ifdef-lines | |
216 Set to non-nil to not show #if, #ifdef, #ifndef, #else, and | |
217 #endif lines when hiding. | |
218 | |
219 hide-ifdef-initially | |
210 | 220 Indicates whether `hide-ifdefs' should be called when Hide-Ifdef mode |
31 | 221 is activated. |
222 | |
223 hide-ifdef-read-only | |
224 Set to non-nil if you want to make buffers read only while hiding. | |
210 | 225 After `show-ifdefs', read-only status is restored to previous value. |
31 | 226 |
227 \\{hide-ifdef-mode-map}" | |
228 | |
229 (interactive "P") | |
230 (make-local-variable 'hide-ifdef-mode) | |
231 (setq hide-ifdef-mode | |
232 (if (null arg) | |
233 (not hide-ifdef-mode) | |
234 (> (prefix-numeric-value arg) 0))) | |
235 | |
7324
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
236 (force-mode-line-update) |
31 | 237 |
238 (if hide-ifdef-mode | |
239 (progn | |
240 ; inherit global values | |
241 (make-local-variable 'hide-ifdef-env) | |
242 (setq hide-ifdef-env (default-value 'hide-ifdef-env)) | |
243 | |
244 (make-local-variable 'hide-ifdef-hiding) | |
245 (setq hide-ifdef-hiding (default-value 'hide-ifdef-hiding)) | |
246 | |
247 (make-local-variable 'hif-outside-read-only) | |
248 (setq hif-outside-read-only buffer-read-only) | |
249 | |
250 (run-hooks 'hide-ifdef-mode-hook) | |
251 | |
252 (if hide-ifdef-initially | |
253 (hide-ifdefs) | |
254 (show-ifdefs)) | |
13855 | 255 (message "Enter Hide-Ifdef mode") |
31 | 256 ) |
257 ; else end hide-ifdef-mode | |
258 (if hide-ifdef-hiding | |
259 (show-ifdefs)) | |
13855 | 260 (message "Exit Hide-Ifdef mode") |
31 | 261 )) |
262 | |
263 | |
264 ;; from outline.el with docstring fixed. | |
265 (defun hif-outline-flag-region (from to flag) | |
13855 | 266 "Hides or shows lines from FROM to TO, according to FLAG. |
267 If FLAG is \\n (newline character) then text is shown, while if FLAG is \\^M | |
31 | 268 \(control-M) the text is hidden." |
269 (let ((modp (buffer-modified-p))) | |
270 (unwind-protect (progn | |
271 (subst-char-in-region from to | |
272 (if (= flag ?\n) ?\^M ?\n) | |
273 flag t) ) | |
274 (set-buffer-modified-p modp)) | |
275 )) | |
276 | |
277 (defun hif-show-all () | |
278 "Show all of the text in the current buffer." | |
279 (interactive) | |
280 (hif-outline-flag-region (point-min) (point-max) ?\n)) | |
281 | |
13855 | 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-before-revert-function () | |
286 (and hide-ifdef-mode hide-ifdef-hiding | |
287 (hide-ifdefs t))) | |
288 (add-hook 'after-revert-hook 'hif-before-revert-function) | |
289 | |
31 | 290 (defun hide-ifdef-region (start end) |
291 "START is the start of a #if or #else form. END is the ending part. | |
292 Everything including these lines is made invisible." | |
293 (hif-outline-flag-region start end ?\^M) | |
294 ) | |
295 | |
296 (defun hif-show-ifdef-region (start end) | |
297 "Everything between START and END is made visible." | |
298 (hif-outline-flag-region start end ?\n) | |
299 ) | |
300 | |
301 | |
302 | |
303 ;===%%SF%% evaluation (Start) === | |
304 | |
13855 | 305 ;; It is not useful to set this to anything but `eval'. |
306 ;; In fact, the variable might as well be eliminated. | |
31 | 307 (defvar hide-ifdef-evaluator 'eval |
13855 | 308 "The function to use to evaluate a form. |
309 The evaluator is given a canonical form and returns t if text under | |
31 | 310 that form should be displayed.") |
311 | |
312 (defvar hif-undefined-symbol nil | |
313 "...is by default considered to be false.") | |
314 | |
315 | |
316 (defun hif-set-var (var value) | |
317 "Prepend (var value) pair to hide-ifdef-env." | |
318 (setq hide-ifdef-env (cons (cons var value) hide-ifdef-env))) | |
319 | |
320 | |
321 (defun hif-lookup (var) | |
322 ; (message "hif-lookup %s" var) | |
323 (let ((val (assoc var hide-ifdef-env))) | |
324 (if val | |
325 (cdr val) | |
326 hif-undefined-symbol))) | |
327 | |
328 (defun hif-defined (var) | |
329 (hif-lookup var) | |
330 ; when #if expressions are fully supported, defined result should be 1 | |
331 ; (if (assoc var hide-ifdef-env) | |
332 ; 1 | |
333 ; nil) | |
334 ) | |
335 | |
336 | |
337 ;===%%SF%% evaluation (End) === | |
338 | |
339 | |
340 | |
341 ;===%%SF%% parsing (Start) === | |
342 ;;; The code that understands what ifs and ifdef in files look like. | |
343 | |
344 (defconst hif-cpp-prefix "\\(^\\|\r\\)[ \t]*#[ \t]*") | |
345 (defconst hif-ifndef-regexp (concat hif-cpp-prefix "ifndef")) | |
346 (defconst hif-ifx-regexp (concat hif-cpp-prefix "if\\(n?def\\)?[ \t]+")) | |
347 (defconst hif-else-regexp (concat hif-cpp-prefix "else")) | |
348 (defconst hif-endif-regexp (concat hif-cpp-prefix "endif")) | |
349 (defconst hif-ifx-else-endif-regexp | |
350 (concat hif-ifx-regexp "\\|" hif-else-regexp "\\|" hif-endif-regexp)) | |
351 | |
23440
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
352 ; Used to store the current token and the whole token list during parsing. |
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
353 ; Only bound dynamically. |
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
354 (defvar hif-token) |
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
355 (defvar hif-token-list) |
31 | 356 |
357 (defun hif-infix-to-prefix (token-list) | |
358 "Convert list of tokens in infix into prefix list" | |
26519 | 359 ; (message "hif-infix-to-prefix: %s" token-list) |
31 | 360 (if (= 1 (length token-list)) |
26519 | 361 `(hif-lookup (quote ,(car token-list))) |
31 | 362 (hif-parse-if-exp token-list)) |
363 ) | |
364 | |
365 ; pattern to match initial identifier, !, &&, ||, (, or ). | |
8914 | 366 ; Added ==, + and -: garyo@avs.com 8/9/94 |
19528
8fc9b5fc7633
(hif-greater, hif-less, hif-greater-equal)
Richard M. Stallman <rms@gnu.org>
parents:
16182
diff
changeset
|
367 (defconst hif-token-regexp "^\\(&&\\|||\\|[!=]=\\|!\\|[()+-]\\|[<>]=?\\|\\w+\\)") |
31 | 368 (defconst hif-end-of-comment "\\*/") |
369 | |
370 | |
371 (defun hif-tokenize (expr-string) | |
372 "Separate string into a list of tokens" | |
373 (let ((token-list nil) | |
374 (expr-start 0) | |
7379
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
375 (expr-length (length expr-string)) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
376 (current-syntax-table (syntax-table))) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
377 (unwind-protect |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
378 (progn |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
379 (set-syntax-table hide-ifdef-syntax-table) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
380 (while (< expr-start expr-length) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
381 ; (message "expr-start = %d" expr-start) (sit-for 1) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
382 (cond |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
383 ((string-match "^[ \t]+" expr-string expr-start) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
384 ;; skip whitespace |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
385 (setq expr-start (match-end 0)) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
386 ;; stick newline in string so ^ matches on the next string-match |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
387 (aset expr-string (1- expr-start) ?\n)) |
31 | 388 |
7379
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
389 ((string-match "^/\\*" expr-string expr-start) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
390 (setq expr-start (match-end 0)) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
391 (aset expr-string (1- expr-start) ?\n) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
392 (or |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
393 (string-match hif-end-of-comment |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
394 expr-string expr-start) ; eat comment |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
395 (string-match "$" expr-string expr-start)) ; multi-line comment |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
396 (setq expr-start (match-end 0)) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
397 (aset expr-string (1- expr-start) ?\n)) |
31 | 398 |
7379
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
399 ((string-match "^//" expr-string expr-start) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
400 (string-match "$" expr-string expr-start) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
401 (setq expr-start (match-end 0))) |
5560
a9924c6db247
(hif-tokenize): Handle // comments after #if.
Richard M. Stallman <rms@gnu.org>
parents:
3591
diff
changeset
|
402 |
7379
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
403 ((string-match hif-token-regexp expr-string expr-start) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
404 (let ((token (substring expr-string expr-start (match-end 0)))) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
405 (setq expr-start (match-end 0)) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
406 (aset expr-string (1- expr-start) ?\n) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
407 ; (message "token: %s" token) (sit-for 1) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
408 (setq token-list |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
409 (cons |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
410 (cond |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
411 ((string-equal token "||") 'or) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
412 ((string-equal token "&&") 'and) |
8914 | 413 ((string-equal token "==") 'equal) |
414 ((string-equal token "!=") 'hif-notequal) | |
7379
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
415 ((string-equal token "!") 'not) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
416 ((string-equal token "defined") 'hif-defined) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
417 ((string-equal token "(") 'lparen) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
418 ((string-equal token ")") 'rparen) |
19528
8fc9b5fc7633
(hif-greater, hif-less, hif-greater-equal)
Richard M. Stallman <rms@gnu.org>
parents:
16182
diff
changeset
|
419 ((string-equal token ">") 'hif-greater) |
8fc9b5fc7633
(hif-greater, hif-less, hif-greater-equal)
Richard M. Stallman <rms@gnu.org>
parents:
16182
diff
changeset
|
420 ((string-equal token "<") 'hif-less) |
8fc9b5fc7633
(hif-greater, hif-less, hif-greater-equal)
Richard M. Stallman <rms@gnu.org>
parents:
16182
diff
changeset
|
421 ((string-equal token ">=") 'hif-greater-equal) |
8fc9b5fc7633
(hif-greater, hif-less, hif-greater-equal)
Richard M. Stallman <rms@gnu.org>
parents:
16182
diff
changeset
|
422 ((string-equal token "<=") 'hif-less-equal) |
8914 | 423 ((string-equal token "+") 'hif-plus) |
424 ((string-equal token "-") 'hif-minus) | |
7379
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
425 (t (intern token))) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
426 token-list)))) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
427 (t (error "Bad #if expression: %s" expr-string))))) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
428 (set-syntax-table current-syntax-table)) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
429 (nreverse token-list))) |
31 | 430 |
431 ;;;----------------------------------------------------------------- | |
432 ;;; Translate C preprocessor #if expressions using recursive descent. | |
433 ;;; This parser is limited to the operators &&, ||, !, and "defined". | |
8914 | 434 ;;; Added ==, !=, +, and -. Gary Oberbrunner, garyo@avs.com, 8/9/94 |
31 | 435 |
23440
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
436 (defun hif-parse-if-exp (hif-token-list) |
31 | 437 "Parse the TOKEN-LIST. Return translated list in prefix form." |
438 (hif-nexttoken) | |
439 (prog1 | |
440 (hif-expr) | |
23440
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
441 (if hif-token ; is there still a token? |
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
442 (error "Error: unexpected token: %s" hif-token)))) |
31 | 443 |
444 (defun hif-nexttoken () | |
23440
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
445 "Pop the next token from token-list into the let variable \"hif-token\"." |
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
446 (setq hif-token (car hif-token-list)) |
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
447 (setq hif-token-list (cdr hif-token-list)) |
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
448 hif-token) |
31 | 449 |
450 (defun hif-expr () | |
13855 | 451 "Parse an expression as found in #if. |
31 | 452 expr : term | expr '||' term." |
453 (let ((result (hif-term))) | |
23440
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
454 (while (eq hif-token 'or) |
31 | 455 (hif-nexttoken) |
456 (setq result (list 'or result (hif-term)))) | |
210 | 457 result)) |
31 | 458 |
459 (defun hif-term () | |
13855 | 460 "Parse a term : eq-expr | term '&&' eq-expr." |
8914 | 461 (let ((result (hif-eq-expr))) |
23440
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
462 (while (eq hif-token 'and) |
31 | 463 (hif-nexttoken) |
8914 | 464 (setq result (list 'and result (hif-eq-expr)))) |
210 | 465 result)) |
31 | 466 |
8914 | 467 (defun hif-eq-expr () |
19528
8fc9b5fc7633
(hif-greater, hif-less, hif-greater-equal)
Richard M. Stallman <rms@gnu.org>
parents:
16182
diff
changeset
|
468 "Parse an eq-expr : math | eq-expr `=='|`!='|`<'|`>'|`>='|`<=' math." |
8914 | 469 (let ((result (hif-math)) |
470 (eq-token nil)) | |
23440
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
471 (while (memq hif-token '(equal hif-notequal hif-greater hif-less |
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
472 hif-greater-equal hif-less-equal)) |
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
473 (setq eq-token hif-token) |
8914 | 474 (hif-nexttoken) |
475 (setq result (list eq-token result (hif-math)))) | |
476 result)) | |
477 | |
478 (defun hif-math () | |
13855 | 479 "Parse an expression with + or - and simpler things. |
8914 | 480 math : factor | math '+|-' factor." |
481 (let ((result (hif-factor)) | |
482 (math-op nil)) | |
23440
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
483 (while (or (eq hif-token 'hif-plus) (eq hif-token 'hif-minus)) |
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
484 (setq math-op hif-token) |
8914 | 485 (hif-nexttoken) |
486 (setq result (list math-op result (hif-factor)))) | |
487 result)) | |
488 | |
31 | 489 (defun hif-factor () |
13855 | 490 "Parse a factor: '!' factor | '(' expr ')' | 'defined(' id ')' | id." |
31 | 491 (cond |
26519 | 492 ((eq hif-token 'not) |
493 (hif-nexttoken) | |
494 (list 'not (hif-factor))) | |
31 | 495 |
26519 | 496 ((eq hif-token 'lparen) |
497 (hif-nexttoken) | |
498 (let ((result (hif-expr))) | |
499 (if (not (eq hif-token 'rparen)) | |
500 (error "Bad token in parenthesized expression: %s" hif-token) | |
501 (hif-nexttoken) | |
502 result))) | |
31 | 503 |
26519 | 504 ((eq hif-token 'hif-defined) |
505 (hif-nexttoken) | |
506 (if (not (eq hif-token 'lparen)) | |
507 (error "Error: expected \"(\" after \"defined\"")) | |
508 (hif-nexttoken) | |
509 (let ((ident hif-token)) | |
510 (if (memq hif-token '(or and not hif-defined lparen rparen)) | |
511 (error "Error: unexpected token: %s" hif-token)) | |
512 (hif-nexttoken) | |
513 (if (not (eq hif-token 'rparen)) | |
514 (error "Error: expected \")\" after identifier")) | |
515 (hif-nexttoken) | |
516 `(hif-defined (quote ,ident)) | |
517 )) | |
31 | 518 |
26519 | 519 (t ; identifier |
520 (let ((ident hif-token)) | |
521 (if (memq ident '(or and)) | |
522 (error "Error: missing identifier")) | |
523 (hif-nexttoken) | |
524 `(hif-lookup (quote ,ident)) | |
525 )) | |
526 )) | |
31 | 527 |
8914 | 528 (defun hif-mathify (val) |
529 "Treat VAL as a number: if it's t or nil, use 1 or 0." | |
530 (cond ((eq val t) | |
531 1) | |
532 ((null val) | |
533 0) | |
534 (t val))) | |
535 | |
536 (defun hif-plus (a b) | |
537 "Like ordinary plus but treat t and nil as 1 and 0." | |
538 (+ (hif-mathify a) (hif-mathify b))) | |
539 (defun hif-minus (a b) | |
540 "Like ordinary minus but treat t and nil as 1 and 0." | |
541 (- (hif-mathify a) (hif-mathify b))) | |
542 (defun hif-notequal (a b) | |
543 "Like (not (equal A B)) but as one symbol." | |
544 (not (equal a b))) | |
19528
8fc9b5fc7633
(hif-greater, hif-less, hif-greater-equal)
Richard M. Stallman <rms@gnu.org>
parents:
16182
diff
changeset
|
545 (defun hif-greater (a b) |
8fc9b5fc7633
(hif-greater, hif-less, hif-greater-equal)
Richard M. Stallman <rms@gnu.org>
parents:
16182
diff
changeset
|
546 "Simple comparison." |
8fc9b5fc7633
(hif-greater, hif-less, hif-greater-equal)
Richard M. Stallman <rms@gnu.org>
parents:
16182
diff
changeset
|
547 (> (hif-mathify a) (hif-mathify b))) |
8fc9b5fc7633
(hif-greater, hif-less, hif-greater-equal)
Richard M. Stallman <rms@gnu.org>
parents:
16182
diff
changeset
|
548 (defun hif-less (a b) |
8fc9b5fc7633
(hif-greater, hif-less, hif-greater-equal)
Richard M. Stallman <rms@gnu.org>
parents:
16182
diff
changeset
|
549 "Simple comparison." |
8fc9b5fc7633
(hif-greater, hif-less, hif-greater-equal)
Richard M. Stallman <rms@gnu.org>
parents:
16182
diff
changeset
|
550 (< (hif-mathify a) (hif-mathify b))) |
8fc9b5fc7633
(hif-greater, hif-less, hif-greater-equal)
Richard M. Stallman <rms@gnu.org>
parents:
16182
diff
changeset
|
551 (defun hif-greater-equal (a b) |
8fc9b5fc7633
(hif-greater, hif-less, hif-greater-equal)
Richard M. Stallman <rms@gnu.org>
parents:
16182
diff
changeset
|
552 "Simple comparison." |
8fc9b5fc7633
(hif-greater, hif-less, hif-greater-equal)
Richard M. Stallman <rms@gnu.org>
parents:
16182
diff
changeset
|
553 (>= (hif-mathify a) (hif-mathify b))) |
8fc9b5fc7633
(hif-greater, hif-less, hif-greater-equal)
Richard M. Stallman <rms@gnu.org>
parents:
16182
diff
changeset
|
554 (defun hif-less-equal (a b) |
8fc9b5fc7633
(hif-greater, hif-less, hif-greater-equal)
Richard M. Stallman <rms@gnu.org>
parents:
16182
diff
changeset
|
555 "Simple comparison." |
8fc9b5fc7633
(hif-greater, hif-less, hif-greater-equal)
Richard M. Stallman <rms@gnu.org>
parents:
16182
diff
changeset
|
556 (<= (hif-mathify a) (hif-mathify b))) |
31 | 557 ;;;----------- end of parser ----------------------- |
558 | |
559 | |
560 (defun hif-canonicalize () | |
13855 | 561 "When at beginning of #ifX, returns a Lisp expression for its condition." |
31 | 562 (save-excursion |
563 (let ((negate (looking-at hif-ifndef-regexp))) | |
564 (re-search-forward hif-ifx-regexp) | |
565 (let* ((expr-string | |
566 (buffer-substring (point) | |
567 (progn (skip-chars-forward "^\n\r") (point)))) | |
568 (expr (hif-infix-to-prefix (hif-tokenize expr-string)))) | |
569 ; (message "hif-canonicalized: %s" expr) | |
570 (if negate | |
571 (list 'not expr) | |
572 expr))))) | |
573 | |
574 | |
575 (defun hif-find-any-ifX () | |
13855 | 576 "Move to next #if..., or #ifndef, at point or after." |
31 | 577 ; (message "find ifX at %d" (point)) |
578 (prog1 | |
579 (re-search-forward hif-ifx-regexp (point-max) t) | |
580 (beginning-of-line))) | |
581 | |
582 | |
583 (defun hif-find-next-relevant () | |
13855 | 584 "Move to next #if..., #else, or #endif, after the current line." |
31 | 585 ; (message "hif-find-next-relevant at %d" (point)) |
586 (end-of-line) | |
587 ; avoid infinite recursion by only going to beginning of line if match found | |
588 (if (re-search-forward hif-ifx-else-endif-regexp (point-max) t) | |
210 | 589 (beginning-of-line))) |
31 | 590 |
591 (defun hif-find-previous-relevant () | |
13855 | 592 "Move to previous #if..., #else, or #endif, before the current line." |
31 | 593 ; (message "hif-find-previous-relevant at %d" (point)) |
594 (beginning-of-line) | |
595 ; avoid infinite recursion by only going to beginning of line if match found | |
596 (if (re-search-backward hif-ifx-else-endif-regexp (point-min) t) | |
210 | 597 (beginning-of-line))) |
31 | 598 |
599 | |
600 (defun hif-looking-at-ifX () ;; Should eventually see #if | |
601 (looking-at hif-ifx-regexp)) | |
602 (defun hif-looking-at-endif () | |
603 (looking-at hif-endif-regexp)) | |
604 (defun hif-looking-at-else () | |
605 (looking-at hif-else-regexp)) | |
606 | |
607 | |
608 | |
609 (defun hif-ifdef-to-endif () | |
610 "If positioned at #ifX or #else form, skip to corresponding #endif." | |
611 ; (message "hif-ifdef-to-endif at %d" (point)) (sit-for 1) | |
612 (hif-find-next-relevant) | |
613 (cond ((hif-looking-at-ifX) | |
614 (hif-ifdef-to-endif) ; find endif of nested if | |
615 (hif-ifdef-to-endif)) ; find outer endif or else | |
616 ((hif-looking-at-else) | |
617 (hif-ifdef-to-endif)) ; find endif following else | |
618 ((hif-looking-at-endif) | |
619 'done) | |
620 (t | |
3591
507f64624555
Apply typo patches from Paul Eggert.
Jim Blandy <jimb@redhat.com>
parents:
3457
diff
changeset
|
621 (error "Mismatched #ifdef #endif pair")))) |
31 | 622 |
623 | |
624 (defun hif-endif-to-ifdef () | |
625 "If positioned at #endif form, skip backward to corresponding #ifX." | |
626 ; (message "hif-endif-to-ifdef at %d" (point)) | |
627 (let ((start (point))) | |
628 (hif-find-previous-relevant) | |
629 (if (= start (point)) | |
3591
507f64624555
Apply typo patches from Paul Eggert.
Jim Blandy <jimb@redhat.com>
parents:
3457
diff
changeset
|
630 (error "Mismatched #ifdef #endif pair"))) |
31 | 631 (cond ((hif-looking-at-endif) |
632 (hif-endif-to-ifdef) ; find beginning of nested if | |
633 (hif-endif-to-ifdef)) ; find beginning of outer if or else | |
634 ((hif-looking-at-else) | |
635 (hif-endif-to-ifdef)) | |
636 ((hif-looking-at-ifX) | |
637 'done) | |
922 | 638 (t))) ; never gets here |
31 | 639 |
640 | |
641 (defun forward-ifdef (&optional arg) | |
642 "Move point to beginning of line of the next ifdef-endif. | |
210 | 643 With argument, do this that many times." |
31 | 644 (interactive "p") |
645 (or arg (setq arg 1)) | |
646 (if (< arg 0) | |
647 (backward-ifdef (- arg))) | |
648 (while (< 0 arg) | |
649 (setq arg (- arg)) | |
650 (let ((start (point))) | |
651 (if (not (hif-looking-at-ifX)) | |
652 (hif-find-next-relevant)) | |
653 (if (hif-looking-at-ifX) | |
654 (hif-ifdef-to-endif) | |
655 (goto-char start) | |
656 (error "No following #ifdef") | |
657 )))) | |
658 | |
659 | |
660 (defun backward-ifdef (&optional arg) | |
661 "Move point to beginning of the previous ifdef-endif. | |
210 | 662 With argument, do this that many times." |
31 | 663 (interactive "p") |
664 (or arg (setq arg 1)) | |
665 (if (< arg 0) | |
666 (forward-ifdef (- arg))) | |
667 (while (< 0 arg) | |
668 (setq arg (1- arg)) | |
669 (beginning-of-line) | |
670 (let ((start (point))) | |
671 (if (not (hif-looking-at-endif)) | |
672 (hif-find-previous-relevant)) | |
673 (if (hif-looking-at-endif) | |
674 (hif-endif-to-ifdef) | |
675 (goto-char start) | |
210 | 676 (error "No previous #ifdef"))))) |
31 | 677 |
678 | |
679 (defun down-ifdef () | |
680 "Move point to beginning of nested ifdef or else-part." | |
681 (interactive) | |
682 (let ((start (point))) | |
683 (hif-find-next-relevant) | |
684 (if (or (hif-looking-at-ifX) (hif-looking-at-else)) | |
685 () | |
686 (goto-char start) | |
210 | 687 (error "No following #ifdef")))) |
31 | 688 |
689 | |
690 (defun up-ifdef () | |
691 "Move point to beginning of enclosing ifdef or else-part." | |
692 (interactive) | |
693 (beginning-of-line) | |
694 (let ((start (point))) | |
695 (if (not (hif-looking-at-endif)) | |
696 (hif-find-previous-relevant)) | |
697 (if (hif-looking-at-endif) | |
698 (hif-endif-to-ifdef)) | |
699 (if (= start (point)) | |
210 | 700 (error "No previous #ifdef")))) |
31 | 701 |
702 (defun next-ifdef (&optional arg) | |
703 "Move to the beginning of the next #ifX, #else, or #endif. | |
210 | 704 With argument, do this that many times." |
31 | 705 (interactive "p") |
706 (or arg (setq arg 1)) | |
707 (if (< arg 0) | |
708 (previous-ifdef (- arg))) | |
709 (while (< 0 arg) | |
710 (setq arg (1- arg)) | |
711 (hif-find-next-relevant) | |
712 (if (eolp) | |
713 (progn | |
714 (beginning-of-line) | |
210 | 715 (error "No following #ifdefs, #elses, or #endifs"))))) |
31 | 716 |
717 (defun previous-ifdef (&optional arg) | |
718 "Move to the beginning of the previous #ifX, #else, or #endif. | |
210 | 719 With argument, do this that many times." |
31 | 720 (interactive "p") |
721 (or arg (setq arg 1)) | |
722 (if (< arg 0) | |
723 (next-ifdef (- arg))) | |
724 (while (< 0 arg) | |
725 (setq arg (1- arg)) | |
726 (let ((start (point))) | |
727 (hif-find-previous-relevant) | |
728 (if (= start (point)) | |
729 (error "No previous #ifdefs, #elses, or #endifs") | |
730 )))) | |
731 | |
732 | |
733 ;===%%SF%% parsing (End) === | |
734 | |
735 | |
736 ;===%%SF%% hide-ifdef-hiding (Start) === | |
737 | |
738 | |
739 ;;; A range is a structure with four components: | |
740 ;;; ELSE-P True if there was an else clause for the ifdef. | |
741 ;;; START The start of the range. (beginning of line) | |
742 ;;; ELSE The else marker (beginning of line) | |
743 ;;; Only valid if ELSE-P is true. | |
744 ;;; END The end of the range. (beginning of line) | |
745 | |
746 (defun hif-make-range (else-p start end &optional else) | |
747 (list else-p start else end)) | |
748 | |
749 (defun hif-range-else-p (range) (elt range 0)) | |
750 (defun hif-range-start (range) (elt range 1)) | |
751 (defun hif-range-else (range) (elt range 2)) | |
752 (defun hif-range-end (range) (elt range 3)) | |
753 | |
754 | |
755 | |
756 ;;; Find-Range | |
757 ;;; The workhorse, it delimits the #if region. Reasonably simple: | |
758 ;;; Skip until an #else or #endif is found, remembering positions. If | |
759 ;;; an #else was found, skip some more, looking for the true #endif. | |
760 | |
761 (defun hif-find-range () | |
762 "Returns a Range structure describing the current #if region. | |
763 Point is left unchanged." | |
764 ; (message "hif-find-range at %d" (point)) | |
765 (save-excursion | |
766 (beginning-of-line) | |
767 (let ((start (point)) | |
768 (else-p nil) | |
769 (else nil) | |
770 (end nil)) | |
771 ;; Part one. Look for either #endif or #else. | |
772 ;; This loop-and-a-half dedicated to E. Dijkstra. | |
773 (hif-find-next-relevant) | |
774 (while (hif-looking-at-ifX) ; Skip nested ifdef | |
775 (hif-ifdef-to-endif) | |
776 (hif-find-next-relevant)) | |
777 ;; Found either a #else or an #endif. | |
778 (cond ((hif-looking-at-else) | |
779 (setq else-p t) | |
780 (setq else (point))) | |
781 (t | |
782 (setq end (point)) ; (save-excursion (end-of-line) (point)) | |
783 )) | |
784 ;; If found #else, look for #endif. | |
785 (if else-p | |
786 (progn | |
787 (hif-find-next-relevant) | |
788 (while (hif-looking-at-ifX) ; Skip nested ifdef | |
789 (hif-ifdef-to-endif) | |
790 (hif-find-next-relevant)) | |
791 (if (hif-looking-at-else) | |
792 (error "Found two elses in a row? Broken!")) | |
793 (setq end (point)) ; (save-excursion (end-of-line) (point)) | |
794 )) | |
795 (hif-make-range else-p start end else)))) | |
796 | |
797 | |
798 ;;; A bit slimy. | |
799 ;;; 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 | |
801 ;;; one, we'd throw off all the counts. Feh. | |
802 | |
803 (defun hif-hide-line (point) | |
210 | 804 "Hide the line containing point. Does nothing if `hide-ifdef-lines' is nil." |
31 | 805 (if hide-ifdef-lines |
806 (save-excursion | |
807 (goto-char point) | |
808 (let ((modp (buffer-modified-p))) | |
809 (unwind-protect | |
810 (progn | |
811 (beginning-of-line) | |
812 (if (not (= (point) 1)) | |
813 (hide-ifdef-region (1- (point)) (point)))) | |
814 (set-buffer-modified-p modp)) | |
815 )) | |
816 )) | |
817 | |
818 | |
819 ;;; Hif-Possibly-Hide | |
820 ;;; There are four cases. The #ifX expression is "taken" if it | |
821 ;;; the hide-ifdef-evaluator returns T. Presumably, this means the code | |
822 ;;; inside the #ifdef would be included when the program was | |
823 ;;; compiled. | |
824 ;;; | |
825 ;;; Case 1: #ifX taken, and there's an #else. | |
826 ;;; The #else part must be hidden. The #if (then) part must be | |
827 ;;; processed for nested #ifX's. | |
828 ;;; Case 2: #ifX taken, and there's no #else. | |
829 ;;; The #if part must be processed for nested #ifX's. | |
830 ;;; Case 3: #ifX not taken, and there's an #else. | |
831 ;;; The #if part must be hidden. The #else part must be processed | |
832 ;;; for nested #ifs. | |
833 ;;; Case 4: #ifX not taken, and there's no #else. | |
834 ;;; The #ifX part must be hidden. | |
835 ;;; | |
836 ;;; Further processing is done by narrowing to the relevant region | |
837 ;;; and just recursively calling hide-ifdef-guts. | |
838 ;;; | |
839 ;;; When hif-possibly-hide returns, point is at the end of the | |
840 ;;; possibly-hidden range. | |
841 | |
842 (defun hif-recurse-on (start end) | |
210 | 843 "Call `hide-ifdef-guts' after narrowing to end of START line and END line." |
31 | 844 (save-excursion |
845 (save-restriction | |
846 (goto-char start) | |
847 (end-of-line) | |
848 (narrow-to-region (point) end) | |
849 (hide-ifdef-guts)))) | |
850 | |
851 (defun hif-possibly-hide () | |
13855 | 852 "Called at #ifX expression, this hides those parts that should be hidden. |
853 It uses the judgement of `hide-ifdef-evaluator'." | |
31 | 854 ; (message "hif-possibly-hide") (sit-for 1) |
855 (let ((test (hif-canonicalize)) | |
856 (range (hif-find-range))) | |
857 ; (message "test = %s" test) (sit-for 1) | |
858 | |
859 (hif-hide-line (hif-range-end range)) | |
860 (if (funcall hide-ifdef-evaluator test) | |
861 (cond ((hif-range-else-p range) ; case 1 | |
862 (hif-hide-line (hif-range-else range)) | |
863 (hide-ifdef-region (hif-range-else range) | |
864 (1- (hif-range-end range))) | |
865 (hif-recurse-on (hif-range-start range) | |
866 (hif-range-else range))) | |
867 (t ; case 2 | |
868 (hif-recurse-on (hif-range-start range) | |
869 (hif-range-end range)))) | |
870 (cond ((hif-range-else-p range) ; case 3 | |
871 (hif-hide-line (hif-range-else range)) | |
872 (hide-ifdef-region (hif-range-start range) | |
873 (1- (hif-range-else range))) | |
874 (hif-recurse-on (hif-range-else range) | |
875 (hif-range-end range))) | |
876 (t ; case 4 | |
877 (hide-ifdef-region (point) | |
878 (1- (hif-range-end range)))) | |
879 )) | |
880 (hif-hide-line (hif-range-start range)) ; Always hide start. | |
881 (goto-char (hif-range-end range)) | |
882 (end-of-line) | |
883 )) | |
884 | |
885 | |
886 | |
887 (defun hide-ifdef-guts () | |
13855 | 888 "Does most of the work of `hide-ifdefs'. |
889 It does not do the work that's pointless to redo on a recursive entry." | |
31 | 890 ; (message "hide-ifdef-guts") |
891 (save-excursion | |
892 (goto-char (point-min)) | |
893 (while (hif-find-any-ifX) | |
894 (hif-possibly-hide)))) | |
895 | |
896 ;===%%SF%% hide-ifdef-hiding (End) === | |
897 | |
898 | |
899 ;===%%SF%% exports (Start) === | |
900 | |
998 | 901 ;;;###autoload |
20781 | 902 (defcustom hide-ifdef-initially nil |
903 "*Non-nil means call `hide-ifdefs' when Hide-Ifdef mode is first activated." | |
904 :type 'boolean | |
905 :group 'hide-ifdef) | |
31 | 906 |
998 | 907 ;;;###autoload |
20781 | 908 (defcustom hide-ifdef-read-only nil |
909 "*Set to non-nil if you want buffer to be read-only while hiding text." | |
910 :type 'boolean | |
911 :group 'hide-ifdef) | |
31 | 912 |
998 | 913 ;;;###autoload |
20781 | 914 (defcustom hide-ifdef-lines nil |
915 "*Non-nil means hide the #ifX, #else, and #endif lines." | |
916 :type 'boolean | |
917 :group 'hide-ifdef) | |
31 | 918 |
919 (defun hide-ifdef-toggle-read-only () | |
920 "Toggle hide-ifdef-read-only." | |
921 (interactive) | |
922 (setq hide-ifdef-read-only (not hide-ifdef-read-only)) | |
923 (message "Hide-Read-Only %s" | |
924 (if hide-ifdef-read-only "ON" "OFF")) | |
925 (if hide-ifdef-hiding | |
926 (setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only))) | |
11581
e21661a78292
(hide-ifdef-toggle-read-only): Use force-mode-line-update.
Karl Heuer <kwzh@gnu.org>
parents:
11455
diff
changeset
|
927 (force-mode-line-update)) |
31 | 928 |
929 (defun hide-ifdef-toggle-outside-read-only () | |
13855 | 930 "Replacement for `toggle-read-only' within Hide-Ifdef mode." |
31 | 931 (interactive) |
932 (setq hif-outside-read-only (not hif-outside-read-only)) | |
933 (message "Read only %s" | |
934 (if hif-outside-read-only "ON" "OFF")) | |
935 (setq buffer-read-only | |
936 (or (and hide-ifdef-hiding hide-ifdef-read-only) | |
937 hif-outside-read-only) | |
938 ) | |
11581
e21661a78292
(hide-ifdef-toggle-read-only): Use force-mode-line-update.
Karl Heuer <kwzh@gnu.org>
parents:
11455
diff
changeset
|
939 (force-mode-line-update)) |
31 | 940 |
941 | |
942 (defun hide-ifdef-define (var) | |
943 "Define a VAR so that #ifdef VAR would be included." | |
944 (interactive "SDefine what? ") | |
8914 | 945 (hif-set-var var 1) |
31 | 946 (if hide-ifdef-hiding (hide-ifdefs))) |
947 | |
948 (defun hide-ifdef-undef (var) | |
949 "Undefine a VAR so that #ifdef VAR would not be included." | |
950 (interactive "SUndefine what? ") | |
951 (hif-set-var var nil) | |
952 (if hide-ifdef-hiding (hide-ifdefs))) | |
953 | |
954 | |
13855 | 955 (defun hide-ifdefs (&optional nomsg) |
1476 | 956 "Hide the contents of some #ifdefs. |
957 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 | |
959 preprocessor if it were given the file with those symbols defined. | |
31 | 960 |
8375 | 961 Turn off hiding by calling `show-ifdefs'." |
31 | 962 |
963 (interactive) | |
964 (message "Hiding...") | |
13856
8046a1616264
(hide-ifdefs): Set hif-outside-read-only here.
Richard M. Stallman <rms@gnu.org>
parents:
13855
diff
changeset
|
965 (setq hif-outside-read-only buffer-read-only) |
31 | 966 (if (not hide-ifdef-mode) |
967 (hide-ifdef-mode 1)) ; turn on hide-ifdef-mode | |
968 (if hide-ifdef-hiding | |
969 (show-ifdefs)) ; Otherwise, deep confusion. | |
7324
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
970 (let ((inhibit-read-only t)) |
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
971 (setq selective-display t) |
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
972 (setq hide-ifdef-hiding t) |
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
973 (hide-ifdef-guts)) |
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
974 (setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only)) |
13855 | 975 (or nomsg |
976 (message "Hiding done"))) | |
31 | 977 |
978 | |
979 (defun show-ifdefs () | |
13855 | 980 "Cancel the effects of `hide-ifdef': show the contents of all #ifdefs." |
31 | 981 (interactive) |
7324
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
982 (setq buffer-read-only hif-outside-read-only) |
31 | 983 (setq selective-display nil) ; defaults |
7324
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
984 (let ((inhibit-read-only t)) |
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
985 (hif-show-all)) |
210 | 986 (setq hide-ifdef-hiding nil)) |
31 | 987 |
988 | |
989 (defun hif-find-ifdef-block () | |
3591
507f64624555
Apply typo patches from Paul Eggert.
Jim Blandy <jimb@redhat.com>
parents:
3457
diff
changeset
|
990 "Utility for hide and show `ifdef-block'. |
23440
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
991 Return as (TOP . BOTTOM) the extent of ifdef block." |
31 | 992 (let (max-bottom) |
23440
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
993 (cons (save-excursion |
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
994 (beginning-of-line) |
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
995 (if (not (or (hif-looking-at-else) (hif-looking-at-ifX))) |
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
996 (up-ifdef)) |
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
997 (prog1 (point) |
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
998 (hif-ifdef-to-endif) |
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
999 (setq max-bottom (1- (point))))) |
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
1000 (save-excursion |
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
1001 (beginning-of-line) |
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
1002 (if (not (hif-looking-at-endif)) |
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
1003 (hif-find-next-relevant)) |
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
1004 (while (hif-looking-at-ifX) |
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
1005 (hif-ifdef-to-endif) |
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
1006 (hif-find-next-relevant)) |
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
1007 (min max-bottom (1- (point))))))) |
31 | 1008 |
1009 | |
1010 (defun hide-ifdef-block () | |
1011 "Hide the ifdef block (true or false part) enclosing or before the cursor." | |
1012 (interactive) | |
1013 (if (not hide-ifdef-mode) | |
1014 (hide-ifdef-mode 1)) | |
1015 (setq selective-display t) | |
23440
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
1016 (let ((top-bottom (hif-find-ifdef-block)) |
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
1017 (inhibit-read-only t)) |
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
1018 (hide-ifdef-region (car top-bottom) (cdr top-bottom)) |
31 | 1019 (if hide-ifdef-lines |
1020 (progn | |
23440
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
1021 (hif-hide-line (car top-bottom)) |
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
1022 (hif-hide-line (1+ (cdr top-bottom))))) |
210 | 1023 (setq hide-ifdef-hiding t)) |
7324
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
1024 (setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only))) |
31 | 1025 |
1026 | |
1027 (defun show-ifdef-block () | |
1028 "Show the ifdef block (true or false part) enclosing or before the cursor." | |
1029 (interactive) | |
7324
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
1030 (let ((inhibit-read-only t)) |
31 | 1031 (if hide-ifdef-lines |
1032 (save-excursion | |
1033 (beginning-of-line) | |
1034 (hif-show-ifdef-region (1- (point)) (progn (end-of-line) (point)))) | |
1035 | |
23440
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
1036 (let ((top-bottom (hif-find-ifdef-block))) |
21dd0dd62240
(hide-ifdef-env, hif-outside-read-only):
Andreas Schwab <schwab@suse.de>
parents:
20781
diff
changeset
|
1037 (hif-show-ifdef-region (1- (car top-bottom)) (cdr top-bottom)))))) |
31 | 1038 |
1039 | |
3591
507f64624555
Apply typo patches from Paul Eggert.
Jim Blandy <jimb@redhat.com>
parents:
3457
diff
changeset
|
1040 ;;; definition alist support |
31 | 1041 |
1042 (defvar hide-ifdef-define-alist nil | |
1043 "A global assoc list of pre-defined symbol lists") | |
1044 | |
1045 (defun hif-compress-define-list (env) | |
1046 "Compress the define list ENV into a list of defined symbols only." | |
29588
0cf06902af64
(hif-compress-define-list)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26519
diff
changeset
|
1047 (let ((defs (mapcar (lambda (arg) |
0cf06902af64
(hif-compress-define-list)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26519
diff
changeset
|
1048 (if (hif-lookup (car arg)) (car arg))) |
31 | 1049 env)) |
1050 (new-defs nil)) | |
1051 (while defs | |
1052 (if (car defs) | |
1053 (setq new-defs (cons (car defs) new-defs))) | |
1054 (setq defs (cdr defs))) | |
210 | 1055 new-defs)) |
31 | 1056 |
1057 (defun hide-ifdef-set-define-alist (name) | |
1476 | 1058 "Set the association for NAME to `hide-ifdef-env'." |
31 | 1059 (interactive "SSet define list: ") |
1060 (setq hide-ifdef-define-alist | |
1061 (cons (cons name (hif-compress-define-list hide-ifdef-env)) | |
210 | 1062 hide-ifdef-define-alist))) |
31 | 1063 |
1064 (defun hide-ifdef-use-define-alist (name) | |
1476 | 1065 "Set `hide-ifdef-env' to the define list specified by NAME." |
31 | 1066 (interactive "SUse define list: ") |
1067 (let ((define-list (assoc name hide-ifdef-define-alist))) | |
1068 (if define-list | |
1069 (setq hide-ifdef-env | |
29588
0cf06902af64
(hif-compress-define-list)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
26519
diff
changeset
|
1070 (mapcar (lambda (arg) (cons arg t)) |
31 | 1071 (cdr define-list))) |
1072 (error "No define list for %s" name)) | |
210 | 1073 (if hide-ifdef-hiding (hide-ifdefs)))) |
31 | 1074 |
13297
9699410bf8bf
Provide hideif; require cc-mode.
Richard M. Stallman <rms@gnu.org>
parents:
12954
diff
changeset
|
1075 (provide 'hideif) |
9699410bf8bf
Provide hideif; require cc-mode.
Richard M. Stallman <rms@gnu.org>
parents:
12954
diff
changeset
|
1076 |
660
08eb386dd0f3
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
210
diff
changeset
|
1077 ;;; hideif.el ends here |
08eb386dd0f3
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
210
diff
changeset
|
1078 |