Mercurial > emacs
annotate lisp/progmodes/c-mode.el @ 1382:9b210c7d6c23
* rmail.el (rmail-mode): Make this autoload; we might find a file
whose first line local variables want to put it in RMAIL mode;
that ought to work.
author | Jim Blandy <jimb@redhat.com> |
---|---|
date | Sun, 11 Oct 1992 06:35:04 +0000 |
parents | 56d315ca10ee |
children | 26d8a65a6e32 |
rev | line source |
---|---|
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
472
diff
changeset
|
1 ;;; c-mode.el --- C code editing commands for Emacs |
909
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
2 ;; Copyright (C) 1985, 1986, 1987, 1992 Free Software Foundation, Inc. |
846
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
837
diff
changeset
|
3 |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
759
diff
changeset
|
4 ;; Maintainer: FSF |
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
759
diff
changeset
|
5 ;; Keywords: c |
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
759
diff
changeset
|
6 |
453 | 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 | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
759
diff
changeset
|
11 ;; the Free Software Foundation; either version 2, or (at your option) |
453 | 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 | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
759
diff
changeset
|
23 ;;; Code: |
453 | 24 |
25 (defvar c-mode-abbrev-table nil | |
26 "Abbrev table in use in C mode.") | |
27 (define-abbrev-table 'c-mode-abbrev-table ()) | |
28 | |
29 (defvar c-mode-map () | |
30 "Keymap used in C mode.") | |
31 (if c-mode-map | |
32 () | |
33 (setq c-mode-map (make-sparse-keymap)) | |
34 (define-key c-mode-map "{" 'electric-c-brace) | |
35 (define-key c-mode-map "}" 'electric-c-brace) | |
36 (define-key c-mode-map ";" 'electric-c-semi) | |
37 (define-key c-mode-map "#" 'electric-c-sharp-sign) | |
38 (define-key c-mode-map ":" 'electric-c-terminator) | |
39 (define-key c-mode-map "\e\C-h" 'mark-c-function) | |
40 (define-key c-mode-map "\e\C-q" 'indent-c-exp) | |
41 (define-key c-mode-map "\ea" 'c-beginning-of-statement) | |
42 (define-key c-mode-map "\ee" 'c-end-of-statement) | |
43 (define-key c-mode-map "\eq" 'c-fill-paragraph) | |
44 (define-key c-mode-map "\177" 'backward-delete-char-untabify) | |
45 (define-key c-mode-map "\t" 'c-indent-command)) | |
46 | |
47 ;; cmacexp is lame because it uses no preprocessor symbols. | |
48 ;; It isn't very extensible either -- hardcodes /lib/cpp. | |
49 (autoload 'c-macro-expand "cmacexp" | |
50 "Display the result of expanding all C macros occurring in the region. | |
51 The expansion is entirely correct because it uses the C preprocessor." | |
52 t) | |
53 | |
54 (defvar c-mode-syntax-table nil | |
55 "Syntax table in use in C-mode buffers.") | |
56 | |
57 (if c-mode-syntax-table | |
58 () | |
59 (setq c-mode-syntax-table (make-syntax-table)) | |
60 (modify-syntax-entry ?\\ "\\" c-mode-syntax-table) | |
61 (modify-syntax-entry ?/ ". 14" c-mode-syntax-table) | |
62 (modify-syntax-entry ?* ". 23" c-mode-syntax-table) | |
63 (modify-syntax-entry ?+ "." c-mode-syntax-table) | |
64 (modify-syntax-entry ?- "." c-mode-syntax-table) | |
65 (modify-syntax-entry ?= "." c-mode-syntax-table) | |
66 (modify-syntax-entry ?% "." c-mode-syntax-table) | |
67 (modify-syntax-entry ?< "." c-mode-syntax-table) | |
68 (modify-syntax-entry ?> "." c-mode-syntax-table) | |
69 (modify-syntax-entry ?& "." c-mode-syntax-table) | |
70 (modify-syntax-entry ?| "." c-mode-syntax-table) | |
71 (modify-syntax-entry ?\' "\"" c-mode-syntax-table)) | |
72 | |
73 (defconst c-indent-level 2 | |
74 "*Indentation of C statements with respect to containing block.") | |
75 (defconst c-brace-imaginary-offset 0 | |
76 "*Imagined indentation of a C open brace that actually follows a statement.") | |
77 (defconst c-brace-offset 0 | |
78 "*Extra indentation for braces, compared with other text in same context.") | |
79 (defconst c-argdecl-indent 5 | |
80 "*Indentation level of declarations of C function arguments.") | |
81 (defconst c-label-offset -2 | |
82 "*Offset of C label lines and case statements relative to usual indentation.") | |
83 (defconst c-continued-statement-offset 2 | |
84 "*Extra indent for lines not starting new statements.") | |
85 (defconst c-continued-brace-offset 0 | |
86 "*Extra indent for substatements that start with open-braces. | |
87 This is in addition to c-continued-statement-offset.") | |
88 (defconst c-style-alist | |
89 '(("GNU" | |
90 (c-indent-level . 2) | |
91 (c-argdecl-indent . 5) | |
92 (c-brace-offset . 0) | |
93 (c-label-offset . -2) | |
94 (c-continued-statement-offset . 2)) | |
95 ("K&R" | |
96 (c-indent-level . 5) | |
97 (c-argdecl-indent . 0) | |
98 (c-brace-offset . -5) | |
99 (c-label-offset . -5) | |
100 (c-continued-statement-offset . 5)) | |
101 ("BSD" | |
102 (c-indent-level . 4) | |
103 (c-argdecl-indent . 4) | |
104 (c-brace-offset . -4) | |
105 (c-label-offset . -4) | |
106 (c-continued-statement-offset . 4)) | |
472 | 107 (C++ |
108 (c-indent-level . 4) | |
109 (c-continued-statement-offset . 4) | |
110 (c-brace-offset . -4) | |
111 (c-argdecl-indent . 0) | |
112 (c-label-offset . -4) | |
113 (c-auto-newline . t)) | |
453 | 114 ("Whitesmith" |
115 (c-indent-level . 4) | |
116 (c-argdecl-indent . 4) | |
117 (c-brace-offset . 0) | |
118 (c-label-offset . -4) | |
119 (c-continued-statement-offset . 4)))) | |
120 | |
121 (defconst c-auto-newline nil | |
122 "*Non-nil means automatically newline before and after braces, | |
123 and after colons and semicolons, inserted in C code. | |
124 If you do not want a leading newline before braces then use: | |
1159 | 125 (define-key c-mode-map \"{\" 'electric-c-semi)") |
453 | 126 |
127 (defconst c-tab-always-indent t | |
128 "*Non-nil means TAB in C mode should always reindent the current line, | |
129 regardless of where in the line point is when the TAB command is used.") | |
130 | |
131 (defun c-mode () | |
132 "Major mode for editing C code. | |
133 Expression and list commands understand all C brackets. | |
134 Tab indents for C code. | |
135 Comments are delimited with /* ... */. | |
136 Paragraphs are separated by blank lines only. | |
137 Delete converts tabs to spaces as it moves back. | |
138 \\{c-mode-map} | |
139 Variables controlling indentation style: | |
140 c-tab-always-indent | |
141 Non-nil means TAB in C mode should always reindent the current line, | |
142 regardless of where in the line point is when the TAB command is used. | |
143 c-auto-newline | |
144 Non-nil means automatically newline before and after braces, | |
145 and after colons and semicolons, inserted in C code. | |
146 c-indent-level | |
147 Indentation of C statements within surrounding block. | |
148 The surrounding block's indentation is the indentation | |
149 of the line on which the open-brace appears. | |
150 c-continued-statement-offset | |
151 Extra indentation given to a substatement, such as the | |
152 then-clause of an if or body of a while. | |
153 c-continued-brace-offset | |
154 Extra indentation given to a brace that starts a substatement. | |
155 This is in addition to c-continued-statement-offset. | |
156 c-brace-offset | |
157 Extra indentation for line if it starts with an open brace. | |
158 c-brace-imaginary-offset | |
159 An open brace following other text is treated as if it were | |
160 this far to the right of the start of its line. | |
161 c-argdecl-indent | |
162 Indentation level of declarations of C function arguments. | |
163 c-label-offset | |
164 Extra indentation for line that is a label, or case or default. | |
165 | |
166 Settings for K&R and BSD indentation styles are | |
167 c-indent-level 5 8 | |
168 c-continued-statement-offset 5 8 | |
169 c-brace-offset -5 -8 | |
170 c-argdecl-indent 0 8 | |
171 c-label-offset -5 -8 | |
172 | |
173 Turning on C mode calls the value of the variable c-mode-hook with no args, | |
174 if that value is non-nil." | |
175 (interactive) | |
176 (kill-all-local-variables) | |
177 (use-local-map c-mode-map) | |
178 (setq major-mode 'c-mode) | |
179 (setq mode-name "C") | |
180 (setq local-abbrev-table c-mode-abbrev-table) | |
181 (set-syntax-table c-mode-syntax-table) | |
182 (make-local-variable 'paragraph-start) | |
183 (setq paragraph-start (concat "^$\\|" page-delimiter)) | |
184 (make-local-variable 'paragraph-separate) | |
185 (setq paragraph-separate paragraph-start) | |
186 (make-local-variable 'paragraph-ignore-fill-prefix) | |
187 (setq paragraph-ignore-fill-prefix t) | |
188 (make-local-variable 'indent-line-function) | |
189 (setq indent-line-function 'c-indent-line) | |
190 (make-local-variable 'indent-region-function) | |
191 (setq indent-region-function 'c-indent-region) | |
192 (make-local-variable 'require-final-newline) | |
193 (setq require-final-newline t) | |
194 (make-local-variable 'comment-start) | |
195 (setq comment-start "/* ") | |
196 (make-local-variable 'comment-end) | |
197 (setq comment-end " */") | |
198 (make-local-variable 'comment-column) | |
199 (setq comment-column 32) | |
200 (make-local-variable 'comment-start-skip) | |
201 (setq comment-start-skip "/\\*+ *") | |
202 (make-local-variable 'comment-indent-hook) | |
203 (setq comment-indent-hook 'c-comment-indent) | |
204 (make-local-variable 'parse-sexp-ignore-comments) | |
205 (setq parse-sexp-ignore-comments t) | |
206 (run-hooks 'c-mode-hook)) | |
207 | |
208 ;; This is used by indent-for-comment | |
209 ;; to decide how much to indent a comment in C code | |
210 ;; based on its context. | |
211 (defun c-comment-indent () | |
212 (if (looking-at "^/\\*") | |
213 0 ;Existing comment at bol stays there. | |
214 (let ((opoint (point))) | |
215 (save-excursion | |
216 (beginning-of-line) | |
217 (cond ((looking-at "[ \t]*}[ \t]*\\($\\|/\\*\\)") | |
218 ;; A comment following a solitary close-brace | |
219 ;; should have only one space. | |
220 (search-forward "}") | |
221 (1+ (current-column))) | |
222 ((or (looking-at "^#[ \t]*endif[ \t]*") | |
223 (looking-at "^#[ \t]*else[ \t]*")) | |
224 7) ;2 spaces after #endif | |
225 ((progn | |
226 (goto-char opoint) | |
227 (skip-chars-backward " \t") | |
228 (and (= comment-column 0) (bolp))) | |
229 ;; If comment-column is 0, and nothing but space | |
230 ;; before the comment, align it at 0 rather than 1. | |
231 0) | |
232 (t | |
233 (max (1+ (current-column)) ;Else indent at comment column | |
234 comment-column))))))) ; except leave at least one space. | |
235 | |
236 (defun c-fill-paragraph (&optional arg) | |
237 "Like \\[fill-paragraph] but handle C comments. | |
1081
4e7d09b779eb
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1064
diff
changeset
|
238 If any of the current line is a comment or within a comment, |
4e7d09b779eb
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1064
diff
changeset
|
239 fill the comment or the paragraph of it that point is in, |
4e7d09b779eb
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1064
diff
changeset
|
240 preserving the comment indentation or line-starting decorations." |
453 | 241 (interactive "P") |
1086
273918bf0a95
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1081
diff
changeset
|
242 (let* (comment-start-place |
273918bf0a95
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1081
diff
changeset
|
243 (first-line |
273918bf0a95
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1081
diff
changeset
|
244 ;; Check for obvious entry to comment. |
273918bf0a95
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1081
diff
changeset
|
245 (save-excursion |
273918bf0a95
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1081
diff
changeset
|
246 (beginning-of-line) |
273918bf0a95
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1081
diff
changeset
|
247 (skip-chars-forward " \t") |
273918bf0a95
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1081
diff
changeset
|
248 (and (looking-at comment-start-skip) |
273918bf0a95
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1081
diff
changeset
|
249 (setq comment-start-place (point)))))) |
453 | 250 (if (or first-line |
1064
e699ce19609f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1054
diff
changeset
|
251 ;; t if we enter a comment between start of function and this line. |
e699ce19609f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1054
diff
changeset
|
252 (eq (calculate-c-indent) t) |
1081
4e7d09b779eb
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1064
diff
changeset
|
253 ;; t if this line contains a comment starter. |
1181
5897b220ad8e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1177
diff
changeset
|
254 (setq first-line |
5897b220ad8e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1177
diff
changeset
|
255 (save-excursion (beginning-of-line) |
5897b220ad8e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1177
diff
changeset
|
256 (prog1 |
5897b220ad8e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1177
diff
changeset
|
257 (re-search-forward comment-start-skip |
5897b220ad8e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1177
diff
changeset
|
258 (save-excursion (end-of-line) |
5897b220ad8e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1177
diff
changeset
|
259 (point)) |
5897b220ad8e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1177
diff
changeset
|
260 t) |
5897b220ad8e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1177
diff
changeset
|
261 (setq comment-start-place (point)))))) |
453 | 262 ;; Inside a comment: fill one comment paragraph. |
263 (let ((fill-prefix | |
264 ;; The prefix for each line of this paragraph | |
265 ;; is the appropriate part of the start of this line, | |
266 ;; up to the column at which text should be indented. | |
267 (save-excursion | |
268 (beginning-of-line) | |
269 (if (looking-at "[ \t]*/\\*.*\\*/") | |
270 (progn (re-search-forward comment-start-skip) | |
271 (make-string (current-column) ?\ )) | |
272 (if first-line (forward-line 1)) | |
273 (buffer-substring (point) | |
274 (progn | |
275 (move-to-column | |
276 (calculate-c-indent-within-comment t) | |
277 t) | |
278 (point)))))) | |
279 (paragraph-start | |
280 ;; Lines containing just a comment start or just an end | |
281 ;; should not be filled into paragraphs they are next to. | |
282 (concat paragraph-start | |
283 "\\|^[ \t]*/\\*[ \t]*$\\|^[ \t]*\\*/[ \t]*$\\|^[^ \t/*]")) | |
284 (paragraph-separate | |
285 (concat paragraph-separate | |
1181
5897b220ad8e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1177
diff
changeset
|
286 "\\|^[ \t]*/\\*[ \t]*$\\|^[ \t]*\\*/[ \t]*$\\|^[^ \t/*]")) |
5897b220ad8e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1177
diff
changeset
|
287 (chars-to-delete 0)) |
453 | 288 (save-restriction |
289 ;; Don't fill the comment together with the code following it. | |
1086
273918bf0a95
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1081
diff
changeset
|
290 ;; So temporarily exclude everything before the comment start, |
273918bf0a95
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1081
diff
changeset
|
291 ;; and everything after the line where the comment ends. |
273918bf0a95
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1081
diff
changeset
|
292 ;; If comment-start-place is non-nil, the comment starter is there. |
273918bf0a95
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1081
diff
changeset
|
293 ;; Otherwise, point is inside the comment. |
1181
5897b220ad8e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1177
diff
changeset
|
294 (narrow-to-region (save-excursion |
5897b220ad8e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1177
diff
changeset
|
295 (if comment-start-place |
5897b220ad8e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1177
diff
changeset
|
296 (goto-char comment-start-place) |
5897b220ad8e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1177
diff
changeset
|
297 (search-backward "/*")) |
5897b220ad8e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1177
diff
changeset
|
298 ;; Protect text before the comment start |
5897b220ad8e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1177
diff
changeset
|
299 ;; by excluding it. Add spaces to bring back |
5897b220ad8e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1177
diff
changeset
|
300 ;; proper indentation of that point. |
5897b220ad8e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1177
diff
changeset
|
301 (let ((column (current-column))) |
5897b220ad8e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1177
diff
changeset
|
302 (prog1 (point) |
5897b220ad8e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1177
diff
changeset
|
303 (setq chars-to-delete column) |
5897b220ad8e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1177
diff
changeset
|
304 (insert-char ?\ column)))) |
453 | 305 (save-excursion |
1086
273918bf0a95
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1081
diff
changeset
|
306 (if comment-start-place |
273918bf0a95
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1081
diff
changeset
|
307 (goto-char (+ comment-start-place 2))) |
453 | 308 (search-forward "*/" nil 'move) |
309 (forward-line 1) | |
310 (point))) | |
1181
5897b220ad8e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1177
diff
changeset
|
311 |
453 | 312 (fill-paragraph arg) |
313 (save-excursion | |
1181
5897b220ad8e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1177
diff
changeset
|
314 ;; Delete the chars we inserted to avoid clobbering |
5897b220ad8e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1177
diff
changeset
|
315 ;; the stuff before the comment start. |
5897b220ad8e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1177
diff
changeset
|
316 (goto-char (point-min)) |
5897b220ad8e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1177
diff
changeset
|
317 (if (> chars-to-delete 0) |
5897b220ad8e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1177
diff
changeset
|
318 (delete-region (point) (+ (point) chars-to-delete))) |
1086
273918bf0a95
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1081
diff
changeset
|
319 ;; Find the comment ender (should be on last line of buffer, |
273918bf0a95
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1081
diff
changeset
|
320 ;; given the narrowing) and don't leave it on its own line. |
273918bf0a95
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1081
diff
changeset
|
321 (goto-char (point-max)) |
273918bf0a95
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1081
diff
changeset
|
322 (forward-line -1) |
453 | 323 (search-forward "*/") |
324 (beginning-of-line) | |
325 (if (looking-at "[ \t]*\\*/") | |
326 (delete-indentation))))) | |
327 ;; Outside of comments: do ordinary filling. | |
328 (fill-paragraph arg)))) | |
329 | |
330 (defun electric-c-brace (arg) | |
331 "Insert character and correct line's indentation." | |
332 (interactive "P") | |
333 (let (insertpos) | |
334 (if (and (not arg) | |
335 (eolp) | |
336 (or (save-excursion | |
337 (skip-chars-backward " \t") | |
338 (bolp)) | |
339 (if c-auto-newline (progn (c-indent-line) (newline) t) nil))) | |
340 (progn | |
341 (insert last-command-char) | |
342 (c-indent-line) | |
343 (if c-auto-newline | |
344 (progn | |
345 (newline) | |
346 ;; (newline) may have done auto-fill | |
347 (setq insertpos (- (point) 2)) | |
348 (c-indent-line))) | |
349 (save-excursion | |
350 (if insertpos (goto-char (1+ insertpos))) | |
351 (delete-char -1)))) | |
352 (if insertpos | |
353 (save-excursion | |
354 (goto-char insertpos) | |
355 (self-insert-command (prefix-numeric-value arg))) | |
356 (self-insert-command (prefix-numeric-value arg))))) | |
357 | |
358 (defun electric-c-sharp-sign (arg) | |
359 "Insert character and correct line's indentation." | |
360 (interactive "P") | |
361 (if (save-excursion | |
362 (skip-chars-backward " \t") | |
363 (bolp)) | |
364 (let ((c-auto-newline nil)) | |
365 (electric-c-terminator arg)) | |
366 (self-insert-command (prefix-numeric-value arg)))) | |
367 | |
368 (defun electric-c-semi (arg) | |
369 "Insert character and correct line's indentation." | |
370 (interactive "P") | |
371 (if c-auto-newline | |
372 (electric-c-terminator arg) | |
373 (self-insert-command (prefix-numeric-value arg)))) | |
374 | |
375 (defun electric-c-terminator (arg) | |
376 "Insert character and correct line's indentation." | |
377 (interactive "P") | |
378 (let (insertpos (end (point))) | |
379 (if (and (not arg) (eolp) | |
380 (not (save-excursion | |
381 (beginning-of-line) | |
382 (skip-chars-forward " \t") | |
383 (or (= (following-char) ?#) | |
384 ;; Colon is special only after a label, or case .... | |
385 ;; So quickly rule out most other uses of colon | |
386 ;; and do no indentation for them. | |
387 (and (eq last-command-char ?:) | |
388 (not (looking-at "case[ \t'/(]\\|default\\>")) | |
389 (save-excursion | |
390 (skip-chars-forward "a-zA-Z0-9_$") | |
391 (skip-chars-forward " \t") | |
392 (< (point) end))) | |
393 (progn | |
394 (beginning-of-defun) | |
395 (let ((pps (parse-partial-sexp (point) end))) | |
396 (or (nth 3 pps) (nth 4 pps) (nth 5 pps)))))))) | |
397 (progn | |
398 (insert last-command-char) | |
399 (c-indent-line) | |
400 (and c-auto-newline | |
401 (not (c-inside-parens-p)) | |
402 (progn | |
403 (newline) | |
404 ;; (newline) may have done auto-fill | |
405 (setq insertpos (- (point) 2)) | |
406 (c-indent-line))) | |
407 (save-excursion | |
408 (if insertpos (goto-char (1+ insertpos))) | |
409 (delete-char -1)))) | |
410 (if insertpos | |
411 (save-excursion | |
412 (goto-char insertpos) | |
413 (self-insert-command (prefix-numeric-value arg))) | |
414 (self-insert-command (prefix-numeric-value arg))))) | |
415 | |
416 (defun c-inside-parens-p () | |
417 (condition-case () | |
418 (save-excursion | |
419 (save-restriction | |
420 (narrow-to-region (point) | |
421 (progn (beginning-of-defun) (point))) | |
422 (goto-char (point-max)) | |
423 (= (char-after (or (scan-lists (point) -1 1) (point-min))) ?\())) | |
424 (error nil))) | |
425 | |
426 (defun c-indent-command (&optional whole-exp) | |
427 "Indent current line as C code, or in some cases insert a tab character. | |
428 If `c-tab-always-indent' is non-nil (the default), always indent current line. | |
429 Otherwise, indent the current line only if point is at the left margin or | |
430 in the line's indentation; otherwise insert a tab. | |
431 | |
432 A numeric argument, regardless of its value, means indent rigidly all the | |
433 lines of the expression starting after point so that this line becomes | |
434 properly indented. The relative indentation among the lines of the | |
435 expression are preserved." | |
436 (interactive "P") | |
437 (if whole-exp | |
438 ;; If arg, always indent this line as C | |
439 ;; and shift remaining lines of expression the same amount. | |
440 (let ((shift-amt (c-indent-line)) | |
441 beg end) | |
442 (save-excursion | |
443 (if c-tab-always-indent | |
444 (beginning-of-line)) | |
445 ;; Find beginning of following line. | |
446 (save-excursion | |
447 (forward-line 1) (setq beg (point))) | |
448 ;; Find first beginning-of-sexp for sexp extending past this line. | |
449 (while (< (point) beg) | |
450 (forward-sexp 1) | |
451 (setq end (point)) | |
452 (skip-chars-forward " \t\n"))) | |
453 (if (> end beg) | |
454 (indent-code-rigidly beg end shift-amt "#"))) | |
455 (if (and (not c-tab-always-indent) | |
456 (save-excursion | |
457 (skip-chars-backward " \t") | |
458 (not (bolp)))) | |
459 (insert-tab) | |
460 (c-indent-line)))) | |
461 | |
462 (defun c-indent-line () | |
463 "Indent current line as C code. | |
464 Return the amount the indentation changed by." | |
465 (let ((indent (calculate-c-indent nil)) | |
466 beg shift-amt | |
467 (case-fold-search nil) | |
468 (pos (- (point-max) (point)))) | |
469 (beginning-of-line) | |
470 (setq beg (point)) | |
471 (cond ((eq indent nil) | |
472 (setq indent (current-indentation))) | |
473 ((eq indent t) | |
474 (setq indent (calculate-c-indent-within-comment))) | |
475 ((looking-at "[ \t]*#") | |
476 (setq indent 0)) | |
477 (t | |
478 (skip-chars-forward " \t") | |
479 (if (listp indent) (setq indent (car indent))) | |
480 (cond ((or (looking-at "case[ \t'/(]\\|default\\>") | |
481 (and (looking-at "[A-Za-z]") | |
482 (save-excursion | |
483 (forward-sexp 1) | |
484 (looking-at ":")))) | |
485 (setq indent (max 1 (+ indent c-label-offset)))) | |
486 ((and (looking-at "else\\b") | |
487 (not (looking-at "else\\s_"))) | |
488 (setq indent (save-excursion | |
489 (c-backward-to-start-of-if) | |
490 (current-indentation)))) | |
491 ((looking-at "}[ \t]*else") | |
492 (setq indent (save-excursion | |
493 (forward-char) | |
494 (backward-sexp) | |
495 (current-indentation)))) | |
496 ((and (looking-at "while\\b") | |
497 (save-excursion | |
498 (c-backward-to-start-of-do))) | |
499 ;; This is a `while' that ends a do-while. | |
500 (setq indent (save-excursion | |
501 (c-backward-to-start-of-do) | |
502 (current-indentation)))) | |
503 ((= (following-char) ?}) | |
504 (setq indent (- indent c-indent-level))) | |
505 ((= (following-char) ?{) | |
506 (setq indent (+ indent c-brace-offset)))))) | |
507 (skip-chars-forward " \t") | |
508 (setq shift-amt (- indent (current-column))) | |
509 (if (zerop shift-amt) | |
510 (if (> (- (point-max) pos) (point)) | |
511 (goto-char (- (point-max) pos))) | |
512 (delete-region beg (point)) | |
513 (indent-to indent) | |
514 ;; If initial point was within line's indentation, | |
515 ;; position after the indentation. Else stay at same point in text. | |
516 (if (> (- (point-max) pos) (point)) | |
517 (goto-char (- (point-max) pos)))) | |
518 shift-amt)) | |
519 | |
520 (defun calculate-c-indent (&optional parse-start) | |
521 "Return appropriate indentation for current line as C code. | |
522 In usual case returns an integer: the column to indent to. | |
523 Returns nil if line starts inside a string, t if in a comment." | |
524 (save-excursion | |
525 (beginning-of-line) | |
526 (let ((indent-point (point)) | |
527 (case-fold-search nil) | |
528 state | |
529 containing-sexp) | |
530 (if parse-start | |
531 (goto-char parse-start) | |
532 (beginning-of-defun)) | |
533 (while (< (point) indent-point) | |
534 (setq parse-start (point)) | |
535 (setq state (parse-partial-sexp (point) indent-point 0)) | |
536 (setq containing-sexp (car (cdr state)))) | |
537 (cond ((or (nth 3 state) (nth 4 state)) | |
538 ;; return nil or t if should not change this line | |
539 (nth 4 state)) | |
540 ((null containing-sexp) | |
541 ;; Line is at top level. May be data or function definition, | |
542 ;; or may be function argument declaration. | |
543 ;; Indent like the previous top level line | |
544 ;; unless that ends in a closeparen without semicolon, | |
545 ;; in which case this line is the first argument decl. | |
546 (goto-char indent-point) | |
547 (skip-chars-forward " \t") | |
548 (if (= (following-char) ?{) | |
549 0 ; Unless it starts a function body | |
550 (c-backward-to-noncomment (or parse-start (point-min))) | |
551 ;; Look at previous line that's at column 0 | |
552 ;; to determine whether we are in top-level decls | |
553 ;; or function's arg decls. Set basic-indent accordingly. | |
554 (let ((basic-indent | |
555 (save-excursion | |
556 (re-search-backward "^[^ \^L\t\n#]" nil 'move) | |
759
58b7fc91b74a
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
662
diff
changeset
|
557 (let (comment lim) |
1064
e699ce19609f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1054
diff
changeset
|
558 ;; Recognize the DEFUN macro in Emacs. |
e699ce19609f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1054
diff
changeset
|
559 (if (save-excursion |
e699ce19609f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1054
diff
changeset
|
560 ;; Move down to the (putative) argnames line. |
1184 | 561 (while (and (not (eobp)) |
562 (not (looking-at " *[({}#/]"))) | |
1064
e699ce19609f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1054
diff
changeset
|
563 (forward-line 1)) |
e699ce19609f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1054
diff
changeset
|
564 ;; Go back to the DEFUN, if it is one. |
e699ce19609f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1054
diff
changeset
|
565 (condition-case nil |
e699ce19609f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1054
diff
changeset
|
566 (backward-sexp 1) |
e699ce19609f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1054
diff
changeset
|
567 (error)) |
e699ce19609f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1054
diff
changeset
|
568 (beginning-of-line) |
e699ce19609f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1054
diff
changeset
|
569 (looking-at "DEFUN\\b")) |
e699ce19609f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1054
diff
changeset
|
570 c-argdecl-indent |
e699ce19609f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1054
diff
changeset
|
571 (if (and (looking-at "\\sw\\|\\s_") |
1205
56d315ca10ee
(calculate-c-indent): When testing for function-start line,
Richard M. Stallman <rms@gnu.org>
parents:
1184
diff
changeset
|
572 ;; This is careful to stop at the first |
56d315ca10ee
(calculate-c-indent): When testing for function-start line,
Richard M. Stallman <rms@gnu.org>
parents:
1184
diff
changeset
|
573 ;; paren if we have |
56d315ca10ee
(calculate-c-indent): When testing for function-start line,
Richard M. Stallman <rms@gnu.org>
parents:
1184
diff
changeset
|
574 ;; int foo Proto ((int, int)); |
56d315ca10ee
(calculate-c-indent): When testing for function-start line,
Richard M. Stallman <rms@gnu.org>
parents:
1184
diff
changeset
|
575 (looking-at "[^\"\n=(]*(") |
1064
e699ce19609f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1054
diff
changeset
|
576 (progn |
e699ce19609f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1054
diff
changeset
|
577 (goto-char (1- (match-end 0))) |
e699ce19609f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1054
diff
changeset
|
578 (setq lim (point)) |
e699ce19609f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1054
diff
changeset
|
579 (condition-case nil |
e699ce19609f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1054
diff
changeset
|
580 (forward-sexp 1) |
e699ce19609f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1054
diff
changeset
|
581 (error)) |
e699ce19609f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1054
diff
changeset
|
582 (skip-chars-forward " \t\f") |
e699ce19609f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1054
diff
changeset
|
583 (and (< (point) indent-point) |
e699ce19609f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1054
diff
changeset
|
584 (not (memq (following-char) |
e699ce19609f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1054
diff
changeset
|
585 '(?\, ?\;))))) |
e699ce19609f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1054
diff
changeset
|
586 ;; Make sure the "function decl" we found |
e699ce19609f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1054
diff
changeset
|
587 ;; is not inside a comment. |
e699ce19609f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1054
diff
changeset
|
588 (progn |
e699ce19609f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1054
diff
changeset
|
589 (beginning-of-line) |
e699ce19609f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1054
diff
changeset
|
590 (while (and (not comment) |
e699ce19609f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1054
diff
changeset
|
591 (search-forward "/*" lim t)) |
e699ce19609f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1054
diff
changeset
|
592 (setq comment |
e699ce19609f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1054
diff
changeset
|
593 (not (search-forward "*/" lim t)))) |
e699ce19609f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1054
diff
changeset
|
594 (not comment))) |
e699ce19609f
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1054
diff
changeset
|
595 c-argdecl-indent 0)))))) |
453 | 596 basic-indent))) |
597 | |
598 ;; ;; Now add a little if this is a continuation line. | |
599 ;; (+ basic-indent (if (or (bobp) | |
600 ;; (memq (preceding-char) '(?\) ?\; ?\})) | |
601 ;; ;; Line with zero indentation | |
602 ;; ;; is probably the return-type | |
603 ;; ;; of a function definition, | |
604 ;; ;; so following line is function name. | |
605 ;; (= (current-indentation) 0)) | |
606 ;; 0 c-continued-statement-offset)) | |
607 | |
608 ((/= (char-after containing-sexp) ?{) | |
609 ;; line is expression, not statement: | |
610 ;; indent to just after the surrounding open. | |
611 (goto-char (1+ containing-sexp)) | |
612 (current-column)) | |
613 (t | |
614 ;; Statement level. Is it a continuation or a new statement? | |
615 ;; Find previous non-comment character. | |
616 (goto-char indent-point) | |
617 (c-backward-to-noncomment containing-sexp) | |
618 ;; Back up over label lines, since they don't | |
619 ;; affect whether our line is a continuation. | |
620 (while (or (eq (preceding-char) ?\,) | |
621 (and (eq (preceding-char) ?:) | |
622 (or (eq (char-after (- (point) 2)) ?\') | |
623 (memq (char-syntax (char-after (- (point) 2))) | |
624 '(?w ?_))))) | |
625 (if (eq (preceding-char) ?\,) | |
626 (progn (forward-char -1) | |
627 (c-backward-to-start-of-continued-exp containing-sexp))) | |
628 (beginning-of-line) | |
629 (c-backward-to-noncomment containing-sexp)) | |
630 ;; Check for a preprocessor statement or its continuation lines. | |
631 ;; Move back to end of previous non-preprocessor line. | |
632 (let ((found (point)) stop) | |
633 (while (not stop) | |
634 (cond ((save-excursion (end-of-line 0) | |
635 (= (preceding-char) ?\\)) | |
636 (end-of-line 0)) | |
637 ;; This line is not preceded by a backslash. | |
638 ;; So either it starts a preprocessor command | |
639 ;; or any following continuation lines | |
640 ;; should not be skipped. | |
641 ((progn (beginning-of-line) (= (following-char) ?#)) | |
642 (end-of-line 0) | |
643 (setq found (point))) | |
644 (t (setq stop t)))) | |
645 (goto-char found)) | |
646 ;; Now we get the answer. | |
647 (if (and (not (memq (preceding-char) '(nil ?\, ?\; ?\} ?\{))) | |
648 ;; But don't treat a line with a close-brace | |
649 ;; as a continuation. It is probably the | |
650 ;; end of an enum type declaration. | |
651 (save-excursion | |
652 (goto-char indent-point) | |
653 (skip-chars-forward " \t") | |
654 (not (= (following-char) ?})))) | |
655 ;; This line is continuation of preceding line's statement; | |
656 ;; indent c-continued-statement-offset more than the | |
657 ;; previous line of the statement. | |
658 (progn | |
659 (c-backward-to-start-of-continued-exp containing-sexp) | |
660 (+ c-continued-statement-offset (current-column) | |
661 (if (save-excursion (goto-char indent-point) | |
662 (skip-chars-forward " \t") | |
663 (eq (following-char) ?{)) | |
664 c-continued-brace-offset 0))) | |
665 ;; This line starts a new statement. | |
666 ;; Position following last unclosed open. | |
667 (goto-char containing-sexp) | |
668 ;; Is line first statement after an open-brace? | |
669 (or | |
670 ;; If no, find that first statement and indent like it. | |
671 (save-excursion | |
672 (forward-char 1) | |
673 (let ((colon-line-end 0)) | |
674 (while (progn (skip-chars-forward " \t\n") | |
675 (looking-at "#\\|/\\*\\|case[ \t\n'/(].*:\\|[a-zA-Z0-9_$]*:")) | |
676 ;; Skip over comments and labels following openbrace. | |
677 (cond ((= (following-char) ?\#) | |
678 (forward-line 1)) | |
679 ((= (following-char) ?\/) | |
680 (forward-char 2) | |
681 (search-forward "*/" nil 'move)) | |
682 ;; case or label: | |
683 (t | |
684 (save-excursion (end-of-line) | |
685 (setq colon-line-end (point))) | |
686 (search-forward ":")))) | |
687 ;; The first following code counts | |
688 ;; if it is before the line we want to indent. | |
689 (and (< (point) indent-point) | |
1054
f6f13367d93c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
909
diff
changeset
|
690 (- |
f6f13367d93c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
909
diff
changeset
|
691 (if (> colon-line-end (point)) |
f6f13367d93c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
909
diff
changeset
|
692 (- (current-indentation) c-label-offset) |
f6f13367d93c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
909
diff
changeset
|
693 (current-column)) |
f6f13367d93c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
909
diff
changeset
|
694 ;; If prev stmt starts with open-brace, that |
f6f13367d93c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
909
diff
changeset
|
695 ;; open brace was offset by c-brace-offset. |
f6f13367d93c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
909
diff
changeset
|
696 ;; Compensate to get the column where |
f6f13367d93c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
909
diff
changeset
|
697 ;; an ordinary statement would start. |
f6f13367d93c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
909
diff
changeset
|
698 (if (= (following-char) ?\{) c-brace-offset 0))))) |
453 | 699 ;; If no previous statement, |
700 ;; indent it relative to line brace is on. | |
701 ;; For open brace in column zero, don't let statement | |
702 ;; start there too. If c-indent-level is zero, | |
703 ;; use c-brace-offset + c-continued-statement-offset instead. | |
704 ;; For open-braces not the first thing in a line, | |
705 ;; add in c-brace-imaginary-offset. | |
706 (+ (if (and (bolp) (zerop c-indent-level)) | |
707 (+ c-brace-offset c-continued-statement-offset) | |
708 c-indent-level) | |
709 ;; Move back over whitespace before the openbrace. | |
710 ;; If openbrace is not first nonwhite thing on the line, | |
711 ;; add the c-brace-imaginary-offset. | |
712 (progn (skip-chars-backward " \t") | |
713 (if (bolp) 0 c-brace-imaginary-offset)) | |
714 ;; If the openbrace is preceded by a parenthesized exp, | |
715 ;; move to the beginning of that; | |
716 ;; possibly a different line | |
717 (progn | |
718 (if (eq (preceding-char) ?\)) | |
719 (forward-sexp -1)) | |
720 ;; Get initial indentation of the line we are on. | |
721 (current-indentation)))))))))) | |
722 | |
723 (defun calculate-c-indent-within-comment (&optional after-star) | |
724 "Return the indentation amount for line inside a block comment. | |
725 Optional arg AFTER-STAR means, if lines in the comment have a leading star, | |
726 return the indentation of the text that would follow this star." | |
727 (let (end star-start) | |
728 (save-excursion | |
729 (beginning-of-line) | |
730 (skip-chars-forward " \t") | |
731 (setq star-start (= (following-char) ?\*)) | |
732 (skip-chars-backward " \t\n") | |
733 (setq end (point)) | |
734 (beginning-of-line) | |
735 (skip-chars-forward " \t") | |
736 (if after-star | |
737 (and (looking-at "\\*") | |
738 (re-search-forward "\\*[ \t]*"))) | |
739 (and (re-search-forward "/\\*[ \t]*" end t) | |
740 star-start | |
741 (not after-star) | |
742 (goto-char (1+ (match-beginning 0)))) | |
743 (if (and (looking-at "[ \t]*$") (= (preceding-char) ?\*)) | |
744 (1+ (current-column)) | |
745 (current-column))))) | |
746 | |
747 | |
748 (defun c-backward-to-noncomment (lim) | |
749 (let (opoint stop) | |
750 (while (not stop) | |
751 (skip-chars-backward " \t\n\f" lim) | |
752 (setq opoint (point)) | |
753 (if (and (>= (point) (+ 2 lim)) | |
754 (save-excursion | |
755 (forward-char -2) | |
756 (looking-at "\\*/"))) | |
757 (search-backward "/*" lim 'move) | |
758 (setq stop (or (<= (point) lim) | |
759 (save-excursion | |
760 (beginning-of-line) | |
761 (skip-chars-forward " \t") | |
762 (not (looking-at "#"))))) | |
763 (or stop (beginning-of-line)))))) | |
764 | |
765 (defun c-backward-to-start-of-continued-exp (lim) | |
766 (if (memq (preceding-char) '(?\) ?\")) | |
767 (forward-sexp -1)) | |
768 (beginning-of-line) | |
769 (if (<= (point) lim) | |
770 (goto-char (1+ lim))) | |
771 (skip-chars-forward " \t")) | |
772 | |
773 (defun c-backward-to-start-of-if (&optional limit) | |
774 "Move to the start of the last \"unbalanced\" `if'." | |
775 (or limit (setq limit (save-excursion (beginning-of-defun) (point)))) | |
776 (let ((if-level 1) | |
777 (case-fold-search nil)) | |
778 (while (and (not (bobp)) (not (zerop if-level))) | |
779 (backward-sexp 1) | |
780 (cond ((looking-at "else\\b") | |
781 (setq if-level (1+ if-level))) | |
782 ((looking-at "if\\b") | |
783 (setq if-level (1- if-level))) | |
784 ((< (point) limit) | |
785 (setq if-level 0) | |
786 (goto-char limit)))))) | |
787 | |
788 (defun c-backward-to-start-of-do (&optional limit) | |
789 "If point follows a `do' statement, move to beginning of it and return t. | |
790 Otherwise return nil and don't move point." | |
791 (or limit (setq limit (save-excursion (beginning-of-defun) (point)))) | |
792 (let ((first t) | |
793 (startpos (point)) | |
794 (done nil)) | |
795 (while (not done) | |
796 (let ((next-start (point))) | |
797 (condition-case nil | |
798 ;; Move back one token or one brace or paren group. | |
799 (backward-sexp 1) | |
800 ;; If we find an open-brace, we lose. | |
801 (error (setq done 'fail))) | |
802 (if done | |
803 nil | |
804 ;; If we reached a `do', we win. | |
805 (if (looking-at "do\\b") | |
806 (setq done 'succeed) | |
807 ;; Otherwise, if we skipped a semicolon, we lose. | |
808 ;; (Exception: we can skip one semicolon before getting | |
809 ;; to a the last token of the statement, unless that token | |
810 ;; is a close brace.) | |
811 (if (save-excursion | |
812 (forward-sexp 1) | |
813 (or (and (not first) (= (preceding-char) ?})) | |
814 (search-forward ";" next-start t | |
815 (if (and first | |
816 (/= (preceding-char) ?})) | |
817 2 1)))) | |
818 (setq done 'fail) | |
819 (setq first nil) | |
820 ;; If we go too far back in the buffer, we lose. | |
821 (if (< (point) limit) | |
822 (setq done 'fail))))))) | |
823 (if (eq done 'succeed) | |
824 t | |
825 (goto-char startpos) | |
826 nil))) | |
827 | |
828 (defun c-beginning-of-statement (count) | |
829 "Go to the beginning of the innermost C statement. | |
830 With prefix arg, go back N - 1 statements. If already at the beginning of a | |
871
34b6b37092c7
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
861
diff
changeset
|
831 statement then go to the beginning of the preceeding one. |
34b6b37092c7
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
861
diff
changeset
|
832 If within a string or comment, move by sentences instead of statements." |
453 | 833 (interactive "p") |
871
34b6b37092c7
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
861
diff
changeset
|
834 (let ((here (point)) state) |
34b6b37092c7
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
861
diff
changeset
|
835 (save-excursion |
34b6b37092c7
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
861
diff
changeset
|
836 (beginning-of-defun) |
34b6b37092c7
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
861
diff
changeset
|
837 (setq state (parse-partial-sexp (point) here nil nil))) |
34b6b37092c7
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
861
diff
changeset
|
838 (if (or (nth 3 state) (nth 4 state)) |
34b6b37092c7
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
861
diff
changeset
|
839 (forward-sentence (- count)) |
34b6b37092c7
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
861
diff
changeset
|
840 (while (> count 0) |
34b6b37092c7
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
861
diff
changeset
|
841 (c-beginning-of-statement-1) |
34b6b37092c7
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
861
diff
changeset
|
842 (setq count (1- count))) |
34b6b37092c7
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
861
diff
changeset
|
843 (while (< count 0) |
34b6b37092c7
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
861
diff
changeset
|
844 (c-end-of-statement-1) |
34b6b37092c7
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
861
diff
changeset
|
845 (setq count (1+ count)))))) |
453 | 846 |
847 (defun c-end-of-statement (count) | |
848 "Go to the end of the innermost C statement. | |
871
34b6b37092c7
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
861
diff
changeset
|
849 With prefix arg, go forward N - 1 statements. |
34b6b37092c7
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
861
diff
changeset
|
850 Move forward to end of the next statement if already at end. |
34b6b37092c7
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
861
diff
changeset
|
851 If within a string or comment, move by sentences instead of statements." |
453 | 852 (interactive "p") |
853 (c-beginning-of-statement (- count))) | |
854 | |
855 (defun c-beginning-of-statement-1 () | |
856 (let ((last-begin (point)) | |
857 (first t)) | |
858 (condition-case () | |
859 (progn | |
860 (while (and (not (bobp)) | |
861 (progn | |
862 (backward-sexp 1) | |
863 (or first | |
864 (not (re-search-forward "[;{}]" last-begin t))))) | |
865 (setq last-begin (point) first nil)) | |
866 (goto-char last-begin)) | |
867 (error (if first (backward-up-list 1) (goto-char last-begin)))))) | |
868 | |
869 (defun c-end-of-statement-1 () | |
870 (condition-case () | |
871 (progn | |
872 (while (and (not (eobp)) | |
873 (let ((beg (point))) | |
874 (forward-sexp 1) | |
875 (let ((end (point))) | |
876 (save-excursion | |
877 (goto-char beg) | |
878 (not (re-search-forward "[;{}]" end t))))))) | |
879 (re-search-backward "[;}]") | |
880 (forward-char 1)) | |
881 (error | |
882 (let ((beg (point))) | |
883 (backward-up-list -1) | |
884 (let ((end (point))) | |
885 (goto-char beg) | |
886 (search-forward ";" end 'move)))))) | |
887 | |
888 (defun mark-c-function () | |
889 "Put mark at end of C function, point at beginning." | |
890 (interactive) | |
891 (push-mark (point)) | |
892 (end-of-defun) | |
893 (push-mark (point)) | |
894 (beginning-of-defun) | |
895 (backward-paragraph)) | |
896 | |
897 (defun indent-c-exp (&optional endpos) | |
898 "Indent each line of the C grouping following point. | |
899 If optional arg ENDPOS is given, indent each line, stopping when | |
900 ENDPOS is encountered." | |
901 (interactive) | |
902 (let* ((indent-stack (list nil)) | |
903 (opoint (point)) ;; May be altered below. | |
904 (contain-stack | |
905 (list (if endpos | |
906 (let (funbeg) | |
907 ;; Find previous fcn-start. | |
908 (save-excursion (forward-char 1) | |
909 (beginning-of-defun) | |
910 (setq funbeg (point))) | |
911 ;; Try to find containing open, | |
912 ;; but don't scan past that fcn-start. | |
913 (save-restriction | |
914 (narrow-to-region funbeg (point)) | |
915 (condition-case nil | |
916 (save-excursion | |
917 (backward-up-list 1) (point)) | |
918 ;; We gave up: must be between fcns. | |
919 ;; Set opoint to beg of prev fcn | |
920 ;; since otherwise calculate-c-indent | |
921 ;; will get wrong answers. | |
922 (error (setq opoint funbeg) | |
923 (point))))) | |
924 (point)))) | |
925 (case-fold-search nil) | |
926 restart outer-loop-done inner-loop-done state ostate | |
927 this-indent last-sexp | |
928 at-else at-brace at-while | |
929 last-depth | |
930 (next-depth 0)) | |
931 ;; If the braces don't match, get an error right away. | |
932 (save-excursion | |
933 (forward-sexp 1)) | |
934 ;; Realign the comment on the first line, even though we don't reindent it. | |
935 (save-excursion | |
936 (let ((beg (point))) | |
937 (and (re-search-forward | |
938 comment-start-skip | |
939 (save-excursion (end-of-line) (point)) t) | |
940 ;; Make sure the comment starter we found | |
941 ;; is not actually in a string or quoted. | |
942 (let ((new-state | |
943 (parse-partial-sexp beg (point) | |
944 nil nil state))) | |
945 (and (not (nth 3 new-state)) (not (nth 5 new-state)))) | |
946 (progn (indent-for-comment) (beginning-of-line))))) | |
947 (save-excursion | |
948 (setq outer-loop-done nil) | |
949 (while (and (not (eobp)) | |
950 (if endpos (< (point) endpos) | |
951 (not outer-loop-done))) | |
952 (setq last-depth next-depth) | |
953 ;; Compute how depth changes over this line | |
954 ;; plus enough other lines to get to one that | |
955 ;; does not end inside a comment or string. | |
956 ;; Meanwhile, do appropriate indentation on comment lines. | |
957 (setq inner-loop-done nil) | |
958 (while (and (not inner-loop-done) | |
959 (not (and (eobp) (setq outer-loop-done t)))) | |
960 (setq ostate state) | |
961 (setq state (parse-partial-sexp (point) (progn (end-of-line) (point)) | |
962 nil nil state)) | |
963 (setq next-depth (car state)) | |
964 (if (and (car (cdr (cdr state))) | |
965 (>= (car (cdr (cdr state))) 0)) | |
966 (setq last-sexp (car (cdr (cdr state))))) | |
967 (if (or (nth 4 ostate)) | |
968 (c-indent-line)) | |
969 (if (or (nth 3 state)) | |
970 (forward-line 1) | |
971 (setq inner-loop-done t))) | |
972 (and endpos | |
973 (while (< next-depth 0) | |
974 (setq indent-stack (append indent-stack (list nil))) | |
975 (setq contain-stack (append contain-stack (list nil))) | |
976 (setq next-depth (1+ next-depth)) | |
977 (setq last-depth (1+ last-depth)) | |
978 (setcar (nthcdr 6 state) (1+ (nth 6 state))))) | |
979 (setq outer-loop-done (and (not endpos) (<= next-depth 0))) | |
980 (if outer-loop-done | |
981 nil | |
982 ;; If this line had ..))) (((.. in it, pop out of the levels | |
983 ;; that ended anywhere in this line, even if the final depth | |
984 ;; doesn't indicate that they ended. | |
985 (while (> last-depth (nth 6 state)) | |
986 (setq indent-stack (cdr indent-stack) | |
987 contain-stack (cdr contain-stack) | |
988 last-depth (1- last-depth))) | |
989 (if (/= last-depth next-depth) | |
990 (setq last-sexp nil)) | |
991 ;; Add levels for any parens that were started in this line. | |
992 (while (< last-depth next-depth) | |
993 (setq indent-stack (cons nil indent-stack) | |
994 contain-stack (cons nil contain-stack) | |
995 last-depth (1+ last-depth))) | |
996 (if (null (car contain-stack)) | |
997 (setcar contain-stack (or (car (cdr state)) | |
998 (save-excursion (forward-sexp -1) | |
999 (point))))) | |
1000 (forward-line 1) | |
1001 (skip-chars-forward " \t") | |
1002 (if (eolp) | |
1003 nil | |
1004 (if (and (car indent-stack) | |
1005 (>= (car indent-stack) 0)) | |
1006 ;; Line is on an existing nesting level. | |
1007 ;; Lines inside parens are handled specially. | |
1008 (if (/= (char-after (car contain-stack)) ?{) | |
1009 (setq this-indent (car indent-stack)) | |
1010 ;; Line is at statement level. | |
1011 ;; Is it a new statement? Is it an else? | |
1012 ;; Find last non-comment character before this line | |
1013 (save-excursion | |
1014 (setq at-else (looking-at "else\\W")) | |
1015 (setq at-brace (= (following-char) ?{)) | |
1016 (setq at-while (looking-at "while\\b")) | |
1017 (c-backward-to-noncomment opoint) | |
1018 (if (not (memq (preceding-char) '(nil ?\, ?\; ?} ?: ?{))) | |
1019 ;; Preceding line did not end in comma or semi; | |
1020 ;; indent this line c-continued-statement-offset | |
1021 ;; more than previous. | |
1022 (progn | |
1023 (c-backward-to-start-of-continued-exp (car contain-stack)) | |
1024 (setq this-indent | |
1025 (+ c-continued-statement-offset (current-column) | |
1026 (if at-brace c-continued-brace-offset 0)))) | |
1027 ;; Preceding line ended in comma or semi; | |
1028 ;; use the standard indent for this level. | |
1029 (cond (at-else (progn (c-backward-to-start-of-if opoint) | |
1030 (setq this-indent | |
1031 (current-indentation)))) | |
1032 ((and at-while (c-backward-to-start-of-do opoint)) | |
1033 (setq this-indent (current-indentation))) | |
1034 (t (setq this-indent (car indent-stack))))))) | |
1035 ;; Just started a new nesting level. | |
1036 ;; Compute the standard indent for this level. | |
1037 (let ((val (calculate-c-indent | |
1038 (if (car indent-stack) | |
1039 (- (car indent-stack)) | |
1040 opoint)))) | |
1041 (setcar indent-stack | |
1042 (setq this-indent val)))) | |
1043 ;; Adjust line indentation according to its contents | |
1044 (if (or (looking-at "case[ \t'/(]\\|default\\>") | |
1045 (and (looking-at "[A-Za-z]") | |
1046 (save-excursion | |
1047 (forward-sexp 1) | |
1048 (looking-at ":")))) | |
1049 (setq this-indent (max 1 (+ this-indent c-label-offset)))) | |
1050 (if (= (following-char) ?}) | |
1051 (setq this-indent (- this-indent c-indent-level))) | |
1052 (if (= (following-char) ?{) | |
1053 (setq this-indent (+ this-indent c-brace-offset))) | |
1054 ;; Don't leave indentation in empty lines. | |
1055 (if (eolp) (setq this-indent 0)) | |
1056 ;; Put chosen indentation into effect. | |
1057 (or (= (current-column) this-indent) | |
1058 (= (following-char) ?\#) | |
1059 (progn | |
1060 (delete-region (point) (progn (beginning-of-line) (point))) | |
1061 (indent-to this-indent))) | |
1062 ;; Indent any comment following the text. | |
1063 (or (looking-at comment-start-skip) | |
1064 (let ((beg (point))) | |
1065 (and (re-search-forward | |
1066 comment-start-skip | |
1067 (save-excursion (end-of-line) (point)) t) | |
1068 ;; Make sure the comment starter we found | |
1069 ;; is not actually in a string or quoted. | |
1070 (let ((new-state | |
1071 (parse-partial-sexp beg (point) | |
1072 nil nil state))) | |
1073 (and (not (nth 3 new-state)) (not (nth 5 new-state)))) | |
1074 (progn (indent-for-comment) (beginning-of-line))))))))))) | |
1075 | |
1076 ;; Look at all comment-start strings in the current line after point. | |
1077 ;; Return t if one of them starts a real comment. | |
1078 ;; This is not used yet, because indent-for-comment | |
1079 ;; isn't smart enough to handle the cases this can find. | |
1080 (defun indent-c-find-real-comment () | |
1081 (let (win) | |
1082 (while (and (not win) | |
1083 (re-search-forward comment-start-skip | |
1084 (save-excursion (end-of-line) (point)) | |
1085 t)) | |
1086 ;; Make sure the comment start is not quoted. | |
1087 (let ((state-1 | |
1088 (parse-partial-sexp | |
1089 (save-excursion (beginning-of-line) (point)) | |
1090 (point) nil nil state))) | |
1091 (setq win (and (null (nth 3 state-1)) (null (nth 5 state-1)))))) | |
1092 win)) | |
1093 | |
1094 ;; Indent every line whose first char is between START and END inclusive. | |
1095 (defun c-indent-region (start end) | |
1096 (save-excursion | |
1097 (goto-char start) | |
1098 (let ((endmark (copy-marker end))) | |
1099 (and (bolp) (not (eolp)) | |
1100 (c-indent-line)) | |
1101 (indent-c-exp endmark) | |
1102 (set-marker endmark nil)))) | |
1103 | |
837
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1104 (defun set-c-style (style &optional global) |
453 | 1105 "Set C-mode variables to use one of several different indentation styles. |
837
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1106 The arguments are a string representing the desired style |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1107 and a flag which, if non-nil, means to set the style globally. |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1108 \(Interactively, the flag comes from the prefix argument.) |
861
345296f94a1e
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
1109 Available styles are GNU, K&R, BSD and Whitesmith." |
453 | 1110 (interactive (list (completing-read "Use which C indentation style? " |
837
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1111 c-style-alist nil t) |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1112 current-prefix-arg)) |
453 | 1113 (let ((vars (cdr (assoc style c-style-alist)))) |
837
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1114 (or vars |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1115 (error "Invalid C indentation style `%s'" style)) |
453 | 1116 (while vars |
837
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1117 (or global |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1118 (make-local-variable (car (car vars)))) |
453 | 1119 (set (car (car vars)) (cdr (car vars))) |
1120 (setq vars (cdr vars))))) | |
837
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1121 |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1122 ;;; This page handles insertion and removal of backslashes for C macros. |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1123 |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1124 (defvar c-backslash-column 48 |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1125 "*Minimum column for end-of-line backslashes of macro definitions.") |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1126 |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1127 (defun c-backslash-region (from to delete-flag) |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1128 "Insert, align, or delete end-of-line backslashes on the lines in the region. |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1129 With no argument, inserts backslashes and aligns existing backslashes. |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1130 With an argument, deletes the backslashes. |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1131 |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1132 This function does not modify the last line of the region if the region ends |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1133 right at the start of the following line; it does not modify blank lines |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1134 at the start of the region. So you can put the region around an entire macro |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1135 definition and conveniently use this command." |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1136 (interactive "r\nP") |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1137 (save-excursion |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1138 (goto-char from) |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1139 (let ((column c-backslash-column) |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1140 (endmark (make-marker))) |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1141 (move-marker endmark to) |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1142 ;; Compute the smallest column number past the ends of all the lines. |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1143 (if (not delete-flag) |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1144 (while (< (point) to) |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1145 (end-of-line) |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1146 (if (= (preceding-char) ?\\) |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1147 (progn (forward-char -1) |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1148 (skip-chars-backward " \t"))) |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1149 (setq column (max column (1+ (current-column)))) |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1150 (forward-line 1))) |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1151 ;; Adjust upward to a tab column, if that doesn't push past the margin. |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1152 (if (> (% column tab-width) 0) |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1153 (let ((adjusted (* (/ (+ column tab-width -1) tab-width) tab-width))) |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1154 (if (< adjusted (window-width)) |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1155 (setq column adjusted)))) |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1156 ;; Don't modify blank lines at start of region. |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1157 (goto-char from) |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1158 (while (and (< (point) endmark) (eolp)) |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1159 (forward-line 1)) |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1160 ;; Add or remove backslashes on all the lines. |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1161 (while (and (< (point) endmark) |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1162 ;; Don't backslashify the last line |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1163 ;; if the region ends right at the start of the next line. |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1164 (save-excursion |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1165 (forward-line 1) |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1166 (< (point) endmark))) |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1167 (if (not delete-flag) |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1168 (c-append-backslash column) |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1169 (c-delete-backslash)) |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1170 (forward-line 1)) |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1171 (move-marker endmark nil)))) |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1172 |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1173 (defun c-append-backslash (column) |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1174 (end-of-line) |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1175 ;; Note that "\\\\" is needed to get one backslash. |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1176 (if (= (preceding-char) ?\\) |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1177 (progn (forward-char -1) |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1178 (delete-horizontal-space) |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1179 (indent-to column)) |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1180 (indent-to column) |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1181 (insert "\\"))) |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1182 |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1183 (defun c-delete-backslash () |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1184 (end-of-line) |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1185 (forward-char -1) |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1186 (if (looking-at "\\\\") |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1187 (delete-region (1+ (point)) |
a8aef92e0025
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
807
diff
changeset
|
1188 (progn (skip-chars-backward " \t") (point))))) |
909
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1189 |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1190 (defun c-up-conditional (count) |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1191 "Move back to the containing preprocessor conditional, leaving mark behind. |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1192 A prefix argument acts as a repeat count. With a negative argument, |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1193 move forward to the end of the containing preprocessor conditional. |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1194 When going backwards, `#elif' is treated like `#else' followed by `#if'. |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1195 When going forwards, `#elif' is ignored." |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1196 (interactive "p") |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1197 (let* ((forward (< count 0)) |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1198 (increment (if forward -1 1)) |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1199 (search-function (if forward 're-search-forward 're-search-backward)) |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1200 (opoint (point)) |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1201 (new)) |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1202 (save-excursion |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1203 (while (/= count 0) |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1204 (if forward (end-of-line)) |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1205 (let ((depth 0) found) |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1206 (save-excursion |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1207 ;; Find the "next" significant line in the proper direction. |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1208 (while (and (not found) |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1209 ;; Rather than searching for a # sign that comes |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1210 ;; at the beginning of a line aside from whitespace, |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1211 ;; search first for a string starting with # sign. |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1212 ;; Then verify what precedes it. |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1213 ;; This is faster on account of the fastmap feature of |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1214 ;; the regexp matcher. |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1215 (funcall search-function |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1216 "#[ \t]*\\(if\\|elif\\|endif\\)" |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1217 nil t) |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1218 (progn |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1219 (beginning-of-line) |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1220 (looking-at "^[ \t]*#[ \t]*\\(if\\|elif\\|endif\\)"))) |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1221 ;; Update depth according to what we found. |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1222 (beginning-of-line) |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1223 (cond ((looking-at "[ \t]*#[ \t]*endif") |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1224 (setq depth (+ depth increment))) |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1225 ((looking-at "[ \t]*#[ \t]*elif") |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1226 (if (and forward (= depth 0)) |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1227 (setq found (point)))) |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1228 (t (setq depth (- depth increment)))) |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1229 ;; If this line exits a level of conditional, exit inner loop. |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1230 (if (< depth 0) |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1231 (setq found (point))) |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1232 ;; When searching forward, start from end of line |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1233 ;; so that we don't find the same line again. |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1234 (if forward (end-of-line)))) |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1235 (or found |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1236 (error "No containing preprocessor conditional")) |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1237 (goto-char (setq new found))) |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1238 (setq count (- count increment)))) |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1239 (push-mark) |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
871
diff
changeset
|
1240 (goto-char new))) |
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
472
diff
changeset
|
1241 |
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
472
diff
changeset
|
1242 ;;; c-mode.el ends here |