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