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