Current function name in R/S-Plus

Wright, Kevin kevin.wright at
Tue Jun 11 22:56:23 CEST 2002

I sometimes want to know the name of the function that I'm editing.  After
some searching on Google, it appears that "which-function-mode" can be used
with imenu.  Since R source files support imenu, this might be a way to
continuously see the function name in the modeline.

My modeline space is precious and also ess seems not to support imenu for S
source code files, so I spent the last hour hacking a function to print the
name of the current function to the minibuffer.  I bind the function to a

The function could be cleaned up a bit, but it works and that's enough for

I offer it to the ESS community.

Best, Kevin Wright

(defun ess-function-name ()
  "Print the name of the current function in the minibuffer.
   Based on ess-beginning-of-function"
  (let ((init-point (point))
        beg end done)
    ;; First search for the start of the function definition
    ;; in case we're sitting in a function header:
    (if (search-forward "(" (ess-line-end-position 2) t); at most end of
next line
        (forward-char 1))
    (while (not done)
      (if (re-search-backward ess-function-pattern (point-min) t)
        (goto-char init-point)
        (error "Point is not in a function."))
      (setq beg (point))
      ;; The point is now at the start of the function name
      (let (word regexp point end)
        (setq point (point))
        ; Look forward for one of '" ' '"_' '"<' ' ' '_ <' 
        (while (not (looking-at "\"*[ _<]"))
          (forward-char 1))
        (setq end (point))
        (goto-char point)
        ; Look backward for the start of function name, excluding quote
        (while (and (not (bobp)) (looking-at "\\w"))
          (backward-char 1))
        (or (looking-at "\\w")
            (forward-char 1))
        (and (= (point) end)
             (error "not looking at a word"))
        (setq word (buffer-substring (point) end))
        (goto-char init-point)
        (error word)
      ;; current function must begin and end around point
      (setq done (<= beg init-point)))
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