Invoking Imenu from a key

215 views Asked by At

Currently emacs has useful imenu thing which allow me to see list of functions in current buffer. To achieve this i need to type M-x, then type imenu, then press return key, then it will display prompt in minibuffer "Index item:" and i need to type func, then it displays another minibuffer prompt with aoutocompletion of all functions in current buffer. This is very good and useful, but now i'd like to reduce amount of typing and to macrosify somehow first part of sequence. I tried this approach:

(defun my-imenu-go-function-list ()
  (interactive)
  (imenu "func"))

(global-set-key (kbd "C-x C-o") 'my-imenu-go-function-list)

Another try:

(defun my-imenu-go-function-list ()
  (interactive)
  (imenu)
  (execute-kbd-macro [?f ?u ?n ?c return]))

But none worked, is there another possibility ?

2

There are 2 answers

1
sebs On BEST ANSWER

You need to call your function interactively.

Try the following. It should work.

UPDATED:

(defun my-imenu-go-function-list ()
  (interactive) 
  (let ((unread-command-events  (listify-key-sequence "func\n") ))
  (call-interactively 'imenu)))

If you are in Windows you might have to change the carriage return to "\r" or "\r\n"

0
phils On

sebs' answer displays an extremely neat trick I'd not seen before; however the following would be a bit more direct:

(imenu (assoc "func" (imenu--make-index-alist)))

It does depend upon a private (by convention) function, though, so YMMV. I can't see an obvious API for returning this alist value.