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