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