Commit | Line | Data |
---|---|---|
c0274f38 | 1 | ;;; c-mode.el --- C code editing commands for Emacs |
732be465 | 2 | ;; Copyright (C) 1985, 86, 87, 92, 94, 95 Free Software Foundation, Inc. |
3a801d0c | 3 | |
e5167999 | 4 | ;; Maintainer: FSF |
e5167999 ER |
5 | ;; Keywords: c |
6 | ||
a17915dc ER |
7 | ;; This file is part of GNU Emacs. |
8 | ||
9 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
10 | ;; it under the terms of the GNU General Public License as published by | |
e5167999 | 11 | ;; the Free Software Foundation; either version 2, or (at your option) |
a17915dc ER |
12 | ;; any later version. |
13 | ||
14 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
15 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | ;; GNU General Public License for more details. | |
18 | ||
19 | ;; You should have received a copy of the GNU General Public License | |
20 | ;; along with GNU Emacs; see the file COPYING. If not, write to | |
21 | ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
22 | ||
e41b2db1 ER |
23 | ;;; Commentary: |
24 | ||
25 | ;; A smart editing mode for C code. It knows a lot about C syntax and tries | |
732be465 | 26 | ;; to position the cursor according to C layout conventions. You can |
e41b2db1 ER |
27 | ;; change the details of the layout style with option variables. Load it |
28 | ;; and do M-x describe-mode for details. | |
29 | ||
e5167999 | 30 | ;;; Code: |
a17915dc ER |
31 | |
32 | (defvar c-mode-abbrev-table nil | |
33 | "Abbrev table in use in C mode.") | |
34 | (define-abbrev-table 'c-mode-abbrev-table ()) | |
35 | ||
885020c5 | 36 | (defvar c-mode-map (make-sparse-keymap) |
a17915dc | 37 | "Keymap used in C mode.") |
885020c5 RS |
38 | |
39 | (define-key c-mode-map "{" 'electric-c-brace) | |
40 | (define-key c-mode-map "}" 'electric-c-brace) | |
41 | (define-key c-mode-map ";" 'electric-c-semi) | |
42 | (define-key c-mode-map "#" 'electric-c-sharp-sign) | |
43 | (define-key c-mode-map ":" 'electric-c-terminator) | |
44 | (define-key c-mode-map "\e\C-h" 'mark-c-function) | |
45 | (define-key c-mode-map "\e\C-q" 'indent-c-exp) | |
46 | (define-key c-mode-map "\ea" 'c-beginning-of-statement) | |
47 | (define-key c-mode-map "\ee" 'c-end-of-statement) | |
885020c5 RS |
48 | (define-key c-mode-map "\C-c\C-n" 'c-forward-conditional) |
49 | (define-key c-mode-map "\C-c\C-p" 'c-backward-conditional) | |
50 | (define-key c-mode-map "\C-c\C-u" 'c-up-conditional) | |
51 | (define-key c-mode-map "\177" 'backward-delete-char-untabify) | |
9a474b25 | 52 | (define-key c-mode-map "\t" 'c-indent-command) |
885020c5 RS |
53 | |
54 | (define-key c-mode-map [menu-bar] (make-sparse-keymap)) | |
55 | ||
c5c170d0 RS |
56 | ;; "C-mode" is not strictly the right punctuation--it should be "C |
57 | ;; mode"--but that would look like two menu items. "C-mode" is the | |
58 | ;; best alternative I can think of. | |
885020c5 | 59 | (define-key c-mode-map [menu-bar c] |
c5c170d0 | 60 | (cons "C-mode" (make-sparse-keymap "C-mode"))) |
885020c5 | 61 | |
1a2ac388 RS |
62 | (define-key c-mode-map [menu-bar c comment-region] |
63 | '("Comment Out Region" . comment-region)) | |
64 | (define-key c-mode-map [menu-bar c c-macro-expand] | |
65 | '("Macro Expand Region" . c-macro-expand)) | |
66 | (define-key c-mode-map [menu-bar c c-backslash-region] | |
67 | '("Backslashify" . c-backslash-region)) | |
885020c5 RS |
68 | (define-key c-mode-map [menu-bar c indent-exp] |
69 | '("Indent Expression" . indent-c-exp)) | |
70 | (define-key c-mode-map [menu-bar c indent-line] | |
71 | '("Indent Line" . c-indent-command)) | |
72 | (define-key c-mode-map [menu-bar c fill] | |
73 | '("Fill Comment Paragraph" . c-fill-paragraph)) | |
c814718b RS |
74 | (define-key c-mode-map [menu-bar c cpp-highlight-buffer] |
75 | '("Highlight Conditionals" . cpp-highlight-buffer)) | |
885020c5 RS |
76 | (define-key c-mode-map [menu-bar c up] |
77 | '("Up Conditional" . c-up-conditional)) | |
78 | (define-key c-mode-map [menu-bar c backward] | |
79 | '("Backward Conditional" . c-backward-conditional)) | |
80 | (define-key c-mode-map [menu-bar c forward] | |
81 | '("Forward Conditional" . c-forward-conditional)) | |
82 | (define-key c-mode-map [menu-bar c backward-stmt] | |
83 | '("Backward Statement" . c-beginning-of-statement)) | |
84 | (define-key c-mode-map [menu-bar c forward-stmt] | |
85 | '("Forward Statement" . c-end-of-statement)) | |
a17915dc | 86 | |
d73ecf45 SM |
87 | (put 'comment-region 'menu-enable 'mark-active) |
88 | (put 'c-macro-expand 'menu-enable 'mark-active) | |
c6b3c155 | 89 | (put 'c-backslash-region 'menu-enable 'mark-active) |
d73ecf45 | 90 | |
a17915dc ER |
91 | (autoload 'c-macro-expand "cmacexp" |
92 | "Display the result of expanding all C macros occurring in the region. | |
93 | The expansion is entirely correct because it uses the C preprocessor." | |
94 | t) | |
95 | ||
96 | (defvar c-mode-syntax-table nil | |
97 | "Syntax table in use in C-mode buffers.") | |
98 | ||
99 | (if c-mode-syntax-table | |
100 | () | |
101 | (setq c-mode-syntax-table (make-syntax-table)) | |
102 | (modify-syntax-entry ?\\ "\\" c-mode-syntax-table) | |
103 | (modify-syntax-entry ?/ ". 14" c-mode-syntax-table) | |
104 | (modify-syntax-entry ?* ". 23" c-mode-syntax-table) | |
105 | (modify-syntax-entry ?+ "." c-mode-syntax-table) | |
106 | (modify-syntax-entry ?- "." c-mode-syntax-table) | |
107 | (modify-syntax-entry ?= "." c-mode-syntax-table) | |
108 | (modify-syntax-entry ?% "." c-mode-syntax-table) | |
109 | (modify-syntax-entry ?< "." c-mode-syntax-table) | |
110 | (modify-syntax-entry ?> "." c-mode-syntax-table) | |
111 | (modify-syntax-entry ?& "." c-mode-syntax-table) | |
112 | (modify-syntax-entry ?| "." c-mode-syntax-table) | |
113 | (modify-syntax-entry ?\' "\"" c-mode-syntax-table)) | |
114 | ||
115 | (defconst c-indent-level 2 | |
116 | "*Indentation of C statements with respect to containing block.") | |
117 | (defconst c-brace-imaginary-offset 0 | |
118 | "*Imagined indentation of a C open brace that actually follows a statement.") | |
119 | (defconst c-brace-offset 0 | |
120 | "*Extra indentation for braces, compared with other text in same context.") | |
121 | (defconst c-argdecl-indent 5 | |
122 | "*Indentation level of declarations of C function arguments.") | |
123 | (defconst c-label-offset -2 | |
124 | "*Offset of C label lines and case statements relative to usual indentation.") | |
125 | (defconst c-continued-statement-offset 2 | |
126 | "*Extra indent for lines not starting new statements.") | |
127 | (defconst c-continued-brace-offset 0 | |
128 | "*Extra indent for substatements that start with open-braces. | |
e7e7be7c | 129 | This is in addition to `c-continued-statement-offset'.") |
a17915dc ER |
130 | (defconst c-style-alist |
131 | '(("GNU" | |
132 | (c-indent-level . 2) | |
133 | (c-argdecl-indent . 5) | |
134 | (c-brace-offset . 0) | |
02da8c82 | 135 | (c-continued-brace-offset . 0) |
a17915dc ER |
136 | (c-label-offset . -2) |
137 | (c-continued-statement-offset . 2)) | |
138 | ("K&R" | |
139 | (c-indent-level . 5) | |
140 | (c-argdecl-indent . 0) | |
02da8c82 KH |
141 | (c-brace-offset . 0) |
142 | (c-continued-brace-offset . -5) | |
a17915dc ER |
143 | (c-label-offset . -5) |
144 | (c-continued-statement-offset . 5)) | |
145 | ("BSD" | |
146 | (c-indent-level . 4) | |
147 | (c-argdecl-indent . 4) | |
02da8c82 | 148 | (c-brace-offset . 0) |
e7e7be7c | 149 | (c-continued-brace-offset . -4) |
a17915dc ER |
150 | (c-label-offset . -4) |
151 | (c-continued-statement-offset . 4)) | |
ce4b7b02 | 152 | ("C++" |
02da8c82 KH |
153 | (c-indent-level . 4) |
154 | (c-argdecl-indent . 0) | |
155 | (c-brace-offset . 0) | |
156 | (c-continued-brace-offset . -4) | |
2b529dd2 | 157 | (c-label-offset . -4) |
02da8c82 KH |
158 | (c-continued-statement-offset . 4) |
159 | (c-auto-newline . t)) | |
a17915dc ER |
160 | ("Whitesmith" |
161 | (c-indent-level . 4) | |
162 | (c-argdecl-indent . 4) | |
163 | (c-brace-offset . 0) | |
02da8c82 | 164 | (c-continued-brace-offset . 0) |
a17915dc ER |
165 | (c-label-offset . -4) |
166 | (c-continued-statement-offset . 4)))) | |
167 | ||
168 | (defconst c-auto-newline nil | |
169 | "*Non-nil means automatically newline before and after braces, | |
170 | and after colons and semicolons, inserted in C code. | |
171 | If you do not want a leading newline before braces then use: | |
5852d145 | 172 | (define-key c-mode-map \"{\" 'electric-c-semi)") |
a17915dc ER |
173 | |
174 | (defconst c-tab-always-indent t | |
175 | "*Non-nil means TAB in C mode should always reindent the current line, | |
176 | regardless of where in the line point is when the TAB command is used.") | |
3a65941e JB |
177 | |
178 | ;;; Regular expression used internally to recognize labels in switch | |
179 | ;;; statements. | |
0ce62eb2 | 180 | (defconst c-switch-label-regexp "case[ \t'/(]\\|default[ \t]*:") |
3a65941e | 181 | |
a17915dc | 182 | \f |
a17915dc ER |
183 | (defun c-mode () |
184 | "Major mode for editing C code. | |
185 | Expression and list commands understand all C brackets. | |
186 | Tab indents for C code. | |
187 | Comments are delimited with /* ... */. | |
188 | Paragraphs are separated by blank lines only. | |
189 | Delete converts tabs to spaces as it moves back. | |
190 | \\{c-mode-map} | |
191 | Variables controlling indentation style: | |
192 | c-tab-always-indent | |
193 | Non-nil means TAB in C mode should always reindent the current line, | |
194 | regardless of where in the line point is when the TAB command is used. | |
195 | c-auto-newline | |
196 | Non-nil means automatically newline before and after braces, | |
197 | and after colons and semicolons, inserted in C code. | |
198 | c-indent-level | |
199 | Indentation of C statements within surrounding block. | |
200 | The surrounding block's indentation is the indentation | |
201 | of the line on which the open-brace appears. | |
202 | c-continued-statement-offset | |
203 | Extra indentation given to a substatement, such as the | |
204 | then-clause of an if or body of a while. | |
205 | c-continued-brace-offset | |
206 | Extra indentation given to a brace that starts a substatement. | |
207 | This is in addition to c-continued-statement-offset. | |
208 | c-brace-offset | |
209 | Extra indentation for line if it starts with an open brace. | |
210 | c-brace-imaginary-offset | |
211 | An open brace following other text is treated as if it were | |
212 | this far to the right of the start of its line. | |
213 | c-argdecl-indent | |
214 | Indentation level of declarations of C function arguments. | |
215 | c-label-offset | |
216 | Extra indentation for line that is a label, or case or default. | |
217 | ||
218 | Settings for K&R and BSD indentation styles are | |
219 | c-indent-level 5 8 | |
220 | c-continued-statement-offset 5 8 | |
221 | c-brace-offset -5 -8 | |
222 | c-argdecl-indent 0 8 | |
223 | c-label-offset -5 -8 | |
224 | ||
225 | Turning on C mode calls the value of the variable c-mode-hook with no args, | |
226 | if that value is non-nil." | |
227 | (interactive) | |
228 | (kill-all-local-variables) | |
229 | (use-local-map c-mode-map) | |
230 | (setq major-mode 'c-mode) | |
231 | (setq mode-name "C") | |
232 | (setq local-abbrev-table c-mode-abbrev-table) | |
233 | (set-syntax-table c-mode-syntax-table) | |
234 | (make-local-variable 'paragraph-start) | |
52af0e47 | 235 | (setq paragraph-start (concat "$\\|" page-delimiter)) |
a17915dc ER |
236 | (make-local-variable 'paragraph-separate) |
237 | (setq paragraph-separate paragraph-start) | |
238 | (make-local-variable 'paragraph-ignore-fill-prefix) | |
239 | (setq paragraph-ignore-fill-prefix t) | |
fcf9745f RS |
240 | (make-local-variable 'fill-paragraph-function) |
241 | (setq fill-paragraph-function 'c-fill-paragraph) | |
a17915dc ER |
242 | (make-local-variable 'indent-line-function) |
243 | (setq indent-line-function 'c-indent-line) | |
244 | (make-local-variable 'indent-region-function) | |
245 | (setq indent-region-function 'c-indent-region) | |
246 | (make-local-variable 'require-final-newline) | |
247 | (setq require-final-newline t) | |
bd133320 RS |
248 | (make-local-variable 'outline-regexp) |
249 | (setq outline-regexp "[^#\n\^M]") | |
250 | (make-local-variable 'outline-level) | |
251 | (setq outline-level 'c-outline-level) | |
a17915dc ER |
252 | (make-local-variable 'comment-start) |
253 | (setq comment-start "/* ") | |
254 | (make-local-variable 'comment-end) | |
255 | (setq comment-end " */") | |
256 | (make-local-variable 'comment-column) | |
257 | (setq comment-column 32) | |
258 | (make-local-variable 'comment-start-skip) | |
259 | (setq comment-start-skip "/\\*+ *") | |
e41b2db1 ER |
260 | (make-local-variable 'comment-indent-function) |
261 | (setq comment-indent-function 'c-comment-indent) | |
752e8dd9 RS |
262 | (make-local-variable 'comment-multi-line) |
263 | (setq comment-multi-line t) | |
a17915dc ER |
264 | (make-local-variable 'parse-sexp-ignore-comments) |
265 | (setq parse-sexp-ignore-comments t) | |
266 | (run-hooks 'c-mode-hook)) | |
bd133320 RS |
267 | |
268 | (defun c-outline-level () | |
269 | (save-excursion | |
270 | (skip-chars-forward "\t ") | |
271 | (current-column))) | |
a17915dc ER |
272 | \f |
273 | ;; This is used by indent-for-comment | |
274 | ;; to decide how much to indent a comment in C code | |
275 | ;; based on its context. | |
276 | (defun c-comment-indent () | |
277 | (if (looking-at "^/\\*") | |
278 | 0 ;Existing comment at bol stays there. | |
279 | (let ((opoint (point))) | |
280 | (save-excursion | |
281 | (beginning-of-line) | |
282 | (cond ((looking-at "[ \t]*}[ \t]*\\($\\|/\\*\\)") | |
283 | ;; A comment following a solitary close-brace | |
284 | ;; should have only one space. | |
285 | (search-forward "}") | |
286 | (1+ (current-column))) | |
287 | ((or (looking-at "^#[ \t]*endif[ \t]*") | |
288 | (looking-at "^#[ \t]*else[ \t]*")) | |
289 | 7) ;2 spaces after #endif | |
290 | ((progn | |
291 | (goto-char opoint) | |
292 | (skip-chars-backward " \t") | |
293 | (and (= comment-column 0) (bolp))) | |
294 | ;; If comment-column is 0, and nothing but space | |
295 | ;; before the comment, align it at 0 rather than 1. | |
296 | 0) | |
297 | (t | |
298 | (max (1+ (current-column)) ;Else indent at comment column | |
299 | comment-column))))))) ; except leave at least one space. | |
300 | ||
301 | (defun c-fill-paragraph (&optional arg) | |
302 | "Like \\[fill-paragraph] but handle C comments. | |
c9c56492 RS |
303 | If any of the current line is a comment or within a comment, |
304 | fill the comment or the paragraph of it that point is in, | |
305 | preserving the comment indentation or line-starting decorations." | |
a17915dc | 306 | (interactive "P") |
6cf42072 RS |
307 | (let* (comment-start-place |
308 | (first-line | |
309 | ;; Check for obvious entry to comment. | |
310 | (save-excursion | |
311 | (beginning-of-line) | |
8931ecc0 | 312 | (skip-chars-forward " \t\n") |
6cf42072 RS |
313 | (and (looking-at comment-start-skip) |
314 | (setq comment-start-place (point)))))) | |
0fef40c0 RS |
315 | (if (and (eq major-mode 'c++-mode) |
316 | (save-excursion | |
317 | (beginning-of-line) | |
318 | (looking-at ".*//"))) | |
319 | (let (fill-prefix | |
a17915dc ER |
320 | (paragraph-start |
321 | ;; Lines containing just a comment start or just an end | |
322 | ;; should not be filled into paragraphs they are next to. | |
dbc4e1c1 JB |
323 | (concat |
324 | paragraph-start | |
52af0e47 | 325 | "\\|[ \t]*/\\*[ \t]*$\\|[ \t]*\\*/[ \t]*$\\|[ \t/*]*$")) |
a17915dc | 326 | (paragraph-separate |
dbc4e1c1 JB |
327 | (concat |
328 | paragraph-separate | |
52af0e47 | 329 | "\\|[ \t]*/\\*[ \t]*$\\|[ \t]*\\*/[ \t]*$\\|[ \t/*]*$"))) |
0fef40c0 RS |
330 | (save-excursion |
331 | (beginning-of-line) | |
332 | ;; Move up to first line of this comment. | |
333 | (while (and (not (bobp)) (looking-at "[ \t]*//")) | |
334 | (forward-line -1)) | |
335 | (if (not (looking-at ".*//")) | |
336 | (forward-line 1)) | |
337 | ;; Find the comment start in this line. | |
338 | (re-search-forward "[ \t]*//[ \t]*") | |
339 | ;; Set the fill-prefix to be what all lines except the first | |
340 | ;; should start with. | |
341 | (let ((endcol (current-column))) | |
342 | (skip-chars-backward " \t") | |
343 | (setq fill-prefix | |
344 | (concat (make-string (- (current-column) 2) ?\ ) | |
345 | "//" | |
346 | (make-string (- endcol (current-column)) ?\ )))) | |
347 | (save-restriction | |
348 | ;; Narrow down to just the lines of this comment. | |
349 | (narrow-to-region (point) | |
350 | (save-excursion | |
351 | (forward-line 1) | |
c9a73a9b | 352 | (while (looking-at "[ \t]*//") |
0fef40c0 RS |
353 | (forward-line 1)) |
354 | (point))) | |
355 | (insert fill-prefix) | |
356 | (fill-paragraph arg) | |
357 | (delete-region (point-min) | |
358 | (+ (point-min) (length fill-prefix)))))) | |
359 | (if (or first-line | |
360 | ;; t if we enter a comment between start of function and this line. | |
361 | (eq (calculate-c-indent) t) | |
362 | ;; t if this line contains a comment starter. | |
363 | (setq first-line | |
364 | (save-excursion | |
365 | (beginning-of-line) | |
366 | (prog1 | |
367 | (re-search-forward comment-start-skip | |
368 | (save-excursion (end-of-line) | |
369 | (point)) | |
370 | t) | |
371 | (setq comment-start-place (point)))))) | |
372 | ;; Inside a comment: fill one comment paragraph. | |
373 | (let ((fill-prefix | |
374 | ;; The prefix for each line of this paragraph | |
375 | ;; is the appropriate part of the start of this line, | |
376 | ;; up to the column at which text should be indented. | |
377 | (save-excursion | |
378 | (beginning-of-line) | |
379 | (if (looking-at "[ \t]*/\\*.*\\*/") | |
380 | (progn (re-search-forward comment-start-skip) | |
381 | (make-string (current-column) ?\ )) | |
382 | (if first-line (forward-line 1)) | |
383 | ||
384 | (let ((line-width (progn (end-of-line) (current-column)))) | |
385 | (beginning-of-line) | |
386 | (prog1 | |
387 | (buffer-substring | |
388 | (point) | |
389 | ||
390 | ;; How shall we decide where the end of the | |
391 | ;; fill-prefix is? | |
392 | ;; calculate-c-indent-within-comment bases its value | |
393 | ;; on the indentation of previous lines; if they're | |
394 | ;; indented specially, it could return a column | |
395 | ;; that's well into the current line's text. So | |
396 | ;; we'll take at most that many space, tab, or * | |
397 | ;; characters, and use that as our fill prefix. | |
398 | (let ((max-prefix-end | |
399 | (progn | |
400 | (move-to-column | |
401 | (calculate-c-indent-within-comment t) | |
402 | t) | |
403 | (point)))) | |
404 | (beginning-of-line) | |
405 | (skip-chars-forward " \t*" max-prefix-end) | |
84f4e216 RS |
406 | ;; Don't include part of comment terminator |
407 | ;; in the fill-prefix. | |
408 | (and (eq (following-char) ?/) | |
409 | (eq (preceding-char) ?*) | |
410 | (backward-char 1)) | |
0fef40c0 RS |
411 | (point))) |
412 | ||
413 | ;; If the comment is only one line followed by a blank | |
414 | ;; line, calling move-to-column above may have added | |
415 | ;; some spaces and tabs to the end of the line; the | |
416 | ;; fill-paragraph function will then delete it and the | |
417 | ;; newline following it, so we'll lose a blank line | |
418 | ;; when we shouldn't. So delete anything | |
419 | ;; move-to-column added to the end of the line. We | |
420 | ;; record the line width instead of the position of the | |
421 | ;; old line end because move-to-column might break a | |
422 | ;; tab into spaces, and the new characters introduced | |
423 | ;; there shouldn't be deleted. | |
424 | ||
425 | ;; If you can see a better way to do this, please make | |
426 | ;; the change. This seems very messy to me. | |
427 | (delete-region (progn (move-to-column line-width) | |
428 | (point)) | |
429 | (progn (end-of-line) (point)))))))) | |
430 | ||
431 | (paragraph-start | |
432 | ;; Lines containing just a comment start or just an end | |
433 | ;; should not be filled into paragraphs they are next to. | |
434 | (concat | |
435 | paragraph-start | |
52af0e47 | 436 | "\\|[ \t]*/\\*[ \t]*$\\|[ \t]*\\*/[ \t]*$\\|[ \t/*]*$")) |
0fef40c0 RS |
437 | (paragraph-separate |
438 | (concat | |
439 | paragraph-separate | |
52af0e47 | 440 | "\\|[ \t]*/\\*[ \t]*$\\|[ \t]*\\*/[ \t]*$\\|[ \t/*]*$")) |
0fef40c0 RS |
441 | (chars-to-delete 0)) |
442 | (save-restriction | |
443 | ;; Don't fill the comment together with the code following it. | |
444 | ;; So temporarily exclude everything before the comment start, | |
445 | ;; and everything after the line where the comment ends. | |
446 | ;; If comment-start-place is non-nil, the comment starter is there. | |
447 | ;; Otherwise, point is inside the comment. | |
448 | (narrow-to-region (save-excursion | |
449 | (if comment-start-place | |
450 | (goto-char comment-start-place) | |
451 | (search-backward "/*")) | |
452 | ;; Protect text before the comment start | |
453 | ;; by excluding it. Add spaces to bring back | |
454 | ;; proper indentation of that point. | |
455 | (let ((column (current-column))) | |
456 | (prog1 (point) | |
457 | (setq chars-to-delete column) | |
458 | (insert-char ?\ column)))) | |
459 | (save-excursion | |
460 | (if comment-start-place | |
461 | (goto-char (+ comment-start-place 2))) | |
462 | (search-forward "*/" nil 'move) | |
463 | (forward-line 1) | |
464 | (point))) | |
58ac467e KH |
465 | (save-excursion |
466 | (goto-char (point-max)) | |
467 | (forward-line -1) | |
468 | ;; And comment terminator was on a separate line before, | |
469 | ;; keep it that way. | |
470 | ;; This also avoids another problem: | |
471 | ;; if the fill-prefix ends in a *, it could eat up | |
472 | ;; the * of the comment terminator. | |
473 | (if (looking-at "[ \t]*\\*/") | |
474 | (narrow-to-region (point-min) (point)))) | |
0fef40c0 RS |
475 | (fill-paragraph arg) |
476 | (save-excursion | |
477 | ;; Delete the chars we inserted to avoid clobbering | |
478 | ;; the stuff before the comment start. | |
479 | (goto-char (point-min)) | |
480 | (if (> chars-to-delete 0) | |
481 | (delete-region (point) (+ (point) chars-to-delete))) | |
482 | ;; Find the comment ender (should be on last line of buffer, | |
483 | ;; given the narrowing) and don't leave it on its own line. | |
176afd04 KH |
484 | ;; Do this with a fill command, so as to preserve sentence |
485 | ;; boundaries. | |
0fef40c0 RS |
486 | (goto-char (point-max)) |
487 | (forward-line -1) | |
488 | (search-forward "*/" nil 'move) | |
489 | (beginning-of-line) | |
490 | (if (looking-at "[ \t]*\\*/") | |
176afd04 KH |
491 | (let ((fill-column (+ fill-column 9999))) |
492 | (forward-line -1) | |
493 | (fill-region-as-paragraph (point) (point-max))))))) | |
0fef40c0 | 494 | ;; Outside of comments: do ordinary filling. |
fcf9745f RS |
495 | (fill-paragraph arg))) |
496 | t)) | |
a17915dc ER |
497 | |
498 | (defun electric-c-brace (arg) | |
499 | "Insert character and correct line's indentation." | |
500 | (interactive "P") | |
501 | (let (insertpos) | |
502 | (if (and (not arg) | |
503 | (eolp) | |
504 | (or (save-excursion | |
505 | (skip-chars-backward " \t") | |
506 | (bolp)) | |
507 | (if c-auto-newline (progn (c-indent-line) (newline) t) nil))) | |
508 | (progn | |
509 | (insert last-command-char) | |
510 | (c-indent-line) | |
511 | (if c-auto-newline | |
512 | (progn | |
513 | (newline) | |
514 | ;; (newline) may have done auto-fill | |
515 | (setq insertpos (- (point) 2)) | |
516 | (c-indent-line))) | |
517 | (save-excursion | |
518 | (if insertpos (goto-char (1+ insertpos))) | |
519 | (delete-char -1)))) | |
520 | (if insertpos | |
521 | (save-excursion | |
522 | (goto-char insertpos) | |
523 | (self-insert-command (prefix-numeric-value arg))) | |
524 | (self-insert-command (prefix-numeric-value arg))))) | |
525 | ||
526 | (defun electric-c-sharp-sign (arg) | |
527 | "Insert character and correct line's indentation." | |
528 | (interactive "P") | |
529 | (if (save-excursion | |
530 | (skip-chars-backward " \t") | |
531 | (bolp)) | |
532 | (let ((c-auto-newline nil)) | |
533 | (electric-c-terminator arg)) | |
534 | (self-insert-command (prefix-numeric-value arg)))) | |
535 | ||
536 | (defun electric-c-semi (arg) | |
537 | "Insert character and correct line's indentation." | |
538 | (interactive "P") | |
539 | (if c-auto-newline | |
540 | (electric-c-terminator arg) | |
541 | (self-insert-command (prefix-numeric-value arg)))) | |
542 | ||
543 | (defun electric-c-terminator (arg) | |
544 | "Insert character and correct line's indentation." | |
545 | (interactive "P") | |
546 | (let (insertpos (end (point))) | |
547 | (if (and (not arg) (eolp) | |
548 | (not (save-excursion | |
549 | (beginning-of-line) | |
550 | (skip-chars-forward " \t") | |
551 | (or (= (following-char) ?#) | |
552 | ;; Colon is special only after a label, or case .... | |
553 | ;; So quickly rule out most other uses of colon | |
554 | ;; and do no indentation for them. | |
555 | (and (eq last-command-char ?:) | |
3a65941e | 556 | (not (looking-at c-switch-label-regexp)) |
a17915dc ER |
557 | (save-excursion |
558 | (skip-chars-forward "a-zA-Z0-9_$") | |
559 | (skip-chars-forward " \t") | |
560 | (< (point) end))) | |
561 | (progn | |
562 | (beginning-of-defun) | |
563 | (let ((pps (parse-partial-sexp (point) end))) | |
564 | (or (nth 3 pps) (nth 4 pps) (nth 5 pps)))))))) | |
565 | (progn | |
566 | (insert last-command-char) | |
567 | (c-indent-line) | |
568 | (and c-auto-newline | |
569 | (not (c-inside-parens-p)) | |
570 | (progn | |
571 | (newline) | |
572 | ;; (newline) may have done auto-fill | |
573 | (setq insertpos (- (point) 2)) | |
574 | (c-indent-line))) | |
575 | (save-excursion | |
576 | (if insertpos (goto-char (1+ insertpos))) | |
577 | (delete-char -1)))) | |
578 | (if insertpos | |
579 | (save-excursion | |
580 | (goto-char insertpos) | |
581 | (self-insert-command (prefix-numeric-value arg))) | |
582 | (self-insert-command (prefix-numeric-value arg))))) | |
583 | ||
584 | (defun c-inside-parens-p () | |
585 | (condition-case () | |
586 | (save-excursion | |
587 | (save-restriction | |
588 | (narrow-to-region (point) | |
589 | (progn (beginning-of-defun) (point))) | |
590 | (goto-char (point-max)) | |
591 | (= (char-after (or (scan-lists (point) -1 1) (point-min))) ?\())) | |
592 | (error nil))) | |
593 | \f | |
594 | (defun c-indent-command (&optional whole-exp) | |
595 | "Indent current line as C code, or in some cases insert a tab character. | |
596 | If `c-tab-always-indent' is non-nil (the default), always indent current line. | |
597 | Otherwise, indent the current line only if point is at the left margin or | |
598 | in the line's indentation; otherwise insert a tab. | |
599 | ||
600 | A numeric argument, regardless of its value, means indent rigidly all the | |
601 | lines of the expression starting after point so that this line becomes | |
602 | properly indented. The relative indentation among the lines of the | |
603 | expression are preserved." | |
604 | (interactive "P") | |
605 | (if whole-exp | |
606 | ;; If arg, always indent this line as C | |
607 | ;; and shift remaining lines of expression the same amount. | |
608 | (let ((shift-amt (c-indent-line)) | |
609 | beg end) | |
610 | (save-excursion | |
611 | (if c-tab-always-indent | |
612 | (beginning-of-line)) | |
613 | ;; Find beginning of following line. | |
614 | (save-excursion | |
615 | (forward-line 1) (setq beg (point))) | |
616 | ;; Find first beginning-of-sexp for sexp extending past this line. | |
617 | (while (< (point) beg) | |
618 | (forward-sexp 1) | |
619 | (setq end (point)) | |
620 | (skip-chars-forward " \t\n"))) | |
621 | (if (> end beg) | |
622 | (indent-code-rigidly beg end shift-amt "#"))) | |
623 | (if (and (not c-tab-always-indent) | |
624 | (save-excursion | |
625 | (skip-chars-backward " \t") | |
626 | (not (bolp)))) | |
627 | (insert-tab) | |
628 | (c-indent-line)))) | |
629 | ||
630 | (defun c-indent-line () | |
631 | "Indent current line as C code. | |
632 | Return the amount the indentation changed by." | |
633 | (let ((indent (calculate-c-indent nil)) | |
634 | beg shift-amt | |
635 | (case-fold-search nil) | |
636 | (pos (- (point-max) (point)))) | |
637 | (beginning-of-line) | |
638 | (setq beg (point)) | |
639 | (cond ((eq indent nil) | |
640 | (setq indent (current-indentation))) | |
641 | ((eq indent t) | |
642 | (setq indent (calculate-c-indent-within-comment))) | |
643 | ((looking-at "[ \t]*#") | |
644 | (setq indent 0)) | |
645 | (t | |
646 | (skip-chars-forward " \t") | |
647 | (if (listp indent) (setq indent (car indent))) | |
3a65941e | 648 | (cond ((or (looking-at c-switch-label-regexp) |
a17915dc ER |
649 | (and (looking-at "[A-Za-z]") |
650 | (save-excursion | |
651 | (forward-sexp 1) | |
652 | (looking-at ":")))) | |
653 | (setq indent (max 1 (+ indent c-label-offset)))) | |
654 | ((and (looking-at "else\\b") | |
655 | (not (looking-at "else\\s_"))) | |
656 | (setq indent (save-excursion | |
657 | (c-backward-to-start-of-if) | |
658 | (current-indentation)))) | |
401662cb RS |
659 | ((and (looking-at "}[ \t]*else\\b") |
660 | (not (looking-at "}[ \t]*else\\s_"))) | |
a17915dc ER |
661 | (setq indent (save-excursion |
662 | (forward-char) | |
663 | (backward-sexp) | |
eca5b04c | 664 | (c-backward-to-start-of-if) |
a17915dc ER |
665 | (current-indentation)))) |
666 | ((and (looking-at "while\\b") | |
401662cb | 667 | (not (looking-at "while\\s_")) |
a17915dc ER |
668 | (save-excursion |
669 | (c-backward-to-start-of-do))) | |
670 | ;; This is a `while' that ends a do-while. | |
671 | (setq indent (save-excursion | |
672 | (c-backward-to-start-of-do) | |
673 | (current-indentation)))) | |
674 | ((= (following-char) ?}) | |
675 | (setq indent (- indent c-indent-level))) | |
676 | ((= (following-char) ?{) | |
677 | (setq indent (+ indent c-brace-offset)))))) | |
678 | (skip-chars-forward " \t") | |
679 | (setq shift-amt (- indent (current-column))) | |
680 | (if (zerop shift-amt) | |
681 | (if (> (- (point-max) pos) (point)) | |
682 | (goto-char (- (point-max) pos))) | |
683 | (delete-region beg (point)) | |
684 | (indent-to indent) | |
685 | ;; If initial point was within line's indentation, | |
686 | ;; position after the indentation. Else stay at same point in text. | |
687 | (if (> (- (point-max) pos) (point)) | |
688 | (goto-char (- (point-max) pos)))) | |
689 | shift-amt)) | |
690 | ||
691 | (defun calculate-c-indent (&optional parse-start) | |
692 | "Return appropriate indentation for current line as C code. | |
693 | In usual case returns an integer: the column to indent to. | |
694 | Returns nil if line starts inside a string, t if in a comment." | |
695 | (save-excursion | |
696 | (beginning-of-line) | |
697 | (let ((indent-point (point)) | |
698 | (case-fold-search nil) | |
699 | state | |
700 | containing-sexp) | |
701 | (if parse-start | |
702 | (goto-char parse-start) | |
703 | (beginning-of-defun)) | |
704 | (while (< (point) indent-point) | |
705 | (setq parse-start (point)) | |
706 | (setq state (parse-partial-sexp (point) indent-point 0)) | |
707 | (setq containing-sexp (car (cdr state)))) | |
708 | (cond ((or (nth 3 state) (nth 4 state)) | |
709 | ;; return nil or t if should not change this line | |
710 | (nth 4 state)) | |
711 | ((null containing-sexp) | |
712 | ;; Line is at top level. May be data or function definition, | |
713 | ;; or may be function argument declaration. | |
714 | ;; Indent like the previous top level line | |
715 | ;; unless that ends in a closeparen without semicolon, | |
716 | ;; in which case this line is the first argument decl. | |
717 | (goto-char indent-point) | |
718 | (skip-chars-forward " \t") | |
719 | (if (= (following-char) ?{) | |
720 | 0 ; Unless it starts a function body | |
721 | (c-backward-to-noncomment (or parse-start (point-min))) | |
722 | ;; Look at previous line that's at column 0 | |
723 | ;; to determine whether we are in top-level decls | |
724 | ;; or function's arg decls. Set basic-indent accordingly. | |
725 | (let ((basic-indent | |
726 | (save-excursion | |
727 | (re-search-backward "^[^ \^L\t\n#]" nil 'move) | |
c763f396 | 728 | (let (comment lim) |
6ec3899e RS |
729 | ;; Recognize the DEFUN macro in Emacs. |
730 | (if (save-excursion | |
731 | ;; Move down to the (putative) argnames line. | |
35a4d143 RS |
732 | (while (and (not (eobp)) |
733 | (not (looking-at " *[({}#/]"))) | |
6ec3899e RS |
734 | (forward-line 1)) |
735 | ;; Go back to the DEFUN, if it is one. | |
736 | (condition-case nil | |
737 | (backward-sexp 1) | |
738 | (error)) | |
739 | (beginning-of-line) | |
6ec3899e RS |
740 | (looking-at "DEFUN\\b")) |
741 | c-argdecl-indent | |
742 | (if (and (looking-at "\\sw\\|\\s_") | |
791cc57d RS |
743 | ;; This is careful to stop at the first |
744 | ;; paren if we have | |
745 | ;; int foo Proto ((int, int)); | |
746 | (looking-at "[^\"\n=(]*(") | |
6ec3899e RS |
747 | (progn |
748 | (goto-char (1- (match-end 0))) | |
caeb79ef RS |
749 | ;; Skip any number of paren-groups. |
750 | ;; Consider typedef int (*fcn) (int); | |
751 | (while (= (following-char) ?\() | |
752 | (setq lim (point)) | |
753 | (condition-case nil | |
754 | (forward-sexp 1) | |
755 | (error)) | |
756 | (skip-chars-forward " \t\f")) | |
757 | ;; Have we reached something | |
758 | ;; that shows this isn't a function | |
759 | ;; definition? | |
6ec3899e RS |
760 | (and (< (point) indent-point) |
761 | (not (memq (following-char) | |
762 | '(?\, ?\;))))) | |
763 | ;; Make sure the "function decl" we found | |
764 | ;; is not inside a comment. | |
765 | (progn | |
95d19fad RS |
766 | ;; Move back to the `(' starting arglist |
767 | (goto-char lim) | |
6ec3899e RS |
768 | (beginning-of-line) |
769 | (while (and (not comment) | |
770 | (search-forward "/*" lim t)) | |
771 | (setq comment | |
772 | (not (search-forward "*/" lim t)))) | |
773 | (not comment))) | |
774 | c-argdecl-indent 0)))))) | |
a17915dc ER |
775 | basic-indent))) |
776 | ||
777 | ;; ;; Now add a little if this is a continuation line. | |
778 | ;; (+ basic-indent (if (or (bobp) | |
779 | ;; (memq (preceding-char) '(?\) ?\; ?\})) | |
780 | ;; ;; Line with zero indentation | |
781 | ;; ;; is probably the return-type | |
782 | ;; ;; of a function definition, | |
783 | ;; ;; so following line is function name. | |
784 | ;; (= (current-indentation) 0)) | |
785 | ;; 0 c-continued-statement-offset)) | |
786 | ||
787 | ((/= (char-after containing-sexp) ?{) | |
788 | ;; line is expression, not statement: | |
789 | ;; indent to just after the surrounding open. | |
790 | (goto-char (1+ containing-sexp)) | |
791 | (current-column)) | |
792 | (t | |
793 | ;; Statement level. Is it a continuation or a new statement? | |
794 | ;; Find previous non-comment character. | |
795 | (goto-char indent-point) | |
796 | (c-backward-to-noncomment containing-sexp) | |
797 | ;; Back up over label lines, since they don't | |
798 | ;; affect whether our line is a continuation. | |
799 | (while (or (eq (preceding-char) ?\,) | |
800 | (and (eq (preceding-char) ?:) | |
801 | (or (eq (char-after (- (point) 2)) ?\') | |
802 | (memq (char-syntax (char-after (- (point) 2))) | |
803 | '(?w ?_))))) | |
804 | (if (eq (preceding-char) ?\,) | |
805 | (progn (forward-char -1) | |
806 | (c-backward-to-start-of-continued-exp containing-sexp))) | |
807 | (beginning-of-line) | |
808 | (c-backward-to-noncomment containing-sexp)) | |
809 | ;; Check for a preprocessor statement or its continuation lines. | |
5ef470be KH |
810 | ;; Move back to end of previous non-preprocessor line, |
811 | ;; or possibly beginning of buffer. | |
a17915dc ER |
812 | (let ((found (point)) stop) |
813 | (while (not stop) | |
5ef470be KH |
814 | (beginning-of-line) |
815 | (cond ((bobp) | |
816 | (setq found (point) | |
817 | stop t)) | |
818 | ((save-excursion (forward-char -1) | |
a17915dc | 819 | (= (preceding-char) ?\\)) |
5ef470be | 820 | (forward-char -1)) |
a17915dc ER |
821 | ;; This line is not preceded by a backslash. |
822 | ;; So either it starts a preprocessor command | |
823 | ;; or any following continuation lines | |
824 | ;; should not be skipped. | |
5ef470be KH |
825 | ((= (following-char) ?#) |
826 | (forward-char -1) | |
a17915dc ER |
827 | (setq found (point))) |
828 | (t (setq stop t)))) | |
829 | (goto-char found)) | |
830 | ;; Now we get the answer. | |
5ef470be | 831 | (if (and (not (memq (preceding-char) '(0 ?\, ?\; ?\} ?\{))) |
a17915dc ER |
832 | ;; But don't treat a line with a close-brace |
833 | ;; as a continuation. It is probably the | |
834 | ;; end of an enum type declaration. | |
835 | (save-excursion | |
836 | (goto-char indent-point) | |
837 | (skip-chars-forward " \t") | |
838 | (not (= (following-char) ?})))) | |
839 | ;; This line is continuation of preceding line's statement; | |
840 | ;; indent c-continued-statement-offset more than the | |
841 | ;; previous line of the statement. | |
842 | (progn | |
843 | (c-backward-to-start-of-continued-exp containing-sexp) | |
844 | (+ c-continued-statement-offset (current-column) | |
845 | (if (save-excursion (goto-char indent-point) | |
846 | (skip-chars-forward " \t") | |
847 | (eq (following-char) ?{)) | |
848 | c-continued-brace-offset 0))) | |
849 | ;; This line starts a new statement. | |
850 | ;; Position following last unclosed open. | |
851 | (goto-char containing-sexp) | |
852 | ;; Is line first statement after an open-brace? | |
853 | (or | |
854 | ;; If no, find that first statement and indent like it. | |
855 | (save-excursion | |
856 | (forward-char 1) | |
857 | (let ((colon-line-end 0)) | |
858 | (while (progn (skip-chars-forward " \t\n") | |
859 | (looking-at "#\\|/\\*\\|case[ \t\n'/(].*:\\|[a-zA-Z0-9_$]*:")) | |
860 | ;; Skip over comments and labels following openbrace. | |
861 | (cond ((= (following-char) ?\#) | |
862 | (forward-line 1)) | |
863 | ((= (following-char) ?\/) | |
864 | (forward-char 2) | |
865 | (search-forward "*/" nil 'move)) | |
866 | ;; case or label: | |
867 | (t | |
868 | (save-excursion (end-of-line) | |
869 | (setq colon-line-end (point))) | |
870 | (search-forward ":")))) | |
871 | ;; The first following code counts | |
872 | ;; if it is before the line we want to indent. | |
873 | (and (< (point) indent-point) | |
1635fcdd RS |
874 | (- |
875 | (if (> colon-line-end (point)) | |
876 | (- (current-indentation) c-label-offset) | |
877 | (current-column)) | |
878 | ;; If prev stmt starts with open-brace, that | |
879 | ;; open brace was offset by c-brace-offset. | |
880 | ;; Compensate to get the column where | |
881 | ;; an ordinary statement would start. | |
882 | (if (= (following-char) ?\{) c-brace-offset 0))))) | |
a17915dc ER |
883 | ;; If no previous statement, |
884 | ;; indent it relative to line brace is on. | |
3a8a46f1 RS |
885 | (calculate-c-indent-after-brace)))))))) |
886 | ||
887 | (defun calculate-c-indent-after-brace () | |
888 | "Return the proper C indent for the first line after an open-brace. | |
889 | This function is called with point before the brace." | |
890 | ;; For open brace in column zero, don't let statement | |
891 | ;; start there too. If c-indent-level is zero, | |
892 | ;; use c-brace-offset + c-continued-statement-offset instead. | |
893 | ;; For open-braces not the first thing in a line, | |
894 | ;; add in c-brace-imaginary-offset. | |
895 | (+ (if (and (bolp) (zerop c-indent-level)) | |
896 | (+ c-brace-offset c-continued-statement-offset) | |
897 | c-indent-level) | |
898 | ;; Move back over whitespace before the openbrace. | |
899 | ;; If openbrace is not first nonwhite thing on the line, | |
900 | ;; add the c-brace-imaginary-offset. | |
901 | (progn (skip-chars-backward " \t") | |
902 | (if (bolp) 0 c-brace-imaginary-offset)) | |
903 | ;; If the openbrace is preceded by a parenthesized exp, | |
904 | ;; move to the beginning of that; | |
905 | ;; possibly a different line | |
906 | (progn | |
907 | (if (eq (preceding-char) ?\)) | |
908 | (forward-sexp -1)) | |
909 | ;; Get initial indentation of the line we are on. | |
910 | (current-indentation)))) | |
a17915dc ER |
911 | |
912 | (defun calculate-c-indent-within-comment (&optional after-star) | |
913 | "Return the indentation amount for line inside a block comment. | |
914 | Optional arg AFTER-STAR means, if lines in the comment have a leading star, | |
915 | return the indentation of the text that would follow this star." | |
916 | (let (end star-start) | |
917 | (save-excursion | |
918 | (beginning-of-line) | |
919 | (skip-chars-forward " \t") | |
920 | (setq star-start (= (following-char) ?\*)) | |
921 | (skip-chars-backward " \t\n") | |
922 | (setq end (point)) | |
923 | (beginning-of-line) | |
924 | (skip-chars-forward " \t") | |
925 | (if after-star | |
926 | (and (looking-at "\\*") | |
927 | (re-search-forward "\\*[ \t]*"))) | |
928 | (and (re-search-forward "/\\*[ \t]*" end t) | |
929 | star-start | |
930 | (not after-star) | |
931 | (goto-char (1+ (match-beginning 0)))) | |
932 | (if (and (looking-at "[ \t]*$") (= (preceding-char) ?\*)) | |
933 | (1+ (current-column)) | |
934 | (current-column))))) | |
935 | ||
936 | ||
937 | (defun c-backward-to-noncomment (lim) | |
938 | (let (opoint stop) | |
939 | (while (not stop) | |
940 | (skip-chars-backward " \t\n\f" lim) | |
941 | (setq opoint (point)) | |
942 | (if (and (>= (point) (+ 2 lim)) | |
943 | (save-excursion | |
944 | (forward-char -2) | |
945 | (looking-at "\\*/"))) | |
946 | (search-backward "/*" lim 'move) | |
947 | (setq stop (or (<= (point) lim) | |
948 | (save-excursion | |
949 | (beginning-of-line) | |
950 | (skip-chars-forward " \t") | |
951 | (not (looking-at "#"))))) | |
952 | (or stop (beginning-of-line)))))) | |
953 | ||
954 | (defun c-backward-to-start-of-continued-exp (lim) | |
955 | (if (memq (preceding-char) '(?\) ?\")) | |
956 | (forward-sexp -1)) | |
957 | (beginning-of-line) | |
958 | (if (<= (point) lim) | |
959 | (goto-char (1+ lim))) | |
960 | (skip-chars-forward " \t")) | |
961 | ||
962 | (defun c-backward-to-start-of-if (&optional limit) | |
963 | "Move to the start of the last \"unbalanced\" `if'." | |
964 | (or limit (setq limit (save-excursion (beginning-of-defun) (point)))) | |
965 | (let ((if-level 1) | |
966 | (case-fold-search nil)) | |
967 | (while (and (not (bobp)) (not (zerop if-level))) | |
968 | (backward-sexp 1) | |
401662cb RS |
969 | (cond ((and (looking-at "else\\b") |
970 | (not (looking-at "else\\s_"))) | |
a17915dc | 971 | (setq if-level (1+ if-level))) |
401662cb RS |
972 | ((and (looking-at "if\\b") |
973 | (not (looking-at "if\\s_"))) | |
a17915dc ER |
974 | (setq if-level (1- if-level))) |
975 | ((< (point) limit) | |
976 | (setq if-level 0) | |
977 | (goto-char limit)))))) | |
978 | ||
979 | (defun c-backward-to-start-of-do (&optional limit) | |
980 | "If point follows a `do' statement, move to beginning of it and return t. | |
981 | Otherwise return nil and don't move point." | |
982 | (or limit (setq limit (save-excursion (beginning-of-defun) (point)))) | |
983 | (let ((first t) | |
984 | (startpos (point)) | |
985 | (done nil)) | |
986 | (while (not done) | |
987 | (let ((next-start (point))) | |
988 | (condition-case nil | |
989 | ;; Move back one token or one brace or paren group. | |
990 | (backward-sexp 1) | |
991 | ;; If we find an open-brace, we lose. | |
992 | (error (setq done 'fail))) | |
993 | (if done | |
994 | nil | |
995 | ;; If we reached a `do', we win. | |
996 | (if (looking-at "do\\b") | |
997 | (setq done 'succeed) | |
998 | ;; Otherwise, if we skipped a semicolon, we lose. | |
999 | ;; (Exception: we can skip one semicolon before getting | |
1000 | ;; to a the last token of the statement, unless that token | |
1001 | ;; is a close brace.) | |
1002 | (if (save-excursion | |
1003 | (forward-sexp 1) | |
1004 | (or (and (not first) (= (preceding-char) ?})) | |
1005 | (search-forward ";" next-start t | |
1006 | (if (and first | |
1007 | (/= (preceding-char) ?})) | |
1008 | 2 1)))) | |
1009 | (setq done 'fail) | |
1010 | (setq first nil) | |
1011 | ;; If we go too far back in the buffer, we lose. | |
1012 | (if (< (point) limit) | |
1013 | (setq done 'fail))))))) | |
1014 | (if (eq done 'succeed) | |
1015 | t | |
1016 | (goto-char startpos) | |
1017 | nil))) | |
1018 | \f | |
1019 | (defun c-beginning-of-statement (count) | |
1020 | "Go to the beginning of the innermost C statement. | |
1021 | With prefix arg, go back N - 1 statements. If already at the beginning of a | |
eb8c3be9 | 1022 | statement then go to the beginning of the preceding one. |
17f0e88a RS |
1023 | If within a string or comment, or next to a comment (only whitespace between), |
1024 | move by sentences instead of statements." | |
a17915dc | 1025 | (interactive "p") |
d4fbc2d5 RS |
1026 | (let ((here (point)) state) |
1027 | (save-excursion | |
1028 | (beginning-of-defun) | |
1029 | (setq state (parse-partial-sexp (point) here nil nil))) | |
17f0e88a RS |
1030 | (if (or (nth 3 state) (nth 4 state) |
1031 | (looking-at (concat "[ \t]*" comment-start-skip)) | |
1032 | (save-excursion (skip-chars-backward " \t") | |
1033 | (goto-char (- (point) 2)) | |
1034 | (looking-at "\\*/"))) | |
d4fbc2d5 RS |
1035 | (forward-sentence (- count)) |
1036 | (while (> count 0) | |
1037 | (c-beginning-of-statement-1) | |
1038 | (setq count (1- count))) | |
1039 | (while (< count 0) | |
1040 | (c-end-of-statement-1) | |
1041 | (setq count (1+ count)))))) | |
a17915dc ER |
1042 | |
1043 | (defun c-end-of-statement (count) | |
1044 | "Go to the end of the innermost C statement. | |
d4fbc2d5 RS |
1045 | With prefix arg, go forward N - 1 statements. |
1046 | Move forward to end of the next statement if already at end. | |
1047 | If within a string or comment, move by sentences instead of statements." | |
a17915dc ER |
1048 | (interactive "p") |
1049 | (c-beginning-of-statement (- count))) | |
1050 | ||
1051 | (defun c-beginning-of-statement-1 () | |
1052 | (let ((last-begin (point)) | |
1053 | (first t)) | |
1054 | (condition-case () | |
1055 | (progn | |
1056 | (while (and (not (bobp)) | |
1057 | (progn | |
1058 | (backward-sexp 1) | |
1059 | (or first | |
1060 | (not (re-search-forward "[;{}]" last-begin t))))) | |
1061 | (setq last-begin (point) first nil)) | |
1062 | (goto-char last-begin)) | |
1063 | (error (if first (backward-up-list 1) (goto-char last-begin)))))) | |
1064 | ||
1065 | (defun c-end-of-statement-1 () | |
1066 | (condition-case () | |
1067 | (progn | |
1068 | (while (and (not (eobp)) | |
1069 | (let ((beg (point))) | |
1070 | (forward-sexp 1) | |
1071 | (let ((end (point))) | |
1072 | (save-excursion | |
1073 | (goto-char beg) | |
1074 | (not (re-search-forward "[;{}]" end t))))))) | |
1075 | (re-search-backward "[;}]") | |
1076 | (forward-char 1)) | |
1077 | (error | |
1078 | (let ((beg (point))) | |
1079 | (backward-up-list -1) | |
1080 | (let ((end (point))) | |
1081 | (goto-char beg) | |
1082 | (search-forward ";" end 'move)))))) | |
1083 | \f | |
1084 | (defun mark-c-function () | |
1085 | "Put mark at end of C function, point at beginning." | |
1086 | (interactive) | |
1087 | (push-mark (point)) | |
1088 | (end-of-defun) | |
069b6ce3 | 1089 | (push-mark (point) nil t) |
a17915dc ER |
1090 | (beginning-of-defun) |
1091 | (backward-paragraph)) | |
1092 | \f | |
eccf8697 RS |
1093 | ;; Idea of ENDPOS is, indent each line, stopping when |
1094 | ;; ENDPOS is encountered. But it's too much of a pain to make that work. | |
a17915dc | 1095 | (defun indent-c-exp (&optional endpos) |
eccf8697 | 1096 | "Indent each line of the C grouping following point." |
a17915dc ER |
1097 | (interactive) |
1098 | (let* ((indent-stack (list nil)) | |
1099 | (opoint (point)) ;; May be altered below. | |
1100 | (contain-stack | |
1101 | (list (if endpos | |
1102 | (let (funbeg) | |
1103 | ;; Find previous fcn-start. | |
1104 | (save-excursion (forward-char 1) | |
1105 | (beginning-of-defun) | |
1106 | (setq funbeg (point))) | |
9ce49c8d | 1107 | (setq opoint funbeg) |
a17915dc ER |
1108 | ;; Try to find containing open, |
1109 | ;; but don't scan past that fcn-start. | |
1110 | (save-restriction | |
1111 | (narrow-to-region funbeg (point)) | |
1112 | (condition-case nil | |
1113 | (save-excursion | |
9ce49c8d RS |
1114 | (backward-up-list 1) |
1115 | (point)) | |
a17915dc ER |
1116 | ;; We gave up: must be between fcns. |
1117 | ;; Set opoint to beg of prev fcn | |
1118 | ;; since otherwise calculate-c-indent | |
1119 | ;; will get wrong answers. | |
1120 | (error (setq opoint funbeg) | |
1121 | (point))))) | |
1122 | (point)))) | |
1123 | (case-fold-search nil) | |
1124 | restart outer-loop-done inner-loop-done state ostate | |
1125 | this-indent last-sexp | |
1126 | at-else at-brace at-while | |
3bbf6363 | 1127 | last-depth this-point |
a17915dc ER |
1128 | (next-depth 0)) |
1129 | ;; If the braces don't match, get an error right away. | |
1130 | (save-excursion | |
1131 | (forward-sexp 1)) | |
1132 | ;; Realign the comment on the first line, even though we don't reindent it. | |
1133 | (save-excursion | |
1134 | (let ((beg (point))) | |
1135 | (and (re-search-forward | |
1136 | comment-start-skip | |
1137 | (save-excursion (end-of-line) (point)) t) | |
9ce49c8d RS |
1138 | ;; Make sure this isn't a comment alone on a line |
1139 | ;; (which should be indented like code instead). | |
1140 | (save-excursion | |
1141 | (goto-char (match-beginning 0)) | |
1142 | (skip-chars-backward " \t") | |
1143 | (not (bolp))) | |
a17915dc ER |
1144 | ;; Make sure the comment starter we found |
1145 | ;; is not actually in a string or quoted. | |
1146 | (let ((new-state | |
1147 | (parse-partial-sexp beg (point) | |
1148 | nil nil state))) | |
1149 | (and (not (nth 3 new-state)) (not (nth 5 new-state)))) | |
1150 | (progn (indent-for-comment) (beginning-of-line))))) | |
1151 | (save-excursion | |
1152 | (setq outer-loop-done nil) | |
1153 | (while (and (not (eobp)) | |
1154 | (if endpos (< (point) endpos) | |
1155 | (not outer-loop-done))) | |
1156 | (setq last-depth next-depth) | |
1157 | ;; Compute how depth changes over this line | |
1158 | ;; plus enough other lines to get to one that | |
1159 | ;; does not end inside a comment or string. | |
1160 | ;; Meanwhile, do appropriate indentation on comment lines. | |
1161 | (setq inner-loop-done nil) | |
1162 | (while (and (not inner-loop-done) | |
1163 | (not (and (eobp) (setq outer-loop-done t)))) | |
1164 | (setq ostate state) | |
1165 | (setq state (parse-partial-sexp (point) (progn (end-of-line) (point)) | |
1166 | nil nil state)) | |
1167 | (setq next-depth (car state)) | |
1168 | (if (and (car (cdr (cdr state))) | |
1169 | (>= (car (cdr (cdr state))) 0)) | |
1170 | (setq last-sexp (car (cdr (cdr state))))) | |
3bbf6363 RS |
1171 | ;; If this line started within a comment, indent it as such. |
1172 | (if (or (nth 4 ostate) (nth 7 ostate)) | |
a17915dc | 1173 | (c-indent-line)) |
3bbf6363 RS |
1174 | ;; If it ends outside of comments or strings, exit the inner loop. |
1175 | ;; Otherwise move on to next line. | |
1176 | (if (or (nth 3 state) (nth 4 state) (nth 7 state)) | |
a17915dc ER |
1177 | (forward-line 1) |
1178 | (setq inner-loop-done t))) | |
1179 | (and endpos | |
1180 | (while (< next-depth 0) | |
1181 | (setq indent-stack (append indent-stack (list nil))) | |
1182 | (setq contain-stack (append contain-stack (list nil))) | |
1183 | (setq next-depth (1+ next-depth)) | |
1184 | (setq last-depth (1+ last-depth)) | |
1185 | (setcar (nthcdr 6 state) (1+ (nth 6 state))))) | |
1186 | (setq outer-loop-done (and (not endpos) (<= next-depth 0))) | |
1187 | (if outer-loop-done | |
1188 | nil | |
1189 | ;; If this line had ..))) (((.. in it, pop out of the levels | |
1190 | ;; that ended anywhere in this line, even if the final depth | |
1191 | ;; doesn't indicate that they ended. | |
1192 | (while (> last-depth (nth 6 state)) | |
1193 | (setq indent-stack (cdr indent-stack) | |
1194 | contain-stack (cdr contain-stack) | |
1195 | last-depth (1- last-depth))) | |
1196 | (if (/= last-depth next-depth) | |
1197 | (setq last-sexp nil)) | |
1198 | ;; Add levels for any parens that were started in this line. | |
1199 | (while (< last-depth next-depth) | |
1200 | (setq indent-stack (cons nil indent-stack) | |
1201 | contain-stack (cons nil contain-stack) | |
1202 | last-depth (1+ last-depth))) | |
1203 | (if (null (car contain-stack)) | |
1204 | (setcar contain-stack (or (car (cdr state)) | |
1205 | (save-excursion (forward-sexp -1) | |
1206 | (point))))) | |
1207 | (forward-line 1) | |
1208 | (skip-chars-forward " \t") | |
9ce49c8d RS |
1209 | ;; Don't really reindent if the line is just whitespace, |
1210 | ;; or if it is past the endpos. | |
1211 | ;; (The exit test in the outer while | |
1212 | ;; does not exit until we have passed the first line | |
1213 | ;; past the region.) | |
1214 | (if (or (eolp) (and endpos (>= (point) endpos))) | |
a17915dc | 1215 | nil |
73dae9a9 RS |
1216 | ;; Is this line in a new nesting level? |
1217 | ;; In other words, is this the first line that | |
1218 | ;; starts in the new level? | |
a17915dc ER |
1219 | (if (and (car indent-stack) |
1220 | (>= (car indent-stack) 0)) | |
73dae9a9 RS |
1221 | nil |
1222 | ;; Yes. | |
a17915dc | 1223 | ;; Compute the standard indent for this level. |
73dae9a9 RS |
1224 | (let (val) |
1225 | (if (= (char-after (car contain-stack)) ?{) | |
1226 | (save-excursion | |
1227 | (goto-char (car contain-stack)) | |
3a8a46f1 | 1228 | (setq val (calculate-c-indent-after-brace))) |
73dae9a9 RS |
1229 | (setq val (calculate-c-indent |
1230 | (if (car indent-stack) | |
1231 | (- (car indent-stack)) | |
1232 | opoint)))) | |
3746159f RS |
1233 | ;; t means we are in a block comment and should |
1234 | ;; calculate accordingly. | |
1235 | (if (eq val t) | |
1236 | (setq val (calculate-c-indent-within-comment))) | |
73dae9a9 RS |
1237 | (setcar indent-stack val))) |
1238 | ;; Adjust indent of this individual line | |
1239 | ;; based on its predecessor. | |
1240 | ;; Handle continuation lines, if, else, while, and so on. | |
1241 | (if (/= (char-after (car contain-stack)) ?{) | |
1242 | (setq this-indent (car indent-stack)) | |
1243 | ;; Line is at statement level. | |
1244 | ;; Is it a new statement? Is it an else? | |
1245 | ;; Find last non-comment character before this line | |
1246 | (save-excursion | |
1247 | (setq this-point (point)) | |
1248 | (setq at-else (and (looking-at "else\\b") | |
1249 | (not (looking-at "else\\s_")))) | |
1250 | (setq at-brace (= (following-char) ?{)) | |
1251 | (setq at-while (and (looking-at "while\\b") | |
1252 | (not (looking-at "while\\s_")))) | |
1253 | (if (= (following-char) ?}) | |
1254 | (setq this-indent (car indent-stack)) | |
1255 | (c-backward-to-noncomment opoint) | |
1256 | (if (not (memq (preceding-char) '(0 ?\, ?\; ?} ?: ?{))) | |
1257 | ;; Preceding line did not end in comma or semi; | |
1258 | ;; indent this line c-continued-statement-offset | |
1259 | ;; more than previous. | |
1260 | (progn | |
1261 | (c-backward-to-start-of-continued-exp (car contain-stack)) | |
1262 | (setq this-indent | |
1263 | (+ c-continued-statement-offset (current-column) | |
1264 | (if at-brace c-continued-brace-offset 0)))) | |
1265 | ;; Preceding line ended in comma or semi; | |
1266 | ;; use the standard indent for this level. | |
1267 | (cond (at-else (progn (c-backward-to-start-of-if opoint) | |
1268 | (setq this-indent | |
1269 | (current-indentation)))) | |
1270 | ((and at-while (c-backward-to-start-of-do opoint)) | |
1271 | (setq this-indent (current-indentation))) | |
1272 | ((eq (preceding-char) ?\,) | |
1273 | (goto-char this-point) | |
1274 | (setq this-indent (calculate-c-indent))) | |
1275 | (t (setq this-indent (car indent-stack)))))))) | |
a17915dc | 1276 | ;; Adjust line indentation according to its contents |
3a65941e | 1277 | (if (or (looking-at c-switch-label-regexp) |
a17915dc ER |
1278 | (and (looking-at "[A-Za-z]") |
1279 | (save-excursion | |
1280 | (forward-sexp 1) | |
1281 | (looking-at ":")))) | |
1282 | (setq this-indent (max 1 (+ this-indent c-label-offset)))) | |
1283 | (if (= (following-char) ?}) | |
1284 | (setq this-indent (- this-indent c-indent-level))) | |
1285 | (if (= (following-char) ?{) | |
e6033883 RS |
1286 | ;; Don't move an open-brace in column 0. |
1287 | ;; This is good when constructs such as | |
1288 | ;; `extern "C" {' surround a function definition | |
1289 | ;; that should be indented as usual. | |
1290 | ;; It is also good for nested functions. | |
1291 | ;; It is bad when an open-brace is indented at column 0 | |
1292 | ;; and you want to fix that, but we can't win 'em all. | |
b70af8d7 RS |
1293 | (if (zerop (current-column)) |
1294 | (setq this-indent 0) | |
1295 | (setq this-indent (+ this-indent c-brace-offset)))) | |
a17915dc ER |
1296 | ;; Don't leave indentation in empty lines. |
1297 | (if (eolp) (setq this-indent 0)) | |
1298 | ;; Put chosen indentation into effect. | |
1299 | (or (= (current-column) this-indent) | |
1300 | (= (following-char) ?\#) | |
1301 | (progn | |
1302 | (delete-region (point) (progn (beginning-of-line) (point))) | |
1303 | (indent-to this-indent))) | |
1304 | ;; Indent any comment following the text. | |
1305 | (or (looking-at comment-start-skip) | |
62057cef RS |
1306 | (save-excursion |
1307 | (let ((beg (point))) | |
1308 | (and (re-search-forward | |
1309 | comment-start-skip | |
1310 | (save-excursion (end-of-line) (point)) t) | |
1311 | ;; Make sure the comment starter we found | |
1312 | ;; is not actually in a string or quoted. | |
1313 | (let ((new-state | |
1314 | (parse-partial-sexp beg (point) | |
1315 | nil nil state))) | |
1316 | (and (not (nth 3 new-state)) (not (nth 5 new-state)))) | |
1317 | (indent-for-comment))))))))))) | |
a17915dc ER |
1318 | |
1319 | ;; Look at all comment-start strings in the current line after point. | |
1320 | ;; Return t if one of them starts a real comment. | |
1321 | ;; This is not used yet, because indent-for-comment | |
1322 | ;; isn't smart enough to handle the cases this can find. | |
1323 | (defun indent-c-find-real-comment () | |
1324 | (let (win) | |
1325 | (while (and (not win) | |
1326 | (re-search-forward comment-start-skip | |
1327 | (save-excursion (end-of-line) (point)) | |
1328 | t)) | |
1329 | ;; Make sure the comment start is not quoted. | |
1330 | (let ((state-1 | |
1331 | (parse-partial-sexp | |
1332 | (save-excursion (beginning-of-line) (point)) | |
1333 | (point) nil nil state))) | |
1334 | (setq win (and (null (nth 3 state-1)) (null (nth 5 state-1)))))) | |
1335 | win)) | |
1336 | ||
1337 | ;; Indent every line whose first char is between START and END inclusive. | |
1338 | (defun c-indent-region (start end) | |
1339 | (save-excursion | |
1340 | (goto-char start) | |
988e2aa6 RS |
1341 | ;; Advance to first nonblank line. |
1342 | (skip-chars-forward " \t\n") | |
1343 | (beginning-of-line) | |
eccf8697 RS |
1344 | (let ((endmark (copy-marker end)) |
1345 | (c-tab-always-indent t)) | |
d52fd67c | 1346 | (while (and (bolp) (not (eobp)) (< (point) endmark)) |
eccf8697 RS |
1347 | ;; Indent one line as with TAB. |
1348 | (let ((shift-amt (c-indent-line)) | |
4fd99ee4 | 1349 | nextline sexpbeg sexpend) |
d52fd67c RS |
1350 | (if (save-excursion (beginning-of-line) (looking-at "[ \t]*#")) |
1351 | (forward-line 1) | |
eccf8697 | 1352 | (save-excursion |
d52fd67c RS |
1353 | ;; Find beginning of following line. |
1354 | (save-excursion | |
1355 | (forward-line 1) (setq nextline (point))) | |
1356 | ;; Find first beginning-of-sexp for sexp extending past this line. | |
1357 | (beginning-of-line) | |
1358 | (while (< (point) nextline) | |
1359 | (condition-case nil | |
1360 | (progn | |
1361 | (forward-sexp 1) | |
1362 | (setq sexpend (point-marker))) | |
1363 | (error (setq sexpend nil) | |
1364 | (goto-char nextline))) | |
1365 | (skip-chars-forward " \t\n")) | |
1366 | (if sexpend | |
eccf8697 | 1367 | (progn |
d52fd67c RS |
1368 | ;; Make sure the sexp we found really starts on the |
1369 | ;; current line and extends past it. | |
1370 | (goto-char sexpend) | |
1371 | (backward-sexp 1) | |
1372 | (setq sexpbeg (point))))) | |
1373 | ;; If that sexp ends within the region, | |
1374 | ;; indent it all at once, fast. | |
1375 | (if (and sexpend (> sexpend nextline) (<= sexpend endmark) | |
1376 | (< sexpbeg nextline)) | |
0b171628 | 1377 | (progn |
d52fd67c RS |
1378 | (indent-c-exp) |
1379 | (goto-char sexpend))) | |
1380 | ;; Move to following line and try again. | |
1381 | (and sexpend (set-marker sexpend nil)) | |
1382 | (forward-line 1)))) | |
a17915dc ER |
1383 | (set-marker endmark nil)))) |
1384 | \f | |
1832dbd1 | 1385 | (defun set-c-style (style &optional global) |
a17915dc | 1386 | "Set C-mode variables to use one of several different indentation styles. |
1832dbd1 RS |
1387 | The arguments are a string representing the desired style |
1388 | and a flag which, if non-nil, means to set the style globally. | |
1389 | \(Interactively, the flag comes from the prefix argument.) | |
e6dfdce5 | 1390 | Available styles are GNU, K&R, BSD and Whitesmith." |
389817fd KH |
1391 | (interactive (list (let ((completion-ignore-case t)) |
1392 | (completing-read "Use which C indentation style? " | |
1393 | c-style-alist nil t)) | |
1832dbd1 | 1394 | current-prefix-arg)) |
a17915dc | 1395 | (let ((vars (cdr (assoc style c-style-alist)))) |
1832dbd1 RS |
1396 | (or vars |
1397 | (error "Invalid C indentation style `%s'" style)) | |
a17915dc | 1398 | (while vars |
1832dbd1 RS |
1399 | (or global |
1400 | (make-local-variable (car (car vars)))) | |
a17915dc ER |
1401 | (set (car (car vars)) (cdr (car vars))) |
1402 | (setq vars (cdr vars))))) | |
1832dbd1 RS |
1403 | \f |
1404 | ;;; This page handles insertion and removal of backslashes for C macros. | |
1405 | ||
1406 | (defvar c-backslash-column 48 | |
1407 | "*Minimum column for end-of-line backslashes of macro definitions.") | |
1408 | ||
1409 | (defun c-backslash-region (from to delete-flag) | |
1410 | "Insert, align, or delete end-of-line backslashes on the lines in the region. | |
1411 | With no argument, inserts backslashes and aligns existing backslashes. | |
1412 | With an argument, deletes the backslashes. | |
1413 | ||
1414 | This function does not modify the last line of the region if the region ends | |
1415 | right at the start of the following line; it does not modify blank lines | |
1416 | at the start of the region. So you can put the region around an entire macro | |
1417 | definition and conveniently use this command." | |
1418 | (interactive "r\nP") | |
1419 | (save-excursion | |
1420 | (goto-char from) | |
1421 | (let ((column c-backslash-column) | |
1422 | (endmark (make-marker))) | |
1423 | (move-marker endmark to) | |
1424 | ;; Compute the smallest column number past the ends of all the lines. | |
1425 | (if (not delete-flag) | |
1426 | (while (< (point) to) | |
1427 | (end-of-line) | |
1428 | (if (= (preceding-char) ?\\) | |
1429 | (progn (forward-char -1) | |
1430 | (skip-chars-backward " \t"))) | |
1431 | (setq column (max column (1+ (current-column)))) | |
1432 | (forward-line 1))) | |
1433 | ;; Adjust upward to a tab column, if that doesn't push past the margin. | |
1434 | (if (> (% column tab-width) 0) | |
1435 | (let ((adjusted (* (/ (+ column tab-width -1) tab-width) tab-width))) | |
1436 | (if (< adjusted (window-width)) | |
1437 | (setq column adjusted)))) | |
1438 | ;; Don't modify blank lines at start of region. | |
1439 | (goto-char from) | |
1440 | (while (and (< (point) endmark) (eolp)) | |
1441 | (forward-line 1)) | |
1442 | ;; Add or remove backslashes on all the lines. | |
1443 | (while (and (< (point) endmark) | |
1444 | ;; Don't backslashify the last line | |
1445 | ;; if the region ends right at the start of the next line. | |
1446 | (save-excursion | |
1447 | (forward-line 1) | |
1448 | (< (point) endmark))) | |
1449 | (if (not delete-flag) | |
1450 | (c-append-backslash column) | |
1451 | (c-delete-backslash)) | |
1452 | (forward-line 1)) | |
1453 | (move-marker endmark nil)))) | |
1454 | ||
1455 | (defun c-append-backslash (column) | |
1456 | (end-of-line) | |
1457 | ;; Note that "\\\\" is needed to get one backslash. | |
1458 | (if (= (preceding-char) ?\\) | |
1459 | (progn (forward-char -1) | |
1460 | (delete-horizontal-space) | |
1461 | (indent-to column)) | |
1462 | (indent-to column) | |
1463 | (insert "\\"))) | |
1464 | ||
1465 | (defun c-delete-backslash () | |
1466 | (end-of-line) | |
7620dcc5 RS |
1467 | (or (bolp) |
1468 | (progn | |
1469 | (forward-char -1) | |
1470 | (if (looking-at "\\\\") | |
1471 | (delete-region (1+ (point)) | |
1472 | (progn (skip-chars-backward " \t") (point))))))) | |
1586b965 RS |
1473 | \f |
1474 | (defun c-up-conditional (count) | |
1475 | "Move back to the containing preprocessor conditional, leaving mark behind. | |
1476 | A prefix argument acts as a repeat count. With a negative argument, | |
1477 | move forward to the end of the containing preprocessor conditional. | |
1478 | When going backwards, `#elif' is treated like `#else' followed by `#if'. | |
1479 | When going forwards, `#elif' is ignored." | |
1480 | (interactive "p") | |
3c8aaef2 RS |
1481 | (c-forward-conditional (- count) t)) |
1482 | ||
1483 | (defun c-backward-conditional (count &optional up-flag) | |
1484 | "Move back across a preprocessor conditional, leaving mark behind. | |
1485 | A prefix argument acts as a repeat count. With a negative argument, | |
1486 | move forward across a preprocessor conditional." | |
1487 | (interactive "p") | |
1488 | (c-forward-conditional (- count) up-flag)) | |
1489 | ||
1490 | (defun c-forward-conditional (count &optional up-flag) | |
1491 | "Move forward across a preprocessor conditional, leaving mark behind. | |
1492 | A prefix argument acts as a repeat count. With a negative argument, | |
1493 | move backward across a preprocessor conditional." | |
1494 | (interactive "p") | |
1495 | (let* ((forward (> count 0)) | |
1586b965 RS |
1496 | (increment (if forward -1 1)) |
1497 | (search-function (if forward 're-search-forward 're-search-backward)) | |
1498 | (opoint (point)) | |
1499 | (new)) | |
1500 | (save-excursion | |
1501 | (while (/= count 0) | |
3c8aaef2 | 1502 | (let ((depth (if up-flag 0 -1)) found) |
1586b965 RS |
1503 | (save-excursion |
1504 | ;; Find the "next" significant line in the proper direction. | |
1505 | (while (and (not found) | |
1506 | ;; Rather than searching for a # sign that comes | |
1507 | ;; at the beginning of a line aside from whitespace, | |
1508 | ;; search first for a string starting with # sign. | |
1509 | ;; Then verify what precedes it. | |
1510 | ;; This is faster on account of the fastmap feature of | |
1511 | ;; the regexp matcher. | |
1512 | (funcall search-function | |
1513 | "#[ \t]*\\(if\\|elif\\|endif\\)" | |
1e2d7cce | 1514 | nil t)) |
1586b965 | 1515 | (beginning-of-line) |
1e2d7cce RS |
1516 | ;; Now verify it is really a preproc line. |
1517 | (if (looking-at "^[ \t]*#[ \t]*\\(if\\|elif\\|endif\\)") | |
3c8aaef2 | 1518 | (let ((prev depth)) |
1e2d7cce RS |
1519 | ;; Update depth according to what we found. |
1520 | (beginning-of-line) | |
1521 | (cond ((looking-at "[ \t]*#[ \t]*endif") | |
1522 | (setq depth (+ depth increment))) | |
1523 | ((looking-at "[ \t]*#[ \t]*elif") | |
1524 | (if (and forward (= depth 0)) | |
1525 | (setq found (point)))) | |
1526 | (t (setq depth (- depth increment)))) | |
3c8aaef2 RS |
1527 | ;; If we are trying to move across, and we find |
1528 | ;; an end before we find a beginning, get an error. | |
1529 | (if (and (< prev 0) (< depth prev)) | |
1530 | (error (if forward | |
1531 | "No following conditional at this level" | |
1532 | "No previous conditional at this level"))) | |
1533 | ;; When searching forward, start from next line | |
1534 | ;; so that we don't find the same line again. | |
1535 | (if forward (forward-line 1)) | |
1e2d7cce RS |
1536 | ;; If this line exits a level of conditional, exit inner loop. |
1537 | (if (< depth 0) | |
29869734 RS |
1538 | (setq found (point)))) |
1539 | ;; If the line is not really a conditional, skip past it. | |
1540 | (if forward (end-of-line))))) | |
1586b965 RS |
1541 | (or found |
1542 | (error "No containing preprocessor conditional")) | |
1543 | (goto-char (setq new found))) | |
3c8aaef2 | 1544 | (setq count (+ count increment)))) |
1586b965 RS |
1545 | (push-mark) |
1546 | (goto-char new))) | |
c0274f38 ER |
1547 | |
1548 | ;;; c-mode.el ends here |