summaryrefslogtreecommitdiff
path: root/imenu-conf.el
blob: af855597e061073f821c3e4da1e7e515bde45c3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
;;; imenu-conf.el --- Flatten imenu                  -*- lexical-binding: t; -*-

;; Copyright (C) 2023  Jean Sévère Durand

;; Author: Jean Sévère Durand <durand@jsdurand.xyz>
;; Keywords: convenience, maint, outlines

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; Flatten the imenu list if needed.

;;; Code:

(require 'imenu)

(defvar imenu-flatten-p t
  "Whether or not to flatten the indices in imenu.")

(defun imenu-flatten-prefix (parent-prefix child-prefix)
  "A helper function to concatenate prefixes."
  (cond
   ((and
     (stringp parent-prefix)
     (not (string-empty-p parent-prefix)))
    ;; We can experiment with different styles, perhaps.
    (format "%s/%s" parent-prefix child-prefix))
   (child-prefix)))

(defun imenu-flatten (alist)
  "Flatten the ALIST.
See the documentation of the variable `imenu--index-alist' to
know what the flattening does."
  (let* ((prefix "")
         ;; Perform a depth-first walk on the tree.
         (stack (mapcar (lambda (item) (cons prefix item)) alist))
         temp result)
    (while (consp stack)        ; `consp' excludes empty lists
      ;; pop the stack from the front
      (setq temp (car stack))
      (setq stack (cdr stack))
      (cond
       ((not (imenu--subalist-p (cdr temp)))
        (setq result
              (cons
               (cons
                (imenu-flatten-prefix (car temp) (cadr temp))
                (cddr temp))
               result)))
       ((let ((prefix (imenu-flatten-prefix (car temp) (cadr temp))))
          (mapc
           (lambda (sub-item)
             (setq stack (cons (cons prefix sub-item) stack)))
           (cddr temp))))))
    (nreverse result)))

(defun imenu-flatten-advice (alist)
  "An advice for `imenu--make-index-alist' that flattens its result.
The variable `imenu-flatten-p' controls whether the flattening
actually happens."
  (cond
   (imenu-flatten-p (imenu-flatten alist))
   (alist)))

(advice-add #'imenu--make-index-alist
            :filter-return #'imenu-flatten-advice)



(provide 'imenu-conf)
;;; imenu-conf.el ends here