diff options
| author | Richard M. Stallman | 1995-01-12 22:45:50 +0000 |
|---|---|---|
| committer | Richard M. Stallman | 1995-01-12 22:45:50 +0000 |
| commit | 73dae9a9d5ff375412a6b23a6e3ba018b24d39bc (patch) | |
| tree | 34f0b349d786ab0c52a39023008730449cae486f | |
| parent | 1103950c980791dbb0173747fe6792a4edebbb62 (diff) | |
| download | emacs-73dae9a9d5ff375412a6b23a6e3ba018b24d39bc.tar.gz emacs-73dae9a9d5ff375412a6b23a6e3ba018b24d39bc.zip | |
(indent-c-exp): Handle `{ if (x)\n foo;\n bar;' case.
| -rw-r--r-- | lisp/progmodes/c-mode.el | 97 |
1 files changed, 53 insertions, 44 deletions
diff --git a/lisp/progmodes/c-mode.el b/lisp/progmodes/c-mode.el index bbd077fd232..2529106e7b1 100644 --- a/lisp/progmodes/c-mode.el +++ b/lisp/progmodes/c-mode.el | |||
| @@ -1182,57 +1182,66 @@ If within a string or comment, move by sentences instead of statements." | |||
| 1182 | ;; past the region.) | 1182 | ;; past the region.) |
| 1183 | (if (or (eolp) (and endpos (>= (point) endpos))) | 1183 | (if (or (eolp) (and endpos (>= (point) endpos))) |
| 1184 | nil | 1184 | nil |
| 1185 | ;; Is this line in a new nesting level? | ||
| 1186 | ;; In other words, is this the first line that | ||
| 1187 | ;; starts in the new level? | ||
| 1185 | (if (and (car indent-stack) | 1188 | (if (and (car indent-stack) |
| 1186 | (>= (car indent-stack) 0)) | 1189 | (>= (car indent-stack) 0)) |
| 1187 | ;; Line is on an existing nesting level. | 1190 | nil |
| 1188 | ;; Lines inside parens are handled specially. | 1191 | ;; Yes. |
| 1189 | (if (/= (char-after (car contain-stack)) ?{) | ||
| 1190 | (setq this-indent (car indent-stack)) | ||
| 1191 | ;; Line is at statement level. | ||
| 1192 | ;; Is it a new statement? Is it an else? | ||
| 1193 | ;; Find last non-comment character before this line | ||
| 1194 | (save-excursion | ||
| 1195 | (setq this-point (point)) | ||
| 1196 | (setq at-else (and (looking-at "else\\b") | ||
| 1197 | (not (looking-at "else\\s_")))) | ||
| 1198 | (setq at-brace (= (following-char) ?{)) | ||
| 1199 | (setq at-while (and (looking-at "while\\b") | ||
| 1200 | (not (looking-at "while\\s_")))) | ||
| 1201 | (if (= (following-char) ?}) | ||
| 1202 | (setq this-indent (car indent-stack)) | ||
| 1203 | (c-backward-to-noncomment opoint) | ||
| 1204 | (if (not (memq (preceding-char) '(0 ?\, ?\; ?} ?: ?{))) | ||
| 1205 | ;; Preceding line did not end in comma or semi; | ||
| 1206 | ;; indent this line c-continued-statement-offset | ||
| 1207 | ;; more than previous. | ||
| 1208 | (progn | ||
| 1209 | (c-backward-to-start-of-continued-exp (car contain-stack)) | ||
| 1210 | (setq this-indent | ||
| 1211 | (+ c-continued-statement-offset (current-column) | ||
| 1212 | (if at-brace c-continued-brace-offset 0)))) | ||
| 1213 | ;; Preceding line ended in comma or semi; | ||
| 1214 | ;; use the standard indent for this level. | ||
| 1215 | (cond (at-else (progn (c-backward-to-start-of-if opoint) | ||
| 1216 | (setq this-indent | ||
| 1217 | (current-indentation)))) | ||
| 1218 | ((and at-while (c-backward-to-start-of-do opoint)) | ||
| 1219 | (setq this-indent (current-indentation))) | ||
| 1220 | ((eq (preceding-char) ?\,) | ||
| 1221 | (goto-char this-point) | ||
| 1222 | (setq this-indent (calculate-c-indent))) | ||
| 1223 | (t (setq this-indent (car indent-stack)))))))) | ||
| 1224 | ;; Just started a new nesting level. | ||
| 1225 | ;; Compute the standard indent for this level. | 1192 | ;; Compute the standard indent for this level. |
| 1226 | (let ((val (calculate-c-indent | 1193 | (let (val) |
| 1227 | (if (car indent-stack) | 1194 | (if (= (char-after (car contain-stack)) ?{) |
| 1228 | (- (car indent-stack)) | 1195 | (save-excursion |
| 1229 | opoint)))) | 1196 | (goto-char (car contain-stack)) |
| 1197 | (setq val (+ c-indent-level (current-column)))) | ||
| 1198 | (setq val (calculate-c-indent | ||
| 1199 | (if (car indent-stack) | ||
| 1200 | (- (car indent-stack)) | ||
| 1201 | opoint)))) | ||
| 1230 | ;; t means we are in a block comment and should | 1202 | ;; t means we are in a block comment and should |
| 1231 | ;; calculate accordingly. | 1203 | ;; calculate accordingly. |
| 1232 | (if (eq val t) | 1204 | (if (eq val t) |
| 1233 | (setq val (calculate-c-indent-within-comment))) | 1205 | (setq val (calculate-c-indent-within-comment))) |
| 1234 | (setcar indent-stack | 1206 | (setcar indent-stack val))) |
| 1235 | (setq this-indent val)))) | 1207 | ;; Adjust indent of this individual line |
| 1208 | ;; based on its predecessor. | ||
| 1209 | ;; Handle continuation lines, if, else, while, and so on. | ||
| 1210 | (if (/= (char-after (car contain-stack)) ?{) | ||
| 1211 | (setq this-indent (car indent-stack)) | ||
| 1212 | ;; Line is at statement level. | ||
| 1213 | ;; Is it a new statement? Is it an else? | ||
| 1214 | ;; Find last non-comment character before this line | ||
| 1215 | (save-excursion | ||
| 1216 | (setq this-point (point)) | ||
| 1217 | (setq at-else (and (looking-at "else\\b") | ||
| 1218 | (not (looking-at "else\\s_")))) | ||
| 1219 | (setq at-brace (= (following-char) ?{)) | ||
| 1220 | (setq at-while (and (looking-at "while\\b") | ||
| 1221 | (not (looking-at "while\\s_")))) | ||
| 1222 | (if (= (following-char) ?}) | ||
| 1223 | (setq this-indent (car indent-stack)) | ||
| 1224 | (c-backward-to-noncomment opoint) | ||
| 1225 | (if (not (memq (preceding-char) '(0 ?\, ?\; ?} ?: ?{))) | ||
| 1226 | ;; Preceding line did not end in comma or semi; | ||
| 1227 | ;; indent this line c-continued-statement-offset | ||
| 1228 | ;; more than previous. | ||
| 1229 | (progn | ||
| 1230 | (c-backward-to-start-of-continued-exp (car contain-stack)) | ||
| 1231 | (setq this-indent | ||
| 1232 | (+ c-continued-statement-offset (current-column) | ||
| 1233 | (if at-brace c-continued-brace-offset 0)))) | ||
| 1234 | ;; Preceding line ended in comma or semi; | ||
| 1235 | ;; use the standard indent for this level. | ||
| 1236 | (cond (at-else (progn (c-backward-to-start-of-if opoint) | ||
| 1237 | (setq this-indent | ||
| 1238 | (current-indentation)))) | ||
| 1239 | ((and at-while (c-backward-to-start-of-do opoint)) | ||
| 1240 | (setq this-indent (current-indentation))) | ||
| 1241 | ((eq (preceding-char) ?\,) | ||
| 1242 | (goto-char this-point) | ||
| 1243 | (setq this-indent (calculate-c-indent))) | ||
| 1244 | (t (setq this-indent (car indent-stack)))))))) | ||
| 1236 | ;; Adjust line indentation according to its contents | 1245 | ;; Adjust line indentation according to its contents |
| 1237 | (if (or (looking-at c-switch-label-regexp) | 1246 | (if (or (looking-at c-switch-label-regexp) |
| 1238 | (and (looking-at "[A-Za-z]") | 1247 | (and (looking-at "[A-Za-z]") |