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