-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelm-selected.el
112 lines (92 loc) · 3.77 KB
/
helm-selected.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
;;; helm-selected.el --- helm extension for selected.el -*- lexical-binding: t; -*-
;; Copyright (C) 2017 Takaaki ISHIKAWA
;;
;; Author: Takaaki ISHIKAWA <takaxp at ieee dot org>
;; Keywords: extensions, convenience
;; Version: 0.9.1
;; Maintainer: Takaaki ISHIKAWA <takaxp at ieee dot org>
;; URL: https://github.com/takaxp/helm-selected
;; Package-Requires: ((emacs "24.4") (helm "2.8.6") (selected "1.01"))
;; Twitter: @takaxp
;; 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:
;; This package provides a `helm' extension for displaying candidates of associated command or action regarding `selected.el'.
;;; Code:
(eval-when-compile
(require 'cl-lib))
(require 'helm)
(require 'selected)
(defvar helm-selected--major-mode nil
"The current `major-mode', not `helm'.")
(defvar helm-selected--source
`((name . "Helm selected")
(action . helm-selected--action)
(candidate-number-limit . 9999)
(candidates . helm-selected--candidates))
"A helm source from selected.el.")
(defun helm-selected--action (candidate)
"An action for selected item specified by `CANDIDATE'."
(when (string-match "^.+\t\\(.*\\)$" candidate)
(call-interactively (intern (match-string 1 candidate)))))
(defun helm-selected--candidates ()
"Generate a list of candidates form `selected-keymap' or extended keymap."
(helm-selected--get-commands
(or (helm-selected--major-map)
selected-keymap)))
(defun helm-selected--get-commands (keymap)
"Create a list from the specified `KEYMAP'."
(if (keymapp keymap)
(cl-loop for i in (cdr keymap)
when (consp i)
unless (string= "helm-selected" (format "%s" (cdr i)))
collect (format "(%s)\t%s"
(let ((key (car i)))
(if (characterp key)
(string key) key))
(cdr i)))
(error "The argument is NOT keymap")))
(defun helm-selected--major-map ()
"Generate a list of candidates if a map is specified for major mode."
(let ((major-map
(intern
(concat "selected-"
(format "%s" helm-selected--major-mode)
"-map"))))
(and (boundp major-map)
(keymapp (symbol-value major-map))
(symbol-value major-map))))
(defun helm-selected--on ()
"An advice function for `selected-on'."
(setq helm-selected--major-mode major-mode))
(defun helm-selected-off ()
"An advice function for `selected-off'."
(interactive)
(helm-selected--banish-major-mode-map)
(setq helm-selected--major-mode nil))
(defun helm-selected--banish-major-mode-map ()
"Unintern selected-<major-mode>-map."
(let ((map
(concat "selected-" (format "%s" helm-selected--major-mode) "-map")))
(unless (boundp (intern-soft map))
(unintern map nil))))
(advice-add 'selected--on :after #'helm-selected--on)
(advice-add 'selected-off :before #'helm-selected-off)
;;;###autoload
(defun helm-selected ()
"Select from selected commands to execute."
(interactive)
(when mark-active
(helm :sources '(helm-selected--source)
:buffer "*helm selected*")
(helm-selected--banish-major-mode-map)))
(provide 'helm-selected)
;;; helm-selected.el ends here