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