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