[ESS] C-c C-c on a short function

Martin Maechler maechler at stat.math.ethz.ch
Wed Jun 25 09:19:11 CEST 2008


>>>>> "TS" == tyler  <tyler.smith at mail.mcgill.ca>
>>>>>     on Fri, 20 Jun 2008 09:11:08 -0300 writes:

    TS> markleeds at verizon.net writes:
    >> When I use control cc on a function such as 

 mindiff <- function(df) df[which.min(df$diff), ]

    >> only the
    >> 
    >> mindiff <- function(df) df
    >> 
    >> part gets recognized by R so the function does nothing.
    >> 
    >> But, if I put squiggly brackets around the same function
    >> , as in
    >> 
    >> mindiff <- function(df) { df[which.min(df$diff), ] }
    >> 
    >> then everything works fine. If this not something that
    >> can be fixed by setting ESS options, then that's okay and
    >> I'll always put the squiggly brackets. But, if it can be
    >> fixed and someone knows what to put in my init.el, that
    >> would be great. thanks.

    TS> After a quick scan of the code I couldn't find a proper
    TS> fix, but if your short function is only one line long
    TS> you can use C-c C-n to send it. If it's more than one
    TS> line, and there's a blank line after it, you can send it
    TS> with C-c C-p.

    TS> HTH,

well... not too much in my view.

This is actually a very old "infelicity" in basic ESS.
It all amounts to what the emacs-lisp function
   ess-end-of-function  {in  <ESS>/lisp/ess-mode.el }
achieves.
And what it does it basically the following algorithm:

 1) jump backwards to the beginning of the function
     {and that also is not perfect in complicated S4
      setMethod(.) settings , but that's a different story}
 2) jump forwards over the argument list
 3) jump forward one (Syntax-specific) "SEXP"; for R this
    means typically means  '{ ... }'

Now we could add a to the above algorithm an

 4) jump forward to end of line

But that would be a bit a kludge, since
in this situation 

f1 <- function(x) be.friendly(x, force=TRUE); f2 <- function(x,y) x*sin(pi*x)

it would do "the wrong thing" and jump over both function definitions.
Also, step '4)' still would not help in a case like

mindiff <- function(df) df[which.min(df$diff),
                           which.max(df$daff)]

Now these examples suggest to me
to use
   4')  if(not at end of line)  jump-forward another SEXP

This may be a real improvement to the current behavior,
but I am sure there will still be cases where it won't work
perfectly.

What do others think?

Martin




More information about the ESS-help mailing list