Refactor imenu-index flattening

This commit is contained in:
Ivan Malison 2016-08-17 17:25:35 -07:00
parent 7f5c885fef
commit 46551d0518
No known key found for this signature in database
GPG Key ID: 62530EFBE99DC2F8

View File

@ -504,32 +504,51 @@ times to find what I'm looking for. The functions that follow allow me
to get this behavior out of functions that provide a nested imenu to get this behavior out of functions that provide a nested imenu
index. index.
#+BEGIN_SRC emacs-lisp #+BEGIN_SRC emacs-lisp
(defun imalison:imenu-prefix-flattened (index) (defvar imalison:imenu-separator ".")
(let ((flattened (imalison:flatten-imenu-index (cdr index))))
(cl-loop for sub-item in flattened (cl-defun imalison:flatten-index-entry (index-entry &optional (prefix ""))
collect (cl-destructuring-bind (entry-name . rest) index-entry
`(,(concat (car index) "." (car sub-item)) . ,(cdr sub-item))))) (message "%s" rest)
(let ((new-entry-name (concat prefix entry-name))
(original-properties (text-properties-at 0 entry-name)))
;; Inherit any text-properties that were attached to the
;; original entry.
(set-text-properties 0 (length new-entry-name)
original-properties
new-entry-name)
(if (listp rest)
;; Internal Node
(let* ((new-prefix (concat new-entry-name imalison:imenu-separator))
(flattened-subentries
(cl-mapcan (lambda (entry)
(imalison:flatten-index-entry entry new-prefix))
rest)))
(if original-properties
;; When the entry had properties, we may want to keep
;; it around, because it might have a marker attached.
(cons (list new-entry-name) flattened-subentries)
flattened-subentries))
;; Leaf Node
(list (cons new-entry-name rest))))))
(defun imalison:flatten-imenu-index (index) (defun imalison:flatten-imenu-index (index)
(let ((cdr-is-index (listp (cdr index)))) (cl-mapcan 'imalison:flatten-index-entry index))
(cond ((not (stringp (car index))) (cl-mapcan
#'imalison:flatten-imenu-index index))
(cdr-is-index (imalison:imenu-prefix-flattened index))
(t (list index)))))
(defun imalison:make-imenu-index-flat () (defun imalison:make-current-imenu-index-flat ()
(let ((original-imenu-function imenu-create-index-function)) (let ((original-imenu-function imenu-create-index-function))
(setq imenu-create-index-function (setq imenu-create-index-function
(lambda () (lambda ()
(imalison:flatten-imenu-index (imalison:flatten-imenu-index
(funcall original-imenu-function)))))) (funcall original-imenu-function))))))
#+END_SRC #+END_SRC
By advising ~imenu--make-index-alist~ with By advising ~imenu--make-index-alist~ with
~imalison:flatten-imenu-index~ we make it so that imenu indexes are ~imalison:flatten-imenu-index~ we make it so that imenu indexes are
always flattened. This is still experimental, so copy to your own always flattened. This is still experimental, so copy to your own
dotfiles with caution. dotfiles with caution.
#+BEGIN_SRC emacs-lisp #+BEGIN_SRC emacs-lisp
(defvar imalison:flatten-imenu-global t) (defvar imalison:flatten-imenu-global nil)
(defvar imalison:flatten-imenu-local t) (defvar imalison:flatten-imenu-local t)
(make-variable-buffer-local 'imalison:flatten-imenu-local) (make-variable-buffer-local 'imalison:flatten-imenu-local)