1 ;;; cplus-md.el --- C++ code editing commands for Emacs
2 ;;; Copyright (C) 1985, 1992, 1994, 1995 Free Software Foundation, Inc.
4 ;; This file is part of GNU Emacs.
5 ;; Keywords: c languages oop
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)
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.
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>
25 ;; 1987 Dave Detlefs <dld@cs.cmu.edu>
26 ;; and Stewart Clamen <clamen@cs.cmu.edu>.
27 ;; Done by fairly faithful modification of:
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.
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.
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.
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".
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.
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
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.)
82 ;; I made "'" *not* be a string delimiter, because that was causing
83 ;; comments containing contractions to ("// don't") to mess up paren
86 ;; I also incorporated another slight indentation fix from Glen
89 ;; We hope this is will make into version 19 of gnu-emacs.
91 ;; September 1988: incorporated changes from Fred Calm at Schlumberger.
92 ;; Also, made beginning(end)-of-defun, indent-defun work.
94 ;; August 1987: incorporated changes done by Glen Ditchfield of Waterloo.
98 (defvar c++-mode-abbrev-table nil
99 "Abbrev table used in C++ mode.")
100 (define-abbrev-table 'c++-mode-abbrev-table ())
102 (defvar c++-mode-map ()
103 "Keymap used in C++ mode.")
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))
121 (defvar c++-mode-syntax-table nil
122 "Syntax table used in C++ mode.")
124 (if c++-mode-syntax-table
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))
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.")
149 "Major mode for editing C++ code. Very much like editing C code.
150 Expression and list commands understand all C++ brackets.
151 Tab at left margin indents for C++ code
152 Comments are delimited with /* ... */ {or with // ... <newline>}
153 Paragraphs are separated by blank lines only.
154 Delete converts tabs to spaces as it moves back.
156 Variables controlling indentation style:
158 Non-nil means TAB in C mode should always reindent the current line,
159 regardless of where in the line point is when the TAB command is used.
162 Non-nil means automatically newline before and after braces,
163 and after colons and semicolons, inserted in C code.
165 Indentation of C statements within surrounding block.
166 The surrounding block's indentation is the indentation
167 of the line on which the open-brace appears.
168 c-continued-statement-offset
169 Extra indentation given to a substatement, such as the
170 then-clause of an if or body of a while.
171 c-continued-brace-offset
172 Extra indentation given to a brace that starts a substatement.
173 This is in addition to c-continued-statement-offset.
175 Extra indentation for line if it starts with an open brace.
176 c-brace-imaginary-offset
177 An open brace following other text is treated as if it were
178 this far to the right of the start of its line.
180 Indentation level of declarations of C function arguments.
182 Extra indentation for line that is a label, or case or ``default:'', or
183 ``public:'' or ``private:'', or ``protected:''.
185 If non-nil at invocation of c++-mode (t is the default) colon electricly
187 c++-empty-arglist-indent
188 If non-nil, a function declaration or invocation which ends a line with a
189 left paren is indented this many extra spaces, instead of flush with the
192 Offset of C++ friend declarations relative to member declarations.
193 c++-member-init-indent
194 Indentation level of member initializations in function declarations,
195 if they are on a separate line beginning with a colon.
196 c++-continued-member-init-offset
197 Extra indentation for continuation lines of member initializations; NIL
198 means to align with previous initializations rather than with the colon.
200 Settings for K&R, BSD, and Stroustrup indentation styles are
202 c-continued-statement-offset 5 8 4
203 c-continued-brace-offset 0
204 c-brace-offset -5 -8 0
205 c-brace-imaginary-offset 0
206 c-argdecl-indent 0 8 4
207 c-label-offset -5 -8 -4
208 c++-empty-arglist-indent 4
211 Turning on C++ mode calls the value of the variable `c++-mode-hook' with
212 no args if that value is non-nil."
214 (kill-all-local-variables)
215 (use-local-map c++-mode-map)
216 (set-syntax-table c++-mode-syntax-table)
217 (setq major-mode 'c++-mode
220 local-abbrev-table c++-mode-abbrev-table)
221 (set (make-local-variable 'indent-line-function) 'c++-indent-line)
222 (set (make-local-variable 'comment-start) "// ")
223 (set (make-local-variable 'comment-end) "")
224 (set (make-local-variable 'comment-start-skip) "/\\*+ *\\|// *")
225 (set (make-local-variable 'comment-indent-function) 'c++-comment-indent)
226 (set (make-local-variable 'paragraph-start) (concat "$\\|" page-delimiter))
227 (set (make-local-variable 'paragraph-separate) paragraph-start)
228 (set (make-local-variable 'paragraph-ignore-fill-prefix) t)
229 (set (make-local-variable 'require-final-newline) t)
230 (set (make-local-variable 'parse-sexp-ignore-comments) t)
231 (run-hooks 'c++-mode-hook)
232 (if c++-electric-colon
233 (define-key c++-mode-map ":" 'electric-c++-terminator)))
235 ;; This is used by indent-for-comment
236 ;; to decide how much to indent a comment in C++ code
237 ;; based on its context.
238 (defun c++-comment-indent ()
239 (if (looking-at "^\\(/\\*\\|//\\)")
240 0 ; Existing comment at bol stays there.
242 (skip-chars-backward " \t")
244 ;; leave at least one space on non-empty lines.
245 (if (zerop (current-column)) 0 (1+ (current-column)))
246 (let ((cur-pt (point)))
247 (beginning-of-line 0)
248 ;; If previous line had a comment, use it's indent
249 (if (re-search-forward comment-start-skip cur-pt t)
251 (goto-char (match-beginning 0))
253 comment-column)))))) ; otherwise indent at comment column.
255 (defun electric-c++-brace (arg)
256 "Insert character and correct line's indentation."
262 (skip-chars-backward " \t")
264 (if c-auto-newline (progn (c++-indent-line) (newline) t))))
266 (insert last-command-char)
271 ;; (newline) may have done auto-fill
272 (setq insertpos (- (point) 2))
275 (if insertpos (goto-char (1+ insertpos)))
279 (goto-char insertpos)
280 (self-insert-command (prefix-numeric-value arg)))
281 (self-insert-command (prefix-numeric-value arg)))))
283 (defun electric-c++-semi (arg)
284 "Insert character and correct line's indentation."
287 (electric-c++-terminator arg)
288 (self-insert-command (prefix-numeric-value arg))))
290 (defun electric-c++-terminator (arg)
291 "Insert character and correct line's indentation."
293 (let (insertpos (end (point)))
294 (if (and (not arg) (eolp)
297 (skip-chars-forward " \t")
298 (or (= (following-char) ?#)
299 ;; Colon is special only after a label, or
300 ;; case, or another colon.
301 ;; So quickly rule out most other uses of colon
302 ;; and do no indentation for them.
303 (and (eq last-command-char ?:)
304 (not (looking-at "case[ \t]"))
307 (skip-chars-forward " \t")
309 ;; Do re-indent double colons
315 (let ((pps (parse-partial-sexp (point) end)))
316 (or (nth 3 pps) (nth 4 pps) (nth 5 pps))))))))
318 (insert last-command-char)
321 (not (c-inside-parens-p))
323 ;; the new marker object, used to be just an integer
324 (setq insertpos (make-marker))
325 ;; changed setq to set-marker
326 (set-marker insertpos (1- (point)))
327 ;; do this before the newline, since in auto fill can break
331 (if insertpos (goto-char (1+ insertpos)))
335 (goto-char insertpos)
336 (self-insert-command (prefix-numeric-value arg)))
337 (self-insert-command (prefix-numeric-value arg)))))
339 (defun c++-indent-command (&optional whole-exp)
340 "Indent current line as C++ code, or in some cases insert a tab character.
341 If `c-tab-always-indent' is non-nil (the default), always indent current
342 line. Otherwise, indent the current line only if point is at the left
343 margin or in the line's indentation; otherwise insert a tab.
345 A numeric argument, regardless of its value, means indent rigidly all means
346 indent rigidly all the lines of the expression starting after point so that
347 this line becomes properly indented. The relative indentation among the
348 lines of the expression are preserved."
351 ;; If arg, always indent this line as C
352 ;; and shift remaining lines of expression the same amount.
353 (let ((shift-amt (c++-indent-line))
356 (if c-tab-always-indent
365 (indent-code-rigidly beg end shift-amt "#")))
366 (if (and (not c-tab-always-indent)
368 (skip-chars-backward " \t")
373 (defun c++-indent-line ()
374 "Indent current line as C++ code.
375 Return the amount the indentation changed by."
376 (let ((indent (calculate-c++-indent nil))
378 (case-fold-search nil)
379 (pos (- (point-max) (point))))
382 (cond ((eq indent nil)
383 (setq indent (current-indentation)))
385 (setq indent (calculate-c-indent-within-comment)))
386 ((looking-at "[ \t]*#")
389 (skip-chars-forward " \t")
390 (if (listp indent) (setq indent (car indent)))
391 (cond ((looking-at "\\(default\\|public\\|private\\|protected\\):")
392 (setq indent (+ indent c-label-offset)))
393 ((or (looking-at "case\\b")
394 (and (looking-at "[A-Za-z]")
397 (looking-at ":[^:]"))))
398 (setq indent (max 1 (+ indent c-label-offset))))
399 ((and (looking-at "else\\b")
400 (not (looking-at "else\\s_")))
401 (setq indent (save-excursion
402 (c-backward-to-start-of-if)
403 (current-indentation))))
404 ((looking-at "friend\[ \t]")
405 (setq indent (+ indent c++-friend-offset)))
406 ((= (following-char) ?})
407 (setq indent (- indent c-indent-level)))
408 ((= (following-char) ?{)
409 (setq indent (+ indent c-brace-offset))))))
410 (skip-chars-forward " \t")
411 (setq shift-amt (- indent (current-column)))
412 (if (zerop shift-amt)
413 (if (> (- (point-max) pos) (point))
414 (goto-char (- (point-max) pos)))
415 (delete-region beg (point))
417 ;; If initial point was within line's indentation,
418 ;; position after the indentation. Else stay at same point in text.
419 (if (> (- (point-max) pos) (point))
420 (goto-char (- (point-max) pos))))
423 (defun calculate-c++-indent (&optional parse-start)
424 "Return appropriate indentation for current line as C++ code.
425 In usual case returns an integer: the column to indent to.
426 Returns nil if line starts inside a string, t if in a comment."
429 (let ((indent-point (point))
430 (case-fold-search nil)
434 (goto-char parse-start)
435 (beginning-of-defun))
436 (while (< (point) indent-point)
437 (setq parse-start (point))
438 (setq state (parse-partial-sexp (point) indent-point 0))
439 (setq containing-sexp (car (cdr state))))
440 (cond ((or (nth 3 state) (nth 4 state))
441 ;; return nil or t if should not change this line
443 ((null containing-sexp)
444 ;; Line is at top level. May be data or function definition, or
445 ;; may be function argument declaration or member initialization.
446 ;; Indent like the previous top level line unless
447 ;; (1) the previous line ends in a closeparen without semicolon,
448 ;; in which case this line is the first argument declaration or
449 ;; member initialization, or
450 ;; (2) the previous line begins with a colon,
451 ;; in which case this is the second line of member inits.
452 ;; It is assumed that arg decls and member inits are not mixed.
453 (goto-char indent-point)
454 (skip-chars-forward " \t")
455 (if (= (following-char) ?{)
456 0 ; Unless it starts a function body
457 (c++-backward-to-noncomment (or parse-start (point-min)))
458 (if (= (preceding-char) ?\))
459 (progn ; first arg decl or member init
460 (goto-char indent-point)
461 (skip-chars-forward " \t")
462 (if (= (following-char) ?:)
463 c++-member-init-indent
465 (if (= (preceding-char) ?\;)
467 (if (= (preceding-char) ?})
469 (beginning-of-line) ; continued arg decls or member inits
470 (skip-chars-forward " \t")
471 (if (= (following-char) ?:)
472 (if c++-continued-member-init-offset
473 (+ (current-indentation)
474 c++-continued-member-init-offset)
477 (skip-chars-forward " \t")
479 (current-indentation)))
481 ((/= (char-after containing-sexp) ?{)
482 ;; line is expression, not statement:
483 ;; indent to just after the surrounding open -- unless
484 ;; empty arg list, in which case we do what
485 ;; c++-empty-arglist-indent says to do.
486 (if (and c++-empty-arglist-indent
487 (or (null (nth 2 state)) ;; indicates empty arg
489 ;; Use a heuristic: if the first
490 ;; non-whitespace following left paren on
491 ;; same line is not a comment,
492 ;; is not an empty arglist.
494 (goto-char (1+ containing-sexp))
496 (looking-at "\\( \\|\t\\)*[^/\n]")))))
498 (goto-char containing-sexp)
500 (skip-chars-forward " \t")
501 (goto-char (min (+ (point) c++-empty-arglist-indent)
502 (1+ containing-sexp)))
504 ;; In C-mode, we would always indent to one after the
505 ;; left paren. Here, though, we may have an
506 ;; empty-arglist, so we'll indent to the min of that
507 ;; and the beginning of the first argument.
508 (goto-char (1+ containing-sexp))
511 ;; Statement. Find previous non-comment character.
512 (goto-char indent-point)
513 (c++-backward-to-noncomment containing-sexp)
514 (if (not (memq (preceding-char) '(nil ?\, ?\; ?} ?: ?\{)))
515 ;; This line is continuation of preceding line's statement;
516 ;; indent c-continued-statement-offset more than the
517 ;; previous line of the statement.
519 (c-backward-to-start-of-continued-exp containing-sexp)
520 (+ c-continued-statement-offset (current-column)
521 (if (save-excursion (goto-char indent-point)
522 (skip-chars-forward " \t")
523 (eq (following-char) ?{))
524 c-continued-brace-offset 0)))
525 ;; This line starts a new statement.
526 ;; Position following last unclosed open.
527 (goto-char containing-sexp)
528 ;; Is line first statement after an open-brace?
530 ;; If no, find that first statement and indent like it.
533 (let ((colon-line-end 0))
534 (while (progn (skip-chars-forward " \t\n")
539 "\\|[a-zA-Z0-9_$]*:[^:]"
541 ;; Skip over comments and labels following openbrace.
542 (cond ((= (following-char) ?\#)
545 (search-forward "*/" nil 'move))
546 ((looking-at "//\\|friend[ \t]")
549 (save-excursion (end-of-line)
550 (setq colon-line-end (point)))
551 (search-forward ":"))))
552 ;; The first following code counts
553 ;; if it is before the line we want to indent.
554 (and (< (point) indent-point)
556 (if (> colon-line-end (point))
557 (- (current-indentation) c-label-offset)
559 ;; If prev stmt starts with open-brace, that
560 ;; open brace was offset by c-brace-offset.
561 ;; Compensate to get the column where
562 ;; an ordinary statement would start.
563 (if (= (following-char) ?\{) c-brace-offset 0)))))
564 ;; If no previous statement,
565 ;; indent it relative to line brace is on.
566 ;; For open brace in column zero, don't let statement
567 ;; start there too. If c-indent-offset is zero,
568 ;; use c-brace-offset + c-continued-statement-offset instead.
569 ;; For open-braces not the first thing in a line,
570 ;; add in c-brace-imaginary-offset.
571 (+ (if (and (bolp) (zerop c-indent-level))
572 (+ c-brace-offset c-continued-statement-offset)
574 ;; Move back over whitespace before the openbrace.
575 ;; If openbrace is not first nonwhite thing on the line,
576 ;; add the c-brace-imaginary-offset.
577 (progn (skip-chars-backward " \t")
578 (if (bolp) 0 c-brace-imaginary-offset))
579 ;; If the openbrace is preceded by a parenthesized exp,
580 ;; move to the beginning of that;
581 ;; possibly a different line
583 (if (eq (preceding-char) ?\))
585 ;; Get initial indentation of the line we are on.
586 (current-indentation))))))))))
588 (defun c++-backward-to-noncomment (lim)
591 (skip-chars-backward " \t\n\r\f" lim)
592 (setq opoint (point))
593 (cond ((and (>= (point) (+ 2 lim))
596 (looking-at "\\*/")))
597 (search-backward "/*" lim 'move))
599 (search-backward "//" (max (c++-point-bol) lim) 'move)
600 (not (c++-within-string-p (point) opoint))))
601 ;; No comment to be found.
602 ;; If there's a # command on this line,
604 (t (beginning-of-line)
605 (skip-chars-forward " \t")
606 ;; But don't get fooled if we are already before the #.
607 (if (and (looking-at "#") (< (point) opoint))
608 (setq stop (<= (point) lim))
610 (goto-char opoint)))))))
612 (defun indent-c++-exp ()
613 "Indent each line of the C++ grouping following point."
615 (let ((indent-stack (list nil))
616 (contain-stack (list (point)))
617 (case-fold-search nil)
618 restart outer-loop-done inner-loop-done state ostate
619 this-indent last-sexp last-depth
626 (setq outer-loop-done nil)
627 (while (and (not (eobp)) (not outer-loop-done))
628 (setq last-depth next-depth)
629 ;; Compute how depth changes over this line
630 ;; plus enough other lines to get to one that
631 ;; does not end inside a comment or string.
632 ;; Meanwhile, do appropriate indentation on comment lines.
633 (setq inner-loop-done nil)
634 (while (and (not inner-loop-done)
635 (not (and (eobp) (setq outer-loop-done t))))
637 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
639 (setq next-depth (car state))
640 (if (and (car (cdr (cdr state)))
641 (>= (car (cdr (cdr state))) 0))
642 (setq last-sexp (car (cdr (cdr state)))))
643 (if (or (nth 4 ostate))
645 (if (or (nth 3 state))
647 (setq inner-loop-done t)))
648 (if (<= next-depth 0)
649 (setq outer-loop-done t))
652 ;; If this line had ..))) (((.. in it, pop out of the levels
653 ;; that ended anywhere in this line, even if the final depth
654 ;; doesn't indicate that they ended.
655 (while (> last-depth (nth 6 state))
656 (setq indent-stack (cdr indent-stack)
657 contain-stack (cdr contain-stack)
658 last-depth (1- last-depth)))
659 (if (/= last-depth next-depth)
660 (setq last-sexp nil))
661 ;; Add levels for any parens that were started in this line.
662 (while (< last-depth next-depth)
663 (setq indent-stack (cons nil indent-stack)
664 contain-stack (cons nil contain-stack)
665 last-depth (1+ last-depth)))
666 (if (null (car contain-stack))
667 (setcar contain-stack (or (car (cdr state))
668 (save-excursion (forward-sexp -1)
671 (skip-chars-forward " \t")
674 (if (and (car indent-stack)
675 (>= (car indent-stack) 0))
676 ;; Line is on an existing nesting level.
677 ;; Lines inside parens are handled specially.
679 ;; Just started a new nesting level.
680 ;; Compute the standard indent for this level.
682 (if (= (char-after (car contain-stack)) ?{)
684 (goto-char (car contain-stack))
685 (setq val (+ c-indent-level (current-column))))
686 (setq val (calculate-c++-indent
687 (if (car indent-stack)
688 (- (car indent-stack))))))
689 (setcar indent-stack val)))
690 ;; Adjust line indentation according to its predecessor.
691 (if (/= (char-after (car contain-stack)) ?\{)
692 (setq this-indent (car indent-stack))
693 ;; Line is at statement level.
694 ;; Is it a new statement? Is it an else?
695 ;; Find last non-comment character before this line
697 (setq at-else (looking-at "else\\W"))
698 (setq at-brace (= (following-char) ?\{))
699 (c++-backward-to-noncomment opoint)
700 (if (not (memq (preceding-char) '(nil ?\, ?\; ?\} ?: ?\{)))
701 ;; Preceding line did not end in comma or semi;
702 ;; indent this line c-continued-statement-offset
703 ;; more than previous.
705 (c-backward-to-start-of-continued-exp
708 (+ c-continued-statement-offset
710 (if at-brace c-continued-brace-offset 0))))
711 ;; Preceding line ended in comma or semi;
712 ;; use the standard indent for this level.
714 (progn (c-backward-to-start-of-if opoint)
715 (setq this-indent (current-indentation)))
716 (setq this-indent (car indent-stack))))))
717 ;; Adjust line indentation according to its contents
718 (if (looking-at "\\(public\\|private\\|protected\\):")
719 (setq this-indent (- this-indent c-indent-level))
720 (if (or (looking-at "case[ \t]")
721 (and (looking-at "[A-Za-z]")
724 (looking-at ":[^:]"))))
725 (setq this-indent (max 1 (+ this-indent c-label-offset)))))
726 (if (looking-at "friend[ \t]")
727 (setq this-indent (+ this-indent c++-friend-offset)))
728 (if (= (following-char) ?\})
729 (setq this-indent (- this-indent c-indent-level)))
730 (if (= (following-char) ?\{)
731 (setq this-indent (+ this-indent c-brace-offset)))
732 ;; Put chosen indentation into effect.
733 (or (= (current-column) this-indent)
734 (= (following-char) ?\#)
736 (delete-region (point) (progn (beginning-of-line) (point)))
737 (indent-to this-indent)))
738 ;; Indent any comment following the text.
739 (or (looking-at comment-start-skip)
740 (if (re-search-forward comment-start-skip
741 (save-excursion (end-of-line)
745 (beginning-of-line))))))))))
747 (defun fill-c++-comment ()
748 "Fill a comment contained in consecutive lines containing point.
749 The fill lines remain a comment."
752 (let ((save fill-prefix))
753 (beginning-of-line 1)
755 (re-search-forward comment-start-skip
756 (save-excursion (end-of-line) (point))
758 (goto-char (match-end 0))
760 (while (looking-at fill-prefix)
766 (setq fill-prefix save))))
768 (defun c++-point-bol ()
769 "Returns the value of the point at the beginning of the current line."
774 ;; (defun c++-insert-header ()
775 ;; "Insert header denoting C++ code at top of buffer."
778 ;; (goto-char (point-min))
780 ;; "This may look like C code, but it is really "
784 (defun c++-within-string-p (point1 point2)
785 "Returns true if number of double quotes between two points is odd."
786 (let ((s (buffer-substring point1 point2)))
787 (not (zerop (% (c++-count-char-in-string ?\" s) 2)))))
789 (defun c++-count-char-in-string (c s)
792 (while (< pos (length s))
793 (setq count (+ count (if (\= (aref s pos) c) 1 0)))
797 ;; rms: This page is creeping featurism, and not worth having.
799 ;;; Below are two regular expressions that attempt to match defuns
800 ;;; "strongly" and "weakly." The strong one almost reconstructs the
801 ;;; grammar of C++; the weak one just figures anything id or curly on
802 ;;; the left begins a defun. The constant "c++-match-header-strongly"
803 ;;; determines which to use; the default is the weak one.
805 ;; (defvar c++-match-header-strongly nil
806 ;; "*If nil, use `c++-defun-header-weak' to identify beginning of definitions.
807 ;; If non-nil, use `c++-defun-header-strong'.")
809 ;; (defvar c++-defun-header-strong-struct-equivs "\\(class\\|struct\\|enum\\)"
810 ;; "Regexp to match names of structure declaration blocks in C++.")
812 ;; (defconst c++-defun-header-strong
814 ;; (; valid identifiers
815 ;; ;; There's a real weirdness here -- if I switch the below
816 ;; (id "\\(\\w\\|_\\)+")
818 ;; ;; (id "\\(_\\|\\w\\)+")
819 ;; ;; things no longer work right. Try it and see!
821 ;; ; overloadable operators
823 ;; "[-+*/%^&|~!=<>]\\|[-+*/%^&|<>=!]=\\|<<=?\\|>>=?")
825 ;; "&&\\|||\\|\\+\\+\\|--\\|()\\|\\[\\]")
826 ;; (op-sym (concat "\\(" op-sym1 "\\|" op-sym2 "\\)"))
828 ;; (middle "[^\\*]*\\(\\*+[^/\\*][^\\*]*\\)*")
829 ;; (c-comment (concat "/\\*" middle "\\*+/"))
830 ;; (wh (concat "\\(\\s \\|\n\\|//.*$\\|" c-comment "\\)"))
831 ;; (wh-opt (concat wh "*"))
832 ;; (wh-nec (concat wh "+"))
833 ;; (oper (concat "\\(" "operator" "\\("
834 ;; wh-opt op-sym "\\|" wh-nec id "\\)" "\\)"))
835 ;; (dcl-list "([^():]*)")
836 ;; (func-name (concat "\\(" oper "\\|" id "::" id "\\|" id "\\)"))
839 ;; "\\(" wh-opt id "(.*\\()" wh-opt "," "\\)\\)*"
840 ;; wh-opt id "(.*)" wh-opt "{"
841 ;; "\\|" wh-opt "{\\)"))
842 ;; (type-name (concat
843 ;; "\\(" c++-defun-header-strong-struct-equivs wh-nec "\\)?"
845 ;; (type (concat "\\(const" wh-nec "\\)?"
846 ;; "\\(" type-name "\\|" type-name wh-opt "\\*+" "\\|"
847 ;; type-name wh-opt "&" "\\)"))
848 ;; (modifier "\\(inline\\|virtual\\|overload\\|auto\\|static\\)")
849 ;; (modifiers (concat "\\(" modifier wh-nec "\\)*"))
852 ;; (concat modifiers type wh-nec func-name wh-opt dcl-list wh-opt inits))
853 ;; (inherit (concat "\\(:" wh-opt "\\(public\\|private\\)?"
855 ;; (cs-header (concat
856 ;; c++-defun-header-strong-struct-equivs
857 ;; wh-nec id wh-opt inherit "?" wh-opt "{")))
858 ;; (concat "^\\(" func-header "\\|" cs-header "\\)"))
859 ;; "Strongly-defined regexp to match beginning of structure or function def.")
862 ;; ;; This part has to do with recognizing defuns.
864 ;; ;; The weak convention we will use is that a defun begins any time
865 ;; ;; there is a left curly brace, or some identifier on the left margin,
866 ;; ;; followed by a left curly somewhere on the line. (This will also
867 ;; ;; incorrectly match some continued strings, but this is after all
868 ;; ;; just a weak heuristic.) Suggestions for improvement (short of the
869 ;; ;; strong scheme shown above) are welcomed.
871 ;; (defconst c++-defun-header-weak "^{\\|^[_a-zA-Z].*{"
872 ;; "Weakly-defined regexp to match beginning of structure or function def.")
874 ;; (defun c++-beginning-of-defun (arg)
876 ;; (let ((c++-defun-header (if c++-match-header-strongly
877 ;; c++-defun-header-strong
878 ;; c++-defun-header-weak)))
879 ;; (cond ((or (= arg 0) (and (> arg 0) (bobp))) nil)
880 ;; ((and (not (looking-at c++-defun-header))
881 ;; (let ((curr-pos (point))
882 ;; (open-pos (if (search-forward "{" nil 'move)
885 ;; (if (re-search-backward c++-defun-header nil 'move)
886 ;; (match-beginning 0))))
887 ;; (if (and open-pos beg-pos
888 ;; (< beg-pos curr-pos)
889 ;; (> open-pos curr-pos))
891 ;; (goto-char beg-pos)
892 ;; (if (= arg 1) t nil));; Are we done?
893 ;; (goto-char curr-pos)
896 ;; (if (and (looking-at c++-defun-header) (not (bobp)))
897 ;; (forward-char (if (< arg 0) 1 -1)))
898 ;; (and (re-search-backward c++-defun-header nil 'move (or arg 1))
899 ;; (goto-char (match-beginning 0)))))))
902 ;; (defun c++-end-of-defun (arg)
904 ;; (let ((c++-defun-header (if c++-match-header-strongly
905 ;; c++-defun-header-strong
906 ;; c++-defun-header-weak)))
907 ;; (if (and (eobp) (> arg 0))
909 ;; (if (and (> arg 0) (looking-at c++-defun-header)) (forward-char 1))
910 ;; (let ((pos (point)))
911 ;; (c++-beginning-of-defun
913 ;; (- (- arg (if (eobp) 0 1)))
915 ;; (if (and (< arg 0) (bobp))
917 ;; (if (re-search-forward c++-defun-header nil 'move)
918 ;; (progn (forward-char -1)
920 ;; (beginning-of-line 2)))
921 ;; (if (and (= pos (point))
922 ;; (re-search-forward c++-defun-header nil 'move))
923 ;; (c++-end-of-defun 1))))
926 ;; (defun c++-indent-defun ()
927 ;; "Indents the current function definition, struct or class declaration."
929 ;; (let ((restore (point)))
930 ;; (c++-end-of-defun 1)
931 ;; (beginning-of-line 1)
932 ;; (let ((end (point)))
933 ;; (c++-beginning-of-defun 1)
934 ;; (while (<= (point) end)
937 ;; (beginning-of-line 1)))
938 ;; (goto-char restore)))
940 ;;; cplus-md.el ends here