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