-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheshell-fixed-prompt.el
160 lines (135 loc) · 5.86 KB
/
eshell-fixed-prompt.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
;;; eshell-fixed-prompt.el --- Restrict eshell to a single fixed prompt -*- lexical-binding: t -*-
;; Copyright © 2017 Tijs Mallaerts
;;
;; Author: Tijs Mallaerts <[email protected]>
;; Package-Requires: ((emacs "25") (s "1.11.0"))
;; 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 <http://www.gnu.org/licenses/>.
;; This file is not part of GNU Emacs.
;;; Commentary:
;; Restrict eshell to a single fixed prompt
;;; Code:
(eval-when-compile
(defvar ivy-sort-functions-alist))
(require 'em-prompt)
(require 'em-hist)
(require 's)
(defun eshell-fixed-prompt-send-input-without-output-filter ()
"Insert prompt in buffer."
(setq-local eshell-output-filter-functions
(delete 'eshell-fixed-prompt-goto-input-start
eshell-output-filter-functions))
(eshell-send-input)
(add-to-list 'eshell-output-filter-functions
'eshell-fixed-prompt-goto-input-start))
(defun eshell-fixed-prompt-send-input ()
"Send input and keep fixed prompt."
(interactive)
(let ((prompt-before (funcall eshell-prompt-function))
(cmd (buffer-substring-no-properties (eshell-fixed-prompt-input-start-position)
(line-end-position))))
(unless (s-blank? cmd)
(eshell/clear-scrollback)
(eshell-fixed-prompt-send-input-without-output-filter)
(insert cmd)
(eshell-send-input)
(let ((prompt-after (funcall eshell-prompt-function)))
(unless (string= prompt-before prompt-after)
(eshell/clear-scrollback)
(eshell-fixed-prompt-send-input-without-output-filter))))))
(defun eshell-fixed-prompt-input-start-position ()
"Return the start position of the fixed prompt."
(save-excursion
(eshell-bol)
(point)))
(defun eshell-fixed-prompt-delete-input ()
"Delete the input at the fixed prompt."
(delete-region
(eshell-fixed-prompt-input-start-position)
(line-end-position))
(remove-overlays
(eshell-fixed-prompt-prompt-start-position)
(line-end-position)))
(defun eshell-fixed-prompt-prompt-start-position ()
"Return the start position of the prompt at point."
(save-excursion
(forward-line (- 1 (eshell-fixed-prompt-prompt-line-count)))
(line-beginning-position)))
(defun eshell-fixed-prompt-prompt-line-count ()
"Return the number of lines of the prompt."
(length (s-split "\n" (funcall eshell-prompt-function))))
(defun eshell-fixed-prompt-remove-next-prompt ()
"Remove the next eshell prompt."
(let ((first-prompt-line (save-excursion
(eshell-goto-input-start)
(line-number-at-pos)))
(last-line (save-excursion
(goto-char (point-max))
(line-number-at-pos)))
(inhibit-read-only t))
(when (and (/= first-prompt-line last-line)
(save-excursion
(forward-line last-line)
(let ((prompt (funcall eshell-prompt-function)))
(s-contains? prompt
(buffer-substring-no-properties
(eshell-fixed-prompt-prompt-start-position)
(line-end-position))))))
(save-excursion
(forward-line last-line)
(delete-region
(eshell-fixed-prompt-prompt-start-position)
(line-end-position))))))
(defun eshell-fixed-prompt-goto-input-start ()
"Move to start of input and remove other prompts."
(let ((curr-line (buffer-substring-no-properties (line-beginning-position)
(line-end-position))))
(if (s-contains? "?" curr-line)
(let ((str (read-string "Input: ")))
(if (stringp str)
(process-send-string (eshell-interactive-process)
(concat str "\n"))))
(unless (or (s-contains? "password" curr-line)
(s-contains? "passphrase" curr-line))
(eshell-goto-input-start)
(eshell-fixed-prompt-delete-input)
(eshell-fixed-prompt-remove-next-prompt)))))
(defun eshell-fixed-prompt-select-history-item ()
"Select eshell history item."
(interactive)
(let ((ivy-sort-functions-alist nil))
(insert (completing-read "History item: " (ring-elements eshell-history-ring)))))
(defvar eshell-fixed-prompt-mode-map
(let ((map (make-sparse-keymap)))
(define-key map [remap eshell-send-input]
'eshell-fixed-prompt-send-input)
(define-key map [remap beginning-of-buffer]
(lambda ()
(interactive)
(eshell-goto-input-start)))
(define-key map [remap eshell-previous-matching-input-from-input]
'eshell-fixed-prompt-select-history-item)
(define-key map [remap eshell-next-matching-input-from-input]
'eshell-fixed-prompt-select-history-item)
map))
;;;###autoload
(define-minor-mode eshell-fixed-prompt-mode
"Minor mode to restrict eshell to a single fixed prompt."
:lighter " esh-fixed"
:keymap eshell-fixed-prompt-mode-map
(if eshell-fixed-prompt-mode
(progn (add-to-list 'eshell-output-filter-functions
'eshell-fixed-prompt-goto-input-start))
(setq-local eshell-output-filter-functions
(delete 'eshell-fixed-prompt-goto-input-start
eshell-output-filter-functions))))
(provide 'eshell-fixed-prompt)
;;; eshell-fixed-prompt.el ends here