-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmajutsu-bookmark.el
More file actions
403 lines (357 loc) · 16.2 KB
/
majutsu-bookmark.el
File metadata and controls
403 lines (357 loc) · 16.2 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
;;; majutsu-bookmark.el --- Bookmark commands for Majutsu -*- lexical-binding: t; -*-
;; Copyright (C) 2025-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 implements jj bookmark commands and integrates them
;; with Majutsu's transient UI.
;;; Code:
(require 'majutsu)
(require 'json)
(require 'seq)
(require 'subr-x)
;;; majutsu-bookmark
(defun majutsu--extract-bookmark-names (text)
"Extract bookmark names from jj command output TEXT."
(let ((names '())
(start 0))
(while (string-match "bookmark[: ]+\\([^ \n,]+\\)" text start)
(push (match-string 1 text) names)
(setq start (match-end 0)))
(nreverse names)))
(defun majutsu--bookmark-split-remote-ref (ref)
"Split remote bookmark REF like NAME@REMOTE into (NAME . REMOTE).
Splits at the last \"@\"."
(let ((ref (string-trim (substring-no-properties ref))))
(if (string-match "\\`\\(.*\\)@\\([^@]+\\)\\'" ref)
(cons (match-string 1 ref) (match-string 2 ref))
(cons ref nil))))
(defun majutsu--bookmark--remote-args (remotes)
"Build repeated `--remote <REMOTE>` args from REMOTES."
(apply #'append
(mapcar (lambda (remote) (list "--remote" remote))
remotes)))
(defun majutsu--bookmark-remote-name-candidates ()
"Return remote bookmark names for completion (unique, plain strings)."
(let* ((template "if(remote && present, json(name) ++ \"\\n\", \"\")")
(args '("bookmark" "list" "--quiet" "--all-remotes" "-T"))
(lines (majutsu-jj-lines args template))
(names (delq nil
(mapcar (lambda (line)
(condition-case nil
(json-parse-string line)
(error nil)))
lines))))
(delete-dups (seq-filter #'stringp names))))
(defun majutsu--bookmark-git-remote-candidates ()
"Return Git remote names for completion."
(let* ((lines (majutsu-jj-lines "git" "remote" "list"))
(names (delq nil
(mapcar (lambda (line)
(unless (string-match-p "\\`\\(Error\\|error\\|fatal\\):" line)
(when (string-match "\\`\\([^ \t]+\\)" line)
(match-string 1 line))))
lines))))
(delete-dups names)))
(defun majutsu--get-bookmark-names (&optional scope)
"Return bookmark names for completion.
SCOPE controls what to return:
- nil or `local': local bookmark names (e.g. \"main\")
- t or `remote': remote bookmark refs (e.g. \"main@origin\")
- `remote-tracked': tracked remote bookmark refs only
- `remote-untracked': untracked remote bookmark refs only"
(let* ((scope (pcase scope
((or 'nil 'local) 'local)
('remote 'remote)
('remote-tracked 'remote-tracked)
('remote-untracked 'remote-untracked)
(_ (user-error "Unknown bookmark name scope: %S" scope))))
(template (pcase scope
('local
"if(!remote && present, name ++ \"\\n\", \"\")")
('remote
"if(remote && present, name ++ \"@\" ++ remote ++ \"\\n\", \"\")")
('remote-tracked
"if(remote && present && tracked, name ++ \"@\" ++ remote ++ \"\\n\", \"\")")
('remote-untracked
"if(remote && present && !tracked, name ++ \"@\" ++ remote ++ \"\\n\", \"\")")))
(args (append '("bookmark" "list" "--quiet")
(pcase scope
((or 'remote 'remote-untracked) '("--all-remotes"))
('remote-tracked '("--tracked"))
(_ nil))
(list "-T" template)))
(names (majutsu-jj-lines args)))
(delete-dups names)))
;;;###autoload
(defun majutsu-bookmark-create ()
"Create a new bookmark."
(interactive)
(let* ((revset (or (magit-section-value-if 'jj-commit) "@"))
(name (read-string "Bookmark name: ")))
(unless (string-empty-p name)
(majutsu-run-jj "bookmark" "create" name "-r" revset))))
;;;###autoload
(defun majutsu-bookmark-delete ()
"Delete a bookmark and propagate on next push."
(interactive)
(let* ((bookmarks (majutsu--get-bookmark-names))
(choice (and bookmarks (majutsu-completing-read
"Delete bookmark (propagates on push)" bookmarks
nil t nil nil nil 'majutsu-bookmark))))
(if (not choice)
(message "No bookmarks found")
(when (zerop (majutsu-run-jj "bookmark" "delete" choice))
(message "Deleted bookmark '%s'" choice)))))
;;;###autoload
(defun majutsu-bookmark-forget ()
"Forget a bookmark (local only, no deletion propagation)."
(interactive)
(let* ((bookmarks (majutsu--get-bookmark-names))
(choice (and bookmarks (majutsu-completing-read
"Forget bookmark" bookmarks
nil t nil nil nil 'majutsu-bookmark))))
(if (not choice)
(message "No bookmarks found")
(when (zerop (majutsu-run-jj "bookmark" "forget" choice))
(message "Forgot bookmark '%s'" choice)))))
;;;###autoload
(defun majutsu-bookmark-track ()
"Track remote bookmark(s)."
(interactive)
(let* ((bookmark-patterns
(majutsu-completing-read-multiple
"Track bookmark name(s)/pattern(s)"
(majutsu--bookmark-remote-name-candidates) nil nil))
(remote-patterns
(majutsu-completing-read-multiple
"Remote(s)/pattern(s) (empty = all)"
(majutsu--bookmark-git-remote-candidates) nil nil))
(bookmark-patterns (seq-filter (lambda (s) (not (string-empty-p s)))
bookmark-patterns))
(remote-patterns (seq-filter (lambda (s) (not (string-empty-p s)))
remote-patterns)))
(if (null bookmark-patterns)
(message "No bookmark name/pattern provided")
(when (zerop (apply #'majutsu-run-jj
(append (list "bookmark" "track")
bookmark-patterns
(majutsu--bookmark--remote-args remote-patterns))))
(message "Tracking remote bookmark(s): %s%s"
(string-join bookmark-patterns ", ")
(if remote-patterns
(format " (remote(s): %s)" (string-join remote-patterns ", "))
""))))))
(defvar-local majutsu-bookmark--list-all nil
"Non-nil when the bookmark list includes remote bookmarks.")
;;;###autoload
(defun majutsu-bookmark-list (&optional all)
"List bookmarks in a dedicated buffer.
With prefix ALL, include remote bookmarks."
(interactive "P")
(majutsu-setup-buffer #'majutsu-bookmark-list-mode nil
:buffer "*Majutsu Bookmarks*"
(majutsu-bookmark--list-all (and all t))))
(defun majutsu-bookmark--list-args ()
"Return arguments for `jj bookmark list'."
(append '("bookmark" "list" "--quiet")
(and majutsu-bookmark--list-all '("--all-remotes"))))
(defun majutsu-bookmark--line-name (line)
"Return the bookmark name parsed from LINE."
(let* ((raw (string-trim (substring-no-properties line)))
(token (car (split-string raw "[ \t]+" t))))
(when token
(string-remove-suffix ":" token))))
(defun majutsu-bookmark--wash-list (_args)
"Wash `jj bookmark list' output into bookmark sections."
(let ((count 0))
(magit-wash-sequence
(lambda ()
(let* ((line (buffer-substring (line-beginning-position)
(line-end-position)))
(trimmed (string-trim (substring-no-properties line)))
(name (and (not (string-empty-p trimmed))
(majutsu-bookmark--line-name line))))
(delete-region (line-beginning-position)
(min (point-max) (1+ (line-end-position))))
(when name
(setq count (1+ count))
(magit-insert-section (jj-bookmark name t)
(magit-insert-heading line)))
t)))
(if (zerop count)
(magit-cancel-section)
(insert "\n"))))
(defun majutsu-bookmark-list-refresh-buffer ()
"Refresh the bookmark list buffer."
(majutsu--assert-mode 'majutsu-bookmark-list-mode)
(magit-insert-section (bookmark-list)
(majutsu-jj-wash #'majutsu-bookmark--wash-list nil
(majutsu-bookmark--list-args))))
(defvar-keymap majutsu-bookmark-list-mode-map
:doc "Keymap for `majutsu-bookmark-list-mode'."
:parent majutsu-mode-map)
(define-derived-mode majutsu-bookmark-list-mode majutsu-mode "Majutsu Bookmarks"
"Major mode for viewing jj bookmarks."
:group 'majutsu
(setq-local line-number-mode nil)
(setq-local revert-buffer-function #'majutsu-refresh-buffer))
;;;###autoload
(defun majutsu-read-bookmarks (prompt &optional _init-input _history)
"Return interactive arguments for bookmark move commands."
(let* ((bookmarks (majutsu--get-bookmark-names))
(default (majutsu-bookmark-at-point)))
(majutsu-completing-read-multiple
prompt bookmarks nil t nil nil default 'majutsu-bookmark)))
(defvar majutsu-bookmark-advance-pattern-history nil
"Minibuffer history for `majutsu-bookmark-advance-patterns'.")
(defun majutsu--bookmark-read-advance-patterns ()
"Read bookmark name patterns for `jj bookmark advance'."
(let ((default (majutsu-bookmark-at-point)))
(seq-filter (lambda (s) (not (string-empty-p s)))
(majutsu-completing-read-multiple
"Advance bookmark name(s)/pattern(s)"
(majutsu--get-bookmark-names)
nil nil nil 'majutsu-bookmark-advance-pattern-history
default 'majutsu-bookmark))))
;;;###autoload
(defun majutsu-bookmark-advance (&optional arg1 arg2)
"Advance bookmarks using jj's configured default selection.
If ARG1 is a string, use it as the target revset. For backward
compatibility, if ARG2 is non-nil, use ARG2 as the target revset and
ignore ARG1. Use `majutsu-bookmark-advance-patterns' for explicit
bookmark-name/pattern selection."
(interactive)
(let ((commit (or arg2 (and (stringp arg1) arg1))))
(apply #'majutsu-run-jj
(append '("bookmark" "advance")
(and commit (list "-t" commit))))))
;;;###autoload
(defun majutsu-bookmark-advance-to (commit)
"Advance bookmarks using jj's default selection to COMMIT."
(interactive (list (majutsu-read-revset "Advance to revset")))
(majutsu-bookmark-advance commit))
;;;###autoload
(defun majutsu-bookmark-advance-patterns (names)
"Advance bookmark name patterns NAMES using jj's default target."
(interactive (list (majutsu--bookmark-read-advance-patterns)))
(if names
(majutsu-run-jj "bookmark" "advance" names)
(message "No bookmark name/pattern provided")))
(defun majutsu--bookmark-move (names commit &optional allow-backwards)
"Internal helper to move bookmark(s) NAMES to COMMIT.
When ALLOW-BACKWARDS is non-nil, include `--allow-backwards'."
(when names
(let ((args (append '("bookmark" "move")
(and allow-backwards '("--allow-backwards"))
(list "-t" commit)
names)))
(when (zerop (apply #'majutsu-run-jj args))
(message (if allow-backwards
"Moved bookmark(s) (allow backwards) to %s: %s"
"Moved bookmark(s) to %s: %s")
commit (string-join names ", "))))))
;;;###autoload
(defun majutsu-bookmark-move (names commit &optional allow-backwards)
"Move bookmark bookmark(s) NAMES to COMMIT.
With optional ALLOW-BACKWARDS, pass `--allow-backwards' to jj."
(interactive (list (majutsu-read-bookmarks "Move bookmark(s)") (majutsu-read-revset "Target revset")))
(majutsu--bookmark-move names commit allow-backwards))
;;;###autoload
(defun majutsu-bookmark-move-allow-backwards (names commit)
"Move bookmark(s) NAMES to COMMIT allowing backwards moves."
(interactive (list (majutsu-read-bookmarks "Move bookmark(s)") (majutsu-read-revset "Target revset")))
(majutsu--bookmark-move names commit t))
;;;###autoload
(defun majutsu-bookmark-rename (old new)
"Rename bookmark OLD to NEW."
(interactive
(let* ((bookmarks (majutsu--get-bookmark-names))
(old (and bookmarks (majutsu-completing-read
"Rename bookmark" bookmarks
nil t nil nil nil 'majutsu-bookmark)))
(new (majutsu-read-string (format "New name for %s" old))))
(list old new)))
(when (and (not (string-empty-p old)) (not (string-empty-p new)))
(when (zerop (majutsu-run-jj "bookmark" "rename" old new))
(message "Renamed bookmark '%s' -> '%s'" old new))))
;;;###autoload
(defun majutsu-bookmark-set (name commit)
"Create or update bookmark NAME to point to COMMIT."
(interactive
(let* ((bookmarks (majutsu--get-bookmark-names))
(name (majutsu-completing-read "Set bookmark" bookmarks
nil nil nil nil nil 'majutsu-bookmark))
(at (or (magit-section-value-if 'jj-commit) "@"))
(rev (majutsu-read-string "Target revision" nil nil at)))
(list name rev)))
(when (zerop (majutsu-run-jj "bookmark" "set" name "-r" commit))
(message "Set bookmark '%s' to %s" name commit)))
;;;###autoload
(defun majutsu-bookmark-untrack (bookmarks &optional remotes)
"Stop tracking remote bookmark(s).
BOOKMARKS are bookmark name patterns (glob/exact/regex/substring).
REMOTES are remote name patterns passed via repeated `--remote`."
(interactive
(list (majutsu-completing-read-multiple
"Untrack bookmark name(s)/pattern(s)"
(majutsu--bookmark-remote-name-candidates))
(majutsu-completing-read-multiple
"Remote(s)/pattern(s) (empty = all)"
(majutsu--bookmark-git-remote-candidates))))
(defvar crm-separator)
(let* ((bookmarks (seq-filter (lambda (s) (not (string-empty-p s))) bookmarks))
(remotes (seq-filter (lambda (s) (not (string-empty-p s))) (or remotes '()))))
(when bookmarks
(when (zerop (apply #'majutsu-run-jj
(append (list "bookmark" "untrack")
bookmarks
(majutsu--bookmark--remote-args remotes))))
(message "Untracked: %s%s"
(string-join bookmarks ", ")
(if remotes
(format " (remote(s): %s)" (string-join remotes ", "))
""))))))
;;; Bookmark Transient
;;;###autoload(autoload 'majutsu-bookmark "majutsu-bookmark" nil t)
(transient-define-prefix majutsu-bookmark ()
"Internal transient for jj bookmark operations."
:transient-non-suffix t
["Bookmark Operations"
[
("l" "List bookmarks" majutsu-bookmark-list
:description "Show bookmark list")
("c" "Create bookmark" majutsu-bookmark-create
:description "Create new bookmark")]
[
("a" "Advance bookmark(s)" majutsu-bookmark-advance
:description "Advance default selection")
("A" "Advance bookmark(s) to revset" majutsu-bookmark-advance-to
:description "Advance default selection to revset")
("p" "Advance name(s)/pattern(s)" majutsu-bookmark-advance-patterns
:description "Advance name/pattern selection")
("s" "Set bookmark" majutsu-bookmark-set
:description "Create/update to commit")
("m" "Move bookmark(s)" majutsu-bookmark-move
:description "Move bookmark to commit")
("M" "Move bookmark(s) --allow-backwards" majutsu-bookmark-move-allow-backwards
:description "Move allowing backwards")
("r" "Rename bookmark" majutsu-bookmark-rename
:description "Rename bookmark")]
[
("t" "Track remote" majutsu-bookmark-track
:description "Track remote bookmark")
("u" "Untrack remote" majutsu-bookmark-untrack
:description "Stop tracking remote")]
[
("d" "Delete bookmark" majutsu-bookmark-delete
:description "Delete (propagate)")
("f" "Forget bookmark" majutsu-bookmark-forget
:description "Forget (local)")]
[("q" "Quit" transient-quit-one)]])
;;; _
(provide 'majutsu-bookmark)
;;; majutsu-bookmark.el ends here