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