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