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