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