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