[comp.emacs] Yet another indentation etc. style

HEFFNER@psi.wharton.upenn.edu ("S. F. Heffner") (12/24/90)

Our C programming shop uses an indent of 4 to mean a change of logic level, and
an indent of 2 to mean continuation of a statement.  E.g. (silly C, to show
effect of our convention):

	if (a == 1 || a == 2 || a == 3 ||
	  a == 4 || a == 5 || a == 6 ||
	  a == 7)				/*if a has interesting value*/
	    {
	    do_something();			/*do something*/
	    do_more();				/*do more*/
	    }					/*"a has interesting value"*/
	keep_going();				/*keep going*/

We feel that an indent of more than 4 for a logic level eats up too much real
estate horizontally (we comment almost every line), and an indent of less than
4 doesn't show strongly enough graphically (and wouldn't allow for a contin-
uation indent). 

We style curly braces the way we do because we think they are important enough
to take up the vertical real estate, and that the style graphically delineates
the logic well.  I know, I know...de gustibus non disputandem est. We DON'T use
"{}" only if the consequence will be a single statement. 

Note the way we repeat a comment, in quotes, to say what the "}" is closing.
This pays off especially when we have a whole cascade of "}". Following is a
little Elisp function I did to do this; it even removes a leading "if " if
present, as shown above.  Not exactly "electric" C; more steam-driven... 8^) 
I have this little function bound to one of our "private prefix" key combin-
ations; I can hit it after entering the comment for an if, for, do, or while.
But if the consequence will be a single statement, I don't bother.

By the way, c-mode.el doesn't support our style very well, even tuning the
various variables available.  I have started to hack c-mode.el a bit; I hope to
convince it sometime soon to accommodate our braces style and continuation
indentation. 

We are especially interested in  fine points of program style because we have
developed an expert system that manipulates code in a variety of assembly and
3GL languages; it performs translation to other languages (including assemblers
to C!), analysis, standardization, and code improvement.  Since our expert
system produces source code, it must concern itself with style etc. 

This probably isn't the right forum for all this...

Stephen F. (Steve) Heffner, Pennington Systems Inc.             -----------
Internet:  heffner@psi.wharton.upenn.edu                       | From C to |
Phone:     609-737-2727                                        | shining C |
FAX:       609-737-2728                                         -----------
Snail:     65 S. Main Street, Bldg B, Pennington, NJ 08534 USA

Cut here -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8< -- 8<

(defun png-do-braces()
"Insert {} for C consequence block; copy comment surrounded by double quotes
to }, deleting leading 'if ' if present; position for block.  Assumes you are
positioned after the '/*' starting the preceding comment but before any other
comment; e.g. at '^' in the following:

	if (a < b)				/*if no room*/^

In this case, after executing the function, you will be at '^' in the
following:

	if (a < b)				/*if no room*/
	    {
	    ^
	    }					/*\"no room\"*/

"

  (interactive)
  (search-backward "/*")			;;; if/for... ^/*comment*/
  (forward-char 2)				;;; /*^comment*/
  (set-mark-command nil)			;;; set mark
  (search-forward "*/")				;;; /*comment*/^
  (backward-char 2)				;;; /*comment^*/
  (copy-region-as-kill (mark) (point))		;;; save comment as kill
  (end-of-line nil)				;;; /*comment*/^
  (newline-and-indent)				;;; open line, indent
  (insert "{")					;;; opening brace
  (newline-and-indent)				;;; open line, indent
  (insert "}")					;;; closing brace
  (indent-for-comment)				;;; }.../*^*/
  (insert "\"")					;;; }.../*"^*/
  (yank)					;;; }.../*"comment^*/
  (insert "\"")					;;; }.../*"comment"^*/
  (search-backward "/*")			;;; }...^/*"comment"*/
  (forward-char 3)				;;; }.../*"^comment"*/
  (if						;;; if..
      (string-equal
          (buffer-substring (point) (+ (point) 3)) ;;;..cmnt starts..
	  "if "					;;; ..w/"if "
      )
      (delete-char 3)				;;; ditch it
  )						;;; "cmnt starts w/if"
  (beginning-of-line nil)			;;; to line start
  (open-line 1)					;;; open line for block
  (c-indent-command)				;;; indent for it
)