Article View: gnu.emacs.help
Article #188798Re: C mode indention variables
From: Miles Bader
Date: Wed, 10 Jul 1991 15:41
Date: Wed, 10 Jul 1991 15:41
236 lines
9732 bytes
9732 bytes
Here's a patch that allows a more sane switch-indenting style, by adding the
variable c-switch-body-offset (so there are two additive indent amounts
inside a switch, one for everything, and one following case labels). {Of
course since I only use *my* indenting style [roughly k&r], I can't guarantee
this won't screw up some other bizarre ones [like the default!]}
Other changes are gratuitous, like using "defvar" for the variables instead
of "defconst"!
-Miles
*** /usr/local/lib/emacs/lisp/c-mode.el Sat Feb 24 16:59:26 1990
--- c-mode.el Fri Apr 7 00:41:33 1989
***************
*** 61,87 ****
(modify-syntax-entry ?| "." c-mode-syntax-table)
(modify-syntax-entry ?\' "\"" c-mode-syntax-table))
! (defconst c-indent-level 2
"*Indentation of C statements with respect to containing block.")
! (defconst c-brace-imaginary-offset 0
"*Imagined indentation of a C open brace that actually follows a statement.")
! (defconst c-brace-offset 0
"*Extra indentation for braces, compared with other text in same context.")
! (defconst c-argdecl-indent 5
"*Indentation level of declarations of C function arguments.")
! (defconst c-label-offset -2
"*Offset of C label lines and case statements relative to usual indentation.")
! (defconst c-continued-statement-offset 2
"*Extra indent for lines not starting new statements.")
! (defconst c-continued-brace-offset 0
"*Extra indent for substatements that start with open-braces.
This is in addition to c-continued-statement-offset.")
! (defconst c-auto-newline nil
"*Non-nil means automatically newline before and after braces,
and after colons and semicolons, inserted in C code.")
! (defconst c-tab-always-indent t
"*Non-nil means TAB in C mode should always reindent the current line,
regardless of where in the line point is when the TAB command is used.")
--- 61,90 ----
(modify-syntax-entry ?| "." c-mode-syntax-table)
(modify-syntax-entry ?\' "\"" c-mode-syntax-table))
! (defvar c-indent-level 2
"*Indentation of C statements with respect to containing block.")
! (defvar c-brace-imaginary-offset 0
"*Imagined indentation of a C open brace that actually follows a statement.")
! (defvar c-brace-offset 0
"*Extra indentation for braces, compared with other text in same context.")
! (defvar c-argdecl-indent 5
"*Indentation level of declarations of C function arguments.")
! (defvar c-label-offset -2
"*Offset of C label lines and case statements relative to usual indentation.")
! (defvar c-continued-statement-offset 2
"*Extra indent for lines not starting new statements.")
! (defvar c-continued-brace-offset 0
"*Extra indent for substatements that start with open-braces.
This is in addition to c-continued-statement-offset.")
! (defvar c-switch-body-offset 0
! "*Extra indentation for statements in body of a switch statement.")
!
! (defvar c-auto-newline nil
"*Non-nil means automatically newline before and after braces,
and after colons and semicolons, inserted in C code.")
! (defvar c-tab-always-indent t
"*Non-nil means TAB in C mode should always reindent the current line,
regardless of where in the line point is when the TAB command is used.")
***************
*** 115,120 ****
--- 118,126 ----
c-brace-imaginary-offset
An open brace following other text is treated as if it were
this far to the right of the start of its line.
+ c-switch-body-offset
+ Indent statements in the body of a switch statement this much
+ more than normal.
c-argdecl-indent
Indentation level of declarations of C function arguments.
c-label-offset
***************
*** 126,131 ****
--- 132,138 ----
c-brace-offset -5 -8
c-argdecl-indent 0 8
c-label-offset -5 -8
+ c-switch-body-offset 5 8
Turning on C mode calls the value of the variable c-mode-hook with no args,
if that value is non-nil."
***************
*** 257,263 ****
(error nil)))
(defun c-indent-command (&optional whole-exp)
- (interactive "P")
"Indent current line as C code, or in some cases insert a tab character.
If c-tab-always-indent is non-nil (the default), always indent current line.
Otherwise, indent the current line only if point is at the left margin
--- 264,269 ----
***************
*** 267,272 ****
--- 273,281 ----
means indent rigidly all the lines of the expression starting after point
so that this line becomes properly indented.
The relative indentation among the lines of the expression are preserved."
+
+ (interactive "P")
+
(if whole-exp
;; If arg, always indent this line as C
;; and shift remaining lines of expression the same amount.
***************
*** 320,326 ****
(c-backward-to-start-of-if)
(current-indentation))))
((= (following-char) ?})
! (setq indent (- indent c-indent-level)))
((= (following-char) ?{)
(setq indent (+ indent c-brace-offset))))))
(skip-chars-forward " \t")
--- 329,339 ----
(c-backward-to-start-of-if)
(current-indentation))))
((= (following-char) ?})
! (save-excursion
! (forward-char 1)
! (forward-sexp -1)
! (c-backward-before-brace)
! (setq indent (current-indentation))))
((= (following-char) ?{)
(setq indent (+ indent c-brace-offset))))))
(skip-chars-forward " \t")
***************
*** 336,341 ****
--- 349,371 ----
(goto-char (- (point-max) pos))))
shift-amt))
+ (defun c-backward-skip-ws ()
+ (skip-chars-backward " \t\n\f")
+ (and (save-excursion
+ (forward-char -2)
+ (looking-at "\\*/"))
+ (search-backward "/*" nil t)))
+
+ ;;; If the openbrace is preceded by a c control-structure
+ ;;; move to the beginning of that; possibly a different line
+ (defun c-backward-before-brace ()
+ (c-backward-skip-ws)
+ (cond ((eq (preceding-char) ?\))
+ (forward-sexp -1)
+ (c-backward-skip-ws)
+ ;; go to start of statement word (if, switch, etc)
+ (forward-word -1))))
+
(defun calculate-c-indent (&optional parse-start)
"Return appropriate indentation for current line as C code.
In usual case returns an integer: the column to indent to.
***************
*** 447,452 ****
--- 477,483 ----
(if (> colon-line-end (point))
(- (current-indentation) c-label-offset)
(current-column)))))
+
;; If no previous statement,
;; indent it relative to line brace is on.
;; For open brace in column zero, don't let statement
***************
*** 457,476 ****
(+ (if (and (bolp) (zerop c-indent-level))
(+ c-brace-offset c-continued-statement-offset)
c-indent-level)
! ;; Move back over whitespace before the openbrace.
! ;; If openbrace is not first nonwhite thing on the line,
! ;; add the c-brace-imaginary-offset.
! (progn (skip-chars-backward " \t")
! (if (bolp) 0 c-brace-imaginary-offset))
! ;; If the openbrace is preceded by a parenthesized exp,
! ;; move to the beginning of that;
! ;; possibly a different line
(progn
! (if (eq (preceding-char) ?\))
! (forward-sexp -1))
! ;; Get initial indentation of the line we are on.
! (current-indentation))))))))))
(defun calculate-c-indent-within-comment ()
"Return the indentation amount for line, assuming that
the current line is to be regarded as part of a block comment."
--- 488,502 ----
(+ (if (and (bolp) (zerop c-indent-level))
(+ c-brace-offset c-continued-statement-offset)
c-indent-level)
!
(progn
! (c-backward-before-brace)
+ (+ (current-indentation)
+ (if (looking-at "switch\\b")
+ c-switch-body-offset
+ 0)))))))))))
+
(defun calculate-c-indent-within-comment ()
"Return the indentation amount for line, assuming that
the current line is to be regarded as part of a block comment."
***************
*** 643,649 ****
(looking-at ":"))))
(setq this-indent (max 1 (+ this-indent c-label-offset))))
(if (= (following-char) ?})
! (setq this-indent (- this-indent c-indent-level)))
(if (= (following-char) ?{)
(setq this-indent (+ this-indent c-brace-offset)))
;; Put chosen indentation into effect.
--- 669,679 ----
(looking-at ":"))))
(setq this-indent (max 1 (+ this-indent c-label-offset))))
(if (= (following-char) ?})
! (save-excursion
! (forward-char 1)
! (forward-sexp -1)
! (c-backward-before-brace)
! (setq this-indent (current-indentation))))
(if (= (following-char) ?{)
(setq this-indent (+ this-indent c-brace-offset)))
;; Put chosen indentation into effect.
--
--
Miles Bader -- HCRC, University of Edinburgh -- Miles.Bader@ed.ac.uk
Technical Details
Message-ID:
<MILES.91Jul10164159@scott.cogsci.ed.ac.uk>Path:
arc1-ca.pugleaf.net!gmdzi!unido!mcsun!ukc!edcastle!edcogsci!cogsci!milesReferences:
<1991Jul8.194451.20300@uokmax.ecn.uoknor.edu>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103b8b,bfe9e4da7be79631 X-Google-Attributes: gid103b8b,public X-Google-ArrivalTime: 1991-07-10 23:20:52 PST Path: arc1-ca.pugleaf.net!gmdzi!unido!mcsun!ukc!edcastle!edcogsci!cogsci!miles From: miles@cogsci.ed.ac.uk (Miles Bader) Newsgroups: gnu.emacs.help Subject: Re: C mode indention variables Message-ID: <MILES.91Jul10164159@scott.cogsci.ed.ac.uk> Date: 10 Jul 91 15:41:59 GMT References: <1991Jul8.194451.20300@uokmax.ecn.uoknor.edu> Sender: miles@cogsci.ed.ac.uk Organization: HCRC, University of Edinburgh Lines: 236 In-reply-to: randy@uokmax.ecn.uoknor.edu's message of 8 Jul 91 19:44:51 GMT