-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquick-actions.el
79 lines (60 loc) · 2.93 KB
/
quick-actions.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
;;; quick-actions.el --- Quick actions menu -*- lexical-binding:t -*-
;; Copyright (C) 2022 Jean-Philippe Gagné Guay
;; Author: Jean-Philippe Gagné Guay <jeanphilippe150@gmail.com>
;; This file is part of GNU Emacs.
;; 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 configurable menu of quick actions.
;;; Code:
;;;###autoload
(defcustom quick-action-alist nil
"Alist of quick actions. The elements are (NAME . (FUNC ARGS)), where:
NAME is the string that will appear as a candidate that can be selected
with `quick-action-select'
FUNC is the symbol of a function and ARGS is a list of arguments passed to FUNC."
:type '(alist :key-type string
:value-type (list function sexp))
:safe #'listp)
(defvar last-quick-action nil)
(make-variable-buffer-local 'last-quick-action)
(defvar quick-action-history nil)
;;;###autoload
(defun quick-action-select ()
"Executes a quick action.
If `quick-action-alist' is nil (the default), this command is equivalent to `compile'.
If `quick-action-alist' contains a single item, do not prompt and execute this item directly.
Else, prompt for an ACTION among `quick-action-alist' keys and execute its associated function."
(interactive)
(cond ((not quick-action-alist)
(call-interactively #'compile))
((equal (length quick-action-alist) 1)
(let* ((entry (car quick-action-alist))
(quick-action (car entry))
(func (cadr entry))
(args (cddr entry)))
(setq last-quick-action quick-action)
(apply func args)))
(t
(let* ((default (if (and last-quick-action
(assoc last-quick-action quick-action-alist))
last-quick-action
(caar quick-action-alist)))
(prompt (format "Quick action (default %s): " default))
(completion-ignore-case t)
(quick-action (completing-read prompt (map-keys quick-action-alist)
nil t nil 'quick-action-history default))
(entry (assoc quick-action quick-action-alist))
(func (cadr entry))
(args (cddr entry)))
(setq last-quick-action quick-action)
(apply func args)))))
(provide 'quick-actions)