2009-01-31

which function is this?

As a short tip: in many of the programming modes, you can get the name of the function where your cursor is currently in using which-function-mode. You can set it globally in your .emacs, or make it mode specific using some hook, for example:
(add-hook 'c-mode-common-hook 
  (lambda ()
    (which-function-mode t)))
After you do this, the name of the current function will appear in the modeline (the status-bar).

Now, apart from the mode line, emacs also knows the concept of the header line. This is the first line of the display; it seems little used. However, if you'd like to have the function name there, maybe because the mode line is full already, see this trick on EmacsWiki (scroll down).

which-function-mode does not work for all modes; it needs some help from the major mode to figure out what counts as a function; in my emacs, the following modes work (you can check from the which-func-modes variable):

(emacs-lisp-mode c-mode c++-mode perl-mode cperl-mode python-mode makefile-mode sh-mode
 fortran-mode f90-mode ada-mode diff-mode)

I hope your own functions do fit on your screen, making which-function-mode less needed -- function should not be too long or complex. But it's still useful when reading other people's code, of course. Note, there are other ways to not loose your orientation when reading code; earlier, we discussed hideshow, which enables showing/hiding the function bodies.

4 comments:

Anonymous said...

In the CEDET package at cedet.sf.net, there is a lot of infrastructure for parsing and tagging files, plus a mode called 'stickyfunc' mode. It is similar to which-func, but shows the signature line of the function that has scrolled off the top of the buffer in the header line.

CEDET also adds which-func support for a few more languages.

djcb said...

@Eric: thanks for the link.

I've never really gotten into cedet - but I'll try it out. At least these days it's as easy a 'apt-get install cedet-common' in ubuntu to install it.

Rajarshi Tiwari said...

Thanks for Sharing this. Was quite useful :-) - Rajarshi

Yassin Philip said...

It does not work when a function is defined inside a function :

(defun first ()
"1st func."
(defun second ()
"2nd func."
(message "plop"))
(second))

(wherever you put your point in this sexp, the modeline will always say "first")

And this is precisely when you want this to work. So not only it is useless, it's also dangerously misleading.