[ESS] [R] Indentation in R code

Martin Maechler maechler at stat.math.ethz.ch
Mon Sep 26 09:27:56 CEST 2005


I'm crossposting to the ESS-help mailing list which is slightly
more appropriate here.  [This may be a rare case where
crossposting seems to make much sense.]

>>>>> "PD" == Peter Dalgaard <p.dalgaard at biostat.ku.dk>
>>>>>     on 25 Sep 2005 19:40:45 +0200 writes:

    PD> Seth Falcon <sfalcon at fhcrc.org> writes:

    >> On 24 Sep 2005, goran.brostrom at gmail.com wrote:
    >> 
    >> > I am using emacs-21.3 when writing R functions on Linux debian, and
    >> > I am trying to follow the advice i R-exts.pdf (2.1.1) regarding
    >> > indentation. That is, I set 'c-default-style' to "bsd" and
    >> > 'c-basic-offset' to 4. However, while this gives me the intended
    >> > indentation in C code, it doesn't change the behavior in R code; I
    >> > still get an indentation of size 2. This is my .emacs file after
    >> > customization:
    >> >
    >> > (require 'ess-site)
    >> > (custom-set-variables
    >> >  ;; custom-set-variables was added by Custom -- don't edit or
    >> >  ;; cut/paste it!  Your init file should contain only one such
    >> >  ;; instance.
    >> >  '(c-basic-offset 4)
    >> >  '(c-default-style "bsd"))
    >> >  (custom-set-faces
    >> >  ;; custom-set-faces was added by Custom -- don't edit or cut/paste it!
    >> >  ;; Your init file should contain only one such instance.
    >> > )
    >> 
    >> Not sure if this is the best way, but I have the following after
    >> loading ess-site:
    >> 
    >> (setq ess-indent-level 4)



    PD> I have (I believe it stems from Martin M. originally):

yes, most probably  {IIRC, Kurt Hornik was involved too}.

    PD>      (add-hook 'c-mode-hook '(lambda()
    PD> 			       (c-set-style "stroustrup")))

the above is not quite what I have or did recommend, 
which is rather  "bsd" + "offset 4"  as Göran has above

In fact, Göran mentions the "R-exts" manual and that has
the following  *before* giving the emacs-lisp statements:

>> (For GNU Emacs 20: for GNU Emacs 21 use
>> customization to set the `c-default-style' to `"bsd"' and
>> `c-basic-offset' to `4'.)

and indeed, that's what Göran did and you should do with a
current emacs, either customize via GUI or,
in your ~/.emacs file, find the section '(custom-set-variables ...)' and add 
 '(c-basic-offset 4)
 '(c-default-style "bsd")

to the lines already there, or if there's no such section, add

(custom-set-variables
  ;; custom-set-variables was added by Custom -- don't edit or cut/paste it!
  ;; Your init file should contain only one such instance.
 '(c-basic-offset 4)
 '(c-default-style "bsd")
)
to the end of your ~/.emacs file

    PD>      (add-hook 'ess-mode-hook
    PD> 	   '(lambda()
    PD> 	      (if (or (string< ess-version "5.0")
    PD> 		      (string= ess-version "5.0"))
    PD> 		  (ess-set-style 'C++)
    PD> 		(ess-set-style 'C++ 'quiet))
    PD> 
    PD> 	      (add-hook 'local-write-file-hooks
    PD> 			'(lambda()
    PD> 			   (delete-trailing-whitespace)
    PD> 			   ))
    PD> 	      ))

yes; using  (add-hook .......) is really more clean than first
requiring ess-site.
Also, since nowadays I assume everyone has an ess-version >= 5, 
the above becomes simply

    (add-hook 'ess-mode-hook
              '(lambda()
		  (ess-set-style 'C++ 'quiet)
		  (add-hook 'local-write-file-hooks
			    '(lambda()
			       (delete-trailing-whitespace)))))

Note that this has the standard e-lisp function
'delete-trailing-whitespace' which is simpler but also less
flexible than the 'ess-nuke-trailing-whitespace' which we've had
there.

Martin Maechler, ETH Zurich




More information about the ESS-help mailing list