Thursday Aug 10, 2006

Sun C style indentation for [X]Emacs cc-mode

Ever since joining Sun a wonderful number of years ago, seven of them,
I have been brainwashing myself to write only C in Sun's C-style. This
post tells part of what I believe in, maybe that also tells you
why I love Python syntax. It was a an uphill struggle because COBOL
from college popped into mind uncomfortably often.

Over the years I have found that, readability of a source base
increases by magnitudes if there is a consistent style followed
throughout. ON code follows Sun-cstyle. Regardless of how much I
hated not being allowed to do my own thing, it is natural now to
expect 4 space intent on a statement continuation and to expect
a 4 space intent to be a continuation.

Being a sideways member of the church of Emacs (XEmacs is what I put
in between keyboard and libraries or syscalls) indentation is a solved
problem for me. Without more verbiage, here it is:


;;
;; Define "sun" mode to get the continuation line indentation
;; proper, ie. sun-like, in cc-mode
;;
(defun c-sun-get-continuation-proper (langelem)
  (save-excursion
    (goto-char (cdr langelem))
    (let ((current-syntax (caar (c-guess-basic-syntax))))
      (if (or (eq current-syntax 'statement)
	      (eq current-syntax 'statement-block-intro))
	  4 0))))

(c-add-style
 "sun"
 '((c-basic-offset . 8)
   (c-comment-only-line-offset . 0)
   (c-offsets-alist . ((statement-block-intro . +)
		       (knr-argdecl-intro . +)
		       (substatement-open . 0)
		       (substatement-label . 0)
		       (arglist-cont . c-sun-get-continuation-proper)
		       (arglist-cont-nonempty . c-sun-get-continuation-proper)
		       (arglist-close . 4)
		       (arglist-intro . 4)
		       (statement-cont . 4)
		       (defun-block-intro . +)
		       ))))

(setq c-default-style '((cc-mode "sun") (java-mode . "java") (other . "gnu")))

;; end "sun" cc-mode stuff

I have corrected this whenever I found an indentation which didn't
match Sun's c-style. Let me know if you decide to use this minor tweak
and finds a problem.

Now for the next problem of correcting c-style errors on an existing
file. elisp to the rescue - autocstyling!. But the code I have isn't
release ready yet. Let me tell you even the hotch-potch job I have now
is extremely usable. Editor wars or IDEs, if a tool for daily use
can't be tweaked and extended programatically - nwat good I say.
About

binujp

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today