forked from manateelazycat/snails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnails-backend-imenu.el
161 lines (137 loc) · 4.73 KB
/
snails-backend-imenu.el
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
;;; snails-backend-imenu.el --- IMenu backend for snails
;; Filename: snails-backend-imenu.el
;; Description: IMenu backend for snails
;; Author: Andy Stewart <[email protected]>
;; Maintainer: Andy Stewart <[email protected]>
;; Copyright (C) 2019, Andy Stewart, all rights reserved.
;; Created: 2019-07-26 08:03:06
;; Version: 0.1
;; Last-Updated: 2019-07-26 08:03:06
;; By: Andy Stewart
;; URL: http://www.emacswiki.org/emacs/download/snails-backend-imenu.el
;; Keywords:
;; Compatibility: GNU Emacs 26.2
;;
;; Features that might be required by this library:
;;
;;
;;
;;; This file is NOT part of GNU Emacs
;;; License
;;
;; 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, 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; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;; Commentary:
;;
;; IMenu backend for snails
;;
;;; Installation:
;;
;; Put snails-backend-imenu.el to your load-path.
;; The load-path is usually ~/elisp/.
;; It's set in your ~/.emacs like this:
;; (add-to-list 'load-path (expand-file-name "~/elisp"))
;;
;; And the following to your ~/.emacs startup file.
;;
;; (require 'snails-backend-imenu)
;;
;; No need more.
;;; Customize:
;;
;;
;;
;; All of the above can customize by:
;; M-x customize-group RET snails-backend-imenu RET
;;
;;; Change log:
;;
;; 2019/07/26
;; * First released.
;;
;;; Acknowledgements:
;;
;;
;;
;;; TODO
;;
;;
;;
;;; Require
(require 'imenu)
;;; Code:
(defvar snails-backend-imenu-cached-candidates nil)
(defvar snails-backend-imenu-cached-buffer nil)
(defun snails-backend-imenu-candidates (buffer)
(with-current-buffer buffer
(prog1
(if
;; Use cache candidates when `snails-backend-imenu-cached-candidates' is non-nil.
;; Need re-generate cache when user switch different buffer.
(and snails-backend-imenu-cached-candidates
(or
(not snails-backend-imenu-cached-buffer)
(equal snails-backend-imenu-cached-buffer buffer)))
snails-backend-imenu-cached-candidates
(setq snails-backend-imenu-cached-candidates
(let ((index (ignore-errors (imenu--make-index-alist t))))
(when index
(snails-backend-imenu-build-candidates
(delete (assoc "*Rescan*" index) index))))))
(setq snails-backend-imenu-cached-buffer buffer))))
(defun snails-backend-imenu-build-candidates (alist)
(cl-remove-if
(lambda (c)
(or (string-equal (car c) "Types")
(string-equal (car c) "Variables")
))
(cl-loop for elm in alist
nconc (cond
((imenu--subalist-p elm)
(snails-backend-imenu-build-candidates
(cl-loop for (e . v) in (cdr elm) collect
(cons
(car elm)
(if (integerp v) (copy-marker v) v)))))
((listp (cdr elm))
(and elm (list elm)))
(t
(and (cdr elm)
(setcdr elm (pcase (cdr elm)
((and ov (pred overlayp))
(copy-overlay ov))
((and mk (or (pred markerp)
(pred integerp)))
(copy-marker mk))))
(list elm)))))))
(snails-create-sync-backend
:name
"IMENU"
:candidate-filter
(lambda (input)
(let ((imenu-items (snails-backend-imenu-candidates snails-start-buffer))
candidates)
(dolist (imenu-item imenu-items)
(when (or
(string-equal input "")
(snails-match-input-p input (car imenu-item)))
(snails-add-candiate 'candidates
(car imenu-item)
(number-to-string (marker-position (cdr imenu-item))))))
(snails-sort-candidates input candidates 0 0)))
:candidate-do
(lambda (candidate)
(goto-char (string-to-number candidate))
(snails-flash-line)))
(provide 'snails-backend-imenu)
;;; snails-backend-imenu.el ends here