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