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