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