-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc-outline.el
299 lines (252 loc) · 9.08 KB
/
c-outline.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
;;; c-outline.el --- outline-minor-mode support for C program editing.
;; Copyright (C) 1994 Kevin Broadey.
;; Author: Kevin Broadey <KevinB@bartley.demon.co.uk>
;; Created: 15 March 1994
;; Version: c-outline.el 1.6 dated 94/05/06 at 13:01:42
;; Keywords: C, c-mode, outline
;; LCD Archive Entry:
;; c-outline|Kevin Broadey|KevinB@bartley.demon.co.uk|
;; outline-minor-mode support for C program editing|
;; 06-May-1994|1.6|~/misc/c-outline.el.Z|
;; This file is not part of GNU Emacs, but it is distributed under the same
;; conditions.
;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 1, or (at your option)
;; any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
;;; Commentary:
;; c-outline.el tailors the outline-minor-mode variables `outline-regexp' and
;; `outline-level' so that `C' statements are treated as outline headings.
;; This allows you to use outline-minor-mode commands to hide and expose blocks
;; of `C' code, provided the code is indented in a consistent manner.
;;
;; To use c-outline.el in a c-mode buffer simply type `M-x c-outline RET'.
;; To turn off outline-minor-mode, do `M-x outline-minor-mode'.
;;; Installation:
;; To use c-outline, put this in your .emacs:-
;;
;; (autoload 'c-outline "c-outline" nil t)
;;
;; If you want to use c-outline in all your c-mode buffers, add this too:-
;;
;; (add-hook 'c-mode-hook 'c-outline)
;;; Advertisements:
;; Get out-xtra.el by Per Abrahamsen <abraham@iesd.auc.dk> for more
;; outline-mode goodies. In particular, `outline-hide-sublevels' makes
;; setup a lot easier.
;;
;; foldout.el (by me) provides folding editor extensions for outline-mode and
;; outline-minor-mode, so with c-outline you can have a folding `C' code editor
;; without having to put in start- and end-of-fold markers. This is a real
;; winner!
;;
;; folding.el by Jamie Lokier <u90jl@ecs.ox.ac.uk> supports folding by
;; recognising special marker text in you file.
;;; ChangeLog:
;; 1.6 6-May-94
;; Fix bug in level for "{". If the previous line is a comment, don't add on
;; c-outline-block-offset as the brace is the start of an inline block.
;; 1.5 15-Apr-94
;; Make `c-outline-block-offset' automatically buffer-local when it is set
;; rather than setting it in individual buffers so the user can use
;; setq-default and get the expected behaviour.
;; 1.4 8-Apr-94
;; Need to (require 'outline) otherwise the default value of outline-regexp and
;; outline-level is NIL.
;; 1.3 23-Mar-94
;; Change level for "{" again. Make it indentation + c-indent-level most of
;; the time, but change it to current indentation if previous line is blank or
;; indented more than indentation + c-indent-level. This stops in-line blocks
;; getting hidden under the preceding statement, and gets around a problem with
;; `show-children' on a function heading. If the function is like this:-
;;
;; void func (int arg1,
;; int arg2)
;; {
;; if (something)
;; {
;; do-this;
;; }
;; else
;; {
;; do-that;
;; }
;; }
;;
;; then previously, `show-children' on the heading would expose the entire
;; function.
;; 1.2 22-Mar-94
;; Changed level for "{" to be indentation + c-indent-level so that:-
;;
;; if (expression)
;; {
;; statement;
;; }
;; else
;; {
;; statement;
;; }
;;
;; folds up to:-
;;
;; if (expression)...
;; else..
;;
;; and doing `show-children' on the "if" or "else" shows the entire block in
;; one go. The only problem with this is that in-line blocks will get hidden
;; under the preceding statement. Get around this by preceding them by a
;; comment - they'll then be hidden under the comment:-
;;
;; func (arg)
;; {
;; statement;
;; statement;
;;
;; /* comment for scoping block */
;;
;; {
;; int i;
;;
;; statement_using_i;
;; }
;; }
;;
;; 1.1 15-Mar-94
;; Released to the net.
;;; required packages
(require 'outline)
;;; Code:
(defconst c-outline-regexp "[ \t]*\\([^* \t\n\^M\^L]\\|\\*+[a-zA-Z_0-9=(]\\)"
"Regexp matching an outline heading in a c-mode buffer.
This matches everything except blank lines and those that look like they're
part of a milti-line block comment -- i.e. they begin with one or more \"*\"s
but don't look like a pointer dereference.")
(defconst c-outline-statement-regexp "[\n\^M][ \t]*[^*#/ \t\n\^M]"
"Regexp matching a non-comment, non-directive heading.
This is used to find the heading to use for to get the level for a comment
in column zero or a preprocessor directive.")
(defvar c-outline-block-offset
(if (boundp 'c-basic-offset)
c-basic-offset ; cc-mode
c-indent-level) ; c-mode
"*Amount by which code following an open brace is indented.
Automatically becomes buffer-local when set in any fashion.")
(make-variable-buffer-local 'c-outline-block-offset)
(defun c-outline ()
"Set up outline-minor-mode in a c-mode buffer."
(interactive)
(set (make-local-variable 'outline-regexp) c-outline-regexp)
(set (make-local-variable 'outline-level) 'c-outline-level)
(outline-minor-mode 1)
)
(defsubst c-outline-find-statement (soh-pos)
"Find a non-comment, non-directive heading.
Searches forwards first, then goes backward if none is found."
(if (re-search-forward c-outline-statement-regexp nil t)
(goto-char (1+ (match-beginning 0)))
(goto-char (1- soh-pos))
(if (re-search-backward c-outline-statement-regexp nil 'move)
(goto-char (1+ (match-beginning 0))))))
(defun c-outline-level ()
(let (soh-pos indentation soh-char level)
(setq soh-pos (point)
indentation (- (- (current-column)
(progn (skip-chars-forward " \t")
(current-column))))
soh-char (char-after (point)))
(setq level
(cond
;; at beginning of buffer level is zero - can't use (bobp) to check
;; this, though - I've just skipped the whitespace!
((= soh-pos (point-min))
0)
;; for statements level is current indentation - this is everything
;; except comments, preprocessor directives and braces
((not (memq soh-char '(?/ ?# ?{ ?})))
indentation)
;; comments, and lines beginning with "/"
((= soh-char ?/)
(if (/= ?* (char-after (1+ (point))))
;; slash not followed by asterisk - this is a statement line,
;; not a comment after all
indentation
(goto-char (1- soh-pos))
(if (re-search-backward "[\n\^M]" nil 'move)
(forward-char 1))
(cond
;; comment after comment - bury it
((looking-at "[ \t]*/\\*")
1000)
;; comment at beginning-of-line - same level as next or previous
;; statement
((zerop indentation)
(c-outline-find-statement soh-pos)
(c-outline-level))
;; comment not at beginning of line - level is current
;; indentation
(t
indentation))))
;; for open braces level is indentation if previous line is indented
;; more, is blank, or is a comment, and is indentation + block
;; offset otherwise
((= soh-char ?{)
(goto-char (1- soh-pos))
(if (re-search-backward "[\n\^M]" nil 'move)
(forward-char 1))
(let ((prev-indentation (- (- (current-column)
(progn
(skip-chars-forward " \t")
(current-column))))))
(if (or (> prev-indentation
(+ indentation c-outline-block-offset)) ;indented more
(= (char-syntax (char-after (point))) ?\ ) ;blank
(looking-at "/\\*"))
indentation
(+ indentation c-outline-block-offset))))
;; for close braces level is indentation + block offset
((= soh-char ?})
(+ indentation c-outline-block-offset))
;; preprocessor directives
((= soh-char ?#)
(forward-char 1)
(skip-chars-forward " \t")
(cond
;; level for #define is zero
((looking-at "define")
0)
;; level for #else, #elif, #endif is same as preceding #if
((looking-at "e\\(ndif\\|l\\(se\\|if\\)\\)")
(if (re-search-backward "[\n\^M]#[ \t]*if" nil 'move)
(progn
(goto-char (1+ (match-beginning 0)))
(c-outline-level))
;; make unmatched directives stand out like a sore thumb!
-1))
;; bury #include
((looking-at "include")
1000)
;; level for other directives is same as next or previous
;; statement
(t
(c-outline-find-statement soh-pos)
(c-outline-level))))
;; dunno what this is - give it the same level as a statement
;; to be on the safe side
(t
indentation)
))
;; go back to the beginning of the heading - this is quicker than using
;; save-excursion
(goto-char soh-pos)
;; and the outline level is...
level
))
(provide 'c-outline)
;;; c-outline.el ends here