Mercurial > emacs
annotate lisp/progmodes/hideif.el @ 13995:33b51573611f
(makefile-insert-special-target,
makefile-backslash-region): Doc fix.
author | Karl Heuer <kwzh@gnu.org> |
---|---|
date | Thu, 04 Jan 1996 23:44:52 +0000 |
parents | 8046a1616264 |
children | 6fb89c4b21c3 |
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)) | |
13855 | 242 (message "Enter Hide-Ifdef mode") |
31 | 243 ) |
244 ; else end hide-ifdef-mode | |
245 (if hide-ifdef-hiding | |
246 (show-ifdefs)) | |
13855 | 247 (message "Exit Hide-Ifdef mode") |
31 | 248 )) |
249 | |
250 | |
251 ;; from outline.el with docstring fixed. | |
252 (defun hif-outline-flag-region (from to flag) | |
13855 | 253 "Hides or shows lines from FROM to TO, according to FLAG. |
254 If FLAG is \\n (newline character) then text is shown, while if FLAG is \\^M | |
31 | 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 | |
13855 | 269 ;; By putting this on after-revert-hook, we arrange that it only |
270 ;; does anything when revert-buffer avoids turning off the mode. | |
271 ;; (That can happen in VC.) | |
272 (defun hif-before-revert-function () | |
273 (and hide-ifdef-mode hide-ifdef-hiding | |
274 (hide-ifdefs t))) | |
275 (add-hook 'after-revert-hook 'hif-before-revert-function) | |
276 | |
31 | 277 (defun hide-ifdef-region (start end) |
278 "START is the start of a #if or #else form. END is the ending part. | |
279 Everything including these lines is made invisible." | |
280 (hif-outline-flag-region start end ?\^M) | |
281 ) | |
282 | |
283 (defun hif-show-ifdef-region (start end) | |
284 "Everything between START and END is made visible." | |
285 (hif-outline-flag-region start end ?\n) | |
286 ) | |
287 | |
288 | |
289 | |
290 ;===%%SF%% evaluation (Start) === | |
291 | |
13855 | 292 ;; It is not useful to set this to anything but `eval'. |
293 ;; In fact, the variable might as well be eliminated. | |
31 | 294 (defvar hide-ifdef-evaluator 'eval |
13855 | 295 "The function to use to evaluate a form. |
296 The evaluator is given a canonical form and returns t if text under | |
31 | 297 that form should be displayed.") |
298 | |
299 (defvar hif-undefined-symbol nil | |
300 "...is by default considered to be false.") | |
301 | |
302 (defvar hide-ifdef-env nil | |
303 "An alist of defined symbols and their values.") | |
304 | |
305 | |
306 (defun hif-set-var (var value) | |
307 "Prepend (var value) pair to hide-ifdef-env." | |
308 (setq hide-ifdef-env (cons (cons var value) hide-ifdef-env))) | |
309 | |
310 | |
311 (defun hif-lookup (var) | |
312 ; (message "hif-lookup %s" var) | |
313 (let ((val (assoc var hide-ifdef-env))) | |
314 (if val | |
315 (cdr val) | |
316 hif-undefined-symbol))) | |
317 | |
318 (defun hif-defined (var) | |
319 (hif-lookup var) | |
320 ; when #if expressions are fully supported, defined result should be 1 | |
321 ; (if (assoc var hide-ifdef-env) | |
322 ; 1 | |
323 ; nil) | |
324 ) | |
325 | |
326 | |
327 ;===%%SF%% evaluation (End) === | |
328 | |
329 | |
330 | |
331 ;===%%SF%% parsing (Start) === | |
332 ;;; The code that understands what ifs and ifdef in files look like. | |
333 | |
334 (defconst hif-cpp-prefix "\\(^\\|\r\\)[ \t]*#[ \t]*") | |
335 (defconst hif-ifndef-regexp (concat hif-cpp-prefix "ifndef")) | |
336 (defconst hif-ifx-regexp (concat hif-cpp-prefix "if\\(n?def\\)?[ \t]+")) | |
337 (defconst hif-else-regexp (concat hif-cpp-prefix "else")) | |
338 (defconst hif-endif-regexp (concat hif-cpp-prefix "endif")) | |
339 (defconst hif-ifx-else-endif-regexp | |
340 (concat hif-ifx-regexp "\\|" hif-else-regexp "\\|" hif-endif-regexp)) | |
341 | |
342 | |
343 (defun hif-infix-to-prefix (token-list) | |
344 "Convert list of tokens in infix into prefix list" | |
345 ; (message "hif-infix-to-prefix: %s" token-list) | |
346 (if (= 1 (length token-list)) | |
347 (` (hif-lookup (quote (, (car token-list))))) | |
348 (hif-parse-if-exp token-list)) | |
349 ) | |
350 | |
351 ; pattern to match initial identifier, !, &&, ||, (, or ). | |
8914 | 352 ; Added ==, + and -: garyo@avs.com 8/9/94 |
353 (defconst hif-token-regexp "^\\(!\\|&&\\|||\\|[!=]=\\|[()+-]\\|\\w+\\)") | |
31 | 354 (defconst hif-end-of-comment "\\*/") |
355 | |
356 | |
357 (defun hif-tokenize (expr-string) | |
358 "Separate string into a list of tokens" | |
359 (let ((token-list nil) | |
360 (expr-start 0) | |
7379
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
361 (expr-length (length expr-string)) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
362 (current-syntax-table (syntax-table))) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
363 (unwind-protect |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
364 (progn |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
365 (set-syntax-table hide-ifdef-syntax-table) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
366 (while (< expr-start expr-length) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
367 ; (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
|
368 (cond |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
369 ((string-match "^[ \t]+" expr-string expr-start) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
370 ;; skip whitespace |
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 ;; 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
|
373 (aset expr-string (1- expr-start) ?\n)) |
31 | 374 |
7379
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)) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
377 (aset expr-string (1- expr-start) ?\n) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
378 (or |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
379 (string-match hif-end-of-comment |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
380 expr-string expr-start) ; eat comment |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
381 (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
|
382 (setq expr-start (match-end 0)) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
383 (aset expr-string (1- expr-start) ?\n)) |
31 | 384 |
7379
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
385 ((string-match "^//" expr-string expr-start) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
386 (string-match "$" expr-string expr-start) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
387 (setq expr-start (match-end 0))) |
5560
a9924c6db247
(hif-tokenize): Handle // comments after #if.
Richard M. Stallman <rms@gnu.org>
parents:
3591
diff
changeset
|
388 |
7379
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
389 ((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
|
390 (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
|
391 (setq expr-start (match-end 0)) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
392 (aset expr-string (1- expr-start) ?\n) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
393 ; (message "token: %s" token) (sit-for 1) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
394 (setq token-list |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
395 (cons |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
396 (cond |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
397 ((string-equal token "||") 'or) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
398 ((string-equal token "&&") 'and) |
8914 | 399 ((string-equal token "==") 'equal) |
400 ((string-equal token "!=") 'hif-notequal) | |
7379
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
401 ((string-equal token "!") 'not) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
402 ((string-equal token "defined") 'hif-defined) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
403 ((string-equal token "(") 'lparen) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
404 ((string-equal token ")") 'rparen) |
8914 | 405 ((string-equal token "+") 'hif-plus) |
406 ((string-equal token "-") 'hif-minus) | |
7379
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
407 (t (intern token))) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
408 token-list)))) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
409 (t (error "Bad #if expression: %s" expr-string))))) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
410 (set-syntax-table current-syntax-table)) |
822429149720
(hide-ifdef-syntax-table): New variable.
Karl Heuer <kwzh@gnu.org>
parents:
7324
diff
changeset
|
411 (nreverse token-list))) |
31 | 412 |
413 ;;;----------------------------------------------------------------- | |
414 ;;; Translate C preprocessor #if expressions using recursive descent. | |
415 ;;; This parser is limited to the operators &&, ||, !, and "defined". | |
8914 | 416 ;;; Added ==, !=, +, and -. Gary Oberbrunner, garyo@avs.com, 8/9/94 |
31 | 417 |
418 (defun hif-parse-if-exp (token-list) | |
419 "Parse the TOKEN-LIST. Return translated list in prefix form." | |
420 (hif-nexttoken) | |
421 (prog1 | |
422 (hif-expr) | |
423 (if token ; is there still a token? | |
210 | 424 (error "Error: unexpected token: %s" token)))) |
31 | 425 |
426 (defun hif-nexttoken () | |
427 "Pop the next token from token-list into the let variable \"token\"." | |
428 (setq token (car token-list)) | |
429 (setq token-list (cdr token-list)) | |
210 | 430 token) |
31 | 431 |
432 (defun hif-expr () | |
13855 | 433 "Parse an expression as found in #if. |
31 | 434 expr : term | expr '||' term." |
435 (let ((result (hif-term))) | |
436 (while (eq token 'or) | |
437 (hif-nexttoken) | |
438 (setq result (list 'or result (hif-term)))) | |
210 | 439 result)) |
31 | 440 |
441 (defun hif-term () | |
13855 | 442 "Parse a term : eq-expr | term '&&' eq-expr." |
8914 | 443 (let ((result (hif-eq-expr))) |
31 | 444 (while (eq token 'and) |
445 (hif-nexttoken) | |
8914 | 446 (setq result (list 'and result (hif-eq-expr)))) |
210 | 447 result)) |
31 | 448 |
8914 | 449 (defun hif-eq-expr () |
13855 | 450 "Parse an eq-expr : math | eq-expr '=='|'!=' math." |
8914 | 451 (let ((result (hif-math)) |
452 (eq-token nil)) | |
453 (while (or (eq token 'equal) (eq token 'hif-notequal)) | |
454 (setq eq-token token) | |
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)) | |
464 (while (or (eq token 'hif-plus) (eq token 'hif-minus)) | |
465 (setq math-op token) | |
466 (hif-nexttoken) | |
467 (setq result (list math-op result (hif-factor)))) | |
468 result)) | |
469 | |
31 | 470 (defun hif-factor () |
13855 | 471 "Parse a factor: '!' factor | '(' expr ')' | 'defined(' id ')' | id." |
31 | 472 (cond |
473 ((eq token 'not) | |
474 (hif-nexttoken) | |
475 (list 'not (hif-factor))) | |
476 | |
477 ((eq token 'lparen) | |
478 (hif-nexttoken) | |
479 (let ((result (hif-expr))) | |
480 (if (not (eq token 'rparen)) | |
481 (error "Bad token in parenthesized expression: %s" token) | |
482 (hif-nexttoken) | |
483 result))) | |
484 | |
485 ((eq token 'hif-defined) | |
486 (hif-nexttoken) | |
487 (if (not (eq token 'lparen)) | |
488 (error "Error: expected \"(\" after \"defined\"")) | |
489 (hif-nexttoken) | |
490 (let ((ident token)) | |
491 (if (memq token '(or and not hif-defined lparen rparen)) | |
492 (error "Error: unexpected token: %s" token)) | |
493 (hif-nexttoken) | |
494 (if (not (eq token 'rparen)) | |
495 (error "Error: expected \")\" after identifier")) | |
496 (hif-nexttoken) | |
497 (` (hif-defined (quote (, ident)))) | |
498 )) | |
499 | |
500 (t ; identifier | |
501 (let ((ident token)) | |
502 (if (memq ident '(or and)) | |
503 (error "Error: missing identifier")) | |
504 (hif-nexttoken) | |
505 (` (hif-lookup (quote (, ident)))) | |
506 )) | |
507 )) | |
508 | |
8914 | 509 (defun hif-mathify (val) |
510 "Treat VAL as a number: if it's t or nil, use 1 or 0." | |
511 (cond ((eq val t) | |
512 1) | |
513 ((null val) | |
514 0) | |
515 (t val))) | |
516 | |
517 (defun hif-plus (a b) | |
518 "Like ordinary plus but treat t and nil as 1 and 0." | |
519 (+ (hif-mathify a) (hif-mathify b))) | |
520 (defun hif-minus (a b) | |
521 "Like ordinary minus but treat t and nil as 1 and 0." | |
522 (- (hif-mathify a) (hif-mathify b))) | |
523 (defun hif-notequal (a b) | |
524 "Like (not (equal A B)) but as one symbol." | |
525 (not (equal a b))) | |
526 | |
31 | 527 ;;;----------- end of parser ----------------------- |
528 | |
529 | |
530 (defun hif-canonicalize () | |
13855 | 531 "When at beginning of #ifX, returns a Lisp expression for its condition." |
31 | 532 (save-excursion |
533 (let ((negate (looking-at hif-ifndef-regexp))) | |
534 (re-search-forward hif-ifx-regexp) | |
535 (let* ((expr-string | |
536 (buffer-substring (point) | |
537 (progn (skip-chars-forward "^\n\r") (point)))) | |
538 (expr (hif-infix-to-prefix (hif-tokenize expr-string)))) | |
539 ; (message "hif-canonicalized: %s" expr) | |
540 (if negate | |
541 (list 'not expr) | |
542 expr))))) | |
543 | |
544 | |
545 (defun hif-find-any-ifX () | |
13855 | 546 "Move to next #if..., or #ifndef, at point or after." |
31 | 547 ; (message "find ifX at %d" (point)) |
548 (prog1 | |
549 (re-search-forward hif-ifx-regexp (point-max) t) | |
550 (beginning-of-line))) | |
551 | |
552 | |
553 (defun hif-find-next-relevant () | |
13855 | 554 "Move to next #if..., #else, or #endif, after the current line." |
31 | 555 ; (message "hif-find-next-relevant at %d" (point)) |
556 (end-of-line) | |
557 ; avoid infinite recursion by only going to beginning of line if match found | |
558 (if (re-search-forward hif-ifx-else-endif-regexp (point-max) t) | |
210 | 559 (beginning-of-line))) |
31 | 560 |
561 (defun hif-find-previous-relevant () | |
13855 | 562 "Move to previous #if..., #else, or #endif, before the current line." |
31 | 563 ; (message "hif-find-previous-relevant at %d" (point)) |
564 (beginning-of-line) | |
565 ; avoid infinite recursion by only going to beginning of line if match found | |
566 (if (re-search-backward hif-ifx-else-endif-regexp (point-min) t) | |
210 | 567 (beginning-of-line))) |
31 | 568 |
569 | |
570 (defun hif-looking-at-ifX () ;; Should eventually see #if | |
571 (looking-at hif-ifx-regexp)) | |
572 (defun hif-looking-at-endif () | |
573 (looking-at hif-endif-regexp)) | |
574 (defun hif-looking-at-else () | |
575 (looking-at hif-else-regexp)) | |
576 | |
577 | |
578 | |
579 (defun hif-ifdef-to-endif () | |
580 "If positioned at #ifX or #else form, skip to corresponding #endif." | |
581 ; (message "hif-ifdef-to-endif at %d" (point)) (sit-for 1) | |
582 (hif-find-next-relevant) | |
583 (cond ((hif-looking-at-ifX) | |
584 (hif-ifdef-to-endif) ; find endif of nested if | |
585 (hif-ifdef-to-endif)) ; find outer endif or else | |
586 ((hif-looking-at-else) | |
587 (hif-ifdef-to-endif)) ; find endif following else | |
588 ((hif-looking-at-endif) | |
589 'done) | |
590 (t | |
3591
507f64624555
Apply typo patches from Paul Eggert.
Jim Blandy <jimb@redhat.com>
parents:
3457
diff
changeset
|
591 (error "Mismatched #ifdef #endif pair")))) |
31 | 592 |
593 | |
594 (defun hif-endif-to-ifdef () | |
595 "If positioned at #endif form, skip backward to corresponding #ifX." | |
596 ; (message "hif-endif-to-ifdef at %d" (point)) | |
597 (let ((start (point))) | |
598 (hif-find-previous-relevant) | |
599 (if (= start (point)) | |
3591
507f64624555
Apply typo patches from Paul Eggert.
Jim Blandy <jimb@redhat.com>
parents:
3457
diff
changeset
|
600 (error "Mismatched #ifdef #endif pair"))) |
31 | 601 (cond ((hif-looking-at-endif) |
602 (hif-endif-to-ifdef) ; find beginning of nested if | |
603 (hif-endif-to-ifdef)) ; find beginning of outer if or else | |
604 ((hif-looking-at-else) | |
605 (hif-endif-to-ifdef)) | |
606 ((hif-looking-at-ifX) | |
607 'done) | |
922 | 608 (t))) ; never gets here |
31 | 609 |
610 | |
611 (defun forward-ifdef (&optional arg) | |
612 "Move point to beginning of line of the next ifdef-endif. | |
210 | 613 With argument, do this that many times." |
31 | 614 (interactive "p") |
615 (or arg (setq arg 1)) | |
616 (if (< arg 0) | |
617 (backward-ifdef (- arg))) | |
618 (while (< 0 arg) | |
619 (setq arg (- arg)) | |
620 (let ((start (point))) | |
621 (if (not (hif-looking-at-ifX)) | |
622 (hif-find-next-relevant)) | |
623 (if (hif-looking-at-ifX) | |
624 (hif-ifdef-to-endif) | |
625 (goto-char start) | |
626 (error "No following #ifdef") | |
627 )))) | |
628 | |
629 | |
630 (defun backward-ifdef (&optional arg) | |
631 "Move point to beginning of the previous ifdef-endif. | |
210 | 632 With argument, do this that many times." |
31 | 633 (interactive "p") |
634 (or arg (setq arg 1)) | |
635 (if (< arg 0) | |
636 (forward-ifdef (- arg))) | |
637 (while (< 0 arg) | |
638 (setq arg (1- arg)) | |
639 (beginning-of-line) | |
640 (let ((start (point))) | |
641 (if (not (hif-looking-at-endif)) | |
642 (hif-find-previous-relevant)) | |
643 (if (hif-looking-at-endif) | |
644 (hif-endif-to-ifdef) | |
645 (goto-char start) | |
210 | 646 (error "No previous #ifdef"))))) |
31 | 647 |
648 | |
649 (defun down-ifdef () | |
650 "Move point to beginning of nested ifdef or else-part." | |
651 (interactive) | |
652 (let ((start (point))) | |
653 (hif-find-next-relevant) | |
654 (if (or (hif-looking-at-ifX) (hif-looking-at-else)) | |
655 () | |
656 (goto-char start) | |
210 | 657 (error "No following #ifdef")))) |
31 | 658 |
659 | |
660 (defun up-ifdef () | |
661 "Move point to beginning of enclosing ifdef or else-part." | |
662 (interactive) | |
663 (beginning-of-line) | |
664 (let ((start (point))) | |
665 (if (not (hif-looking-at-endif)) | |
666 (hif-find-previous-relevant)) | |
667 (if (hif-looking-at-endif) | |
668 (hif-endif-to-ifdef)) | |
669 (if (= start (point)) | |
210 | 670 (error "No previous #ifdef")))) |
31 | 671 |
672 (defun next-ifdef (&optional arg) | |
673 "Move to the beginning of the next #ifX, #else, or #endif. | |
210 | 674 With argument, do this that many times." |
31 | 675 (interactive "p") |
676 (or arg (setq arg 1)) | |
677 (if (< arg 0) | |
678 (previous-ifdef (- arg))) | |
679 (while (< 0 arg) | |
680 (setq arg (1- arg)) | |
681 (hif-find-next-relevant) | |
682 (if (eolp) | |
683 (progn | |
684 (beginning-of-line) | |
210 | 685 (error "No following #ifdefs, #elses, or #endifs"))))) |
31 | 686 |
687 (defun previous-ifdef (&optional arg) | |
688 "Move to the beginning of the previous #ifX, #else, or #endif. | |
210 | 689 With argument, do this that many times." |
31 | 690 (interactive "p") |
691 (or arg (setq arg 1)) | |
692 (if (< arg 0) | |
693 (next-ifdef (- arg))) | |
694 (while (< 0 arg) | |
695 (setq arg (1- arg)) | |
696 (let ((start (point))) | |
697 (hif-find-previous-relevant) | |
698 (if (= start (point)) | |
699 (error "No previous #ifdefs, #elses, or #endifs") | |
700 )))) | |
701 | |
702 | |
703 ;===%%SF%% parsing (End) === | |
704 | |
705 | |
706 ;===%%SF%% hide-ifdef-hiding (Start) === | |
707 | |
708 | |
709 ;;; A range is a structure with four components: | |
710 ;;; ELSE-P True if there was an else clause for the ifdef. | |
711 ;;; START The start of the range. (beginning of line) | |
712 ;;; ELSE The else marker (beginning of line) | |
713 ;;; Only valid if ELSE-P is true. | |
714 ;;; END The end of the range. (beginning of line) | |
715 | |
716 (defun hif-make-range (else-p start end &optional else) | |
717 (list else-p start else end)) | |
718 | |
719 (defun hif-range-else-p (range) (elt range 0)) | |
720 (defun hif-range-start (range) (elt range 1)) | |
721 (defun hif-range-else (range) (elt range 2)) | |
722 (defun hif-range-end (range) (elt range 3)) | |
723 | |
724 | |
725 | |
726 ;;; Find-Range | |
727 ;;; The workhorse, it delimits the #if region. Reasonably simple: | |
728 ;;; Skip until an #else or #endif is found, remembering positions. If | |
729 ;;; an #else was found, skip some more, looking for the true #endif. | |
730 | |
731 (defun hif-find-range () | |
732 "Returns a Range structure describing the current #if region. | |
733 Point is left unchanged." | |
734 ; (message "hif-find-range at %d" (point)) | |
735 (save-excursion | |
736 (beginning-of-line) | |
737 (let ((start (point)) | |
738 (else-p nil) | |
739 (else nil) | |
740 (end nil)) | |
741 ;; Part one. Look for either #endif or #else. | |
742 ;; This loop-and-a-half dedicated to E. Dijkstra. | |
743 (hif-find-next-relevant) | |
744 (while (hif-looking-at-ifX) ; Skip nested ifdef | |
745 (hif-ifdef-to-endif) | |
746 (hif-find-next-relevant)) | |
747 ;; Found either a #else or an #endif. | |
748 (cond ((hif-looking-at-else) | |
749 (setq else-p t) | |
750 (setq else (point))) | |
751 (t | |
752 (setq end (point)) ; (save-excursion (end-of-line) (point)) | |
753 )) | |
754 ;; If found #else, look for #endif. | |
755 (if else-p | |
756 (progn | |
757 (hif-find-next-relevant) | |
758 (while (hif-looking-at-ifX) ; Skip nested ifdef | |
759 (hif-ifdef-to-endif) | |
760 (hif-find-next-relevant)) | |
761 (if (hif-looking-at-else) | |
762 (error "Found two elses in a row? Broken!")) | |
763 (setq end (point)) ; (save-excursion (end-of-line) (point)) | |
764 )) | |
765 (hif-make-range else-p start end else)))) | |
766 | |
767 | |
768 ;;; A bit slimy. | |
769 ;;; NOTE: If there's an #ifdef at the beginning of the file, we can't | |
770 ;;; hide it. There's no previous newline to replace. If we added | |
771 ;;; one, we'd throw off all the counts. Feh. | |
772 | |
773 (defun hif-hide-line (point) | |
210 | 774 "Hide the line containing point. Does nothing if `hide-ifdef-lines' is nil." |
31 | 775 (if hide-ifdef-lines |
776 (save-excursion | |
777 (goto-char point) | |
778 (let ((modp (buffer-modified-p))) | |
779 (unwind-protect | |
780 (progn | |
781 (beginning-of-line) | |
782 (if (not (= (point) 1)) | |
783 (hide-ifdef-region (1- (point)) (point)))) | |
784 (set-buffer-modified-p modp)) | |
785 )) | |
786 )) | |
787 | |
788 | |
789 ;;; Hif-Possibly-Hide | |
790 ;;; There are four cases. The #ifX expression is "taken" if it | |
791 ;;; the hide-ifdef-evaluator returns T. Presumably, this means the code | |
792 ;;; inside the #ifdef would be included when the program was | |
793 ;;; compiled. | |
794 ;;; | |
795 ;;; Case 1: #ifX taken, and there's an #else. | |
796 ;;; The #else part must be hidden. The #if (then) part must be | |
797 ;;; processed for nested #ifX's. | |
798 ;;; Case 2: #ifX taken, and there's no #else. | |
799 ;;; The #if part must be processed for nested #ifX's. | |
800 ;;; Case 3: #ifX not taken, and there's an #else. | |
801 ;;; The #if part must be hidden. The #else part must be processed | |
802 ;;; for nested #ifs. | |
803 ;;; Case 4: #ifX not taken, and there's no #else. | |
804 ;;; The #ifX part must be hidden. | |
805 ;;; | |
806 ;;; Further processing is done by narrowing to the relevant region | |
807 ;;; and just recursively calling hide-ifdef-guts. | |
808 ;;; | |
809 ;;; When hif-possibly-hide returns, point is at the end of the | |
810 ;;; possibly-hidden range. | |
811 | |
812 (defun hif-recurse-on (start end) | |
210 | 813 "Call `hide-ifdef-guts' after narrowing to end of START line and END line." |
31 | 814 (save-excursion |
815 (save-restriction | |
816 (goto-char start) | |
817 (end-of-line) | |
818 (narrow-to-region (point) end) | |
819 (hide-ifdef-guts)))) | |
820 | |
821 (defun hif-possibly-hide () | |
13855 | 822 "Called at #ifX expression, this hides those parts that should be hidden. |
823 It uses the judgement of `hide-ifdef-evaluator'." | |
31 | 824 ; (message "hif-possibly-hide") (sit-for 1) |
825 (let ((test (hif-canonicalize)) | |
826 (range (hif-find-range))) | |
827 ; (message "test = %s" test) (sit-for 1) | |
828 | |
829 (hif-hide-line (hif-range-end range)) | |
830 (if (funcall hide-ifdef-evaluator test) | |
831 (cond ((hif-range-else-p range) ; case 1 | |
832 (hif-hide-line (hif-range-else range)) | |
833 (hide-ifdef-region (hif-range-else range) | |
834 (1- (hif-range-end range))) | |
835 (hif-recurse-on (hif-range-start range) | |
836 (hif-range-else range))) | |
837 (t ; case 2 | |
838 (hif-recurse-on (hif-range-start range) | |
839 (hif-range-end range)))) | |
840 (cond ((hif-range-else-p range) ; case 3 | |
841 (hif-hide-line (hif-range-else range)) | |
842 (hide-ifdef-region (hif-range-start range) | |
843 (1- (hif-range-else range))) | |
844 (hif-recurse-on (hif-range-else range) | |
845 (hif-range-end range))) | |
846 (t ; case 4 | |
847 (hide-ifdef-region (point) | |
848 (1- (hif-range-end range)))) | |
849 )) | |
850 (hif-hide-line (hif-range-start range)) ; Always hide start. | |
851 (goto-char (hif-range-end range)) | |
852 (end-of-line) | |
853 )) | |
854 | |
855 | |
856 | |
857 (defun hide-ifdef-guts () | |
13855 | 858 "Does most of the work of `hide-ifdefs'. |
859 It does not do the work that's pointless to redo on a recursive entry." | |
31 | 860 ; (message "hide-ifdef-guts") |
861 (save-excursion | |
862 (goto-char (point-min)) | |
863 (while (hif-find-any-ifX) | |
864 (hif-possibly-hide)))) | |
865 | |
866 ;===%%SF%% hide-ifdef-hiding (End) === | |
867 | |
868 | |
869 ;===%%SF%% exports (Start) === | |
870 | |
998 | 871 ;;;###autoload |
31 | 872 (defvar hide-ifdef-initially nil |
13855 | 873 "*Non-nil means call `hide-ifdefs' when Hide-Ifdef mode is first activated.") |
31 | 874 |
998 | 875 ;;;###autoload |
31 | 876 (defvar hide-ifdef-read-only nil |
877 "*Set to non-nil if you want buffer to be read-only while hiding text.") | |
878 | |
879 (defvar hif-outside-read-only nil | |
210 | 880 "Internal variable. Saves the value of `buffer-read-only' while hiding.") |
31 | 881 |
998 | 882 ;;;###autoload |
31 | 883 (defvar hide-ifdef-lines nil |
13855 | 884 "*Non-nil means hide the #ifX, #else, and #endif lines.") |
31 | 885 |
886 (defun hide-ifdef-toggle-read-only () | |
887 "Toggle hide-ifdef-read-only." | |
888 (interactive) | |
889 (setq hide-ifdef-read-only (not hide-ifdef-read-only)) | |
890 (message "Hide-Read-Only %s" | |
891 (if hide-ifdef-read-only "ON" "OFF")) | |
892 (if hide-ifdef-hiding | |
893 (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
|
894 (force-mode-line-update)) |
31 | 895 |
896 (defun hide-ifdef-toggle-outside-read-only () | |
13855 | 897 "Replacement for `toggle-read-only' within Hide-Ifdef mode." |
31 | 898 (interactive) |
899 (setq hif-outside-read-only (not hif-outside-read-only)) | |
900 (message "Read only %s" | |
901 (if hif-outside-read-only "ON" "OFF")) | |
902 (setq buffer-read-only | |
903 (or (and hide-ifdef-hiding hide-ifdef-read-only) | |
904 hif-outside-read-only) | |
905 ) | |
11581
e21661a78292
(hide-ifdef-toggle-read-only): Use force-mode-line-update.
Karl Heuer <kwzh@gnu.org>
parents:
11455
diff
changeset
|
906 (force-mode-line-update)) |
31 | 907 |
908 | |
909 (defun hide-ifdef-define (var) | |
910 "Define a VAR so that #ifdef VAR would be included." | |
911 (interactive "SDefine what? ") | |
8914 | 912 (hif-set-var var 1) |
31 | 913 (if hide-ifdef-hiding (hide-ifdefs))) |
914 | |
915 (defun hide-ifdef-undef (var) | |
916 "Undefine a VAR so that #ifdef VAR would not be included." | |
917 (interactive "SUndefine what? ") | |
918 (hif-set-var var nil) | |
919 (if hide-ifdef-hiding (hide-ifdefs))) | |
920 | |
921 | |
13855 | 922 (defun hide-ifdefs (&optional nomsg) |
1476 | 923 "Hide the contents of some #ifdefs. |
924 Assume that defined symbols have been added to `hide-ifdef-env'. | |
925 The text hidden is the text that would not be included by the C | |
926 preprocessor if it were given the file with those symbols defined. | |
31 | 927 |
8375 | 928 Turn off hiding by calling `show-ifdefs'." |
31 | 929 |
930 (interactive) | |
931 (message "Hiding...") | |
13856
8046a1616264
(hide-ifdefs): Set hif-outside-read-only here.
Richard M. Stallman <rms@gnu.org>
parents:
13855
diff
changeset
|
932 (setq hif-outside-read-only buffer-read-only) |
31 | 933 (if (not hide-ifdef-mode) |
934 (hide-ifdef-mode 1)) ; turn on hide-ifdef-mode | |
935 (if hide-ifdef-hiding | |
936 (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
|
937 (let ((inhibit-read-only t)) |
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
938 (setq selective-display t) |
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
939 (setq hide-ifdef-hiding t) |
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
940 (hide-ifdef-guts)) |
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
941 (setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only)) |
13855 | 942 (or nomsg |
943 (message "Hiding done"))) | |
31 | 944 |
945 | |
946 (defun show-ifdefs () | |
13855 | 947 "Cancel the effects of `hide-ifdef': show the contents of all #ifdefs." |
31 | 948 (interactive) |
7324
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
949 (setq buffer-read-only hif-outside-read-only) |
31 | 950 (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
|
951 (let ((inhibit-read-only t)) |
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
952 (hif-show-all)) |
210 | 953 (setq hide-ifdef-hiding nil)) |
31 | 954 |
955 | |
956 (defun hif-find-ifdef-block () | |
3591
507f64624555
Apply typo patches from Paul Eggert.
Jim Blandy <jimb@redhat.com>
parents:
3457
diff
changeset
|
957 "Utility for hide and show `ifdef-block'. |
1476 | 958 Set top and bottom of ifdef block." |
31 | 959 (let (max-bottom) |
960 (save-excursion | |
961 (beginning-of-line) | |
962 (if (not (or (hif-looking-at-else) (hif-looking-at-ifX))) | |
963 (up-ifdef)) | |
964 (setq top (point)) | |
965 (hif-ifdef-to-endif) | |
210 | 966 (setq max-bottom (1- (point)))) |
31 | 967 (save-excursion |
968 (beginning-of-line) | |
969 (if (not (hif-looking-at-endif)) | |
970 (hif-find-next-relevant)) | |
971 (while (hif-looking-at-ifX) | |
972 (hif-ifdef-to-endif) | |
210 | 973 (hif-find-next-relevant)) |
974 (setq bottom (min max-bottom (1- (point)))))) | |
31 | 975 ) |
976 | |
977 | |
978 (defun hide-ifdef-block () | |
979 "Hide the ifdef block (true or false part) enclosing or before the cursor." | |
980 (interactive) | |
981 (if (not hide-ifdef-mode) | |
982 (hide-ifdef-mode 1)) | |
983 (setq selective-display t) | |
7324
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
984 (let (top bottom (inhibit-read-only t)) |
31 | 985 (hif-find-ifdef-block) ; set top and bottom - dynamic scoping |
986 (hide-ifdef-region top bottom) | |
987 (if hide-ifdef-lines | |
988 (progn | |
989 (hif-hide-line top) | |
990 (hif-hide-line (1+ bottom)))) | |
210 | 991 (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
|
992 (setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only))) |
31 | 993 |
994 | |
995 (defun show-ifdef-block () | |
996 "Show the ifdef block (true or false part) enclosing or before the cursor." | |
997 (interactive) | |
7324
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
998 (let ((inhibit-read-only t)) |
31 | 999 (if hide-ifdef-lines |
1000 (save-excursion | |
1001 (beginning-of-line) | |
1002 (hif-show-ifdef-region (1- (point)) (progn (end-of-line) (point)))) | |
1003 | |
1004 (let (top bottom) | |
1005 (hif-find-ifdef-block) | |
7324
7e78d145539b
(show-ifdefs, hide-ifdefs): Bind inhibit-read-only.
Richard M. Stallman <rms@gnu.org>
parents:
5560
diff
changeset
|
1006 (hif-show-ifdef-region (1- top) bottom))))) |
31 | 1007 |
1008 | |
3591
507f64624555
Apply typo patches from Paul Eggert.
Jim Blandy <jimb@redhat.com>
parents:
3457
diff
changeset
|
1009 ;;; definition alist support |
31 | 1010 |
1011 (defvar hide-ifdef-define-alist nil | |
1012 "A global assoc list of pre-defined symbol lists") | |
1013 | |
1014 (defun hif-compress-define-list (env) | |
1015 "Compress the define list ENV into a list of defined symbols only." | |
1016 (let ((defs (mapcar '(lambda (arg) | |
1017 (if (hif-lookup (car arg)) (car arg))) | |
1018 env)) | |
1019 (new-defs nil)) | |
1020 (while defs | |
1021 (if (car defs) | |
1022 (setq new-defs (cons (car defs) new-defs))) | |
1023 (setq defs (cdr defs))) | |
210 | 1024 new-defs)) |
31 | 1025 |
1026 (defun hide-ifdef-set-define-alist (name) | |
1476 | 1027 "Set the association for NAME to `hide-ifdef-env'." |
31 | 1028 (interactive "SSet define list: ") |
1029 (setq hide-ifdef-define-alist | |
1030 (cons (cons name (hif-compress-define-list hide-ifdef-env)) | |
210 | 1031 hide-ifdef-define-alist))) |
31 | 1032 |
1033 (defun hide-ifdef-use-define-alist (name) | |
1476 | 1034 "Set `hide-ifdef-env' to the define list specified by NAME." |
31 | 1035 (interactive "SUse define list: ") |
1036 (let ((define-list (assoc name hide-ifdef-define-alist))) | |
1037 (if define-list | |
1038 (setq hide-ifdef-env | |
1039 (mapcar '(lambda (arg) (cons arg t)) | |
1040 (cdr define-list))) | |
1041 (error "No define list for %s" name)) | |
210 | 1042 (if hide-ifdef-hiding (hide-ifdefs)))) |
31 | 1043 |
13297
9699410bf8bf
Provide hideif; require cc-mode.
Richard M. Stallman <rms@gnu.org>
parents:
12954
diff
changeset
|
1044 (provide 'hideif) |
9699410bf8bf
Provide hideif; require cc-mode.
Richard M. Stallman <rms@gnu.org>
parents:
12954
diff
changeset
|
1045 |
660
08eb386dd0f3
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
210
diff
changeset
|
1046 ;;; hideif.el ends here |
08eb386dd0f3
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
210
diff
changeset
|
1047 |