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