-
Notifications
You must be signed in to change notification settings - Fork 1
/
quick-shell-keybind.el
82 lines (65 loc) · 2.59 KB
/
quick-shell-keybind.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
;;; quick-shell-keybind.el --- Interactively bind a key to shell commands
;; URL: https://github.com/eyeinsky/quick-shell-keybind
;; Version: 0.0.2
;; Author: eyeinsky <eyeinsky9@gmail.com>
;; Keywords: maint convenience processes
;; Package-Requires: ((emacs "24"))
;; 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.
;; For a full copy of the GNU General Public License
;; see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Interactive prompt for key, a shell buffer name, and a sequence of
;; shell commands to run in that buffer. Simply pressing <RET> at
;; "Command to run" will stop it for asking for more.
;;
;; To set it up you bind the definer to some key, e.g:
;;
;; (global-set-key (kbd "C-c C-t") #'quick-shell-keybind)
;;
;; Using this will now start the prompt sequence.
;;
;; The variable 'quick-shell-keybind-default-buffer-name' can be used
;; customize the default buffer to something other than "*shell*".
;;; Code:
(require 'comint)
(defgroup quick-shell-keybind nil
"Quick shell keybind"
:group 'tools)
(defcustom quick-shell-keybind-default-buffer-name "*shell*"
"Default buffer name for target shell."
:type 'string
:group 'quick-shell-keybind)
(defun quick-shell-keybind--ask-until (n)
"Ask commands until empty string, keep count with N."
(let ((str (read-string (message "Command to run [%i]: " n))))
(if (string= str
"")
'()
(cons str (quick-shell-keybind--ask-until (+ 1 n))))))
(defun quick-shell-keybind--send (command)
"Insert and execute COMMAND in sell."
(insert command)
(comint-send-input))
;;;###autoload
(defun quick-shell-keybind (key buffer commands)
"Bind KEY to run in shell BUFFER a sequence of COMMANDS."
(interactive (list (read-key-sequence "Enter key: ")
(read-string (message "Buffer (default %s): "
quick-shell-keybind-default-buffer-name) nil nil
quick-shell-keybind-default-buffer-name)
(quick-shell-keybind--ask-until 1)))
(global-set-key (kbd key)
`(lambda ()
(interactive)
(with-current-buffer ',buffer
(goto-char (point-max))
(mapcar #'quick-shell-keybind--send ',commands)))))
(provide 'quick-shell-keybind)
;;; quick-shell-keybind.el ends here