Regexp for imenu / adding ChangeLog entries.

Stephen Eglen eglen at
Mon Jan 14 23:48:20 CET 2002

To follow up on a couple of related threads:

 Martin said:
 > The real problem with the imenu regexp is that it's not
 > compatible with, the Emacs syntax table or at least font-lock's
 > behavior, and IMO the real bug of ess-imenu's regexp is that
 > mylongnamedfunction.Method <-
 >    function(x, y, ....) {
 > }
 > is not `seen' by the current regexp!
 > So, yes, we *do* have to fix this, and may also allow the
 > VERY-UGLY-no-whitespace assignment arrow.

How about changing the imenu regexp to the following:

  (setq imenu-generic-expression
	 '( (nil "^\\(.+\\)\\s-*<-[ \t\n]*function" 1)))

(this matches the case of no whitespace around "<-"; I agree with
Martin that adding space around the operator is preferred.  If you
want to enforce this constraint of extra whitespace, change the two
instances of * to +.)

This matches the case above when the word function is on the next
line.  The change is simply that after matching <-, we match for [
\t\n]* rather than [\\s-]*.  (Note that the whitespace syntax class
does not include \n, this instead means "end comment" in ESS S/R

Assuming that regexp is okay, I think we can re-use this regexp for
the  query I raised today:

 > Has anyone got Emacs to work so that the "add-log" functions (such as
 > "C-x 4 a" for writing ChangeLog entries) can figure out the name of
 > the current function within an .R buffer?  There are a couple of
 > variables that can be set:
 >   add-log-current-defun-function
 >   add-log-current-defun-header-regexp

The following code works nicely for me; in an .R buffer, if I now do
C-x 4 a, the current function name gets correctly inserted into the
ChangeLog file.

(add-hook 'ess-mode-hook 'my-ess-hook) 
(defun my-ess-hook ()
  "my ESS hook"
  (set (make-local-variable 'add-log-current-defun-header-regexp )
       "^\\(.+\\)\\s-*<-[ \t\n]*function"))

I've been testing it and found one little quirk.  Imagine the
following function in a file called ms_funs.r.  

corr.get.means <- function(dvi) {

  ## case A: point on this line.

  corr.get.means.helper <- function(x) {
    ## Helper function to create  mean and sd of one set of distances.
    indexes <- which(dvi[,1] == x)
    ## case B: point on this line.
    c(x, mean(dvi[indexes,2]), sd(dvi[indexes,2]), length(indexes))
  d.uniq <- sort(unique(dvi[,1]))
  ## case C: point on this line.
  means <- t(sapply(d.uniq, corr.get.means.helper))
  colnames(means) <- c("dist", "mean", "sd", "n")

If I place the point on either line A, B, or C, and then do C-x 4 a
(add-change-log-entry-other-window) I get the following results in the

2002-01-14  Stephen Eglen  <eglen at>
	* ms_funs.r (corr.get.means): case A: finds top level function okay.
	(corr.get.means.helper): case B; finds helper function.
	(corr.get.means.helper): case C; even though oustide finder, still
	finds the helper function.

case C is arguably wrong.  The changelog code simply looks back to
find the first previous line that matches the regexp.  But that can't
be helped I think.  If case C annoys, the workaround for now is to
move to the start of the function (using ess-beginning-of-function).


ess-help mailing list -- To (un)subscribe, send
subscribe	or	unsubscribe
(in the "body", not the subject !)  To: ess-help-request at

More information about the ESS-help mailing list