The custom function below my-jump-to
works after being evaluated in a scratch buffer, but does not work when I stick it into a library with lexical-binding
non-nil in the header. When byte-compiling, I get a warning: reference to free variable ‘the-fn’
. When I restart Emacs after byte-compiling, and try to use the function, I get a *Backtrace*
: Debugger entered--Lisp error: (void-variable the-fn)
. I tried using the trick of putting a #'
in front of the (lambda ...
, but that did not appear to have any effect. I tried using a backtick
in front of the (lambda ...
, and placing a comma before the-fn
, but that didn't work either. Is it possible to make this work inside a library with lexical-binding
non-nil in the header, and if so, then how can that be achieved?
(defun my-jump-to ()
"Jump to an imenu item using ido."
(let* ((completion-ignore-case t)
(collection (imenu--make-index-alist))
(first-amended-collection nil)
(the-fn
(lambda (index-alist depth)
(dolist (entry index-alist)
(setq imenu-list--line-entries (append imenu-list--line-entries (list entry)))
(push entry first-amended-collection)
(when (imenu--subalist-p entry)
(funcall the-fn (cdr entry) (1+ depth))))))
second-amended-collection)
(funcall the-fn collection 0)
(mapc (lambda (elt)
(push (cons (car elt) (list elt)) second-amended-collection))
first-amended-collection)
(let ((choice (ido-completing-read "SELECT: " second-amended-collection nil 'confirm)))
(imenu (car (cdr (assoc choice second-amended-collection)))))))