[ESS] Yet another indentation question: indentation after parenthese

Mathieu Basille basille at ase-research.org
Fri Dec 16 19:23:42 CET 2011


Alright, I think I start to understand the problem, and what seemed easy to
me might not be possible. I thought that taking parentheses into account
would be easier than ignoring them, thus my question!

I will stick to 'C++' or 'RRR' styles and manually adjust them when needed
(using e.g. 'source' with 'options(keep.source = FALSE)'). This makes,
however, the use of 'auto-fill-mode' virtually impossible to me, because
such statements:

toto <- someFunction(arg1 = bli, arg2 = bla, arg3 = blo, arg2 = c("a",
"bla", c(1, 2, 3, 4), "blo", "blu", "bli"))

will render awfully with a column width of 70. I can live with this (well,
I did for many years now!), I just hoped that there was a way to follow
what R uses internally to format R code. I find it a pity that such a great
editor like Emacs (which is, by far, the best tool I tried to edit R/Rnw
scripts, with the result that I couldn't work without it now) can't
reproduce R behaviour.

Vitalie and Richard, many thanks go to you for your efforts to 1)
understand (!) and 2) trying to find a solution to my little problems!

All the best,
Mathieu.


Le 16/12/2011 13:01, Vitalie Spinu a écrit :
> 
> 
> It might not be possible to achieve what you want. Basically you are
> asking to ignore all "(" in indentation. I believe that R is doing so
> for two reasons, first it's easier to compute, second it consumes less
> horizontal space. The problem is that it's not necessary easier to read.
> 
> I agree that it would be nice if ESS would support this style. May be
> some day it will support it. For time being you should probably stick to
> what the most people use, i.e. C++ or RRR styles.
> 
> Vitalie.
> 
> 
> Mathieu Basille <basille at ase-research.org> writes:
> 
>> Until now, I did it interactively for a given buffer (M-x ess-set-style
>> RET RRR) or globally by adding in my .emacs (I always start with an
>> empty emacs) as recommended in the R manual:
>>
>> (add-hook 'ess-mode-hook
>> 	  (lambda ()
>> 	    (ess-set-style 'RRR 'quiet)
>> 	    (add-hook 'local-write-file-hooks
>> 		      (lambda ()
>> 			(ess-nuke-trailing-whitespace)))))
>> (setq ess-nuke-trailing-whitespace-p t)
>>
>> I thus tried, again with an empty .emacs, the sequence you just
>> described. It added in the custom variables:
>>
>> (custom-set-variables
>>  '(ess-default-style (quote RRR)))
>>
>> But still, nothing changed for the open-parenthesis behaviour: I still
>> have indentation after the open-parenthesis. And I can confirm that, in
>> all cases, 'ess-style' is set to 'RRR' for the buffer (I checked with
>> 'C-h v ess-style').
>>
>> Once again, thanks for your efforts. I'm still feeling like I'm doing
>> something wrong, but can't see what...
>> Mathieu.
>>
>> Le 16/12/2011 11:49, Richard M. Heiberger a écrit :
>>> How did you change the variable?
>>>  
>>> The correct way is to use customize.
>>>  
>>> C-h v ess-default-style <RET>
>>>
>>> middle-click on the word customize
>>> click on the button Value menu
>>> Click Common R
>>> click Save for future sessions
>>> click Exit
>>> close emacs
>>> start a new emacs session
>>>  
>>>
>>>  
>>> On Fri, Dec 16, 2011 at 11:39 AM, Mathieu Basille
>>> <basille at ase-research.org <mailto:basille at ase-research.org>> wrote:
>>>
>>>     Dear Richard,
>>>
>>>     Thanks for your answer! I didn't know about the RRR style, which was not
>>>     described in the R manual at
>>>     http://cran.r-project.org/doc/manuals/R-ints.html#R-coding-standards>     (!). It seems that RRR does what I want to do. And my problem seemed to
>>>     lie in 'ess-arg-function-offset', which indents the statements at
>>>     open-parenthesis if it is not set to a number.
>>>
>>>     However, to my surprise, using RRR style did not change anything to my
>>>     problem. I then tried with an empty .emacs (which should thus use the
>>>     default style, with 'ess-arg-function-offset' set to 2), and my problem
>>>     was still not solved. I also tried to manually set every indentation
>>>     variable ('ess-indent-level', 'ess-continued-statement-offset',
>>>     'ess-brace-offset', 'ess-arg-function-offset', 'ess-expression-offset',
>>>     'ess-else-offset', 'ess-close-brace-offset') to 4, and it did not change
>>>     anything either to this problem... Of course, I checked the value of the
>>>     style and each variable with 'C-h v', and they are set up as expected.
>>>     All the time, the indentation (with C-M-q or TAB) ends up at the
>>>     open-parenthesis.
>>>
>>>     I'm more lost than ever... Is there any reason that the indentation to
>>>     the open-parenthesis of 'ess-arg-function-offset' (if this is the
>>>     correct variable) would prevail against any user setting? Or am I
>>>     missing something again?
>>>
>>>     I deeply thank you for your efforts. I wanted to achieve what I thought
>>>     to be a simple task, i.e. using R formatting style as used by the R
>>>     parser, and I realise it is not so easy.
>>>
>>>     All the best,
>>>     Mathieu.
>>>
>>>
>>>     PS: If it is of any relevance, I have the same behaviour with ESS 5.14-1
>>>     (Debian Testing) and 5.11-1 (Debian Stable).
>>>
>>>
>>>
>>>     Le 16/12/2011 10:38, Richard M. Heiberger a écrit :
>>>     > Mathieu,
>>>     >
>>>     > ESS has several options on indenting code.
>>>     > Please look at ess-style-alist in file ess-custom.el and then try the
>>>     > styles out.
>>>     > My guess is that style RRR is the one you are looking for.
>>>     > If you need something else, then you can probably figure out how
>>>     to set
>>>     > the parameters
>>>     > for your needs.  When you have what you want, give it a name and
>>>     send it
>>>     > to us for
>>>     > inclusion in the next release.
>>>     >
>>>     > Rich
>>>     >
>>>     > On Fri, Dec 16, 2011 at 9:01 AM, Mathieu Basille
>>>     > <basille at ase-research.org <mailto:basille at ase-research.org>
>>>     <mailto:basille at ase-research.org <mailto:basille at ase-research.org>>>
>>>     wrote:
>>>     >
>>>     >     Le 16/12/2011 08:49, Vitalie Spinu a écrit :
>>>     >     > Mathieu Basille <basille at ase-research.org
>>>     <mailto:basille at ase-research.org>
>>>     >     <mailto:basille at ase-research.org
>>>     <mailto:basille at ase-research.org>>> writes:
>>>     >     >
>>>     >     >> Dear Vitalie,
>>>     >     >>
>>>     >     >> Thanks for you comment. My first message was not perfectly
>>>     >     clear... Your
>>>     >     >> example with 'foo' is misleading, since foo is exactly 3
>>>     >     character long.
>>>     >     >> Take this silly example instead:
>>>     >     >>
>>>     >     >> foo <- function(aaaaaaaaaa = 1, bbbbbbbbbb = 2, cccccccccc = 3,
>>>     >     >>                 dddddddddd = 4, eeeeeeeeee = 5)
>>>     >     >> {
>>>     >     >>     return(data.frame(aaaaaaaaaa, bbbbbbbbbb, cccccccccc,
>>>     dddddddddd,
>>>     >     >>                       eeeeeeeeee))
>>>     >     >> }
>>>     >     >>
>>>     >     >> The new line formatted by ESS starts just after the opening
>>>     >     parenthesis.
>>>     >     >> If I save the file in bla.R and source it with 'keep.source =
>>>     >     FALSE' to
>>>     >     >> use the R parser, here is what I get:
>>>     >     >>
>>>     >     >>> options(keep.source = FALSE)
>>>     >     >>> source("bla.R")
>>>     >     >>> foo
>>>     >     >> function (aaaaaaaaaa = 1, bbbbbbbbbb = 2, cccccccccc = 3,
>>>     >     dddddddddd = 4,
>>>     >     >>     eeeeeeeeee = 5)
>>>     >     >> {
>>>     >     >>     return(data.frame(aaaaaaaaaa, bbbbbbbbbb, cccccccccc,
>>>     dddddddddd,
>>>     >     >>         eeeeeeeeee))
>>>     >     >> }
>>>     >     >
>>>     >     > Ok, I got it. Check the keep.source option:
>>>     >     >
>>>     >     > options("keep.source")
>>>     >     >
>>>     >     > If it's FALSE I am getting your output and this is the
>>>     problem of
>>>     >     R, ESS
>>>     >     > cannot easily help you here. In my setup  keep.source is T,
>>>     so I am
>>>     >     > getting everything ok.
>>>     >
>>>     >     It is OK from an ESS perspective only. From a R perspective,
>>>     it is not.
>>>     >     The main problem is that R and ESS do not format code the same
>>>     way, and
>>>     >     I'd like to use R formatting, not ESS. The 'keep.source' option is
>>>     >     usually set to TRUE, but is generally set to FALSE for
>>>     packages (which
>>>     >     makes formatting of R code consistent). Using 'keep.source =
>>>     TRUE' does
>>>     >     not format at all the code; what the user wrote is totally
>>>     kept as-is.
>>>     >     Basically, I'd like to write code which follows the rules of R
>>>     itself.
>>>     >     In my opinion, this is a problem of ESS, not R!
>>>     >
>>>     >     Is there really no possibility to get the R behaviour here? What I
>>>     >     normally do is 1) write my functions, 2) source them using
>>>     'keep.source
>>>     >     = FALSE' to use the R parser, and 3) paste them back in my
>>>     .R/Rnw file.
>>>     >     But this is messy since it then conflicts with ESS style (e.g.
>>>     every
>>>     >     time I add a new line, or hit C-M-q, the formatting gets
>>>     changed back to
>>>     >     ESS rules)...
>>>     >
>>>     >     Thanks for your answer,
>>>     >     Mathieu.
>>>     >
>>>     >
>>>     >     > HTH,
>>>     >     > Vitalie.
>>>     >     >
>>>     >     >>
>>>     >     >> That is, the new line simply adds 4 spaces from the left
>>>     margin (not
>>>     >     >> from the opening parenthesis, which, I agree, would make no
>>>     >     sense) and
>>>     >     >> do not start after the opening parenthesis. There is two
>>>     reasons
>>>     >     I would
>>>     >     >> like to have this behaviour with ESS: 1) first, it would not
>>>     >     change the
>>>     >     >> formatting of my functions if I source them with the R parser,
>>>     >     and 2) it
>>>     >     >> can be a real source of nightmares when there are many nested
>>>     >     >> parentheses, and everything gets pushed to the right.
>>>     >     >>
>>>     >     >> All the best,
>>>     >     >> Mathieu.
>>>     >     >>
>>>     >     >> Le 16/12/2011 06:32, Vitalie Spinu a écrit :
>>>     >     >>> Mathieu Basille <basille at ase-research.org
>>>     <mailto:basille at ase-research.org>
>>>     >     <mailto:basille at ase-research.org
>>>     <mailto:basille at ase-research.org>>> writes:
>>>     >     >>>
>>>     >     >>>> Dear ESS gurus,
>>>     >     >>>>
>>>     >     >>>> I feel like I'm really missing something... I searched
>>>     heavily
>>>     >     on the net, in
>>>     >     >>>> the ESS manual, on this list, without finding what I'm
>>>     looking
>>>     >     >> for. Basically,
>>>     >     >>>> when I try to indent some code (with M-C-q), if a line break
>>>     >     occurs in the
>>>     >     >>>> middle of parentheses or brackets, the next line will be
>>>     >     indented to the
>>>     >     >> first
>>>     >     >>>> character after the parenthesis. How can I get rid of
>>>     this, and
>>>     >     get a
>>>     >     >> normal' 4
>>>     >     >>>> spaces indentation? (this is, I think, the behaviour of the R
>>>     >     parser)
>>>     >     >>>>
>>>     >     >>>
>>>     >     >>> I am not really understanding that. You mean that
>>>     >     >>>
>>>     >     >>>
>>>     >     >>>     foo(arg1 = 1, arg2 = 2,
>>>     >     >>>         arg3 = 3)
>>>     >     >>>
>>>     >     >>> Is not what you want? and you would like:
>>>     >     >>>
>>>     >     >>>
>>>     >     >>>     foo(arg1 = 1, arg2 = 2,
>>>     >     >>>             arg3 = 3)
>>>     >     >>>
>>>     >     >>> instead?
>>>     >     >>>
>>>     >     >>> Doesn't make sense to me.
>>>     >     >>>
>>>     >     >>> Vitalie.
>>>     >
>>>     >     --
>>>     >
>>>     >     ~$ whoami
>>>     >     Mathieu Basille, Post-Doc
>>>     >
>>>     >     ~$ locate
>>>     >     Laboratoire d'Écologie Comportementale et de Conservation de
>>>     la Faune
>>>     >     + Centre d'Étude de la Forêt
>>>     >     Département de Biologie
>>>     >     Université Laval, Québec
>>>     >
>>>     >     ~$ info
>>>     >     http://ase-research.org/basille>     >
>>>     >     ~$ fortune
>>>     >     ``If you can't win by reason, go for volume.''
>>>     >     Calvin, by Bill Watterson.
>>>     >
>>>     >     ______________________________________________
>>>     >     ESS-help at r-project.org <mailto:ESS-help at r-project.org>
>>>     <mailto:ESS-help at r-project.org <mailto:ESS-help at r-project.org>>
>>>     mailing list
>>>     >     https://stat.ethz.ch/mailman/listinfo/ess-help>     >
>>>     >
>>>
>>>     --
>>>
>>>     ~$ whoami
>>>     Mathieu Basille, Post-Doc
>>>
>>>     ~$ locate
>>>     Laboratoire d'Écologie Comportementale et de Conservation de la Faune
>>>     + Centre d'Étude de la Forêt
>>>     Département de Biologie
>>>     Université Laval, Québec
>>>
>>>     ~$ info
>>>     http://ase-research.org/basille> 
>>>     ~$ fortune
>>>     ``If you can't win by reason, go for volume.''
>>>     Calvin, by Bill Watterson.
>>>
>>>

-- 

~$ whoami
Mathieu Basille, Post-Doc

~$ locate
Laboratoire d'Écologie Comportementale et de Conservation de la Faune
+ Centre d'Étude de la Forêt
Département de Biologie
Université Laval, Québec

~$ info
http://ase-research.org/basille

~$ fortune
``If you can't win by reason, go for volume.''
Calvin, by Bill Watterson.



More information about the ESS-help mailing list