Search code examples
emacselispimenu

Invoking Imenu from a key


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 ?


Solution

  • 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"