-
Notifications
You must be signed in to change notification settings - Fork 12
/
helm-descbinds.el
248 lines (213 loc) · 7.98 KB
/
helm-descbinds.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
;;; helm-descbinds.el --- Yet Another `describe-bindings' with `helm'.
;; Copyright (C) 2008, 2009, 2010 Taiki SUGAWARA <buzz.taiki@gmail.com>
;; Copyright (C) 2012 Michael Markert <markert.michael@googlemail.com>
;; Author: Taiki SUGAWARA <buzz.taiki@gmail.com>
;; Keywords: helm, help
;; Version: 1.06
;; Time-stamp: <2012-03-18 01:35:51 cofi>
;; This file 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 file 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 GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; This package is a replacement of `describe-bindings'.
;;; Usage:
;; Add followings on your .emacs.
;;
;; (require 'descbinds-helm)
;; (descbinds-helm-install)
;;
;; Now, `describe-bindings' is replaced to `descbinds-helm'. Type
;; `C-h b', `C-x C-h' these run `descbinds-helm'.
;;
;; In the Helm buffer, you can select key-binds with helm interface.
;;
;; - When type RET, selected candidate command is executed.
;;
;; - When type ESC, You can "Execute", "Describe Function" or "Find
;; Function" by the menu.
;;
;; - When type C-z, selected command is described without quiting.
;;; History:
;; 2012-03-18 Michael Markert <markert.michael@googlemail.com>
;; * descbinds-helm.el: Version 1.06
;; port to helm
;;
;; 2010-02-05 Taiki SUGAWARA <sugawara_t@ariel-networks.com>
;;
;; * descbinds-anything.el: Version 1.05
;; bug fix.
;;
;; 2010-02-02 UTC Taiki SUGAWARA <buzz.taiki@gmail.com>
;;
;; * descbinds-anything.el: Version 1.04
;; add sorting feature.
;; separete sorce creation function.
;; add persistent action.
;;
;; 2009-03-29 UTC Taiki SUGAWARA <buzz.taiki@gmail.com>
;;
;; * descbinds-anything.el: Version 1.03
;; fix typo.
;;
;; 2008-11-16 UTC Taiki SUGAWARA <buzz.taiki@gmail.com>
;;
;; * descbinds-anything.el: Version 1.02
;; bound `indent-tabs-mode` to t for nil environment.
;;
;; 2008-11-16 UTC Taiki SUGAWARA <buzz.taiki@gmail.com>
;;
;; * descbinds-anything.el: fix infinitive-loop when binding-line
;; has not tab.
;;; Code:
(require 'helm)
(defgroup helm-descbinds nil
"Yet Another `describe-bindings' with `helm'."
:prefix "helm-descbinds-"
:group 'helm)
(defcustom helm-descbinds-actions
'(("Execute" . helm-descbinds-action:execute)
("Describe Function" . helm-descbinds-action:describe)
("Find Function" . helm-descbinds-action:find-func))
"Actions of selected candidate."
:type '(repeat
(cons
:tag "Action"
(string :tag "Name")
(function :tag "Function")))
:group 'helm-descbinds)
(defcustom helm-descbinds-candidate-formatter
'helm-descbinds-default-candidate-formatter
"Candidate formatter function.
This function called two argument KEY and BINDING."
:type 'function
:group 'helm-descbinds)
(defcustom helm-descbinds-window-style 'one-window
"Window splitting style."
:type '(choice
(const :tag "One Window" one-window)
(const :tag "Same Window" same-window)
(const :tag "Split Window" split-window))
:group 'helm-descbinds)
(defcustom helm-descbinds-section-order
'("Major Mode Bindings" "Minor Mode Bindings" "Global Bindings")
"A list of section order by name regexp."
:type '(repeat (regexp :tag "Regexp"))
:group 'helm-descbinds)
(defcustom helm-descbinds-source-template
'((candidate-transformer . helm-descbinds-transform-candidates)
(persistent-action . helm-descbinds-action:describe)
(action-transformer . helm-descbinds-transform-actions))
"A template of `helm-descbinds' source."
:type 'sexp
:group 'helm-descbinds)
(defun helm-descbinds-all-sections (buffer &optional prefix menus)
(with-temp-buffer
(let ((indent-tabs-mode t))
(describe-buffer-bindings buffer prefix menus))
(goto-char (point-min))
(let ((header-p (not (= (char-after) ?\f)))
sections header section)
(while (not (eobp))
(cond
(header-p
(setq header (buffer-substring-no-properties
(point)
(line-end-position)))
(setq header-p nil)
(forward-line 3))
((= (char-after) ?\f)
(push (cons header (nreverse section)) sections)
(setq section nil)
(setq header-p t))
((looking-at "^[ \t]*$")
;; ignore
)
(t
(let ((binding-start (save-excursion
(and (re-search-forward "\t+" nil t)
(match-end 0))))
key binding)
(when binding-start
(setq key (buffer-substring-no-properties (point) binding-start)
key (replace-regexp-in-string"^[ \t\n]+" "" key)
key (replace-regexp-in-string"[ \t\n]+$" "" key))
(goto-char binding-start)
(setq binding (buffer-substring-no-properties
binding-start
(line-end-position)))
(unless (member binding '("self-insert-command"))
(push (cons key binding) section))))))
(forward-line))
(push (cons header (nreverse section)) sections)
(nreverse sections))))
(defun helm-descbinds-action:execute (candidate)
"An action that execute selected CANDIDATE command."
(call-interactively (cdr candidate)))
(defun helm-descbinds-action:describe (candidate)
"An action that describe selected CANDIDATE function."
(describe-function (cdr candidate)))
(defun helm-descbinds-action:find-func (candidate)
"An action that find selected CANDIDATE function."
(find-function (cdr candidate)))
(defun helm-descbinds-default-candidate-formatter (key binding)
"Default candidate formatter."
(format "%-10s\t%s" key binding))
(defun helm-descbinds-sort-sections (sections)
(flet ((order (x)
(loop for n = 0 then (1+ n)
for regexp in helm-descbinds-section-order
if (and (car x) (string-match regexp (car x))) return n
finally return n)))
(sort sections (lambda (a b)
(< (order a) (order b))))))
(defun helm-descbinds-transform-candidates (candidates)
(mapcar
(lambda (pair)
(cons (funcall helm-descbinds-candidate-formatter
(car pair) (cdr pair))
(cons (car pair) (intern-soft (cdr pair)))))
candidates))
(defun helm-descbinds-transform-actions (actions candidate)
(and (commandp (cdr candidate)) (or actions helm-descbinds-actions)))
(defun helm-descbinds-sources (buffer &optional prefix menus)
(mapcar
(lambda (section)
(helm-descbinds-source (car section) (cdr section)))
(helm-descbinds-sort-sections
(helm-descbinds-all-sections buffer prefix menus))))
(defun helm-descbinds-source (name candidates)
`((name . ,name)
(candidates . ,candidates)
,@helm-descbinds-source-template))
(defun helm-descbinds (&optional prefix buffer)
"Yet Another `describe-bindings' with `helm'."
(interactive)
(let ((helm-samewindow (and (not (minibufferp))
(memq helm-descbinds-window-style
'(same-window one-window))))
(helm-before-initialize-hook (if (and (not (minibufferp))
(eq helm-descbinds-window-style 'one-window))
(cons 'delete-other-windows helm-before-initialize-hook)
helm-before-initialize-hook)))
(helm :sources (helm-descbinds-sources (or buffer (current-buffer)) prefix))))
(defvar helm-descbinds-Orig-describe-bindings
(symbol-function 'describe-bindings))
(defun helm-descbinds-install ()
"Use `helm-descbinds' as a replacement of `describe-bindings'."
(interactive)
(fset 'describe-bindings 'helm-descbinds))
(defun helm-descbinds-uninstall ()
"Restore original `describe-bindings'."
(interactive)
(fset 'describe-bindings helm-descbinds-Orig-describe-bindings))
(provide 'helm-descbinds)
;;; helm-descbinds.el ends here