-
Notifications
You must be signed in to change notification settings - Fork 101
/
rustic-popup.el
253 lines (223 loc) · 8.67 KB
/
rustic-popup.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
;;; rustic-popup.el --- Cargo popup -*-lexical-binding: t-*-
;;; Commentary:
;; Provides magit like popup.
;;; Code:
(require 'rustic-cargo)
;;; Customization
(defcustom rustic-popup-commands
'((?b "build" build)
(?f "fmt" fmt)
(?r "run" run)
(?c "clippy" clippy)
(?o "outdated" outdated)
(?e "clean" clean)
(?k "check" check)
(?t "test" test)
(?d "doc" doc))
"List of commands that are displayed in the popup.
The first element of each list contains a command's binding."
:type 'list
:group 'rustic-popup)
(defcustom rustic-kill-buffer-and-window t
"Whether to kill popup window and buffer after command execution."
:type 'boolean
:group 'rustic)
(define-obsolete-face-alias 'rustic-popup-key-face
'rustic-popup-key "1.2")
(define-obsolete-face-alias 'rustic-popup-section-face
'rustic-popup-section "1.2")
(defface rustic-popup-key
'((t (:foreground "DeepSkyBlue")))
"Face used for command shortcuts."
:group 'rustic)
(defface rustic-popup-section
'((t (:foreground "#f74c00")))
"Face used for popup section description."
:group 'rustic)
;;; Popup Mode
(defvar rustic-popup-buffer-name "rustic-popup-buffer"
"Buffer name for rustic popup buffers.")
(defvar rustic--popup-rust-src-name nil
"Rust source code file name from which rustic-popup was invoked.")
(defvar rustic-popup-mode-map
(let ((map (make-sparse-keymap)))
(define-key map [remap self-insert-command] 'rustic-popup-invoke-popup-action)
(define-key map (kbd "g") 'rustic-recompile)
(define-key map (kbd "RET") 'rustic-popup-default-action)
(define-key map (kbd "<tab>") 'rustic-popup-default-action)
(define-key map (kbd "h") 'rustic-popup-cargo-command-help)
(define-key map (kbd "q") 'kill-buffer-and-window)
map)
"Keymap for rustic popup buffers.")
(define-derived-mode rustic-popup-mode fundamental-mode "RusticPopup"
"Mode for rustic popup buffers."
(setq truncate-lines t)
(setq buffer-read-only t)
(setq-local scroll-margin 0))
(defun rustic-popup-insert-backtrace ()
"Insert backtrace section."
(let ((inhibit-read-only t)
(prop (lambda (s)
(propertize s 'face 'rustic-popup-section))))
(insert (funcall prop "Backtrace: "))
(cond
((string= rustic-compile-backtrace "0")
(insert " " (funcall prop "0") " | 1 | full"))
((string= rustic-compile-backtrace "1")
(insert " 0 | " (funcall prop "1") " | full"))
((string= rustic-compile-backtrace "full")
(insert " 0 | 1 | " (funcall prop "full")))))
(insert "\n\n"))
(defun rustic-popup-insert-contents (buf)
"Insert popup buffer contents."
(let ((inhibit-read-only t))
(with-current-buffer buf
(erase-buffer)
(rustic-popup-mode)
(rustic-popup-insert-backtrace)
(insert (propertize "Commands: " 'face 'rustic-popup-section) "\n")
(insert " " (propertize "g" 'face 'rustic-popup-key)
" " "recompile" " " "\""
(or (car compilation-arguments) (rustic-compile-command))
"\"" "\n\n")
(dolist (command rustic-popup-commands)
(insert "\s")
(insert (propertize (char-to-string (nth 0 command))
'face 'rustic-popup-key))
(insert "\s\s\s\s\s\s")
(insert (nth 1 command))
(when (and (string= (nth 1 command) "test")
(> (length rustic-test-arguments) 0))
(insert " " "\"" rustic-test-arguments "\""))
(insert "\n"))
(goto-char (point-min)))))
;;;###autoload
(defun rustic-popup (&optional args)
"Setup popup.
If directory is not in a rust project call `read-directory-name'."
(interactive "P")
(rustic--inheritenv
(setq rustic--popup-rust-src-name buffer-file-name)
(let ((func (lambda ()
(let ((buf (get-buffer-create rustic-popup-buffer-name))
(win (split-window-below))
(inhibit-read-only t))
(rustic-popup-insert-contents buf)
(set-window-buffer win buf)
(select-window win)
(fit-window-to-buffer)
(set-window-text-height win (+ (window-height) 1))))))
(if args
(let ((dir (read-directory-name "Rust project:")))
(let ((default-directory dir))
(funcall func)))
(funcall func)))))
;;; Interactive
;;;###autoload
(defun rustic-popup-invoke-popup-action (event)
"Execute commands which are listed in `rustic-popup-commands'."
(interactive (list last-command-event))
(save-excursion
(let ((char (char-to-string event)))
(save-match-data
(goto-char (point-min))
(when (re-search-forward (concat "\s" char "\s"))
(goto-char (match-beginning 0)))))
(let* ((command (cadr (split-string
(buffer-substring-no-properties
(point) (line-end-position)))))
(c (intern (concat "rustic-cargo-" command))))
(if (commandp c)
(call-interactively c)
(call-interactively 'rustic-compile (concat "cargo " command)))))
(when rustic-kill-buffer-and-window
(kill-buffer-and-window)))
;;;###autoload
(defun rustic-popup-default-action ()
"Change backtrace and `compilation-arguments' when executed on
corresponding line."
(interactive)
(let ((inhibit-read-only t))
(save-excursion
(goto-char (line-beginning-position))
(cond
((looking-at "Backtrace:")
(cond
((string= rustic-compile-backtrace "0")
(setq rustic-compile-backtrace "1"))
((string= rustic-compile-backtrace "1")
(setq rustic-compile-backtrace "full"))
((string= rustic-compile-backtrace "full")
(setq rustic-compile-backtrace "0")))
(rustic-popup-insert-contents (current-buffer)))
((search-forward-regexp "\srecompile\s" (line-end-position) t)
(rustic-set-compilation-arguments
(read-string "Compilation arguments: "
(or (car compilation-arguments)
(rustic-compile-command))))
(rustic-popup-insert-contents (current-buffer)))
((search-forward-regexp "\stest" (line-end-position) t)
(setq rustic-test-arguments
(read-string "Cargo test arguments: " rustic-test-arguments))
(rustic-popup-insert-contents (current-buffer)))
(t
(message "No default action for line."))))))
;;; Help Popup
(defvar rustic-popup-help-buffer-name "rustic-popup-help-buffer"
"Buffer name for rustic popup help buffers.")
(defvar rustic-popup-help-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "q") 'rustic-popup-kill-help-buffer)
map)
"Keymap for rustic popup help buffers.")
(define-derived-mode rustic-popup-help-mode fundamental-mode "RusticHelpPopup"
"Mode for rustic popup help buffers."
(setq truncate-lines t)
(setq buffer-read-only t)
(setq-local scroll-margin 0))
;;;###autoload
(defun rustic-popup-cargo-command-help ()
"Display help buffer for cargo command at point."
(interactive)
(let (command)
(save-excursion
(goto-char (line-beginning-position))
(setq command (cadr (split-string
(buffer-substring-no-properties (line-beginning-position)
(line-end-position))))))
(let* ((string (rustic-popup-help-flags command))
(inhibit-read-only t))
(if (not (and (> (length command) 0)
(> (length (split-string string "\n")) 0)))
(message "No help information for command at point.")
(rustic-popup-setup-help-popup string)))))
(defun rustic-popup-help-flags (command)
"Get flags of COMMAND."
(let ((string (shell-command-to-string (format "cargo %s -h" command)))
flags)
(dolist (s (split-string string "\n"))
(when (and (not (string-match "^\s+\-h" s))
(string-match "^\s+\-" s))
(setq flags (concat flags s "\n"))))
flags))
(defun rustic-popup-setup-help-popup (string)
"Switch to help buffer."
(rustic--inheritenv
(let ((buf (get-buffer-create rustic-popup-help-buffer-name)))
(switch-to-buffer buf)
(erase-buffer)
(rustic-popup-help-mode)
(insert string)
(fit-window-to-buffer)
(set-window-text-height (selected-window) (+ (window-height) 1))
(goto-char (point-min)))))
;;;###autoload
(defun rustic-popup-kill-help-buffer ()
"Kill popup help buffer and switch to popup buffer."
(interactive)
(switch-to-buffer rustic-popup-buffer-name)
(fit-window-to-buffer)
(set-window-text-height (selected-window) (+ (window-height) 1)))
;;; _
(provide 'rustic-popup)
;;; rustic-popup.el ends here