Mercurial > emacs
annotate lisp/progmodes/cplus-md.el @ 873:0abaf590e0ca
entered into RCS
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Sun, 26 Jul 1992 19:36:03 +0000 |
parents | 345296f94a1e |
children | 7e4999005da1 |
rev | line source |
---|---|
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
257
diff
changeset
|
1 ;;; c++-mode.el --- C++ code editing commands for Emacs |
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
257
diff
changeset
|
2 |
846
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
3 ;; Copyright (C) 1985 Richard M. Stallman. |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
4 |
798
b7932f859d4e
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
5 ;; Author: Dave Detlefs <dld@cs.cmu.edu> |
b7932f859d4e
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
6 ;; Stewart Clamen <clamen@cs.cmu.edu> |
b7932f859d4e
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
7 ;; Maintainer: Dave Detlefs <dld@cs.cmu.edu> |
b7932f859d4e
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
8 ;; Keywords: c |
b7932f859d4e
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
9 |
b7932f859d4e
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
10 ;;; Commentary: |
b7932f859d4e
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
11 |
257 | 12 ;; 1987 Dave Detlefs (dld@cs.cmu.edu) |
13 ;; and Stewart Clamen (clamen@cs.cmu.edu). | |
14 ;; Done by fairly faithful modification of: | |
798
b7932f859d4e
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
15 ;;; Change Log: |
b7932f859d4e
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
16 |
257 | 17 ;; Feb, 1990 (Dave Detlefs, dld@cs.cmu.edu) |
18 ;; Fixed electric-c++-terminator to handle double colons, at the | |
19 ;; request of John Hagerman. | |
20 ;; | |
21 ;; Jan, 1990 (Doug Lea, dl@oswego.edu) | |
22 ;; Replaced c++-comment-region and c++-uncomment-region with | |
23 ;; versions from Igor Metz that avoid potential infinite loops. | |
24 ;; | |
25 ;; Oct, 1989 (Dave Detlefs, dld@cs.cmu.edu) | |
26 ;; Added contribution from Igor Metz <metz@iam.unibe.ch>: | |
27 ;; functions c++-comment-region and c++-uncomment-region and | |
28 ;; corresponding key-binding. | |
29 ;; Also fixed bug in indentation of second line after an empty | |
30 ;; arglist with empty-arglist non-null. | |
31 ;; | |
32 ;; Sept, 1989 (Glen Ditchfield, gjditchfield@violet.uwaterloo.ca): | |
33 ;; Textual changes to more closely imitate Emacs 18.55's c-mode. | |
34 ;; Fixed handling of "default:", where ":" was the last character in the | |
35 ;; buffer. Fixed indentation of comments starting in column 0, and when | |
36 ;; previous line contained more than one comment start string. Fixed | |
37 ;; handling of "friend class". | |
38 ;; | |
39 ;; Aug 7, 1989; John Hagerman (hagerman@ece.cmu.edu): | |
40 ;; Changed calculate-c++-indent to handle member initializations | |
41 ;; more flexibly. Two new variables are used to control behavior: | |
42 ;; c++-member-init-indent and c++-continued-member-init-offset. | |
43 ;; Note the assumption that member initializations and argument | |
44 ;; declarations are not mixed in one function definition. | |
45 ;; | |
46 ;; June 1989 (Dave Detlefs, dld@cs.cmu.edu) | |
47 ;; Fixed calculate-c++-indent to handle continued lines ending in | |
48 ;; {'s. (I wasn't following C-mode closely enough, or C-mode | |
49 ;; changed.) Made ' a quote character, at the behest of someone | |
50 ;; whose mail I apparently deleted (if they send me mail I'll credit | |
51 ;; them here in a future revision.) | |
52 ;; Dan Weinreb (dlw@odi.com) pointed out that 'c++-mode successively | |
53 ;; bound c++-indent-exp and c++-indent-defun to ESC-^q. ESC-^q is | |
54 ;; now bound to c++-indent-exp, while, c++-indent-defun is invoked | |
55 ;; with ESC-^x. | |
56 | |
57 ;; February 1989 (Dave Detlefs, dld@cs.cmu.edu) | |
58 ;; Fixed some errors in c++-indent-defun, as pointed out by Sam | |
59 ;; Haradhvala (odi!sam@talcott.harvard.edu). | |
60 ;; October 1988 (Dave Detlefs, dld@cs.cmu.edu) | |
61 ;; It turns out I had only *thought* I had made | |
62 ;; beginning(end)-of-defun work. It should work better now -- you | |
63 ;; can either attempt to match defun headers "strongly," using a | |
64 ;; very complicated regexp, or "weakly," using a simple one. This | |
65 ;; is settable by a variable; the default is the cheaper weak | |
66 ;; method. (Stewart Clamen was intimately involved in this, too.) | |
67 ;; | |
68 ;; I made "'" *not* be a string delimiter, because that was causing | |
69 ;; comments containing contractions to ("// don't") to mess up paren | |
70 ;; balancing. | |
71 ;; | |
72 ;; I also incorporated another slight indentation fix from Glen | |
73 ;; Ditchfield. | |
74 ;; | |
75 ;; We hope this is will make into version 19 of gnu-emacs. | |
76 ;; | |
77 ;; September 1988: incorporated changes from Fred Calm at Schlumberger. | |
78 ;; Also, made beginning(end)-of-defun, indent-defun work. | |
79 ;; | |
80 ;; August 1987: incorporated changes done by Glen Ditchfield of Waterloo. | |
81 | |
798
b7932f859d4e
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
82 ;;; Code: |
b7932f859d4e
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
662
diff
changeset
|
83 |
257 | 84 (defvar c++-mode-abbrev-table nil |
85 "Abbrev table used in C++ mode.") | |
86 (define-abbrev-table 'c++-mode-abbrev-table ()) | |
87 | |
88 (defvar c++-mode-map () | |
89 "Keymap used in C++ mode.") | |
90 (if c++-mode-map | |
91 () | |
92 (setq c++-mode-map (make-sparse-keymap)) | |
93 (define-key c++-mode-map "\C-j" 'reindent-then-newline-and-indent) | |
94 (define-key c++-mode-map "{" 'electric-c++-brace) | |
95 (define-key c++-mode-map "}" 'electric-c++-brace) | |
96 (define-key c++-mode-map ";" 'electric-c++-semi) | |
97 (define-key c++-mode-map "\e\C-h" 'mark-c-function) | |
98 (define-key c++-mode-map "\e\C-q" 'indent-c++-exp) | |
99 (define-key c++-mode-map "\177" 'backward-delete-char-untabify) | |
100 (define-key c++-mode-map "\t" 'c++-indent-command) | |
846
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
101 ;; (define-key c++-mode-map "\C-c\C-i" 'c++-insert-header) |
861
345296f94a1e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
102 (define-key c++-mode-map "\C-c\C-\\" 'c-backslash-region)) |
846
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
103 ;; (define-key c++-mode-map "\e\C-a" 'c++-beginning-of-defun) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
104 ;; (define-key c++-mode-map "\e\C-e" 'c++-end-of-defun) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
105 ;; (define-key c++-mode-map "\e\C-x" 'c++-indent-defun)) |
257 | 106 |
107 (defvar c++-mode-syntax-table nil | |
108 "Syntax table used in C++ mode.") | |
109 | |
110 (if c++-mode-syntax-table | |
111 () | |
112 (setq c++-mode-syntax-table (copy-syntax-table c-mode-syntax-table)) | |
113 (modify-syntax-entry ?/ ". 12" c++-mode-syntax-table) | |
114 (modify-syntax-entry ?\n ">" c++-mode-syntax-table) | |
115 (modify-syntax-entry ?\' "." c++-mode-syntax-table)) | |
116 | |
117 (defvar c++-continued-member-init-offset nil | |
118 "*Extra indent for continuation lines of member inits; | |
846
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
119 nil means to align with previous initializations rather than |
257 | 120 with the colon on the first line.") |
121 (defvar c++-member-init-indent 0 | |
122 "*Indentation level of member initializations in function declarations.") | |
123 (defvar c++-friend-offset -4 | |
124 "*Offset of C++ friend class declarations relative to member declarations.") | |
125 (defvar c++-electric-colon t | |
126 "*If t, colon is an electric terminator.") | |
127 (defvar c++-empty-arglist-indent nil | |
128 "*Indicates how far to indent an line following an empty argument | |
129 list. Nil indicates to just after the paren.") | |
130 | |
131 | |
132 ;;;###autoload | |
133 (defun c++-mode () | |
134 "Major mode for editing C++ code. Very much like editing C code. | |
135 Expression and list commands understand all C++ brackets. | |
136 Tab at left margin indents for C++ code | |
137 Comments are delimited with /* ... */ {or with // ... <newline>} | |
138 Paragraphs are separated by blank lines only. | |
139 Delete converts tabs to spaces as it moves back. | |
140 \\{c++-mode-map} | |
141 Variables controlling indentation style: | |
142 c-tab-always-indent | |
143 Non-nil means TAB in C mode should always reindent the current line, | |
144 regardless of where in the line point is when the TAB command is used. | |
145 Default is t. | |
146 c-auto-newline | |
147 Non-nil means automatically newline before and after braces, | |
148 and after colons and semicolons, inserted in C code. | |
149 c-indent-level | |
150 Indentation of C statements within surrounding block. | |
151 The surrounding block's indentation is the indentation | |
152 of the line on which the open-brace appears. | |
153 c-continued-statement-offset | |
154 Extra indentation given to a substatement, such as the | |
155 then-clause of an if or body of a while. | |
156 c-continued-brace-offset | |
157 Extra indentation given to a brace that starts a substatement. | |
158 This is in addition to c-continued-statement-offset. | |
159 c-brace-offset | |
160 Extra indentation for line if it starts with an open brace. | |
161 c-brace-imaginary-offset | |
162 An open brace following other text is treated as if it were | |
163 this far to the right of the start of its line. | |
164 c-argdecl-indent | |
165 Indentation level of declarations of C function arguments. | |
166 c-label-offset | |
167 Extra indentation for line that is a label, or case or ``default:'', or | |
168 ``public:'' or ``private:'', or ``protected:''. | |
169 c++-electric-colon | |
170 If non-nil at invocation of c++-mode (t is the default) colon electricly | |
171 indents. | |
172 c++-empty-arglist-indent | |
173 If non-nil, a function declaration or invocation which ends a line with a | |
174 left paren is indented this many extra spaces, instead of flush with the | |
175 left paren. | |
176 c++-friend-offset | |
177 Offset of C++ friend class declarations relative to member declarations. | |
178 c++-member-init-indent | |
179 Indentation level of member initializations in function declarations, | |
180 if they are on a separate line beginning with a colon. | |
181 c++-continued-member-init-offset | |
182 Extra indentation for continuation lines of member initializations; NIL | |
183 means to align with previous initializations rather than with the colon. | |
184 | |
185 Settings for K&R, BSD, and Stroustrup indentation styles are | |
186 c-indent-level 5 8 4 | |
187 c-continued-statement-offset 5 8 4 | |
188 c-continued-brace-offset 0 | |
189 c-brace-offset -5 -8 0 | |
190 c-brace-imaginary-offset 0 | |
191 c-argdecl-indent 0 8 4 | |
192 c-label-offset -5 -8 -4 | |
193 c++-empty-arglist-indent 4 | |
194 c++-friend-offset 0 | |
195 | |
196 Turning on C++ mode calls the value of the variable `c++-mode-hook' with | |
197 no args if that value is non-nil." | |
198 (interactive) | |
199 (kill-all-local-variables) | |
200 (use-local-map c++-mode-map) | |
201 (set-syntax-table c++-mode-syntax-table) | |
202 (setq major-mode 'c++-mode | |
203 mode-name "C++" | |
204 comment-column 32 | |
205 local-abbrev-table c++-mode-abbrev-table) | |
206 (set (make-local-variable 'indent-line-function) 'c++-indent-line) | |
207 (set (make-local-variable 'comment-start) "// ") | |
208 (set (make-local-variable 'comment-end) "") | |
209 (set (make-local-variable 'comment-start-skip) "/\\*+ *\\|// *") | |
210 (set (make-local-variable 'comment-indent-hook) 'c++-comment-indent) | |
211 (set (make-local-variable 'paragraph-start) (concat "^$\\|" page-delimiter)) | |
212 (set (make-local-variable 'paragraph-separate) paragraph-start) | |
213 (set (make-local-variable 'paragraph-ignore-fill-prefix) t) | |
214 (set (make-local-variable 'require-final-newline) t) | |
215 (set (make-local-variable 'parse-sexp-ignore-comments) nil) | |
216 (run-hooks 'c++-mode-hook) | |
217 (if c++-electric-colon | |
218 (define-key c++-mode-map ":" 'electric-c++-terminator))) | |
219 | |
220 ;; This is used by indent-for-comment | |
221 ;; to decide how much to indent a comment in C++ code | |
222 ;; based on its context. | |
223 (defun c++-comment-indent () | |
224 (if (looking-at "^\\(/\\*\\|//\\)") | |
225 0 ; Existing comment at bol stays there. | |
226 (save-excursion | |
227 (skip-chars-backward " \t") | |
228 (max | |
229 ;; leave at least one space on non-empty lines. | |
230 (if (zerop (current-column)) 0 (1+ (current-column))) | |
231 (let ((cur-pt (point))) | |
232 (beginning-of-line 0) | |
233 ;; If previous line had a comment, use it's indent | |
234 (if (re-search-forward comment-start-skip cur-pt t) | |
235 (progn | |
236 (goto-char (match-beginning 0)) | |
237 (current-column)) | |
238 comment-column)))))) ; otherwise indent at comment column. | |
239 | |
240 (defun electric-c++-brace (arg) | |
241 "Insert character and correct line's indentation." | |
242 (interactive "P") | |
243 (let (insertpos) | |
244 (if (and (not arg) | |
245 (eolp) | |
246 (or (save-excursion | |
247 (skip-chars-backward " \t") | |
248 (bolp)) | |
249 (if c-auto-newline (progn (c++-indent-line) (newline) t)))) | |
250 (progn | |
251 (insert last-command-char) | |
252 (c++-indent-line) | |
253 (if c-auto-newline | |
254 (progn | |
255 (newline) | |
256 ;; (newline) may have done auto-fill | |
257 (setq insertpos (- (point) 2)) | |
258 (c++-indent-line))) | |
259 (save-excursion | |
260 (if insertpos (goto-char (1+ insertpos))) | |
261 (delete-char -1)))) | |
262 (if insertpos | |
263 (save-excursion | |
264 (goto-char insertpos) | |
265 (self-insert-command (prefix-numeric-value arg))) | |
266 (self-insert-command (prefix-numeric-value arg))))) | |
267 | |
268 (defun electric-c++-semi (arg) | |
269 "Insert character and correct line's indentation." | |
270 (interactive "P") | |
271 (if c-auto-newline | |
272 (electric-c++-terminator arg) | |
273 (self-insert-command (prefix-numeric-value arg)))) | |
274 | |
275 (defun electric-c++-terminator (arg) | |
276 "Insert character and correct line's indentation." | |
277 (interactive "P") | |
278 (let (insertpos (end (point))) | |
279 (if (and (not arg) (eolp) | |
280 (not (save-excursion | |
281 (beginning-of-line) | |
282 (skip-chars-forward " \t") | |
283 (or (= (following-char) ?#) | |
284 ;; Colon is special only after a label, or | |
285 ;; case, or another colon. | |
286 ;; So quickly rule out most other uses of colon | |
287 ;; and do no indentation for them. | |
288 (and (eq last-command-char ?:) | |
289 (not (looking-at "case[ \t]")) | |
290 (save-excursion | |
291 (forward-word 1) | |
292 (skip-chars-forward " \t") | |
293 (< (point) end)) | |
294 ;; Do re-indent double colons | |
295 (save-excursion | |
296 (end-of-line 1) | |
297 (looking-at ":"))) | |
298 (progn | |
299 (beginning-of-defun) | |
300 (let ((pps (parse-partial-sexp (point) end))) | |
301 (or (nth 3 pps) (nth 4 pps) (nth 5 pps)))))))) | |
302 (progn | |
303 (insert last-command-char) | |
304 (c++-indent-line) | |
305 (and c-auto-newline | |
306 (not (c-inside-parens-p)) | |
307 (progn | |
308 ;; the new marker object, used to be just an integer | |
309 (setq insertpos (make-marker)) | |
310 ;; changed setq to set-marker | |
311 (set-marker insertpos (1- (point))) | |
312 ;; do this before the newline, since in auto fill can break | |
313 (newline) | |
314 (c-indent-line))) | |
315 (save-excursion | |
316 (if insertpos (goto-char (1+ insertpos))) | |
317 (delete-char -1)))) | |
318 (if insertpos | |
319 (save-excursion | |
320 (goto-char insertpos) | |
321 (self-insert-command (prefix-numeric-value arg))) | |
322 (self-insert-command (prefix-numeric-value arg))))) | |
323 | |
324 (defun c++-indent-command (&optional whole-exp) | |
325 "Indent current line as C++ code, or in some cases insert a tab character. | |
326 If `c-tab-always-indent' is non-nil (the default), always indent current | |
327 line. Otherwise, indent the current line only if point is at the left | |
328 margin or in the line's indentation; otherwise insert a tab. | |
329 | |
330 A numeric argument, regardless of its value, means indent rigidly all means | |
331 indent rigidly all the lines of the expression starting after point so that | |
332 this line becomes properly indented. The relative indentation among the | |
333 lines of the expression are preserved." | |
334 (interactive "P") | |
335 (if whole-exp | |
336 ;; If arg, always indent this line as C | |
337 ;; and shift remaining lines of expression the same amount. | |
338 (let ((shift-amt (c++-indent-line)) | |
339 beg end) | |
340 (save-excursion | |
341 (if c-tab-always-indent | |
342 (beginning-of-line)) | |
343 (setq beg (point)) | |
344 (forward-sexp 1) | |
345 (setq end (point)) | |
346 (goto-char beg) | |
347 (forward-line 1) | |
348 (setq beg (point))) | |
349 (if (> end beg) | |
350 (indent-code-rigidly beg end shift-amt "#"))) | |
351 (if (and (not c-tab-always-indent) | |
352 (save-excursion | |
353 (skip-chars-backward " \t") | |
354 (not (bolp)))) | |
355 (insert-tab) | |
356 (c++-indent-line)))) | |
357 | |
358 (defun c++-indent-line () | |
359 "Indent current line as C++ code. | |
360 Return the amount the indentation changed by." | |
361 (let ((indent (calculate-c++-indent nil)) | |
362 beg shift-amt | |
363 (case-fold-search nil) | |
364 (pos (- (point-max) (point)))) | |
365 (beginning-of-line) | |
366 (setq beg (point)) | |
367 (cond ((eq indent nil) | |
368 (setq indent (current-indentation))) | |
369 ((eq indent t) | |
370 (setq indent (calculate-c-indent-within-comment))) | |
371 ((looking-at "[ \t]*#") | |
372 (setq indent 0)) | |
373 (t | |
374 (skip-chars-forward " \t") | |
375 (if (listp indent) (setq indent (car indent))) | |
376 (cond ((looking-at "\\(default\\|public\\|private\\|protected\\):") | |
377 (setq indent (+ indent c-label-offset))) | |
378 ((or (looking-at "case\\b") | |
379 (and (looking-at "[A-Za-z]") | |
380 (save-excursion | |
381 (forward-sexp 1) | |
382 (looking-at ":[^:]")))) | |
383 (setq indent (max 1 (+ indent c-label-offset)))) | |
384 ((and (looking-at "else\\b") | |
385 (not (looking-at "else\\s_"))) | |
386 (setq indent (save-excursion | |
387 (c-backward-to-start-of-if) | |
388 (current-indentation)))) | |
389 ((looking-at "friend\[ \t]class[ \t]") | |
390 (setq indent (+ indent c++-friend-offset))) | |
391 ((= (following-char) ?}) | |
392 (setq indent (- indent c-indent-level))) | |
393 ((= (following-char) ?{) | |
394 (setq indent (+ indent c-brace-offset)))))) | |
395 (skip-chars-forward " \t") | |
396 (setq shift-amt (- indent (current-column))) | |
397 (if (zerop shift-amt) | |
398 (if (> (- (point-max) pos) (point)) | |
399 (goto-char (- (point-max) pos))) | |
400 (delete-region beg (point)) | |
401 (indent-to indent) | |
402 ;; If initial point was within line's indentation, | |
403 ;; position after the indentation. Else stay at same point in text. | |
404 (if (> (- (point-max) pos) (point)) | |
405 (goto-char (- (point-max) pos)))) | |
406 shift-amt)) | |
407 | |
408 (defun calculate-c++-indent (&optional parse-start) | |
409 "Return appropriate indentation for current line as C++ code. | |
410 In usual case returns an integer: the column to indent to. | |
411 Returns nil if line starts inside a string, t if in a comment." | |
412 (save-excursion | |
413 (beginning-of-line) | |
414 (let ((indent-point (point)) | |
415 (case-fold-search nil) | |
416 state | |
417 containing-sexp) | |
418 (if parse-start | |
419 (goto-char parse-start) | |
420 (beginning-of-defun)) | |
421 (while (< (point) indent-point) | |
422 (setq parse-start (point)) | |
423 (setq state (parse-partial-sexp (point) indent-point 0)) | |
424 (setq containing-sexp (car (cdr state)))) | |
425 (cond ((or (nth 3 state) (nth 4 state)) | |
426 ;; return nil or t if should not change this line | |
427 (nth 4 state)) | |
428 ((null containing-sexp) | |
429 ;; Line is at top level. May be data or function definition, or | |
430 ;; may be function argument declaration or member initialization. | |
431 ;; Indent like the previous top level line unless | |
432 ;; (1) the previous line ends in a closeparen without semicolon, | |
433 ;; in which case this line is the first argument declaration or | |
434 ;; member initialization, or | |
435 ;; (2) the previous line begins with a colon, | |
436 ;; in which case this is the second line of member inits. | |
437 ;; It is assumed that arg decls and member inits are not mixed. | |
438 (goto-char indent-point) | |
439 (skip-chars-forward " \t") | |
440 (if (= (following-char) ?{) | |
441 0 ; Unless it starts a function body | |
442 (c++-backward-to-noncomment (or parse-start (point-min))) | |
443 (if (= (preceding-char) ?\)) | |
444 (progn ; first arg decl or member init | |
445 (goto-char indent-point) | |
446 (skip-chars-forward " \t") | |
447 (if (= (following-char) ?:) | |
448 c++-member-init-indent | |
449 c-argdecl-indent)) | |
450 (if (= (preceding-char) ?\;) | |
451 (backward-char 1)) | |
452 (if (= (preceding-char) ?}) | |
453 0 | |
454 (beginning-of-line) ; continued arg decls or member inits | |
455 (skip-chars-forward " \t") | |
456 (if (= (following-char) ?:) | |
457 (if c++-continued-member-init-offset | |
458 (+ (current-indentation) | |
459 c++-continued-member-init-offset) | |
460 (progn | |
461 (forward-char 1) | |
462 (skip-chars-forward " \t") | |
463 (current-column))) | |
464 (current-indentation))) | |
465 ))) | |
466 ((/= (char-after containing-sexp) ?{) | |
467 ;; line is expression, not statement: | |
468 ;; indent to just after the surrounding open -- unless | |
469 ;; empty arg list, in which case we do what | |
470 ;; c++-empty-arglist-indent says to do. | |
471 (if (and c++-empty-arglist-indent | |
472 (or (null (nth 2 state)) ;; indicates empty arg | |
473 ;; list. | |
474 ;; Use a heuristic: if the first | |
475 ;; non-whitespace following left paren on | |
476 ;; same line is not a comment, | |
477 ;; is not an empty arglist. | |
478 (save-excursion | |
479 (goto-char (1+ containing-sexp)) | |
480 (not | |
481 (looking-at "\\( \\|\t\\)*[^/\n]"))))) | |
482 (progn | |
483 (goto-char containing-sexp) | |
484 (beginning-of-line) | |
485 (skip-chars-forward " \t") | |
486 (goto-char (min (+ (point) c++-empty-arglist-indent) | |
487 (1+ containing-sexp))) | |
488 (current-column)) | |
489 ;; In C-mode, we would always indent to one after the | |
490 ;; left paren. Here, though, we may have an | |
491 ;; empty-arglist, so we'll indent to the min of that | |
492 ;; and the beginning of the first argument. | |
493 (goto-char (1+ containing-sexp)) | |
494 (current-column))) | |
495 (t | |
496 ;; Statement. Find previous non-comment character. | |
497 (goto-char indent-point) | |
498 (c++-backward-to-noncomment containing-sexp) | |
499 (if (not (memq (preceding-char) '(nil ?\, ?\; ?} ?: ?\{))) | |
500 ;; This line is continuation of preceding line's statement; | |
501 ;; indent c-continued-statement-offset more than the | |
502 ;; previous line of the statement. | |
503 (progn | |
504 (c-backward-to-start-of-continued-exp containing-sexp) | |
505 (+ c-continued-statement-offset (current-column))) | |
506 ;; This line starts a new statement. | |
507 ;; Position following last unclosed open. | |
508 (goto-char containing-sexp) | |
509 ;; Is line first statement after an open-brace? | |
510 (or | |
511 ;; If no, find that first statement and indent like it. | |
512 (save-excursion | |
513 (forward-char 1) | |
514 (while (progn (skip-chars-forward " \t\n") | |
515 (looking-at | |
516 (concat | |
517 "#\\|/\\*\\|//" | |
518 "\\|case[ \t]" | |
519 "\\|[a-zA-Z0-9_$]*:[^:]" | |
520 "\\|friend[ \t]class[ \t]"))) | |
521 ;; Skip over comments and labels following openbrace. | |
522 (cond ((= (following-char) ?\#) | |
523 (forward-line 1)) | |
524 ((looking-at "/\\*") | |
525 (search-forward "*/" nil 'move)) | |
526 ((looking-at "//\\|friend[ \t]class[ \t]") | |
527 (forward-line 1)) | |
528 (t | |
529 (re-search-forward ":[^:]" nil 'move)))) | |
530 ;; The first following code counts | |
531 ;; if it is before the line we want to indent. | |
532 (and (< (point) indent-point) | |
533 (current-column))) | |
534 ;; If no previous statement, | |
535 ;; indent it relative to line brace is on. | |
536 ;; For open brace in column zero, don't let statement | |
537 ;; start there too. If c-indent-offset is zero, | |
538 ;; use c-brace-offset + c-continued-statement-offset instead. | |
539 ;; For open-braces not the first thing in a line, | |
540 ;; add in c-brace-imaginary-offset. | |
541 (+ (if (and (bolp) (zerop c-indent-level)) | |
542 (+ c-brace-offset c-continued-statement-offset) | |
543 c-indent-level) | |
544 ;; Move back over whitespace before the openbrace. | |
545 ;; If openbrace is not first nonwhite thing on the line, | |
546 ;; add the c-brace-imaginary-offset. | |
547 (progn (skip-chars-backward " \t") | |
548 (if (bolp) 0 c-brace-imaginary-offset)) | |
549 ;; If the openbrace is preceded by a parenthesized exp, | |
550 ;; move to the beginning of that; | |
551 ;; possibly a different line | |
552 (progn | |
553 (if (eq (preceding-char) ?\)) | |
554 (forward-sexp -1)) | |
555 ;; Get initial indentation of the line we are on. | |
556 (current-indentation)))))))))) | |
557 | |
558 (defun c++-backward-to-noncomment (lim) | |
559 (let (opoint stop) | |
560 (while (not stop) | |
561 (skip-chars-backward " \t\n\r\f" lim) | |
562 (setq opoint (point)) | |
563 (cond ((and (>= (point) (+ 2 lim)) | |
564 (save-excursion | |
565 (forward-char -2) | |
566 (looking-at "\\*/"))) | |
567 (search-backward "/*" lim 'move)) | |
568 ((and | |
846
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
569 (search-backward "//" (max (c++-point-bol) lim) 'move) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
570 (not (c++-within-string-p (point) opoint)))) |
257 | 571 (t (beginning-of-line) |
572 (skip-chars-forward " \t") | |
573 (if (looking-at "#") | |
574 (setq stop (<= (point) lim)) | |
575 (setq stop t) | |
576 (goto-char opoint))))))) | |
577 | |
578 (defun indent-c++-exp () | |
579 "Indent each line of the C++ grouping following point." | |
580 (interactive) | |
581 (let ((indent-stack (list nil)) | |
582 (contain-stack (list (point))) | |
583 (case-fold-search nil) | |
584 restart outer-loop-done inner-loop-done state ostate | |
861
345296f94a1e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
585 this-indent last-sexp last-depth |
257 | 586 at-else at-brace |
587 (opoint (point)) | |
588 (next-depth 0)) | |
589 (save-excursion | |
590 (forward-sexp 1)) | |
591 (save-excursion | |
592 (setq outer-loop-done nil) | |
593 (while (and (not (eobp)) (not outer-loop-done)) | |
594 (setq last-depth next-depth) | |
595 ;; Compute how depth changes over this line | |
596 ;; plus enough other lines to get to one that | |
597 ;; does not end inside a comment or string. | |
598 ;; Meanwhile, do appropriate indentation on comment lines. | |
861
345296f94a1e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
599 (setq inner-loop-done nil) |
345296f94a1e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
600 (while (and (not inner-loop-done) |
257 | 601 (not (and (eobp) (setq outer-loop-done t)))) |
602 (setq ostate state) | |
603 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point)) | |
604 nil nil state)) | |
605 (setq next-depth (car state)) | |
606 (if (and (car (cdr (cdr state))) | |
607 (>= (car (cdr (cdr state))) 0)) | |
608 (setq last-sexp (car (cdr (cdr state))))) | |
609 (if (or (nth 4 ostate)) | |
610 (c++-indent-line)) | |
611 (if (or (nth 3 state)) | |
612 (forward-line 1) | |
861
345296f94a1e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
613 (setq inner-loop-done t))) |
257 | 614 (if (<= next-depth 0) |
615 (setq outer-loop-done t)) | |
616 (if outer-loop-done | |
617 nil | |
618 ;; If this line had ..))) (((.. in it, pop out of the levels | |
619 ;; that ended anywhere in this line, even if the final depth | |
620 ;; doesn't indicate that they ended. | |
621 (while (> last-depth (nth 6 state)) | |
622 (setq indent-stack (cdr indent-stack) | |
623 contain-stack (cdr contain-stack) | |
624 last-depth (1- last-depth))) | |
625 (if (/= last-depth next-depth) | |
626 (setq last-sexp nil)) | |
627 ;; Add levels for any parens that were started in this line. | |
628 (while (< last-depth next-depth) | |
629 (setq indent-stack (cons nil indent-stack) | |
630 contain-stack (cons nil contain-stack) | |
631 last-depth (1+ last-depth))) | |
632 (if (null (car contain-stack)) | |
633 (setcar contain-stack (or (car (cdr state)) | |
634 (save-excursion (forward-sexp -1) | |
635 (point))))) | |
636 (forward-line 1) | |
637 (skip-chars-forward " \t") | |
638 (if (eolp) | |
639 nil | |
640 (if (and (car indent-stack) | |
641 (>= (car indent-stack) 0)) | |
642 ;; Line is on an existing nesting level. | |
643 ;; Lines inside parens are handled specially. | |
846
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
644 (if (/= (char-after (car contain-stack)) ?\{) |
257 | 645 (setq this-indent (car indent-stack)) |
646 ;; Line is at statement level. | |
647 ;; Is it a new statement? Is it an else? | |
648 ;; Find last non-comment character before this line | |
649 (save-excursion | |
650 (setq at-else (looking-at "else\\W")) | |
846
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
651 (setq at-brace (= (following-char) ?\{)) |
257 | 652 (c++-backward-to-noncomment opoint) |
846
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
653 (if (not (memq (preceding-char) '(nil ?\, ?\; ?\} ?: ?\{))) |
257 | 654 ;; Preceding line did not end in comma or semi; |
655 ;; indent this line c-continued-statement-offset | |
656 ;; more than previous. | |
657 (progn | |
658 (c-backward-to-start-of-continued-exp | |
659 (car contain-stack)) | |
660 (setq this-indent | |
661 (+ c-continued-statement-offset | |
662 (current-column) | |
663 (if at-brace c-continued-brace-offset 0)))) | |
664 ;; Preceding line ended in comma or semi; | |
665 ;; use the standard indent for this level. | |
666 (if at-else | |
667 (progn (c-backward-to-start-of-if opoint) | |
668 (setq this-indent (current-indentation))) | |
669 (setq this-indent (car indent-stack)))))) | |
670 ;; Just started a new nesting level. | |
671 ;; Compute the standard indent for this level. | |
672 (let ((val (calculate-c++-indent | |
673 (if (car indent-stack) | |
674 (- (car indent-stack)))))) | |
675 (setcar indent-stack | |
676 (setq this-indent val)))) | |
677 ;; Adjust line indentation according to its contents | |
678 (if (looking-at "\\(public\\|private\\|protected\\):") | |
679 (setq this-indent (- this-indent c-indent-level))) | |
680 (if (or (looking-at "case[ \t]") | |
681 (and (looking-at "[A-Za-z]") | |
682 (save-excursion | |
683 (forward-sexp 1) | |
684 (looking-at ":[^:]")))) | |
685 (setq this-indent (max 1 (+ this-indent c-label-offset)))) | |
686 (if (looking-at "friend[ \t]class[ \t]") | |
687 (setq this-indent (+ this-indent c++-friend-offset))) | |
846
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
688 (if (= (following-char) ?\}) |
257 | 689 (setq this-indent (- this-indent c-indent-level))) |
846
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
690 (if (= (following-char) ?\{) |
257 | 691 (setq this-indent (+ this-indent c-brace-offset))) |
692 ;; Put chosen indentation into effect. | |
693 (or (= (current-column) this-indent) | |
694 (= (following-char) ?\#) | |
695 (progn | |
696 (delete-region (point) (progn (beginning-of-line) (point))) | |
697 (indent-to this-indent))) | |
698 ;; Indent any comment following the text. | |
699 (or (looking-at comment-start-skip) | |
700 (if (re-search-forward comment-start-skip | |
701 (save-excursion (end-of-line) | |
702 (point)) t) | |
703 (progn | |
704 (indent-for-comment) | |
705 (beginning-of-line)))))))))) | |
846
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
706 |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
707 (defun fill-c++-comment () |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
708 "Fill a comment contained in consecutive lines containing point. |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
709 The fill lines remain a comment." |
257 | 710 (interactive) |
711 (save-excursion | |
712 (let ((save fill-prefix)) | |
713 (beginning-of-line 1) | |
714 (save-excursion | |
715 (re-search-forward comment-start-skip | |
716 (save-excursion (end-of-line) (point)) | |
717 t) | |
718 (goto-char (match-end 0)) | |
719 (set-fill-prefix)) | |
720 (while (looking-at fill-prefix) | |
721 (previous-line 1)) | |
722 (next-line 1) | |
723 (insert-string "\n") | |
724 (fill-paragraph nil) | |
725 (delete-char -1) | |
726 (setq fill-prefix save)))) | |
727 | |
846
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
728 (defun c++-point-bol () |
257 | 729 "Returns the value of the point at the beginning of the current line." |
730 (save-excursion | |
731 (beginning-of-line) | |
732 (point))) | |
733 | |
846
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
734 ;; (defun c++-insert-header () |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
735 ;; "Insert header denoting C++ code at top of buffer." |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
736 ;; (interactive) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
737 ;; (save-excursion |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
738 ;; (goto-char (point-min)) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
739 ;; (insert "// " |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
740 ;; "This may look like C code, but it is really " |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
741 ;; "-*- C++ -*-" |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
742 ;; "\n\n"))) |
257 | 743 |
846
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
744 (defun c++-within-string-p (point1 point2) |
257 | 745 "Returns true if number of double quotes between two points is odd." |
746 (let ((s (buffer-substring point1 point2))) | |
846
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
747 (not (zerop (% (c++-count-char-in-string ?\" s) 2))))) |
257 | 748 |
846
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
749 (defun c++-count-char-in-string (c s) |
257 | 750 (let ((count 0) |
751 (pos 0)) | |
752 (while (< pos (length s)) | |
753 (setq count (+ count (if (\= (aref s pos) c) 1 0))) | |
754 (setq pos (1+ pos))) | |
755 count)) | |
756 | |
846
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
757 ;; rms: This page is creeping featurism, and not worth having. |
257 | 758 |
759 ;;; Below are two regular expressions that attempt to match defuns | |
760 ;;; "strongly" and "weakly." The strong one almost reconstructs the | |
761 ;;; grammar of C++; the weak one just figures anything id or curly on | |
762 ;;; the left begins a defun. The constant "c++-match-header-strongly" | |
763 ;;; determines which to use; the default is the weak one. | |
764 | |
846
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
765 ;; (defvar c++-match-header-strongly nil |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
766 ;; "*If nil, use `c++-defun-header-weak' to identify beginning of definitions. |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
767 ;; If non-nil, use `c++-defun-header-strong'.") |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
768 ;; |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
769 ;; (defvar c++-defun-header-strong-struct-equivs "\\(class\\|struct\\|enum\\)" |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
770 ;; "Regexp to match names of structure declaration blocks in C++.") |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
771 ;; |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
772 ;; (defconst c++-defun-header-strong |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
773 ;; (let* |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
774 ;; (; valid identifiers |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
775 ;; ;; There's a real wierdness here -- if I switch the below |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
776 ;; (id "\\(\\w\\|_\\)+") |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
777 ;; ;; to be |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
778 ;; ;; (id "\\(_\\|\\w\\)+") |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
779 ;; ;; things no longer work right. Try it and see! |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
780 ;; |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
781 ;; ; overloadable operators |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
782 ;; (op-sym1 |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
783 ;; "[---+*/%^&|~!=<>]\\|[---+*/%^&|<>=!]=\\|<<=?\\|>>=?") |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
784 ;; (op-sym2 |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
785 ;; "&&\\|||\\|\\+\\+\\|--\\|()\\|\\[\\]") |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
786 ;; (op-sym (concat "\\(" op-sym1 "\\|" op-sym2 "\\)")) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
787 ;; ; whitespace |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
788 ;; (middle "[^\\*]*\\(\\*+[^/\\*][^\\*]*\\)*") |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
789 ;; (c-comment (concat "/\\*" middle "\\*+/")) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
790 ;; (wh (concat "\\(\\s \\|\n\\|//.*$\\|" c-comment "\\)")) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
791 ;; (wh-opt (concat wh "*")) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
792 ;; (wh-nec (concat wh "+")) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
793 ;; (oper (concat "\\(" "operator" "\\(" |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
794 ;; wh-opt op-sym "\\|" wh-nec id "\\)" "\\)")) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
795 ;; (dcl-list "([^():]*)") |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
796 ;; (func-name (concat "\\(" oper "\\|" id "::" id "\\|" id "\\)")) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
797 ;; (inits |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
798 ;; (concat "\\(:" |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
799 ;; "\\(" wh-opt id "(.*\\()" wh-opt "," "\\)\\)*" |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
800 ;; wh-opt id "(.*)" wh-opt "{" |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
801 ;; "\\|" wh-opt "{\\)")) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
802 ;; (type-name (concat |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
803 ;; "\\(" c++-defun-header-strong-struct-equivs wh-nec "\\)?" |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
804 ;; id)) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
805 ;; (type (concat "\\(const" wh-nec "\\)?" |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
806 ;; "\\(" type-name "\\|" type-name wh-opt "\\*+" "\\|" |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
807 ;; type-name wh-opt "&" "\\)")) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
808 ;; (modifier "\\(inline\\|virtual\\|overload\\|auto\\|static\\)") |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
809 ;; (modifiers (concat "\\(" modifier wh-nec "\\)*")) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
810 ;; (func-header |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
811 ;; ;; type arg-dcl |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
812 ;; (concat modifiers type wh-nec func-name wh-opt dcl-list wh-opt inits)) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
813 ;; (inherit (concat "\\(:" wh-opt "\\(public\\|private\\)?" |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
814 ;; wh-nec id "\\)")) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
815 ;; (cs-header (concat |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
816 ;; c++-defun-header-strong-struct-equivs |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
817 ;; wh-nec id wh-opt inherit "?" wh-opt "{"))) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
818 ;; (concat "^\\(" func-header "\\|" cs-header "\\)")) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
819 ;; "Strongly-defined regexp to match beginning of structure or function def.") |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
820 ;; |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
821 ;; |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
822 ;; ;; This part has to do with recognizing defuns. |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
823 ;; |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
824 ;; ;; The weak convention we will use is that a defun begins any time |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
825 ;; ;; there is a left curly brace, or some identifier on the left margin, |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
826 ;; ;; followed by a left curly somewhere on the line. (This will also |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
827 ;; ;; incorrectly match some continued strings, but this is after all |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
828 ;; ;; just a weak heuristic.) Suggestions for improvement (short of the |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
829 ;; ;; strong scheme shown above) are welcomed. |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
830 ;; |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
831 ;; (defconst c++-defun-header-weak "^{\\|^[_a-zA-Z].*{" |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
832 ;; "Weakly-defined regexp to match beginning of structure or function def.") |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
833 ;; |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
834 ;; (defun c++-beginning-of-defun (arg) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
835 ;; (interactive "p") |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
836 ;; (let ((c++-defun-header (if c++-match-header-strongly |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
837 ;; c++-defun-header-strong |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
838 ;; c++-defun-header-weak))) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
839 ;; (cond ((or (= arg 0) (and (> arg 0) (bobp))) nil) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
840 ;; ((and (not (looking-at c++-defun-header)) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
841 ;; (let ((curr-pos (point)) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
842 ;; (open-pos (if (search-forward "{" nil 'move) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
843 ;; (point))) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
844 ;; (beg-pos |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
845 ;; (if (re-search-backward c++-defun-header nil 'move) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
846 ;; (match-beginning 0)))) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
847 ;; (if (and open-pos beg-pos |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
848 ;; (< beg-pos curr-pos) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
849 ;; (> open-pos curr-pos)) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
850 ;; (progn |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
851 ;; (goto-char beg-pos) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
852 ;; (if (= arg 1) t nil));; Are we done? |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
853 ;; (goto-char curr-pos) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
854 ;; nil)))) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
855 ;; (t |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
856 ;; (if (and (looking-at c++-defun-header) (not (bobp))) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
857 ;; (forward-char (if (< arg 0) 1 -1))) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
858 ;; (and (re-search-backward c++-defun-header nil 'move (or arg 1)) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
859 ;; (goto-char (match-beginning 0))))))) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
860 ;; |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
861 ;; |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
862 ;; (defun c++-end-of-defun (arg) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
863 ;; (interactive "p") |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
864 ;; (let ((c++-defun-header (if c++-match-header-strongly |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
865 ;; c++-defun-header-strong |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
866 ;; c++-defun-header-weak))) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
867 ;; (if (and (eobp) (> arg 0)) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
868 ;; nil |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
869 ;; (if (and (> arg 0) (looking-at c++-defun-header)) (forward-char 1)) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
870 ;; (let ((pos (point))) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
871 ;; (c++-beginning-of-defun |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
872 ;; (if (< arg 0) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
873 ;; (- (- arg (if (eobp) 0 1))) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
874 ;; arg)) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
875 ;; (if (and (< arg 0) (bobp)) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
876 ;; t |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
877 ;; (if (re-search-forward c++-defun-header nil 'move) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
878 ;; (progn (forward-char -1) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
879 ;; (forward-sexp) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
880 ;; (beginning-of-line 2))) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
881 ;; (if (and (= pos (point)) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
882 ;; (re-search-forward c++-defun-header nil 'move)) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
883 ;; (c++-end-of-defun 1)))) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
884 ;; t))) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
885 ;; |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
886 ;; (defun c++-indent-defun () |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
887 ;; "Indents the current function definition, struct or class declaration." |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
888 ;; (interactive) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
889 ;; (let ((restore (point))) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
890 ;; (c++-end-of-defun 1) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
891 ;; (beginning-of-line 1) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
892 ;; (let ((end (point))) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
893 ;; (c++-beginning-of-defun 1) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
894 ;; (while (<= (point) end) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
895 ;; (c++-indent-line) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
896 ;; (next-line 1) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
897 ;; (beginning-of-line 1))) |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
798
diff
changeset
|
898 ;; (goto-char restore))) |
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
257
diff
changeset
|
899 |
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
257
diff
changeset
|
900 ;;; c++-mode.el ends here |