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