Skip to content

Indentation in R code

6 messages · Göran Broström, Seth Falcon, Peter Dalgaard +1 more

#
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.
 )

What is missing in the documentation in 'R-exts.pdf'? Or what have I misssed?

--
GÃ¶ran BrostrÃ¶m
#
On 24 Sep 2005, goran.brostrom at gmail.com wrote:

            
Not sure if this is the best way, but I have the following after
loading ess-site:

(setq ess-indent-level 4)


+ seth
#
Seth Falcon <sfalcon at fhcrc.org> writes:
I have (I believe it stems from Martin M. originally):

(add-hook 'c-mode-hook '(lambda()
                          (c-set-style "stroustrup")))
(add-hook 'ess-mode-hook
      '(lambda()
         (if (or (string< ess-version "5.0")
                 (string= ess-version "5.0"))
             (ess-set-style 'C++)
           (ess-set-style 'C++ 'quiet))

         (add-hook 'local-write-file-hooks
                   '(lambda()
                      (delete-trailing-whitespace)
                      ))
         ))
#
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> 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:
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
#
On Mon, Sep 26, 2005 at 09:27:56AM +0200, Martin Maechler wrote:
[...]

but this is not sufficient to get correct (4) indentation (ess) in R 
functions. We need some reference to ess as well, right? Maybe another
reference to the ESS manual is in order in 'R-exts'?

Thanks for all the help. I got it working now.

G??ran
1 day later
#
On Mon, Sep 26, 2005 at 07:05:33PM +0200, G??ran Brostr??m wrote:
Martin, I realize that it was stupid of me to cut here; I only refer to C
indentation, and forget that the lines of lisp code referring to ESS of 
course is necessary to get indentation working in R functions.

Once again, thanks for your good help.

G??ran