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