(undigestify-rmail-message): Better error messages.
[bpt/emacs.git] / 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 ;;;###autoload
243 (defun c++-mode ()
244 "Major mode for editing C++ code. Very much like editing C code.
245 Expression and list commands understand all C++ brackets.
246 Tab at left margin indents for C++ code
247 Comments are delimited with /* ... */ {or with // ... <newline>}
248 Paragraphs are separated by blank lines only.
249 Delete converts tabs to spaces as it moves back.
250 \\{c++-mode-map}
251 Variables controlling indentation style:
252 c-tab-always-indent
253 Non-nil means TAB in C mode should always reindent the current line,
254 regardless of where in the line point is when the TAB command is used.
255 Default is t.
256 c-auto-newline
257 Non-nil means automatically newline before and after braces,
258 and after colons and semicolons, inserted in C code.
259 c-indent-level
260 Indentation of C statements within surrounding block.
261 The surrounding block's indentation is the indentation
262 of the line on which the open-brace appears.
263 c-continued-statement-offset
264 Extra indentation given to a substatement, such as the
265 then-clause of an if or body of a while.
266 c-continued-brace-offset
267 Extra indentation given to a brace that starts a substatement.
268 This is in addition to c-continued-statement-offset.
269 c-brace-offset
270 Extra indentation for line if it starts with an open brace.
271 c-brace-imaginary-offset
272 An open brace following other text is treated as if it were
273 this far to the right of the start of its line.
274 c-argdecl-indent
275 Indentation level of declarations of C function arguments.
276 c-label-offset
277 Extra indentation for line that is a label, or case or ``default:'', or
278 ``public:'' or ``private:'', or ``protected:''.
279 c++-electric-colon
280 If non-nil at invocation of c++-mode (t is the default) colon electricly
281 indents.
282 c++-empty-arglist-indent
283 If non-nil, a function declaration or invocation which ends a line with a
284 left paren is indented this many extra spaces, instead of flush with the
285 left paren.
286 c++-friend-offset
287 Offset of C++ friend declarations relative to member declarations.
288 c++-member-init-indent
289 Indentation level of member initializations in function declarations,
290 if they are on a separate line beginning with a colon.
291 c++-continued-member-init-offset
292 Extra indentation for continuation lines of member initializations; NIL
293 means to align with previous initializations rather than with the colon.
294
295 Settings for K&R, BSD, and Stroustrup indentation styles are
296 c-indent-level 5 8 4
297 c-continued-statement-offset 5 8 4
298 c-continued-brace-offset 0
299 c-brace-offset -5 -8 0
300 c-brace-imaginary-offset 0
301 c-argdecl-indent 0 8 4
302 c-label-offset -5 -8 -4
303 c++-empty-arglist-indent 4
304 c++-friend-offset 0
305
306 Turning on C++ mode calls the value of the variable `c++-mode-hook' with
307 no args if that value is non-nil."
308 (interactive)
309 (kill-all-local-variables)
310 (use-local-map c++-mode-map)
311 (set-syntax-table c++-mode-syntax-table)
312 (setq major-mode 'c++-mode
313 mode-name "C++"
314 comment-column 32
315 local-abbrev-table c++-mode-abbrev-table)
316 (set (make-local-variable 'indent-line-function) 'c++-indent-line)
317 (set (make-local-variable 'comment-start) "// ")
318 (set (make-local-variable 'comment-end) "")
319 (set (make-local-variable 'comment-start-skip) "/\\*+ *\\|// *")
320 (set (make-local-variable 'comment-indent-function) 'c++-comment-indent)
321 (set (make-local-variable 'paragraph-start) (concat "$\\|" page-delimiter))
322 (set (make-local-variable 'paragraph-separate) paragraph-start)
323 (set (make-local-variable 'paragraph-ignore-fill-prefix) t)
324 (set (make-local-variable 'require-final-newline) t)
325 (set (make-local-variable 'parse-sexp-ignore-comments) t)
326 (make-local-variable 'imenu-generic-expression)
327 (setq imenu-generic-expression c++-imenu-generic-expression)
328 (run-hooks 'c++-mode-hook)
329 (if c++-electric-colon
330 (define-key c++-mode-map ":" 'electric-c++-terminator)))
331
332 ;; This is used by indent-for-comment
333 ;; to decide how much to indent a comment in C++ code
334 ;; based on its context.
335 (defun c++-comment-indent ()
336 (if (looking-at "^\\(/\\*\\|//\\)")
337 0 ; Existing comment at bol stays there.
338 (save-excursion
339 (skip-chars-backward " \t")
340 (max
341 ;; leave at least one space on non-empty lines.
342 (if (zerop (current-column)) 0 (1+ (current-column)))
343 (let ((cur-pt (point)))
344 (beginning-of-line 0)
345 ;; If previous line had a comment, use it's indent
346 (if (re-search-forward comment-start-skip cur-pt t)
347 (progn
348 (goto-char (match-beginning 0))
349 (current-column))
350 comment-column)))))) ; otherwise indent at comment column.
351
352 (defun electric-c++-brace (arg)
353 "Insert character and correct line's indentation."
354 (interactive "P")
355 (let (insertpos)
356 (if (and (not arg)
357 (eolp)
358 (or (save-excursion
359 (skip-chars-backward " \t")
360 (bolp))
361 (if c-auto-newline (progn (c++-indent-line) (newline) t))))
362 (progn
363 (insert last-command-char)
364 (c++-indent-line)
365 (if c-auto-newline
366 (progn
367 (newline)
368 ;; (newline) may have done auto-fill
369 (setq insertpos (- (point) 2))
370 (c++-indent-line)))
371 (save-excursion
372 (if insertpos (goto-char (1+ insertpos)))
373 (delete-char -1))))
374 (if insertpos
375 (save-excursion
376 (goto-char insertpos)
377 (self-insert-command (prefix-numeric-value arg)))
378 (self-insert-command (prefix-numeric-value arg)))))
379
380 (defun electric-c++-semi (arg)
381 "Insert character and correct line's indentation."
382 (interactive "P")
383 (if c-auto-newline
384 (electric-c++-terminator arg)
385 (self-insert-command (prefix-numeric-value arg))))
386
387 (defun electric-c++-terminator (arg)
388 "Insert character and correct line's indentation."
389 (interactive "P")
390 (let (insertpos (end (point)))
391 (if (and (not arg) (eolp)
392 (not (save-excursion
393 (beginning-of-line)
394 (skip-chars-forward " \t")
395 (or (= (following-char) ?#)
396 ;; Colon is special only after a label, or
397 ;; case, or another colon.
398 ;; So quickly rule out most other uses of colon
399 ;; and do no indentation for them.
400 (and (eq last-command-char ?:)
401 (or (not (or (looking-at "case[ \t]")
402 (save-excursion
403 (forward-word 1)
404 (skip-chars-forward " \t")
405 (>= (point) end))))
406 ;; Do re-indent double colons
407 (save-excursion
408 (end-of-line 1)
409 (looking-at ":"))))
410 (progn
411 (beginning-of-defun)
412 (let ((pps (parse-partial-sexp (point) end)))
413 (or (nth 3 pps) (nth 4 pps) (nth 5 pps))))))))
414 (progn
415 (insert last-command-char)
416 (c++-indent-line)
417 (and c-auto-newline
418 (not (c-inside-parens-p))
419 (progn
420 ;; the new marker object, used to be just an integer
421 (setq insertpos (make-marker))
422 ;; changed setq to set-marker
423 (set-marker insertpos (1- (point)))
424 ;; do this before the newline, since in auto fill can break
425 (newline)
426 (c-indent-line)))
427 (save-excursion
428 (if insertpos (goto-char (1+ insertpos)))
429 (delete-char -1))))
430 (if insertpos
431 (save-excursion
432 (goto-char insertpos)
433 (self-insert-command (prefix-numeric-value arg)))
434 (self-insert-command (prefix-numeric-value arg)))))
435
436 (defun c++-indent-command (&optional whole-exp)
437 "Indent current line as C++ code, or in some cases insert a tab character.
438 If `c-tab-always-indent' is non-nil (the default), always indent current
439 line. Otherwise, indent the current line only if point is at the left
440 margin or in the line's indentation; otherwise insert a tab.
441
442 A numeric argument, regardless of its value, means indent rigidly all means
443 indent rigidly all the lines of the expression starting after point so that
444 this line becomes properly indented. The relative indentation among the
445 lines of the expression are preserved."
446 (interactive "P")
447 (if whole-exp
448 ;; If arg, always indent this line as C
449 ;; and shift remaining lines of expression the same amount.
450 (let ((shift-amt (c++-indent-line))
451 beg end)
452 (save-excursion
453 (if c-tab-always-indent
454 (beginning-of-line))
455 (setq beg (point))
456 (forward-sexp 1)
457 (setq end (point))
458 (goto-char beg)
459 (forward-line 1)
460 (setq beg (point)))
461 (if (> end beg)
462 (indent-code-rigidly beg end shift-amt "#")))
463 (if (and (not c-tab-always-indent)
464 (save-excursion
465 (skip-chars-backward " \t")
466 (not (bolp))))
467 (insert-tab)
468 (c++-indent-line))))
469
470 (defun c++-indent-line ()
471 "Indent current line as C++ code.
472 Return the amount the indentation changed by."
473 (let ((indent (calculate-c++-indent nil))
474 beg shift-amt
475 (case-fold-search nil)
476 (pos (- (point-max) (point))))
477 (beginning-of-line)
478 (setq beg (point))
479 (cond ((eq indent nil)
480 (setq indent (current-indentation)))
481 ((eq indent t)
482 (setq indent (calculate-c-indent-within-comment)))
483 ((looking-at "[ \t]*#")
484 (setq indent 0))
485 (t
486 (skip-chars-forward " \t")
487 (if (listp indent) (setq indent (car indent)))
488 (cond ((looking-at "\\(default\\|public\\|private\\|protected\\):")
489 (setq indent (+ indent c-label-offset)))
490 ((or (looking-at "case\\b")
491 (and (looking-at "[A-Za-z]")
492 (save-excursion
493 (forward-sexp 1)
494 (looking-at ":[^:]"))))
495 (setq indent (max 1 (+ indent c-label-offset))))
496 ((and (looking-at "else\\b")
497 (not (looking-at "else\\s_")))
498 (setq indent (save-excursion
499 (c-backward-to-start-of-if)
500 (current-indentation))))
501 ((looking-at "friend\[ \t]")
502 (setq indent (+ indent c++-friend-offset)))
503 ((= (following-char) ?})
504 (setq indent (- indent c-indent-level)))
505 ((= (following-char) ?{)
506 (setq indent (+ indent c-brace-offset))))))
507 (skip-chars-forward " \t")
508 (setq shift-amt (- indent (current-column)))
509 (if (zerop shift-amt)
510 (if (> (- (point-max) pos) (point))
511 (goto-char (- (point-max) pos)))
512 (delete-region beg (point))
513 (indent-to indent)
514 ;; If initial point was within line's indentation,
515 ;; position after the indentation. Else stay at same point in text.
516 (if (> (- (point-max) pos) (point))
517 (goto-char (- (point-max) pos))))
518 shift-amt))
519
520 (defun calculate-c++-indent (&optional parse-start)
521 "Return appropriate indentation for current line as C++ code.
522 In usual case returns an integer: the column to indent to.
523 Returns nil if line starts inside a string, t if in a comment."
524 (save-excursion
525 (beginning-of-line)
526 (let ((indent-point (point))
527 (case-fold-search nil)
528 state
529 containing-sexp)
530 (if parse-start
531 (goto-char parse-start)
532 (beginning-of-defun))
533 (while (< (point) indent-point)
534 (setq parse-start (point))
535 (setq state (parse-partial-sexp (point) indent-point 0))
536 (setq containing-sexp (car (cdr state))))
537 (cond ((or (nth 3 state) (nth 4 state))
538 ;; return nil or t if should not change this line
539 (nth 4 state))
540 ((null containing-sexp)
541 ;; Line is at top level. May be data or function definition, or
542 ;; may be function argument declaration or member initialization.
543 ;; Indent like the previous top level line unless
544 ;; (1) the previous line ends in a closeparen without semicolon,
545 ;; in which case this line is the first argument declaration or
546 ;; member initialization, or
547 ;; (2) the previous line begins with a colon,
548 ;; in which case this is the second line of member inits.
549 ;; It is assumed that arg decls and member inits are not mixed.
550 (goto-char indent-point)
551 (skip-chars-forward " \t")
552 (if (= (following-char) ?{)
553 0 ; Unless it starts a function body
554 (c++-backward-to-noncomment (or parse-start (point-min)))
555 (if (= (preceding-char) ?\))
556 (progn ; first arg decl or member init
557 (goto-char indent-point)
558 (skip-chars-forward " \t")
559 (if (= (following-char) ?:)
560 c++-member-init-indent
561 c-argdecl-indent))
562 (if (= (preceding-char) ?\;)
563 (backward-char 1))
564 (if (= (preceding-char) ?})
565 0
566 (beginning-of-line) ; continued arg decls or member inits
567 (skip-chars-forward " \t")
568 (if (= (following-char) ?:)
569 (if c++-continued-member-init-offset
570 (+ (current-indentation)
571 c++-continued-member-init-offset)
572 (progn
573 (forward-char 1)
574 (skip-chars-forward " \t")
575 (current-column)))
576 (current-indentation)))
577 )))
578 ((/= (char-after containing-sexp) ?{)
579 ;; line is expression, not statement:
580 ;; indent to just after the surrounding open -- unless
581 ;; empty arg list, in which case we do what
582 ;; c++-empty-arglist-indent says to do.
583 (if (and c++-empty-arglist-indent
584 (or (null (nth 2 state)) ;; indicates empty arg
585 ;; list.
586 ;; Use a heuristic: if the first
587 ;; non-whitespace following left paren on
588 ;; same line is not a comment,
589 ;; is not an empty arglist.
590 (save-excursion
591 (goto-char (1+ containing-sexp))
592 (not
593 (looking-at "\\( \\|\t\\)*[^/\n]")))))
594 (progn
595 (goto-char containing-sexp)
596 (beginning-of-line)
597 (skip-chars-forward " \t")
598 (goto-char (min (+ (point) c++-empty-arglist-indent)
599 (1+ containing-sexp)))
600 (current-column))
601 ;; In C-mode, we would always indent to one after the
602 ;; left paren. Here, though, we may have an
603 ;; empty-arglist, so we'll indent to the min of that
604 ;; and the beginning of the first argument.
605 (goto-char (1+ containing-sexp))
606 (current-column)))
607 (t
608 ;; Statement. Find previous non-comment character.
609 (goto-char indent-point)
610 (c++-backward-to-noncomment containing-sexp)
611 (if (not (memq (preceding-char) '(nil ?\, ?\; ?} ?: ?\{)))
612 ;; This line is continuation of preceding line's statement;
613 ;; indent c-continued-statement-offset more than the
614 ;; previous line of the statement.
615 (progn
616 (c-backward-to-start-of-continued-exp containing-sexp)
617 (+ c-continued-statement-offset (current-column)
618 (if (save-excursion (goto-char indent-point)
619 (skip-chars-forward " \t")
620 (eq (following-char) ?{))
621 c-continued-brace-offset 0)))
622 ;; This line starts a new statement.
623 ;; Position following last unclosed open.
624 (goto-char containing-sexp)
625 ;; Is line first statement after an open-brace?
626 (or
627 ;; If no, find that first statement and indent like it.
628 (save-excursion
629 (forward-char 1)
630 (let ((colon-line-end 0))
631 (while (progn (skip-chars-forward " \t\n")
632 (looking-at
633 (concat
634 "#\\|/\\*\\|//"
635 "\\|case[ \t]"
636 "\\|[a-zA-Z0-9_$]*:[^:]"
637 "\\|friend[ \t]")))
638 ;; Skip over comments and labels following openbrace.
639 (cond ((= (following-char) ?\#)
640 (forward-line 1))
641 ((looking-at "/\\*")
642 (search-forward "*/" nil 'move))
643 ((looking-at "//\\|friend[ \t]")
644 (forward-line 1))
645 (t
646 (save-excursion (end-of-line)
647 (setq colon-line-end (point)))
648 (search-forward ":"))))
649 ;; The first following code counts
650 ;; if it is before the line we want to indent.
651 (and (< (point) indent-point)
652 (-
653 (if (> colon-line-end (point))
654 (- (current-indentation) c-label-offset)
655 (current-column))
656 ;; If prev stmt starts with open-brace, that
657 ;; open brace was offset by c-brace-offset.
658 ;; Compensate to get the column where
659 ;; an ordinary statement would start.
660 (if (= (following-char) ?\{) c-brace-offset 0)))))
661 ;; If no previous statement,
662 ;; indent it relative to line brace is on.
663 ;; For open brace in column zero, don't let statement
664 ;; start there too. If c-indent-offset is zero,
665 ;; use c-brace-offset + c-continued-statement-offset instead.
666 ;; For open-braces not the first thing in a line,
667 ;; add in c-brace-imaginary-offset.
668 (+ (if (and (bolp) (zerop c-indent-level))
669 (+ c-brace-offset c-continued-statement-offset)
670 c-indent-level)
671 ;; Move back over whitespace before the openbrace.
672 ;; If openbrace is not first nonwhite thing on the line,
673 ;; add the c-brace-imaginary-offset.
674 (progn (skip-chars-backward " \t")
675 (if (bolp) 0 c-brace-imaginary-offset))
676 ;; If the openbrace is preceded by a parenthesized exp,
677 ;; move to the beginning of that;
678 ;; possibly a different line
679 (progn
680 (if (eq (preceding-char) ?\))
681 (forward-sexp -1))
682 ;; Get initial indentation of the line we are on.
683 (current-indentation))))))))))
684
685 (defun c++-backward-to-noncomment (lim)
686 (let (opoint stop)
687 (while (not stop)
688 (skip-chars-backward " \t\n\r\f" lim)
689 (setq opoint (point))
690 (cond ((and (>= (point) (+ 2 lim))
691 (save-excursion
692 (forward-char -2)
693 (looking-at "\\*/")))
694 (search-backward "/*" lim 'move))
695 ((and
696 (search-backward "//" (max (c++-point-bol) lim) 'move)
697 (not (c++-within-string-p (point) opoint))))
698 ;; No comment to be found.
699 ;; If there's a # command on this line,
700 ;; move back to it.
701 (t (beginning-of-line)
702 (skip-chars-forward " \t")
703 ;; But don't get fooled if we are already before the #.
704 (if (and (looking-at "#") (< (point) opoint))
705 (setq stop (<= (point) lim))
706 (setq stop t)
707 (goto-char opoint)))))))
708
709 (defun indent-c++-exp ()
710 "Indent each line of the C++ grouping following point."
711 (interactive)
712 (let ((indent-stack (list nil))
713 (contain-stack (list (point)))
714 (case-fold-search nil)
715 restart outer-loop-done inner-loop-done state ostate
716 this-indent last-sexp last-depth
717 at-else at-brace
718 (opoint (point))
719 (next-depth 0))
720 (save-excursion
721 (forward-sexp 1))
722 (save-excursion
723 (setq outer-loop-done nil)
724 (while (and (not (eobp)) (not outer-loop-done))
725 (setq last-depth next-depth)
726 ;; Compute how depth changes over this line
727 ;; plus enough other lines to get to one that
728 ;; does not end inside a comment or string.
729 ;; Meanwhile, do appropriate indentation on comment lines.
730 (setq inner-loop-done nil)
731 (while (and (not inner-loop-done)
732 (not (and (eobp) (setq outer-loop-done t))))
733 (setq ostate state)
734 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
735 nil nil state))
736 (setq next-depth (car state))
737 (if (and (car (cdr (cdr state)))
738 (>= (car (cdr (cdr state))) 0))
739 (setq last-sexp (car (cdr (cdr state)))))
740 (if (or (nth 4 ostate))
741 (c++-indent-line))
742 (if (or (nth 3 state))
743 (forward-line 1)
744 (setq inner-loop-done t)))
745 (if (<= next-depth 0)
746 (setq outer-loop-done t))
747 (if outer-loop-done
748 nil
749 ;; If this line had ..))) (((.. in it, pop out of the levels
750 ;; that ended anywhere in this line, even if the final depth
751 ;; doesn't indicate that they ended.
752 (while (> last-depth (nth 6 state))
753 (setq indent-stack (cdr indent-stack)
754 contain-stack (cdr contain-stack)
755 last-depth (1- last-depth)))
756 (if (/= last-depth next-depth)
757 (setq last-sexp nil))
758 ;; Add levels for any parens that were started in this line.
759 (while (< last-depth next-depth)
760 (setq indent-stack (cons nil indent-stack)
761 contain-stack (cons nil contain-stack)
762 last-depth (1+ last-depth)))
763 (if (null (car contain-stack))
764 (setcar contain-stack (or (car (cdr state))
765 (save-excursion (forward-sexp -1)
766 (point)))))
767 (forward-line 1)
768 (skip-chars-forward " \t")
769 (if (eolp)
770 nil
771 (if (and (car indent-stack)
772 (>= (car indent-stack) 0))
773 ;; Line is on an existing nesting level.
774 ;; Lines inside parens are handled specially.
775 nil
776 ;; Just started a new nesting level.
777 ;; Compute the standard indent for this level.
778 (let (val)
779 (if (= (char-after (car contain-stack)) ?{)
780 (save-excursion
781 (goto-char (car contain-stack))
782 (setq val (+ c-indent-level (current-column))))
783 (setq val (calculate-c++-indent
784 (if (car indent-stack)
785 (- (car indent-stack))))))
786 (setcar indent-stack val)))
787 ;; Adjust line indentation according to its predecessor.
788 (if (/= (char-after (car contain-stack)) ?\{)
789 (setq this-indent (car indent-stack))
790 ;; Line is at statement level.
791 ;; Is it a new statement? Is it an else?
792 ;; Find last non-comment character before this line
793 (save-excursion
794 (setq at-else (looking-at "else\\W"))
795 (setq at-brace (= (following-char) ?\{))
796 (c++-backward-to-noncomment opoint)
797 (if (not (memq (preceding-char) '(nil ?\, ?\; ?\} ?: ?\{)))
798 ;; Preceding line did not end in comma or semi;
799 ;; indent this line c-continued-statement-offset
800 ;; more than previous.
801 (progn
802 (c-backward-to-start-of-continued-exp
803 (car contain-stack))
804 (setq this-indent
805 (+ c-continued-statement-offset
806 (current-column)
807 (if at-brace c-continued-brace-offset 0))))
808 ;; Preceding line ended in comma or semi;
809 ;; use the standard indent for this level.
810 (if at-else
811 (progn (c-backward-to-start-of-if opoint)
812 (setq this-indent (current-indentation)))
813 (setq this-indent (car indent-stack))))))
814 ;; Adjust line indentation according to its contents
815 (if (looking-at "\\(public\\|private\\|protected\\):")
816 (setq this-indent (- this-indent c-indent-level))
817 (if (or (looking-at "case[ \t]")
818 (and (looking-at "[A-Za-z]")
819 (save-excursion
820 (forward-sexp 1)
821 (looking-at ":[^:]"))))
822 (setq this-indent (max 1 (+ this-indent c-label-offset)))))
823 (if (looking-at "friend[ \t]")
824 (setq this-indent (+ this-indent c++-friend-offset)))
825 (if (= (following-char) ?\})
826 (setq this-indent (- this-indent c-indent-level)))
827 (if (= (following-char) ?\{)
828 (setq this-indent (+ this-indent c-brace-offset)))
829 ;; Put chosen indentation into effect.
830 (or (= (current-column) this-indent)
831 (= (following-char) ?\#)
832 (progn
833 (delete-region (point) (progn (beginning-of-line) (point)))
834 (indent-to this-indent)))
835 ;; Indent any comment following the text.
836 (or (looking-at comment-start-skip)
837 (if (re-search-forward comment-start-skip
838 (save-excursion (end-of-line)
839 (point)) t)
840 (progn
841 (indent-for-comment)
842 (beginning-of-line))))))))))
843 \f
844 (defun fill-c++-comment ()
845 "Fill a comment contained in consecutive lines containing point.
846 The fill lines remain a comment."
847 (interactive)
848 (save-excursion
849 (let ((save fill-prefix))
850 (beginning-of-line 1)
851 (save-excursion
852 (re-search-forward comment-start-skip
853 (save-excursion (end-of-line) (point))
854 t)
855 (goto-char (match-end 0))
856 (set-fill-prefix))
857 (while (looking-at fill-prefix)
858 (previous-line 1))
859 (next-line 1)
860 (insert-string "\n")
861 (fill-paragraph nil)
862 (delete-char -1)
863 (setq fill-prefix save))))
864
865 (defun c++-point-bol ()
866 "Returns the value of the point at the beginning of the current line."
867 (save-excursion
868 (beginning-of-line)
869 (point)))
870
871 ;; (defun c++-insert-header ()
872 ;; "Insert header denoting C++ code at top of buffer."
873 ;; (interactive)
874 ;; (save-excursion
875 ;; (goto-char (point-min))
876 ;; (insert "// "
877 ;; "This may look like C code, but it is really "
878 ;; "-*- C++ -*-"
879 ;; "\n\n")))
880
881 (defun c++-within-string-p (point1 point2)
882 "Returns true if number of double quotes between two points is odd."
883 (let ((s (buffer-substring point1 point2)))
884 (not (zerop (% (c++-count-char-in-string ?\" s) 2)))))
885
886 (defun c++-count-char-in-string (c s)
887 (let ((count 0)
888 (pos 0))
889 (while (< pos (length s))
890 (setq count (+ count (if (\= (aref s pos) c) 1 0)))
891 (setq pos (1+ pos)))
892 count))
893 \f
894 ;; rms: This page is creeping featurism, and not worth having.
895
896 ;;; Below are two regular expressions that attempt to match defuns
897 ;;; "strongly" and "weakly." The strong one almost reconstructs the
898 ;;; grammar of C++; the weak one just figures anything id or curly on
899 ;;; the left begins a defun. The constant "c++-match-header-strongly"
900 ;;; determines which to use; the default is the weak one.
901
902 ;; (defvar c++-match-header-strongly nil
903 ;; "*If nil, use `c++-defun-header-weak' to identify beginning of definitions.
904 ;; If non-nil, use `c++-defun-header-strong'.")
905 ;;
906 ;; (defvar c++-defun-header-strong-struct-equivs "\\(class\\|struct\\|enum\\)"
907 ;; "Regexp to match names of structure declaration blocks in C++.")
908 ;;
909 ;; (defconst c++-defun-header-strong
910 ;; (let*
911 ;; (; valid identifiers
912 ;; ;; There's a real weirdness here -- if I switch the below
913 ;; (id "\\(\\w\\|_\\)+")
914 ;; ;; to be
915 ;; ;; (id "\\(_\\|\\w\\)+")
916 ;; ;; things no longer work right. Try it and see!
917 ;;
918 ;; ; overloadable operators
919 ;; (op-sym1
920 ;; "[-+*/%^&|~!=<>]\\|[-+*/%^&|<>=!]=\\|<<=?\\|>>=?")
921 ;; (op-sym2
922 ;; "&&\\|||\\|\\+\\+\\|--\\|()\\|\\[\\]")
923 ;; (op-sym (concat "\\(" op-sym1 "\\|" op-sym2 "\\)"))
924 ;; ; whitespace
925 ;; (middle "[^\\*]*\\(\\*+[^/\\*][^\\*]*\\)*")
926 ;; (c-comment (concat "/\\*" middle "\\*+/"))
927 ;; (wh (concat "\\(\\s \\|\n\\|//.*$\\|" c-comment "\\)"))
928 ;; (wh-opt (concat wh "*"))
929 ;; (wh-nec (concat wh "+"))
930 ;; (oper (concat "\\(" "operator" "\\("
931 ;; wh-opt op-sym "\\|" wh-nec id "\\)" "\\)"))
932 ;; (dcl-list "([^():]*)")
933 ;; (func-name (concat "\\(" oper "\\|" id "::" id "\\|" id "\\)"))
934 ;; (inits
935 ;; (concat "\\(:"
936 ;; "\\(" wh-opt id "(.*\\()" wh-opt "," "\\)\\)*"
937 ;; wh-opt id "(.*)" wh-opt "{"
938 ;; "\\|" wh-opt "{\\)"))
939 ;; (type-name (concat
940 ;; "\\(" c++-defun-header-strong-struct-equivs wh-nec "\\)?"
941 ;; id))
942 ;; (type (concat "\\(const" wh-nec "\\)?"
943 ;; "\\(" type-name "\\|" type-name wh-opt "\\*+" "\\|"
944 ;; type-name wh-opt "&" "\\)"))
945 ;; (modifier "\\(inline\\|virtual\\|overload\\|auto\\|static\\)")
946 ;; (modifiers (concat "\\(" modifier wh-nec "\\)*"))
947 ;; (func-header
948 ;; ;; type arg-dcl
949 ;; (concat modifiers type wh-nec func-name wh-opt dcl-list wh-opt inits))
950 ;; (inherit (concat "\\(:" wh-opt "\\(public\\|private\\)?"
951 ;; wh-nec id "\\)"))
952 ;; (cs-header (concat
953 ;; c++-defun-header-strong-struct-equivs
954 ;; wh-nec id wh-opt inherit "?" wh-opt "{")))
955 ;; (concat "^\\(" func-header "\\|" cs-header "\\)"))
956 ;; "Strongly-defined regexp to match beginning of structure or function def.")
957 ;;
958 ;;
959 ;; ;; This part has to do with recognizing defuns.
960 ;;
961 ;; ;; The weak convention we will use is that a defun begins any time
962 ;; ;; there is a left curly brace, or some identifier on the left margin,
963 ;; ;; followed by a left curly somewhere on the line. (This will also
964 ;; ;; incorrectly match some continued strings, but this is after all
965 ;; ;; just a weak heuristic.) Suggestions for improvement (short of the
966 ;; ;; strong scheme shown above) are welcomed.
967 ;;
968 ;; (defconst c++-defun-header-weak "^{\\|^[_a-zA-Z].*{"
969 ;; "Weakly-defined regexp to match beginning of structure or function def.")
970 ;;
971 ;; (defun c++-beginning-of-defun (arg)
972 ;; (interactive "p")
973 ;; (let ((c++-defun-header (if c++-match-header-strongly
974 ;; c++-defun-header-strong
975 ;; c++-defun-header-weak)))
976 ;; (cond ((or (= arg 0) (and (> arg 0) (bobp))) nil)
977 ;; ((and (not (looking-at c++-defun-header))
978 ;; (let ((curr-pos (point))
979 ;; (open-pos (if (search-forward "{" nil 'move)
980 ;; (point)))
981 ;; (beg-pos
982 ;; (if (re-search-backward c++-defun-header nil 'move)
983 ;; (match-beginning 0))))
984 ;; (if (and open-pos beg-pos
985 ;; (< beg-pos curr-pos)
986 ;; (> open-pos curr-pos))
987 ;; (progn
988 ;; (goto-char beg-pos)
989 ;; (if (= arg 1) t nil));; Are we done?
990 ;; (goto-char curr-pos)
991 ;; nil))))
992 ;; (t
993 ;; (if (and (looking-at c++-defun-header) (not (bobp)))
994 ;; (forward-char (if (< arg 0) 1 -1)))
995 ;; (and (re-search-backward c++-defun-header nil 'move (or arg 1))
996 ;; (goto-char (match-beginning 0)))))))
997 ;;
998 ;;
999 ;; (defun c++-end-of-defun (arg)
1000 ;; (interactive "p")
1001 ;; (let ((c++-defun-header (if c++-match-header-strongly
1002 ;; c++-defun-header-strong
1003 ;; c++-defun-header-weak)))
1004 ;; (if (and (eobp) (> arg 0))
1005 ;; nil
1006 ;; (if (and (> arg 0) (looking-at c++-defun-header)) (forward-char 1))
1007 ;; (let ((pos (point)))
1008 ;; (c++-beginning-of-defun
1009 ;; (if (< arg 0)
1010 ;; (- (- arg (if (eobp) 0 1)))
1011 ;; arg))
1012 ;; (if (and (< arg 0) (bobp))
1013 ;; t
1014 ;; (if (re-search-forward c++-defun-header nil 'move)
1015 ;; (progn (forward-char -1)
1016 ;; (forward-sexp)
1017 ;; (beginning-of-line 2)))
1018 ;; (if (and (= pos (point))
1019 ;; (re-search-forward c++-defun-header nil 'move))
1020 ;; (c++-end-of-defun 1))))
1021 ;; t)))
1022 ;;
1023 ;; (defun c++-indent-defun ()
1024 ;; "Indents the current function definition, struct or class declaration."
1025 ;; (interactive)
1026 ;; (let ((restore (point)))
1027 ;; (c++-end-of-defun 1)
1028 ;; (beginning-of-line 1)
1029 ;; (let ((end (point)))
1030 ;; (c++-beginning-of-defun 1)
1031 ;; (while (<= (point) end)
1032 ;; (c++-indent-line)
1033 ;; (next-line 1)
1034 ;; (beginning-of-line 1)))
1035 ;; (goto-char restore)))
1036
1037 ;;; cplus-md.el ends here