-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathship-mate-edit.el
280 lines (200 loc) · 8.86 KB
/
ship-mate-edit.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
;;; ship-mate-edit.el --- Edit history and environment -*- lexical-binding: t; -*-
;; Author: Krister Schuchardt <[email protected]>
;; Homepage: https://github.com/Walheimat/ship-mate
;; Version: 0.7.0
;; Package-Requires: ((emacs "29.1"))
;; Keywords: convenience
;;; Commentary:
;; Conveniently edit a command's environment and history in a separate
;; buffer.
;;; Code:
(require 'ship-mate)
(require 'ship-mate-dinghy)
;;;; Variables
(defvar ship-mate-edit-environment-buffer-name "*ship-mate-edit-env*"
"The name of the buffer used for `ship-mate-edit-environment'.")
(defvar ship-mate-edit-environment--target-buffer nil
"The buffer `ship-mate-edit-environment' was called from.")
(defvar ship-mate-edit-environment-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "\C-c\C-c" #'ship-mate-edit-environment-apply)
(define-key map "\C-c\C-q" #'ship-mate-edit-environment-clear)
(define-key map "\C-c\C-k" #'ship-mate-edit-environment-abort)
map)
"Map used in buffer created by `ship-mate-edit-environment'.")
(defvar ship-mate-edit-history-buffer-name "*ship-mate-edit-history*"
"The name of the buffer used for `ship-mate-edit-history'.")
(defvar ship-mate-edit-history--command nil
"The symbol of the command currently edited.")
(defvar ship-mate-edit-history-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "\C-c\C-c" #'ship-mate-edit-history-apply)
(define-key map "\C-c\C-q" #'ship-mate-edit-history-clear)
(define-key map "\C-c\C-k" #'ship-mate-edit-history-abort)
map)
"Map used in buffer created by `ship-mate-edit-history'.")
;;;; Common
(defun ship-mate-edit--in-buffer (buffer-name elements mode)
"Edit ELEMENTS in buffer BUFFER-NAME.
Sets MODE unless already set."
(let* ((buffer (get-buffer-create buffer-name))
(length (length elements)))
(with-current-buffer buffer
(erase-buffer)
(seq-map-indexed
(lambda (it i)
(insert it)
(unless (eq i (1- length))
(insert "\n")))
elements)
(set-buffer-modified-p nil)
(unless (buffer-local-value mode (current-buffer))
(funcall mode))
(pop-to-buffer buffer nil t))))
;;;; Editing environment
(define-minor-mode ship-mate-edit-environment-mode
"Minor mode to edit the environment.
\\{ship-mate-edit-environment-mode-map}"
:lighter " sme"
(setq-local header-line-format
(substitute-command-keys
"\\<ship-mate-edit-environment-mode-map>\
`\\[ship-mate-edit-environment-apply]' applies and recompiles, \
`\\[ship-mate-edit-environment-clear]' clears all env variables, \
`\\[ship-mate-edit-environment-abort]' reverts.")))
(defun ship-mate-edit-environment--internal ()
"Edit environment in the current buffer."
(unless (ship-mate--command-buffer-p (current-buffer))
(user-error "Can only edit `ship-mate' command buffers"))
(setq ship-mate-edit-environment--target-buffer (current-buffer))
(ship-mate-edit-environment--in-buffer (buffer-local-value 'compilation-environment ship-mate-edit-environment--target-buffer)))
(defun ship-mate-edit-environment--in-buffer (environment)
"Create the buffer for editing the ENVIRONMENT."
(ship-mate-edit--in-buffer ship-mate-edit-environment-buffer-name
environment
'ship-mate-edit-environment-mode))
(defun ship-mate-edit-environment--validate ()
"Validate the current edit state."
(let ((new-state (ship-mate-edit-environment--listify))
(warnings nil))
(unless (ship-mate-environment--valid-env-p new-state)
(push "Invalid assignments" warnings))
warnings))
(defun ship-mate-edit-environment--listify ()
"Listify the environment buffer."
(ship-mate-edit--listify-buffer (get-buffer ship-mate-edit-environment-buffer-name)))
(defun ship-mate-edit-environment-apply ()
"Apply the edited environment."
(interactive nil ship-mate-edit-environment-mode)
(when-let ((warnings (ship-mate-edit-environment--validate)))
(user-error (string-join warnings ", ")))
(ship-mate-edit-environment--set-environment (ship-mate-edit-environment--listify))
(ship-mate-edit-environment--quit))
(defun ship-mate-edit-environment--quit ()
"Quit the editing."
(quit-window t (get-buffer-window ship-mate-edit-environment-buffer-name t))
(setq ship-mate-edit-environment--target-buffer nil))
(defun ship-mate-edit-environment-abort ()
"Abort editing."
(interactive nil ship-mate-edit-environment-mode)
(ship-mate-edit-environment--quit))
(defun ship-mate-edit-environment-clear ()
"Clear the environment."
(interactive nil ship-mate-edit-environment-mode)
(ship-mate-edit-environment--set-environment nil)
(ship-mate-edit-environment--quit))
(defun ship-mate-edit-environment--set-environment (env)
"Set `compilation-environment' to ENV.
This is set in buffer `ship-mate-edit-environment-buffer-name'."
(with-current-buffer ship-mate-edit-environment--target-buffer
(setq-local compilation-environment env)
(run-hooks 'ship-mate-environment-set-hook)))
;;;; Editing history
(define-minor-mode ship-mate-edit-history-mode
"Minor mode to edit the history.
\\{ship-mate-edit-history-mode-map}"
:lighter " smh"
(setq-local header-line-format
(substitute-command-keys
"\\<ship-mate-edit-history-mode-map>\
`\\[ship-mate-edit-history-apply]' applies, \
`\\[ship-mate-edit-history-clear]' clears the history, \
`\\[ship-mate-edit-history-abort]' reverts.")))
(defun ship-mate-edit-history--internal ()
"Edit the history of the current buffer."
(unless (ship-mate--command-buffer-p (current-buffer))
(user-error "Can only edit `ship-mate' command buffer"))
(setq ship-mate-edit-history--command (buffer-local-value 'ship-mate--this-command (current-buffer)))
(ship-mate-edit-history--in-buffer))
(defun ship-mate-edit-history--in-buffer ()
"Edit the history in a buffer."
(let ((history (ship-mate-command--history ship-mate-edit-history--command)))
(ship-mate-edit--in-buffer ship-mate-edit-history-buffer-name
(ring-elements history)
'ship-mate-edit-history-mode)))
(defun ship-mate-edit-history-apply ()
"Apply the edited history."
(interactive nil ship-mate-edit-history-mode)
(ship-mate-edit-history--set-history (ship-mate-edit-history--listify))
(ship-mate-edit-history--quit))
(defun ship-mate-edit-history-clear ()
"Clear the history."
(interactive nil ship-mate-edit-history-mode)
(ship-mate-edit-history--set-history nil)
(ship-mate-edit-history--quit))
(defun ship-mate-edit-history-abort ()
"Abort editing history."
(interactive nil ship-mate-edit-history-mode)
(ship-mate-edit-history--quit))
(defun ship-mate-edit-history--set-history (new-elements)
"Set the edited HISTORY to include NEW-ELEMENTS."
(let ((history (ship-mate-command--history ship-mate-edit-history--command)))
(ring-resize history 0)
(ring-resize history ship-mate-command-history-size)
(dolist (it new-elements)
(ring-insert history it))))
(defun ship-mate-edit-history--quit ()
"Quit the history editing buffer."
(quit-window t (get-buffer-window ship-mate-edit-history-buffer-name t))
(setq ship-mate-edit-history--command nil))
(defun ship-mate-edit-history--listify ()
"Listify history buffer."
(reverse (ship-mate-edit--listify-buffer (get-buffer ship-mate-edit-history-buffer-name))))
;;;; Utility
(defun ship-mate-edit--listify-buffer (buffer)
"Listify the content of BUFFER."
(with-current-buffer buffer
(let* ((raw (buffer-string)))
(seq-filter (lambda (it) (not (string-empty-p it))) (string-split raw "\n")))))
;;;; API
;;;###autoload
(defun ship-mate-edit-environment (buffer)
"Edit the `compilation-environment' for BUFFER.
If BUFFER isn't a compilation buffer, this prompts to select one."
(interactive
(list (if (ship-mate--command-buffer-p)
(current-buffer)
(ship-mate--complete-buffer "Edit environment for buffer: "))))
(with-current-buffer buffer
(ship-mate-edit-environment--internal)))
;;;###autoload
(defun ship-mate-edit-history (buffer)
"Edit the history for BUFFER.
If BUFFER isn't a compilation buffer, this prompts to select one."
(interactive
(list (if (ship-mate--command-buffer-p)
(current-buffer)
(ship-mate--complete-buffer "Edit history for buffer: "))))
(with-current-buffer buffer
(ship-mate-edit-history--internal)))
;;;###autoload
(defun ship-mate-edit-setup-bindings ()
"Set up bindings in two command maps."
(let ((map ship-mate-command-map))
(define-key map (kbd ",") #'ship-mate-edit-environment)
(define-key map (kbd ".") #'ship-mate-edit-history))
(let ((map ship-mate-dinghy-mode-map))
(define-key map (kbd "C-c ,") #'ship-mate-edit-environment)
(define-key map (kbd "C-c .") #'ship-mate-edit-history)))
(provide 'ship-mate-edit)
;;; ship-mate-edit.el ends here