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