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