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