Skip to content

code indentation

I use emacs for reading and writing code. I try to follow a subset of the GNU coding style. One annoyance with the GNU style is the block indentation. I find it very difficult to follow the 2-space width of the block and in my humble opinion, it is unneccesary. I like to begin at the same column as the previous line starting column.

That is, instead of:

if ()
  {
      ....;
      ....;
  }

I prefer:

if ()
{
  ....;
  ....;
}

I have also tried many other styles like the K&R style, linux-kernel style etc, but have found that some of the spacing guidelines very difficult to follow.

Thanks to the c-mode in GNU Emacs, it is very easy to follow just about any custom style. One has to define the rules clearly and program it into emacs. Emacs defines each syntax element style very clearly. The manual has all the gory details.


(defconst vu3rdd-c-style
  '((c-basic-offset . 2)
    (tab-width . 4)
    (indent-tabs-mode . nil)
    (c-comment-only-line-offset . 0)
    (c-hanging-braces-alist . ((substatement-open before after)))
    (c-offsets-alist . ((topmost-intro . 0)
			(substatement . +)
			(substatement-open . 0)
			(case-label . +)
			(access-label . -)
			(inclass . ++)
			(inline-open . 0)
			))
    )
  ) 

(c-add-style "vu3rddstyle" vu3rdd-c-style nil)

(defun vu3rdd-c-hook ()
(c-set-style "vu3rddstyle"))

(add-hook 'c-mode-common-hook 'vu3rdd-c-hook)

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*