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