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