I would like to have a single shortcut to jump to the next item on the imenu of current buffer. E.g., if the current buffer is in org mode, next-imenu-item = org-next-visible-heading.
Anyone did that before?
I would like to have a single shortcut to jump to the next item on the imenu of current buffer. E.g., if the current buffer is in org mode, next-imenu-item = org-next-visible-heading.
Anyone did that before?
(defun my-imenu-goto--closest-dir (direction)
"Jump to the closest imenu item on the current buffer.
If direction is 1, jump to next imenu item.
If direction is -1, jump to previous imenu item.
See https://emacs.stackexchange.com/questions/30673
Adapted from `which-function' in::
https://github.com/typester/emacs/blob/master/lisp/progmodes/which-func.el"
;; Ensure `imenu--index-alist' is populated.
(imenu--make-index-alist)
(let ((alist imenu--index-alist)
(minoffset (point-max))
offset pair mark imstack destination)
;; Elements of alist are either ("name" . marker), or
;; ("submenu" ("name" . marker) ... ). The list can be
;; Arbitrarily nested.
(while (or alist imstack)
(if alist
(progn
(setq pair (car-safe alist)
alist (cdr-safe alist))
(cond
((atom pair)) ;; Skip anything not a cons.
((imenu--subalist-p pair)
(setq imstack (cons alist imstack)
alist (cdr pair)))
((number-or-marker-p (setq mark (cdr pair)))
(when (> (setq offset (* (- mark (point)) direction)) 0)
(when (< offset minoffset) ;; Find the closest item.
(setq minoffset offset
destination mark))))))
(setq alist (car imstack)
imstack (cdr imstack))))
(when destination
(imenu-default-goto-function "" destination ""))))
(defun my-imenu-goto-next ()
(interactive)
(unless (my-imenu-goto--closest-dir 1)
(goto-char (point-max))))
(defun my-imenu-goto-prev ()
(interactive)
(unless (my-imenu-goto--closest-dir -1)
(goto-char (point-min))))