-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathmajutsu-command.el
More file actions
270 lines (232 loc) · 10.8 KB
/
Copy pathmajutsu-command.el
File metadata and controls
270 lines (232 loc) · 10.8 KB
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
;;; majutsu-command.el --- Ad-hoc command runners for Majutsu -*- lexical-binding: t; -*-
;; Copyright (C) 2026 0WD0
;; Author: 0WD0 <wd.1105848296@gmail.com>
;; Maintainer: 0WD0 <wd.1105848296@gmail.com>
;; Keywords: tools, vc
;; URL: https://github.com/0WD0/majutsu
;; SPDX-License-Identifier: GPL-3.0-or-later
;;; Commentary:
;; This library provides Magit-style ad-hoc command runners for Majutsu.
;; Plain `jj ...' commands are routed through Majutsu's process helpers so
;; they inherit `--no-pager', color handling, process buffering, and
;; with-editor integration. Other commands are run through the shell.
;;; Code:
(require 'majutsu-core)
(require 'majutsu-completion)
(require 'subr-x)
(defgroup majutsu-command nil
"Ad-hoc command runners for Majutsu."
:group 'majutsu)
(defcustom majutsu-shell-command-verbose-prompt t
"Whether to show the working directory when reading a command.
This affects `majutsu-jj-command', `majutsu-jj-command-topdir',
`majutsu-shell-command', and `majutsu-shell-command-topdir'."
:group 'majutsu-command
:type 'boolean)
(defvar majutsu-jj-command-history nil)
(defvar majutsu-shell-command-history nil)
(defvar majutsu-read-jj-command-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "TAB") #'majutsu-jj-command-complete)
(define-key map (kbd "<tab>") #'majutsu-jj-command-complete)
(define-key map [remap completion-at-point]
#'majutsu-jj-command-complete)
(make-composed-keymap map minibuffer-local-shell-command-map))
"Keymap used while reading jj commands.")
(defun majutsu--shell-command-directory (&optional topdir)
"Return directory used for ad-hoc command runners.
When TOPDIR or `current-prefix-arg' is non-nil, use the current
workspace root. Otherwise use `default-directory'."
(if (or topdir current-prefix-arg)
(majutsu--toplevel-safe)
default-directory))
(defun majutsu--command-program-name (program)
"Return PROGRAM's normalized executable name."
(let ((name (downcase (file-name-nondirectory program))))
(if (string-suffix-p ".exe" name)
(string-remove-suffix ".exe" name)
name)))
(defun majutsu--shell-command-needs-shell-p (command)
"Return non-nil when COMMAND should be handed to the shell.
This conservatively detects shell operators that cannot be preserved by
`split-string-shell-command'."
(string-match-p "[|&;<>`]" command))
(defun majutsu--shell-command-jj-args (command)
"Return jj arguments parsed from COMMAND, or nil.
Only plain `jj ...' commands are recognized. Commands using shell
operators are left for the shell runner."
(unless (majutsu--shell-command-needs-shell-p command)
(condition-case nil
(when-let* ((argv (split-string-shell-command command))
(program (car argv))
(jj (majutsu-jj--executable))
((string= (majutsu--command-program-name program)
(majutsu--command-program-name jj))))
(cdr argv))
(error nil))))
(defun majutsu--jj-command-args (command)
"Return jj arguments parsed from COMMAND.
COMMAND may start with the jj executable name, but it does not have to.
Signal a user error if COMMAND is empty or uses shell syntax."
(setq command (string-trim (or command "")))
(when (string-empty-p command)
(user-error "Need non-empty input"))
(when (majutsu--shell-command-needs-shell-p command)
(user-error "Shell syntax is not supported here; use `majutsu-shell-command'"))
(let* ((argv (condition-case nil
(split-string-shell-command command)
(error
(user-error "Failed to parse command: %s" command))))
(program (car argv))
(jj (majutsu-jj--executable)))
(cond
((null argv)
(user-error "Need non-empty input"))
((string= (majutsu--command-program-name program)
(majutsu--command-program-name jj))
(setq argv (cdr argv))
(when (and argv
(string= (majutsu--command-program-name (car argv))
(majutsu--command-program-name jj)))
(setq argv (cdr argv)))
(or argv
(user-error "Need jj subcommand")))
(t argv))))
(defun majutsu--start-jj-command (command)
"Start jj COMMAND asynchronously and return the process."
(apply #'majutsu-start-process
(majutsu-jj--executable)
nil
(majutsu-process-jj-arguments (majutsu--jj-command-args command))))
(defun majutsu--jj-completion-argv (command)
"Return argv to pass to jj's completion engine for COMMAND.
COMMAND is the minibuffer text before point. Preserve an empty trailing
argument when completion happens after whitespace."
(let* ((ends-in-space (or (string-empty-p command)
(string-match-p "[ ]\'" command)))
(argv (condition-case nil
(split-string-shell-command command)
(error nil)))
(jj (majutsu-jj--executable)))
(when (and argv
(string= (majutsu--command-program-name (car argv))
(majutsu--command-program-name jj)))
(setq argv (cdr argv)))
(when ends-in-space
(setq argv (append argv '(""))))
argv))
(defun majutsu--jj-completion-items (command)
"Return completion items for COMMAND using jj's native completer.
Each item is (CANDIDATE . HELP)."
(majutsu-jj-completion-items (majutsu--jj-completion-argv command)))
(defun majutsu--jj-command-completion-bounds ()
"Return minibuffer token bounds for jj command completion."
(let ((end (point)))
(save-excursion
(skip-chars-backward "^ \n" (minibuffer-prompt-end))
(cons (point) end))))
(defun majutsu-jj-command-complete ()
"Complete the jj command at point using jj's native completer."
(interactive)
(let* ((input (buffer-substring-no-properties (minibuffer-prompt-end) (point)))
(items (majutsu--jj-completion-items input)))
(if items
(pcase-let ((`(,beg . ,end) (majutsu--jj-command-completion-bounds)))
(completion-in-region beg end (majutsu-completion-table items)))
(minibuffer-message "No jj completions"))))
(defun majutsu--start-shell-command (command)
"Start COMMAND asynchronously and return the process.
Plain `jj ...' commands are started directly using Majutsu's jj process
helpers. Other commands are run through `shell-file-name'."
(if-let* ((jj-args (majutsu--shell-command-jj-args command)))
(apply #'majutsu-start-process
(majutsu-jj--executable)
nil
(majutsu-process-jj-arguments jj-args))
(majutsu-start-process shell-file-name nil shell-command-switch command)))
(defun majutsu-read-jj-command (&optional topdir)
"Read a jj command for Majutsu runners.
When TOPDIR or `current-prefix-arg' is non-nil, prompt relative to the
workspace root. The leading `jj' executable name is optional."
(let ((default-directory (majutsu--shell-command-directory topdir)))
(read-from-minibuffer (if majutsu-shell-command-verbose-prompt
(format "Async jj command in %s: "
(abbreviate-file-name default-directory))
"Async jj command: ")
nil
majutsu-read-jj-command-map
nil
'majutsu-jj-command-history)))
(defun majutsu-read-shell-command (&optional topdir initial-input)
"Read a shell command for Majutsu runners.
When TOPDIR or `current-prefix-arg' is non-nil, prompt relative to the
workspace root. INITIAL-INPUT is inserted into the minibuffer."
(let ((default-directory (majutsu--shell-command-directory topdir)))
(read-shell-command (if majutsu-shell-command-verbose-prompt
(format "Async shell command in %s: "
(abbreviate-file-name default-directory))
"Async shell command: ")
initial-input
'majutsu-shell-command-history)))
(defun majutsu--shell-command (command &optional directory)
"Execute COMMAND asynchronously in DIRECTORY and display output."
(setq command (string-trim (or command "")))
(when (string-empty-p command)
(user-error "Need non-empty input"))
(let ((default-directory (or directory default-directory))
process)
(with-connection-local-variables
(majutsu-with-editor
(setq process (majutsu--start-shell-command command))))
(majutsu-process-buffer)
process))
(defun majutsu--run-jj-command (command &optional directory)
"Execute jj COMMAND asynchronously in DIRECTORY and display output."
(let ((default-directory (or directory default-directory))
process)
(with-connection-local-variables
(majutsu-with-editor
(setq process (majutsu--start-jj-command command))))
(majutsu-process-buffer)
process))
;;;###autoload
(defun majutsu-jj-command (command)
"Execute COMMAND asynchronously; display output.
Interactively, prompt for a jj subcommand in the minibuffer. A leading
`jj' executable name is accepted but not required.
With a prefix argument COMMAND is run in the workspace root, otherwise
in `default-directory'."
(interactive (list (majutsu-read-jj-command nil)))
(majutsu--run-jj-command command (majutsu--shell-command-directory)))
;;;###autoload
(defun majutsu-jj-command-topdir (command)
"Execute COMMAND asynchronously in the workspace root; display output.
Interactively, prompt for a jj subcommand in the minibuffer. A leading
`jj' executable name is accepted but not required."
(interactive (list (majutsu-read-jj-command t)))
(majutsu--run-jj-command command (majutsu--shell-command-directory t)))
;;;###autoload
(defun majutsu-shell-command (command)
"Execute COMMAND asynchronously; display output.
Interactively, prompt for COMMAND in the minibuffer. With a prefix
argument COMMAND is run in the workspace root, otherwise in
`default-directory'."
(interactive (list (majutsu-read-shell-command)))
(majutsu--shell-command command (majutsu--shell-command-directory)))
;;;###autoload
(defun majutsu-shell-command-topdir (command)
"Execute COMMAND asynchronously in the workspace root; display output.
Interactively, prompt for COMMAND in the minibuffer."
(interactive (list (majutsu-read-shell-command t)))
(majutsu--shell-command command (majutsu--shell-command-directory t)))
;;;###autoload(autoload 'majutsu-command "majutsu-command" nil t)
(transient-define-prefix majutsu-command ()
"Run jj or another command."
[["Run jj subcommand"
("!" "in workspace root" majutsu-jj-command-topdir)
("p" "in working directory" majutsu-jj-command)]
["Run shell command"
("s" "in workspace root" majutsu-shell-command-topdir)
("S" "in working directory" majutsu-shell-command)]])
(provide 'majutsu-command)
;;; majutsu-command.el ends here